2002-04-22 Eric Christopher <echristo@redhat.com>
[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
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 (! HAVE_NEWABI)
1383           {
1384             sec = subseg_new (".reginfo", (subsegT) 0);
1385
1386             (void) bfd_set_section_flags (stdoutput, sec, flags);
1387             (void) bfd_set_section_alignment (stdoutput, sec, 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             (void) bfd_set_section_flags (stdoutput, sec, flags);
1399             (void) 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           unsigned long 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       if (strcmp (fmt, insn.insn_mo->args) == 0
2917           && insn.insn_mo->pinfo != INSN_MACRO
2918           && OPCODE_IS_MEMBER (insn.insn_mo,
2919                                (mips_opts.isa
2920                                 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
2921                                mips_arch)
2922           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2923         break;
2924
2925       ++insn.insn_mo;
2926       assert (insn.insn_mo->name);
2927       assert (strcmp (name, insn.insn_mo->name) == 0);
2928     }
2929
2930   insn.insn_opcode = insn.insn_mo->match;
2931   for (;;)
2932     {
2933       switch (*fmt++)
2934         {
2935         case '\0':
2936           break;
2937
2938         case ',':
2939         case '(':
2940         case ')':
2941           continue;
2942
2943         case 't':
2944         case 'w':
2945         case 'E':
2946           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2947           continue;
2948
2949         case 'c':
2950           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2951           continue;
2952
2953         case 'T':
2954         case 'W':
2955           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2956           continue;
2957
2958         case 'd':
2959         case 'G':
2960           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2961           continue;
2962
2963         case 'U':
2964           {
2965             int tmp = va_arg (args, int);
2966
2967             insn.insn_opcode |= tmp << OP_SH_RT;
2968             insn.insn_opcode |= tmp << OP_SH_RD;
2969             continue;
2970           }
2971
2972         case 'V':
2973         case 'S':
2974           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2975           continue;
2976
2977         case 'z':
2978           continue;
2979
2980         case '<':
2981           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2982           continue;
2983
2984         case 'D':
2985           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2986           continue;
2987
2988         case 'B':
2989           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2990           continue;
2991
2992         case 'J':
2993           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2994           continue;
2995
2996         case 'q':
2997           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2998           continue;
2999
3000         case 'b':
3001         case 's':
3002         case 'r':
3003         case 'v':
3004           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3005           continue;
3006
3007         case 'i':
3008         case 'j':
3009         case 'o':
3010           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3011           assert (*r == BFD_RELOC_GPREL16
3012                   || *r == BFD_RELOC_MIPS_LITERAL
3013                   || *r == BFD_RELOC_MIPS_HIGHER
3014                   || *r == BFD_RELOC_HI16_S
3015                   || *r == BFD_RELOC_LO16
3016                   || *r == BFD_RELOC_MIPS_GOT16
3017                   || *r == BFD_RELOC_MIPS_CALL16
3018                   || *r == BFD_RELOC_MIPS_GOT_LO16
3019                   || *r == BFD_RELOC_MIPS_CALL_LO16
3020                   || (ep->X_op == O_subtract
3021                       && *r == BFD_RELOC_PCREL_LO16));
3022           continue;
3023
3024         case 'u':
3025           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3026           assert (ep != NULL
3027                   && (ep->X_op == O_constant
3028                       || (ep->X_op == O_symbol
3029                           && (*r == BFD_RELOC_MIPS_HIGHEST
3030                               || *r == BFD_RELOC_HI16_S
3031                               || *r == BFD_RELOC_HI16
3032                               || *r == BFD_RELOC_GPREL16
3033                               || *r == BFD_RELOC_MIPS_GOT_HI16
3034                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3035                       || (ep->X_op == O_subtract
3036                           && *r == BFD_RELOC_PCREL_HI16_S)));
3037           continue;
3038
3039         case 'p':
3040           assert (ep != NULL);
3041           /*
3042            * This allows macro() to pass an immediate expression for
3043            * creating short branches without creating a symbol.
3044            * Note that the expression still might come from the assembly
3045            * input, in which case the value is not checked for range nor
3046            * is a relocation entry generated (yuck).
3047            */
3048           if (ep->X_op == O_constant)
3049             {
3050               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3051               ep = NULL;
3052             }
3053           else
3054             if (mips_pic == EMBEDDED_PIC)
3055               *r = BFD_RELOC_16_PCREL_S2;
3056             else
3057               *r = BFD_RELOC_16_PCREL;
3058           continue;
3059
3060         case 'a':
3061           assert (ep != NULL);
3062           *r = BFD_RELOC_MIPS_JMP;
3063           continue;
3064
3065         case 'C':
3066           insn.insn_opcode |= va_arg (args, unsigned long);
3067           continue;
3068
3069         default:
3070           internalError ();
3071         }
3072       break;
3073     }
3074   va_end (args);
3075   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3076
3077   append_insn (place, &insn, ep, r, false);
3078 }
3079
3080 static void
3081 mips16_macro_build (place, counter, ep, name, fmt, args)
3082      char *place;
3083      int *counter ATTRIBUTE_UNUSED;
3084      expressionS *ep;
3085      const char *name;
3086      const char *fmt;
3087      va_list args;
3088 {
3089   struct mips_cl_insn insn;
3090   bfd_reloc_code_real_type r[3]
3091     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3092
3093   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3094   assert (insn.insn_mo);
3095   assert (strcmp (name, insn.insn_mo->name) == 0);
3096
3097   while (strcmp (fmt, insn.insn_mo->args) != 0
3098          || insn.insn_mo->pinfo == INSN_MACRO)
3099     {
3100       ++insn.insn_mo;
3101       assert (insn.insn_mo->name);
3102       assert (strcmp (name, insn.insn_mo->name) == 0);
3103     }
3104
3105   insn.insn_opcode = insn.insn_mo->match;
3106   insn.use_extend = false;
3107
3108   for (;;)
3109     {
3110       int c;
3111
3112       c = *fmt++;
3113       switch (c)
3114         {
3115         case '\0':
3116           break;
3117
3118         case ',':
3119         case '(':
3120         case ')':
3121           continue;
3122
3123         case 'y':
3124         case 'w':
3125           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3126           continue;
3127
3128         case 'x':
3129         case 'v':
3130           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3131           continue;
3132
3133         case 'z':
3134           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3135           continue;
3136
3137         case 'Z':
3138           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3139           continue;
3140
3141         case '0':
3142         case 'S':
3143         case 'P':
3144         case 'R':
3145           continue;
3146
3147         case 'X':
3148           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3149           continue;
3150
3151         case 'Y':
3152           {
3153             int regno;
3154
3155             regno = va_arg (args, int);
3156             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3157             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3158           }
3159           continue;
3160
3161         case '<':
3162         case '>':
3163         case '4':
3164         case '5':
3165         case 'H':
3166         case 'W':
3167         case 'D':
3168         case 'j':
3169         case '8':
3170         case 'V':
3171         case 'C':
3172         case 'U':
3173         case 'k':
3174         case 'K':
3175         case 'p':
3176         case 'q':
3177           {
3178             assert (ep != NULL);
3179
3180             if (ep->X_op != O_constant)
3181               *r = (int) BFD_RELOC_UNUSED + c;
3182             else
3183               {
3184                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3185                               false, &insn.insn_opcode, &insn.use_extend,
3186                               &insn.extend);
3187                 ep = NULL;
3188                 *r = BFD_RELOC_UNUSED;
3189               }
3190           }
3191           continue;
3192
3193         case '6':
3194           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3195           continue;
3196         }
3197
3198       break;
3199     }
3200
3201   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3202
3203   append_insn (place, &insn, ep, r, false);
3204 }
3205
3206 /*
3207  * Generate a "lui" instruction.
3208  */
3209 static void
3210 macro_build_lui (place, counter, ep, regnum)
3211      char *place;
3212      int *counter;
3213      expressionS *ep;
3214      int regnum;
3215 {
3216   expressionS high_expr;
3217   struct mips_cl_insn insn;
3218   bfd_reloc_code_real_type r[3]
3219     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3220   CONST char *name = "lui";
3221   CONST char *fmt = "t,u";
3222
3223   assert (! mips_opts.mips16);
3224
3225   if (place == NULL)
3226     high_expr = *ep;
3227   else
3228     {
3229       high_expr.X_op = O_constant;
3230       high_expr.X_add_number = ep->X_add_number;
3231     }
3232
3233   if (high_expr.X_op == O_constant)
3234     {
3235       /* we can compute the instruction now without a relocation entry */
3236       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3237                                 >> 16) & 0xffff;
3238       *r = BFD_RELOC_UNUSED;
3239     }
3240   else if (! HAVE_NEWABI)
3241     {
3242       assert (ep->X_op == O_symbol);
3243       /* _gp_disp is a special case, used from s_cpload.  */
3244       assert (mips_pic == NO_PIC
3245               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3246       *r = BFD_RELOC_HI16_S;
3247     }
3248
3249   /*
3250    * If the macro is about to expand into a second instruction,
3251    * print a warning if needed. We need to pass ip as a parameter
3252    * to generate a better warning message here...
3253    */
3254   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3255     as_warn (_("Macro instruction expanded into multiple instructions"));
3256
3257   if (place == NULL)
3258     *counter += 1;              /* bump instruction counter */
3259
3260   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3261   assert (insn.insn_mo);
3262   assert (strcmp (name, insn.insn_mo->name) == 0);
3263   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3264
3265   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3266   if (*r == BFD_RELOC_UNUSED)
3267     {
3268       insn.insn_opcode |= high_expr.X_add_number;
3269       append_insn (place, &insn, NULL, r, false);
3270     }
3271   else
3272     append_insn (place, &insn, &high_expr, r, false);
3273 }
3274
3275 /*                      set_at()
3276  * Generates code to set the $at register to true (one)
3277  * if reg is less than the immediate expression.
3278  */
3279 static void
3280 set_at (counter, reg, unsignedp)
3281      int *counter;
3282      int reg;
3283      int unsignedp;
3284 {
3285   if (imm_expr.X_op == O_constant
3286       && imm_expr.X_add_number >= -0x8000
3287       && imm_expr.X_add_number < 0x8000)
3288     macro_build ((char *) NULL, counter, &imm_expr,
3289                  unsignedp ? "sltiu" : "slti",
3290                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3291   else
3292     {
3293       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3294       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3295                    unsignedp ? "sltu" : "slt",
3296                    "d,v,t", AT, reg, AT);
3297     }
3298 }
3299
3300 /* Warn if an expression is not a constant.  */
3301
3302 static void
3303 check_absolute_expr (ip, ex)
3304      struct mips_cl_insn *ip;
3305      expressionS *ex;
3306 {
3307   if (ex->X_op == O_big)
3308     as_bad (_("unsupported large constant"));
3309   else if (ex->X_op != O_constant)
3310     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3311 }
3312
3313 /* Count the leading zeroes by performing a binary chop. This is a
3314    bulky bit of source, but performance is a LOT better for the
3315    majority of values than a simple loop to count the bits:
3316        for (lcnt = 0; (lcnt < 32); lcnt++)
3317          if ((v) & (1 << (31 - lcnt)))
3318            break;
3319   However it is not code size friendly, and the gain will drop a bit
3320   on certain cached systems.
3321 */
3322 #define COUNT_TOP_ZEROES(v)             \
3323   (((v) & ~0xffff) == 0                 \
3324    ? ((v) & ~0xff) == 0                 \
3325      ? ((v) & ~0xf) == 0                \
3326        ? ((v) & ~0x3) == 0              \
3327          ? ((v) & ~0x1) == 0            \
3328            ? !(v)                       \
3329              ? 32                       \
3330              : 31                       \
3331            : 30                         \
3332          : ((v) & ~0x7) == 0            \
3333            ? 29                         \
3334            : 28                         \
3335        : ((v) & ~0x3f) == 0             \
3336          ? ((v) & ~0x1f) == 0           \
3337            ? 27                         \
3338            : 26                         \
3339          : ((v) & ~0x7f) == 0           \
3340            ? 25                         \
3341            : 24                         \
3342      : ((v) & ~0xfff) == 0              \
3343        ? ((v) & ~0x3ff) == 0            \
3344          ? ((v) & ~0x1ff) == 0          \
3345            ? 23                         \
3346            : 22                         \
3347          : ((v) & ~0x7ff) == 0          \
3348            ? 21                         \
3349            : 20                         \
3350        : ((v) & ~0x3fff) == 0           \
3351          ? ((v) & ~0x1fff) == 0         \
3352            ? 19                         \
3353            : 18                         \
3354          : ((v) & ~0x7fff) == 0         \
3355            ? 17                         \
3356            : 16                         \
3357    : ((v) & ~0xffffff) == 0             \
3358      ? ((v) & ~0xfffff) == 0            \
3359        ? ((v) & ~0x3ffff) == 0          \
3360          ? ((v) & ~0x1ffff) == 0        \
3361            ? 15                         \
3362            : 14                         \
3363          : ((v) & ~0x7ffff) == 0        \
3364            ? 13                         \
3365            : 12                         \
3366        : ((v) & ~0x3fffff) == 0         \
3367          ? ((v) & ~0x1fffff) == 0       \
3368            ? 11                         \
3369            : 10                         \
3370          : ((v) & ~0x7fffff) == 0       \
3371            ? 9                          \
3372            : 8                          \
3373      : ((v) & ~0xfffffff) == 0          \
3374        ? ((v) & ~0x3ffffff) == 0        \
3375          ? ((v) & ~0x1ffffff) == 0      \
3376            ? 7                          \
3377            : 6                          \
3378          : ((v) & ~0x7ffffff) == 0      \
3379            ? 5                          \
3380            : 4                          \
3381        : ((v) & ~0x3fffffff) == 0       \
3382          ? ((v) & ~0x1fffffff) == 0     \
3383            ? 3                          \
3384            : 2                          \
3385          : ((v) & ~0x7fffffff) == 0     \
3386            ? 1                          \
3387            : 0)
3388
3389 /* Is the given value a sign-extended 32-bit value?  */
3390 #define IS_SEXT_32BIT_NUM(x)                                            \
3391   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
3392    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3393
3394 /*                      load_register()
3395  *  This routine generates the least number of instructions neccessary to load
3396  *  an absolute expression value into a register.
3397  */
3398 static void
3399 load_register (counter, reg, ep, dbl)
3400      int *counter;
3401      int reg;
3402      expressionS *ep;
3403      int dbl;
3404 {
3405   int freg;
3406   expressionS hi32, lo32;
3407
3408   if (ep->X_op != O_big)
3409     {
3410       assert (ep->X_op == O_constant);
3411       if (ep->X_add_number < 0x8000
3412           && (ep->X_add_number >= 0
3413               || (ep->X_add_number >= -0x8000
3414                   && (! dbl
3415                       || ! ep->X_unsigned
3416                       || sizeof (ep->X_add_number) > 4))))
3417         {
3418           /* We can handle 16 bit signed values with an addiu to
3419              $zero.  No need to ever use daddiu here, since $zero and
3420              the result are always correct in 32 bit mode.  */
3421           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3422                        (int) BFD_RELOC_LO16);
3423           return;
3424         }
3425       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3426         {
3427           /* We can handle 16 bit unsigned values with an ori to
3428              $zero.  */
3429           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3430                        (int) BFD_RELOC_LO16);
3431           return;
3432         }
3433       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3434                 && (! dbl
3435                     || ! ep->X_unsigned
3436                     || sizeof (ep->X_add_number) > 4
3437                     || (ep->X_add_number & 0x80000000) == 0))
3438                || ((HAVE_32BIT_GPRS || ! dbl)
3439                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3440                || (HAVE_32BIT_GPRS
3441                    && ! dbl
3442                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3443                        == ~ (offsetT) 0xffffffff)))
3444         {
3445           /* 32 bit values require an lui.  */
3446           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3447                        (int) BFD_RELOC_HI16);
3448           if ((ep->X_add_number & 0xffff) != 0)
3449             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3450                          (int) BFD_RELOC_LO16);
3451           return;
3452         }
3453     }
3454
3455   /* The value is larger than 32 bits.  */
3456
3457   if (HAVE_32BIT_GPRS)
3458     {
3459       as_bad (_("Number (0x%lx) larger than 32 bits"),
3460               (unsigned long) ep->X_add_number);
3461       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3462                    (int) BFD_RELOC_LO16);
3463       return;
3464     }
3465
3466   if (ep->X_op != O_big)
3467     {
3468       hi32 = *ep;
3469       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3470       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3471       hi32.X_add_number &= 0xffffffff;
3472       lo32 = *ep;
3473       lo32.X_add_number &= 0xffffffff;
3474     }
3475   else
3476     {
3477       assert (ep->X_add_number > 2);
3478       if (ep->X_add_number == 3)
3479         generic_bignum[3] = 0;
3480       else if (ep->X_add_number > 4)
3481         as_bad (_("Number larger than 64 bits"));
3482       lo32.X_op = O_constant;
3483       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3484       hi32.X_op = O_constant;
3485       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3486     }
3487
3488   if (hi32.X_add_number == 0)
3489     freg = 0;
3490   else
3491     {
3492       int shift, bit;
3493       unsigned long hi, lo;
3494
3495       if (hi32.X_add_number == (offsetT) 0xffffffff)
3496         {
3497           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3498             {
3499               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3500                            reg, 0, (int) BFD_RELOC_LO16);
3501               return;
3502             }
3503           if (lo32.X_add_number & 0x80000000)
3504             {
3505               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3506                            (int) BFD_RELOC_HI16);
3507               if (lo32.X_add_number & 0xffff)
3508                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3509                              reg, reg, (int) BFD_RELOC_LO16);
3510               return;
3511             }
3512         }
3513
3514       /* Check for 16bit shifted constant.  We know that hi32 is
3515          non-zero, so start the mask on the first bit of the hi32
3516          value.  */
3517       shift = 17;
3518       do
3519         {
3520           unsigned long himask, lomask;
3521
3522           if (shift < 32)
3523             {
3524               himask = 0xffff >> (32 - shift);
3525               lomask = (0xffff << shift) & 0xffffffff;
3526             }
3527           else
3528             {
3529               himask = 0xffff << (shift - 32);
3530               lomask = 0;
3531             }
3532           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3533               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3534             {
3535               expressionS tmp;
3536
3537               tmp.X_op = O_constant;
3538               if (shift < 32)
3539                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3540                                     | (lo32.X_add_number >> shift));
3541               else
3542                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3543               macro_build ((char *) NULL, counter, &tmp,
3544                            "ori", "t,r,i", reg, 0,
3545                            (int) BFD_RELOC_LO16);
3546               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3547                            (shift >= 32) ? "dsll32" : "dsll",
3548                            "d,w,<", reg, reg,
3549                            (shift >= 32) ? shift - 32 : shift);
3550               return;
3551             }
3552           shift++;
3553         }
3554       while (shift <= (64 - 16));
3555
3556       /* Find the bit number of the lowest one bit, and store the
3557          shifted value in hi/lo.  */
3558       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3559       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3560       if (lo != 0)
3561         {
3562           bit = 0;
3563           while ((lo & 1) == 0)
3564             {
3565               lo >>= 1;
3566               ++bit;
3567             }
3568           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3569           hi >>= bit;
3570         }
3571       else
3572         {
3573           bit = 32;
3574           while ((hi & 1) == 0)
3575             {
3576               hi >>= 1;
3577               ++bit;
3578             }
3579           lo = hi;
3580           hi = 0;
3581         }
3582
3583       /* Optimize if the shifted value is a (power of 2) - 1.  */
3584       if ((hi == 0 && ((lo + 1) & lo) == 0)
3585           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3586         {
3587           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3588           if (shift != 0)
3589             {
3590               expressionS tmp;
3591
3592               /* This instruction will set the register to be all
3593                  ones.  */
3594               tmp.X_op = O_constant;
3595               tmp.X_add_number = (offsetT) -1;
3596               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3597                            reg, 0, (int) BFD_RELOC_LO16);
3598               if (bit != 0)
3599                 {
3600                   bit += shift;
3601                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3602                                (bit >= 32) ? "dsll32" : "dsll",
3603                                "d,w,<", reg, reg,
3604                                (bit >= 32) ? bit - 32 : bit);
3605                 }
3606               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3607                            (shift >= 32) ? "dsrl32" : "dsrl",
3608                            "d,w,<", reg, reg,
3609                            (shift >= 32) ? shift - 32 : shift);
3610               return;
3611             }
3612         }
3613
3614       /* Sign extend hi32 before calling load_register, because we can
3615          generally get better code when we load a sign extended value.  */
3616       if ((hi32.X_add_number & 0x80000000) != 0)
3617         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3618       load_register (counter, reg, &hi32, 0);
3619       freg = reg;
3620     }
3621   if ((lo32.X_add_number & 0xffff0000) == 0)
3622     {
3623       if (freg != 0)
3624         {
3625           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3626                        "dsll32", "d,w,<", reg, freg, 0);
3627           freg = reg;
3628         }
3629     }
3630   else
3631     {
3632       expressionS mid16;
3633
3634       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3635         {
3636           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3637                        (int) BFD_RELOC_HI16);
3638           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3639                        "dsrl32", "d,w,<", reg, reg, 0);
3640           return;
3641         }
3642
3643       if (freg != 0)
3644         {
3645           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3646                        "d,w,<", reg, freg, 16);
3647           freg = reg;
3648         }
3649       mid16 = lo32;
3650       mid16.X_add_number >>= 16;
3651       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3652                    freg, (int) BFD_RELOC_LO16);
3653       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3654                    "d,w,<", reg, reg, 16);
3655       freg = reg;
3656     }
3657   if ((lo32.X_add_number & 0xffff) != 0)
3658     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3659                  (int) BFD_RELOC_LO16);
3660 }
3661
3662 /* Load an address into a register.  */
3663
3664 static void
3665 load_address (counter, reg, ep, dbl, used_at)
3666      int *counter;
3667      int reg;
3668      expressionS *ep;
3669      int dbl;
3670      int *used_at;
3671 {
3672   char *p;
3673
3674   if (ep->X_op != O_constant
3675       && ep->X_op != O_symbol)
3676     {
3677       as_bad (_("expression too complex"));
3678       ep->X_op = O_constant;
3679     }
3680
3681   if (ep->X_op == O_constant)
3682     {
3683       load_register (counter, reg, ep, dbl);
3684       return;
3685     }
3686
3687   if (mips_pic == NO_PIC)
3688     {
3689       /* If this is a reference to a GP relative symbol, we want
3690            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3691          Otherwise we want
3692            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3693            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3694          If we have an addend, we always use the latter form.
3695
3696          With 64bit address space and a usable $at we want
3697            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3698            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3699            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3700            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3701            dsll32       $reg,0
3702            dadd         $reg,$reg,$at
3703
3704          If $at is already in use, we use an path which is suboptimal
3705          on superscalar processors.
3706            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3707            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3708            dsll         $reg,16
3709            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3710            dsll         $reg,16
3711            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3712        */
3713       if (dbl)
3714         {
3715           p = NULL;
3716
3717           /* We don't do GP optimization for now because RELAX_ENCODE can't
3718              hold the data for such large chunks.  */
3719
3720           if (*used_at == 0)
3721             {
3722               macro_build (p, counter, ep, "lui", "t,u",
3723                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3724               macro_build (p, counter, ep, "lui", "t,u",
3725                            AT, (int) BFD_RELOC_HI16_S);
3726               macro_build (p, counter, ep, "daddiu", "t,r,j",
3727                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3728               macro_build (p, counter, ep, "daddiu", "t,r,j",
3729                            AT, AT, (int) BFD_RELOC_LO16);
3730               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3731                            "d,w,<", reg, reg, 0);
3732               macro_build (p, counter, (expressionS *) NULL, "dadd",
3733                            "d,v,t", reg, reg, AT);
3734               *used_at = 1;
3735             }
3736           else
3737             {
3738               macro_build (p, counter, ep, "lui", "t,u",
3739                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3740               macro_build (p, counter, ep, "daddiu", "t,r,j",
3741                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3742               macro_build (p, counter, (expressionS *) NULL, "dsll",
3743                            "d,w,<", reg, reg, 16);
3744               macro_build (p, counter, ep, "daddiu", "t,r,j",
3745                            reg, reg, (int) BFD_RELOC_HI16_S);
3746               macro_build (p, counter, (expressionS *) NULL, "dsll",
3747                            "d,w,<", reg, reg, 16);
3748               macro_build (p, counter, ep, "daddiu", "t,r,j",
3749                            reg, reg, (int) BFD_RELOC_LO16);
3750             }
3751         }
3752       else
3753         {
3754           p = NULL;
3755           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3756               && ! nopic_need_relax (ep->X_add_symbol, 1))
3757             {
3758               frag_grow (20);
3759               macro_build ((char *) NULL, counter, ep,
3760                            dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3761                            (int) BFD_RELOC_GPREL16);
3762               p = frag_var (rs_machine_dependent, 8, 0,
3763                             RELAX_ENCODE (4, 8, 0, 4, 0,
3764                                           mips_opts.warn_about_macros),
3765                             ep->X_add_symbol, 0, NULL);
3766             }
3767           macro_build_lui (p, counter, ep, reg);
3768           if (p != NULL)
3769             p += 4;
3770           macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3771                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3772         }
3773     }
3774   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3775     {
3776       expressionS ex;
3777
3778       /* If this is a reference to an external symbol, we want
3779            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3780          Otherwise we want
3781            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3782            nop
3783            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3784          If there is a constant, it must be added in after.  */
3785       ex.X_add_number = ep->X_add_number;
3786       ep->X_add_number = 0;
3787       frag_grow (20);
3788       macro_build ((char *) NULL, counter, ep,
3789                    HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3790                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3791       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3792       p = frag_var (rs_machine_dependent, 4, 0,
3793                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3794                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3795       macro_build (p, counter, ep,
3796                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3797                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3798       if (ex.X_add_number != 0)
3799         {
3800           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3801             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3802           ex.X_op = O_constant;
3803           macro_build ((char *) NULL, counter, &ex,
3804                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3805                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3806         }
3807     }
3808   else if (mips_pic == SVR4_PIC)
3809     {
3810       expressionS ex;
3811       int off;
3812
3813       /* This is the large GOT case.  If this is a reference to an
3814          external symbol, we want
3815            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3816            addu         $reg,$reg,$gp
3817            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3818          Otherwise, for a reference to a local symbol, we want
3819            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3820            nop
3821            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3822          If there is a constant, it must be added in after.  */
3823       ex.X_add_number = ep->X_add_number;
3824       ep->X_add_number = 0;
3825       if (reg_needs_delay (GP))
3826         off = 4;
3827       else
3828         off = 0;
3829       frag_grow (32);
3830       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3831                    (int) BFD_RELOC_MIPS_GOT_HI16);
3832       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3833                    dbl ? "daddu" : "addu", "d,v,t", reg, reg, GP);
3834       macro_build ((char *) NULL, counter, ep, dbl ? "ld" : "lw",
3835                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3836       p = frag_var (rs_machine_dependent, 12 + off, 0,
3837                     RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3838                                   mips_opts.warn_about_macros),
3839                     ep->X_add_symbol, 0, NULL);
3840       if (off > 0)
3841         {
3842           /* We need a nop before loading from $gp.  This special
3843              check is required because the lui which starts the main
3844              instruction stream does not refer to $gp, and so will not
3845              insert the nop which may be required.  */
3846           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3847           p += 4;
3848         }
3849       macro_build (p, counter, ep, dbl ? "ld" : "lw",
3850                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3851       p += 4;
3852       macro_build (p, counter, (expressionS *) NULL, "nop", "");
3853       p += 4;
3854       macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3855                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3856       if (ex.X_add_number != 0)
3857         {
3858           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3859             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3860           ex.X_op = O_constant;
3861           macro_build ((char *) NULL, counter, &ex, dbl ? "daddiu" : "addiu",
3862                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3863         }
3864     }
3865   else if (mips_pic == EMBEDDED_PIC)
3866     {
3867       /* We always do
3868            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3869          */
3870       macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3871                    "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
3872     }
3873   else
3874     abort ();
3875 }
3876
3877 /* Move the contents of register SOURCE into register DEST.  */
3878
3879 static void
3880 move_register (counter, dest, source)
3881      int *counter;
3882      int dest;
3883      int source;
3884 {
3885   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3886                HAVE_32BIT_GPRS ? "addu" : "daddu",
3887                "d,v,t", dest, source, 0);
3888 }
3889
3890 /*
3891  *                      Build macros
3892  *   This routine implements the seemingly endless macro or synthesized
3893  * instructions and addressing modes in the mips assembly language. Many
3894  * of these macros are simple and are similar to each other. These could
3895  * probably be handled by some kind of table or grammer aproach instead of
3896  * this verbose method. Others are not simple macros but are more like
3897  * optimizing code generation.
3898  *   One interesting optimization is when several store macros appear
3899  * consecutivly that would load AT with the upper half of the same address.
3900  * The ensuing load upper instructions are ommited. This implies some kind
3901  * of global optimization. We currently only optimize within a single macro.
3902  *   For many of the load and store macros if the address is specified as a
3903  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3904  * first load register 'at' with zero and use it as the base register. The
3905  * mips assembler simply uses register $zero. Just one tiny optimization
3906  * we're missing.
3907  */
3908 static void
3909 macro (ip)
3910      struct mips_cl_insn *ip;
3911 {
3912   register int treg, sreg, dreg, breg;
3913   int tempreg;
3914   int mask;
3915   int icnt = 0;
3916   int used_at = 0;
3917   expressionS expr1;
3918   const char *s;
3919   const char *s2;
3920   const char *fmt;
3921   int likely = 0;
3922   int dbl = 0;
3923   int coproc = 0;
3924   int lr = 0;
3925   int imm = 0;
3926   offsetT maxnum;
3927   int off;
3928   bfd_reloc_code_real_type r;
3929   char *p;
3930   int hold_mips_optimize;
3931
3932   assert (! mips_opts.mips16);
3933
3934   treg = (ip->insn_opcode >> 16) & 0x1f;
3935   dreg = (ip->insn_opcode >> 11) & 0x1f;
3936   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3937   mask = ip->insn_mo->mask;
3938
3939   expr1.X_op = O_constant;
3940   expr1.X_op_symbol = NULL;
3941   expr1.X_add_symbol = NULL;
3942   expr1.X_add_number = 1;
3943
3944   switch (mask)
3945     {
3946     case M_DABS:
3947       dbl = 1;
3948     case M_ABS:
3949       /* bgez $a0,.+12
3950          move v0,$a0
3951          sub v0,$zero,$a0
3952          */
3953
3954       mips_emit_delays (true);
3955       ++mips_opts.noreorder;
3956       mips_any_noreorder = 1;
3957
3958       expr1.X_add_number = 8;
3959       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3960       if (dreg == sreg)
3961         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3962                      0);
3963       else
3964         move_register (&icnt, dreg, sreg);
3965       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3966                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3967
3968       --mips_opts.noreorder;
3969       return;
3970
3971     case M_ADD_I:
3972       s = "addi";
3973       s2 = "add";
3974       goto do_addi;
3975     case M_ADDU_I:
3976       s = "addiu";
3977       s2 = "addu";
3978       goto do_addi;
3979     case M_DADD_I:
3980       dbl = 1;
3981       s = "daddi";
3982       s2 = "dadd";
3983       goto do_addi;
3984     case M_DADDU_I:
3985       dbl = 1;
3986       s = "daddiu";
3987       s2 = "daddu";
3988     do_addi:
3989       if (imm_expr.X_op == O_constant
3990           && imm_expr.X_add_number >= -0x8000
3991           && imm_expr.X_add_number < 0x8000)
3992         {
3993           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3994                        (int) BFD_RELOC_LO16);
3995           return;
3996         }
3997       load_register (&icnt, AT, &imm_expr, dbl);
3998       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3999                    treg, sreg, AT);
4000       break;
4001
4002     case M_AND_I:
4003       s = "andi";
4004       s2 = "and";
4005       goto do_bit;
4006     case M_OR_I:
4007       s = "ori";
4008       s2 = "or";
4009       goto do_bit;
4010     case M_NOR_I:
4011       s = "";
4012       s2 = "nor";
4013       goto do_bit;
4014     case M_XOR_I:
4015       s = "xori";
4016       s2 = "xor";
4017     do_bit:
4018       if (imm_expr.X_op == O_constant
4019           && imm_expr.X_add_number >= 0
4020           && imm_expr.X_add_number < 0x10000)
4021         {
4022           if (mask != M_NOR_I)
4023             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4024                          sreg, (int) BFD_RELOC_LO16);
4025           else
4026             {
4027               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4028                            treg, sreg, (int) BFD_RELOC_LO16);
4029               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4030                            "d,v,t", treg, treg, 0);
4031             }
4032           return;
4033         }
4034
4035       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4036       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4037                    treg, sreg, AT);
4038       break;
4039
4040     case M_BEQ_I:
4041       s = "beq";
4042       goto beq_i;
4043     case M_BEQL_I:
4044       s = "beql";
4045       likely = 1;
4046       goto beq_i;
4047     case M_BNE_I:
4048       s = "bne";
4049       goto beq_i;
4050     case M_BNEL_I:
4051       s = "bnel";
4052       likely = 1;
4053     beq_i:
4054       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4055         {
4056           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4057                        0);
4058           return;
4059         }
4060       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4061       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4062       break;
4063
4064     case M_BGEL:
4065       likely = 1;
4066     case M_BGE:
4067       if (treg == 0)
4068         {
4069           macro_build ((char *) NULL, &icnt, &offset_expr,
4070                        likely ? "bgezl" : "bgez", "s,p", sreg);
4071           return;
4072         }
4073       if (sreg == 0)
4074         {
4075           macro_build ((char *) NULL, &icnt, &offset_expr,
4076                        likely ? "blezl" : "blez", "s,p", treg);
4077           return;
4078         }
4079       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4080                    AT, sreg, treg);
4081       macro_build ((char *) NULL, &icnt, &offset_expr,
4082                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4083       break;
4084
4085     case M_BGTL_I:
4086       likely = 1;
4087     case M_BGT_I:
4088       /* check for > max integer */
4089       maxnum = 0x7fffffff;
4090       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4091         {
4092           maxnum <<= 16;
4093           maxnum |= 0xffff;
4094           maxnum <<= 16;
4095           maxnum |= 0xffff;
4096         }
4097       if (imm_expr.X_op == O_constant
4098           && imm_expr.X_add_number >= maxnum
4099           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4100         {
4101         do_false:
4102           /* result is always false */
4103           if (! likely)
4104             {
4105               if (warn_nops)
4106                 as_warn (_("Branch %s is always false (nop)"),
4107                          ip->insn_mo->name);
4108               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4109                            "", 0);
4110             }
4111           else
4112             {
4113               if (warn_nops)
4114                 as_warn (_("Branch likely %s is always false"),
4115                          ip->insn_mo->name);
4116               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4117                            "s,t,p", 0, 0);
4118             }
4119           return;
4120         }
4121       if (imm_expr.X_op != O_constant)
4122         as_bad (_("Unsupported large constant"));
4123       imm_expr.X_add_number++;
4124       /* FALLTHROUGH */
4125     case M_BGE_I:
4126     case M_BGEL_I:
4127       if (mask == M_BGEL_I)
4128         likely = 1;
4129       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4130         {
4131           macro_build ((char *) NULL, &icnt, &offset_expr,
4132                        likely ? "bgezl" : "bgez", "s,p", sreg);
4133           return;
4134         }
4135       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4136         {
4137           macro_build ((char *) NULL, &icnt, &offset_expr,
4138                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4139           return;
4140         }
4141       maxnum = 0x7fffffff;
4142       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4143         {
4144           maxnum <<= 16;
4145           maxnum |= 0xffff;
4146           maxnum <<= 16;
4147           maxnum |= 0xffff;
4148         }
4149       maxnum = - maxnum - 1;
4150       if (imm_expr.X_op == O_constant
4151           && imm_expr.X_add_number <= maxnum
4152           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4153         {
4154         do_true:
4155           /* result is always true */
4156           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4157           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4158           return;
4159         }
4160       set_at (&icnt, sreg, 0);
4161       macro_build ((char *) NULL, &icnt, &offset_expr,
4162                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4163       break;
4164
4165     case M_BGEUL:
4166       likely = 1;
4167     case M_BGEU:
4168       if (treg == 0)
4169         goto do_true;
4170       if (sreg == 0)
4171         {
4172           macro_build ((char *) NULL, &icnt, &offset_expr,
4173                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4174           return;
4175         }
4176       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4177                    "d,v,t", AT, sreg, treg);
4178       macro_build ((char *) NULL, &icnt, &offset_expr,
4179                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4180       break;
4181
4182     case M_BGTUL_I:
4183       likely = 1;
4184     case M_BGTU_I:
4185       if (sreg == 0
4186           || (HAVE_32BIT_GPRS
4187               && imm_expr.X_op == O_constant
4188               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4189         goto do_false;
4190       if (imm_expr.X_op != O_constant)
4191         as_bad (_("Unsupported large constant"));
4192       imm_expr.X_add_number++;
4193       /* FALLTHROUGH */
4194     case M_BGEU_I:
4195     case M_BGEUL_I:
4196       if (mask == M_BGEUL_I)
4197         likely = 1;
4198       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4199         goto do_true;
4200       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4201         {
4202           macro_build ((char *) NULL, &icnt, &offset_expr,
4203                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4204           return;
4205         }
4206       set_at (&icnt, sreg, 1);
4207       macro_build ((char *) NULL, &icnt, &offset_expr,
4208                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4209       break;
4210
4211     case M_BGTL:
4212       likely = 1;
4213     case M_BGT:
4214       if (treg == 0)
4215         {
4216           macro_build ((char *) NULL, &icnt, &offset_expr,
4217                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4218           return;
4219         }
4220       if (sreg == 0)
4221         {
4222           macro_build ((char *) NULL, &icnt, &offset_expr,
4223                        likely ? "bltzl" : "bltz", "s,p", treg);
4224           return;
4225         }
4226       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4227                    AT, treg, sreg);
4228       macro_build ((char *) NULL, &icnt, &offset_expr,
4229                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4230       break;
4231
4232     case M_BGTUL:
4233       likely = 1;
4234     case M_BGTU:
4235       if (treg == 0)
4236         {
4237           macro_build ((char *) NULL, &icnt, &offset_expr,
4238                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4239           return;
4240         }
4241       if (sreg == 0)
4242         goto do_false;
4243       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4244                    "d,v,t", AT, treg, sreg);
4245       macro_build ((char *) NULL, &icnt, &offset_expr,
4246                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4247       break;
4248
4249     case M_BLEL:
4250       likely = 1;
4251     case M_BLE:
4252       if (treg == 0)
4253         {
4254           macro_build ((char *) NULL, &icnt, &offset_expr,
4255                        likely ? "blezl" : "blez", "s,p", sreg);
4256           return;
4257         }
4258       if (sreg == 0)
4259         {
4260           macro_build ((char *) NULL, &icnt, &offset_expr,
4261                        likely ? "bgezl" : "bgez", "s,p", treg);
4262           return;
4263         }
4264       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4265                    AT, treg, sreg);
4266       macro_build ((char *) NULL, &icnt, &offset_expr,
4267                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4268       break;
4269
4270     case M_BLEL_I:
4271       likely = 1;
4272     case M_BLE_I:
4273       maxnum = 0x7fffffff;
4274       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4275         {
4276           maxnum <<= 16;
4277           maxnum |= 0xffff;
4278           maxnum <<= 16;
4279           maxnum |= 0xffff;
4280         }
4281       if (imm_expr.X_op == O_constant
4282           && imm_expr.X_add_number >= maxnum
4283           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4284         goto do_true;
4285       if (imm_expr.X_op != O_constant)
4286         as_bad (_("Unsupported large constant"));
4287       imm_expr.X_add_number++;
4288       /* FALLTHROUGH */
4289     case M_BLT_I:
4290     case M_BLTL_I:
4291       if (mask == M_BLTL_I)
4292         likely = 1;
4293       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4294         {
4295           macro_build ((char *) NULL, &icnt, &offset_expr,
4296                        likely ? "bltzl" : "bltz", "s,p", sreg);
4297           return;
4298         }
4299       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4300         {
4301           macro_build ((char *) NULL, &icnt, &offset_expr,
4302                        likely ? "blezl" : "blez", "s,p", sreg);
4303           return;
4304         }
4305       set_at (&icnt, sreg, 0);
4306       macro_build ((char *) NULL, &icnt, &offset_expr,
4307                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4308       break;
4309
4310     case M_BLEUL:
4311       likely = 1;
4312     case M_BLEU:
4313       if (treg == 0)
4314         {
4315           macro_build ((char *) NULL, &icnt, &offset_expr,
4316                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4317           return;
4318         }
4319       if (sreg == 0)
4320         goto do_true;
4321       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4322                    "d,v,t", AT, treg, sreg);
4323       macro_build ((char *) NULL, &icnt, &offset_expr,
4324                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4325       break;
4326
4327     case M_BLEUL_I:
4328       likely = 1;
4329     case M_BLEU_I:
4330       if (sreg == 0
4331           || (HAVE_32BIT_GPRS
4332               && imm_expr.X_op == O_constant
4333               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4334         goto do_true;
4335       if (imm_expr.X_op != O_constant)
4336         as_bad (_("Unsupported large constant"));
4337       imm_expr.X_add_number++;
4338       /* FALLTHROUGH */
4339     case M_BLTU_I:
4340     case M_BLTUL_I:
4341       if (mask == M_BLTUL_I)
4342         likely = 1;
4343       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4344         goto do_false;
4345       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4346         {
4347           macro_build ((char *) NULL, &icnt, &offset_expr,
4348                        likely ? "beql" : "beq",
4349                        "s,t,p", sreg, 0);
4350           return;
4351         }
4352       set_at (&icnt, sreg, 1);
4353       macro_build ((char *) NULL, &icnt, &offset_expr,
4354                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4355       break;
4356
4357     case M_BLTL:
4358       likely = 1;
4359     case M_BLT:
4360       if (treg == 0)
4361         {
4362           macro_build ((char *) NULL, &icnt, &offset_expr,
4363                        likely ? "bltzl" : "bltz", "s,p", sreg);
4364           return;
4365         }
4366       if (sreg == 0)
4367         {
4368           macro_build ((char *) NULL, &icnt, &offset_expr,
4369                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4370           return;
4371         }
4372       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4373                    AT, sreg, treg);
4374       macro_build ((char *) NULL, &icnt, &offset_expr,
4375                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4376       break;
4377
4378     case M_BLTUL:
4379       likely = 1;
4380     case M_BLTU:
4381       if (treg == 0)
4382         goto do_false;
4383       if (sreg == 0)
4384         {
4385           macro_build ((char *) NULL, &icnt, &offset_expr,
4386                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4387           return;
4388         }
4389       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4390                    "d,v,t", AT, sreg,
4391                    treg);
4392       macro_build ((char *) NULL, &icnt, &offset_expr,
4393                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4394       break;
4395
4396     case M_DDIV_3:
4397       dbl = 1;
4398     case M_DIV_3:
4399       s = "mflo";
4400       goto do_div3;
4401     case M_DREM_3:
4402       dbl = 1;
4403     case M_REM_3:
4404       s = "mfhi";
4405     do_div3:
4406       if (treg == 0)
4407         {
4408           as_warn (_("Divide by zero."));
4409           if (mips_trap)
4410             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4411                          "s,t", 0, 0);
4412           else
4413             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4414                          "c", 7);
4415           return;
4416         }
4417
4418       mips_emit_delays (true);
4419       ++mips_opts.noreorder;
4420       mips_any_noreorder = 1;
4421       if (mips_trap)
4422         {
4423           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4424                        "s,t", treg, 0);
4425           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4426                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4427         }
4428       else
4429         {
4430           expr1.X_add_number = 8;
4431           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4432           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4433                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4434           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4435                        "c", 7);
4436         }
4437       expr1.X_add_number = -1;
4438       macro_build ((char *) NULL, &icnt, &expr1,
4439                    dbl ? "daddiu" : "addiu",
4440                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4441       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4442       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4443       if (dbl)
4444         {
4445           expr1.X_add_number = 1;
4446           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4447                        (int) BFD_RELOC_LO16);
4448           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4449                        "d,w,<", AT, AT, 31);
4450         }
4451       else
4452         {
4453           expr1.X_add_number = 0x80000000;
4454           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4455                        (int) BFD_RELOC_HI16);
4456         }
4457       if (mips_trap)
4458         {
4459           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4460                        "s,t", sreg, AT);
4461           /* We want to close the noreorder block as soon as possible, so
4462              that later insns are available for delay slot filling.  */
4463           --mips_opts.noreorder;
4464         }
4465       else
4466         {
4467           expr1.X_add_number = 8;
4468           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4469           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4470                        0);
4471
4472           /* We want to close the noreorder block as soon as possible, so
4473              that later insns are available for delay slot filling.  */
4474           --mips_opts.noreorder;
4475
4476           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4477                        "c", 6);
4478         }
4479       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4480       break;
4481
4482     case M_DIV_3I:
4483       s = "div";
4484       s2 = "mflo";
4485       goto do_divi;
4486     case M_DIVU_3I:
4487       s = "divu";
4488       s2 = "mflo";
4489       goto do_divi;
4490     case M_REM_3I:
4491       s = "div";
4492       s2 = "mfhi";
4493       goto do_divi;
4494     case M_REMU_3I:
4495       s = "divu";
4496       s2 = "mfhi";
4497       goto do_divi;
4498     case M_DDIV_3I:
4499       dbl = 1;
4500       s = "ddiv";
4501       s2 = "mflo";
4502       goto do_divi;
4503     case M_DDIVU_3I:
4504       dbl = 1;
4505       s = "ddivu";
4506       s2 = "mflo";
4507       goto do_divi;
4508     case M_DREM_3I:
4509       dbl = 1;
4510       s = "ddiv";
4511       s2 = "mfhi";
4512       goto do_divi;
4513     case M_DREMU_3I:
4514       dbl = 1;
4515       s = "ddivu";
4516       s2 = "mfhi";
4517     do_divi:
4518       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4519         {
4520           as_warn (_("Divide by zero."));
4521           if (mips_trap)
4522             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4523                          "s,t", 0, 0);
4524           else
4525             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4526                          "c", 7);
4527           return;
4528         }
4529       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4530         {
4531           if (strcmp (s2, "mflo") == 0)
4532             move_register (&icnt, dreg, sreg);
4533           else
4534             move_register (&icnt, dreg, 0);
4535           return;
4536         }
4537       if (imm_expr.X_op == O_constant
4538           && imm_expr.X_add_number == -1
4539           && s[strlen (s) - 1] != 'u')
4540         {
4541           if (strcmp (s2, "mflo") == 0)
4542             {
4543               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4544                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4545             }
4546           else
4547             move_register (&icnt, dreg, 0);
4548           return;
4549         }
4550
4551       load_register (&icnt, AT, &imm_expr, dbl);
4552       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4553                    sreg, AT);
4554       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4555       break;
4556
4557     case M_DIVU_3:
4558       s = "divu";
4559       s2 = "mflo";
4560       goto do_divu3;
4561     case M_REMU_3:
4562       s = "divu";
4563       s2 = "mfhi";
4564       goto do_divu3;
4565     case M_DDIVU_3:
4566       s = "ddivu";
4567       s2 = "mflo";
4568       goto do_divu3;
4569     case M_DREMU_3:
4570       s = "ddivu";
4571       s2 = "mfhi";
4572     do_divu3:
4573       mips_emit_delays (true);
4574       ++mips_opts.noreorder;
4575       mips_any_noreorder = 1;
4576       if (mips_trap)
4577         {
4578           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4579                        "s,t", treg, 0);
4580           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4581                        sreg, treg);
4582           /* We want to close the noreorder block as soon as possible, so
4583              that later insns are available for delay slot filling.  */
4584           --mips_opts.noreorder;
4585         }
4586       else
4587         {
4588           expr1.X_add_number = 8;
4589           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4590           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4591                        sreg, treg);
4592
4593           /* We want to close the noreorder block as soon as possible, so
4594              that later insns are available for delay slot filling.  */
4595           --mips_opts.noreorder;
4596           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4597                        "c", 7);
4598         }
4599       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4600       return;
4601
4602     case M_DLA_AB:
4603       dbl = 1;
4604     case M_LA_AB:
4605       /* Load the address of a symbol into a register.  If breg is not
4606          zero, we then add a base register to it.  */
4607
4608       if (treg == breg)
4609         {
4610           tempreg = AT;
4611           used_at = 1;
4612         }
4613       else
4614         {
4615           tempreg = treg;
4616           used_at = 0;
4617         }
4618
4619       /* When generating embedded PIC code, we permit expressions of
4620          the form
4621            la   $treg,foo-bar
4622            la   $treg,foo-bar($breg)
4623          where bar is an address in the current section.  These are used
4624          when getting the addresses of functions.  We don't permit
4625          X_add_number to be non-zero, because if the symbol is
4626          external the relaxing code needs to know that any addend is
4627          purely the offset to X_op_symbol.  */
4628       if (mips_pic == EMBEDDED_PIC
4629           && offset_expr.X_op == O_subtract
4630           && (symbol_constant_p (offset_expr.X_op_symbol)
4631               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4632               : (symbol_equated_p (offset_expr.X_op_symbol)
4633                  && (S_GET_SEGMENT
4634                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4635                       ->X_add_symbol)
4636                      == now_seg)))
4637           && (offset_expr.X_add_number == 0
4638               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4639         {
4640           if (breg == 0)
4641             {
4642               tempreg = treg;
4643               used_at = 0;
4644               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4645                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4646             }
4647           else
4648             {
4649               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4650                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4651               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4652                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4653                            "d,v,t", tempreg, tempreg, breg);
4654             }
4655           macro_build ((char *) NULL, &icnt, &offset_expr,
4656                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4657                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4658           if (! used_at)
4659             return;
4660           break;
4661         }
4662
4663       if (offset_expr.X_op != O_symbol
4664           && offset_expr.X_op != O_constant)
4665         {
4666           as_bad (_("expression too complex"));
4667           offset_expr.X_op = O_constant;
4668         }
4669
4670       if (offset_expr.X_op == O_constant)
4671         load_register (&icnt, tempreg, &offset_expr,
4672                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4673                         ? (dbl || HAVE_64BIT_ADDRESSES)
4674                         : HAVE_64BIT_ADDRESSES));
4675       else if (mips_pic == NO_PIC)
4676         {
4677           /* If this is a reference to a GP relative symbol, we want
4678                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4679              Otherwise we want
4680                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4681                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4682              If we have a constant, we need two instructions anyhow,
4683              so we may as well always use the latter form.
4684
4685             With 64bit address space and a usable $at we want
4686               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4687               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4688               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4689               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4690               dsll32    $tempreg,0
4691               dadd      $tempreg,$tempreg,$at
4692
4693             If $at is already in use, we use an path which is suboptimal
4694             on superscalar processors.
4695               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4696               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4697               dsll      $tempreg,16
4698               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4699               dsll      $tempreg,16
4700               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4701           */
4702           p = NULL;
4703           if (HAVE_64BIT_ADDRESSES)
4704             {
4705               /* We don't do GP optimization for now because RELAX_ENCODE can't
4706                  hold the data for such large chunks.  */
4707
4708             if (used_at == 0)
4709               {
4710                 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4711                              tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4712                 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4713                              AT, (int) BFD_RELOC_HI16_S);
4714                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4715                              tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4716                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4717                              AT, AT, (int) BFD_RELOC_LO16);
4718                 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4719                              "d,w,<", tempreg, tempreg, 0);
4720                 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4721                              tempreg, tempreg, AT);
4722                 used_at = 1;
4723               }
4724             else
4725               {
4726                 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4727                              tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4728                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4729                              tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4730                 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4731                              tempreg, tempreg, 16);
4732                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4733                              tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4734                 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4735                              tempreg, tempreg, 16);
4736                 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4737                              tempreg, tempreg, (int) BFD_RELOC_LO16);
4738               }
4739           }
4740         else
4741           {
4742             if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4743                 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4744               {
4745                 frag_grow (20);
4746                 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4747                              "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4748                 p = frag_var (rs_machine_dependent, 8, 0,
4749                               RELAX_ENCODE (4, 8, 0, 4, 0,
4750                                             mips_opts.warn_about_macros),
4751                               offset_expr.X_add_symbol, 0, NULL);
4752               }
4753             macro_build_lui (p, &icnt, &offset_expr, tempreg);
4754             if (p != NULL)
4755               p += 4;
4756             macro_build (p, &icnt, &offset_expr, "addiu",
4757                          "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4758           }
4759         }
4760       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4761         {
4762           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4763
4764           /* If this is a reference to an external symbol, and there
4765              is no constant, we want
4766                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4767              or if tempreg is PIC_CALL_REG
4768                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4769              For a local symbol, we want
4770                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4771                nop
4772                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4773
4774              If we have a small constant, and this is a reference to
4775              an external symbol, we want
4776                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4777                nop
4778                addiu    $tempreg,$tempreg,<constant>
4779              For a local symbol, we want the same instruction
4780              sequence, but we output a BFD_RELOC_LO16 reloc on the
4781              addiu instruction.
4782
4783              If we have a large constant, and this is a reference to
4784              an external symbol, we want
4785                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4786                lui      $at,<hiconstant>
4787                addiu    $at,$at,<loconstant>
4788                addu     $tempreg,$tempreg,$at
4789              For a local symbol, we want the same instruction
4790              sequence, but we output a BFD_RELOC_LO16 reloc on the
4791              addiu instruction.  */
4792           expr1.X_add_number = offset_expr.X_add_number;
4793           offset_expr.X_add_number = 0;
4794           frag_grow (32);
4795           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4796             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4797           macro_build ((char *) NULL, &icnt, &offset_expr,
4798                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4799                        "t,o(b)", tempreg, lw_reloc_type, GP);
4800           if (expr1.X_add_number == 0)
4801             {
4802               int off;
4803
4804               if (breg == 0)
4805                 off = 0;
4806               else
4807                 {
4808                   /* We're going to put in an addu instruction using
4809                      tempreg, so we may as well insert the nop right
4810                      now.  */
4811                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4812                                "nop", "");
4813                   off = 4;
4814                 }
4815               p = frag_var (rs_machine_dependent, 8 - off, 0,
4816                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4817                                           (breg == 0
4818                                            ? mips_opts.warn_about_macros
4819                                            : 0)),
4820                             offset_expr.X_add_symbol, 0, NULL);
4821               if (breg == 0)
4822                 {
4823                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4824                   p += 4;
4825                 }
4826               macro_build (p, &icnt, &expr1,
4827                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4828                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4829               /* FIXME: If breg == 0, and the next instruction uses
4830                  $tempreg, then if this variant case is used an extra
4831                  nop will be generated.  */
4832             }
4833           else if (expr1.X_add_number >= -0x8000
4834                    && expr1.X_add_number < 0x8000)
4835             {
4836               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4837                            "nop", "");
4838               macro_build ((char *) NULL, &icnt, &expr1,
4839                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4840                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4841               frag_var (rs_machine_dependent, 0, 0,
4842                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4843                         offset_expr.X_add_symbol, 0, NULL);
4844             }
4845           else
4846             {
4847               int off1;
4848
4849               /* If we are going to add in a base register, and the
4850                  target register and the base register are the same,
4851                  then we are using AT as a temporary register.  Since
4852                  we want to load the constant into AT, we add our
4853                  current AT (from the global offset table) and the
4854                  register into the register now, and pretend we were
4855                  not using a base register.  */
4856               if (breg != treg)
4857                 off1 = 0;
4858               else
4859                 {
4860                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4861                                "nop", "");
4862                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4863                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4864                                "d,v,t", treg, AT, breg);
4865                   breg = 0;
4866                   tempreg = treg;
4867                   off1 = -8;
4868                 }
4869
4870               /* Set mips_optimize around the lui instruction to avoid
4871                  inserting an unnecessary nop after the lw.  */
4872               hold_mips_optimize = mips_optimize;
4873               mips_optimize = 2;
4874               macro_build_lui (NULL, &icnt, &expr1, AT);
4875               mips_optimize = hold_mips_optimize;
4876
4877               macro_build ((char *) NULL, &icnt, &expr1,
4878                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4879                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4880               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4881                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4882                            "d,v,t", tempreg, tempreg, AT);
4883               frag_var (rs_machine_dependent, 0, 0,
4884                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4885                         offset_expr.X_add_symbol, 0, NULL);
4886               used_at = 1;
4887             }
4888         }
4889       else if (mips_pic == SVR4_PIC)
4890         {
4891           int gpdel;
4892           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4893           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4894
4895           /* This is the large GOT case.  If this is a reference to an
4896              external symbol, and there is no constant, we want
4897                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4898                addu     $tempreg,$tempreg,$gp
4899                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4900              or if tempreg is PIC_CALL_REG
4901                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
4902                addu     $tempreg,$tempreg,$gp
4903                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4904              For a local symbol, we want
4905                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4906                nop
4907                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4908
4909              If we have a small constant, and this is a reference to
4910              an external symbol, we want
4911                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4912                addu     $tempreg,$tempreg,$gp
4913                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4914                nop
4915                addiu    $tempreg,$tempreg,<constant>
4916              For a local symbol, we want
4917                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4918                nop
4919                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4920
4921              If we have a large constant, and this is a reference to
4922              an external symbol, we want
4923                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4924                addu     $tempreg,$tempreg,$gp
4925                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4926                lui      $at,<hiconstant>
4927                addiu    $at,$at,<loconstant>
4928                addu     $tempreg,$tempreg,$at
4929              For a local symbol, we want
4930                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4931                lui      $at,<hiconstant>
4932                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4933                addu     $tempreg,$tempreg,$at
4934              */
4935           expr1.X_add_number = offset_expr.X_add_number;
4936           offset_expr.X_add_number = 0;
4937           frag_grow (52);
4938           if (reg_needs_delay (GP))
4939             gpdel = 4;
4940           else
4941             gpdel = 0;
4942           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4943             {
4944               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4945               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4946             }
4947           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4948                        tempreg, lui_reloc_type);
4949           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4950                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4951                        "d,v,t", tempreg, tempreg, GP);
4952           macro_build ((char *) NULL, &icnt, &offset_expr,
4953                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4954                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
4955           if (expr1.X_add_number == 0)
4956             {
4957               int off;
4958
4959               if (breg == 0)
4960                 off = 0;
4961               else
4962                 {
4963                   /* We're going to put in an addu instruction using
4964                      tempreg, so we may as well insert the nop right
4965                      now.  */
4966                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4967                                "nop", "");
4968                   off = 4;
4969                 }
4970
4971               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4972                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4973                                           8 + gpdel, 0,
4974                                           (breg == 0
4975                                            ? mips_opts.warn_about_macros
4976                                            : 0)),
4977                             offset_expr.X_add_symbol, 0, NULL);
4978             }
4979           else if (expr1.X_add_number >= -0x8000
4980                    && expr1.X_add_number < 0x8000)
4981             {
4982               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4983                            "nop", "");
4984               macro_build ((char *) NULL, &icnt, &expr1,
4985                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4986                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4987
4988               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4989                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4990                                           (breg == 0
4991                                            ? mips_opts.warn_about_macros
4992                                            : 0)),
4993                             offset_expr.X_add_symbol, 0, NULL);
4994             }
4995           else
4996             {
4997               int adj, dreg;
4998
4999               /* If we are going to add in a base register, and the
5000                  target register and the base register are the same,
5001                  then we are using AT as a temporary register.  Since
5002                  we want to load the constant into AT, we add our
5003                  current AT (from the global offset table) and the
5004                  register into the register now, and pretend we were
5005                  not using a base register.  */
5006               if (breg != treg)
5007                 {
5008                   adj = 0;
5009                   dreg = tempreg;
5010                 }
5011               else
5012                 {
5013                   assert (tempreg == AT);
5014                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5015                                "nop", "");
5016                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5017                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5018                                "d,v,t", treg, AT, breg);
5019                   dreg = treg;
5020                   adj = 8;
5021                 }
5022
5023               /* Set mips_optimize around the lui instruction to avoid
5024                  inserting an unnecessary nop after the lw.  */
5025               hold_mips_optimize = mips_optimize;
5026               mips_optimize = 2;
5027               macro_build_lui (NULL, &icnt, &expr1, AT);
5028               mips_optimize = hold_mips_optimize;
5029
5030               macro_build ((char *) NULL, &icnt, &expr1,
5031                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5032                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5033               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5034                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5035                            "d,v,t", dreg, dreg, AT);
5036
5037               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5038                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5039                                           8 + gpdel, 0,
5040                                           (breg == 0
5041                                            ? mips_opts.warn_about_macros
5042                                            : 0)),
5043                             offset_expr.X_add_symbol, 0, NULL);
5044
5045               used_at = 1;
5046             }
5047
5048           if (gpdel > 0)
5049             {
5050               /* This is needed because this instruction uses $gp, but
5051                  the first instruction on the main stream does not.  */
5052               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5053               p += 4;
5054             }
5055           macro_build (p, &icnt, &offset_expr,
5056                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5057                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5058           p += 4;
5059           if (expr1.X_add_number >= -0x8000
5060               && expr1.X_add_number < 0x8000)
5061             {
5062               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5063               p += 4;
5064               macro_build (p, &icnt, &expr1,
5065                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5066                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5067               /* FIXME: If add_number is 0, and there was no base
5068                  register, the external symbol case ended with a load,
5069                  so if the symbol turns out to not be external, and
5070                  the next instruction uses tempreg, an unnecessary nop
5071                  will be inserted.  */
5072             }
5073           else
5074             {
5075               if (breg == treg)
5076                 {
5077                   /* We must add in the base register now, as in the
5078                      external symbol case.  */
5079                   assert (tempreg == AT);
5080                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5081                   p += 4;
5082                   macro_build (p, &icnt, (expressionS *) NULL,
5083                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5084                                "d,v,t", treg, AT, breg);
5085                   p += 4;
5086                   tempreg = treg;
5087                   /* We set breg to 0 because we have arranged to add
5088                      it in in both cases.  */
5089                   breg = 0;
5090                 }
5091
5092               macro_build_lui (p, &icnt, &expr1, AT);
5093               p += 4;
5094               macro_build (p, &icnt, &expr1,
5095                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5096                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5097               p += 4;
5098               macro_build (p, &icnt, (expressionS *) NULL,
5099                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5100                            "d,v,t", tempreg, tempreg, AT);
5101               p += 4;
5102             }
5103         }
5104       else if (mips_pic == EMBEDDED_PIC)
5105         {
5106           /* We use
5107                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5108              */
5109           macro_build ((char *) NULL, &icnt, &offset_expr,
5110                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5111                        "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
5112         }
5113       else
5114         abort ();
5115
5116       if (breg != 0)
5117         {
5118           char *s;
5119
5120           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5121             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5122           else
5123             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5124
5125           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5126                        "d,v,t", treg, tempreg, breg);
5127         }
5128
5129       if (! used_at)
5130         return;
5131
5132       break;
5133
5134     case M_J_A:
5135       /* The j instruction may not be used in PIC code, since it
5136          requires an absolute address.  We convert it to a b
5137          instruction.  */
5138       if (mips_pic == NO_PIC)
5139         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5140       else
5141         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5142       return;
5143
5144       /* The jal instructions must be handled as macros because when
5145          generating PIC code they expand to multi-instruction
5146          sequences.  Normally they are simple instructions.  */
5147     case M_JAL_1:
5148       dreg = RA;
5149       /* Fall through.  */
5150     case M_JAL_2:
5151       if (mips_pic == NO_PIC
5152           || mips_pic == EMBEDDED_PIC)
5153         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5154                      "d,s", dreg, sreg);
5155       else if (mips_pic == SVR4_PIC)
5156         {
5157           if (sreg != PIC_CALL_REG)
5158             as_warn (_("MIPS PIC call to register other than $25"));
5159
5160           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5161                        "d,s", dreg, sreg);
5162           if (! HAVE_NEWABI)
5163             {
5164               if (mips_cprestore_offset < 0)
5165                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5166               else
5167                 {
5168                   if (! mips_frame_reg_valid)
5169                     {
5170                       as_warn (_("No .frame pseudo-op used in PIC code"));
5171                       /* Quiet this warning.  */
5172                       mips_frame_reg_valid = 1;
5173                     }
5174                   if (! mips_cprestore_valid)
5175                     {
5176                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5177                       /* Quiet this warning.  */
5178                       mips_cprestore_valid = 1;
5179                     }
5180                   expr1.X_add_number = mips_cprestore_offset;
5181                   macro_build ((char *) NULL, &icnt, &expr1,
5182                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5183                                GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5184                 }
5185             }
5186         }
5187       else
5188         abort ();
5189
5190       return;
5191
5192     case M_JAL_A:
5193       if (mips_pic == NO_PIC)
5194         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5195       else if (mips_pic == SVR4_PIC)
5196         {
5197           /* If this is a reference to an external symbol, and we are
5198              using a small GOT, we want
5199                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5200                nop
5201                jalr     $25
5202                nop
5203                lw       $gp,cprestore($sp)
5204              The cprestore value is set using the .cprestore
5205              pseudo-op.  If we are using a big GOT, we want
5206                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5207                addu     $25,$25,$gp
5208                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5209                nop
5210                jalr     $25
5211                nop
5212                lw       $gp,cprestore($sp)
5213              If the symbol is not external, we want
5214                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5215                nop
5216                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5217                jalr     $25
5218                nop
5219                lw $gp,cprestore($sp) */
5220           frag_grow (40);
5221           if (! mips_big_got)
5222             {
5223               macro_build ((char *) NULL, &icnt, &offset_expr,
5224                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5225                            "t,o(b)", PIC_CALL_REG,
5226                            (int) BFD_RELOC_MIPS_CALL16, GP);
5227               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5228                            "nop", "");
5229               p = frag_var (rs_machine_dependent, 4, 0,
5230                             RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5231                             offset_expr.X_add_symbol, 0, NULL);
5232             }
5233           else
5234             {
5235               int gpdel;
5236
5237               if (reg_needs_delay (GP))
5238                 gpdel = 4;
5239               else
5240                 gpdel = 0;
5241               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5242                            PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5243               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5244                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5245                            "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5246               macro_build ((char *) NULL, &icnt, &offset_expr,
5247                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5248                            "t,o(b)", PIC_CALL_REG,
5249                            (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5250               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5251                            "nop", "");
5252               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5253                             RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5254                                           0, 0),
5255                             offset_expr.X_add_symbol, 0, NULL);
5256               if (gpdel > 0)
5257                 {
5258                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5259                   p += 4;
5260                 }
5261               macro_build (p, &icnt, &offset_expr,
5262                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5263                            "t,o(b)", PIC_CALL_REG,
5264                            (int) BFD_RELOC_MIPS_GOT16, GP);
5265               p += 4;
5266               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5267               p += 4;
5268             }
5269           macro_build (p, &icnt, &offset_expr,
5270                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5271                        "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5272                        (int) BFD_RELOC_LO16);
5273           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5274                        "jalr", "s", PIC_CALL_REG);
5275           if (! HAVE_NEWABI)
5276             {
5277               if (mips_cprestore_offset < 0)
5278                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5279               else
5280                 {
5281                   if (! mips_frame_reg_valid)
5282                     {
5283                       as_warn (_("No .frame pseudo-op used in PIC code"));
5284                       /* Quiet this warning.  */
5285                       mips_frame_reg_valid = 1;
5286                     }
5287                   if (! mips_cprestore_valid)
5288                     {
5289                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5290                       /* Quiet this warning.  */
5291                       mips_cprestore_valid = 1;
5292                     }
5293                   if (mips_opts.noreorder)
5294                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5295                              "nop", "");
5296                   expr1.X_add_number = mips_cprestore_offset;
5297                   macro_build ((char *) NULL, &icnt, &expr1,
5298                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5299                                GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5300                 }
5301             }
5302         }
5303       else if (mips_pic == EMBEDDED_PIC)
5304         {
5305           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5306           /* The linker may expand the call to a longer sequence which
5307              uses $at, so we must break rather than return.  */
5308           break;
5309         }
5310       else
5311         abort ();
5312
5313       return;
5314
5315     case M_LB_AB:
5316       s = "lb";
5317       goto ld;
5318     case M_LBU_AB:
5319       s = "lbu";
5320       goto ld;
5321     case M_LH_AB:
5322       s = "lh";
5323       goto ld;
5324     case M_LHU_AB:
5325       s = "lhu";
5326       goto ld;
5327     case M_LW_AB:
5328       s = "lw";
5329       goto ld;
5330     case M_LWC0_AB:
5331       s = "lwc0";
5332       /* Itbl support may require additional care here.  */
5333       coproc = 1;
5334       goto ld;
5335     case M_LWC1_AB:
5336       s = "lwc1";
5337       /* Itbl support may require additional care here.  */
5338       coproc = 1;
5339       goto ld;
5340     case M_LWC2_AB:
5341       s = "lwc2";
5342       /* Itbl support may require additional care here.  */
5343       coproc = 1;
5344       goto ld;
5345     case M_LWC3_AB:
5346       s = "lwc3";
5347       /* Itbl support may require additional care here.  */
5348       coproc = 1;
5349       goto ld;
5350     case M_LWL_AB:
5351       s = "lwl";
5352       lr = 1;
5353       goto ld;
5354     case M_LWR_AB:
5355       s = "lwr";
5356       lr = 1;
5357       goto ld;
5358     case M_LDC1_AB:
5359       if (mips_arch == CPU_R4650)
5360         {
5361           as_bad (_("opcode not supported on this processor"));
5362           return;
5363         }
5364       s = "ldc1";
5365       /* Itbl support may require additional care here.  */
5366       coproc = 1;
5367       goto ld;
5368     case M_LDC2_AB:
5369       s = "ldc2";
5370       /* Itbl support may require additional care here.  */
5371       coproc = 1;
5372       goto ld;
5373     case M_LDC3_AB:
5374       s = "ldc3";
5375       /* Itbl support may require additional care here.  */
5376       coproc = 1;
5377       goto ld;
5378     case M_LDL_AB:
5379       s = "ldl";
5380       lr = 1;
5381       goto ld;
5382     case M_LDR_AB:
5383       s = "ldr";
5384       lr = 1;
5385       goto ld;
5386     case M_LL_AB:
5387       s = "ll";
5388       goto ld;
5389     case M_LLD_AB:
5390       s = "lld";
5391       goto ld;
5392     case M_LWU_AB:
5393       s = "lwu";
5394     ld:
5395       if (breg == treg || coproc || lr)
5396         {
5397           tempreg = AT;
5398           used_at = 1;
5399         }
5400       else
5401         {
5402           tempreg = treg;
5403           used_at = 0;
5404         }
5405       goto ld_st;
5406     case M_SB_AB:
5407       s = "sb";
5408       goto st;
5409     case M_SH_AB:
5410       s = "sh";
5411       goto st;
5412     case M_SW_AB:
5413       s = "sw";
5414       goto st;
5415     case M_SWC0_AB:
5416       s = "swc0";
5417       /* Itbl support may require additional care here.  */
5418       coproc = 1;
5419       goto st;
5420     case M_SWC1_AB:
5421       s = "swc1";
5422       /* Itbl support may require additional care here.  */
5423       coproc = 1;
5424       goto st;
5425     case M_SWC2_AB:
5426       s = "swc2";
5427       /* Itbl support may require additional care here.  */
5428       coproc = 1;
5429       goto st;
5430     case M_SWC3_AB:
5431       s = "swc3";
5432       /* Itbl support may require additional care here.  */
5433       coproc = 1;
5434       goto st;
5435     case M_SWL_AB:
5436       s = "swl";
5437       goto st;
5438     case M_SWR_AB:
5439       s = "swr";
5440       goto st;
5441     case M_SC_AB:
5442       s = "sc";
5443       goto st;
5444     case M_SCD_AB:
5445       s = "scd";
5446       goto st;
5447     case M_SDC1_AB:
5448       if (mips_arch == CPU_R4650)
5449         {
5450           as_bad (_("opcode not supported on this processor"));
5451           return;
5452         }
5453       s = "sdc1";
5454       coproc = 1;
5455       /* Itbl support may require additional care here.  */
5456       goto st;
5457     case M_SDC2_AB:
5458       s = "sdc2";
5459       /* Itbl support may require additional care here.  */
5460       coproc = 1;
5461       goto st;
5462     case M_SDC3_AB:
5463       s = "sdc3";
5464       /* Itbl support may require additional care here.  */
5465       coproc = 1;
5466       goto st;
5467     case M_SDL_AB:
5468       s = "sdl";
5469       goto st;
5470     case M_SDR_AB:
5471       s = "sdr";
5472     st:
5473       tempreg = AT;
5474       used_at = 1;
5475     ld_st:
5476       /* Itbl support may require additional care here.  */
5477       if (mask == M_LWC1_AB
5478           || mask == M_SWC1_AB
5479           || mask == M_LDC1_AB
5480           || mask == M_SDC1_AB
5481           || mask == M_L_DAB
5482           || mask == M_S_DAB)
5483         fmt = "T,o(b)";
5484       else if (coproc)
5485         fmt = "E,o(b)";
5486       else
5487         fmt = "t,o(b)";
5488
5489       /* For embedded PIC, we allow loads where the offset is calculated
5490          by subtracting a symbol in the current segment from an unknown
5491          symbol, relative to a base register, e.g.:
5492                 <op>    $treg, <sym>-<localsym>($breg)
5493          This is used by the compiler for switch statements.  */
5494       if (mips_pic == EMBEDDED_PIC
5495           && offset_expr.X_op == O_subtract
5496           && (symbol_constant_p (offset_expr.X_op_symbol)
5497               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5498               : (symbol_equated_p (offset_expr.X_op_symbol)
5499                  && (S_GET_SEGMENT
5500                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5501                       ->X_add_symbol)
5502                      == now_seg)))
5503           && breg != 0
5504           && (offset_expr.X_add_number == 0
5505               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5506         {
5507           /* For this case, we output the instructions:
5508                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5509                 addiu   $tempreg,$tempreg,$breg
5510                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5511              If the relocation would fit entirely in 16 bits, it would be
5512              nice to emit:
5513                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5514              instead, but that seems quite difficult.  */
5515           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5516                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5517           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5518                        ((bfd_arch_bits_per_address (stdoutput) == 32
5519                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5520                         ? "addu" : "daddu"),
5521                        "d,v,t", tempreg, tempreg, breg);
5522           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5523                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5524           if (! used_at)
5525             return;
5526           break;
5527         }
5528
5529       if (offset_expr.X_op != O_constant
5530           && offset_expr.X_op != O_symbol)
5531         {
5532           as_bad (_("expression too complex"));
5533           offset_expr.X_op = O_constant;
5534         }
5535
5536       /* A constant expression in PIC code can be handled just as it
5537          is in non PIC code.  */
5538       if (mips_pic == NO_PIC
5539           || offset_expr.X_op == O_constant)
5540         {
5541           /* If this is a reference to a GP relative symbol, and there
5542              is no base register, we want
5543                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5544              Otherwise, if there is no base register, we want
5545                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5546                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5547              If we have a constant, we need two instructions anyhow,
5548              so we always use the latter form.
5549
5550              If we have a base register, and this is a reference to a
5551              GP relative symbol, we want
5552                addu     $tempreg,$breg,$gp
5553                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5554              Otherwise we want
5555                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5556                addu     $tempreg,$tempreg,$breg
5557                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5558              With a constant we always use the latter case.
5559
5560              With 64bit address space and no base register and $at usable,
5561              we want
5562                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5563                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5564                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5565                dsll32   $tempreg,0
5566                daddu    $tempreg,$at
5567                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5568              If we have a base register, we want
5569                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5570                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5571                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5572                daddu    $at,$breg
5573                dsll32   $tempreg,0
5574                daddu    $tempreg,$at
5575                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5576
5577              Without $at we can't generate the optimal path for superscalar
5578              processors here since this would require two temporary registers.
5579                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5580                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5581                dsll     $tempreg,16
5582                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5583                dsll     $tempreg,16
5584                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5585              If we have a base register, we want
5586                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5587                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5588                dsll     $tempreg,16
5589                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5590                dsll     $tempreg,16
5591                daddu    $tempreg,$tempreg,$breg
5592                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5593
5594              If we have 64-bit addresses, as an optimization, for
5595              addresses which are 32-bit constants (e.g. kseg0/kseg1
5596              addresses) we fall back to the 32-bit address generation
5597              mechanism since it is more efficient.  This code should
5598              probably attempt to generate 64-bit constants more
5599              efficiently in general.
5600            */
5601           if (HAVE_64BIT_ADDRESSES
5602               && !(offset_expr.X_op == O_constant
5603                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5604             {
5605               p = NULL;
5606
5607               /* We don't do GP optimization for now because RELAX_ENCODE can't
5608                  hold the data for such large chunks.  */
5609
5610               if (used_at == 0)
5611                 {
5612                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5613                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5614                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5615                                AT, (int) BFD_RELOC_HI16_S);
5616                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5617                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5618                   if (breg != 0)
5619                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5620                                  "d,v,t", AT, AT, breg);
5621                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5622                                "d,w,<", tempreg, tempreg, 0);
5623                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5624                                "d,v,t", tempreg, tempreg, AT);
5625                   macro_build (p, &icnt, &offset_expr, s,
5626                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5627                   used_at = 1;
5628                 }
5629               else
5630                 {
5631                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5632                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5633                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5634                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5635                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5636                                "d,w,<", tempreg, tempreg, 16);
5637                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5638                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5639                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5640                                "d,w,<", tempreg, tempreg, 16);
5641                   if (breg != 0)
5642                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5643                                  "d,v,t", tempreg, tempreg, breg);
5644                   macro_build (p, &icnt, &offset_expr, s,
5645                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5646                 }
5647
5648               return;
5649             }
5650
5651           if (breg == 0)
5652             {
5653               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5654                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5655                 p = NULL;
5656               else
5657                 {
5658                   frag_grow (20);
5659                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5660                                treg, (int) BFD_RELOC_GPREL16, GP);
5661                   p = frag_var (rs_machine_dependent, 8, 0,
5662                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5663                                               (mips_opts.warn_about_macros
5664                                                || (used_at
5665                                                    && mips_opts.noat))),
5666                                 offset_expr.X_add_symbol, 0, NULL);
5667                   used_at = 0;
5668                 }
5669               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5670               if (p != NULL)
5671                 p += 4;
5672               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5673                            (int) BFD_RELOC_LO16, tempreg);
5674             }
5675           else
5676             {
5677               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5678                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5679                 p = NULL;
5680               else
5681                 {
5682                   frag_grow (28);
5683                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5684                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5685                                "d,v,t", tempreg, breg, GP);
5686                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5687                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5688                   p = frag_var (rs_machine_dependent, 12, 0,
5689                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5690                                 offset_expr.X_add_symbol, 0, NULL);
5691                 }
5692               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5693               if (p != NULL)
5694                 p += 4;
5695               macro_build (p, &icnt, (expressionS *) NULL,
5696                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5697                            "d,v,t", tempreg, tempreg, breg);
5698               if (p != NULL)
5699                 p += 4;
5700               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5701                            (int) BFD_RELOC_LO16, tempreg);
5702             }
5703         }
5704       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5705         {
5706           /* If this is a reference to an external symbol, we want
5707                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5708                nop
5709                <op>     $treg,0($tempreg)
5710              Otherwise we want
5711                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5712                nop
5713                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5714                <op>     $treg,0($tempreg)
5715              If there is a base register, we add it to $tempreg before
5716              the <op>.  If there is a constant, we stick it in the
5717              <op> instruction.  We don't handle constants larger than
5718              16 bits, because we have no way to load the upper 16 bits
5719              (actually, we could handle them for the subset of cases
5720              in which we are not using $at).  */
5721           assert (offset_expr.X_op == O_symbol);
5722           expr1.X_add_number = offset_expr.X_add_number;
5723           offset_expr.X_add_number = 0;
5724           if (expr1.X_add_number < -0x8000
5725               || expr1.X_add_number >= 0x8000)
5726             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5727           frag_grow (20);
5728           macro_build ((char *) NULL, &icnt, &offset_expr,
5729                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5730                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5731           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5732           p = frag_var (rs_machine_dependent, 4, 0,
5733                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5734                         offset_expr.X_add_symbol, 0, NULL);
5735           macro_build (p, &icnt, &offset_expr,
5736                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5737                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5738           if (breg != 0)
5739             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5740                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5741                          "d,v,t", tempreg, tempreg, breg);
5742           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5743                        (int) BFD_RELOC_LO16, tempreg);
5744         }
5745       else if (mips_pic == SVR4_PIC)
5746         {
5747           int gpdel;
5748
5749           /* If this is a reference to an external symbol, we want
5750                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5751                addu     $tempreg,$tempreg,$gp
5752                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5753                <op>     $treg,0($tempreg)
5754              Otherwise we want
5755                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5756                nop
5757                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5758                <op>     $treg,0($tempreg)
5759              If there is a base register, we add it to $tempreg before
5760              the <op>.  If there is a constant, we stick it in the
5761              <op> instruction.  We don't handle constants larger than
5762              16 bits, because we have no way to load the upper 16 bits
5763              (actually, we could handle them for the subset of cases
5764              in which we are not using $at).  */
5765           assert (offset_expr.X_op == O_symbol);
5766           expr1.X_add_number = offset_expr.X_add_number;
5767           offset_expr.X_add_number = 0;
5768           if (expr1.X_add_number < -0x8000
5769               || expr1.X_add_number >= 0x8000)
5770             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5771           if (reg_needs_delay (GP))
5772             gpdel = 4;
5773           else
5774             gpdel = 0;
5775           frag_grow (36);
5776           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5777                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5778           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5779                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5780                        "d,v,t", tempreg, tempreg, GP);
5781           macro_build ((char *) NULL, &icnt, &offset_expr,
5782                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5783                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5784                        tempreg);
5785           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5786                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5787                         offset_expr.X_add_symbol, 0, NULL);
5788           if (gpdel > 0)
5789             {
5790               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5791               p += 4;
5792             }
5793           macro_build (p, &icnt, &offset_expr,
5794                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5795                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5796           p += 4;
5797           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5798           p += 4;
5799           macro_build (p, &icnt, &offset_expr,
5800                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5801                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5802           if (breg != 0)
5803             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5804                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5805                          "d,v,t", tempreg, tempreg, breg);
5806           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5807                        (int) BFD_RELOC_LO16, tempreg);
5808         }
5809       else if (mips_pic == EMBEDDED_PIC)
5810         {
5811           /* If there is no base register, we want
5812                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5813              If there is a base register, we want
5814                addu     $tempreg,$breg,$gp
5815                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5816              */
5817           assert (offset_expr.X_op == O_symbol);
5818           if (breg == 0)
5819             {
5820               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5821                            treg, (int) BFD_RELOC_GPREL16, GP);
5822               used_at = 0;
5823             }
5824           else
5825             {
5826               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5827                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5828                            "d,v,t", tempreg, breg, GP);
5829               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5830                            treg, (int) BFD_RELOC_GPREL16, tempreg);
5831             }
5832         }
5833       else
5834         abort ();
5835
5836       if (! used_at)
5837         return;
5838
5839       break;
5840
5841     case M_LI:
5842     case M_LI_S:
5843       load_register (&icnt, treg, &imm_expr, 0);
5844       return;
5845
5846     case M_DLI:
5847       load_register (&icnt, treg, &imm_expr, 1);
5848       return;
5849
5850     case M_LI_SS:
5851       if (imm_expr.X_op == O_constant)
5852         {
5853           load_register (&icnt, AT, &imm_expr, 0);
5854           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5855                        "mtc1", "t,G", AT, treg);
5856           break;
5857         }
5858       else
5859         {
5860           assert (offset_expr.X_op == O_symbol
5861                   && strcmp (segment_name (S_GET_SEGMENT
5862                                            (offset_expr.X_add_symbol)),
5863                              ".lit4") == 0
5864                   && offset_expr.X_add_number == 0);
5865           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5866                        treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5867           return;
5868         }
5869
5870     case M_LI_D:
5871       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
5872          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
5873          order 32 bits of the value and the low order 32 bits are either
5874          zero or in OFFSET_EXPR.  */
5875       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5876         {
5877           if (HAVE_64BIT_GPRS)
5878             load_register (&icnt, treg, &imm_expr, 1);
5879           else
5880             {
5881               int hreg, lreg;
5882
5883               if (target_big_endian)
5884                 {
5885                   hreg = treg;
5886                   lreg = treg + 1;
5887                 }
5888               else
5889                 {
5890                   hreg = treg + 1;
5891                   lreg = treg;
5892                 }
5893
5894               if (hreg <= 31)
5895                 load_register (&icnt, hreg, &imm_expr, 0);
5896               if (lreg <= 31)
5897                 {
5898                   if (offset_expr.X_op == O_absent)
5899                     move_register (&icnt, lreg, 0);
5900                   else
5901                     {
5902                       assert (offset_expr.X_op == O_constant);
5903                       load_register (&icnt, lreg, &offset_expr, 0);
5904                     }
5905                 }
5906             }
5907           return;
5908         }
5909
5910       /* We know that sym is in the .rdata section.  First we get the
5911          upper 16 bits of the address.  */
5912       if (mips_pic == NO_PIC)
5913         {
5914           macro_build_lui (NULL, &icnt, &offset_expr, AT);
5915         }
5916       else if (mips_pic == SVR4_PIC)
5917         {
5918           macro_build ((char *) NULL, &icnt, &offset_expr,
5919                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5920                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5921         }
5922       else if (mips_pic == EMBEDDED_PIC)
5923         {
5924           /* For embedded PIC we pick up the entire address off $gp in
5925              a single instruction.  */
5926           macro_build ((char *) NULL, &icnt, &offset_expr,
5927                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5928                        "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5929           offset_expr.X_op = O_constant;
5930           offset_expr.X_add_number = 0;
5931         }
5932       else
5933         abort ();
5934
5935       /* Now we load the register(s).  */
5936       if (HAVE_64BIT_GPRS)
5937         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5938                      treg, (int) BFD_RELOC_LO16, AT);
5939       else
5940         {
5941           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5942                        treg, (int) BFD_RELOC_LO16, AT);
5943           if (treg != 31)
5944             {
5945               /* FIXME: How in the world do we deal with the possible
5946                  overflow here?  */
5947               offset_expr.X_add_number += 4;
5948               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5949                            treg + 1, (int) BFD_RELOC_LO16, AT);
5950             }
5951         }
5952
5953       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5954          does not become a variant frag.  */
5955       frag_wane (frag_now);
5956       frag_new (0);
5957
5958       break;
5959
5960     case M_LI_DD:
5961       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
5962          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5963          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
5964          the value and the low order 32 bits are either zero or in
5965          OFFSET_EXPR.  */
5966       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5967         {
5968           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5969           if (HAVE_64BIT_FPRS)
5970             {
5971               assert (HAVE_64BIT_GPRS);
5972               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5973                            "dmtc1", "t,S", AT, treg);
5974             }
5975           else
5976             {
5977               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5978                            "mtc1", "t,G", AT, treg + 1);
5979               if (offset_expr.X_op == O_absent)
5980                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5981                              "mtc1", "t,G", 0, treg);
5982               else
5983                 {
5984                   assert (offset_expr.X_op == O_constant);
5985                   load_register (&icnt, AT, &offset_expr, 0);
5986                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5987                                "mtc1", "t,G", AT, treg);
5988                 }
5989             }
5990           break;
5991         }
5992
5993       assert (offset_expr.X_op == O_symbol
5994               && offset_expr.X_add_number == 0);
5995       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5996       if (strcmp (s, ".lit8") == 0)
5997         {
5998           if (mips_opts.isa != ISA_MIPS1)
5999             {
6000               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6001                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
6002               return;
6003             }
6004           breg = GP;
6005           r = BFD_RELOC_MIPS_LITERAL;
6006           goto dob;
6007         }
6008       else
6009         {
6010           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6011           if (mips_pic == SVR4_PIC)
6012             macro_build ((char *) NULL, &icnt, &offset_expr,
6013                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6014                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6015           else
6016             {
6017               /* FIXME: This won't work for a 64 bit address.  */
6018               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6019             }
6020
6021           if (mips_opts.isa != ISA_MIPS1)
6022             {
6023               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6024                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6025
6026               /* To avoid confusion in tc_gen_reloc, we must ensure
6027                  that this does not become a variant frag.  */
6028               frag_wane (frag_now);
6029               frag_new (0);
6030
6031               break;
6032             }
6033           breg = AT;
6034           r = BFD_RELOC_LO16;
6035           goto dob;
6036         }
6037
6038     case M_L_DOB:
6039       if (mips_arch == CPU_R4650)
6040         {
6041           as_bad (_("opcode not supported on this processor"));
6042           return;
6043         }
6044       /* Even on a big endian machine $fn comes before $fn+1.  We have
6045          to adjust when loading from memory.  */
6046       r = BFD_RELOC_LO16;
6047     dob:
6048       assert (mips_opts.isa == ISA_MIPS1);
6049       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6050                    target_big_endian ? treg + 1 : treg,
6051                    (int) r, breg);
6052       /* FIXME: A possible overflow which I don't know how to deal
6053          with.  */
6054       offset_expr.X_add_number += 4;
6055       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6056                    target_big_endian ? treg : treg + 1,
6057                    (int) r, breg);
6058
6059       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6060          does not become a variant frag.  */
6061       frag_wane (frag_now);
6062       frag_new (0);
6063
6064       if (breg != AT)
6065         return;
6066       break;
6067
6068     case M_L_DAB:
6069       /*
6070        * The MIPS assembler seems to check for X_add_number not
6071        * being double aligned and generating:
6072        *        lui     at,%hi(foo+1)
6073        *        addu    at,at,v1
6074        *        addiu   at,at,%lo(foo+1)
6075        *        lwc1    f2,0(at)
6076        *        lwc1    f3,4(at)
6077        * But, the resulting address is the same after relocation so why
6078        * generate the extra instruction?
6079        */
6080       if (mips_arch == CPU_R4650)
6081         {
6082           as_bad (_("opcode not supported on this processor"));
6083           return;
6084         }
6085       /* Itbl support may require additional care here.  */
6086       coproc = 1;
6087       if (mips_opts.isa != ISA_MIPS1)
6088         {
6089           s = "ldc1";
6090           goto ld;
6091         }
6092
6093       s = "lwc1";
6094       fmt = "T,o(b)";
6095       goto ldd_std;
6096
6097     case M_S_DAB:
6098       if (mips_arch == CPU_R4650)
6099         {
6100           as_bad (_("opcode not supported on this processor"));
6101           return;
6102         }
6103
6104       if (mips_opts.isa != ISA_MIPS1)
6105         {
6106           s = "sdc1";
6107           goto st;
6108         }
6109
6110       s = "swc1";
6111       fmt = "T,o(b)";
6112       /* Itbl support may require additional care here.  */
6113       coproc = 1;
6114       goto ldd_std;
6115
6116     case M_LD_AB:
6117       if (HAVE_64BIT_GPRS)
6118         {
6119           s = "ld";
6120           goto ld;
6121         }
6122
6123       s = "lw";
6124       fmt = "t,o(b)";
6125       goto ldd_std;
6126
6127     case M_SD_AB:
6128       if (HAVE_64BIT_GPRS)
6129         {
6130           s = "sd";
6131           goto st;
6132         }
6133
6134       s = "sw";
6135       fmt = "t,o(b)";
6136
6137     ldd_std:
6138       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6139          loads for the case of doing a pair of loads to simulate an 'ld'.
6140          This is not currently done by the compiler, and assembly coders
6141          writing embedded-pic code can cope.  */
6142
6143       if (offset_expr.X_op != O_symbol
6144           && offset_expr.X_op != O_constant)
6145         {
6146           as_bad (_("expression too complex"));
6147           offset_expr.X_op = O_constant;
6148         }
6149
6150       /* Even on a big endian machine $fn comes before $fn+1.  We have
6151          to adjust when loading from memory.  We set coproc if we must
6152          load $fn+1 first.  */
6153       /* Itbl support may require additional care here.  */
6154       if (! target_big_endian)
6155         coproc = 0;
6156
6157       if (mips_pic == NO_PIC
6158           || offset_expr.X_op == O_constant)
6159         {
6160           /* If this is a reference to a GP relative symbol, we want
6161                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6162                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6163              If we have a base register, we use this
6164                addu     $at,$breg,$gp
6165                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6166                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6167              If this is not a GP relative symbol, we want
6168                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6169                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6170                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6171              If there is a base register, we add it to $at after the
6172              lui instruction.  If there is a constant, we always use
6173              the last case.  */
6174           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6175               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6176             {
6177               p = NULL;
6178               used_at = 1;
6179             }
6180           else
6181             {
6182               int off;
6183
6184               if (breg == 0)
6185                 {
6186                   frag_grow (28);
6187                   tempreg = GP;
6188                   off = 0;
6189                   used_at = 0;
6190                 }
6191               else
6192                 {
6193                   frag_grow (36);
6194                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6195                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6196                                "d,v,t", AT, breg, GP);
6197                   tempreg = AT;
6198                   off = 4;
6199                   used_at = 1;
6200                 }
6201
6202               /* Itbl support may require additional care here.  */
6203               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6204                            coproc ? treg + 1 : treg,
6205                            (int) BFD_RELOC_GPREL16, tempreg);
6206               offset_expr.X_add_number += 4;
6207
6208               /* Set mips_optimize to 2 to avoid inserting an
6209                  undesired nop.  */
6210               hold_mips_optimize = mips_optimize;
6211               mips_optimize = 2;
6212               /* Itbl support may require additional care here.  */
6213               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6214                            coproc ? treg : treg + 1,
6215                            (int) BFD_RELOC_GPREL16, tempreg);
6216               mips_optimize = hold_mips_optimize;
6217
6218               p = frag_var (rs_machine_dependent, 12 + off, 0,
6219                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6220                                           used_at && mips_opts.noat),
6221                             offset_expr.X_add_symbol, 0, NULL);
6222
6223               /* We just generated two relocs.  When tc_gen_reloc
6224                  handles this case, it will skip the first reloc and
6225                  handle the second.  The second reloc already has an
6226                  extra addend of 4, which we added above.  We must
6227                  subtract it out, and then subtract another 4 to make
6228                  the first reloc come out right.  The second reloc
6229                  will come out right because we are going to add 4 to
6230                  offset_expr when we build its instruction below.
6231
6232                  If we have a symbol, then we don't want to include
6233                  the offset, because it will wind up being included
6234                  when we generate the reloc.  */
6235
6236               if (offset_expr.X_op == O_constant)
6237                 offset_expr.X_add_number -= 8;
6238               else
6239                 {
6240                   offset_expr.X_add_number = -4;
6241                   offset_expr.X_op = O_constant;
6242                 }
6243             }
6244           macro_build_lui (p, &icnt, &offset_expr, AT);
6245           if (p != NULL)
6246             p += 4;
6247           if (breg != 0)
6248             {
6249               macro_build (p, &icnt, (expressionS *) NULL,
6250                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6251                            "d,v,t", AT, breg, AT);
6252               if (p != NULL)
6253                 p += 4;
6254             }
6255           /* Itbl support may require additional care here.  */
6256           macro_build (p, &icnt, &offset_expr, s, fmt,
6257                        coproc ? treg + 1 : treg,
6258                        (int) BFD_RELOC_LO16, AT);
6259           if (p != NULL)
6260             p += 4;
6261           /* FIXME: How do we handle overflow here?  */
6262           offset_expr.X_add_number += 4;
6263           /* Itbl support may require additional care here.  */
6264           macro_build (p, &icnt, &offset_expr, s, fmt,
6265                        coproc ? treg : treg + 1,
6266                        (int) BFD_RELOC_LO16, AT);
6267         }
6268       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6269         {
6270           int off;
6271
6272           /* If this is a reference to an external symbol, we want
6273                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6274                nop
6275                <op>     $treg,0($at)
6276                <op>     $treg+1,4($at)
6277              Otherwise we want
6278                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6279                nop
6280                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6281                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6282              If there is a base register we add it to $at before the
6283              lwc1 instructions.  If there is a constant we include it
6284              in the lwc1 instructions.  */
6285           used_at = 1;
6286           expr1.X_add_number = offset_expr.X_add_number;
6287           offset_expr.X_add_number = 0;
6288           if (expr1.X_add_number < -0x8000
6289               || expr1.X_add_number >= 0x8000 - 4)
6290             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6291           if (breg == 0)
6292             off = 0;
6293           else
6294             off = 4;
6295           frag_grow (24 + off);
6296           macro_build ((char *) NULL, &icnt, &offset_expr,
6297                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6298                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6299           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6300           if (breg != 0)
6301             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6302                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6303                          "d,v,t", AT, breg, AT);
6304           /* Itbl support may require additional care here.  */
6305           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6306                        coproc ? treg + 1 : treg,
6307                        (int) BFD_RELOC_LO16, AT);
6308           expr1.X_add_number += 4;
6309
6310           /* Set mips_optimize to 2 to avoid inserting an undesired
6311              nop.  */
6312           hold_mips_optimize = mips_optimize;
6313           mips_optimize = 2;
6314           /* Itbl support may require additional care here.  */
6315           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6316                        coproc ? treg : treg + 1,
6317                        (int) BFD_RELOC_LO16, AT);
6318           mips_optimize = hold_mips_optimize;
6319
6320           (void) frag_var (rs_machine_dependent, 0, 0,
6321                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6322                            offset_expr.X_add_symbol, 0, NULL);
6323         }
6324       else if (mips_pic == SVR4_PIC)
6325         {
6326           int gpdel, off;
6327
6328           /* If this is a reference to an external symbol, we want
6329                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6330                addu     $at,$at,$gp
6331                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6332                nop
6333                <op>     $treg,0($at)
6334                <op>     $treg+1,4($at)
6335              Otherwise we want
6336                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6337                nop
6338                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6339                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6340              If there is a base register we add it to $at before the
6341              lwc1 instructions.  If there is a constant we include it
6342              in the lwc1 instructions.  */
6343           used_at = 1;
6344           expr1.X_add_number = offset_expr.X_add_number;
6345           offset_expr.X_add_number = 0;
6346           if (expr1.X_add_number < -0x8000
6347               || expr1.X_add_number >= 0x8000 - 4)
6348             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6349           if (reg_needs_delay (GP))
6350             gpdel = 4;
6351           else
6352             gpdel = 0;
6353           if (breg == 0)
6354             off = 0;
6355           else
6356             off = 4;
6357           frag_grow (56);
6358           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6359                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6360           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6361                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6362                        "d,v,t", AT, AT, GP);
6363           macro_build ((char *) NULL, &icnt, &offset_expr,
6364                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6365                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6366           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6367           if (breg != 0)
6368             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6369                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6370                          "d,v,t", AT, breg, AT);
6371           /* Itbl support may require additional care here.  */
6372           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6373                        coproc ? treg + 1 : treg,
6374                        (int) BFD_RELOC_LO16, AT);
6375           expr1.X_add_number += 4;
6376
6377           /* Set mips_optimize to 2 to avoid inserting an undesired
6378              nop.  */
6379           hold_mips_optimize = mips_optimize;
6380           mips_optimize = 2;
6381           /* Itbl support may require additional care here.  */
6382           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6383                        coproc ? treg : treg + 1,
6384                        (int) BFD_RELOC_LO16, AT);
6385           mips_optimize = hold_mips_optimize;
6386           expr1.X_add_number -= 4;
6387
6388           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6389                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6390                                       8 + gpdel + off, 1, 0),
6391                         offset_expr.X_add_symbol, 0, NULL);
6392           if (gpdel > 0)
6393             {
6394               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6395               p += 4;
6396             }
6397           macro_build (p, &icnt, &offset_expr,
6398                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6399                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6400           p += 4;
6401           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6402           p += 4;
6403           if (breg != 0)
6404             {
6405               macro_build (p, &icnt, (expressionS *) NULL,
6406                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6407                            "d,v,t", AT, breg, AT);
6408               p += 4;
6409             }
6410           /* Itbl support may require additional care here.  */
6411           macro_build (p, &icnt, &expr1, s, fmt,
6412                        coproc ? treg + 1 : treg,
6413                        (int) BFD_RELOC_LO16, AT);
6414           p += 4;
6415           expr1.X_add_number += 4;
6416
6417           /* Set mips_optimize to 2 to avoid inserting an undesired
6418              nop.  */
6419           hold_mips_optimize = mips_optimize;
6420           mips_optimize = 2;
6421           /* Itbl support may require additional care here.  */
6422           macro_build (p, &icnt, &expr1, s, fmt,
6423                        coproc ? treg : treg + 1,
6424                        (int) BFD_RELOC_LO16, AT);
6425           mips_optimize = hold_mips_optimize;
6426         }
6427       else if (mips_pic == EMBEDDED_PIC)
6428         {
6429           /* If there is no base register, we use
6430                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6431                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6432              If we have a base register, we use
6433                addu     $at,$breg,$gp
6434                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6435                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6436              */
6437           if (breg == 0)
6438             {
6439               tempreg = GP;
6440               used_at = 0;
6441             }
6442           else
6443             {
6444               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6445                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6446                            "d,v,t", AT, breg, GP);
6447               tempreg = AT;
6448               used_at = 1;
6449             }
6450
6451           /* Itbl support may require additional care here.  */
6452           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6453                        coproc ? treg + 1 : treg,
6454                        (int) BFD_RELOC_GPREL16, tempreg);
6455           offset_expr.X_add_number += 4;
6456           /* Itbl support may require additional care here.  */
6457           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6458                        coproc ? treg : treg + 1,
6459                        (int) BFD_RELOC_GPREL16, tempreg);
6460         }
6461       else
6462         abort ();
6463
6464       if (! used_at)
6465         return;
6466
6467       break;
6468
6469     case M_LD_OB:
6470       s = "lw";
6471       goto sd_ob;
6472     case M_SD_OB:
6473       s = "sw";
6474     sd_ob:
6475       assert (HAVE_32BIT_ADDRESSES);
6476       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6477                    (int) BFD_RELOC_LO16, breg);
6478       offset_expr.X_add_number += 4;
6479       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6480                    (int) BFD_RELOC_LO16, breg);
6481       return;
6482
6483    /* New code added to support COPZ instructions.
6484       This code builds table entries out of the macros in mip_opcodes.
6485       R4000 uses interlocks to handle coproc delays.
6486       Other chips (like the R3000) require nops to be inserted for delays.
6487
6488       FIXME: Currently, we require that the user handle delays.
6489       In order to fill delay slots for non-interlocked chips,
6490       we must have a way to specify delays based on the coprocessor.
6491       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6492       What are the side-effects of the cop instruction?
6493       What cache support might we have and what are its effects?
6494       Both coprocessor & memory require delays. how long???
6495       What registers are read/set/modified?
6496
6497       If an itbl is provided to interpret cop instructions,
6498       this knowledge can be encoded in the itbl spec.  */
6499
6500     case M_COP0:
6501       s = "c0";
6502       goto copz;
6503     case M_COP1:
6504       s = "c1";
6505       goto copz;
6506     case M_COP2:
6507       s = "c2";
6508       goto copz;
6509     case M_COP3:
6510       s = "c3";
6511     copz:
6512       /* For now we just do C (same as Cz).  The parameter will be
6513          stored in insn_opcode by mips_ip.  */
6514       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6515                    ip->insn_opcode);
6516       return;
6517
6518     case M_MOVE:
6519       move_register (&icnt, dreg, sreg);
6520       return;
6521
6522 #ifdef LOSING_COMPILER
6523     default:
6524       /* Try and see if this is a new itbl instruction.
6525          This code builds table entries out of the macros in mip_opcodes.
6526          FIXME: For now we just assemble the expression and pass it's
6527          value along as a 32-bit immediate.
6528          We may want to have the assembler assemble this value,
6529          so that we gain the assembler's knowledge of delay slots,
6530          symbols, etc.
6531          Would it be more efficient to use mask (id) here? */
6532       if (itbl_have_entries
6533           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6534         {
6535           s = ip->insn_mo->name;
6536           s2 = "cop3";
6537           coproc = ITBL_DECODE_PNUM (immed_expr);;
6538           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6539           return;
6540         }
6541       macro2 (ip);
6542       return;
6543     }
6544   if (mips_opts.noat)
6545     as_warn (_("Macro used $at after \".set noat\""));
6546 }
6547
6548 static void
6549 macro2 (ip)
6550      struct mips_cl_insn *ip;
6551 {
6552   register int treg, sreg, dreg, breg;
6553   int tempreg;
6554   int mask;
6555   int icnt = 0;
6556   int used_at;
6557   expressionS expr1;
6558   const char *s;
6559   const char *s2;
6560   const char *fmt;
6561   int likely = 0;
6562   int dbl = 0;
6563   int coproc = 0;
6564   int lr = 0;
6565   int imm = 0;
6566   int off;
6567   offsetT maxnum;
6568   bfd_reloc_code_real_type r;
6569   char *p;
6570
6571   treg = (ip->insn_opcode >> 16) & 0x1f;
6572   dreg = (ip->insn_opcode >> 11) & 0x1f;
6573   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6574   mask = ip->insn_mo->mask;
6575
6576   expr1.X_op = O_constant;
6577   expr1.X_op_symbol = NULL;
6578   expr1.X_add_symbol = NULL;
6579   expr1.X_add_number = 1;
6580
6581   switch (mask)
6582     {
6583 #endif /* LOSING_COMPILER */
6584
6585     case M_DMUL:
6586       dbl = 1;
6587     case M_MUL:
6588       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6589                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6590       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6591                    dreg);
6592       return;
6593
6594     case M_DMUL_I:
6595       dbl = 1;
6596     case M_MUL_I:
6597       /* The MIPS assembler some times generates shifts and adds.  I'm
6598          not trying to be that fancy. GCC should do this for us
6599          anyway.  */
6600       load_register (&icnt, AT, &imm_expr, dbl);
6601       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6602                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6603       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6604                    dreg);
6605       break;
6606
6607     case M_DMULO_I:
6608       dbl = 1;
6609     case M_MULO_I:
6610       imm = 1;
6611       goto do_mulo;
6612
6613     case M_DMULO:
6614       dbl = 1;
6615     case M_MULO:
6616     do_mulo:
6617       mips_emit_delays (true);
6618       ++mips_opts.noreorder;
6619       mips_any_noreorder = 1;
6620       if (imm)
6621         load_register (&icnt, AT, &imm_expr, dbl);
6622       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6623                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6624       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6625                    dreg);
6626       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6627                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6628       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6629                    AT);
6630       if (mips_trap)
6631         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6632                      dreg, AT);
6633       else
6634         {
6635           expr1.X_add_number = 8;
6636           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6637                        AT);
6638           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6639                        0);
6640           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6641                        "c", 6);
6642         }
6643       --mips_opts.noreorder;
6644       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6645       break;
6646
6647     case M_DMULOU_I:
6648       dbl = 1;
6649     case M_MULOU_I:
6650       imm = 1;
6651       goto do_mulou;
6652
6653     case M_DMULOU:
6654       dbl = 1;
6655     case M_MULOU:
6656     do_mulou:
6657       mips_emit_delays (true);
6658       ++mips_opts.noreorder;
6659       mips_any_noreorder = 1;
6660       if (imm)
6661         load_register (&icnt, AT, &imm_expr, dbl);
6662       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6663                    dbl ? "dmultu" : "multu",
6664                    "s,t", sreg, imm ? AT : treg);
6665       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6666                    AT);
6667       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6668                    dreg);
6669       if (mips_trap)
6670         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6671                      AT, 0);
6672       else
6673         {
6674           expr1.X_add_number = 8;
6675           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6676           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6677                        0);
6678           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6679                        "c", 6);
6680         }
6681       --mips_opts.noreorder;
6682       break;
6683
6684     case M_ROL:
6685       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6686                    "d,v,t", AT, 0, treg);
6687       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6688                    "d,t,s", AT, sreg, AT);
6689       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6690                    "d,t,s", dreg, sreg, treg);
6691       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6692                    "d,v,t", dreg, dreg, AT);
6693       break;
6694
6695     case M_ROL_I:
6696       if (imm_expr.X_op != O_constant)
6697         as_bad (_("rotate count too large"));
6698       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6699                    AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6700       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6701                    dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6702       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6703                    dreg, dreg, AT);
6704       break;
6705
6706     case M_ROR:
6707       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6708                    "d,v,t", AT, 0, treg);
6709       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6710                    "d,t,s", AT, sreg, AT);
6711       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6712                    "d,t,s", dreg, sreg, treg);
6713       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6714                    "d,v,t", dreg, dreg, AT);
6715       break;
6716
6717     case M_ROR_I:
6718       if (imm_expr.X_op != O_constant)
6719         as_bad (_("rotate count too large"));
6720       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6721                    AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6722       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6723                    dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6724       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6725                    dreg, dreg, AT);
6726       break;
6727
6728     case M_S_DOB:
6729       if (mips_arch == CPU_R4650)
6730         {
6731           as_bad (_("opcode not supported on this processor"));
6732           return;
6733         }
6734       assert (mips_opts.isa == ISA_MIPS1);
6735       /* Even on a big endian machine $fn comes before $fn+1.  We have
6736          to adjust when storing to memory.  */
6737       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6738                    target_big_endian ? treg + 1 : treg,
6739                    (int) BFD_RELOC_LO16, breg);
6740       offset_expr.X_add_number += 4;
6741       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6742                    target_big_endian ? treg : treg + 1,
6743                    (int) BFD_RELOC_LO16, breg);
6744       return;
6745
6746     case M_SEQ:
6747       if (sreg == 0)
6748         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6749                      treg, (int) BFD_RELOC_LO16);
6750       else if (treg == 0)
6751         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6752                      sreg, (int) BFD_RELOC_LO16);
6753       else
6754         {
6755           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6756                        "d,v,t", dreg, sreg, treg);
6757           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6758                        dreg, (int) BFD_RELOC_LO16);
6759         }
6760       return;
6761
6762     case M_SEQ_I:
6763       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6764         {
6765           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6766                        sreg, (int) BFD_RELOC_LO16);
6767           return;
6768         }
6769       if (sreg == 0)
6770         {
6771           as_warn (_("Instruction %s: result is always false"),
6772                    ip->insn_mo->name);
6773           move_register (&icnt, dreg, 0);
6774           return;
6775         }
6776       if (imm_expr.X_op == O_constant
6777           && imm_expr.X_add_number >= 0
6778           && imm_expr.X_add_number < 0x10000)
6779         {
6780           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6781                        sreg, (int) BFD_RELOC_LO16);
6782           used_at = 0;
6783         }
6784       else if (imm_expr.X_op == O_constant
6785                && imm_expr.X_add_number > -0x8000
6786                && imm_expr.X_add_number < 0)
6787         {
6788           imm_expr.X_add_number = -imm_expr.X_add_number;
6789           macro_build ((char *) NULL, &icnt, &imm_expr,
6790                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6791                        "t,r,j", dreg, sreg,
6792                        (int) BFD_RELOC_LO16);
6793           used_at = 0;
6794         }
6795       else
6796         {
6797           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6798           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6799                        "d,v,t", dreg, sreg, AT);
6800           used_at = 1;
6801         }
6802       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6803                    (int) BFD_RELOC_LO16);
6804       if (used_at)
6805         break;
6806       return;
6807
6808     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
6809       s = "slt";
6810       goto sge;
6811     case M_SGEU:
6812       s = "sltu";
6813     sge:
6814       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6815                    dreg, sreg, treg);
6816       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6817                    (int) BFD_RELOC_LO16);
6818       return;
6819
6820     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
6821     case M_SGEU_I:
6822       if (imm_expr.X_op == O_constant
6823           && imm_expr.X_add_number >= -0x8000
6824           && imm_expr.X_add_number < 0x8000)
6825         {
6826           macro_build ((char *) NULL, &icnt, &imm_expr,
6827                        mask == M_SGE_I ? "slti" : "sltiu",
6828                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6829           used_at = 0;
6830         }
6831       else
6832         {
6833           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6834           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6835                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6836                        AT);
6837           used_at = 1;
6838         }
6839       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6840                    (int) BFD_RELOC_LO16);
6841       if (used_at)
6842         break;
6843       return;
6844
6845     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
6846       s = "slt";
6847       goto sgt;
6848     case M_SGTU:
6849       s = "sltu";
6850     sgt:
6851       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6852                    dreg, treg, sreg);
6853       return;
6854
6855     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
6856       s = "slt";
6857       goto sgti;
6858     case M_SGTU_I:
6859       s = "sltu";
6860     sgti:
6861       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6862       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6863                    dreg, AT, sreg);
6864       break;
6865
6866     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
6867       s = "slt";
6868       goto sle;
6869     case M_SLEU:
6870       s = "sltu";
6871     sle:
6872       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6873                    dreg, treg, sreg);
6874       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6875                    (int) BFD_RELOC_LO16);
6876       return;
6877
6878     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6879       s = "slt";
6880       goto slei;
6881     case M_SLEU_I:
6882       s = "sltu";
6883     slei:
6884       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6885       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6886                    dreg, AT, sreg);
6887       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6888                    (int) BFD_RELOC_LO16);
6889       break;
6890
6891     case M_SLT_I:
6892       if (imm_expr.X_op == O_constant
6893           && imm_expr.X_add_number >= -0x8000
6894           && imm_expr.X_add_number < 0x8000)
6895         {
6896           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6897                        dreg, sreg, (int) BFD_RELOC_LO16);
6898           return;
6899         }
6900       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6901       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6902                    dreg, sreg, AT);
6903       break;
6904
6905     case M_SLTU_I:
6906       if (imm_expr.X_op == O_constant
6907           && imm_expr.X_add_number >= -0x8000
6908           && imm_expr.X_add_number < 0x8000)
6909         {
6910           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6911                        dreg, sreg, (int) BFD_RELOC_LO16);
6912           return;
6913         }
6914       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6915       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6916                    "d,v,t", dreg, sreg, AT);
6917       break;
6918
6919     case M_SNE:
6920       if (sreg == 0)
6921         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6922                      "d,v,t", dreg, 0, treg);
6923       else if (treg == 0)
6924         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6925                      "d,v,t", dreg, 0, sreg);
6926       else
6927         {
6928           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6929                        "d,v,t", dreg, sreg, treg);
6930           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6931                        "d,v,t", dreg, 0, dreg);
6932         }
6933       return;
6934
6935     case M_SNE_I:
6936       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6937         {
6938           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6939                        "d,v,t", dreg, 0, sreg);
6940           return;
6941         }
6942       if (sreg == 0)
6943         {
6944           as_warn (_("Instruction %s: result is always true"),
6945                    ip->insn_mo->name);
6946           macro_build ((char *) NULL, &icnt, &expr1,
6947                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6948                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6949           return;
6950         }
6951       if (imm_expr.X_op == O_constant
6952           && imm_expr.X_add_number >= 0
6953           && imm_expr.X_add_number < 0x10000)
6954         {
6955           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6956                        dreg, sreg, (int) BFD_RELOC_LO16);
6957           used_at = 0;
6958         }
6959       else if (imm_expr.X_op == O_constant
6960                && imm_expr.X_add_number > -0x8000
6961                && imm_expr.X_add_number < 0)
6962         {
6963           imm_expr.X_add_number = -imm_expr.X_add_number;
6964           macro_build ((char *) NULL, &icnt, &imm_expr,
6965                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6966                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6967           used_at = 0;
6968         }
6969       else
6970         {
6971           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6972           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6973                        "d,v,t", dreg, sreg, AT);
6974           used_at = 1;
6975         }
6976       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6977                    "d,v,t", dreg, 0, dreg);
6978       if (used_at)
6979         break;
6980       return;
6981
6982     case M_DSUB_I:
6983       dbl = 1;
6984     case M_SUB_I:
6985       if (imm_expr.X_op == O_constant
6986           && imm_expr.X_add_number > -0x8000
6987           && imm_expr.X_add_number <= 0x8000)
6988         {
6989           imm_expr.X_add_number = -imm_expr.X_add_number;
6990           macro_build ((char *) NULL, &icnt, &imm_expr,
6991                        dbl ? "daddi" : "addi",
6992                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6993           return;
6994         }
6995       load_register (&icnt, AT, &imm_expr, dbl);
6996       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6997                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6998       break;
6999
7000     case M_DSUBU_I:
7001       dbl = 1;
7002     case M_SUBU_I:
7003       if (imm_expr.X_op == O_constant
7004           && imm_expr.X_add_number > -0x8000
7005           && imm_expr.X_add_number <= 0x8000)
7006         {
7007           imm_expr.X_add_number = -imm_expr.X_add_number;
7008           macro_build ((char *) NULL, &icnt, &imm_expr,
7009                        dbl ? "daddiu" : "addiu",
7010                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7011           return;
7012         }
7013       load_register (&icnt, AT, &imm_expr, dbl);
7014       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7015                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7016       break;
7017
7018     case M_TEQ_I:
7019       s = "teq";
7020       goto trap;
7021     case M_TGE_I:
7022       s = "tge";
7023       goto trap;
7024     case M_TGEU_I:
7025       s = "tgeu";
7026       goto trap;
7027     case M_TLT_I:
7028       s = "tlt";
7029       goto trap;
7030     case M_TLTU_I:
7031       s = "tltu";
7032       goto trap;
7033     case M_TNE_I:
7034       s = "tne";
7035     trap:
7036       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7037       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7038                    AT);
7039       break;
7040
7041     case M_TRUNCWS:
7042     case M_TRUNCWD:
7043       assert (mips_opts.isa == ISA_MIPS1);
7044       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7045       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7046
7047       /*
7048        * Is the double cfc1 instruction a bug in the mips assembler;
7049        * or is there a reason for it?
7050        */
7051       mips_emit_delays (true);
7052       ++mips_opts.noreorder;
7053       mips_any_noreorder = 1;
7054       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7055                    treg, 31);
7056       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7057                    treg, 31);
7058       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7059       expr1.X_add_number = 3;
7060       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7061                    (int) BFD_RELOC_LO16);
7062       expr1.X_add_number = 2;
7063       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7064                      (int) BFD_RELOC_LO16);
7065       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7066                    AT, 31);
7067       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7068       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7069               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7070       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7071                    treg, 31);
7072       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7073       --mips_opts.noreorder;
7074       break;
7075
7076     case M_ULH:
7077       s = "lb";
7078       goto ulh;
7079     case M_ULHU:
7080       s = "lbu";
7081     ulh:
7082       if (offset_expr.X_add_number >= 0x7fff)
7083         as_bad (_("operand overflow"));
7084       /* avoid load delay */
7085       if (! target_big_endian)
7086         offset_expr.X_add_number += 1;
7087       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7088                    (int) BFD_RELOC_LO16, breg);
7089       if (! target_big_endian)
7090         offset_expr.X_add_number -= 1;
7091       else
7092         offset_expr.X_add_number += 1;
7093       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7094                    (int) BFD_RELOC_LO16, breg);
7095       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7096                    treg, treg, 8);
7097       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7098                    treg, treg, AT);
7099       break;
7100
7101     case M_ULD:
7102       s = "ldl";
7103       s2 = "ldr";
7104       off = 7;
7105       goto ulw;
7106     case M_ULW:
7107       s = "lwl";
7108       s2 = "lwr";
7109       off = 3;
7110     ulw:
7111       if (offset_expr.X_add_number >= 0x8000 - off)
7112         as_bad (_("operand overflow"));
7113       if (! target_big_endian)
7114         offset_expr.X_add_number += off;
7115       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7116                    (int) BFD_RELOC_LO16, breg);
7117       if (! target_big_endian)
7118         offset_expr.X_add_number -= off;
7119       else
7120         offset_expr.X_add_number += off;
7121       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7122                    (int) BFD_RELOC_LO16, breg);
7123       return;
7124
7125     case M_ULD_A:
7126       s = "ldl";
7127       s2 = "ldr";
7128       off = 7;
7129       goto ulwa;
7130     case M_ULW_A:
7131       s = "lwl";
7132       s2 = "lwr";
7133       off = 3;
7134     ulwa:
7135       used_at = 1;
7136       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7137       if (breg != 0)
7138         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7139                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7140                      "d,v,t", AT, AT, breg);
7141       if (! target_big_endian)
7142         expr1.X_add_number = off;
7143       else
7144         expr1.X_add_number = 0;
7145       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7146                    (int) BFD_RELOC_LO16, AT);
7147       if (! target_big_endian)
7148         expr1.X_add_number = 0;
7149       else
7150         expr1.X_add_number = off;
7151       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7152                    (int) BFD_RELOC_LO16, AT);
7153       break;
7154
7155     case M_ULH_A:
7156     case M_ULHU_A:
7157       used_at = 1;
7158       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7159       if (breg != 0)
7160         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7161                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7162                      "d,v,t", AT, AT, breg);
7163       if (target_big_endian)
7164         expr1.X_add_number = 0;
7165       macro_build ((char *) NULL, &icnt, &expr1,
7166                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7167                    (int) BFD_RELOC_LO16, AT);
7168       if (target_big_endian)
7169         expr1.X_add_number = 1;
7170       else
7171         expr1.X_add_number = 0;
7172       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7173                    (int) BFD_RELOC_LO16, AT);
7174       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7175                    treg, treg, 8);
7176       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7177                    treg, treg, AT);
7178       break;
7179
7180     case M_USH:
7181       if (offset_expr.X_add_number >= 0x7fff)
7182         as_bad (_("operand overflow"));
7183       if (target_big_endian)
7184         offset_expr.X_add_number += 1;
7185       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7186                    (int) BFD_RELOC_LO16, breg);
7187       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7188                    AT, treg, 8);
7189       if (target_big_endian)
7190         offset_expr.X_add_number -= 1;
7191       else
7192         offset_expr.X_add_number += 1;
7193       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7194                    (int) BFD_RELOC_LO16, breg);
7195       break;
7196
7197     case M_USD:
7198       s = "sdl";
7199       s2 = "sdr";
7200       off = 7;
7201       goto usw;
7202     case M_USW:
7203       s = "swl";
7204       s2 = "swr";
7205       off = 3;
7206     usw:
7207       if (offset_expr.X_add_number >= 0x8000 - off)
7208         as_bad (_("operand overflow"));
7209       if (! target_big_endian)
7210         offset_expr.X_add_number += off;
7211       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7212                    (int) BFD_RELOC_LO16, breg);
7213       if (! target_big_endian)
7214         offset_expr.X_add_number -= off;
7215       else
7216         offset_expr.X_add_number += off;
7217       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7218                    (int) BFD_RELOC_LO16, breg);
7219       return;
7220
7221     case M_USD_A:
7222       s = "sdl";
7223       s2 = "sdr";
7224       off = 7;
7225       goto uswa;
7226     case M_USW_A:
7227       s = "swl";
7228       s2 = "swr";
7229       off = 3;
7230     uswa:
7231       used_at = 1;
7232       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7233       if (breg != 0)
7234         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7235                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7236                      "d,v,t", AT, AT, breg);
7237       if (! target_big_endian)
7238         expr1.X_add_number = off;
7239       else
7240         expr1.X_add_number = 0;
7241       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7242                    (int) BFD_RELOC_LO16, AT);
7243       if (! target_big_endian)
7244         expr1.X_add_number = 0;
7245       else
7246         expr1.X_add_number = off;
7247       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7248                    (int) BFD_RELOC_LO16, AT);
7249       break;
7250
7251     case M_USH_A:
7252       used_at = 1;
7253       load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7254       if (breg != 0)
7255         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7256                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7257                      "d,v,t", AT, AT, breg);
7258       if (! target_big_endian)
7259         expr1.X_add_number = 0;
7260       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7261                    (int) BFD_RELOC_LO16, AT);
7262       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7263                    treg, treg, 8);
7264       if (! target_big_endian)
7265         expr1.X_add_number = 1;
7266       else
7267         expr1.X_add_number = 0;
7268       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7269                    (int) BFD_RELOC_LO16, AT);
7270       if (! target_big_endian)
7271         expr1.X_add_number = 0;
7272       else
7273         expr1.X_add_number = 1;
7274       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7275                    (int) BFD_RELOC_LO16, AT);
7276       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7277                    treg, treg, 8);
7278       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7279                    treg, treg, AT);
7280       break;
7281
7282     default:
7283       /* FIXME: Check if this is one of the itbl macros, since they
7284          are added dynamically.  */
7285       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7286       break;
7287     }
7288   if (mips_opts.noat)
7289     as_warn (_("Macro used $at after \".set noat\""));
7290 }
7291
7292 /* Implement macros in mips16 mode.  */
7293
7294 static void
7295 mips16_macro (ip)
7296      struct mips_cl_insn *ip;
7297 {
7298   int mask;
7299   int xreg, yreg, zreg, tmp;
7300   int icnt;
7301   expressionS expr1;
7302   int dbl;
7303   const char *s, *s2, *s3;
7304
7305   mask = ip->insn_mo->mask;
7306
7307   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7308   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7309   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7310
7311   icnt = 0;
7312
7313   expr1.X_op = O_constant;
7314   expr1.X_op_symbol = NULL;
7315   expr1.X_add_symbol = NULL;
7316   expr1.X_add_number = 1;
7317
7318   dbl = 0;
7319
7320   switch (mask)
7321     {
7322     default:
7323       internalError ();
7324
7325     case M_DDIV_3:
7326       dbl = 1;
7327     case M_DIV_3:
7328       s = "mflo";
7329       goto do_div3;
7330     case M_DREM_3:
7331       dbl = 1;
7332     case M_REM_3:
7333       s = "mfhi";
7334     do_div3:
7335       mips_emit_delays (true);
7336       ++mips_opts.noreorder;
7337       mips_any_noreorder = 1;
7338       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7339                    dbl ? "ddiv" : "div",
7340                    "0,x,y", xreg, yreg);
7341       expr1.X_add_number = 2;
7342       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7343       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7344                    7);
7345
7346       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7347          since that causes an overflow.  We should do that as well,
7348          but I don't see how to do the comparisons without a temporary
7349          register.  */
7350       --mips_opts.noreorder;
7351       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7352       break;
7353
7354     case M_DIVU_3:
7355       s = "divu";
7356       s2 = "mflo";
7357       goto do_divu3;
7358     case M_REMU_3:
7359       s = "divu";
7360       s2 = "mfhi";
7361       goto do_divu3;
7362     case M_DDIVU_3:
7363       s = "ddivu";
7364       s2 = "mflo";
7365       goto do_divu3;
7366     case M_DREMU_3:
7367       s = "ddivu";
7368       s2 = "mfhi";
7369     do_divu3:
7370       mips_emit_delays (true);
7371       ++mips_opts.noreorder;
7372       mips_any_noreorder = 1;
7373       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7374                    xreg, yreg);
7375       expr1.X_add_number = 2;
7376       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7377         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7378                      "6", 7);
7379       --mips_opts.noreorder;
7380       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7381       break;
7382
7383     case M_DMUL:
7384       dbl = 1;
7385     case M_MUL:
7386       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7387                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7388       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7389                    zreg);
7390       return;
7391
7392     case M_DSUBU_I:
7393       dbl = 1;
7394       goto do_subu;
7395     case M_SUBU_I:
7396     do_subu:
7397       if (imm_expr.X_op != O_constant)
7398         as_bad (_("Unsupported large constant"));
7399       imm_expr.X_add_number = -imm_expr.X_add_number;
7400       macro_build ((char *) NULL, &icnt, &imm_expr,
7401                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7402       break;
7403
7404     case M_SUBU_I_2:
7405       if (imm_expr.X_op != O_constant)
7406         as_bad (_("Unsupported large constant"));
7407       imm_expr.X_add_number = -imm_expr.X_add_number;
7408       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7409                    "x,k", xreg);
7410       break;
7411
7412     case M_DSUBU_I_2:
7413       if (imm_expr.X_op != O_constant)
7414         as_bad (_("Unsupported large constant"));
7415       imm_expr.X_add_number = -imm_expr.X_add_number;
7416       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7417                    "y,j", yreg);
7418       break;
7419
7420     case M_BEQ:
7421       s = "cmp";
7422       s2 = "bteqz";
7423       goto do_branch;
7424     case M_BNE:
7425       s = "cmp";
7426       s2 = "btnez";
7427       goto do_branch;
7428     case M_BLT:
7429       s = "slt";
7430       s2 = "btnez";
7431       goto do_branch;
7432     case M_BLTU:
7433       s = "sltu";
7434       s2 = "btnez";
7435       goto do_branch;
7436     case M_BLE:
7437       s = "slt";
7438       s2 = "bteqz";
7439       goto do_reverse_branch;
7440     case M_BLEU:
7441       s = "sltu";
7442       s2 = "bteqz";
7443       goto do_reverse_branch;
7444     case M_BGE:
7445       s = "slt";
7446       s2 = "bteqz";
7447       goto do_branch;
7448     case M_BGEU:
7449       s = "sltu";
7450       s2 = "bteqz";
7451       goto do_branch;
7452     case M_BGT:
7453       s = "slt";
7454       s2 = "btnez";
7455       goto do_reverse_branch;
7456     case M_BGTU:
7457       s = "sltu";
7458       s2 = "btnez";
7459
7460     do_reverse_branch:
7461       tmp = xreg;
7462       xreg = yreg;
7463       yreg = tmp;
7464
7465     do_branch:
7466       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7467                    xreg, yreg);
7468       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7469       break;
7470
7471     case M_BEQ_I:
7472       s = "cmpi";
7473       s2 = "bteqz";
7474       s3 = "x,U";
7475       goto do_branch_i;
7476     case M_BNE_I:
7477       s = "cmpi";
7478       s2 = "btnez";
7479       s3 = "x,U";
7480       goto do_branch_i;
7481     case M_BLT_I:
7482       s = "slti";
7483       s2 = "btnez";
7484       s3 = "x,8";
7485       goto do_branch_i;
7486     case M_BLTU_I:
7487       s = "sltiu";
7488       s2 = "btnez";
7489       s3 = "x,8";
7490       goto do_branch_i;
7491     case M_BLE_I:
7492       s = "slti";
7493       s2 = "btnez";
7494       s3 = "x,8";
7495       goto do_addone_branch_i;
7496     case M_BLEU_I:
7497       s = "sltiu";
7498       s2 = "btnez";
7499       s3 = "x,8";
7500       goto do_addone_branch_i;
7501     case M_BGE_I:
7502       s = "slti";
7503       s2 = "bteqz";
7504       s3 = "x,8";
7505       goto do_branch_i;
7506     case M_BGEU_I:
7507       s = "sltiu";
7508       s2 = "bteqz";
7509       s3 = "x,8";
7510       goto do_branch_i;
7511     case M_BGT_I:
7512       s = "slti";
7513       s2 = "bteqz";
7514       s3 = "x,8";
7515       goto do_addone_branch_i;
7516     case M_BGTU_I:
7517       s = "sltiu";
7518       s2 = "bteqz";
7519       s3 = "x,8";
7520
7521     do_addone_branch_i:
7522       if (imm_expr.X_op != O_constant)
7523         as_bad (_("Unsupported large constant"));
7524       ++imm_expr.X_add_number;
7525
7526     do_branch_i:
7527       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7528       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7529       break;
7530
7531     case M_ABS:
7532       expr1.X_add_number = 0;
7533       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8",  yreg);
7534       if (xreg != yreg)
7535         move_register (&icnt, xreg, yreg);
7536       expr1.X_add_number = 2;
7537       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7538       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7539                    "neg", "x,w", xreg, xreg);
7540     }
7541 }
7542
7543 /* For consistency checking, verify that all bits are specified either
7544    by the match/mask part of the instruction definition, or by the
7545    operand list.  */
7546 static int
7547 validate_mips_insn (opc)
7548      const struct mips_opcode *opc;
7549 {
7550   const char *p = opc->args;
7551   char c;
7552   unsigned long used_bits = opc->mask;
7553
7554   if ((used_bits & opc->match) != opc->match)
7555     {
7556       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7557               opc->name, opc->args);
7558       return 0;
7559     }
7560 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7561   while (*p)
7562     switch (c = *p++)
7563       {
7564       case ',': break;
7565       case '(': break;
7566       case ')': break;
7567       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7568       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7569       case 'A': break;
7570       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7571       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7572       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7573       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7574       case 'F': break;
7575       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7576       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7577       case 'I': break;
7578       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7579       case 'L': break;
7580       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7581       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7582       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7583       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7584       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7585       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7586       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7587       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7588       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7589       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7590       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7591       case 'f': break;
7592       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7593       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7594       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7595       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7596       case 'l': break;
7597       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7598       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7599       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7600       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7601       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7602       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7603       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7604       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7605       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7606       case 'x': break;
7607       case 'z': break;
7608       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7609       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7610                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7611       default:
7612         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7613                 c, opc->name, opc->args);
7614         return 0;
7615       }
7616 #undef USE_BITS
7617   if (used_bits != 0xffffffff)
7618     {
7619       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7620               ~used_bits & 0xffffffff, opc->name, opc->args);
7621       return 0;
7622     }
7623   return 1;
7624 }
7625
7626 /* This routine assembles an instruction into its binary format.  As a
7627    side effect, it sets one of the global variables imm_reloc or
7628    offset_reloc to the type of relocation to do if one of the operands
7629    is an address expression.  */
7630
7631 static void
7632 mips_ip (str, ip)
7633      char *str;
7634      struct mips_cl_insn *ip;
7635 {
7636   char *s;
7637   const char *args;
7638   char c = 0;
7639   struct mips_opcode *insn;
7640   char *argsStart;
7641   unsigned int regno;
7642   unsigned int lastregno = 0;
7643   char *s_reset;
7644   char save_c = 0;
7645
7646   insn_error = NULL;
7647
7648   /* If the instruction contains a '.', we first try to match an instruction
7649      including the '.'.  Then we try again without the '.'.  */
7650   insn = NULL;
7651   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7652     continue;
7653
7654   /* If we stopped on whitespace, then replace the whitespace with null for
7655      the call to hash_find.  Save the character we replaced just in case we
7656      have to re-parse the instruction.  */
7657   if (ISSPACE (*s))
7658     {
7659       save_c = *s;
7660       *s++ = '\0';
7661     }
7662
7663   insn = (struct mips_opcode *) hash_find (op_hash, str);
7664
7665   /* If we didn't find the instruction in the opcode table, try again, but
7666      this time with just the instruction up to, but not including the
7667      first '.'.  */
7668   if (insn == NULL)
7669     {
7670       /* Restore the character we overwrite above (if any).  */
7671       if (save_c)
7672         *(--s) = save_c;
7673
7674       /* Scan up to the first '.' or whitespace.  */
7675       for (s = str;
7676            *s != '\0' && *s != '.' && !ISSPACE (*s);
7677            ++s)
7678         continue;
7679
7680       /* If we did not find a '.', then we can quit now.  */
7681       if (*s != '.')
7682         {
7683           insn_error = "unrecognized opcode";
7684           return;
7685         }
7686
7687       /* Lookup the instruction in the hash table.  */
7688       *s++ = '\0';
7689       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7690         {
7691           insn_error = "unrecognized opcode";
7692           return;
7693         }
7694     }
7695
7696   argsStart = s;
7697   for (;;)
7698     {
7699       boolean ok;
7700
7701       assert (strcmp (insn->name, str) == 0);
7702
7703       if (OPCODE_IS_MEMBER (insn,
7704                             (mips_opts.isa
7705                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7706                             mips_arch))
7707         ok = true;
7708       else
7709         ok = false;
7710
7711       if (insn->pinfo != INSN_MACRO)
7712         {
7713           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7714             ok = false;
7715         }
7716
7717       if (! ok)
7718         {
7719           if (insn + 1 < &mips_opcodes[NUMOPCODES]
7720               && strcmp (insn->name, insn[1].name) == 0)
7721             {
7722               ++insn;
7723               continue;
7724             }
7725           else
7726             {
7727               if (!insn_error)
7728                 {
7729                   static char buf[100];
7730                   sprintf (buf,
7731                            _("opcode not supported on this processor: %s (%s)"),
7732                            mips_cpu_to_str (mips_arch),
7733                            mips_isa_to_str (mips_opts.isa));
7734
7735                   insn_error = buf;
7736                 }
7737               if (save_c)
7738                 *(--s) = save_c;
7739               return;
7740             }
7741         }
7742
7743       ip->insn_mo = insn;
7744       ip->insn_opcode = insn->match;
7745       insn_error = NULL;
7746       for (args = insn->args;; ++args)
7747         {
7748           s += strspn (s, " \t");
7749           switch (*args)
7750             {
7751             case '\0':          /* end of args */
7752               if (*s == '\0')
7753                 return;
7754               break;
7755
7756             case ',':
7757               if (*s++ == *args)
7758                 continue;
7759               s--;
7760               switch (*++args)
7761                 {
7762                 case 'r':
7763                 case 'v':
7764                   ip->insn_opcode |= lastregno << OP_SH_RS;
7765                   continue;
7766
7767                 case 'w':
7768                   ip->insn_opcode |= lastregno << OP_SH_RT;
7769                   continue;
7770
7771                 case 'W':
7772                   ip->insn_opcode |= lastregno << OP_SH_FT;
7773                   continue;
7774
7775                 case 'V':
7776                   ip->insn_opcode |= lastregno << OP_SH_FS;
7777                   continue;
7778                 }
7779               break;
7780
7781             case '(':
7782               /* Handle optional base register.
7783                  Either the base register is omitted or
7784                  we must have a left paren.  */
7785               /* This is dependent on the next operand specifier
7786                  is a base register specification.  */
7787               assert (args[1] == 'b' || args[1] == '5'
7788                       || args[1] == '-' || args[1] == '4');
7789               if (*s == '\0')
7790                 return;
7791
7792             case ')':           /* these must match exactly */
7793               if (*s++ == *args)
7794                 continue;
7795               break;
7796
7797             case '<':           /* must be at least one digit */
7798               /*
7799                * According to the manual, if the shift amount is greater
7800                * than 31 or less than 0, then the shift amount should be
7801                * mod 32.  In reality the mips assembler issues an error.
7802                * We issue a warning and mask out all but the low 5 bits.
7803                */
7804               my_getExpression (&imm_expr, s);
7805               check_absolute_expr (ip, &imm_expr);
7806               if ((unsigned long) imm_expr.X_add_number > 31)
7807                 {
7808                   as_warn (_("Improper shift amount (%ld)"),
7809                            (long) imm_expr.X_add_number);
7810                   imm_expr.X_add_number &= OP_MASK_SHAMT;
7811                 }
7812               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7813               imm_expr.X_op = O_absent;
7814               s = expr_end;
7815               continue;
7816
7817             case '>':           /* shift amount minus 32 */
7818               my_getExpression (&imm_expr, s);
7819               check_absolute_expr (ip, &imm_expr);
7820               if ((unsigned long) imm_expr.X_add_number < 32
7821                   || (unsigned long) imm_expr.X_add_number > 63)
7822                 break;
7823               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7824               imm_expr.X_op = O_absent;
7825               s = expr_end;
7826               continue;
7827
7828             case 'k':           /* cache code */
7829             case 'h':           /* prefx code */
7830               my_getExpression (&imm_expr, s);
7831               check_absolute_expr (ip, &imm_expr);
7832               if ((unsigned long) imm_expr.X_add_number > 31)
7833                 {
7834                   as_warn (_("Invalid value for `%s' (%lu)"),
7835                            ip->insn_mo->name,
7836                            (unsigned long) imm_expr.X_add_number);
7837                   imm_expr.X_add_number &= 0x1f;
7838                 }
7839               if (*args == 'k')
7840                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7841               else
7842                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7843               imm_expr.X_op = O_absent;
7844               s = expr_end;
7845               continue;
7846
7847             case 'c':           /* break code */
7848               my_getExpression (&imm_expr, s);
7849               check_absolute_expr (ip, &imm_expr);
7850               if ((unsigned) imm_expr.X_add_number > 1023)
7851                 {
7852                   as_warn (_("Illegal break code (%ld)"),
7853                            (long) imm_expr.X_add_number);
7854                   imm_expr.X_add_number &= OP_MASK_CODE;
7855                 }
7856               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7857               imm_expr.X_op = O_absent;
7858               s = expr_end;
7859               continue;
7860
7861             case 'q':           /* lower break code */
7862               my_getExpression (&imm_expr, s);
7863               check_absolute_expr (ip, &imm_expr);
7864               if ((unsigned) imm_expr.X_add_number > 1023)
7865                 {
7866                   as_warn (_("Illegal lower break code (%ld)"),
7867                            (long) imm_expr.X_add_number);
7868                   imm_expr.X_add_number &= OP_MASK_CODE2;
7869                 }
7870               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7871               imm_expr.X_op = O_absent;
7872               s = expr_end;
7873               continue;
7874
7875             case 'B':           /* 20-bit syscall/break code.  */
7876               my_getExpression (&imm_expr, s);
7877               check_absolute_expr (ip, &imm_expr);
7878               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7879                 as_warn (_("Illegal 20-bit code (%ld)"),
7880                          (long) imm_expr.X_add_number);
7881               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7882               imm_expr.X_op = O_absent;
7883               s = expr_end;
7884               continue;
7885
7886             case 'C':           /* Coprocessor code */
7887               my_getExpression (&imm_expr, s);
7888               check_absolute_expr (ip, &imm_expr);
7889               if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7890                 {
7891                   as_warn (_("Coproccesor code > 25 bits (%ld)"),
7892                            (long) imm_expr.X_add_number);
7893                   imm_expr.X_add_number &= ((1<<25) - 1);
7894                 }
7895               ip->insn_opcode |= imm_expr.X_add_number;
7896               imm_expr.X_op = O_absent;
7897               s = expr_end;
7898               continue;
7899
7900             case 'J':           /* 19-bit wait code.  */
7901               my_getExpression (&imm_expr, s);
7902               check_absolute_expr (ip, &imm_expr);
7903               if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7904                 as_warn (_("Illegal 19-bit code (%ld)"),
7905                          (long) imm_expr.X_add_number);
7906               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7907               imm_expr.X_op = O_absent;
7908               s = expr_end;
7909               continue;
7910
7911             case 'P':           /* Performance register */
7912               my_getExpression (&imm_expr, s);
7913               check_absolute_expr (ip, &imm_expr);
7914               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7915                 {
7916                   as_warn (_("Invalid performance register (%ld)"),
7917                            (long) imm_expr.X_add_number);
7918                   imm_expr.X_add_number &= OP_MASK_PERFREG;
7919                 }
7920               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7921               imm_expr.X_op = O_absent;
7922               s = expr_end;
7923               continue;
7924
7925             case 'b':           /* base register */
7926             case 'd':           /* destination register */
7927             case 's':           /* source register */
7928             case 't':           /* target register */
7929             case 'r':           /* both target and source */
7930             case 'v':           /* both dest and source */
7931             case 'w':           /* both dest and target */
7932             case 'E':           /* coprocessor target register */
7933             case 'G':           /* coprocessor destination register */
7934             case 'x':           /* ignore register name */
7935             case 'z':           /* must be zero register */
7936             case 'U':           /* destination register (clo/clz).  */
7937               s_reset = s;
7938               if (s[0] == '$')
7939                 {
7940
7941                   if (ISDIGIT (s[1]))
7942                     {
7943                       ++s;
7944                       regno = 0;
7945                       do
7946                         {
7947                           regno *= 10;
7948                           regno += *s - '0';
7949                           ++s;
7950                         }
7951                       while (ISDIGIT (*s));
7952                       if (regno > 31)
7953                         as_bad (_("Invalid register number (%d)"), regno);
7954                     }
7955                   else if (*args == 'E' || *args == 'G')
7956                     goto notreg;
7957                   else
7958                     {
7959                       if (s[1] == 'f' && s[2] == 'p')
7960                         {
7961                           s += 3;
7962                           regno = FP;
7963                         }
7964                       else if (s[1] == 's' && s[2] == 'p')
7965                         {
7966                           s += 3;
7967                           regno = SP;
7968                         }
7969                       else if (s[1] == 'g' && s[2] == 'p')
7970                         {
7971                           s += 3;
7972                           regno = GP;
7973                         }
7974                       else if (s[1] == 'a' && s[2] == 't')
7975                         {
7976                           s += 3;
7977                           regno = AT;
7978                         }
7979                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7980                         {
7981                           s += 4;
7982                           regno = KT0;
7983                         }
7984                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7985                         {
7986                           s += 4;
7987                           regno = KT1;
7988                         }
7989                       else if (itbl_have_entries)
7990                         {
7991                           char *p, *n;
7992                           unsigned long r;
7993
7994                           p = s + 1;    /* advance past '$' */
7995                           n = itbl_get_field (&p);  /* n is name */
7996
7997                           /* See if this is a register defined in an
7998                              itbl entry.  */
7999                           if (itbl_get_reg_val (n, &r))
8000                             {
8001                               /* Get_field advances to the start of
8002                                  the next field, so we need to back
8003                                  rack to the end of the last field.  */
8004                               if (p)
8005                                 s = p - 1;
8006                               else
8007                                 s = strchr (s, '\0');
8008                               regno = r;
8009                             }
8010                           else
8011                             goto notreg;
8012                         }
8013                       else
8014                         goto notreg;
8015                     }
8016                   if (regno == AT
8017                       && ! mips_opts.noat
8018                       && *args != 'E'
8019                       && *args != 'G')
8020                     as_warn (_("Used $at without \".set noat\""));
8021                   c = *args;
8022                   if (*s == ' ')
8023                     s++;
8024                   if (args[1] != *s)
8025                     {
8026                       if (c == 'r' || c == 'v' || c == 'w')
8027                         {
8028                           regno = lastregno;
8029                           s = s_reset;
8030                           args++;
8031                         }
8032                     }
8033                   /* 'z' only matches $0.  */
8034                   if (c == 'z' && regno != 0)
8035                     break;
8036
8037         /* Now that we have assembled one operand, we use the args string
8038          * to figure out where it goes in the instruction.  */
8039                   switch (c)
8040                     {
8041                     case 'r':
8042                     case 's':
8043                     case 'v':
8044                     case 'b':
8045                       ip->insn_opcode |= regno << OP_SH_RS;
8046                       break;
8047                     case 'd':
8048                     case 'G':
8049                       ip->insn_opcode |= regno << OP_SH_RD;
8050                       break;
8051                     case 'U':
8052                       ip->insn_opcode |= regno << OP_SH_RD;
8053                       ip->insn_opcode |= regno << OP_SH_RT;
8054                       break;
8055                     case 'w':
8056                     case 't':
8057                     case 'E':
8058                       ip->insn_opcode |= regno << OP_SH_RT;
8059                       break;
8060                     case 'x':
8061                       /* This case exists because on the r3000 trunc
8062                          expands into a macro which requires a gp
8063                          register.  On the r6000 or r4000 it is
8064                          assembled into a single instruction which
8065                          ignores the register.  Thus the insn version
8066                          is MIPS_ISA2 and uses 'x', and the macro
8067                          version is MIPS_ISA1 and uses 't'.  */
8068                       break;
8069                     case 'z':
8070                       /* This case is for the div instruction, which
8071                          acts differently if the destination argument
8072                          is $0.  This only matches $0, and is checked
8073                          outside the switch.  */
8074                       break;
8075                     case 'D':
8076                       /* Itbl operand; not yet implemented. FIXME ?? */
8077                       break;
8078                       /* What about all other operands like 'i', which
8079                          can be specified in the opcode table? */
8080                     }
8081                   lastregno = regno;
8082                   continue;
8083                 }
8084             notreg:
8085               switch (*args++)
8086                 {
8087                 case 'r':
8088                 case 'v':
8089                   ip->insn_opcode |= lastregno << OP_SH_RS;
8090                   continue;
8091                 case 'w':
8092                   ip->insn_opcode |= lastregno << OP_SH_RT;
8093                   continue;
8094                 }
8095               break;
8096
8097             case 'D':           /* floating point destination register */
8098             case 'S':           /* floating point source register */
8099             case 'T':           /* floating point target register */
8100             case 'R':           /* floating point source register */
8101             case 'V':
8102             case 'W':
8103               s_reset = s;
8104               if (s[0] == '$' && s[1] == 'f'
8105                   && ISDIGIT (s[2]))
8106                 {
8107                   s += 2;
8108                   regno = 0;
8109                   do
8110                     {
8111                       regno *= 10;
8112                       regno += *s - '0';
8113                       ++s;
8114                     }
8115                   while (ISDIGIT (*s));
8116
8117                   if (regno > 31)
8118                     as_bad (_("Invalid float register number (%d)"), regno);
8119
8120                   if ((regno & 1) != 0
8121                       && HAVE_32BIT_FPRS
8122                       && ! (strcmp (str, "mtc1") == 0
8123                             || strcmp (str, "mfc1") == 0
8124                             || strcmp (str, "lwc1") == 0
8125                             || strcmp (str, "swc1") == 0
8126                             || strcmp (str, "l.s") == 0
8127                             || strcmp (str, "s.s") == 0))
8128                     as_warn (_("Float register should be even, was %d"),
8129                              regno);
8130
8131                   c = *args;
8132                   if (*s == ' ')
8133                     s++;
8134                   if (args[1] != *s)
8135                     {
8136                       if (c == 'V' || c == 'W')
8137                         {
8138                           regno = lastregno;
8139                           s = s_reset;
8140                           args++;
8141                         }
8142                     }
8143                   switch (c)
8144                     {
8145                     case 'D':
8146                       ip->insn_opcode |= regno << OP_SH_FD;
8147                       break;
8148                     case 'V':
8149                     case 'S':
8150                       ip->insn_opcode |= regno << OP_SH_FS;
8151                       break;
8152                     case 'W':
8153                     case 'T':
8154                       ip->insn_opcode |= regno << OP_SH_FT;
8155                       break;
8156                     case 'R':
8157                       ip->insn_opcode |= regno << OP_SH_FR;
8158                       break;
8159                     }
8160                   lastregno = regno;
8161                   continue;
8162                 }
8163
8164               switch (*args++)
8165                 {
8166                 case 'V':
8167                   ip->insn_opcode |= lastregno << OP_SH_FS;
8168                   continue;
8169                 case 'W':
8170                   ip->insn_opcode |= lastregno << OP_SH_FT;
8171                   continue;
8172                 }
8173               break;
8174
8175             case 'I':
8176               my_getExpression (&imm_expr, s);
8177               if (imm_expr.X_op != O_big
8178                   && imm_expr.X_op != O_constant)
8179                 insn_error = _("absolute expression required");
8180               s = expr_end;
8181               continue;
8182
8183             case 'A':
8184               my_getExpression (&offset_expr, s);
8185               *imm_reloc = BFD_RELOC_32;
8186               s = expr_end;
8187               continue;
8188
8189             case 'F':
8190             case 'L':
8191             case 'f':
8192             case 'l':
8193               {
8194                 int f64;
8195                 int using_gprs;
8196                 char *save_in;
8197                 char *err;
8198                 unsigned char temp[8];
8199                 int len;
8200                 unsigned int length;
8201                 segT seg;
8202                 subsegT subseg;
8203                 char *p;
8204
8205                 /* These only appear as the last operand in an
8206                    instruction, and every instruction that accepts
8207                    them in any variant accepts them in all variants.
8208                    This means we don't have to worry about backing out
8209                    any changes if the instruction does not match.
8210
8211                    The difference between them is the size of the
8212                    floating point constant and where it goes.  For 'F'
8213                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8214                    is 32 bits.  Where the constant is placed is based
8215                    on how the MIPS assembler does things:
8216                     F -- .rdata
8217                     L -- .lit8
8218                     f -- immediate value
8219                     l -- .lit4
8220
8221                     The .lit4 and .lit8 sections are only used if
8222                     permitted by the -G argument.
8223
8224                     When generating embedded PIC code, we use the
8225                     .lit8 section but not the .lit4 section (we can do
8226                     .lit4 inline easily; we need to put .lit8
8227                     somewhere in the data segment, and using .lit8
8228                     permits the linker to eventually combine identical
8229                     .lit8 entries).
8230
8231                     The code below needs to know whether the target register
8232                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8233                     'F' are used with GPR-based instructions and 'l' and
8234                     'L' are used with FPR-based instructions.  */
8235
8236                 f64 = *args == 'F' || *args == 'L';
8237                 using_gprs = *args == 'F' || *args == 'f';
8238
8239                 save_in = input_line_pointer;
8240                 input_line_pointer = s;
8241                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8242                 length = len;
8243                 s = input_line_pointer;
8244                 input_line_pointer = save_in;
8245                 if (err != NULL && *err != '\0')
8246                   {
8247                     as_bad (_("Bad floating point constant: %s"), err);
8248                     memset (temp, '\0', sizeof temp);
8249                     length = f64 ? 8 : 4;
8250                   }
8251
8252                 assert (length == (unsigned) (f64 ? 8 : 4));
8253
8254                 if (*args == 'f'
8255                     || (*args == 'l'
8256                         && (! USE_GLOBAL_POINTER_OPT
8257                             || mips_pic == EMBEDDED_PIC
8258                             || g_switch_value < 4
8259                             || (temp[0] == 0 && temp[1] == 0)
8260                             || (temp[2] == 0 && temp[3] == 0))))
8261                   {
8262                     imm_expr.X_op = O_constant;
8263                     if (! target_big_endian)
8264                       imm_expr.X_add_number = bfd_getl32 (temp);
8265                     else
8266                       imm_expr.X_add_number = bfd_getb32 (temp);
8267                   }
8268                 else if (length > 4
8269                          && ! mips_disable_float_construction
8270                          /* Constants can only be constructed in GPRs and
8271                             copied to FPRs if the GPRs are at least as wide
8272                             as the FPRs.  Force the constant into memory if
8273                             we are using 64-bit FPRs but the GPRs are only
8274                             32 bits wide.  */
8275                          && (using_gprs
8276                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8277                          && ((temp[0] == 0 && temp[1] == 0)
8278                              || (temp[2] == 0 && temp[3] == 0))
8279                          && ((temp[4] == 0 && temp[5] == 0)
8280                              || (temp[6] == 0 && temp[7] == 0)))
8281                   {
8282                     /* The value is simple enough to load with a couple of
8283                        instructions.  If using 32-bit registers, set
8284                        imm_expr to the high order 32 bits and offset_expr to
8285                        the low order 32 bits.  Otherwise, set imm_expr to
8286                        the entire 64 bit constant.  */
8287                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8288                       {
8289                         imm_expr.X_op = O_constant;
8290                         offset_expr.X_op = O_constant;
8291                         if (! target_big_endian)
8292                           {
8293                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8294                             offset_expr.X_add_number = bfd_getl32 (temp);
8295                           }
8296                         else
8297                           {
8298                             imm_expr.X_add_number = bfd_getb32 (temp);
8299                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8300                           }
8301                         if (offset_expr.X_add_number == 0)
8302                           offset_expr.X_op = O_absent;
8303                       }
8304                     else if (sizeof (imm_expr.X_add_number) > 4)
8305                       {
8306                         imm_expr.X_op = O_constant;
8307                         if (! target_big_endian)
8308                           imm_expr.X_add_number = bfd_getl64 (temp);
8309                         else
8310                           imm_expr.X_add_number = bfd_getb64 (temp);
8311                       }
8312                     else
8313                       {
8314                         imm_expr.X_op = O_big;
8315                         imm_expr.X_add_number = 4;
8316                         if (! target_big_endian)
8317                           {
8318                             generic_bignum[0] = bfd_getl16 (temp);
8319                             generic_bignum[1] = bfd_getl16 (temp + 2);
8320                             generic_bignum[2] = bfd_getl16 (temp + 4);
8321                             generic_bignum[3] = bfd_getl16 (temp + 6);
8322                           }
8323                         else
8324                           {
8325                             generic_bignum[0] = bfd_getb16 (temp + 6);
8326                             generic_bignum[1] = bfd_getb16 (temp + 4);
8327                             generic_bignum[2] = bfd_getb16 (temp + 2);
8328                             generic_bignum[3] = bfd_getb16 (temp);
8329                           }
8330                       }
8331                   }
8332                 else
8333                   {
8334                     const char *newname;
8335                     segT new_seg;
8336
8337                     /* Switch to the right section.  */
8338                     seg = now_seg;
8339                     subseg = now_subseg;
8340                     switch (*args)
8341                       {
8342                       default: /* unused default case avoids warnings.  */
8343                       case 'L':
8344                         newname = RDATA_SECTION_NAME;
8345                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8346                             || mips_pic == EMBEDDED_PIC)
8347                           newname = ".lit8";
8348                         break;
8349                       case 'F':
8350                         if (mips_pic == EMBEDDED_PIC)
8351                           newname = ".lit8";
8352                         else
8353                           newname = RDATA_SECTION_NAME;
8354                         break;
8355                       case 'l':
8356                         assert (!USE_GLOBAL_POINTER_OPT
8357                                 || g_switch_value >= 4);
8358                         newname = ".lit4";
8359                         break;
8360                       }
8361                     new_seg = subseg_new (newname, (subsegT) 0);
8362                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8363                       bfd_set_section_flags (stdoutput, new_seg,
8364                                              (SEC_ALLOC
8365                                               | SEC_LOAD
8366                                               | SEC_READONLY
8367                                               | SEC_DATA));
8368                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8369                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8370                         && strcmp (TARGET_OS, "elf") != 0)
8371                       record_alignment (new_seg, 4);
8372                     else
8373                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8374                     if (seg == now_seg)
8375                       as_bad (_("Can't use floating point insn in this section"));
8376
8377                     /* Set the argument to the current address in the
8378                        section.  */
8379                     offset_expr.X_op = O_symbol;
8380                     offset_expr.X_add_symbol =
8381                       symbol_new ("L0\001", now_seg,
8382                                   (valueT) frag_now_fix (), frag_now);
8383                     offset_expr.X_add_number = 0;
8384
8385                     /* Put the floating point number into the section.  */
8386                     p = frag_more ((int) length);
8387                     memcpy (p, temp, length);
8388
8389                     /* Switch back to the original section.  */
8390                     subseg_set (seg, subseg);
8391                   }
8392               }
8393               continue;
8394
8395             case 'i':           /* 16 bit unsigned immediate */
8396             case 'j':           /* 16 bit signed immediate */
8397               *imm_reloc = BFD_RELOC_LO16;
8398               c = my_getSmallExpression (&imm_expr, s);
8399               if (c != S_EX_NONE)
8400                 {
8401                   if (c != S_EX_LO)
8402                     {
8403                       if (imm_expr.X_op == O_constant)
8404                         imm_expr.X_add_number =
8405                           (imm_expr.X_add_number >> 16) & 0xffff;
8406 #ifdef OBJ_ELF
8407                       else if (c == S_EX_HIGHEST)
8408                           *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8409                       else if (c == S_EX_HIGHER)
8410                           *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8411                       else if (c == S_EX_GP_REL)
8412                         {
8413                           /* This occurs in NewABI only.  */
8414                           c = my_getSmallExpression (&imm_expr, s);
8415                           if (c != S_EX_NEG)
8416                             as_bad (_("bad composition of relocations"));
8417                           else
8418                             {
8419                               c = my_getSmallExpression (&imm_expr, s);
8420                               if (c != S_EX_LO)
8421                                 as_bad (_("bad composition of relocations"));
8422                               else
8423                                 {
8424                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8425                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8426                                   imm_reloc[2] = BFD_RELOC_LO16;
8427                                 }
8428                             }
8429                         }
8430 #endif
8431                       else if (c == S_EX_HI)
8432                         {
8433                           *imm_reloc = BFD_RELOC_HI16_S;
8434                           imm_unmatched_hi = true;
8435                         }
8436                       else
8437                         *imm_reloc = BFD_RELOC_HI16;
8438                     }
8439                   else if (imm_expr.X_op == O_constant)
8440                     imm_expr.X_add_number &= 0xffff;
8441                 }
8442               if (*args == 'i')
8443                 {
8444                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8445                       || ((imm_expr.X_add_number < 0
8446                            || imm_expr.X_add_number >= 0x10000)
8447                           && imm_expr.X_op == O_constant))
8448                     {
8449                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8450                           !strcmp (insn->name, insn[1].name))
8451                         break;
8452                       if (imm_expr.X_op == O_constant
8453                           || imm_expr.X_op == O_big)
8454                         as_bad (_("16 bit expression not in range 0..65535"));
8455                     }
8456                 }
8457               else
8458                 {
8459                   int more;
8460                   offsetT max;
8461
8462                   /* The upper bound should be 0x8000, but
8463                      unfortunately the MIPS assembler accepts numbers
8464                      from 0x8000 to 0xffff and sign extends them, and
8465                      we want to be compatible.  We only permit this
8466                      extended range for an instruction which does not
8467                      provide any further alternates, since those
8468                      alternates may handle other cases.  People should
8469                      use the numbers they mean, rather than relying on
8470                      a mysterious sign extension.  */
8471                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8472                           strcmp (insn->name, insn[1].name) == 0);
8473                   if (more)
8474                     max = 0x8000;
8475                   else
8476                     max = 0x10000;
8477                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8478                       || ((imm_expr.X_add_number < -0x8000
8479                            || imm_expr.X_add_number >= max)
8480                           && imm_expr.X_op == O_constant)
8481                       || (more
8482                           && imm_expr.X_add_number < 0
8483                           && HAVE_64BIT_GPRS
8484                           && imm_expr.X_unsigned
8485                           && sizeof (imm_expr.X_add_number) <= 4))
8486                     {
8487                       if (more)
8488                         break;
8489                       if (imm_expr.X_op == O_constant
8490                           || imm_expr.X_op == O_big)
8491                         as_bad (_("16 bit expression not in range -32768..32767"));
8492                     }
8493                 }
8494               s = expr_end;
8495               continue;
8496
8497             case 'o':           /* 16 bit offset */
8498               c = my_getSmallExpression (&offset_expr, s);
8499
8500               /* If this value won't fit into a 16 bit offset, then go
8501                  find a macro that will generate the 32 bit offset
8502                  code pattern.  */
8503               if (c == S_EX_NONE
8504                   && (offset_expr.X_op != O_constant
8505                       || offset_expr.X_add_number >= 0x8000
8506                       || offset_expr.X_add_number < -0x8000))
8507                 break;
8508
8509               if (c == S_EX_HI)
8510                 {
8511                   if (offset_expr.X_op != O_constant)
8512                     break;
8513                   offset_expr.X_add_number =
8514                     (offset_expr.X_add_number >> 16) & 0xffff;
8515                 }
8516               *offset_reloc = BFD_RELOC_LO16;
8517               s = expr_end;
8518               continue;
8519
8520             case 'p':           /* pc relative offset */
8521               if (mips_pic == EMBEDDED_PIC)
8522                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8523               else
8524                 *offset_reloc = BFD_RELOC_16_PCREL;
8525               my_getExpression (&offset_expr, s);
8526               s = expr_end;
8527               continue;
8528
8529             case 'u':           /* upper 16 bits */
8530               c = my_getSmallExpression (&imm_expr, s);
8531               *imm_reloc = BFD_RELOC_LO16;
8532               if (c != S_EX_NONE)
8533                 {
8534                   if (c != S_EX_LO)
8535                     {
8536                       if (imm_expr.X_op == O_constant)
8537                         imm_expr.X_add_number =
8538                           (imm_expr.X_add_number >> 16) & 0xffff;
8539                       else if (c == S_EX_HI)
8540                         {
8541                           *imm_reloc = BFD_RELOC_HI16_S;
8542                           imm_unmatched_hi = true;
8543                         }
8544 #ifdef OBJ_ELF
8545                       else if (c == S_EX_HIGHEST)
8546                           *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8547                       else if (c == S_EX_GP_REL)
8548                         {
8549                           /* This occurs in NewABI only.  */
8550                           c = my_getSmallExpression (&imm_expr, s);
8551                           if (c != S_EX_NEG)
8552                             as_bad (_("bad composition of relocations"));
8553                           else
8554                             {
8555                               c = my_getSmallExpression (&imm_expr, s);
8556                               if (c != S_EX_HI)
8557                                 as_bad (_("bad composition of relocations"));
8558                               else
8559                                 {
8560                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8561                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8562                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8563                                 }
8564                             }
8565                         }
8566 #endif
8567                       else
8568                         *imm_reloc = BFD_RELOC_HI16;
8569                     }
8570                   else if (imm_expr.X_op == O_constant)
8571                     imm_expr.X_add_number &= 0xffff;
8572                 }
8573               if (imm_expr.X_op == O_constant
8574                   && (imm_expr.X_add_number < 0
8575                       || imm_expr.X_add_number >= 0x10000))
8576                 as_bad (_("lui expression not in range 0..65535"));
8577               s = expr_end;
8578               continue;
8579
8580             case 'a':           /* 26 bit address */
8581               my_getExpression (&offset_expr, s);
8582               s = expr_end;
8583               *offset_reloc = BFD_RELOC_MIPS_JMP;
8584               continue;
8585
8586             case 'N':           /* 3 bit branch condition code */
8587             case 'M':           /* 3 bit compare condition code */
8588               if (strncmp (s, "$fcc", 4) != 0)
8589                 break;
8590               s += 4;
8591               regno = 0;
8592               do
8593                 {
8594                   regno *= 10;
8595                   regno += *s - '0';
8596                   ++s;
8597                 }
8598               while (ISDIGIT (*s));
8599               if (regno > 7)
8600                 as_bad (_("invalid condition code register $fcc%d"), regno);
8601               if (*args == 'N')
8602                 ip->insn_opcode |= regno << OP_SH_BCC;
8603               else
8604                 ip->insn_opcode |= regno << OP_SH_CCC;
8605               continue;
8606
8607             case 'H':
8608               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8609                 s += 2;
8610               if (ISDIGIT (*s))
8611                 {
8612                   c = 0;
8613                   do
8614                     {
8615                       c *= 10;
8616                       c += *s - '0';
8617                       ++s;
8618                     }
8619                   while (ISDIGIT (*s));
8620                 }
8621               else
8622                 c = 8; /* Invalid sel value.  */
8623
8624               if (c > 7)
8625                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8626               ip->insn_opcode |= c;
8627               continue;
8628
8629             default:
8630               as_bad (_("bad char = '%c'\n"), *args);
8631               internalError ();
8632             }
8633           break;
8634         }
8635       /* Args don't match.  */
8636       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8637           !strcmp (insn->name, insn[1].name))
8638         {
8639           ++insn;
8640           s = argsStart;
8641           insn_error = _("illegal operands");
8642           continue;
8643         }
8644       if (save_c)
8645         *(--s) = save_c;
8646       insn_error = _("illegal operands");
8647       return;
8648     }
8649 }
8650
8651 /* This routine assembles an instruction into its binary format when
8652    assembling for the mips16.  As a side effect, it sets one of the
8653    global variables imm_reloc or offset_reloc to the type of
8654    relocation to do if one of the operands is an address expression.
8655    It also sets mips16_small and mips16_ext if the user explicitly
8656    requested a small or extended instruction.  */
8657
8658 static void
8659 mips16_ip (str, ip)
8660      char *str;
8661      struct mips_cl_insn *ip;
8662 {
8663   char *s;
8664   const char *args;
8665   struct mips_opcode *insn;
8666   char *argsstart;
8667   unsigned int regno;
8668   unsigned int lastregno = 0;
8669   char *s_reset;
8670
8671   insn_error = NULL;
8672
8673   mips16_small = false;
8674   mips16_ext = false;
8675
8676   for (s = str; ISLOWER (*s); ++s)
8677     ;
8678   switch (*s)
8679     {
8680     case '\0':
8681       break;
8682
8683     case ' ':
8684       *s++ = '\0';
8685       break;
8686
8687     case '.':
8688       if (s[1] == 't' && s[2] == ' ')
8689         {
8690           *s = '\0';
8691           mips16_small = true;
8692           s += 3;
8693           break;
8694         }
8695       else if (s[1] == 'e' && s[2] == ' ')
8696         {
8697           *s = '\0';
8698           mips16_ext = true;
8699           s += 3;
8700           break;
8701         }
8702       /* Fall through.  */
8703     default:
8704       insn_error = _("unknown opcode");
8705       return;
8706     }
8707
8708   if (mips_opts.noautoextend && ! mips16_ext)
8709     mips16_small = true;
8710
8711   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8712     {
8713       insn_error = _("unrecognized opcode");
8714       return;
8715     }
8716
8717   argsstart = s;
8718   for (;;)
8719     {
8720       assert (strcmp (insn->name, str) == 0);
8721
8722       ip->insn_mo = insn;
8723       ip->insn_opcode = insn->match;
8724       ip->use_extend = false;
8725       imm_expr.X_op = O_absent;
8726       imm_reloc[0] = BFD_RELOC_UNUSED;
8727       imm_reloc[1] = BFD_RELOC_UNUSED;
8728       imm_reloc[2] = BFD_RELOC_UNUSED;
8729       offset_expr.X_op = O_absent;
8730       offset_reloc[0] = BFD_RELOC_UNUSED;
8731       offset_reloc[1] = BFD_RELOC_UNUSED;
8732       offset_reloc[2] = BFD_RELOC_UNUSED;
8733       for (args = insn->args; 1; ++args)
8734         {
8735           int c;
8736
8737           if (*s == ' ')
8738             ++s;
8739
8740           /* In this switch statement we call break if we did not find
8741              a match, continue if we did find a match, or return if we
8742              are done.  */
8743
8744           c = *args;
8745           switch (c)
8746             {
8747             case '\0':
8748               if (*s == '\0')
8749                 {
8750                   /* Stuff the immediate value in now, if we can.  */
8751                   if (imm_expr.X_op == O_constant
8752                       && *imm_reloc > BFD_RELOC_UNUSED
8753                       && insn->pinfo != INSN_MACRO)
8754                     {
8755                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8756                                     imm_expr.X_add_number, true, mips16_small,
8757                                     mips16_ext, &ip->insn_opcode,
8758                                     &ip->use_extend, &ip->extend);
8759                       imm_expr.X_op = O_absent;
8760                       *imm_reloc = BFD_RELOC_UNUSED;
8761                     }
8762
8763                   return;
8764                 }
8765               break;
8766
8767             case ',':
8768               if (*s++ == c)
8769                 continue;
8770               s--;
8771               switch (*++args)
8772                 {
8773                 case 'v':
8774                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8775                   continue;
8776                 case 'w':
8777                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8778                   continue;
8779                 }
8780               break;
8781
8782             case '(':
8783             case ')':
8784               if (*s++ == c)
8785                 continue;
8786               break;
8787
8788             case 'v':
8789             case 'w':
8790               if (s[0] != '$')
8791                 {
8792                   if (c == 'v')
8793                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8794                   else
8795                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8796                   ++args;
8797                   continue;
8798                 }
8799               /* Fall through.  */
8800             case 'x':
8801             case 'y':
8802             case 'z':
8803             case 'Z':
8804             case '0':
8805             case 'S':
8806             case 'R':
8807             case 'X':
8808             case 'Y':
8809               if (s[0] != '$')
8810                 break;
8811               s_reset = s;
8812               if (ISDIGIT (s[1]))
8813                 {
8814                   ++s;
8815                   regno = 0;
8816                   do
8817                     {
8818                       regno *= 10;
8819                       regno += *s - '0';
8820                       ++s;
8821                     }
8822                   while (ISDIGIT (*s));
8823                   if (regno > 31)
8824                     {
8825                       as_bad (_("invalid register number (%d)"), regno);
8826                       regno = 2;
8827                     }
8828                 }
8829               else
8830                 {
8831                   if (s[1] == 'f' && s[2] == 'p')
8832                     {
8833                       s += 3;
8834                       regno = FP;
8835                     }
8836                   else if (s[1] == 's' && s[2] == 'p')
8837                     {
8838                       s += 3;
8839                       regno = SP;
8840                     }
8841                   else if (s[1] == 'g' && s[2] == 'p')
8842                     {
8843                       s += 3;
8844                       regno = GP;
8845                     }
8846                   else if (s[1] == 'a' && s[2] == 't')
8847                     {
8848                       s += 3;
8849                       regno = AT;
8850                     }
8851                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8852                     {
8853                       s += 4;
8854                       regno = KT0;
8855                     }
8856                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8857                     {
8858                       s += 4;
8859                       regno = KT1;
8860                     }
8861                   else
8862                     break;
8863                 }
8864
8865               if (*s == ' ')
8866                 ++s;
8867               if (args[1] != *s)
8868                 {
8869                   if (c == 'v' || c == 'w')
8870                     {
8871                       regno = mips16_to_32_reg_map[lastregno];
8872                       s = s_reset;
8873                       args++;
8874                     }
8875                 }
8876
8877               switch (c)
8878                 {
8879                 case 'x':
8880                 case 'y':
8881                 case 'z':
8882                 case 'v':
8883                 case 'w':
8884                 case 'Z':
8885                   regno = mips32_to_16_reg_map[regno];
8886                   break;
8887
8888                 case '0':
8889                   if (regno != 0)
8890                     regno = ILLEGAL_REG;
8891                   break;
8892
8893                 case 'S':
8894                   if (regno != SP)
8895                     regno = ILLEGAL_REG;
8896                   break;
8897
8898                 case 'R':
8899                   if (regno != RA)
8900                     regno = ILLEGAL_REG;
8901                   break;
8902
8903                 case 'X':
8904                 case 'Y':
8905                   if (regno == AT && ! mips_opts.noat)
8906                     as_warn (_("used $at without \".set noat\""));
8907                   break;
8908
8909                 default:
8910                   internalError ();
8911                 }
8912
8913               if (regno == ILLEGAL_REG)
8914                 break;
8915
8916               switch (c)
8917                 {
8918                 case 'x':
8919                 case 'v':
8920                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8921                   break;
8922                 case 'y':
8923                 case 'w':
8924                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8925                   break;
8926                 case 'z':
8927                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8928                   break;
8929                 case 'Z':
8930                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8931                 case '0':
8932                 case 'S':
8933                 case 'R':
8934                   break;
8935                 case 'X':
8936                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8937                   break;
8938                 case 'Y':
8939                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8940                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8941                   break;
8942                 default:
8943                   internalError ();
8944                 }
8945
8946               lastregno = regno;
8947               continue;
8948
8949             case 'P':
8950               if (strncmp (s, "$pc", 3) == 0)
8951                 {
8952                   s += 3;
8953                   continue;
8954                 }
8955               break;
8956
8957             case '<':
8958             case '>':
8959             case '[':
8960             case ']':
8961             case '4':
8962             case '5':
8963             case 'H':
8964             case 'W':
8965             case 'D':
8966             case 'j':
8967             case '8':
8968             case 'V':
8969             case 'C':
8970             case 'U':
8971             case 'k':
8972             case 'K':
8973               if (s[0] == '%'
8974                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8975                 {
8976                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
8977                      and generate the appropriate reloc.  If the text
8978                      inside %gprel is not a symbol name with an
8979                      optional offset, then we generate a normal reloc
8980                      and will probably fail later.  */
8981                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8982                   if (imm_expr.X_op == O_symbol)
8983                     {
8984                       mips16_ext = true;
8985                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8986                       s = expr_end;
8987                       ip->use_extend = true;
8988                       ip->extend = 0;
8989                       continue;
8990                     }
8991                 }
8992               else
8993                 {
8994                   /* Just pick up a normal expression.  */
8995                   my_getExpression (&imm_expr, s);
8996                 }
8997
8998               if (imm_expr.X_op == O_register)
8999                 {
9000                   /* What we thought was an expression turned out to
9001                      be a register.  */
9002
9003                   if (s[0] == '(' && args[1] == '(')
9004                     {
9005                       /* It looks like the expression was omitted
9006                          before a register indirection, which means
9007                          that the expression is implicitly zero.  We
9008                          still set up imm_expr, so that we handle
9009                          explicit extensions correctly.  */
9010                       imm_expr.X_op = O_constant;
9011                       imm_expr.X_add_number = 0;
9012                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9013                       continue;
9014                     }
9015
9016                   break;
9017                 }
9018
9019               /* We need to relax this instruction.  */
9020               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9021               s = expr_end;
9022               continue;
9023
9024             case 'p':
9025             case 'q':
9026             case 'A':
9027             case 'B':
9028             case 'E':
9029               /* We use offset_reloc rather than imm_reloc for the PC
9030                  relative operands.  This lets macros with both
9031                  immediate and address operands work correctly.  */
9032               my_getExpression (&offset_expr, s);
9033
9034               if (offset_expr.X_op == O_register)
9035                 break;
9036
9037               /* We need to relax this instruction.  */
9038               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9039               s = expr_end;
9040               continue;
9041
9042             case '6':           /* break code */
9043               my_getExpression (&imm_expr, s);
9044               check_absolute_expr (ip, &imm_expr);
9045               if ((unsigned long) imm_expr.X_add_number > 63)
9046                 {
9047                   as_warn (_("Invalid value for `%s' (%lu)"),
9048                            ip->insn_mo->name,
9049                            (unsigned long) imm_expr.X_add_number);
9050                   imm_expr.X_add_number &= 0x3f;
9051                 }
9052               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9053               imm_expr.X_op = O_absent;
9054               s = expr_end;
9055               continue;
9056
9057             case 'a':           /* 26 bit address */
9058               my_getExpression (&offset_expr, s);
9059               s = expr_end;
9060               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9061               ip->insn_opcode <<= 16;
9062               continue;
9063
9064             case 'l':           /* register list for entry macro */
9065             case 'L':           /* register list for exit macro */
9066               {
9067                 int mask;
9068
9069                 if (c == 'l')
9070                   mask = 0;
9071                 else
9072                   mask = 7 << 3;
9073                 while (*s != '\0')
9074                   {
9075                     int freg, reg1, reg2;
9076
9077                     while (*s == ' ' || *s == ',')
9078                       ++s;
9079                     if (*s != '$')
9080                       {
9081                         as_bad (_("can't parse register list"));
9082                         break;
9083                       }
9084                     ++s;
9085                     if (*s != 'f')
9086                       freg = 0;
9087                     else
9088                       {
9089                         freg = 1;
9090                         ++s;
9091                       }
9092                     reg1 = 0;
9093                     while (ISDIGIT (*s))
9094                       {
9095                         reg1 *= 10;
9096                         reg1 += *s - '0';
9097                         ++s;
9098                       }
9099                     if (*s == ' ')
9100                       ++s;
9101                     if (*s != '-')
9102                       reg2 = reg1;
9103                     else
9104                       {
9105                         ++s;
9106                         if (*s != '$')
9107                           break;
9108                         ++s;
9109                         if (freg)
9110                           {
9111                             if (*s == 'f')
9112                               ++s;
9113                             else
9114                               {
9115                                 as_bad (_("invalid register list"));
9116                                 break;
9117                               }
9118                           }
9119                         reg2 = 0;
9120                         while (ISDIGIT (*s))
9121                           {
9122                             reg2 *= 10;
9123                             reg2 += *s - '0';
9124                             ++s;
9125                           }
9126                       }
9127                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9128                       {
9129                         mask &= ~ (7 << 3);
9130                         mask |= 5 << 3;
9131                       }
9132                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9133                       {
9134                         mask &= ~ (7 << 3);
9135                         mask |= 6 << 3;
9136                       }
9137                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9138                       mask |= (reg2 - 3) << 3;
9139                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9140                       mask |= (reg2 - 15) << 1;
9141                     else if (reg1 == 31 && reg2 == 31)
9142                       mask |= 1;
9143                     else
9144                       {
9145                         as_bad (_("invalid register list"));
9146                         break;
9147                       }
9148                   }
9149                 /* The mask is filled in in the opcode table for the
9150                    benefit of the disassembler.  We remove it before
9151                    applying the actual mask.  */
9152                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9153                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9154               }
9155             continue;
9156
9157             case 'e':           /* extend code */
9158               my_getExpression (&imm_expr, s);
9159               check_absolute_expr (ip, &imm_expr);
9160               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9161                 {
9162                   as_warn (_("Invalid value for `%s' (%lu)"),
9163                            ip->insn_mo->name,
9164                            (unsigned long) imm_expr.X_add_number);
9165                   imm_expr.X_add_number &= 0x7ff;
9166                 }
9167               ip->insn_opcode |= imm_expr.X_add_number;
9168               imm_expr.X_op = O_absent;
9169               s = expr_end;
9170               continue;
9171
9172             default:
9173               internalError ();
9174             }
9175           break;
9176         }
9177
9178       /* Args don't match.  */
9179       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9180           strcmp (insn->name, insn[1].name) == 0)
9181         {
9182           ++insn;
9183           s = argsstart;
9184           continue;
9185         }
9186
9187       insn_error = _("illegal operands");
9188
9189       return;
9190     }
9191 }
9192
9193 /* This structure holds information we know about a mips16 immediate
9194    argument type.  */
9195
9196 struct mips16_immed_operand
9197 {
9198   /* The type code used in the argument string in the opcode table.  */
9199   int type;
9200   /* The number of bits in the short form of the opcode.  */
9201   int nbits;
9202   /* The number of bits in the extended form of the opcode.  */
9203   int extbits;
9204   /* The amount by which the short form is shifted when it is used;
9205      for example, the sw instruction has a shift count of 2.  */
9206   int shift;
9207   /* The amount by which the short form is shifted when it is stored
9208      into the instruction code.  */
9209   int op_shift;
9210   /* Non-zero if the short form is unsigned.  */
9211   int unsp;
9212   /* Non-zero if the extended form is unsigned.  */
9213   int extu;
9214   /* Non-zero if the value is PC relative.  */
9215   int pcrel;
9216 };
9217
9218 /* The mips16 immediate operand types.  */
9219
9220 static const struct mips16_immed_operand mips16_immed_operands[] =
9221 {
9222   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9223   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9224   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9225   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9226   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9227   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9228   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9229   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9230   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9231   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9232   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9233   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9234   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9235   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9236   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9237   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9238   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9239   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9240   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9241   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9242   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9243 };
9244
9245 #define MIPS16_NUM_IMMED \
9246   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9247
9248 /* Handle a mips16 instruction with an immediate value.  This or's the
9249    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9250    whether an extended value is needed; if one is needed, it sets
9251    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9252    If SMALL is true, an unextended opcode was explicitly requested.
9253    If EXT is true, an extended opcode was explicitly requested.  If
9254    WARN is true, warn if EXT does not match reality.  */
9255
9256 static void
9257 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9258               extend)
9259      char *file;
9260      unsigned int line;
9261      int type;
9262      offsetT val;
9263      boolean warn;
9264      boolean small;
9265      boolean ext;
9266      unsigned long *insn;
9267      boolean *use_extend;
9268      unsigned short *extend;
9269 {
9270   register const struct mips16_immed_operand *op;
9271   int mintiny, maxtiny;
9272   boolean needext;
9273
9274   op = mips16_immed_operands;
9275   while (op->type != type)
9276     {
9277       ++op;
9278       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9279     }
9280
9281   if (op->unsp)
9282     {
9283       if (type == '<' || type == '>' || type == '[' || type == ']')
9284         {
9285           mintiny = 1;
9286           maxtiny = 1 << op->nbits;
9287         }
9288       else
9289         {
9290           mintiny = 0;
9291           maxtiny = (1 << op->nbits) - 1;
9292         }
9293     }
9294   else
9295     {
9296       mintiny = - (1 << (op->nbits - 1));
9297       maxtiny = (1 << (op->nbits - 1)) - 1;
9298     }
9299
9300   /* Branch offsets have an implicit 0 in the lowest bit.  */
9301   if (type == 'p' || type == 'q')
9302     val /= 2;
9303
9304   if ((val & ((1 << op->shift) - 1)) != 0
9305       || val < (mintiny << op->shift)
9306       || val > (maxtiny << op->shift))
9307     needext = true;
9308   else
9309     needext = false;
9310
9311   if (warn && ext && ! needext)
9312     as_warn_where (file, line,
9313                    _("extended operand requested but not required"));
9314   if (small && needext)
9315     as_bad_where (file, line, _("invalid unextended operand value"));
9316
9317   if (small || (! ext && ! needext))
9318     {
9319       int insnval;
9320
9321       *use_extend = false;
9322       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9323       insnval <<= op->op_shift;
9324       *insn |= insnval;
9325     }
9326   else
9327     {
9328       long minext, maxext;
9329       int extval;
9330
9331       if (op->extu)
9332         {
9333           minext = 0;
9334           maxext = (1 << op->extbits) - 1;
9335         }
9336       else
9337         {
9338           minext = - (1 << (op->extbits - 1));
9339           maxext = (1 << (op->extbits - 1)) - 1;
9340         }
9341       if (val < minext || val > maxext)
9342         as_bad_where (file, line,
9343                       _("operand value out of range for instruction"));
9344
9345       *use_extend = true;
9346       if (op->extbits == 16)
9347         {
9348           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9349           val &= 0x1f;
9350         }
9351       else if (op->extbits == 15)
9352         {
9353           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9354           val &= 0xf;
9355         }
9356       else
9357         {
9358           extval = ((val & 0x1f) << 6) | (val & 0x20);
9359           val = 0;
9360         }
9361
9362       *extend = (unsigned short) extval;
9363       *insn |= val;
9364     }
9365 }
9366 \f
9367 static struct percent_op_match
9368 {
9369    const char *str;
9370    const enum small_ex_type type;
9371 } percent_op[] =
9372 {
9373   {"%lo", S_EX_LO},
9374 #ifdef OBJ_ELF
9375   {"%call_hi", S_EX_CALL_HI},
9376   {"%call_lo", S_EX_CALL_LO},
9377   {"%call16", S_EX_CALL16},
9378   {"%got_disp", S_EX_GOT_DISP},
9379   {"%got_page", S_EX_GOT_PAGE},
9380   {"%got_ofst", S_EX_GOT_OFST},
9381   {"%got_hi", S_EX_GOT_HI},
9382   {"%got_lo", S_EX_GOT_LO},
9383   {"%got", S_EX_GOT},
9384   {"%gp_rel", S_EX_GP_REL},
9385   {"%half", S_EX_HALF},
9386   {"%highest", S_EX_HIGHEST},
9387   {"%higher", S_EX_HIGHER},
9388   {"%neg", S_EX_NEG},
9389 #endif
9390   {"%hi", S_EX_HI}
9391 };
9392
9393 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9394    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9395    can be nested, this is handled by blanking the innermost, parsing the
9396    rest by subsequent calls.  */
9397
9398 static int
9399 my_getSmallParser (str, len, nestlevel)
9400      char **str;
9401      unsigned int *len;
9402      int *nestlevel;
9403 {
9404   *len = 0;
9405   *str += strspn (*str, " \t");
9406   /* Check for expression in parentheses.  */
9407   if (**str == '(')
9408     {
9409       char *b = *str + 1 + strspn (*str + 1, " \t");
9410       char *e;
9411
9412       /* Check for base register.  */
9413       if (b[0] == '$')
9414         {
9415           if (strchr (b, ')')
9416               && (e = b + strcspn (b, ") \t"))
9417               && e - b > 1 && e - b < 4)
9418             {
9419                if ((e - b == 3
9420                     && ((b[1] == 'f' && b[2] == 'p')
9421                         || (b[1] == 's' && b[2] == 'p')
9422                         || (b[1] == 'g' && b[2] == 'p')
9423                         || (b[1] == 'a' && b[2] == 't')
9424                         || (ISDIGIT (b[1])
9425                             && ISDIGIT (b[2]))))
9426                    || (ISDIGIT (b[1])))
9427                  {
9428                    *len = strcspn (*str, ")") + 1;
9429                    return S_EX_REGISTER;
9430                  }
9431             }
9432         }
9433       /* Check for percent_op (in parentheses).  */
9434       else if (b[0] == '%')
9435         {
9436           *str = b;
9437           return my_getPercentOp (str, len, nestlevel);
9438         }
9439
9440       /* Some other expression in the parentheses, which can contain
9441          parentheses itself. Attempt to find the matching one.  */
9442       {
9443         int pcnt = 1;
9444         char *s;
9445
9446         *len = 1;
9447         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9448           {
9449             if (*s == '(')
9450               pcnt++;
9451             else if (*s == ')')
9452               pcnt--;
9453           }
9454       }
9455     }
9456   /* Check for percent_op (outside of parentheses).  */
9457   else if (*str[0] == '%')
9458     return my_getPercentOp (str, len, nestlevel);
9459
9460   /* Any other expression.  */
9461   return S_EX_NONE;
9462 }
9463
9464 static int
9465 my_getPercentOp (str, len, nestlevel)
9466      char **str;
9467      unsigned int *len;
9468      int *nestlevel;
9469 {
9470   char *tmp = *str + 1;
9471   unsigned int i = 0;
9472
9473   while (ISALPHA (*tmp) || *tmp == '_')
9474     {
9475       *tmp = TOLOWER (*tmp);
9476       tmp++;
9477     }
9478   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9479     {
9480       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9481           i++;
9482       else
9483         {
9484           int type = percent_op[i].type;
9485
9486           /* Only %hi and %lo are allowed for OldABI.  */
9487           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9488             return S_EX_NONE;
9489
9490           *len = strlen (percent_op[i].str);
9491           (*nestlevel)++;
9492           return type;
9493         }
9494     }
9495   return S_EX_NONE;
9496 }
9497
9498 static int
9499 my_getSmallExpression (ep, str)
9500      expressionS *ep;
9501      char *str;
9502 {
9503   static char *oldstr = NULL;
9504   int c = S_EX_NONE;
9505   int oldc;
9506   int nestlevel = -1;
9507   unsigned int len;
9508
9509   /* Don't update oldstr if the last call had nested percent_op's. We need
9510      it to parse the outer ones later.  */
9511   if (! oldstr)
9512     oldstr = str;
9513
9514   do
9515     {
9516       oldc = c;
9517       c = my_getSmallParser (&str, &len, &nestlevel);
9518       if (c != S_EX_NONE && c != S_EX_REGISTER)
9519         str += len;
9520     }
9521   while (c != S_EX_NONE && c != S_EX_REGISTER);
9522
9523   if (nestlevel >= 0)
9524     {
9525       /* A percent_op was encountered.  Don't try to get an expression if
9526          it is already blanked out.  */
9527       if (*(str + strspn (str + 1, " )")) != ')')
9528         {
9529           char save;
9530
9531           /* Let my_getExpression() stop at the closing parenthesis.  */
9532           save = *(str + len);
9533           *(str + len) = '\0';
9534           my_getExpression (ep, str);
9535           *(str + len) = save;
9536         }
9537       if (nestlevel > 0)
9538         {
9539           /* Blank out including the % sign and the proper matching
9540              parenthesis.  */
9541           int pcnt = 1;
9542           char *s = strrchr (oldstr, '%');
9543           char *end;
9544
9545           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9546             {
9547               if (*end == '(')
9548                 pcnt++;
9549               else if (*end == ')')
9550                 pcnt--;
9551             }
9552
9553           memset (s, ' ', end - s);
9554           str = oldstr;
9555         }
9556       else
9557         expr_end = str + len;
9558
9559       c = oldc;
9560     }
9561   else if (c == S_EX_NONE)
9562     {
9563       my_getExpression (ep, str);
9564     }
9565   else if (c == S_EX_REGISTER)
9566     {
9567       ep->X_op = O_constant;
9568       expr_end = str;
9569       ep->X_add_symbol = NULL;
9570       ep->X_op_symbol = NULL;
9571       ep->X_add_number = 0;
9572     }
9573   else
9574     {
9575       as_fatal(_("internal error"));
9576     }
9577
9578   if (nestlevel <= 0)
9579     /* All percent_op's have been handled.  */
9580     oldstr = NULL;
9581
9582   return c;
9583 }
9584
9585 static void
9586 my_getExpression (ep, str)
9587      expressionS *ep;
9588      char *str;
9589 {
9590   char *save_in;
9591   valueT val;
9592
9593   save_in = input_line_pointer;
9594   input_line_pointer = str;
9595   expression (ep);
9596   expr_end = input_line_pointer;
9597   input_line_pointer = save_in;
9598
9599   /* If we are in mips16 mode, and this is an expression based on `.',
9600      then we bump the value of the symbol by 1 since that is how other
9601      text symbols are handled.  We don't bother to handle complex
9602      expressions, just `.' plus or minus a constant.  */
9603   if (mips_opts.mips16
9604       && ep->X_op == O_symbol
9605       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9606       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9607       && symbol_get_frag (ep->X_add_symbol) == frag_now
9608       && symbol_constant_p (ep->X_add_symbol)
9609       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9610     S_SET_VALUE (ep->X_add_symbol, val + 1);
9611 }
9612
9613 /* Turn a string in input_line_pointer into a floating point constant
9614    of type TYPE, and store the appropriate bytes in *LITP.  The number
9615    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9616    returned, or NULL on OK.  */
9617
9618 char *
9619 md_atof (type, litP, sizeP)
9620      int type;
9621      char *litP;
9622      int *sizeP;
9623 {
9624   int prec;
9625   LITTLENUM_TYPE words[4];
9626   char *t;
9627   int i;
9628
9629   switch (type)
9630     {
9631     case 'f':
9632       prec = 2;
9633       break;
9634
9635     case 'd':
9636       prec = 4;
9637       break;
9638
9639     default:
9640       *sizeP = 0;
9641       return _("bad call to md_atof");
9642     }
9643
9644   t = atof_ieee (input_line_pointer, type, words);
9645   if (t)
9646     input_line_pointer = t;
9647
9648   *sizeP = prec * 2;
9649
9650   if (! target_big_endian)
9651     {
9652       for (i = prec - 1; i >= 0; i--)
9653         {
9654           md_number_to_chars (litP, (valueT) words[i], 2);
9655           litP += 2;
9656         }
9657     }
9658   else
9659     {
9660       for (i = 0; i < prec; i++)
9661         {
9662           md_number_to_chars (litP, (valueT) words[i], 2);
9663           litP += 2;
9664         }
9665     }
9666
9667   return NULL;
9668 }
9669
9670 void
9671 md_number_to_chars (buf, val, n)
9672      char *buf;
9673      valueT val;
9674      int n;
9675 {
9676   if (target_big_endian)
9677     number_to_chars_bigendian (buf, val, n);
9678   else
9679     number_to_chars_littleendian (buf, val, n);
9680 }
9681 \f
9682 #ifdef OBJ_ELF
9683 static int support_64bit_objects(void)
9684 {
9685   const char **list, **l;
9686
9687   list = bfd_target_list ();
9688   for (l = list; *l != NULL; l++)
9689 #ifdef TE_TMIPS
9690     /* This is traditional mips */
9691     if (strcmp (*l, "elf64-tradbigmips") == 0
9692         || strcmp (*l, "elf64-tradlittlemips") == 0)
9693 #else
9694     if (strcmp (*l, "elf64-bigmips") == 0
9695         || strcmp (*l, "elf64-littlemips") == 0)
9696 #endif
9697       break;
9698   free (list);
9699   return (*l != NULL);
9700 }
9701 #endif /* OBJ_ELF */
9702
9703 CONST char *md_shortopts = "nO::g::G:";
9704
9705 struct option md_longopts[] =
9706 {
9707 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9708   {"mips0", no_argument, NULL, OPTION_MIPS1},
9709   {"mips1", no_argument, NULL, OPTION_MIPS1},
9710 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9711   {"mips2", no_argument, NULL, OPTION_MIPS2},
9712 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9713   {"mips3", no_argument, NULL, OPTION_MIPS3},
9714 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9715   {"mips4", no_argument, NULL, OPTION_MIPS4},
9716 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9717   {"mips5", no_argument, NULL, OPTION_MIPS5},
9718 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9719   {"mips32", no_argument, NULL, OPTION_MIPS32},
9720 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9721   {"mips64", no_argument, NULL, OPTION_MIPS64},
9722 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9723   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9724 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9725   {"trap", no_argument, NULL, OPTION_TRAP},
9726   {"no-break", no_argument, NULL, OPTION_TRAP},
9727 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9728   {"break", no_argument, NULL, OPTION_BREAK},
9729   {"no-trap", no_argument, NULL, OPTION_BREAK},
9730 #define OPTION_EB (OPTION_MD_BASE + 11)
9731   {"EB", no_argument, NULL, OPTION_EB},
9732 #define OPTION_EL (OPTION_MD_BASE + 12)
9733   {"EL", no_argument, NULL, OPTION_EL},
9734 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9735   {"mips16", no_argument, NULL, OPTION_MIPS16},
9736 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9737   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9738 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9739   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9740 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9741   {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9742 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9743   {"mfp32", no_argument, NULL, OPTION_FP32},
9744 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9745   {"mgp32", no_argument, NULL, OPTION_GP32},
9746 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9747   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9748 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9749   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9750 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9751   {"march", required_argument, NULL, OPTION_MARCH},
9752 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9753   {"mtune", required_argument, NULL, OPTION_MTUNE},
9754 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9755   {"mcpu", required_argument, NULL, OPTION_MCPU},
9756 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9757   {"m4650", no_argument, NULL, OPTION_M4650},
9758 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9759   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9760 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9761   {"m4010", no_argument, NULL, OPTION_M4010},
9762 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9763   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9764 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9765   {"m4100", no_argument, NULL, OPTION_M4100},
9766 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9767   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9768 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9769   {"m3900", no_argument, NULL, OPTION_M3900},
9770 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9771   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9772 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9773   {"mgp64", no_argument, NULL, OPTION_GP64},
9774 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9775   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9776 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9777   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9778 #ifdef OBJ_ELF
9779 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 35)
9780 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9781   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
9782   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9783 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
9784   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
9785 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
9786   {"xgot",        no_argument, NULL, OPTION_XGOT},
9787 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
9788   {"mabi", required_argument, NULL, OPTION_MABI},
9789 #define OPTION_32          (OPTION_ELF_BASE + 4)
9790   {"32",          no_argument, NULL, OPTION_32},
9791 #define OPTION_N32         (OPTION_ELF_BASE + 5)
9792   {"n32",         no_argument, NULL, OPTION_N32},
9793 #define OPTION_64          (OPTION_ELF_BASE + 6)
9794   {"64",          no_argument, NULL, OPTION_64},
9795 #endif /* OBJ_ELF */
9796   {NULL, no_argument, NULL, 0}
9797 };
9798 size_t md_longopts_size = sizeof (md_longopts);
9799
9800 int
9801 md_parse_option (c, arg)
9802      int c;
9803      char *arg;
9804 {
9805   switch (c)
9806     {
9807     case OPTION_CONSTRUCT_FLOATS:
9808       mips_disable_float_construction = 0;
9809       break;
9810
9811     case OPTION_NO_CONSTRUCT_FLOATS:
9812       mips_disable_float_construction = 1;
9813       break;
9814
9815     case OPTION_TRAP:
9816       mips_trap = 1;
9817       break;
9818
9819     case OPTION_BREAK:
9820       mips_trap = 0;
9821       break;
9822
9823     case OPTION_EB:
9824       target_big_endian = 1;
9825       break;
9826
9827     case OPTION_EL:
9828       target_big_endian = 0;
9829       break;
9830
9831     case 'n':
9832       warn_nops = 1;
9833       break;
9834
9835     case 'O':
9836       if (arg && arg[1] == '0')
9837         mips_optimize = 1;
9838       else
9839         mips_optimize = 2;
9840       break;
9841
9842     case 'g':
9843       if (arg == NULL)
9844         mips_debug = 2;
9845       else
9846         mips_debug = atoi (arg);
9847       /* When the MIPS assembler sees -g or -g2, it does not do
9848          optimizations which limit full symbolic debugging.  We take
9849          that to be equivalent to -O0.  */
9850       if (mips_debug == 2)
9851         mips_optimize = 1;
9852       break;
9853
9854     case OPTION_MIPS1:
9855       mips_opts.isa = ISA_MIPS1;
9856       break;
9857
9858     case OPTION_MIPS2:
9859       mips_opts.isa = ISA_MIPS2;
9860       break;
9861
9862     case OPTION_MIPS3:
9863       mips_opts.isa = ISA_MIPS3;
9864       break;
9865
9866     case OPTION_MIPS4:
9867       mips_opts.isa = ISA_MIPS4;
9868       break;
9869
9870     case OPTION_MIPS5:
9871       mips_opts.isa = ISA_MIPS5;
9872       break;
9873
9874     case OPTION_MIPS32:
9875       mips_opts.isa = ISA_MIPS32;
9876       break;
9877
9878     case OPTION_MIPS64:
9879       mips_opts.isa = ISA_MIPS64;
9880       break;
9881
9882     case OPTION_MTUNE:
9883     case OPTION_MARCH:
9884     case OPTION_MCPU:
9885       {
9886         int cpu = CPU_UNKNOWN;
9887
9888         /* Identify the processor type.  */
9889         if (strcasecmp (arg, "default") != 0)
9890           {
9891             const struct mips_cpu_info *ci;
9892
9893             ci = mips_cpu_info_from_name (arg);
9894             if (ci == NULL || ci->is_isa)
9895               {
9896                 switch (c)
9897                   {
9898                   case OPTION_MTUNE:
9899                     as_fatal (_("invalid architecture -mtune=%s"), arg);
9900                     break;
9901                   case OPTION_MARCH:
9902                     as_fatal (_("invalid architecture -march=%s"), arg);
9903                     break;
9904                   case OPTION_MCPU:
9905                     as_fatal (_("invalid architecture -mcpu=%s"), arg);
9906                     break;
9907                   }
9908               }
9909             else
9910                 cpu = ci->cpu;
9911           }
9912
9913         switch (c)
9914           {
9915           case OPTION_MTUNE:
9916             if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9917               as_warn(_("A different -mtune= was already specified, is now "
9918                         "-mtune=%s"), arg);
9919             mips_tune = cpu;
9920             break;
9921           case OPTION_MARCH:
9922             if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9923               as_warn(_("A different -march= was already specified, is now "
9924                         "-march=%s"), arg);
9925             mips_arch = cpu;
9926             break;
9927           case OPTION_MCPU:
9928             if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9929               as_warn(_("A different -mcpu= was already specified, is now "
9930                         "-mcpu=%s"), arg);
9931             mips_cpu = cpu;
9932           }
9933       }
9934       break;
9935
9936     case OPTION_M4650:
9937       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9938           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9939         as_warn(_("A different -march= or -mtune= was already specified, "
9940                   "is now -m4650"));
9941       mips_arch = CPU_R4650;
9942       mips_tune = CPU_R4650;
9943       break;
9944
9945     case OPTION_NO_M4650:
9946       break;
9947
9948     case OPTION_M4010:
9949       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9950           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9951         as_warn(_("A different -march= or -mtune= was already specified, "
9952                   "is now -m4010"));
9953       mips_arch = CPU_R4010;
9954       mips_tune = CPU_R4010;
9955       break;
9956
9957     case OPTION_NO_M4010:
9958       break;
9959
9960     case OPTION_M4100:
9961       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9962           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9963         as_warn(_("A different -march= or -mtune= was already specified, "
9964                   "is now -m4100"));
9965       mips_arch = CPU_VR4100;
9966       mips_tune = CPU_VR4100;
9967       break;
9968
9969     case OPTION_NO_M4100:
9970       break;
9971
9972     case OPTION_M3900:
9973       if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9974           || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9975         as_warn(_("A different -march= or -mtune= was already specified, "
9976                   "is now -m3900"));
9977       mips_arch = CPU_R3900;
9978       mips_tune = CPU_R3900;
9979       break;
9980
9981     case OPTION_NO_M3900:
9982       break;
9983
9984     case OPTION_MIPS16:
9985       mips_opts.mips16 = 1;
9986       mips_no_prev_insn (false);
9987       break;
9988
9989     case OPTION_NO_MIPS16:
9990       mips_opts.mips16 = 0;
9991       mips_no_prev_insn (false);
9992       break;
9993
9994     case OPTION_MIPS3D:
9995       mips_opts.ase_mips3d = 1;
9996       break;
9997
9998     case OPTION_NO_MIPS3D:
9999       mips_opts.ase_mips3d = 0;
10000       break;
10001
10002     case OPTION_MEMBEDDED_PIC:
10003       mips_pic = EMBEDDED_PIC;
10004       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10005         {
10006           as_bad (_("-G may not be used with embedded PIC code"));
10007           return 0;
10008         }
10009       g_switch_value = 0x7fffffff;
10010       break;
10011
10012 #ifdef OBJ_ELF
10013       /* When generating ELF code, we permit -KPIC and -call_shared to
10014          select SVR4_PIC, and -non_shared to select no PIC.  This is
10015          intended to be compatible with Irix 5.  */
10016     case OPTION_CALL_SHARED:
10017       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10018         {
10019           as_bad (_("-call_shared is supported only for ELF format"));
10020           return 0;
10021         }
10022       mips_pic = SVR4_PIC;
10023       if (g_switch_seen && g_switch_value != 0)
10024         {
10025           as_bad (_("-G may not be used with SVR4 PIC code"));
10026           return 0;
10027         }
10028       g_switch_value = 0;
10029       break;
10030
10031     case OPTION_NON_SHARED:
10032       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10033         {
10034           as_bad (_("-non_shared is supported only for ELF format"));
10035           return 0;
10036         }
10037       mips_pic = NO_PIC;
10038       break;
10039
10040       /* The -xgot option tells the assembler to use 32 offsets when
10041          accessing the got in SVR4_PIC mode.  It is for Irix
10042          compatibility.  */
10043     case OPTION_XGOT:
10044       mips_big_got = 1;
10045       break;
10046 #endif /* OBJ_ELF */
10047
10048     case 'G':
10049       if (! USE_GLOBAL_POINTER_OPT)
10050         {
10051           as_bad (_("-G is not supported for this configuration"));
10052           return 0;
10053         }
10054       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10055         {
10056           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10057           return 0;
10058         }
10059       else
10060         g_switch_value = atoi (arg);
10061       g_switch_seen = 1;
10062       break;
10063
10064 #ifdef OBJ_ELF
10065       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10066          and -mabi=64.  */
10067     case OPTION_32:
10068       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10069         {
10070           as_bad (_("-32 is supported for ELF format only"));
10071           return 0;
10072         }
10073       mips_opts.abi = O32_ABI;
10074       break;
10075
10076     case OPTION_N32:
10077       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10078         {
10079           as_bad (_("-n32 is supported for ELF format only"));
10080           return 0;
10081         }
10082       mips_opts.abi = N32_ABI;
10083       break;
10084
10085     case OPTION_64:
10086       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10087         {
10088           as_bad (_("-64 is supported for ELF format only"));
10089           return 0;
10090         }
10091       mips_opts.abi = N64_ABI;
10092       if (! support_64bit_objects())
10093         as_fatal (_("No compiled in support for 64 bit object file format"));
10094       break;
10095 #endif /* OBJ_ELF */
10096
10097     case OPTION_GP32:
10098       file_mips_gp32 = 1;
10099       if (mips_opts.abi != O32_ABI)
10100         mips_opts.abi = NO_ABI;
10101       break;
10102
10103     case OPTION_GP64:
10104       file_mips_gp32 = 0;
10105       if (mips_opts.abi == O32_ABI)
10106         mips_opts.abi = NO_ABI;
10107       break;
10108
10109     case OPTION_FP32:
10110       file_mips_fp32 = 1;
10111       if (mips_opts.abi != O32_ABI)
10112         mips_opts.abi = NO_ABI;
10113       break;
10114
10115 #ifdef OBJ_ELF
10116     case OPTION_MABI:
10117       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10118         {
10119           as_bad (_("-mabi is supported for ELF format only"));
10120           return 0;
10121         }
10122       if (strcmp (arg, "32") == 0)
10123         mips_opts.abi = O32_ABI;
10124       else if (strcmp (arg, "o64") == 0)
10125         mips_opts.abi = O64_ABI;
10126       else if (strcmp (arg, "n32") == 0)
10127         mips_opts.abi = N32_ABI;
10128       else if (strcmp (arg, "64") == 0)
10129         {
10130           mips_opts.abi = N64_ABI;
10131           if (! support_64bit_objects())
10132             as_fatal (_("No compiled in support for 64 bit object file "
10133                         "format"));
10134         }
10135       else if (strcmp (arg, "eabi") == 0)
10136         mips_opts.abi = EABI_ABI;
10137       else
10138         {
10139           as_fatal (_("invalid abi -mabi=%s"), arg);
10140           return 0;
10141         }
10142       break;
10143 #endif /* OBJ_ELF */
10144
10145     case OPTION_M7000_HILO_FIX:
10146       mips_7000_hilo_fix = true;
10147       break;
10148
10149     case OPTION_NO_M7000_HILO_FIX:
10150       mips_7000_hilo_fix = false;
10151       break;
10152
10153     default:
10154       return 0;
10155     }
10156
10157   return 1;
10158 }
10159
10160 static void
10161 show (stream, string, col_p, first_p)
10162      FILE *stream;
10163      char *string;
10164      int *col_p;
10165      int *first_p;
10166 {
10167   if (*first_p)
10168     {
10169       fprintf (stream, "%24s", "");
10170       *col_p = 24;
10171     }
10172   else
10173     {
10174       fprintf (stream, ", ");
10175       *col_p += 2;
10176     }
10177
10178   if (*col_p + strlen (string) > 72)
10179     {
10180       fprintf (stream, "\n%24s", "");
10181       *col_p = 24;
10182     }
10183
10184   fprintf (stream, "%s", string);
10185   *col_p += strlen (string);
10186
10187   *first_p = 0;
10188 }
10189
10190 void
10191 md_show_usage (stream)
10192      FILE *stream;
10193 {
10194   int column, first;
10195
10196   fprintf (stream, _("\
10197 MIPS options:\n\
10198 -membedded-pic          generate embedded position independent code\n\
10199 -EB                     generate big endian output\n\
10200 -EL                     generate little endian output\n\
10201 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
10202 -G NUM                  allow referencing objects up to NUM bytes\n\
10203                         implicitly with the gp register [default 8]\n"));
10204   fprintf (stream, _("\
10205 -mips1                  generate MIPS ISA I instructions\n\
10206 -mips2                  generate MIPS ISA II instructions\n\
10207 -mips3                  generate MIPS ISA III instructions\n\
10208 -mips4                  generate MIPS ISA IV instructions\n\
10209 -mips5                  generate MIPS ISA V instructions\n\
10210 -mips32                 generate MIPS32 ISA instructions\n\
10211 -mips64                 generate MIPS64 ISA instructions\n\
10212 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
10213
10214   first = 1;
10215
10216   show (stream, "2000", &column, &first);
10217   show (stream, "3000", &column, &first);
10218   show (stream, "3900", &column, &first);
10219   show (stream, "4000", &column, &first);
10220   show (stream, "4010", &column, &first);
10221   show (stream, "4100", &column, &first);
10222   show (stream, "4111", &column, &first);
10223   show (stream, "4300", &column, &first);
10224   show (stream, "4400", &column, &first);
10225   show (stream, "4600", &column, &first);
10226   show (stream, "4650", &column, &first);
10227   show (stream, "5000", &column, &first);
10228   show (stream, "5200", &column, &first);
10229   show (stream, "5230", &column, &first);
10230   show (stream, "5231", &column, &first);
10231   show (stream, "5261", &column, &first);
10232   show (stream, "5721", &column, &first);
10233   show (stream, "6000", &column, &first);
10234   show (stream, "8000", &column, &first);
10235   show (stream, "10000", &column, &first);
10236   show (stream, "12000", &column, &first);
10237   show (stream, "sb1", &column, &first);
10238   fputc ('\n', stream);
10239
10240   fprintf (stream, _("\
10241 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10242 -no-mCPU                don't generate code specific to CPU.\n\
10243                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
10244
10245   first = 1;
10246
10247   show (stream, "3900", &column, &first);
10248   show (stream, "4010", &column, &first);
10249   show (stream, "4100", &column, &first);
10250   show (stream, "4650", &column, &first);
10251   fputc ('\n', stream);
10252
10253   fprintf (stream, _("\
10254 -mips16                 generate mips16 instructions\n\
10255 -no-mips16              do not generate mips16 instructions\n"));
10256   fprintf (stream, _("\
10257 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
10258 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
10259 -O0                     remove unneeded NOPs, do not swap branches\n\
10260 -O                      remove unneeded NOPs and swap branches\n\
10261 -n                      warn about NOPs generated from macros\n\
10262 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10263 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
10264 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
10265 #ifdef OBJ_ELF
10266   fprintf (stream, _("\
10267 -KPIC, -call_shared     generate SVR4 position independent code\n\
10268 -non_shared             do not generate position independent code\n\
10269 -xgot                   assume a 32 bit GOT\n\
10270 -mabi=ABI               create ABI conformant object file for:\n"));
10271
10272   first = 1;
10273
10274   show (stream, "32", &column, &first);
10275   show (stream, "o64", &column, &first);
10276   show (stream, "n32", &column, &first);
10277   show (stream, "64", &column, &first);
10278   show (stream, "eabi", &column, &first);
10279
10280   fputc ('\n', stream);
10281
10282   fprintf (stream, _("\
10283 -32                     create o32 ABI object file (default)\n\
10284 -n32                    create n32 ABI object file\n\
10285 -64                     create 64 ABI object file\n"));
10286 #endif
10287 }
10288 \f
10289 void
10290 mips_init_after_args ()
10291 {
10292   /* initialize opcodes */
10293   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10294   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10295 }
10296
10297 long
10298 md_pcrel_from (fixP)
10299      fixS *fixP;
10300 {
10301   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10302       && fixP->fx_addsy != (symbolS *) NULL
10303       && ! S_IS_DEFINED (fixP->fx_addsy))
10304     {
10305       /* This makes a branch to an undefined symbol be a branch to the
10306          current location.  */
10307       if (mips_pic == EMBEDDED_PIC)
10308         return 4;
10309       else
10310         return 1;
10311     }
10312
10313   /* return the address of the delay slot */
10314   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10315 }
10316
10317 /* This is called before the symbol table is processed.  In order to
10318    work with gcc when using mips-tfile, we must keep all local labels.
10319    However, in other cases, we want to discard them.  If we were
10320    called with -g, but we didn't see any debugging information, it may
10321    mean that gcc is smuggling debugging information through to
10322    mips-tfile, in which case we must generate all local labels.  */
10323
10324 void
10325 mips_frob_file_before_adjust ()
10326 {
10327 #ifndef NO_ECOFF_DEBUGGING
10328   if (ECOFF_DEBUGGING
10329       && mips_debug != 0
10330       && ! ecoff_debugging_seen)
10331     flag_keep_locals = 1;
10332 #endif
10333 }
10334
10335 /* Sort any unmatched HI16_S relocs so that they immediately precede
10336    the corresponding LO reloc.  This is called before md_apply_fix3 and
10337    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10338    explicit use of the %hi modifier.  */
10339
10340 void
10341 mips_frob_file ()
10342 {
10343   struct mips_hi_fixup *l;
10344
10345   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10346     {
10347       segment_info_type *seginfo;
10348       int pass;
10349
10350       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10351
10352       /* Check quickly whether the next fixup happens to be a matching
10353          %lo.  */
10354       if (l->fixp->fx_next != NULL
10355           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10356           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10357           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10358         continue;
10359
10360       /* Look through the fixups for this segment for a matching %lo.
10361          When we find one, move the %hi just in front of it.  We do
10362          this in two passes.  In the first pass, we try to find a
10363          unique %lo.  In the second pass, we permit multiple %hi
10364          relocs for a single %lo (this is a GNU extension).  */
10365       seginfo = seg_info (l->seg);
10366       for (pass = 0; pass < 2; pass++)
10367         {
10368           fixS *f, *prev;
10369
10370           prev = NULL;
10371           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10372             {
10373               /* Check whether this is a %lo fixup which matches l->fixp.  */
10374               if (f->fx_r_type == BFD_RELOC_LO16
10375                   && f->fx_addsy == l->fixp->fx_addsy
10376                   && f->fx_offset == l->fixp->fx_offset
10377                   && (pass == 1
10378                       || prev == NULL
10379                       || prev->fx_r_type != BFD_RELOC_HI16_S
10380                       || prev->fx_addsy != f->fx_addsy
10381                       || prev->fx_offset !=  f->fx_offset))
10382                 {
10383                   fixS **pf;
10384
10385                   /* Move l->fixp before f.  */
10386                   for (pf = &seginfo->fix_root;
10387                        *pf != l->fixp;
10388                        pf = &(*pf)->fx_next)
10389                     assert (*pf != NULL);
10390
10391                   *pf = l->fixp->fx_next;
10392
10393                   l->fixp->fx_next = f;
10394                   if (prev == NULL)
10395                     seginfo->fix_root = l->fixp;
10396                   else
10397                     prev->fx_next = l->fixp;
10398
10399                   break;
10400                 }
10401
10402               prev = f;
10403             }
10404
10405           if (f != NULL)
10406             break;
10407
10408 #if 0 /* GCC code motion plus incomplete dead code elimination
10409          can leave a %hi without a %lo.  */
10410           if (pass == 1)
10411             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10412                            _("Unmatched %%hi reloc"));
10413 #endif
10414         }
10415     }
10416 }
10417
10418 /* When generating embedded PIC code we need to use a special
10419    relocation to represent the difference of two symbols in the .text
10420    section (switch tables use a difference of this sort).  See
10421    include/coff/mips.h for details.  This macro checks whether this
10422    fixup requires the special reloc.  */
10423 #define SWITCH_TABLE(fixp) \
10424   ((fixp)->fx_r_type == BFD_RELOC_32 \
10425    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10426    && (fixp)->fx_addsy != NULL \
10427    && (fixp)->fx_subsy != NULL \
10428    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10429    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10430
10431 /* When generating embedded PIC code we must keep all PC relative
10432    relocations, in case the linker has to relax a call.  We also need
10433    to keep relocations for switch table entries.
10434
10435    We may have combined relocations without symbols in the N32/N64 ABI.
10436    We have to prevent gas from dropping them.  */
10437
10438 int
10439 mips_force_relocation (fixp)
10440      fixS *fixp;
10441 {
10442   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10443       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10444     return 1;
10445
10446   if (HAVE_NEWABI
10447       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10448       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10449           || fixp->fx_r_type == BFD_RELOC_HI16_S
10450           || fixp->fx_r_type == BFD_RELOC_LO16))
10451     return 1;
10452
10453   return (mips_pic == EMBEDDED_PIC
10454           && (fixp->fx_pcrel
10455               || SWITCH_TABLE (fixp)
10456               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10457               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10458 }
10459
10460 #ifdef OBJ_ELF
10461 static int
10462 mips_need_elf_addend_fixup (fixP)
10463      fixS *fixP;
10464 {
10465   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10466     return 1;
10467   if (mips_pic == EMBEDDED_PIC
10468       && S_IS_WEAK (fixP->fx_addsy))
10469     return 1;
10470   if (mips_pic != EMBEDDED_PIC
10471       && (S_IS_WEAK (fixP->fx_addsy)
10472           || S_IS_EXTERN (fixP->fx_addsy))
10473       && !S_IS_COMMON (fixP->fx_addsy))
10474     return 1;
10475   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10476       && (((bfd_get_section_flags (stdoutput,
10477                                    S_GET_SEGMENT (fixP->fx_addsy))
10478             & SEC_LINK_ONCE) != 0)
10479           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10480                        ".gnu.linkonce",
10481                        sizeof (".gnu.linkonce") - 1)))
10482     return 1;
10483   return 0;
10484 }
10485 #endif
10486
10487 /* Apply a fixup to the object file.  */
10488
10489 void
10490 md_apply_fix3 (fixP, valP, seg)
10491      fixS *fixP;
10492      valueT * valP;
10493      segT seg ATTRIBUTE_UNUSED;
10494 {
10495   bfd_byte *buf;
10496   long insn;
10497   valueT value;
10498
10499   assert (fixP->fx_size == 4
10500           || fixP->fx_r_type == BFD_RELOC_16
10501           || fixP->fx_r_type == BFD_RELOC_32
10502           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10503           || fixP->fx_r_type == BFD_RELOC_HI16_S
10504           || fixP->fx_r_type == BFD_RELOC_LO16
10505           || fixP->fx_r_type == BFD_RELOC_GPREL16
10506           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10507           || fixP->fx_r_type == BFD_RELOC_GPREL32
10508           || fixP->fx_r_type == BFD_RELOC_64
10509           || fixP->fx_r_type == BFD_RELOC_CTOR
10510           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10511           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10512           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10513           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10514           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10515           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10516           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10517           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10518
10519   value = * valP;
10520
10521   /* If we aren't adjusting this fixup to be against the section
10522      symbol, we need to adjust the value.  */
10523 #ifdef OBJ_ELF
10524   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10525     {
10526       if (mips_need_elf_addend_fixup (fixP))
10527         {
10528           valueT symval = S_GET_VALUE (fixP->fx_addsy);
10529
10530           value -= symval;
10531           if (value != 0 && ! fixP->fx_pcrel)
10532             {
10533               /* In this case, the bfd_install_relocation routine will
10534                  incorrectly add the symbol value back in.  We just want
10535                  the addend to appear in the object file.  */
10536               value -= symval;
10537
10538               /* Make sure the addend is still non-zero.  If it became zero
10539                  after the last operation, set it to a spurious value and
10540                  subtract the same value from the object file's contents.  */
10541               if (value == 0)
10542                 {
10543                   value = 8;
10544
10545                   /* The in-place addends for LO16 relocations are signed;
10546                      leave the matching HI16 in-place addends as zero.  */
10547                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10548                     {
10549                       reloc_howto_type *howto;
10550                       bfd_vma contents, mask, field;
10551
10552                       howto = bfd_reloc_type_lookup (stdoutput,
10553                                                      fixP->fx_r_type);
10554
10555                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
10556                                                + fixP->fx_where,
10557                                                fixP->fx_size * 8,
10558                                                target_big_endian);
10559
10560                       /* MASK has bits set where the relocation should go.
10561                          FIELD is -value, shifted into the appropriate place
10562                          for this relocation.  */
10563                       mask = 1 << (howto->bitsize - 1);
10564                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10565                       field = (-value >> howto->rightshift) << howto->bitpos;
10566
10567                       bfd_put_bits ((field & mask) | (contents & ~mask),
10568                                     fixP->fx_frag->fr_literal + fixP->fx_where,
10569                                     fixP->fx_size * 8,
10570                                     target_big_endian);
10571                     }
10572                 }
10573             }
10574         }
10575
10576       /* This code was generated using trial and error and so is
10577          fragile and not trustworthy.  If you change it, you should
10578          rerun the elf-rel, elf-rel2, and empic testcases and ensure
10579          they still pass.  */
10580       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10581         {
10582           value += fixP->fx_frag->fr_address + fixP->fx_where;
10583
10584           /* BFD's REL handling, for MIPS, is _very_ weird.
10585              This gives the right results, but it can't possibly
10586              be the way things are supposed to work.  */
10587           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10588                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10589               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10590             value += fixP->fx_frag->fr_address + fixP->fx_where;
10591         }
10592     }
10593 #endif
10594
10595   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
10596
10597   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10598     fixP->fx_done = 1;
10599
10600   switch (fixP->fx_r_type)
10601     {
10602     case BFD_RELOC_MIPS_JMP:
10603     case BFD_RELOC_MIPS_SHIFT5:
10604     case BFD_RELOC_MIPS_SHIFT6:
10605     case BFD_RELOC_MIPS_GOT_DISP:
10606     case BFD_RELOC_MIPS_GOT_PAGE:
10607     case BFD_RELOC_MIPS_GOT_OFST:
10608     case BFD_RELOC_MIPS_SUB:
10609     case BFD_RELOC_MIPS_INSERT_A:
10610     case BFD_RELOC_MIPS_INSERT_B:
10611     case BFD_RELOC_MIPS_DELETE:
10612     case BFD_RELOC_MIPS_HIGHEST:
10613     case BFD_RELOC_MIPS_HIGHER:
10614     case BFD_RELOC_MIPS_SCN_DISP:
10615     case BFD_RELOC_MIPS_REL16:
10616     case BFD_RELOC_MIPS_RELGOT:
10617     case BFD_RELOC_MIPS_JALR:
10618     case BFD_RELOC_HI16:
10619     case BFD_RELOC_HI16_S:
10620     case BFD_RELOC_GPREL16:
10621     case BFD_RELOC_MIPS_LITERAL:
10622     case BFD_RELOC_MIPS_CALL16:
10623     case BFD_RELOC_MIPS_GOT16:
10624     case BFD_RELOC_GPREL32:
10625     case BFD_RELOC_MIPS_GOT_HI16:
10626     case BFD_RELOC_MIPS_GOT_LO16:
10627     case BFD_RELOC_MIPS_CALL_HI16:
10628     case BFD_RELOC_MIPS_CALL_LO16:
10629     case BFD_RELOC_MIPS16_GPREL:
10630       if (fixP->fx_pcrel)
10631         as_bad_where (fixP->fx_file, fixP->fx_line,
10632                       _("Invalid PC relative reloc"));
10633       /* Nothing needed to do. The value comes from the reloc entry */
10634       break;
10635
10636     case BFD_RELOC_MIPS16_JMP:
10637       /* We currently always generate a reloc against a symbol, which
10638          means that we don't want an addend even if the symbol is
10639          defined.  */
10640       fixP->fx_addnumber = 0;
10641       break;
10642
10643     case BFD_RELOC_PCREL_HI16_S:
10644       /* The addend for this is tricky if it is internal, so we just
10645          do everything here rather than in bfd_install_relocation.  */
10646       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10647           && !fixP->fx_done
10648           && value != 0)
10649         break;
10650       if (fixP->fx_addsy
10651           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10652         {
10653           /* For an external symbol adjust by the address to make it
10654              pcrel_offset.  We use the address of the RELLO reloc
10655              which follows this one.  */
10656           value += (fixP->fx_next->fx_frag->fr_address
10657                     + fixP->fx_next->fx_where);
10658         }
10659       value = ((value + 0x8000) >> 16) & 0xffff;
10660       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10661       if (target_big_endian)
10662         buf += 2;
10663       md_number_to_chars ((char *) buf, value, 2);
10664       break;
10665
10666     case BFD_RELOC_PCREL_LO16:
10667       /* The addend for this is tricky if it is internal, so we just
10668          do everything here rather than in bfd_install_relocation.  */
10669       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10670           && !fixP->fx_done
10671           && value != 0)
10672         break;
10673       if (fixP->fx_addsy
10674           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10675         value += fixP->fx_frag->fr_address + fixP->fx_where;
10676       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10677       if (target_big_endian)
10678         buf += 2;
10679       md_number_to_chars ((char *) buf, value, 2);
10680       break;
10681
10682     case BFD_RELOC_64:
10683       /* This is handled like BFD_RELOC_32, but we output a sign
10684          extended value if we are only 32 bits.  */
10685       if (fixP->fx_done
10686           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10687         {
10688           if (8 <= sizeof (valueT))
10689             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10690                                 value, 8);
10691           else
10692             {
10693               long w1, w2;
10694               long hiv;
10695
10696               w1 = w2 = fixP->fx_where;
10697               if (target_big_endian)
10698                 w1 += 4;
10699               else
10700                 w2 += 4;
10701               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10702               if ((value & 0x80000000) != 0)
10703                 hiv = 0xffffffff;
10704               else
10705                 hiv = 0;
10706               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10707             }
10708         }
10709       break;
10710
10711     case BFD_RELOC_RVA:
10712     case BFD_RELOC_32:
10713       /* If we are deleting this reloc entry, we must fill in the
10714          value now.  This can happen if we have a .word which is not
10715          resolved when it appears but is later defined.  We also need
10716          to fill in the value if this is an embedded PIC switch table
10717          entry.  */
10718       if (fixP->fx_done
10719           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10720         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10721                             value, 4);
10722       break;
10723
10724     case BFD_RELOC_16:
10725       /* If we are deleting this reloc entry, we must fill in the
10726          value now.  */
10727       assert (fixP->fx_size == 2);
10728       if (fixP->fx_done)
10729         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10730                             value, 2);
10731       break;
10732
10733     case BFD_RELOC_LO16:
10734       /* When handling an embedded PIC switch statement, we can wind
10735          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
10736       if (fixP->fx_done)
10737         {
10738           if (value + 0x8000 > 0xffff)
10739             as_bad_where (fixP->fx_file, fixP->fx_line,
10740                           _("relocation overflow"));
10741           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
10742           if (target_big_endian)
10743             buf += 2;
10744           md_number_to_chars ((char *) buf, value, 2);
10745         }
10746       break;
10747
10748     case BFD_RELOC_16_PCREL_S2:
10749       if ((value & 0x3) != 0)
10750         as_bad_where (fixP->fx_file, fixP->fx_line,
10751                       _("Branch to odd address (%lx)"), (long) value);
10752
10753       /* Fall through.  */
10754
10755     case BFD_RELOC_16_PCREL:
10756       /*
10757        * We need to save the bits in the instruction since fixup_segment()
10758        * might be deleting the relocation entry (i.e., a branch within
10759        * the current segment).
10760        */
10761       if (!fixP->fx_done && value != 0)
10762         break;
10763       /* If 'value' is zero, the remaining reloc code won't actually
10764          do the store, so it must be done here.  This is probably
10765          a bug somewhere.  */
10766       if (!fixP->fx_done
10767           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10768               || fixP->fx_addsy == NULL                 /* ??? */
10769               || ! S_IS_DEFINED (fixP->fx_addsy)))
10770         value -= fixP->fx_frag->fr_address + fixP->fx_where;
10771
10772       value = (offsetT) value >> 2;
10773
10774       /* update old instruction data */
10775       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10776       if (target_big_endian)
10777         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10778       else
10779         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10780
10781       if (value + 0x8000 <= 0xffff)
10782         insn |= value & 0xffff;
10783       else
10784         {
10785           /* The branch offset is too large.  If this is an
10786              unconditional branch, and we are not generating PIC code,
10787              we can convert it to an absolute jump instruction.  */
10788           if (mips_pic == NO_PIC
10789               && fixP->fx_done
10790               && fixP->fx_frag->fr_address >= text_section->vma
10791               && (fixP->fx_frag->fr_address
10792                   < text_section->vma + text_section->_raw_size)
10793               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
10794                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
10795                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10796             {
10797               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
10798                 insn = 0x0c000000;      /* jal */
10799               else
10800                 insn = 0x08000000;      /* j */
10801               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10802               fixP->fx_done = 0;
10803               fixP->fx_addsy = section_symbol (text_section);
10804               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10805             }
10806           else
10807             {
10808               /* FIXME.  It would be possible in principle to handle
10809                  conditional branches which overflow.  They could be
10810                  transformed into a branch around a jump.  This would
10811                  require setting up variant frags for each different
10812                  branch type.  The native MIPS assembler attempts to
10813                  handle these cases, but it appears to do it
10814                  incorrectly.  */
10815               as_bad_where (fixP->fx_file, fixP->fx_line,
10816                             _("Branch out of range"));
10817             }
10818         }
10819
10820       md_number_to_chars ((char *) buf, (valueT) insn, 4);
10821       break;
10822
10823     case BFD_RELOC_VTABLE_INHERIT:
10824       fixP->fx_done = 0;
10825       if (fixP->fx_addsy
10826           && !S_IS_DEFINED (fixP->fx_addsy)
10827           && !S_IS_WEAK (fixP->fx_addsy))
10828         S_SET_WEAK (fixP->fx_addsy);
10829       break;
10830
10831     case BFD_RELOC_VTABLE_ENTRY:
10832       fixP->fx_done = 0;
10833       break;
10834
10835     default:
10836       internalError ();
10837     }
10838 }
10839
10840 #if 0
10841 void
10842 printInsn (oc)
10843      unsigned long oc;
10844 {
10845   const struct mips_opcode *p;
10846   int treg, sreg, dreg, shamt;
10847   short imm;
10848   const char *args;
10849   int i;
10850
10851   for (i = 0; i < NUMOPCODES; ++i)
10852     {
10853       p = &mips_opcodes[i];
10854       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10855         {
10856           printf ("%08lx %s\t", oc, p->name);
10857           treg = (oc >> 16) & 0x1f;
10858           sreg = (oc >> 21) & 0x1f;
10859           dreg = (oc >> 11) & 0x1f;
10860           shamt = (oc >> 6) & 0x1f;
10861           imm = oc;
10862           for (args = p->args;; ++args)
10863             {
10864               switch (*args)
10865                 {
10866                 case '\0':
10867                   printf ("\n");
10868                   break;
10869
10870                 case ',':
10871                 case '(':
10872                 case ')':
10873                   printf ("%c", *args);
10874                   continue;
10875
10876                 case 'r':
10877                   assert (treg == sreg);
10878                   printf ("$%d,$%d", treg, sreg);
10879                   continue;
10880
10881                 case 'd':
10882                 case 'G':
10883                   printf ("$%d", dreg);
10884                   continue;
10885
10886                 case 't':
10887                 case 'E':
10888                   printf ("$%d", treg);
10889                   continue;
10890
10891                 case 'k':
10892                   printf ("0x%x", treg);
10893                   continue;
10894
10895                 case 'b':
10896                 case 's':
10897                   printf ("$%d", sreg);
10898                   continue;
10899
10900                 case 'a':
10901                   printf ("0x%08lx", oc & 0x1ffffff);
10902                   continue;
10903
10904                 case 'i':
10905                 case 'j':
10906                 case 'o':
10907                 case 'u':
10908                   printf ("%d", imm);
10909                   continue;
10910
10911                 case '<':
10912                 case '>':
10913                   printf ("$%d", shamt);
10914                   continue;
10915
10916                 default:
10917                   internalError ();
10918                 }
10919               break;
10920             }
10921           return;
10922         }
10923     }
10924   printf (_("%08lx  UNDEFINED\n"), oc);
10925 }
10926 #endif
10927
10928 static symbolS *
10929 get_symbol ()
10930 {
10931   int c;
10932   char *name;
10933   symbolS *p;
10934
10935   name = input_line_pointer;
10936   c = get_symbol_end ();
10937   p = (symbolS *) symbol_find_or_make (name);
10938   *input_line_pointer = c;
10939   return p;
10940 }
10941
10942 /* Align the current frag to a given power of two.  The MIPS assembler
10943    also automatically adjusts any preceding label.  */
10944
10945 static void
10946 mips_align (to, fill, label)
10947      int to;
10948      int fill;
10949      symbolS *label;
10950 {
10951   mips_emit_delays (false);
10952   frag_align (to, fill, 0);
10953   record_alignment (now_seg, to);
10954   if (label != NULL)
10955     {
10956       assert (S_GET_SEGMENT (label) == now_seg);
10957       symbol_set_frag (label, frag_now);
10958       S_SET_VALUE (label, (valueT) frag_now_fix ());
10959     }
10960 }
10961
10962 /* Align to a given power of two.  .align 0 turns off the automatic
10963    alignment used by the data creating pseudo-ops.  */
10964
10965 static void
10966 s_align (x)
10967      int x ATTRIBUTE_UNUSED;
10968 {
10969   register int temp;
10970   register long temp_fill;
10971   long max_alignment = 15;
10972
10973   /*
10974
10975     o  Note that the assembler pulls down any immediately preceeding label
10976        to the aligned address.
10977     o  It's not documented but auto alignment is reinstated by
10978        a .align pseudo instruction.
10979     o  Note also that after auto alignment is turned off the mips assembler
10980        issues an error on attempt to assemble an improperly aligned data item.
10981        We don't.
10982
10983     */
10984
10985   temp = get_absolute_expression ();
10986   if (temp > max_alignment)
10987     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10988   else if (temp < 0)
10989     {
10990       as_warn (_("Alignment negative: 0 assumed."));
10991       temp = 0;
10992     }
10993   if (*input_line_pointer == ',')
10994     {
10995       input_line_pointer++;
10996       temp_fill = get_absolute_expression ();
10997     }
10998   else
10999     temp_fill = 0;
11000   if (temp)
11001     {
11002       auto_align = 1;
11003       mips_align (temp, (int) temp_fill,
11004                   insn_labels != NULL ? insn_labels->label : NULL);
11005     }
11006   else
11007     {
11008       auto_align = 0;
11009     }
11010
11011   demand_empty_rest_of_line ();
11012 }
11013
11014 void
11015 mips_flush_pending_output ()
11016 {
11017   mips_emit_delays (false);
11018   mips_clear_insn_labels ();
11019 }
11020
11021 static void
11022 s_change_sec (sec)
11023      int sec;
11024 {
11025   segT seg;
11026
11027   /* When generating embedded PIC code, we only use the .text, .lit8,
11028      .sdata and .sbss sections.  We change the .data and .rdata
11029      pseudo-ops to use .sdata.  */
11030   if (mips_pic == EMBEDDED_PIC
11031       && (sec == 'd' || sec == 'r'))
11032     sec = 's';
11033
11034 #ifdef OBJ_ELF
11035   /* The ELF backend needs to know that we are changing sections, so
11036      that .previous works correctly.  We could do something like check
11037      for an obj_section_change_hook macro, but that might be confusing
11038      as it would not be appropriate to use it in the section changing
11039      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11040      This should be cleaner, somehow.  */
11041   obj_elf_section_change_hook ();
11042 #endif
11043
11044   mips_emit_delays (false);
11045   switch (sec)
11046     {
11047     case 't':
11048       s_text (0);
11049       break;
11050     case 'd':
11051       s_data (0);
11052       break;
11053     case 'b':
11054       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11055       demand_empty_rest_of_line ();
11056       break;
11057
11058     case 'r':
11059       if (USE_GLOBAL_POINTER_OPT)
11060         {
11061           seg = subseg_new (RDATA_SECTION_NAME,
11062                             (subsegT) get_absolute_expression ());
11063           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11064             {
11065               bfd_set_section_flags (stdoutput, seg,
11066                                      (SEC_ALLOC
11067                                       | SEC_LOAD
11068                                       | SEC_READONLY
11069                                       | SEC_RELOC
11070                                       | SEC_DATA));
11071               if (strcmp (TARGET_OS, "elf") != 0)
11072                 record_alignment (seg, 4);
11073             }
11074           demand_empty_rest_of_line ();
11075         }
11076       else
11077         {
11078           as_bad (_("No read only data section in this object file format"));
11079           demand_empty_rest_of_line ();
11080           return;
11081         }
11082       break;
11083
11084     case 's':
11085       if (USE_GLOBAL_POINTER_OPT)
11086         {
11087           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11088           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11089             {
11090               bfd_set_section_flags (stdoutput, seg,
11091                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11092                                      | SEC_DATA);
11093               if (strcmp (TARGET_OS, "elf") != 0)
11094                 record_alignment (seg, 4);
11095             }
11096           demand_empty_rest_of_line ();
11097           break;
11098         }
11099       else
11100         {
11101           as_bad (_("Global pointers not supported; recompile -G 0"));
11102           demand_empty_rest_of_line ();
11103           return;
11104         }
11105     }
11106
11107   auto_align = 1;
11108 }
11109
11110 void
11111 mips_enable_auto_align ()
11112 {
11113   auto_align = 1;
11114 }
11115
11116 static void
11117 s_cons (log_size)
11118      int log_size;
11119 {
11120   symbolS *label;
11121
11122   label = insn_labels != NULL ? insn_labels->label : NULL;
11123   mips_emit_delays (false);
11124   if (log_size > 0 && auto_align)
11125     mips_align (log_size, 0, label);
11126   mips_clear_insn_labels ();
11127   cons (1 << log_size);
11128 }
11129
11130 static void
11131 s_float_cons (type)
11132      int type;
11133 {
11134   symbolS *label;
11135
11136   label = insn_labels != NULL ? insn_labels->label : NULL;
11137
11138   mips_emit_delays (false);
11139
11140   if (auto_align)
11141     {
11142       if (type == 'd')
11143         mips_align (3, 0, label);
11144       else
11145         mips_align (2, 0, label);
11146     }
11147
11148   mips_clear_insn_labels ();
11149
11150   float_cons (type);
11151 }
11152
11153 /* Handle .globl.  We need to override it because on Irix 5 you are
11154    permitted to say
11155        .globl foo .text
11156    where foo is an undefined symbol, to mean that foo should be
11157    considered to be the address of a function.  */
11158
11159 static void
11160 s_mips_globl (x)
11161      int x ATTRIBUTE_UNUSED;
11162 {
11163   char *name;
11164   int c;
11165   symbolS *symbolP;
11166   flagword flag;
11167
11168   name = input_line_pointer;
11169   c = get_symbol_end ();
11170   symbolP = symbol_find_or_make (name);
11171   *input_line_pointer = c;
11172   SKIP_WHITESPACE ();
11173
11174   /* On Irix 5, every global symbol that is not explicitly labelled as
11175      being a function is apparently labelled as being an object.  */
11176   flag = BSF_OBJECT;
11177
11178   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11179     {
11180       char *secname;
11181       asection *sec;
11182
11183       secname = input_line_pointer;
11184       c = get_symbol_end ();
11185       sec = bfd_get_section_by_name (stdoutput, secname);
11186       if (sec == NULL)
11187         as_bad (_("%s: no such section"), secname);
11188       *input_line_pointer = c;
11189
11190       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11191         flag = BSF_FUNCTION;
11192     }
11193
11194   symbol_get_bfdsym (symbolP)->flags |= flag;
11195
11196   S_SET_EXTERNAL (symbolP);
11197   demand_empty_rest_of_line ();
11198 }
11199
11200 static void
11201 s_option (x)
11202      int x ATTRIBUTE_UNUSED;
11203 {
11204   char *opt;
11205   char c;
11206
11207   opt = input_line_pointer;
11208   c = get_symbol_end ();
11209
11210   if (*opt == 'O')
11211     {
11212       /* FIXME: What does this mean?  */
11213     }
11214   else if (strncmp (opt, "pic", 3) == 0)
11215     {
11216       int i;
11217
11218       i = atoi (opt + 3);
11219       if (i == 0)
11220         mips_pic = NO_PIC;
11221       else if (i == 2)
11222         mips_pic = SVR4_PIC;
11223       else
11224         as_bad (_(".option pic%d not supported"), i);
11225
11226       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11227         {
11228           if (g_switch_seen && g_switch_value != 0)
11229             as_warn (_("-G may not be used with SVR4 PIC code"));
11230           g_switch_value = 0;
11231           bfd_set_gp_size (stdoutput, 0);
11232         }
11233     }
11234   else
11235     as_warn (_("Unrecognized option \"%s\""), opt);
11236
11237   *input_line_pointer = c;
11238   demand_empty_rest_of_line ();
11239 }
11240
11241 /* This structure is used to hold a stack of .set values.  */
11242
11243 struct mips_option_stack
11244 {
11245   struct mips_option_stack *next;
11246   struct mips_set_options options;
11247 };
11248
11249 static struct mips_option_stack *mips_opts_stack;
11250
11251 /* Handle the .set pseudo-op.  */
11252
11253 static void
11254 s_mipsset (x)
11255      int x ATTRIBUTE_UNUSED;
11256 {
11257   char *name = input_line_pointer, ch;
11258
11259   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11260     input_line_pointer++;
11261   ch = *input_line_pointer;
11262   *input_line_pointer = '\0';
11263
11264   if (strcmp (name, "reorder") == 0)
11265     {
11266       if (mips_opts.noreorder && prev_nop_frag != NULL)
11267         {
11268           /* If we still have pending nops, we can discard them.  The
11269              usual nop handling will insert any that are still
11270              needed.  */
11271           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11272                                     * (mips_opts.mips16 ? 2 : 4));
11273           prev_nop_frag = NULL;
11274         }
11275       mips_opts.noreorder = 0;
11276     }
11277   else if (strcmp (name, "noreorder") == 0)
11278     {
11279       mips_emit_delays (true);
11280       mips_opts.noreorder = 1;
11281       mips_any_noreorder = 1;
11282     }
11283   else if (strcmp (name, "at") == 0)
11284     {
11285       mips_opts.noat = 0;
11286     }
11287   else if (strcmp (name, "noat") == 0)
11288     {
11289       mips_opts.noat = 1;
11290     }
11291   else if (strcmp (name, "macro") == 0)
11292     {
11293       mips_opts.warn_about_macros = 0;
11294     }
11295   else if (strcmp (name, "nomacro") == 0)
11296     {
11297       if (mips_opts.noreorder == 0)
11298         as_bad (_("`noreorder' must be set before `nomacro'"));
11299       mips_opts.warn_about_macros = 1;
11300     }
11301   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11302     {
11303       mips_opts.nomove = 0;
11304     }
11305   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11306     {
11307       mips_opts.nomove = 1;
11308     }
11309   else if (strcmp (name, "bopt") == 0)
11310     {
11311       mips_opts.nobopt = 0;
11312     }
11313   else if (strcmp (name, "nobopt") == 0)
11314     {
11315       mips_opts.nobopt = 1;
11316     }
11317   else if (strcmp (name, "mips16") == 0
11318            || strcmp (name, "MIPS-16") == 0)
11319     mips_opts.mips16 = 1;
11320   else if (strcmp (name, "nomips16") == 0
11321            || strcmp (name, "noMIPS-16") == 0)
11322     mips_opts.mips16 = 0;
11323   else if (strcmp (name, "mips3d") == 0)
11324     mips_opts.ase_mips3d = 1;
11325   else if (strcmp (name, "nomips3d") == 0)
11326     mips_opts.ase_mips3d = 0;
11327   else if (strncmp (name, "mips", 4) == 0)
11328     {
11329       int isa;
11330
11331       /* Permit the user to change the ISA on the fly.  Needless to
11332          say, misuse can cause serious problems.  */
11333       isa = atoi (name + 4);
11334       switch (isa)
11335       {
11336       case  0:
11337         mips_opts.gp32 = file_mips_gp32;
11338         mips_opts.fp32 = file_mips_fp32;
11339         mips_opts.abi = file_mips_abi;
11340         break;
11341       case  1:
11342       case  2:
11343       case 32:
11344         mips_opts.gp32 = 1;
11345         mips_opts.fp32 = 1;
11346         break;
11347       case  3:
11348       case  4:
11349       case  5:
11350       case 64:
11351         /* Loosen ABI register width restriction.  */
11352         if (mips_opts.abi == O32_ABI)
11353           mips_opts.abi = NO_ABI;
11354         mips_opts.gp32 = 0;
11355         mips_opts.fp32 = 0;
11356         break;
11357       default:
11358         as_bad (_("unknown ISA level %s"), name + 4);
11359         break;
11360       }
11361
11362       switch (isa)
11363       {
11364       case  0: mips_opts.isa = file_mips_isa;   break;
11365       case  1: mips_opts.isa = ISA_MIPS1;       break;
11366       case  2: mips_opts.isa = ISA_MIPS2;       break;
11367       case  3: mips_opts.isa = ISA_MIPS3;       break;
11368       case  4: mips_opts.isa = ISA_MIPS4;       break;
11369       case  5: mips_opts.isa = ISA_MIPS5;       break;
11370       case 32: mips_opts.isa = ISA_MIPS32;      break;
11371       case 64: mips_opts.isa = ISA_MIPS64;      break;
11372       default: as_bad (_("unknown ISA level %s"), name + 4); break;
11373       }
11374     }
11375   else if (strcmp (name, "autoextend") == 0)
11376     mips_opts.noautoextend = 0;
11377   else if (strcmp (name, "noautoextend") == 0)
11378     mips_opts.noautoextend = 1;
11379   else if (strcmp (name, "push") == 0)
11380     {
11381       struct mips_option_stack *s;
11382
11383       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11384       s->next = mips_opts_stack;
11385       s->options = mips_opts;
11386       mips_opts_stack = s;
11387     }
11388   else if (strcmp (name, "pop") == 0)
11389     {
11390       struct mips_option_stack *s;
11391
11392       s = mips_opts_stack;
11393       if (s == NULL)
11394         as_bad (_(".set pop with no .set push"));
11395       else
11396         {
11397           /* If we're changing the reorder mode we need to handle
11398              delay slots correctly.  */
11399           if (s->options.noreorder && ! mips_opts.noreorder)
11400             mips_emit_delays (true);
11401           else if (! s->options.noreorder && mips_opts.noreorder)
11402             {
11403               if (prev_nop_frag != NULL)
11404                 {
11405                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11406                                             * (mips_opts.mips16 ? 2 : 4));
11407                   prev_nop_frag = NULL;
11408                 }
11409             }
11410
11411           mips_opts = s->options;
11412           mips_opts_stack = s->next;
11413           free (s);
11414         }
11415     }
11416   else
11417     {
11418       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11419     }
11420   *input_line_pointer = ch;
11421   demand_empty_rest_of_line ();
11422 }
11423
11424 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11425    .option pic2.  It means to generate SVR4 PIC calls.  */
11426
11427 static void
11428 s_abicalls (ignore)
11429      int ignore ATTRIBUTE_UNUSED;
11430 {
11431   mips_pic = SVR4_PIC;
11432   if (USE_GLOBAL_POINTER_OPT)
11433     {
11434       if (g_switch_seen && g_switch_value != 0)
11435         as_warn (_("-G may not be used with SVR4 PIC code"));
11436       g_switch_value = 0;
11437     }
11438   bfd_set_gp_size (stdoutput, 0);
11439   demand_empty_rest_of_line ();
11440 }
11441
11442 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11443    PIC code.  It sets the $gp register for the function based on the
11444    function address, which is in the register named in the argument.
11445    This uses a relocation against _gp_disp, which is handled specially
11446    by the linker.  The result is:
11447         lui     $gp,%hi(_gp_disp)
11448         addiu   $gp,$gp,%lo(_gp_disp)
11449         addu    $gp,$gp,.cpload argument
11450    The .cpload argument is normally $25 == $t9.  */
11451
11452 static void
11453 s_cpload (ignore)
11454      int ignore ATTRIBUTE_UNUSED;
11455 {
11456   expressionS ex;
11457   int icnt = 0;
11458
11459   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11460      .cpload is ignored.  */
11461   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11462     {
11463       s_ignore (0);
11464       return;
11465     }
11466
11467   /* .cpload should be in a .set noreorder section.  */
11468   if (mips_opts.noreorder == 0)
11469     as_warn (_(".cpload not in noreorder section"));
11470
11471   ex.X_op = O_symbol;
11472   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11473   ex.X_op_symbol = NULL;
11474   ex.X_add_number = 0;
11475
11476   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11477   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11478
11479   macro_build_lui (NULL, &icnt, &ex, GP);
11480   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11481                (int) BFD_RELOC_LO16);
11482
11483   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11484                GP, GP, tc_get_register (0));
11485
11486   demand_empty_rest_of_line ();
11487 }
11488
11489 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11490      .cpsetup $reg1, offset|$reg2, label
11491
11492    If offset is given, this results in:
11493      sd         $gp, offset($sp)
11494      lui        $gp, %hi(%neg(%gp_rel(label)))
11495      daddiu     $gp, $gp, %lo(%neg(%gp_rel(label)))
11496      addu       $gp, $gp, $reg1
11497
11498    If $reg2 is given, this results in:
11499      daddu      $reg2, $gp, $0
11500      lui        $gp, %hi(%neg(%gp_rel(label)))
11501      daddiu     $gp, $gp, %lo(%neg(%gp_rel(label)))
11502      addu       $gp, $gp, $reg1
11503  */
11504 static void
11505 s_cpsetup (ignore)
11506      int ignore ATTRIBUTE_UNUSED;
11507 {
11508   expressionS ex_off;
11509   expressionS ex_sym;
11510   int reg1;
11511   int icnt = 0;
11512   char *sym;
11513
11514   /* If we are not generating SVR4 PIC code, .cpload is ignored.
11515      We also need NewABI support.  */
11516   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11517     {
11518       s_ignore (0);
11519       return;
11520     }
11521
11522   reg1 = tc_get_register (0);
11523   SKIP_WHITESPACE ();
11524   if (*input_line_pointer != ',')
11525     {
11526       as_bad (_("missing argument separator ',' for .cpsetup"));
11527       return;
11528     }
11529   else
11530     input_line_pointer++;
11531   SKIP_WHITESPACE ();
11532   if (*input_line_pointer == '$')
11533     mips_cpreturn_register = tc_get_register (0);
11534   else
11535     mips_cpreturn_offset = get_absolute_expression ();
11536   SKIP_WHITESPACE ();
11537   if (*input_line_pointer != ',')
11538     {
11539       as_bad (_("missing argument separator ',' for .cpsetup"));
11540       return;
11541     }
11542   else
11543     input_line_pointer++;
11544   SKIP_WHITESPACE ();
11545   sym = input_line_pointer;
11546   while (ISALNUM (*input_line_pointer))
11547     input_line_pointer++;
11548   *input_line_pointer = 0;
11549
11550   ex_sym.X_op = O_symbol;
11551   ex_sym.X_add_symbol = symbol_find_or_make (sym);
11552   ex_sym.X_op_symbol = NULL;
11553   ex_sym.X_add_number = 0;
11554
11555   if (mips_cpreturn_register == -1)
11556     {
11557       ex_off.X_op = O_constant;
11558       ex_off.X_add_symbol = NULL;
11559       ex_off.X_op_symbol = NULL;
11560       ex_off.X_add_number = mips_cpreturn_offset;
11561
11562       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11563                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11564     }
11565   else
11566     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11567                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11568
11569   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11570                (int) BFD_RELOC_GPREL16);
11571   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11572            NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11573   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11574            NULL, 0, 0, BFD_RELOC_HI16_S);
11575   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11576                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11577   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11578            NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11579   fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11580            NULL, 0, 0, BFD_RELOC_LO16);
11581   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11582                "d,v,t", mips_gp_register, mips_gp_register, reg1);
11583
11584   demand_empty_rest_of_line ();
11585 }
11586
11587 static void
11588 s_cplocal (ignore)
11589      int ignore ATTRIBUTE_UNUSED;
11590 {
11591   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11592    .cplocal is ignored.  */
11593   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11594     {
11595       s_ignore (0);
11596       return;
11597     }
11598
11599   mips_gp_register = tc_get_register (0);
11600 }
11601
11602 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11603    offset from $sp.  The offset is remembered, and after making a PIC
11604    call $gp is restored from that location.  */
11605
11606 static void
11607 s_cprestore (ignore)
11608      int ignore ATTRIBUTE_UNUSED;
11609 {
11610   expressionS ex;
11611   int icnt = 0;
11612
11613   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11614    .cprestore is ignored.  */
11615   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11616     {
11617       s_ignore (0);
11618       return;
11619     }
11620
11621   mips_cprestore_offset = get_absolute_expression ();
11622   mips_cprestore_valid = 1;
11623
11624   ex.X_op = O_constant;
11625   ex.X_add_symbol = NULL;
11626   ex.X_op_symbol = NULL;
11627   ex.X_add_number = mips_cprestore_offset;
11628
11629   macro_build ((char *) NULL, &icnt, &ex,
11630                HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11631                "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11632
11633   demand_empty_rest_of_line ();
11634 }
11635
11636 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11637    was given in the preceeding .gpsetup, it results in:
11638      ld         $gp, offset($sp)
11639
11640    If a register $reg2 was given there, it results in:
11641      daddiu     $gp, $gp, $reg2
11642  */
11643 static void
11644 s_cpreturn (ignore)
11645      int ignore ATTRIBUTE_UNUSED;
11646 {
11647   expressionS ex;
11648   int icnt = 0;
11649
11650   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11651      We also need NewABI support.  */
11652   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11653     {
11654       s_ignore (0);
11655       return;
11656     }
11657
11658   if (mips_cpreturn_register == -1)
11659     {
11660       ex.X_op = O_constant;
11661       ex.X_add_symbol = NULL;
11662       ex.X_op_symbol = NULL;
11663       ex.X_add_number = mips_cpreturn_offset;
11664
11665       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11666                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
11667     }
11668   else
11669     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11670                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11671
11672   demand_empty_rest_of_line ();
11673 }
11674
11675 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11676    code.  It sets the offset to use in gp_rel relocations.  */
11677
11678 static void
11679 s_gpvalue (ignore)
11680      int ignore ATTRIBUTE_UNUSED;
11681 {
11682   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11683      We also need NewABI support.  */
11684   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11685     {
11686       s_ignore (0);
11687       return;
11688     }
11689
11690   mips_cpreturn_offset = get_absolute_expression ();
11691
11692   demand_empty_rest_of_line ();
11693 }
11694
11695 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11696    code.  It generates a 32 bit GP relative reloc.  */
11697
11698 static void
11699 s_gpword (ignore)
11700      int ignore ATTRIBUTE_UNUSED;
11701 {
11702   symbolS *label;
11703   expressionS ex;
11704   char *p;
11705
11706   /* When not generating PIC code, this is treated as .word.  */
11707   if (mips_pic != SVR4_PIC)
11708     {
11709       s_cons (2);
11710       return;
11711     }
11712
11713   label = insn_labels != NULL ? insn_labels->label : NULL;
11714   mips_emit_delays (true);
11715   if (auto_align)
11716     mips_align (2, 0, label);
11717   mips_clear_insn_labels ();
11718
11719   expression (&ex);
11720
11721   if (ex.X_op != O_symbol || ex.X_add_number != 0)
11722     {
11723       as_bad (_("Unsupported use of .gpword"));
11724       ignore_rest_of_line ();
11725     }
11726
11727   p = frag_more (4);
11728   md_number_to_chars (p, (valueT) 0, 4);
11729   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11730                BFD_RELOC_GPREL32);
11731
11732   demand_empty_rest_of_line ();
11733 }
11734
11735 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
11736    tables in SVR4 PIC code.  */
11737
11738 static void
11739 s_cpadd (ignore)
11740      int ignore ATTRIBUTE_UNUSED;
11741 {
11742   int icnt = 0;
11743   int reg;
11744
11745   /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11746      code.  */
11747   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11748     {
11749       s_ignore (0);
11750       return;
11751     }
11752
11753   /* Add $gp to the register named as an argument.  */
11754   reg = tc_get_register (0);
11755   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11756                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11757                "d,v,t", reg, reg, GP);
11758
11759   demand_empty_rest_of_line ();
11760 }
11761
11762 /* Handle the .insn pseudo-op.  This marks instruction labels in
11763    mips16 mode.  This permits the linker to handle them specially,
11764    such as generating jalx instructions when needed.  We also make
11765    them odd for the duration of the assembly, in order to generate the
11766    right sort of code.  We will make them even in the adjust_symtab
11767    routine, while leaving them marked.  This is convenient for the
11768    debugger and the disassembler.  The linker knows to make them odd
11769    again.  */
11770
11771 static void
11772 s_insn (ignore)
11773      int ignore ATTRIBUTE_UNUSED;
11774 {
11775   if (mips_opts.mips16)
11776     mips16_mark_labels ();
11777
11778   demand_empty_rest_of_line ();
11779 }
11780
11781 /* Handle a .stabn directive.  We need these in order to mark a label
11782    as being a mips16 text label correctly.  Sometimes the compiler
11783    will emit a label, followed by a .stabn, and then switch sections.
11784    If the label and .stabn are in mips16 mode, then the label is
11785    really a mips16 text label.  */
11786
11787 static void
11788 s_mips_stab (type)
11789      int type;
11790 {
11791   if (type == 'n' && mips_opts.mips16)
11792     mips16_mark_labels ();
11793
11794   s_stab (type);
11795 }
11796
11797 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11798  */
11799
11800 static void
11801 s_mips_weakext (ignore)
11802      int ignore ATTRIBUTE_UNUSED;
11803 {
11804   char *name;
11805   int c;
11806   symbolS *symbolP;
11807   expressionS exp;
11808
11809   name = input_line_pointer;
11810   c = get_symbol_end ();
11811   symbolP = symbol_find_or_make (name);
11812   S_SET_WEAK (symbolP);
11813   *input_line_pointer = c;
11814
11815   SKIP_WHITESPACE ();
11816
11817   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11818     {
11819       if (S_IS_DEFINED (symbolP))
11820         {
11821           as_bad ("ignoring attempt to redefine symbol %s",
11822                   S_GET_NAME (symbolP));
11823           ignore_rest_of_line ();
11824           return;
11825         }
11826
11827       if (*input_line_pointer == ',')
11828         {
11829           ++input_line_pointer;
11830           SKIP_WHITESPACE ();
11831         }
11832
11833       expression (&exp);
11834       if (exp.X_op != O_symbol)
11835         {
11836           as_bad ("bad .weakext directive");
11837           ignore_rest_of_line();
11838           return;
11839         }
11840       symbol_set_value_expression (symbolP, &exp);
11841     }
11842
11843   demand_empty_rest_of_line ();
11844 }
11845
11846 /* Parse a register string into a number.  Called from the ECOFF code
11847    to parse .frame.  The argument is non-zero if this is the frame
11848    register, so that we can record it in mips_frame_reg.  */
11849
11850 int
11851 tc_get_register (frame)
11852      int frame;
11853 {
11854   int reg;
11855
11856   SKIP_WHITESPACE ();
11857   if (*input_line_pointer++ != '$')
11858     {
11859       as_warn (_("expected `$'"));
11860       reg = 0;
11861     }
11862   else if (ISDIGIT (*input_line_pointer))
11863     {
11864       reg = get_absolute_expression ();
11865       if (reg < 0 || reg >= 32)
11866         {
11867           as_warn (_("Bad register number"));
11868           reg = 0;
11869         }
11870     }
11871   else
11872     {
11873       if (strncmp (input_line_pointer, "fp", 2) == 0)
11874         reg = FP;
11875       else if (strncmp (input_line_pointer, "sp", 2) == 0)
11876         reg = SP;
11877       else if (strncmp (input_line_pointer, "gp", 2) == 0)
11878         reg = GP;
11879       else if (strncmp (input_line_pointer, "at", 2) == 0)
11880         reg = AT;
11881       else
11882         {
11883           as_warn (_("Unrecognized register name"));
11884           reg = 0;
11885         }
11886       input_line_pointer += 2;
11887     }
11888   if (frame)
11889     {
11890       mips_frame_reg = reg != 0 ? reg : SP;
11891       mips_frame_reg_valid = 1;
11892       mips_cprestore_valid = 0;
11893     }
11894   return reg;
11895 }
11896
11897 valueT
11898 md_section_align (seg, addr)
11899      asection *seg;
11900      valueT addr;
11901 {
11902   int align = bfd_get_section_alignment (stdoutput, seg);
11903
11904 #ifdef OBJ_ELF
11905   /* We don't need to align ELF sections to the full alignment.
11906      However, Irix 5 may prefer that we align them at least to a 16
11907      byte boundary.  We don't bother to align the sections if we are
11908      targeted for an embedded system.  */
11909   if (strcmp (TARGET_OS, "elf") == 0)
11910     return addr;
11911   if (align > 4)
11912     align = 4;
11913 #endif
11914
11915   return ((addr + (1 << align) - 1) & (-1 << align));
11916 }
11917
11918 /* Utility routine, called from above as well.  If called while the
11919    input file is still being read, it's only an approximation.  (For
11920    example, a symbol may later become defined which appeared to be
11921    undefined earlier.)  */
11922
11923 static int
11924 nopic_need_relax (sym, before_relaxing)
11925      symbolS *sym;
11926      int before_relaxing;
11927 {
11928   if (sym == 0)
11929     return 0;
11930
11931   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11932     {
11933       const char *symname;
11934       int change;
11935
11936       /* Find out whether this symbol can be referenced off the GP
11937          register.  It can be if it is smaller than the -G size or if
11938          it is in the .sdata or .sbss section.  Certain symbols can
11939          not be referenced off the GP, although it appears as though
11940          they can.  */
11941       symname = S_GET_NAME (sym);
11942       if (symname != (const char *) NULL
11943           && (strcmp (symname, "eprol") == 0
11944               || strcmp (symname, "etext") == 0
11945               || strcmp (symname, "_gp") == 0
11946               || strcmp (symname, "edata") == 0
11947               || strcmp (symname, "_fbss") == 0
11948               || strcmp (symname, "_fdata") == 0
11949               || strcmp (symname, "_ftext") == 0
11950               || strcmp (symname, "end") == 0
11951               || strcmp (symname, "_gp_disp") == 0))
11952         change = 1;
11953       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11954                && (0
11955 #ifndef NO_ECOFF_DEBUGGING
11956                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
11957                        && (symbol_get_obj (sym)->ecoff_extern_size
11958                            <= g_switch_value))
11959 #endif
11960                    /* We must defer this decision until after the whole
11961                       file has been read, since there might be a .extern
11962                       after the first use of this symbol.  */
11963                    || (before_relaxing
11964 #ifndef NO_ECOFF_DEBUGGING
11965                        && symbol_get_obj (sym)->ecoff_extern_size == 0
11966 #endif
11967                        && S_GET_VALUE (sym) == 0)
11968                    || (S_GET_VALUE (sym) != 0
11969                        && S_GET_VALUE (sym) <= g_switch_value)))
11970         change = 0;
11971       else
11972         {
11973           const char *segname;
11974
11975           segname = segment_name (S_GET_SEGMENT (sym));
11976           assert (strcmp (segname, ".lit8") != 0
11977                   && strcmp (segname, ".lit4") != 0);
11978           change = (strcmp (segname, ".sdata") != 0
11979                     && strcmp (segname, ".sbss") != 0
11980                     && strncmp (segname, ".sdata.", 7) != 0
11981                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11982         }
11983       return change;
11984     }
11985   else
11986     /* We are not optimizing for the GP register.  */
11987     return 1;
11988 }
11989
11990 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11991    extended opcode.  SEC is the section the frag is in.  */
11992
11993 static int
11994 mips16_extended_frag (fragp, sec, stretch)
11995      fragS *fragp;
11996      asection *sec;
11997      long stretch;
11998 {
11999   int type;
12000   register const struct mips16_immed_operand *op;
12001   offsetT val;
12002   int mintiny, maxtiny;
12003   segT symsec;
12004   fragS *sym_frag;
12005
12006   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12007     return 0;
12008   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12009     return 1;
12010
12011   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12012   op = mips16_immed_operands;
12013   while (op->type != type)
12014     {
12015       ++op;
12016       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12017     }
12018
12019   if (op->unsp)
12020     {
12021       if (type == '<' || type == '>' || type == '[' || type == ']')
12022         {
12023           mintiny = 1;
12024           maxtiny = 1 << op->nbits;
12025         }
12026       else
12027         {
12028           mintiny = 0;
12029           maxtiny = (1 << op->nbits) - 1;
12030         }
12031     }
12032   else
12033     {
12034       mintiny = - (1 << (op->nbits - 1));
12035       maxtiny = (1 << (op->nbits - 1)) - 1;
12036     }
12037
12038   sym_frag = symbol_get_frag (fragp->fr_symbol);
12039   val = S_GET_VALUE (fragp->fr_symbol);
12040   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12041
12042   if (op->pcrel)
12043     {
12044       addressT addr;
12045
12046       /* We won't have the section when we are called from
12047          mips_relax_frag.  However, we will always have been called
12048          from md_estimate_size_before_relax first.  If this is a
12049          branch to a different section, we mark it as such.  If SEC is
12050          NULL, and the frag is not marked, then it must be a branch to
12051          the same section.  */
12052       if (sec == NULL)
12053         {
12054           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12055             return 1;
12056         }
12057       else
12058         {
12059           /* Must have been called from md_estimate_size_before_relax.  */
12060           if (symsec != sec)
12061             {
12062               fragp->fr_subtype =
12063                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12064
12065               /* FIXME: We should support this, and let the linker
12066                  catch branches and loads that are out of range.  */
12067               as_bad_where (fragp->fr_file, fragp->fr_line,
12068                             _("unsupported PC relative reference to different section"));
12069
12070               return 1;
12071             }
12072           if (fragp != sym_frag && sym_frag->fr_address == 0)
12073             /* Assume non-extended on the first relaxation pass.
12074                The address we have calculated will be bogus if this is
12075                a forward branch to another frag, as the forward frag
12076                will have fr_address == 0.  */
12077             return 0;
12078         }
12079
12080       /* In this case, we know for sure that the symbol fragment is in
12081          the same section.  If the relax_marker of the symbol fragment
12082          differs from the relax_marker of this fragment, we have not
12083          yet adjusted the symbol fragment fr_address.  We want to add
12084          in STRETCH in order to get a better estimate of the address.
12085          This particularly matters because of the shift bits.  */
12086       if (stretch != 0
12087           && sym_frag->relax_marker != fragp->relax_marker)
12088         {
12089           fragS *f;
12090
12091           /* Adjust stretch for any alignment frag.  Note that if have
12092              been expanding the earlier code, the symbol may be
12093              defined in what appears to be an earlier frag.  FIXME:
12094              This doesn't handle the fr_subtype field, which specifies
12095              a maximum number of bytes to skip when doing an
12096              alignment.  */
12097           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12098             {
12099               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12100                 {
12101                   if (stretch < 0)
12102                     stretch = - ((- stretch)
12103                                  & ~ ((1 << (int) f->fr_offset) - 1));
12104                   else
12105                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12106                   if (stretch == 0)
12107                     break;
12108                 }
12109             }
12110           if (f != NULL)
12111             val += stretch;
12112         }
12113
12114       addr = fragp->fr_address + fragp->fr_fix;
12115
12116       /* The base address rules are complicated.  The base address of
12117          a branch is the following instruction.  The base address of a
12118          PC relative load or add is the instruction itself, but if it
12119          is in a delay slot (in which case it can not be extended) use
12120          the address of the instruction whose delay slot it is in.  */
12121       if (type == 'p' || type == 'q')
12122         {
12123           addr += 2;
12124
12125           /* If we are currently assuming that this frag should be
12126              extended, then, the current address is two bytes
12127              higher.  */
12128           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12129             addr += 2;
12130
12131           /* Ignore the low bit in the target, since it will be set
12132              for a text label.  */
12133           if ((val & 1) != 0)
12134             --val;
12135         }
12136       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12137         addr -= 4;
12138       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12139         addr -= 2;
12140
12141       val -= addr & ~ ((1 << op->shift) - 1);
12142
12143       /* Branch offsets have an implicit 0 in the lowest bit.  */
12144       if (type == 'p' || type == 'q')
12145         val /= 2;
12146
12147       /* If any of the shifted bits are set, we must use an extended
12148          opcode.  If the address depends on the size of this
12149          instruction, this can lead to a loop, so we arrange to always
12150          use an extended opcode.  We only check this when we are in
12151          the main relaxation loop, when SEC is NULL.  */
12152       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12153         {
12154           fragp->fr_subtype =
12155             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12156           return 1;
12157         }
12158
12159       /* If we are about to mark a frag as extended because the value
12160          is precisely maxtiny + 1, then there is a chance of an
12161          infinite loop as in the following code:
12162              la $4,foo
12163              .skip      1020
12164              .align     2
12165            foo:
12166          In this case when the la is extended, foo is 0x3fc bytes
12167          away, so the la can be shrunk, but then foo is 0x400 away, so
12168          the la must be extended.  To avoid this loop, we mark the
12169          frag as extended if it was small, and is about to become
12170          extended with a value of maxtiny + 1.  */
12171       if (val == ((maxtiny + 1) << op->shift)
12172           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12173           && sec == NULL)
12174         {
12175           fragp->fr_subtype =
12176             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12177           return 1;
12178         }
12179     }
12180   else if (symsec != absolute_section && sec != NULL)
12181     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12182
12183   if ((val & ((1 << op->shift) - 1)) != 0
12184       || val < (mintiny << op->shift)
12185       || val > (maxtiny << op->shift))
12186     return 1;
12187   else
12188     return 0;
12189 }
12190
12191 /* Estimate the size of a frag before relaxing.  Unless this is the
12192    mips16, we are not really relaxing here, and the final size is
12193    encoded in the subtype information.  For the mips16, we have to
12194    decide whether we are using an extended opcode or not.  */
12195
12196 int
12197 md_estimate_size_before_relax (fragp, segtype)
12198      fragS *fragp;
12199      asection *segtype;
12200 {
12201   int change = 0;
12202   boolean linkonce = false;
12203
12204   if (RELAX_MIPS16_P (fragp->fr_subtype))
12205     /* We don't want to modify the EXTENDED bit here; it might get us
12206        into infinite loops.  We change it only in mips_relax_frag().  */
12207     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12208
12209   if (mips_pic == NO_PIC)
12210     {
12211       change = nopic_need_relax (fragp->fr_symbol, 0);
12212     }
12213   else if (mips_pic == SVR4_PIC)
12214     {
12215       symbolS *sym;
12216       asection *symsec;
12217
12218       sym = fragp->fr_symbol;
12219
12220       /* Handle the case of a symbol equated to another symbol.  */
12221       while (symbol_equated_reloc_p (sym))
12222         {
12223           symbolS *n;
12224
12225           /* It's possible to get a loop here in a badly written
12226              program.  */
12227           n = symbol_get_value_expression (sym)->X_add_symbol;
12228           if (n == sym)
12229             break;
12230           sym = n;
12231         }
12232
12233       symsec = S_GET_SEGMENT (sym);
12234
12235       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12236       if (symsec != segtype && ! S_IS_LOCAL (sym))
12237         {
12238           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12239               != 0)
12240             linkonce = true;
12241
12242           /* The GNU toolchain uses an extension for ELF: a section
12243              beginning with the magic string .gnu.linkonce is a linkonce
12244              section.  */
12245           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12246                        sizeof ".gnu.linkonce" - 1) == 0)
12247             linkonce = true;
12248         }
12249
12250       /* This must duplicate the test in adjust_reloc_syms.  */
12251       change = (symsec != &bfd_und_section
12252                 && symsec != &bfd_abs_section
12253                 && ! bfd_is_com_section (symsec)
12254                 && !linkonce
12255 #ifdef OBJ_ELF
12256                 /* A global or weak symbol is treated as external.  */
12257                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12258                     || (! S_IS_WEAK (sym)
12259                         && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12260 #endif
12261                 );
12262     }
12263   else
12264     abort ();
12265
12266   if (change)
12267     {
12268       /* Record the offset to the first reloc in the fr_opcode field.
12269          This lets md_convert_frag and tc_gen_reloc know that the code
12270          must be expanded.  */
12271       fragp->fr_opcode = (fragp->fr_literal
12272                           + fragp->fr_fix
12273                           - RELAX_OLD (fragp->fr_subtype)
12274                           + RELAX_RELOC1 (fragp->fr_subtype));
12275       /* FIXME: This really needs as_warn_where.  */
12276       if (RELAX_WARN (fragp->fr_subtype))
12277         as_warn (_("AT used after \".set noat\" or macro used after "
12278                    "\".set nomacro\""));
12279
12280       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12281     }
12282
12283   return 0;
12284 }
12285
12286 /* This is called to see whether a reloc against a defined symbol
12287    should be converted into a reloc against a section.  Don't adjust
12288    MIPS16 jump relocations, so we don't have to worry about the format
12289    of the offset in the .o file.  Don't adjust relocations against
12290    mips16 symbols, so that the linker can find them if it needs to set
12291    up a stub.  */
12292
12293 int
12294 mips_fix_adjustable (fixp)
12295      fixS *fixp;
12296 {
12297 #ifdef OBJ_ELF
12298   /* Prevent all adjustments to global symbols.  */
12299   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12300       && mips_pic != EMBEDDED_PIC
12301       && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12302     return 0;
12303 #endif
12304   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12305     return 0;
12306   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12307       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12308     return 0;
12309   if (fixp->fx_addsy == NULL)
12310     return 1;
12311 #ifdef OBJ_ELF
12312   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12313       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12314       && fixp->fx_subsy == NULL)
12315     return 0;
12316 #endif
12317   return 1;
12318 }
12319
12320 /* Translate internal representation of relocation info to BFD target
12321    format.  */
12322
12323 arelent **
12324 tc_gen_reloc (section, fixp)
12325      asection *section ATTRIBUTE_UNUSED;
12326      fixS *fixp;
12327 {
12328   static arelent *retval[4];
12329   arelent *reloc;
12330   bfd_reloc_code_real_type code;
12331
12332   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12333   retval[1] = NULL;
12334
12335   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12336   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12337   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12338
12339   if (mips_pic == EMBEDDED_PIC
12340       && SWITCH_TABLE (fixp))
12341     {
12342       /* For a switch table entry we use a special reloc.  The addend
12343          is actually the difference between the reloc address and the
12344          subtrahend.  */
12345       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12346       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12347         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12348       fixp->fx_r_type = BFD_RELOC_GPREL32;
12349     }
12350   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12351     {
12352       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12353         reloc->addend = fixp->fx_addnumber;
12354       else
12355         {
12356           /* We use a special addend for an internal RELLO reloc.  */
12357           if (symbol_section_p (fixp->fx_addsy))
12358             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12359           else
12360             reloc->addend = fixp->fx_addnumber + reloc->address;
12361         }
12362     }
12363   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12364     {
12365       assert (fixp->fx_next != NULL
12366               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12367
12368       /* The reloc is relative to the RELLO; adjust the addend
12369          accordingly.  */
12370       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12371         reloc->addend = fixp->fx_next->fx_addnumber;
12372       else
12373         {
12374           /* We use a special addend for an internal RELHI reloc.  */
12375           if (symbol_section_p (fixp->fx_addsy))
12376             reloc->addend = (fixp->fx_next->fx_frag->fr_address
12377                              + fixp->fx_next->fx_where
12378                              - S_GET_VALUE (fixp->fx_subsy));
12379           else
12380             reloc->addend = (fixp->fx_addnumber
12381                              + fixp->fx_next->fx_frag->fr_address
12382                              + fixp->fx_next->fx_where);
12383         }
12384     }
12385   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12386     reloc->addend = fixp->fx_addnumber;
12387   else
12388     {
12389       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12390         /* A gruesome hack which is a result of the gruesome gas reloc
12391            handling.  */
12392         reloc->addend = reloc->address;
12393       else
12394         reloc->addend = -reloc->address;
12395     }
12396
12397   /* If this is a variant frag, we may need to adjust the existing
12398      reloc and generate a new one.  */
12399   if (fixp->fx_frag->fr_opcode != NULL
12400       && (fixp->fx_r_type == BFD_RELOC_GPREL16
12401           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12402           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12403           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12404           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12405           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12406           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12407       && ! HAVE_NEWABI)
12408     {
12409       arelent *reloc2;
12410
12411       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12412
12413       /* If this is not the last reloc in this frag, then we have two
12414          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12415          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
12416          the second one handle all of them.  */
12417       if (fixp->fx_next != NULL
12418           && fixp->fx_frag == fixp->fx_next->fx_frag)
12419         {
12420           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12421                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12422                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12423                       && (fixp->fx_next->fx_r_type
12424                           == BFD_RELOC_MIPS_GOT_LO16))
12425                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12426                       && (fixp->fx_next->fx_r_type
12427                           == BFD_RELOC_MIPS_CALL_LO16)));
12428           retval[0] = NULL;
12429           return retval;
12430         }
12431
12432       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12433       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12434       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12435       retval[2] = NULL;
12436       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12437       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12438       reloc2->address = (reloc->address
12439                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12440                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12441       reloc2->addend = fixp->fx_addnumber;
12442       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12443       assert (reloc2->howto != NULL);
12444
12445       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12446         {
12447           arelent *reloc3;
12448
12449           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12450           retval[3] = NULL;
12451           *reloc3 = *reloc2;
12452           reloc3->address += 4;
12453         }
12454
12455       if (mips_pic == NO_PIC)
12456         {
12457           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12458           fixp->fx_r_type = BFD_RELOC_HI16_S;
12459         }
12460       else if (mips_pic == SVR4_PIC)
12461         {
12462           switch (fixp->fx_r_type)
12463             {
12464             default:
12465               abort ();
12466             case BFD_RELOC_MIPS_GOT16:
12467               break;
12468             case BFD_RELOC_MIPS_CALL16:
12469             case BFD_RELOC_MIPS_GOT_LO16:
12470             case BFD_RELOC_MIPS_CALL_LO16:
12471               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12472               break;
12473             }
12474         }
12475       else
12476         abort ();
12477     }
12478
12479   /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12480      to be used in the relocation's section offset.  */
12481   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12482     {
12483       reloc->address = reloc->addend;
12484       reloc->addend = 0;
12485     }
12486
12487   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12488      fixup_segment converted a non-PC relative reloc into a PC
12489      relative reloc.  In such a case, we need to convert the reloc
12490      code.  */
12491   code = fixp->fx_r_type;
12492   if (fixp->fx_pcrel)
12493     {
12494       switch (code)
12495         {
12496         case BFD_RELOC_8:
12497           code = BFD_RELOC_8_PCREL;
12498           break;
12499         case BFD_RELOC_16:
12500           code = BFD_RELOC_16_PCREL;
12501           break;
12502         case BFD_RELOC_32:
12503           code = BFD_RELOC_32_PCREL;
12504           break;
12505         case BFD_RELOC_64:
12506           code = BFD_RELOC_64_PCREL;
12507           break;
12508         case BFD_RELOC_8_PCREL:
12509         case BFD_RELOC_16_PCREL:
12510         case BFD_RELOC_32_PCREL:
12511         case BFD_RELOC_64_PCREL:
12512         case BFD_RELOC_16_PCREL_S2:
12513         case BFD_RELOC_PCREL_HI16_S:
12514         case BFD_RELOC_PCREL_LO16:
12515           break;
12516         default:
12517           as_bad_where (fixp->fx_file, fixp->fx_line,
12518                         _("Cannot make %s relocation PC relative"),
12519                         bfd_get_reloc_code_name (code));
12520         }
12521     }
12522
12523 #ifdef OBJ_ELF
12524   /* md_apply_fix3 has a double-subtraction hack to get
12525      bfd_install_relocation to behave nicely.  GPREL relocations are
12526      handled correctly without this hack, so undo it here.  We can't
12527      stop md_apply_fix3 from subtracting twice in the first place since
12528      the fake addend is required for variant frags above.  */
12529   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12530       && code == BFD_RELOC_GPREL16
12531       && reloc->addend != 0
12532       && mips_need_elf_addend_fixup (fixp))
12533     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12534 #endif
12535
12536   /* To support a PC relative reloc when generating embedded PIC code
12537      for ECOFF, we use a Cygnus extension.  We check for that here to
12538      make sure that we don't let such a reloc escape normally.  */
12539   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12540        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12541       && code == BFD_RELOC_16_PCREL_S2
12542       && mips_pic != EMBEDDED_PIC)
12543     reloc->howto = NULL;
12544   else
12545     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12546
12547   if (reloc->howto == NULL)
12548     {
12549       as_bad_where (fixp->fx_file, fixp->fx_line,
12550                     _("Can not represent %s relocation in this object file format"),
12551                     bfd_get_reloc_code_name (code));
12552       retval[0] = NULL;
12553     }
12554
12555   return retval;
12556 }
12557
12558 /* Relax a machine dependent frag.  This returns the amount by which
12559    the current size of the frag should change.  */
12560
12561 int
12562 mips_relax_frag (fragp, stretch)
12563      fragS *fragp;
12564      long stretch;
12565 {
12566   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12567     return 0;
12568
12569   if (mips16_extended_frag (fragp, NULL, stretch))
12570     {
12571       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12572         return 0;
12573       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12574       return 2;
12575     }
12576   else
12577     {
12578       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12579         return 0;
12580       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12581       return -2;
12582     }
12583
12584   return 0;
12585 }
12586
12587 /* Convert a machine dependent frag.  */
12588
12589 void
12590 md_convert_frag (abfd, asec, fragp)
12591      bfd *abfd ATTRIBUTE_UNUSED;
12592      segT asec;
12593      fragS *fragp;
12594 {
12595   int old, new;
12596   char *fixptr;
12597
12598   if (RELAX_MIPS16_P (fragp->fr_subtype))
12599     {
12600       int type;
12601       register const struct mips16_immed_operand *op;
12602       boolean small, ext;
12603       offsetT val;
12604       bfd_byte *buf;
12605       unsigned long insn;
12606       boolean use_extend;
12607       unsigned short extend;
12608
12609       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12610       op = mips16_immed_operands;
12611       while (op->type != type)
12612         ++op;
12613
12614       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12615         {
12616           small = false;
12617           ext = true;
12618         }
12619       else
12620         {
12621           small = true;
12622           ext = false;
12623         }
12624
12625       resolve_symbol_value (fragp->fr_symbol);
12626       val = S_GET_VALUE (fragp->fr_symbol);
12627       if (op->pcrel)
12628         {
12629           addressT addr;
12630
12631           addr = fragp->fr_address + fragp->fr_fix;
12632
12633           /* The rules for the base address of a PC relative reloc are
12634              complicated; see mips16_extended_frag.  */
12635           if (type == 'p' || type == 'q')
12636             {
12637               addr += 2;
12638               if (ext)
12639                 addr += 2;
12640               /* Ignore the low bit in the target, since it will be
12641                  set for a text label.  */
12642               if ((val & 1) != 0)
12643                 --val;
12644             }
12645           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12646             addr -= 4;
12647           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12648             addr -= 2;
12649
12650           addr &= ~ (addressT) ((1 << op->shift) - 1);
12651           val -= addr;
12652
12653           /* Make sure the section winds up with the alignment we have
12654              assumed.  */
12655           if (op->shift > 0)
12656             record_alignment (asec, op->shift);
12657         }
12658
12659       if (ext
12660           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12661               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12662         as_warn_where (fragp->fr_file, fragp->fr_line,
12663                        _("extended instruction in delay slot"));
12664
12665       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12666
12667       if (target_big_endian)
12668         insn = bfd_getb16 (buf);
12669       else
12670         insn = bfd_getl16 (buf);
12671
12672       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12673                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12674                     small, ext, &insn, &use_extend, &extend);
12675
12676       if (use_extend)
12677         {
12678           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
12679           fragp->fr_fix += 2;
12680           buf += 2;
12681         }
12682
12683       md_number_to_chars ((char *) buf, insn, 2);
12684       fragp->fr_fix += 2;
12685       buf += 2;
12686     }
12687   else
12688     {
12689       if (fragp->fr_opcode == NULL)
12690         return;
12691
12692       old = RELAX_OLD (fragp->fr_subtype);
12693       new = RELAX_NEW (fragp->fr_subtype);
12694       fixptr = fragp->fr_literal + fragp->fr_fix;
12695
12696       if (new > 0)
12697         memcpy (fixptr - old, fixptr, new);
12698
12699       fragp->fr_fix += new - old;
12700     }
12701 }
12702
12703 #ifdef OBJ_ELF
12704
12705 /* This function is called after the relocs have been generated.
12706    We've been storing mips16 text labels as odd.  Here we convert them
12707    back to even for the convenience of the debugger.  */
12708
12709 void
12710 mips_frob_file_after_relocs ()
12711 {
12712   asymbol **syms;
12713   unsigned int count, i;
12714
12715   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12716     return;
12717
12718   syms = bfd_get_outsymbols (stdoutput);
12719   count = bfd_get_symcount (stdoutput);
12720   for (i = 0; i < count; i++, syms++)
12721     {
12722       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12723           && ((*syms)->value & 1) != 0)
12724         {
12725           (*syms)->value &= ~1;
12726           /* If the symbol has an odd size, it was probably computed
12727              incorrectly, so adjust that as well.  */
12728           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12729             ++elf_symbol (*syms)->internal_elf_sym.st_size;
12730         }
12731     }
12732 }
12733
12734 #endif
12735
12736 /* This function is called whenever a label is defined.  It is used
12737    when handling branch delays; if a branch has a label, we assume we
12738    can not move it.  */
12739
12740 void
12741 mips_define_label (sym)
12742      symbolS *sym;
12743 {
12744   struct insn_label_list *l;
12745
12746   if (free_insn_labels == NULL)
12747     l = (struct insn_label_list *) xmalloc (sizeof *l);
12748   else
12749     {
12750       l = free_insn_labels;
12751       free_insn_labels = l->next;
12752     }
12753
12754   l->label = sym;
12755   l->next = insn_labels;
12756   insn_labels = l;
12757 }
12758 \f
12759 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12760
12761 /* Some special processing for a MIPS ELF file.  */
12762
12763 void
12764 mips_elf_final_processing ()
12765 {
12766   /* Write out the register information.  */
12767   if (! HAVE_NEWABI)
12768     {
12769       Elf32_RegInfo s;
12770
12771       s.ri_gprmask = mips_gprmask;
12772       s.ri_cprmask[0] = mips_cprmask[0];
12773       s.ri_cprmask[1] = mips_cprmask[1];
12774       s.ri_cprmask[2] = mips_cprmask[2];
12775       s.ri_cprmask[3] = mips_cprmask[3];
12776       /* The gp_value field is set by the MIPS ELF backend.  */
12777
12778       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12779                                        ((Elf32_External_RegInfo *)
12780                                         mips_regmask_frag));
12781     }
12782   else
12783     {
12784       Elf64_Internal_RegInfo s;
12785
12786       s.ri_gprmask = mips_gprmask;
12787       s.ri_pad = 0;
12788       s.ri_cprmask[0] = mips_cprmask[0];
12789       s.ri_cprmask[1] = mips_cprmask[1];
12790       s.ri_cprmask[2] = mips_cprmask[2];
12791       s.ri_cprmask[3] = mips_cprmask[3];
12792       /* The gp_value field is set by the MIPS ELF backend.  */
12793
12794       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12795                                        ((Elf64_External_RegInfo *)
12796                                         mips_regmask_frag));
12797     }
12798
12799   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
12800      sort of BFD interface for this.  */
12801   if (mips_any_noreorder)
12802     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12803   if (mips_pic != NO_PIC)
12804     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12805
12806   /* Set MIPS ELF flags for ASEs. */
12807 #if 0 /* XXX FIXME */
12808   if (file_ase_mips3d)
12809     elf_elfheader (stdoutput)->e_flags |= ???;
12810 #endif
12811
12812   /* Set the MIPS ELF ABI flags.  */
12813   if (file_mips_abi == NO_ABI)
12814     ;
12815   else if (file_mips_abi == O32_ABI)
12816     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12817   else if (file_mips_abi == O64_ABI)
12818     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12819   else if (file_mips_abi == EABI_ABI)
12820     {
12821       if (mips_eabi64)
12822         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12823       else
12824         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12825     }
12826   else if (file_mips_abi == N32_ABI)
12827     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12828
12829   /* Nothing to do for "64".  */
12830
12831   if (mips_32bitmode)
12832     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12833 }
12834
12835 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12836 \f
12837 typedef struct proc {
12838   symbolS *isym;
12839   unsigned long reg_mask;
12840   unsigned long reg_offset;
12841   unsigned long fpreg_mask;
12842   unsigned long fpreg_offset;
12843   unsigned long frame_offset;
12844   unsigned long frame_reg;
12845   unsigned long pc_reg;
12846 } procS;
12847
12848 static procS cur_proc;
12849 static procS *cur_proc_ptr;
12850 static int numprocs;
12851
12852 /* Fill in an rs_align_code fragment.  */
12853
12854 void
12855 mips_handle_align (fragp)
12856      fragS *fragp;
12857 {
12858   if (fragp->fr_type != rs_align_code)
12859     return;
12860
12861   if (mips_opts.mips16)
12862     {
12863       static const unsigned char be_nop[] = { 0x65, 0x00 };
12864       static const unsigned char le_nop[] = { 0x00, 0x65 };
12865
12866       int bytes;
12867       char *p;
12868
12869       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12870       p = fragp->fr_literal + fragp->fr_fix;
12871
12872       if (bytes & 1)
12873         {
12874           *p++ = 0;
12875           fragp->fr_fix += 1;
12876         }
12877
12878       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12879       fragp->fr_var = 2;
12880     }
12881
12882   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
12883 }
12884
12885 static void
12886 md_obj_begin ()
12887 {
12888 }
12889
12890 static void
12891 md_obj_end ()
12892 {
12893   /* check for premature end, nesting errors, etc */
12894   if (cur_proc_ptr)
12895     as_warn (_("missing .end at end of assembly"));
12896 }
12897
12898 static long
12899 get_number ()
12900 {
12901   int negative = 0;
12902   long val = 0;
12903
12904   if (*input_line_pointer == '-')
12905     {
12906       ++input_line_pointer;
12907       negative = 1;
12908     }
12909   if (!ISDIGIT (*input_line_pointer))
12910     as_bad (_("expected simple number"));
12911   if (input_line_pointer[0] == '0')
12912     {
12913       if (input_line_pointer[1] == 'x')
12914         {
12915           input_line_pointer += 2;
12916           while (ISXDIGIT (*input_line_pointer))
12917             {
12918               val <<= 4;
12919               val |= hex_value (*input_line_pointer++);
12920             }
12921           return negative ? -val : val;
12922         }
12923       else
12924         {
12925           ++input_line_pointer;
12926           while (ISDIGIT (*input_line_pointer))
12927             {
12928               val <<= 3;
12929               val |= *input_line_pointer++ - '0';
12930             }
12931           return negative ? -val : val;
12932         }
12933     }
12934   if (!ISDIGIT (*input_line_pointer))
12935     {
12936       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12937               *input_line_pointer, *input_line_pointer);
12938       as_warn (_("invalid number"));
12939       return -1;
12940     }
12941   while (ISDIGIT (*input_line_pointer))
12942     {
12943       val *= 10;
12944       val += *input_line_pointer++ - '0';
12945     }
12946   return negative ? -val : val;
12947 }
12948
12949 /* The .file directive; just like the usual .file directive, but there
12950    is an initial number which is the ECOFF file index.  */
12951
12952 static void
12953 s_file (x)
12954      int x ATTRIBUTE_UNUSED;
12955 {
12956   get_number ();
12957   s_app_file (0);
12958 }
12959
12960 /* The .end directive.  */
12961
12962 static void
12963 s_mips_end (x)
12964      int x ATTRIBUTE_UNUSED;
12965 {
12966   symbolS *p;
12967   int maybe_text;
12968
12969   /* Following functions need their own .frame and .cprestore directives.  */
12970   mips_frame_reg_valid = 0;
12971   mips_cprestore_valid = 0;
12972
12973   if (!is_end_of_line[(unsigned char) *input_line_pointer])
12974     {
12975       p = get_symbol ();
12976       demand_empty_rest_of_line ();
12977     }
12978   else
12979     p = NULL;
12980
12981 #ifdef BFD_ASSEMBLER
12982   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12983     maybe_text = 1;
12984   else
12985     maybe_text = 0;
12986 #else
12987   if (now_seg != data_section && now_seg != bss_section)
12988     maybe_text = 1;
12989   else
12990     maybe_text = 0;
12991 #endif
12992
12993   if (!maybe_text)
12994     as_warn (_(".end not in text section"));
12995
12996   if (!cur_proc_ptr)
12997     {
12998       as_warn (_(".end directive without a preceding .ent directive."));
12999       demand_empty_rest_of_line ();
13000       return;
13001     }
13002
13003   if (p != NULL)
13004     {
13005       assert (S_GET_NAME (p));
13006       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13007         as_warn (_(".end symbol does not match .ent symbol."));
13008     }
13009   else
13010     as_warn (_(".end directive missing or unknown symbol"));
13011
13012 #ifdef MIPS_STABS_ELF
13013   {
13014     segT saved_seg = now_seg;
13015     subsegT saved_subseg = now_subseg;
13016     valueT dot;
13017     expressionS exp;
13018     char *fragp;
13019
13020     dot = frag_now_fix ();
13021
13022 #ifdef md_flush_pending_output
13023     md_flush_pending_output ();
13024 #endif
13025
13026     assert (pdr_seg);
13027     subseg_set (pdr_seg, 0);
13028
13029     /* Write the symbol.  */
13030     exp.X_op = O_symbol;
13031     exp.X_add_symbol = p;
13032     exp.X_add_number = 0;
13033     emit_expr (&exp, 4);
13034
13035     fragp = frag_more (7 * 4);
13036
13037     md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
13038     md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
13039     md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13040     md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13041     md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13042     md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13043     md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13044
13045     subseg_set (saved_seg, saved_subseg);
13046   }
13047 #endif
13048
13049   cur_proc_ptr = NULL;
13050 }
13051
13052 /* The .aent and .ent directives.  */
13053
13054 static void
13055 s_mips_ent (aent)
13056      int aent;
13057 {
13058   symbolS *symbolP;
13059   int maybe_text;
13060
13061   symbolP = get_symbol ();
13062   if (*input_line_pointer == ',')
13063     input_line_pointer++;
13064   SKIP_WHITESPACE ();
13065   if (ISDIGIT (*input_line_pointer)
13066       || *input_line_pointer == '-')
13067     get_number ();
13068
13069 #ifdef BFD_ASSEMBLER
13070   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13071     maybe_text = 1;
13072   else
13073     maybe_text = 0;
13074 #else
13075   if (now_seg != data_section && now_seg != bss_section)
13076     maybe_text = 1;
13077   else
13078     maybe_text = 0;
13079 #endif
13080
13081   if (!maybe_text)
13082     as_warn (_(".ent or .aent not in text section."));
13083
13084   if (!aent && cur_proc_ptr)
13085     as_warn (_("missing .end"));
13086
13087   if (!aent)
13088     {
13089       /* This function needs its own .frame and .cprestore directives.  */
13090       mips_frame_reg_valid = 0;
13091       mips_cprestore_valid = 0;
13092
13093       cur_proc_ptr = &cur_proc;
13094       memset (cur_proc_ptr, '\0', sizeof (procS));
13095
13096       cur_proc_ptr->isym = symbolP;
13097
13098       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13099
13100       numprocs++;
13101     }
13102
13103   demand_empty_rest_of_line ();
13104 }
13105
13106 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13107    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13108    s_mips_frame is used so that we can set the PDR information correctly.
13109    We can't use the ecoff routines because they make reference to the ecoff
13110    symbol table (in the mdebug section).  */
13111
13112 static void
13113 s_mips_frame (ignore)
13114      int ignore ATTRIBUTE_UNUSED;
13115 {
13116 #ifdef MIPS_STABS_ELF
13117
13118   long val;
13119
13120   if (cur_proc_ptr == (procS *) NULL)
13121     {
13122       as_warn (_(".frame outside of .ent"));
13123       demand_empty_rest_of_line ();
13124       return;
13125     }
13126
13127   cur_proc_ptr->frame_reg = tc_get_register (1);
13128
13129   SKIP_WHITESPACE ();
13130   if (*input_line_pointer++ != ','
13131       || get_absolute_expression_and_terminator (&val) != ',')
13132     {
13133       as_warn (_("Bad .frame directive"));
13134       --input_line_pointer;
13135       demand_empty_rest_of_line ();
13136       return;
13137     }
13138
13139   cur_proc_ptr->frame_offset = val;
13140   cur_proc_ptr->pc_reg = tc_get_register (0);
13141
13142   demand_empty_rest_of_line ();
13143 #else
13144   s_ignore (ignore);
13145 #endif /* MIPS_STABS_ELF */
13146 }
13147
13148 /* The .fmask and .mask directives. If the mdebug section is present
13149    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13150    embedded targets, s_mips_mask is used so that we can set the PDR
13151    information correctly. We can't use the ecoff routines because they
13152    make reference to the ecoff symbol table (in the mdebug section).  */
13153
13154 static void
13155 s_mips_mask (reg_type)
13156      char reg_type;
13157 {
13158 #ifdef MIPS_STABS_ELF
13159   long mask, off;
13160
13161   if (cur_proc_ptr == (procS *) NULL)
13162     {
13163       as_warn (_(".mask/.fmask outside of .ent"));
13164       demand_empty_rest_of_line ();
13165       return;
13166     }
13167
13168   if (get_absolute_expression_and_terminator (&mask) != ',')
13169     {
13170       as_warn (_("Bad .mask/.fmask directive"));
13171       --input_line_pointer;
13172       demand_empty_rest_of_line ();
13173       return;
13174     }
13175
13176   off = get_absolute_expression ();
13177
13178   if (reg_type == 'F')
13179     {
13180       cur_proc_ptr->fpreg_mask = mask;
13181       cur_proc_ptr->fpreg_offset = off;
13182     }
13183   else
13184     {
13185       cur_proc_ptr->reg_mask = mask;
13186       cur_proc_ptr->reg_offset = off;
13187     }
13188
13189   demand_empty_rest_of_line ();
13190 #else
13191   s_ignore (reg_type);
13192 #endif /* MIPS_STABS_ELF */
13193 }
13194
13195 /* The .loc directive.  */
13196
13197 #if 0
13198 static void
13199 s_loc (x)
13200      int x;
13201 {
13202   symbolS *symbolP;
13203   int lineno;
13204   int addroff;
13205
13206   assert (now_seg == text_section);
13207
13208   lineno = get_number ();
13209   addroff = frag_now_fix ();
13210
13211   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13212   S_SET_TYPE (symbolP, N_SLINE);
13213   S_SET_OTHER (symbolP, 0);
13214   S_SET_DESC (symbolP, lineno);
13215   symbolP->sy_segment = now_seg;
13216 }
13217 #endif
13218
13219 /* CPU name/ISA/number mapping table.
13220
13221    Entries are grouped by type.  The first matching CPU or ISA entry
13222    gets chosen by CPU or ISA, so it should be the 'canonical' name
13223    for that type.  Entries after that within the type are sorted
13224    alphabetically.
13225
13226    Case is ignored in comparison, so put the canonical entry in the
13227    appropriate case but everything else in lower case to ease eye pain.  */
13228 static const struct mips_cpu_info mips_cpu_info_table[] =
13229 {
13230   /* MIPS1 ISA */
13231   { "MIPS1",          1,      ISA_MIPS1,      CPU_R3000, },
13232   { "mips",           1,      ISA_MIPS1,      CPU_R3000, },
13233
13234   /* MIPS2 ISA */
13235   { "MIPS2",          1,      ISA_MIPS2,      CPU_R6000, },
13236
13237   /* MIPS3 ISA */
13238   { "MIPS3",          1,      ISA_MIPS3,      CPU_R4000, },
13239
13240   /* MIPS4 ISA */
13241   { "MIPS4",          1,      ISA_MIPS4,      CPU_R8000, },
13242
13243   /* MIPS5 ISA */
13244   { "MIPS5",          1,      ISA_MIPS5,      CPU_MIPS5, },
13245   { "Generic-MIPS5",  0,      ISA_MIPS5,      CPU_MIPS5, },
13246
13247   /* MIPS32 ISA */
13248   { "MIPS32",         1,      ISA_MIPS32,     CPU_MIPS32, },
13249   { "mipsisa32",      0,      ISA_MIPS32,     CPU_MIPS32, },
13250   { "Generic-MIPS32", 0,      ISA_MIPS32,     CPU_MIPS32, },
13251   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
13252   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32, },
13253   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32, },
13254
13255   /* For historical reasons.  */
13256   { "MIPS64",         1,      ISA_MIPS3,      CPU_R4000, },
13257
13258   /* MIPS64 ISA */
13259   { "mipsisa64",      1,      ISA_MIPS64,     CPU_MIPS64, },
13260   { "Generic-MIPS64", 0,      ISA_MIPS64,     CPU_MIPS64, },
13261   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64, },
13262   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64, },
13263
13264   /* R2000 CPU */
13265   { "R2000",          0,      ISA_MIPS1,      CPU_R2000, },
13266   { "2000",           0,      ISA_MIPS1,      CPU_R2000, },
13267   { "2k",             0,      ISA_MIPS1,      CPU_R2000, },
13268   { "r2k",            0,      ISA_MIPS1,      CPU_R2000, },
13269
13270   /* R3000 CPU */
13271   { "R3000",          0,      ISA_MIPS1,      CPU_R3000, },
13272   { "3000",           0,      ISA_MIPS1,      CPU_R3000, },
13273   { "3k",             0,      ISA_MIPS1,      CPU_R3000, },
13274   { "r3k",            0,      ISA_MIPS1,      CPU_R3000, },
13275
13276   /* TX3900 CPU */
13277   { "R3900",          0,      ISA_MIPS1,      CPU_R3900, },
13278   { "3900",           0,      ISA_MIPS1,      CPU_R3900, },
13279   { "mipstx39",       0,      ISA_MIPS1,      CPU_R3900, },
13280
13281   /* R4000 CPU */
13282   { "R4000",          0,      ISA_MIPS3,      CPU_R4000, },
13283   { "4000",           0,      ISA_MIPS3,      CPU_R4000, },
13284   { "4k",             0,      ISA_MIPS3,      CPU_R4000, },   /* beware */
13285   { "r4k",            0,      ISA_MIPS3,      CPU_R4000, },
13286
13287   /* R4010 CPU */
13288   { "R4010",          0,      ISA_MIPS2,      CPU_R4010, },
13289   { "4010",           0,      ISA_MIPS2,      CPU_R4010, },
13290
13291   /* R4400 CPU */
13292   { "R4400",          0,      ISA_MIPS3,      CPU_R4400, },
13293   { "4400",           0,      ISA_MIPS3,      CPU_R4400, },
13294
13295   /* R4600 CPU */
13296   { "R4600",          0,      ISA_MIPS3,      CPU_R4600, },
13297   { "4600",           0,      ISA_MIPS3,      CPU_R4600, },
13298   { "mips64orion",    0,      ISA_MIPS3,      CPU_R4600, },
13299   { "orion",          0,      ISA_MIPS3,      CPU_R4600, },
13300
13301   /* R4650 CPU */
13302   { "R4650",          0,      ISA_MIPS3,      CPU_R4650, },
13303   { "4650",           0,      ISA_MIPS3,      CPU_R4650, },
13304
13305   /* R6000 CPU */
13306   { "R6000",          0,      ISA_MIPS2,      CPU_R6000, },
13307   { "6000",           0,      ISA_MIPS2,      CPU_R6000, },
13308   { "6k",             0,      ISA_MIPS2,      CPU_R6000, },
13309   { "r6k",            0,      ISA_MIPS2,      CPU_R6000, },
13310
13311   /* R8000 CPU */
13312   { "R8000",          0,      ISA_MIPS4,      CPU_R8000, },
13313   { "8000",           0,      ISA_MIPS4,      CPU_R8000, },
13314   { "8k",             0,      ISA_MIPS4,      CPU_R8000, },
13315   { "r8k",            0,      ISA_MIPS4,      CPU_R8000, },
13316
13317   /* R10000 CPU */
13318   { "R10000",         0,      ISA_MIPS4,      CPU_R10000, },
13319   { "10000",          0,      ISA_MIPS4,      CPU_R10000, },
13320   { "10k",            0,      ISA_MIPS4,      CPU_R10000, },
13321   { "r10k",           0,      ISA_MIPS4,      CPU_R10000, },
13322
13323   /* R12000 CPU */
13324   { "R12000",         0,      ISA_MIPS4,      CPU_R12000, },
13325   { "12000",          0,      ISA_MIPS4,      CPU_R12000, },
13326   { "12k",            0,      ISA_MIPS4,      CPU_R12000, },
13327   { "r12k",           0,      ISA_MIPS4,      CPU_R12000, },
13328
13329   /* VR4100 CPU */
13330   { "VR4100",         0,      ISA_MIPS3,      CPU_VR4100, },
13331   { "4100",           0,      ISA_MIPS3,      CPU_VR4100, },
13332   { "mips64vr4100",   0,      ISA_MIPS3,      CPU_VR4100, },
13333   { "r4100",          0,      ISA_MIPS3,      CPU_VR4100, },
13334
13335   /* VR4111 CPU */
13336   { "VR4111",         0,      ISA_MIPS3,      CPU_R4111, },
13337   { "4111",           0,      ISA_MIPS3,      CPU_R4111, },
13338   { "mips64vr4111",   0,      ISA_MIPS3,      CPU_R4111, },
13339   { "r4111",          0,      ISA_MIPS3,      CPU_R4111, },
13340
13341   /* VR4300 CPU */
13342   { "VR4300",         0,      ISA_MIPS3,      CPU_R4300, },
13343   { "4300",           0,      ISA_MIPS3,      CPU_R4300, },
13344   { "mips64vr4300",   0,      ISA_MIPS3,      CPU_R4300, },
13345   { "r4300",          0,      ISA_MIPS3,      CPU_R4300, },
13346
13347   /* VR5000 CPU */
13348   { "VR5000",         0,      ISA_MIPS4,      CPU_R5000, },
13349   { "5000",           0,      ISA_MIPS4,      CPU_R5000, },
13350   { "5k",             0,      ISA_MIPS4,      CPU_R5000, },
13351   { "mips64vr5000",   0,      ISA_MIPS4,      CPU_R5000, },
13352   { "r5000",          0,      ISA_MIPS4,      CPU_R5000, },
13353   { "r5200",          0,      ISA_MIPS4,      CPU_R5000, },
13354   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000, },
13355   { "r5230",          0,      ISA_MIPS4,      CPU_R5000, },
13356   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000, },
13357   { "r5231",          0,      ISA_MIPS4,      CPU_R5000, },
13358   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000, },
13359   { "r5261",          0,      ISA_MIPS4,      CPU_R5000, },
13360   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000, },
13361   { "r5721",          0,      ISA_MIPS4,      CPU_R5000, },
13362   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000, },
13363   { "r5k",            0,      ISA_MIPS4,      CPU_R5000, },
13364   { "r7000",          0,      ISA_MIPS4,      CPU_R5000, },
13365
13366   /* Broadcom SB-1 CPU */
13367   { "SB-1",           0,      ISA_MIPS64,     CPU_SB1, },
13368   { "sb-1250",        0,      ISA_MIPS64,     CPU_SB1, },
13369   { "sb1",            0,      ISA_MIPS64,     CPU_SB1, },
13370   { "sb1250",         0,      ISA_MIPS64,     CPU_SB1, },
13371
13372   /* End marker.  */
13373   { NULL, 0, 0, 0, },
13374 };
13375
13376 static const struct mips_cpu_info *
13377 mips_cpu_info_from_name (name)
13378      const char *name;
13379 {
13380   int i;
13381
13382   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13383     if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13384       return (&mips_cpu_info_table[i]);
13385
13386   return NULL;
13387 }
13388
13389 static const struct mips_cpu_info *
13390 mips_cpu_info_from_isa (isa)
13391      int isa;
13392 {
13393   int i;
13394
13395   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13396     if (mips_cpu_info_table[i].is_isa
13397       && isa == mips_cpu_info_table[i].isa)
13398       return (&mips_cpu_info_table[i]);
13399
13400   return NULL;
13401 }
13402
13403 static const struct mips_cpu_info *
13404 mips_cpu_info_from_cpu (cpu)
13405      int cpu;
13406 {
13407   int i;
13408
13409   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13410     if (!mips_cpu_info_table[i].is_isa
13411       && cpu == mips_cpu_info_table[i].cpu)
13412       return (&mips_cpu_info_table[i]);
13413
13414   return NULL;
13415 }