* config/tc-mips.c (md_begin): Fix .reginfo and .MIPS.option section
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too.  */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef obj_frob_file
58 #undef obj_frob_file_after_relocs
59 #undef obj_frob_symbol
60 #undef obj_pop_insert
61 #undef obj_sec_sym_ok_for_reloc
62 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64 #include "obj-elf.h"
65 /* Fix any of them that we actually care about.  */
66 #undef OUTPUT_FLAVOR
67 #define OUTPUT_FLAVOR mips_output_flavor()
68 #endif
69
70 #if defined (OBJ_ELF)
71 #include "elf/mips.h"
72 #endif
73
74 #ifndef ECOFF_DEBUGGING
75 #define NO_ECOFF_DEBUGGING
76 #define ECOFF_DEBUGGING 0
77 #endif
78
79 #include "ecoff.h"
80
81 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82 static char *mips_regmask_frag;
83 #endif
84
85 #define AT  1
86 #define TREG 24
87 #define PIC_CALL_REG 25
88 #define KT0 26
89 #define KT1 27
90 #define GP  28
91 #define SP  29
92 #define FP  30
93 #define RA  31
94
95 #define ILLEGAL_REG (32)
96
97 /* Allow override of standard little-endian ECOFF format.  */
98
99 #ifndef ECOFF_LITTLE_FORMAT
100 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101 #endif
102
103 extern int target_big_endian;
104
105 /* The name of the readonly data section.  */
106 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107                             ? ".data" \
108                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109                             ? ".rdata" \
110                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111                             ? ".rdata" \
112                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113                             ? ".rodata" \
114                             : (abort (), ""))
115
116 /* The ABI to use.  */
117 enum mips_abi_level
118 {
119   NO_ABI = 0,
120   O32_ABI,
121   O64_ABI,
122   N32_ABI,
123   N64_ABI,
124   EABI_ABI
125 };
126
127 /* MIPS ABI we are using for this output file.  */
128 static enum mips_abi_level file_mips_abi = NO_ABI;
129
130 /* This is the set of options which may be modified by the .set
131    pseudo-op.  We use a struct so that .set push and .set pop are more
132    reliable.  */
133
134 struct mips_set_options
135 {
136   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
137      if it has not been initialized.  Changed by `.set mipsN', and the
138      -mipsN command line option, and the default CPU.  */
139   int isa;
140   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
141      if they have not been initialized.  Changed by `.set <asename>', by
142      command line options, and based on the default architecture.  */
143   int ase_mips3d;
144   /* Whether we are assembling for the mips16 processor.  0 if we are
145      not, 1 if we are, and -1 if the value has not been initialized.
146      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
147      -nomips16 command line options, and the default CPU.  */
148   int mips16;
149   /* Non-zero if we should not reorder instructions.  Changed by `.set
150      reorder' and `.set noreorder'.  */
151   int noreorder;
152   /* Non-zero if we should not permit the $at ($1) register to be used
153      in instructions.  Changed by `.set at' and `.set noat'.  */
154   int noat;
155   /* Non-zero if we should warn when a macro instruction expands into
156      more than one machine instruction.  Changed by `.set nomacro' and
157      `.set macro'.  */
158   int warn_about_macros;
159   /* Non-zero if we should not move instructions.  Changed by `.set
160      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
161   int nomove;
162   /* Non-zero if we should not optimize branches by moving the target
163      of the branch into the delay slot.  Actually, we don't perform
164      this optimization anyhow.  Changed by `.set bopt' and `.set
165      nobopt'.  */
166   int nobopt;
167   /* Non-zero if we should not autoextend mips16 instructions.
168      Changed by `.set autoextend' and `.set noautoextend'.  */
169   int noautoextend;
170   /* Restrict general purpose registers and floating point registers
171      to 32 bit.  This is initially determined when -mgp32 or -mfp32
172      is passed but can changed if the assembler code uses .set mipsN.  */
173   int gp32;
174   int fp32;
175   /* The ABI currently in use. This is changed by .set mipsN to loosen
176      restrictions and doesn't affect the whole file.  */
177   enum mips_abi_level abi;
178 };
179
180 /* True if -mgp32 was passed.  */
181 static int file_mips_gp32 = -1;
182
183 /* True if -mfp32 was passed.  */
184 static int file_mips_fp32 = -1;
185
186 /* This is the struct we use to hold the current set of options.  Note
187    that we must set the isa field to ISA_UNKNOWN and the mips16 field to
188    -1 to indicate that they have not been initialized.  */
189
190 static struct mips_set_options mips_opts =
191 {
192   ISA_UNKNOWN, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
193 };
194
195 /* These variables are filled in with the masks of registers used.
196    The object format code reads them and puts them in the appropriate
197    place.  */
198 unsigned long mips_gprmask;
199 unsigned long mips_cprmask[4];
200
201 /* MIPS ISA we are using for this output file.  */
202 static int file_mips_isa = ISA_UNKNOWN;
203
204 /* True if -mips3d was passed or implied by arguments passed on the
205    command line (e.g., by -march).  */
206 static int file_ase_mips3d;
207
208 /* The argument of the -mcpu= flag.  Historical for code generation.  */
209 static int mips_cpu = CPU_UNKNOWN;
210
211 /* The argument of the -march= flag.  The architecture we are assembling.  */
212 static int mips_arch = CPU_UNKNOWN;
213
214 /* The argument of the -mtune= flag.  The architecture for which we
215    are optimizing.  */
216 static int mips_tune = CPU_UNKNOWN;
217
218 /* Whether we should mark the file EABI64 or EABI32.  */
219 static int mips_eabi64 = 0;
220
221 /* If they asked for mips1 or mips2 and a cpu that is
222    mips3 or greater, then mark the object file 32BITMODE.  */
223 static int mips_32bitmode = 0;
224
225 /* Some ISA's have delay slots for instructions which read or write
226    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
227    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
228    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
229    delay slot in this ISA.  The uses of this macro assume that any
230    ISA that has delay slots for one of these, has them for all.  They
231    also assume that ISAs which don't have delays for these insns, don't
232    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
233 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
234    (ISA) == ISA_MIPS1                       \
235    || (ISA) == ISA_MIPS2                    \
236    || (ISA) == ISA_MIPS3                    \
237    )
238
239 /*  Return true if ISA supports 64 bit gp register instructions.  */
240 #define ISA_HAS_64BIT_REGS(ISA) (    \
241    (ISA) == ISA_MIPS3                \
242    || (ISA) == ISA_MIPS4             \
243    || (ISA) == ISA_MIPS5             \
244    || (ISA) == ISA_MIPS64            \
245    )
246
247 #define HAVE_32BIT_GPRS                            \
248     (mips_opts.gp32                                \
249      || mips_opts.abi == O32_ABI                   \
250      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
251
252 #define HAVE_32BIT_FPRS                            \
253     (mips_opts.fp32                                \
254      || mips_opts.abi == O32_ABI                   \
255      || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
256
257 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
258 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
259
260 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
261
262 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
263
264 /* We can only have 64bit addresses if the object file format
265    supports it.  */
266 #define HAVE_32BIT_ADDRESSES                           \
267    (HAVE_32BIT_GPRS                                    \
268     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
269          || ! HAVE_64BIT_OBJECTS)                      \
270         && mips_pic != EMBEDDED_PIC))
271
272 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
273
274 /* Return true if the given CPU supports the MIPS3D ASE.  */
275 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
276                                  )
277
278 /* Whether the processor uses hardware interlocks to protect
279    reads from the HI and LO registers, and thus does not
280    require nops to be inserted.  */
281
282 #define hilo_interlocks (mips_arch == CPU_R4010                       \
283                          || mips_arch == CPU_SB1                      \
284                          )
285
286 /* Whether the processor uses hardware interlocks to protect reads
287    from the GPRs, and thus does not require nops to be inserted.  */
288 #define gpr_interlocks \
289   (mips_opts.isa != ISA_MIPS1  \
290    || mips_arch == CPU_R3900)
291
292 /* As with other "interlocks" this is used by hardware that has FP
293    (co-processor) interlocks.  */
294 /* Itbl support may require additional care here.  */
295 #define cop_interlocks (mips_arch == CPU_R4300                        \
296                         || mips_arch == CPU_SB1                       \
297                         )
298
299 /* Is this a mfhi or mflo instruction?  */
300 #define MF_HILO_INSN(PINFO) \
301           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
302
303 /* MIPS PIC level.  */
304
305 enum mips_pic_level
306 {
307   /* Do not generate PIC code.  */
308   NO_PIC,
309
310   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
311      not sure what it is supposed to do.  */
312   IRIX4_PIC,
313
314   /* Generate PIC code as in the SVR4 MIPS ABI.  */
315   SVR4_PIC,
316
317   /* Generate PIC code without using a global offset table: the data
318      segment has a maximum size of 64K, all data references are off
319      the $gp register, and all text references are PC relative.  This
320      is used on some embedded systems.  */
321   EMBEDDED_PIC
322 };
323
324 static enum mips_pic_level mips_pic;
325
326 /* Warn about all NOPS that the assembler generates.  */
327 static int warn_nops = 0;
328
329 /* 1 if we should generate 32 bit offsets from the GP register in
330    SVR4_PIC mode.  Currently has no meaning in other modes.  */
331 static int mips_big_got;
332
333 /* 1 if trap instructions should used for overflow rather than break
334    instructions.  */
335 static int mips_trap;
336
337 /* 1 if double width floating point constants should not be constructed
338    by assembling two single width halves into two single width floating
339    point registers which just happen to alias the double width destination
340    register.  On some architectures this aliasing can be disabled by a bit
341    in the status register, and the setting of this bit cannot be determined
342    automatically at assemble time.  */
343 static int mips_disable_float_construction;
344
345 /* Non-zero if any .set noreorder directives were used.  */
346
347 static int mips_any_noreorder;
348
349 /* Non-zero if nops should be inserted when the register referenced in
350    an mfhi/mflo instruction is read in the next two instructions.  */
351 static int mips_7000_hilo_fix;
352
353 /* The size of the small data section.  */
354 static unsigned int g_switch_value = 8;
355 /* Whether the -G option was used.  */
356 static int g_switch_seen = 0;
357
358 #define N_RMASK 0xc4
359 #define N_VFP   0xd4
360
361 /* If we can determine in advance that GP optimization won't be
362    possible, we can skip the relaxation stuff that tries to produce
363    GP-relative references.  This makes delay slot optimization work
364    better.
365
366    This function can only provide a guess, but it seems to work for
367    gcc output.  It needs to guess right for gcc, otherwise gcc
368    will put what it thinks is a GP-relative instruction in a branch
369    delay slot.
370
371    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
372    fixed it for the non-PIC mode.  KR 95/04/07  */
373 static int nopic_need_relax PARAMS ((symbolS *, int));
374
375 /* handle of the OPCODE hash table */
376 static struct hash_control *op_hash = NULL;
377
378 /* The opcode hash table we use for the mips16.  */
379 static struct hash_control *mips16_op_hash = NULL;
380
381 /* This array holds the chars that always start a comment.  If the
382     pre-processor is disabled, these aren't very useful */
383 const char comment_chars[] = "#";
384
385 /* This array holds the chars that only start a comment at the beginning of
386    a line.  If the line seems to have the form '# 123 filename'
387    .line and .file directives will appear in the pre-processed output */
388 /* Note that input_file.c hand checks for '#' at the beginning of the
389    first line of the input file.  This is because the compiler outputs
390    #NO_APP at the beginning of its output.  */
391 /* Also note that C style comments are always supported.  */
392 const char line_comment_chars[] = "#";
393
394 /* This array holds machine specific line separator characters.  */
395 const char line_separator_chars[] = ";";
396
397 /* Chars that can be used to separate mant from exp in floating point nums */
398 const char EXP_CHARS[] = "eE";
399
400 /* Chars that mean this number is a floating point constant */
401 /* As in 0f12.456 */
402 /* or    0d1.2345e12 */
403 const char FLT_CHARS[] = "rRsSfFdDxXpP";
404
405 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
406    changed in read.c .  Ideally it shouldn't have to know about it at all,
407    but nothing is ideal around here.
408  */
409
410 static char *insn_error;
411
412 static int auto_align = 1;
413
414 /* When outputting SVR4 PIC code, the assembler needs to know the
415    offset in the stack frame from which to restore the $gp register.
416    This is set by the .cprestore pseudo-op, and saved in this
417    variable.  */
418 static offsetT mips_cprestore_offset = -1;
419
420 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
421    more optimizations, it can use a register value instead of a memory-saved
422    offset and even an other register than $gp as global pointer.  */
423 static offsetT mips_cpreturn_offset = -1;
424 static int mips_cpreturn_register = -1;
425 static int mips_gp_register = GP;
426
427 /* Whether mips_cprestore_offset has been set in the current function
428    (or whether it has already been warned about, if not).  */
429 static int mips_cprestore_valid = 0;
430
431 /* This is the register which holds the stack frame, as set by the
432    .frame pseudo-op.  This is needed to implement .cprestore.  */
433 static int mips_frame_reg = SP;
434
435 /* Whether mips_frame_reg has been set in the current function
436    (or whether it has already been warned about, if not).  */
437 static int mips_frame_reg_valid = 0;
438
439 /* To output NOP instructions correctly, we need to keep information
440    about the previous two instructions.  */
441
442 /* Whether we are optimizing.  The default value of 2 means to remove
443    unneeded NOPs and swap branch instructions when possible.  A value
444    of 1 means to not swap branches.  A value of 0 means to always
445    insert NOPs.  */
446 static int mips_optimize = 2;
447
448 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
449    equivalent to seeing no -g option at all.  */
450 static int mips_debug = 0;
451
452 /* The previous instruction.  */
453 static struct mips_cl_insn prev_insn;
454
455 /* The instruction before prev_insn.  */
456 static struct mips_cl_insn prev_prev_insn;
457
458 /* If we don't want information for prev_insn or prev_prev_insn, we
459    point the insn_mo field at this dummy integer.  */
460 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
461
462 /* Non-zero if prev_insn is valid.  */
463 static int prev_insn_valid;
464
465 /* The frag for the previous instruction.  */
466 static struct frag *prev_insn_frag;
467
468 /* The offset into prev_insn_frag for the previous instruction.  */
469 static long prev_insn_where;
470
471 /* The reloc type for the previous instruction, if any.  */
472 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
473
474 /* The reloc for the previous instruction, if any.  */
475 static fixS *prev_insn_fixp[3];
476
477 /* Non-zero if the previous instruction was in a delay slot.  */
478 static int prev_insn_is_delay_slot;
479
480 /* Non-zero if the previous instruction was in a .set noreorder.  */
481 static int prev_insn_unreordered;
482
483 /* Non-zero if the previous instruction uses an extend opcode (if
484    mips16).  */
485 static int prev_insn_extended;
486
487 /* Non-zero if the previous previous instruction was in a .set
488    noreorder.  */
489 static int prev_prev_insn_unreordered;
490
491 /* If this is set, it points to a frag holding nop instructions which
492    were inserted before the start of a noreorder section.  If those
493    nops turn out to be unnecessary, the size of the frag can be
494    decreased.  */
495 static fragS *prev_nop_frag;
496
497 /* The number of nop instructions we created in prev_nop_frag.  */
498 static int prev_nop_frag_holds;
499
500 /* The number of nop instructions that we know we need in
501    prev_nop_frag.  */
502 static int prev_nop_frag_required;
503
504 /* The number of instructions we've seen since prev_nop_frag.  */
505 static int prev_nop_frag_since;
506
507 /* For ECOFF and ELF, relocations against symbols are done in two
508    parts, with a HI relocation and a LO relocation.  Each relocation
509    has only 16 bits of space to store an addend.  This means that in
510    order for the linker to handle carries correctly, it must be able
511    to locate both the HI and the LO relocation.  This means that the
512    relocations must appear in order in the relocation table.
513
514    In order to implement this, we keep track of each unmatched HI
515    relocation.  We then sort them so that they immediately precede the
516    corresponding LO relocation.  */
517
518 struct mips_hi_fixup
519 {
520   /* Next HI fixup.  */
521   struct mips_hi_fixup *next;
522   /* This fixup.  */
523   fixS *fixp;
524   /* The section this fixup is in.  */
525   segT seg;
526 };
527
528 /* The list of unmatched HI relocs.  */
529
530 static struct mips_hi_fixup *mips_hi_fixup_list;
531
532 /* Map normal MIPS register numbers to mips16 register numbers.  */
533
534 #define X ILLEGAL_REG
535 static const int mips32_to_16_reg_map[] =
536 {
537   X, X, 2, 3, 4, 5, 6, 7,
538   X, X, X, X, X, X, X, X,
539   0, 1, X, X, X, X, X, X,
540   X, X, X, X, X, X, X, X
541 };
542 #undef X
543
544 /* Map mips16 register numbers to normal MIPS register numbers.  */
545
546 static const unsigned int mips16_to_32_reg_map[] =
547 {
548   16, 17, 2, 3, 4, 5, 6, 7
549 };
550 \f
551 /* Since the MIPS does not have multiple forms of PC relative
552    instructions, we do not have to do relaxing as is done on other
553    platforms.  However, we do have to handle GP relative addressing
554    correctly, which turns out to be a similar problem.
555
556    Every macro that refers to a symbol can occur in (at least) two
557    forms, one with GP relative addressing and one without.  For
558    example, loading a global variable into a register generally uses
559    a macro instruction like this:
560      lw $4,i
561    If i can be addressed off the GP register (this is true if it is in
562    the .sbss or .sdata section, or if it is known to be smaller than
563    the -G argument) this will generate the following instruction:
564      lw $4,i($gp)
565    This instruction will use a GPREL reloc.  If i can not be addressed
566    off the GP register, the following instruction sequence will be used:
567      lui $at,i
568      lw $4,i($at)
569    In this case the first instruction will have a HI16 reloc, and the
570    second reloc will have a LO16 reloc.  Both relocs will be against
571    the symbol i.
572
573    The issue here is that we may not know whether i is GP addressable
574    until after we see the instruction that uses it.  Therefore, we
575    want to be able to choose the final instruction sequence only at
576    the end of the assembly.  This is similar to the way other
577    platforms choose the size of a PC relative instruction only at the
578    end of assembly.
579
580    When generating position independent code we do not use GP
581    addressing in quite the same way, but the issue still arises as
582    external symbols and local symbols must be handled differently.
583
584    We handle these issues by actually generating both possible
585    instruction sequences.  The longer one is put in a frag_var with
586    type rs_machine_dependent.  We encode what to do with the frag in
587    the subtype field.  We encode (1) the number of existing bytes to
588    replace, (2) the number of new bytes to use, (3) the offset from
589    the start of the existing bytes to the first reloc we must generate
590    (that is, the offset is applied from the start of the existing
591    bytes after they are replaced by the new bytes, if any), (4) the
592    offset from the start of the existing bytes to the second reloc,
593    (5) whether a third reloc is needed (the third reloc is always four
594    bytes after the second reloc), and (6) whether to warn if this
595    variant is used (this is sometimes needed if .set nomacro or .set
596    noat is in effect).  All these numbers are reasonably small.
597
598    Generating two instruction sequences must be handled carefully to
599    ensure that delay slots are handled correctly.  Fortunately, there
600    are a limited number of cases.  When the second instruction
601    sequence is generated, append_insn is directed to maintain the
602    existing delay slot information, so it continues to apply to any
603    code after the second instruction sequence.  This means that the
604    second instruction sequence must not impose any requirements not
605    required by the first instruction sequence.
606
607    These variant frags are then handled in functions called by the
608    machine independent code.  md_estimate_size_before_relax returns
609    the final size of the frag.  md_convert_frag sets up the final form
610    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
611    one if needed.  */
612 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
613   ((relax_substateT) \
614    (((old) << 23) \
615     | ((new) << 16) \
616     | (((reloc1) + 64) << 9) \
617     | (((reloc2) + 64) << 2) \
618     | ((reloc3) ? (1 << 1) : 0) \
619     | ((warn) ? 1 : 0)))
620 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
621 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
622 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
623 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
624 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
625 #define RELAX_WARN(i) ((i) & 1)
626
627 /* For mips16 code, we use an entirely different form of relaxation.
628    mips16 supports two versions of most instructions which take
629    immediate values: a small one which takes some small value, and a
630    larger one which takes a 16 bit value.  Since branches also follow
631    this pattern, relaxing these values is required.
632
633    We can assemble both mips16 and normal MIPS code in a single
634    object.  Therefore, we need to support this type of relaxation at
635    the same time that we support the relaxation described above.  We
636    use the high bit of the subtype field to distinguish these cases.
637
638    The information we store for this type of relaxation is the
639    argument code found in the opcode file for this relocation, whether
640    the user explicitly requested a small or extended form, and whether
641    the relocation is in a jump or jal delay slot.  That tells us the
642    size of the value, and how it should be stored.  We also store
643    whether the fragment is considered to be extended or not.  We also
644    store whether this is known to be a branch to a different section,
645    whether we have tried to relax this frag yet, and whether we have
646    ever extended a PC relative fragment because of a shift count.  */
647 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
648   (0x80000000                                                   \
649    | ((type) & 0xff)                                            \
650    | ((small) ? 0x100 : 0)                                      \
651    | ((ext) ? 0x200 : 0)                                        \
652    | ((dslot) ? 0x400 : 0)                                      \
653    | ((jal_dslot) ? 0x800 : 0))
654 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
655 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
656 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
657 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
658 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
659 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
660 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
661 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
662 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
663 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
664 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
665 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
666 \f
667 /* Prototypes for static functions.  */
668
669 #ifdef __STDC__
670 #define internalError() \
671     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
672 #else
673 #define internalError() as_fatal (_("MIPS internal Error"));
674 #endif
675
676 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
677
678 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
679                                   unsigned int reg, enum mips_regclass class));
680 static int reg_needs_delay PARAMS ((unsigned int));
681 static void mips16_mark_labels PARAMS ((void));
682 static void append_insn PARAMS ((char *place,
683                                  struct mips_cl_insn * ip,
684                                  expressionS * p,
685                                  bfd_reloc_code_real_type *r,
686                                  boolean));
687 static void mips_no_prev_insn PARAMS ((int));
688 static void mips_emit_delays PARAMS ((boolean));
689 #ifdef USE_STDARG
690 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
691                                  const char *name, const char *fmt,
692                                  ...));
693 #else
694 static void macro_build ();
695 #endif
696 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
697                                         const char *, const char *,
698                                         va_list));
699 static void macro_build_lui PARAMS ((char *place, int *counter,
700                                      expressionS * ep, int regnum));
701 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
702 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
703                                          expressionS *));
704 static void load_register PARAMS ((int *, int, expressionS *, int));
705 static void load_address PARAMS ((int *, int, expressionS *, int, int *));
706 static void move_register PARAMS ((int *, int, int));
707 static void macro PARAMS ((struct mips_cl_insn * ip));
708 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
709 #ifdef LOSING_COMPILER
710 static void macro2 PARAMS ((struct mips_cl_insn * ip));
711 #endif
712 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
713 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
714 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
715                                   boolean, boolean, unsigned long *,
716                                   boolean *, unsigned short *));
717 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
718 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
719 static int my_getSmallExpression PARAMS ((expressionS *, char *));
720 static void my_getExpression PARAMS ((expressionS *, char *));
721 #ifdef OBJ_ELF
722 static int support_64bit_objects PARAMS((void));
723 #endif
724 static symbolS *get_symbol PARAMS ((void));
725 static void mips_align PARAMS ((int to, int fill, symbolS *label));
726 static void s_align PARAMS ((int));
727 static void s_change_sec PARAMS ((int));
728 static void s_cons PARAMS ((int));
729 static void s_float_cons PARAMS ((int));
730 static void s_mips_globl PARAMS ((int));
731 static void s_option PARAMS ((int));
732 static void s_mipsset PARAMS ((int));
733 static void s_abicalls PARAMS ((int));
734 static void s_cpload PARAMS ((int));
735 static void s_cpsetup PARAMS ((int));
736 static void s_cplocal PARAMS ((int));
737 static void s_cprestore PARAMS ((int));
738 static void s_cpreturn PARAMS ((int));
739 static void s_gpvalue PARAMS ((int));
740 static void s_gpword PARAMS ((int));
741 static void s_cpadd PARAMS ((int));
742 static void s_insn PARAMS ((int));
743 static void md_obj_begin PARAMS ((void));
744 static void md_obj_end PARAMS ((void));
745 static long get_number PARAMS ((void));
746 static void s_mips_ent PARAMS ((int));
747 static void s_mips_end PARAMS ((int));
748 static void s_mips_frame PARAMS ((int));
749 static void s_mips_mask PARAMS ((int));
750 static void s_mips_stab PARAMS ((int));
751 static void s_mips_weakext PARAMS ((int));
752 static void s_file PARAMS ((int));
753 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
754 static const char *mips_isa_to_str PARAMS ((int));
755 static const char *mips_cpu_to_str PARAMS ((int));
756 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
757 static void show PARAMS ((FILE *, char *, int *, int *));
758 #ifdef OBJ_ELF
759 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
760 #endif
761
762 /* Return values of my_getSmallExpression().  */
763
764 enum small_ex_type
765 {
766   S_EX_NONE = 0,
767   S_EX_REGISTER,
768
769   /* Direct relocation creation by %percent_op().  */
770   S_EX_HALF,
771   S_EX_HI,
772   S_EX_LO,
773   S_EX_GP_REL,
774   S_EX_GOT,
775   S_EX_CALL16,
776   S_EX_GOT_DISP,
777   S_EX_GOT_PAGE,
778   S_EX_GOT_OFST,
779   S_EX_GOT_HI,
780   S_EX_GOT_LO,
781   S_EX_NEG,
782   S_EX_HIGHER,
783   S_EX_HIGHEST,
784   S_EX_CALL_HI,
785   S_EX_CALL_LO
786 };
787
788 /* Table and functions used to map between CPU/ISA names, and
789    ISA levels, and CPU numbers.  */
790
791 struct mips_cpu_info
792 {
793   const char *name;           /* CPU or ISA name.  */
794   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
795   int isa;                    /* ISA level.  */
796   int cpu;                    /* CPU number (default CPU if ISA).  */
797 };
798
799 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
800 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
801 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
802 \f
803 /* Pseudo-op table.
804
805    The following pseudo-ops from the Kane and Heinrich MIPS book
806    should be defined here, but are currently unsupported: .alias,
807    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
808
809    The following pseudo-ops from the Kane and Heinrich MIPS book are
810    specific to the type of debugging information being generated, and
811    should be defined by the object format: .aent, .begin, .bend,
812    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
813    .vreg.
814
815    The following pseudo-ops from the Kane and Heinrich MIPS book are
816    not MIPS CPU specific, but are also not specific to the object file
817    format.  This file is probably the best place to define them, but
818    they are not currently supported: .asm0, .endr, .lab, .repeat,
819    .struct.  */
820
821 static const pseudo_typeS mips_pseudo_table[] =
822 {
823   /* MIPS specific pseudo-ops.  */
824   {"option", s_option, 0},
825   {"set", s_mipsset, 0},
826   {"rdata", s_change_sec, 'r'},
827   {"sdata", s_change_sec, 's'},
828   {"livereg", s_ignore, 0},
829   {"abicalls", s_abicalls, 0},
830   {"cpload", s_cpload, 0},
831   {"cpsetup", s_cpsetup, 0},
832   {"cplocal", s_cplocal, 0},
833   {"cprestore", s_cprestore, 0},
834   {"cpreturn", s_cpreturn, 0},
835   {"gpvalue", s_gpvalue, 0},
836   {"gpword", s_gpword, 0},
837   {"cpadd", s_cpadd, 0},
838   {"insn", s_insn, 0},
839
840   /* Relatively generic pseudo-ops that happen to be used on MIPS
841      chips.  */
842   {"asciiz", stringer, 1},
843   {"bss", s_change_sec, 'b'},
844   {"err", s_err, 0},
845   {"half", s_cons, 1},
846   {"dword", s_cons, 3},
847   {"weakext", s_mips_weakext, 0},
848
849   /* These pseudo-ops are defined in read.c, but must be overridden
850      here for one reason or another.  */
851   {"align", s_align, 0},
852   {"byte", s_cons, 0},
853   {"data", s_change_sec, 'd'},
854   {"double", s_float_cons, 'd'},
855   {"float", s_float_cons, 'f'},
856   {"globl", s_mips_globl, 0},
857   {"global", s_mips_globl, 0},
858   {"hword", s_cons, 1},
859   {"int", s_cons, 2},
860   {"long", s_cons, 2},
861   {"octa", s_cons, 4},
862   {"quad", s_cons, 3},
863   {"short", s_cons, 1},
864   {"single", s_float_cons, 'f'},
865   {"stabn", s_mips_stab, 'n'},
866   {"text", s_change_sec, 't'},
867   {"word", s_cons, 2},
868
869 #ifdef MIPS_STABS_ELF
870   { "extern", ecoff_directive_extern, 0},
871 #endif
872
873   { NULL, NULL, 0 },
874 };
875
876 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
877 {
878   /* These pseudo-ops should be defined by the object file format.
879      However, a.out doesn't support them, so we have versions here.  */
880   {"aent", s_mips_ent, 1},
881   {"bgnb", s_ignore, 0},
882   {"end", s_mips_end, 0},
883   {"endb", s_ignore, 0},
884   {"ent", s_mips_ent, 0},
885   {"file", s_file, 0},
886   {"fmask", s_mips_mask, 'F'},
887   {"frame", s_mips_frame, 0},
888   {"loc", s_ignore, 0},
889   {"mask", s_mips_mask, 'R'},
890   {"verstamp", s_ignore, 0},
891   { NULL, NULL, 0 },
892 };
893
894 extern void pop_insert PARAMS ((const pseudo_typeS *));
895
896 void
897 mips_pop_insert ()
898 {
899   pop_insert (mips_pseudo_table);
900   if (! ECOFF_DEBUGGING)
901     pop_insert (mips_nonecoff_pseudo_table);
902 }
903 \f
904 /* Symbols labelling the current insn.  */
905
906 struct insn_label_list
907 {
908   struct insn_label_list *next;
909   symbolS *label;
910 };
911
912 static struct insn_label_list *insn_labels;
913 static struct insn_label_list *free_insn_labels;
914
915 static void mips_clear_insn_labels PARAMS ((void));
916
917 static inline void
918 mips_clear_insn_labels ()
919 {
920   register struct insn_label_list **pl;
921
922   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
923     ;
924   *pl = insn_labels;
925   insn_labels = NULL;
926 }
927 \f
928 static char *expr_end;
929
930 /* Expressions which appear in instructions.  These are set by
931    mips_ip.  */
932
933 static expressionS imm_expr;
934 static expressionS offset_expr;
935
936 /* Relocs associated with imm_expr and offset_expr.  */
937
938 static bfd_reloc_code_real_type imm_reloc[3]
939   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
940 static bfd_reloc_code_real_type offset_reloc[3]
941   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
942
943 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
944
945 static boolean imm_unmatched_hi;
946
947 /* These are set by mips16_ip if an explicit extension is used.  */
948
949 static boolean mips16_small, mips16_ext;
950
951 #ifdef MIPS_STABS_ELF
952 /* The pdr segment for per procedure frame/regmask info */
953
954 static segT pdr_seg;
955 #endif
956
957 static const char *
958 mips_isa_to_str (isa)
959      int isa;
960 {
961   const struct mips_cpu_info *ci;
962   static char s[20];
963
964   ci = mips_cpu_info_from_isa (isa);
965   if (ci != NULL)
966     return (ci->name);
967
968   sprintf (s, "ISA#%d", isa);
969   return s;
970 }
971
972 static const char *
973 mips_cpu_to_str (cpu)
974      int cpu;
975 {
976   const struct mips_cpu_info *ci;
977   static char s[16];
978
979   ci = mips_cpu_info_from_cpu (cpu);
980   if (ci != NULL)
981     return (ci->name);
982
983   sprintf (s, "CPU#%d", cpu);
984   return s;
985 }
986
987 /* The default target format to use.  */
988
989 const char *
990 mips_target_format ()
991 {
992   switch (OUTPUT_FLAVOR)
993     {
994     case bfd_target_aout_flavour:
995       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
996     case bfd_target_ecoff_flavour:
997       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
998     case bfd_target_coff_flavour:
999       return "pe-mips";
1000     case bfd_target_elf_flavour:
1001 #ifdef TE_TMIPS
1002       /* This is traditional mips */
1003       return (target_big_endian
1004               ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1005                  : "elf32-tradbigmips")
1006               : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1007                  : "elf32-tradlittlemips"));
1008 #else
1009       return (target_big_endian
1010               ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1011               : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1012                  : "elf32-littlemips"));
1013 #endif
1014     default:
1015       abort ();
1016       return NULL;
1017     }
1018 }
1019
1020 /* This function is called once, at assembler startup time.  It should
1021    set up all the tables, etc. that the MD part of the assembler will need.  */
1022
1023 void
1024 md_begin ()
1025 {
1026   register const char *retval = NULL;
1027   int i = 0;
1028   const char *cpu;
1029   char *a = NULL;
1030   int broken = 0;
1031   int mips_isa_from_cpu;
1032   int target_cpu_had_mips16 = 0;
1033   const struct mips_cpu_info *ci;
1034
1035   /* GP relative stuff not working for PE */
1036   if (strncmp (TARGET_OS, "pe", 2) == 0
1037       && g_switch_value != 0)
1038     {
1039       if (g_switch_seen)
1040         as_bad (_("-G not supported in this configuration."));
1041       g_switch_value = 0;
1042     }
1043
1044   cpu = TARGET_CPU;
1045   if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1046     {
1047       a = xmalloc (sizeof TARGET_CPU);
1048       strcpy (a, TARGET_CPU);
1049       a[(sizeof TARGET_CPU) - 3] = '\0';
1050       cpu = a;
1051     }
1052
1053   if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1054     {
1055       target_cpu_had_mips16 = 1;
1056       cpu += sizeof "mips16" - 1;
1057     }
1058
1059   if (mips_opts.mips16 < 0)
1060     mips_opts.mips16 = target_cpu_had_mips16;
1061
1062   /* Backward compatibility for historic -mcpu= option.  Check for
1063      incompatible options, warn if -mcpu is used.  */
1064   if (mips_cpu != CPU_UNKNOWN
1065       && mips_arch != CPU_UNKNOWN
1066       && mips_cpu != mips_arch)
1067     {
1068       as_fatal (_("The -mcpu option can't be used together with -march. "
1069                   "Use -mtune instead of -mcpu."));
1070     }
1071
1072   if (mips_cpu != CPU_UNKNOWN
1073       && mips_tune != CPU_UNKNOWN
1074       && mips_cpu != mips_tune)
1075     {
1076       as_fatal (_("The -mcpu option can't be used together with -mtune. "
1077                   "Use -march instead of -mcpu."));
1078     }
1079
1080 #if 1
1081   /* For backward compatibility, let -mipsN set various defaults.  */
1082   /* This code should go away, to be replaced with something rather more
1083      draconian.  Until GCC 3.1 has been released for some reasonable
1084      amount of time, however, we need to support this.  */
1085   if (mips_opts.isa != ISA_UNKNOWN)
1086     {
1087       /* Translate -mipsN to the appropriate settings of file_mips_gp32
1088          and file_mips_fp32.  Tag binaries as using the mipsN ISA.  */
1089       if (file_mips_gp32 < 0)
1090         {
1091           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1092             file_mips_gp32 = 0;
1093           else
1094             file_mips_gp32 = 1;
1095         }
1096       if (file_mips_fp32 < 0)
1097         {
1098           if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1099             file_mips_fp32 = 0;
1100           else
1101             file_mips_fp32 = 1;
1102         }
1103
1104       ci = mips_cpu_info_from_isa (mips_opts.isa);
1105       assert (ci != NULL);
1106       /* -mipsN has higher priority than -mcpu but lower than -march.  */
1107       if (mips_arch == CPU_UNKNOWN)
1108         mips_arch = ci->cpu;
1109
1110       /* Default mips_abi.  */
1111       if (mips_opts.abi == NO_ABI)
1112         {
1113           if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1114             mips_opts.abi = O32_ABI;
1115           else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1116             mips_opts.abi = O64_ABI;
1117         }
1118     }
1119
1120   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1121     {
1122       ci = mips_cpu_info_from_cpu (mips_cpu);
1123       assert (ci != NULL);
1124       mips_arch = ci->cpu;
1125       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
1126                  "-mtune instead."));
1127     }
1128
1129   /* Set tune from -mcpu, not from -mipsN.  */
1130   if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1131     {
1132       ci = mips_cpu_info_from_cpu (mips_cpu);
1133       assert (ci != NULL);
1134       mips_tune = ci->cpu;
1135     }
1136
1137   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1138      specified on the command line, or some other value if one was.
1139      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1140      the command line, or will be set otherwise if one was.  */
1141
1142   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1143     /* Handled above.  */;
1144 #else
1145   if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1146     {
1147       ci = mips_cpu_info_from_cpu (mips_cpu);
1148       assert (ci != NULL);
1149       mips_arch = ci->cpu;
1150       as_warn (_("The -mcpu option is deprecated.  Please use -march and "
1151                  "-mtune instead."));
1152     }
1153
1154   /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1155      specified on the command line, or some other value if one was.
1156      Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1157      the command line, or will be set otherwise if one was.  */
1158
1159   if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1160     {
1161       /* We have to check if the isa is the default isa of arch.  Otherwise
1162          we'll get invalid object file headers.  */
1163       ci = mips_cpu_info_from_cpu (mips_arch);
1164       assert (ci != NULL);
1165       if (mips_opts.isa != ci->isa)
1166         {
1167           /* This really should be an error instead of a warning, but old
1168              compilers only have -mcpu which sets both arch and tune.  For
1169              now, we discard arch and preserve tune.  */
1170           as_warn (_("The -march option is incompatible to -mipsN and "
1171                      "therefore ignored."));
1172           if (mips_tune == CPU_UNKNOWN)
1173             mips_tune = mips_arch;
1174           ci = mips_cpu_info_from_isa (mips_opts.isa);
1175           assert (ci != NULL);
1176           mips_arch = ci->cpu;
1177         }
1178     }
1179 #endif
1180   else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1181     {
1182       /* We have ARCH, we need ISA.  */
1183       ci = mips_cpu_info_from_cpu (mips_arch);
1184       assert (ci != NULL);
1185       mips_opts.isa = ci->isa;
1186     }
1187   else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1188     {
1189       /* We have ISA, we need default ARCH.  */
1190       ci = mips_cpu_info_from_isa (mips_opts.isa);
1191       assert (ci != NULL);
1192       mips_arch = ci->cpu;
1193     }
1194   else
1195     {
1196       /* We need to set both ISA and ARCH from target cpu.  */
1197       ci = mips_cpu_info_from_name (cpu);
1198       if (ci == NULL)
1199         ci = mips_cpu_info_from_cpu (CPU_R3000);
1200       assert (ci != NULL);
1201       mips_opts.isa = ci->isa;
1202       mips_arch = ci->cpu;
1203     }
1204
1205   if (mips_tune == CPU_UNKNOWN)
1206     mips_tune = mips_arch;
1207
1208   ci = mips_cpu_info_from_cpu (mips_arch);
1209   assert (ci != NULL);
1210   mips_isa_from_cpu = ci->isa;
1211
1212   /* End of TARGET_CPU processing, get rid of malloced memory
1213      if necessary.  */
1214   cpu = NULL;
1215   if (a != NULL)
1216     {
1217       free (a);
1218       a = NULL;
1219     }
1220
1221   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1222     as_bad (_("trap exception not supported at ISA 1"));
1223
1224   /* Set the EABI kind based on the ISA before the user gets
1225      to change the ISA with directives.  This isn't really
1226      the best, but then neither is basing the abi on the isa.  */
1227   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1228       && mips_opts.abi == EABI_ABI)
1229     mips_eabi64 = 1;
1230
1231   /* If they asked for mips1 or mips2 and a cpu that is
1232      mips3 or greater, then mark the object file 32BITMODE.  */
1233   if (mips_isa_from_cpu != ISA_UNKNOWN
1234       && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1235       && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1236     mips_32bitmode = 1;
1237
1238   /* If the selected architecture includes support for ASEs, enable
1239      generation of code for them.  */
1240   if (mips_opts.ase_mips3d == -1 && CPU_HAS_MIPS3D (mips_arch))
1241     mips_opts.ase_mips3d = 1;
1242
1243   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1244     as_warn (_("Could not set architecture and machine"));
1245
1246   if (file_mips_gp32 < 0)
1247     file_mips_gp32 = 0;
1248   if (file_mips_fp32 < 0)
1249     file_mips_fp32 = 0;
1250
1251   file_mips_isa = mips_opts.isa;
1252   file_mips_abi = mips_opts.abi;
1253   file_ase_mips3d = mips_opts.ase_mips3d;
1254   mips_opts.gp32 = file_mips_gp32;
1255   mips_opts.fp32 = file_mips_fp32;
1256
1257   op_hash = hash_new ();
1258
1259   for (i = 0; i < NUMOPCODES;)
1260     {
1261       const char *name = mips_opcodes[i].name;
1262
1263       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1264       if (retval != NULL)
1265         {
1266           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1267                    mips_opcodes[i].name, retval);
1268           /* Probably a memory allocation problem?  Give up now.  */
1269           as_fatal (_("Broken assembler.  No assembly attempted."));
1270         }
1271       do
1272         {
1273           if (mips_opcodes[i].pinfo != INSN_MACRO)
1274             {
1275               if (!validate_mips_insn (&mips_opcodes[i]))
1276                 broken = 1;
1277             }
1278           ++i;
1279         }
1280       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1281     }
1282
1283   mips16_op_hash = hash_new ();
1284
1285   i = 0;
1286   while (i < bfd_mips16_num_opcodes)
1287     {
1288       const char *name = mips16_opcodes[i].name;
1289
1290       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1291       if (retval != NULL)
1292         as_fatal (_("internal: can't hash `%s': %s"),
1293                   mips16_opcodes[i].name, retval);
1294       do
1295         {
1296           if (mips16_opcodes[i].pinfo != INSN_MACRO
1297               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1298                   != mips16_opcodes[i].match))
1299             {
1300               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1301                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1302               broken = 1;
1303             }
1304           ++i;
1305         }
1306       while (i < bfd_mips16_num_opcodes
1307              && strcmp (mips16_opcodes[i].name, name) == 0);
1308     }
1309
1310   if (broken)
1311     as_fatal (_("Broken assembler.  No assembly attempted."));
1312
1313   /* We add all the general register names to the symbol table.  This
1314      helps us detect invalid uses of them.  */
1315   for (i = 0; i < 32; i++)
1316     {
1317       char buf[5];
1318
1319       sprintf (buf, "$%d", i);
1320       symbol_table_insert (symbol_new (buf, reg_section, i,
1321                                        &zero_address_frag));
1322     }
1323   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1324                                    &zero_address_frag));
1325   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1326                                    &zero_address_frag));
1327   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1328                                    &zero_address_frag));
1329   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1330                                    &zero_address_frag));
1331   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1332                                    &zero_address_frag));
1333   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1334                                    &zero_address_frag));
1335   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1336                                    &zero_address_frag));
1337
1338   mips_no_prev_insn (false);
1339
1340   mips_gprmask = 0;
1341   mips_cprmask[0] = 0;
1342   mips_cprmask[1] = 0;
1343   mips_cprmask[2] = 0;
1344   mips_cprmask[3] = 0;
1345
1346   /* set the default alignment for the text section (2**2) */
1347   record_alignment (text_section, 2);
1348
1349   if (USE_GLOBAL_POINTER_OPT)
1350     bfd_set_gp_size (stdoutput, g_switch_value);
1351
1352   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1353     {
1354       /* On a native system, sections must be aligned to 16 byte
1355          boundaries.  When configured for an embedded ELF target, we
1356          don't bother.  */
1357       if (strcmp (TARGET_OS, "elf") != 0)
1358         {
1359           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1360           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1361           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1362         }
1363
1364       /* Create a .reginfo section for register masks and a .mdebug
1365          section for debugging information.  */
1366       {
1367         segT seg;
1368         subsegT subseg;
1369         flagword flags;
1370         segT sec;
1371
1372         seg = now_seg;
1373         subseg = now_subseg;
1374
1375         /* The ABI says this section should be loaded so that the
1376            running program can access it.  However, we don't load it
1377            if we are configured for an embedded target */
1378         flags = SEC_READONLY | SEC_DATA;
1379         if (strcmp (TARGET_OS, "elf") != 0)
1380           flags |= SEC_ALLOC | SEC_LOAD;
1381
1382         if (file_mips_abi != N64_ABI)
1383           {
1384             sec = subseg_new (".reginfo", (subsegT) 0);
1385
1386             bfd_set_section_flags (stdoutput, sec, flags);
1387             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1388
1389 #ifdef OBJ_ELF
1390             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1391 #endif
1392           }
1393         else
1394           {
1395             /* The 64-bit ABI uses a .MIPS.options section rather than
1396                .reginfo section.  */
1397             sec = subseg_new (".MIPS.options", (subsegT) 0);
1398             bfd_set_section_flags (stdoutput, sec, flags);
1399             bfd_set_section_alignment (stdoutput, sec, 3);
1400
1401 #ifdef OBJ_ELF
1402             /* Set up the option header.  */
1403             {
1404               Elf_Internal_Options opthdr;
1405               char *f;
1406
1407               opthdr.kind = ODK_REGINFO;
1408               opthdr.size = (sizeof (Elf_External_Options)
1409                              + sizeof (Elf64_External_RegInfo));
1410               opthdr.section = 0;
1411               opthdr.info = 0;
1412               f = frag_more (sizeof (Elf_External_Options));
1413               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1414                                              (Elf_External_Options *) f);
1415
1416               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1417             }
1418 #endif
1419           }
1420
1421         if (ECOFF_DEBUGGING)
1422           {
1423             sec = subseg_new (".mdebug", (subsegT) 0);
1424             (void) bfd_set_section_flags (stdoutput, sec,
1425                                           SEC_HAS_CONTENTS | SEC_READONLY);
1426             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1427           }
1428
1429 #ifdef MIPS_STABS_ELF
1430         pdr_seg = subseg_new (".pdr", (subsegT) 0);
1431         (void) bfd_set_section_flags (stdoutput, pdr_seg,
1432                              SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1433         (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1434 #endif
1435
1436         subseg_set (seg, subseg);
1437       }
1438     }
1439
1440   if (! ECOFF_DEBUGGING)
1441     md_obj_begin ();
1442 }
1443
1444 void
1445 md_mips_end ()
1446 {
1447   if (! ECOFF_DEBUGGING)
1448     md_obj_end ();
1449 }
1450
1451 void
1452 md_assemble (str)
1453      char *str;
1454 {
1455   struct mips_cl_insn insn;
1456   bfd_reloc_code_real_type unused_reloc[3]
1457     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1458
1459   imm_expr.X_op = O_absent;
1460   imm_unmatched_hi = false;
1461   offset_expr.X_op = O_absent;
1462   imm_reloc[0] = BFD_RELOC_UNUSED;
1463   imm_reloc[1] = BFD_RELOC_UNUSED;
1464   imm_reloc[2] = BFD_RELOC_UNUSED;
1465   offset_reloc[0] = BFD_RELOC_UNUSED;
1466   offset_reloc[1] = BFD_RELOC_UNUSED;
1467   offset_reloc[2] = BFD_RELOC_UNUSED;
1468
1469   if (mips_opts.mips16)
1470     mips16_ip (str, &insn);
1471   else
1472     {
1473       mips_ip (str, &insn);
1474       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1475             str, insn.insn_opcode));
1476     }
1477
1478   if (insn_error)
1479     {
1480       as_bad ("%s `%s'", insn_error, str);
1481       return;
1482     }
1483
1484   if (insn.insn_mo->pinfo == INSN_MACRO)
1485     {
1486       if (mips_opts.mips16)
1487         mips16_macro (&insn);
1488       else
1489         macro (&insn);
1490     }
1491   else
1492     {
1493       if (imm_expr.X_op != O_absent)
1494         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1495       else if (offset_expr.X_op != O_absent)
1496         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1497       else
1498         append_insn (NULL, &insn, NULL, unused_reloc, false);
1499     }
1500 }
1501
1502 /* See whether instruction IP reads register REG.  CLASS is the type
1503    of register.  */
1504
1505 static int
1506 insn_uses_reg (ip, reg, class)
1507      struct mips_cl_insn *ip;
1508      unsigned int reg;
1509      enum mips_regclass class;
1510 {
1511   if (class == MIPS16_REG)
1512     {
1513       assert (mips_opts.mips16);
1514       reg = mips16_to_32_reg_map[reg];
1515       class = MIPS_GR_REG;
1516     }
1517
1518   /* Don't report on general register 0, since it never changes.  */
1519   if (class == MIPS_GR_REG && reg == 0)
1520     return 0;
1521
1522   if (class == MIPS_FP_REG)
1523     {
1524       assert (! mips_opts.mips16);
1525       /* If we are called with either $f0 or $f1, we must check $f0.
1526          This is not optimal, because it will introduce an unnecessary
1527          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1528          need to distinguish reading both $f0 and $f1 or just one of
1529          them.  Note that we don't have to check the other way,
1530          because there is no instruction that sets both $f0 and $f1
1531          and requires a delay.  */
1532       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1533           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1534               == (reg &~ (unsigned) 1)))
1535         return 1;
1536       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1537           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1538               == (reg &~ (unsigned) 1)))
1539         return 1;
1540     }
1541   else if (! mips_opts.mips16)
1542     {
1543       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1544           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1545         return 1;
1546       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1547           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1548         return 1;
1549     }
1550   else
1551     {
1552       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1553           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1554                                     & MIPS16OP_MASK_RX)]
1555               == reg))
1556         return 1;
1557       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1558           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1559                                     & MIPS16OP_MASK_RY)]
1560               == reg))
1561         return 1;
1562       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1563           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1564                                     & MIPS16OP_MASK_MOVE32Z)]
1565               == reg))
1566         return 1;
1567       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1568         return 1;
1569       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1570         return 1;
1571       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1572         return 1;
1573       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1574           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1575               & MIPS16OP_MASK_REGR32) == reg)
1576         return 1;
1577     }
1578
1579   return 0;
1580 }
1581
1582 /* This function returns true if modifying a register requires a
1583    delay.  */
1584
1585 static int
1586 reg_needs_delay (reg)
1587      unsigned int reg;
1588 {
1589   unsigned long prev_pinfo;
1590
1591   prev_pinfo = prev_insn.insn_mo->pinfo;
1592   if (! mips_opts.noreorder
1593       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1594       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1595           || (! gpr_interlocks
1596               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1597     {
1598       /* A load from a coprocessor or from memory.  All load
1599          delays delay the use of general register rt for one
1600          instruction on the r3000.  The r6000 and r4000 use
1601          interlocks.  */
1602       /* Itbl support may require additional care here.  */
1603       know (prev_pinfo & INSN_WRITE_GPR_T);
1604       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1605         return 1;
1606     }
1607
1608   return 0;
1609 }
1610
1611 /* Mark instruction labels in mips16 mode.  This permits the linker to
1612    handle them specially, such as generating jalx instructions when
1613    needed.  We also make them odd for the duration of the assembly, in
1614    order to generate the right sort of code.  We will make them even
1615    in the adjust_symtab routine, while leaving them marked.  This is
1616    convenient for the debugger and the disassembler.  The linker knows
1617    to make them odd again.  */
1618
1619 static void
1620 mips16_mark_labels ()
1621 {
1622   if (mips_opts.mips16)
1623     {
1624       struct insn_label_list *l;
1625       valueT val;
1626
1627       for (l = insn_labels; l != NULL; l = l->next)
1628         {
1629 #ifdef OBJ_ELF
1630           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1631             S_SET_OTHER (l->label, STO_MIPS16);
1632 #endif
1633           val = S_GET_VALUE (l->label);
1634           if ((val & 1) == 0)
1635             S_SET_VALUE (l->label, val + 1);
1636         }
1637     }
1638 }
1639
1640 /* Output an instruction.  PLACE is where to put the instruction; if
1641    it is NULL, this uses frag_more to get room.  IP is the instruction
1642    information.  ADDRESS_EXPR is an operand of the instruction to be
1643    used with RELOC_TYPE.  */
1644
1645 static void
1646 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1647      char *place;
1648      struct mips_cl_insn *ip;
1649      expressionS *address_expr;
1650      bfd_reloc_code_real_type *reloc_type;
1651      boolean unmatched_hi;
1652 {
1653   register unsigned long prev_pinfo, pinfo;
1654   char *f;
1655   fixS *fixp[3];
1656   int nops = 0;
1657
1658   /* Mark instruction labels in mips16 mode.  */
1659   if (mips_opts.mips16)
1660     mips16_mark_labels ();
1661
1662   prev_pinfo = prev_insn.insn_mo->pinfo;
1663   pinfo = ip->insn_mo->pinfo;
1664
1665   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1666     {
1667       int prev_prev_nop;
1668
1669       /* If the previous insn required any delay slots, see if we need
1670          to insert a NOP or two.  There are eight kinds of possible
1671          hazards, of which an instruction can have at most one type.
1672          (1) a load from memory delay
1673          (2) a load from a coprocessor delay
1674          (3) an unconditional branch delay
1675          (4) a conditional branch delay
1676          (5) a move to coprocessor register delay
1677          (6) a load coprocessor register from memory delay
1678          (7) a coprocessor condition code delay
1679          (8) a HI/LO special register delay
1680
1681          There are a lot of optimizations we could do that we don't.
1682          In particular, we do not, in general, reorder instructions.
1683          If you use gcc with optimization, it will reorder
1684          instructions and generally do much more optimization then we
1685          do here; repeating all that work in the assembler would only
1686          benefit hand written assembly code, and does not seem worth
1687          it.  */
1688
1689       /* This is how a NOP is emitted.  */
1690 #define emit_nop()                                      \
1691   (mips_opts.mips16                                     \
1692    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1693    : md_number_to_chars (frag_more (4), 0, 4))
1694
1695       /* The previous insn might require a delay slot, depending upon
1696          the contents of the current insn.  */
1697       if (! mips_opts.mips16
1698           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1699           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1700                && ! cop_interlocks)
1701               || (! gpr_interlocks
1702                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1703         {
1704           /* A load from a coprocessor or from memory.  All load
1705              delays delay the use of general register rt for one
1706              instruction on the r3000.  The r6000 and r4000 use
1707              interlocks.  */
1708           /* Itbl support may require additional care here.  */
1709           know (prev_pinfo & INSN_WRITE_GPR_T);
1710           if (mips_optimize == 0
1711               || insn_uses_reg (ip,
1712                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1713                                  & OP_MASK_RT),
1714                                 MIPS_GR_REG))
1715             ++nops;
1716         }
1717       else if (! mips_opts.mips16
1718                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1719                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1720                     && ! cop_interlocks)
1721                    || (mips_opts.isa == ISA_MIPS1
1722                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1723         {
1724           /* A generic coprocessor delay.  The previous instruction
1725              modified a coprocessor general or control register.  If
1726              it modified a control register, we need to avoid any
1727              coprocessor instruction (this is probably not always
1728              required, but it sometimes is).  If it modified a general
1729              register, we avoid using that register.
1730
1731              On the r6000 and r4000 loading a coprocessor register
1732              from memory is interlocked, and does not require a delay.
1733
1734              This case is not handled very well.  There is no special
1735              knowledge of CP0 handling, and the coprocessors other
1736              than the floating point unit are not distinguished at
1737              all.  */
1738           /* Itbl support may require additional care here. FIXME!
1739              Need to modify this to include knowledge about
1740              user specified delays!  */
1741           if (prev_pinfo & INSN_WRITE_FPR_T)
1742             {
1743               if (mips_optimize == 0
1744                   || insn_uses_reg (ip,
1745                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1746                                      & OP_MASK_FT),
1747                                     MIPS_FP_REG))
1748                 ++nops;
1749             }
1750           else if (prev_pinfo & INSN_WRITE_FPR_S)
1751             {
1752               if (mips_optimize == 0
1753                   || insn_uses_reg (ip,
1754                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1755                                      & OP_MASK_FS),
1756                                     MIPS_FP_REG))
1757                 ++nops;
1758             }
1759           else
1760             {
1761               /* We don't know exactly what the previous instruction
1762                  does.  If the current instruction uses a coprocessor
1763                  register, we must insert a NOP.  If previous
1764                  instruction may set the condition codes, and the
1765                  current instruction uses them, we must insert two
1766                  NOPS.  */
1767               /* Itbl support may require additional care here.  */
1768               if (mips_optimize == 0
1769                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1770                       && (pinfo & INSN_READ_COND_CODE)))
1771                 nops += 2;
1772               else if (pinfo & INSN_COP)
1773                 ++nops;
1774             }
1775         }
1776       else if (! mips_opts.mips16
1777                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1778                && (prev_pinfo & INSN_WRITE_COND_CODE)
1779                && ! cop_interlocks)
1780         {
1781           /* The previous instruction sets the coprocessor condition
1782              codes, but does not require a general coprocessor delay
1783              (this means it is a floating point comparison
1784              instruction).  If this instruction uses the condition
1785              codes, we need to insert a single NOP.  */
1786           /* Itbl support may require additional care here.  */
1787           if (mips_optimize == 0
1788               || (pinfo & INSN_READ_COND_CODE))
1789             ++nops;
1790         }
1791
1792       /* If we're fixing up mfhi/mflo for the r7000 and the
1793          previous insn was an mfhi/mflo and the current insn
1794          reads the register that the mfhi/mflo wrote to, then
1795          insert two nops.  */
1796
1797       else if (mips_7000_hilo_fix
1798                && MF_HILO_INSN (prev_pinfo)
1799                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1800                                       & OP_MASK_RD),
1801                                  MIPS_GR_REG))
1802         {
1803           nops += 2;
1804         }
1805
1806       /* If we're fixing up mfhi/mflo for the r7000 and the
1807          2nd previous insn was an mfhi/mflo and the current insn
1808          reads the register that the mfhi/mflo wrote to, then
1809          insert one nop.  */
1810
1811       else if (mips_7000_hilo_fix
1812                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1813                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1814                                        & OP_MASK_RD),
1815                                     MIPS_GR_REG))
1816
1817         {
1818           nops += 1;
1819         }
1820
1821       else if (prev_pinfo & INSN_READ_LO)
1822         {
1823           /* The previous instruction reads the LO register; if the
1824              current instruction writes to the LO register, we must
1825              insert two NOPS.  Some newer processors have interlocks.
1826              Also the tx39's multiply instructions can be exectuted
1827              immediatly after a read from HI/LO (without the delay),
1828              though the tx39's divide insns still do require the
1829              delay.  */
1830           if (! (hilo_interlocks
1831                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1832               && (mips_optimize == 0
1833                   || (pinfo & INSN_WRITE_LO)))
1834             nops += 2;
1835           /* Most mips16 branch insns don't have a delay slot.
1836              If a read from LO is immediately followed by a branch
1837              to a write to LO we have a read followed by a write
1838              less than 2 insns away.  We assume the target of
1839              a branch might be a write to LO, and insert a nop
1840              between a read and an immediately following branch.  */
1841           else if (mips_opts.mips16
1842                    && (mips_optimize == 0
1843                        || (pinfo & MIPS16_INSN_BRANCH)))
1844             nops += 1;
1845         }
1846       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1847         {
1848           /* The previous instruction reads the HI register; if the
1849              current instruction writes to the HI register, we must
1850              insert a NOP.  Some newer processors have interlocks.
1851              Also the note tx39's multiply above.  */
1852           if (! (hilo_interlocks
1853                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1854               && (mips_optimize == 0
1855                   || (pinfo & INSN_WRITE_HI)))
1856             nops += 2;
1857           /* Most mips16 branch insns don't have a delay slot.
1858              If a read from HI is immediately followed by a branch
1859              to a write to HI we have a read followed by a write
1860              less than 2 insns away.  We assume the target of
1861              a branch might be a write to HI, and insert a nop
1862              between a read and an immediately following branch.  */
1863           else if (mips_opts.mips16
1864                    && (mips_optimize == 0
1865                        || (pinfo & MIPS16_INSN_BRANCH)))
1866             nops += 1;
1867         }
1868
1869       /* If the previous instruction was in a noreorder section, then
1870          we don't want to insert the nop after all.  */
1871       /* Itbl support may require additional care here.  */
1872       if (prev_insn_unreordered)
1873         nops = 0;
1874
1875       /* There are two cases which require two intervening
1876          instructions: 1) setting the condition codes using a move to
1877          coprocessor instruction which requires a general coprocessor
1878          delay and then reading the condition codes 2) reading the HI
1879          or LO register and then writing to it (except on processors
1880          which have interlocks).  If we are not already emitting a NOP
1881          instruction, we must check for these cases compared to the
1882          instruction previous to the previous instruction.  */
1883       if ((! mips_opts.mips16
1884            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1885            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1886            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1887            && (pinfo & INSN_READ_COND_CODE)
1888            && ! cop_interlocks)
1889           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1890               && (pinfo & INSN_WRITE_LO)
1891               && ! (hilo_interlocks
1892                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1893           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1894               && (pinfo & INSN_WRITE_HI)
1895               && ! (hilo_interlocks
1896                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1897         prev_prev_nop = 1;
1898       else
1899         prev_prev_nop = 0;
1900
1901       if (prev_prev_insn_unreordered)
1902         prev_prev_nop = 0;
1903
1904       if (prev_prev_nop && nops == 0)
1905         ++nops;
1906
1907       /* If we are being given a nop instruction, don't bother with
1908          one of the nops we would otherwise output.  This will only
1909          happen when a nop instruction is used with mips_optimize set
1910          to 0.  */
1911       if (nops > 0
1912           && ! mips_opts.noreorder
1913           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1914         --nops;
1915
1916       /* Now emit the right number of NOP instructions.  */
1917       if (nops > 0 && ! mips_opts.noreorder)
1918         {
1919           fragS *old_frag;
1920           unsigned long old_frag_offset;
1921           int i;
1922           struct insn_label_list *l;
1923
1924           old_frag = frag_now;
1925           old_frag_offset = frag_now_fix ();
1926
1927           for (i = 0; i < nops; i++)
1928             emit_nop ();
1929
1930           if (listing)
1931             {
1932               listing_prev_line ();
1933               /* We may be at the start of a variant frag.  In case we
1934                  are, make sure there is enough space for the frag
1935                  after the frags created by listing_prev_line.  The
1936                  argument to frag_grow here must be at least as large
1937                  as the argument to all other calls to frag_grow in
1938                  this file.  We don't have to worry about being in the
1939                  middle of a variant frag, because the variants insert
1940                  all needed nop instructions themselves.  */
1941               frag_grow (40);
1942             }
1943
1944           for (l = insn_labels; l != NULL; l = l->next)
1945             {
1946               valueT val;
1947
1948               assert (S_GET_SEGMENT (l->label) == now_seg);
1949               symbol_set_frag (l->label, frag_now);
1950               val = (valueT) frag_now_fix ();
1951               /* mips16 text labels are stored as odd.  */
1952               if (mips_opts.mips16)
1953                 val += 1;
1954               S_SET_VALUE (l->label, val);
1955             }
1956
1957 #ifndef NO_ECOFF_DEBUGGING
1958           if (ECOFF_DEBUGGING)
1959             ecoff_fix_loc (old_frag, old_frag_offset);
1960 #endif
1961         }
1962       else if (prev_nop_frag != NULL)
1963         {
1964           /* We have a frag holding nops we may be able to remove.  If
1965              we don't need any nops, we can decrease the size of
1966              prev_nop_frag by the size of one instruction.  If we do
1967              need some nops, we count them in prev_nops_required.  */
1968           if (prev_nop_frag_since == 0)
1969             {
1970               if (nops == 0)
1971                 {
1972                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1973                   --prev_nop_frag_holds;
1974                 }
1975               else
1976                 prev_nop_frag_required += nops;
1977             }
1978           else
1979             {
1980               if (prev_prev_nop == 0)
1981                 {
1982                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1983                   --prev_nop_frag_holds;
1984                 }
1985               else
1986                 ++prev_nop_frag_required;
1987             }
1988
1989           if (prev_nop_frag_holds <= prev_nop_frag_required)
1990             prev_nop_frag = NULL;
1991
1992           ++prev_nop_frag_since;
1993
1994           /* Sanity check: by the time we reach the second instruction
1995              after prev_nop_frag, we should have used up all the nops
1996              one way or another.  */
1997           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1998         }
1999     }
2000
2001   if (*reloc_type > BFD_RELOC_UNUSED)
2002     {
2003       /* We need to set up a variant frag.  */
2004       assert (mips_opts.mips16 && address_expr != NULL);
2005       f = frag_var (rs_machine_dependent, 4, 0,
2006                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2007                                          mips16_small, mips16_ext,
2008                                          (prev_pinfo
2009                                           & INSN_UNCOND_BRANCH_DELAY),
2010                                          (*prev_insn_reloc_type
2011                                           == BFD_RELOC_MIPS16_JMP)),
2012                     make_expr_symbol (address_expr), 0, NULL);
2013     }
2014   else if (place != NULL)
2015     f = place;
2016   else if (mips_opts.mips16
2017            && ! ip->use_extend
2018            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2019     {
2020       /* Make sure there is enough room to swap this instruction with
2021          a following jump instruction.  */
2022       frag_grow (6);
2023       f = frag_more (2);
2024     }
2025   else
2026     {
2027       if (mips_opts.mips16
2028           && mips_opts.noreorder
2029           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2030         as_warn (_("extended instruction in delay slot"));
2031
2032       f = frag_more (4);
2033     }
2034
2035   fixp[0] = fixp[1] = fixp[2] = NULL;
2036   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2037     {
2038       if (address_expr->X_op == O_constant)
2039         {
2040           valueT tmp;
2041
2042           switch (*reloc_type)
2043             {
2044             case BFD_RELOC_32:
2045               ip->insn_opcode |= address_expr->X_add_number;
2046               break;
2047
2048             case BFD_RELOC_MIPS_HIGHEST:
2049               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2050               tmp >>= 16;
2051               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2052               break;
2053
2054             case BFD_RELOC_MIPS_HIGHER:
2055               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2056               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2057               break;
2058
2059             case BFD_RELOC_HI16_S:
2060               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2061                                   >> 16) & 0xffff;
2062               break;
2063
2064             case BFD_RELOC_HI16:
2065               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2066               break;
2067
2068             case BFD_RELOC_LO16:
2069               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2070               break;
2071
2072             case BFD_RELOC_MIPS_JMP:
2073               if ((address_expr->X_add_number & 3) != 0)
2074                 as_bad (_("jump to misaligned address (0x%lx)"),
2075                         (unsigned long) address_expr->X_add_number);
2076               if (address_expr->X_add_number & ~0xfffffff
2077                   || address_expr->X_add_number > 0x7fffffc)
2078                 as_bad (_("jump address range overflow (0x%lx)"),
2079                         (unsigned long) address_expr->X_add_number);
2080               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2081               break;
2082
2083             case BFD_RELOC_MIPS16_JMP:
2084               if ((address_expr->X_add_number & 3) != 0)
2085                 as_bad (_("jump to misaligned address (0x%lx)"),
2086                         (unsigned long) address_expr->X_add_number);
2087               if (address_expr->X_add_number & ~0xfffffff
2088                   || address_expr->X_add_number > 0x7fffffc)
2089                 as_bad (_("jump address range overflow (0x%lx)"),
2090                         (unsigned long) address_expr->X_add_number);
2091               ip->insn_opcode |=
2092                 (((address_expr->X_add_number & 0x7c0000) << 3)
2093                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2094                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2095               break;
2096
2097             case BFD_RELOC_16_PCREL:
2098               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2099               break;
2100
2101             case BFD_RELOC_16_PCREL_S2:
2102               goto need_reloc;
2103
2104             default:
2105               internalError ();
2106             }
2107         }
2108       else
2109         {
2110         need_reloc:
2111           /* Don't generate a reloc if we are writing into a variant frag.  */
2112           if (place == NULL)
2113             {
2114               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2115                                      address_expr,
2116                                      (*reloc_type == BFD_RELOC_16_PCREL
2117                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
2118                                      reloc_type[0]);
2119
2120               /* These relocations can have an addend that won't fit in
2121                  4 octets for 64bit assembly.  */
2122               if (HAVE_64BIT_GPRS &&
2123                   (*reloc_type == BFD_RELOC_16
2124                    || *reloc_type == BFD_RELOC_32
2125                    || *reloc_type == BFD_RELOC_MIPS_JMP
2126                    || *reloc_type == BFD_RELOC_HI16_S
2127                    || *reloc_type == BFD_RELOC_LO16
2128                    || *reloc_type == BFD_RELOC_GPREL16
2129                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2130                    || *reloc_type == BFD_RELOC_GPREL32
2131                    || *reloc_type == BFD_RELOC_64
2132                    || *reloc_type == BFD_RELOC_CTOR
2133                    || *reloc_type == BFD_RELOC_MIPS_SUB
2134                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2135                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2136                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2137                    || *reloc_type == BFD_RELOC_MIPS_REL16
2138                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2139                 fixp[0]->fx_no_overflow = 1;
2140
2141               if (unmatched_hi)
2142                 {
2143                   struct mips_hi_fixup *hi_fixup;
2144
2145                   assert (*reloc_type == BFD_RELOC_HI16_S);
2146                   hi_fixup = ((struct mips_hi_fixup *)
2147                               xmalloc (sizeof (struct mips_hi_fixup)));
2148                   hi_fixup->fixp = fixp[0];
2149                   hi_fixup->seg = now_seg;
2150                   hi_fixup->next = mips_hi_fixup_list;
2151                   mips_hi_fixup_list = hi_fixup;
2152                 }
2153
2154               if (reloc_type[1] != BFD_RELOC_UNUSED)
2155                 {
2156                   /* FIXME: This symbol can be one of
2157                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2158                   address_expr->X_op = O_absent;
2159                   address_expr->X_add_symbol = 0;
2160                   address_expr->X_add_number = 0;
2161
2162                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2163                                          4, address_expr, false,
2164                                          reloc_type[1]);
2165
2166                   /* These relocations can have an addend that won't fit in
2167                      4 octets for 64bit assembly.  */
2168                   if (HAVE_64BIT_GPRS &&
2169                       (*reloc_type == BFD_RELOC_16
2170                        || *reloc_type == BFD_RELOC_32
2171                        || *reloc_type == BFD_RELOC_MIPS_JMP
2172                        || *reloc_type == BFD_RELOC_HI16_S
2173                        || *reloc_type == BFD_RELOC_LO16
2174                        || *reloc_type == BFD_RELOC_GPREL16
2175                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2176                        || *reloc_type == BFD_RELOC_GPREL32
2177                        || *reloc_type == BFD_RELOC_64
2178                        || *reloc_type == BFD_RELOC_CTOR
2179                        || *reloc_type == BFD_RELOC_MIPS_SUB
2180                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2181                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2182                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2183                        || *reloc_type == BFD_RELOC_MIPS_REL16
2184                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2185                     fixp[1]->fx_no_overflow = 1;
2186
2187                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2188                     {
2189                       address_expr->X_op = O_absent;
2190                       address_expr->X_add_symbol = 0;
2191                       address_expr->X_add_number = 0;
2192
2193                       fixp[2] = fix_new_exp (frag_now,
2194                                              f - frag_now->fr_literal, 4,
2195                                              address_expr, false,
2196                                              reloc_type[2]);
2197
2198                       /* These relocations can have an addend that won't fit in
2199                          4 octets for 64bit assembly.  */
2200                       if (HAVE_64BIT_GPRS &&
2201                           (*reloc_type == BFD_RELOC_16
2202                            || *reloc_type == BFD_RELOC_32
2203                            || *reloc_type == BFD_RELOC_MIPS_JMP
2204                            || *reloc_type == BFD_RELOC_HI16_S
2205                            || *reloc_type == BFD_RELOC_LO16
2206                            || *reloc_type == BFD_RELOC_GPREL16
2207                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2208                            || *reloc_type == BFD_RELOC_GPREL32
2209                            || *reloc_type == BFD_RELOC_64
2210                            || *reloc_type == BFD_RELOC_CTOR
2211                            || *reloc_type == BFD_RELOC_MIPS_SUB
2212                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2213                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2214                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2215                            || *reloc_type == BFD_RELOC_MIPS_REL16
2216                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2217                         fixp[2]->fx_no_overflow = 1;
2218                     }
2219                 }
2220             }
2221         }
2222     }
2223
2224   if (! mips_opts.mips16)
2225     md_number_to_chars (f, ip->insn_opcode, 4);
2226   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2227     {
2228       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2229       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2230     }
2231   else
2232     {
2233       if (ip->use_extend)
2234         {
2235           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2236           f += 2;
2237         }
2238       md_number_to_chars (f, ip->insn_opcode, 2);
2239     }
2240
2241   /* Update the register mask information.  */
2242   if (! mips_opts.mips16)
2243     {
2244       if (pinfo & INSN_WRITE_GPR_D)
2245         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2246       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2247         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2248       if (pinfo & INSN_READ_GPR_S)
2249         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2250       if (pinfo & INSN_WRITE_GPR_31)
2251         mips_gprmask |= 1 << 31;
2252       if (pinfo & INSN_WRITE_FPR_D)
2253         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2254       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2255         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2256       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2257         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2258       if ((pinfo & INSN_READ_FPR_R) != 0)
2259         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2260       if (pinfo & INSN_COP)
2261         {
2262           /* We don't keep enough information to sort these cases out.
2263              The itbl support does keep this information however, although
2264              we currently don't support itbl fprmats as part of the cop
2265              instruction.  May want to add this support in the future.  */
2266         }
2267       /* Never set the bit for $0, which is always zero.  */
2268       mips_gprmask &= ~1 << 0;
2269     }
2270   else
2271     {
2272       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2273         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2274                               & MIPS16OP_MASK_RX);
2275       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2276         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2277                               & MIPS16OP_MASK_RY);
2278       if (pinfo & MIPS16_INSN_WRITE_Z)
2279         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2280                               & MIPS16OP_MASK_RZ);
2281       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2282         mips_gprmask |= 1 << TREG;
2283       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2284         mips_gprmask |= 1 << SP;
2285       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2286         mips_gprmask |= 1 << RA;
2287       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2288         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2289       if (pinfo & MIPS16_INSN_READ_Z)
2290         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2291                               & MIPS16OP_MASK_MOVE32Z);
2292       if (pinfo & MIPS16_INSN_READ_GPR_X)
2293         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2294                               & MIPS16OP_MASK_REGR32);
2295     }
2296
2297   if (place == NULL && ! mips_opts.noreorder)
2298     {
2299       /* Filling the branch delay slot is more complex.  We try to
2300          switch the branch with the previous instruction, which we can
2301          do if the previous instruction does not set up a condition
2302          that the branch tests and if the branch is not itself the
2303          target of any branch.  */
2304       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2305           || (pinfo & INSN_COND_BRANCH_DELAY))
2306         {
2307           if (mips_optimize < 2
2308               /* If we have seen .set volatile or .set nomove, don't
2309                  optimize.  */
2310               || mips_opts.nomove != 0
2311               /* If we had to emit any NOP instructions, then we
2312                  already know we can not swap.  */
2313               || nops != 0
2314               /* If we don't even know the previous insn, we can not
2315                  swap.  */
2316               || ! prev_insn_valid
2317               /* If the previous insn is already in a branch delay
2318                  slot, then we can not swap.  */
2319               || prev_insn_is_delay_slot
2320               /* If the previous previous insn was in a .set
2321                  noreorder, we can't swap.  Actually, the MIPS
2322                  assembler will swap in this situation.  However, gcc
2323                  configured -with-gnu-as will generate code like
2324                    .set noreorder
2325                    lw   $4,XXX
2326                    .set reorder
2327                    INSN
2328                    bne  $4,$0,foo
2329                  in which we can not swap the bne and INSN.  If gcc is
2330                  not configured -with-gnu-as, it does not output the
2331                  .set pseudo-ops.  We don't have to check
2332                  prev_insn_unreordered, because prev_insn_valid will
2333                  be 0 in that case.  We don't want to use
2334                  prev_prev_insn_valid, because we do want to be able
2335                  to swap at the start of a function.  */
2336               || prev_prev_insn_unreordered
2337               /* If the branch is itself the target of a branch, we
2338                  can not swap.  We cheat on this; all we check for is
2339                  whether there is a label on this instruction.  If
2340                  there are any branches to anything other than a
2341                  label, users must use .set noreorder.  */
2342               || insn_labels != NULL
2343               /* If the previous instruction is in a variant frag, we
2344                  can not do the swap.  This does not apply to the
2345                  mips16, which uses variant frags for different
2346                  purposes.  */
2347               || (! mips_opts.mips16
2348                   && prev_insn_frag->fr_type == rs_machine_dependent)
2349               /* If the branch reads the condition codes, we don't
2350                  even try to swap, because in the sequence
2351                    ctc1 $X,$31
2352                    INSN
2353                    INSN
2354                    bc1t LABEL
2355                  we can not swap, and I don't feel like handling that
2356                  case.  */
2357               || (! mips_opts.mips16
2358                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2359                   && (pinfo & INSN_READ_COND_CODE))
2360               /* We can not swap with an instruction that requires a
2361                  delay slot, becase the target of the branch might
2362                  interfere with that instruction.  */
2363               || (! mips_opts.mips16
2364                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2365                   && (prev_pinfo
2366               /* Itbl support may require additional care here.  */
2367                       & (INSN_LOAD_COPROC_DELAY
2368                          | INSN_COPROC_MOVE_DELAY
2369                          | INSN_WRITE_COND_CODE)))
2370               || (! (hilo_interlocks
2371                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2372                   && (prev_pinfo
2373                       & (INSN_READ_LO
2374                          | INSN_READ_HI)))
2375               || (! mips_opts.mips16
2376                   && ! gpr_interlocks
2377                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2378               || (! mips_opts.mips16
2379                   && mips_opts.isa == ISA_MIPS1
2380                   /* Itbl support may require additional care here.  */
2381                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2382               /* We can not swap with a branch instruction.  */
2383               || (prev_pinfo
2384                   & (INSN_UNCOND_BRANCH_DELAY
2385                      | INSN_COND_BRANCH_DELAY
2386                      | INSN_COND_BRANCH_LIKELY))
2387               /* We do not swap with a trap instruction, since it
2388                  complicates trap handlers to have the trap
2389                  instruction be in a delay slot.  */
2390               || (prev_pinfo & INSN_TRAP)
2391               /* If the branch reads a register that the previous
2392                  instruction sets, we can not swap.  */
2393               || (! mips_opts.mips16
2394                   && (prev_pinfo & INSN_WRITE_GPR_T)
2395                   && insn_uses_reg (ip,
2396                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2397                                      & OP_MASK_RT),
2398                                     MIPS_GR_REG))
2399               || (! mips_opts.mips16
2400                   && (prev_pinfo & INSN_WRITE_GPR_D)
2401                   && insn_uses_reg (ip,
2402                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2403                                      & OP_MASK_RD),
2404                                     MIPS_GR_REG))
2405               || (mips_opts.mips16
2406                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2407                        && insn_uses_reg (ip,
2408                                          ((prev_insn.insn_opcode
2409                                            >> MIPS16OP_SH_RX)
2410                                           & MIPS16OP_MASK_RX),
2411                                          MIPS16_REG))
2412                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2413                           && insn_uses_reg (ip,
2414                                             ((prev_insn.insn_opcode
2415                                               >> MIPS16OP_SH_RY)
2416                                              & MIPS16OP_MASK_RY),
2417                                             MIPS16_REG))
2418                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2419                           && insn_uses_reg (ip,
2420                                             ((prev_insn.insn_opcode
2421                                               >> MIPS16OP_SH_RZ)
2422                                              & MIPS16OP_MASK_RZ),
2423                                             MIPS16_REG))
2424                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2425                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2426                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2427                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2428                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2429                           && insn_uses_reg (ip,
2430                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2431                                                                      insn_opcode),
2432                                             MIPS_GR_REG))))
2433               /* If the branch writes a register that the previous
2434                  instruction sets, we can not swap (we know that
2435                  branches write only to RD or to $31).  */
2436               || (! mips_opts.mips16
2437                   && (prev_pinfo & INSN_WRITE_GPR_T)
2438                   && (((pinfo & INSN_WRITE_GPR_D)
2439                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2440                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2441                       || ((pinfo & INSN_WRITE_GPR_31)
2442                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2443                                & OP_MASK_RT)
2444                               == 31))))
2445               || (! mips_opts.mips16
2446                   && (prev_pinfo & INSN_WRITE_GPR_D)
2447                   && (((pinfo & INSN_WRITE_GPR_D)
2448                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2449                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2450                       || ((pinfo & INSN_WRITE_GPR_31)
2451                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2452                                & OP_MASK_RD)
2453                               == 31))))
2454               || (mips_opts.mips16
2455                   && (pinfo & MIPS16_INSN_WRITE_31)
2456                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2457                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2458                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2459                               == RA))))
2460               /* If the branch writes a register that the previous
2461                  instruction reads, we can not swap (we know that
2462                  branches only write to RD or to $31).  */
2463               || (! mips_opts.mips16
2464                   && (pinfo & INSN_WRITE_GPR_D)
2465                   && insn_uses_reg (&prev_insn,
2466                                     ((ip->insn_opcode >> OP_SH_RD)
2467                                      & OP_MASK_RD),
2468                                     MIPS_GR_REG))
2469               || (! mips_opts.mips16
2470                   && (pinfo & INSN_WRITE_GPR_31)
2471                   && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2472               || (mips_opts.mips16
2473                   && (pinfo & MIPS16_INSN_WRITE_31)
2474                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2475               /* If we are generating embedded PIC code, the branch
2476                  might be expanded into a sequence which uses $at, so
2477                  we can't swap with an instruction which reads it.  */
2478               || (mips_pic == EMBEDDED_PIC
2479                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2480               /* If the previous previous instruction has a load
2481                  delay, and sets a register that the branch reads, we
2482                  can not swap.  */
2483               || (! mips_opts.mips16
2484                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2485               /* Itbl support may require additional care here.  */
2486                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2487                       || (! gpr_interlocks
2488                           && (prev_prev_insn.insn_mo->pinfo
2489                               & INSN_LOAD_MEMORY_DELAY)))
2490                   && insn_uses_reg (ip,
2491                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2492                                      & OP_MASK_RT),
2493                                     MIPS_GR_REG))
2494               /* If one instruction sets a condition code and the
2495                  other one uses a condition code, we can not swap.  */
2496               || ((pinfo & INSN_READ_COND_CODE)
2497                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2498               || ((pinfo & INSN_WRITE_COND_CODE)
2499                   && (prev_pinfo & INSN_READ_COND_CODE))
2500               /* If the previous instruction uses the PC, we can not
2501                  swap.  */
2502               || (mips_opts.mips16
2503                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2504               /* If the previous instruction was extended, we can not
2505                  swap.  */
2506               || (mips_opts.mips16 && prev_insn_extended)
2507               /* If the previous instruction had a fixup in mips16
2508                  mode, we can not swap.  This normally means that the
2509                  previous instruction was a 4 byte branch anyhow.  */
2510               || (mips_opts.mips16 && prev_insn_fixp[0])
2511               /* If the previous instruction is a sync, sync.l, or
2512                  sync.p, we can not swap.  */
2513               || (prev_pinfo & INSN_SYNC))
2514             {
2515               /* We could do even better for unconditional branches to
2516                  portions of this object file; we could pick up the
2517                  instruction at the destination, put it in the delay
2518                  slot, and bump the destination address.  */
2519               emit_nop ();
2520               /* Update the previous insn information.  */
2521               prev_prev_insn = *ip;
2522               prev_insn.insn_mo = &dummy_opcode;
2523             }
2524           else
2525             {
2526               /* It looks like we can actually do the swap.  */
2527               if (! mips_opts.mips16)
2528                 {
2529                   char *prev_f;
2530                   char temp[4];
2531
2532                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2533                   memcpy (temp, prev_f, 4);
2534                   memcpy (prev_f, f, 4);
2535                   memcpy (f, temp, 4);
2536                   if (prev_insn_fixp[0])
2537                     {
2538                       prev_insn_fixp[0]->fx_frag = frag_now;
2539                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2540                     }
2541                   if (prev_insn_fixp[1])
2542                     {
2543                       prev_insn_fixp[1]->fx_frag = frag_now;
2544                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2545                     }
2546                   if (prev_insn_fixp[2])
2547                     {
2548                       prev_insn_fixp[2]->fx_frag = frag_now;
2549                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2550                     }
2551                   if (fixp[0])
2552                     {
2553                       fixp[0]->fx_frag = prev_insn_frag;
2554                       fixp[0]->fx_where = prev_insn_where;
2555                     }
2556                   if (fixp[1])
2557                     {
2558                       fixp[1]->fx_frag = prev_insn_frag;
2559                       fixp[1]->fx_where = prev_insn_where;
2560                     }
2561                   if (fixp[2])
2562                     {
2563                       fixp[2]->fx_frag = prev_insn_frag;
2564                       fixp[2]->fx_where = prev_insn_where;
2565                     }
2566                 }
2567               else
2568                 {
2569                   char *prev_f;
2570                   char temp[2];
2571
2572                   assert (prev_insn_fixp[0] == NULL);
2573                   assert (prev_insn_fixp[1] == NULL);
2574                   assert (prev_insn_fixp[2] == NULL);
2575                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2576                   memcpy (temp, prev_f, 2);
2577                   memcpy (prev_f, f, 2);
2578                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2579                     {
2580                       assert (*reloc_type == BFD_RELOC_UNUSED);
2581                       memcpy (f, temp, 2);
2582                     }
2583                   else
2584                     {
2585                       memcpy (f, f + 2, 2);
2586                       memcpy (f + 2, temp, 2);
2587                     }
2588                   if (fixp[0])
2589                     {
2590                       fixp[0]->fx_frag = prev_insn_frag;
2591                       fixp[0]->fx_where = prev_insn_where;
2592                     }
2593                   if (fixp[1])
2594                     {
2595                       fixp[1]->fx_frag = prev_insn_frag;
2596                       fixp[1]->fx_where = prev_insn_where;
2597                     }
2598                   if (fixp[2])
2599                     {
2600                       fixp[2]->fx_frag = prev_insn_frag;
2601                       fixp[2]->fx_where = prev_insn_where;
2602                     }
2603                 }
2604
2605               /* Update the previous insn information; leave prev_insn
2606                  unchanged.  */
2607               prev_prev_insn = *ip;
2608             }
2609           prev_insn_is_delay_slot = 1;
2610
2611           /* If that was an unconditional branch, forget the previous
2612              insn information.  */
2613           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2614             {
2615               prev_prev_insn.insn_mo = &dummy_opcode;
2616               prev_insn.insn_mo = &dummy_opcode;
2617             }
2618
2619           prev_insn_fixp[0] = NULL;
2620           prev_insn_fixp[1] = NULL;
2621           prev_insn_fixp[2] = NULL;
2622           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2623           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2624           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2625           prev_insn_extended = 0;
2626         }
2627       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2628         {
2629           /* We don't yet optimize a branch likely.  What we should do
2630              is look at the target, copy the instruction found there
2631              into the delay slot, and increment the branch to jump to
2632              the next instruction.  */
2633           emit_nop ();
2634           /* Update the previous insn information.  */
2635           prev_prev_insn = *ip;
2636           prev_insn.insn_mo = &dummy_opcode;
2637           prev_insn_fixp[0] = NULL;
2638           prev_insn_fixp[1] = NULL;
2639           prev_insn_fixp[2] = NULL;
2640           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2641           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2642           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2643           prev_insn_extended = 0;
2644         }
2645       else
2646         {
2647           /* Update the previous insn information.  */
2648           if (nops > 0)
2649             prev_prev_insn.insn_mo = &dummy_opcode;
2650           else
2651             prev_prev_insn = prev_insn;
2652           prev_insn = *ip;
2653
2654           /* Any time we see a branch, we always fill the delay slot
2655              immediately; since this insn is not a branch, we know it
2656              is not in a delay slot.  */
2657           prev_insn_is_delay_slot = 0;
2658
2659           prev_insn_fixp[0] = fixp[0];
2660           prev_insn_fixp[1] = fixp[1];
2661           prev_insn_fixp[2] = fixp[2];
2662           prev_insn_reloc_type[0] = reloc_type[0];
2663           prev_insn_reloc_type[1] = reloc_type[1];
2664           prev_insn_reloc_type[2] = reloc_type[2];
2665           if (mips_opts.mips16)
2666             prev_insn_extended = (ip->use_extend
2667                                   || *reloc_type > BFD_RELOC_UNUSED);
2668         }
2669
2670       prev_prev_insn_unreordered = prev_insn_unreordered;
2671       prev_insn_unreordered = 0;
2672       prev_insn_frag = frag_now;
2673       prev_insn_where = f - frag_now->fr_literal;
2674       prev_insn_valid = 1;
2675     }
2676   else if (place == NULL)
2677     {
2678       /* We need to record a bit of information even when we are not
2679          reordering, in order to determine the base address for mips16
2680          PC relative relocs.  */
2681       prev_prev_insn = prev_insn;
2682       prev_insn = *ip;
2683       prev_insn_reloc_type[0] = reloc_type[0];
2684       prev_insn_reloc_type[1] = reloc_type[1];
2685       prev_insn_reloc_type[2] = reloc_type[2];
2686       prev_prev_insn_unreordered = prev_insn_unreordered;
2687       prev_insn_unreordered = 1;
2688     }
2689
2690   /* We just output an insn, so the next one doesn't have a label.  */
2691   mips_clear_insn_labels ();
2692
2693   /* We must ensure that a fixup associated with an unmatched %hi
2694      reloc does not become a variant frag.  Otherwise, the
2695      rearrangement of %hi relocs in frob_file may confuse
2696      tc_gen_reloc.  */
2697   if (unmatched_hi)
2698     {
2699       frag_wane (frag_now);
2700       frag_new (0);
2701     }
2702 }
2703
2704 /* This function forgets that there was any previous instruction or
2705    label.  If PRESERVE is non-zero, it remembers enough information to
2706    know whether nops are needed before a noreorder section.  */
2707
2708 static void
2709 mips_no_prev_insn (preserve)
2710      int preserve;
2711 {
2712   if (! preserve)
2713     {
2714       prev_insn.insn_mo = &dummy_opcode;
2715       prev_prev_insn.insn_mo = &dummy_opcode;
2716       prev_nop_frag = NULL;
2717       prev_nop_frag_holds = 0;
2718       prev_nop_frag_required = 0;
2719       prev_nop_frag_since = 0;
2720     }
2721   prev_insn_valid = 0;
2722   prev_insn_is_delay_slot = 0;
2723   prev_insn_unreordered = 0;
2724   prev_insn_extended = 0;
2725   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2726   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2727   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2728   prev_prev_insn_unreordered = 0;
2729   mips_clear_insn_labels ();
2730 }
2731
2732 /* This function must be called whenever we turn on noreorder or emit
2733    something other than instructions.  It inserts any NOPS which might
2734    be needed by the previous instruction, and clears the information
2735    kept for the previous instructions.  The INSNS parameter is true if
2736    instructions are to follow.  */
2737
2738 static void
2739 mips_emit_delays (insns)
2740      boolean insns;
2741 {
2742   if (! mips_opts.noreorder)
2743     {
2744       int nops;
2745
2746       nops = 0;
2747       if ((! mips_opts.mips16
2748            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2749            && (! cop_interlocks
2750                && (prev_insn.insn_mo->pinfo
2751                    & (INSN_LOAD_COPROC_DELAY
2752                       | INSN_COPROC_MOVE_DELAY
2753                       | INSN_WRITE_COND_CODE))))
2754           || (! hilo_interlocks
2755               && (prev_insn.insn_mo->pinfo
2756                   & (INSN_READ_LO
2757                      | INSN_READ_HI)))
2758           || (! mips_opts.mips16
2759               && ! gpr_interlocks
2760               && (prev_insn.insn_mo->pinfo
2761                   & INSN_LOAD_MEMORY_DELAY))
2762           || (! mips_opts.mips16
2763               && mips_opts.isa == ISA_MIPS1
2764               && (prev_insn.insn_mo->pinfo
2765                   & INSN_COPROC_MEMORY_DELAY)))
2766         {
2767           /* Itbl support may require additional care here.  */
2768           ++nops;
2769           if ((! mips_opts.mips16
2770                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2771                && (! cop_interlocks
2772                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2773               || (! hilo_interlocks
2774                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2775                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2776             ++nops;
2777
2778           if (prev_insn_unreordered)
2779             nops = 0;
2780         }
2781       else if ((! mips_opts.mips16
2782                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2783                 && (! cop_interlocks
2784                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2785                || (! hilo_interlocks
2786                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2787                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2788         {
2789           /* Itbl support may require additional care here.  */
2790           if (! prev_prev_insn_unreordered)
2791             ++nops;
2792         }
2793
2794       if (nops > 0)
2795         {
2796           struct insn_label_list *l;
2797
2798           if (insns)
2799             {
2800               /* Record the frag which holds the nop instructions, so
2801                  that we can remove them if we don't need them.  */
2802               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2803               prev_nop_frag = frag_now;
2804               prev_nop_frag_holds = nops;
2805               prev_nop_frag_required = 0;
2806               prev_nop_frag_since = 0;
2807             }
2808
2809           for (; nops > 0; --nops)
2810             emit_nop ();
2811
2812           if (insns)
2813             {
2814               /* Move on to a new frag, so that it is safe to simply
2815                  decrease the size of prev_nop_frag.  */
2816               frag_wane (frag_now);
2817               frag_new (0);
2818             }
2819
2820           for (l = insn_labels; l != NULL; l = l->next)
2821             {
2822               valueT val;
2823
2824               assert (S_GET_SEGMENT (l->label) == now_seg);
2825               symbol_set_frag (l->label, frag_now);
2826               val = (valueT) frag_now_fix ();
2827               /* mips16 text labels are stored as odd.  */
2828               if (mips_opts.mips16)
2829                 val += 1;
2830               S_SET_VALUE (l->label, val);
2831             }
2832         }
2833     }
2834
2835   /* Mark instruction labels in mips16 mode.  */
2836   if (mips_opts.mips16 && insns)
2837     mips16_mark_labels ();
2838
2839   mips_no_prev_insn (insns);
2840 }
2841
2842 /* Build an instruction created by a macro expansion.  This is passed
2843    a pointer to the count of instructions created so far, an
2844    expression, the name of the instruction to build, an operand format
2845    string, and corresponding arguments.  */
2846
2847 #ifdef USE_STDARG
2848 static void
2849 macro_build (char *place,
2850              int *counter,
2851              expressionS * ep,
2852              const char *name,
2853              const char *fmt,
2854              ...)
2855 #else
2856 static void
2857 macro_build (place, counter, ep, name, fmt, va_alist)
2858      char *place;
2859      int *counter;
2860      expressionS *ep;
2861      const char *name;
2862      const char *fmt;
2863      va_dcl
2864 #endif
2865 {
2866   struct mips_cl_insn insn;
2867   bfd_reloc_code_real_type r[3];
2868   va_list args;
2869
2870 #ifdef USE_STDARG
2871   va_start (args, fmt);
2872 #else
2873   va_start (args);
2874 #endif
2875
2876   /*
2877    * If the macro is about to expand into a second instruction,
2878    * print a warning if needed. We need to pass ip as a parameter
2879    * to generate a better warning message here...
2880    */
2881   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2882     as_warn (_("Macro instruction expanded into multiple instructions"));
2883
2884   /*
2885    * If the macro is about to expand into a second instruction,
2886    * and it is in a delay slot, print a warning.
2887    */
2888   if (place == NULL
2889       && *counter == 1
2890       && mips_opts.noreorder
2891       && (prev_prev_insn.insn_mo->pinfo
2892           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2893              | INSN_COND_BRANCH_LIKELY)) != 0)
2894     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2895
2896   if (place == NULL)
2897     *counter += 1;              /* bump instruction counter */
2898
2899   if (mips_opts.mips16)
2900     {
2901       mips16_macro_build (place, counter, ep, name, fmt, args);
2902       va_end (args);
2903       return;
2904     }
2905
2906   r[0] = BFD_RELOC_UNUSED;
2907   r[1] = BFD_RELOC_UNUSED;
2908   r[2] = BFD_RELOC_UNUSED;
2909   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2910   assert (insn.insn_mo);
2911   assert (strcmp (name, insn.insn_mo->name) == 0);
2912
2913   /* Search until we get a match for NAME.  */
2914   while (1)
2915     {
2916       /* It is assumed here that macros will never generate
2917          MIPS-3D instructions.  */
2918       if (strcmp (fmt, insn.insn_mo->args) == 0
2919           && insn.insn_mo->pinfo != INSN_MACRO
2920           && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2921           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2922         break;
2923
2924       ++insn.insn_mo;
2925       assert (insn.insn_mo->name);
2926       assert (strcmp (name, insn.insn_mo->name) == 0);
2927     }
2928
2929   insn.insn_opcode = insn.insn_mo->match;
2930   for (;;)
2931     {
2932       switch (*fmt++)
2933         {
2934         case '\0':
2935           break;
2936
2937         case ',':
2938         case '(':
2939         case ')':
2940           continue;
2941
2942         case 't':
2943         case 'w':
2944         case 'E':
2945           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2946           continue;
2947
2948         case 'c':
2949           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2950           continue;
2951
2952         case 'T':
2953         case 'W':
2954           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2955           continue;
2956
2957         case 'd':
2958         case 'G':
2959           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2960           continue;
2961
2962         case 'U':
2963           {
2964             int tmp = va_arg (args, int);
2965
2966             insn.insn_opcode |= tmp << OP_SH_RT;
2967             insn.insn_opcode |= tmp << OP_SH_RD;
2968             continue;
2969           }
2970
2971         case 'V':
2972         case 'S':
2973           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2974           continue;
2975
2976         case 'z':
2977           continue;
2978
2979         case '<':
2980           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2981           continue;
2982
2983         case 'D':
2984           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2985           continue;
2986
2987         case 'B':
2988           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2989           continue;
2990
2991         case 'J':
2992           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2993           continue;
2994
2995         case 'q':
2996           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2997           continue;
2998
2999         case 'b':
3000         case 's':
3001         case 'r':
3002         case 'v':
3003           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3004           continue;
3005
3006         case 'i':
3007         case 'j':
3008         case 'o':
3009           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3010           assert (*r == BFD_RELOC_GPREL16
3011                   || *r == BFD_RELOC_MIPS_LITERAL
3012                   || *r == BFD_RELOC_MIPS_HIGHER
3013                   || *r == BFD_RELOC_HI16_S
3014                   || *r == BFD_RELOC_LO16
3015                   || *r == BFD_RELOC_MIPS_GOT16
3016                   || *r == BFD_RELOC_MIPS_CALL16
3017                   || *r == BFD_RELOC_MIPS_GOT_LO16
3018                   || *r == BFD_RELOC_MIPS_CALL_LO16
3019                   || (ep->X_op == O_subtract
3020                       && *r == BFD_RELOC_PCREL_LO16));
3021           continue;
3022
3023         case 'u':
3024           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3025           assert (ep != NULL
3026                   && (ep->X_op == O_constant
3027                       || (ep->X_op == O_symbol
3028                           && (*r == BFD_RELOC_MIPS_HIGHEST
3029                               || *r == BFD_RELOC_HI16_S
3030                               || *r == BFD_RELOC_HI16
3031                               || *r == BFD_RELOC_GPREL16
3032                               || *r == BFD_RELOC_MIPS_GOT_HI16
3033                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3034                       || (ep->X_op == O_subtract
3035                           && *r == BFD_RELOC_PCREL_HI16_S)));
3036           continue;
3037
3038         case 'p':
3039           assert (ep != NULL);
3040           /*
3041            * This allows macro() to pass an immediate expression for
3042            * creating short branches without creating a symbol.
3043            * Note that the expression still might come from the assembly
3044            * input, in which case the value is not checked for range nor
3045            * is a relocation entry generated (yuck).
3046            */
3047           if (ep->X_op == O_constant)
3048             {
3049               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3050               ep = NULL;
3051             }
3052           else
3053             if (mips_pic == EMBEDDED_PIC)
3054               *r = BFD_RELOC_16_PCREL_S2;
3055             else
3056               *r = BFD_RELOC_16_PCREL;
3057           continue;
3058
3059         case 'a':
3060           assert (ep != NULL);
3061           *r = BFD_RELOC_MIPS_JMP;
3062           continue;
3063
3064         case 'C':
3065           insn.insn_opcode |= va_arg (args, unsigned long);
3066           continue;
3067
3068         default:
3069           internalError ();
3070         }
3071       break;
3072     }
3073   va_end (args);
3074   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3075
3076   append_insn (place, &insn, ep, r, false);
3077 }
3078
3079 static void
3080 mips16_macro_build (place, counter, ep, name, fmt, args)
3081      char *place;
3082      int *counter ATTRIBUTE_UNUSED;
3083      expressionS *ep;
3084      const char *name;
3085      const char *fmt;
3086      va_list args;
3087 {
3088   struct mips_cl_insn insn;
3089   bfd_reloc_code_real_type r[3]
3090     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3091
3092   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3093   assert (insn.insn_mo);
3094   assert (strcmp (name, insn.insn_mo->name) == 0);
3095
3096   while (strcmp (fmt, insn.insn_mo->args) != 0
3097          || insn.insn_mo->pinfo == INSN_MACRO)
3098     {
3099       ++insn.insn_mo;
3100       assert (insn.insn_mo->name);
3101       assert (strcmp (name, insn.insn_mo->name) == 0);
3102     }
3103
3104   insn.insn_opcode = insn.insn_mo->match;
3105   insn.use_extend = false;
3106
3107   for (;;)
3108     {
3109       int c;
3110
3111       c = *fmt++;
3112       switch (c)
3113         {
3114         case '\0':
3115           break;
3116
3117         case ',':
3118         case '(':
3119         case ')':
3120           continue;
3121
3122         case 'y':
3123         case 'w':
3124           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3125           continue;
3126
3127         case 'x':
3128         case 'v':
3129           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3130           continue;
3131
3132         case 'z':
3133           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3134           continue;
3135
3136         case 'Z':
3137           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3138           continue;
3139
3140         case '0':
3141         case 'S':
3142         case 'P':
3143         case 'R':
3144           continue;
3145
3146         case 'X':
3147           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3148           continue;
3149
3150         case 'Y':
3151           {
3152             int regno;
3153
3154             regno = va_arg (args, int);
3155             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3156             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3157           }
3158           continue;
3159
3160         case '<':
3161         case '>':
3162         case '4':
3163         case '5':
3164         case 'H':
3165         case 'W':
3166         case 'D':
3167         case 'j':
3168         case '8':
3169         case 'V':
3170         case 'C':
3171         case 'U':
3172         case 'k':
3173         case 'K':
3174         case 'p':
3175         case 'q':
3176           {
3177             assert (ep != NULL);
3178
3179             if (ep->X_op != O_constant)
3180               *r = (int) BFD_RELOC_UNUSED + c;
3181             else
3182               {
3183                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3184                               false, &insn.insn_opcode, &insn.use_extend,
3185                               &insn.extend);
3186                 ep = NULL;
3187                 *r = BFD_RELOC_UNUSED;
3188               }
3189           }
3190           continue;
3191
3192         case '6':
3193           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3194           continue;
3195         }
3196
3197       break;
3198     }
3199
3200   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3201
3202   append_insn (place, &insn, ep, r, false);
3203 }
3204
3205 /*
3206  * Generate a "lui" instruction.
3207  */
3208 static void
3209 macro_build_lui (place, counter, ep, regnum)
3210      char *place;
3211      int *counter;
3212      expressionS *ep;
3213      int regnum;
3214 {
3215   expressionS high_expr;
3216   struct mips_cl_insn insn;
3217   bfd_reloc_code_real_type r[3]
3218     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3219   CONST char *name = "lui";
3220   CONST char *fmt = "t,u";
3221
3222   assert (! mips_opts.mips16);
3223
3224   if (place == NULL)
3225     high_expr = *ep;
3226   else
3227     {
3228       high_expr.X_op = O_constant;
3229       high_expr.X_add_number = ep->X_add_number;
3230     }
3231
3232   if (high_expr.X_op == O_constant)
3233     {
3234       /* we can compute the instruction now without a relocation entry */
3235       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3236                                 >> 16) & 0xffff;
3237       *r = BFD_RELOC_UNUSED;
3238     }
3239   else if (! HAVE_NEWABI)
3240     {
3241       assert (ep->X_op == O_symbol);
3242       /* _gp_disp is a special case, used from s_cpload.  */
3243       assert (mips_pic == NO_PIC
3244               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3245       *r = BFD_RELOC_HI16_S;
3246     }
3247
3248   /*
3249    * If the macro is about to expand into a second instruction,
3250    * print a warning if needed. We need to pass ip as a parameter
3251    * to generate a better warning message here...
3252    */
3253   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3254     as_warn (_("Macro instruction expanded into multiple instructions"));
3255
3256   if (place == NULL)
3257     *counter += 1;              /* bump instruction counter */
3258
3259   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3260   assert (insn.insn_mo);
3261   assert (strcmp (name, insn.insn_mo->name) == 0);
3262   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3263
3264   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3265   if (*r == BFD_RELOC_UNUSED)
3266     {
3267       insn.insn_opcode |= high_expr.X_add_number;
3268       append_insn (place, &insn, NULL, r, false);
3269     }
3270   else
3271     append_insn (place, &insn, &high_expr, r, false);
3272 }
3273
3274 /*                      set_at()
3275  * Generates code to set the $at register to true (one)
3276  * if reg is less than the immediate expression.
3277  */
3278 static void
3279 set_at (counter, reg, unsignedp)
3280      int *counter;
3281      int reg;
3282      int unsignedp;
3283 {
3284   if (imm_expr.X_op == O_constant
3285       && imm_expr.X_add_number >= -0x8000
3286       && imm_expr.X_add_number < 0x8000)
3287     macro_build ((char *) NULL, counter, &imm_expr,
3288                  unsignedp ? "sltiu" : "slti",
3289                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3290   else
3291     {
3292       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3293       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3294                    unsignedp ? "sltu" : "slt",
3295                    "d,v,t", AT, reg, AT);
3296     }
3297 }
3298
3299 /* Warn if an expression is not a constant.  */
3300
3301 static void
3302 check_absolute_expr (ip, ex)
3303      struct mips_cl_insn *ip;
3304      expressionS *ex;
3305 {
3306   if (ex->X_op == O_big)
3307     as_bad (_("unsupported large constant"));
3308   else if (ex->X_op != O_constant)
3309     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3310 }
3311
3312 /* Count the leading zeroes by performing a binary chop. This is a
3313    bulky bit of source, but performance is a LOT better for the
3314    majority of values than a simple loop to count the bits:
3315        for (lcnt = 0; (lcnt < 32); lcnt++)
3316          if ((v) & (1 << (31 - lcnt)))
3317            break;
3318   However it is not code size friendly, and the gain will drop a bit
3319   on certain cached systems.
3320 */
3321 #define COUNT_TOP_ZEROES(v)             \
3322   (((v) & ~0xffff) == 0                 \
3323    ? ((v) & ~0xff) == 0                 \
3324      ? ((v) & ~0xf) == 0                \
3325        ? ((v) & ~0x3) == 0              \
3326          ? ((v) & ~0x1) == 0            \
3327            ? !(v)                       \
3328              ? 32                       \
3329              : 31                       \
3330            : 30                         \
3331          : ((v) & ~0x7) == 0            \
3332            ? 29                         \
3333            : 28                         \
3334        : ((v) & ~0x3f) == 0             \
3335          ? ((v) & ~0x1f) == 0           \
3336            ? 27                         \
3337            : 26                         \
3338          : ((v) & ~0x7f) == 0           \
3339            ? 25                         \
3340            : 24                         \
3341      : ((v) & ~0xfff) == 0              \
3342        ? ((v) & ~0x3ff) == 0            \
3343          ? ((v) & ~0x1ff) == 0          \
3344            ? 23                         \
3345            : 22                         \
3346          : ((v) & ~0x7ff) == 0          \
3347            ? 21                         \
3348            : 20                         \
3349        : ((v) & ~0x3fff) == 0           \
3350          ? ((v) & ~0x1fff) == 0         \
3351            ? 19                         \
3352            : 18                         \
3353          : ((v) & ~0x7fff) == 0         \
3354            ? 17                         \
3355            : 16                         \
3356    : ((v) & ~0xffffff) == 0             \
3357      ? ((v) & ~0xfffff) == 0            \
3358        ? ((v) & ~0x3ffff) == 0          \
3359          ? ((v) & ~0x1ffff) == 0        \
3360            ? 15                         \
3361            : 14                         \
3362          : ((v) & ~0x7ffff) == 0        \
3363            ? 13                         \
3364            : 12                         \
3365        : ((v) & ~0x3fffff) == 0         \
3366          ? ((v) & ~0x1fffff) == 0       \
3367            ? 11                         \
3368            : 10                         \
3369          : ((v) & ~0x7fffff) == 0       \
3370            ? 9                          \
3371            : 8                          \
3372      : ((v) & ~0xfffffff) == 0          \
3373        ? ((v) & ~0x3ffffff) == 0        \
3374          ? ((v) & ~0x1ffffff) == 0      \
3375            ? 7                          \
3376            : 6                          \
3377          : ((v) & ~0x7ffffff) == 0      \
3378            ? 5                          \
3379            : 4                          \
3380        : ((v) & ~0x3fffffff) == 0       \
3381          ? ((v) & ~0x1fffffff) == 0     \
3382            ? 3                          \
3383            : 2                          \
3384          : ((v) & ~0x7fffffff) == 0     \
3385            ? 1                          \
3386            : 0)
3387
3388 /* Is the given value a sign-extended 32-bit value?  */
3389 #define IS_SEXT_32BIT_NUM(x)                                            \
3390   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3391    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3392
3393 /*                      load_register()
3394  *  This routine generates the least number of instructions neccessary to load
3395  *  an absolute expression value into a register.
3396  */
3397 static void
3398 load_register (counter, reg, ep, dbl)
3399      int *counter;
3400      int reg;
3401      expressionS *ep;
3402      int dbl;
3403 {
3404   int freg;
3405   expressionS hi32, lo32;
3406
3407   if (ep->X_op != O_big)
3408     {
3409       assert (ep->X_op == O_constant);
3410       if (ep->X_add_number < 0x8000
3411           && (ep->X_add_number >= 0
3412               || (ep->X_add_number >= -0x8000
3413                   && (! dbl
3414                       || ! ep->X_unsigned
3415                       || sizeof (ep->X_add_number) > 4))))
3416         {
3417           /* We can handle 16 bit signed values with an addiu to
3418              $zero.  No need to ever use daddiu here, since $zero and
3419              the result are always correct in 32 bit mode.  */
3420           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3421                        (int) BFD_RELOC_LO16);
3422           return;
3423         }
3424       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3425         {
3426           /* We can handle 16 bit unsigned values with an ori to
3427              $zero.  */
3428           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3429                        (int) BFD_RELOC_LO16);
3430           return;
3431         }
3432       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3433                 && (! dbl
3434                     || ! ep->X_unsigned
3435                     || sizeof (ep->X_add_number) > 4
3436                     || (ep->X_add_number & 0x80000000) == 0))
3437                || ((HAVE_32BIT_GPRS || ! dbl)
3438                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3439                || (HAVE_32BIT_GPRS
3440                    && ! dbl
3441                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3442                        == ~ (offsetT) 0xffffffff)))
3443         {
3444           /* 32 bit values require an lui.  */
3445           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3446                        (int) BFD_RELOC_HI16);
3447           if ((ep->X_add_number & 0xffff) != 0)
3448             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3449                          (int) BFD_RELOC_LO16);
3450           return;
3451         }
3452     }
3453
3454   /* The value is larger than 32 bits.  */
3455
3456   if (HAVE_32BIT_GPRS)
3457     {
3458       as_bad (_("Number (0x%lx) larger than 32 bits"),
3459               (unsigned long) ep->X_add_number);
3460       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3461                    (int) BFD_RELOC_LO16);
3462       return;
3463     }
3464
3465   if (ep->X_op != O_big)
3466     {
3467       hi32 = *ep;
3468       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3469       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3470       hi32.X_add_number &= 0xffffffff;
3471       lo32 = *ep;
3472       lo32.X_add_number &= 0xffffffff;
3473     }
3474   else
3475     {
3476       assert (ep->X_add_number > 2);
3477       if (ep->X_add_number == 3)
3478         generic_bignum[3] = 0;
3479       else if (ep->X_add_number > 4)
3480         as_bad (_("Number larger than 64 bits"));
3481       lo32.X_op = O_constant;
3482       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3483       hi32.X_op = O_constant;
3484       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3485     }
3486
3487   if (hi32.X_add_number == 0)
3488     freg = 0;
3489   else
3490     {
3491       int shift, bit;
3492       unsigned long hi, lo;
3493
3494       if (hi32.X_add_number == (offsetT) 0xffffffff)
3495         {
3496           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3497             {
3498               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3499                            reg, 0, (int) BFD_RELOC_LO16);
3500               return;
3501             }
3502           if (lo32.X_add_number & 0x80000000)
3503             {
3504               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3505                            (int) BFD_RELOC_HI16);
3506               if (lo32.X_add_number & 0xffff)
3507                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3508                              reg, reg, (int) BFD_RELOC_LO16);
3509               return;
3510             }
3511         }
3512
3513       /* Check for 16bit shifted constant.  We know that hi32 is
3514          non-zero, so start the mask on the first bit of the hi32
3515          value.  */
3516       shift = 17;
3517       do
3518         {
3519           unsigned long himask, lomask;
3520
3521           if (shift < 32)
3522             {
3523               himask = 0xffff >> (32 - shift);
3524               lomask = (0xffff << shift) & 0xffffffff;
3525             }
3526           else
3527             {
3528               himask = 0xffff << (shift - 32);
3529               lomask = 0;
3530             }
3531           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3532               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3533             {
3534               expressionS tmp;
3535
3536               tmp.X_op = O_constant;
3537               if (shift < 32)
3538                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3539                                     | (lo32.X_add_number >> shift));
3540               else
3541                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3542               macro_build ((char *) NULL, counter, &tmp,
3543                            "ori", "t,r,i", reg, 0,
3544                            (int) BFD_RELOC_LO16);
3545               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3546                            (shift >= 32) ? "dsll32" : "dsll",
3547                            "d,w,<", reg, reg,
3548                            (shift >= 32) ? shift - 32 : shift);
3549               return;
3550             }
3551           shift++;
3552         }
3553       while (shift <= (64 - 16));
3554
3555       /* Find the bit number of the lowest one bit, and store the
3556          shifted value in hi/lo.  */
3557       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3558       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3559       if (lo != 0)
3560         {
3561           bit = 0;
3562           while ((lo & 1) == 0)
3563             {
3564               lo >>= 1;
3565               ++bit;
3566             }
3567           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3568           hi >>= bit;
3569         }
3570       else
3571         {
3572           bit = 32;
3573           while ((hi & 1) == 0)
3574             {
3575               hi >>= 1;
3576               ++bit;
3577             }
3578           lo = hi;
3579           hi = 0;
3580         }
3581
3582       /* Optimize if the shifted value is a (power of 2) - 1.  */
3583       if ((hi == 0 && ((lo + 1) & lo) == 0)
3584           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3585         {
3586           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3587           if (shift != 0)
3588             {
3589               expressionS tmp;
3590
3591               /* This instruction will set the register to be all
3592                  ones.  */
3593               tmp.X_op = O_constant;
3594               tmp.X_add_number = (offsetT) -1;
3595               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3596                            reg, 0, (int) BFD_RELOC_LO16);
3597               if (bit != 0)
3598                 {
3599                   bit += shift;
3600                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3601                                (bit >= 32) ? "dsll32" : "dsll",
3602                                "d,w,<", reg, reg,
3603                                (bit >= 32) ? bit - 32 : bit);
3604                 }
3605               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3606                            (shift >= 32) ? "dsrl32" : "dsrl",
3607                            "d,w,<", reg, reg,
3608                            (shift >= 32) ? shift - 32 : shift);
3609               return;
3610             }
3611         }
3612
3613       /* Sign extend hi32 before calling load_register, because we can
3614          generally get better code when we load a sign extended value.  */
3615       if ((hi32.X_add_number & 0x80000000) != 0)
3616         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3617       load_register (counter, reg, &hi32, 0);
3618       freg = reg;
3619     }
3620   if ((lo32.X_add_number & 0xffff0000) == 0)
3621     {
3622       if (freg != 0)
3623         {
3624           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3625                        "dsll32", "d,w,<", reg, freg, 0);
3626           freg = reg;
3627         }
3628     }
3629   else
3630     {
3631       expressionS mid16;
3632
3633       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3634         {
3635           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3636                        (int) BFD_RELOC_HI16);
3637           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3638                        "dsrl32", "d,w,<", reg, reg, 0);
3639           return;
3640         }
3641
3642       if (freg != 0)
3643         {
3644           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3645                        "d,w,<", reg, freg, 16);
3646           freg = reg;
3647         }
3648       mid16 = lo32;
3649       mid16.X_add_number >>= 16;
3650       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3651                    freg, (int) BFD_RELOC_LO16);
3652       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3653                    "d,w,<", reg, reg, 16);
3654       freg = reg;
3655     }
3656   if ((lo32.X_add_number & 0xffff) != 0)
3657     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3658                  (int) BFD_RELOC_LO16);
3659 }
3660
3661 /* Load an address into a register.  */
3662
3663 static void
3664 load_address (counter, reg, ep, dbl, used_at)
3665      int *counter;
3666      int reg;
3667      expressionS *ep;
3668      int dbl;
3669      int *used_at;
3670 {
3671   char *p;
3672
3673   if (ep->X_op != O_constant
3674       && ep->X_op != O_symbol)
3675     {
3676       as_bad (_("expression too complex"));
3677       ep->X_op = O_constant;
3678     }
3679
3680   if (ep->X_op == O_constant)
3681     {
3682       load_register (counter, reg, ep, dbl);
3683       return;
3684     }
3685
3686   if (mips_pic == NO_PIC)
3687     {
3688       /* If this is a reference to a GP relative symbol, we want
3689            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3690          Otherwise we want
3691            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3692            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3693          If we have an addend, we always use the latter form.
3694
3695          With 64bit address space and a usable $at we want
3696            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3697            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3698            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3699            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3700            dsll32       $reg,0
3701            dadd         $reg,$reg,$at
3702
3703          If $at is already in use, we use an path which is suboptimal
3704          on superscalar processors.
3705            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3706            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3707            dsll         $reg,16
3708            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3709            dsll         $reg,16
3710            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3711        */
3712       if (dbl)
3713         {
3714           p = NULL;
3715
3716           /* We don't do GP optimization for now because RELAX_ENCODE can't
3717              hold the data for such large chunks.  */
3718
3719           if (*used_at == 0)
3720             {
3721               macro_build (p, counter, ep, "lui", "t,u",
3722                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3723               macro_build (p, counter, ep, "lui", "t,u",
3724                            AT, (int) BFD_RELOC_HI16_S);
3725               macro_build (p, counter, ep, "daddiu", "t,r,j",
3726                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3727               macro_build (p, counter, ep, "daddiu", "t,r,j",
3728                            AT, AT, (int) BFD_RELOC_LO16);
3729               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3730                            "d,w,<", reg, reg, 0);
3731               macro_build (p, counter, (expressionS *) NULL, "dadd",
3732                            "d,v,t", reg, reg, AT);
3733               *used_at = 1;
3734             }
3735           else
3736             {
3737               macro_build (p, counter, ep, "lui", "t,u",
3738                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3739               macro_build (p, counter, ep, "daddiu", "t,r,j",
3740                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3741               macro_build (p, counter, (expressionS *) NULL, "dsll",
3742                            "d,w,<", reg, reg, 16);
3743               macro_build (p, counter, ep, "daddiu", "t,r,j",
3744                            reg, reg, (int) BFD_RELOC_HI16_S);
3745               macro_build (p, counter, (expressionS *) NULL, "dsll",
3746                            "d,w,<", reg, reg, 16);
3747               macro_build (p, counter, ep, "daddiu", "t,r,j",
3748                            reg, reg, (int) BFD_RELOC_LO16);
3749             }
3750         }
3751       else
3752         {
3753           p = NULL;
3754           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3755               && ! nopic_need_relax (ep->X_add_symbol, 1))
3756             {
3757               frag_grow (20);
3758               macro_build ((char *) NULL, counter, ep,
3759                            dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3760                            (int) BFD_RELOC_GPREL16);
3761               p = frag_var (rs_machine_dependent, 8, 0,
3762                             RELAX_ENCODE (4, 8, 0, 4, 0,
3763                                           mips_opts.warn_about_macros),
3764                             ep->X_add_symbol, 0, NULL);
3765             }
3766           macro_build_lui (p, counter, ep, reg);
3767           if (p != NULL)
3768             p += 4;
3769           macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3770                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3771         }
3772     }
3773   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3774     {
3775       expressionS ex;
3776
3777       /* If this is a reference to an external symbol, we want
3778            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3779          Otherwise we want
3780            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3781            nop
3782            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3783          If there is a constant, it must be added in after.  */
3784       ex.X_add_number = ep->X_add_number;
3785       ep->X_add_number = 0;
3786       frag_grow (20);
3787       macro_build ((char *) NULL, counter, ep,
3788                    HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3789                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3790       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3791       p = frag_var (rs_machine_dependent, 4, 0,
3792                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3793                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3794       macro_build (p, counter, ep,
3795                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3796                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3797       if (ex.X_add_number != 0)
3798         {
3799           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3800             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3801           ex.X_op = O_constant;
3802           macro_build ((char *) NULL, counter, &ex,
3803                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3804                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3805         }
3806     }
3807   else if (mips_pic == SVR4_PIC)
3808     {
3809       expressionS ex;
3810       int off;
3811
3812       /* This is the large GOT case.  If this is a reference to an
3813          external symbol, we want
3814            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3815            addu         $reg,$reg,$gp
3816            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3817          Otherwise, for a reference to a local symbol, we want
3818            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3819            nop
3820            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3821          If there is a constant, it must be added in after.  */
3822       ex.X_add_number = ep->X_add_number;
3823       ep->X_add_number = 0;
3824       if (reg_needs_delay (GP))
3825         off = 4;
3826       else
3827         off = 0;
3828       frag_grow (32);
3829       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3830                    (int) BFD_RELOC_MIPS_GOT_HI16);
3831       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3832                    dbl ? "daddu" : "addu", "d,v,t", reg, reg, GP);
3833       macro_build ((char *) NULL, counter, ep, dbl ? "ld" : "lw",
3834                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3835       p = frag_var (rs_machine_dependent, 12 + off, 0,
3836                     RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3837                                   mips_opts.warn_about_macros),
3838                     ep->X_add_symbol, 0, NULL);
3839       if (off > 0)
3840         {
3841           /* We need a nop before loading from $gp.  This special
3842              check is required because the lui which starts the main
3843              instruction stream does not refer to $gp, and so will not
3844              insert the nop which may be required.  */
3845           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3846           p += 4;
3847         }
3848       macro_build (p, counter, ep, dbl ? "ld" : "lw",
3849                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3850       p += 4;
3851       macro_build (p, counter, (expressionS *) NULL, "nop", "");
3852       p += 4;
3853       macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3854                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3855       if (ex.X_add_number != 0)
3856         {
3857           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3858             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3859           ex.X_op = O_constant;
3860           macro_build ((char *) NULL, counter, &ex, dbl ? "daddiu" : "addiu",
3861                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3862         }
3863     }
3864   else if (mips_pic == EMBEDDED_PIC)
3865     {
3866       /* We always do
3867            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3868          */
3869       macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3870                    "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
3871     }
3872   else
3873     abort ();
3874 }
3875
3876 /* Move the contents of register SOURCE into register DEST.  */
3877
3878 static void
3879 move_register (counter, dest, source)
3880      int *counter;
3881      int dest;
3882      int source;
3883 {
3884   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3885                HAVE_32BIT_GPRS ? "addu" : "daddu",
3886                "d,v,t", dest, source, 0);
3887 }
3888
3889 /*
3890  *                      Build macros
3891  *   This routine implements the seemingly endless macro or synthesized
3892  * instructions and addressing modes in the mips assembly language. Many
3893  * of these macros are simple and are similar to each other. These could
3894  * probably be handled by some kind of table or grammer aproach instead of
3895  * this verbose method. Others are not simple macros but are more like
3896  * optimizing code generation.
3897  *   One interesting optimization is when several store macros appear
3898  * consecutivly that would load AT with the upper half of the same address.
3899  * The ensuing load upper instructions are ommited. This implies some kind
3900  * of global optimization. We currently only optimize within a single macro.
3901  *   For many of the load and store macros if the address is specified as a
3902  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3903  * first load register 'at' with zero and use it as the base register. The
3904  * mips assembler simply uses register $zero. Just one tiny optimization
3905  * we're missing.
3906  */
3907 static void
3908 macro (ip)
3909      struct mips_cl_insn *ip;
3910 {
3911   register int treg, sreg, dreg, breg;
3912   int tempreg;
3913   int mask;
3914   int icnt = 0;
3915   int used_at = 0;
3916   expressionS expr1;
3917   const char *s;
3918   const char *s2;
3919   const char *fmt;
3920   int likely = 0;
3921   int dbl = 0;
3922   int coproc = 0;
3923   int lr = 0;
3924   int imm = 0;
3925   offsetT maxnum;
3926   int off;
3927   bfd_reloc_code_real_type r;
3928   char *p;
3929   int hold_mips_optimize;
3930
3931   assert (! mips_opts.mips16);
3932
3933   treg = (ip->insn_opcode >> 16) & 0x1f;
3934   dreg = (ip->insn_opcode >> 11) & 0x1f;
3935   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3936   mask = ip->insn_mo->mask;
3937
3938   expr1.X_op = O_constant;
3939   expr1.X_op_symbol = NULL;
3940   expr1.X_add_symbol = NULL;
3941   expr1.X_add_number = 1;
3942
3943   switch (mask)
3944     {
3945     case M_DABS:
3946       dbl = 1;
3947     case M_ABS:
3948       /* bgez $a0,.+12
3949          move v0,$a0
3950          sub v0,$zero,$a0
3951          */
3952
3953       mips_emit_delays (true);
3954       ++mips_opts.noreorder;
3955       mips_any_noreorder = 1;
3956
3957       expr1.X_add_number = 8;
3958       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3959       if (dreg == sreg)
3960         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3961                      0);
3962       else
3963         move_register (&icnt, dreg, sreg);
3964       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3965                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3966
3967       --mips_opts.noreorder;
3968       return;
3969
3970     case M_ADD_I:
3971       s = "addi";
3972       s2 = "add";
3973       goto do_addi;
3974     case M_ADDU_I:
3975       s = "addiu";
3976       s2 = "addu";
3977       goto do_addi;
3978     case M_DADD_I:
3979       dbl = 1;
3980       s = "daddi";
3981       s2 = "dadd";
3982       goto do_addi;
3983     case M_DADDU_I:
3984       dbl = 1;
3985       s = "daddiu";
3986       s2 = "daddu";
3987     do_addi:
3988       if (imm_expr.X_op == O_constant
3989           && imm_expr.X_add_number >= -0x8000
3990           && imm_expr.X_add_number < 0x8000)
3991         {
3992           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3993                        (int) BFD_RELOC_LO16);
3994           return;
3995         }
3996       load_register (&icnt, AT, &imm_expr, dbl);
3997       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3998                    treg, sreg, AT);
3999       break;
4000
4001     case M_AND_I:
4002       s = "andi";
4003       s2 = "and";
4004       goto do_bit;
4005     case M_OR_I:
4006       s = "ori";
4007       s2 = "or";
4008       goto do_bit;
4009     case M_NOR_I:
4010       s = "";
4011       s2 = "nor";
4012       goto do_bit;
4013     case M_XOR_I:
4014       s = "xori";
4015       s2 = "xor";
4016     do_bit:
4017       if (imm_expr.X_op == O_constant
4018           && imm_expr.X_add_number >= 0
4019           && imm_expr.X_add_number < 0x10000)
4020         {
4021           if (mask != M_NOR_I)
4022             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4023                          sreg, (int) BFD_RELOC_LO16);
4024           else
4025             {
4026               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4027                            treg, sreg, (int) BFD_RELOC_LO16);
4028               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4029                            "d,v,t", treg, treg, 0);
4030             }
4031           return;
4032         }
4033
4034       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4035       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4036                    treg, sreg, AT);
4037       break;
4038
4039     case M_BEQ_I:
4040       s = "beq";
4041       goto beq_i;
4042     case M_BEQL_I:
4043       s = "beql";
4044       likely = 1;
4045       goto beq_i;
4046     case M_BNE_I:
4047       s = "bne";
4048       goto beq_i;
4049     case M_BNEL_I:
4050       s = "bnel";
4051       likely = 1;
4052     beq_i:
4053       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4054         {
4055           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4056                        0);
4057           return;
4058         }
4059       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4060       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4061       break;
4062
4063     case M_BGEL:
4064       likely = 1;
4065     case M_BGE:
4066       if (treg == 0)
4067         {
4068           macro_build ((char *) NULL, &icnt, &offset_expr,
4069                        likely ? "bgezl" : "bgez", "s,p", sreg);
4070           return;
4071         }
4072       if (sreg == 0)
4073         {
4074           macro_build ((char *) NULL, &icnt, &offset_expr,
4075                        likely ? "blezl" : "blez", "s,p", treg);
4076           return;
4077         }
4078       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4079                    AT, sreg, treg);
4080       macro_build ((char *) NULL, &icnt, &offset_expr,
4081                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4082       break;
4083
4084     case M_BGTL_I:
4085       likely = 1;
4086     case M_BGT_I:
4087       /* check for > max integer */
4088       maxnum = 0x7fffffff;
4089       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4090         {
4091           maxnum <<= 16;
4092           maxnum |= 0xffff;
4093           maxnum <<= 16;
4094           maxnum |= 0xffff;
4095         }
4096       if (imm_expr.X_op == O_constant
4097           && imm_expr.X_add_number >= maxnum
4098           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4099         {
4100         do_false:
4101           /* result is always false */
4102           if (! likely)
4103             {
4104               if (warn_nops)
4105                 as_warn (_("Branch %s is always false (nop)"),
4106                          ip->insn_mo->name);
4107               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4108                            "", 0);
4109             }
4110           else
4111             {
4112               if (warn_nops)
4113                 as_warn (_("Branch likely %s is always false"),
4114                          ip->insn_mo->name);
4115               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4116                            "s,t,p", 0, 0);
4117             }
4118           return;
4119         }
4120       if (imm_expr.X_op != O_constant)
4121         as_bad (_("Unsupported large constant"));
4122       imm_expr.X_add_number++;
4123       /* FALLTHROUGH */
4124     case M_BGE_I:
4125     case M_BGEL_I:
4126       if (mask == M_BGEL_I)
4127         likely = 1;
4128       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4129         {
4130           macro_build ((char *) NULL, &icnt, &offset_expr,
4131                        likely ? "bgezl" : "bgez", "s,p", sreg);
4132           return;
4133         }
4134       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4135         {
4136           macro_build ((char *) NULL, &icnt, &offset_expr,
4137                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4138           return;
4139         }
4140       maxnum = 0x7fffffff;
4141       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4142         {
4143           maxnum <<= 16;
4144           maxnum |= 0xffff;
4145           maxnum <<= 16;
4146           maxnum |= 0xffff;
4147         }
4148       maxnum = - maxnum - 1;
4149       if (imm_expr.X_op == O_constant
4150           && imm_expr.X_add_number <= maxnum
4151           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4152         {
4153         do_true:
4154           /* result is always true */
4155           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4156           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4157           return;
4158         }
4159       set_at (&icnt, sreg, 0);
4160       macro_build ((char *) NULL, &icnt, &offset_expr,
4161                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4162       break;
4163
4164     case M_BGEUL:
4165       likely = 1;
4166     case M_BGEU:
4167       if (treg == 0)
4168         goto do_true;
4169       if (sreg == 0)
4170         {
4171           macro_build ((char *) NULL, &icnt, &offset_expr,
4172                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4173           return;
4174         }
4175       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4176                    "d,v,t", AT, sreg, treg);
4177       macro_build ((char *) NULL, &icnt, &offset_expr,
4178                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4179       break;
4180
4181     case M_BGTUL_I:
4182       likely = 1;
4183     case M_BGTU_I:
4184       if (sreg == 0
4185           || (HAVE_32BIT_GPRS
4186               && imm_expr.X_op == O_constant
4187               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4188         goto do_false;
4189       if (imm_expr.X_op != O_constant)
4190         as_bad (_("Unsupported large constant"));
4191       imm_expr.X_add_number++;
4192       /* FALLTHROUGH */
4193     case M_BGEU_I:
4194     case M_BGEUL_I:
4195       if (mask == M_BGEUL_I)
4196         likely = 1;
4197       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4198         goto do_true;
4199       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4200         {
4201           macro_build ((char *) NULL, &icnt, &offset_expr,
4202                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4203           return;
4204         }
4205       set_at (&icnt, sreg, 1);
4206       macro_build ((char *) NULL, &icnt, &offset_expr,
4207                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4208       break;
4209
4210     case M_BGTL:
4211       likely = 1;
4212     case M_BGT:
4213       if (treg == 0)
4214         {
4215           macro_build ((char *) NULL, &icnt, &offset_expr,
4216                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4217           return;
4218         }
4219       if (sreg == 0)
4220         {
4221           macro_build ((char *) NULL, &icnt, &offset_expr,
4222                        likely ? "bltzl" : "bltz", "s,p", treg);
4223           return;
4224         }
4225       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4226                    AT, treg, sreg);
4227       macro_build ((char *) NULL, &icnt, &offset_expr,
4228                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4229       break;
4230
4231     case M_BGTUL:
4232       likely = 1;
4233     case M_BGTU:
4234       if (treg == 0)
4235         {
4236           macro_build ((char *) NULL, &icnt, &offset_expr,
4237                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4238           return;
4239         }
4240       if (sreg == 0)
4241         goto do_false;
4242       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4243                    "d,v,t", AT, treg, sreg);
4244       macro_build ((char *) NULL, &icnt, &offset_expr,
4245                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4246       break;
4247
4248     case M_BLEL:
4249       likely = 1;
4250     case M_BLE:
4251       if (treg == 0)
4252         {
4253           macro_build ((char *) NULL, &icnt, &offset_expr,
4254                        likely ? "blezl" : "blez", "s,p", sreg);
4255           return;
4256         }
4257       if (sreg == 0)
4258         {
4259           macro_build ((char *) NULL, &icnt, &offset_expr,
4260                        likely ? "bgezl" : "bgez", "s,p", treg);
4261           return;
4262         }
4263       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4264                    AT, treg, sreg);
4265       macro_build ((char *) NULL, &icnt, &offset_expr,
4266                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4267       break;
4268
4269     case M_BLEL_I:
4270       likely = 1;
4271     case M_BLE_I:
4272       maxnum = 0x7fffffff;
4273       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4274         {
4275           maxnum <<= 16;
4276           maxnum |= 0xffff;
4277           maxnum <<= 16;
4278           maxnum |= 0xffff;
4279         }
4280       if (imm_expr.X_op == O_constant
4281           && imm_expr.X_add_number >= maxnum
4282           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4283         goto do_true;
4284       if (imm_expr.X_op != O_constant)
4285         as_bad (_("Unsupported large constant"));
4286       imm_expr.X_add_number++;
4287       /* FALLTHROUGH */
4288     case M_BLT_I:
4289     case M_BLTL_I:
4290       if (mask == M_BLTL_I)
4291         likely = 1;
4292       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4293         {
4294           macro_build ((char *) NULL, &icnt, &offset_expr,
4295                        likely ? "bltzl" : "bltz", "s,p", sreg);
4296           return;
4297         }
4298       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4299         {
4300           macro_build ((char *) NULL, &icnt, &offset_expr,
4301                        likely ? "blezl" : "blez", "s,p", sreg);
4302           return;
4303         }
4304       set_at (&icnt, sreg, 0);
4305       macro_build ((char *) NULL, &icnt, &offset_expr,
4306                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4307       break;
4308
4309     case M_BLEUL:
4310       likely = 1;
4311     case M_BLEU:
4312       if (treg == 0)
4313         {
4314           macro_build ((char *) NULL, &icnt, &offset_expr,
4315                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4316           return;
4317         }
4318       if (sreg == 0)
4319         goto do_true;
4320       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4321                    "d,v,t", AT, treg, sreg);
4322       macro_build ((char *) NULL, &icnt, &offset_expr,
4323                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4324       break;
4325
4326     case M_BLEUL_I:
4327       likely = 1;
4328     case M_BLEU_I:
4329       if (sreg == 0
4330           || (HAVE_32BIT_GPRS
4331               && imm_expr.X_op == O_constant
4332               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4333         goto do_true;
4334       if (imm_expr.X_op != O_constant)
4335         as_bad (_("Unsupported large constant"));
4336       imm_expr.X_add_number++;
4337       /* FALLTHROUGH */
4338     case M_BLTU_I:
4339     case M_BLTUL_I:
4340       if (mask == M_BLTUL_I)
4341         likely = 1;
4342       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4343         goto do_false;
4344       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4345         {
4346           macro_build ((char *) NULL, &icnt, &offset_expr,
4347                        likely ? "beql" : "beq",
4348                        "s,t,p", sreg, 0);
4349           return;
4350         }
4351       set_at (&icnt, sreg, 1);
4352       macro_build ((char *) NULL, &icnt, &offset_expr,
4353                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4354       break;
4355
4356     case M_BLTL:
4357       likely = 1;
4358     case M_BLT:
4359       if (treg == 0)
4360         {
4361           macro_build ((char *) NULL, &icnt, &offset_expr,
4362                        likely ? "bltzl" : "bltz", "s,p", sreg);
4363           return;
4364         }
4365       if (sreg == 0)
4366         {
4367           macro_build ((char *) NULL, &icnt, &offset_expr,
4368                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4369           return;
4370         }
4371       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4372                    AT, sreg, treg);
4373       macro_build ((char *) NULL, &icnt, &offset_expr,
4374                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4375       break;
4376
4377     case M_BLTUL:
4378       likely = 1;
4379     case M_BLTU:
4380       if (treg == 0)
4381         goto do_false;
4382       if (sreg == 0)
4383         {
4384           macro_build ((char *) NULL, &icnt, &offset_expr,
4385                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4386           return;
4387         }
4388       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4389                    "d,v,t", AT, sreg,
4390                    treg);
4391       macro_build ((char *) NULL, &icnt, &offset_expr,
4392                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4393       break;
4394
4395     case M_DDIV_3:
4396       dbl = 1;
4397     case M_DIV_3:
4398       s = "mflo";
4399       goto do_div3;
4400     case M_DREM_3:
4401       dbl = 1;
4402     case M_REM_3:
4403       s = "mfhi";
4404     do_div3:
4405       if (treg == 0)
4406         {
4407           as_warn (_("Divide by zero."));
4408           if (mips_trap)
4409             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4410                          "s,t", 0, 0);
4411           else
4412             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4413                          "c", 7);
4414           return;
4415         }
4416
4417       mips_emit_delays (true);
4418       ++mips_opts.noreorder;
4419       mips_any_noreorder = 1;
4420       if (mips_trap)
4421         {
4422           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4423                        "s,t", treg, 0);
4424           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4425                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4426         }
4427       else
4428         {
4429           expr1.X_add_number = 8;
4430           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4431           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4432                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4433           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4434                        "c", 7);
4435         }
4436       expr1.X_add_number = -1;
4437       macro_build ((char *) NULL, &icnt, &expr1,
4438                    dbl ? "daddiu" : "addiu",
4439                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4440       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4441       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4442       if (dbl)
4443         {
4444           expr1.X_add_number = 1;
4445           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4446                        (int) BFD_RELOC_LO16);
4447           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4448                        "d,w,<", AT, AT, 31);
4449         }
4450       else
4451         {
4452           expr1.X_add_number = 0x80000000;
4453           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4454                        (int) BFD_RELOC_HI16);
4455         }
4456       if (mips_trap)
4457         {
4458           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4459                        "s,t", sreg, AT);
4460           /* We want to close the noreorder block as soon as possible, so
4461              that later insns are available for delay slot filling.  */
4462           --mips_opts.noreorder;
4463         }
4464       else
4465         {
4466           expr1.X_add_number = 8;
4467           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4468           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4469                        0);
4470
4471           /* We want to close the noreorder block as soon as possible, so
4472              that later insns are available for delay slot filling.  */
4473           --mips_opts.noreorder;
4474
4475           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4476                        "c", 6);
4477         }
4478       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4479       break;
4480
4481     case M_DIV_3I:
4482       s = "div";
4483       s2 = "mflo";
4484       goto do_divi;
4485     case M_DIVU_3I:
4486       s = "divu";
4487       s2 = "mflo";
4488       goto do_divi;
4489     case M_REM_3I:
4490       s = "div";
4491       s2 = "mfhi";
4492       goto do_divi;
4493     case M_REMU_3I:
4494       s = "divu";
4495       s2 = "mfhi";
4496       goto do_divi;
4497     case M_DDIV_3I:
4498       dbl = 1;
4499       s = "ddiv";
4500       s2 = "mflo";
4501       goto do_divi;
4502     case M_DDIVU_3I:
4503       dbl = 1;
4504       s = "ddivu";
4505       s2 = "mflo";
4506       goto do_divi;
4507     case M_DREM_3I:
4508       dbl = 1;
4509       s = "ddiv";
4510       s2 = "mfhi";
4511       goto do_divi;
4512     case M_DREMU_3I:
4513       dbl = 1;
4514       s = "ddivu";
4515       s2 = "mfhi";
4516     do_divi:
4517       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4518         {
4519           as_warn (_("Divide by zero."));
4520           if (mips_trap)
4521             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4522                          "s,t", 0, 0);
4523           else
4524             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4525                          "c", 7);
4526           return;
4527         }
4528       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4529         {
4530           if (strcmp (s2, "mflo") == 0)
4531             move_register (&icnt, dreg, sreg);
4532           else
4533             move_register (&icnt, dreg, 0);
4534           return;
4535         }
4536       if (imm_expr.X_op == O_constant
4537           && imm_expr.X_add_number == -1
4538           && s[strlen (s) - 1] != 'u')
4539         {
4540           if (strcmp (s2, "mflo") == 0)
4541             {
4542               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4543                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4544             }
4545           else
4546             move_register (&icnt, dreg, 0);
4547           return;
4548         }
4549
4550       load_register (&icnt, AT, &imm_expr, dbl);
4551       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4552                    sreg, AT);
4553       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4554       break;
4555
4556     case M_DIVU_3:
4557       s = "divu";
4558       s2 = "mflo";
4559       goto do_divu3;
4560     case M_REMU_3:
4561       s = "divu";
4562       s2 = "mfhi";
4563       goto do_divu3;
4564     case M_DDIVU_3:
4565       s = "ddivu";
4566       s2 = "mflo";
4567       goto do_divu3;
4568     case M_DREMU_3:
4569       s = "ddivu";
4570       s2 = "mfhi";
4571     do_divu3:
4572       mips_emit_delays (true);
4573       ++mips_opts.noreorder;
4574       mips_any_noreorder = 1;
4575       if (mips_trap)
4576         {
4577           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4578                        "s,t", treg, 0);
4579           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4580                        sreg, treg);
4581           /* We want to close the noreorder block as soon as possible, so
4582              that later insns are available for delay slot filling.  */
4583           --mips_opts.noreorder;
4584         }
4585       else
4586         {
4587           expr1.X_add_number = 8;
4588           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4589           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4590                        sreg, treg);
4591
4592           /* We want to close the noreorder block as soon as possible, so
4593              that later insns are available for delay slot filling.  */
4594           --mips_opts.noreorder;
4595           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4596                        "c", 7);
4597         }
4598       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4599       return;
4600
4601     case M_DLA_AB:
4602       dbl = 1;
4603     case M_LA_AB:
4604       /* Load the address of a symbol into a register.  If breg is not
4605          zero, we then add a base register to it.  */
4606
4607       if (treg == breg)
4608         {
4609           tempreg = AT;
4610           used_at = 1;
4611         }
4612       else
4613         {
4614           tempreg = treg;
4615           used_at = 0;
4616         }
4617
4618       /* When generating embedded PIC code, we permit expressions of
4619          the form
4620            la   $treg,foo-bar
4621            la   $treg,foo-bar($breg)
4622          where bar is an address in the current section.  These are used
4623          when getting the addresses of functions.  We don't permit
4624          X_add_number to be non-zero, because if the symbol is
4625          external the relaxing code needs to know that any addend is
4626          purely the offset to X_op_symbol.  */
4627       if (mips_pic == EMBEDDED_PIC
4628           && offset_expr.X_op == O_subtract
4629           && (symbol_constant_p (offset_expr.X_op_symbol)
4630               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4631               : (symbol_equated_p (offset_expr.X_op_symbol)
4632                  && (S_GET_SEGMENT
4633                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4634                       ->X_add_symbol)
4635                      == now_seg)))
4636           && (offset_expr.X_add_number == 0
4637               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4638         {
4639           if (breg == 0)
4640             {
4641               tempreg = treg;
4642               used_at = 0;
4643               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4644                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4645             }
4646           else
4647             {
4648               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4649                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4650               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4651                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4652                            "d,v,t", tempreg, tempreg, breg);
4653             }
4654           macro_build ((char *) NULL, &icnt, &offset_expr,
4655                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4656                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4657           if (! used_at)
4658             return;
4659           break;
4660         }
4661
4662       if (offset_expr.X_op != O_symbol
4663           && offset_expr.X_op != O_constant)
4664         {
4665           as_bad (_("expression too complex"));
4666           offset_expr.X_op = O_constant;
4667         }
4668
4669       if (offset_expr.X_op == O_constant)
4670         load_register (&icnt, tempreg, &offset_expr,
4671                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4672                         ? (dbl || HAVE_64BIT_ADDRESSES)
4673                         : HAVE_64BIT_ADDRESSES));
4674       else if (mips_pic == NO_PIC)
4675         {
4676           /* If this is a reference to a GP relative symbol, we want
4677                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4678              Otherwise we want
4679                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4680                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4681              If we have a constant, we need two instructions anyhow,
4682              so we may as well always use the latter form.
4683
4684             With 64bit address space and a usable $at we want
4685               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4686               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4687               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4688               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4689               dsll32    $tempreg,0
4690               dadd      $tempreg,$tempreg,$at
4691
4692             If $at is already in use, we use an path which is suboptimal
4693             on superscalar processors.
4694               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4695               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4696               dsll      $tempreg,16
4697               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4698               dsll      $tempreg,16
4699               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4700           */
4701           p = NULL;
4702           if (HAVE_64BIT_ADDRESSES)
4703             {
4704               /* We don't do GP optimization for now because RELAX_ENCODE can't
4705                  hold the data for such large chunks.  */
4706
4707               if (used_at == 0)
4708                 {
4709                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4710                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4711                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4712                                AT, (int) BFD_RELOC_HI16_S);
4713                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4714                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4715                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4716                                AT, AT, (int) BFD_RELOC_LO16);
4717                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4718                                "d,w,<", tempreg, tempreg, 0);
4719                   macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4720                                tempreg, tempreg, AT);
4721                   used_at = 1;
4722                 }
4723               else
4724                 {
4725                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4726                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4727                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4728                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4729                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4730                                tempreg, tempreg, 16);
4731                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4732                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4733                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4734                                tempreg, tempreg, 16);
4735                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4736                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4737                 }
4738             }
4739           else
4740             {
4741               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4742                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4743                 {
4744                   frag_grow (20);
4745                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4746                                "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4747                   p = frag_var (rs_machine_dependent, 8, 0,
4748                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4749                                               mips_opts.warn_about_macros),
4750                                 offset_expr.X_add_symbol, 0, NULL);
4751                 }
4752               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4753               if (p != NULL)
4754                 p += 4;
4755               macro_build (p, &icnt, &offset_expr, "addiu",
4756                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4757             }
4758         }
4759       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4760         {
4761           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4762
4763           /* If this is a reference to an external symbol, and there
4764              is no constant, we want
4765                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4766              or if tempreg is PIC_CALL_REG
4767                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4768              For a local symbol, we want
4769                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4770                nop
4771                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4772
4773              If we have a small constant, and this is a reference to
4774              an external symbol, we want
4775                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4776                nop
4777                addiu    $tempreg,$tempreg,<constant>
4778              For a local symbol, we want the same instruction
4779              sequence, but we output a BFD_RELOC_LO16 reloc on the
4780              addiu instruction.
4781
4782              If we have a large constant, and this is a reference to
4783              an external symbol, we want
4784                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4785                lui      $at,<hiconstant>
4786                addiu    $at,$at,<loconstant>
4787                addu     $tempreg,$tempreg,$at
4788              For a local symbol, we want the same instruction
4789              sequence, but we output a BFD_RELOC_LO16 reloc on the
4790              addiu instruction.  */
4791           expr1.X_add_number = offset_expr.X_add_number;
4792           offset_expr.X_add_number = 0;
4793           frag_grow (32);
4794           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4795             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4796           macro_build ((char *) NULL, &icnt, &offset_expr,
4797                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4798                        "t,o(b)", tempreg, lw_reloc_type, GP);
4799           if (expr1.X_add_number == 0)
4800             {
4801               int off;
4802
4803               if (breg == 0)
4804                 off = 0;
4805               else
4806                 {
4807                   /* We're going to put in an addu instruction using
4808                      tempreg, so we may as well insert the nop right
4809                      now.  */
4810                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4811                                "nop", "");
4812                   off = 4;
4813                 }
4814               p = frag_var (rs_machine_dependent, 8 - off, 0,
4815                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4816                                           (breg == 0
4817                                            ? mips_opts.warn_about_macros
4818                                            : 0)),
4819                             offset_expr.X_add_symbol, 0, NULL);
4820               if (breg == 0)
4821                 {
4822                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4823                   p += 4;
4824                 }
4825               macro_build (p, &icnt, &expr1,
4826                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4827                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4828               /* FIXME: If breg == 0, and the next instruction uses
4829                  $tempreg, then if this variant case is used an extra
4830                  nop will be generated.  */
4831             }
4832           else if (expr1.X_add_number >= -0x8000
4833                    && expr1.X_add_number < 0x8000)
4834             {
4835               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4836                            "nop", "");
4837               macro_build ((char *) NULL, &icnt, &expr1,
4838                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4839                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4840               frag_var (rs_machine_dependent, 0, 0,
4841                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4842                         offset_expr.X_add_symbol, 0, NULL);
4843             }
4844           else
4845             {
4846               int off1;
4847
4848               /* If we are going to add in a base register, and the
4849                  target register and the base register are the same,
4850                  then we are using AT as a temporary register.  Since
4851                  we want to load the constant into AT, we add our
4852                  current AT (from the global offset table) and the
4853                  register into the register now, and pretend we were
4854                  not using a base register.  */
4855               if (breg != treg)
4856                 off1 = 0;
4857               else
4858                 {
4859                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4860                                "nop", "");
4861                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4862                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4863                                "d,v,t", treg, AT, breg);
4864                   breg = 0;
4865                   tempreg = treg;
4866                   off1 = -8;
4867                 }
4868
4869               /* Set mips_optimize around the lui instruction to avoid
4870                  inserting an unnecessary nop after the lw.  */
4871               hold_mips_optimize = mips_optimize;
4872               mips_optimize = 2;
4873               macro_build_lui (NULL, &icnt, &expr1, AT);
4874               mips_optimize = hold_mips_optimize;
4875
4876               macro_build ((char *) NULL, &icnt, &expr1,
4877                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4878                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4879               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4880                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4881                            "d,v,t", tempreg, tempreg, AT);
4882               frag_var (rs_machine_dependent, 0, 0,
4883                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4884                         offset_expr.X_add_symbol, 0, NULL);
4885               used_at = 1;
4886             }
4887         }
4888       else if (mips_pic == SVR4_PIC)
4889         {
4890           int gpdel;
4891           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4892           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4893
4894           /* This is the large GOT case.  If this is a reference to an
4895              external symbol, and there is no constant, we want
4896                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4897                addu     $tempreg,$tempreg,$gp
4898                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4899              or if tempreg is PIC_CALL_REG
4900                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4901                addu     $tempreg,$tempreg,$gp
4902                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4903              For a local symbol, we want
4904                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4905                nop
4906                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4907
4908              If we have a small constant, and this is a reference to
4909              an external symbol, we want
4910                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4911                addu     $tempreg,$tempreg,$gp
4912                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4913                nop
4914                addiu    $tempreg,$tempreg,<constant>
4915              For a local symbol, we want
4916                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4917                nop
4918                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4919
4920              If we have a large constant, and this is a reference to
4921              an external symbol, we want
4922                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4923                addu     $tempreg,$tempreg,$gp
4924                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4925                lui      $at,<hiconstant>
4926                addiu    $at,$at,<loconstant>
4927                addu     $tempreg,$tempreg,$at
4928              For a local symbol, we want
4929                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4930                lui      $at,<hiconstant>
4931                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4932                addu     $tempreg,$tempreg,$at
4933              */
4934           expr1.X_add_number = offset_expr.X_add_number;
4935           offset_expr.X_add_number = 0;
4936           frag_grow (52);
4937           if (reg_needs_delay (GP))
4938             gpdel = 4;
4939           else
4940             gpdel = 0;
4941           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4942             {
4943               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4944               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4945             }
4946           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4947                        tempreg, lui_reloc_type);
4948           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4949                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4950                        "d,v,t", tempreg, tempreg, GP);
4951           macro_build ((char *) NULL, &icnt, &offset_expr,
4952                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4953                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4954           if (expr1.X_add_number == 0)
4955             {
4956               int off;
4957
4958               if (breg == 0)
4959                 off = 0;
4960               else
4961                 {
4962                   /* We're going to put in an addu instruction using
4963                      tempreg, so we may as well insert the nop right
4964                      now.  */
4965                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4966                                "nop", "");
4967                   off = 4;
4968                 }
4969
4970               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4971                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4972                                           8 + gpdel, 0,
4973                                           (breg == 0
4974                                            ? mips_opts.warn_about_macros
4975                                            : 0)),
4976                             offset_expr.X_add_symbol, 0, NULL);
4977             }
4978           else if (expr1.X_add_number >= -0x8000
4979                    && expr1.X_add_number < 0x8000)
4980             {
4981               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4982                            "nop", "");
4983               macro_build ((char *) NULL, &icnt, &expr1,
4984                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4985                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4986
4987               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4988                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4989                                           (breg == 0
4990                                            ? mips_opts.warn_about_macros
4991                                            : 0)),
4992                             offset_expr.X_add_symbol, 0, NULL);
4993             }
4994           else
4995             {
4996               int adj, dreg;
4997
4998               /* If we are going to add in a base register, and the
4999                  target register and the base register are the same,
5000                  then we are using AT as a temporary register.  Since
5001                  we want to load the constant into AT, we add our
5002                  current AT (from the global offset table) and the
5003                  register into the register now, and pretend we were
5004                  not using a base register.  */
5005               if (breg != treg)
5006                 {
5007                   adj = 0;
5008                   dreg = tempreg;
5009                 }
5010               else
5011                 {
5012                   assert (tempreg == AT);
5013                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5014                                "nop", "");
5015                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5016                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5017                                "d,v,t", treg, AT, breg);
5018                   dreg = treg;
5019                   adj = 8;
5020                 }
5021
5022               /* Set mips_optimize around the lui instruction to avoid
5023                  inserting an unnecessary nop after the lw.  */
5024               hold_mips_optimize = mips_optimize;
5025               mips_optimize = 2;
5026               macro_build_lui (NULL, &icnt, &expr1, AT);
5027               mips_optimize = hold_mips_optimize;
5028
5029               macro_build ((char *) NULL, &icnt, &expr1,
5030                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5031                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5032               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5033                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5034                            "d,v,t", dreg, dreg, AT);
5035
5036               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5037                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5038                                           8 + gpdel, 0,
5039                                           (breg == 0
5040                                            ? mips_opts.warn_about_macros
5041                                            : 0)),
5042                             offset_expr.X_add_symbol, 0, NULL);
5043
5044               used_at = 1;
5045             }
5046
5047           if (gpdel > 0)
5048             {
5049               /* This is needed because this instruction uses $gp, but
5050                  the first instruction on the main stream does not.  */
5051               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5052               p += 4;
5053             }
5054           macro_build (p, &icnt, &offset_expr,
5055                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5056                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5057           p += 4;
5058           if (expr1.X_add_number >= -0x8000
5059               && expr1.X_add_number < 0x8000)
5060             {
5061               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5062               p += 4;
5063               macro_build (p, &icnt, &expr1,
5064                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5065                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5066               /* FIXME: If add_number is 0, and there was no base
5067                  register, the external symbol case ended with a load,
5068                  so if the symbol turns out to not be external, and
5069                  the next instruction uses tempreg, an unnecessary nop
5070                  will be inserted.  */
5071             }
5072           else
5073             {
5074               if (breg == treg)
5075                 {
5076                   /* We must add in the base register now, as in the
5077                      external symbol case.  */
5078                   assert (tempreg == AT);
5079                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5080                   p += 4;
5081                   macro_build (p, &icnt, (expressionS *) NULL,
5082                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5083                                "d,v,t", treg, AT, breg);
5084                   p += 4;
5085                   tempreg = treg;
5086                   /* We set breg to 0 because we have arranged to add
5087                      it in in both cases.  */
5088                   breg = 0;
5089                 }
5090
5091               macro_build_lui (p, &icnt, &expr1, AT);
5092               p += 4;
5093               macro_build (p, &icnt, &expr1,
5094                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5095                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5096               p += 4;
5097               macro_build (p, &icnt, (expressionS *) NULL,
5098                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5099                            "d,v,t", tempreg, tempreg, AT);
5100               p += 4;
5101             }
5102         }
5103       else if (mips_pic == EMBEDDED_PIC)
5104         {
5105           /* We use
5106                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5107              */
5108           macro_build ((char *) NULL, &icnt, &offset_expr,
5109                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5110                        "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
5111         }
5112       else
5113         abort ();
5114
5115       if (breg != 0)
5116         {
5117           char *s;
5118
5119           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5120             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5121           else
5122             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5123
5124           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5125                        "d,v,t", treg, tempreg, breg);
5126         }
5127
5128       if (! used_at)
5129         return;
5130
5131       break;
5132
5133     case M_J_A:
5134       /* The j instruction may not be used in PIC code, since it
5135          requires an absolute address.  We convert it to a b
5136          instruction.  */
5137       if (mips_pic == NO_PIC)
5138         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5139       else
5140         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5141       return;
5142
5143       /* The jal instructions must be handled as macros because when
5144          generating PIC code they expand to multi-instruction
5145          sequences.  Normally they are simple instructions.  */
5146     case M_JAL_1:
5147       dreg = RA;
5148       /* Fall through.  */
5149     case M_JAL_2:
5150       if (mips_pic == NO_PIC
5151           || mips_pic == EMBEDDED_PIC)
5152         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5153                      "d,s", dreg, sreg);
5154       else if (mips_pic == SVR4_PIC)
5155         {
5156           if (sreg != PIC_CALL_REG)
5157             as_warn (_("MIPS PIC call to register other than $25"));
5158
5159           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5160                        "d,s", dreg, sreg);
5161           if (! HAVE_NEWABI)
5162             {
5163               if (mips_cprestore_offset < 0)
5164                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5165               else
5166                 {
5167                   if (! mips_frame_reg_valid)
5168                     {
5169                       as_warn (_("No .frame pseudo-op used in PIC code"));
5170                       /* Quiet this warning.  */
5171                       mips_frame_reg_valid = 1;
5172                     }
5173                   if (! mips_cprestore_valid)
5174                     {
5175                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5176                       /* Quiet this warning.  */
5177                       mips_cprestore_valid = 1;
5178                     }
5179                   expr1.X_add_number = mips_cprestore_offset;
5180                   macro_build ((char *) NULL, &icnt, &expr1,
5181                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5182                                GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5183                 }
5184             }
5185         }
5186       else
5187         abort ();
5188
5189       return;
5190
5191     case M_JAL_A:
5192       if (mips_pic == NO_PIC)
5193         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5194       else if (mips_pic == SVR4_PIC)
5195         {
5196           /* If this is a reference to an external symbol, and we are
5197              using a small GOT, we want
5198                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5199                nop
5200                jalr     $25
5201                nop
5202                lw       $gp,cprestore($sp)
5203              The cprestore value is set using the .cprestore
5204              pseudo-op.  If we are using a big GOT, we want
5205                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5206                addu     $25,$25,$gp
5207                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5208                nop
5209                jalr     $25
5210                nop
5211                lw       $gp,cprestore($sp)
5212              If the symbol is not external, we want
5213                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5214                nop
5215                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5216                jalr     $25
5217                nop
5218                lw $gp,cprestore($sp) */
5219           frag_grow (40);
5220           if (! mips_big_got)
5221             {
5222               macro_build ((char *) NULL, &icnt, &offset_expr,
5223                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5224                            "t,o(b)", PIC_CALL_REG,
5225                            (int) BFD_RELOC_MIPS_CALL16, GP);
5226               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5227                            "nop", "");
5228               p = frag_var (rs_machine_dependent, 4, 0,
5229                             RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5230                             offset_expr.X_add_symbol, 0, NULL);
5231             }
5232           else
5233             {
5234               int gpdel;
5235
5236               if (reg_needs_delay (GP))
5237                 gpdel = 4;
5238               else
5239                 gpdel = 0;
5240               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5241                            PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5242               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5243                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5244                            "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5245               macro_build ((char *) NULL, &icnt, &offset_expr,
5246                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5247                            "t,o(b)", PIC_CALL_REG,
5248                            (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5249               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5250                            "nop", "");
5251               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5252                             RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5253                                           0, 0),
5254                             offset_expr.X_add_symbol, 0, NULL);
5255               if (gpdel > 0)
5256                 {
5257                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5258                   p += 4;
5259                 }
5260               macro_build (p, &icnt, &offset_expr,
5261                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5262                            "t,o(b)", PIC_CALL_REG,
5263                            (int) BFD_RELOC_MIPS_GOT16, GP);
5264               p += 4;
5265               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5266               p += 4;
5267             }
5268           macro_build (p, &icnt, &offset_expr,
5269                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5270                        "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5271                        (int) BFD_RELOC_LO16);
5272           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5273                        "jalr", "s", PIC_CALL_REG);
5274           if (! HAVE_NEWABI)
5275             {
5276               if (mips_cprestore_offset < 0)
5277                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5278               else
5279                 {
5280                   if (! mips_frame_reg_valid)
5281                     {
5282                       as_warn (_("No .frame pseudo-op used in PIC code"));
5283                       /* Quiet this warning.  */
5284                       mips_frame_reg_valid = 1;
5285                     }
5286                   if (! mips_cprestore_valid)
5287                     {
5288                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5289                       /* Quiet this warning.  */
5290                       mips_cprestore_valid = 1;
5291                     }
5292                   if (mips_opts.noreorder)
5293                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5294                                  "nop", "");
5295                   expr1.X_add_number = mips_cprestore_offset;
5296                   macro_build ((char *) NULL, &icnt, &expr1,
5297                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5298                                GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5299                 }
5300             }
5301         }
5302       else if (mips_pic == EMBEDDED_PIC)
5303         {
5304           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5305           /* The linker may expand the call to a longer sequence which
5306              uses $at, so we must break rather than return.  */
5307           break;
5308         }
5309       else
5310         abort ();
5311
5312       return;
5313
5314     case M_LB_AB:
5315       s = "lb";
5316       goto ld;
5317     case M_LBU_AB:
5318       s = "lbu";
5319       goto ld;
5320     case M_LH_AB:
5321       s = "lh";
5322       goto ld;
5323     case M_LHU_AB:
5324       s = "lhu";
5325       goto ld;
5326     case M_LW_AB:
5327       s = "lw";
5328       goto ld;
5329     case M_LWC0_AB:
5330       s = "lwc0";
5331       /* Itbl support may require additional care here.  */
5332       coproc = 1;
5333       goto ld;
5334     case M_LWC1_AB:
5335       s = "lwc1";
5336       /* Itbl support may require additional care here.  */
5337       coproc = 1;
5338       goto ld;
5339     case M_LWC2_AB:
5340       s = "lwc2";
5341       /* Itbl support may require additional care here.  */
5342       coproc = 1;
5343       goto ld;
5344     case M_LWC3_AB:
5345       s = "lwc3";
5346       /* Itbl support may require additional care here.  */
5347       coproc = 1;
5348       goto ld;
5349     case M_LWL_AB:
5350       s = "lwl";
5351       lr = 1;
5352       goto ld;
5353     case M_LWR_AB:
5354       s = "lwr";
5355       lr = 1;
5356       goto ld;
5357     case M_LDC1_AB:
5358       if (mips_arch == CPU_R4650)
5359         {
5360           as_bad (_("opcode not supported on this processor"));
5361           return;
5362         }
5363       s = "ldc1";
5364       /* Itbl support may require additional care here.  */
5365       coproc = 1;
5366       goto ld;
5367     case M_LDC2_AB:
5368       s = "ldc2";
5369       /* Itbl support may require additional care here.  */
5370       coproc = 1;
5371       goto ld;
5372     case M_LDC3_AB:
5373       s = "ldc3";
5374       /* Itbl support may require additional care here.  */
5375       coproc = 1;
5376       goto ld;
5377     case M_LDL_AB:
5378       s = "ldl";
5379       lr = 1;
5380       goto ld;
5381     case M_LDR_AB:
5382       s = "ldr";
5383       lr = 1;
5384       goto ld;
5385     case M_LL_AB:
5386       s = "ll";
5387       goto ld;
5388     case M_LLD_AB:
5389       s = "lld";
5390       goto ld;
5391     case M_LWU_AB:
5392       s = "lwu";
5393     ld:
5394       if (breg == treg || coproc || lr)
5395         {
5396           tempreg = AT;
5397           used_at = 1;
5398         }
5399       else
5400         {
5401           tempreg = treg;
5402           used_at = 0;
5403         }
5404       goto ld_st;
5405     case M_SB_AB:
5406       s = "sb";
5407       goto st;
5408     case M_SH_AB:
5409       s = "sh";
5410       goto st;
5411     case M_SW_AB:
5412       s = "sw";
5413       goto st;
5414     case M_SWC0_AB:
5415       s = "swc0";
5416       /* Itbl support may require additional care here.  */
5417       coproc = 1;
5418       goto st;
5419     case M_SWC1_AB:
5420       s = "swc1";
5421       /* Itbl support may require additional care here.  */
5422       coproc = 1;
5423       goto st;
5424     case M_SWC2_AB:
5425       s = "swc2";
5426       /* Itbl support may require additional care here.  */
5427       coproc = 1;
5428       goto st;
5429     case M_SWC3_AB:
5430       s = "swc3";
5431       /* Itbl support may require additional care here.  */
5432       coproc = 1;
5433       goto st;
5434     case M_SWL_AB:
5435       s = "swl";
5436       goto st;
5437     case M_SWR_AB:
5438       s = "swr";
5439       goto st;
5440     case M_SC_AB:
5441       s = "sc";
5442       goto st;
5443     case M_SCD_AB:
5444       s = "scd";
5445       goto st;
5446     case M_SDC1_AB:
5447       if (mips_arch == CPU_R4650)
5448         {
5449           as_bad (_("opcode not supported on this processor"));
5450           return;
5451         }
5452       s = "sdc1";
5453       coproc = 1;
5454       /* Itbl support may require additional care here.  */
5455       goto st;
5456     case M_SDC2_AB:
5457       s = "sdc2";
5458       /* Itbl support may require additional care here.  */
5459       coproc = 1;
5460       goto st;
5461     case M_SDC3_AB:
5462       s = "sdc3";
5463       /* Itbl support may require additional care here.  */
5464       coproc = 1;
5465       goto st;
5466     case M_SDL_AB:
5467       s = "sdl";
5468       goto st;
5469     case M_SDR_AB:
5470       s = "sdr";
5471     st:
5472       tempreg = AT;
5473       used_at = 1;
5474     ld_st:
5475       /* Itbl support may require additional care here.  */
5476       if (mask == M_LWC1_AB
5477           || mask == M_SWC1_AB
5478           || mask == M_LDC1_AB
5479           || mask == M_SDC1_AB
5480           || mask == M_L_DAB
5481           || mask == M_S_DAB)
5482         fmt = "T,o(b)";
5483       else if (coproc)
5484         fmt = "E,o(b)";
5485       else
5486         fmt = "t,o(b)";
5487
5488       /* For embedded PIC, we allow loads where the offset is calculated
5489          by subtracting a symbol in the current segment from an unknown
5490          symbol, relative to a base register, e.g.:
5491                 <op>    $treg, <sym>-<localsym>($breg)
5492          This is used by the compiler for switch statements.  */
5493       if (mips_pic == EMBEDDED_PIC
5494           && offset_expr.X_op == O_subtract
5495           && (symbol_constant_p (offset_expr.X_op_symbol)
5496               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5497               : (symbol_equated_p (offset_expr.X_op_symbol)
5498                  && (S_GET_SEGMENT
5499                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5500                       ->X_add_symbol)
5501                      == now_seg)))
5502           && breg != 0
5503           && (offset_expr.X_add_number == 0
5504               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5505         {
5506           /* For this case, we output the instructions:
5507                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5508                 addiu   $tempreg,$tempreg,$breg
5509                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5510              If the relocation would fit entirely in 16 bits, it would be
5511              nice to emit:
5512                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5513              instead, but that seems quite difficult.  */
5514           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5515                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5516           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5517                        ((bfd_arch_bits_per_address (stdoutput) == 32
5518                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5519                         ? "addu" : "daddu"),
5520                        "d,v,t", tempreg, tempreg, breg);
5521           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5522                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5523           if (! used_at)
5524             return;
5525           break;
5526         }
5527
5528       if (offset_expr.X_op != O_constant
5529           && offset_expr.X_op != O_symbol)
5530         {
5531           as_bad (_("expression too complex"));
5532           offset_expr.X_op = O_constant;
5533         }
5534
5535       /* A constant expression in PIC code can be handled just as it
5536          is in non PIC code.  */
5537       if (mips_pic == NO_PIC
5538           || offset_expr.X_op == O_constant)
5539         {
5540           /* If this is a reference to a GP relative symbol, and there
5541              is no base register, we want
5542                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5543              Otherwise, if there is no base register, we want
5544                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5545                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5546              If we have a constant, we need two instructions anyhow,
5547              so we always use the latter form.
5548
5549              If we have a base register, and this is a reference to a
5550              GP relative symbol, we want
5551                addu     $tempreg,$breg,$gp
5552                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5553              Otherwise we want
5554                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5555                addu     $tempreg,$tempreg,$breg
5556                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5557              With a constant we always use the latter case.
5558
5559              With 64bit address space and no base register and $at usable,
5560              we want
5561                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5562                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5563                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5564                dsll32   $tempreg,0
5565                daddu    $tempreg,$at
5566                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5567              If we have a base register, we want
5568                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5569                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5570                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5571                daddu    $at,$breg
5572                dsll32   $tempreg,0
5573                daddu    $tempreg,$at
5574                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5575
5576              Without $at we can't generate the optimal path for superscalar
5577              processors here since this would require two temporary registers.
5578                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5579                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5580                dsll     $tempreg,16
5581                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5582                dsll     $tempreg,16
5583                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5584              If we have a base register, we want
5585                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5586                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5587                dsll     $tempreg,16
5588                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5589                dsll     $tempreg,16
5590                daddu    $tempreg,$tempreg,$breg
5591                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5592
5593              If we have 64-bit addresses, as an optimization, for
5594              addresses which are 32-bit constants (e.g. kseg0/kseg1
5595              addresses) we fall back to the 32-bit address generation
5596              mechanism since it is more efficient.  This code should
5597              probably attempt to generate 64-bit constants more
5598              efficiently in general.
5599            */
5600           if (HAVE_64BIT_ADDRESSES
5601               && !(offset_expr.X_op == O_constant
5602                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5603             {
5604               p = NULL;
5605
5606               /* We don't do GP optimization for now because RELAX_ENCODE can't
5607                  hold the data for such large chunks.  */
5608
5609               if (used_at == 0)
5610                 {
5611                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5612                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5613                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5614                                AT, (int) BFD_RELOC_HI16_S);
5615                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5616                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5617                   if (breg != 0)
5618                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5619                                  "d,v,t", AT, AT, breg);
5620                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5621                                "d,w,<", tempreg, tempreg, 0);
5622                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5623                                "d,v,t", tempreg, tempreg, AT);
5624                   macro_build (p, &icnt, &offset_expr, s,
5625                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5626                   used_at = 1;
5627                 }
5628               else
5629                 {
5630                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5631                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5632                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5633                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5634                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5635                                "d,w,<", tempreg, tempreg, 16);
5636                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5637                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5638                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5639                                "d,w,<", tempreg, tempreg, 16);
5640                   if (breg != 0)
5641                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5642                                  "d,v,t", tempreg, tempreg, breg);
5643                   macro_build (p, &icnt, &offset_expr, s,
5644                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5645                 }
5646
5647               return;
5648             }
5649
5650           if (breg == 0)
5651             {
5652               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5653                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5654                 p = NULL;
5655               else
5656                 {
5657                   frag_grow (20);
5658                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5659                                treg, (int) BFD_RELOC_GPREL16, GP);
5660                   p = frag_var (rs_machine_dependent, 8, 0,
5661                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5662                                               (mips_opts.warn_about_macros
5663                                                || (used_at
5664                                                    && mips_opts.noat))),
5665                                 offset_expr.X_add_symbol, 0, NULL);
5666                   used_at = 0;
5667                 }
5668               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5669               if (p != NULL)
5670                 p += 4;
5671               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5672                            (int) BFD_RELOC_LO16, tempreg);
5673             }
5674           else
5675             {
5676               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5677                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5678                 p = NULL;
5679               else
5680                 {
5681                   frag_grow (28);
5682                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5683                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5684                                "d,v,t", tempreg, breg, GP);
5685                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5686                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5687                   p = frag_var (rs_machine_dependent, 12, 0,
5688                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5689                                 offset_expr.X_add_symbol, 0, NULL);
5690                 }
5691               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5692               if (p != NULL)
5693                 p += 4;
5694               macro_build (p, &icnt, (expressionS *) NULL,
5695                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5696                            "d,v,t", tempreg, tempreg, breg);
5697               if (p != NULL)
5698                 p += 4;
5699               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5700                            (int) BFD_RELOC_LO16, tempreg);
5701             }
5702         }
5703       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5704         {
5705           /* If this is a reference to an external symbol, we want
5706                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5707                nop
5708                <op>     $treg,0($tempreg)
5709              Otherwise we want
5710                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5711                nop
5712                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5713                <op>     $treg,0($tempreg)
5714              If there is a base register, we add it to $tempreg before
5715              the <op>.  If there is a constant, we stick it in the
5716              <op> instruction.  We don't handle constants larger than
5717              16 bits, because we have no way to load the upper 16 bits
5718              (actually, we could handle them for the subset of cases
5719              in which we are not using $at).  */
5720           assert (offset_expr.X_op == O_symbol);
5721           expr1.X_add_number = offset_expr.X_add_number;
5722           offset_expr.X_add_number = 0;
5723           if (expr1.X_add_number < -0x8000
5724               || expr1.X_add_number >= 0x8000)
5725             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5726           frag_grow (20);
5727           macro_build ((char *) NULL, &icnt, &offset_expr,
5728                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5729                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5730           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5731           p = frag_var (rs_machine_dependent, 4, 0,
5732                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5733                         offset_expr.X_add_symbol, 0, NULL);
5734           macro_build (p, &icnt, &offset_expr,
5735                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5736                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5737           if (breg != 0)
5738             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5739                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5740                          "d,v,t", tempreg, tempreg, breg);
5741           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5742                        (int) BFD_RELOC_LO16, tempreg);
5743         }
5744       else if (mips_pic == SVR4_PIC)
5745         {
5746           int gpdel;
5747
5748           /* If this is a reference to an external symbol, we want
5749                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5750                addu     $tempreg,$tempreg,$gp
5751                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5752                <op>     $treg,0($tempreg)
5753              Otherwise we want
5754                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5755                nop
5756                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5757                <op>     $treg,0($tempreg)
5758              If there is a base register, we add it to $tempreg before
5759              the <op>.  If there is a constant, we stick it in the
5760              <op> instruction.  We don't handle constants larger than
5761              16 bits, because we have no way to load the upper 16 bits
5762              (actually, we could handle them for the subset of cases
5763              in which we are not using $at).  */
5764           assert (offset_expr.X_op == O_symbol);
5765           expr1.X_add_number = offset_expr.X_add_number;
5766           offset_expr.X_add_number = 0;
5767           if (expr1.X_add_number < -0x8000
5768               || expr1.X_add_number >= 0x8000)
5769             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5770           if (reg_needs_delay (GP))
5771             gpdel = 4;
5772           else
5773             gpdel = 0;
5774           frag_grow (36);
5775           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5776                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5777           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5778                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5779                        "d,v,t", tempreg, tempreg, GP);
5780           macro_build ((char *) NULL, &icnt, &offset_expr,
5781                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5782                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5783                        tempreg);
5784           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5785                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5786                         offset_expr.X_add_symbol, 0, NULL);
5787           if (gpdel > 0)
5788             {
5789               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5790               p += 4;
5791             }
5792           macro_build (p, &icnt, &offset_expr,
5793                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5794                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5795           p += 4;
5796           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5797           p += 4;
5798           macro_build (p, &icnt, &offset_expr,
5799                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5800                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5801           if (breg != 0)
5802             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5803                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5804                          "d,v,t", tempreg, tempreg, breg);
5805           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5806                        (int) BFD_RELOC_LO16, tempreg);
5807         }
5808       else if (mips_pic == EMBEDDED_PIC)
5809         {
5810           /* If there is no base register, we want
5811                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5812              If there is a base register, we want
5813                addu     $tempreg,$breg,$gp
5814                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5815              */
5816           assert (offset_expr.X_op == O_symbol);
5817           if (breg == 0)
5818             {
5819               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5820                            treg, (int) BFD_RELOC_GPREL16, GP);
5821               used_at = 0;
5822             }
5823           else
5824             {
5825               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5826                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5827                            "d,v,t", tempreg, breg, GP);
5828               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5829                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5830             }
5831         }
5832       else
5833         abort ();
5834
5835       if (! used_at)
5836         return;
5837
5838       break;
5839
5840     case M_LI:
5841     case M_LI_S:
5842       load_register (&icnt, treg, &imm_expr, 0);
5843       return;
5844
5845     case M_DLI:
5846       load_register (&icnt, treg, &imm_expr, 1);
5847       return;
5848
5849     case M_LI_SS:
5850       if (imm_expr.X_op == O_constant)
5851         {
5852           load_register (&icnt, AT, &imm_expr, 0);
5853           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5854                        "mtc1", "t,G", AT, treg);
5855           break;
5856         }
5857       else
5858         {
5859           assert (offset_expr.X_op == O_symbol
5860                   && strcmp (segment_name (S_GET_SEGMENT
5861                                            (offset_expr.X_add_symbol)),
5862                              ".lit4") == 0
5863                   && offset_expr.X_add_number == 0);
5864           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5865                        treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5866           return;
5867         }
5868
5869     case M_LI_D:
5870       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5871          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5872          order 32 bits of the value and the low order 32 bits are either
5873          zero or in OFFSET_EXPR.  */
5874       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5875         {
5876           if (HAVE_64BIT_GPRS)
5877             load_register (&icnt, treg, &imm_expr, 1);
5878           else
5879             {
5880               int hreg, lreg;
5881
5882               if (target_big_endian)
5883                 {
5884                   hreg = treg;
5885                   lreg = treg + 1;
5886                 }
5887               else
5888                 {
5889                   hreg = treg + 1;
5890                   lreg = treg;
5891                 }
5892
5893               if (hreg <= 31)
5894                 load_register (&icnt, hreg, &imm_expr, 0);
5895               if (lreg <= 31)
5896                 {
5897                   if (offset_expr.X_op == O_absent)
5898                     move_register (&icnt, lreg, 0);
5899                   else
5900                     {
5901                       assert (offset_expr.X_op == O_constant);
5902                       load_register (&icnt, lreg, &offset_expr, 0);
5903                     }
5904                 }
5905             }
5906           return;
5907         }
5908
5909       /* We know that sym is in the .rdata section.  First we get the
5910          upper 16 bits of the address.  */
5911       if (mips_pic == NO_PIC)
5912         {
5913           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5914         }
5915       else if (mips_pic == SVR4_PIC)
5916         {
5917           macro_build ((char *) NULL, &icnt, &offset_expr,
5918                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5919                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5920         }
5921       else if (mips_pic == EMBEDDED_PIC)
5922         {
5923           /* For embedded PIC we pick up the entire address off $gp in
5924              a single instruction.  */
5925           macro_build ((char *) NULL, &icnt, &offset_expr,
5926                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5927                        "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5928           offset_expr.X_op = O_constant;
5929           offset_expr.X_add_number = 0;
5930         }
5931       else
5932         abort ();
5933
5934       /* Now we load the register(s).  */
5935       if (HAVE_64BIT_GPRS)
5936         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5937                      treg, (int) BFD_RELOC_LO16, AT);
5938       else
5939         {
5940           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5941                        treg, (int) BFD_RELOC_LO16, AT);
5942           if (treg != 31)
5943             {
5944               /* FIXME: How in the world do we deal with the possible
5945                  overflow here?  */
5946               offset_expr.X_add_number += 4;
5947               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5948                            treg + 1, (int) BFD_RELOC_LO16, AT);
5949             }
5950         }
5951
5952       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5953          does not become a variant frag.  */
5954       frag_wane (frag_now);
5955       frag_new (0);
5956
5957       break;
5958
5959     case M_LI_DD:
5960       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5961          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5962          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5963          the value and the low order 32 bits are either zero or in
5964          OFFSET_EXPR.  */
5965       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5966         {
5967           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5968           if (HAVE_64BIT_FPRS)
5969             {
5970               assert (HAVE_64BIT_GPRS);
5971               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5972                            "dmtc1", "t,S", AT, treg);
5973             }
5974           else
5975             {
5976               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5977                            "mtc1", "t,G", AT, treg + 1);
5978               if (offset_expr.X_op == O_absent)
5979                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5980                              "mtc1", "t,G", 0, treg);
5981               else
5982                 {
5983                   assert (offset_expr.X_op == O_constant);
5984                   load_register (&icnt, AT, &offset_expr, 0);
5985                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5986                                "mtc1", "t,G", AT, treg);
5987                 }
5988             }
5989           break;
5990         }
5991
5992       assert (offset_expr.X_op == O_symbol
5993               && offset_expr.X_add_number == 0);
5994       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5995       if (strcmp (s, ".lit8") == 0)
5996         {
5997           if (mips_opts.isa != ISA_MIPS1)
5998             {
5999               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6000                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
6001               return;
6002             }
6003           breg = GP;
6004           r = BFD_RELOC_MIPS_LITERAL;
6005           goto dob;
6006         }
6007       else
6008         {
6009           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6010           if (mips_pic == SVR4_PIC)
6011             macro_build ((char *) NULL, &icnt, &offset_expr,
6012                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6013                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6014           else
6015             {
6016               /* FIXME: This won't work for a 64 bit address.  */
6017               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6018             }
6019
6020           if (mips_opts.isa != ISA_MIPS1)
6021             {
6022               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6023                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6024
6025               /* To avoid confusion in tc_gen_reloc, we must ensure
6026                  that this does not become a variant frag.  */
6027               frag_wane (frag_now);
6028               frag_new (0);
6029
6030               break;
6031             }
6032           breg = AT;
6033           r = BFD_RELOC_LO16;
6034           goto dob;
6035         }
6036
6037     case M_L_DOB:
6038       if (mips_arch == CPU_R4650)
6039         {
6040           as_bad (_("opcode not supported on this processor"));
6041           return;
6042         }
6043       /* Even on a big endian machine $fn comes before $fn+1.  We have
6044          to adjust when loading from memory.  */
6045       r = BFD_RELOC_LO16;
6046     dob:
6047       assert (mips_opts.isa == ISA_MIPS1);
6048       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6049                    target_big_endian ? treg + 1 : treg,
6050                    (int) r, breg);
6051       /* FIXME: A possible overflow which I don't know how to deal
6052          with.  */
6053       offset_expr.X_add_number += 4;
6054       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6055                    target_big_endian ? treg : treg + 1,
6056                    (int) r, breg);
6057
6058       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6059          does not become a variant frag.  */
6060       frag_wane (frag_now);
6061       frag_new (0);
6062
6063       if (breg != AT)
6064         return;
6065       break;
6066
6067     case M_L_DAB:
6068       /*
6069        * The MIPS assembler seems to check for X_add_number not
6070        * being double aligned and generating:
6071        *        lui     at,%hi(foo+1)
6072        *        addu    at,at,v1
6073        *        addiu   at,at,%lo(foo+1)
6074        *        lwc1    f2,0(at)
6075        *        lwc1    f3,4(at)
6076        * But, the resulting address is the same after relocation so why
6077        * generate the extra instruction?
6078        */
6079       if (mips_arch == CPU_R4650)
6080         {
6081           as_bad (_("opcode not supported on this processor"));
6082           return;
6083         }
6084       /* Itbl support may require additional care here.  */
6085       coproc = 1;
6086       if (mips_opts.isa != ISA_MIPS1)
6087         {
6088           s = "ldc1";
6089           goto ld;
6090         }
6091
6092       s = "lwc1";
6093       fmt = "T,o(b)";
6094       goto ldd_std;
6095
6096     case M_S_DAB:
6097       if (mips_arch == CPU_R4650)
6098         {
6099           as_bad (_("opcode not supported on this processor"));
6100           return;
6101         }
6102
6103       if (mips_opts.isa != ISA_MIPS1)
6104         {
6105           s = "sdc1";
6106           goto st;
6107         }
6108
6109       s = "swc1";
6110       fmt = "T,o(b)";
6111       /* Itbl support may require additional care here.  */
6112       coproc = 1;
6113       goto ldd_std;
6114
6115     case M_LD_AB:
6116       if (HAVE_64BIT_GPRS)
6117         {
6118           s = "ld";
6119           goto ld;
6120         }
6121
6122       s = "lw";
6123       fmt = "t,o(b)";
6124       goto ldd_std;
6125
6126     case M_SD_AB:
6127       if (HAVE_64BIT_GPRS)
6128         {
6129           s = "sd";
6130           goto st;
6131         }
6132
6133       s = "sw";
6134       fmt = "t,o(b)";
6135
6136     ldd_std:
6137       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6138          loads for the case of doing a pair of loads to simulate an 'ld'.
6139          This is not currently done by the compiler, and assembly coders
6140          writing embedded-pic code can cope.  */
6141
6142       if (offset_expr.X_op != O_symbol
6143           && offset_expr.X_op != O_constant)
6144         {
6145           as_bad (_("expression too complex"));
6146           offset_expr.X_op = O_constant;
6147         }
6148
6149       /* Even on a big endian machine $fn comes before $fn+1.  We have
6150          to adjust when loading from memory.  We set coproc if we must
6151          load $fn+1 first.  */
6152       /* Itbl support may require additional care here.  */
6153       if (! target_big_endian)
6154         coproc = 0;
6155
6156       if (mips_pic == NO_PIC
6157           || offset_expr.X_op == O_constant)
6158         {
6159           /* If this is a reference to a GP relative symbol, we want
6160                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6161                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6162              If we have a base register, we use this
6163                addu     $at,$breg,$gp
6164                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6165                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6166              If this is not a GP relative symbol, we want
6167                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6168                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6169                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6170              If there is a base register, we add it to $at after the
6171              lui instruction.  If there is a constant, we always use
6172              the last case.  */
6173           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6174               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6175             {
6176               p = NULL;
6177               used_at = 1;
6178             }
6179           else
6180             {
6181               int off;
6182
6183               if (breg == 0)
6184                 {
6185                   frag_grow (28);
6186                   tempreg = GP;
6187                   off = 0;
6188                   used_at = 0;
6189                 }
6190               else
6191                 {
6192                   frag_grow (36);
6193                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6194                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6195                                "d,v,t", AT, breg, GP);
6196                   tempreg = AT;
6197                   off = 4;
6198                   used_at = 1;
6199                 }
6200
6201               /* Itbl support may require additional care here.  */
6202               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6203                            coproc ? treg + 1 : treg,
6204                            (int) BFD_RELOC_GPREL16, tempreg);
6205               offset_expr.X_add_number += 4;
6206
6207               /* Set mips_optimize to 2 to avoid inserting an
6208                  undesired nop.  */
6209               hold_mips_optimize = mips_optimize;
6210               mips_optimize = 2;
6211               /* Itbl support may require additional care here.  */
6212               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6213                            coproc ? treg : treg + 1,
6214                            (int) BFD_RELOC_GPREL16, tempreg);
6215               mips_optimize = hold_mips_optimize;
6216
6217               p = frag_var (rs_machine_dependent, 12 + off, 0,
6218                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6219                                           used_at && mips_opts.noat),
6220                             offset_expr.X_add_symbol, 0, NULL);
6221
6222               /* We just generated two relocs.  When tc_gen_reloc
6223                  handles this case, it will skip the first reloc and
6224                  handle the second.  The second reloc already has an
6225                  extra addend of 4, which we added above.  We must
6226                  subtract it out, and then subtract another 4 to make
6227                  the first reloc come out right.  The second reloc
6228                  will come out right because we are going to add 4 to
6229                  offset_expr when we build its instruction below.
6230
6231                  If we have a symbol, then we don't want to include
6232                  the offset, because it will wind up being included
6233                  when we generate the reloc.  */
6234
6235               if (offset_expr.X_op == O_constant)
6236                 offset_expr.X_add_number -= 8;
6237               else
6238                 {
6239                   offset_expr.X_add_number = -4;
6240                   offset_expr.X_op = O_constant;
6241                 }
6242             }
6243           macro_build_lui (p, &icnt, &offset_expr, AT);
6244           if (p != NULL)
6245             p += 4;
6246           if (breg != 0)
6247             {
6248               macro_build (p, &icnt, (expressionS *) NULL,
6249                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6250                            "d,v,t", AT, breg, AT);
6251               if (p != NULL)
6252                 p += 4;
6253             }
6254           /* Itbl support may require additional care here.  */
6255           macro_build (p, &icnt, &offset_expr, s, fmt,
6256                        coproc ? treg + 1 : treg,
6257                        (int) BFD_RELOC_LO16, AT);
6258           if (p != NULL)
6259             p += 4;
6260           /* FIXME: How do we handle overflow here?  */
6261           offset_expr.X_add_number += 4;
6262           /* Itbl support may require additional care here.  */
6263           macro_build (p, &icnt, &offset_expr, s, fmt,
6264                        coproc ? treg : treg + 1,
6265                        (int) BFD_RELOC_LO16, AT);
6266         }
6267       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6268         {
6269           int off;
6270
6271           /* If this is a reference to an external symbol, we want
6272                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6273                nop
6274                <op>     $treg,0($at)
6275                <op>     $treg+1,4($at)
6276              Otherwise we want
6277                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6278                nop
6279                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6280                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6281              If there is a base register we add it to $at before the
6282              lwc1 instructions.  If there is a constant we include it
6283              in the lwc1 instructions.  */
6284           used_at = 1;
6285           expr1.X_add_number = offset_expr.X_add_number;
6286           offset_expr.X_add_number = 0;
6287           if (expr1.X_add_number < -0x8000
6288               || expr1.X_add_number >= 0x8000 - 4)
6289             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6290           if (breg == 0)
6291             off = 0;
6292           else
6293             off = 4;
6294           frag_grow (24 + off);
6295           macro_build ((char *) NULL, &icnt, &offset_expr,
6296                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6297                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6298           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6299           if (breg != 0)
6300             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6301                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6302                          "d,v,t", AT, breg, AT);
6303           /* Itbl support may require additional care here.  */
6304           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6305                        coproc ? treg + 1 : treg,
6306                        (int) BFD_RELOC_LO16, AT);
6307           expr1.X_add_number += 4;
6308
6309           /* Set mips_optimize to 2 to avoid inserting an undesired
6310              nop.  */
6311           hold_mips_optimize = mips_optimize;
6312           mips_optimize = 2;
6313           /* Itbl support may require additional care here.  */
6314           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6315                        coproc ? treg : treg + 1,
6316                        (int) BFD_RELOC_LO16, AT);
6317           mips_optimize = hold_mips_optimize;
6318
6319           (void) frag_var (rs_machine_dependent, 0, 0,
6320                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6321                            offset_expr.X_add_symbol, 0, NULL);
6322         }
6323       else if (mips_pic == SVR4_PIC)
6324         {
6325           int gpdel, off;
6326
6327           /* If this is a reference to an external symbol, we want
6328                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6329                addu     $at,$at,$gp
6330                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6331                nop
6332                <op>     $treg,0($at)
6333                <op>     $treg+1,4($at)
6334              Otherwise we want
6335                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6336                nop
6337                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6338                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6339              If there is a base register we add it to $at before the
6340              lwc1 instructions.  If there is a constant we include it
6341              in the lwc1 instructions.  */
6342           used_at = 1;
6343           expr1.X_add_number = offset_expr.X_add_number;
6344           offset_expr.X_add_number = 0;
6345           if (expr1.X_add_number < -0x8000
6346               || expr1.X_add_number >= 0x8000 - 4)
6347             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6348           if (reg_needs_delay (GP))
6349             gpdel = 4;
6350           else
6351             gpdel = 0;
6352           if (breg == 0)
6353             off = 0;
6354           else
6355             off = 4;
6356           frag_grow (56);
6357           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6358                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6359           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6360                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6361                        "d,v,t", AT, AT, GP);
6362           macro_build ((char *) NULL, &icnt, &offset_expr,
6363                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6364                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6365           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6366           if (breg != 0)
6367             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6368                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6369                          "d,v,t", AT, breg, AT);
6370           /* Itbl support may require additional care here.  */
6371           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6372                        coproc ? treg + 1 : treg,
6373                        (int) BFD_RELOC_LO16, AT);
6374           expr1.X_add_number += 4;
6375
6376           /* Set mips_optimize to 2 to avoid inserting an undesired
6377              nop.  */
6378           hold_mips_optimize = mips_optimize;
6379           mips_optimize = 2;
6380           /* Itbl support may require additional care here.  */
6381           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6382                        coproc ? treg : treg + 1,
6383                        (int) BFD_RELOC_LO16, AT);
6384           mips_optimize = hold_mips_optimize;
6385           expr1.X_add_number -= 4;
6386
6387           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6388                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6389                                       8 + gpdel + off, 1, 0),
6390                         offset_expr.X_add_symbol, 0, NULL);
6391           if (gpdel > 0)
6392             {
6393               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6394               p += 4;
6395             }
6396           macro_build (p, &icnt, &offset_expr,
6397                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6398                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6399           p += 4;
6400           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6401           p += 4;
6402           if (breg != 0)
6403             {
6404               macro_build (p, &icnt, (expressionS *) NULL,
6405                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6406                            "d,v,t", AT, breg, AT);
6407               p += 4;
6408             }
6409           /* Itbl support may require additional care here.  */
6410           macro_build (p, &icnt, &expr1, s, fmt,
6411                        coproc ? treg + 1 : treg,
6412                        (int) BFD_RELOC_LO16, AT);
6413           p += 4;
6414           expr1.X_add_number += 4;
6415
6416           /* Set mips_optimize to 2 to avoid inserting an undesired
6417              nop.  */
6418           hold_mips_optimize = mips_optimize;
6419           mips_optimize = 2;
6420           /* Itbl support may require additional care here.  */
6421           macro_build (p, &icnt, &expr1, s, fmt,
6422                        coproc ? treg : treg + 1,
6423                        (int) BFD_RELOC_LO16, AT);
6424           mips_optimize = hold_mips_optimize;
6425         }
6426       else if (mips_pic == EMBEDDED_PIC)
6427         {
6428           /* If there is no base register, we use
6429                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6430                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6431              If we have a base register, we use
6432                addu     $at,$breg,$gp
6433                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6434                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6435              */
6436           if (breg == 0)
6437             {
6438               tempreg = GP;
6439               used_at = 0;
6440             }
6441           else
6442             {
6443               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6444                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6445                            "d,v,t", AT, breg, GP);
6446               tempreg = AT;
6447               used_at = 1;
6448             }
6449
6450           /* Itbl support may require additional care here.  */
6451           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6452                        coproc ? treg + 1 : treg,
6453                        (int) BFD_RELOC_GPREL16, tempreg);
6454           offset_expr.X_add_number += 4;
6455           /* Itbl support may require additional care here.  */
6456           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6457                        coproc ? treg : treg + 1,
6458                        (int) BFD_RELOC_GPREL16, tempreg);
6459         }
6460       else
6461         abort ();
6462
6463       if (! used_at)
6464         return;
6465
6466       break;
6467
6468     case M_LD_OB:
6469       s = "lw";
6470       goto sd_ob;
6471     case M_SD_OB:
6472       s = "sw";
6473     sd_ob:
6474       assert (HAVE_32BIT_ADDRESSES);
6475       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6476                    (int) BFD_RELOC_LO16, breg);
6477       offset_expr.X_add_number += 4;
6478       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6479                    (int) BFD_RELOC_LO16, breg);
6480       return;
6481
6482    /* New code added to support COPZ instructions.
6483       This code builds table entries out of the macros in mip_opcodes.
6484       R4000 uses interlocks to handle coproc delays.
6485       Other chips (like the R3000) require nops to be inserted for delays.
6486
6487       FIXME: Currently, we require that the user handle delays.
6488       In order to fill delay slots for non-interlocked chips,
6489       we must have a way to specify delays based on the coprocessor.
6490       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6491       What are the side-effects of the cop instruction?
6492       What cache support might we have and what are its effects?
6493       Both coprocessor & memory require delays. how long???
6494       What registers are read/set/modified?
6495
6496       If an itbl is provided to interpret cop instructions,
6497       this knowledge can be encoded in the itbl spec.  */
6498
6499     case M_COP0:
6500       s = "c0";
6501       goto copz;
6502     case M_COP1:
6503       s = "c1";
6504       goto copz;
6505     case M_COP2:
6506       s = "c2";
6507       goto copz;
6508     case M_COP3:
6509       s = "c3";
6510     copz:
6511       /* For now we just do C (same as Cz).  The parameter will be
6512          stored in insn_opcode by mips_ip.  */
6513       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6514                    ip->insn_opcode);
6515       return;
6516
6517     case M_MOVE:
6518       move_register (&icnt, dreg, sreg);
6519       return;
6520
6521 #ifdef LOSING_COMPILER
6522     default:
6523       /* Try and see if this is a new itbl instruction.
6524          This code builds table entries out of the macros in mip_opcodes.
6525          FIXME: For now we just assemble the expression and pass it's
6526          value along as a 32-bit immediate.
6527          We may want to have the assembler assemble this value,
6528          so that we gain the assembler's knowledge of delay slots,
6529          symbols, etc.
6530          Would it be more efficient to use mask (id) here? */
6531       if (itbl_have_entries
6532           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6533         {
6534           s = ip->insn_mo->name;
6535           s2 = "cop3";
6536           coproc = ITBL_DECODE_PNUM (immed_expr);;
6537           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6538           return;
6539         }
6540       macro2 (ip);
6541       return;
6542     }
6543   if (mips_opts.noat)
6544     as_warn (_("Macro used $at after \".set noat\""));
6545 }
6546
6547 static void
6548 macro2 (ip)
6549      struct mips_cl_insn *ip;
6550 {
6551   register int treg, sreg, dreg, breg;
6552   int tempreg;
6553   int mask;
6554   int icnt = 0;
6555   int used_at;
6556   expressionS expr1;
6557   const char *s;
6558   const char *s2;
6559   const char *fmt;
6560   int likely = 0;
6561   int dbl = 0;
6562   int coproc = 0;
6563   int lr = 0;
6564   int imm = 0;
6565   int off;
6566   offsetT maxnum;
6567   bfd_reloc_code_real_type r;
6568   char *p;
6569
6570   treg = (ip->insn_opcode >> 16) & 0x1f;
6571   dreg = (ip->insn_opcode >> 11) & 0x1f;
6572   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6573   mask = ip->insn_mo->mask;
6574
6575   expr1.X_op = O_constant;
6576   expr1.X_op_symbol = NULL;
6577   expr1.X_add_symbol = NULL;
6578   expr1.X_add_number = 1;
6579
6580   switch (mask)
6581     {
6582 #endif /* LOSING_COMPILER */
6583
6584     case M_DMUL:
6585       dbl = 1;
6586     case M_MUL:
6587       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6588                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6589       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6590                    dreg);
6591       return;
6592
6593     case M_DMUL_I:
6594       dbl = 1;
6595     case M_MUL_I:
6596       /* The MIPS assembler some times generates shifts and adds.  I'm
6597          not trying to be that fancy. GCC should do this for us
6598          anyway.  */
6599       load_register (&icnt, AT, &imm_expr, dbl);
6600       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6601                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6602       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6603                    dreg);
6604       break;
6605
6606     case M_DMULO_I:
6607       dbl = 1;
6608     case M_MULO_I:
6609       imm = 1;
6610       goto do_mulo;
6611
6612     case M_DMULO:
6613       dbl = 1;
6614     case M_MULO:
6615     do_mulo:
6616       mips_emit_delays (true);
6617       ++mips_opts.noreorder;
6618       mips_any_noreorder = 1;
6619       if (imm)
6620         load_register (&icnt, AT, &imm_expr, dbl);
6621       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6622                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6623       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6624                    dreg);
6625       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6626                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6627       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6628                    AT);
6629       if (mips_trap)
6630         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6631                      dreg, AT);
6632       else
6633         {
6634           expr1.X_add_number = 8;
6635           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6636                        AT);
6637           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6638                        0);
6639           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6640                        "c", 6);
6641         }
6642       --mips_opts.noreorder;
6643       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6644       break;
6645
6646     case M_DMULOU_I:
6647       dbl = 1;
6648     case M_MULOU_I:
6649       imm = 1;
6650       goto do_mulou;
6651
6652     case M_DMULOU:
6653       dbl = 1;
6654     case M_MULOU:
6655     do_mulou:
6656       mips_emit_delays (true);
6657       ++mips_opts.noreorder;
6658       mips_any_noreorder = 1;
6659       if (imm)
6660         load_register (&icnt, AT, &imm_expr, dbl);
6661       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6662                    dbl ? "dmultu" : "multu",
6663                    "s,t", sreg, imm ? AT : treg);
6664       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6665                    AT);
6666       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6667                    dreg);
6668       if (mips_trap)
6669         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6670                      AT, 0);
6671       else
6672         {
6673           expr1.X_add_number = 8;
6674           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6675           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6676                        0);
6677           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6678                        "c", 6);
6679         }
6680       --mips_opts.noreorder;
6681       break;
6682
6683     case M_ROL:
6684       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6685                    "d,v,t", AT, 0, treg);
6686       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6687                    "d,t,s", AT, sreg, AT);
6688       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6689                    "d,t,s", dreg, sreg, treg);
6690       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6691                    "d,v,t", dreg, dreg, AT);
6692       break;
6693
6694     case M_ROL_I:
6695       if (imm_expr.X_op != O_constant)
6696         as_bad (_("rotate count too large"));
6697       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6698                    AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6699       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6700                    dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6702                    dreg, dreg, AT);
6703       break;
6704
6705     case M_ROR:
6706       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6707                    "d,v,t", AT, 0, treg);
6708       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6709                    "d,t,s", AT, sreg, AT);
6710       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6711                    "d,t,s", dreg, sreg, treg);
6712       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6713                    "d,v,t", dreg, dreg, AT);
6714       break;
6715
6716     case M_ROR_I:
6717       if (imm_expr.X_op != O_constant)
6718         as_bad (_("rotate count too large"));
6719       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6720                    AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6721       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6722                    dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6723       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6724                    dreg, dreg, AT);
6725       break;
6726
6727     case M_S_DOB:
6728       if (mips_arch == CPU_R4650)
6729         {
6730           as_bad (_("opcode not supported on this processor"));
6731           return;
6732         }
6733       assert (mips_opts.isa == ISA_MIPS1);
6734       /* Even on a big endian machine $fn comes before $fn+1.  We have
6735          to adjust when storing to memory.  */
6736       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6737                    target_big_endian ? treg + 1 : treg,
6738                    (int) BFD_RELOC_LO16, breg);
6739       offset_expr.X_add_number += 4;
6740       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6741                    target_big_endian ? treg : treg + 1,
6742                    (int) BFD_RELOC_LO16, breg);
6743       return;
6744
6745     case M_SEQ:
6746       if (sreg == 0)
6747         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6748                      treg, (int) BFD_RELOC_LO16);
6749       else if (treg == 0)
6750         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6751                      sreg, (int) BFD_RELOC_LO16);
6752       else
6753         {
6754           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6755                        "d,v,t", dreg, sreg, treg);
6756           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6757                        dreg, (int) BFD_RELOC_LO16);
6758         }
6759       return;
6760
6761     case M_SEQ_I:
6762       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6763         {
6764           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6765                        sreg, (int) BFD_RELOC_LO16);
6766           return;
6767         }
6768       if (sreg == 0)
6769         {
6770           as_warn (_("Instruction %s: result is always false"),
6771                    ip->insn_mo->name);
6772           move_register (&icnt, dreg, 0);
6773           return;
6774         }
6775       if (imm_expr.X_op == O_constant
6776           && imm_expr.X_add_number >= 0
6777           && imm_expr.X_add_number < 0x10000)
6778         {
6779           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6780                        sreg, (int) BFD_RELOC_LO16);
6781           used_at = 0;
6782         }
6783       else if (imm_expr.X_op == O_constant
6784                && imm_expr.X_add_number > -0x8000
6785                && imm_expr.X_add_number < 0)
6786         {
6787           imm_expr.X_add_number = -imm_expr.X_add_number;
6788           macro_build ((char *) NULL, &icnt, &imm_expr,
6789                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6790                        "t,r,j", dreg, sreg,
6791                        (int) BFD_RELOC_LO16);
6792           used_at = 0;
6793         }
6794       else
6795         {
6796           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6797           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6798                        "d,v,t", dreg, sreg, AT);
6799           used_at = 1;
6800         }
6801       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6802                    (int) BFD_RELOC_LO16);
6803       if (used_at)
6804         break;
6805       return;
6806
6807     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6808       s = "slt";
6809       goto sge;
6810     case M_SGEU:
6811       s = "sltu";
6812     sge:
6813       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6814                    dreg, sreg, treg);
6815       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6816                    (int) BFD_RELOC_LO16);
6817       return;
6818
6819     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6820     case M_SGEU_I:
6821       if (imm_expr.X_op == O_constant
6822           && imm_expr.X_add_number >= -0x8000
6823           && imm_expr.X_add_number < 0x8000)
6824         {
6825           macro_build ((char *) NULL, &icnt, &imm_expr,
6826                        mask == M_SGE_I ? "slti" : "sltiu",
6827                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6828           used_at = 0;
6829         }
6830       else
6831         {
6832           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6833           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6834                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6835                        AT);
6836           used_at = 1;
6837         }
6838       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6839                    (int) BFD_RELOC_LO16);
6840       if (used_at)
6841         break;
6842       return;
6843
6844     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6845       s = "slt";
6846       goto sgt;
6847     case M_SGTU:
6848       s = "sltu";
6849     sgt:
6850       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6851                    dreg, treg, sreg);
6852       return;
6853
6854     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6855       s = "slt";
6856       goto sgti;
6857     case M_SGTU_I:
6858       s = "sltu";
6859     sgti:
6860       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6861       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6862                    dreg, AT, sreg);
6863       break;
6864
6865     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6866       s = "slt";
6867       goto sle;
6868     case M_SLEU:
6869       s = "sltu";
6870     sle:
6871       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6872                    dreg, treg, sreg);
6873       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6874                    (int) BFD_RELOC_LO16);
6875       return;
6876
6877     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6878       s = "slt";
6879       goto slei;
6880     case M_SLEU_I:
6881       s = "sltu";
6882     slei:
6883       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6884       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6885                    dreg, AT, sreg);
6886       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6887                    (int) BFD_RELOC_LO16);
6888       break;
6889
6890     case M_SLT_I:
6891       if (imm_expr.X_op == O_constant
6892           && imm_expr.X_add_number >= -0x8000
6893           && imm_expr.X_add_number < 0x8000)
6894         {
6895           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6896                        dreg, sreg, (int) BFD_RELOC_LO16);
6897           return;
6898         }
6899       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6900       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6901                    dreg, sreg, AT);
6902       break;
6903
6904     case M_SLTU_I:
6905       if (imm_expr.X_op == O_constant
6906           && imm_expr.X_add_number >= -0x8000
6907           && imm_expr.X_add_number < 0x8000)
6908         {
6909           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6910                        dreg, sreg, (int) BFD_RELOC_LO16);
6911           return;
6912         }
6913       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6914       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6915                    "d,v,t", dreg, sreg, AT);
6916       break;
6917
6918     case M_SNE:
6919       if (sreg == 0)
6920         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6921                      "d,v,t", dreg, 0, treg);
6922       else if (treg == 0)
6923         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6924                      "d,v,t", dreg, 0, sreg);
6925       else
6926         {
6927           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6928                        "d,v,t", dreg, sreg, treg);
6929           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6930                        "d,v,t", dreg, 0, dreg);
6931         }
6932       return;
6933
6934     case M_SNE_I:
6935       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6936         {
6937           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6938                        "d,v,t", dreg, 0, sreg);
6939           return;
6940         }
6941       if (sreg == 0)
6942         {
6943           as_warn (_("Instruction %s: result is always true"),
6944                    ip->insn_mo->name);
6945           macro_build ((char *) NULL, &icnt, &expr1,
6946                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6947                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6948           return;
6949         }
6950       if (imm_expr.X_op == O_constant
6951           && imm_expr.X_add_number >= 0
6952           && imm_expr.X_add_number < 0x10000)
6953         {
6954           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6955                        dreg, sreg, (int) BFD_RELOC_LO16);
6956           used_at = 0;
6957         }
6958       else if (imm_expr.X_op == O_constant
6959                && imm_expr.X_add_number > -0x8000
6960                && imm_expr.X_add_number < 0)
6961         {
6962           imm_expr.X_add_number = -imm_expr.X_add_number;
6963           macro_build ((char *) NULL, &icnt, &imm_expr,
6964                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6965                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6966           used_at = 0;
6967         }
6968       else
6969         {
6970           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6971           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6972                        "d,v,t", dreg, sreg, AT);
6973           used_at = 1;
6974         }
6975       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6976                    "d,v,t", dreg, 0, dreg);
6977       if (used_at)
6978         break;
6979       return;
6980
6981     case M_DSUB_I:
6982       dbl = 1;
6983     case M_SUB_I:
6984       if (imm_expr.X_op == O_constant
6985           && imm_expr.X_add_number > -0x8000
6986           && imm_expr.X_add_number <= 0x8000)
6987         {
6988           imm_expr.X_add_number = -imm_expr.X_add_number;
6989           macro_build ((char *) NULL, &icnt, &imm_expr,
6990                        dbl ? "daddi" : "addi",
6991                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6992           return;
6993         }
6994       load_register (&icnt, AT, &imm_expr, dbl);
6995       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6996                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6997       break;
6998
6999     case M_DSUBU_I:
7000       dbl = 1;
7001     case M_SUBU_I:
7002       if (imm_expr.X_op == O_constant
7003           && imm_expr.X_add_number > -0x8000
7004           && imm_expr.X_add_number <= 0x8000)
7005         {
7006           imm_expr.X_add_number = -imm_expr.X_add_number;
7007           macro_build ((char *) NULL, &icnt, &imm_expr,
7008                        dbl ? "daddiu" : "addiu",
7009                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7010           return;
7011         }
7012       load_register (&icnt, AT, &imm_expr, dbl);
7013       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7014                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7015       break;
7016
7017     case M_TEQ_I:
7018       s = "teq";
7019       goto trap;
7020     case M_TGE_I:
7021       s = "tge";
7022       goto trap;
7023     case M_TGEU_I:
7024       s = "tgeu";
7025       goto trap;
7026     case M_TLT_I:
7027       s = "tlt";
7028       goto trap;
7029     case M_TLTU_I:
7030       s = "tltu";
7031       goto trap;
7032     case M_TNE_I:
7033       s = "tne";
7034     trap:
7035       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7036       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7037                    AT);
7038       break;
7039
7040     case M_TRUNCWS:
7041     case M_TRUNCWD:
7042       assert (mips_opts.isa == ISA_MIPS1);
7043       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7044       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7045
7046       /*
7047        * Is the double cfc1 instruction a bug in the mips assembler;
7048        * or is there a reason for it?
7049        */
7050       mips_emit_delays (true);
7051       ++mips_opts.noreorder;
7052       mips_any_noreorder = 1;
7053       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7054                    treg, 31);
7055       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7056                    treg, 31);
7057       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7058       expr1.X_add_number = 3;
7059       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7060                    (int) BFD_RELOC_LO16);
7061       expr1.X_add_number = 2;
7062       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7063                      (int) BFD_RELOC_LO16);
7064       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7065                    AT, 31);
7066       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7067       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7068               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7069       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7070                    treg, 31);
7071       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7072       --mips_opts.noreorder;
7073       break;
7074
7075     case M_ULH:
7076       s = "lb";
7077       goto ulh;
7078     case M_ULHU:
7079       s = "lbu";
7080     ulh:
7081       if (offset_expr.X_add_number >= 0x7fff)
7082         as_bad (_("operand overflow"));
7083       /* avoid load delay */
7084       if (! target_big_endian)
7085         offset_expr.X_add_number += 1;
7086       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7087                    (int) BFD_RELOC_LO16, breg);
7088       if (! target_big_endian)
7089         offset_expr.X_add_number -= 1;
7090       else
7091         offset_expr.X_add_number += 1;
7092       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7093                    (int) BFD_RELOC_LO16, breg);
7094       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7095                    treg, treg, 8);
7096       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7097                    treg, treg, AT);
7098       break;
7099
7100     case M_ULD:
7101       s = "ldl";
7102       s2 = "ldr";
7103       off = 7;
7104       goto ulw;
7105     case M_ULW:
7106       s = "lwl";
7107       s2 = "lwr";
7108       off = 3;
7109     ulw:
7110       if (offset_expr.X_add_number >= 0x8000 - off)
7111         as_bad (_("operand overflow"));
7112       if (! target_big_endian)
7113         offset_expr.X_add_number += off;
7114       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7115                    (int) BFD_RELOC_LO16, breg);
7116       if (! target_big_endian)
7117         offset_expr.X_add_number -= off;
7118       else
7119         offset_expr.X_add_number += off;
7120       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7121                    (int) BFD_RELOC_LO16, breg);
7122       return;
7123
7124     case M_ULD_A:
7125       s = "ldl";
7126       s2 = "ldr";
7127       off = 7;
7128       goto ulwa;
7129     case M_ULW_A:
7130       s = "lwl";
7131       s2 = "lwr";
7132       off = 3;
7133     ulwa:
7134       used_at = 1;
7135       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7136       if (breg != 0)
7137         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7138                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7139                      "d,v,t", AT, AT, breg);
7140       if (! target_big_endian)
7141         expr1.X_add_number = off;
7142       else
7143         expr1.X_add_number = 0;
7144       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7145                    (int) BFD_RELOC_LO16, AT);
7146       if (! target_big_endian)
7147         expr1.X_add_number = 0;
7148       else
7149         expr1.X_add_number = off;
7150       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7151                    (int) BFD_RELOC_LO16, AT);
7152       break;
7153
7154     case M_ULH_A:
7155     case M_ULHU_A:
7156       used_at = 1;
7157       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7158       if (breg != 0)
7159         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7160                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7161                      "d,v,t", AT, AT, breg);
7162       if (target_big_endian)
7163         expr1.X_add_number = 0;
7164       macro_build ((char *) NULL, &icnt, &expr1,
7165                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7166                    (int) BFD_RELOC_LO16, AT);
7167       if (target_big_endian)
7168         expr1.X_add_number = 1;
7169       else
7170         expr1.X_add_number = 0;
7171       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7172                    (int) BFD_RELOC_LO16, AT);
7173       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7174                    treg, treg, 8);
7175       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7176                    treg, treg, AT);
7177       break;
7178
7179     case M_USH:
7180       if (offset_expr.X_add_number >= 0x7fff)
7181         as_bad (_("operand overflow"));
7182       if (target_big_endian)
7183         offset_expr.X_add_number += 1;
7184       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7185                    (int) BFD_RELOC_LO16, breg);
7186       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7187                    AT, treg, 8);
7188       if (target_big_endian)
7189         offset_expr.X_add_number -= 1;
7190       else
7191         offset_expr.X_add_number += 1;
7192       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7193                    (int) BFD_RELOC_LO16, breg);
7194       break;
7195
7196     case M_USD:
7197       s = "sdl";
7198       s2 = "sdr";
7199       off = 7;
7200       goto usw;
7201     case M_USW:
7202       s = "swl";
7203       s2 = "swr";
7204       off = 3;
7205     usw:
7206       if (offset_expr.X_add_number >= 0x8000 - off)
7207         as_bad (_("operand overflow"));
7208       if (! target_big_endian)
7209         offset_expr.X_add_number += off;
7210       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7211                    (int) BFD_RELOC_LO16, breg);
7212       if (! target_big_endian)
7213         offset_expr.X_add_number -= off;
7214       else
7215         offset_expr.X_add_number += off;
7216       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7217                    (int) BFD_RELOC_LO16, breg);
7218       return;
7219
7220     case M_USD_A:
7221       s = "sdl";
7222       s2 = "sdr";
7223       off = 7;
7224       goto uswa;
7225     case M_USW_A:
7226       s = "swl";
7227       s2 = "swr";
7228       off = 3;
7229     uswa:
7230       used_at = 1;
7231       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7232       if (breg != 0)
7233         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7234                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7235                      "d,v,t", AT, AT, breg);
7236       if (! target_big_endian)
7237         expr1.X_add_number = off;
7238       else
7239         expr1.X_add_number = 0;
7240       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7241                    (int) BFD_RELOC_LO16, AT);
7242       if (! target_big_endian)
7243         expr1.X_add_number = 0;
7244       else
7245         expr1.X_add_number = off;
7246       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7247                    (int) BFD_RELOC_LO16, AT);
7248       break;
7249
7250     case M_USH_A:
7251       used_at = 1;
7252       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7253       if (breg != 0)
7254         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7255                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7256                      "d,v,t", AT, AT, breg);
7257       if (! target_big_endian)
7258         expr1.X_add_number = 0;
7259       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7260                    (int) BFD_RELOC_LO16, AT);
7261       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7262                    treg, treg, 8);
7263       if (! target_big_endian)
7264         expr1.X_add_number = 1;
7265       else
7266         expr1.X_add_number = 0;
7267       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7268                    (int) BFD_RELOC_LO16, AT);
7269       if (! target_big_endian)
7270         expr1.X_add_number = 0;
7271       else
7272         expr1.X_add_number = 1;
7273       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7274                    (int) BFD_RELOC_LO16, AT);
7275       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7276                    treg, treg, 8);
7277       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7278                    treg, treg, AT);
7279       break;
7280
7281     default:
7282       /* FIXME: Check if this is one of the itbl macros, since they
7283          are added dynamically.  */
7284       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7285       break;
7286     }
7287   if (mips_opts.noat)
7288     as_warn (_("Macro used $at after \".set noat\""));
7289 }
7290
7291 /* Implement macros in mips16 mode.  */
7292
7293 static void
7294 mips16_macro (ip)
7295      struct mips_cl_insn *ip;
7296 {
7297   int mask;
7298   int xreg, yreg, zreg, tmp;
7299   int icnt;
7300   expressionS expr1;
7301   int dbl;
7302   const char *s, *s2, *s3;
7303
7304   mask = ip->insn_mo->mask;
7305
7306   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7307   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7308   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7309
7310   icnt = 0;
7311
7312   expr1.X_op = O_constant;
7313   expr1.X_op_symbol = NULL;
7314   expr1.X_add_symbol = NULL;
7315   expr1.X_add_number = 1;
7316
7317   dbl = 0;
7318
7319   switch (mask)
7320     {
7321     default:
7322       internalError ();
7323
7324     case M_DDIV_3:
7325       dbl = 1;
7326     case M_DIV_3:
7327       s = "mflo";
7328       goto do_div3;
7329     case M_DREM_3:
7330       dbl = 1;
7331     case M_REM_3:
7332       s = "mfhi";
7333     do_div3:
7334       mips_emit_delays (true);
7335       ++mips_opts.noreorder;
7336       mips_any_noreorder = 1;
7337       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7338                    dbl ? "ddiv" : "div",
7339                    "0,x,y", xreg, yreg);
7340       expr1.X_add_number = 2;
7341       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7342       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7343                    7);
7344
7345       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7346          since that causes an overflow.  We should do that as well,
7347          but I don't see how to do the comparisons without a temporary
7348          register.  */
7349       --mips_opts.noreorder;
7350       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7351       break;
7352
7353     case M_DIVU_3:
7354       s = "divu";
7355       s2 = "mflo";
7356       goto do_divu3;
7357     case M_REMU_3:
7358       s = "divu";
7359       s2 = "mfhi";
7360       goto do_divu3;
7361     case M_DDIVU_3:
7362       s = "ddivu";
7363       s2 = "mflo";
7364       goto do_divu3;
7365     case M_DREMU_3:
7366       s = "ddivu";
7367       s2 = "mfhi";
7368     do_divu3:
7369       mips_emit_delays (true);
7370       ++mips_opts.noreorder;
7371       mips_any_noreorder = 1;
7372       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7373                    xreg, yreg);
7374       expr1.X_add_number = 2;
7375       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7377                    "6", 7);
7378       --mips_opts.noreorder;
7379       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7380       break;
7381
7382     case M_DMUL:
7383       dbl = 1;
7384     case M_MUL:
7385       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7386                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7387       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7388                    zreg);
7389       return;
7390
7391     case M_DSUBU_I:
7392       dbl = 1;
7393       goto do_subu;
7394     case M_SUBU_I:
7395     do_subu:
7396       if (imm_expr.X_op != O_constant)
7397         as_bad (_("Unsupported large constant"));
7398       imm_expr.X_add_number = -imm_expr.X_add_number;
7399       macro_build ((char *) NULL, &icnt, &imm_expr,
7400                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7401       break;
7402
7403     case M_SUBU_I_2:
7404       if (imm_expr.X_op != O_constant)
7405         as_bad (_("Unsupported large constant"));
7406       imm_expr.X_add_number = -imm_expr.X_add_number;
7407       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7408                    "x,k", xreg);
7409       break;
7410
7411     case M_DSUBU_I_2:
7412       if (imm_expr.X_op != O_constant)
7413         as_bad (_("Unsupported large constant"));
7414       imm_expr.X_add_number = -imm_expr.X_add_number;
7415       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7416                    "y,j", yreg);
7417       break;
7418
7419     case M_BEQ:
7420       s = "cmp";
7421       s2 = "bteqz";
7422       goto do_branch;
7423     case M_BNE:
7424       s = "cmp";
7425       s2 = "btnez";
7426       goto do_branch;
7427     case M_BLT:
7428       s = "slt";
7429       s2 = "btnez";
7430       goto do_branch;
7431     case M_BLTU:
7432       s = "sltu";
7433       s2 = "btnez";
7434       goto do_branch;
7435     case M_BLE:
7436       s = "slt";
7437       s2 = "bteqz";
7438       goto do_reverse_branch;
7439     case M_BLEU:
7440       s = "sltu";
7441       s2 = "bteqz";
7442       goto do_reverse_branch;
7443     case M_BGE:
7444       s = "slt";
7445       s2 = "bteqz";
7446       goto do_branch;
7447     case M_BGEU:
7448       s = "sltu";
7449       s2 = "bteqz";
7450       goto do_branch;
7451     case M_BGT:
7452       s = "slt";
7453       s2 = "btnez";
7454       goto do_reverse_branch;
7455     case M_BGTU:
7456       s = "sltu";
7457       s2 = "btnez";
7458
7459     do_reverse_branch:
7460       tmp = xreg;
7461       xreg = yreg;
7462       yreg = tmp;
7463
7464     do_branch:
7465       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7466                    xreg, yreg);
7467       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7468       break;
7469
7470     case M_BEQ_I:
7471       s = "cmpi";
7472       s2 = "bteqz";
7473       s3 = "x,U";
7474       goto do_branch_i;
7475     case M_BNE_I:
7476       s = "cmpi";
7477       s2 = "btnez";
7478       s3 = "x,U";
7479       goto do_branch_i;
7480     case M_BLT_I:
7481       s = "slti";
7482       s2 = "btnez";
7483       s3 = "x,8";
7484       goto do_branch_i;
7485     case M_BLTU_I:
7486       s = "sltiu";
7487       s2 = "btnez";
7488       s3 = "x,8";
7489       goto do_branch_i;
7490     case M_BLE_I:
7491       s = "slti";
7492       s2 = "btnez";
7493       s3 = "x,8";
7494       goto do_addone_branch_i;
7495     case M_BLEU_I:
7496       s = "sltiu";
7497       s2 = "btnez";
7498       s3 = "x,8";
7499       goto do_addone_branch_i;
7500     case M_BGE_I:
7501       s = "slti";
7502       s2 = "bteqz";
7503       s3 = "x,8";
7504       goto do_branch_i;
7505     case M_BGEU_I:
7506       s = "sltiu";
7507       s2 = "bteqz";
7508       s3 = "x,8";
7509       goto do_branch_i;
7510     case M_BGT_I:
7511       s = "slti";
7512       s2 = "bteqz";
7513       s3 = "x,8";
7514       goto do_addone_branch_i;
7515     case M_BGTU_I:
7516       s = "sltiu";
7517       s2 = "bteqz";
7518       s3 = "x,8";
7519
7520     do_addone_branch_i:
7521       if (imm_expr.X_op != O_constant)
7522         as_bad (_("Unsupported large constant"));
7523       ++imm_expr.X_add_number;
7524
7525     do_branch_i:
7526       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7527       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7528       break;
7529
7530     case M_ABS:
7531       expr1.X_add_number = 0;
7532       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7533       if (xreg != yreg)
7534         move_register (&icnt, xreg, yreg);
7535       expr1.X_add_number = 2;
7536       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7537       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7538                    "neg", "x,w", xreg, xreg);
7539     }
7540 }
7541
7542 /* For consistency checking, verify that all bits are specified either
7543    by the match/mask part of the instruction definition, or by the
7544    operand list.  */
7545 static int
7546 validate_mips_insn (opc)
7547      const struct mips_opcode *opc;
7548 {
7549   const char *p = opc->args;
7550   char c;
7551   unsigned long used_bits = opc->mask;
7552
7553   if ((used_bits & opc->match) != opc->match)
7554     {
7555       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7556               opc->name, opc->args);
7557       return 0;
7558     }
7559 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7560   while (*p)
7561     switch (c = *p++)
7562       {
7563       case ',': break;
7564       case '(': break;
7565       case ')': break;
7566       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7567       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7568       case 'A': break;
7569       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7570       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7571       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7572       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7573       case 'F': break;
7574       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7575       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7576       case 'I': break;
7577       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7578       case 'L': break;
7579       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7580       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7581       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7582       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7583       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7584       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7585       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7586       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7587       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7588       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7589       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7590       case 'f': break;
7591       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7592       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7593       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7594       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7595       case 'l': break;
7596       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7597       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7598       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7599       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7600       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7601       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7602       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7603       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7604       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7605       case 'x': break;
7606       case 'z': break;
7607       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7608       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7609                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7610       default:
7611         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7612                 c, opc->name, opc->args);
7613         return 0;
7614       }
7615 #undef USE_BITS
7616   if (used_bits != 0xffffffff)
7617     {
7618       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7619               ~used_bits & 0xffffffff, opc->name, opc->args);
7620       return 0;
7621     }
7622   return 1;
7623 }
7624
7625 /* This routine assembles an instruction into its binary format.  As a
7626    side effect, it sets one of the global variables imm_reloc or
7627    offset_reloc to the type of relocation to do if one of the operands
7628    is an address expression.  */
7629
7630 static void
7631 mips_ip (str, ip)
7632      char *str;
7633      struct mips_cl_insn *ip;
7634 {
7635   char *s;
7636   const char *args;
7637   char c = 0;
7638   struct mips_opcode *insn;
7639   char *argsStart;
7640   unsigned int regno;
7641   unsigned int lastregno = 0;
7642   char *s_reset;
7643   char save_c = 0;
7644
7645   insn_error = NULL;
7646
7647   /* If the instruction contains a '.', we first try to match an instruction
7648      including the '.'.  Then we try again without the '.'.  */
7649   insn = NULL;
7650   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7651     continue;
7652
7653   /* If we stopped on whitespace, then replace the whitespace with null for
7654      the call to hash_find.  Save the character we replaced just in case we
7655      have to re-parse the instruction.  */
7656   if (ISSPACE (*s))
7657     {
7658       save_c = *s;
7659       *s++ = '\0';
7660     }
7661
7662   insn = (struct mips_opcode *) hash_find (op_hash, str);
7663
7664   /* If we didn't find the instruction in the opcode table, try again, but
7665      this time with just the instruction up to, but not including the
7666      first '.'.  */
7667   if (insn == NULL)
7668     {
7669       /* Restore the character we overwrite above (if any).  */
7670       if (save_c)
7671         *(--s) = save_c;
7672
7673       /* Scan up to the first '.' or whitespace.  */
7674       for (s = str;
7675            *s != '\0' && *s != '.' && !ISSPACE (*s);
7676            ++s)
7677         continue;
7678
7679       /* If we did not find a '.', then we can quit now.  */
7680       if (*s != '.')
7681         {
7682           insn_error = "unrecognized opcode";
7683           return;
7684         }
7685
7686       /* Lookup the instruction in the hash table.  */
7687       *s++ = '\0';
7688       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7689         {
7690           insn_error = "unrecognized opcode";
7691           return;
7692         }
7693     }
7694
7695   argsStart = s;
7696   for (;;)
7697     {
7698       boolean ok;
7699
7700       assert (strcmp (insn->name, str) == 0);
7701
7702       if (OPCODE_IS_MEMBER (insn,
7703                             (mips_opts.isa
7704                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7705                             mips_arch))
7706         ok = true;
7707       else
7708         ok = false;
7709
7710       if (insn->pinfo != INSN_MACRO)
7711         {
7712           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7713             ok = false;
7714         }
7715
7716       if (! ok)
7717         {
7718           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7719               && strcmp (insn->name, insn[1].name) == 0)
7720             {
7721               ++insn;
7722               continue;
7723             }
7724           else
7725             {
7726               if (!insn_error)
7727                 {
7728                   static char buf[100];
7729                   sprintf (buf,
7730                            _("opcode not supported on this processor: %s (%s)"),
7731                            mips_cpu_to_str (mips_arch),
7732                            mips_isa_to_str (mips_opts.isa));
7733
7734                   insn_error = buf;
7735                 }
7736               if (save_c)
7737                 *(--s) = save_c;
7738               return;
7739             }
7740         }
7741
7742       ip->insn_mo = insn;
7743       ip->insn_opcode = insn->match;
7744       insn_error = NULL;
7745       for (args = insn->args;; ++args)
7746         {
7747           s += strspn (s, " \t");
7748           switch (*args)
7749             {
7750             case '\0':          /* end of args */
7751               if (*s == '\0')
7752                 return;
7753               break;
7754
7755             case ',':
7756               if (*s++ == *args)
7757                 continue;
7758               s--;
7759               switch (*++args)
7760                 {
7761                 case 'r':
7762                 case 'v':
7763                   ip->insn_opcode |= lastregno << OP_SH_RS;
7764                   continue;
7765
7766                 case 'w':
7767                   ip->insn_opcode |= lastregno << OP_SH_RT;
7768                   continue;
7769
7770                 case 'W':
7771                   ip->insn_opcode |= lastregno << OP_SH_FT;
7772                   continue;
7773
7774                 case 'V':
7775                   ip->insn_opcode |= lastregno << OP_SH_FS;
7776                   continue;
7777                 }
7778               break;
7779
7780             case '(':
7781               /* Handle optional base register.
7782                  Either the base register is omitted or
7783                  we must have a left paren.  */
7784               /* This is dependent on the next operand specifier
7785                  is a base register specification.  */
7786               assert (args[1] == 'b' || args[1] == '5'
7787                       || args[1] == '-' || args[1] == '4');
7788               if (*s == '\0')
7789                 return;
7790
7791             case ')':           /* these must match exactly */
7792               if (*s++ == *args)
7793                 continue;
7794               break;
7795
7796             case '<':           /* must be at least one digit */
7797               /*
7798                * According to the manual, if the shift amount is greater
7799                * than 31 or less than 0, then the shift amount should be
7800                * mod 32.  In reality the mips assembler issues an error.
7801                * We issue a warning and mask out all but the low 5 bits.
7802                */
7803               my_getExpression (&imm_expr, s);
7804               check_absolute_expr (ip, &imm_expr);
7805               if ((unsigned long) imm_expr.X_add_number > 31)
7806                 {
7807                   as_warn (_("Improper shift amount (%ld)"),
7808                            (long) imm_expr.X_add_number);
7809                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7810                 }
7811               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7812               imm_expr.X_op = O_absent;
7813               s = expr_end;
7814               continue;
7815
7816             case '>':           /* shift amount minus 32 */
7817               my_getExpression (&imm_expr, s);
7818               check_absolute_expr (ip, &imm_expr);
7819               if ((unsigned long) imm_expr.X_add_number < 32
7820                   || (unsigned long) imm_expr.X_add_number > 63)
7821                 break;
7822               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7823               imm_expr.X_op = O_absent;
7824               s = expr_end;
7825               continue;
7826
7827             case 'k':           /* cache code */
7828             case 'h':           /* prefx code */
7829               my_getExpression (&imm_expr, s);
7830               check_absolute_expr (ip, &imm_expr);
7831               if ((unsigned long) imm_expr.X_add_number > 31)
7832                 {
7833                   as_warn (_("Invalid value for `%s' (%lu)"),
7834                            ip->insn_mo->name,
7835                            (unsigned long) imm_expr.X_add_number);
7836                   imm_expr.X_add_number &= 0x1f;
7837                 }
7838               if (*args == 'k')
7839                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7840               else
7841                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7842               imm_expr.X_op = O_absent;
7843               s = expr_end;
7844               continue;
7845
7846             case 'c':           /* break code */
7847               my_getExpression (&imm_expr, s);
7848               check_absolute_expr (ip, &imm_expr);
7849               if ((unsigned) imm_expr.X_add_number > 1023)
7850                 {
7851                   as_warn (_("Illegal break code (%ld)"),
7852                            (long) imm_expr.X_add_number);
7853                   imm_expr.X_add_number &= OP_MASK_CODE;
7854                 }
7855               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7856               imm_expr.X_op = O_absent;
7857               s = expr_end;
7858               continue;
7859
7860             case 'q':           /* lower break code */
7861               my_getExpression (&imm_expr, s);
7862               check_absolute_expr (ip, &imm_expr);
7863               if ((unsigned) imm_expr.X_add_number > 1023)
7864                 {
7865                   as_warn (_("Illegal lower break code (%ld)"),
7866                            (long) imm_expr.X_add_number);
7867                   imm_expr.X_add_number &= OP_MASK_CODE2;
7868                 }
7869               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7870               imm_expr.X_op = O_absent;
7871               s = expr_end;
7872               continue;
7873
7874             case 'B':           /* 20-bit syscall/break code.  */
7875               my_getExpression (&imm_expr, s);
7876               check_absolute_expr (ip, &imm_expr);
7877               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7878                 as_warn (_("Illegal 20-bit code (%ld)"),
7879                          (long) imm_expr.X_add_number);
7880               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7881               imm_expr.X_op = O_absent;
7882               s = expr_end;
7883               continue;
7884
7885             case 'C':           /* Coprocessor code */
7886               my_getExpression (&imm_expr, s);
7887               check_absolute_expr (ip, &imm_expr);
7888               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
7889                 {
7890                   as_warn (_("Coproccesor code > 25 bits (%ld)"),
7891                            (long) imm_expr.X_add_number);
7892                   imm_expr.X_add_number &= ((1 << 25) - 1);
7893                 }
7894               ip->insn_opcode |= imm_expr.X_add_number;
7895               imm_expr.X_op = O_absent;
7896               s = expr_end;
7897               continue;
7898
7899             case 'J':           /* 19-bit wait code.  */
7900               my_getExpression (&imm_expr, s);
7901               check_absolute_expr (ip, &imm_expr);
7902               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7903                 as_warn (_("Illegal 19-bit code (%ld)"),
7904                          (long) imm_expr.X_add_number);
7905               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7906               imm_expr.X_op = O_absent;
7907               s = expr_end;
7908               continue;
7909
7910             case 'P':           /* Performance register */
7911               my_getExpression (&imm_expr, s);
7912               check_absolute_expr (ip, &imm_expr);
7913               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7914                 {
7915                   as_warn (_("Invalid performance register (%ld)"),
7916                            (long) imm_expr.X_add_number);
7917                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7918                 }
7919               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7920               imm_expr.X_op = O_absent;
7921               s = expr_end;
7922               continue;
7923
7924             case 'b':           /* base register */
7925             case 'd':           /* destination register */
7926             case 's':           /* source register */
7927             case 't':           /* target register */
7928             case 'r':           /* both target and source */
7929             case 'v':           /* both dest and source */
7930             case 'w':           /* both dest and target */
7931             case 'E':           /* coprocessor target register */
7932             case 'G':           /* coprocessor destination register */
7933             case 'x':           /* ignore register name */
7934             case 'z':           /* must be zero register */
7935             case 'U':           /* destination register (clo/clz).  */
7936               s_reset = s;
7937               if (s[0] == '$')
7938                 {
7939
7940                   if (ISDIGIT (s[1]))
7941                     {
7942                       ++s;
7943                       regno = 0;
7944                       do
7945                         {
7946                           regno *= 10;
7947                           regno += *s - '0';
7948                           ++s;
7949                         }
7950                       while (ISDIGIT (*s));
7951                       if (regno > 31)
7952                         as_bad (_("Invalid register number (%d)"), regno);
7953                     }
7954                   else if (*args == 'E' || *args == 'G')
7955                     goto notreg;
7956                   else
7957                     {
7958                       if (s[1] == 'f' && s[2] == 'p')
7959                         {
7960                           s += 3;
7961                           regno = FP;
7962                         }
7963                       else if (s[1] == 's' && s[2] == 'p')
7964                         {
7965                           s += 3;
7966                           regno = SP;
7967                         }
7968                       else if (s[1] == 'g' && s[2] == 'p')
7969                         {
7970                           s += 3;
7971                           regno = GP;
7972                         }
7973                       else if (s[1] == 'a' && s[2] == 't')
7974                         {
7975                           s += 3;
7976                           regno = AT;
7977                         }
7978                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7979                         {
7980                           s += 4;
7981                           regno = KT0;
7982                         }
7983                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7984                         {
7985                           s += 4;
7986                           regno = KT1;
7987                         }
7988                       else if (itbl_have_entries)
7989                         {
7990                           char *p, *n;
7991                           unsigned long r;
7992
7993                           p = s + 1;    /* advance past '$' */
7994                           n = itbl_get_field (&p);  /* n is name */
7995
7996                           /* See if this is a register defined in an
7997                              itbl entry.  */
7998                           if (itbl_get_reg_val (n, &r))
7999                             {
8000                               /* Get_field advances to the start of
8001                                  the next field, so we need to back
8002                                  rack to the end of the last field.  */
8003                               if (p)
8004                                 s = p - 1;
8005                               else
8006                                 s = strchr (s, '\0');
8007                               regno = r;
8008                             }
8009                           else
8010                             goto notreg;
8011                         }
8012                       else
8013                         goto notreg;
8014                     }
8015                   if (regno == AT
8016                       && ! mips_opts.noat
8017                       && *args != 'E'
8018                       && *args != 'G')
8019                     as_warn (_("Used $at without \".set noat\""));
8020                   c = *args;
8021                   if (*s == ' ')
8022                     s++;
8023                   if (args[1] != *s)
8024                     {
8025                       if (c == 'r' || c == 'v' || c == 'w')
8026                         {
8027                           regno = lastregno;
8028                           s = s_reset;
8029                           args++;
8030                         }
8031                     }
8032                   /* 'z' only matches $0.  */
8033                   if (c == 'z' && regno != 0)
8034                     break;
8035
8036         /* Now that we have assembled one operand, we use the args string
8037          * to figure out where it goes in the instruction.  */
8038                   switch (c)
8039                     {
8040                     case 'r':
8041                     case 's':
8042                     case 'v':
8043                     case 'b':
8044                       ip->insn_opcode |= regno << OP_SH_RS;
8045                       break;
8046                     case 'd':
8047                     case 'G':
8048                       ip->insn_opcode |= regno << OP_SH_RD;
8049                       break;
8050                     case 'U':
8051                       ip->insn_opcode |= regno << OP_SH_RD;
8052                       ip->insn_opcode |= regno << OP_SH_RT;
8053                       break;
8054                     case 'w':
8055                     case 't':
8056                     case 'E':
8057                       ip->insn_opcode |= regno << OP_SH_RT;
8058                       break;
8059                     case 'x':
8060                       /* This case exists because on the r3000 trunc
8061                          expands into a macro which requires a gp
8062                          register.  On the r6000 or r4000 it is
8063                          assembled into a single instruction which
8064                          ignores the register.  Thus the insn version
8065                          is MIPS_ISA2 and uses 'x', and the macro
8066                          version is MIPS_ISA1 and uses 't'.  */
8067                       break;
8068                     case 'z':
8069                       /* This case is for the div instruction, which
8070                          acts differently if the destination argument
8071                          is $0.  This only matches $0, and is checked
8072                          outside the switch.  */
8073                       break;
8074                     case 'D':
8075                       /* Itbl operand; not yet implemented. FIXME ?? */
8076                       break;
8077                       /* What about all other operands like 'i', which
8078                          can be specified in the opcode table? */
8079                     }
8080                   lastregno = regno;
8081                   continue;
8082                 }
8083             notreg:
8084               switch (*args++)
8085                 {
8086                 case 'r':
8087                 case 'v':
8088                   ip->insn_opcode |= lastregno << OP_SH_RS;
8089                   continue;
8090                 case 'w':
8091                   ip->insn_opcode |= lastregno << OP_SH_RT;
8092                   continue;
8093                 }
8094               break;
8095
8096             case 'D':           /* floating point destination register */
8097             case 'S':           /* floating point source register */
8098             case 'T':           /* floating point target register */
8099             case 'R':           /* floating point source register */
8100             case 'V':
8101             case 'W':
8102               s_reset = s;
8103               if (s[0] == '$' && s[1] == 'f'
8104                   && ISDIGIT (s[2]))
8105                 {
8106                   s += 2;
8107                   regno = 0;
8108                   do
8109                     {
8110                       regno *= 10;
8111                       regno += *s - '0';
8112                       ++s;
8113                     }
8114                   while (ISDIGIT (*s));
8115
8116                   if (regno > 31)
8117                     as_bad (_("Invalid float register number (%d)"), regno);
8118
8119                   if ((regno & 1) != 0
8120                       && HAVE_32BIT_FPRS
8121                       && ! (strcmp (str, "mtc1") == 0
8122                             || strcmp (str, "mfc1") == 0
8123                             || strcmp (str, "lwc1") == 0
8124                             || strcmp (str, "swc1") == 0
8125                             || strcmp (str, "l.s") == 0
8126                             || strcmp (str, "s.s") == 0))
8127                     as_warn (_("Float register should be even, was %d"),
8128                              regno);
8129
8130                   c = *args;
8131                   if (*s == ' ')
8132                     s++;
8133                   if (args[1] != *s)
8134                     {
8135                       if (c == 'V' || c == 'W')
8136                         {
8137                           regno = lastregno;
8138                           s = s_reset;
8139                           args++;
8140                         }
8141                     }
8142                   switch (c)
8143                     {
8144                     case 'D':
8145                       ip->insn_opcode |= regno << OP_SH_FD;
8146                       break;
8147                     case 'V':
8148                     case 'S':
8149                       ip->insn_opcode |= regno << OP_SH_FS;
8150                       break;
8151                     case 'W':
8152                     case 'T':
8153                       ip->insn_opcode |= regno << OP_SH_FT;
8154                       break;
8155                     case 'R':
8156                       ip->insn_opcode |= regno << OP_SH_FR;
8157                       break;
8158                     }
8159                   lastregno = regno;
8160                   continue;
8161                 }
8162
8163               switch (*args++)
8164                 {
8165                 case 'V':
8166                   ip->insn_opcode |= lastregno << OP_SH_FS;
8167                   continue;
8168                 case 'W':
8169                   ip->insn_opcode |= lastregno << OP_SH_FT;
8170                   continue;
8171                 }
8172               break;
8173
8174             case 'I':
8175               my_getExpression (&imm_expr, s);
8176               if (imm_expr.X_op != O_big
8177                   && imm_expr.X_op != O_constant)
8178                 insn_error = _("absolute expression required");
8179               s = expr_end;
8180               continue;
8181
8182             case 'A':
8183               my_getExpression (&offset_expr, s);
8184               *imm_reloc = BFD_RELOC_32;
8185               s = expr_end;
8186               continue;
8187
8188             case 'F':
8189             case 'L':
8190             case 'f':
8191             case 'l':
8192               {
8193                 int f64;
8194                 int using_gprs;
8195                 char *save_in;
8196                 char *err;
8197                 unsigned char temp[8];
8198                 int len;
8199                 unsigned int length;
8200                 segT seg;
8201                 subsegT subseg;
8202                 char *p;
8203
8204                 /* These only appear as the last operand in an
8205                    instruction, and every instruction that accepts
8206                    them in any variant accepts them in all variants.
8207                    This means we don't have to worry about backing out
8208                    any changes if the instruction does not match.
8209
8210                    The difference between them is the size of the
8211                    floating point constant and where it goes.  For 'F'
8212                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8213                    is 32 bits.  Where the constant is placed is based
8214                    on how the MIPS assembler does things:
8215                     F -- .rdata
8216                     L -- .lit8
8217                     f -- immediate value
8218                     l -- .lit4
8219
8220                     The .lit4 and .lit8 sections are only used if
8221                     permitted by the -G argument.
8222
8223                     When generating embedded PIC code, we use the
8224                     .lit8 section but not the .lit4 section (we can do
8225                     .lit4 inline easily; we need to put .lit8
8226                     somewhere in the data segment, and using .lit8
8227                     permits the linker to eventually combine identical
8228                     .lit8 entries).
8229
8230                     The code below needs to know whether the target register
8231                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8232                     'F' are used with GPR-based instructions and 'l' and
8233                     'L' are used with FPR-based instructions.  */
8234
8235                 f64 = *args == 'F' || *args == 'L';
8236                 using_gprs = *args == 'F' || *args == 'f';
8237
8238                 save_in = input_line_pointer;
8239                 input_line_pointer = s;
8240                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8241                 length = len;
8242                 s = input_line_pointer;
8243                 input_line_pointer = save_in;
8244                 if (err != NULL && *err != '\0')
8245                   {
8246                     as_bad (_("Bad floating point constant: %s"), err);
8247                     memset (temp, '\0', sizeof temp);
8248                     length = f64 ? 8 : 4;
8249                   }
8250
8251                 assert (length == (unsigned) (f64 ? 8 : 4));
8252
8253                 if (*args == 'f'
8254                     || (*args == 'l'
8255                         && (! USE_GLOBAL_POINTER_OPT
8256                             || mips_pic == EMBEDDED_PIC
8257                             || g_switch_value < 4
8258                             || (temp[0] == 0 && temp[1] == 0)
8259                             || (temp[2] == 0 && temp[3] == 0))))
8260                   {
8261                     imm_expr.X_op = O_constant;
8262                     if (! target_big_endian)
8263                       imm_expr.X_add_number = bfd_getl32 (temp);
8264                     else
8265                       imm_expr.X_add_number = bfd_getb32 (temp);
8266                   }
8267                 else if (length > 4
8268                          && ! mips_disable_float_construction
8269                          /* Constants can only be constructed in GPRs and
8270                             copied to FPRs if the GPRs are at least as wide
8271                             as the FPRs.  Force the constant into memory if
8272                             we are using 64-bit FPRs but the GPRs are only
8273                             32 bits wide.  */
8274                          && (using_gprs
8275                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8276                          && ((temp[0] == 0 && temp[1] == 0)
8277                              || (temp[2] == 0 && temp[3] == 0))
8278                          && ((temp[4] == 0 && temp[5] == 0)
8279                              || (temp[6] == 0 && temp[7] == 0)))
8280                   {
8281                     /* The value is simple enough to load with a couple of
8282                        instructions.  If using 32-bit registers, set
8283                        imm_expr to the high order 32 bits and offset_expr to
8284                        the low order 32 bits.  Otherwise, set imm_expr to
8285                        the entire 64 bit constant.  */
8286                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8287                       {
8288                         imm_expr.X_op = O_constant;
8289                         offset_expr.X_op = O_constant;
8290                         if (! target_big_endian)
8291                           {
8292                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8293                             offset_expr.X_add_number = bfd_getl32 (temp);
8294                           }
8295                         else
8296                           {
8297                             imm_expr.X_add_number = bfd_getb32 (temp);
8298                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8299                           }
8300                         if (offset_expr.X_add_number == 0)
8301                           offset_expr.X_op = O_absent;
8302                       }
8303                     else if (sizeof (imm_expr.X_add_number) > 4)
8304                       {
8305                         imm_expr.X_op = O_constant;
8306                         if (! target_big_endian)
8307                           imm_expr.X_add_number = bfd_getl64 (temp);
8308                         else
8309                           imm_expr.X_add_number = bfd_getb64 (temp);
8310                       }
8311                     else
8312                       {
8313                         imm_expr.X_op = O_big;
8314                         imm_expr.X_add_number = 4;
8315                         if (! target_big_endian)
8316                           {
8317                             generic_bignum[0] = bfd_getl16 (temp);
8318                             generic_bignum[1] = bfd_getl16 (temp + 2);
8319                             generic_bignum[2] = bfd_getl16 (temp + 4);
8320                             generic_bignum[3] = bfd_getl16 (temp + 6);
8321                           }
8322                         else
8323                           {
8324                             generic_bignum[0] = bfd_getb16 (temp + 6);
8325                             generic_bignum[1] = bfd_getb16 (temp + 4);
8326                             generic_bignum[2] = bfd_getb16 (temp + 2);
8327                             generic_bignum[3] = bfd_getb16 (temp);
8328                           }
8329                       }
8330                   }
8331                 else
8332                   {
8333                     const char *newname;
8334                     segT new_seg;
8335
8336                     /* Switch to the right section.  */
8337                     seg = now_seg;
8338                     subseg = now_subseg;
8339                     switch (*args)
8340                       {
8341                       default: /* unused default case avoids warnings.  */
8342                       case 'L':
8343                         newname = RDATA_SECTION_NAME;
8344                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8345                             || mips_pic == EMBEDDED_PIC)
8346                           newname = ".lit8";
8347                         break;
8348                       case 'F':
8349                         if (mips_pic == EMBEDDED_PIC)
8350                           newname = ".lit8";
8351                         else
8352                           newname = RDATA_SECTION_NAME;
8353                         break;
8354                       case 'l':
8355                         assert (!USE_GLOBAL_POINTER_OPT
8356                                 || g_switch_value >= 4);
8357                         newname = ".lit4";
8358                         break;
8359                       }
8360                     new_seg = subseg_new (newname, (subsegT) 0);
8361                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8362                       bfd_set_section_flags (stdoutput, new_seg,
8363                                              (SEC_ALLOC
8364                                               | SEC_LOAD
8365                                               | SEC_READONLY
8366                                               | SEC_DATA));
8367                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8368                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8369                         && strcmp (TARGET_OS, "elf") != 0)
8370                       record_alignment (new_seg, 4);
8371                     else
8372                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8373                     if (seg == now_seg)
8374                       as_bad (_("Can't use floating point insn in this section"));
8375
8376                     /* Set the argument to the current address in the
8377                        section.  */
8378                     offset_expr.X_op = O_symbol;
8379                     offset_expr.X_add_symbol =
8380                       symbol_new ("L0\001", now_seg,
8381                                   (valueT) frag_now_fix (), frag_now);
8382                     offset_expr.X_add_number = 0;
8383
8384                     /* Put the floating point number into the section.  */
8385                     p = frag_more ((int) length);
8386                     memcpy (p, temp, length);
8387
8388                     /* Switch back to the original section.  */
8389                     subseg_set (seg, subseg);
8390                   }
8391               }
8392               continue;
8393
8394             case 'i':           /* 16 bit unsigned immediate */
8395             case 'j':           /* 16 bit signed immediate */
8396               *imm_reloc = BFD_RELOC_LO16;
8397               c = my_getSmallExpression (&imm_expr, s);
8398               if (c != S_EX_NONE)
8399                 {
8400                   if (c != S_EX_LO)
8401                     {
8402                       if (imm_expr.X_op == O_constant)
8403                         imm_expr.X_add_number =
8404                           (imm_expr.X_add_number >> 16) & 0xffff;
8405 #ifdef OBJ_ELF
8406                       else if (c == S_EX_HIGHEST)
8407                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8408                       else if (c == S_EX_HIGHER)
8409                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8410                       else if (c == S_EX_GP_REL)
8411                         {
8412                           /* This occurs in NewABI only.  */
8413                           c = my_getSmallExpression (&imm_expr, s);
8414                           if (c != S_EX_NEG)
8415                             as_bad (_("bad composition of relocations"));
8416                           else
8417                             {
8418                               c = my_getSmallExpression (&imm_expr, s);
8419                               if (c != S_EX_LO)
8420                                 as_bad (_("bad composition of relocations"));
8421                               else
8422                                 {
8423                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8424                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8425                                   imm_reloc[2] = BFD_RELOC_LO16;
8426                                 }
8427                             }
8428                         }
8429 #endif
8430                       else if (c == S_EX_HI)
8431                         {
8432                           *imm_reloc = BFD_RELOC_HI16_S;
8433                           imm_unmatched_hi = true;
8434                         }
8435                       else
8436                         *imm_reloc = BFD_RELOC_HI16;
8437                     }
8438                   else if (imm_expr.X_op == O_constant)
8439                     imm_expr.X_add_number &= 0xffff;
8440                 }
8441               if (*args == 'i')
8442                 {
8443                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8444                       || ((imm_expr.X_add_number < 0
8445                            || imm_expr.X_add_number >= 0x10000)
8446                           && imm_expr.X_op == O_constant))
8447                     {
8448                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8449                           !strcmp (insn->name, insn[1].name))
8450                         break;
8451                       if (imm_expr.X_op == O_constant
8452                           || imm_expr.X_op == O_big)
8453                         as_bad (_("16 bit expression not in range 0..65535"));
8454                     }
8455                 }
8456               else
8457                 {
8458                   int more;
8459                   offsetT max;
8460
8461                   /* The upper bound should be 0x8000, but
8462                      unfortunately the MIPS assembler accepts numbers
8463                      from 0x8000 to 0xffff and sign extends them, and
8464                      we want to be compatible.  We only permit this
8465                      extended range for an instruction which does not
8466                      provide any further alternates, since those
8467                      alternates may handle other cases.  People should
8468                      use the numbers they mean, rather than relying on
8469                      a mysterious sign extension.  */
8470                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8471                           strcmp (insn->name, insn[1].name) == 0);
8472                   if (more)
8473                     max = 0x8000;
8474                   else
8475                     max = 0x10000;
8476                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8477                       || ((imm_expr.X_add_number < -0x8000
8478                            || imm_expr.X_add_number >= max)
8479                           && imm_expr.X_op == O_constant)
8480                       || (more
8481                           && imm_expr.X_add_number < 0
8482                           && HAVE_64BIT_GPRS
8483                           && imm_expr.X_unsigned
8484                           && sizeof (imm_expr.X_add_number) <= 4))
8485                     {
8486                       if (more)
8487                         break;
8488                       if (imm_expr.X_op == O_constant
8489                           || imm_expr.X_op == O_big)
8490                         as_bad (_("16 bit expression not in range -32768..32767"));
8491                     }
8492                 }
8493               s = expr_end;
8494               continue;
8495
8496             case 'o':           /* 16 bit offset */
8497               c = my_getSmallExpression (&offset_expr, s);
8498
8499               /* If this value won't fit into a 16 bit offset, then go
8500                  find a macro that will generate the 32 bit offset
8501                  code pattern.  */
8502               if (c == S_EX_NONE
8503                   && (offset_expr.X_op != O_constant
8504                       || offset_expr.X_add_number >= 0x8000
8505                       || offset_expr.X_add_number < -0x8000))
8506                 break;
8507
8508               if (c == S_EX_HI)
8509                 {
8510                   if (offset_expr.X_op != O_constant)
8511                     break;
8512                   offset_expr.X_add_number =
8513                     (offset_expr.X_add_number >> 16) & 0xffff;
8514                 }
8515               *offset_reloc = BFD_RELOC_LO16;
8516               s = expr_end;
8517               continue;
8518
8519             case 'p':           /* pc relative offset */
8520               if (mips_pic == EMBEDDED_PIC)
8521                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8522               else
8523                 *offset_reloc = BFD_RELOC_16_PCREL;
8524               my_getExpression (&offset_expr, s);
8525               s = expr_end;
8526               continue;
8527
8528             case 'u':           /* upper 16 bits */
8529               c = my_getSmallExpression (&imm_expr, s);
8530               *imm_reloc = BFD_RELOC_LO16;
8531               if (c != S_EX_NONE)
8532                 {
8533                   if (c != S_EX_LO)
8534                     {
8535                       if (imm_expr.X_op == O_constant)
8536                         imm_expr.X_add_number =
8537                           (imm_expr.X_add_number >> 16) & 0xffff;
8538                       else if (c == S_EX_HI)
8539                         {
8540                           *imm_reloc = BFD_RELOC_HI16_S;
8541                           imm_unmatched_hi = true;
8542                         }
8543 #ifdef OBJ_ELF
8544                       else if (c == S_EX_HIGHEST)
8545                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8546                       else if (c == S_EX_GP_REL)
8547                         {
8548                           /* This occurs in NewABI only.  */
8549                           c = my_getSmallExpression (&imm_expr, s);
8550                           if (c != S_EX_NEG)
8551                             as_bad (_("bad composition of relocations"));
8552                           else
8553                             {
8554                               c = my_getSmallExpression (&imm_expr, s);
8555                               if (c != S_EX_HI)
8556                                 as_bad (_("bad composition of relocations"));
8557                               else
8558                                 {
8559                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8560                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8561                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8562                                 }
8563                             }
8564                         }
8565 #endif
8566                       else
8567                         *imm_reloc = BFD_RELOC_HI16;
8568                     }
8569                   else if (imm_expr.X_op == O_constant)
8570                     imm_expr.X_add_number &= 0xffff;
8571                 }
8572               if (imm_expr.X_op == O_constant
8573                   && (imm_expr.X_add_number < 0
8574                       || imm_expr.X_add_number >= 0x10000))
8575                 as_bad (_("lui expression not in range 0..65535"));
8576               s = expr_end;
8577               continue;
8578
8579             case 'a':           /* 26 bit address */
8580               my_getExpression (&offset_expr, s);
8581               s = expr_end;
8582               *offset_reloc = BFD_RELOC_MIPS_JMP;
8583               continue;
8584
8585             case 'N':           /* 3 bit branch condition code */
8586             case 'M':           /* 3 bit compare condition code */
8587               if (strncmp (s, "$fcc", 4) != 0)
8588                 break;
8589               s += 4;
8590               regno = 0;
8591               do
8592                 {
8593                   regno *= 10;
8594                   regno += *s - '0';
8595                   ++s;
8596                 }
8597               while (ISDIGIT (*s));
8598               if (regno > 7)
8599                 as_bad (_("invalid condition code register $fcc%d"), regno);
8600               if (*args == 'N')
8601                 ip->insn_opcode |= regno << OP_SH_BCC;
8602               else
8603                 ip->insn_opcode |= regno << OP_SH_CCC;
8604               continue;
8605
8606             case 'H':
8607               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8608                 s += 2;
8609               if (ISDIGIT (*s))
8610                 {
8611                   c = 0;
8612                   do
8613                     {
8614                       c *= 10;
8615                       c += *s - '0';
8616                       ++s;
8617                     }
8618                   while (ISDIGIT (*s));
8619                 }
8620               else
8621                 c = 8; /* Invalid sel value.  */
8622
8623               if (c > 7)
8624                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8625               ip->insn_opcode |= c;
8626               continue;
8627
8628             default:
8629               as_bad (_("bad char = '%c'\n"), *args);
8630               internalError ();
8631             }
8632           break;
8633         }
8634       /* Args don't match.  */
8635       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8636           !strcmp (insn->name, insn[1].name))
8637         {
8638           ++insn;
8639           s = argsStart;
8640           insn_error = _("illegal operands");
8641           continue;
8642         }
8643       if (save_c)
8644         *(--s) = save_c;
8645       insn_error = _("illegal operands");
8646       return;
8647     }
8648 }
8649
8650 /* This routine assembles an instruction into its binary format when
8651    assembling for the mips16.  As a side effect, it sets one of the
8652    global variables imm_reloc or offset_reloc to the type of
8653    relocation to do if one of the operands is an address expression.
8654    It also sets mips16_small and mips16_ext if the user explicitly
8655    requested a small or extended instruction.  */
8656
8657 static void
8658 mips16_ip (str, ip)
8659      char *str;
8660      struct mips_cl_insn *ip;
8661 {
8662   char *s;
8663   const char *args;
8664   struct mips_opcode *insn;
8665   char *argsstart;
8666   unsigned int regno;
8667   unsigned int lastregno = 0;
8668   char *s_reset;
8669
8670   insn_error = NULL;
8671
8672   mips16_small = false;
8673   mips16_ext = false;
8674
8675   for (s = str; ISLOWER (*s); ++s)
8676     ;
8677   switch (*s)
8678     {
8679     case '\0':
8680       break;
8681
8682     case ' ':
8683       *s++ = '\0';
8684       break;
8685
8686     case '.':
8687       if (s[1] == 't' && s[2] == ' ')
8688         {
8689           *s = '\0';
8690           mips16_small = true;
8691           s += 3;
8692           break;
8693         }
8694       else if (s[1] == 'e' && s[2] == ' ')
8695         {
8696           *s = '\0';
8697           mips16_ext = true;
8698           s += 3;
8699           break;
8700         }
8701       /* Fall through.  */
8702     default:
8703       insn_error = _("unknown opcode");
8704       return;
8705     }
8706
8707   if (mips_opts.noautoextend && ! mips16_ext)
8708     mips16_small = true;
8709
8710   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8711     {
8712       insn_error = _("unrecognized opcode");
8713       return;
8714     }
8715
8716   argsstart = s;
8717   for (;;)
8718     {
8719       assert (strcmp (insn->name, str) == 0);
8720
8721       ip->insn_mo = insn;
8722       ip->insn_opcode = insn->match;
8723       ip->use_extend = false;
8724       imm_expr.X_op = O_absent;
8725       imm_reloc[0] = BFD_RELOC_UNUSED;
8726       imm_reloc[1] = BFD_RELOC_UNUSED;
8727       imm_reloc[2] = BFD_RELOC_UNUSED;
8728       offset_expr.X_op = O_absent;
8729       offset_reloc[0] = BFD_RELOC_UNUSED;
8730       offset_reloc[1] = BFD_RELOC_UNUSED;
8731       offset_reloc[2] = BFD_RELOC_UNUSED;
8732       for (args = insn->args; 1; ++args)
8733         {
8734           int c;
8735
8736           if (*s == ' ')
8737             ++s;
8738
8739           /* In this switch statement we call break if we did not find
8740              a match, continue if we did find a match, or return if we
8741              are done.  */
8742
8743           c = *args;
8744           switch (c)
8745             {
8746             case '\0':
8747               if (*s == '\0')
8748                 {
8749                   /* Stuff the immediate value in now, if we can.  */
8750                   if (imm_expr.X_op == O_constant
8751                       && *imm_reloc > BFD_RELOC_UNUSED
8752                       && insn->pinfo != INSN_MACRO)
8753                     {
8754                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8755                                     imm_expr.X_add_number, true, mips16_small,
8756                                     mips16_ext, &ip->insn_opcode,
8757                                     &ip->use_extend, &ip->extend);
8758                       imm_expr.X_op = O_absent;
8759                       *imm_reloc = BFD_RELOC_UNUSED;
8760                     }
8761
8762                   return;
8763                 }
8764               break;
8765
8766             case ',':
8767               if (*s++ == c)
8768                 continue;
8769               s--;
8770               switch (*++args)
8771                 {
8772                 case 'v':
8773                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8774                   continue;
8775                 case 'w':
8776                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8777                   continue;
8778                 }
8779               break;
8780
8781             case '(':
8782             case ')':
8783               if (*s++ == c)
8784                 continue;
8785               break;
8786
8787             case 'v':
8788             case 'w':
8789               if (s[0] != '$')
8790                 {
8791                   if (c == 'v')
8792                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8793                   else
8794                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8795                   ++args;
8796                   continue;
8797                 }
8798               /* Fall through.  */
8799             case 'x':
8800             case 'y':
8801             case 'z':
8802             case 'Z':
8803             case '0':
8804             case 'S':
8805             case 'R':
8806             case 'X':
8807             case 'Y':
8808               if (s[0] != '$')
8809                 break;
8810               s_reset = s;
8811               if (ISDIGIT (s[1]))
8812                 {
8813                   ++s;
8814                   regno = 0;
8815                   do
8816                     {
8817                       regno *= 10;
8818                       regno += *s - '0';
8819                       ++s;
8820                     }
8821                   while (ISDIGIT (*s));
8822                   if (regno > 31)
8823                     {
8824                       as_bad (_("invalid register number (%d)"), regno);
8825                       regno = 2;
8826                     }
8827                 }
8828               else
8829                 {
8830                   if (s[1] == 'f' && s[2] == 'p')
8831                     {
8832                       s += 3;
8833                       regno = FP;
8834                     }
8835                   else if (s[1] == 's' && s[2] == 'p')
8836                     {
8837                       s += 3;
8838                       regno = SP;
8839                     }
8840                   else if (s[1] == 'g' && s[2] == 'p')
8841                     {
8842                       s += 3;
8843                       regno = GP;
8844                     }
8845                   else if (s[1] == 'a' && s[2] == 't')
8846                     {
8847                       s += 3;
8848                       regno = AT;
8849                     }
8850                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8851                     {
8852                       s += 4;
8853                       regno = KT0;
8854                     }
8855                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8856                     {
8857                       s += 4;
8858                       regno = KT1;
8859                     }
8860                   else
8861                     break;
8862                 }
8863
8864               if (*s == ' ')
8865                 ++s;
8866               if (args[1] != *s)
8867                 {
8868                   if (c == 'v' || c == 'w')
8869                     {
8870                       regno = mips16_to_32_reg_map[lastregno];
8871                       s = s_reset;
8872                       args++;
8873                     }
8874                 }
8875
8876               switch (c)
8877                 {
8878                 case 'x':
8879                 case 'y':
8880                 case 'z':
8881                 case 'v':
8882                 case 'w':
8883                 case 'Z':
8884                   regno = mips32_to_16_reg_map[regno];
8885                   break;
8886
8887                 case '0':
8888                   if (regno != 0)
8889                     regno = ILLEGAL_REG;
8890                   break;
8891
8892                 case 'S':
8893                   if (regno != SP)
8894                     regno = ILLEGAL_REG;
8895                   break;
8896
8897                 case 'R':
8898                   if (regno != RA)
8899                     regno = ILLEGAL_REG;
8900                   break;
8901
8902                 case 'X':
8903                 case 'Y':
8904                   if (regno == AT && ! mips_opts.noat)
8905                     as_warn (_("used $at without \".set noat\""));
8906                   break;
8907
8908                 default:
8909                   internalError ();
8910                 }
8911
8912               if (regno == ILLEGAL_REG)
8913                 break;
8914
8915               switch (c)
8916                 {
8917                 case 'x':
8918                 case 'v':
8919                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8920                   break;
8921                 case 'y':
8922                 case 'w':
8923                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8924                   break;
8925                 case 'z':
8926                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8927                   break;
8928                 case 'Z':
8929                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8930                 case '0':
8931                 case 'S':
8932                 case 'R':
8933                   break;
8934                 case 'X':
8935                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8936                   break;
8937                 case 'Y':
8938                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8939                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8940                   break;
8941                 default:
8942                   internalError ();
8943                 }
8944
8945               lastregno = regno;
8946               continue;
8947
8948             case 'P':
8949               if (strncmp (s, "$pc", 3) == 0)
8950                 {
8951                   s += 3;
8952                   continue;
8953                 }
8954               break;
8955
8956             case '<':
8957             case '>':
8958             case '[':
8959             case ']':
8960             case '4':
8961             case '5':
8962             case 'H':
8963             case 'W':
8964             case 'D':
8965             case 'j':
8966             case '8':
8967             case 'V':
8968             case 'C':
8969             case 'U':
8970             case 'k':
8971             case 'K':
8972               if (s[0] == '%'
8973                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8974                 {
8975                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
8976                      and generate the appropriate reloc.  If the text
8977                      inside %gprel is not a symbol name with an
8978                      optional offset, then we generate a normal reloc
8979                      and will probably fail later.  */
8980                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8981                   if (imm_expr.X_op == O_symbol)
8982                     {
8983                       mips16_ext = true;
8984                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8985                       s = expr_end;
8986                       ip->use_extend = true;
8987                       ip->extend = 0;
8988                       continue;
8989                     }
8990                 }
8991               else
8992                 {
8993                   /* Just pick up a normal expression.  */
8994                   my_getExpression (&imm_expr, s);
8995                 }
8996
8997               if (imm_expr.X_op == O_register)
8998                 {
8999                   /* What we thought was an expression turned out to
9000                      be a register.  */
9001
9002                   if (s[0] == '(' && args[1] == '(')
9003                     {
9004                       /* It looks like the expression was omitted
9005                          before a register indirection, which means
9006                          that the expression is implicitly zero.  We
9007                          still set up imm_expr, so that we handle
9008                          explicit extensions correctly.  */
9009                       imm_expr.X_op = O_constant;
9010                       imm_expr.X_add_number = 0;
9011                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9012                       continue;
9013                     }
9014
9015                   break;
9016                 }
9017
9018               /* We need to relax this instruction.  */
9019               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9020               s = expr_end;
9021               continue;
9022
9023             case 'p':
9024             case 'q':
9025             case 'A':
9026             case 'B':
9027             case 'E':
9028               /* We use offset_reloc rather than imm_reloc for the PC
9029                  relative operands.  This lets macros with both
9030                  immediate and address operands work correctly.  */
9031               my_getExpression (&offset_expr, s);
9032
9033               if (offset_expr.X_op == O_register)
9034                 break;
9035
9036               /* We need to relax this instruction.  */
9037               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9038               s = expr_end;
9039               continue;
9040
9041             case '6':           /* break code */
9042               my_getExpression (&imm_expr, s);
9043               check_absolute_expr (ip, &imm_expr);
9044               if ((unsigned long) imm_expr.X_add_number > 63)
9045                 {
9046                   as_warn (_("Invalid value for `%s' (%lu)"),
9047                            ip->insn_mo->name,
9048                            (unsigned long) imm_expr.X_add_number);
9049                   imm_expr.X_add_number &= 0x3f;
9050                 }
9051               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9052               imm_expr.X_op = O_absent;
9053               s = expr_end;
9054               continue;
9055
9056             case 'a':           /* 26 bit address */
9057               my_getExpression (&offset_expr, s);
9058               s = expr_end;
9059               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9060               ip->insn_opcode <<= 16;
9061               continue;
9062
9063             case 'l':           /* register list for entry macro */
9064             case 'L':           /* register list for exit macro */
9065               {
9066                 int mask;
9067
9068                 if (c == 'l')
9069                   mask = 0;
9070                 else
9071                   mask = 7 << 3;
9072                 while (*s != '\0')
9073                   {
9074                     int freg, reg1, reg2;
9075
9076                     while (*s == ' ' || *s == ',')
9077                       ++s;
9078                     if (*s != '$')
9079                       {
9080                         as_bad (_("can't parse register list"));
9081                         break;
9082                       }
9083                     ++s;
9084                     if (*s != 'f')
9085                       freg = 0;
9086                     else
9087                       {
9088                         freg = 1;
9089                         ++s;
9090                       }
9091                     reg1 = 0;
9092                     while (ISDIGIT (*s))
9093                       {
9094                         reg1 *= 10;
9095                         reg1 += *s - '0';
9096                         ++s;
9097                       }
9098                     if (*s == ' ')
9099                       ++s;
9100                     if (*s != '-')
9101                       reg2 = reg1;
9102                     else
9103                       {
9104                         ++s;
9105                         if (*s != '$')
9106                           break;
9107                         ++s;
9108                         if (freg)
9109                           {
9110                             if (*s == 'f')
9111                               ++s;
9112                             else
9113                               {
9114                                 as_bad (_("invalid register list"));
9115                                 break;
9116                               }
9117                           }
9118                         reg2 = 0;
9119                         while (ISDIGIT (*s))
9120                           {
9121                             reg2 *= 10;
9122                             reg2 += *s - '0';
9123                             ++s;
9124                           }
9125                       }
9126                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9127                       {
9128                         mask &= ~ (7 << 3);
9129                         mask |= 5 << 3;
9130                       }
9131                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9132                       {
9133                         mask &= ~ (7 << 3);
9134                         mask |= 6 << 3;
9135                       }
9136                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9137                       mask |= (reg2 - 3) << 3;
9138                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9139                       mask |= (reg2 - 15) << 1;
9140                     else if (reg1 == 31 && reg2 == 31)
9141                       mask |= 1;
9142                     else
9143                       {
9144                         as_bad (_("invalid register list"));
9145                         break;
9146                       }
9147                   }
9148                 /* The mask is filled in in the opcode table for the
9149                    benefit of the disassembler.  We remove it before
9150                    applying the actual mask.  */
9151                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9152                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9153               }
9154             continue;
9155
9156             case 'e':           /* extend code */
9157               my_getExpression (&imm_expr, s);
9158               check_absolute_expr (ip, &imm_expr);
9159               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9160                 {
9161                   as_warn (_("Invalid value for `%s' (%lu)"),
9162                            ip->insn_mo->name,
9163                            (unsigned long) imm_expr.X_add_number);
9164                   imm_expr.X_add_number &= 0x7ff;
9165                 }
9166               ip->insn_opcode |= imm_expr.X_add_number;
9167               imm_expr.X_op = O_absent;
9168               s = expr_end;
9169               continue;
9170
9171             default:
9172               internalError ();
9173             }
9174           break;
9175         }
9176
9177       /* Args don't match.  */
9178       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9179           strcmp (insn->name, insn[1].name) == 0)
9180         {
9181           ++insn;
9182           s = argsstart;
9183           continue;
9184         }
9185
9186       insn_error = _("illegal operands");
9187
9188       return;
9189     }
9190 }
9191
9192 /* This structure holds information we know about a mips16 immediate
9193    argument type.  */
9194
9195 struct mips16_immed_operand
9196 {
9197   /* The type code used in the argument string in the opcode table.  */
9198   int type;
9199   /* The number of bits in the short form of the opcode.  */
9200   int nbits;
9201   /* The number of bits in the extended form of the opcode.  */
9202   int extbits;
9203   /* The amount by which the short form is shifted when it is used;
9204      for example, the sw instruction has a shift count of 2.  */
9205   int shift;
9206   /* The amount by which the short form is shifted when it is stored
9207      into the instruction code.  */
9208   int op_shift;
9209   /* Non-zero if the short form is unsigned.  */
9210   int unsp;
9211   /* Non-zero if the extended form is unsigned.  */
9212   int extu;
9213   /* Non-zero if the value is PC relative.  */
9214   int pcrel;
9215 };
9216
9217 /* The mips16 immediate operand types.  */
9218
9219 static const struct mips16_immed_operand mips16_immed_operands[] =
9220 {
9221   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9222   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9223   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9224   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9225   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9226   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9227   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9228   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9229   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9230   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9231   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9232   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9233   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9234   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9235   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9236   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9237   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9238   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9239   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9240   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9241   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9242 };
9243
9244 #define MIPS16_NUM_IMMED \
9245   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9246
9247 /* Handle a mips16 instruction with an immediate value.  This or's the
9248    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9249    whether an extended value is needed; if one is needed, it sets
9250    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9251    If SMALL is true, an unextended opcode was explicitly requested.
9252    If EXT is true, an extended opcode was explicitly requested.  If
9253    WARN is true, warn if EXT does not match reality.  */
9254
9255 static void
9256 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9257               extend)
9258      char *file;
9259      unsigned int line;
9260      int type;
9261      offsetT val;
9262      boolean warn;
9263      boolean small;
9264      boolean ext;
9265      unsigned long *insn;
9266      boolean *use_extend;
9267      unsigned short *extend;
9268 {
9269   register const struct mips16_immed_operand *op;
9270   int mintiny, maxtiny;
9271   boolean needext;
9272
9273   op = mips16_immed_operands;
9274   while (op->type != type)
9275     {
9276       ++op;
9277       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9278     }
9279
9280   if (op->unsp)
9281     {
9282       if (type == '<' || type == '>' || type == '[' || type == ']')
9283         {
9284           mintiny = 1;
9285           maxtiny = 1 << op->nbits;
9286         }
9287       else
9288         {
9289           mintiny = 0;
9290           maxtiny = (1 << op->nbits) - 1;
9291         }
9292     }
9293   else
9294     {
9295       mintiny = - (1 << (op->nbits - 1));
9296       maxtiny = (1 << (op->nbits - 1)) - 1;
9297     }
9298
9299   /* Branch offsets have an implicit 0 in the lowest bit.  */
9300   if (type == 'p' || type == 'q')
9301     val /= 2;
9302
9303   if ((val & ((1 << op->shift) - 1)) != 0
9304       || val < (mintiny << op->shift)
9305       || val > (maxtiny << op->shift))
9306     needext = true;
9307   else
9308     needext = false;
9309
9310   if (warn && ext && ! needext)
9311     as_warn_where (file, line,
9312                    _("extended operand requested but not required"));
9313   if (small && needext)
9314     as_bad_where (file, line, _("invalid unextended operand value"));
9315
9316   if (small || (! ext && ! needext))
9317     {
9318       int insnval;
9319
9320       *use_extend = false;
9321       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9322       insnval <<= op->op_shift;
9323       *insn |= insnval;
9324     }
9325   else
9326     {
9327       long minext, maxext;
9328       int extval;
9329
9330       if (op->extu)
9331         {
9332           minext = 0;
9333           maxext = (1 << op->extbits) - 1;
9334         }
9335       else
9336         {
9337           minext = - (1 << (op->extbits - 1));
9338           maxext = (1 << (op->extbits - 1)) - 1;
9339         }
9340       if (val < minext || val > maxext)
9341         as_bad_where (file, line,
9342                       _("operand value out of range for instruction"));
9343
9344       *use_extend = true;
9345       if (op->extbits == 16)
9346         {
9347           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9348           val &= 0x1f;
9349         }
9350       else if (op->extbits == 15)
9351         {
9352           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9353           val &= 0xf;
9354         }
9355       else
9356         {
9357           extval = ((val & 0x1f) << 6) | (val & 0x20);
9358           val = 0;
9359         }
9360
9361       *extend = (unsigned short) extval;
9362       *insn |= val;
9363     }
9364 }
9365 \f
9366 static struct percent_op_match
9367 {
9368    const char *str;
9369    const enum small_ex_type type;
9370 } percent_op[] =
9371 {
9372   {"%lo", S_EX_LO},
9373 #ifdef OBJ_ELF
9374   {"%call_hi", S_EX_CALL_HI},
9375   {"%call_lo", S_EX_CALL_LO},
9376   {"%call16", S_EX_CALL16},
9377   {"%got_disp", S_EX_GOT_DISP},
9378   {"%got_page", S_EX_GOT_PAGE},
9379   {"%got_ofst", S_EX_GOT_OFST},
9380   {"%got_hi", S_EX_GOT_HI},
9381   {"%got_lo", S_EX_GOT_LO},
9382   {"%got", S_EX_GOT},
9383   {"%gp_rel", S_EX_GP_REL},
9384   {"%half", S_EX_HALF},
9385   {"%highest", S_EX_HIGHEST},
9386   {"%higher", S_EX_HIGHER},
9387   {"%neg", S_EX_NEG},
9388 #endif
9389   {"%hi", S_EX_HI}
9390 };
9391
9392 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9393    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9394    can be nested, this is handled by blanking the innermost, parsing the
9395    rest by subsequent calls.  */
9396
9397 static int
9398 my_getSmallParser (str, len, nestlevel)
9399      char **str;
9400      unsigned int *len;
9401      int *nestlevel;
9402 {
9403   *len = 0;
9404   *str += strspn (*str, " \t");
9405   /* Check for expression in parentheses.  */
9406   if (**str == '(')
9407     {
9408       char *b = *str + 1 + strspn (*str + 1, " \t");
9409       char *e;
9410
9411       /* Check for base register.  */
9412       if (b[0] == '$')
9413         {
9414           if (strchr (b, ')')
9415               && (e = b + strcspn (b, ") \t"))
9416               && e - b > 1 && e - b < 4)
9417             {
9418               if ((e - b == 3
9419                    && ((b[1] == 'f' && b[2] == 'p')
9420                        || (b[1] == 's' && b[2] == 'p')
9421                        || (b[1] == 'g' && b[2] == 'p')
9422                        || (b[1] == 'a' && b[2] == 't')
9423                        || (ISDIGIT (b[1])
9424                            && ISDIGIT (b[2]))))
9425                   || (ISDIGIT (b[1])))
9426                 {
9427                   *len = strcspn (*str, ")") + 1;
9428                   return S_EX_REGISTER;
9429                 }
9430             }
9431         }
9432       /* Check for percent_op (in parentheses).  */
9433       else if (b[0] == '%')
9434         {
9435           *str = b;
9436           return my_getPercentOp (str, len, nestlevel);
9437         }
9438
9439       /* Some other expression in the parentheses, which can contain
9440          parentheses itself. Attempt to find the matching one.  */
9441       {
9442         int pcnt = 1;
9443         char *s;
9444
9445         *len = 1;
9446         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9447           {
9448             if (*s == '(')
9449               pcnt++;
9450             else if (*s == ')')
9451               pcnt--;
9452           }
9453       }
9454     }
9455   /* Check for percent_op (outside of parentheses).  */
9456   else if (*str[0] == '%')
9457     return my_getPercentOp (str, len, nestlevel);
9458
9459   /* Any other expression.  */
9460   return S_EX_NONE;
9461 }
9462
9463 static int
9464 my_getPercentOp (str, len, nestlevel)
9465      char **str;
9466      unsigned int *len;
9467      int *nestlevel;
9468 {
9469   char *tmp = *str + 1;
9470   unsigned int i = 0;
9471
9472   while (ISALPHA (*tmp) || *tmp == '_')
9473     {
9474       *tmp = TOLOWER (*tmp);
9475       tmp++;
9476     }
9477   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9478     {
9479       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9480         i++;
9481       else
9482         {
9483           int type = percent_op[i].type;
9484
9485           /* Only %hi and %lo are allowed for OldABI.  */
9486           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9487             return S_EX_NONE;
9488
9489           *len = strlen (percent_op[i].str);
9490           (*nestlevel)++;
9491           return type;
9492         }
9493     }
9494   return S_EX_NONE;
9495 }
9496
9497 static int
9498 my_getSmallExpression (ep, str)
9499      expressionS *ep;
9500      char *str;
9501 {
9502   static char *oldstr = NULL;
9503   int c = S_EX_NONE;
9504   int oldc;
9505   int nestlevel = -1;
9506   unsigned int len;
9507
9508   /* Don't update oldstr if the last call had nested percent_op's. We need
9509      it to parse the outer ones later.  */
9510   if (! oldstr)
9511     oldstr = str;
9512
9513   do
9514     {
9515       oldc = c;
9516       c = my_getSmallParser (&str, &len, &nestlevel);
9517       if (c != S_EX_NONE && c != S_EX_REGISTER)
9518         str += len;
9519     }
9520   while (c != S_EX_NONE && c != S_EX_REGISTER);
9521
9522   if (nestlevel >= 0)
9523     {
9524       /* A percent_op was encountered.  Don't try to get an expression if
9525          it is already blanked out.  */
9526       if (*(str + strspn (str + 1, " )")) != ')')
9527         {
9528           char save;
9529
9530           /* Let my_getExpression() stop at the closing parenthesis.  */
9531           save = *(str + len);
9532           *(str + len) = '\0';
9533           my_getExpression (ep, str);
9534           *(str + len) = save;
9535         }
9536       if (nestlevel > 0)
9537         {
9538           /* Blank out including the % sign and the proper matching
9539              parenthesis.  */
9540           int pcnt = 1;
9541           char *s = strrchr (oldstr, '%');
9542           char *end;
9543
9544           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9545             {
9546               if (*end == '(')
9547                 pcnt++;
9548               else if (*end == ')')
9549                 pcnt--;
9550             }
9551
9552           memset (s, ' ', end - s);
9553           str = oldstr;
9554         }
9555       else
9556         expr_end = str + len;
9557
9558       c = oldc;
9559     }
9560   else if (c == S_EX_NONE)
9561     {
9562       my_getExpression (ep, str);
9563     }
9564   else if (c == S_EX_REGISTER)
9565     {
9566       ep->X_op = O_constant;
9567       expr_end = str;
9568       ep->X_add_symbol = NULL;
9569       ep->X_op_symbol = NULL;
9570       ep->X_add_number = 0;
9571     }
9572   else
9573     {
9574       as_fatal (_("internal error"));
9575     }
9576
9577   if (nestlevel <= 0)
9578     /* All percent_op's have been handled.  */
9579     oldstr = NULL;
9580
9581   return c;
9582 }
9583
9584 static void
9585 my_getExpression (ep, str)
9586      expressionS *ep;
9587      char *str;
9588 {
9589   char *save_in;
9590   valueT val;
9591
9592   save_in = input_line_pointer;
9593   input_line_pointer = str;
9594   expression (ep);
9595   expr_end = input_line_pointer;
9596   input_line_pointer = save_in;
9597
9598   /* If we are in mips16 mode, and this is an expression based on `.',
9599      then we bump the value of the symbol by 1 since that is how other
9600      text symbols are handled.  We don't bother to handle complex
9601      expressions, just `.' plus or minus a constant.  */
9602   if (mips_opts.mips16
9603       && ep->X_op == O_symbol
9604       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9605       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9606       && symbol_get_frag (ep->X_add_symbol) == frag_now
9607       && symbol_constant_p (ep->X_add_symbol)
9608       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9609     S_SET_VALUE (ep->X_add_symbol, val + 1);
9610 }
9611
9612 /* Turn a string in input_line_pointer into a floating point constant
9613    of type TYPE, and store the appropriate bytes in *LITP.  The number
9614    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9615    returned, or NULL on OK.  */
9616
9617 char *
9618 md_atof (type, litP, sizeP)
9619      int type;
9620      char *litP;
9621      int *sizeP;
9622 {
9623   int prec;
9624   LITTLENUM_TYPE words[4];
9625   char *t;
9626   int i;
9627
9628   switch (type)
9629     {
9630     case 'f':
9631       prec = 2;
9632       break;
9633
9634     case 'd':
9635       prec = 4;
9636       break;
9637
9638     default:
9639       *sizeP = 0;
9640       return _("bad call to md_atof");
9641     }
9642
9643   t = atof_ieee (input_line_pointer, type, words);
9644   if (t)
9645     input_line_pointer = t;
9646
9647   *sizeP = prec * 2;
9648
9649   if (! target_big_endian)
9650     {
9651       for (i = prec - 1; i >= 0; i--)
9652         {
9653           md_number_to_chars (litP, (valueT) words[i], 2);
9654           litP += 2;
9655         }
9656     }
9657   else
9658     {
9659       for (i = 0; i < prec; i++)
9660         {
9661           md_number_to_chars (litP, (valueT) words[i], 2);
9662           litP += 2;
9663         }
9664     }
9665
9666   return NULL;
9667 }
9668
9669 void
9670 md_number_to_chars (buf, val, n)
9671      char *buf;
9672      valueT val;
9673      int n;
9674 {
9675   if (target_big_endian)
9676     number_to_chars_bigendian (buf, val, n);
9677   else
9678     number_to_chars_littleendian (buf, val, n);
9679 }
9680 \f
9681 #ifdef OBJ_ELF
9682 static int support_64bit_objects(void)
9683 {
9684   const char **list, **l;
9685
9686   list = bfd_target_list ();
9687   for (l = list; *l != NULL; l++)
9688 #ifdef TE_TMIPS
9689     /* This is traditional mips */
9690     if (strcmp (*l, "elf64-tradbigmips") == 0
9691         || strcmp (*l, "elf64-tradlittlemips") == 0)
9692 #else
9693     if (strcmp (*l, "elf64-bigmips") == 0
9694         || strcmp (*l, "elf64-littlemips") == 0)
9695 #endif
9696       break;
9697   free (list);
9698   return (*l != NULL);
9699 }
9700 #endif /* OBJ_ELF */
9701
9702 CONST char *md_shortopts = "nO::g::G:";
9703
9704 struct option md_longopts[] =
9705 {
9706 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9707   {"mips0", no_argument, NULL, OPTION_MIPS1},
9708   {"mips1", no_argument, NULL, OPTION_MIPS1},
9709 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9710   {"mips2", no_argument, NULL, OPTION_MIPS2},
9711 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9712   {"mips3", no_argument, NULL, OPTION_MIPS3},
9713 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9714   {"mips4", no_argument, NULL, OPTION_MIPS4},
9715 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9716   {"mips5", no_argument, NULL, OPTION_MIPS5},
9717 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9718   {"mips32", no_argument, NULL, OPTION_MIPS32},
9719 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9720   {"mips64", no_argument, NULL, OPTION_MIPS64},
9721 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9722   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9723 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9724   {"trap", no_argument, NULL, OPTION_TRAP},
9725   {"no-break", no_argument, NULL, OPTION_TRAP},
9726 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9727   {"break", no_argument, NULL, OPTION_BREAK},
9728   {"no-trap", no_argument, NULL, OPTION_BREAK},
9729 #define OPTION_EB (OPTION_MD_BASE + 11)
9730   {"EB", no_argument, NULL, OPTION_EB},
9731 #define OPTION_EL (OPTION_MD_BASE + 12)
9732   {"EL", no_argument, NULL, OPTION_EL},
9733 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9734   {"mips16", no_argument, NULL, OPTION_MIPS16},
9735 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9736   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9737 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9738   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9739 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9740   {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9741 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9742   {"mfp32", no_argument, NULL, OPTION_FP32},
9743 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9744   {"mgp32", no_argument, NULL, OPTION_GP32},
9745 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9746   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9747 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9748   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9749 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9750   {"march", required_argument, NULL, OPTION_MARCH},
9751 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9752   {"mtune", required_argument, NULL, OPTION_MTUNE},
9753 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9754   {"mcpu", required_argument, NULL, OPTION_MCPU},
9755 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9756   {"m4650", no_argument, NULL, OPTION_M4650},
9757 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9758   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9759 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9760   {"m4010", no_argument, NULL, OPTION_M4010},
9761 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9762   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9763 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9764   {"m4100", no_argument, NULL, OPTION_M4100},
9765 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9766   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9767 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9768   {"m3900", no_argument, NULL, OPTION_M3900},
9769 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9770   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9771 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9772   {"mgp64", no_argument, NULL, OPTION_GP64},
9773 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9774   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9775 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9776   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9777 #ifdef OBJ_ELF
9778 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 35)
9779 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9780   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9781   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9782 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9783   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9784 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9785   {"xgot",        no_argument, NULL, OPTION_XGOT},
9786 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9787   {"mabi", required_argument, NULL, OPTION_MABI},
9788 #define OPTION_32          (OPTION_ELF_BASE + 4)
9789   {"32",          no_argument, NULL, OPTION_32},
9790 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9791   {"n32",         no_argument, NULL, OPTION_N32},
9792 #define OPTION_64          (OPTION_ELF_BASE + 6)
9793   {"64",          no_argument, NULL, OPTION_64},
9794 #endif /* OBJ_ELF */
9795   {NULL, no_argument, NULL, 0}
9796 };
9797 size_t md_longopts_size = sizeof (md_longopts);
9798
9799 int
9800 md_parse_option (c, arg)
9801      int c;
9802      char *arg;
9803 {
9804   switch (c)
9805     {
9806     case OPTION_CONSTRUCT_FLOATS:
9807       mips_disable_float_construction = 0;
9808       break;
9809
9810     case OPTION_NO_CONSTRUCT_FLOATS:
9811       mips_disable_float_construction = 1;
9812       break;
9813
9814     case OPTION_TRAP:
9815       mips_trap = 1;
9816       break;
9817
9818     case OPTION_BREAK:
9819       mips_trap = 0;
9820       break;
9821
9822     case OPTION_EB:
9823       target_big_endian = 1;
9824       break;
9825
9826     case OPTION_EL:
9827       target_big_endian = 0;
9828       break;
9829
9830     case 'n':
9831       warn_nops = 1;
9832       break;
9833
9834     case 'O':
9835       if (arg && arg[1] == '0')
9836         mips_optimize = 1;
9837       else
9838         mips_optimize = 2;
9839       break;
9840
9841     case 'g':
9842       if (arg == NULL)
9843         mips_debug = 2;
9844       else
9845         mips_debug = atoi (arg);
9846       /* When the MIPS assembler sees -g or -g2, it does not do
9847          optimizations which limit full symbolic debugging.  We take
9848          that to be equivalent to -O0.  */
9849       if (mips_debug == 2)
9850         mips_optimize = 1;
9851       break;
9852
9853     case OPTION_MIPS1:
9854       mips_opts.isa = ISA_MIPS1;
9855       break;
9856
9857     case OPTION_MIPS2:
9858       mips_opts.isa = ISA_MIPS2;
9859       break;
9860
9861     case OPTION_MIPS3:
9862       mips_opts.isa = ISA_MIPS3;
9863       break;
9864
9865     case OPTION_MIPS4:
9866       mips_opts.isa = ISA_MIPS4;
9867       break;
9868
9869     case OPTION_MIPS5:
9870       mips_opts.isa = ISA_MIPS5;
9871       break;
9872
9873     case OPTION_MIPS32:
9874       mips_opts.isa = ISA_MIPS32;
9875       break;
9876
9877     case OPTION_MIPS64:
9878       mips_opts.isa = ISA_MIPS64;
9879       break;
9880
9881     case OPTION_MTUNE:
9882     case OPTION_MARCH:
9883     case OPTION_MCPU:
9884       {
9885         int cpu = CPU_UNKNOWN;
9886
9887         /* Identify the processor type.  */
9888         if (strcasecmp (arg, "default") != 0)
9889           {
9890             const struct mips_cpu_info *ci;
9891
9892             ci = mips_cpu_info_from_name (arg);
9893             if (ci == NULL || ci->is_isa)
9894               {
9895                 switch (c)
9896                   {
9897                   case OPTION_MTUNE:
9898                     as_fatal (_("invalid architecture -mtune=%s"), arg);
9899                     break;
9900                   case OPTION_MARCH:
9901                     as_fatal (_("invalid architecture -march=%s"), arg);
9902                     break;
9903                   case OPTION_MCPU:
9904                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
9905                     break;
9906                   }
9907               }
9908             else
9909               cpu = ci->cpu;
9910           }
9911
9912         switch (c)
9913           {
9914           case OPTION_MTUNE:
9915             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9916               as_warn (_("A different -mtune= was already specified, is now "
9917                          "-mtune=%s"), arg);
9918             mips_tune = cpu;
9919             break;
9920           case OPTION_MARCH:
9921             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9922               as_warn (_("A different -march= was already specified, is now "
9923                          "-march=%s"), arg);
9924             mips_arch = cpu;
9925             break;
9926           case OPTION_MCPU:
9927             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9928               as_warn (_("A different -mcpu= was already specified, is now "
9929                          "-mcpu=%s"), arg);
9930             mips_cpu = cpu;
9931           }
9932       }
9933       break;
9934
9935     case OPTION_M4650:
9936       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9937           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9938         as_warn (_("A different -march= or -mtune= was already specified, "
9939                    "is now -m4650"));
9940       mips_arch = CPU_R4650;
9941       mips_tune = CPU_R4650;
9942       break;
9943
9944     case OPTION_NO_M4650:
9945       break;
9946
9947     case OPTION_M4010:
9948       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9949           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9950         as_warn (_("A different -march= or -mtune= was already specified, "
9951                    "is now -m4010"));
9952       mips_arch = CPU_R4010;
9953       mips_tune = CPU_R4010;
9954       break;
9955
9956     case OPTION_NO_M4010:
9957       break;
9958
9959     case OPTION_M4100:
9960       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9961           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9962         as_warn (_("A different -march= or -mtune= was already specified, "
9963                    "is now -m4100"));
9964       mips_arch = CPU_VR4100;
9965       mips_tune = CPU_VR4100;
9966       break;
9967
9968     case OPTION_NO_M4100:
9969       break;
9970
9971     case OPTION_M3900:
9972       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9973           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9974         as_warn (_("A different -march= or -mtune= was already specified, "
9975                    "is now -m3900"));
9976       mips_arch = CPU_R3900;
9977       mips_tune = CPU_R3900;
9978       break;
9979
9980     case OPTION_NO_M3900:
9981       break;
9982
9983     case OPTION_MIPS16:
9984       mips_opts.mips16 = 1;
9985       mips_no_prev_insn (false);
9986       break;
9987
9988     case OPTION_NO_MIPS16:
9989       mips_opts.mips16 = 0;
9990       mips_no_prev_insn (false);
9991       break;
9992
9993     case OPTION_MIPS3D:
9994       mips_opts.ase_mips3d = 1;
9995       break;
9996
9997     case OPTION_NO_MIPS3D:
9998       mips_opts.ase_mips3d = 0;
9999       break;
10000
10001     case OPTION_MEMBEDDED_PIC:
10002       mips_pic = EMBEDDED_PIC;
10003       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10004         {
10005           as_bad (_("-G may not be used with embedded PIC code"));
10006           return 0;
10007         }
10008       g_switch_value = 0x7fffffff;
10009       break;
10010
10011 #ifdef OBJ_ELF
10012       /* When generating ELF code, we permit -KPIC and -call_shared to
10013          select SVR4_PIC, and -non_shared to select no PIC.  This is
10014          intended to be compatible with Irix 5.  */
10015     case OPTION_CALL_SHARED:
10016       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10017         {
10018           as_bad (_("-call_shared is supported only for ELF format"));
10019           return 0;
10020         }
10021       mips_pic = SVR4_PIC;
10022       if (g_switch_seen && g_switch_value != 0)
10023         {
10024           as_bad (_("-G may not be used with SVR4 PIC code"));
10025           return 0;
10026         }
10027       g_switch_value = 0;
10028       break;
10029
10030     case OPTION_NON_SHARED:
10031       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10032         {
10033           as_bad (_("-non_shared is supported only for ELF format"));
10034           return 0;
10035         }
10036       mips_pic = NO_PIC;
10037       break;
10038
10039       /* The -xgot option tells the assembler to use 32 offsets when
10040          accessing the got in SVR4_PIC mode.  It is for Irix
10041          compatibility.  */
10042     case OPTION_XGOT:
10043       mips_big_got = 1;
10044       break;
10045 #endif /* OBJ_ELF */
10046
10047     case 'G':
10048       if (! USE_GLOBAL_POINTER_OPT)
10049         {
10050           as_bad (_("-G is not supported for this configuration"));
10051           return 0;
10052         }
10053       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10054         {
10055           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10056           return 0;
10057         }
10058       else
10059         g_switch_value = atoi (arg);
10060       g_switch_seen = 1;
10061       break;
10062
10063 #ifdef OBJ_ELF
10064       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10065          and -mabi=64.  */
10066     case OPTION_32:
10067       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10068         {
10069           as_bad (_("-32 is supported for ELF format only"));
10070           return 0;
10071         }
10072       mips_opts.abi = O32_ABI;
10073       break;
10074
10075     case OPTION_N32:
10076       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10077         {
10078           as_bad (_("-n32 is supported for ELF format only"));
10079           return 0;
10080         }
10081       mips_opts.abi = N32_ABI;
10082       break;
10083
10084     case OPTION_64:
10085       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10086         {
10087           as_bad (_("-64 is supported for ELF format only"));
10088           return 0;
10089         }
10090       mips_opts.abi = N64_ABI;
10091       if (! support_64bit_objects())
10092         as_fatal (_("No compiled in support for 64 bit object file format"));
10093       break;
10094 #endif /* OBJ_ELF */
10095
10096     case OPTION_GP32:
10097       file_mips_gp32 = 1;
10098       if (mips_opts.abi != O32_ABI)
10099         mips_opts.abi = NO_ABI;
10100       break;
10101
10102     case OPTION_GP64:
10103       file_mips_gp32 = 0;
10104       if (mips_opts.abi == O32_ABI)
10105         mips_opts.abi = NO_ABI;
10106       break;
10107
10108     case OPTION_FP32:
10109       file_mips_fp32 = 1;
10110       if (mips_opts.abi != O32_ABI)
10111         mips_opts.abi = NO_ABI;
10112       break;
10113
10114 #ifdef OBJ_ELF
10115     case OPTION_MABI:
10116       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10117         {
10118           as_bad (_("-mabi is supported for ELF format only"));
10119           return 0;
10120         }
10121       if (strcmp (arg, "32") == 0)
10122         mips_opts.abi = O32_ABI;
10123       else if (strcmp (arg, "o64") == 0)
10124         mips_opts.abi = O64_ABI;
10125       else if (strcmp (arg, "n32") == 0)
10126         mips_opts.abi = N32_ABI;
10127       else if (strcmp (arg, "64") == 0)
10128         {
10129           mips_opts.abi = N64_ABI;
10130           if (! support_64bit_objects())
10131             as_fatal (_("No compiled in support for 64 bit object file "
10132                         "format"));
10133         }
10134       else if (strcmp (arg, "eabi") == 0)
10135         mips_opts.abi = EABI_ABI;
10136       else
10137         {
10138           as_fatal (_("invalid abi -mabi=%s"), arg);
10139           return 0;
10140         }
10141       break;
10142 #endif /* OBJ_ELF */
10143
10144     case OPTION_M7000_HILO_FIX:
10145       mips_7000_hilo_fix = true;
10146       break;
10147
10148     case OPTION_NO_M7000_HILO_FIX:
10149       mips_7000_hilo_fix = false;
10150       break;
10151
10152     default:
10153       return 0;
10154     }
10155
10156   return 1;
10157 }
10158
10159 static void
10160 show (stream, string, col_p, first_p)
10161      FILE *stream;
10162      char *string;
10163      int *col_p;
10164      int *first_p;
10165 {
10166   if (*first_p)
10167     {
10168       fprintf (stream, "%24s", "");
10169       *col_p = 24;
10170     }
10171   else
10172     {
10173       fprintf (stream, ", ");
10174       *col_p += 2;
10175     }
10176
10177   if (*col_p + strlen (string) > 72)
10178     {
10179       fprintf (stream, "\n%24s", "");
10180       *col_p = 24;
10181     }
10182
10183   fprintf (stream, "%s", string);
10184   *col_p += strlen (string);
10185
10186   *first_p = 0;
10187 }
10188
10189 void
10190 md_show_usage (stream)
10191      FILE *stream;
10192 {
10193   int column, first;
10194
10195   fprintf (stream, _("\
10196 MIPS options:\n\
10197 -membedded-pic          generate embedded position independent code\n\
10198 -EB                     generate big endian output\n\
10199 -EL                     generate little endian output\n\
10200 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10201 -G NUM                  allow referencing objects up to NUM bytes\n\
10202                         implicitly with the gp register [default 8]\n"));
10203   fprintf (stream, _("\
10204 -mips1                  generate MIPS ISA I instructions\n\
10205 -mips2                  generate MIPS ISA II instructions\n\
10206 -mips3                  generate MIPS ISA III instructions\n\
10207 -mips4                  generate MIPS ISA IV instructions\n\
10208 -mips5                  generate MIPS ISA V instructions\n\
10209 -mips32                 generate MIPS32 ISA instructions\n\
10210 -mips64                 generate MIPS64 ISA instructions\n\
10211 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10212
10213   first = 1;
10214
10215   show (stream, "2000", &column, &first);
10216   show (stream, "3000", &column, &first);
10217   show (stream, "3900", &column, &first);
10218   show (stream, "4000", &column, &first);
10219   show (stream, "4010", &column, &first);
10220   show (stream, "4100", &column, &first);
10221   show (stream, "4111", &column, &first);
10222   show (stream, "4300", &column, &first);
10223   show (stream, "4400", &column, &first);
10224   show (stream, "4600", &column, &first);
10225   show (stream, "4650", &column, &first);
10226   show (stream, "5000", &column, &first);
10227   show (stream, "5200", &column, &first);
10228   show (stream, "5230", &column, &first);
10229   show (stream, "5231", &column, &first);
10230   show (stream, "5261", &column, &first);
10231   show (stream, "5721", &column, &first);
10232   show (stream, "6000", &column, &first);
10233   show (stream, "8000", &column, &first);
10234   show (stream, "10000", &column, &first);
10235   show (stream, "12000", &column, &first);
10236   show (stream, "sb1", &column, &first);
10237   fputc ('\n', stream);
10238
10239   fprintf (stream, _("\
10240 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10241 -no-mCPU                don't generate code specific to CPU.\n\
10242                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10243
10244   first = 1;
10245
10246   show (stream, "3900", &column, &first);
10247   show (stream, "4010", &column, &first);
10248   show (stream, "4100", &column, &first);
10249   show (stream, "4650", &column, &first);
10250   fputc ('\n', stream);
10251
10252   fprintf (stream, _("\
10253 -mips16                 generate mips16 instructions\n\
10254 -no-mips16              do not generate mips16 instructions\n"));
10255   fprintf (stream, _("\
10256 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10257 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10258 -O0                     remove unneeded NOPs, do not swap branches\n\
10259 -O                      remove unneeded NOPs and swap branches\n\
10260 -n                      warn about NOPs generated from macros\n\
10261 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10262 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10263 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10264 #ifdef OBJ_ELF
10265   fprintf (stream, _("\
10266 -KPIC, -call_shared     generate SVR4 position independent code\n\
10267 -non_shared             do not generate position independent code\n\
10268 -xgot                   assume a 32 bit GOT\n\
10269 -mabi=ABI               create ABI conformant object file for:\n"));
10270
10271   first = 1;
10272
10273   show (stream, "32", &column, &first);
10274   show (stream, "o64", &column, &first);
10275   show (stream, "n32", &column, &first);
10276   show (stream, "64", &column, &first);
10277   show (stream, "eabi", &column, &first);
10278
10279   fputc ('\n', stream);
10280
10281   fprintf (stream, _("\
10282 -32                     create o32 ABI object file (default)\n\
10283 -n32                    create n32 ABI object file\n\
10284 -64                     create 64 ABI object file\n"));
10285 #endif
10286 }
10287 \f
10288 void
10289 mips_init_after_args ()
10290 {
10291   /* initialize opcodes */
10292   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10293   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10294 }
10295
10296 long
10297 md_pcrel_from (fixP)
10298      fixS *fixP;
10299 {
10300   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10301       && fixP->fx_addsy != (symbolS *) NULL
10302       && ! S_IS_DEFINED (fixP->fx_addsy))
10303     {
10304       /* This makes a branch to an undefined symbol be a branch to the
10305          current location.  */
10306       if (mips_pic == EMBEDDED_PIC)
10307         return 4;
10308       else
10309         return 1;
10310     }
10311
10312   /* return the address of the delay slot */
10313   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10314 }
10315
10316 /* This is called before the symbol table is processed.  In order to
10317    work with gcc when using mips-tfile, we must keep all local labels.
10318    However, in other cases, we want to discard them.  If we were
10319    called with -g, but we didn't see any debugging information, it may
10320    mean that gcc is smuggling debugging information through to
10321    mips-tfile, in which case we must generate all local labels.  */
10322
10323 void
10324 mips_frob_file_before_adjust ()
10325 {
10326 #ifndef NO_ECOFF_DEBUGGING
10327   if (ECOFF_DEBUGGING
10328       && mips_debug != 0
10329       && ! ecoff_debugging_seen)
10330     flag_keep_locals = 1;
10331 #endif
10332 }
10333
10334 /* Sort any unmatched HI16_S relocs so that they immediately precede
10335    the corresponding LO reloc.  This is called before md_apply_fix3 and
10336    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10337    explicit use of the %hi modifier.  */
10338
10339 void
10340 mips_frob_file ()
10341 {
10342   struct mips_hi_fixup *l;
10343
10344   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10345     {
10346       segment_info_type *seginfo;
10347       int pass;
10348
10349       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10350
10351       /* Check quickly whether the next fixup happens to be a matching
10352          %lo.  */
10353       if (l->fixp->fx_next != NULL
10354           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10355           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10356           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10357         continue;
10358
10359       /* Look through the fixups for this segment for a matching %lo.
10360          When we find one, move the %hi just in front of it.  We do
10361          this in two passes.  In the first pass, we try to find a
10362          unique %lo.  In the second pass, we permit multiple %hi
10363          relocs for a single %lo (this is a GNU extension).  */
10364       seginfo = seg_info (l->seg);
10365       for (pass = 0; pass < 2; pass++)
10366         {
10367           fixS *f, *prev;
10368
10369           prev = NULL;
10370           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10371             {
10372               /* Check whether this is a %lo fixup which matches l->fixp.  */
10373               if (f->fx_r_type == BFD_RELOC_LO16
10374                   && f->fx_addsy == l->fixp->fx_addsy
10375                   && f->fx_offset == l->fixp->fx_offset
10376                   && (pass == 1
10377                       || prev == NULL
10378                       || prev->fx_r_type != BFD_RELOC_HI16_S
10379                       || prev->fx_addsy != f->fx_addsy
10380                       || prev->fx_offset !=  f->fx_offset))
10381                 {
10382                   fixS **pf;
10383
10384                   /* Move l->fixp before f.  */
10385                   for (pf = &seginfo->fix_root;
10386                        *pf != l->fixp;
10387                        pf = &(*pf)->fx_next)
10388                     assert (*pf != NULL);
10389
10390                   *pf = l->fixp->fx_next;
10391
10392                   l->fixp->fx_next = f;
10393                   if (prev == NULL)
10394                     seginfo->fix_root = l->fixp;
10395                   else
10396                     prev->fx_next = l->fixp;
10397
10398                   break;
10399                 }
10400
10401               prev = f;
10402             }
10403
10404           if (f != NULL)
10405             break;
10406
10407 #if 0 /* GCC code motion plus incomplete dead code elimination
10408          can leave a %hi without a %lo.  */
10409           if (pass == 1)
10410             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10411                            _("Unmatched %%hi reloc"));
10412 #endif
10413         }
10414     }
10415 }
10416
10417 /* When generating embedded PIC code we need to use a special
10418    relocation to represent the difference of two symbols in the .text
10419    section (switch tables use a difference of this sort).  See
10420    include/coff/mips.h for details.  This macro checks whether this
10421    fixup requires the special reloc.  */
10422 #define SWITCH_TABLE(fixp) \
10423   ((fixp)->fx_r_type == BFD_RELOC_32 \
10424    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10425    && (fixp)->fx_addsy != NULL \
10426    && (fixp)->fx_subsy != NULL \
10427    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10428    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10429
10430 /* When generating embedded PIC code we must keep all PC relative
10431    relocations, in case the linker has to relax a call.  We also need
10432    to keep relocations for switch table entries.
10433
10434    We may have combined relocations without symbols in the N32/N64 ABI.
10435    We have to prevent gas from dropping them.  */
10436
10437 int
10438 mips_force_relocation (fixp)
10439      fixS *fixp;
10440 {
10441   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10442       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10443     return 1;
10444
10445   if (HAVE_NEWABI
10446       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10447       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10448           || fixp->fx_r_type == BFD_RELOC_HI16_S
10449           || fixp->fx_r_type == BFD_RELOC_LO16))
10450     return 1;
10451
10452   return (mips_pic == EMBEDDED_PIC
10453           && (fixp->fx_pcrel
10454               || SWITCH_TABLE (fixp)
10455               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10456               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10457 }
10458
10459 #ifdef OBJ_ELF
10460 static int
10461 mips_need_elf_addend_fixup (fixP)
10462      fixS *fixP;
10463 {
10464   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10465     return 1;
10466   if (mips_pic == EMBEDDED_PIC
10467       && S_IS_WEAK (fixP->fx_addsy))
10468     return 1;
10469   if (mips_pic != EMBEDDED_PIC
10470       && (S_IS_WEAK (fixP->fx_addsy)
10471           || S_IS_EXTERN (fixP->fx_addsy))
10472       && !S_IS_COMMON (fixP->fx_addsy))
10473     return 1;
10474   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10475       && (((bfd_get_section_flags (stdoutput,
10476                                    S_GET_SEGMENT (fixP->fx_addsy))
10477             & SEC_LINK_ONCE) != 0)
10478           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10479                        ".gnu.linkonce",
10480                        sizeof (".gnu.linkonce") - 1)))
10481     return 1;
10482   return 0;
10483 }
10484 #endif
10485
10486 /* Apply a fixup to the object file.  */
10487
10488 void
10489 md_apply_fix3 (fixP, valP, seg)
10490      fixS *fixP;
10491      valueT *valP;
10492      segT seg ATTRIBUTE_UNUSED;
10493 {
10494   bfd_byte *buf;
10495   long insn;
10496   valueT value;
10497
10498   assert (fixP->fx_size == 4
10499           || fixP->fx_r_type == BFD_RELOC_16
10500           || fixP->fx_r_type == BFD_RELOC_32
10501           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10502           || fixP->fx_r_type == BFD_RELOC_HI16_S
10503           || fixP->fx_r_type == BFD_RELOC_LO16
10504           || fixP->fx_r_type == BFD_RELOC_GPREL16
10505           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10506           || fixP->fx_r_type == BFD_RELOC_GPREL32
10507           || fixP->fx_r_type == BFD_RELOC_64
10508           || fixP->fx_r_type == BFD_RELOC_CTOR
10509           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10510           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10511           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10512           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10513           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10514           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10515           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10516           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10517
10518   value = *valP;
10519
10520   /* If we aren't adjusting this fixup to be against the section
10521      symbol, we need to adjust the value.  */
10522 #ifdef OBJ_ELF
10523   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10524     {
10525       if (mips_need_elf_addend_fixup (fixP))
10526         {
10527           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10528
10529           value -= symval;
10530           if (value != 0 && ! fixP->fx_pcrel)
10531             {
10532               /* In this case, the bfd_install_relocation routine will
10533                  incorrectly add the symbol value back in.  We just want
10534                  the addend to appear in the object file.  */
10535               value -= symval;
10536
10537               /* Make sure the addend is still non-zero.  If it became zero
10538                  after the last operation, set it to a spurious value and
10539                  subtract the same value from the object file's contents.  */
10540               if (value == 0)
10541                 {
10542                   value = 8;
10543
10544                   /* The in-place addends for LO16 relocations are signed;
10545                      leave the matching HI16 in-place addends as zero.  */
10546                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10547                     {
10548                       reloc_howto_type *howto;
10549                       bfd_vma contents, mask, field;
10550
10551                       howto = bfd_reloc_type_lookup (stdoutput,
10552                                                      fixP->fx_r_type);
10553
10554                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10555                                                + fixP->fx_where,
10556                                                fixP->fx_size * 8,
10557                                                target_big_endian);
10558
10559                       /* MASK has bits set where the relocation should go.
10560                          FIELD is -value, shifted into the appropriate place
10561                          for this relocation.  */
10562                       mask = 1 << (howto->bitsize - 1);
10563                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10564                       field = (-value >> howto->rightshift) << howto->bitpos;
10565
10566                       bfd_put_bits ((field & mask) | (contents & ~mask),
10567                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10568                                     fixP->fx_size * 8,
10569                                     target_big_endian);
10570                     }
10571                 }
10572             }
10573         }
10574
10575       /* This code was generated using trial and error and so is
10576          fragile and not trustworthy.  If you change it, you should
10577          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10578          they still pass.  */
10579       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10580         {
10581           value += fixP->fx_frag->fr_address + fixP->fx_where;
10582
10583           /* BFD's REL handling, for MIPS, is _very_ weird.
10584              This gives the right results, but it can't possibly
10585              be the way things are supposed to work.  */
10586           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10587                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10588               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10589             value += fixP->fx_frag->fr_address + fixP->fx_where;
10590         }
10591     }
10592 #endif
10593
10594   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10595
10596   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10597     fixP->fx_done = 1;
10598
10599   switch (fixP->fx_r_type)
10600     {
10601     case BFD_RELOC_MIPS_JMP:
10602     case BFD_RELOC_MIPS_SHIFT5:
10603     case BFD_RELOC_MIPS_SHIFT6:
10604     case BFD_RELOC_MIPS_GOT_DISP:
10605     case BFD_RELOC_MIPS_GOT_PAGE:
10606     case BFD_RELOC_MIPS_GOT_OFST:
10607     case BFD_RELOC_MIPS_SUB:
10608     case BFD_RELOC_MIPS_INSERT_A:
10609     case BFD_RELOC_MIPS_INSERT_B:
10610     case BFD_RELOC_MIPS_DELETE:
10611     case BFD_RELOC_MIPS_HIGHEST:
10612     case BFD_RELOC_MIPS_HIGHER:
10613     case BFD_RELOC_MIPS_SCN_DISP:
10614     case BFD_RELOC_MIPS_REL16:
10615     case BFD_RELOC_MIPS_RELGOT:
10616     case BFD_RELOC_MIPS_JALR:
10617     case BFD_RELOC_HI16:
10618     case BFD_RELOC_HI16_S:
10619     case BFD_RELOC_GPREL16:
10620     case BFD_RELOC_MIPS_LITERAL:
10621     case BFD_RELOC_MIPS_CALL16:
10622     case BFD_RELOC_MIPS_GOT16:
10623     case BFD_RELOC_GPREL32:
10624     case BFD_RELOC_MIPS_GOT_HI16:
10625     case BFD_RELOC_MIPS_GOT_LO16:
10626     case BFD_RELOC_MIPS_CALL_HI16:
10627     case BFD_RELOC_MIPS_CALL_LO16:
10628     case BFD_RELOC_MIPS16_GPREL:
10629       if (fixP->fx_pcrel)
10630         as_bad_where (fixP->fx_file, fixP->fx_line,
10631                       _("Invalid PC relative reloc"));
10632       /* Nothing needed to do. The value comes from the reloc entry */
10633       break;
10634
10635     case BFD_RELOC_MIPS16_JMP:
10636       /* We currently always generate a reloc against a symbol, which
10637          means that we don't want an addend even if the symbol is
10638          defined.  */
10639       fixP->fx_addnumber = 0;
10640       break;
10641
10642     case BFD_RELOC_PCREL_HI16_S:
10643       /* The addend for this is tricky if it is internal, so we just
10644          do everything here rather than in bfd_install_relocation.  */
10645       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10646           && !fixP->fx_done
10647           && value != 0)
10648         break;
10649       if (fixP->fx_addsy
10650           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10651         {
10652           /* For an external symbol adjust by the address to make it
10653              pcrel_offset.  We use the address of the RELLO reloc
10654              which follows this one.  */
10655           value += (fixP->fx_next->fx_frag->fr_address
10656                     + fixP->fx_next->fx_where);
10657         }
10658       value = ((value + 0x8000) >> 16) & 0xffff;
10659       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10660       if (target_big_endian)
10661         buf += 2;
10662       md_number_to_chars ((char *) buf, value, 2);
10663       break;
10664
10665     case BFD_RELOC_PCREL_LO16:
10666       /* The addend for this is tricky if it is internal, so we just
10667          do everything here rather than in bfd_install_relocation.  */
10668       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10669           && !fixP->fx_done
10670           && value != 0)
10671         break;
10672       if (fixP->fx_addsy
10673           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10674         value += fixP->fx_frag->fr_address + fixP->fx_where;
10675       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10676       if (target_big_endian)
10677         buf += 2;
10678       md_number_to_chars ((char *) buf, value, 2);
10679       break;
10680
10681     case BFD_RELOC_64:
10682       /* This is handled like BFD_RELOC_32, but we output a sign
10683          extended value if we are only 32 bits.  */
10684       if (fixP->fx_done
10685           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10686         {
10687           if (8 <= sizeof (valueT))
10688             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10689                                 value, 8);
10690           else
10691             {
10692               long w1, w2;
10693               long hiv;
10694
10695               w1 = w2 = fixP->fx_where;
10696               if (target_big_endian)
10697                 w1 += 4;
10698               else
10699                 w2 += 4;
10700               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10701               if ((value & 0x80000000) != 0)
10702                 hiv = 0xffffffff;
10703               else
10704                 hiv = 0;
10705               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10706             }
10707         }
10708       break;
10709
10710     case BFD_RELOC_RVA:
10711     case BFD_RELOC_32:
10712       /* If we are deleting this reloc entry, we must fill in the
10713          value now.  This can happen if we have a .word which is not
10714          resolved when it appears but is later defined.  We also need
10715          to fill in the value if this is an embedded PIC switch table
10716          entry.  */
10717       if (fixP->fx_done
10718           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10719         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10720                             value, 4);
10721       break;
10722
10723     case BFD_RELOC_16:
10724       /* If we are deleting this reloc entry, we must fill in the
10725          value now.  */
10726       assert (fixP->fx_size == 2);
10727       if (fixP->fx_done)
10728         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10729                             value, 2);
10730       break;
10731
10732     case BFD_RELOC_LO16:
10733       /* When handling an embedded PIC switch statement, we can wind
10734          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10735       if (fixP->fx_done)
10736         {
10737           if (value + 0x8000 > 0xffff)
10738             as_bad_where (fixP->fx_file, fixP->fx_line,
10739                           _("relocation overflow"));
10740           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10741           if (target_big_endian)
10742             buf += 2;
10743           md_number_to_chars ((char *) buf, value, 2);
10744         }
10745       break;
10746
10747     case BFD_RELOC_16_PCREL_S2:
10748       if ((value & 0x3) != 0)
10749         as_bad_where (fixP->fx_file, fixP->fx_line,
10750                       _("Branch to odd address (%lx)"), (long) value);
10751
10752       /* Fall through.  */
10753
10754     case BFD_RELOC_16_PCREL:
10755       /*
10756        * We need to save the bits in the instruction since fixup_segment()
10757        * might be deleting the relocation entry (i.e., a branch within
10758        * the current segment).
10759        */
10760       if (!fixP->fx_done && value != 0)
10761         break;
10762       /* If 'value' is zero, the remaining reloc code won't actually
10763          do the store, so it must be done here.  This is probably
10764          a bug somewhere.  */
10765       if (!fixP->fx_done
10766           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10767               || fixP->fx_addsy == NULL                 /* ??? */
10768               || ! S_IS_DEFINED (fixP->fx_addsy)))
10769         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10770
10771       value = (offsetT) value >> 2;
10772
10773       /* update old instruction data */
10774       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10775       if (target_big_endian)
10776         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10777       else
10778         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10779
10780       if (value + 0x8000 <= 0xffff)
10781         insn |= value & 0xffff;
10782       else
10783         {
10784           /* The branch offset is too large.  If this is an
10785              unconditional branch, and we are not generating PIC code,
10786              we can convert it to an absolute jump instruction.  */
10787           if (mips_pic == NO_PIC
10788               && fixP->fx_done
10789               && fixP->fx_frag->fr_address >= text_section->vma
10790               && (fixP->fx_frag->fr_address
10791                   < text_section->vma + text_section->_raw_size)
10792               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
10793                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
10794                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10795             {
10796               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
10797                 insn = 0x0c000000;      /* jal */
10798               else
10799                 insn = 0x08000000;      /* j */
10800               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10801               fixP->fx_done = 0;
10802               fixP->fx_addsy = section_symbol (text_section);
10803               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10804             }
10805           else
10806             {
10807               /* FIXME.  It would be possible in principle to handle
10808                  conditional branches which overflow.  They could be
10809                  transformed into a branch around a jump.  This would
10810                  require setting up variant frags for each different
10811                  branch type.  The native MIPS assembler attempts to
10812                  handle these cases, but it appears to do it
10813                  incorrectly.  */
10814               as_bad_where (fixP->fx_file, fixP->fx_line,
10815                             _("Branch out of range"));
10816             }
10817         }
10818
10819       md_number_to_chars ((char *) buf, (valueT) insn, 4);
10820       break;
10821
10822     case BFD_RELOC_VTABLE_INHERIT:
10823       fixP->fx_done = 0;
10824       if (fixP->fx_addsy
10825           && !S_IS_DEFINED (fixP->fx_addsy)
10826           && !S_IS_WEAK (fixP->fx_addsy))
10827         S_SET_WEAK (fixP->fx_addsy);
10828       break;
10829
10830     case BFD_RELOC_VTABLE_ENTRY:
10831       fixP->fx_done = 0;
10832       break;
10833
10834     default:
10835       internalError ();
10836     }
10837 }
10838
10839 #if 0
10840 void
10841 printInsn (oc)
10842      unsigned long oc;
10843 {
10844   const struct mips_opcode *p;
10845   int treg, sreg, dreg, shamt;
10846   short imm;
10847   const char *args;
10848   int i;
10849
10850   for (i = 0; i < NUMOPCODES; ++i)
10851     {
10852       p = &mips_opcodes[i];
10853       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10854         {
10855           printf ("%08lx %s\t", oc, p->name);
10856           treg = (oc >> 16) & 0x1f;
10857           sreg = (oc >> 21) & 0x1f;
10858           dreg = (oc >> 11) & 0x1f;
10859           shamt = (oc >> 6) & 0x1f;
10860           imm = oc;
10861           for (args = p->args;; ++args)
10862             {
10863               switch (*args)
10864                 {
10865                 case '\0':
10866                   printf ("\n");
10867                   break;
10868
10869                 case ',':
10870                 case '(':
10871                 case ')':
10872                   printf ("%c", *args);
10873                   continue;
10874
10875                 case 'r':
10876                   assert (treg == sreg);
10877                   printf ("$%d,$%d", treg, sreg);
10878                   continue;
10879
10880                 case 'd':
10881                 case 'G':
10882                   printf ("$%d", dreg);
10883                   continue;
10884
10885                 case 't':
10886                 case 'E':
10887                   printf ("$%d", treg);
10888                   continue;
10889
10890                 case 'k':
10891                   printf ("0x%x", treg);
10892                   continue;
10893
10894                 case 'b':
10895                 case 's':
10896                   printf ("$%d", sreg);
10897                   continue;
10898
10899                 case 'a':
10900                   printf ("0x%08lx", oc & 0x1ffffff);
10901                   continue;
10902
10903                 case 'i':
10904                 case 'j':
10905                 case 'o':
10906                 case 'u':
10907                   printf ("%d", imm);
10908                   continue;
10909
10910                 case '<':
10911                 case '>':
10912                   printf ("$%d", shamt);
10913                   continue;
10914
10915                 default:
10916                   internalError ();
10917                 }
10918               break;
10919             }
10920           return;
10921         }
10922     }
10923   printf (_("%08lx  UNDEFINED\n"), oc);
10924 }
10925 #endif
10926
10927 static symbolS *
10928 get_symbol ()
10929 {
10930   int c;
10931   char *name;
10932   symbolS *p;
10933
10934   name = input_line_pointer;
10935   c = get_symbol_end ();
10936   p = (symbolS *) symbol_find_or_make (name);
10937   *input_line_pointer = c;
10938   return p;
10939 }
10940
10941 /* Align the current frag to a given power of two.  The MIPS assembler
10942    also automatically adjusts any preceding label.  */
10943
10944 static void
10945 mips_align (to, fill, label)
10946      int to;
10947      int fill;
10948      symbolS *label;
10949 {
10950   mips_emit_delays (false);
10951   frag_align (to, fill, 0);
10952   record_alignment (now_seg, to);
10953   if (label != NULL)
10954     {
10955       assert (S_GET_SEGMENT (label) == now_seg);
10956       symbol_set_frag (label, frag_now);
10957       S_SET_VALUE (label, (valueT) frag_now_fix ());
10958     }
10959 }
10960
10961 /* Align to a given power of two.  .align 0 turns off the automatic
10962    alignment used by the data creating pseudo-ops.  */
10963
10964 static void
10965 s_align (x)
10966      int x ATTRIBUTE_UNUSED;
10967 {
10968   register int temp;
10969   register long temp_fill;
10970   long max_alignment = 15;
10971
10972   /*
10973
10974     o  Note that the assembler pulls down any immediately preceeding label
10975        to the aligned address.
10976     o  It's not documented but auto alignment is reinstated by
10977        a .align pseudo instruction.
10978     o  Note also that after auto alignment is turned off the mips assembler
10979        issues an error on attempt to assemble an improperly aligned data item.
10980        We don't.
10981
10982     */
10983
10984   temp = get_absolute_expression ();
10985   if (temp > max_alignment)
10986     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10987   else if (temp < 0)
10988     {
10989       as_warn (_("Alignment negative: 0 assumed."));
10990       temp = 0;
10991     }
10992   if (*input_line_pointer == ',')
10993     {
10994       input_line_pointer++;
10995       temp_fill = get_absolute_expression ();
10996     }
10997   else
10998     temp_fill = 0;
10999   if (temp)
11000     {
11001       auto_align = 1;
11002       mips_align (temp, (int) temp_fill,
11003                   insn_labels != NULL ? insn_labels->label : NULL);
11004     }
11005   else
11006     {
11007       auto_align = 0;
11008     }
11009
11010   demand_empty_rest_of_line ();
11011 }
11012
11013 void
11014 mips_flush_pending_output ()
11015 {
11016   mips_emit_delays (false);
11017   mips_clear_insn_labels ();
11018 }
11019
11020 static void
11021 s_change_sec (sec)
11022      int sec;
11023 {
11024   segT seg;
11025
11026   /* When generating embedded PIC code, we only use the .text, .lit8,
11027      .sdata and .sbss sections.  We change the .data and .rdata
11028      pseudo-ops to use .sdata.  */
11029   if (mips_pic == EMBEDDED_PIC
11030       && (sec == 'd' || sec == 'r'))
11031     sec = 's';
11032
11033 #ifdef OBJ_ELF
11034   /* The ELF backend needs to know that we are changing sections, so
11035      that .previous works correctly.  We could do something like check
11036      for an obj_section_change_hook macro, but that might be confusing
11037      as it would not be appropriate to use it in the section changing
11038      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11039      This should be cleaner, somehow.  */
11040   obj_elf_section_change_hook ();
11041 #endif
11042
11043   mips_emit_delays (false);
11044   switch (sec)
11045     {
11046     case 't':
11047       s_text (0);
11048       break;
11049     case 'd':
11050       s_data (0);
11051       break;
11052     case 'b':
11053       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11054       demand_empty_rest_of_line ();
11055       break;
11056
11057     case 'r':
11058       if (USE_GLOBAL_POINTER_OPT)
11059         {
11060           seg = subseg_new (RDATA_SECTION_NAME,
11061                             (subsegT) get_absolute_expression ());
11062           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11063             {
11064               bfd_set_section_flags (stdoutput, seg,
11065                                      (SEC_ALLOC
11066                                       | SEC_LOAD
11067                                       | SEC_READONLY
11068                                       | SEC_RELOC
11069                                       | SEC_DATA));
11070               if (strcmp (TARGET_OS, "elf") != 0)
11071                 record_alignment (seg, 4);
11072             }
11073           demand_empty_rest_of_line ();
11074         }
11075       else
11076         {
11077           as_bad (_("No read only data section in this object file format"));
11078           demand_empty_rest_of_line ();
11079           return;
11080         }
11081       break;
11082
11083     case 's':
11084       if (USE_GLOBAL_POINTER_OPT)
11085         {
11086           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11087           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11088             {
11089               bfd_set_section_flags (stdoutput, seg,
11090                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11091                                      | SEC_DATA);
11092               if (strcmp (TARGET_OS, "elf") != 0)
11093                 record_alignment (seg, 4);
11094             }
11095           demand_empty_rest_of_line ();
11096           break;
11097         }
11098       else
11099         {
11100           as_bad (_("Global pointers not supported; recompile -G 0"));
11101           demand_empty_rest_of_line ();
11102           return;
11103         }
11104     }
11105
11106   auto_align = 1;
11107 }
11108
11109 void
11110 mips_enable_auto_align ()
11111 {
11112   auto_align = 1;
11113 }
11114
11115 static void
11116 s_cons (log_size)
11117      int log_size;
11118 {
11119   symbolS *label;
11120
11121   label = insn_labels != NULL ? insn_labels->label : NULL;
11122   mips_emit_delays (false);
11123   if (log_size > 0 && auto_align)
11124     mips_align (log_size, 0, label);
11125   mips_clear_insn_labels ();
11126   cons (1 << log_size);
11127 }
11128
11129 static void
11130 s_float_cons (type)
11131      int type;
11132 {
11133   symbolS *label;
11134
11135   label = insn_labels != NULL ? insn_labels->label : NULL;
11136
11137   mips_emit_delays (false);
11138
11139   if (auto_align)
11140     {
11141       if (type == 'd')
11142         mips_align (3, 0, label);
11143       else
11144         mips_align (2, 0, label);
11145     }
11146
11147   mips_clear_insn_labels ();
11148
11149   float_cons (type);
11150 }
11151
11152 /* Handle .globl.  We need to override it because on Irix 5 you are
11153    permitted to say
11154        .globl foo .text
11155    where foo is an undefined symbol, to mean that foo should be
11156    considered to be the address of a function.  */
11157
11158 static void
11159 s_mips_globl (x)
11160      int x ATTRIBUTE_UNUSED;
11161 {
11162   char *name;
11163   int c;
11164   symbolS *symbolP;
11165   flagword flag;
11166
11167   name = input_line_pointer;
11168   c = get_symbol_end ();
11169   symbolP = symbol_find_or_make (name);
11170   *input_line_pointer = c;
11171   SKIP_WHITESPACE ();
11172
11173   /* On Irix 5, every global symbol that is not explicitly labelled as
11174      being a function is apparently labelled as being an object.  */
11175   flag = BSF_OBJECT;
11176
11177   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11178     {
11179       char *secname;
11180       asection *sec;
11181
11182       secname = input_line_pointer;
11183       c = get_symbol_end ();
11184       sec = bfd_get_section_by_name (stdoutput, secname);
11185       if (sec == NULL)
11186         as_bad (_("%s: no such section"), secname);
11187       *input_line_pointer = c;
11188
11189       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11190         flag = BSF_FUNCTION;
11191     }
11192
11193   symbol_get_bfdsym (symbolP)->flags |= flag;
11194
11195   S_SET_EXTERNAL (symbolP);
11196   demand_empty_rest_of_line ();
11197 }
11198
11199 static void
11200 s_option (x)
11201      int x ATTRIBUTE_UNUSED;
11202 {
11203   char *opt;
11204   char c;
11205
11206   opt = input_line_pointer;
11207   c = get_symbol_end ();
11208
11209   if (*opt == 'O')
11210     {
11211       /* FIXME: What does this mean?  */
11212     }
11213   else if (strncmp (opt, "pic", 3) == 0)
11214     {
11215       int i;
11216
11217       i = atoi (opt + 3);
11218       if (i == 0)
11219         mips_pic = NO_PIC;
11220       else if (i == 2)
11221         mips_pic = SVR4_PIC;
11222       else
11223         as_bad (_(".option pic%d not supported"), i);
11224
11225       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11226         {
11227           if (g_switch_seen && g_switch_value != 0)
11228             as_warn (_("-G may not be used with SVR4 PIC code"));
11229           g_switch_value = 0;
11230           bfd_set_gp_size (stdoutput, 0);
11231         }
11232     }
11233   else
11234     as_warn (_("Unrecognized option \"%s\""), opt);
11235
11236   *input_line_pointer = c;
11237   demand_empty_rest_of_line ();
11238 }
11239
11240 /* This structure is used to hold a stack of .set values.  */
11241
11242 struct mips_option_stack
11243 {
11244   struct mips_option_stack *next;
11245   struct mips_set_options options;
11246 };
11247
11248 static struct mips_option_stack *mips_opts_stack;
11249
11250 /* Handle the .set pseudo-op.  */
11251
11252 static void
11253 s_mipsset (x)
11254      int x ATTRIBUTE_UNUSED;
11255 {
11256   char *name = input_line_pointer, ch;
11257
11258   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11259     input_line_pointer++;
11260   ch = *input_line_pointer;
11261   *input_line_pointer = '\0';
11262
11263   if (strcmp (name, "reorder") == 0)
11264     {
11265       if (mips_opts.noreorder && prev_nop_frag != NULL)
11266         {
11267           /* If we still have pending nops, we can discard them.  The
11268              usual nop handling will insert any that are still
11269              needed.  */
11270           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11271                                     * (mips_opts.mips16 ? 2 : 4));
11272           prev_nop_frag = NULL;
11273         }
11274       mips_opts.noreorder = 0;
11275     }
11276   else if (strcmp (name, "noreorder") == 0)
11277     {
11278       mips_emit_delays (true);
11279       mips_opts.noreorder = 1;
11280       mips_any_noreorder = 1;
11281     }
11282   else if (strcmp (name, "at") == 0)
11283     {
11284       mips_opts.noat = 0;
11285     }
11286   else if (strcmp (name, "noat") == 0)
11287     {
11288       mips_opts.noat = 1;
11289     }
11290   else if (strcmp (name, "macro") == 0)
11291     {
11292       mips_opts.warn_about_macros = 0;
11293     }
11294   else if (strcmp (name, "nomacro") == 0)
11295     {
11296       if (mips_opts.noreorder == 0)
11297         as_bad (_("`noreorder' must be set before `nomacro'"));
11298       mips_opts.warn_about_macros = 1;
11299     }
11300   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11301     {
11302       mips_opts.nomove = 0;
11303     }
11304   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11305     {
11306       mips_opts.nomove = 1;
11307     }
11308   else if (strcmp (name, "bopt") == 0)
11309     {
11310       mips_opts.nobopt = 0;
11311     }
11312   else if (strcmp (name, "nobopt") == 0)
11313     {
11314       mips_opts.nobopt = 1;
11315     }
11316   else if (strcmp (name, "mips16") == 0
11317            || strcmp (name, "MIPS-16") == 0)
11318     mips_opts.mips16 = 1;
11319   else if (strcmp (name, "nomips16") == 0
11320            || strcmp (name, "noMIPS-16") == 0)
11321     mips_opts.mips16 = 0;
11322   else if (strcmp (name, "mips3d") == 0)
11323     mips_opts.ase_mips3d = 1;
11324   else if (strcmp (name, "nomips3d") == 0)
11325     mips_opts.ase_mips3d = 0;
11326   else if (strncmp (name, "mips", 4) == 0)
11327     {
11328       int isa;
11329
11330       /* Permit the user to change the ISA on the fly.  Needless to
11331          say, misuse can cause serious problems.  */
11332       isa = atoi (name + 4);
11333       switch (isa)
11334         {
11335         case  0:
11336           mips_opts.gp32 = file_mips_gp32;
11337           mips_opts.fp32 = file_mips_fp32;
11338           mips_opts.abi = file_mips_abi;
11339           break;
11340         case  1:
11341         case  2:
11342         case 32:
11343           mips_opts.gp32 = 1;
11344           mips_opts.fp32 = 1;
11345           break;
11346         case  3:
11347         case  4:
11348         case  5:
11349         case 64:
11350           /* Loosen ABI register width restriction.  */
11351           if (mips_opts.abi == O32_ABI)
11352             mips_opts.abi = NO_ABI;
11353           mips_opts.gp32 = 0;
11354           mips_opts.fp32 = 0;
11355           break;
11356         default:
11357           as_bad (_("unknown ISA level %s"), name + 4);
11358           break;
11359         }
11360
11361       switch (isa)
11362         {
11363         case  0: mips_opts.isa = file_mips_isa;   break;
11364         case  1: mips_opts.isa = ISA_MIPS1;       break;
11365         case  2: mips_opts.isa = ISA_MIPS2;       break;
11366         case  3: mips_opts.isa = ISA_MIPS3;       break;
11367         case  4: mips_opts.isa = ISA_MIPS4;       break;
11368         case  5: mips_opts.isa = ISA_MIPS5;       break;
11369         case 32: mips_opts.isa = ISA_MIPS32;      break;
11370         case 64: mips_opts.isa = ISA_MIPS64;      break;
11371         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11372         }
11373     }
11374   else if (strcmp (name, "autoextend") == 0)
11375     mips_opts.noautoextend = 0;
11376   else if (strcmp (name, "noautoextend") == 0)
11377     mips_opts.noautoextend = 1;
11378   else if (strcmp (name, "push") == 0)
11379     {
11380       struct mips_option_stack *s;
11381
11382       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11383       s->next = mips_opts_stack;
11384       s->options = mips_opts;
11385       mips_opts_stack = s;
11386     }
11387   else if (strcmp (name, "pop") == 0)
11388     {
11389       struct mips_option_stack *s;
11390
11391       s = mips_opts_stack;
11392       if (s == NULL)
11393         as_bad (_(".set pop with no .set push"));
11394       else
11395         {
11396           /* If we're changing the reorder mode we need to handle
11397              delay slots correctly.  */
11398           if (s->options.noreorder && ! mips_opts.noreorder)
11399             mips_emit_delays (true);
11400           else if (! s->options.noreorder && mips_opts.noreorder)
11401             {
11402               if (prev_nop_frag != NULL)
11403                 {
11404                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11405                                             * (mips_opts.mips16 ? 2 : 4));
11406                   prev_nop_frag = NULL;
11407                 }
11408             }
11409
11410           mips_opts = s->options;
11411           mips_opts_stack = s->next;
11412           free (s);
11413         }
11414     }
11415   else
11416     {
11417       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11418     }
11419   *input_line_pointer = ch;
11420   demand_empty_rest_of_line ();
11421 }
11422
11423 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11424    .option pic2.  It means to generate SVR4 PIC calls.  */
11425
11426 static void
11427 s_abicalls (ignore)
11428      int ignore ATTRIBUTE_UNUSED;
11429 {
11430   mips_pic = SVR4_PIC;
11431   if (USE_GLOBAL_POINTER_OPT)
11432     {
11433       if (g_switch_seen && g_switch_value != 0)
11434         as_warn (_("-G may not be used with SVR4 PIC code"));
11435       g_switch_value = 0;
11436     }
11437   bfd_set_gp_size (stdoutput, 0);
11438   demand_empty_rest_of_line ();
11439 }
11440
11441 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11442    PIC code.  It sets the $gp register for the function based on the
11443    function address, which is in the register named in the argument.
11444    This uses a relocation against _gp_disp, which is handled specially
11445    by the linker.  The result is:
11446         lui     $gp,%hi(_gp_disp)
11447         addiu   $gp,$gp,%lo(_gp_disp)
11448         addu    $gp,$gp,.cpload argument
11449    The .cpload argument is normally $25 == $t9.  */
11450
11451 static void
11452 s_cpload (ignore)
11453      int ignore ATTRIBUTE_UNUSED;
11454 {
11455   expressionS ex;
11456   int icnt = 0;
11457
11458   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11459      .cpload is ignored.  */
11460   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11461     {
11462       s_ignore (0);
11463       return;
11464     }
11465
11466   /* .cpload should be in a .set noreorder section.  */
11467   if (mips_opts.noreorder == 0)
11468     as_warn (_(".cpload not in noreorder section"));
11469
11470   ex.X_op = O_symbol;
11471   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11472   ex.X_op_symbol = NULL;
11473   ex.X_add_number = 0;
11474
11475   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11476   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11477
11478   macro_build_lui (NULL, &icnt, &ex, GP);
11479   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11480                (int) BFD_RELOC_LO16);
11481
11482   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11483                GP, GP, tc_get_register (0));
11484
11485   demand_empty_rest_of_line ();
11486 }
11487
11488 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11489      .cpsetup $reg1, offset|$reg2, label
11490
11491    If offset is given, this results in:
11492      sd         $gp, offset($sp)
11493      lui        $gp, %hi(%neg(%gp_rel(label)))
11494      daddiu     $gp, $gp, %lo(%neg(%gp_rel(label)))
11495      addu       $gp, $gp, $reg1
11496
11497    If $reg2 is given, this results in:
11498      daddu      $reg2, $gp, $0
11499      lui        $gp, %hi(%neg(%gp_rel(label)))
11500      daddiu     $gp, $gp, %lo(%neg(%gp_rel(label)))
11501      addu       $gp, $gp, $reg1
11502  */
11503 static void
11504 s_cpsetup (ignore)
11505      int ignore ATTRIBUTE_UNUSED;
11506 {
11507   expressionS ex_off;
11508   expressionS ex_sym;
11509   int reg1;
11510   int icnt = 0;
11511   char *sym;
11512
11513   /* If we are not generating SVR4 PIC code, .cpload is ignored.
11514      We also need NewABI support.  */
11515   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11516     {
11517       s_ignore (0);
11518       return;
11519     }
11520
11521   reg1 = tc_get_register (0);
11522   SKIP_WHITESPACE ();
11523   if (*input_line_pointer != ',')
11524     {
11525       as_bad (_("missing argument separator ',' for .cpsetup"));
11526       return;
11527     }
11528   else
11529     input_line_pointer++;
11530   SKIP_WHITESPACE ();
11531   if (*input_line_pointer == '$')
11532     mips_cpreturn_register = tc_get_register (0);
11533   else
11534     mips_cpreturn_offset = get_absolute_expression ();
11535   SKIP_WHITESPACE ();
11536   if (*input_line_pointer != ',')
11537     {
11538       as_bad (_("missing argument separator ',' for .cpsetup"));
11539       return;
11540     }
11541   else
11542     input_line_pointer++;
11543   SKIP_WHITESPACE ();
11544   sym = input_line_pointer;
11545   while (ISALNUM (*input_line_pointer))
11546     input_line_pointer++;
11547   *input_line_pointer = 0;
11548
11549   ex_sym.X_op = O_symbol;
11550   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11551   ex_sym.X_op_symbol = NULL;
11552   ex_sym.X_add_number = 0;
11553
11554   if (mips_cpreturn_register == -1)
11555     {
11556       ex_off.X_op = O_constant;
11557       ex_off.X_add_symbol = NULL;
11558       ex_off.X_op_symbol = NULL;
11559       ex_off.X_add_number = mips_cpreturn_offset;
11560
11561       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11562                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11563     }
11564   else
11565     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11566                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11567
11568   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11569                (int) BFD_RELOC_GPREL16);
11570   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11571            NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11572   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11573            NULL, 0, 0, BFD_RELOC_HI16_S);
11574   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11575                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11576   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11577            NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11578   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11579            NULL, 0, 0, BFD_RELOC_LO16);
11580   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11581                "d,v,t", mips_gp_register, mips_gp_register, reg1);
11582
11583   demand_empty_rest_of_line ();
11584 }
11585
11586 static void
11587 s_cplocal (ignore)
11588      int ignore ATTRIBUTE_UNUSED;
11589 {
11590   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11591    .cplocal is ignored.  */
11592   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11593     {
11594       s_ignore (0);
11595       return;
11596     }
11597
11598   mips_gp_register = tc_get_register (0);
11599 }
11600
11601 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11602    offset from $sp.  The offset is remembered, and after making a PIC
11603    call $gp is restored from that location.  */
11604
11605 static void
11606 s_cprestore (ignore)
11607      int ignore ATTRIBUTE_UNUSED;
11608 {
11609   expressionS ex;
11610   int icnt = 0;
11611
11612   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11613    .cprestore is ignored.  */
11614   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11615     {
11616       s_ignore (0);
11617       return;
11618     }
11619
11620   mips_cprestore_offset = get_absolute_expression ();
11621   mips_cprestore_valid = 1;
11622
11623   ex.X_op = O_constant;
11624   ex.X_add_symbol = NULL;
11625   ex.X_op_symbol = NULL;
11626   ex.X_add_number = mips_cprestore_offset;
11627
11628   macro_build ((char *) NULL, &icnt, &ex,
11629                HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11630                "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11631
11632   demand_empty_rest_of_line ();
11633 }
11634
11635 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11636    was given in the preceeding .gpsetup, it results in:
11637      ld         $gp, offset($sp)
11638
11639    If a register $reg2 was given there, it results in:
11640      daddiu     $gp, $gp, $reg2
11641  */
11642 static void
11643 s_cpreturn (ignore)
11644      int ignore ATTRIBUTE_UNUSED;
11645 {
11646   expressionS ex;
11647   int icnt = 0;
11648
11649   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11650      We also need NewABI support.  */
11651   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11652     {
11653       s_ignore (0);
11654       return;
11655     }
11656
11657   if (mips_cpreturn_register == -1)
11658     {
11659       ex.X_op = O_constant;
11660       ex.X_add_symbol = NULL;
11661       ex.X_op_symbol = NULL;
11662       ex.X_add_number = mips_cpreturn_offset;
11663
11664       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11665                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11666     }
11667   else
11668     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11669                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11670
11671   demand_empty_rest_of_line ();
11672 }
11673
11674 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11675    code.  It sets the offset to use in gp_rel relocations.  */
11676
11677 static void
11678 s_gpvalue (ignore)
11679      int ignore ATTRIBUTE_UNUSED;
11680 {
11681   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11682      We also need NewABI support.  */
11683   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11684     {
11685       s_ignore (0);
11686       return;
11687     }
11688
11689   mips_cpreturn_offset = get_absolute_expression ();
11690
11691   demand_empty_rest_of_line ();
11692 }
11693
11694 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11695    code.  It generates a 32 bit GP relative reloc.  */
11696
11697 static void
11698 s_gpword (ignore)
11699      int ignore ATTRIBUTE_UNUSED;
11700 {
11701   symbolS *label;
11702   expressionS ex;
11703   char *p;
11704
11705   /* When not generating PIC code, this is treated as .word.  */
11706   if (mips_pic != SVR4_PIC)
11707     {
11708       s_cons (2);
11709       return;
11710     }
11711
11712   label = insn_labels != NULL ? insn_labels->label : NULL;
11713   mips_emit_delays (true);
11714   if (auto_align)
11715     mips_align (2, 0, label);
11716   mips_clear_insn_labels ();
11717
11718   expression (&ex);
11719
11720   if (ex.X_op != O_symbol || ex.X_add_number != 0)
11721     {
11722       as_bad (_("Unsupported use of .gpword"));
11723       ignore_rest_of_line ();
11724     }
11725
11726   p = frag_more (4);
11727   md_number_to_chars (p, (valueT) 0, 4);
11728   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11729                BFD_RELOC_GPREL32);
11730
11731   demand_empty_rest_of_line ();
11732 }
11733
11734 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
11735    tables in SVR4 PIC code.  */
11736
11737 static void
11738 s_cpadd (ignore)
11739      int ignore ATTRIBUTE_UNUSED;
11740 {
11741   int icnt = 0;
11742   int reg;
11743
11744   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11745      code.  */
11746   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11747     {
11748       s_ignore (0);
11749       return;
11750     }
11751
11752   /* Add $gp to the register named as an argument.  */
11753   reg = tc_get_register (0);
11754   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11755                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11756                "d,v,t", reg, reg, GP);
11757
11758   demand_empty_rest_of_line ();
11759 }
11760
11761 /* Handle the .insn pseudo-op.  This marks instruction labels in
11762    mips16 mode.  This permits the linker to handle them specially,
11763    such as generating jalx instructions when needed.  We also make
11764    them odd for the duration of the assembly, in order to generate the
11765    right sort of code.  We will make them even in the adjust_symtab
11766    routine, while leaving them marked.  This is convenient for the
11767    debugger and the disassembler.  The linker knows to make them odd
11768    again.  */
11769
11770 static void
11771 s_insn (ignore)
11772      int ignore ATTRIBUTE_UNUSED;
11773 {
11774   if (mips_opts.mips16)
11775     mips16_mark_labels ();
11776
11777   demand_empty_rest_of_line ();
11778 }
11779
11780 /* Handle a .stabn directive.  We need these in order to mark a label
11781    as being a mips16 text label correctly.  Sometimes the compiler
11782    will emit a label, followed by a .stabn, and then switch sections.
11783    If the label and .stabn are in mips16 mode, then the label is
11784    really a mips16 text label.  */
11785
11786 static void
11787 s_mips_stab (type)
11788      int type;
11789 {
11790   if (type == 'n' && mips_opts.mips16)
11791     mips16_mark_labels ();
11792
11793   s_stab (type);
11794 }
11795
11796 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11797  */
11798
11799 static void
11800 s_mips_weakext (ignore)
11801      int ignore ATTRIBUTE_UNUSED;
11802 {
11803   char *name;
11804   int c;
11805   symbolS *symbolP;
11806   expressionS exp;
11807
11808   name = input_line_pointer;
11809   c = get_symbol_end ();
11810   symbolP = symbol_find_or_make (name);
11811   S_SET_WEAK (symbolP);
11812   *input_line_pointer = c;
11813
11814   SKIP_WHITESPACE ();
11815
11816   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11817     {
11818       if (S_IS_DEFINED (symbolP))
11819         {
11820           as_bad ("ignoring attempt to redefine symbol %s",
11821                   S_GET_NAME (symbolP));
11822           ignore_rest_of_line ();
11823           return;
11824         }
11825
11826       if (*input_line_pointer == ',')
11827         {
11828           ++input_line_pointer;
11829           SKIP_WHITESPACE ();
11830         }
11831
11832       expression (&exp);
11833       if (exp.X_op != O_symbol)
11834         {
11835           as_bad ("bad .weakext directive");
11836           ignore_rest_of_line ();
11837           return;
11838         }
11839       symbol_set_value_expression (symbolP, &exp);
11840     }
11841
11842   demand_empty_rest_of_line ();
11843 }
11844
11845 /* Parse a register string into a number.  Called from the ECOFF code
11846    to parse .frame.  The argument is non-zero if this is the frame
11847    register, so that we can record it in mips_frame_reg.  */
11848
11849 int
11850 tc_get_register (frame)
11851      int frame;
11852 {
11853   int reg;
11854
11855   SKIP_WHITESPACE ();
11856   if (*input_line_pointer++ != '$')
11857     {
11858       as_warn (_("expected `$'"));
11859       reg = 0;
11860     }
11861   else if (ISDIGIT (*input_line_pointer))
11862     {
11863       reg = get_absolute_expression ();
11864       if (reg < 0 || reg >= 32)
11865         {
11866           as_warn (_("Bad register number"));
11867           reg = 0;
11868         }
11869     }
11870   else
11871     {
11872       if (strncmp (input_line_pointer, "fp", 2) == 0)
11873         reg = FP;
11874       else if (strncmp (input_line_pointer, "sp", 2) == 0)
11875         reg = SP;
11876       else if (strncmp (input_line_pointer, "gp", 2) == 0)
11877         reg = GP;
11878       else if (strncmp (input_line_pointer, "at", 2) == 0)
11879         reg = AT;
11880       else
11881         {
11882           as_warn (_("Unrecognized register name"));
11883           reg = 0;
11884         }
11885       input_line_pointer += 2;
11886     }
11887   if (frame)
11888     {
11889       mips_frame_reg = reg != 0 ? reg : SP;
11890       mips_frame_reg_valid = 1;
11891       mips_cprestore_valid = 0;
11892     }
11893   return reg;
11894 }
11895
11896 valueT
11897 md_section_align (seg, addr)
11898      asection *seg;
11899      valueT addr;
11900 {
11901   int align = bfd_get_section_alignment (stdoutput, seg);
11902
11903 #ifdef OBJ_ELF
11904   /* We don't need to align ELF sections to the full alignment.
11905      However, Irix 5 may prefer that we align them at least to a 16
11906      byte boundary.  We don't bother to align the sections if we are
11907      targeted for an embedded system.  */
11908   if (strcmp (TARGET_OS, "elf") == 0)
11909     return addr;
11910   if (align > 4)
11911     align = 4;
11912 #endif
11913
11914   return ((addr + (1 << align) - 1) & (-1 << align));
11915 }
11916
11917 /* Utility routine, called from above as well.  If called while the
11918    input file is still being read, it's only an approximation.  (For
11919    example, a symbol may later become defined which appeared to be
11920    undefined earlier.)  */
11921
11922 static int
11923 nopic_need_relax (sym, before_relaxing)
11924      symbolS *sym;
11925      int before_relaxing;
11926 {
11927   if (sym == 0)
11928     return 0;
11929
11930   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11931     {
11932       const char *symname;
11933       int change;
11934
11935       /* Find out whether this symbol can be referenced off the GP
11936          register.  It can be if it is smaller than the -G size or if
11937          it is in the .sdata or .sbss section.  Certain symbols can
11938          not be referenced off the GP, although it appears as though
11939          they can.  */
11940       symname = S_GET_NAME (sym);
11941       if (symname != (const char *) NULL
11942           && (strcmp (symname, "eprol") == 0
11943               || strcmp (symname, "etext") == 0
11944               || strcmp (symname, "_gp") == 0
11945               || strcmp (symname, "edata") == 0
11946               || strcmp (symname, "_fbss") == 0
11947               || strcmp (symname, "_fdata") == 0
11948               || strcmp (symname, "_ftext") == 0
11949               || strcmp (symname, "end") == 0
11950               || strcmp (symname, "_gp_disp") == 0))
11951         change = 1;
11952       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11953                && (0
11954 #ifndef NO_ECOFF_DEBUGGING
11955                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
11956                        && (symbol_get_obj (sym)->ecoff_extern_size
11957                            <= g_switch_value))
11958 #endif
11959                    /* We must defer this decision until after the whole
11960                       file has been read, since there might be a .extern
11961                       after the first use of this symbol.  */
11962                    || (before_relaxing
11963 #ifndef NO_ECOFF_DEBUGGING
11964                        && symbol_get_obj (sym)->ecoff_extern_size == 0
11965 #endif
11966                        && S_GET_VALUE (sym) == 0)
11967                    || (S_GET_VALUE (sym) != 0
11968                        && S_GET_VALUE (sym) <= g_switch_value)))
11969         change = 0;
11970       else
11971         {
11972           const char *segname;
11973
11974           segname = segment_name (S_GET_SEGMENT (sym));
11975           assert (strcmp (segname, ".lit8") != 0
11976                   && strcmp (segname, ".lit4") != 0);
11977           change = (strcmp (segname, ".sdata") != 0
11978                     && strcmp (segname, ".sbss") != 0
11979                     && strncmp (segname, ".sdata.", 7) != 0
11980                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11981         }
11982       return change;
11983     }
11984   else
11985     /* We are not optimizing for the GP register.  */
11986     return 1;
11987 }
11988
11989 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11990    extended opcode.  SEC is the section the frag is in.  */
11991
11992 static int
11993 mips16_extended_frag (fragp, sec, stretch)
11994      fragS *fragp;
11995      asection *sec;
11996      long stretch;
11997 {
11998   int type;
11999   register const struct mips16_immed_operand *op;
12000   offsetT val;
12001   int mintiny, maxtiny;
12002   segT symsec;
12003   fragS *sym_frag;
12004
12005   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12006     return 0;
12007   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12008     return 1;
12009
12010   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12011   op = mips16_immed_operands;
12012   while (op->type != type)
12013     {
12014       ++op;
12015       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12016     }
12017
12018   if (op->unsp)
12019     {
12020       if (type == '<' || type == '>' || type == '[' || type == ']')
12021         {
12022           mintiny = 1;
12023           maxtiny = 1 << op->nbits;
12024         }
12025       else
12026         {
12027           mintiny = 0;
12028           maxtiny = (1 << op->nbits) - 1;
12029         }
12030     }
12031   else
12032     {
12033       mintiny = - (1 << (op->nbits - 1));
12034       maxtiny = (1 << (op->nbits - 1)) - 1;
12035     }
12036
12037   sym_frag = symbol_get_frag (fragp->fr_symbol);
12038   val = S_GET_VALUE (fragp->fr_symbol);
12039   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12040
12041   if (op->pcrel)
12042     {
12043       addressT addr;
12044
12045       /* We won't have the section when we are called from
12046          mips_relax_frag.  However, we will always have been called
12047          from md_estimate_size_before_relax first.  If this is a
12048          branch to a different section, we mark it as such.  If SEC is
12049          NULL, and the frag is not marked, then it must be a branch to
12050          the same section.  */
12051       if (sec == NULL)
12052         {
12053           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12054             return 1;
12055         }
12056       else
12057         {
12058           /* Must have been called from md_estimate_size_before_relax.  */
12059           if (symsec != sec)
12060             {
12061               fragp->fr_subtype =
12062                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12063
12064               /* FIXME: We should support this, and let the linker
12065                  catch branches and loads that are out of range.  */
12066               as_bad_where (fragp->fr_file, fragp->fr_line,
12067                             _("unsupported PC relative reference to different section"));
12068
12069               return 1;
12070             }
12071           if (fragp != sym_frag && sym_frag->fr_address == 0)
12072             /* Assume non-extended on the first relaxation pass.
12073                The address we have calculated will be bogus if this is
12074                a forward branch to another frag, as the forward frag
12075                will have fr_address == 0.  */
12076             return 0;
12077         }
12078
12079       /* In this case, we know for sure that the symbol fragment is in
12080          the same section.  If the relax_marker of the symbol fragment
12081          differs from the relax_marker of this fragment, we have not
12082          yet adjusted the symbol fragment fr_address.  We want to add
12083          in STRETCH in order to get a better estimate of the address.
12084          This particularly matters because of the shift bits.  */
12085       if (stretch != 0
12086           && sym_frag->relax_marker != fragp->relax_marker)
12087         {
12088           fragS *f;
12089
12090           /* Adjust stretch for any alignment frag.  Note that if have
12091              been expanding the earlier code, the symbol may be
12092              defined in what appears to be an earlier frag.  FIXME:
12093              This doesn't handle the fr_subtype field, which specifies
12094              a maximum number of bytes to skip when doing an
12095              alignment.  */
12096           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12097             {
12098               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12099                 {
12100                   if (stretch < 0)
12101                     stretch = - ((- stretch)
12102                                  & ~ ((1 << (int) f->fr_offset) - 1));
12103                   else
12104                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12105                   if (stretch == 0)
12106                     break;
12107                 }
12108             }
12109           if (f != NULL)
12110             val += stretch;
12111         }
12112
12113       addr = fragp->fr_address + fragp->fr_fix;
12114
12115       /* The base address rules are complicated.  The base address of
12116          a branch is the following instruction.  The base address of a
12117          PC relative load or add is the instruction itself, but if it
12118          is in a delay slot (in which case it can not be extended) use
12119          the address of the instruction whose delay slot it is in.  */
12120       if (type == 'p' || type == 'q')
12121         {
12122           addr += 2;
12123
12124           /* If we are currently assuming that this frag should be
12125              extended, then, the current address is two bytes
12126              higher.  */
12127           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12128             addr += 2;
12129
12130           /* Ignore the low bit in the target, since it will be set
12131              for a text label.  */
12132           if ((val & 1) != 0)
12133             --val;
12134         }
12135       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12136         addr -= 4;
12137       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12138         addr -= 2;
12139
12140       val -= addr & ~ ((1 << op->shift) - 1);
12141
12142       /* Branch offsets have an implicit 0 in the lowest bit.  */
12143       if (type == 'p' || type == 'q')
12144         val /= 2;
12145
12146       /* If any of the shifted bits are set, we must use an extended
12147          opcode.  If the address depends on the size of this
12148          instruction, this can lead to a loop, so we arrange to always
12149          use an extended opcode.  We only check this when we are in
12150          the main relaxation loop, when SEC is NULL.  */
12151       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12152         {
12153           fragp->fr_subtype =
12154             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12155           return 1;
12156         }
12157
12158       /* If we are about to mark a frag as extended because the value
12159          is precisely maxtiny + 1, then there is a chance of an
12160          infinite loop as in the following code:
12161              la $4,foo
12162              .skip      1020
12163              .align     2
12164            foo:
12165          In this case when the la is extended, foo is 0x3fc bytes
12166          away, so the la can be shrunk, but then foo is 0x400 away, so
12167          the la must be extended.  To avoid this loop, we mark the
12168          frag as extended if it was small, and is about to become
12169          extended with a value of maxtiny + 1.  */
12170       if (val == ((maxtiny + 1) << op->shift)
12171           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12172           && sec == NULL)
12173         {
12174           fragp->fr_subtype =
12175             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12176           return 1;
12177         }
12178     }
12179   else if (symsec != absolute_section && sec != NULL)
12180     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12181
12182   if ((val & ((1 << op->shift) - 1)) != 0
12183       || val < (mintiny << op->shift)
12184       || val > (maxtiny << op->shift))
12185     return 1;
12186   else
12187     return 0;
12188 }
12189
12190 /* Estimate the size of a frag before relaxing.  Unless this is the
12191    mips16, we are not really relaxing here, and the final size is
12192    encoded in the subtype information.  For the mips16, we have to
12193    decide whether we are using an extended opcode or not.  */
12194
12195 int
12196 md_estimate_size_before_relax (fragp, segtype)
12197      fragS *fragp;
12198      asection *segtype;
12199 {
12200   int change = 0;
12201   boolean linkonce = false;
12202
12203   if (RELAX_MIPS16_P (fragp->fr_subtype))
12204     /* We don't want to modify the EXTENDED bit here; it might get us
12205        into infinite loops.  We change it only in mips_relax_frag().  */
12206     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12207
12208   if (mips_pic == NO_PIC)
12209     {
12210       change = nopic_need_relax (fragp->fr_symbol, 0);
12211     }
12212   else if (mips_pic == SVR4_PIC)
12213     {
12214       symbolS *sym;
12215       asection *symsec;
12216
12217       sym = fragp->fr_symbol;
12218
12219       /* Handle the case of a symbol equated to another symbol.  */
12220       while (symbol_equated_reloc_p (sym))
12221         {
12222           symbolS *n;
12223
12224           /* It's possible to get a loop here in a badly written
12225              program.  */
12226           n = symbol_get_value_expression (sym)->X_add_symbol;
12227           if (n == sym)
12228             break;
12229           sym = n;
12230         }
12231
12232       symsec = S_GET_SEGMENT (sym);
12233
12234       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12235       if (symsec != segtype && ! S_IS_LOCAL (sym))
12236         {
12237           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12238               != 0)
12239             linkonce = true;
12240
12241           /* The GNU toolchain uses an extension for ELF: a section
12242              beginning with the magic string .gnu.linkonce is a linkonce
12243              section.  */
12244           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12245                        sizeof ".gnu.linkonce" - 1) == 0)
12246             linkonce = true;
12247         }
12248
12249       /* This must duplicate the test in adjust_reloc_syms.  */
12250       change = (symsec != &bfd_und_section
12251                 && symsec != &bfd_abs_section
12252                 && ! bfd_is_com_section (symsec)
12253                 && !linkonce
12254 #ifdef OBJ_ELF
12255                 /* A global or weak symbol is treated as external.  */
12256                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12257                     || (! S_IS_WEAK (sym)
12258                         && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12259 #endif
12260                 );
12261     }
12262   else
12263     abort ();
12264
12265   if (change)
12266     {
12267       /* Record the offset to the first reloc in the fr_opcode field.
12268          This lets md_convert_frag and tc_gen_reloc know that the code
12269          must be expanded.  */
12270       fragp->fr_opcode = (fragp->fr_literal
12271                           + fragp->fr_fix
12272                           - RELAX_OLD (fragp->fr_subtype)
12273                           + RELAX_RELOC1 (fragp->fr_subtype));
12274       /* FIXME: This really needs as_warn_where.  */
12275       if (RELAX_WARN (fragp->fr_subtype))
12276         as_warn (_("AT used after \".set noat\" or macro used after "
12277                    "\".set nomacro\""));
12278
12279       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12280     }
12281
12282   return 0;
12283 }
12284
12285 /* This is called to see whether a reloc against a defined symbol
12286    should be converted into a reloc against a section.  Don't adjust
12287    MIPS16 jump relocations, so we don't have to worry about the format
12288    of the offset in the .o file.  Don't adjust relocations against
12289    mips16 symbols, so that the linker can find them if it needs to set
12290    up a stub.  */
12291
12292 int
12293 mips_fix_adjustable (fixp)
12294      fixS *fixp;
12295 {
12296 #ifdef OBJ_ELF
12297   /* Prevent all adjustments to global symbols.  */
12298   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12299       && mips_pic != EMBEDDED_PIC
12300       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12301     return 0;
12302 #endif
12303   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12304     return 0;
12305   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12306       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12307     return 0;
12308   if (fixp->fx_addsy == NULL)
12309     return 1;
12310 #ifdef OBJ_ELF
12311   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12312       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12313       && fixp->fx_subsy == NULL)
12314     return 0;
12315 #endif
12316   return 1;
12317 }
12318
12319 /* Translate internal representation of relocation info to BFD target
12320    format.  */
12321
12322 arelent **
12323 tc_gen_reloc (section, fixp)
12324      asection *section ATTRIBUTE_UNUSED;
12325      fixS *fixp;
12326 {
12327   static arelent *retval[4];
12328   arelent *reloc;
12329   bfd_reloc_code_real_type code;
12330
12331   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12332   retval[1] = NULL;
12333
12334   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12335   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12336   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12337
12338   if (mips_pic == EMBEDDED_PIC
12339       && SWITCH_TABLE (fixp))
12340     {
12341       /* For a switch table entry we use a special reloc.  The addend
12342          is actually the difference between the reloc address and the
12343          subtrahend.  */
12344       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12345       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12346         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12347       fixp->fx_r_type = BFD_RELOC_GPREL32;
12348     }
12349   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12350     {
12351       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12352         reloc->addend = fixp->fx_addnumber;
12353       else
12354         {
12355           /* We use a special addend for an internal RELLO reloc.  */
12356           if (symbol_section_p (fixp->fx_addsy))
12357             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12358           else
12359             reloc->addend = fixp->fx_addnumber + reloc->address;
12360         }
12361     }
12362   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12363     {
12364       assert (fixp->fx_next != NULL
12365               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12366
12367       /* The reloc is relative to the RELLO; adjust the addend
12368          accordingly.  */
12369       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12370         reloc->addend = fixp->fx_next->fx_addnumber;
12371       else
12372         {
12373           /* We use a special addend for an internal RELHI reloc.  */
12374           if (symbol_section_p (fixp->fx_addsy))
12375             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12376                              + fixp->fx_next->fx_where
12377                              - S_GET_VALUE (fixp->fx_subsy));
12378           else
12379             reloc->addend = (fixp->fx_addnumber
12380                              + fixp->fx_next->fx_frag->fr_address
12381                              + fixp->fx_next->fx_where);
12382         }
12383     }
12384   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12385     reloc->addend = fixp->fx_addnumber;
12386   else
12387     {
12388       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12389         /* A gruesome hack which is a result of the gruesome gas reloc
12390            handling.  */
12391         reloc->addend = reloc->address;
12392       else
12393         reloc->addend = -reloc->address;
12394     }
12395
12396   /* If this is a variant frag, we may need to adjust the existing
12397      reloc and generate a new one.  */
12398   if (fixp->fx_frag->fr_opcode != NULL
12399       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12400           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12401           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12402           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12403           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12404           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12405           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12406       && ! HAVE_NEWABI)
12407     {
12408       arelent *reloc2;
12409
12410       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12411
12412       /* If this is not the last reloc in this frag, then we have two
12413          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12414          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12415          the second one handle all of them.  */
12416       if (fixp->fx_next != NULL
12417           && fixp->fx_frag == fixp->fx_next->fx_frag)
12418         {
12419           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12420                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12421                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12422                       && (fixp->fx_next->fx_r_type
12423                           == BFD_RELOC_MIPS_GOT_LO16))
12424                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12425                       && (fixp->fx_next->fx_r_type
12426                           == BFD_RELOC_MIPS_CALL_LO16)));
12427           retval[0] = NULL;
12428           return retval;
12429         }
12430
12431       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12432       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12433       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12434       retval[2] = NULL;
12435       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12436       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12437       reloc2->address = (reloc->address
12438                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12439                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12440       reloc2->addend = fixp->fx_addnumber;
12441       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12442       assert (reloc2->howto != NULL);
12443
12444       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12445         {
12446           arelent *reloc3;
12447
12448           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12449           retval[3] = NULL;
12450           *reloc3 = *reloc2;
12451           reloc3->address += 4;
12452         }
12453
12454       if (mips_pic == NO_PIC)
12455         {
12456           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12457           fixp->fx_r_type = BFD_RELOC_HI16_S;
12458         }
12459       else if (mips_pic == SVR4_PIC)
12460         {
12461           switch (fixp->fx_r_type)
12462             {
12463             default:
12464               abort ();
12465             case BFD_RELOC_MIPS_GOT16:
12466               break;
12467             case BFD_RELOC_MIPS_CALL16:
12468             case BFD_RELOC_MIPS_GOT_LO16:
12469             case BFD_RELOC_MIPS_CALL_LO16:
12470               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12471               break;
12472             }
12473         }
12474       else
12475         abort ();
12476     }
12477
12478   /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12479      to be used in the relocation's section offset.  */
12480   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12481     {
12482       reloc->address = reloc->addend;
12483       reloc->addend = 0;
12484     }
12485
12486   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12487      fixup_segment converted a non-PC relative reloc into a PC
12488      relative reloc.  In such a case, we need to convert the reloc
12489      code.  */
12490   code = fixp->fx_r_type;
12491   if (fixp->fx_pcrel)
12492     {
12493       switch (code)
12494         {
12495         case BFD_RELOC_8:
12496           code = BFD_RELOC_8_PCREL;
12497           break;
12498         case BFD_RELOC_16:
12499           code = BFD_RELOC_16_PCREL;
12500           break;
12501         case BFD_RELOC_32:
12502           code = BFD_RELOC_32_PCREL;
12503           break;
12504         case BFD_RELOC_64:
12505           code = BFD_RELOC_64_PCREL;
12506           break;
12507         case BFD_RELOC_8_PCREL:
12508         case BFD_RELOC_16_PCREL:
12509         case BFD_RELOC_32_PCREL:
12510         case BFD_RELOC_64_PCREL:
12511         case BFD_RELOC_16_PCREL_S2:
12512         case BFD_RELOC_PCREL_HI16_S:
12513         case BFD_RELOC_PCREL_LO16:
12514           break;
12515         default:
12516           as_bad_where (fixp->fx_file, fixp->fx_line,
12517                         _("Cannot make %s relocation PC relative"),
12518                         bfd_get_reloc_code_name (code));
12519         }
12520     }
12521
12522 #ifdef OBJ_ELF
12523   /* md_apply_fix3 has a double-subtraction hack to get
12524      bfd_install_relocation to behave nicely.  GPREL relocations are
12525      handled correctly without this hack, so undo it here.  We can't
12526      stop md_apply_fix3 from subtracting twice in the first place since
12527      the fake addend is required for variant frags above.  */
12528   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12529       && code == BFD_RELOC_GPREL16
12530       && reloc->addend != 0
12531       && mips_need_elf_addend_fixup (fixp))
12532     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12533 #endif
12534
12535   /* To support a PC relative reloc when generating embedded PIC code
12536      for ECOFF, we use a Cygnus extension.  We check for that here to
12537      make sure that we don't let such a reloc escape normally.  */
12538   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12539        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12540       && code == BFD_RELOC_16_PCREL_S2
12541       && mips_pic != EMBEDDED_PIC)
12542     reloc->howto = NULL;
12543   else
12544     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12545
12546   if (reloc->howto == NULL)
12547     {
12548       as_bad_where (fixp->fx_file, fixp->fx_line,
12549                     _("Can not represent %s relocation in this object file format"),
12550                     bfd_get_reloc_code_name (code));
12551       retval[0] = NULL;
12552     }
12553
12554   return retval;
12555 }
12556
12557 /* Relax a machine dependent frag.  This returns the amount by which
12558    the current size of the frag should change.  */
12559
12560 int
12561 mips_relax_frag (fragp, stretch)
12562      fragS *fragp;
12563      long stretch;
12564 {
12565   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12566     return 0;
12567
12568   if (mips16_extended_frag (fragp, NULL, stretch))
12569     {
12570       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12571         return 0;
12572       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12573       return 2;
12574     }
12575   else
12576     {
12577       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12578         return 0;
12579       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12580       return -2;
12581     }
12582
12583   return 0;
12584 }
12585
12586 /* Convert a machine dependent frag.  */
12587
12588 void
12589 md_convert_frag (abfd, asec, fragp)
12590      bfd *abfd ATTRIBUTE_UNUSED;
12591      segT asec;
12592      fragS *fragp;
12593 {
12594   int old, new;
12595   char *fixptr;
12596
12597   if (RELAX_MIPS16_P (fragp->fr_subtype))
12598     {
12599       int type;
12600       register const struct mips16_immed_operand *op;
12601       boolean small, ext;
12602       offsetT val;
12603       bfd_byte *buf;
12604       unsigned long insn;
12605       boolean use_extend;
12606       unsigned short extend;
12607
12608       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12609       op = mips16_immed_operands;
12610       while (op->type != type)
12611         ++op;
12612
12613       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12614         {
12615           small = false;
12616           ext = true;
12617         }
12618       else
12619         {
12620           small = true;
12621           ext = false;
12622         }
12623
12624       resolve_symbol_value (fragp->fr_symbol);
12625       val = S_GET_VALUE (fragp->fr_symbol);
12626       if (op->pcrel)
12627         {
12628           addressT addr;
12629
12630           addr = fragp->fr_address + fragp->fr_fix;
12631
12632           /* The rules for the base address of a PC relative reloc are
12633              complicated; see mips16_extended_frag.  */
12634           if (type == 'p' || type == 'q')
12635             {
12636               addr += 2;
12637               if (ext)
12638                 addr += 2;
12639               /* Ignore the low bit in the target, since it will be
12640                  set for a text label.  */
12641               if ((val & 1) != 0)
12642                 --val;
12643             }
12644           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12645             addr -= 4;
12646           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12647             addr -= 2;
12648
12649           addr &= ~ (addressT) ((1 << op->shift) - 1);
12650           val -= addr;
12651
12652           /* Make sure the section winds up with the alignment we have
12653              assumed.  */
12654           if (op->shift > 0)
12655             record_alignment (asec, op->shift);
12656         }
12657
12658       if (ext
12659           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12660               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12661         as_warn_where (fragp->fr_file, fragp->fr_line,
12662                        _("extended instruction in delay slot"));
12663
12664       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12665
12666       if (target_big_endian)
12667         insn = bfd_getb16 (buf);
12668       else
12669         insn = bfd_getl16 (buf);
12670
12671       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12672                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12673                     small, ext, &insn, &use_extend, &extend);
12674
12675       if (use_extend)
12676         {
12677           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
12678           fragp->fr_fix += 2;
12679           buf += 2;
12680         }
12681
12682       md_number_to_chars ((char *) buf, insn, 2);
12683       fragp->fr_fix += 2;
12684       buf += 2;
12685     }
12686   else
12687     {
12688       if (fragp->fr_opcode == NULL)
12689         return;
12690
12691       old = RELAX_OLD (fragp->fr_subtype);
12692       new = RELAX_NEW (fragp->fr_subtype);
12693       fixptr = fragp->fr_literal + fragp->fr_fix;
12694
12695       if (new > 0)
12696         memcpy (fixptr - old, fixptr, new);
12697
12698       fragp->fr_fix += new - old;
12699     }
12700 }
12701
12702 #ifdef OBJ_ELF
12703
12704 /* This function is called after the relocs have been generated.
12705    We've been storing mips16 text labels as odd.  Here we convert them
12706    back to even for the convenience of the debugger.  */
12707
12708 void
12709 mips_frob_file_after_relocs ()
12710 {
12711   asymbol **syms;
12712   unsigned int count, i;
12713
12714   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12715     return;
12716
12717   syms = bfd_get_outsymbols (stdoutput);
12718   count = bfd_get_symcount (stdoutput);
12719   for (i = 0; i < count; i++, syms++)
12720     {
12721       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12722           && ((*syms)->value & 1) != 0)
12723         {
12724           (*syms)->value &= ~1;
12725           /* If the symbol has an odd size, it was probably computed
12726              incorrectly, so adjust that as well.  */
12727           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12728             ++elf_symbol (*syms)->internal_elf_sym.st_size;
12729         }
12730     }
12731 }
12732
12733 #endif
12734
12735 /* This function is called whenever a label is defined.  It is used
12736    when handling branch delays; if a branch has a label, we assume we
12737    can not move it.  */
12738
12739 void
12740 mips_define_label (sym)
12741      symbolS *sym;
12742 {
12743   struct insn_label_list *l;
12744
12745   if (free_insn_labels == NULL)
12746     l = (struct insn_label_list *) xmalloc (sizeof *l);
12747   else
12748     {
12749       l = free_insn_labels;
12750       free_insn_labels = l->next;
12751     }
12752
12753   l->label = sym;
12754   l->next = insn_labels;
12755   insn_labels = l;
12756 }
12757 \f
12758 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12759
12760 /* Some special processing for a MIPS ELF file.  */
12761
12762 void
12763 mips_elf_final_processing ()
12764 {
12765   /* Write out the register information.  */
12766   if (file_mips_abi != N64_ABI)
12767     {
12768       Elf32_RegInfo s;
12769
12770       s.ri_gprmask = mips_gprmask;
12771       s.ri_cprmask[0] = mips_cprmask[0];
12772       s.ri_cprmask[1] = mips_cprmask[1];
12773       s.ri_cprmask[2] = mips_cprmask[2];
12774       s.ri_cprmask[3] = mips_cprmask[3];
12775       /* The gp_value field is set by the MIPS ELF backend.  */
12776
12777       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12778                                        ((Elf32_External_RegInfo *)
12779                                         mips_regmask_frag));
12780     }
12781   else
12782     {
12783       Elf64_Internal_RegInfo s;
12784
12785       s.ri_gprmask = mips_gprmask;
12786       s.ri_pad = 0;
12787       s.ri_cprmask[0] = mips_cprmask[0];
12788       s.ri_cprmask[1] = mips_cprmask[1];
12789       s.ri_cprmask[2] = mips_cprmask[2];
12790       s.ri_cprmask[3] = mips_cprmask[3];
12791       /* The gp_value field is set by the MIPS ELF backend.  */
12792
12793       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12794                                        ((Elf64_External_RegInfo *)
12795                                         mips_regmask_frag));
12796     }
12797
12798   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
12799      sort of BFD interface for this.  */
12800   if (mips_any_noreorder)
12801     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12802   if (mips_pic != NO_PIC)
12803     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12804
12805   /* Set MIPS ELF flags for ASEs.  */
12806 #if 0 /* XXX FIXME */
12807   if (file_ase_mips3d)
12808     elf_elfheader (stdoutput)->e_flags |= ???;
12809 #endif
12810
12811   /* Set the MIPS ELF ABI flags.  */
12812   if (file_mips_abi == NO_ABI)
12813     ;
12814   else if (file_mips_abi == O32_ABI)
12815     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12816   else if (file_mips_abi == O64_ABI)
12817     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12818   else if (file_mips_abi == EABI_ABI)
12819     {
12820       if (mips_eabi64)
12821         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12822       else
12823         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12824     }
12825   else if (file_mips_abi == N32_ABI)
12826     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12827
12828   /* Nothing to do for "64".  */
12829
12830   if (mips_32bitmode)
12831     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12832 }
12833
12834 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12835 \f
12836 typedef struct proc {
12837   symbolS *isym;
12838   unsigned long reg_mask;
12839   unsigned long reg_offset;
12840   unsigned long fpreg_mask;
12841   unsigned long fpreg_offset;
12842   unsigned long frame_offset;
12843   unsigned long frame_reg;
12844   unsigned long pc_reg;
12845 } procS;
12846
12847 static procS cur_proc;
12848 static procS *cur_proc_ptr;
12849 static int numprocs;
12850
12851 /* Fill in an rs_align_code fragment.  */
12852
12853 void
12854 mips_handle_align (fragp)
12855      fragS *fragp;
12856 {
12857   if (fragp->fr_type != rs_align_code)
12858     return;
12859
12860   if (mips_opts.mips16)
12861     {
12862       static const unsigned char be_nop[] = { 0x65, 0x00 };
12863       static const unsigned char le_nop[] = { 0x00, 0x65 };
12864
12865       int bytes;
12866       char *p;
12867
12868       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12869       p = fragp->fr_literal + fragp->fr_fix;
12870
12871       if (bytes & 1)
12872         {
12873           *p++ = 0;
12874           fragp->fr_fix += 1;
12875         }
12876
12877       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12878       fragp->fr_var = 2;
12879     }
12880
12881   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
12882 }
12883
12884 static void
12885 md_obj_begin ()
12886 {
12887 }
12888
12889 static void
12890 md_obj_end ()
12891 {
12892   /* check for premature end, nesting errors, etc */
12893   if (cur_proc_ptr)
12894     as_warn (_("missing .end at end of assembly"));
12895 }
12896
12897 static long
12898 get_number ()
12899 {
12900   int negative = 0;
12901   long val = 0;
12902
12903   if (*input_line_pointer == '-')
12904     {
12905       ++input_line_pointer;
12906       negative = 1;
12907     }
12908   if (!ISDIGIT (*input_line_pointer))
12909     as_bad (_("expected simple number"));
12910   if (input_line_pointer[0] == '0')
12911     {
12912       if (input_line_pointer[1] == 'x')
12913         {
12914           input_line_pointer += 2;
12915           while (ISXDIGIT (*input_line_pointer))
12916             {
12917               val <<= 4;
12918               val |= hex_value (*input_line_pointer++);
12919             }
12920           return negative ? -val : val;
12921         }
12922       else
12923         {
12924           ++input_line_pointer;
12925           while (ISDIGIT (*input_line_pointer))
12926             {
12927               val <<= 3;
12928               val |= *input_line_pointer++ - '0';
12929             }
12930           return negative ? -val : val;
12931         }
12932     }
12933   if (!ISDIGIT (*input_line_pointer))
12934     {
12935       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12936               *input_line_pointer, *input_line_pointer);
12937       as_warn (_("invalid number"));
12938       return -1;
12939     }
12940   while (ISDIGIT (*input_line_pointer))
12941     {
12942       val *= 10;
12943       val += *input_line_pointer++ - '0';
12944     }
12945   return negative ? -val : val;
12946 }
12947
12948 /* The .file directive; just like the usual .file directive, but there
12949    is an initial number which is the ECOFF file index.  */
12950
12951 static void
12952 s_file (x)
12953      int x ATTRIBUTE_UNUSED;
12954 {
12955   get_number ();
12956   s_app_file (0);
12957 }
12958
12959 /* The .end directive.  */
12960
12961 static void
12962 s_mips_end (x)
12963      int x ATTRIBUTE_UNUSED;
12964 {
12965   symbolS *p;
12966   int maybe_text;
12967
12968   /* Following functions need their own .frame and .cprestore directives.  */
12969   mips_frame_reg_valid = 0;
12970   mips_cprestore_valid = 0;
12971
12972   if (!is_end_of_line[(unsigned char) *input_line_pointer])
12973     {
12974       p = get_symbol ();
12975       demand_empty_rest_of_line ();
12976     }
12977   else
12978     p = NULL;
12979
12980 #ifdef BFD_ASSEMBLER
12981   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12982     maybe_text = 1;
12983   else
12984     maybe_text = 0;
12985 #else
12986   if (now_seg != data_section && now_seg != bss_section)
12987     maybe_text = 1;
12988   else
12989     maybe_text = 0;
12990 #endif
12991
12992   if (!maybe_text)
12993     as_warn (_(".end not in text section"));
12994
12995   if (!cur_proc_ptr)
12996     {
12997       as_warn (_(".end directive without a preceding .ent directive."));
12998       demand_empty_rest_of_line ();
12999       return;
13000     }
13001
13002   if (p != NULL)
13003     {
13004       assert (S_GET_NAME (p));
13005       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13006         as_warn (_(".end symbol does not match .ent symbol."));
13007     }
13008   else
13009     as_warn (_(".end directive missing or unknown symbol"));
13010
13011 #ifdef MIPS_STABS_ELF
13012   {
13013     segT saved_seg = now_seg;
13014     subsegT saved_subseg = now_subseg;
13015     valueT dot;
13016     expressionS exp;
13017     char *fragp;
13018
13019     dot = frag_now_fix ();
13020
13021 #ifdef md_flush_pending_output
13022     md_flush_pending_output ();
13023 #endif
13024
13025     assert (pdr_seg);
13026     subseg_set (pdr_seg, 0);
13027
13028     /* Write the symbol.  */
13029     exp.X_op = O_symbol;
13030     exp.X_add_symbol = p;
13031     exp.X_add_number = 0;
13032     emit_expr (&exp, 4);
13033
13034     fragp = frag_more (7 * 4);
13035
13036     md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13037     md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13038     md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13039     md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13040     md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13041     md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13042     md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13043
13044     subseg_set (saved_seg, saved_subseg);
13045   }
13046 #endif
13047
13048   cur_proc_ptr = NULL;
13049 }
13050
13051 /* The .aent and .ent directives.  */
13052
13053 static void
13054 s_mips_ent (aent)
13055      int aent;
13056 {
13057   symbolS *symbolP;
13058   int maybe_text;
13059
13060   symbolP = get_symbol ();
13061   if (*input_line_pointer == ',')
13062     input_line_pointer++;
13063   SKIP_WHITESPACE ();
13064   if (ISDIGIT (*input_line_pointer)
13065       || *input_line_pointer == '-')
13066     get_number ();
13067
13068 #ifdef BFD_ASSEMBLER
13069   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13070     maybe_text = 1;
13071   else
13072     maybe_text = 0;
13073 #else
13074   if (now_seg != data_section && now_seg != bss_section)
13075     maybe_text = 1;
13076   else
13077     maybe_text = 0;
13078 #endif
13079
13080   if (!maybe_text)
13081     as_warn (_(".ent or .aent not in text section."));
13082
13083   if (!aent && cur_proc_ptr)
13084     as_warn (_("missing .end"));
13085
13086   if (!aent)
13087     {
13088       /* This function needs its own .frame and .cprestore directives.  */
13089       mips_frame_reg_valid = 0;
13090       mips_cprestore_valid = 0;
13091
13092       cur_proc_ptr = &cur_proc;
13093       memset (cur_proc_ptr, '\0', sizeof (procS));
13094
13095       cur_proc_ptr->isym = symbolP;
13096
13097       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13098
13099       numprocs++;
13100     }
13101
13102   demand_empty_rest_of_line ();
13103 }
13104
13105 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13106    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13107    s_mips_frame is used so that we can set the PDR information correctly.
13108    We can't use the ecoff routines because they make reference to the ecoff
13109    symbol table (in the mdebug section).  */
13110
13111 static void
13112 s_mips_frame (ignore)
13113      int ignore ATTRIBUTE_UNUSED;
13114 {
13115 #ifdef MIPS_STABS_ELF
13116
13117   long val;
13118
13119   if (cur_proc_ptr == (procS *) NULL)
13120     {
13121       as_warn (_(".frame outside of .ent"));
13122       demand_empty_rest_of_line ();
13123       return;
13124     }
13125
13126   cur_proc_ptr->frame_reg = tc_get_register (1);
13127
13128   SKIP_WHITESPACE ();
13129   if (*input_line_pointer++ != ','
13130       || get_absolute_expression_and_terminator (&val) != ',')
13131     {
13132       as_warn (_("Bad .frame directive"));
13133       --input_line_pointer;
13134       demand_empty_rest_of_line ();
13135       return;
13136     }
13137
13138   cur_proc_ptr->frame_offset = val;
13139   cur_proc_ptr->pc_reg = tc_get_register (0);
13140
13141   demand_empty_rest_of_line ();
13142 #else
13143   s_ignore (ignore);
13144 #endif /* MIPS_STABS_ELF */
13145 }
13146
13147 /* The .fmask and .mask directives. If the mdebug section is present
13148    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13149    embedded targets, s_mips_mask is used so that we can set the PDR
13150    information correctly. We can't use the ecoff routines because they
13151    make reference to the ecoff symbol table (in the mdebug section).  */
13152
13153 static void
13154 s_mips_mask (reg_type)
13155      char reg_type;
13156 {
13157 #ifdef MIPS_STABS_ELF
13158   long mask, off;
13159
13160   if (cur_proc_ptr == (procS *) NULL)
13161     {
13162       as_warn (_(".mask/.fmask outside of .ent"));
13163       demand_empty_rest_of_line ();
13164       return;
13165     }
13166
13167   if (get_absolute_expression_and_terminator (&mask) != ',')
13168     {
13169       as_warn (_("Bad .mask/.fmask directive"));
13170       --input_line_pointer;
13171       demand_empty_rest_of_line ();
13172       return;
13173     }
13174
13175   off = get_absolute_expression ();
13176
13177   if (reg_type == 'F')
13178     {
13179       cur_proc_ptr->fpreg_mask = mask;
13180       cur_proc_ptr->fpreg_offset = off;
13181     }
13182   else
13183     {
13184       cur_proc_ptr->reg_mask = mask;
13185       cur_proc_ptr->reg_offset = off;
13186     }
13187
13188   demand_empty_rest_of_line ();
13189 #else
13190   s_ignore (reg_type);
13191 #endif /* MIPS_STABS_ELF */
13192 }
13193
13194 /* The .loc directive.  */
13195
13196 #if 0
13197 static void
13198 s_loc (x)
13199      int x;
13200 {
13201   symbolS *symbolP;
13202   int lineno;
13203   int addroff;
13204
13205   assert (now_seg == text_section);
13206
13207   lineno = get_number ();
13208   addroff = frag_now_fix ();
13209
13210   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13211   S_SET_TYPE (symbolP, N_SLINE);
13212   S_SET_OTHER (symbolP, 0);
13213   S_SET_DESC (symbolP, lineno);
13214   symbolP->sy_segment = now_seg;
13215 }
13216 #endif
13217
13218 /* CPU name/ISA/number mapping table.
13219
13220    Entries are grouped by type.  The first matching CPU or ISA entry
13221    gets chosen by CPU or ISA, so it should be the 'canonical' name
13222    for that type.  Entries after that within the type are sorted
13223    alphabetically.
13224
13225    Case is ignored in comparison, so put the canonical entry in the
13226    appropriate case but everything else in lower case to ease eye pain.  */
13227 static const struct mips_cpu_info mips_cpu_info_table[] =
13228 {
13229   /* MIPS1 ISA */
13230   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13231   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13232
13233   /* MIPS2 ISA */
13234   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13235
13236   /* MIPS3 ISA */
13237   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13238
13239   /* MIPS4 ISA */
13240   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13241
13242   /* MIPS5 ISA */
13243   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13244   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13245
13246   /* MIPS32 ISA */
13247   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13248   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13249   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13250   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13251   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13252   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13253
13254   /* For historical reasons.  */
13255   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13256
13257   /* MIPS64 ISA */
13258   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13259   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13260   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13261   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13262
13263   /* R2000 CPU */
13264   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13265   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13266   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13267   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13268
13269   /* R3000 CPU */
13270   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13271   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13272   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13273   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13274
13275   /* TX3900 CPU */
13276   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13277   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13278   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13279
13280   /* R4000 CPU */
13281   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13282   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13283   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13284   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13285
13286   /* R4010 CPU */
13287   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13288   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13289
13290   /* R4400 CPU */
13291   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13292   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13293
13294   /* R4600 CPU */
13295   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13296   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13297   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13298   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13299
13300   /* R4650 CPU */
13301   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13302   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13303
13304   /* R6000 CPU */
13305   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13306   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13307   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13308   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13309
13310   /* R8000 CPU */
13311   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13312   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13313   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13314   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13315
13316   /* R10000 CPU */
13317   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13318   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13319   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13320   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13321
13322   /* R12000 CPU */
13323   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13324   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13325   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13326   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13327
13328   /* VR4100 CPU */
13329   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13330   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13331   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13332   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13333
13334   /* VR4111 CPU */
13335   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13336   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13337   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13338   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13339
13340   /* VR4300 CPU */
13341   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13342   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13343   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13344   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13345
13346   /* VR5000 CPU */
13347   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13348   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13349   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13350   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13351   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13352   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13353   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13354   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13355   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13356   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13357   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13358   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13359   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13360   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13361   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13362   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13363   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13364
13365   /* Broadcom SB-1 CPU */
13366   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13367   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13368   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13369   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13370
13371   /* End marker.  */
13372   { NULL, 0, 0, 0, },
13373 };
13374
13375 static const struct mips_cpu_info *
13376 mips_cpu_info_from_name (name)
13377      const char *name;
13378 {
13379   int i;
13380
13381   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13382     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13383       return (&mips_cpu_info_table[i]);
13384
13385   return NULL;
13386 }
13387
13388 static const struct mips_cpu_info *
13389 mips_cpu_info_from_isa (isa)
13390      int isa;
13391 {
13392   int i;
13393
13394   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13395     if (mips_cpu_info_table[i].is_isa
13396       && isa == mips_cpu_info_table[i].isa)
13397       return (&mips_cpu_info_table[i]);
13398
13399   return NULL;
13400 }
13401
13402 static const struct mips_cpu_info *
13403 mips_cpu_info_from_cpu (cpu)
13404      int cpu;
13405 {
13406   int i;
13407
13408   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13409     if (!mips_cpu_info_table[i].is_isa
13410       && cpu == mips_cpu_info_table[i].cpu)
13411       return (&mips_cpu_info_table[i]);
13412
13413   return NULL;
13414 }