* config/tc-mips.c (s_change_section): Make sure input buffer
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 #define HAVE_32BIT_GPRS                            \
264     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
265
266 #define HAVE_32BIT_FPRS                            \
267     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
268
269 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
272 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
273
274 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
275
276 /* We can only have 64bit addresses if the object file format
277    supports it.  */
278 #define HAVE_32BIT_ADDRESSES                           \
279    (HAVE_32BIT_GPRS                                    \
280     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
281          || ! HAVE_64BIT_OBJECTS)                      \
282         && mips_pic != EMBEDDED_PIC))
283
284 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
285
286 /* Return true if the given CPU supports the MIPS16 ASE.  */
287 #define CPU_HAS_MIPS16(cpu)                                             \
288    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
289     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
290
291 /* Return true if the given CPU supports the MIPS3D ASE.  */
292 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
293                                  )
294
295 /* Return true if the given CPU supports the MDMX ASE.  */
296 #define CPU_HAS_MDMX(cpu)       (false                 \
297                                  )
298
299 /* True if CPU has a dror instruction.  */
300 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
301
302 /* True if CPU has a ror instruction.  */
303 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
304
305 /* Whether the processor uses hardware interlocks to protect
306    reads from the HI and LO registers, and thus does not
307    require nops to be inserted.  */
308
309 #define hilo_interlocks (mips_arch == CPU_R4010                       \
310                          || mips_arch == CPU_VR5500                   \
311                          || mips_arch == CPU_SB1                      \
312                          )
313
314 /* Whether the processor uses hardware interlocks to protect reads
315    from the GPRs, and thus does not require nops to be inserted.  */
316 #define gpr_interlocks \
317   (mips_opts.isa != ISA_MIPS1  \
318    || mips_arch == CPU_VR5400  \
319    || mips_arch == CPU_VR5500  \
320    || mips_arch == CPU_R3900)
321
322 /* As with other "interlocks" this is used by hardware that has FP
323    (co-processor) interlocks.  */
324 /* Itbl support may require additional care here.  */
325 #define cop_interlocks (mips_arch == CPU_R4300                        \
326                         || mips_arch == CPU_VR5400                    \
327                         || mips_arch == CPU_VR5500                    \
328                         || mips_arch == CPU_SB1                       \
329                         )
330
331 /* Is this a mfhi or mflo instruction?  */
332 #define MF_HILO_INSN(PINFO) \
333           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
334
335 /* MIPS PIC level.  */
336
337 enum mips_pic_level mips_pic;
338
339 /* Warn about all NOPS that the assembler generates.  */
340 static int warn_nops = 0;
341
342 /* 1 if we should generate 32 bit offsets from the $gp register in
343    SVR4_PIC mode.  Currently has no meaning in other modes.  */
344 static int mips_big_got = 0;
345
346 /* 1 if trap instructions should used for overflow rather than break
347    instructions.  */
348 static int mips_trap = 0;
349
350 /* 1 if double width floating point constants should not be constructed
351    by assembling two single width halves into two single width floating
352    point registers which just happen to alias the double width destination
353    register.  On some architectures this aliasing can be disabled by a bit
354    in the status register, and the setting of this bit cannot be determined
355    automatically at assemble time.  */
356 static int mips_disable_float_construction;
357
358 /* Non-zero if any .set noreorder directives were used.  */
359
360 static int mips_any_noreorder;
361
362 /* Non-zero if nops should be inserted when the register referenced in
363    an mfhi/mflo instruction is read in the next two instructions.  */
364 static int mips_7000_hilo_fix;
365
366 /* The size of the small data section.  */
367 static unsigned int g_switch_value = 8;
368 /* Whether the -G option was used.  */
369 static int g_switch_seen = 0;
370
371 #define N_RMASK 0xc4
372 #define N_VFP   0xd4
373
374 /* If we can determine in advance that GP optimization won't be
375    possible, we can skip the relaxation stuff that tries to produce
376    GP-relative references.  This makes delay slot optimization work
377    better.
378
379    This function can only provide a guess, but it seems to work for
380    gcc output.  It needs to guess right for gcc, otherwise gcc
381    will put what it thinks is a GP-relative instruction in a branch
382    delay slot.
383
384    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
385    fixed it for the non-PIC mode.  KR 95/04/07  */
386 static int nopic_need_relax PARAMS ((symbolS *, int));
387
388 /* handle of the OPCODE hash table */
389 static struct hash_control *op_hash = NULL;
390
391 /* The opcode hash table we use for the mips16.  */
392 static struct hash_control *mips16_op_hash = NULL;
393
394 /* This array holds the chars that always start a comment.  If the
395     pre-processor is disabled, these aren't very useful */
396 const char comment_chars[] = "#";
397
398 /* This array holds the chars that only start a comment at the beginning of
399    a line.  If the line seems to have the form '# 123 filename'
400    .line and .file directives will appear in the pre-processed output */
401 /* Note that input_file.c hand checks for '#' at the beginning of the
402    first line of the input file.  This is because the compiler outputs
403    #NO_APP at the beginning of its output.  */
404 /* Also note that C style comments are always supported.  */
405 const char line_comment_chars[] = "#";
406
407 /* This array holds machine specific line separator characters.  */
408 const char line_separator_chars[] = ";";
409
410 /* Chars that can be used to separate mant from exp in floating point nums */
411 const char EXP_CHARS[] = "eE";
412
413 /* Chars that mean this number is a floating point constant */
414 /* As in 0f12.456 */
415 /* or    0d1.2345e12 */
416 const char FLT_CHARS[] = "rRsSfFdDxXpP";
417
418 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
419    changed in read.c .  Ideally it shouldn't have to know about it at all,
420    but nothing is ideal around here.
421  */
422
423 static char *insn_error;
424
425 static int auto_align = 1;
426
427 /* When outputting SVR4 PIC code, the assembler needs to know the
428    offset in the stack frame from which to restore the $gp register.
429    This is set by the .cprestore pseudo-op, and saved in this
430    variable.  */
431 static offsetT mips_cprestore_offset = -1;
432
433 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
434    more optimizations, it can use a register value instead of a memory-saved
435    offset and even an other register than $gp as global pointer.  */
436 static offsetT mips_cpreturn_offset = -1;
437 static int mips_cpreturn_register = -1;
438 static int mips_gp_register = GP;
439 static int mips_gprel_offset = 0;
440
441 /* Whether mips_cprestore_offset has been set in the current function
442    (or whether it has already been warned about, if not).  */
443 static int mips_cprestore_valid = 0;
444
445 /* This is the register which holds the stack frame, as set by the
446    .frame pseudo-op.  This is needed to implement .cprestore.  */
447 static int mips_frame_reg = SP;
448
449 /* Whether mips_frame_reg has been set in the current function
450    (or whether it has already been warned about, if not).  */
451 static int mips_frame_reg_valid = 0;
452
453 /* To output NOP instructions correctly, we need to keep information
454    about the previous two instructions.  */
455
456 /* Whether we are optimizing.  The default value of 2 means to remove
457    unneeded NOPs and swap branch instructions when possible.  A value
458    of 1 means to not swap branches.  A value of 0 means to always
459    insert NOPs.  */
460 static int mips_optimize = 2;
461
462 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
463    equivalent to seeing no -g option at all.  */
464 static int mips_debug = 0;
465
466 /* The previous instruction.  */
467 static struct mips_cl_insn prev_insn;
468
469 /* The instruction before prev_insn.  */
470 static struct mips_cl_insn prev_prev_insn;
471
472 /* If we don't want information for prev_insn or prev_prev_insn, we
473    point the insn_mo field at this dummy integer.  */
474 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
475
476 /* Non-zero if prev_insn is valid.  */
477 static int prev_insn_valid;
478
479 /* The frag for the previous instruction.  */
480 static struct frag *prev_insn_frag;
481
482 /* The offset into prev_insn_frag for the previous instruction.  */
483 static long prev_insn_where;
484
485 /* The reloc type for the previous instruction, if any.  */
486 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
487
488 /* The reloc for the previous instruction, if any.  */
489 static fixS *prev_insn_fixp[3];
490
491 /* Non-zero if the previous instruction was in a delay slot.  */
492 static int prev_insn_is_delay_slot;
493
494 /* Non-zero if the previous instruction was in a .set noreorder.  */
495 static int prev_insn_unreordered;
496
497 /* Non-zero if the previous instruction uses an extend opcode (if
498    mips16).  */
499 static int prev_insn_extended;
500
501 /* Non-zero if the previous previous instruction was in a .set
502    noreorder.  */
503 static int prev_prev_insn_unreordered;
504
505 /* If this is set, it points to a frag holding nop instructions which
506    were inserted before the start of a noreorder section.  If those
507    nops turn out to be unnecessary, the size of the frag can be
508    decreased.  */
509 static fragS *prev_nop_frag;
510
511 /* The number of nop instructions we created in prev_nop_frag.  */
512 static int prev_nop_frag_holds;
513
514 /* The number of nop instructions that we know we need in
515    prev_nop_frag.  */
516 static int prev_nop_frag_required;
517
518 /* The number of instructions we've seen since prev_nop_frag.  */
519 static int prev_nop_frag_since;
520
521 /* For ECOFF and ELF, relocations against symbols are done in two
522    parts, with a HI relocation and a LO relocation.  Each relocation
523    has only 16 bits of space to store an addend.  This means that in
524    order for the linker to handle carries correctly, it must be able
525    to locate both the HI and the LO relocation.  This means that the
526    relocations must appear in order in the relocation table.
527
528    In order to implement this, we keep track of each unmatched HI
529    relocation.  We then sort them so that they immediately precede the
530    corresponding LO relocation.  */
531
532 struct mips_hi_fixup
533 {
534   /* Next HI fixup.  */
535   struct mips_hi_fixup *next;
536   /* This fixup.  */
537   fixS *fixp;
538   /* The section this fixup is in.  */
539   segT seg;
540 };
541
542 /* The list of unmatched HI relocs.  */
543
544 static struct mips_hi_fixup *mips_hi_fixup_list;
545
546 /* Map normal MIPS register numbers to mips16 register numbers.  */
547
548 #define X ILLEGAL_REG
549 static const int mips32_to_16_reg_map[] =
550 {
551   X, X, 2, 3, 4, 5, 6, 7,
552   X, X, X, X, X, X, X, X,
553   0, 1, X, X, X, X, X, X,
554   X, X, X, X, X, X, X, X
555 };
556 #undef X
557
558 /* Map mips16 register numbers to normal MIPS register numbers.  */
559
560 static const unsigned int mips16_to_32_reg_map[] =
561 {
562   16, 17, 2, 3, 4, 5, 6, 7
563 };
564
565 static int mips_fix_4122_bugs;
566
567 /* We don't relax branches by default, since this causes us to expand
568    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
569    fail to compute the offset before expanding the macro to the most
570    efficient expansion.  */
571
572 static int mips_relax_branch;
573 \f
574 /* Since the MIPS does not have multiple forms of PC relative
575    instructions, we do not have to do relaxing as is done on other
576    platforms.  However, we do have to handle GP relative addressing
577    correctly, which turns out to be a similar problem.
578
579    Every macro that refers to a symbol can occur in (at least) two
580    forms, one with GP relative addressing and one without.  For
581    example, loading a global variable into a register generally uses
582    a macro instruction like this:
583      lw $4,i
584    If i can be addressed off the GP register (this is true if it is in
585    the .sbss or .sdata section, or if it is known to be smaller than
586    the -G argument) this will generate the following instruction:
587      lw $4,i($gp)
588    This instruction will use a GPREL reloc.  If i can not be addressed
589    off the GP register, the following instruction sequence will be used:
590      lui $at,i
591      lw $4,i($at)
592    In this case the first instruction will have a HI16 reloc, and the
593    second reloc will have a LO16 reloc.  Both relocs will be against
594    the symbol i.
595
596    The issue here is that we may not know whether i is GP addressable
597    until after we see the instruction that uses it.  Therefore, we
598    want to be able to choose the final instruction sequence only at
599    the end of the assembly.  This is similar to the way other
600    platforms choose the size of a PC relative instruction only at the
601    end of assembly.
602
603    When generating position independent code we do not use GP
604    addressing in quite the same way, but the issue still arises as
605    external symbols and local symbols must be handled differently.
606
607    We handle these issues by actually generating both possible
608    instruction sequences.  The longer one is put in a frag_var with
609    type rs_machine_dependent.  We encode what to do with the frag in
610    the subtype field.  We encode (1) the number of existing bytes to
611    replace, (2) the number of new bytes to use, (3) the offset from
612    the start of the existing bytes to the first reloc we must generate
613    (that is, the offset is applied from the start of the existing
614    bytes after they are replaced by the new bytes, if any), (4) the
615    offset from the start of the existing bytes to the second reloc,
616    (5) whether a third reloc is needed (the third reloc is always four
617    bytes after the second reloc), and (6) whether to warn if this
618    variant is used (this is sometimes needed if .set nomacro or .set
619    noat is in effect).  All these numbers are reasonably small.
620
621    Generating two instruction sequences must be handled carefully to
622    ensure that delay slots are handled correctly.  Fortunately, there
623    are a limited number of cases.  When the second instruction
624    sequence is generated, append_insn is directed to maintain the
625    existing delay slot information, so it continues to apply to any
626    code after the second instruction sequence.  This means that the
627    second instruction sequence must not impose any requirements not
628    required by the first instruction sequence.
629
630    These variant frags are then handled in functions called by the
631    machine independent code.  md_estimate_size_before_relax returns
632    the final size of the frag.  md_convert_frag sets up the final form
633    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
634    one if needed.  */
635 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
636   ((relax_substateT) \
637    (((old) << 23) \
638     | ((new) << 16) \
639     | (((reloc1) + 64) << 9) \
640     | (((reloc2) + 64) << 2) \
641     | ((reloc3) ? (1 << 1) : 0) \
642     | ((warn) ? 1 : 0)))
643 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
644 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
645 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
646 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
647 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
648 #define RELAX_WARN(i) ((i) & 1)
649
650 /* Branch without likely bit.  If label is out of range, we turn:
651
652         beq reg1, reg2, label
653         delay slot
654
655    into
656
657         bne reg1, reg2, 0f
658         nop
659         j label
660      0: delay slot
661
662    with the following opcode replacements:
663
664         beq <-> bne
665         blez <-> bgtz
666         bltz <-> bgez
667         bc1f <-> bc1t
668
669         bltzal <-> bgezal  (with jal label instead of j label)
670
671    Even though keeping the delay slot instruction in the delay slot of
672    the branch would be more efficient, it would be very tricky to do
673    correctly, because we'd have to introduce a variable frag *after*
674    the delay slot instruction, and expand that instead.  Let's do it
675    the easy way for now, even if the branch-not-taken case now costs
676    one additional instruction.  Out-of-range branches are not supposed
677    to be common, anyway.
678
679    Branch likely.  If label is out of range, we turn:
680
681         beql reg1, reg2, label
682         delay slot (annulled if branch not taken)
683
684    into
685
686         beql reg1, reg2, 1f
687         nop
688         beql $0, $0, 2f
689         nop
690      1: j[al] label
691         delay slot (executed only if branch taken)
692      2:
693
694    It would be possible to generate a shorter sequence by losing the
695    likely bit, generating something like:
696      
697         bne reg1, reg2, 0f
698         nop
699         j[al] label
700         delay slot (executed only if branch taken)
701      0:
702
703         beql -> bne
704         bnel -> beq
705         blezl -> bgtz
706         bgtzl -> blez
707         bltzl -> bgez
708         bgezl -> bltz
709         bc1fl -> bc1t
710         bc1tl -> bc1f
711
712         bltzall -> bgezal  (with jal label instead of j label)
713         bgezall -> bltzal  (ditto)
714
715
716    but it's not clear that it would actually improve performance.  */
717 #define RELAX_BRANCH_ENCODE(reloc_s2, uncond, likely, link, toofar) \
718   ((relax_substateT) \
719    (0xc0000000 \
720     | ((toofar) ? 1 : 0) \
721     | ((link) ? 2 : 0) \
722     | ((likely) ? 4 : 0) \
723     | ((uncond) ? 8 : 0) \
724     | ((reloc_s2) ? 16 : 0)))
725 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
726 #define RELAX_BRANCH_RELOC_S2(i) (((i) & 16) != 0)
727 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
728 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
729 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
730 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1))
731
732 /* For mips16 code, we use an entirely different form of relaxation.
733    mips16 supports two versions of most instructions which take
734    immediate values: a small one which takes some small value, and a
735    larger one which takes a 16 bit value.  Since branches also follow
736    this pattern, relaxing these values is required.
737
738    We can assemble both mips16 and normal MIPS code in a single
739    object.  Therefore, we need to support this type of relaxation at
740    the same time that we support the relaxation described above.  We
741    use the high bit of the subtype field to distinguish these cases.
742
743    The information we store for this type of relaxation is the
744    argument code found in the opcode file for this relocation, whether
745    the user explicitly requested a small or extended form, and whether
746    the relocation is in a jump or jal delay slot.  That tells us the
747    size of the value, and how it should be stored.  We also store
748    whether the fragment is considered to be extended or not.  We also
749    store whether this is known to be a branch to a different section,
750    whether we have tried to relax this frag yet, and whether we have
751    ever extended a PC relative fragment because of a shift count.  */
752 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
753   (0x80000000                                                   \
754    | ((type) & 0xff)                                            \
755    | ((small) ? 0x100 : 0)                                      \
756    | ((ext) ? 0x200 : 0)                                        \
757    | ((dslot) ? 0x400 : 0)                                      \
758    | ((jal_dslot) ? 0x800 : 0))
759 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
760 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
761 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
762 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
763 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
764 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
765 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
766 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
767 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
768 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
769 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
770 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
771
772 /* Is the given value a sign-extended 32-bit value?  */
773 #define IS_SEXT_32BIT_NUM(x)                                            \
774   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
775    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
776
777 /* Is the given value a sign-extended 16-bit value?  */
778 #define IS_SEXT_16BIT_NUM(x)                                            \
779   (((x) &~ (offsetT) 0x7fff) == 0                                       \
780    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
781
782 \f
783 /* Prototypes for static functions.  */
784
785 #ifdef __STDC__
786 #define internalError() \
787     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
788 #else
789 #define internalError() as_fatal (_("MIPS internal Error"));
790 #endif
791
792 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
793
794 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
795                                   unsigned int reg, enum mips_regclass class));
796 static int reg_needs_delay PARAMS ((unsigned int));
797 static void mips16_mark_labels PARAMS ((void));
798 static void append_insn PARAMS ((char *place,
799                                  struct mips_cl_insn * ip,
800                                  expressionS * p,
801                                  bfd_reloc_code_real_type *r,
802                                  boolean));
803 static void mips_no_prev_insn PARAMS ((int));
804 static void mips_emit_delays PARAMS ((boolean));
805 #ifdef USE_STDARG
806 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
807                                  const char *name, const char *fmt,
808                                  ...));
809 #else
810 static void macro_build ();
811 #endif
812 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
813                                         const char *, const char *,
814                                         va_list));
815 static void macro_build_jalr PARAMS ((int, expressionS *));
816 static void macro_build_lui PARAMS ((char *place, int *counter,
817                                      expressionS * ep, int regnum));
818 static void macro_build_ldst_constoffset PARAMS ((char *place, int *counter,
819                                                   expressionS * ep, const char *op,
820                                                   int valreg, int breg));
821 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
822 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
823                                          expressionS *));
824 static void load_register PARAMS ((int *, int, expressionS *, int));
825 static void load_address PARAMS ((int *, int, expressionS *, int *));
826 static void move_register PARAMS ((int *, int, int));
827 static void macro PARAMS ((struct mips_cl_insn * ip));
828 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
829 #ifdef LOSING_COMPILER
830 static void macro2 PARAMS ((struct mips_cl_insn * ip));
831 #endif
832 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
833 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
834 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
835                                   boolean, boolean, unsigned long *,
836                                   boolean *, unsigned short *));
837 static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
838 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
839 static int my_getSmallExpression PARAMS ((expressionS *, char *));
840 static void my_getExpression PARAMS ((expressionS *, char *));
841 #ifdef OBJ_ELF
842 static int support_64bit_objects PARAMS((void));
843 #endif
844 static void mips_set_option_string PARAMS ((const char **, const char *));
845 static symbolS *get_symbol PARAMS ((void));
846 static void mips_align PARAMS ((int to, int fill, symbolS *label));
847 static void s_align PARAMS ((int));
848 static void s_change_sec PARAMS ((int));
849 static void s_change_section PARAMS ((int));
850 static void s_cons PARAMS ((int));
851 static void s_float_cons PARAMS ((int));
852 static void s_mips_globl PARAMS ((int));
853 static void s_option PARAMS ((int));
854 static void s_mipsset PARAMS ((int));
855 static void s_abicalls PARAMS ((int));
856 static void s_cpload PARAMS ((int));
857 static void s_cpsetup PARAMS ((int));
858 static void s_cplocal PARAMS ((int));
859 static void s_cprestore PARAMS ((int));
860 static void s_cpreturn PARAMS ((int));
861 static void s_gpvalue PARAMS ((int));
862 static void s_gpword PARAMS ((int));
863 static void s_gpdword PARAMS ((int));
864 static void s_cpadd PARAMS ((int));
865 static void s_insn PARAMS ((int));
866 static void md_obj_begin PARAMS ((void));
867 static void md_obj_end PARAMS ((void));
868 static long get_number PARAMS ((void));
869 static void s_mips_ent PARAMS ((int));
870 static void s_mips_end PARAMS ((int));
871 static void s_mips_frame PARAMS ((int));
872 static void s_mips_mask PARAMS ((int));
873 static void s_mips_stab PARAMS ((int));
874 static void s_mips_weakext PARAMS ((int));
875 static void s_mips_file PARAMS ((int));
876 static void s_mips_loc PARAMS ((int));
877 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
878 static int relaxed_branch_length (fragS *, asection *, int);
879 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
880 static void show PARAMS ((FILE *, const char *, int *, int *));
881 #ifdef OBJ_ELF
882 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
883 #endif
884
885 /* Return values of my_getSmallExpression().  */
886
887 enum small_ex_type
888 {
889   S_EX_NONE = 0,
890   S_EX_REGISTER,
891
892   /* Direct relocation creation by %percent_op().  */
893   S_EX_HALF,
894   S_EX_HI,
895   S_EX_LO,
896   S_EX_GP_REL,
897   S_EX_GOT,
898   S_EX_CALL16,
899   S_EX_GOT_DISP,
900   S_EX_GOT_PAGE,
901   S_EX_GOT_OFST,
902   S_EX_GOT_HI,
903   S_EX_GOT_LO,
904   S_EX_NEG,
905   S_EX_HIGHER,
906   S_EX_HIGHEST,
907   S_EX_CALL_HI,
908   S_EX_CALL_LO
909 };
910
911 /* Table and functions used to map between CPU/ISA names, and
912    ISA levels, and CPU numbers.  */
913
914 struct mips_cpu_info
915 {
916   const char *name;           /* CPU or ISA name.  */
917   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
918   int isa;                    /* ISA level.  */
919   int cpu;                    /* CPU number (default CPU if ISA).  */
920 };
921
922 static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
923 static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
924 static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
925                                                         const char *));
926 static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
927 static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
928                                                            const char *));
929 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
930 \f
931 /* Pseudo-op table.
932
933    The following pseudo-ops from the Kane and Heinrich MIPS book
934    should be defined here, but are currently unsupported: .alias,
935    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
936
937    The following pseudo-ops from the Kane and Heinrich MIPS book are
938    specific to the type of debugging information being generated, and
939    should be defined by the object format: .aent, .begin, .bend,
940    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
941    .vreg.
942
943    The following pseudo-ops from the Kane and Heinrich MIPS book are
944    not MIPS CPU specific, but are also not specific to the object file
945    format.  This file is probably the best place to define them, but
946    they are not currently supported: .asm0, .endr, .lab, .repeat,
947    .struct.  */
948
949 static const pseudo_typeS mips_pseudo_table[] =
950 {
951   /* MIPS specific pseudo-ops.  */
952   {"option", s_option, 0},
953   {"set", s_mipsset, 0},
954   {"rdata", s_change_sec, 'r'},
955   {"sdata", s_change_sec, 's'},
956   {"livereg", s_ignore, 0},
957   {"abicalls", s_abicalls, 0},
958   {"cpload", s_cpload, 0},
959   {"cpsetup", s_cpsetup, 0},
960   {"cplocal", s_cplocal, 0},
961   {"cprestore", s_cprestore, 0},
962   {"cpreturn", s_cpreturn, 0},
963   {"gpvalue", s_gpvalue, 0},
964   {"gpword", s_gpword, 0},
965   {"gpdword", s_gpdword, 0},
966   {"cpadd", s_cpadd, 0},
967   {"insn", s_insn, 0},
968
969   /* Relatively generic pseudo-ops that happen to be used on MIPS
970      chips.  */
971   {"asciiz", stringer, 1},
972   {"bss", s_change_sec, 'b'},
973   {"err", s_err, 0},
974   {"half", s_cons, 1},
975   {"dword", s_cons, 3},
976   {"weakext", s_mips_weakext, 0},
977
978   /* These pseudo-ops are defined in read.c, but must be overridden
979      here for one reason or another.  */
980   {"align", s_align, 0},
981   {"byte", s_cons, 0},
982   {"data", s_change_sec, 'd'},
983   {"double", s_float_cons, 'd'},
984   {"float", s_float_cons, 'f'},
985   {"globl", s_mips_globl, 0},
986   {"global", s_mips_globl, 0},
987   {"hword", s_cons, 1},
988   {"int", s_cons, 2},
989   {"long", s_cons, 2},
990   {"octa", s_cons, 4},
991   {"quad", s_cons, 3},
992   {"section", s_change_section, 0},
993   {"short", s_cons, 1},
994   {"single", s_float_cons, 'f'},
995   {"stabn", s_mips_stab, 'n'},
996   {"text", s_change_sec, 't'},
997   {"word", s_cons, 2},
998
999   { "extern", ecoff_directive_extern, 0},
1000
1001   { NULL, NULL, 0 },
1002 };
1003
1004 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1005 {
1006   /* These pseudo-ops should be defined by the object file format.
1007      However, a.out doesn't support them, so we have versions here.  */
1008   {"aent", s_mips_ent, 1},
1009   {"bgnb", s_ignore, 0},
1010   {"end", s_mips_end, 0},
1011   {"endb", s_ignore, 0},
1012   {"ent", s_mips_ent, 0},
1013   {"file", s_mips_file, 0},
1014   {"fmask", s_mips_mask, 'F'},
1015   {"frame", s_mips_frame, 0},
1016   {"loc", s_mips_loc, 0},
1017   {"mask", s_mips_mask, 'R'},
1018   {"verstamp", s_ignore, 0},
1019   { NULL, NULL, 0 },
1020 };
1021
1022 extern void pop_insert PARAMS ((const pseudo_typeS *));
1023
1024 void
1025 mips_pop_insert ()
1026 {
1027   pop_insert (mips_pseudo_table);
1028   if (! ECOFF_DEBUGGING)
1029     pop_insert (mips_nonecoff_pseudo_table);
1030 }
1031 \f
1032 /* Symbols labelling the current insn.  */
1033
1034 struct insn_label_list
1035 {
1036   struct insn_label_list *next;
1037   symbolS *label;
1038 };
1039
1040 static struct insn_label_list *insn_labels;
1041 static struct insn_label_list *free_insn_labels;
1042
1043 static void mips_clear_insn_labels PARAMS ((void));
1044
1045 static inline void
1046 mips_clear_insn_labels ()
1047 {
1048   register struct insn_label_list **pl;
1049
1050   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1051     ;
1052   *pl = insn_labels;
1053   insn_labels = NULL;
1054 }
1055 \f
1056 static char *expr_end;
1057
1058 /* Expressions which appear in instructions.  These are set by
1059    mips_ip.  */
1060
1061 static expressionS imm_expr;
1062 static expressionS offset_expr;
1063
1064 /* Relocs associated with imm_expr and offset_expr.  */
1065
1066 static bfd_reloc_code_real_type imm_reloc[3]
1067   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068 static bfd_reloc_code_real_type offset_reloc[3]
1069   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1070
1071 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
1072
1073 static boolean imm_unmatched_hi;
1074
1075 /* These are set by mips16_ip if an explicit extension is used.  */
1076
1077 static boolean mips16_small, mips16_ext;
1078
1079 #ifdef OBJ_ELF
1080 /* The pdr segment for per procedure frame/regmask info.  Not used for
1081    ECOFF debugging.  */
1082
1083 static segT pdr_seg;
1084 #endif
1085
1086 /* The default target format to use.  */
1087
1088 const char *
1089 mips_target_format ()
1090 {
1091   switch (OUTPUT_FLAVOR)
1092     {
1093     case bfd_target_aout_flavour:
1094       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1095     case bfd_target_ecoff_flavour:
1096       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1097     case bfd_target_coff_flavour:
1098       return "pe-mips";
1099     case bfd_target_elf_flavour:
1100 #ifdef TE_TMIPS
1101       /* This is traditional mips.  */
1102       return (target_big_endian
1103               ? (HAVE_64BIT_OBJECTS
1104                  ? "elf64-tradbigmips"
1105                  : (HAVE_NEWABI
1106                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1107               : (HAVE_64BIT_OBJECTS
1108                  ? "elf64-tradlittlemips"
1109                  : (HAVE_NEWABI
1110                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1111 #else
1112       return (target_big_endian
1113               ? (HAVE_64BIT_OBJECTS
1114                  ? "elf64-bigmips"
1115                  : (HAVE_NEWABI
1116                     ? "elf32-nbigmips" : "elf32-bigmips"))
1117               : (HAVE_64BIT_OBJECTS
1118                  ? "elf64-littlemips"
1119                  : (HAVE_NEWABI
1120                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1121 #endif
1122     default:
1123       abort ();
1124       return NULL;
1125     }
1126 }
1127
1128 /* This function is called once, at assembler startup time.  It should
1129    set up all the tables, etc. that the MD part of the assembler will need.  */
1130
1131 void
1132 md_begin ()
1133 {
1134   register const char *retval = NULL;
1135   int i = 0;
1136   int broken = 0;
1137
1138   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1139     as_warn (_("Could not set architecture and machine"));
1140
1141   op_hash = hash_new ();
1142
1143   for (i = 0; i < NUMOPCODES;)
1144     {
1145       const char *name = mips_opcodes[i].name;
1146
1147       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1148       if (retval != NULL)
1149         {
1150           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1151                    mips_opcodes[i].name, retval);
1152           /* Probably a memory allocation problem?  Give up now.  */
1153           as_fatal (_("Broken assembler.  No assembly attempted."));
1154         }
1155       do
1156         {
1157           if (mips_opcodes[i].pinfo != INSN_MACRO)
1158             {
1159               if (!validate_mips_insn (&mips_opcodes[i]))
1160                 broken = 1;
1161             }
1162           ++i;
1163         }
1164       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1165     }
1166
1167   mips16_op_hash = hash_new ();
1168
1169   i = 0;
1170   while (i < bfd_mips16_num_opcodes)
1171     {
1172       const char *name = mips16_opcodes[i].name;
1173
1174       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1175       if (retval != NULL)
1176         as_fatal (_("internal: can't hash `%s': %s"),
1177                   mips16_opcodes[i].name, retval);
1178       do
1179         {
1180           if (mips16_opcodes[i].pinfo != INSN_MACRO
1181               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1182                   != mips16_opcodes[i].match))
1183             {
1184               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1185                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1186               broken = 1;
1187             }
1188           ++i;
1189         }
1190       while (i < bfd_mips16_num_opcodes
1191              && strcmp (mips16_opcodes[i].name, name) == 0);
1192     }
1193
1194   if (broken)
1195     as_fatal (_("Broken assembler.  No assembly attempted."));
1196
1197   /* We add all the general register names to the symbol table.  This
1198      helps us detect invalid uses of them.  */
1199   for (i = 0; i < 32; i++)
1200     {
1201       char buf[5];
1202
1203       sprintf (buf, "$%d", i);
1204       symbol_table_insert (symbol_new (buf, reg_section, i,
1205                                        &zero_address_frag));
1206     }
1207   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1208                                    &zero_address_frag));
1209   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1210                                    &zero_address_frag));
1211   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1212                                    &zero_address_frag));
1213   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1214                                    &zero_address_frag));
1215   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1216                                    &zero_address_frag));
1217   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1218                                    &zero_address_frag));
1219   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1220                                    &zero_address_frag));
1221   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1222                                    &zero_address_frag));
1223   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1224                                    &zero_address_frag));
1225
1226   /* If we don't add these register names to the symbol table, they
1227      may end up being added as regular symbols by operand(), and then
1228      make it to the object file as undefined in case they're not
1229      regarded as local symbols.  They're local in o32, since `$' is a
1230      local symbol prefix, but not in n32 or n64.  */
1231   for (i = 0; i < 8; i++)
1232     {
1233       char buf[6];
1234
1235       sprintf (buf, "$fcc%i", i);
1236       symbol_table_insert (symbol_new (buf, reg_section, -1,
1237                                        &zero_address_frag));
1238     }
1239
1240   mips_no_prev_insn (false);
1241
1242   mips_gprmask = 0;
1243   mips_cprmask[0] = 0;
1244   mips_cprmask[1] = 0;
1245   mips_cprmask[2] = 0;
1246   mips_cprmask[3] = 0;
1247
1248   /* set the default alignment for the text section (2**2) */
1249   record_alignment (text_section, 2);
1250
1251   if (USE_GLOBAL_POINTER_OPT)
1252     bfd_set_gp_size (stdoutput, g_switch_value);
1253
1254   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1255     {
1256       /* On a native system, sections must be aligned to 16 byte
1257          boundaries.  When configured for an embedded ELF target, we
1258          don't bother.  */
1259       if (strcmp (TARGET_OS, "elf") != 0)
1260         {
1261           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1262           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1263           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1264         }
1265
1266       /* Create a .reginfo section for register masks and a .mdebug
1267          section for debugging information.  */
1268       {
1269         segT seg;
1270         subsegT subseg;
1271         flagword flags;
1272         segT sec;
1273
1274         seg = now_seg;
1275         subseg = now_subseg;
1276
1277         /* The ABI says this section should be loaded so that the
1278            running program can access it.  However, we don't load it
1279            if we are configured for an embedded target */
1280         flags = SEC_READONLY | SEC_DATA;
1281         if (strcmp (TARGET_OS, "elf") != 0)
1282           flags |= SEC_ALLOC | SEC_LOAD;
1283
1284         if (mips_abi != N64_ABI)
1285           {
1286             sec = subseg_new (".reginfo", (subsegT) 0);
1287
1288             bfd_set_section_flags (stdoutput, sec, flags);
1289             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1290
1291 #ifdef OBJ_ELF
1292             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1293 #endif
1294           }
1295         else
1296           {
1297             /* The 64-bit ABI uses a .MIPS.options section rather than
1298                .reginfo section.  */
1299             sec = subseg_new (".MIPS.options", (subsegT) 0);
1300             bfd_set_section_flags (stdoutput, sec, flags);
1301             bfd_set_section_alignment (stdoutput, sec, 3);
1302
1303 #ifdef OBJ_ELF
1304             /* Set up the option header.  */
1305             {
1306               Elf_Internal_Options opthdr;
1307               char *f;
1308
1309               opthdr.kind = ODK_REGINFO;
1310               opthdr.size = (sizeof (Elf_External_Options)
1311                              + sizeof (Elf64_External_RegInfo));
1312               opthdr.section = 0;
1313               opthdr.info = 0;
1314               f = frag_more (sizeof (Elf_External_Options));
1315               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1316                                              (Elf_External_Options *) f);
1317
1318               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1319             }
1320 #endif
1321           }
1322
1323         if (ECOFF_DEBUGGING)
1324           {
1325             sec = subseg_new (".mdebug", (subsegT) 0);
1326             (void) bfd_set_section_flags (stdoutput, sec,
1327                                           SEC_HAS_CONTENTS | SEC_READONLY);
1328             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1329           }
1330 #ifdef OBJ_ELF
1331         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1332           {
1333             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1334             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1335                                           SEC_READONLY | SEC_RELOC
1336                                           | SEC_DEBUGGING);
1337             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1338           }
1339 #endif
1340
1341         subseg_set (seg, subseg);
1342       }
1343     }
1344
1345   if (! ECOFF_DEBUGGING)
1346     md_obj_begin ();
1347 }
1348
1349 void
1350 md_mips_end ()
1351 {
1352   if (! ECOFF_DEBUGGING)
1353     md_obj_end ();
1354 }
1355
1356 void
1357 md_assemble (str)
1358      char *str;
1359 {
1360   struct mips_cl_insn insn;
1361   bfd_reloc_code_real_type unused_reloc[3]
1362     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1363
1364   imm_expr.X_op = O_absent;
1365   imm_unmatched_hi = false;
1366   offset_expr.X_op = O_absent;
1367   imm_reloc[0] = BFD_RELOC_UNUSED;
1368   imm_reloc[1] = BFD_RELOC_UNUSED;
1369   imm_reloc[2] = BFD_RELOC_UNUSED;
1370   offset_reloc[0] = BFD_RELOC_UNUSED;
1371   offset_reloc[1] = BFD_RELOC_UNUSED;
1372   offset_reloc[2] = BFD_RELOC_UNUSED;
1373
1374   if (mips_opts.mips16)
1375     mips16_ip (str, &insn);
1376   else
1377     {
1378       mips_ip (str, &insn);
1379       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1380             str, insn.insn_opcode));
1381     }
1382
1383   if (insn_error)
1384     {
1385       as_bad ("%s `%s'", insn_error, str);
1386       return;
1387     }
1388
1389   if (insn.insn_mo->pinfo == INSN_MACRO)
1390     {
1391       if (mips_opts.mips16)
1392         mips16_macro (&insn);
1393       else
1394         macro (&insn);
1395     }
1396   else
1397     {
1398       if (imm_expr.X_op != O_absent)
1399         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1400       else if (offset_expr.X_op != O_absent)
1401         append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1402       else
1403         append_insn (NULL, &insn, NULL, unused_reloc, false);
1404     }
1405 }
1406
1407 /* See whether instruction IP reads register REG.  CLASS is the type
1408    of register.  */
1409
1410 static int
1411 insn_uses_reg (ip, reg, class)
1412      struct mips_cl_insn *ip;
1413      unsigned int reg;
1414      enum mips_regclass class;
1415 {
1416   if (class == MIPS16_REG)
1417     {
1418       assert (mips_opts.mips16);
1419       reg = mips16_to_32_reg_map[reg];
1420       class = MIPS_GR_REG;
1421     }
1422
1423   /* Don't report on general register ZERO, since it never changes.  */
1424   if (class == MIPS_GR_REG && reg == ZERO)
1425     return 0;
1426
1427   if (class == MIPS_FP_REG)
1428     {
1429       assert (! mips_opts.mips16);
1430       /* If we are called with either $f0 or $f1, we must check $f0.
1431          This is not optimal, because it will introduce an unnecessary
1432          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1433          need to distinguish reading both $f0 and $f1 or just one of
1434          them.  Note that we don't have to check the other way,
1435          because there is no instruction that sets both $f0 and $f1
1436          and requires a delay.  */
1437       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1438           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1439               == (reg &~ (unsigned) 1)))
1440         return 1;
1441       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1442           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1443               == (reg &~ (unsigned) 1)))
1444         return 1;
1445     }
1446   else if (! mips_opts.mips16)
1447     {
1448       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1449           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1450         return 1;
1451       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1452           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1453         return 1;
1454     }
1455   else
1456     {
1457       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1458           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1459                                     & MIPS16OP_MASK_RX)]
1460               == reg))
1461         return 1;
1462       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1463           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1464                                     & MIPS16OP_MASK_RY)]
1465               == reg))
1466         return 1;
1467       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1468           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1469                                     & MIPS16OP_MASK_MOVE32Z)]
1470               == reg))
1471         return 1;
1472       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1473         return 1;
1474       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1475         return 1;
1476       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1477         return 1;
1478       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1479           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1480               & MIPS16OP_MASK_REGR32) == reg)
1481         return 1;
1482     }
1483
1484   return 0;
1485 }
1486
1487 /* This function returns true if modifying a register requires a
1488    delay.  */
1489
1490 static int
1491 reg_needs_delay (reg)
1492      unsigned int reg;
1493 {
1494   unsigned long prev_pinfo;
1495
1496   prev_pinfo = prev_insn.insn_mo->pinfo;
1497   if (! mips_opts.noreorder
1498       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1499       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1500           || (! gpr_interlocks
1501               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1502     {
1503       /* A load from a coprocessor or from memory.  All load
1504          delays delay the use of general register rt for one
1505          instruction on the r3000.  The r6000 and r4000 use
1506          interlocks.  */
1507       /* Itbl support may require additional care here.  */
1508       know (prev_pinfo & INSN_WRITE_GPR_T);
1509       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1510         return 1;
1511     }
1512
1513   return 0;
1514 }
1515
1516 /* Mark instruction labels in mips16 mode.  This permits the linker to
1517    handle them specially, such as generating jalx instructions when
1518    needed.  We also make them odd for the duration of the assembly, in
1519    order to generate the right sort of code.  We will make them even
1520    in the adjust_symtab routine, while leaving them marked.  This is
1521    convenient for the debugger and the disassembler.  The linker knows
1522    to make them odd again.  */
1523
1524 static void
1525 mips16_mark_labels ()
1526 {
1527   if (mips_opts.mips16)
1528     {
1529       struct insn_label_list *l;
1530       valueT val;
1531
1532       for (l = insn_labels; l != NULL; l = l->next)
1533         {
1534 #ifdef OBJ_ELF
1535           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1536             S_SET_OTHER (l->label, STO_MIPS16);
1537 #endif
1538           val = S_GET_VALUE (l->label);
1539           if ((val & 1) == 0)
1540             S_SET_VALUE (l->label, val + 1);
1541         }
1542     }
1543 }
1544
1545 /* Output an instruction.  PLACE is where to put the instruction; if
1546    it is NULL, this uses frag_more to get room.  IP is the instruction
1547    information.  ADDRESS_EXPR is an operand of the instruction to be
1548    used with RELOC_TYPE.  */
1549
1550 static void
1551 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1552      char *place;
1553      struct mips_cl_insn *ip;
1554      expressionS *address_expr;
1555      bfd_reloc_code_real_type *reloc_type;
1556      boolean unmatched_hi;
1557 {
1558   register unsigned long prev_pinfo, pinfo;
1559   char *f;
1560   fixS *fixp[3];
1561   int nops = 0;
1562
1563   /* Mark instruction labels in mips16 mode.  */
1564   mips16_mark_labels ();
1565
1566   prev_pinfo = prev_insn.insn_mo->pinfo;
1567   pinfo = ip->insn_mo->pinfo;
1568
1569   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1570     {
1571       int prev_prev_nop;
1572
1573       /* If the previous insn required any delay slots, see if we need
1574          to insert a NOP or two.  There are eight kinds of possible
1575          hazards, of which an instruction can have at most one type.
1576          (1) a load from memory delay
1577          (2) a load from a coprocessor delay
1578          (3) an unconditional branch delay
1579          (4) a conditional branch delay
1580          (5) a move to coprocessor register delay
1581          (6) a load coprocessor register from memory delay
1582          (7) a coprocessor condition code delay
1583          (8) a HI/LO special register delay
1584
1585          There are a lot of optimizations we could do that we don't.
1586          In particular, we do not, in general, reorder instructions.
1587          If you use gcc with optimization, it will reorder
1588          instructions and generally do much more optimization then we
1589          do here; repeating all that work in the assembler would only
1590          benefit hand written assembly code, and does not seem worth
1591          it.  */
1592
1593       /* This is how a NOP is emitted.  */
1594 #define emit_nop()                                      \
1595   (mips_opts.mips16                                     \
1596    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1597    : md_number_to_chars (frag_more (4), 0, 4))
1598
1599       /* The previous insn might require a delay slot, depending upon
1600          the contents of the current insn.  */
1601       if (! mips_opts.mips16
1602           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1603           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1604                && ! cop_interlocks)
1605               || (! gpr_interlocks
1606                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1607         {
1608           /* A load from a coprocessor or from memory.  All load
1609              delays delay the use of general register rt for one
1610              instruction on the r3000.  The r6000 and r4000 use
1611              interlocks.  */
1612           /* Itbl support may require additional care here.  */
1613           know (prev_pinfo & INSN_WRITE_GPR_T);
1614           if (mips_optimize == 0
1615               || insn_uses_reg (ip,
1616                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1617                                  & OP_MASK_RT),
1618                                 MIPS_GR_REG))
1619             ++nops;
1620         }
1621       else if (! mips_opts.mips16
1622                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1623                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1624                     && ! cop_interlocks)
1625                    || (mips_opts.isa == ISA_MIPS1
1626                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1627         {
1628           /* A generic coprocessor delay.  The previous instruction
1629              modified a coprocessor general or control register.  If
1630              it modified a control register, we need to avoid any
1631              coprocessor instruction (this is probably not always
1632              required, but it sometimes is).  If it modified a general
1633              register, we avoid using that register.
1634
1635              On the r6000 and r4000 loading a coprocessor register
1636              from memory is interlocked, and does not require a delay.
1637
1638              This case is not handled very well.  There is no special
1639              knowledge of CP0 handling, and the coprocessors other
1640              than the floating point unit are not distinguished at
1641              all.  */
1642           /* Itbl support may require additional care here. FIXME!
1643              Need to modify this to include knowledge about
1644              user specified delays!  */
1645           if (prev_pinfo & INSN_WRITE_FPR_T)
1646             {
1647               if (mips_optimize == 0
1648                   || insn_uses_reg (ip,
1649                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1650                                      & OP_MASK_FT),
1651                                     MIPS_FP_REG))
1652                 ++nops;
1653             }
1654           else if (prev_pinfo & INSN_WRITE_FPR_S)
1655             {
1656               if (mips_optimize == 0
1657                   || insn_uses_reg (ip,
1658                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1659                                      & OP_MASK_FS),
1660                                     MIPS_FP_REG))
1661                 ++nops;
1662             }
1663           else
1664             {
1665               /* We don't know exactly what the previous instruction
1666                  does.  If the current instruction uses a coprocessor
1667                  register, we must insert a NOP.  If previous
1668                  instruction may set the condition codes, and the
1669                  current instruction uses them, we must insert two
1670                  NOPS.  */
1671               /* Itbl support may require additional care here.  */
1672               if (mips_optimize == 0
1673                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1674                       && (pinfo & INSN_READ_COND_CODE)))
1675                 nops += 2;
1676               else if (pinfo & INSN_COP)
1677                 ++nops;
1678             }
1679         }
1680       else if (! mips_opts.mips16
1681                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1682                && (prev_pinfo & INSN_WRITE_COND_CODE)
1683                && ! cop_interlocks)
1684         {
1685           /* The previous instruction sets the coprocessor condition
1686              codes, but does not require a general coprocessor delay
1687              (this means it is a floating point comparison
1688              instruction).  If this instruction uses the condition
1689              codes, we need to insert a single NOP.  */
1690           /* Itbl support may require additional care here.  */
1691           if (mips_optimize == 0
1692               || (pinfo & INSN_READ_COND_CODE))
1693             ++nops;
1694         }
1695
1696       /* If we're fixing up mfhi/mflo for the r7000 and the
1697          previous insn was an mfhi/mflo and the current insn
1698          reads the register that the mfhi/mflo wrote to, then
1699          insert two nops.  */
1700
1701       else if (mips_7000_hilo_fix
1702                && MF_HILO_INSN (prev_pinfo)
1703                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1704                                       & OP_MASK_RD),
1705                                  MIPS_GR_REG))
1706         {
1707           nops += 2;
1708         }
1709
1710       /* If we're fixing up mfhi/mflo for the r7000 and the
1711          2nd previous insn was an mfhi/mflo and the current insn
1712          reads the register that the mfhi/mflo wrote to, then
1713          insert one nop.  */
1714
1715       else if (mips_7000_hilo_fix
1716                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1717                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1718                                        & OP_MASK_RD),
1719                                     MIPS_GR_REG))
1720
1721         {
1722           ++nops;
1723         }
1724
1725       else if (prev_pinfo & INSN_READ_LO)
1726         {
1727           /* The previous instruction reads the LO register; if the
1728              current instruction writes to the LO register, we must
1729              insert two NOPS.  Some newer processors have interlocks.
1730              Also the tx39's multiply instructions can be exectuted
1731              immediatly after a read from HI/LO (without the delay),
1732              though the tx39's divide insns still do require the
1733              delay.  */
1734           if (! (hilo_interlocks
1735                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1736               && (mips_optimize == 0
1737                   || (pinfo & INSN_WRITE_LO)))
1738             nops += 2;
1739           /* Most mips16 branch insns don't have a delay slot.
1740              If a read from LO is immediately followed by a branch
1741              to a write to LO we have a read followed by a write
1742              less than 2 insns away.  We assume the target of
1743              a branch might be a write to LO, and insert a nop
1744              between a read and an immediately following branch.  */
1745           else if (mips_opts.mips16
1746                    && (mips_optimize == 0
1747                        || (pinfo & MIPS16_INSN_BRANCH)))
1748             ++nops;
1749         }
1750       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1751         {
1752           /* The previous instruction reads the HI register; if the
1753              current instruction writes to the HI register, we must
1754              insert a NOP.  Some newer processors have interlocks.
1755              Also the note tx39's multiply above.  */
1756           if (! (hilo_interlocks
1757                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1758               && (mips_optimize == 0
1759                   || (pinfo & INSN_WRITE_HI)))
1760             nops += 2;
1761           /* Most mips16 branch insns don't have a delay slot.
1762              If a read from HI is immediately followed by a branch
1763              to a write to HI we have a read followed by a write
1764              less than 2 insns away.  We assume the target of
1765              a branch might be a write to HI, and insert a nop
1766              between a read and an immediately following branch.  */
1767           else if (mips_opts.mips16
1768                    && (mips_optimize == 0
1769                        || (pinfo & MIPS16_INSN_BRANCH)))
1770             ++nops;
1771         }
1772
1773       /* If the previous instruction was in a noreorder section, then
1774          we don't want to insert the nop after all.  */
1775       /* Itbl support may require additional care here.  */
1776       if (prev_insn_unreordered)
1777         nops = 0;
1778
1779       /* There are two cases which require two intervening
1780          instructions: 1) setting the condition codes using a move to
1781          coprocessor instruction which requires a general coprocessor
1782          delay and then reading the condition codes 2) reading the HI
1783          or LO register and then writing to it (except on processors
1784          which have interlocks).  If we are not already emitting a NOP
1785          instruction, we must check for these cases compared to the
1786          instruction previous to the previous instruction.  */
1787       if ((! mips_opts.mips16
1788            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1789            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1790            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1791            && (pinfo & INSN_READ_COND_CODE)
1792            && ! cop_interlocks)
1793           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1794               && (pinfo & INSN_WRITE_LO)
1795               && ! (hilo_interlocks
1796                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1797           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1798               && (pinfo & INSN_WRITE_HI)
1799               && ! (hilo_interlocks
1800                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1801         prev_prev_nop = 1;
1802       else
1803         prev_prev_nop = 0;
1804
1805       if (prev_prev_insn_unreordered)
1806         prev_prev_nop = 0;
1807
1808       if (prev_prev_nop && nops == 0)
1809         ++nops;
1810
1811       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1812         {
1813           /* We're out of bits in pinfo, so we must resort to string
1814              ops here.  Shortcuts are selected based on opcodes being
1815              limited to the VR4122 instruction set.  */
1816           int min_nops = 0;
1817           const char *pn = prev_insn.insn_mo->name;
1818           const char *tn = ip->insn_mo->name;
1819           if (strncmp(pn, "macc", 4) == 0
1820               || strncmp(pn, "dmacc", 5) == 0)
1821             {
1822               /* Errata 21 - [D]DIV[U] after [D]MACC */
1823               if (strstr (tn, "div"))
1824                 {
1825                   min_nops = 1;
1826                 }
1827
1828               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1829               if (pn[0] == 'd' /* dmacc */
1830                   && (strncmp(tn, "dmult", 5) == 0
1831                       || strncmp(tn, "dmacc", 5) == 0))
1832                 {
1833                   min_nops = 1;
1834                 }
1835
1836               /* Errata 24 - MT{LO,HI} after [D]MACC */
1837               if (strcmp (tn, "mtlo") == 0
1838                   || strcmp (tn, "mthi") == 0)
1839                 {
1840                   min_nops = 1;
1841                 }
1842
1843             }
1844           else if (strncmp(pn, "dmult", 5) == 0
1845                    && (strncmp(tn, "dmult", 5) == 0
1846                        || strncmp(tn, "dmacc", 5) == 0))
1847             {
1848               /* Here is the rest of errata 23.  */
1849               min_nops = 1;
1850             }
1851           if (nops < min_nops)
1852             nops = min_nops;
1853         }
1854
1855       /* If we are being given a nop instruction, don't bother with
1856          one of the nops we would otherwise output.  This will only
1857          happen when a nop instruction is used with mips_optimize set
1858          to 0.  */
1859       if (nops > 0
1860           && ! mips_opts.noreorder
1861           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1862         --nops;
1863
1864       /* Now emit the right number of NOP instructions.  */
1865       if (nops > 0 && ! mips_opts.noreorder)
1866         {
1867           fragS *old_frag;
1868           unsigned long old_frag_offset;
1869           int i;
1870           struct insn_label_list *l;
1871
1872           old_frag = frag_now;
1873           old_frag_offset = frag_now_fix ();
1874
1875           for (i = 0; i < nops; i++)
1876             emit_nop ();
1877
1878           if (listing)
1879             {
1880               listing_prev_line ();
1881               /* We may be at the start of a variant frag.  In case we
1882                  are, make sure there is enough space for the frag
1883                  after the frags created by listing_prev_line.  The
1884                  argument to frag_grow here must be at least as large
1885                  as the argument to all other calls to frag_grow in
1886                  this file.  We don't have to worry about being in the
1887                  middle of a variant frag, because the variants insert
1888                  all needed nop instructions themselves.  */
1889               frag_grow (40);
1890             }
1891
1892           for (l = insn_labels; l != NULL; l = l->next)
1893             {
1894               valueT val;
1895
1896               assert (S_GET_SEGMENT (l->label) == now_seg);
1897               symbol_set_frag (l->label, frag_now);
1898               val = (valueT) frag_now_fix ();
1899               /* mips16 text labels are stored as odd.  */
1900               if (mips_opts.mips16)
1901                 ++val;
1902               S_SET_VALUE (l->label, val);
1903             }
1904
1905 #ifndef NO_ECOFF_DEBUGGING
1906           if (ECOFF_DEBUGGING)
1907             ecoff_fix_loc (old_frag, old_frag_offset);
1908 #endif
1909         }
1910       else if (prev_nop_frag != NULL)
1911         {
1912           /* We have a frag holding nops we may be able to remove.  If
1913              we don't need any nops, we can decrease the size of
1914              prev_nop_frag by the size of one instruction.  If we do
1915              need some nops, we count them in prev_nops_required.  */
1916           if (prev_nop_frag_since == 0)
1917             {
1918               if (nops == 0)
1919                 {
1920                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1921                   --prev_nop_frag_holds;
1922                 }
1923               else
1924                 prev_nop_frag_required += nops;
1925             }
1926           else
1927             {
1928               if (prev_prev_nop == 0)
1929                 {
1930                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1931                   --prev_nop_frag_holds;
1932                 }
1933               else
1934                 ++prev_nop_frag_required;
1935             }
1936
1937           if (prev_nop_frag_holds <= prev_nop_frag_required)
1938             prev_nop_frag = NULL;
1939
1940           ++prev_nop_frag_since;
1941
1942           /* Sanity check: by the time we reach the second instruction
1943              after prev_nop_frag, we should have used up all the nops
1944              one way or another.  */
1945           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1946         }
1947     }
1948
1949   if (place == NULL
1950       && address_expr
1951       && ((*reloc_type == BFD_RELOC_16_PCREL
1952            && address_expr->X_op != O_constant)
1953           || *reloc_type == BFD_RELOC_16_PCREL_S2)
1954       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1955           || pinfo & INSN_COND_BRANCH_LIKELY)
1956       && mips_relax_branch
1957       /* Don't try branch relaxation within .set nomacro, or within
1958          .set noat if we use $at for PIC computations.  If it turns
1959          out that the branch was out-of-range, we'll get an error.  */
1960       && !mips_opts.warn_about_macros
1961       && !(mips_opts.noat && mips_pic != NO_PIC)
1962       && !mips_opts.mips16)
1963     {
1964       f = frag_var (rs_machine_dependent,
1965                     relaxed_branch_length
1966                     (NULL, NULL,
1967                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1968                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1969                     RELAX_BRANCH_ENCODE
1970                     (*reloc_type == BFD_RELOC_16_PCREL_S2,
1971                      pinfo & INSN_UNCOND_BRANCH_DELAY,
1972                      pinfo & INSN_COND_BRANCH_LIKELY,
1973                      pinfo & INSN_WRITE_GPR_31,
1974                      0),
1975                     address_expr->X_add_symbol,
1976                     address_expr->X_add_number,
1977                     0);
1978       *reloc_type = BFD_RELOC_UNUSED;
1979     }
1980   else if (*reloc_type > BFD_RELOC_UNUSED)
1981     {
1982       /* We need to set up a variant frag.  */
1983       assert (mips_opts.mips16 && address_expr != NULL);
1984       f = frag_var (rs_machine_dependent, 4, 0,
1985                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1986                                          mips16_small, mips16_ext,
1987                                          (prev_pinfo
1988                                           & INSN_UNCOND_BRANCH_DELAY),
1989                                          (*prev_insn_reloc_type
1990                                           == BFD_RELOC_MIPS16_JMP)),
1991                     make_expr_symbol (address_expr), 0, NULL);
1992     }
1993   else if (place != NULL)
1994     f = place;
1995   else if (mips_opts.mips16
1996            && ! ip->use_extend
1997            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1998     {
1999       /* Make sure there is enough room to swap this instruction with
2000          a following jump instruction.  */
2001       frag_grow (6);
2002       f = frag_more (2);
2003     }
2004   else
2005     {
2006       if (mips_opts.mips16
2007           && mips_opts.noreorder
2008           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2009         as_warn (_("extended instruction in delay slot"));
2010
2011       f = frag_more (4);
2012     }
2013
2014   fixp[0] = fixp[1] = fixp[2] = NULL;
2015   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2016     {
2017       if (address_expr->X_op == O_constant)
2018         {
2019           valueT tmp;
2020
2021           switch (*reloc_type)
2022             {
2023             case BFD_RELOC_32:
2024               ip->insn_opcode |= address_expr->X_add_number;
2025               break;
2026
2027             case BFD_RELOC_MIPS_HIGHEST:
2028               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2029               tmp >>= 16;
2030               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2031               break;
2032
2033             case BFD_RELOC_MIPS_HIGHER:
2034               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2035               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2036               break;
2037
2038             case BFD_RELOC_HI16_S:
2039               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2040                                   >> 16) & 0xffff;
2041               break;
2042
2043             case BFD_RELOC_HI16:
2044               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2045               break;
2046
2047             case BFD_RELOC_LO16:
2048             case BFD_RELOC_MIPS_GOT_DISP:
2049               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2050               break;
2051
2052             case BFD_RELOC_MIPS_JMP:
2053               if ((address_expr->X_add_number & 3) != 0)
2054                 as_bad (_("jump to misaligned address (0x%lx)"),
2055                         (unsigned long) address_expr->X_add_number);
2056               if (address_expr->X_add_number & ~0xfffffff)
2057                 as_bad (_("jump address range overflow (0x%lx)"),
2058                         (unsigned long) address_expr->X_add_number);
2059               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2060               break;
2061
2062             case BFD_RELOC_MIPS16_JMP:
2063               if ((address_expr->X_add_number & 3) != 0)
2064                 as_bad (_("jump to misaligned address (0x%lx)"),
2065                         (unsigned long) address_expr->X_add_number);
2066               if (address_expr->X_add_number & ~0xfffffff)
2067                 as_bad (_("jump address range overflow (0x%lx)"),
2068                         (unsigned long) address_expr->X_add_number);
2069               ip->insn_opcode |=
2070                 (((address_expr->X_add_number & 0x7c0000) << 3)
2071                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2072                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2073               break;
2074
2075             case BFD_RELOC_16_PCREL:
2076               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2077               break;
2078
2079             case BFD_RELOC_16_PCREL_S2:
2080               goto need_reloc;
2081
2082             default:
2083               internalError ();
2084             }
2085         }
2086       else
2087         {
2088         need_reloc:
2089           /* Don't generate a reloc if we are writing into a variant frag.  */
2090           if (place == NULL)
2091             {
2092               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2093                                      address_expr,
2094                                      (*reloc_type == BFD_RELOC_16_PCREL
2095                                       || *reloc_type == BFD_RELOC_16_PCREL_S2),
2096                                      reloc_type[0]);
2097
2098               /* These relocations can have an addend that won't fit in
2099                  4 octets for 64bit assembly.  */
2100               if (HAVE_64BIT_GPRS &&
2101                   (*reloc_type == BFD_RELOC_16
2102                    || *reloc_type == BFD_RELOC_32
2103                    || *reloc_type == BFD_RELOC_MIPS_JMP
2104                    || *reloc_type == BFD_RELOC_HI16_S
2105                    || *reloc_type == BFD_RELOC_LO16
2106                    || *reloc_type == BFD_RELOC_GPREL16
2107                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2108                    || *reloc_type == BFD_RELOC_GPREL32
2109                    || *reloc_type == BFD_RELOC_64
2110                    || *reloc_type == BFD_RELOC_CTOR
2111                    || *reloc_type == BFD_RELOC_MIPS_SUB
2112                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2113                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2114                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2115                    || *reloc_type == BFD_RELOC_MIPS_REL16
2116                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2117                 fixp[0]->fx_no_overflow = 1;
2118
2119               if (unmatched_hi)
2120                 {
2121                   struct mips_hi_fixup *hi_fixup;
2122
2123                   assert (*reloc_type == BFD_RELOC_HI16_S);
2124                   hi_fixup = ((struct mips_hi_fixup *)
2125                               xmalloc (sizeof (struct mips_hi_fixup)));
2126                   hi_fixup->fixp = fixp[0];
2127                   hi_fixup->seg = now_seg;
2128                   hi_fixup->next = mips_hi_fixup_list;
2129                   mips_hi_fixup_list = hi_fixup;
2130                 }
2131
2132               if (reloc_type[1] != BFD_RELOC_UNUSED)
2133                 {
2134                   /* FIXME: This symbol can be one of
2135                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2136                   address_expr->X_op = O_absent;
2137                   address_expr->X_add_symbol = 0;
2138                   address_expr->X_add_number = 0;
2139
2140                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2141                                          4, address_expr, false,
2142                                          reloc_type[1]);
2143
2144                   /* These relocations can have an addend that won't fit in
2145                      4 octets for 64bit assembly.  */
2146                   if (HAVE_64BIT_GPRS &&
2147                       (*reloc_type == BFD_RELOC_16
2148                        || *reloc_type == BFD_RELOC_32
2149                        || *reloc_type == BFD_RELOC_MIPS_JMP
2150                        || *reloc_type == BFD_RELOC_HI16_S
2151                        || *reloc_type == BFD_RELOC_LO16
2152                        || *reloc_type == BFD_RELOC_GPREL16
2153                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2154                        || *reloc_type == BFD_RELOC_GPREL32
2155                        || *reloc_type == BFD_RELOC_64
2156                        || *reloc_type == BFD_RELOC_CTOR
2157                        || *reloc_type == BFD_RELOC_MIPS_SUB
2158                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2159                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2160                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2161                        || *reloc_type == BFD_RELOC_MIPS_REL16
2162                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2163                     fixp[1]->fx_no_overflow = 1;
2164
2165                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2166                     {
2167                       address_expr->X_op = O_absent;
2168                       address_expr->X_add_symbol = 0;
2169                       address_expr->X_add_number = 0;
2170
2171                       fixp[2] = fix_new_exp (frag_now,
2172                                              f - frag_now->fr_literal, 4,
2173                                              address_expr, false,
2174                                              reloc_type[2]);
2175
2176                       /* These relocations can have an addend that won't fit in
2177                          4 octets for 64bit assembly.  */
2178                       if (HAVE_64BIT_GPRS &&
2179                           (*reloc_type == BFD_RELOC_16
2180                            || *reloc_type == BFD_RELOC_32
2181                            || *reloc_type == BFD_RELOC_MIPS_JMP
2182                            || *reloc_type == BFD_RELOC_HI16_S
2183                            || *reloc_type == BFD_RELOC_LO16
2184                            || *reloc_type == BFD_RELOC_GPREL16
2185                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2186                            || *reloc_type == BFD_RELOC_GPREL32
2187                            || *reloc_type == BFD_RELOC_64
2188                            || *reloc_type == BFD_RELOC_CTOR
2189                            || *reloc_type == BFD_RELOC_MIPS_SUB
2190                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2191                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2192                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2193                            || *reloc_type == BFD_RELOC_MIPS_REL16
2194                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2195                         fixp[2]->fx_no_overflow = 1;
2196                     }
2197                 }
2198             }
2199         }
2200     }
2201
2202   if (! mips_opts.mips16)
2203     {
2204       md_number_to_chars (f, ip->insn_opcode, 4);
2205 #ifdef OBJ_ELF
2206       dwarf2_emit_insn (4);
2207 #endif
2208     }
2209   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2210     {
2211       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2212       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2213 #ifdef OBJ_ELF
2214       dwarf2_emit_insn (4);
2215 #endif
2216     }
2217   else
2218     {
2219       if (ip->use_extend)
2220         {
2221           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2222           f += 2;
2223         }
2224       md_number_to_chars (f, ip->insn_opcode, 2);
2225 #ifdef OBJ_ELF
2226       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2227 #endif
2228     }
2229
2230   /* Update the register mask information.  */
2231   if (! mips_opts.mips16)
2232     {
2233       if (pinfo & INSN_WRITE_GPR_D)
2234         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2235       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2236         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2237       if (pinfo & INSN_READ_GPR_S)
2238         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2239       if (pinfo & INSN_WRITE_GPR_31)
2240         mips_gprmask |= 1 << RA;
2241       if (pinfo & INSN_WRITE_FPR_D)
2242         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2243       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2244         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2245       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2246         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2247       if ((pinfo & INSN_READ_FPR_R) != 0)
2248         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2249       if (pinfo & INSN_COP)
2250         {
2251           /* We don't keep enough information to sort these cases out.
2252              The itbl support does keep this information however, although
2253              we currently don't support itbl fprmats as part of the cop
2254              instruction.  May want to add this support in the future.  */
2255         }
2256       /* Never set the bit for $0, which is always zero.  */
2257       mips_gprmask &= ~1 << 0;
2258     }
2259   else
2260     {
2261       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2262         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2263                               & MIPS16OP_MASK_RX);
2264       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2265         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2266                               & MIPS16OP_MASK_RY);
2267       if (pinfo & MIPS16_INSN_WRITE_Z)
2268         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2269                               & MIPS16OP_MASK_RZ);
2270       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2271         mips_gprmask |= 1 << TREG;
2272       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2273         mips_gprmask |= 1 << SP;
2274       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2275         mips_gprmask |= 1 << RA;
2276       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2277         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2278       if (pinfo & MIPS16_INSN_READ_Z)
2279         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2280                               & MIPS16OP_MASK_MOVE32Z);
2281       if (pinfo & MIPS16_INSN_READ_GPR_X)
2282         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2283                               & MIPS16OP_MASK_REGR32);
2284     }
2285
2286   if (place == NULL && ! mips_opts.noreorder)
2287     {
2288       /* Filling the branch delay slot is more complex.  We try to
2289          switch the branch with the previous instruction, which we can
2290          do if the previous instruction does not set up a condition
2291          that the branch tests and if the branch is not itself the
2292          target of any branch.  */
2293       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2294           || (pinfo & INSN_COND_BRANCH_DELAY))
2295         {
2296           if (mips_optimize < 2
2297               /* If we have seen .set volatile or .set nomove, don't
2298                  optimize.  */
2299               || mips_opts.nomove != 0
2300               /* If we had to emit any NOP instructions, then we
2301                  already know we can not swap.  */
2302               || nops != 0
2303               /* If we don't even know the previous insn, we can not
2304                  swap.  */
2305               || ! prev_insn_valid
2306               /* If the previous insn is already in a branch delay
2307                  slot, then we can not swap.  */
2308               || prev_insn_is_delay_slot
2309               /* If the previous previous insn was in a .set
2310                  noreorder, we can't swap.  Actually, the MIPS
2311                  assembler will swap in this situation.  However, gcc
2312                  configured -with-gnu-as will generate code like
2313                    .set noreorder
2314                    lw   $4,XXX
2315                    .set reorder
2316                    INSN
2317                    bne  $4,$0,foo
2318                  in which we can not swap the bne and INSN.  If gcc is
2319                  not configured -with-gnu-as, it does not output the
2320                  .set pseudo-ops.  We don't have to check
2321                  prev_insn_unreordered, because prev_insn_valid will
2322                  be 0 in that case.  We don't want to use
2323                  prev_prev_insn_valid, because we do want to be able
2324                  to swap at the start of a function.  */
2325               || prev_prev_insn_unreordered
2326               /* If the branch is itself the target of a branch, we
2327                  can not swap.  We cheat on this; all we check for is
2328                  whether there is a label on this instruction.  If
2329                  there are any branches to anything other than a
2330                  label, users must use .set noreorder.  */
2331               || insn_labels != NULL
2332               /* If the previous instruction is in a variant frag, we
2333                  can not do the swap.  This does not apply to the
2334                  mips16, which uses variant frags for different
2335                  purposes.  */
2336               || (! mips_opts.mips16
2337                   && prev_insn_frag->fr_type == rs_machine_dependent)
2338               /* If the branch reads the condition codes, we don't
2339                  even try to swap, because in the sequence
2340                    ctc1 $X,$31
2341                    INSN
2342                    INSN
2343                    bc1t LABEL
2344                  we can not swap, and I don't feel like handling that
2345                  case.  */
2346               || (! mips_opts.mips16
2347                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2348                   && (pinfo & INSN_READ_COND_CODE))
2349               /* We can not swap with an instruction that requires a
2350                  delay slot, becase the target of the branch might
2351                  interfere with that instruction.  */
2352               || (! mips_opts.mips16
2353                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2354                   && (prev_pinfo
2355               /* Itbl support may require additional care here.  */
2356                       & (INSN_LOAD_COPROC_DELAY
2357                          | INSN_COPROC_MOVE_DELAY
2358                          | INSN_WRITE_COND_CODE)))
2359               || (! (hilo_interlocks
2360                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2361                   && (prev_pinfo
2362                       & (INSN_READ_LO
2363                          | INSN_READ_HI)))
2364               || (! mips_opts.mips16
2365                   && ! gpr_interlocks
2366                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2367               || (! mips_opts.mips16
2368                   && mips_opts.isa == ISA_MIPS1
2369                   /* Itbl support may require additional care here.  */
2370                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2371               /* We can not swap with a branch instruction.  */
2372               || (prev_pinfo
2373                   & (INSN_UNCOND_BRANCH_DELAY
2374                      | INSN_COND_BRANCH_DELAY
2375                      | INSN_COND_BRANCH_LIKELY))
2376               /* We do not swap with a trap instruction, since it
2377                  complicates trap handlers to have the trap
2378                  instruction be in a delay slot.  */
2379               || (prev_pinfo & INSN_TRAP)
2380               /* If the branch reads a register that the previous
2381                  instruction sets, we can not swap.  */
2382               || (! mips_opts.mips16
2383                   && (prev_pinfo & INSN_WRITE_GPR_T)
2384                   && insn_uses_reg (ip,
2385                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2386                                      & OP_MASK_RT),
2387                                     MIPS_GR_REG))
2388               || (! mips_opts.mips16
2389                   && (prev_pinfo & INSN_WRITE_GPR_D)
2390                   && insn_uses_reg (ip,
2391                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2392                                      & OP_MASK_RD),
2393                                     MIPS_GR_REG))
2394               || (mips_opts.mips16
2395                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2396                        && insn_uses_reg (ip,
2397                                          ((prev_insn.insn_opcode
2398                                            >> MIPS16OP_SH_RX)
2399                                           & MIPS16OP_MASK_RX),
2400                                          MIPS16_REG))
2401                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2402                           && insn_uses_reg (ip,
2403                                             ((prev_insn.insn_opcode
2404                                               >> MIPS16OP_SH_RY)
2405                                              & MIPS16OP_MASK_RY),
2406                                             MIPS16_REG))
2407                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2408                           && insn_uses_reg (ip,
2409                                             ((prev_insn.insn_opcode
2410                                               >> MIPS16OP_SH_RZ)
2411                                              & MIPS16OP_MASK_RZ),
2412                                             MIPS16_REG))
2413                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2414                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2415                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2416                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2417                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2418                           && insn_uses_reg (ip,
2419                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2420                                                                      insn_opcode),
2421                                             MIPS_GR_REG))))
2422               /* If the branch writes a register that the previous
2423                  instruction sets, we can not swap (we know that
2424                  branches write only to RD or to $31).  */
2425               || (! mips_opts.mips16
2426                   && (prev_pinfo & INSN_WRITE_GPR_T)
2427                   && (((pinfo & INSN_WRITE_GPR_D)
2428                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2429                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2430                       || ((pinfo & INSN_WRITE_GPR_31)
2431                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2432                                & OP_MASK_RT)
2433                               == RA))))
2434               || (! mips_opts.mips16
2435                   && (prev_pinfo & INSN_WRITE_GPR_D)
2436                   && (((pinfo & INSN_WRITE_GPR_D)
2437                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2438                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2439                       || ((pinfo & INSN_WRITE_GPR_31)
2440                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2441                                & OP_MASK_RD)
2442                               == RA))))
2443               || (mips_opts.mips16
2444                   && (pinfo & MIPS16_INSN_WRITE_31)
2445                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2446                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2447                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2448                               == RA))))
2449               /* If the branch writes a register that the previous
2450                  instruction reads, we can not swap (we know that
2451                  branches only write to RD or to $31).  */
2452               || (! mips_opts.mips16
2453                   && (pinfo & INSN_WRITE_GPR_D)
2454                   && insn_uses_reg (&prev_insn,
2455                                     ((ip->insn_opcode >> OP_SH_RD)
2456                                      & OP_MASK_RD),
2457                                     MIPS_GR_REG))
2458               || (! mips_opts.mips16
2459                   && (pinfo & INSN_WRITE_GPR_31)
2460                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2461               || (mips_opts.mips16
2462                   && (pinfo & MIPS16_INSN_WRITE_31)
2463                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2464               /* If we are generating embedded PIC code, the branch
2465                  might be expanded into a sequence which uses $at, so
2466                  we can't swap with an instruction which reads it.  */
2467               || (mips_pic == EMBEDDED_PIC
2468                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2469               /* If the previous previous instruction has a load
2470                  delay, and sets a register that the branch reads, we
2471                  can not swap.  */
2472               || (! mips_opts.mips16
2473                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2474               /* Itbl support may require additional care here.  */
2475                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2476                       || (! gpr_interlocks
2477                           && (prev_prev_insn.insn_mo->pinfo
2478                               & INSN_LOAD_MEMORY_DELAY)))
2479                   && insn_uses_reg (ip,
2480                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2481                                      & OP_MASK_RT),
2482                                     MIPS_GR_REG))
2483               /* If one instruction sets a condition code and the
2484                  other one uses a condition code, we can not swap.  */
2485               || ((pinfo & INSN_READ_COND_CODE)
2486                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2487               || ((pinfo & INSN_WRITE_COND_CODE)
2488                   && (prev_pinfo & INSN_READ_COND_CODE))
2489               /* If the previous instruction uses the PC, we can not
2490                  swap.  */
2491               || (mips_opts.mips16
2492                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2493               /* If the previous instruction was extended, we can not
2494                  swap.  */
2495               || (mips_opts.mips16 && prev_insn_extended)
2496               /* If the previous instruction had a fixup in mips16
2497                  mode, we can not swap.  This normally means that the
2498                  previous instruction was a 4 byte branch anyhow.  */
2499               || (mips_opts.mips16 && prev_insn_fixp[0])
2500               /* If the previous instruction is a sync, sync.l, or
2501                  sync.p, we can not swap.  */
2502               || (prev_pinfo & INSN_SYNC))
2503             {
2504               /* We could do even better for unconditional branches to
2505                  portions of this object file; we could pick up the
2506                  instruction at the destination, put it in the delay
2507                  slot, and bump the destination address.  */
2508               emit_nop ();
2509               /* Update the previous insn information.  */
2510               prev_prev_insn = *ip;
2511               prev_insn.insn_mo = &dummy_opcode;
2512             }
2513           else
2514             {
2515               /* It looks like we can actually do the swap.  */
2516               if (! mips_opts.mips16)
2517                 {
2518                   char *prev_f;
2519                   char temp[4];
2520
2521                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2522                   memcpy (temp, prev_f, 4);
2523                   memcpy (prev_f, f, 4);
2524                   memcpy (f, temp, 4);
2525                   if (prev_insn_fixp[0])
2526                     {
2527                       prev_insn_fixp[0]->fx_frag = frag_now;
2528                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2529                     }
2530                   if (prev_insn_fixp[1])
2531                     {
2532                       prev_insn_fixp[1]->fx_frag = frag_now;
2533                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2534                     }
2535                   if (prev_insn_fixp[2])
2536                     {
2537                       prev_insn_fixp[2]->fx_frag = frag_now;
2538                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2539                     }
2540                   if (fixp[0])
2541                     {
2542                       fixp[0]->fx_frag = prev_insn_frag;
2543                       fixp[0]->fx_where = prev_insn_where;
2544                     }
2545                   if (fixp[1])
2546                     {
2547                       fixp[1]->fx_frag = prev_insn_frag;
2548                       fixp[1]->fx_where = prev_insn_where;
2549                     }
2550                   if (fixp[2])
2551                     {
2552                       fixp[2]->fx_frag = prev_insn_frag;
2553                       fixp[2]->fx_where = prev_insn_where;
2554                     }
2555                 }
2556               else
2557                 {
2558                   char *prev_f;
2559                   char temp[2];
2560
2561                   assert (prev_insn_fixp[0] == NULL);
2562                   assert (prev_insn_fixp[1] == NULL);
2563                   assert (prev_insn_fixp[2] == NULL);
2564                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2565                   memcpy (temp, prev_f, 2);
2566                   memcpy (prev_f, f, 2);
2567                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2568                     {
2569                       assert (*reloc_type == BFD_RELOC_UNUSED);
2570                       memcpy (f, temp, 2);
2571                     }
2572                   else
2573                     {
2574                       memcpy (f, f + 2, 2);
2575                       memcpy (f + 2, temp, 2);
2576                     }
2577                   if (fixp[0])
2578                     {
2579                       fixp[0]->fx_frag = prev_insn_frag;
2580                       fixp[0]->fx_where = prev_insn_where;
2581                     }
2582                   if (fixp[1])
2583                     {
2584                       fixp[1]->fx_frag = prev_insn_frag;
2585                       fixp[1]->fx_where = prev_insn_where;
2586                     }
2587                   if (fixp[2])
2588                     {
2589                       fixp[2]->fx_frag = prev_insn_frag;
2590                       fixp[2]->fx_where = prev_insn_where;
2591                     }
2592                 }
2593
2594               /* Update the previous insn information; leave prev_insn
2595                  unchanged.  */
2596               prev_prev_insn = *ip;
2597             }
2598           prev_insn_is_delay_slot = 1;
2599
2600           /* If that was an unconditional branch, forget the previous
2601              insn information.  */
2602           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2603             {
2604               prev_prev_insn.insn_mo = &dummy_opcode;
2605               prev_insn.insn_mo = &dummy_opcode;
2606             }
2607
2608           prev_insn_fixp[0] = NULL;
2609           prev_insn_fixp[1] = NULL;
2610           prev_insn_fixp[2] = NULL;
2611           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2612           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2613           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2614           prev_insn_extended = 0;
2615         }
2616       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2617         {
2618           /* We don't yet optimize a branch likely.  What we should do
2619              is look at the target, copy the instruction found there
2620              into the delay slot, and increment the branch to jump to
2621              the next instruction.  */
2622           emit_nop ();
2623           /* Update the previous insn information.  */
2624           prev_prev_insn = *ip;
2625           prev_insn.insn_mo = &dummy_opcode;
2626           prev_insn_fixp[0] = NULL;
2627           prev_insn_fixp[1] = NULL;
2628           prev_insn_fixp[2] = NULL;
2629           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2630           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2631           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2632           prev_insn_extended = 0;
2633         }
2634       else
2635         {
2636           /* Update the previous insn information.  */
2637           if (nops > 0)
2638             prev_prev_insn.insn_mo = &dummy_opcode;
2639           else
2640             prev_prev_insn = prev_insn;
2641           prev_insn = *ip;
2642
2643           /* Any time we see a branch, we always fill the delay slot
2644              immediately; since this insn is not a branch, we know it
2645              is not in a delay slot.  */
2646           prev_insn_is_delay_slot = 0;
2647
2648           prev_insn_fixp[0] = fixp[0];
2649           prev_insn_fixp[1] = fixp[1];
2650           prev_insn_fixp[2] = fixp[2];
2651           prev_insn_reloc_type[0] = reloc_type[0];
2652           prev_insn_reloc_type[1] = reloc_type[1];
2653           prev_insn_reloc_type[2] = reloc_type[2];
2654           if (mips_opts.mips16)
2655             prev_insn_extended = (ip->use_extend
2656                                   || *reloc_type > BFD_RELOC_UNUSED);
2657         }
2658
2659       prev_prev_insn_unreordered = prev_insn_unreordered;
2660       prev_insn_unreordered = 0;
2661       prev_insn_frag = frag_now;
2662       prev_insn_where = f - frag_now->fr_literal;
2663       prev_insn_valid = 1;
2664     }
2665   else if (place == NULL)
2666     {
2667       /* We need to record a bit of information even when we are not
2668          reordering, in order to determine the base address for mips16
2669          PC relative relocs.  */
2670       prev_prev_insn = prev_insn;
2671       prev_insn = *ip;
2672       prev_insn_reloc_type[0] = reloc_type[0];
2673       prev_insn_reloc_type[1] = reloc_type[1];
2674       prev_insn_reloc_type[2] = reloc_type[2];
2675       prev_prev_insn_unreordered = prev_insn_unreordered;
2676       prev_insn_unreordered = 1;
2677     }
2678
2679   /* We just output an insn, so the next one doesn't have a label.  */
2680   mips_clear_insn_labels ();
2681
2682   /* We must ensure that a fixup associated with an unmatched %hi
2683      reloc does not become a variant frag.  Otherwise, the
2684      rearrangement of %hi relocs in frob_file may confuse
2685      tc_gen_reloc.  */
2686   if (unmatched_hi)
2687     {
2688       frag_wane (frag_now);
2689       frag_new (0);
2690     }
2691 }
2692
2693 /* This function forgets that there was any previous instruction or
2694    label.  If PRESERVE is non-zero, it remembers enough information to
2695    know whether nops are needed before a noreorder section.  */
2696
2697 static void
2698 mips_no_prev_insn (preserve)
2699      int preserve;
2700 {
2701   if (! preserve)
2702     {
2703       prev_insn.insn_mo = &dummy_opcode;
2704       prev_prev_insn.insn_mo = &dummy_opcode;
2705       prev_nop_frag = NULL;
2706       prev_nop_frag_holds = 0;
2707       prev_nop_frag_required = 0;
2708       prev_nop_frag_since = 0;
2709     }
2710   prev_insn_valid = 0;
2711   prev_insn_is_delay_slot = 0;
2712   prev_insn_unreordered = 0;
2713   prev_insn_extended = 0;
2714   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2715   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2716   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2717   prev_prev_insn_unreordered = 0;
2718   mips_clear_insn_labels ();
2719 }
2720
2721 /* This function must be called whenever we turn on noreorder or emit
2722    something other than instructions.  It inserts any NOPS which might
2723    be needed by the previous instruction, and clears the information
2724    kept for the previous instructions.  The INSNS parameter is true if
2725    instructions are to follow.  */
2726
2727 static void
2728 mips_emit_delays (insns)
2729      boolean insns;
2730 {
2731   if (! mips_opts.noreorder)
2732     {
2733       int nops;
2734
2735       nops = 0;
2736       if ((! mips_opts.mips16
2737            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2738            && (! cop_interlocks
2739                && (prev_insn.insn_mo->pinfo
2740                    & (INSN_LOAD_COPROC_DELAY
2741                       | INSN_COPROC_MOVE_DELAY
2742                       | INSN_WRITE_COND_CODE))))
2743           || (! hilo_interlocks
2744               && (prev_insn.insn_mo->pinfo
2745                   & (INSN_READ_LO
2746                      | INSN_READ_HI)))
2747           || (! mips_opts.mips16
2748               && ! gpr_interlocks
2749               && (prev_insn.insn_mo->pinfo
2750                   & INSN_LOAD_MEMORY_DELAY))
2751           || (! mips_opts.mips16
2752               && mips_opts.isa == ISA_MIPS1
2753               && (prev_insn.insn_mo->pinfo
2754                   & INSN_COPROC_MEMORY_DELAY)))
2755         {
2756           /* Itbl support may require additional care here.  */
2757           ++nops;
2758           if ((! mips_opts.mips16
2759                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2760                && (! cop_interlocks
2761                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2762               || (! hilo_interlocks
2763                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2764                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2765             ++nops;
2766
2767           if (prev_insn_unreordered)
2768             nops = 0;
2769         }
2770       else if ((! mips_opts.mips16
2771                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2772                 && (! cop_interlocks
2773                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2774                || (! hilo_interlocks
2775                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2776                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2777         {
2778           /* Itbl support may require additional care here.  */
2779           if (! prev_prev_insn_unreordered)
2780             ++nops;
2781         }
2782
2783       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2784         {
2785           int min_nops = 0;
2786           const char *pn = prev_insn.insn_mo->name;
2787           if (strncmp(pn, "macc", 4) == 0
2788               || strncmp(pn, "dmacc", 5) == 0
2789               || strncmp(pn, "dmult", 5) == 0)
2790             {
2791               min_nops = 1;
2792             }
2793           if (nops < min_nops)
2794             nops = min_nops;
2795         }
2796
2797       if (nops > 0)
2798         {
2799           struct insn_label_list *l;
2800
2801           if (insns)
2802             {
2803               /* Record the frag which holds the nop instructions, so
2804                  that we can remove them if we don't need them.  */
2805               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2806               prev_nop_frag = frag_now;
2807               prev_nop_frag_holds = nops;
2808               prev_nop_frag_required = 0;
2809               prev_nop_frag_since = 0;
2810             }
2811
2812           for (; nops > 0; --nops)
2813             emit_nop ();
2814
2815           if (insns)
2816             {
2817               /* Move on to a new frag, so that it is safe to simply
2818                  decrease the size of prev_nop_frag.  */
2819               frag_wane (frag_now);
2820               frag_new (0);
2821             }
2822
2823           for (l = insn_labels; l != NULL; l = l->next)
2824             {
2825               valueT val;
2826
2827               assert (S_GET_SEGMENT (l->label) == now_seg);
2828               symbol_set_frag (l->label, frag_now);
2829               val = (valueT) frag_now_fix ();
2830               /* mips16 text labels are stored as odd.  */
2831               if (mips_opts.mips16)
2832                 ++val;
2833               S_SET_VALUE (l->label, val);
2834             }
2835         }
2836     }
2837
2838   /* Mark instruction labels in mips16 mode.  */
2839   if (insns)
2840     mips16_mark_labels ();
2841
2842   mips_no_prev_insn (insns);
2843 }
2844
2845 /* Build an instruction created by a macro expansion.  This is passed
2846    a pointer to the count of instructions created so far, an
2847    expression, the name of the instruction to build, an operand format
2848    string, and corresponding arguments.  */
2849
2850 #ifdef USE_STDARG
2851 static void
2852 macro_build (char *place,
2853              int *counter,
2854              expressionS * ep,
2855              const char *name,
2856              const char *fmt,
2857              ...)
2858 #else
2859 static void
2860 macro_build (place, counter, ep, name, fmt, va_alist)
2861      char *place;
2862      int *counter;
2863      expressionS *ep;
2864      const char *name;
2865      const char *fmt;
2866      va_dcl
2867 #endif
2868 {
2869   struct mips_cl_insn insn;
2870   bfd_reloc_code_real_type r[3];
2871   va_list args;
2872
2873 #ifdef USE_STDARG
2874   va_start (args, fmt);
2875 #else
2876   va_start (args);
2877 #endif
2878
2879   /*
2880    * If the macro is about to expand into a second instruction,
2881    * print a warning if needed. We need to pass ip as a parameter
2882    * to generate a better warning message here...
2883    */
2884   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2885     as_warn (_("Macro instruction expanded into multiple instructions"));
2886
2887   /*
2888    * If the macro is about to expand into a second instruction,
2889    * and it is in a delay slot, print a warning.
2890    */
2891   if (place == NULL
2892       && *counter == 1
2893       && mips_opts.noreorder
2894       && (prev_prev_insn.insn_mo->pinfo
2895           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2896              | INSN_COND_BRANCH_LIKELY)) != 0)
2897     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2898
2899   if (place == NULL)
2900     ++*counter;         /* bump instruction counter */
2901
2902   if (mips_opts.mips16)
2903     {
2904       mips16_macro_build (place, counter, ep, name, fmt, args);
2905       va_end (args);
2906       return;
2907     }
2908
2909   r[0] = BFD_RELOC_UNUSED;
2910   r[1] = BFD_RELOC_UNUSED;
2911   r[2] = BFD_RELOC_UNUSED;
2912   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2913   assert (insn.insn_mo);
2914   assert (strcmp (name, insn.insn_mo->name) == 0);
2915
2916   /* Search until we get a match for NAME.  */
2917   while (1)
2918     {
2919       /* It is assumed here that macros will never generate 
2920          MDMX or MIPS-3D instructions.  */
2921       if (strcmp (fmt, insn.insn_mo->args) == 0
2922           && insn.insn_mo->pinfo != INSN_MACRO
2923           && OPCODE_IS_MEMBER (insn.insn_mo,
2924                                (mips_opts.isa
2925                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2926                                mips_arch)
2927           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2928         break;
2929
2930       ++insn.insn_mo;
2931       assert (insn.insn_mo->name);
2932       assert (strcmp (name, insn.insn_mo->name) == 0);
2933     }
2934
2935   insn.insn_opcode = insn.insn_mo->match;
2936   for (;;)
2937     {
2938       switch (*fmt++)
2939         {
2940         case '\0':
2941           break;
2942
2943         case ',':
2944         case '(':
2945         case ')':
2946           continue;
2947
2948         case 't':
2949         case 'w':
2950         case 'E':
2951           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2952           continue;
2953
2954         case 'c':
2955           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2956           continue;
2957
2958         case 'T':
2959         case 'W':
2960           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2961           continue;
2962
2963         case 'd':
2964         case 'G':
2965           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2966           continue;
2967
2968         case 'U':
2969           {
2970             int tmp = va_arg (args, int);
2971
2972             insn.insn_opcode |= tmp << OP_SH_RT;
2973             insn.insn_opcode |= tmp << OP_SH_RD;
2974             continue;
2975           }
2976
2977         case 'V':
2978         case 'S':
2979           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2980           continue;
2981
2982         case 'z':
2983           continue;
2984
2985         case '<':
2986           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2987           continue;
2988
2989         case 'D':
2990           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2991           continue;
2992
2993         case 'B':
2994           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2995           continue;
2996
2997         case 'J':
2998           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2999           continue;
3000
3001         case 'q':
3002           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3003           continue;
3004
3005         case 'b':
3006         case 's':
3007         case 'r':
3008         case 'v':
3009           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3010           continue;
3011
3012         case 'i':
3013         case 'j':
3014         case 'o':
3015           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3016           assert (*r == BFD_RELOC_GPREL16
3017                   || *r == BFD_RELOC_MIPS_LITERAL
3018                   || *r == BFD_RELOC_MIPS_HIGHER
3019                   || *r == BFD_RELOC_HI16_S
3020                   || *r == BFD_RELOC_LO16
3021                   || *r == BFD_RELOC_MIPS_GOT16
3022                   || *r == BFD_RELOC_MIPS_CALL16
3023                   || *r == BFD_RELOC_MIPS_GOT_DISP
3024                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3025                   || *r == BFD_RELOC_MIPS_GOT_OFST
3026                   || *r == BFD_RELOC_MIPS_GOT_LO16
3027                   || *r == BFD_RELOC_MIPS_CALL_LO16
3028                   || (ep->X_op == O_subtract
3029                       && *r == BFD_RELOC_PCREL_LO16));
3030           continue;
3031
3032         case 'u':
3033           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3034           assert (ep != NULL
3035                   && (ep->X_op == O_constant
3036                       || (ep->X_op == O_symbol
3037                           && (*r == BFD_RELOC_MIPS_HIGHEST
3038                               || *r == BFD_RELOC_HI16_S
3039                               || *r == BFD_RELOC_HI16
3040                               || *r == BFD_RELOC_GPREL16
3041                               || *r == BFD_RELOC_MIPS_GOT_HI16
3042                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3043                       || (ep->X_op == O_subtract
3044                           && *r == BFD_RELOC_PCREL_HI16_S)));
3045           continue;
3046
3047         case 'p':
3048           assert (ep != NULL);
3049           /*
3050            * This allows macro() to pass an immediate expression for
3051            * creating short branches without creating a symbol.
3052            * Note that the expression still might come from the assembly
3053            * input, in which case the value is not checked for range nor
3054            * is a relocation entry generated (yuck).
3055            */
3056           if (ep->X_op == O_constant)
3057             {
3058               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3059               ep = NULL;
3060             }
3061           else
3062             if (mips_pic == EMBEDDED_PIC)
3063               *r = BFD_RELOC_16_PCREL_S2;
3064             else
3065               *r = BFD_RELOC_16_PCREL;
3066           continue;
3067
3068         case 'a':
3069           assert (ep != NULL);
3070           *r = BFD_RELOC_MIPS_JMP;
3071           continue;
3072
3073         case 'C':
3074           insn.insn_opcode |= va_arg (args, unsigned long);
3075           continue;
3076
3077         default:
3078           internalError ();
3079         }
3080       break;
3081     }
3082   va_end (args);
3083   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3084
3085   append_insn (place, &insn, ep, r, false);
3086 }
3087
3088 static void
3089 mips16_macro_build (place, counter, ep, name, fmt, args)
3090      char *place;
3091      int *counter ATTRIBUTE_UNUSED;
3092      expressionS *ep;
3093      const char *name;
3094      const char *fmt;
3095      va_list args;
3096 {
3097   struct mips_cl_insn insn;
3098   bfd_reloc_code_real_type r[3]
3099     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3100
3101   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3102   assert (insn.insn_mo);
3103   assert (strcmp (name, insn.insn_mo->name) == 0);
3104
3105   while (strcmp (fmt, insn.insn_mo->args) != 0
3106          || insn.insn_mo->pinfo == INSN_MACRO)
3107     {
3108       ++insn.insn_mo;
3109       assert (insn.insn_mo->name);
3110       assert (strcmp (name, insn.insn_mo->name) == 0);
3111     }
3112
3113   insn.insn_opcode = insn.insn_mo->match;
3114   insn.use_extend = false;
3115
3116   for (;;)
3117     {
3118       int c;
3119
3120       c = *fmt++;
3121       switch (c)
3122         {
3123         case '\0':
3124           break;
3125
3126         case ',':
3127         case '(':
3128         case ')':
3129           continue;
3130
3131         case 'y':
3132         case 'w':
3133           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3134           continue;
3135
3136         case 'x':
3137         case 'v':
3138           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3139           continue;
3140
3141         case 'z':
3142           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3143           continue;
3144
3145         case 'Z':
3146           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3147           continue;
3148
3149         case '0':
3150         case 'S':
3151         case 'P':
3152         case 'R':
3153           continue;
3154
3155         case 'X':
3156           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3157           continue;
3158
3159         case 'Y':
3160           {
3161             int regno;
3162
3163             regno = va_arg (args, int);
3164             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3165             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3166           }
3167           continue;
3168
3169         case '<':
3170         case '>':
3171         case '4':
3172         case '5':
3173         case 'H':
3174         case 'W':
3175         case 'D':
3176         case 'j':
3177         case '8':
3178         case 'V':
3179         case 'C':
3180         case 'U':
3181         case 'k':
3182         case 'K':
3183         case 'p':
3184         case 'q':
3185           {
3186             assert (ep != NULL);
3187
3188             if (ep->X_op != O_constant)
3189               *r = (int) BFD_RELOC_UNUSED + c;
3190             else
3191               {
3192                 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3193                               false, &insn.insn_opcode, &insn.use_extend,
3194                               &insn.extend);
3195                 ep = NULL;
3196                 *r = BFD_RELOC_UNUSED;
3197               }
3198           }
3199           continue;
3200
3201         case '6':
3202           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3203           continue;
3204         }
3205
3206       break;
3207     }
3208
3209   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3210
3211   append_insn (place, &insn, ep, r, false);
3212 }
3213
3214 /*
3215  * Generate a "jalr" instruction with a relocation hint to the called
3216  * function.  This occurs in NewABI PIC code.
3217  */
3218 static void
3219 macro_build_jalr (icnt, ep)
3220      int icnt;
3221      expressionS *ep;
3222 {
3223   char *f;
3224   
3225   if (HAVE_NEWABI)
3226     {
3227       frag_grow (4);
3228       f = frag_more (0);
3229     }
3230   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3231                RA, PIC_CALL_REG);
3232   if (HAVE_NEWABI)
3233     fix_new_exp (frag_now, f - frag_now->fr_literal,
3234                  0, ep, false, BFD_RELOC_MIPS_JALR);
3235 }
3236
3237 /*
3238  * Generate a "lui" instruction.
3239  */
3240 static void
3241 macro_build_lui (place, counter, ep, regnum)
3242      char *place;
3243      int *counter;
3244      expressionS *ep;
3245      int regnum;
3246 {
3247   expressionS high_expr;
3248   struct mips_cl_insn insn;
3249   bfd_reloc_code_real_type r[3]
3250     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3251   const char *name = "lui";
3252   const char *fmt = "t,u";
3253
3254   assert (! mips_opts.mips16);
3255
3256   if (place == NULL)
3257     high_expr = *ep;
3258   else
3259     {
3260       high_expr.X_op = O_constant;
3261       high_expr.X_add_number = ep->X_add_number;
3262     }
3263
3264   if (high_expr.X_op == O_constant)
3265     {
3266       /* we can compute the instruction now without a relocation entry */
3267       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3268                                 >> 16) & 0xffff;
3269       *r = BFD_RELOC_UNUSED;
3270     }
3271   else
3272     {
3273       assert (ep->X_op == O_symbol);
3274       /* _gp_disp is a special case, used from s_cpload.  */
3275       assert (mips_pic == NO_PIC
3276               || (! HAVE_NEWABI
3277                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3278       *r = BFD_RELOC_HI16_S;
3279     }
3280
3281   /*
3282    * If the macro is about to expand into a second instruction,
3283    * print a warning if needed. We need to pass ip as a parameter
3284    * to generate a better warning message here...
3285    */
3286   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3287     as_warn (_("Macro instruction expanded into multiple instructions"));
3288
3289   if (place == NULL)
3290     ++*counter;         /* bump instruction counter */
3291
3292   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3293   assert (insn.insn_mo);
3294   assert (strcmp (name, insn.insn_mo->name) == 0);
3295   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3296
3297   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3298   if (*r == BFD_RELOC_UNUSED)
3299     {
3300       insn.insn_opcode |= high_expr.X_add_number;
3301       append_insn (place, &insn, NULL, r, false);
3302     }
3303   else
3304     append_insn (place, &insn, &high_expr, r, false);
3305 }
3306
3307 /* Generate a sequence of instructions to do a load or store from a constant
3308    offset off of a base register (breg) into/from a target register (treg),
3309    using AT if necessary.  */
3310 static void
3311 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3312      char *place;
3313      int *counter;
3314      expressionS *ep;
3315      const char *op;
3316      int treg, breg;
3317 {
3318   assert (ep->X_op == O_constant);
3319
3320   /* Right now, this routine can only handle signed 32-bit contants.  */
3321   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3322     as_warn (_("operand overflow"));
3323
3324   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3325     {
3326       /* Signed 16-bit offset will fit in the op.  Easy!  */
3327       macro_build (place, counter, ep, op, "t,o(b)", treg,
3328                    (int) BFD_RELOC_LO16, breg);
3329     }
3330   else
3331     {
3332       /* 32-bit offset, need multiple instructions and AT, like:
3333            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3334            addu     $tempreg,$tempreg,$breg
3335            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3336          to handle the complete offset.  */
3337       macro_build_lui (place, counter, ep, AT);
3338       if (place != NULL)
3339         place += 4;
3340       macro_build (place, counter, (expressionS *) NULL,
3341                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3342                    "d,v,t", AT, AT, breg);
3343       if (place != NULL)
3344         place += 4;
3345       macro_build (place, counter, ep, op, "t,o(b)", treg,
3346                    (int) BFD_RELOC_LO16, AT);
3347
3348       if (mips_opts.noat)
3349         as_warn (_("Macro used $at after \".set noat\""));
3350     }
3351 }
3352
3353 /*                      set_at()
3354  * Generates code to set the $at register to true (one)
3355  * if reg is less than the immediate expression.
3356  */
3357 static void
3358 set_at (counter, reg, unsignedp)
3359      int *counter;
3360      int reg;
3361      int unsignedp;
3362 {
3363   if (imm_expr.X_op == O_constant
3364       && imm_expr.X_add_number >= -0x8000
3365       && imm_expr.X_add_number < 0x8000)
3366     macro_build ((char *) NULL, counter, &imm_expr,
3367                  unsignedp ? "sltiu" : "slti",
3368                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3369   else
3370     {
3371       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3372       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3373                    unsignedp ? "sltu" : "slt",
3374                    "d,v,t", AT, reg, AT);
3375     }
3376 }
3377
3378 /* Warn if an expression is not a constant.  */
3379
3380 static void
3381 check_absolute_expr (ip, ex)
3382      struct mips_cl_insn *ip;
3383      expressionS *ex;
3384 {
3385   if (ex->X_op == O_big)
3386     as_bad (_("unsupported large constant"));
3387   else if (ex->X_op != O_constant)
3388     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3389 }
3390
3391 /* Count the leading zeroes by performing a binary chop. This is a
3392    bulky bit of source, but performance is a LOT better for the
3393    majority of values than a simple loop to count the bits:
3394        for (lcnt = 0; (lcnt < 32); lcnt++)
3395          if ((v) & (1 << (31 - lcnt)))
3396            break;
3397   However it is not code size friendly, and the gain will drop a bit
3398   on certain cached systems.
3399 */
3400 #define COUNT_TOP_ZEROES(v)             \
3401   (((v) & ~0xffff) == 0                 \
3402    ? ((v) & ~0xff) == 0                 \
3403      ? ((v) & ~0xf) == 0                \
3404        ? ((v) & ~0x3) == 0              \
3405          ? ((v) & ~0x1) == 0            \
3406            ? !(v)                       \
3407              ? 32                       \
3408              : 31                       \
3409            : 30                         \
3410          : ((v) & ~0x7) == 0            \
3411            ? 29                         \
3412            : 28                         \
3413        : ((v) & ~0x3f) == 0             \
3414          ? ((v) & ~0x1f) == 0           \
3415            ? 27                         \
3416            : 26                         \
3417          : ((v) & ~0x7f) == 0           \
3418            ? 25                         \
3419            : 24                         \
3420      : ((v) & ~0xfff) == 0              \
3421        ? ((v) & ~0x3ff) == 0            \
3422          ? ((v) & ~0x1ff) == 0          \
3423            ? 23                         \
3424            : 22                         \
3425          : ((v) & ~0x7ff) == 0          \
3426            ? 21                         \
3427            : 20                         \
3428        : ((v) & ~0x3fff) == 0           \
3429          ? ((v) & ~0x1fff) == 0         \
3430            ? 19                         \
3431            : 18                         \
3432          : ((v) & ~0x7fff) == 0         \
3433            ? 17                         \
3434            : 16                         \
3435    : ((v) & ~0xffffff) == 0             \
3436      ? ((v) & ~0xfffff) == 0            \
3437        ? ((v) & ~0x3ffff) == 0          \
3438          ? ((v) & ~0x1ffff) == 0        \
3439            ? 15                         \
3440            : 14                         \
3441          : ((v) & ~0x7ffff) == 0        \
3442            ? 13                         \
3443            : 12                         \
3444        : ((v) & ~0x3fffff) == 0         \
3445          ? ((v) & ~0x1fffff) == 0       \
3446            ? 11                         \
3447            : 10                         \
3448          : ((v) & ~0x7fffff) == 0       \
3449            ? 9                          \
3450            : 8                          \
3451      : ((v) & ~0xfffffff) == 0          \
3452        ? ((v) & ~0x3ffffff) == 0        \
3453          ? ((v) & ~0x1ffffff) == 0      \
3454            ? 7                          \
3455            : 6                          \
3456          : ((v) & ~0x7ffffff) == 0      \
3457            ? 5                          \
3458            : 4                          \
3459        : ((v) & ~0x3fffffff) == 0       \
3460          ? ((v) & ~0x1fffffff) == 0     \
3461            ? 3                          \
3462            : 2                          \
3463          : ((v) & ~0x7fffffff) == 0     \
3464            ? 1                          \
3465            : 0)
3466
3467 /*                      load_register()
3468  *  This routine generates the least number of instructions neccessary to load
3469  *  an absolute expression value into a register.
3470  */
3471 static void
3472 load_register (counter, reg, ep, dbl)
3473      int *counter;
3474      int reg;
3475      expressionS *ep;
3476      int dbl;
3477 {
3478   int freg;
3479   expressionS hi32, lo32;
3480
3481   if (ep->X_op != O_big)
3482     {
3483       assert (ep->X_op == O_constant);
3484       if (ep->X_add_number < 0x8000
3485           && (ep->X_add_number >= 0
3486               || (ep->X_add_number >= -0x8000
3487                   && (! dbl
3488                       || ! ep->X_unsigned
3489                       || sizeof (ep->X_add_number) > 4))))
3490         {
3491           /* We can handle 16 bit signed values with an addiu to
3492              $zero.  No need to ever use daddiu here, since $zero and
3493              the result are always correct in 32 bit mode.  */
3494           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3495                        (int) BFD_RELOC_LO16);
3496           return;
3497         }
3498       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3499         {
3500           /* We can handle 16 bit unsigned values with an ori to
3501              $zero.  */
3502           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3503                        (int) BFD_RELOC_LO16);
3504           return;
3505         }
3506       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3507                 && (! dbl
3508                     || ! ep->X_unsigned
3509                     || sizeof (ep->X_add_number) > 4
3510                     || (ep->X_add_number & 0x80000000) == 0))
3511                || ((HAVE_32BIT_GPRS || ! dbl)
3512                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3513                || (HAVE_32BIT_GPRS
3514                    && ! dbl
3515                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3516                        == ~ (offsetT) 0xffffffff)))
3517         {
3518           /* 32 bit values require an lui.  */
3519           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3520                        (int) BFD_RELOC_HI16);
3521           if ((ep->X_add_number & 0xffff) != 0)
3522             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3523                          (int) BFD_RELOC_LO16);
3524           return;
3525         }
3526     }
3527
3528   /* The value is larger than 32 bits.  */
3529
3530   if (HAVE_32BIT_GPRS)
3531     {
3532       as_bad (_("Number (0x%lx) larger than 32 bits"),
3533               (unsigned long) ep->X_add_number);
3534       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3535                    (int) BFD_RELOC_LO16);
3536       return;
3537     }
3538
3539   if (ep->X_op != O_big)
3540     {
3541       hi32 = *ep;
3542       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3543       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3544       hi32.X_add_number &= 0xffffffff;
3545       lo32 = *ep;
3546       lo32.X_add_number &= 0xffffffff;
3547     }
3548   else
3549     {
3550       assert (ep->X_add_number > 2);
3551       if (ep->X_add_number == 3)
3552         generic_bignum[3] = 0;
3553       else if (ep->X_add_number > 4)
3554         as_bad (_("Number larger than 64 bits"));
3555       lo32.X_op = O_constant;
3556       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3557       hi32.X_op = O_constant;
3558       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3559     }
3560
3561   if (hi32.X_add_number == 0)
3562     freg = 0;
3563   else
3564     {
3565       int shift, bit;
3566       unsigned long hi, lo;
3567
3568       if (hi32.X_add_number == (offsetT) 0xffffffff)
3569         {
3570           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3571             {
3572               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3573                            reg, 0, (int) BFD_RELOC_LO16);
3574               return;
3575             }
3576           if (lo32.X_add_number & 0x80000000)
3577             {
3578               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3579                            (int) BFD_RELOC_HI16);
3580               if (lo32.X_add_number & 0xffff)
3581                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3582                              reg, reg, (int) BFD_RELOC_LO16);
3583               return;
3584             }
3585         }
3586
3587       /* Check for 16bit shifted constant.  We know that hi32 is
3588          non-zero, so start the mask on the first bit of the hi32
3589          value.  */
3590       shift = 17;
3591       do
3592         {
3593           unsigned long himask, lomask;
3594
3595           if (shift < 32)
3596             {
3597               himask = 0xffff >> (32 - shift);
3598               lomask = (0xffff << shift) & 0xffffffff;
3599             }
3600           else
3601             {
3602               himask = 0xffff << (shift - 32);
3603               lomask = 0;
3604             }
3605           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3606               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3607             {
3608               expressionS tmp;
3609
3610               tmp.X_op = O_constant;
3611               if (shift < 32)
3612                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3613                                     | (lo32.X_add_number >> shift));
3614               else
3615                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3616               macro_build ((char *) NULL, counter, &tmp,
3617                            "ori", "t,r,i", reg, 0,
3618                            (int) BFD_RELOC_LO16);
3619               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3620                            (shift >= 32) ? "dsll32" : "dsll",
3621                            "d,w,<", reg, reg,
3622                            (shift >= 32) ? shift - 32 : shift);
3623               return;
3624             }
3625           ++shift;
3626         }
3627       while (shift <= (64 - 16));
3628
3629       /* Find the bit number of the lowest one bit, and store the
3630          shifted value in hi/lo.  */
3631       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3632       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3633       if (lo != 0)
3634         {
3635           bit = 0;
3636           while ((lo & 1) == 0)
3637             {
3638               lo >>= 1;
3639               ++bit;
3640             }
3641           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3642           hi >>= bit;
3643         }
3644       else
3645         {
3646           bit = 32;
3647           while ((hi & 1) == 0)
3648             {
3649               hi >>= 1;
3650               ++bit;
3651             }
3652           lo = hi;
3653           hi = 0;
3654         }
3655
3656       /* Optimize if the shifted value is a (power of 2) - 1.  */
3657       if ((hi == 0 && ((lo + 1) & lo) == 0)
3658           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3659         {
3660           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3661           if (shift != 0)
3662             {
3663               expressionS tmp;
3664
3665               /* This instruction will set the register to be all
3666                  ones.  */
3667               tmp.X_op = O_constant;
3668               tmp.X_add_number = (offsetT) -1;
3669               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3670                            reg, 0, (int) BFD_RELOC_LO16);
3671               if (bit != 0)
3672                 {
3673                   bit += shift;
3674                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3675                                (bit >= 32) ? "dsll32" : "dsll",
3676                                "d,w,<", reg, reg,
3677                                (bit >= 32) ? bit - 32 : bit);
3678                 }
3679               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3680                            (shift >= 32) ? "dsrl32" : "dsrl",
3681                            "d,w,<", reg, reg,
3682                            (shift >= 32) ? shift - 32 : shift);
3683               return;
3684             }
3685         }
3686
3687       /* Sign extend hi32 before calling load_register, because we can
3688          generally get better code when we load a sign extended value.  */
3689       if ((hi32.X_add_number & 0x80000000) != 0)
3690         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3691       load_register (counter, reg, &hi32, 0);
3692       freg = reg;
3693     }
3694   if ((lo32.X_add_number & 0xffff0000) == 0)
3695     {
3696       if (freg != 0)
3697         {
3698           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3699                        "dsll32", "d,w,<", reg, freg, 0);
3700           freg = reg;
3701         }
3702     }
3703   else
3704     {
3705       expressionS mid16;
3706
3707       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3708         {
3709           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3710                        (int) BFD_RELOC_HI16);
3711           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3712                        "dsrl32", "d,w,<", reg, reg, 0);
3713           return;
3714         }
3715
3716       if (freg != 0)
3717         {
3718           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3719                        "d,w,<", reg, freg, 16);
3720           freg = reg;
3721         }
3722       mid16 = lo32;
3723       mid16.X_add_number >>= 16;
3724       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3725                    freg, (int) BFD_RELOC_LO16);
3726       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3727                    "d,w,<", reg, reg, 16);
3728       freg = reg;
3729     }
3730   if ((lo32.X_add_number & 0xffff) != 0)
3731     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3732                  (int) BFD_RELOC_LO16);
3733 }
3734
3735 /* Load an address into a register.  */
3736
3737 static void
3738 load_address (counter, reg, ep, used_at)
3739      int *counter;
3740      int reg;
3741      expressionS *ep;
3742      int *used_at;
3743 {
3744   char *p = NULL;
3745
3746   if (ep->X_op != O_constant
3747       && ep->X_op != O_symbol)
3748     {
3749       as_bad (_("expression too complex"));
3750       ep->X_op = O_constant;
3751     }
3752
3753   if (ep->X_op == O_constant)
3754     {
3755       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3756       return;
3757     }
3758
3759   if (mips_pic == NO_PIC)
3760     {
3761       /* If this is a reference to a GP relative symbol, we want
3762            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3763          Otherwise we want
3764            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3765            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3766          If we have an addend, we always use the latter form.
3767
3768          With 64bit address space and a usable $at we want
3769            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3770            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3771            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3772            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3773            dsll32       $reg,0
3774            daddu        $reg,$reg,$at
3775
3776          If $at is already in use, we use an path which is suboptimal
3777          on superscalar processors.
3778            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3779            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3780            dsll         $reg,16
3781            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3782            dsll         $reg,16
3783            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3784        */
3785       if (HAVE_64BIT_ADDRESSES)
3786         {
3787           /* We don't do GP optimization for now because RELAX_ENCODE can't
3788              hold the data for such large chunks.  */
3789
3790           if (*used_at == 0 && ! mips_opts.noat)
3791             {
3792               macro_build (p, counter, ep, "lui", "t,u",
3793                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3794               macro_build (p, counter, ep, "lui", "t,u",
3795                            AT, (int) BFD_RELOC_HI16_S);
3796               macro_build (p, counter, ep, "daddiu", "t,r,j",
3797                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3798               macro_build (p, counter, ep, "daddiu", "t,r,j",
3799                            AT, AT, (int) BFD_RELOC_LO16);
3800               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3801                            "d,w,<", reg, reg, 0);
3802               macro_build (p, counter, (expressionS *) NULL, "daddu",
3803                            "d,v,t", reg, reg, AT);
3804               *used_at = 1;
3805             }
3806           else
3807             {
3808               macro_build (p, counter, ep, "lui", "t,u",
3809                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3810               macro_build (p, counter, ep, "daddiu", "t,r,j",
3811                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3812               macro_build (p, counter, (expressionS *) NULL, "dsll",
3813                            "d,w,<", reg, reg, 16);
3814               macro_build (p, counter, ep, "daddiu", "t,r,j",
3815                            reg, reg, (int) BFD_RELOC_HI16_S);
3816               macro_build (p, counter, (expressionS *) NULL, "dsll",
3817                            "d,w,<", reg, reg, 16);
3818               macro_build (p, counter, ep, "daddiu", "t,r,j",
3819                            reg, reg, (int) BFD_RELOC_LO16);
3820             }
3821         }
3822       else
3823         {
3824           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3825               && ! nopic_need_relax (ep->X_add_symbol, 1))
3826             {
3827               frag_grow (20);
3828               macro_build ((char *) NULL, counter, ep,
3829                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3830                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3831               p = frag_var (rs_machine_dependent, 8, 0,
3832                             RELAX_ENCODE (4, 8, 0, 4, 0,
3833                                           mips_opts.warn_about_macros),
3834                             ep->X_add_symbol, 0, NULL);
3835             }
3836           macro_build_lui (p, counter, ep, reg);
3837           if (p != NULL)
3838             p += 4;
3839           macro_build (p, counter, ep,
3840                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3841                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3842         }
3843     }
3844   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3845     {
3846       expressionS ex;
3847
3848       /* If this is a reference to an external symbol, we want
3849            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3850          Otherwise we want
3851            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3852            nop
3853            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3854          If we have NewABI, we want
3855            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3856          If there is a constant, it must be added in after.  */
3857       ex.X_add_number = ep->X_add_number;
3858       ep->X_add_number = 0;
3859       frag_grow (20);
3860       if (HAVE_NEWABI)
3861         {
3862           macro_build ((char *) NULL, counter, ep,
3863                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3864                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3865         }
3866       else
3867         {
3868           macro_build ((char *) NULL, counter, ep,
3869                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3870                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3871           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3872           p = frag_var (rs_machine_dependent, 4, 0,
3873                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3874                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3875           macro_build (p, counter, ep,
3876                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3877                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3878         }
3879
3880       if (ex.X_add_number != 0)
3881         {
3882           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3883             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3884           ex.X_op = O_constant;
3885           macro_build ((char *) NULL, counter, &ex,
3886                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3887                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3888         }
3889     }
3890   else if (mips_pic == SVR4_PIC)
3891     {
3892       expressionS ex;
3893       int off;
3894
3895       /* This is the large GOT case.  If this is a reference to an
3896          external symbol, we want
3897            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3898            addu         $reg,$reg,$gp
3899            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3900          Otherwise, for a reference to a local symbol, we want
3901            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3902            nop
3903            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3904          If we have NewABI, we want
3905            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3906            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3907          If there is a constant, it must be added in after.  */
3908       ex.X_add_number = ep->X_add_number;
3909       ep->X_add_number = 0;
3910       if (HAVE_NEWABI)
3911         {
3912           macro_build ((char *) NULL, counter, ep,
3913                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3914                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3915           macro_build (p, counter, ep,
3916                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3917                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3918         }
3919       else
3920         {
3921           if (reg_needs_delay (mips_gp_register))
3922             off = 4;
3923           else
3924             off = 0;
3925           frag_grow (32);
3926           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3927                        (int) BFD_RELOC_MIPS_GOT_HI16);
3928           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3929                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3930                        reg, mips_gp_register);
3931           macro_build ((char *) NULL, counter, ep,
3932                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3933                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3934           p = frag_var (rs_machine_dependent, 12 + off, 0,
3935                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3936                                       mips_opts.warn_about_macros),
3937                         ep->X_add_symbol, 0, NULL);
3938           if (off > 0)
3939             {
3940               /* We need a nop before loading from $gp.  This special
3941                  check is required because the lui which starts the main
3942                  instruction stream does not refer to $gp, and so will not
3943                  insert the nop which may be required.  */
3944               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3945                 p += 4;
3946             }
3947           macro_build (p, counter, ep,
3948                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3949                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3950           p += 4;
3951           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3952           p += 4;
3953           macro_build (p, counter, ep,
3954                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3955                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3956         }
3957
3958       if (ex.X_add_number != 0)
3959         {
3960           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3961             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3962           ex.X_op = O_constant;
3963           macro_build ((char *) NULL, counter, &ex,
3964                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3965                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3966         }
3967     }
3968   else if (mips_pic == EMBEDDED_PIC)
3969     {
3970       /* We always do
3971            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3972        */
3973       macro_build ((char *) NULL, counter, ep,
3974                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3975                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3976     }
3977   else
3978     abort ();
3979 }
3980
3981 /* Move the contents of register SOURCE into register DEST.  */
3982
3983 static void
3984 move_register (counter, dest, source)
3985      int *counter;
3986      int dest;
3987      int source;
3988 {
3989   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3990                HAVE_32BIT_GPRS ? "addu" : "daddu",
3991                "d,v,t", dest, source, 0);
3992 }
3993
3994 /*
3995  *                      Build macros
3996  *   This routine implements the seemingly endless macro or synthesized
3997  * instructions and addressing modes in the mips assembly language. Many
3998  * of these macros are simple and are similar to each other. These could
3999  * probably be handled by some kind of table or grammer aproach instead of
4000  * this verbose method. Others are not simple macros but are more like
4001  * optimizing code generation.
4002  *   One interesting optimization is when several store macros appear
4003  * consecutivly that would load AT with the upper half of the same address.
4004  * The ensuing load upper instructions are ommited. This implies some kind
4005  * of global optimization. We currently only optimize within a single macro.
4006  *   For many of the load and store macros if the address is specified as a
4007  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4008  * first load register 'at' with zero and use it as the base register. The
4009  * mips assembler simply uses register $zero. Just one tiny optimization
4010  * we're missing.
4011  */
4012 static void
4013 macro (ip)
4014      struct mips_cl_insn *ip;
4015 {
4016   register int treg, sreg, dreg, breg;
4017   int tempreg;
4018   int mask;
4019   int icnt = 0;
4020   int used_at = 0;
4021   expressionS expr1;
4022   const char *s;
4023   const char *s2;
4024   const char *fmt;
4025   int likely = 0;
4026   int dbl = 0;
4027   int coproc = 0;
4028   int lr = 0;
4029   int imm = 0;
4030   offsetT maxnum;
4031   int off;
4032   bfd_reloc_code_real_type r;
4033   int hold_mips_optimize;
4034
4035   assert (! mips_opts.mips16);
4036
4037   treg = (ip->insn_opcode >> 16) & 0x1f;
4038   dreg = (ip->insn_opcode >> 11) & 0x1f;
4039   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4040   mask = ip->insn_mo->mask;
4041
4042   expr1.X_op = O_constant;
4043   expr1.X_op_symbol = NULL;
4044   expr1.X_add_symbol = NULL;
4045   expr1.X_add_number = 1;
4046
4047   switch (mask)
4048     {
4049     case M_DABS:
4050       dbl = 1;
4051     case M_ABS:
4052       /* bgez $a0,.+12
4053          move v0,$a0
4054          sub v0,$zero,$a0
4055          */
4056
4057       mips_emit_delays (true);
4058       ++mips_opts.noreorder;
4059       mips_any_noreorder = 1;
4060
4061       expr1.X_add_number = 8;
4062       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4063       if (dreg == sreg)
4064         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4065                      0);
4066       else
4067         move_register (&icnt, dreg, sreg);
4068       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4069                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4070
4071       --mips_opts.noreorder;
4072       return;
4073
4074     case M_ADD_I:
4075       s = "addi";
4076       s2 = "add";
4077       goto do_addi;
4078     case M_ADDU_I:
4079       s = "addiu";
4080       s2 = "addu";
4081       goto do_addi;
4082     case M_DADD_I:
4083       dbl = 1;
4084       s = "daddi";
4085       s2 = "dadd";
4086       goto do_addi;
4087     case M_DADDU_I:
4088       dbl = 1;
4089       s = "daddiu";
4090       s2 = "daddu";
4091     do_addi:
4092       if (imm_expr.X_op == O_constant
4093           && imm_expr.X_add_number >= -0x8000
4094           && imm_expr.X_add_number < 0x8000)
4095         {
4096           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4097                        (int) BFD_RELOC_LO16);
4098           return;
4099         }
4100       load_register (&icnt, AT, &imm_expr, dbl);
4101       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4102                    treg, sreg, AT);
4103       break;
4104
4105     case M_AND_I:
4106       s = "andi";
4107       s2 = "and";
4108       goto do_bit;
4109     case M_OR_I:
4110       s = "ori";
4111       s2 = "or";
4112       goto do_bit;
4113     case M_NOR_I:
4114       s = "";
4115       s2 = "nor";
4116       goto do_bit;
4117     case M_XOR_I:
4118       s = "xori";
4119       s2 = "xor";
4120     do_bit:
4121       if (imm_expr.X_op == O_constant
4122           && imm_expr.X_add_number >= 0
4123           && imm_expr.X_add_number < 0x10000)
4124         {
4125           if (mask != M_NOR_I)
4126             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4127                          sreg, (int) BFD_RELOC_LO16);
4128           else
4129             {
4130               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4131                            treg, sreg, (int) BFD_RELOC_LO16);
4132               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4133                            "d,v,t", treg, treg, 0);
4134             }
4135           return;
4136         }
4137
4138       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4139       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4140                    treg, sreg, AT);
4141       break;
4142
4143     case M_BEQ_I:
4144       s = "beq";
4145       goto beq_i;
4146     case M_BEQL_I:
4147       s = "beql";
4148       likely = 1;
4149       goto beq_i;
4150     case M_BNE_I:
4151       s = "bne";
4152       goto beq_i;
4153     case M_BNEL_I:
4154       s = "bnel";
4155       likely = 1;
4156     beq_i:
4157       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4158         {
4159           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4160                        0);
4161           return;
4162         }
4163       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4164       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4165       break;
4166
4167     case M_BGEL:
4168       likely = 1;
4169     case M_BGE:
4170       if (treg == 0)
4171         {
4172           macro_build ((char *) NULL, &icnt, &offset_expr,
4173                        likely ? "bgezl" : "bgez", "s,p", sreg);
4174           return;
4175         }
4176       if (sreg == 0)
4177         {
4178           macro_build ((char *) NULL, &icnt, &offset_expr,
4179                        likely ? "blezl" : "blez", "s,p", treg);
4180           return;
4181         }
4182       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4183                    AT, sreg, treg);
4184       macro_build ((char *) NULL, &icnt, &offset_expr,
4185                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4186       break;
4187
4188     case M_BGTL_I:
4189       likely = 1;
4190     case M_BGT_I:
4191       /* check for > max integer */
4192       maxnum = 0x7fffffff;
4193       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4194         {
4195           maxnum <<= 16;
4196           maxnum |= 0xffff;
4197           maxnum <<= 16;
4198           maxnum |= 0xffff;
4199         }
4200       if (imm_expr.X_op == O_constant
4201           && imm_expr.X_add_number >= maxnum
4202           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4203         {
4204         do_false:
4205           /* result is always false */
4206           if (! likely)
4207             {
4208               if (warn_nops)
4209                 as_warn (_("Branch %s is always false (nop)"),
4210                          ip->insn_mo->name);
4211               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4212                            "", 0);
4213             }
4214           else
4215             {
4216               if (warn_nops)
4217                 as_warn (_("Branch likely %s is always false"),
4218                          ip->insn_mo->name);
4219               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4220                            "s,t,p", 0, 0);
4221             }
4222           return;
4223         }
4224       if (imm_expr.X_op != O_constant)
4225         as_bad (_("Unsupported large constant"));
4226       ++imm_expr.X_add_number;
4227       /* FALLTHROUGH */
4228     case M_BGE_I:
4229     case M_BGEL_I:
4230       if (mask == M_BGEL_I)
4231         likely = 1;
4232       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4233         {
4234           macro_build ((char *) NULL, &icnt, &offset_expr,
4235                        likely ? "bgezl" : "bgez", "s,p", sreg);
4236           return;
4237         }
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4239         {
4240           macro_build ((char *) NULL, &icnt, &offset_expr,
4241                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4242           return;
4243         }
4244       maxnum = 0x7fffffff;
4245       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4246         {
4247           maxnum <<= 16;
4248           maxnum |= 0xffff;
4249           maxnum <<= 16;
4250           maxnum |= 0xffff;
4251         }
4252       maxnum = - maxnum - 1;
4253       if (imm_expr.X_op == O_constant
4254           && imm_expr.X_add_number <= maxnum
4255           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4256         {
4257         do_true:
4258           /* result is always true */
4259           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4260           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4261           return;
4262         }
4263       set_at (&icnt, sreg, 0);
4264       macro_build ((char *) NULL, &icnt, &offset_expr,
4265                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4266       break;
4267
4268     case M_BGEUL:
4269       likely = 1;
4270     case M_BGEU:
4271       if (treg == 0)
4272         goto do_true;
4273       if (sreg == 0)
4274         {
4275           macro_build ((char *) NULL, &icnt, &offset_expr,
4276                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4277           return;
4278         }
4279       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4280                    "d,v,t", AT, sreg, treg);
4281       macro_build ((char *) NULL, &icnt, &offset_expr,
4282                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4283       break;
4284
4285     case M_BGTUL_I:
4286       likely = 1;
4287     case M_BGTU_I:
4288       if (sreg == 0
4289           || (HAVE_32BIT_GPRS
4290               && imm_expr.X_op == O_constant
4291               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4292         goto do_false;
4293       if (imm_expr.X_op != O_constant)
4294         as_bad (_("Unsupported large constant"));
4295       ++imm_expr.X_add_number;
4296       /* FALLTHROUGH */
4297     case M_BGEU_I:
4298     case M_BGEUL_I:
4299       if (mask == M_BGEUL_I)
4300         likely = 1;
4301       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4302         goto do_true;
4303       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4304         {
4305           macro_build ((char *) NULL, &icnt, &offset_expr,
4306                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4307           return;
4308         }
4309       set_at (&icnt, sreg, 1);
4310       macro_build ((char *) NULL, &icnt, &offset_expr,
4311                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4312       break;
4313
4314     case M_BGTL:
4315       likely = 1;
4316     case M_BGT:
4317       if (treg == 0)
4318         {
4319           macro_build ((char *) NULL, &icnt, &offset_expr,
4320                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4321           return;
4322         }
4323       if (sreg == 0)
4324         {
4325           macro_build ((char *) NULL, &icnt, &offset_expr,
4326                        likely ? "bltzl" : "bltz", "s,p", treg);
4327           return;
4328         }
4329       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4330                    AT, treg, sreg);
4331       macro_build ((char *) NULL, &icnt, &offset_expr,
4332                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4333       break;
4334
4335     case M_BGTUL:
4336       likely = 1;
4337     case M_BGTU:
4338       if (treg == 0)
4339         {
4340           macro_build ((char *) NULL, &icnt, &offset_expr,
4341                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4342           return;
4343         }
4344       if (sreg == 0)
4345         goto do_false;
4346       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4347                    "d,v,t", AT, treg, sreg);
4348       macro_build ((char *) NULL, &icnt, &offset_expr,
4349                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4350       break;
4351
4352     case M_BLEL:
4353       likely = 1;
4354     case M_BLE:
4355       if (treg == 0)
4356         {
4357           macro_build ((char *) NULL, &icnt, &offset_expr,
4358                        likely ? "blezl" : "blez", "s,p", sreg);
4359           return;
4360         }
4361       if (sreg == 0)
4362         {
4363           macro_build ((char *) NULL, &icnt, &offset_expr,
4364                        likely ? "bgezl" : "bgez", "s,p", treg);
4365           return;
4366         }
4367       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4368                    AT, treg, sreg);
4369       macro_build ((char *) NULL, &icnt, &offset_expr,
4370                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4371       break;
4372
4373     case M_BLEL_I:
4374       likely = 1;
4375     case M_BLE_I:
4376       maxnum = 0x7fffffff;
4377       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4378         {
4379           maxnum <<= 16;
4380           maxnum |= 0xffff;
4381           maxnum <<= 16;
4382           maxnum |= 0xffff;
4383         }
4384       if (imm_expr.X_op == O_constant
4385           && imm_expr.X_add_number >= maxnum
4386           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4387         goto do_true;
4388       if (imm_expr.X_op != O_constant)
4389         as_bad (_("Unsupported large constant"));
4390       ++imm_expr.X_add_number;
4391       /* FALLTHROUGH */
4392     case M_BLT_I:
4393     case M_BLTL_I:
4394       if (mask == M_BLTL_I)
4395         likely = 1;
4396       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4397         {
4398           macro_build ((char *) NULL, &icnt, &offset_expr,
4399                        likely ? "bltzl" : "bltz", "s,p", sreg);
4400           return;
4401         }
4402       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4403         {
4404           macro_build ((char *) NULL, &icnt, &offset_expr,
4405                        likely ? "blezl" : "blez", "s,p", sreg);
4406           return;
4407         }
4408       set_at (&icnt, sreg, 0);
4409       macro_build ((char *) NULL, &icnt, &offset_expr,
4410                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4411       break;
4412
4413     case M_BLEUL:
4414       likely = 1;
4415     case M_BLEU:
4416       if (treg == 0)
4417         {
4418           macro_build ((char *) NULL, &icnt, &offset_expr,
4419                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4420           return;
4421         }
4422       if (sreg == 0)
4423         goto do_true;
4424       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4425                    "d,v,t", AT, treg, sreg);
4426       macro_build ((char *) NULL, &icnt, &offset_expr,
4427                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4428       break;
4429
4430     case M_BLEUL_I:
4431       likely = 1;
4432     case M_BLEU_I:
4433       if (sreg == 0
4434           || (HAVE_32BIT_GPRS
4435               && imm_expr.X_op == O_constant
4436               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4437         goto do_true;
4438       if (imm_expr.X_op != O_constant)
4439         as_bad (_("Unsupported large constant"));
4440       ++imm_expr.X_add_number;
4441       /* FALLTHROUGH */
4442     case M_BLTU_I:
4443     case M_BLTUL_I:
4444       if (mask == M_BLTUL_I)
4445         likely = 1;
4446       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4447         goto do_false;
4448       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4449         {
4450           macro_build ((char *) NULL, &icnt, &offset_expr,
4451                        likely ? "beql" : "beq",
4452                        "s,t,p", sreg, 0);
4453           return;
4454         }
4455       set_at (&icnt, sreg, 1);
4456       macro_build ((char *) NULL, &icnt, &offset_expr,
4457                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4458       break;
4459
4460     case M_BLTL:
4461       likely = 1;
4462     case M_BLT:
4463       if (treg == 0)
4464         {
4465           macro_build ((char *) NULL, &icnt, &offset_expr,
4466                        likely ? "bltzl" : "bltz", "s,p", sreg);
4467           return;
4468         }
4469       if (sreg == 0)
4470         {
4471           macro_build ((char *) NULL, &icnt, &offset_expr,
4472                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4473           return;
4474         }
4475       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4476                    AT, sreg, treg);
4477       macro_build ((char *) NULL, &icnt, &offset_expr,
4478                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4479       break;
4480
4481     case M_BLTUL:
4482       likely = 1;
4483     case M_BLTU:
4484       if (treg == 0)
4485         goto do_false;
4486       if (sreg == 0)
4487         {
4488           macro_build ((char *) NULL, &icnt, &offset_expr,
4489                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4490           return;
4491         }
4492       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4493                    "d,v,t", AT, sreg,
4494                    treg);
4495       macro_build ((char *) NULL, &icnt, &offset_expr,
4496                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4497       break;
4498
4499     case M_DDIV_3:
4500       dbl = 1;
4501     case M_DIV_3:
4502       s = "mflo";
4503       goto do_div3;
4504     case M_DREM_3:
4505       dbl = 1;
4506     case M_REM_3:
4507       s = "mfhi";
4508     do_div3:
4509       if (treg == 0)
4510         {
4511           as_warn (_("Divide by zero."));
4512           if (mips_trap)
4513             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4514                          "s,t,q", 0, 0, 7);
4515           else
4516             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4517                          "c", 7);
4518           return;
4519         }
4520
4521       mips_emit_delays (true);
4522       ++mips_opts.noreorder;
4523       mips_any_noreorder = 1;
4524       if (mips_trap)
4525         {
4526           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4527                        "s,t,q", treg, 0, 7);
4528           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4529                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4530         }
4531       else
4532         {
4533           expr1.X_add_number = 8;
4534           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4535           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4536                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4537           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4538                        "c", 7);
4539         }
4540       expr1.X_add_number = -1;
4541       macro_build ((char *) NULL, &icnt, &expr1,
4542                    dbl ? "daddiu" : "addiu",
4543                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4544       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4545       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4546       if (dbl)
4547         {
4548           expr1.X_add_number = 1;
4549           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4550                        (int) BFD_RELOC_LO16);
4551           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4552                        "d,w,<", AT, AT, 31);
4553         }
4554       else
4555         {
4556           expr1.X_add_number = 0x80000000;
4557           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4558                        (int) BFD_RELOC_HI16);
4559         }
4560       if (mips_trap)
4561         {
4562           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4563                        "s,t,q", sreg, AT, 6);
4564           /* We want to close the noreorder block as soon as possible, so
4565              that later insns are available for delay slot filling.  */
4566           --mips_opts.noreorder;
4567         }
4568       else
4569         {
4570           expr1.X_add_number = 8;
4571           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4572           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4573                        0);
4574
4575           /* We want to close the noreorder block as soon as possible, so
4576              that later insns are available for delay slot filling.  */
4577           --mips_opts.noreorder;
4578
4579           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4580                        "c", 6);
4581         }
4582       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4583       break;
4584
4585     case M_DIV_3I:
4586       s = "div";
4587       s2 = "mflo";
4588       goto do_divi;
4589     case M_DIVU_3I:
4590       s = "divu";
4591       s2 = "mflo";
4592       goto do_divi;
4593     case M_REM_3I:
4594       s = "div";
4595       s2 = "mfhi";
4596       goto do_divi;
4597     case M_REMU_3I:
4598       s = "divu";
4599       s2 = "mfhi";
4600       goto do_divi;
4601     case M_DDIV_3I:
4602       dbl = 1;
4603       s = "ddiv";
4604       s2 = "mflo";
4605       goto do_divi;
4606     case M_DDIVU_3I:
4607       dbl = 1;
4608       s = "ddivu";
4609       s2 = "mflo";
4610       goto do_divi;
4611     case M_DREM_3I:
4612       dbl = 1;
4613       s = "ddiv";
4614       s2 = "mfhi";
4615       goto do_divi;
4616     case M_DREMU_3I:
4617       dbl = 1;
4618       s = "ddivu";
4619       s2 = "mfhi";
4620     do_divi:
4621       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4622         {
4623           as_warn (_("Divide by zero."));
4624           if (mips_trap)
4625             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4626                          "s,t,q", 0, 0, 7);
4627           else
4628             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4629                          "c", 7);
4630           return;
4631         }
4632       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4633         {
4634           if (strcmp (s2, "mflo") == 0)
4635             move_register (&icnt, dreg, sreg);
4636           else
4637             move_register (&icnt, dreg, 0);
4638           return;
4639         }
4640       if (imm_expr.X_op == O_constant
4641           && imm_expr.X_add_number == -1
4642           && s[strlen (s) - 1] != 'u')
4643         {
4644           if (strcmp (s2, "mflo") == 0)
4645             {
4646               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4647                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4648             }
4649           else
4650             move_register (&icnt, dreg, 0);
4651           return;
4652         }
4653
4654       load_register (&icnt, AT, &imm_expr, dbl);
4655       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4656                    sreg, AT);
4657       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4658       break;
4659
4660     case M_DIVU_3:
4661       s = "divu";
4662       s2 = "mflo";
4663       goto do_divu3;
4664     case M_REMU_3:
4665       s = "divu";
4666       s2 = "mfhi";
4667       goto do_divu3;
4668     case M_DDIVU_3:
4669       s = "ddivu";
4670       s2 = "mflo";
4671       goto do_divu3;
4672     case M_DREMU_3:
4673       s = "ddivu";
4674       s2 = "mfhi";
4675     do_divu3:
4676       mips_emit_delays (true);
4677       ++mips_opts.noreorder;
4678       mips_any_noreorder = 1;
4679       if (mips_trap)
4680         {
4681           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4682                        "s,t,q", treg, 0, 7);
4683           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4684                        sreg, treg);
4685           /* We want to close the noreorder block as soon as possible, so
4686              that later insns are available for delay slot filling.  */
4687           --mips_opts.noreorder;
4688         }
4689       else
4690         {
4691           expr1.X_add_number = 8;
4692           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4693           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4694                        sreg, treg);
4695
4696           /* We want to close the noreorder block as soon as possible, so
4697              that later insns are available for delay slot filling.  */
4698           --mips_opts.noreorder;
4699           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4700                        "c", 7);
4701         }
4702       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4703       return;
4704
4705     case M_DLA_AB:
4706       dbl = 1;
4707     case M_LA_AB:
4708       /* Load the address of a symbol into a register.  If breg is not
4709          zero, we then add a base register to it.  */
4710
4711       if (dbl && HAVE_32BIT_GPRS)
4712         as_warn (_("dla used to load 32-bit register"));
4713
4714       if (! dbl && HAVE_64BIT_OBJECTS)
4715         as_warn (_("la used to load 64-bit address"));
4716
4717       if (offset_expr.X_op == O_constant
4718           && offset_expr.X_add_number >= -0x8000
4719           && offset_expr.X_add_number < 0x8000)
4720         {
4721           macro_build ((char *) NULL, &icnt, &offset_expr,
4722                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4723                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4724           return;
4725         }
4726
4727       if (treg == breg)
4728         {
4729           tempreg = AT;
4730           used_at = 1;
4731         }
4732       else
4733         {
4734           tempreg = treg;
4735           used_at = 0;
4736         }
4737
4738       /* When generating embedded PIC code, we permit expressions of
4739          the form
4740            la   $treg,foo-bar
4741            la   $treg,foo-bar($breg)
4742          where bar is an address in the current section.  These are used
4743          when getting the addresses of functions.  We don't permit
4744          X_add_number to be non-zero, because if the symbol is
4745          external the relaxing code needs to know that any addend is
4746          purely the offset to X_op_symbol.  */
4747       if (mips_pic == EMBEDDED_PIC
4748           && offset_expr.X_op == O_subtract
4749           && (symbol_constant_p (offset_expr.X_op_symbol)
4750               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4751               : (symbol_equated_p (offset_expr.X_op_symbol)
4752                  && (S_GET_SEGMENT
4753                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4754                       ->X_add_symbol)
4755                      == now_seg)))
4756           && (offset_expr.X_add_number == 0
4757               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4758         {
4759           if (breg == 0)
4760             {
4761               tempreg = treg;
4762               used_at = 0;
4763               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4764                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4765             }
4766           else
4767             {
4768               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4769                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4770               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4771                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4772                            "d,v,t", tempreg, tempreg, breg);
4773             }
4774           macro_build ((char *) NULL, &icnt, &offset_expr,
4775                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4776                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4777           if (! used_at)
4778             return;
4779           break;
4780         }
4781
4782       if (offset_expr.X_op != O_symbol
4783           && offset_expr.X_op != O_constant)
4784         {
4785           as_bad (_("expression too complex"));
4786           offset_expr.X_op = O_constant;
4787         }
4788
4789       if (offset_expr.X_op == O_constant)
4790         load_register (&icnt, tempreg, &offset_expr,
4791                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4792                         ? (dbl || HAVE_64BIT_ADDRESSES)
4793                         : HAVE_64BIT_ADDRESSES));
4794       else if (mips_pic == NO_PIC)
4795         {
4796           /* If this is a reference to a GP relative symbol, we want
4797                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4798              Otherwise we want
4799                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4800                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4801              If we have a constant, we need two instructions anyhow,
4802              so we may as well always use the latter form.
4803
4804             With 64bit address space and a usable $at we want
4805               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4806               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4807               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4808               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4809               dsll32    $tempreg,0
4810               daddu     $tempreg,$tempreg,$at
4811
4812             If $at is already in use, we use an path which is suboptimal
4813             on superscalar processors.
4814               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4815               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4816               dsll      $tempreg,16
4817               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4818               dsll      $tempreg,16
4819               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4820           */
4821           char *p = NULL;
4822           if (HAVE_64BIT_ADDRESSES)
4823             {
4824               /* We don't do GP optimization for now because RELAX_ENCODE can't
4825                  hold the data for such large chunks.  */
4826
4827               if (used_at == 0 && ! mips_opts.noat)
4828                 {
4829                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4830                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4831                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4832                                AT, (int) BFD_RELOC_HI16_S);
4833                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4834                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4835                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4836                                AT, AT, (int) BFD_RELOC_LO16);
4837                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4838                                "d,w,<", tempreg, tempreg, 0);
4839                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4840                                "d,v,t", tempreg, tempreg, AT);
4841                   used_at = 1;
4842                 }
4843               else
4844                 {
4845                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4846                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4847                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4848                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4849                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4850                                tempreg, tempreg, 16);
4851                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4852                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4853                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4854                                tempreg, tempreg, 16);
4855                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4856                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4857                 }
4858             }
4859           else
4860             {
4861               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4862                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4863                 {
4864                   frag_grow (20);
4865                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4866                                "t,r,j", tempreg, mips_gp_register,
4867                                (int) BFD_RELOC_GPREL16);
4868                   p = frag_var (rs_machine_dependent, 8, 0,
4869                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4870                                               mips_opts.warn_about_macros),
4871                                 offset_expr.X_add_symbol, 0, NULL);
4872                 }
4873               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4874               if (p != NULL)
4875                 p += 4;
4876               macro_build (p, &icnt, &offset_expr, "addiu",
4877                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4878             }
4879         }
4880       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4881         {
4882           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4883
4884           /* If this is a reference to an external symbol, and there
4885              is no constant, we want
4886                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4887              or if tempreg is PIC_CALL_REG
4888                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4889              For a local symbol, we want
4890                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4891                nop
4892                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4893
4894              If we have a small constant, and this is a reference to
4895              an external symbol, we want
4896                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4897                nop
4898                addiu    $tempreg,$tempreg,<constant>
4899              For a local symbol, we want the same instruction
4900              sequence, but we output a BFD_RELOC_LO16 reloc on the
4901              addiu instruction.
4902
4903              If we have a large constant, and this is a reference to
4904              an external symbol, we want
4905                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4906                lui      $at,<hiconstant>
4907                addiu    $at,$at,<loconstant>
4908                addu     $tempreg,$tempreg,$at
4909              For a local symbol, we want the same instruction
4910              sequence, but we output a BFD_RELOC_LO16 reloc on the
4911              addiu instruction.
4912
4913              For NewABI, we want for local or external data addresses
4914                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4915              For a local function symbol, we want
4916                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4917                nop
4918                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4919            */
4920
4921           expr1.X_add_number = offset_expr.X_add_number;
4922           offset_expr.X_add_number = 0;
4923           frag_grow (32);
4924           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4925             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4926           else if (HAVE_NEWABI)
4927             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4928           macro_build ((char *) NULL, &icnt, &offset_expr,
4929                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4930                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4931           if (expr1.X_add_number == 0)
4932             {
4933               int off;
4934               char *p;
4935
4936               if (breg == 0)
4937                 off = 0;
4938               else
4939                 {
4940                   /* We're going to put in an addu instruction using
4941                      tempreg, so we may as well insert the nop right
4942                      now.  */
4943                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4944                                "nop", "");
4945                   off = 4;
4946                 }
4947               p = frag_var (rs_machine_dependent, 8 - off, 0,
4948                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4949                                           (breg == 0
4950                                            ? mips_opts.warn_about_macros
4951                                            : 0)),
4952                             offset_expr.X_add_symbol, 0, NULL);
4953               if (breg == 0)
4954                 {
4955                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4956                   p += 4;
4957                 }
4958               macro_build (p, &icnt, &expr1,
4959                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4960                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4961               /* FIXME: If breg == 0, and the next instruction uses
4962                  $tempreg, then if this variant case is used an extra
4963                  nop will be generated.  */
4964             }
4965           else if (expr1.X_add_number >= -0x8000
4966                    && expr1.X_add_number < 0x8000)
4967             {
4968               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4969                            "nop", "");
4970               macro_build ((char *) NULL, &icnt, &expr1,
4971                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4972                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4973               frag_var (rs_machine_dependent, 0, 0,
4974                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4975                         offset_expr.X_add_symbol, 0, NULL);
4976             }
4977           else
4978             {
4979               int off1;
4980
4981               /* If we are going to add in a base register, and the
4982                  target register and the base register are the same,
4983                  then we are using AT as a temporary register.  Since
4984                  we want to load the constant into AT, we add our
4985                  current AT (from the global offset table) and the
4986                  register into the register now, and pretend we were
4987                  not using a base register.  */
4988               if (breg != treg)
4989                 off1 = 0;
4990               else
4991                 {
4992                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4993                                "nop", "");
4994                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4995                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4996                                "d,v,t", treg, AT, breg);
4997                   breg = 0;
4998                   tempreg = treg;
4999                   off1 = -8;
5000                 }
5001
5002               /* Set mips_optimize around the lui instruction to avoid
5003                  inserting an unnecessary nop after the lw.  */
5004               hold_mips_optimize = mips_optimize;
5005               mips_optimize = 2;
5006               macro_build_lui (NULL, &icnt, &expr1, AT);
5007               mips_optimize = hold_mips_optimize;
5008
5009               macro_build ((char *) NULL, &icnt, &expr1,
5010                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5011                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5012               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5013                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5014                            "d,v,t", tempreg, tempreg, AT);
5015               frag_var (rs_machine_dependent, 0, 0,
5016                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5017                         offset_expr.X_add_symbol, 0, NULL);
5018               used_at = 1;
5019             }
5020         }
5021       else if (mips_pic == SVR4_PIC)
5022         {
5023           int gpdel;
5024           char *p;
5025           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5026           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5027           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5028
5029           /* This is the large GOT case.  If this is a reference to an
5030              external symbol, and there is no constant, we want
5031                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5032                addu     $tempreg,$tempreg,$gp
5033                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5034              or if tempreg is PIC_CALL_REG
5035                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5036                addu     $tempreg,$tempreg,$gp
5037                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5038              For a local symbol, we want
5039                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5040                nop
5041                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5042
5043              If we have a small constant, and this is a reference to
5044              an external symbol, we want
5045                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5046                addu     $tempreg,$tempreg,$gp
5047                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5048                nop
5049                addiu    $tempreg,$tempreg,<constant>
5050              For a local symbol, we want
5051                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5052                nop
5053                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5054
5055              If we have a large constant, and this is a reference to
5056              an external symbol, we want
5057                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5058                addu     $tempreg,$tempreg,$gp
5059                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5060                lui      $at,<hiconstant>
5061                addiu    $at,$at,<loconstant>
5062                addu     $tempreg,$tempreg,$at
5063              For a local symbol, we want
5064                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5065                lui      $at,<hiconstant>
5066                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5067                addu     $tempreg,$tempreg,$at
5068
5069              For NewABI, we want for local data addresses
5070               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5071            */
5072
5073           expr1.X_add_number = offset_expr.X_add_number;
5074           offset_expr.X_add_number = 0;
5075           frag_grow (52);
5076           if (reg_needs_delay (mips_gp_register))
5077             gpdel = 4;
5078           else
5079             gpdel = 0;
5080           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5081             {
5082               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5083               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5084             }
5085           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5086                        tempreg, lui_reloc_type);
5087           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5088                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5089                        "d,v,t", tempreg, tempreg, mips_gp_register);
5090           macro_build ((char *) NULL, &icnt, &offset_expr,
5091                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5092                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5093           if (expr1.X_add_number == 0)
5094             {
5095               int off;
5096
5097               if (breg == 0)
5098                 off = 0;
5099               else
5100                 {
5101                   /* We're going to put in an addu instruction using
5102                      tempreg, so we may as well insert the nop right
5103                      now.  */
5104                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5105                                "nop", "");
5106                   off = 4;
5107                 }
5108
5109               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5110                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5111                                           8 + gpdel, 0,
5112                                           (breg == 0
5113                                            ? mips_opts.warn_about_macros
5114                                            : 0)),
5115                             offset_expr.X_add_symbol, 0, NULL);
5116             }
5117           else if (expr1.X_add_number >= -0x8000
5118                    && expr1.X_add_number < 0x8000)
5119             {
5120               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5121                            "nop", "");
5122               macro_build ((char *) NULL, &icnt, &expr1,
5123                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5124                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5125
5126               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5127                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5128                                           (breg == 0
5129                                            ? mips_opts.warn_about_macros
5130                                            : 0)),
5131                             offset_expr.X_add_symbol, 0, NULL);
5132             }
5133           else
5134             {
5135               int adj, dreg;
5136
5137               /* If we are going to add in a base register, and the
5138                  target register and the base register are the same,
5139                  then we are using AT as a temporary register.  Since
5140                  we want to load the constant into AT, we add our
5141                  current AT (from the global offset table) and the
5142                  register into the register now, and pretend we were
5143                  not using a base register.  */
5144               if (breg != treg)
5145                 {
5146                   adj = 0;
5147                   dreg = tempreg;
5148                 }
5149               else
5150                 {
5151                   assert (tempreg == AT);
5152                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5153                                "nop", "");
5154                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5155                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5156                                "d,v,t", treg, AT, breg);
5157                   dreg = treg;
5158                   adj = 8;
5159                 }
5160
5161               /* Set mips_optimize around the lui instruction to avoid
5162                  inserting an unnecessary nop after the lw.  */
5163               hold_mips_optimize = mips_optimize;
5164               mips_optimize = 2;
5165               macro_build_lui (NULL, &icnt, &expr1, AT);
5166               mips_optimize = hold_mips_optimize;
5167
5168               macro_build ((char *) NULL, &icnt, &expr1,
5169                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5170                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5171               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5172                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5173                            "d,v,t", dreg, dreg, AT);
5174
5175               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5176                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5177                                           8 + gpdel, 0,
5178                                           (breg == 0
5179                                            ? mips_opts.warn_about_macros
5180                                            : 0)),
5181                             offset_expr.X_add_symbol, 0, NULL);
5182
5183               used_at = 1;
5184             }
5185
5186           if (gpdel > 0)
5187             {
5188               /* This is needed because this instruction uses $gp, but
5189                  the first instruction on the main stream does not.  */
5190               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5191               p += 4;
5192             }
5193
5194           if (HAVE_NEWABI)
5195             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5196           macro_build (p, &icnt, &offset_expr,
5197                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5198                        "t,o(b)", tempreg,
5199                        local_reloc_type,
5200                        mips_gp_register);
5201           p += 4;
5202           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5203             {
5204               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5205             }
5206          else
5207            if (expr1.X_add_number >= -0x8000
5208               && expr1.X_add_number < 0x8000)
5209             {
5210               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5211               p += 4;
5212               macro_build (p, &icnt, &expr1,
5213                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5214                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5215               /* FIXME: If add_number is 0, and there was no base
5216                  register, the external symbol case ended with a load,
5217                  so if the symbol turns out to not be external, and
5218                  the next instruction uses tempreg, an unnecessary nop
5219                  will be inserted.  */
5220             }
5221           else
5222             {
5223               if (breg == treg)
5224                 {
5225                   /* We must add in the base register now, as in the
5226                      external symbol case.  */
5227                   assert (tempreg == AT);
5228                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5229                   p += 4;
5230                   macro_build (p, &icnt, (expressionS *) NULL,
5231                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5232                                "d,v,t", treg, AT, breg);
5233                   p += 4;
5234                   tempreg = treg;
5235                   /* We set breg to 0 because we have arranged to add
5236                      it in in both cases.  */
5237                   breg = 0;
5238                 }
5239
5240               macro_build_lui (p, &icnt, &expr1, AT);
5241               p += 4;
5242               macro_build (p, &icnt, &expr1,
5243                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5244                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5245               p += 4;
5246               macro_build (p, &icnt, (expressionS *) NULL,
5247                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5248                            "d,v,t", tempreg, tempreg, AT);
5249               p += 4;
5250             }
5251         }
5252       else if (mips_pic == EMBEDDED_PIC)
5253         {
5254           /* We use
5255                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5256              */
5257           macro_build ((char *) NULL, &icnt, &offset_expr,
5258                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5259                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5260         }
5261       else
5262         abort ();
5263
5264       if (breg != 0)
5265         {
5266           char *s;
5267
5268           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5269             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5270           else
5271             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5272
5273           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5274                        "d,v,t", treg, tempreg, breg);
5275         }
5276
5277       if (! used_at)
5278         return;
5279
5280       break;
5281
5282     case M_J_A:
5283       /* The j instruction may not be used in PIC code, since it
5284          requires an absolute address.  We convert it to a b
5285          instruction.  */
5286       if (mips_pic == NO_PIC)
5287         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5288       else
5289         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5290       return;
5291
5292       /* The jal instructions must be handled as macros because when
5293          generating PIC code they expand to multi-instruction
5294          sequences.  Normally they are simple instructions.  */
5295     case M_JAL_1:
5296       dreg = RA;
5297       /* Fall through.  */
5298     case M_JAL_2:
5299       if (mips_pic == NO_PIC
5300           || mips_pic == EMBEDDED_PIC)
5301         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5302                      "d,s", dreg, sreg);
5303       else if (mips_pic == SVR4_PIC)
5304         {
5305           if (sreg != PIC_CALL_REG)
5306             as_warn (_("MIPS PIC call to register other than $25"));
5307
5308           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5309                        "d,s", dreg, sreg);
5310           if (! HAVE_NEWABI)
5311             {
5312               if (mips_cprestore_offset < 0)
5313                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5314               else
5315                 {
5316                   if (! mips_frame_reg_valid)
5317                     {
5318                       as_warn (_("No .frame pseudo-op used in PIC code"));
5319                       /* Quiet this warning.  */
5320                       mips_frame_reg_valid = 1;
5321                     }
5322                   if (! mips_cprestore_valid)
5323                     {
5324                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5325                       /* Quiet this warning.  */
5326                       mips_cprestore_valid = 1;
5327                     }
5328                   expr1.X_add_number = mips_cprestore_offset;
5329                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5330                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5331                                                 mips_gp_register, mips_frame_reg);
5332                 }
5333             }
5334         }
5335       else
5336         abort ();
5337
5338       return;
5339
5340     case M_JAL_A:
5341       if (mips_pic == NO_PIC)
5342         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5343       else if (mips_pic == SVR4_PIC)
5344         {
5345           char *p;
5346
5347           /* If this is a reference to an external symbol, and we are
5348              using a small GOT, we want
5349                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5350                nop
5351                jalr     $ra,$25
5352                nop
5353                lw       $gp,cprestore($sp)
5354              The cprestore value is set using the .cprestore
5355              pseudo-op.  If we are using a big GOT, we want
5356                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5357                addu     $25,$25,$gp
5358                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5359                nop
5360                jalr     $ra,$25
5361                nop
5362                lw       $gp,cprestore($sp)
5363              If the symbol is not external, we want
5364                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5365                nop
5366                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5367                jalr     $ra,$25
5368                nop
5369                lw $gp,cprestore($sp)
5370              For NewABI, we want
5371                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5372                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5373            */
5374           if (HAVE_NEWABI)
5375             {
5376               macro_build ((char *) NULL, &icnt, &offset_expr,
5377                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5378                            "t,o(b)", PIC_CALL_REG,
5379                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5380               macro_build_jalr (icnt, &offset_expr);
5381             }
5382           else
5383             {
5384               frag_grow (40);
5385               if (! mips_big_got)
5386                 {
5387                   macro_build ((char *) NULL, &icnt, &offset_expr,
5388                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5389                                "t,o(b)", PIC_CALL_REG,
5390                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5391                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5392                                "nop", "");
5393                   p = frag_var (rs_machine_dependent, 4, 0,
5394                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5395                                 offset_expr.X_add_symbol, 0, NULL);
5396                 }
5397               else
5398                 {
5399                   int gpdel;
5400
5401                   if (reg_needs_delay (mips_gp_register))
5402                     gpdel = 4;
5403                   else
5404                     gpdel = 0;
5405                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5406                                "t,u", PIC_CALL_REG,
5407                                (int) BFD_RELOC_MIPS_CALL_HI16);
5408                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5409                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5410                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5411                                mips_gp_register);
5412                   macro_build ((char *) NULL, &icnt, &offset_expr,
5413                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5414                                "t,o(b)", PIC_CALL_REG,
5415                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5416                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5417                                "nop", "");
5418                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5419                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5420                                               8 + gpdel, 0, 0),
5421                                 offset_expr.X_add_symbol, 0, NULL);
5422                   if (gpdel > 0)
5423                     {
5424                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5425                       p += 4;
5426                     }
5427                   macro_build (p, &icnt, &offset_expr,
5428                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5429                                "t,o(b)", PIC_CALL_REG,
5430                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5431                   p += 4;
5432                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5433                   p += 4;
5434                 }
5435               macro_build (p, &icnt, &offset_expr,
5436                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5437                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5438                            (int) BFD_RELOC_LO16);
5439               macro_build_jalr (icnt, &offset_expr);
5440
5441               if (mips_cprestore_offset < 0)
5442                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5443               else
5444                 {
5445                   if (! mips_frame_reg_valid)
5446                     {
5447                       as_warn (_("No .frame pseudo-op used in PIC code"));
5448                       /* Quiet this warning.  */
5449                       mips_frame_reg_valid = 1;
5450                     }
5451                   if (! mips_cprestore_valid)
5452                     {
5453                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5454                       /* Quiet this warning.  */
5455                       mips_cprestore_valid = 1;
5456                     }
5457                   if (mips_opts.noreorder)
5458                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5459                                  "nop", "");
5460                   expr1.X_add_number = mips_cprestore_offset;
5461                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5462                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5463                                                 mips_gp_register, mips_frame_reg);
5464                 }
5465             }
5466         }
5467       else if (mips_pic == EMBEDDED_PIC)
5468         {
5469           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5470           /* The linker may expand the call to a longer sequence which
5471              uses $at, so we must break rather than return.  */
5472           break;
5473         }
5474       else
5475         abort ();
5476
5477       return;
5478
5479     case M_LB_AB:
5480       s = "lb";
5481       goto ld;
5482     case M_LBU_AB:
5483       s = "lbu";
5484       goto ld;
5485     case M_LH_AB:
5486       s = "lh";
5487       goto ld;
5488     case M_LHU_AB:
5489       s = "lhu";
5490       goto ld;
5491     case M_LW_AB:
5492       s = "lw";
5493       goto ld;
5494     case M_LWC0_AB:
5495       s = "lwc0";
5496       /* Itbl support may require additional care here.  */
5497       coproc = 1;
5498       goto ld;
5499     case M_LWC1_AB:
5500       s = "lwc1";
5501       /* Itbl support may require additional care here.  */
5502       coproc = 1;
5503       goto ld;
5504     case M_LWC2_AB:
5505       s = "lwc2";
5506       /* Itbl support may require additional care here.  */
5507       coproc = 1;
5508       goto ld;
5509     case M_LWC3_AB:
5510       s = "lwc3";
5511       /* Itbl support may require additional care here.  */
5512       coproc = 1;
5513       goto ld;
5514     case M_LWL_AB:
5515       s = "lwl";
5516       lr = 1;
5517       goto ld;
5518     case M_LWR_AB:
5519       s = "lwr";
5520       lr = 1;
5521       goto ld;
5522     case M_LDC1_AB:
5523       if (mips_arch == CPU_R4650)
5524         {
5525           as_bad (_("opcode not supported on this processor"));
5526           return;
5527         }
5528       s = "ldc1";
5529       /* Itbl support may require additional care here.  */
5530       coproc = 1;
5531       goto ld;
5532     case M_LDC2_AB:
5533       s = "ldc2";
5534       /* Itbl support may require additional care here.  */
5535       coproc = 1;
5536       goto ld;
5537     case M_LDC3_AB:
5538       s = "ldc3";
5539       /* Itbl support may require additional care here.  */
5540       coproc = 1;
5541       goto ld;
5542     case M_LDL_AB:
5543       s = "ldl";
5544       lr = 1;
5545       goto ld;
5546     case M_LDR_AB:
5547       s = "ldr";
5548       lr = 1;
5549       goto ld;
5550     case M_LL_AB:
5551       s = "ll";
5552       goto ld;
5553     case M_LLD_AB:
5554       s = "lld";
5555       goto ld;
5556     case M_LWU_AB:
5557       s = "lwu";
5558     ld:
5559       if (breg == treg || coproc || lr)
5560         {
5561           tempreg = AT;
5562           used_at = 1;
5563         }
5564       else
5565         {
5566           tempreg = treg;
5567           used_at = 0;
5568         }
5569       goto ld_st;
5570     case M_SB_AB:
5571       s = "sb";
5572       goto st;
5573     case M_SH_AB:
5574       s = "sh";
5575       goto st;
5576     case M_SW_AB:
5577       s = "sw";
5578       goto st;
5579     case M_SWC0_AB:
5580       s = "swc0";
5581       /* Itbl support may require additional care here.  */
5582       coproc = 1;
5583       goto st;
5584     case M_SWC1_AB:
5585       s = "swc1";
5586       /* Itbl support may require additional care here.  */
5587       coproc = 1;
5588       goto st;
5589     case M_SWC2_AB:
5590       s = "swc2";
5591       /* Itbl support may require additional care here.  */
5592       coproc = 1;
5593       goto st;
5594     case M_SWC3_AB:
5595       s = "swc3";
5596       /* Itbl support may require additional care here.  */
5597       coproc = 1;
5598       goto st;
5599     case M_SWL_AB:
5600       s = "swl";
5601       goto st;
5602     case M_SWR_AB:
5603       s = "swr";
5604       goto st;
5605     case M_SC_AB:
5606       s = "sc";
5607       goto st;
5608     case M_SCD_AB:
5609       s = "scd";
5610       goto st;
5611     case M_SDC1_AB:
5612       if (mips_arch == CPU_R4650)
5613         {
5614           as_bad (_("opcode not supported on this processor"));
5615           return;
5616         }
5617       s = "sdc1";
5618       coproc = 1;
5619       /* Itbl support may require additional care here.  */
5620       goto st;
5621     case M_SDC2_AB:
5622       s = "sdc2";
5623       /* Itbl support may require additional care here.  */
5624       coproc = 1;
5625       goto st;
5626     case M_SDC3_AB:
5627       s = "sdc3";
5628       /* Itbl support may require additional care here.  */
5629       coproc = 1;
5630       goto st;
5631     case M_SDL_AB:
5632       s = "sdl";
5633       goto st;
5634     case M_SDR_AB:
5635       s = "sdr";
5636     st:
5637       tempreg = AT;
5638       used_at = 1;
5639     ld_st:
5640       /* Itbl support may require additional care here.  */
5641       if (mask == M_LWC1_AB
5642           || mask == M_SWC1_AB
5643           || mask == M_LDC1_AB
5644           || mask == M_SDC1_AB
5645           || mask == M_L_DAB
5646           || mask == M_S_DAB)
5647         fmt = "T,o(b)";
5648       else if (coproc)
5649         fmt = "E,o(b)";
5650       else
5651         fmt = "t,o(b)";
5652
5653       /* For embedded PIC, we allow loads where the offset is calculated
5654          by subtracting a symbol in the current segment from an unknown
5655          symbol, relative to a base register, e.g.:
5656                 <op>    $treg, <sym>-<localsym>($breg)
5657          This is used by the compiler for switch statements.  */
5658       if (mips_pic == EMBEDDED_PIC
5659           && offset_expr.X_op == O_subtract
5660           && (symbol_constant_p (offset_expr.X_op_symbol)
5661               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5662               : (symbol_equated_p (offset_expr.X_op_symbol)
5663                  && (S_GET_SEGMENT
5664                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5665                       ->X_add_symbol)
5666                      == now_seg)))
5667           && breg != 0
5668           && (offset_expr.X_add_number == 0
5669               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5670         {
5671           /* For this case, we output the instructions:
5672                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5673                 addiu   $tempreg,$tempreg,$breg
5674                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5675              If the relocation would fit entirely in 16 bits, it would be
5676              nice to emit:
5677                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5678              instead, but that seems quite difficult.  */
5679           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5680                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5681           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5682                        ((bfd_arch_bits_per_address (stdoutput) == 32
5683                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5684                         ? "addu" : "daddu"),
5685                        "d,v,t", tempreg, tempreg, breg);
5686           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5687                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5688           if (! used_at)
5689             return;
5690           break;
5691         }
5692
5693       if (offset_expr.X_op != O_constant
5694           && offset_expr.X_op != O_symbol)
5695         {
5696           as_bad (_("expression too complex"));
5697           offset_expr.X_op = O_constant;
5698         }
5699
5700       /* A constant expression in PIC code can be handled just as it
5701          is in non PIC code.  */
5702       if (mips_pic == NO_PIC
5703           || offset_expr.X_op == O_constant)
5704         {
5705           char *p;
5706
5707           /* If this is a reference to a GP relative symbol, and there
5708              is no base register, we want
5709                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5710              Otherwise, if there is no base register, we want
5711                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5712                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5713              If we have a constant, we need two instructions anyhow,
5714              so we always use the latter form.
5715
5716              If we have a base register, and this is a reference to a
5717              GP relative symbol, we want
5718                addu     $tempreg,$breg,$gp
5719                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5720              Otherwise we want
5721                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5722                addu     $tempreg,$tempreg,$breg
5723                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5724              With a constant we always use the latter case.
5725
5726              With 64bit address space and no base register and $at usable,
5727              we want
5728                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5729                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5730                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5731                dsll32   $tempreg,0
5732                daddu    $tempreg,$at
5733                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5734              If we have a base register, we want
5735                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5736                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5737                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5738                daddu    $at,$breg
5739                dsll32   $tempreg,0
5740                daddu    $tempreg,$at
5741                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5742
5743              Without $at we can't generate the optimal path for superscalar
5744              processors here since this would require two temporary registers.
5745                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5746                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5747                dsll     $tempreg,16
5748                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5749                dsll     $tempreg,16
5750                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5751              If we have a base register, we want
5752                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5753                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5754                dsll     $tempreg,16
5755                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5756                dsll     $tempreg,16
5757                daddu    $tempreg,$tempreg,$breg
5758                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5759
5760              If we have 64-bit addresses, as an optimization, for
5761              addresses which are 32-bit constants (e.g. kseg0/kseg1
5762              addresses) we fall back to the 32-bit address generation
5763              mechanism since it is more efficient.  Note that due to
5764              the signed offset used by memory operations, the 32-bit
5765              range is shifted down by 32768 here.  This code should
5766              probably attempt to generate 64-bit constants more
5767              efficiently in general.
5768            */
5769           if (HAVE_64BIT_ADDRESSES
5770               && !(offset_expr.X_op == O_constant
5771                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5772             {
5773               p = NULL;
5774
5775               /* We don't do GP optimization for now because RELAX_ENCODE can't
5776                  hold the data for such large chunks.  */
5777
5778               if (used_at == 0 && ! mips_opts.noat)
5779                 {
5780                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5781                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5782                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5783                                AT, (int) BFD_RELOC_HI16_S);
5784                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5785                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5786                   if (breg != 0)
5787                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5788                                  "d,v,t", AT, AT, breg);
5789                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5790                                "d,w,<", tempreg, tempreg, 0);
5791                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5792                                "d,v,t", tempreg, tempreg, AT);
5793                   macro_build (p, &icnt, &offset_expr, s,
5794                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5795                   used_at = 1;
5796                 }
5797               else
5798                 {
5799                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5800                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5801                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5802                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5803                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5804                                "d,w,<", tempreg, tempreg, 16);
5805                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5806                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5807                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5808                                "d,w,<", tempreg, tempreg, 16);
5809                   if (breg != 0)
5810                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5811                                  "d,v,t", tempreg, tempreg, breg);
5812                   macro_build (p, &icnt, &offset_expr, s,
5813                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5814                 }
5815
5816               return;
5817             }
5818
5819           if (breg == 0)
5820             {
5821               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5822                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5823                 p = NULL;
5824               else
5825                 {
5826                   frag_grow (20);
5827                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5828                                treg, (int) BFD_RELOC_GPREL16,
5829                                mips_gp_register);
5830                   p = frag_var (rs_machine_dependent, 8, 0,
5831                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5832                                               (mips_opts.warn_about_macros
5833                                                || (used_at
5834                                                    && mips_opts.noat))),
5835                                 offset_expr.X_add_symbol, 0, NULL);
5836                   used_at = 0;
5837                 }
5838               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5839               if (p != NULL)
5840                 p += 4;
5841               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5842                            (int) BFD_RELOC_LO16, tempreg);
5843             }
5844           else
5845             {
5846               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5847                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5848                 p = NULL;
5849               else
5850                 {
5851                   frag_grow (28);
5852                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5853                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5854                                "d,v,t", tempreg, breg, mips_gp_register);
5855                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5856                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5857                   p = frag_var (rs_machine_dependent, 12, 0,
5858                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5859                                 offset_expr.X_add_symbol, 0, NULL);
5860                 }
5861               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5862               if (p != NULL)
5863                 p += 4;
5864               macro_build (p, &icnt, (expressionS *) NULL,
5865                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5866                            "d,v,t", tempreg, tempreg, breg);
5867               if (p != NULL)
5868                 p += 4;
5869               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5870                            (int) BFD_RELOC_LO16, tempreg);
5871             }
5872         }
5873       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5874         {
5875           char *p;
5876           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5877
5878           /* If this is a reference to an external symbol, we want
5879                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5880                nop
5881                <op>     $treg,0($tempreg)
5882              Otherwise we want
5883                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5884                nop
5885                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5886                <op>     $treg,0($tempreg)
5887              If we have NewABI, we want
5888                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5889              If there is a base register, we add it to $tempreg before
5890              the <op>.  If there is a constant, we stick it in the
5891              <op> instruction.  We don't handle constants larger than
5892              16 bits, because we have no way to load the upper 16 bits
5893              (actually, we could handle them for the subset of cases
5894              in which we are not using $at).  */
5895           assert (offset_expr.X_op == O_symbol);
5896           expr1.X_add_number = offset_expr.X_add_number;
5897           offset_expr.X_add_number = 0;
5898           if (HAVE_NEWABI)
5899             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5900           if (expr1.X_add_number < -0x8000
5901               || expr1.X_add_number >= 0x8000)
5902             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5903           frag_grow (20);
5904           macro_build ((char *) NULL, &icnt, &offset_expr,
5905                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5906                        (int) lw_reloc_type, mips_gp_register);
5907           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5908           p = frag_var (rs_machine_dependent, 4, 0,
5909                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5910                         offset_expr.X_add_symbol, 0, NULL);
5911           macro_build (p, &icnt, &offset_expr,
5912                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5913                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5914           if (breg != 0)
5915             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5916                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5917                          "d,v,t", tempreg, tempreg, breg);
5918           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5919                        (int) BFD_RELOC_LO16, tempreg);
5920         }
5921       else if (mips_pic == SVR4_PIC)
5922         {
5923           int gpdel;
5924           char *p;
5925
5926           /* If this is a reference to an external symbol, we want
5927                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5928                addu     $tempreg,$tempreg,$gp
5929                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5930                <op>     $treg,0($tempreg)
5931              Otherwise we want
5932                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5933                nop
5934                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5935                <op>     $treg,0($tempreg)
5936              If there is a base register, we add it to $tempreg before
5937              the <op>.  If there is a constant, we stick it in the
5938              <op> instruction.  We don't handle constants larger than
5939              16 bits, because we have no way to load the upper 16 bits
5940              (actually, we could handle them for the subset of cases
5941              in which we are not using $at).
5942
5943              For NewABI, we want
5944                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5945                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5946                <op>     $treg,0($tempreg)
5947            */
5948           assert (offset_expr.X_op == O_symbol);
5949           expr1.X_add_number = offset_expr.X_add_number;
5950           offset_expr.X_add_number = 0;
5951           if (expr1.X_add_number < -0x8000
5952               || expr1.X_add_number >= 0x8000)
5953             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5954           if (HAVE_NEWABI)
5955             {
5956               macro_build ((char *) NULL, &icnt, &offset_expr,
5957                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5958                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5959                            mips_gp_register);
5960               macro_build ((char *) NULL, &icnt, &offset_expr,
5961                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5962                            "t,r,j", tempreg, tempreg,
5963                            BFD_RELOC_MIPS_GOT_OFST);
5964               if (breg != 0)
5965                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5966                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5967                              "d,v,t", tempreg, tempreg, breg);
5968               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5969                            (int) BFD_RELOC_LO16, tempreg);
5970
5971               if (! used_at)
5972                 return;
5973
5974               break;
5975             }
5976           if (reg_needs_delay (mips_gp_register))
5977             gpdel = 4;
5978           else
5979             gpdel = 0;
5980           frag_grow (36);
5981           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5982                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5983           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5984                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5985                        "d,v,t", tempreg, tempreg, mips_gp_register);
5986           macro_build ((char *) NULL, &icnt, &offset_expr,
5987                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5988                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5989                        tempreg);
5990           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5991                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5992                         offset_expr.X_add_symbol, 0, NULL);
5993           if (gpdel > 0)
5994             {
5995               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5996               p += 4;
5997             }
5998           macro_build (p, &icnt, &offset_expr,
5999                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6000                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6001                        mips_gp_register);
6002           p += 4;
6003           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6004           p += 4;
6005           macro_build (p, &icnt, &offset_expr,
6006                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6007                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6008           if (breg != 0)
6009             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6010                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6011                          "d,v,t", tempreg, tempreg, breg);
6012           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6013                        (int) BFD_RELOC_LO16, tempreg);
6014         }
6015       else if (mips_pic == EMBEDDED_PIC)
6016         {
6017           /* If there is no base register, we want
6018                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6019              If there is a base register, we want
6020                addu     $tempreg,$breg,$gp
6021                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6022              */
6023           assert (offset_expr.X_op == O_symbol);
6024           if (breg == 0)
6025             {
6026               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6027                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6028               used_at = 0;
6029             }
6030           else
6031             {
6032               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6033                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6034                            "d,v,t", tempreg, breg, mips_gp_register);
6035               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6036                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6037             }
6038         }
6039       else
6040         abort ();
6041
6042       if (! used_at)
6043         return;
6044
6045       break;
6046
6047     case M_LI:
6048     case M_LI_S:
6049       load_register (&icnt, treg, &imm_expr, 0);
6050       return;
6051
6052     case M_DLI:
6053       load_register (&icnt, treg, &imm_expr, 1);
6054       return;
6055
6056     case M_LI_SS:
6057       if (imm_expr.X_op == O_constant)
6058         {
6059           load_register (&icnt, AT, &imm_expr, 0);
6060           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6061                        "mtc1", "t,G", AT, treg);
6062           break;
6063         }
6064       else
6065         {
6066           assert (offset_expr.X_op == O_symbol
6067                   && strcmp (segment_name (S_GET_SEGMENT
6068                                            (offset_expr.X_add_symbol)),
6069                              ".lit4") == 0
6070                   && offset_expr.X_add_number == 0);
6071           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6072                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6073           return;
6074         }
6075
6076     case M_LI_D:
6077       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6078          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6079          order 32 bits of the value and the low order 32 bits are either
6080          zero or in OFFSET_EXPR.  */
6081       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6082         {
6083           if (HAVE_64BIT_GPRS)
6084             load_register (&icnt, treg, &imm_expr, 1);
6085           else
6086             {
6087               int hreg, lreg;
6088
6089               if (target_big_endian)
6090                 {
6091                   hreg = treg;
6092                   lreg = treg + 1;
6093                 }
6094               else
6095                 {
6096                   hreg = treg + 1;
6097                   lreg = treg;
6098                 }
6099
6100               if (hreg <= 31)
6101                 load_register (&icnt, hreg, &imm_expr, 0);
6102               if (lreg <= 31)
6103                 {
6104                   if (offset_expr.X_op == O_absent)
6105                     move_register (&icnt, lreg, 0);
6106                   else
6107                     {
6108                       assert (offset_expr.X_op == O_constant);
6109                       load_register (&icnt, lreg, &offset_expr, 0);
6110                     }
6111                 }
6112             }
6113           return;
6114         }
6115
6116       /* We know that sym is in the .rdata section.  First we get the
6117          upper 16 bits of the address.  */
6118       if (mips_pic == NO_PIC)
6119         {
6120           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6121         }
6122       else if (mips_pic == SVR4_PIC)
6123         {
6124           macro_build ((char *) NULL, &icnt, &offset_expr,
6125                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6126                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6127                        mips_gp_register);
6128         }
6129       else if (mips_pic == EMBEDDED_PIC)
6130         {
6131           /* For embedded PIC we pick up the entire address off $gp in
6132              a single instruction.  */
6133           macro_build ((char *) NULL, &icnt, &offset_expr,
6134                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6135                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6136           offset_expr.X_op = O_constant;
6137           offset_expr.X_add_number = 0;
6138         }
6139       else
6140         abort ();
6141
6142       /* Now we load the register(s).  */
6143       if (HAVE_64BIT_GPRS)
6144         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6145                      treg, (int) BFD_RELOC_LO16, AT);
6146       else
6147         {
6148           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6149                        treg, (int) BFD_RELOC_LO16, AT);
6150           if (treg != RA)
6151             {
6152               /* FIXME: How in the world do we deal with the possible
6153                  overflow here?  */
6154               offset_expr.X_add_number += 4;
6155               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6156                            treg + 1, (int) BFD_RELOC_LO16, AT);
6157             }
6158         }
6159
6160       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6161          does not become a variant frag.  */
6162       frag_wane (frag_now);
6163       frag_new (0);
6164
6165       break;
6166
6167     case M_LI_DD:
6168       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6169          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6170          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6171          the value and the low order 32 bits are either zero or in
6172          OFFSET_EXPR.  */
6173       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6174         {
6175           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6176           if (HAVE_64BIT_FPRS)
6177             {
6178               assert (HAVE_64BIT_GPRS);
6179               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6180                            "dmtc1", "t,S", AT, treg);
6181             }
6182           else
6183             {
6184               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6185                            "mtc1", "t,G", AT, treg + 1);
6186               if (offset_expr.X_op == O_absent)
6187                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6188                              "mtc1", "t,G", 0, treg);
6189               else
6190                 {
6191                   assert (offset_expr.X_op == O_constant);
6192                   load_register (&icnt, AT, &offset_expr, 0);
6193                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6194                                "mtc1", "t,G", AT, treg);
6195                 }
6196             }
6197           break;
6198         }
6199
6200       assert (offset_expr.X_op == O_symbol
6201               && offset_expr.X_add_number == 0);
6202       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6203       if (strcmp (s, ".lit8") == 0)
6204         {
6205           if (mips_opts.isa != ISA_MIPS1)
6206             {
6207               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6208                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6209                            mips_gp_register);
6210               return;
6211             }
6212           breg = mips_gp_register;
6213           r = BFD_RELOC_MIPS_LITERAL;
6214           goto dob;
6215         }
6216       else
6217         {
6218           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6219           if (mips_pic == SVR4_PIC)
6220             macro_build ((char *) NULL, &icnt, &offset_expr,
6221                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6222                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6223                          mips_gp_register);
6224           else
6225             {
6226               /* FIXME: This won't work for a 64 bit address.  */
6227               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6228             }
6229
6230           if (mips_opts.isa != ISA_MIPS1)
6231             {
6232               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6233                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6234
6235               /* To avoid confusion in tc_gen_reloc, we must ensure
6236                  that this does not become a variant frag.  */
6237               frag_wane (frag_now);
6238               frag_new (0);
6239
6240               break;
6241             }
6242           breg = AT;
6243           r = BFD_RELOC_LO16;
6244           goto dob;
6245         }
6246
6247     case M_L_DOB:
6248       if (mips_arch == CPU_R4650)
6249         {
6250           as_bad (_("opcode not supported on this processor"));
6251           return;
6252         }
6253       /* Even on a big endian machine $fn comes before $fn+1.  We have
6254          to adjust when loading from memory.  */
6255       r = BFD_RELOC_LO16;
6256     dob:
6257       assert (mips_opts.isa == ISA_MIPS1);
6258       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6259                    target_big_endian ? treg + 1 : treg,
6260                    (int) r, breg);
6261       /* FIXME: A possible overflow which I don't know how to deal
6262          with.  */
6263       offset_expr.X_add_number += 4;
6264       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6265                    target_big_endian ? treg : treg + 1,
6266                    (int) r, breg);
6267
6268       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6269          does not become a variant frag.  */
6270       frag_wane (frag_now);
6271       frag_new (0);
6272
6273       if (breg != AT)
6274         return;
6275       break;
6276
6277     case M_L_DAB:
6278       /*
6279        * The MIPS assembler seems to check for X_add_number not
6280        * being double aligned and generating:
6281        *        lui     at,%hi(foo+1)
6282        *        addu    at,at,v1
6283        *        addiu   at,at,%lo(foo+1)
6284        *        lwc1    f2,0(at)
6285        *        lwc1    f3,4(at)
6286        * But, the resulting address is the same after relocation so why
6287        * generate the extra instruction?
6288        */
6289       if (mips_arch == CPU_R4650)
6290         {
6291           as_bad (_("opcode not supported on this processor"));
6292           return;
6293         }
6294       /* Itbl support may require additional care here.  */
6295       coproc = 1;
6296       if (mips_opts.isa != ISA_MIPS1)
6297         {
6298           s = "ldc1";
6299           goto ld;
6300         }
6301
6302       s = "lwc1";
6303       fmt = "T,o(b)";
6304       goto ldd_std;
6305
6306     case M_S_DAB:
6307       if (mips_arch == CPU_R4650)
6308         {
6309           as_bad (_("opcode not supported on this processor"));
6310           return;
6311         }
6312
6313       if (mips_opts.isa != ISA_MIPS1)
6314         {
6315           s = "sdc1";
6316           goto st;
6317         }
6318
6319       s = "swc1";
6320       fmt = "T,o(b)";
6321       /* Itbl support may require additional care here.  */
6322       coproc = 1;
6323       goto ldd_std;
6324
6325     case M_LD_AB:
6326       if (HAVE_64BIT_GPRS)
6327         {
6328           s = "ld";
6329           goto ld;
6330         }
6331
6332       s = "lw";
6333       fmt = "t,o(b)";
6334       goto ldd_std;
6335
6336     case M_SD_AB:
6337       if (HAVE_64BIT_GPRS)
6338         {
6339           s = "sd";
6340           goto st;
6341         }
6342
6343       s = "sw";
6344       fmt = "t,o(b)";
6345
6346     ldd_std:
6347       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6348          loads for the case of doing a pair of loads to simulate an 'ld'.
6349          This is not currently done by the compiler, and assembly coders
6350          writing embedded-pic code can cope.  */
6351
6352       if (offset_expr.X_op != O_symbol
6353           && offset_expr.X_op != O_constant)
6354         {
6355           as_bad (_("expression too complex"));
6356           offset_expr.X_op = O_constant;
6357         }
6358
6359       /* Even on a big endian machine $fn comes before $fn+1.  We have
6360          to adjust when loading from memory.  We set coproc if we must
6361          load $fn+1 first.  */
6362       /* Itbl support may require additional care here.  */
6363       if (! target_big_endian)
6364         coproc = 0;
6365
6366       if (mips_pic == NO_PIC
6367           || offset_expr.X_op == O_constant)
6368         {
6369           char *p;
6370
6371           /* If this is a reference to a GP relative symbol, we want
6372                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6373                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6374              If we have a base register, we use this
6375                addu     $at,$breg,$gp
6376                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6377                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6378              If this is not a GP relative symbol, we want
6379                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6380                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6381                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6382              If there is a base register, we add it to $at after the
6383              lui instruction.  If there is a constant, we always use
6384              the last case.  */
6385           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6386               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6387             {
6388               p = NULL;
6389               used_at = 1;
6390             }
6391           else
6392             {
6393               int off;
6394
6395               if (breg == 0)
6396                 {
6397                   frag_grow (28);
6398                   tempreg = mips_gp_register;
6399                   off = 0;
6400                   used_at = 0;
6401                 }
6402               else
6403                 {
6404                   frag_grow (36);
6405                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6406                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6407                                "d,v,t", AT, breg, mips_gp_register);
6408                   tempreg = AT;
6409                   off = 4;
6410                   used_at = 1;
6411                 }
6412
6413               /* Itbl support may require additional care here.  */
6414               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6415                            coproc ? treg + 1 : treg,
6416                            (int) BFD_RELOC_GPREL16, tempreg);
6417               offset_expr.X_add_number += 4;
6418
6419               /* Set mips_optimize to 2 to avoid inserting an
6420                  undesired nop.  */
6421               hold_mips_optimize = mips_optimize;
6422               mips_optimize = 2;
6423               /* Itbl support may require additional care here.  */
6424               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6425                            coproc ? treg : treg + 1,
6426                            (int) BFD_RELOC_GPREL16, tempreg);
6427               mips_optimize = hold_mips_optimize;
6428
6429               p = frag_var (rs_machine_dependent, 12 + off, 0,
6430                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6431                                           used_at && mips_opts.noat),
6432                             offset_expr.X_add_symbol, 0, NULL);
6433
6434               /* We just generated two relocs.  When tc_gen_reloc
6435                  handles this case, it will skip the first reloc and
6436                  handle the second.  The second reloc already has an
6437                  extra addend of 4, which we added above.  We must
6438                  subtract it out, and then subtract another 4 to make
6439                  the first reloc come out right.  The second reloc
6440                  will come out right because we are going to add 4 to
6441                  offset_expr when we build its instruction below.
6442
6443                  If we have a symbol, then we don't want to include
6444                  the offset, because it will wind up being included
6445                  when we generate the reloc.  */
6446
6447               if (offset_expr.X_op == O_constant)
6448                 offset_expr.X_add_number -= 8;
6449               else
6450                 {
6451                   offset_expr.X_add_number = -4;
6452                   offset_expr.X_op = O_constant;
6453                 }
6454             }
6455           macro_build_lui (p, &icnt, &offset_expr, AT);
6456           if (p != NULL)
6457             p += 4;
6458           if (breg != 0)
6459             {
6460               macro_build (p, &icnt, (expressionS *) NULL,
6461                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6462                            "d,v,t", AT, breg, AT);
6463               if (p != NULL)
6464                 p += 4;
6465             }
6466           /* Itbl support may require additional care here.  */
6467           macro_build (p, &icnt, &offset_expr, s, fmt,
6468                        coproc ? treg + 1 : treg,
6469                        (int) BFD_RELOC_LO16, AT);
6470           if (p != NULL)
6471             p += 4;
6472           /* FIXME: How do we handle overflow here?  */
6473           offset_expr.X_add_number += 4;
6474           /* Itbl support may require additional care here.  */
6475           macro_build (p, &icnt, &offset_expr, s, fmt,
6476                        coproc ? treg : treg + 1,
6477                        (int) BFD_RELOC_LO16, AT);
6478         }
6479       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6480         {
6481           int off;
6482
6483           /* If this is a reference to an external symbol, we want
6484                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6485                nop
6486                <op>     $treg,0($at)
6487                <op>     $treg+1,4($at)
6488              Otherwise we want
6489                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6490                nop
6491                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6492                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6493              If there is a base register we add it to $at before the
6494              lwc1 instructions.  If there is a constant we include it
6495              in the lwc1 instructions.  */
6496           used_at = 1;
6497           expr1.X_add_number = offset_expr.X_add_number;
6498           offset_expr.X_add_number = 0;
6499           if (expr1.X_add_number < -0x8000
6500               || expr1.X_add_number >= 0x8000 - 4)
6501             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6502           if (breg == 0)
6503             off = 0;
6504           else
6505             off = 4;
6506           frag_grow (24 + off);
6507           macro_build ((char *) NULL, &icnt, &offset_expr,
6508                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6509                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6510           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6511           if (breg != 0)
6512             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6513                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6514                          "d,v,t", AT, breg, AT);
6515           /* Itbl support may require additional care here.  */
6516           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6517                        coproc ? treg + 1 : treg,
6518                        (int) BFD_RELOC_LO16, AT);
6519           expr1.X_add_number += 4;
6520
6521           /* Set mips_optimize to 2 to avoid inserting an undesired
6522              nop.  */
6523           hold_mips_optimize = mips_optimize;
6524           mips_optimize = 2;
6525           /* Itbl support may require additional care here.  */
6526           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6527                        coproc ? treg : treg + 1,
6528                        (int) BFD_RELOC_LO16, AT);
6529           mips_optimize = hold_mips_optimize;
6530
6531           (void) frag_var (rs_machine_dependent, 0, 0,
6532                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6533                            offset_expr.X_add_symbol, 0, NULL);
6534         }
6535       else if (mips_pic == SVR4_PIC)
6536         {
6537           int gpdel, off;
6538           char *p;
6539
6540           /* If this is a reference to an external symbol, we want
6541                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6542                addu     $at,$at,$gp
6543                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6544                nop
6545                <op>     $treg,0($at)
6546                <op>     $treg+1,4($at)
6547              Otherwise we want
6548                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6549                nop
6550                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6551                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6552              If there is a base register we add it to $at before the
6553              lwc1 instructions.  If there is a constant we include it
6554              in the lwc1 instructions.  */
6555           used_at = 1;
6556           expr1.X_add_number = offset_expr.X_add_number;
6557           offset_expr.X_add_number = 0;
6558           if (expr1.X_add_number < -0x8000
6559               || expr1.X_add_number >= 0x8000 - 4)
6560             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6561           if (reg_needs_delay (mips_gp_register))
6562             gpdel = 4;
6563           else
6564             gpdel = 0;
6565           if (breg == 0)
6566             off = 0;
6567           else
6568             off = 4;
6569           frag_grow (56);
6570           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6571                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6572           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6573                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6574                        "d,v,t", AT, AT, mips_gp_register);
6575           macro_build ((char *) NULL, &icnt, &offset_expr,
6576                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6577                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6578           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6579           if (breg != 0)
6580             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6581                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6582                          "d,v,t", AT, breg, AT);
6583           /* Itbl support may require additional care here.  */
6584           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6585                        coproc ? treg + 1 : treg,
6586                        (int) BFD_RELOC_LO16, AT);
6587           expr1.X_add_number += 4;
6588
6589           /* Set mips_optimize to 2 to avoid inserting an undesired
6590              nop.  */
6591           hold_mips_optimize = mips_optimize;
6592           mips_optimize = 2;
6593           /* Itbl support may require additional care here.  */
6594           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6595                        coproc ? treg : treg + 1,
6596                        (int) BFD_RELOC_LO16, AT);
6597           mips_optimize = hold_mips_optimize;
6598           expr1.X_add_number -= 4;
6599
6600           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6601                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6602                                       8 + gpdel + off, 1, 0),
6603                         offset_expr.X_add_symbol, 0, NULL);
6604           if (gpdel > 0)
6605             {
6606               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6607               p += 4;
6608             }
6609           macro_build (p, &icnt, &offset_expr,
6610                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6611                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6612                        mips_gp_register);
6613           p += 4;
6614           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6615           p += 4;
6616           if (breg != 0)
6617             {
6618               macro_build (p, &icnt, (expressionS *) NULL,
6619                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6620                            "d,v,t", AT, breg, AT);
6621               p += 4;
6622             }
6623           /* Itbl support may require additional care here.  */
6624           macro_build (p, &icnt, &expr1, s, fmt,
6625                        coproc ? treg + 1 : treg,
6626                        (int) BFD_RELOC_LO16, AT);
6627           p += 4;
6628           expr1.X_add_number += 4;
6629
6630           /* Set mips_optimize to 2 to avoid inserting an undesired
6631              nop.  */
6632           hold_mips_optimize = mips_optimize;
6633           mips_optimize = 2;
6634           /* Itbl support may require additional care here.  */
6635           macro_build (p, &icnt, &expr1, s, fmt,
6636                        coproc ? treg : treg + 1,
6637                        (int) BFD_RELOC_LO16, AT);
6638           mips_optimize = hold_mips_optimize;
6639         }
6640       else if (mips_pic == EMBEDDED_PIC)
6641         {
6642           /* If there is no base register, we use
6643                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6644                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6645              If we have a base register, we use
6646                addu     $at,$breg,$gp
6647                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6648                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6649              */
6650           if (breg == 0)
6651             {
6652               tempreg = mips_gp_register;
6653               used_at = 0;
6654             }
6655           else
6656             {
6657               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6658                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6659                            "d,v,t", AT, breg, mips_gp_register);
6660               tempreg = AT;
6661               used_at = 1;
6662             }
6663
6664           /* Itbl support may require additional care here.  */
6665           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6666                        coproc ? treg + 1 : treg,
6667                        (int) BFD_RELOC_GPREL16, tempreg);
6668           offset_expr.X_add_number += 4;
6669           /* Itbl support may require additional care here.  */
6670           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6671                        coproc ? treg : treg + 1,
6672                        (int) BFD_RELOC_GPREL16, tempreg);
6673         }
6674       else
6675         abort ();
6676
6677       if (! used_at)
6678         return;
6679
6680       break;
6681
6682     case M_LD_OB:
6683       s = "lw";
6684       goto sd_ob;
6685     case M_SD_OB:
6686       s = "sw";
6687     sd_ob:
6688       assert (HAVE_32BIT_ADDRESSES);
6689       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6690                    (int) BFD_RELOC_LO16, breg);
6691       offset_expr.X_add_number += 4;
6692       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6693                    (int) BFD_RELOC_LO16, breg);
6694       return;
6695
6696    /* New code added to support COPZ instructions.
6697       This code builds table entries out of the macros in mip_opcodes.
6698       R4000 uses interlocks to handle coproc delays.
6699       Other chips (like the R3000) require nops to be inserted for delays.
6700
6701       FIXME: Currently, we require that the user handle delays.
6702       In order to fill delay slots for non-interlocked chips,
6703       we must have a way to specify delays based on the coprocessor.
6704       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6705       What are the side-effects of the cop instruction?
6706       What cache support might we have and what are its effects?
6707       Both coprocessor & memory require delays. how long???
6708       What registers are read/set/modified?
6709
6710       If an itbl is provided to interpret cop instructions,
6711       this knowledge can be encoded in the itbl spec.  */
6712
6713     case M_COP0:
6714       s = "c0";
6715       goto copz;
6716     case M_COP1:
6717       s = "c1";
6718       goto copz;
6719     case M_COP2:
6720       s = "c2";
6721       goto copz;
6722     case M_COP3:
6723       s = "c3";
6724     copz:
6725       /* For now we just do C (same as Cz).  The parameter will be
6726          stored in insn_opcode by mips_ip.  */
6727       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6728                    ip->insn_opcode);
6729       return;
6730
6731     case M_MOVE:
6732       move_register (&icnt, dreg, sreg);
6733       return;
6734
6735 #ifdef LOSING_COMPILER
6736     default:
6737       /* Try and see if this is a new itbl instruction.
6738          This code builds table entries out of the macros in mip_opcodes.
6739          FIXME: For now we just assemble the expression and pass it's
6740          value along as a 32-bit immediate.
6741          We may want to have the assembler assemble this value,
6742          so that we gain the assembler's knowledge of delay slots,
6743          symbols, etc.
6744          Would it be more efficient to use mask (id) here? */
6745       if (itbl_have_entries
6746           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6747         {
6748           s = ip->insn_mo->name;
6749           s2 = "cop3";
6750           coproc = ITBL_DECODE_PNUM (immed_expr);;
6751           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6752           return;
6753         }
6754       macro2 (ip);
6755       return;
6756     }
6757   if (mips_opts.noat)
6758     as_warn (_("Macro used $at after \".set noat\""));
6759 }
6760
6761 static void
6762 macro2 (ip)
6763      struct mips_cl_insn *ip;
6764 {
6765   register int treg, sreg, dreg, breg;
6766   int tempreg;
6767   int mask;
6768   int icnt = 0;
6769   int used_at;
6770   expressionS expr1;
6771   const char *s;
6772   const char *s2;
6773   const char *fmt;
6774   int likely = 0;
6775   int dbl = 0;
6776   int coproc = 0;
6777   int lr = 0;
6778   int imm = 0;
6779   int off;
6780   offsetT maxnum;
6781   bfd_reloc_code_real_type r;
6782   char *p;
6783
6784   treg = (ip->insn_opcode >> 16) & 0x1f;
6785   dreg = (ip->insn_opcode >> 11) & 0x1f;
6786   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6787   mask = ip->insn_mo->mask;
6788
6789   expr1.X_op = O_constant;
6790   expr1.X_op_symbol = NULL;
6791   expr1.X_add_symbol = NULL;
6792   expr1.X_add_number = 1;
6793
6794   switch (mask)
6795     {
6796 #endif /* LOSING_COMPILER */
6797
6798     case M_DMUL:
6799       dbl = 1;
6800     case M_MUL:
6801       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6802                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6803       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6804                    dreg);
6805       return;
6806
6807     case M_DMUL_I:
6808       dbl = 1;
6809     case M_MUL_I:
6810       /* The MIPS assembler some times generates shifts and adds.  I'm
6811          not trying to be that fancy. GCC should do this for us
6812          anyway.  */
6813       load_register (&icnt, AT, &imm_expr, dbl);
6814       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6815                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6816       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6817                    dreg);
6818       break;
6819
6820     case M_DMULO_I:
6821       dbl = 1;
6822     case M_MULO_I:
6823       imm = 1;
6824       goto do_mulo;
6825
6826     case M_DMULO:
6827       dbl = 1;
6828     case M_MULO:
6829     do_mulo:
6830       mips_emit_delays (true);
6831       ++mips_opts.noreorder;
6832       mips_any_noreorder = 1;
6833       if (imm)
6834         load_register (&icnt, AT, &imm_expr, dbl);
6835       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6836                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6837       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6838                    dreg);
6839       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6840                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6841       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6842                    AT);
6843       if (mips_trap)
6844         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6845                      "s,t,q", dreg, AT, 6);
6846       else
6847         {
6848           expr1.X_add_number = 8;
6849           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6850                        AT);
6851           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6852                        0);
6853           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6854                        "c", 6);
6855         }
6856       --mips_opts.noreorder;
6857       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6858       break;
6859
6860     case M_DMULOU_I:
6861       dbl = 1;
6862     case M_MULOU_I:
6863       imm = 1;
6864       goto do_mulou;
6865
6866     case M_DMULOU:
6867       dbl = 1;
6868     case M_MULOU:
6869     do_mulou:
6870       mips_emit_delays (true);
6871       ++mips_opts.noreorder;
6872       mips_any_noreorder = 1;
6873       if (imm)
6874         load_register (&icnt, AT, &imm_expr, dbl);
6875       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6876                    dbl ? "dmultu" : "multu",
6877                    "s,t", sreg, imm ? AT : treg);
6878       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6879                    AT);
6880       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6881                    dreg);
6882       if (mips_trap)
6883         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6884                      "s,t,q", AT, 0, 6);
6885       else
6886         {
6887           expr1.X_add_number = 8;
6888           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6889           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6890                        0);
6891           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6892                        "c", 6);
6893         }
6894       --mips_opts.noreorder;
6895       break;
6896
6897     case M_DROL:
6898       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6899                    "d,v,t", AT, 0, treg);
6900       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6901                    "d,t,s", AT, sreg, AT);
6902       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6903                    "d,t,s", dreg, sreg, treg);
6904       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6905                    "d,v,t", dreg, dreg, AT);
6906       break;
6907
6908     case M_ROL:
6909       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6910                    "d,v,t", AT, 0, treg);
6911       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6912                    "d,t,s", AT, sreg, AT);
6913       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6914                    "d,t,s", dreg, sreg, treg);
6915       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6916                    "d,v,t", dreg, dreg, AT);
6917       break;
6918
6919     case M_DROL_I:
6920       {
6921         unsigned int rot;
6922
6923         if (imm_expr.X_op != O_constant)
6924           as_bad (_("rotate count too large"));
6925         rot = imm_expr.X_add_number & 0x3f;
6926         if (CPU_HAS_DROR (mips_arch))
6927           {
6928             rot = (64 - rot) & 0x3f;
6929             if (rot >= 32)
6930               macro_build ((char *) NULL, &icnt, NULL, "dror32",
6931                            "d,w,<", dreg, sreg, rot - 32);
6932             else
6933               macro_build ((char *) NULL, &icnt, NULL, "dror",
6934                            "d,w,<", dreg, sreg, rot);
6935             break;
6936           }
6937         if (rot == 0)
6938           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6939                        "d,w,<", dreg, sreg, 0);
6940         else
6941           {
6942             char *l, *r;
6943
6944             l = (rot < 0x20) ? "dsll" : "dsll32";
6945             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6946             rot &= 0x1f;
6947             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6948                          "d,w,<", AT, sreg, rot);
6949             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6950                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6951             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6952                          "d,v,t", dreg, dreg, AT);
6953           }
6954       }
6955       break;
6956
6957     case M_ROL_I:
6958       {
6959         unsigned int rot;
6960
6961         if (imm_expr.X_op != O_constant)
6962           as_bad (_("rotate count too large"));
6963         rot = imm_expr.X_add_number & 0x1f;
6964         if (CPU_HAS_ROR (mips_arch))
6965           {
6966             macro_build ((char *) NULL, &icnt, NULL, "ror",
6967                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6968             break;
6969           }
6970         if (rot == 0)
6971           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6972                        "d,w,<", dreg, sreg, 0);
6973         else
6974           {
6975             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6976                          "d,w,<", AT, sreg, rot);
6977             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6978                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6979             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6980                          "d,v,t", dreg, dreg, AT);
6981           }
6982       }
6983       break;
6984
6985     case M_DROR:
6986       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6987                    "d,v,t", AT, 0, treg);
6988       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6989                    "d,t,s", AT, sreg, AT);
6990       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6991                    "d,t,s", dreg, sreg, treg);
6992       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6993                    "d,v,t", dreg, dreg, AT);
6994       break;
6995
6996     case M_ROR:
6997       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6998                    "d,v,t", AT, 0, treg);
6999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7000                    "d,t,s", AT, sreg, AT);
7001       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7002                    "d,t,s", dreg, sreg, treg);
7003       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7004                    "d,v,t", dreg, dreg, AT);
7005       break;
7006
7007     case M_DROR_I:
7008       {
7009         unsigned int rot;
7010
7011         if (imm_expr.X_op != O_constant)
7012           as_bad (_("rotate count too large"));
7013         rot = imm_expr.X_add_number & 0x3f;
7014         if (rot == 0)
7015           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7016                        "d,w,<", dreg, sreg, 0);
7017         else
7018           {
7019             char *l, *r;
7020
7021             r = (rot < 0x20) ? "dsrl" : "dsrl32";
7022             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7023             rot &= 0x1f;
7024             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7025                          "d,w,<", AT, sreg, rot);
7026             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7027                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7028             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7029                          "d,v,t", dreg, dreg, AT);
7030           }
7031       }
7032       break;
7033
7034     case M_ROR_I:
7035       {
7036         unsigned int rot;
7037
7038         if (imm_expr.X_op != O_constant)
7039           as_bad (_("rotate count too large"));
7040         rot = imm_expr.X_add_number & 0x1f;
7041         if (rot == 0)
7042           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7043                        "d,w,<", dreg, sreg, 0);
7044         else
7045           {
7046             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7047                          "d,w,<", AT, sreg, rot);
7048             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7049                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7050             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7051                          "d,v,t", dreg, dreg, AT);
7052           }
7053       }
7054       break;
7055
7056     case M_S_DOB:
7057       if (mips_arch == CPU_R4650)
7058         {
7059           as_bad (_("opcode not supported on this processor"));
7060           return;
7061         }
7062       assert (mips_opts.isa == ISA_MIPS1);
7063       /* Even on a big endian machine $fn comes before $fn+1.  We have
7064          to adjust when storing to memory.  */
7065       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7066                    target_big_endian ? treg + 1 : treg,
7067                    (int) BFD_RELOC_LO16, breg);
7068       offset_expr.X_add_number += 4;
7069       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7070                    target_big_endian ? treg : treg + 1,
7071                    (int) BFD_RELOC_LO16, breg);
7072       return;
7073
7074     case M_SEQ:
7075       if (sreg == 0)
7076         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7077                      treg, (int) BFD_RELOC_LO16);
7078       else if (treg == 0)
7079         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7080                      sreg, (int) BFD_RELOC_LO16);
7081       else
7082         {
7083           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7084                        "d,v,t", dreg, sreg, treg);
7085           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7086                        dreg, (int) BFD_RELOC_LO16);
7087         }
7088       return;
7089
7090     case M_SEQ_I:
7091       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7092         {
7093           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7094                        sreg, (int) BFD_RELOC_LO16);
7095           return;
7096         }
7097       if (sreg == 0)
7098         {
7099           as_warn (_("Instruction %s: result is always false"),
7100                    ip->insn_mo->name);
7101           move_register (&icnt, dreg, 0);
7102           return;
7103         }
7104       if (imm_expr.X_op == O_constant
7105           && imm_expr.X_add_number >= 0
7106           && imm_expr.X_add_number < 0x10000)
7107         {
7108           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7109                        sreg, (int) BFD_RELOC_LO16);
7110           used_at = 0;
7111         }
7112       else if (imm_expr.X_op == O_constant
7113                && imm_expr.X_add_number > -0x8000
7114                && imm_expr.X_add_number < 0)
7115         {
7116           imm_expr.X_add_number = -imm_expr.X_add_number;
7117           macro_build ((char *) NULL, &icnt, &imm_expr,
7118                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7119                        "t,r,j", dreg, sreg,
7120                        (int) BFD_RELOC_LO16);
7121           used_at = 0;
7122         }
7123       else
7124         {
7125           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7126           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7127                        "d,v,t", dreg, sreg, AT);
7128           used_at = 1;
7129         }
7130       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7131                    (int) BFD_RELOC_LO16);
7132       if (used_at)
7133         break;
7134       return;
7135
7136     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7137       s = "slt";
7138       goto sge;
7139     case M_SGEU:
7140       s = "sltu";
7141     sge:
7142       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7143                    dreg, sreg, treg);
7144       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7145                    (int) BFD_RELOC_LO16);
7146       return;
7147
7148     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7149     case M_SGEU_I:
7150       if (imm_expr.X_op == O_constant
7151           && imm_expr.X_add_number >= -0x8000
7152           && imm_expr.X_add_number < 0x8000)
7153         {
7154           macro_build ((char *) NULL, &icnt, &imm_expr,
7155                        mask == M_SGE_I ? "slti" : "sltiu",
7156                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7157           used_at = 0;
7158         }
7159       else
7160         {
7161           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7162           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7163                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7164                        AT);
7165           used_at = 1;
7166         }
7167       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7168                    (int) BFD_RELOC_LO16);
7169       if (used_at)
7170         break;
7171       return;
7172
7173     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7174       s = "slt";
7175       goto sgt;
7176     case M_SGTU:
7177       s = "sltu";
7178     sgt:
7179       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7180                    dreg, treg, sreg);
7181       return;
7182
7183     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7184       s = "slt";
7185       goto sgti;
7186     case M_SGTU_I:
7187       s = "sltu";
7188     sgti:
7189       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7190       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7191                    dreg, AT, sreg);
7192       break;
7193
7194     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7195       s = "slt";
7196       goto sle;
7197     case M_SLEU:
7198       s = "sltu";
7199     sle:
7200       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7201                    dreg, treg, sreg);
7202       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7203                    (int) BFD_RELOC_LO16);
7204       return;
7205
7206     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7207       s = "slt";
7208       goto slei;
7209     case M_SLEU_I:
7210       s = "sltu";
7211     slei:
7212       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7213       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7214                    dreg, AT, sreg);
7215       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7216                    (int) BFD_RELOC_LO16);
7217       break;
7218
7219     case M_SLT_I:
7220       if (imm_expr.X_op == O_constant
7221           && imm_expr.X_add_number >= -0x8000
7222           && imm_expr.X_add_number < 0x8000)
7223         {
7224           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7225                        dreg, sreg, (int) BFD_RELOC_LO16);
7226           return;
7227         }
7228       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7229       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7230                    dreg, sreg, AT);
7231       break;
7232
7233     case M_SLTU_I:
7234       if (imm_expr.X_op == O_constant
7235           && imm_expr.X_add_number >= -0x8000
7236           && imm_expr.X_add_number < 0x8000)
7237         {
7238           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7239                        dreg, sreg, (int) BFD_RELOC_LO16);
7240           return;
7241         }
7242       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7243       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7244                    "d,v,t", dreg, sreg, AT);
7245       break;
7246
7247     case M_SNE:
7248       if (sreg == 0)
7249         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7250                      "d,v,t", dreg, 0, treg);
7251       else if (treg == 0)
7252         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7253                      "d,v,t", dreg, 0, sreg);
7254       else
7255         {
7256           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7257                        "d,v,t", dreg, sreg, treg);
7258           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7259                        "d,v,t", dreg, 0, dreg);
7260         }
7261       return;
7262
7263     case M_SNE_I:
7264       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7265         {
7266           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7267                        "d,v,t", dreg, 0, sreg);
7268           return;
7269         }
7270       if (sreg == 0)
7271         {
7272           as_warn (_("Instruction %s: result is always true"),
7273                    ip->insn_mo->name);
7274           macro_build ((char *) NULL, &icnt, &expr1,
7275                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7276                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7277           return;
7278         }
7279       if (imm_expr.X_op == O_constant
7280           && imm_expr.X_add_number >= 0
7281           && imm_expr.X_add_number < 0x10000)
7282         {
7283           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7284                        dreg, sreg, (int) BFD_RELOC_LO16);
7285           used_at = 0;
7286         }
7287       else if (imm_expr.X_op == O_constant
7288                && imm_expr.X_add_number > -0x8000
7289                && imm_expr.X_add_number < 0)
7290         {
7291           imm_expr.X_add_number = -imm_expr.X_add_number;
7292           macro_build ((char *) NULL, &icnt, &imm_expr,
7293                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7294                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7295           used_at = 0;
7296         }
7297       else
7298         {
7299           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7300           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7301                        "d,v,t", dreg, sreg, AT);
7302           used_at = 1;
7303         }
7304       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7305                    "d,v,t", dreg, 0, dreg);
7306       if (used_at)
7307         break;
7308       return;
7309
7310     case M_DSUB_I:
7311       dbl = 1;
7312     case M_SUB_I:
7313       if (imm_expr.X_op == O_constant
7314           && imm_expr.X_add_number > -0x8000
7315           && imm_expr.X_add_number <= 0x8000)
7316         {
7317           imm_expr.X_add_number = -imm_expr.X_add_number;
7318           macro_build ((char *) NULL, &icnt, &imm_expr,
7319                        dbl ? "daddi" : "addi",
7320                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7321           return;
7322         }
7323       load_register (&icnt, AT, &imm_expr, dbl);
7324       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7325                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7326       break;
7327
7328     case M_DSUBU_I:
7329       dbl = 1;
7330     case M_SUBU_I:
7331       if (imm_expr.X_op == O_constant
7332           && imm_expr.X_add_number > -0x8000
7333           && imm_expr.X_add_number <= 0x8000)
7334         {
7335           imm_expr.X_add_number = -imm_expr.X_add_number;
7336           macro_build ((char *) NULL, &icnt, &imm_expr,
7337                        dbl ? "daddiu" : "addiu",
7338                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7339           return;
7340         }
7341       load_register (&icnt, AT, &imm_expr, dbl);
7342       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7343                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7344       break;
7345
7346     case M_TEQ_I:
7347       s = "teq";
7348       goto trap;
7349     case M_TGE_I:
7350       s = "tge";
7351       goto trap;
7352     case M_TGEU_I:
7353       s = "tgeu";
7354       goto trap;
7355     case M_TLT_I:
7356       s = "tlt";
7357       goto trap;
7358     case M_TLTU_I:
7359       s = "tltu";
7360       goto trap;
7361     case M_TNE_I:
7362       s = "tne";
7363     trap:
7364       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7365       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7366                    AT);
7367       break;
7368
7369     case M_TRUNCWS:
7370     case M_TRUNCWD:
7371       assert (mips_opts.isa == ISA_MIPS1);
7372       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7373       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7374
7375       /*
7376        * Is the double cfc1 instruction a bug in the mips assembler;
7377        * or is there a reason for it?
7378        */
7379       mips_emit_delays (true);
7380       ++mips_opts.noreorder;
7381       mips_any_noreorder = 1;
7382       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7383                    treg, RA);
7384       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7385                    treg, RA);
7386       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7387       expr1.X_add_number = 3;
7388       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7389                    (int) BFD_RELOC_LO16);
7390       expr1.X_add_number = 2;
7391       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7392                      (int) BFD_RELOC_LO16);
7393       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7394                    AT, RA);
7395       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7396       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7397               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7398       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7399                    treg, RA);
7400       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7401       --mips_opts.noreorder;
7402       break;
7403
7404     case M_ULH:
7405       s = "lb";
7406       goto ulh;
7407     case M_ULHU:
7408       s = "lbu";
7409     ulh:
7410       if (offset_expr.X_add_number >= 0x7fff)
7411         as_bad (_("operand overflow"));
7412       /* avoid load delay */
7413       if (! target_big_endian)
7414         ++offset_expr.X_add_number;
7415       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7416                    (int) BFD_RELOC_LO16, breg);
7417       if (! target_big_endian)
7418         --offset_expr.X_add_number;
7419       else
7420         ++offset_expr.X_add_number;
7421       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7422                    (int) BFD_RELOC_LO16, breg);
7423       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7424                    treg, treg, 8);
7425       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7426                    treg, treg, AT);
7427       break;
7428
7429     case M_ULD:
7430       s = "ldl";
7431       s2 = "ldr";
7432       off = 7;
7433       goto ulw;
7434     case M_ULW:
7435       s = "lwl";
7436       s2 = "lwr";
7437       off = 3;
7438     ulw:
7439       if (offset_expr.X_add_number >= 0x8000 - off)
7440         as_bad (_("operand overflow"));
7441       if (! target_big_endian)
7442         offset_expr.X_add_number += off;
7443       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7444                    (int) BFD_RELOC_LO16, breg);
7445       if (! target_big_endian)
7446         offset_expr.X_add_number -= off;
7447       else
7448         offset_expr.X_add_number += off;
7449       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7450                    (int) BFD_RELOC_LO16, breg);
7451       return;
7452
7453     case M_ULD_A:
7454       s = "ldl";
7455       s2 = "ldr";
7456       off = 7;
7457       goto ulwa;
7458     case M_ULW_A:
7459       s = "lwl";
7460       s2 = "lwr";
7461       off = 3;
7462     ulwa:
7463       used_at = 1;
7464       load_address (&icnt, AT, &offset_expr, &used_at);
7465       if (breg != 0)
7466         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7467                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7468                      "d,v,t", AT, AT, breg);
7469       if (! target_big_endian)
7470         expr1.X_add_number = off;
7471       else
7472         expr1.X_add_number = 0;
7473       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7474                    (int) BFD_RELOC_LO16, AT);
7475       if (! target_big_endian)
7476         expr1.X_add_number = 0;
7477       else
7478         expr1.X_add_number = off;
7479       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7480                    (int) BFD_RELOC_LO16, AT);
7481       break;
7482
7483     case M_ULH_A:
7484     case M_ULHU_A:
7485       used_at = 1;
7486       load_address (&icnt, AT, &offset_expr, &used_at);
7487       if (breg != 0)
7488         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7489                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7490                      "d,v,t", AT, AT, breg);
7491       if (target_big_endian)
7492         expr1.X_add_number = 0;
7493       macro_build ((char *) NULL, &icnt, &expr1,
7494                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7495                    (int) BFD_RELOC_LO16, AT);
7496       if (target_big_endian)
7497         expr1.X_add_number = 1;
7498       else
7499         expr1.X_add_number = 0;
7500       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7501                    (int) BFD_RELOC_LO16, AT);
7502       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7503                    treg, treg, 8);
7504       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7505                    treg, treg, AT);
7506       break;
7507
7508     case M_USH:
7509       if (offset_expr.X_add_number >= 0x7fff)
7510         as_bad (_("operand overflow"));
7511       if (target_big_endian)
7512         ++offset_expr.X_add_number;
7513       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7514                    (int) BFD_RELOC_LO16, breg);
7515       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7516                    AT, treg, 8);
7517       if (target_big_endian)
7518         --offset_expr.X_add_number;
7519       else
7520         ++offset_expr.X_add_number;
7521       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7522                    (int) BFD_RELOC_LO16, breg);
7523       break;
7524
7525     case M_USD:
7526       s = "sdl";
7527       s2 = "sdr";
7528       off = 7;
7529       goto usw;
7530     case M_USW:
7531       s = "swl";
7532       s2 = "swr";
7533       off = 3;
7534     usw:
7535       if (offset_expr.X_add_number >= 0x8000 - off)
7536         as_bad (_("operand overflow"));
7537       if (! target_big_endian)
7538         offset_expr.X_add_number += off;
7539       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7540                    (int) BFD_RELOC_LO16, breg);
7541       if (! target_big_endian)
7542         offset_expr.X_add_number -= off;
7543       else
7544         offset_expr.X_add_number += off;
7545       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7546                    (int) BFD_RELOC_LO16, breg);
7547       return;
7548
7549     case M_USD_A:
7550       s = "sdl";
7551       s2 = "sdr";
7552       off = 7;
7553       goto uswa;
7554     case M_USW_A:
7555       s = "swl";
7556       s2 = "swr";
7557       off = 3;
7558     uswa:
7559       used_at = 1;
7560       load_address (&icnt, AT, &offset_expr, &used_at);
7561       if (breg != 0)
7562         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7563                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7564                      "d,v,t", AT, AT, breg);
7565       if (! target_big_endian)
7566         expr1.X_add_number = off;
7567       else
7568         expr1.X_add_number = 0;
7569       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7570                    (int) BFD_RELOC_LO16, AT);
7571       if (! target_big_endian)
7572         expr1.X_add_number = 0;
7573       else
7574         expr1.X_add_number = off;
7575       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7576                    (int) BFD_RELOC_LO16, AT);
7577       break;
7578
7579     case M_USH_A:
7580       used_at = 1;
7581       load_address (&icnt, AT, &offset_expr, &used_at);
7582       if (breg != 0)
7583         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7584                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7585                      "d,v,t", AT, AT, breg);
7586       if (! target_big_endian)
7587         expr1.X_add_number = 0;
7588       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7589                    (int) BFD_RELOC_LO16, AT);
7590       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7591                    treg, treg, 8);
7592       if (! target_big_endian)
7593         expr1.X_add_number = 1;
7594       else
7595         expr1.X_add_number = 0;
7596       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7597                    (int) BFD_RELOC_LO16, AT);
7598       if (! target_big_endian)
7599         expr1.X_add_number = 0;
7600       else
7601         expr1.X_add_number = 1;
7602       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7603                    (int) BFD_RELOC_LO16, AT);
7604       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7605                    treg, treg, 8);
7606       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7607                    treg, treg, AT);
7608       break;
7609
7610     default:
7611       /* FIXME: Check if this is one of the itbl macros, since they
7612          are added dynamically.  */
7613       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7614       break;
7615     }
7616   if (mips_opts.noat)
7617     as_warn (_("Macro used $at after \".set noat\""));
7618 }
7619
7620 /* Implement macros in mips16 mode.  */
7621
7622 static void
7623 mips16_macro (ip)
7624      struct mips_cl_insn *ip;
7625 {
7626   int mask;
7627   int xreg, yreg, zreg, tmp;
7628   int icnt;
7629   expressionS expr1;
7630   int dbl;
7631   const char *s, *s2, *s3;
7632
7633   mask = ip->insn_mo->mask;
7634
7635   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7636   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7637   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7638
7639   icnt = 0;
7640
7641   expr1.X_op = O_constant;
7642   expr1.X_op_symbol = NULL;
7643   expr1.X_add_symbol = NULL;
7644   expr1.X_add_number = 1;
7645
7646   dbl = 0;
7647
7648   switch (mask)
7649     {
7650     default:
7651       internalError ();
7652
7653     case M_DDIV_3:
7654       dbl = 1;
7655     case M_DIV_3:
7656       s = "mflo";
7657       goto do_div3;
7658     case M_DREM_3:
7659       dbl = 1;
7660     case M_REM_3:
7661       s = "mfhi";
7662     do_div3:
7663       mips_emit_delays (true);
7664       ++mips_opts.noreorder;
7665       mips_any_noreorder = 1;
7666       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7667                    dbl ? "ddiv" : "div",
7668                    "0,x,y", xreg, yreg);
7669       expr1.X_add_number = 2;
7670       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7671       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7672                    7);
7673
7674       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7675          since that causes an overflow.  We should do that as well,
7676          but I don't see how to do the comparisons without a temporary
7677          register.  */
7678       --mips_opts.noreorder;
7679       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7680       break;
7681
7682     case M_DIVU_3:
7683       s = "divu";
7684       s2 = "mflo";
7685       goto do_divu3;
7686     case M_REMU_3:
7687       s = "divu";
7688       s2 = "mfhi";
7689       goto do_divu3;
7690     case M_DDIVU_3:
7691       s = "ddivu";
7692       s2 = "mflo";
7693       goto do_divu3;
7694     case M_DREMU_3:
7695       s = "ddivu";
7696       s2 = "mfhi";
7697     do_divu3:
7698       mips_emit_delays (true);
7699       ++mips_opts.noreorder;
7700       mips_any_noreorder = 1;
7701       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7702                    xreg, yreg);
7703       expr1.X_add_number = 2;
7704       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7706                    "6", 7);
7707       --mips_opts.noreorder;
7708       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7709       break;
7710
7711     case M_DMUL:
7712       dbl = 1;
7713     case M_MUL:
7714       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7715                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7716       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7717                    zreg);
7718       return;
7719
7720     case M_DSUBU_I:
7721       dbl = 1;
7722       goto do_subu;
7723     case M_SUBU_I:
7724     do_subu:
7725       if (imm_expr.X_op != O_constant)
7726         as_bad (_("Unsupported large constant"));
7727       imm_expr.X_add_number = -imm_expr.X_add_number;
7728       macro_build ((char *) NULL, &icnt, &imm_expr,
7729                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7730       break;
7731
7732     case M_SUBU_I_2:
7733       if (imm_expr.X_op != O_constant)
7734         as_bad (_("Unsupported large constant"));
7735       imm_expr.X_add_number = -imm_expr.X_add_number;
7736       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7737                    "x,k", xreg);
7738       break;
7739
7740     case M_DSUBU_I_2:
7741       if (imm_expr.X_op != O_constant)
7742         as_bad (_("Unsupported large constant"));
7743       imm_expr.X_add_number = -imm_expr.X_add_number;
7744       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7745                    "y,j", yreg);
7746       break;
7747
7748     case M_BEQ:
7749       s = "cmp";
7750       s2 = "bteqz";
7751       goto do_branch;
7752     case M_BNE:
7753       s = "cmp";
7754       s2 = "btnez";
7755       goto do_branch;
7756     case M_BLT:
7757       s = "slt";
7758       s2 = "btnez";
7759       goto do_branch;
7760     case M_BLTU:
7761       s = "sltu";
7762       s2 = "btnez";
7763       goto do_branch;
7764     case M_BLE:
7765       s = "slt";
7766       s2 = "bteqz";
7767       goto do_reverse_branch;
7768     case M_BLEU:
7769       s = "sltu";
7770       s2 = "bteqz";
7771       goto do_reverse_branch;
7772     case M_BGE:
7773       s = "slt";
7774       s2 = "bteqz";
7775       goto do_branch;
7776     case M_BGEU:
7777       s = "sltu";
7778       s2 = "bteqz";
7779       goto do_branch;
7780     case M_BGT:
7781       s = "slt";
7782       s2 = "btnez";
7783       goto do_reverse_branch;
7784     case M_BGTU:
7785       s = "sltu";
7786       s2 = "btnez";
7787
7788     do_reverse_branch:
7789       tmp = xreg;
7790       xreg = yreg;
7791       yreg = tmp;
7792
7793     do_branch:
7794       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7795                    xreg, yreg);
7796       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7797       break;
7798
7799     case M_BEQ_I:
7800       s = "cmpi";
7801       s2 = "bteqz";
7802       s3 = "x,U";
7803       goto do_branch_i;
7804     case M_BNE_I:
7805       s = "cmpi";
7806       s2 = "btnez";
7807       s3 = "x,U";
7808       goto do_branch_i;
7809     case M_BLT_I:
7810       s = "slti";
7811       s2 = "btnez";
7812       s3 = "x,8";
7813       goto do_branch_i;
7814     case M_BLTU_I:
7815       s = "sltiu";
7816       s2 = "btnez";
7817       s3 = "x,8";
7818       goto do_branch_i;
7819     case M_BLE_I:
7820       s = "slti";
7821       s2 = "btnez";
7822       s3 = "x,8";
7823       goto do_addone_branch_i;
7824     case M_BLEU_I:
7825       s = "sltiu";
7826       s2 = "btnez";
7827       s3 = "x,8";
7828       goto do_addone_branch_i;
7829     case M_BGE_I:
7830       s = "slti";
7831       s2 = "bteqz";
7832       s3 = "x,8";
7833       goto do_branch_i;
7834     case M_BGEU_I:
7835       s = "sltiu";
7836       s2 = "bteqz";
7837       s3 = "x,8";
7838       goto do_branch_i;
7839     case M_BGT_I:
7840       s = "slti";
7841       s2 = "bteqz";
7842       s3 = "x,8";
7843       goto do_addone_branch_i;
7844     case M_BGTU_I:
7845       s = "sltiu";
7846       s2 = "bteqz";
7847       s3 = "x,8";
7848
7849     do_addone_branch_i:
7850       if (imm_expr.X_op != O_constant)
7851         as_bad (_("Unsupported large constant"));
7852       ++imm_expr.X_add_number;
7853
7854     do_branch_i:
7855       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7856       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7857       break;
7858
7859     case M_ABS:
7860       expr1.X_add_number = 0;
7861       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7862       if (xreg != yreg)
7863         move_register (&icnt, xreg, yreg);
7864       expr1.X_add_number = 2;
7865       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7866       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7867                    "neg", "x,w", xreg, xreg);
7868     }
7869 }
7870
7871 /* For consistency checking, verify that all bits are specified either
7872    by the match/mask part of the instruction definition, or by the
7873    operand list.  */
7874 static int
7875 validate_mips_insn (opc)
7876      const struct mips_opcode *opc;
7877 {
7878   const char *p = opc->args;
7879   char c;
7880   unsigned long used_bits = opc->mask;
7881
7882   if ((used_bits & opc->match) != opc->match)
7883     {
7884       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7885               opc->name, opc->args);
7886       return 0;
7887     }
7888 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7889   while (*p)
7890     switch (c = *p++)
7891       {
7892       case ',': break;
7893       case '(': break;
7894       case ')': break;
7895       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7896       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7897       case 'A': break;
7898       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7899       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7900       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7901       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7902       case 'F': break;
7903       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7904       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7905       case 'I': break;
7906       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7907       case 'L': break;
7908       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7909       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7910       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7911       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7912                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7913       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7914       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7915       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7916       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7917       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7918       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7919       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7920       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7921       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7922       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7923       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7924       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7925       case 'f': break;
7926       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7927       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7928       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7929       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7930       case 'l': break;
7931       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7932       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7933       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7934       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7935       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7936       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7937       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7938       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7939       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7940       case 'x': break;
7941       case 'z': break;
7942       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7943       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7944                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7945       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
7946       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
7947       case '[': break;
7948       case ']': break;
7949       default:
7950         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7951                 c, opc->name, opc->args);
7952         return 0;
7953       }
7954 #undef USE_BITS
7955   if (used_bits != 0xffffffff)
7956     {
7957       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7958               ~used_bits & 0xffffffff, opc->name, opc->args);
7959       return 0;
7960     }
7961   return 1;
7962 }
7963
7964 /* This routine assembles an instruction into its binary format.  As a
7965    side effect, it sets one of the global variables imm_reloc or
7966    offset_reloc to the type of relocation to do if one of the operands
7967    is an address expression.  */
7968
7969 static void
7970 mips_ip (str, ip)
7971      char *str;
7972      struct mips_cl_insn *ip;
7973 {
7974   char *s;
7975   const char *args;
7976   char c = 0;
7977   struct mips_opcode *insn;
7978   char *argsStart;
7979   unsigned int regno;
7980   unsigned int lastregno = 0;
7981   char *s_reset;
7982   char save_c = 0;
7983
7984   insn_error = NULL;
7985
7986   /* If the instruction contains a '.', we first try to match an instruction
7987      including the '.'.  Then we try again without the '.'.  */
7988   insn = NULL;
7989   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7990     continue;
7991
7992   /* If we stopped on whitespace, then replace the whitespace with null for
7993      the call to hash_find.  Save the character we replaced just in case we
7994      have to re-parse the instruction.  */
7995   if (ISSPACE (*s))
7996     {
7997       save_c = *s;
7998       *s++ = '\0';
7999     }
8000
8001   insn = (struct mips_opcode *) hash_find (op_hash, str);
8002
8003   /* If we didn't find the instruction in the opcode table, try again, but
8004      this time with just the instruction up to, but not including the
8005      first '.'.  */
8006   if (insn == NULL)
8007     {
8008       /* Restore the character we overwrite above (if any).  */
8009       if (save_c)
8010         *(--s) = save_c;
8011
8012       /* Scan up to the first '.' or whitespace.  */
8013       for (s = str;
8014            *s != '\0' && *s != '.' && !ISSPACE (*s);
8015            ++s)
8016         continue;
8017
8018       /* If we did not find a '.', then we can quit now.  */
8019       if (*s != '.')
8020         {
8021           insn_error = "unrecognized opcode";
8022           return;
8023         }
8024
8025       /* Lookup the instruction in the hash table.  */
8026       *s++ = '\0';
8027       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8028         {
8029           insn_error = "unrecognized opcode";
8030           return;
8031         }
8032     }
8033
8034   argsStart = s;
8035   for (;;)
8036     {
8037       boolean ok;
8038
8039       assert (strcmp (insn->name, str) == 0);
8040
8041       if (OPCODE_IS_MEMBER (insn,
8042                             (mips_opts.isa
8043                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8044                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8045                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8046                             mips_arch))
8047         ok = true;
8048       else
8049         ok = false;
8050
8051       if (insn->pinfo != INSN_MACRO)
8052         {
8053           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8054             ok = false;
8055         }
8056
8057       if (! ok)
8058         {
8059           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8060               && strcmp (insn->name, insn[1].name) == 0)
8061             {
8062               ++insn;
8063               continue;
8064             }
8065           else
8066             {
8067               if (!insn_error)
8068                 {
8069                   static char buf[100];
8070                   if (mips_arch_info->is_isa)
8071                     sprintf (buf,
8072                              _("opcode not supported at this ISA level (%s)"),
8073                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8074                   else
8075                     sprintf (buf,
8076                              _("opcode not supported on this processor: %s (%s)"),
8077                              mips_arch_info->name,
8078                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8079                   insn_error = buf;
8080                 }
8081               if (save_c)
8082                 *(--s) = save_c;
8083               return;
8084             }
8085         }
8086
8087       ip->insn_mo = insn;
8088       ip->insn_opcode = insn->match;
8089       insn_error = NULL;
8090       for (args = insn->args;; ++args)
8091         {
8092           int is_mdmx;
8093
8094           s += strspn (s, " \t");
8095           is_mdmx = 0;
8096           switch (*args)
8097             {
8098             case '\0':          /* end of args */
8099               if (*s == '\0')
8100                 return;
8101               break;
8102
8103             case ',':
8104               if (*s++ == *args)
8105                 continue;
8106               s--;
8107               switch (*++args)
8108                 {
8109                 case 'r':
8110                 case 'v':
8111                   ip->insn_opcode |= lastregno << OP_SH_RS;
8112                   continue;
8113
8114                 case 'w':
8115                   ip->insn_opcode |= lastregno << OP_SH_RT;
8116                   continue;
8117
8118                 case 'W':
8119                   ip->insn_opcode |= lastregno << OP_SH_FT;
8120                   continue;
8121
8122                 case 'V':
8123                   ip->insn_opcode |= lastregno << OP_SH_FS;
8124                   continue;
8125                 }
8126               break;
8127
8128             case '(':
8129               /* Handle optional base register.
8130                  Either the base register is omitted or
8131                  we must have a left paren.  */
8132               /* This is dependent on the next operand specifier
8133                  is a base register specification.  */
8134               assert (args[1] == 'b' || args[1] == '5'
8135                       || args[1] == '-' || args[1] == '4');
8136               if (*s == '\0')
8137                 return;
8138
8139             case ')':           /* these must match exactly */
8140             case '[':
8141             case ']':
8142               if (*s++ == *args)
8143                 continue;
8144               break;
8145
8146             case '<':           /* must be at least one digit */
8147               /*
8148                * According to the manual, if the shift amount is greater
8149                * than 31 or less than 0, then the shift amount should be
8150                * mod 32.  In reality the mips assembler issues an error.
8151                * We issue a warning and mask out all but the low 5 bits.
8152                */
8153               my_getExpression (&imm_expr, s);
8154               check_absolute_expr (ip, &imm_expr);
8155               if ((unsigned long) imm_expr.X_add_number > 31)
8156                 {
8157                   as_warn (_("Improper shift amount (%lu)"),
8158                            (unsigned long) imm_expr.X_add_number);
8159                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8160                 }
8161               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8162               imm_expr.X_op = O_absent;
8163               s = expr_end;
8164               continue;
8165
8166             case '>':           /* shift amount minus 32 */
8167               my_getExpression (&imm_expr, s);
8168               check_absolute_expr (ip, &imm_expr);
8169               if ((unsigned long) imm_expr.X_add_number < 32
8170                   || (unsigned long) imm_expr.X_add_number > 63)
8171                 break;
8172               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8173               imm_expr.X_op = O_absent;
8174               s = expr_end;
8175               continue;
8176
8177             case 'k':           /* cache code */
8178             case 'h':           /* prefx code */
8179               my_getExpression (&imm_expr, s);
8180               check_absolute_expr (ip, &imm_expr);
8181               if ((unsigned long) imm_expr.X_add_number > 31)
8182                 {
8183                   as_warn (_("Invalid value for `%s' (%lu)"),
8184                            ip->insn_mo->name,
8185                            (unsigned long) imm_expr.X_add_number);
8186                   imm_expr.X_add_number &= 0x1f;
8187                 }
8188               if (*args == 'k')
8189                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8190               else
8191                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8192               imm_expr.X_op = O_absent;
8193               s = expr_end;
8194               continue;
8195
8196             case 'c':           /* break code */
8197               my_getExpression (&imm_expr, s);
8198               check_absolute_expr (ip, &imm_expr);
8199               if ((unsigned long) imm_expr.X_add_number > 1023)
8200                 {
8201                   as_warn (_("Illegal break code (%lu)"),
8202                            (unsigned long) imm_expr.X_add_number);
8203                   imm_expr.X_add_number &= OP_MASK_CODE;
8204                 }
8205               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8206               imm_expr.X_op = O_absent;
8207               s = expr_end;
8208               continue;
8209
8210             case 'q':           /* lower break code */
8211               my_getExpression (&imm_expr, s);
8212               check_absolute_expr (ip, &imm_expr);
8213               if ((unsigned long) imm_expr.X_add_number > 1023)
8214                 {
8215                   as_warn (_("Illegal lower break code (%lu)"),
8216                            (unsigned long) imm_expr.X_add_number);
8217                   imm_expr.X_add_number &= OP_MASK_CODE2;
8218                 }
8219               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8220               imm_expr.X_op = O_absent;
8221               s = expr_end;
8222               continue;
8223
8224             case 'B':           /* 20-bit syscall/break code.  */
8225               my_getExpression (&imm_expr, s);
8226               check_absolute_expr (ip, &imm_expr);
8227               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8228                 as_warn (_("Illegal 20-bit code (%lu)"),
8229                          (unsigned long) imm_expr.X_add_number);
8230               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8231               imm_expr.X_op = O_absent;
8232               s = expr_end;
8233               continue;
8234
8235             case 'C':           /* Coprocessor code */
8236               my_getExpression (&imm_expr, s);
8237               check_absolute_expr (ip, &imm_expr);
8238               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8239                 {
8240                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8241                            (unsigned long) imm_expr.X_add_number);
8242                   imm_expr.X_add_number &= ((1 << 25) - 1);
8243                 }
8244               ip->insn_opcode |= imm_expr.X_add_number;
8245               imm_expr.X_op = O_absent;
8246               s = expr_end;
8247               continue;
8248
8249             case 'J':           /* 19-bit wait code.  */
8250               my_getExpression (&imm_expr, s);
8251               check_absolute_expr (ip, &imm_expr);
8252               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8253                 as_warn (_("Illegal 19-bit code (%lu)"),
8254                          (unsigned long) imm_expr.X_add_number);
8255               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8256               imm_expr.X_op = O_absent;
8257               s = expr_end;
8258               continue;
8259
8260             case 'P':           /* Performance register */
8261               my_getExpression (&imm_expr, s);
8262               check_absolute_expr (ip, &imm_expr);
8263               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8264                 {
8265                   as_warn (_("Invalid performance register (%lu)"),
8266                            (unsigned long) imm_expr.X_add_number);
8267                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8268                 }
8269               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8270               imm_expr.X_op = O_absent;
8271               s = expr_end;
8272               continue;
8273
8274             case 'b':           /* base register */
8275             case 'd':           /* destination register */
8276             case 's':           /* source register */
8277             case 't':           /* target register */
8278             case 'r':           /* both target and source */
8279             case 'v':           /* both dest and source */
8280             case 'w':           /* both dest and target */
8281             case 'E':           /* coprocessor target register */
8282             case 'G':           /* coprocessor destination register */
8283             case 'x':           /* ignore register name */
8284             case 'z':           /* must be zero register */
8285             case 'U':           /* destination register (clo/clz).  */
8286               s_reset = s;
8287               if (s[0] == '$')
8288                 {
8289
8290                   if (ISDIGIT (s[1]))
8291                     {
8292                       ++s;
8293                       regno = 0;
8294                       do
8295                         {
8296                           regno *= 10;
8297                           regno += *s - '0';
8298                           ++s;
8299                         }
8300                       while (ISDIGIT (*s));
8301                       if (regno > 31)
8302                         as_bad (_("Invalid register number (%d)"), regno);
8303                     }
8304                   else if (*args == 'E' || *args == 'G')
8305                     goto notreg;
8306                   else
8307                     {
8308                       if (s[1] == 'r' && s[2] == 'a')
8309                         {
8310                           s += 3;
8311                           regno = RA;
8312                         }
8313                       else if (s[1] == 'f' && s[2] == 'p')
8314                         {
8315                           s += 3;
8316                           regno = FP;
8317                         }
8318                       else if (s[1] == 's' && s[2] == 'p')
8319                         {
8320                           s += 3;
8321                           regno = SP;
8322                         }
8323                       else if (s[1] == 'g' && s[2] == 'p')
8324                         {
8325                           s += 3;
8326                           regno = GP;
8327                         }
8328                       else if (s[1] == 'a' && s[2] == 't')
8329                         {
8330                           s += 3;
8331                           regno = AT;
8332                         }
8333                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8334                         {
8335                           s += 4;
8336                           regno = KT0;
8337                         }
8338                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8339                         {
8340                           s += 4;
8341                           regno = KT1;
8342                         }
8343                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8344                         {
8345                           s += 5;
8346                           regno = ZERO;
8347                         }
8348                       else if (itbl_have_entries)
8349                         {
8350                           char *p, *n;
8351                           unsigned long r;
8352
8353                           p = s + 1;    /* advance past '$' */
8354                           n = itbl_get_field (&p);  /* n is name */
8355
8356                           /* See if this is a register defined in an
8357                              itbl entry.  */
8358                           if (itbl_get_reg_val (n, &r))
8359                             {
8360                               /* Get_field advances to the start of
8361                                  the next field, so we need to back
8362                                  rack to the end of the last field.  */
8363                               if (p)
8364                                 s = p - 1;
8365                               else
8366                                 s = strchr (s, '\0');
8367                               regno = r;
8368                             }
8369                           else
8370                             goto notreg;
8371                         }
8372                       else
8373                         goto notreg;
8374                     }
8375                   if (regno == AT
8376                       && ! mips_opts.noat
8377                       && *args != 'E'
8378                       && *args != 'G')
8379                     as_warn (_("Used $at without \".set noat\""));
8380                   c = *args;
8381                   if (*s == ' ')
8382                     ++s;
8383                   if (args[1] != *s)
8384                     {
8385                       if (c == 'r' || c == 'v' || c == 'w')
8386                         {
8387                           regno = lastregno;
8388                           s = s_reset;
8389                           ++args;
8390                         }
8391                     }
8392                   /* 'z' only matches $0.  */
8393                   if (c == 'z' && regno != 0)
8394                     break;
8395
8396         /* Now that we have assembled one operand, we use the args string
8397          * to figure out where it goes in the instruction.  */
8398                   switch (c)
8399                     {
8400                     case 'r':
8401                     case 's':
8402                     case 'v':
8403                     case 'b':
8404                       ip->insn_opcode |= regno << OP_SH_RS;
8405                       break;
8406                     case 'd':
8407                     case 'G':
8408                       ip->insn_opcode |= regno << OP_SH_RD;
8409                       break;
8410                     case 'U':
8411                       ip->insn_opcode |= regno << OP_SH_RD;
8412                       ip->insn_opcode |= regno << OP_SH_RT;
8413                       break;
8414                     case 'w':
8415                     case 't':
8416                     case 'E':
8417                       ip->insn_opcode |= regno << OP_SH_RT;
8418                       break;
8419                     case 'x':
8420                       /* This case exists because on the r3000 trunc
8421                          expands into a macro which requires a gp
8422                          register.  On the r6000 or r4000 it is
8423                          assembled into a single instruction which
8424                          ignores the register.  Thus the insn version
8425                          is MIPS_ISA2 and uses 'x', and the macro
8426                          version is MIPS_ISA1 and uses 't'.  */
8427                       break;
8428                     case 'z':
8429                       /* This case is for the div instruction, which
8430                          acts differently if the destination argument
8431                          is $0.  This only matches $0, and is checked
8432                          outside the switch.  */
8433                       break;
8434                     case 'D':
8435                       /* Itbl operand; not yet implemented. FIXME ?? */
8436                       break;
8437                       /* What about all other operands like 'i', which
8438                          can be specified in the opcode table? */
8439                     }
8440                   lastregno = regno;
8441                   continue;
8442                 }
8443             notreg:
8444               switch (*args++)
8445                 {
8446                 case 'r':
8447                 case 'v':
8448                   ip->insn_opcode |= lastregno << OP_SH_RS;
8449                   continue;
8450                 case 'w':
8451                   ip->insn_opcode |= lastregno << OP_SH_RT;
8452                   continue;
8453                 }
8454               break;
8455
8456             case 'O':           /* MDMX alignment immediate constant.  */
8457               my_getExpression (&imm_expr, s);
8458               check_absolute_expr (ip, &imm_expr);
8459               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8460                 {
8461                   as_warn ("Improper align amount (%ld), using low bits",
8462                            (long) imm_expr.X_add_number);
8463                   imm_expr.X_add_number &= OP_MASK_ALN;
8464                 }
8465               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8466               imm_expr.X_op = O_absent;
8467               s = expr_end;
8468               continue;
8469
8470             case 'Q':           /* MDMX vector, element sel, or const.  */
8471               if (s[0] != '$')
8472                 {
8473                   /* MDMX Immediate.  */
8474                   my_getExpression (&imm_expr, s);
8475                   check_absolute_expr (ip, &imm_expr);
8476                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8477                     {
8478                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8479                                (long) imm_expr.X_add_number);
8480                       imm_expr.X_add_number &= OP_MASK_FT;
8481                     }
8482                   imm_expr.X_add_number &= OP_MASK_FT;
8483                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8484                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8485                   else
8486                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8487                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8488                   imm_expr.X_op = O_absent;
8489                   s = expr_end;
8490                   continue;
8491                 }
8492               /* Not MDMX Immediate.  Fall through.  */
8493             case 'X':           /* MDMX destination register.  */
8494             case 'Y':           /* MDMX source register.  */
8495             case 'Z':           /* MDMX target register.  */
8496               is_mdmx = 1;
8497             case 'D':           /* floating point destination register */
8498             case 'S':           /* floating point source register */
8499             case 'T':           /* floating point target register */
8500             case 'R':           /* floating point source register */
8501             case 'V':
8502             case 'W':
8503               s_reset = s;
8504               /* Accept $fN for FP and MDMX register numbers, and in
8505                  addition accept $vN for MDMX register numbers.  */
8506               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8507                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8508                       && ISDIGIT (s[2])))
8509                 {
8510                   s += 2;
8511                   regno = 0;
8512                   do
8513                     {
8514                       regno *= 10;
8515                       regno += *s - '0';
8516                       ++s;
8517                     }
8518                   while (ISDIGIT (*s));
8519
8520                   if (regno > 31)
8521                     as_bad (_("Invalid float register number (%d)"), regno);
8522
8523                   if ((regno & 1) != 0
8524                       && HAVE_32BIT_FPRS
8525                       && ! (strcmp (str, "mtc1") == 0
8526                             || strcmp (str, "mfc1") == 0
8527                             || strcmp (str, "lwc1") == 0
8528                             || strcmp (str, "swc1") == 0
8529                             || strcmp (str, "l.s") == 0
8530                             || strcmp (str, "s.s") == 0))
8531                     as_warn (_("Float register should be even, was %d"),
8532                              regno);
8533
8534                   c = *args;
8535                   if (*s == ' ')
8536                     ++s;
8537                   if (args[1] != *s)
8538                     {
8539                       if (c == 'V' || c == 'W')
8540                         {
8541                           regno = lastregno;
8542                           s = s_reset;
8543                           ++args;
8544                         }
8545                     }
8546                   switch (c)
8547                     {
8548                     case 'D':
8549                     case 'X':
8550                       ip->insn_opcode |= regno << OP_SH_FD;
8551                       break;
8552                     case 'V':
8553                     case 'S':
8554                     case 'Y':
8555                       ip->insn_opcode |= regno << OP_SH_FS;
8556                       break;
8557                     case 'Q':
8558                       /* This is like 'Z', but also needs to fix the MDMX
8559                          vector/scalar select bits.  Note that the
8560                          scalar immediate case is handled above.  */
8561                       if (*s == '[')
8562                         {
8563                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8564                           int max_el = (is_qh ? 3 : 7);
8565                           s++;
8566                           my_getExpression(&imm_expr, s);
8567                           check_absolute_expr (ip, &imm_expr);
8568                           s = expr_end;
8569                           if (imm_expr.X_add_number > max_el)
8570                             as_bad(_("Bad element selector %ld"),
8571                                    (long) imm_expr.X_add_number);
8572                           imm_expr.X_add_number &= max_el;
8573                           ip->insn_opcode |= (imm_expr.X_add_number
8574                                               << (OP_SH_VSEL +
8575                                                   (is_qh ? 2 : 1)));
8576                           if (*s != ']')
8577                             as_warn(_("Expecting ']' found '%s'"), s);
8578                           else
8579                             s++;
8580                         }
8581                       else
8582                         {
8583                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8584                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8585                                                 << OP_SH_VSEL);
8586                           else
8587                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8588                                                 OP_SH_VSEL);
8589                         }
8590                       /* Fall through */
8591                     case 'W':
8592                     case 'T':
8593                     case 'Z':
8594                       ip->insn_opcode |= regno << OP_SH_FT;
8595                       break;
8596                     case 'R':
8597                       ip->insn_opcode |= regno << OP_SH_FR;
8598                       break;
8599                     }
8600                   lastregno = regno;
8601                   continue;
8602                 }
8603
8604               switch (*args++)
8605                 {
8606                 case 'V':
8607                   ip->insn_opcode |= lastregno << OP_SH_FS;
8608                   continue;
8609                 case 'W':
8610                   ip->insn_opcode |= lastregno << OP_SH_FT;
8611                   continue;
8612                 }
8613               break;
8614
8615             case 'I':
8616               my_getExpression (&imm_expr, s);
8617               if (imm_expr.X_op != O_big
8618                   && imm_expr.X_op != O_constant)
8619                 insn_error = _("absolute expression required");
8620               s = expr_end;
8621               continue;
8622
8623             case 'A':
8624               my_getExpression (&offset_expr, s);
8625               *imm_reloc = BFD_RELOC_32;
8626               s = expr_end;
8627               continue;
8628
8629             case 'F':
8630             case 'L':
8631             case 'f':
8632             case 'l':
8633               {
8634                 int f64;
8635                 int using_gprs;
8636                 char *save_in;
8637                 char *err;
8638                 unsigned char temp[8];
8639                 int len;
8640                 unsigned int length;
8641                 segT seg;
8642                 subsegT subseg;
8643                 char *p;
8644
8645                 /* These only appear as the last operand in an
8646                    instruction, and every instruction that accepts
8647                    them in any variant accepts them in all variants.
8648                    This means we don't have to worry about backing out
8649                    any changes if the instruction does not match.
8650
8651                    The difference between them is the size of the
8652                    floating point constant and where it goes.  For 'F'
8653                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8654                    is 32 bits.  Where the constant is placed is based
8655                    on how the MIPS assembler does things:
8656                     F -- .rdata
8657                     L -- .lit8
8658                     f -- immediate value
8659                     l -- .lit4
8660
8661                     The .lit4 and .lit8 sections are only used if
8662                     permitted by the -G argument.
8663
8664                     When generating embedded PIC code, we use the
8665                     .lit8 section but not the .lit4 section (we can do
8666                     .lit4 inline easily; we need to put .lit8
8667                     somewhere in the data segment, and using .lit8
8668                     permits the linker to eventually combine identical
8669                     .lit8 entries).
8670
8671                     The code below needs to know whether the target register
8672                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8673                     'F' are used with GPR-based instructions and 'l' and
8674                     'L' are used with FPR-based instructions.  */
8675
8676                 f64 = *args == 'F' || *args == 'L';
8677                 using_gprs = *args == 'F' || *args == 'f';
8678
8679                 save_in = input_line_pointer;
8680                 input_line_pointer = s;
8681                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8682                 length = len;
8683                 s = input_line_pointer;
8684                 input_line_pointer = save_in;
8685                 if (err != NULL && *err != '\0')
8686                   {
8687                     as_bad (_("Bad floating point constant: %s"), err);
8688                     memset (temp, '\0', sizeof temp);
8689                     length = f64 ? 8 : 4;
8690                   }
8691
8692                 assert (length == (unsigned) (f64 ? 8 : 4));
8693
8694                 if (*args == 'f'
8695                     || (*args == 'l'
8696                         && (! USE_GLOBAL_POINTER_OPT
8697                             || mips_pic == EMBEDDED_PIC
8698                             || g_switch_value < 4
8699                             || (temp[0] == 0 && temp[1] == 0)
8700                             || (temp[2] == 0 && temp[3] == 0))))
8701                   {
8702                     imm_expr.X_op = O_constant;
8703                     if (! target_big_endian)
8704                       imm_expr.X_add_number = bfd_getl32 (temp);
8705                     else
8706                       imm_expr.X_add_number = bfd_getb32 (temp);
8707                   }
8708                 else if (length > 4
8709                          && ! mips_disable_float_construction
8710                          /* Constants can only be constructed in GPRs and
8711                             copied to FPRs if the GPRs are at least as wide
8712                             as the FPRs.  Force the constant into memory if
8713                             we are using 64-bit FPRs but the GPRs are only
8714                             32 bits wide.  */
8715                          && (using_gprs
8716                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8717                          && ((temp[0] == 0 && temp[1] == 0)
8718                              || (temp[2] == 0 && temp[3] == 0))
8719                          && ((temp[4] == 0 && temp[5] == 0)
8720                              || (temp[6] == 0 && temp[7] == 0)))
8721                   {
8722                     /* The value is simple enough to load with a couple of
8723                        instructions.  If using 32-bit registers, set
8724                        imm_expr to the high order 32 bits and offset_expr to
8725                        the low order 32 bits.  Otherwise, set imm_expr to
8726                        the entire 64 bit constant.  */
8727                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8728                       {
8729                         imm_expr.X_op = O_constant;
8730                         offset_expr.X_op = O_constant;
8731                         if (! target_big_endian)
8732                           {
8733                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8734                             offset_expr.X_add_number = bfd_getl32 (temp);
8735                           }
8736                         else
8737                           {
8738                             imm_expr.X_add_number = bfd_getb32 (temp);
8739                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8740                           }
8741                         if (offset_expr.X_add_number == 0)
8742                           offset_expr.X_op = O_absent;
8743                       }
8744                     else if (sizeof (imm_expr.X_add_number) > 4)
8745                       {
8746                         imm_expr.X_op = O_constant;
8747                         if (! target_big_endian)
8748                           imm_expr.X_add_number = bfd_getl64 (temp);
8749                         else
8750                           imm_expr.X_add_number = bfd_getb64 (temp);
8751                       }
8752                     else
8753                       {
8754                         imm_expr.X_op = O_big;
8755                         imm_expr.X_add_number = 4;
8756                         if (! target_big_endian)
8757                           {
8758                             generic_bignum[0] = bfd_getl16 (temp);
8759                             generic_bignum[1] = bfd_getl16 (temp + 2);
8760                             generic_bignum[2] = bfd_getl16 (temp + 4);
8761                             generic_bignum[3] = bfd_getl16 (temp + 6);
8762                           }
8763                         else
8764                           {
8765                             generic_bignum[0] = bfd_getb16 (temp + 6);
8766                             generic_bignum[1] = bfd_getb16 (temp + 4);
8767                             generic_bignum[2] = bfd_getb16 (temp + 2);
8768                             generic_bignum[3] = bfd_getb16 (temp);
8769                           }
8770                       }
8771                   }
8772                 else
8773                   {
8774                     const char *newname;
8775                     segT new_seg;
8776
8777                     /* Switch to the right section.  */
8778                     seg = now_seg;
8779                     subseg = now_subseg;
8780                     switch (*args)
8781                       {
8782                       default: /* unused default case avoids warnings.  */
8783                       case 'L':
8784                         newname = RDATA_SECTION_NAME;
8785                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8786                             || mips_pic == EMBEDDED_PIC)
8787                           newname = ".lit8";
8788                         break;
8789                       case 'F':
8790                         if (mips_pic == EMBEDDED_PIC)
8791                           newname = ".lit8";
8792                         else
8793                           newname = RDATA_SECTION_NAME;
8794                         break;
8795                       case 'l':
8796                         assert (!USE_GLOBAL_POINTER_OPT
8797                                 || g_switch_value >= 4);
8798                         newname = ".lit4";
8799                         break;
8800                       }
8801                     new_seg = subseg_new (newname, (subsegT) 0);
8802                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8803                       bfd_set_section_flags (stdoutput, new_seg,
8804                                              (SEC_ALLOC
8805                                               | SEC_LOAD
8806                                               | SEC_READONLY
8807                                               | SEC_DATA));
8808                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8809                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8810                         && strcmp (TARGET_OS, "elf") != 0)
8811                       record_alignment (new_seg, 4);
8812                     else
8813                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8814                     if (seg == now_seg)
8815                       as_bad (_("Can't use floating point insn in this section"));
8816
8817                     /* Set the argument to the current address in the
8818                        section.  */
8819                     offset_expr.X_op = O_symbol;
8820                     offset_expr.X_add_symbol =
8821                       symbol_new ("L0\001", now_seg,
8822                                   (valueT) frag_now_fix (), frag_now);
8823                     offset_expr.X_add_number = 0;
8824
8825                     /* Put the floating point number into the section.  */
8826                     p = frag_more ((int) length);
8827                     memcpy (p, temp, length);
8828
8829                     /* Switch back to the original section.  */
8830                     subseg_set (seg, subseg);
8831                   }
8832               }
8833               continue;
8834
8835             case 'i':           /* 16 bit unsigned immediate */
8836             case 'j':           /* 16 bit signed immediate */
8837               *imm_reloc = BFD_RELOC_LO16;
8838               c = my_getSmallExpression (&imm_expr, s);
8839               if (c != S_EX_NONE)
8840                 {
8841                   if (c != S_EX_LO)
8842                     {
8843                       if (c == S_EX_HI)
8844                         {
8845                           *imm_reloc = BFD_RELOC_HI16_S;
8846                           imm_unmatched_hi = true;
8847                         }
8848 #ifdef OBJ_ELF
8849                       else if (c == S_EX_HIGHEST)
8850                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8851                       else if (c == S_EX_HIGHER)
8852                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8853                       else if (c == S_EX_GP_REL)
8854                         {
8855                           /* This occurs in NewABI only.  */
8856                           c = my_getSmallExpression (&imm_expr, s);
8857                           if (c != S_EX_NEG)
8858                             as_bad (_("bad composition of relocations"));
8859                           else
8860                             {
8861                               c = my_getSmallExpression (&imm_expr, s);
8862                               if (c != S_EX_LO)
8863                                 as_bad (_("bad composition of relocations"));
8864                               else
8865                                 {
8866                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8867                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8868                                   imm_reloc[2] = BFD_RELOC_LO16;
8869                                 }
8870                             }
8871                         }
8872 #endif
8873                       else
8874                         *imm_reloc = BFD_RELOC_HI16;
8875                     }
8876                   else if (imm_expr.X_op == O_constant)
8877                     imm_expr.X_add_number &= 0xffff;
8878                 }
8879               if (*args == 'i')
8880                 {
8881                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8882                       || ((imm_expr.X_add_number < 0
8883                            || imm_expr.X_add_number >= 0x10000)
8884                           && imm_expr.X_op == O_constant))
8885                     {
8886                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8887                           !strcmp (insn->name, insn[1].name))
8888                         break;
8889                       if (imm_expr.X_op == O_constant
8890                           || imm_expr.X_op == O_big)
8891                         as_bad (_("16 bit expression not in range 0..65535"));
8892                     }
8893                 }
8894               else
8895                 {
8896                   int more;
8897                   offsetT max;
8898
8899                   /* The upper bound should be 0x8000, but
8900                      unfortunately the MIPS assembler accepts numbers
8901                      from 0x8000 to 0xffff and sign extends them, and
8902                      we want to be compatible.  We only permit this
8903                      extended range for an instruction which does not
8904                      provide any further alternates, since those
8905                      alternates may handle other cases.  People should
8906                      use the numbers they mean, rather than relying on
8907                      a mysterious sign extension.  */
8908                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8909                           strcmp (insn->name, insn[1].name) == 0);
8910                   if (more)
8911                     max = 0x8000;
8912                   else
8913                     max = 0x10000;
8914                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8915                       || ((imm_expr.X_add_number < -0x8000
8916                            || imm_expr.X_add_number >= max)
8917                           && imm_expr.X_op == O_constant)
8918                       || (more
8919                           && imm_expr.X_add_number < 0
8920                           && HAVE_64BIT_GPRS
8921                           && imm_expr.X_unsigned
8922                           && sizeof (imm_expr.X_add_number) <= 4))
8923                     {
8924                       if (more)
8925                         break;
8926                       if (imm_expr.X_op == O_constant
8927                           || imm_expr.X_op == O_big)
8928                         as_bad (_("16 bit expression not in range -32768..32767"));
8929                     }
8930                 }
8931               s = expr_end;
8932               continue;
8933
8934             case 'o':           /* 16 bit offset */
8935               c = my_getSmallExpression (&offset_expr, s);
8936
8937               /* If this value won't fit into a 16 bit offset, then go
8938                  find a macro that will generate the 32 bit offset
8939                  code pattern.  */
8940               if (c == S_EX_NONE
8941                   && (offset_expr.X_op != O_constant
8942                       || offset_expr.X_add_number >= 0x8000
8943                       || offset_expr.X_add_number < -0x8000))
8944                 break;
8945
8946               if (c == S_EX_HI)
8947                 {
8948                   if (offset_expr.X_op != O_constant)
8949                     break;
8950                   offset_expr.X_add_number =
8951                     (offset_expr.X_add_number >> 16) & 0xffff;
8952                 }
8953               *offset_reloc = BFD_RELOC_LO16;
8954               s = expr_end;
8955               continue;
8956
8957             case 'p':           /* pc relative offset */
8958               if (mips_pic == EMBEDDED_PIC)
8959                 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8960               else
8961                 *offset_reloc = BFD_RELOC_16_PCREL;
8962               my_getExpression (&offset_expr, s);
8963               s = expr_end;
8964               continue;
8965
8966             case 'u':           /* upper 16 bits */
8967               c = my_getSmallExpression (&imm_expr, s);
8968               *imm_reloc = BFD_RELOC_LO16;
8969               if (c != S_EX_NONE)
8970                 {
8971                   if (c != S_EX_LO)
8972                     {
8973                       if (c == S_EX_HI)
8974                         {
8975                           *imm_reloc = BFD_RELOC_HI16_S;
8976                           imm_unmatched_hi = true;
8977                         }
8978 #ifdef OBJ_ELF
8979                       else if (c == S_EX_HIGHEST)
8980                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8981                       else if (c == S_EX_GP_REL)
8982                         {
8983                           /* This occurs in NewABI only.  */
8984                           c = my_getSmallExpression (&imm_expr, s);
8985                           if (c != S_EX_NEG)
8986                             as_bad (_("bad composition of relocations"));
8987                           else
8988                             {
8989                               c = my_getSmallExpression (&imm_expr, s);
8990                               if (c != S_EX_HI)
8991                                 as_bad (_("bad composition of relocations"));
8992                               else
8993                                 {
8994                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8995                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8996                                   imm_reloc[2] = BFD_RELOC_HI16_S;
8997                                 }
8998                             }
8999                         }
9000 #endif
9001                       else
9002                         *imm_reloc = BFD_RELOC_HI16;
9003                     }
9004                   else if (imm_expr.X_op == O_constant)
9005                     imm_expr.X_add_number &= 0xffff;
9006                 }
9007               else if (imm_expr.X_op == O_constant
9008                        && (imm_expr.X_add_number < 0
9009                            || imm_expr.X_add_number >= 0x10000))
9010                 as_bad (_("lui expression not in range 0..65535"));
9011               s = expr_end;
9012               continue;
9013
9014             case 'a':           /* 26 bit address */
9015               my_getExpression (&offset_expr, s);
9016               s = expr_end;
9017               *offset_reloc = BFD_RELOC_MIPS_JMP;
9018               continue;
9019
9020             case 'N':           /* 3 bit branch condition code */
9021             case 'M':           /* 3 bit compare condition code */
9022               if (strncmp (s, "$fcc", 4) != 0)
9023                 break;
9024               s += 4;
9025               regno = 0;
9026               do
9027                 {
9028                   regno *= 10;
9029                   regno += *s - '0';
9030                   ++s;
9031                 }
9032               while (ISDIGIT (*s));
9033               if (regno > 7)
9034                 as_bad (_("invalid condition code register $fcc%d"), regno);
9035               if (*args == 'N')
9036                 ip->insn_opcode |= regno << OP_SH_BCC;
9037               else
9038                 ip->insn_opcode |= regno << OP_SH_CCC;
9039               continue;
9040
9041             case 'H':
9042               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9043                 s += 2;
9044               if (ISDIGIT (*s))
9045                 {
9046                   c = 0;
9047                   do
9048                     {
9049                       c *= 10;
9050                       c += *s - '0';
9051                       ++s;
9052                     }
9053                   while (ISDIGIT (*s));
9054                 }
9055               else
9056                 c = 8; /* Invalid sel value.  */
9057
9058               if (c > 7)
9059                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9060               ip->insn_opcode |= c;
9061               continue;
9062
9063             case 'e':
9064               /* Must be at least one digit.  */
9065               my_getExpression (&imm_expr, s);
9066               check_absolute_expr (ip, &imm_expr);
9067
9068               if ((unsigned long) imm_expr.X_add_number
9069                   > (unsigned long) OP_MASK_VECBYTE)
9070                 {
9071                   as_bad (_("bad byte vector index (%ld)"),
9072                            (long) imm_expr.X_add_number);
9073                   imm_expr.X_add_number = 0;
9074                 }
9075
9076               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9077               imm_expr.X_op = O_absent;
9078               s = expr_end;
9079               continue;
9080
9081             case '%':
9082               my_getExpression (&imm_expr, s);
9083               check_absolute_expr (ip, &imm_expr);
9084
9085               if ((unsigned long) imm_expr.X_add_number
9086                   > (unsigned long) OP_MASK_VECALIGN)
9087                 {
9088                   as_bad (_("bad byte vector index (%ld)"),
9089                            (long) imm_expr.X_add_number);
9090                   imm_expr.X_add_number = 0;
9091                 }
9092
9093               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9094               imm_expr.X_op = O_absent;
9095               s = expr_end;
9096               continue;
9097
9098             default:
9099               as_bad (_("bad char = '%c'\n"), *args);
9100               internalError ();
9101             }
9102           break;
9103         }
9104       /* Args don't match.  */
9105       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9106           !strcmp (insn->name, insn[1].name))
9107         {
9108           ++insn;
9109           s = argsStart;
9110           insn_error = _("illegal operands");
9111           continue;
9112         }
9113       if (save_c)
9114         *(--s) = save_c;
9115       insn_error = _("illegal operands");
9116       return;
9117     }
9118 }
9119
9120 /* This routine assembles an instruction into its binary format when
9121    assembling for the mips16.  As a side effect, it sets one of the
9122    global variables imm_reloc or offset_reloc to the type of
9123    relocation to do if one of the operands is an address expression.
9124    It also sets mips16_small and mips16_ext if the user explicitly
9125    requested a small or extended instruction.  */
9126
9127 static void
9128 mips16_ip (str, ip)
9129      char *str;
9130      struct mips_cl_insn *ip;
9131 {
9132   char *s;
9133   const char *args;
9134   struct mips_opcode *insn;
9135   char *argsstart;
9136   unsigned int regno;
9137   unsigned int lastregno = 0;
9138   char *s_reset;
9139
9140   insn_error = NULL;
9141
9142   mips16_small = false;
9143   mips16_ext = false;
9144
9145   for (s = str; ISLOWER (*s); ++s)
9146     ;
9147   switch (*s)
9148     {
9149     case '\0':
9150       break;
9151
9152     case ' ':
9153       *s++ = '\0';
9154       break;
9155
9156     case '.':
9157       if (s[1] == 't' && s[2] == ' ')
9158         {
9159           *s = '\0';
9160           mips16_small = true;
9161           s += 3;
9162           break;
9163         }
9164       else if (s[1] == 'e' && s[2] == ' ')
9165         {
9166           *s = '\0';
9167           mips16_ext = true;
9168           s += 3;
9169           break;
9170         }
9171       /* Fall through.  */
9172     default:
9173       insn_error = _("unknown opcode");
9174       return;
9175     }
9176
9177   if (mips_opts.noautoextend && ! mips16_ext)
9178     mips16_small = true;
9179
9180   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9181     {
9182       insn_error = _("unrecognized opcode");
9183       return;
9184     }
9185
9186   argsstart = s;
9187   for (;;)
9188     {
9189       assert (strcmp (insn->name, str) == 0);
9190
9191       ip->insn_mo = insn;
9192       ip->insn_opcode = insn->match;
9193       ip->use_extend = false;
9194       imm_expr.X_op = O_absent;
9195       imm_reloc[0] = BFD_RELOC_UNUSED;
9196       imm_reloc[1] = BFD_RELOC_UNUSED;
9197       imm_reloc[2] = BFD_RELOC_UNUSED;
9198       offset_expr.X_op = O_absent;
9199       offset_reloc[0] = BFD_RELOC_UNUSED;
9200       offset_reloc[1] = BFD_RELOC_UNUSED;
9201       offset_reloc[2] = BFD_RELOC_UNUSED;
9202       for (args = insn->args; 1; ++args)
9203         {
9204           int c;
9205
9206           if (*s == ' ')
9207             ++s;
9208
9209           /* In this switch statement we call break if we did not find
9210              a match, continue if we did find a match, or return if we
9211              are done.  */
9212
9213           c = *args;
9214           switch (c)
9215             {
9216             case '\0':
9217               if (*s == '\0')
9218                 {
9219                   /* Stuff the immediate value in now, if we can.  */
9220                   if (imm_expr.X_op == O_constant
9221                       && *imm_reloc > BFD_RELOC_UNUSED
9222                       && insn->pinfo != INSN_MACRO)
9223                     {
9224                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9225                                     imm_expr.X_add_number, true, mips16_small,
9226                                     mips16_ext, &ip->insn_opcode,
9227                                     &ip->use_extend, &ip->extend);
9228                       imm_expr.X_op = O_absent;
9229                       *imm_reloc = BFD_RELOC_UNUSED;
9230                     }
9231
9232                   return;
9233                 }
9234               break;
9235
9236             case ',':
9237               if (*s++ == c)
9238                 continue;
9239               s--;
9240               switch (*++args)
9241                 {
9242                 case 'v':
9243                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9244                   continue;
9245                 case 'w':
9246                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9247                   continue;
9248                 }
9249               break;
9250
9251             case '(':
9252             case ')':
9253               if (*s++ == c)
9254                 continue;
9255               break;
9256
9257             case 'v':
9258             case 'w':
9259               if (s[0] != '$')
9260                 {
9261                   if (c == 'v')
9262                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9263                   else
9264                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9265                   ++args;
9266                   continue;
9267                 }
9268               /* Fall through.  */
9269             case 'x':
9270             case 'y':
9271             case 'z':
9272             case 'Z':
9273             case '0':
9274             case 'S':
9275             case 'R':
9276             case 'X':
9277             case 'Y':
9278               if (s[0] != '$')
9279                 break;
9280               s_reset = s;
9281               if (ISDIGIT (s[1]))
9282                 {
9283                   ++s;
9284                   regno = 0;
9285                   do
9286                     {
9287                       regno *= 10;
9288                       regno += *s - '0';
9289                       ++s;
9290                     }
9291                   while (ISDIGIT (*s));
9292                   if (regno > 31)
9293                     {
9294                       as_bad (_("invalid register number (%d)"), regno);
9295                       regno = 2;
9296                     }
9297                 }
9298               else
9299                 {
9300                   if (s[1] == 'r' && s[2] == 'a')
9301                     {
9302                       s += 3;
9303                       regno = RA;
9304                     }
9305                   else if (s[1] == 'f' && s[2] == 'p')
9306                     {
9307                       s += 3;
9308                       regno = FP;
9309                     }
9310                   else if (s[1] == 's' && s[2] == 'p')
9311                     {
9312                       s += 3;
9313                       regno = SP;
9314                     }
9315                   else if (s[1] == 'g' && s[2] == 'p')
9316                     {
9317                       s += 3;
9318                       regno = GP;
9319                     }
9320                   else if (s[1] == 'a' && s[2] == 't')
9321                     {
9322                       s += 3;
9323                       regno = AT;
9324                     }
9325                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9326                     {
9327                       s += 4;
9328                       regno = KT0;
9329                     }
9330                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9331                     {
9332                       s += 4;
9333                       regno = KT1;
9334                     }
9335                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9336                     {
9337                       s += 5;
9338                       regno = ZERO;
9339                     }
9340                   else
9341                     break;
9342                 }
9343
9344               if (*s == ' ')
9345                 ++s;
9346               if (args[1] != *s)
9347                 {
9348                   if (c == 'v' || c == 'w')
9349                     {
9350                       regno = mips16_to_32_reg_map[lastregno];
9351                       s = s_reset;
9352                       ++args;
9353                     }
9354                 }
9355
9356               switch (c)
9357                 {
9358                 case 'x':
9359                 case 'y':
9360                 case 'z':
9361                 case 'v':
9362                 case 'w':
9363                 case 'Z':
9364                   regno = mips32_to_16_reg_map[regno];
9365                   break;
9366
9367                 case '0':
9368                   if (regno != 0)
9369                     regno = ILLEGAL_REG;
9370                   break;
9371
9372                 case 'S':
9373                   if (regno != SP)
9374                     regno = ILLEGAL_REG;
9375                   break;
9376
9377                 case 'R':
9378                   if (regno != RA)
9379                     regno = ILLEGAL_REG;
9380                   break;
9381
9382                 case 'X':
9383                 case 'Y':
9384                   if (regno == AT && ! mips_opts.noat)
9385                     as_warn (_("used $at without \".set noat\""));
9386                   break;
9387
9388                 default:
9389                   internalError ();
9390                 }
9391
9392               if (regno == ILLEGAL_REG)
9393                 break;
9394
9395               switch (c)
9396                 {
9397                 case 'x':
9398                 case 'v':
9399                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9400                   break;
9401                 case 'y':
9402                 case 'w':
9403                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9404                   break;
9405                 case 'z':
9406                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9407                   break;
9408                 case 'Z':
9409                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9410                 case '0':
9411                 case 'S':
9412                 case 'R':
9413                   break;
9414                 case 'X':
9415                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9416                   break;
9417                 case 'Y':
9418                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9419                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9420                   break;
9421                 default:
9422                   internalError ();
9423                 }
9424
9425               lastregno = regno;
9426               continue;
9427
9428             case 'P':
9429               if (strncmp (s, "$pc", 3) == 0)
9430                 {
9431                   s += 3;
9432                   continue;
9433                 }
9434               break;
9435
9436             case '<':
9437             case '>':
9438             case '[':
9439             case ']':
9440             case '4':
9441             case '5':
9442             case 'H':
9443             case 'W':
9444             case 'D':
9445             case 'j':
9446             case '8':
9447             case 'V':
9448             case 'C':
9449             case 'U':
9450             case 'k':
9451             case 'K':
9452               if (s[0] == '%'
9453                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9454                 {
9455                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9456                      and generate the appropriate reloc.  If the text
9457                      inside %gprel is not a symbol name with an
9458                      optional offset, then we generate a normal reloc
9459                      and will probably fail later.  */
9460                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9461                   if (imm_expr.X_op == O_symbol)
9462                     {
9463                       mips16_ext = true;
9464                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9465                       s = expr_end;
9466                       ip->use_extend = true;
9467                       ip->extend = 0;
9468                       continue;
9469                     }
9470                 }
9471               else
9472                 {
9473                   /* Just pick up a normal expression.  */
9474                   my_getExpression (&imm_expr, s);
9475                 }
9476
9477               if (imm_expr.X_op == O_register)
9478                 {
9479                   /* What we thought was an expression turned out to
9480                      be a register.  */
9481
9482                   if (s[0] == '(' && args[1] == '(')
9483                     {
9484                       /* It looks like the expression was omitted
9485                          before a register indirection, which means
9486                          that the expression is implicitly zero.  We
9487                          still set up imm_expr, so that we handle
9488                          explicit extensions correctly.  */
9489                       imm_expr.X_op = O_constant;
9490                       imm_expr.X_add_number = 0;
9491                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9492                       continue;
9493                     }
9494
9495                   break;
9496                 }
9497
9498               /* We need to relax this instruction.  */
9499               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9500               s = expr_end;
9501               continue;
9502
9503             case 'p':
9504             case 'q':
9505             case 'A':
9506             case 'B':
9507             case 'E':
9508               /* We use offset_reloc rather than imm_reloc for the PC
9509                  relative operands.  This lets macros with both
9510                  immediate and address operands work correctly.  */
9511               my_getExpression (&offset_expr, s);
9512
9513               if (offset_expr.X_op == O_register)
9514                 break;
9515
9516               /* We need to relax this instruction.  */
9517               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9518               s = expr_end;
9519               continue;
9520
9521             case '6':           /* break code */
9522               my_getExpression (&imm_expr, s);
9523               check_absolute_expr (ip, &imm_expr);
9524               if ((unsigned long) imm_expr.X_add_number > 63)
9525                 {
9526                   as_warn (_("Invalid value for `%s' (%lu)"),
9527                            ip->insn_mo->name,
9528                            (unsigned long) imm_expr.X_add_number);
9529                   imm_expr.X_add_number &= 0x3f;
9530                 }
9531               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9532               imm_expr.X_op = O_absent;
9533               s = expr_end;
9534               continue;
9535
9536             case 'a':           /* 26 bit address */
9537               my_getExpression (&offset_expr, s);
9538               s = expr_end;
9539               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9540               ip->insn_opcode <<= 16;
9541               continue;
9542
9543             case 'l':           /* register list for entry macro */
9544             case 'L':           /* register list for exit macro */
9545               {
9546                 int mask;
9547
9548                 if (c == 'l')
9549                   mask = 0;
9550                 else
9551                   mask = 7 << 3;
9552                 while (*s != '\0')
9553                   {
9554                     int freg, reg1, reg2;
9555
9556                     while (*s == ' ' || *s == ',')
9557                       ++s;
9558                     if (*s != '$')
9559                       {
9560                         as_bad (_("can't parse register list"));
9561                         break;
9562                       }
9563                     ++s;
9564                     if (*s != 'f')
9565                       freg = 0;
9566                     else
9567                       {
9568                         freg = 1;
9569                         ++s;
9570                       }
9571                     reg1 = 0;
9572                     while (ISDIGIT (*s))
9573                       {
9574                         reg1 *= 10;
9575                         reg1 += *s - '0';
9576                         ++s;
9577                       }
9578                     if (*s == ' ')
9579                       ++s;
9580                     if (*s != '-')
9581                       reg2 = reg1;
9582                     else
9583                       {
9584                         ++s;
9585                         if (*s != '$')
9586                           break;
9587                         ++s;
9588                         if (freg)
9589                           {
9590                             if (*s == 'f')
9591                               ++s;
9592                             else
9593                               {
9594                                 as_bad (_("invalid register list"));
9595                                 break;
9596                               }
9597                           }
9598                         reg2 = 0;
9599                         while (ISDIGIT (*s))
9600                           {
9601                             reg2 *= 10;
9602                             reg2 += *s - '0';
9603                             ++s;
9604                           }
9605                       }
9606                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9607                       {
9608                         mask &= ~ (7 << 3);
9609                         mask |= 5 << 3;
9610                       }
9611                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9612                       {
9613                         mask &= ~ (7 << 3);
9614                         mask |= 6 << 3;
9615                       }
9616                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9617                       mask |= (reg2 - 3) << 3;
9618                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9619                       mask |= (reg2 - 15) << 1;
9620                     else if (reg1 == RA && reg2 == RA)
9621                       mask |= 1;
9622                     else
9623                       {
9624                         as_bad (_("invalid register list"));
9625                         break;
9626                       }
9627                   }
9628                 /* The mask is filled in in the opcode table for the
9629                    benefit of the disassembler.  We remove it before
9630                    applying the actual mask.  */
9631                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9632                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9633               }
9634             continue;
9635
9636             case 'e':           /* extend code */
9637               my_getExpression (&imm_expr, s);
9638               check_absolute_expr (ip, &imm_expr);
9639               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9640                 {
9641                   as_warn (_("Invalid value for `%s' (%lu)"),
9642                            ip->insn_mo->name,
9643                            (unsigned long) imm_expr.X_add_number);
9644                   imm_expr.X_add_number &= 0x7ff;
9645                 }
9646               ip->insn_opcode |= imm_expr.X_add_number;
9647               imm_expr.X_op = O_absent;
9648               s = expr_end;
9649               continue;
9650
9651             default:
9652               internalError ();
9653             }
9654           break;
9655         }
9656
9657       /* Args don't match.  */
9658       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9659           strcmp (insn->name, insn[1].name) == 0)
9660         {
9661           ++insn;
9662           s = argsstart;
9663           continue;
9664         }
9665
9666       insn_error = _("illegal operands");
9667
9668       return;
9669     }
9670 }
9671
9672 /* This structure holds information we know about a mips16 immediate
9673    argument type.  */
9674
9675 struct mips16_immed_operand
9676 {
9677   /* The type code used in the argument string in the opcode table.  */
9678   int type;
9679   /* The number of bits in the short form of the opcode.  */
9680   int nbits;
9681   /* The number of bits in the extended form of the opcode.  */
9682   int extbits;
9683   /* The amount by which the short form is shifted when it is used;
9684      for example, the sw instruction has a shift count of 2.  */
9685   int shift;
9686   /* The amount by which the short form is shifted when it is stored
9687      into the instruction code.  */
9688   int op_shift;
9689   /* Non-zero if the short form is unsigned.  */
9690   int unsp;
9691   /* Non-zero if the extended form is unsigned.  */
9692   int extu;
9693   /* Non-zero if the value is PC relative.  */
9694   int pcrel;
9695 };
9696
9697 /* The mips16 immediate operand types.  */
9698
9699 static const struct mips16_immed_operand mips16_immed_operands[] =
9700 {
9701   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9702   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9703   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9704   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9705   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9706   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9707   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9708   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9709   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9710   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9711   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9712   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9713   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9714   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9715   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9716   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9717   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9718   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9719   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9720   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9721   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9722 };
9723
9724 #define MIPS16_NUM_IMMED \
9725   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9726
9727 /* Handle a mips16 instruction with an immediate value.  This or's the
9728    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9729    whether an extended value is needed; if one is needed, it sets
9730    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9731    If SMALL is true, an unextended opcode was explicitly requested.
9732    If EXT is true, an extended opcode was explicitly requested.  If
9733    WARN is true, warn if EXT does not match reality.  */
9734
9735 static void
9736 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9737               extend)
9738      char *file;
9739      unsigned int line;
9740      int type;
9741      offsetT val;
9742      boolean warn;
9743      boolean small;
9744      boolean ext;
9745      unsigned long *insn;
9746      boolean *use_extend;
9747      unsigned short *extend;
9748 {
9749   register const struct mips16_immed_operand *op;
9750   int mintiny, maxtiny;
9751   boolean needext;
9752
9753   op = mips16_immed_operands;
9754   while (op->type != type)
9755     {
9756       ++op;
9757       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9758     }
9759
9760   if (op->unsp)
9761     {
9762       if (type == '<' || type == '>' || type == '[' || type == ']')
9763         {
9764           mintiny = 1;
9765           maxtiny = 1 << op->nbits;
9766         }
9767       else
9768         {
9769           mintiny = 0;
9770           maxtiny = (1 << op->nbits) - 1;
9771         }
9772     }
9773   else
9774     {
9775       mintiny = - (1 << (op->nbits - 1));
9776       maxtiny = (1 << (op->nbits - 1)) - 1;
9777     }
9778
9779   /* Branch offsets have an implicit 0 in the lowest bit.  */
9780   if (type == 'p' || type == 'q')
9781     val /= 2;
9782
9783   if ((val & ((1 << op->shift) - 1)) != 0
9784       || val < (mintiny << op->shift)
9785       || val > (maxtiny << op->shift))
9786     needext = true;
9787   else
9788     needext = false;
9789
9790   if (warn && ext && ! needext)
9791     as_warn_where (file, line,
9792                    _("extended operand requested but not required"));
9793   if (small && needext)
9794     as_bad_where (file, line, _("invalid unextended operand value"));
9795
9796   if (small || (! ext && ! needext))
9797     {
9798       int insnval;
9799
9800       *use_extend = false;
9801       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9802       insnval <<= op->op_shift;
9803       *insn |= insnval;
9804     }
9805   else
9806     {
9807       long minext, maxext;
9808       int extval;
9809
9810       if (op->extu)
9811         {
9812           minext = 0;
9813           maxext = (1 << op->extbits) - 1;
9814         }
9815       else
9816         {
9817           minext = - (1 << (op->extbits - 1));
9818           maxext = (1 << (op->extbits - 1)) - 1;
9819         }
9820       if (val < minext || val > maxext)
9821         as_bad_where (file, line,
9822                       _("operand value out of range for instruction"));
9823
9824       *use_extend = true;
9825       if (op->extbits == 16)
9826         {
9827           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9828           val &= 0x1f;
9829         }
9830       else if (op->extbits == 15)
9831         {
9832           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9833           val &= 0xf;
9834         }
9835       else
9836         {
9837           extval = ((val & 0x1f) << 6) | (val & 0x20);
9838           val = 0;
9839         }
9840
9841       *extend = (unsigned short) extval;
9842       *insn |= val;
9843     }
9844 }
9845 \f
9846 static struct percent_op_match
9847 {
9848    const char *str;
9849    const enum small_ex_type type;
9850 } percent_op[] =
9851 {
9852   {"%lo", S_EX_LO},
9853 #ifdef OBJ_ELF
9854   {"%call_hi", S_EX_CALL_HI},
9855   {"%call_lo", S_EX_CALL_LO},
9856   {"%call16", S_EX_CALL16},
9857   {"%got_disp", S_EX_GOT_DISP},
9858   {"%got_page", S_EX_GOT_PAGE},
9859   {"%got_ofst", S_EX_GOT_OFST},
9860   {"%got_hi", S_EX_GOT_HI},
9861   {"%got_lo", S_EX_GOT_LO},
9862   {"%got", S_EX_GOT},
9863   {"%gp_rel", S_EX_GP_REL},
9864   {"%half", S_EX_HALF},
9865   {"%highest", S_EX_HIGHEST},
9866   {"%higher", S_EX_HIGHER},
9867   {"%neg", S_EX_NEG},
9868 #endif
9869   {"%hi", S_EX_HI}
9870 };
9871
9872 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9873    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9874    can be nested, this is handled by blanking the innermost, parsing the
9875    rest by subsequent calls.  */
9876
9877 static int
9878 my_getSmallParser (str, len, nestlevel)
9879      char **str;
9880      unsigned int *len;
9881      int *nestlevel;
9882 {
9883   *len = 0;
9884   *str += strspn (*str, " \t");
9885   /* Check for expression in parentheses.  */
9886   if (**str == '(')
9887     {
9888       char *b = *str + 1 + strspn (*str + 1, " \t");
9889       char *e;
9890
9891       /* Check for base register.  */
9892       if (b[0] == '$')
9893         {
9894           if (strchr (b, ')')
9895               && (e = b + strcspn (b, ") \t"))
9896               && e - b > 1 && e - b < 4)
9897             {
9898               if ((e - b == 3
9899                    && ((b[1] == 'f' && b[2] == 'p')
9900                        || (b[1] == 's' && b[2] == 'p')
9901                        || (b[1] == 'g' && b[2] == 'p')
9902                        || (b[1] == 'a' && b[2] == 't')
9903                        || (ISDIGIT (b[1])
9904                            && ISDIGIT (b[2]))))
9905                   || (ISDIGIT (b[1])))
9906                 {
9907                   *len = strcspn (*str, ")") + 1;
9908                   return S_EX_REGISTER;
9909                 }
9910             }
9911         }
9912       /* Check for percent_op (in parentheses).  */
9913       else if (b[0] == '%')
9914         {
9915           *str = b;
9916           return my_getPercentOp (str, len, nestlevel);
9917         }
9918
9919       /* Some other expression in the parentheses, which can contain
9920          parentheses itself. Attempt to find the matching one.  */
9921       {
9922         int pcnt = 1;
9923         char *s;
9924
9925         *len = 1;
9926         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9927           {
9928             if (*s == '(')
9929               ++pcnt;
9930             else if (*s == ')')
9931               --pcnt;
9932           }
9933       }
9934     }
9935   /* Check for percent_op (outside of parentheses).  */
9936   else if (*str[0] == '%')
9937     return my_getPercentOp (str, len, nestlevel);
9938
9939   /* Any other expression.  */
9940   return S_EX_NONE;
9941 }
9942
9943 static int
9944 my_getPercentOp (str, len, nestlevel)
9945      char **str;
9946      unsigned int *len;
9947      int *nestlevel;
9948 {
9949   char *tmp = *str + 1;
9950   unsigned int i = 0;
9951
9952   while (ISALPHA (*tmp) || *tmp == '_')
9953     {
9954       *tmp = TOLOWER (*tmp);
9955       tmp++;
9956     }
9957   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9958     {
9959       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9960         i++;
9961       else
9962         {
9963           int type = percent_op[i].type;
9964
9965           /* Only %hi and %lo are allowed for OldABI.  */
9966           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9967             return S_EX_NONE;
9968
9969           *len = strlen (percent_op[i].str);
9970           ++(*nestlevel);
9971           return type;
9972         }
9973     }
9974   return S_EX_NONE;
9975 }
9976
9977 static int
9978 my_getSmallExpression (ep, str)
9979      expressionS *ep;
9980      char *str;
9981 {
9982   static char *oldstr = NULL;
9983   int c = S_EX_NONE;
9984   int oldc;
9985   int nestlevel = -1;
9986   unsigned int len;
9987
9988   /* Don't update oldstr if the last call had nested percent_op's. We need
9989      it to parse the outer ones later.  */
9990   if (! oldstr)
9991     oldstr = str;
9992
9993   do
9994     {
9995       oldc = c;
9996       c = my_getSmallParser (&str, &len, &nestlevel);
9997       if (c != S_EX_NONE && c != S_EX_REGISTER)
9998         str += len;
9999     }
10000   while (c != S_EX_NONE && c != S_EX_REGISTER);
10001
10002   if (nestlevel >= 0)
10003     {
10004       /* A percent_op was encountered.  Don't try to get an expression if
10005          it is already blanked out.  */
10006       if (*(str + strspn (str + 1, " )")) != ')')
10007         {
10008           char save;
10009
10010           /* Let my_getExpression() stop at the closing parenthesis.  */
10011           save = *(str + len);
10012           *(str + len) = '\0';
10013           my_getExpression (ep, str);
10014           *(str + len) = save;
10015         }
10016       if (nestlevel > 0)
10017         {
10018           /* Blank out including the % sign and the proper matching
10019              parenthesis.  */
10020           int pcnt = 1;
10021           char *s = strrchr (oldstr, '%');
10022           char *end;
10023
10024           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10025             {
10026               if (*end == '(')
10027                 ++pcnt;
10028               else if (*end == ')')
10029                 --pcnt;
10030             }
10031
10032           memset (s, ' ', end - s);
10033           str = oldstr;
10034         }
10035       else
10036         expr_end = str + len;
10037
10038       c = oldc;
10039     }
10040   else if (c == S_EX_NONE)
10041     {
10042       my_getExpression (ep, str);
10043     }
10044   else if (c == S_EX_REGISTER)
10045     {
10046       ep->X_op = O_constant;
10047       expr_end = str;
10048       ep->X_add_symbol = NULL;
10049       ep->X_op_symbol = NULL;
10050       ep->X_add_number = 0;
10051     }
10052   else
10053     {
10054       as_fatal (_("internal error"));
10055     }
10056
10057   if (nestlevel <= 0)
10058     /* All percent_op's have been handled.  */
10059     oldstr = NULL;
10060
10061   return c;
10062 }
10063
10064 static void
10065 my_getExpression (ep, str)
10066      expressionS *ep;
10067      char *str;
10068 {
10069   char *save_in;
10070   valueT val;
10071
10072   save_in = input_line_pointer;
10073   input_line_pointer = str;
10074   expression (ep);
10075   expr_end = input_line_pointer;
10076   input_line_pointer = save_in;
10077
10078   /* If we are in mips16 mode, and this is an expression based on `.',
10079      then we bump the value of the symbol by 1 since that is how other
10080      text symbols are handled.  We don't bother to handle complex
10081      expressions, just `.' plus or minus a constant.  */
10082   if (mips_opts.mips16
10083       && ep->X_op == O_symbol
10084       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10085       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10086       && symbol_get_frag (ep->X_add_symbol) == frag_now
10087       && symbol_constant_p (ep->X_add_symbol)
10088       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10089     S_SET_VALUE (ep->X_add_symbol, val + 1);
10090 }
10091
10092 /* Turn a string in input_line_pointer into a floating point constant
10093    of type TYPE, and store the appropriate bytes in *LITP.  The number
10094    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10095    returned, or NULL on OK.  */
10096
10097 char *
10098 md_atof (type, litP, sizeP)
10099      int type;
10100      char *litP;
10101      int *sizeP;
10102 {
10103   int prec;
10104   LITTLENUM_TYPE words[4];
10105   char *t;
10106   int i;
10107
10108   switch (type)
10109     {
10110     case 'f':
10111       prec = 2;
10112       break;
10113
10114     case 'd':
10115       prec = 4;
10116       break;
10117
10118     default:
10119       *sizeP = 0;
10120       return _("bad call to md_atof");
10121     }
10122
10123   t = atof_ieee (input_line_pointer, type, words);
10124   if (t)
10125     input_line_pointer = t;
10126
10127   *sizeP = prec * 2;
10128
10129   if (! target_big_endian)
10130     {
10131       for (i = prec - 1; i >= 0; i--)
10132         {
10133           md_number_to_chars (litP, (valueT) words[i], 2);
10134           litP += 2;
10135         }
10136     }
10137   else
10138     {
10139       for (i = 0; i < prec; i++)
10140         {
10141           md_number_to_chars (litP, (valueT) words[i], 2);
10142           litP += 2;
10143         }
10144     }
10145
10146   return NULL;
10147 }
10148
10149 void
10150 md_number_to_chars (buf, val, n)
10151      char *buf;
10152      valueT val;
10153      int n;
10154 {
10155   if (target_big_endian)
10156     number_to_chars_bigendian (buf, val, n);
10157   else
10158     number_to_chars_littleendian (buf, val, n);
10159 }
10160 \f
10161 #ifdef OBJ_ELF
10162 static int support_64bit_objects(void)
10163 {
10164   const char **list, **l;
10165   int yes;
10166
10167   list = bfd_target_list ();
10168   for (l = list; *l != NULL; l++)
10169 #ifdef TE_TMIPS
10170     /* This is traditional mips */
10171     if (strcmp (*l, "elf64-tradbigmips") == 0
10172         || strcmp (*l, "elf64-tradlittlemips") == 0)
10173 #else
10174     if (strcmp (*l, "elf64-bigmips") == 0
10175         || strcmp (*l, "elf64-littlemips") == 0)
10176 #endif
10177       break;
10178   yes = (*l != NULL);
10179   free (list);
10180   return yes;
10181 }
10182 #endif /* OBJ_ELF */
10183
10184 const char *md_shortopts = "nO::g::G:";
10185
10186 struct option md_longopts[] =
10187 {
10188 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10189   {"mips0", no_argument, NULL, OPTION_MIPS1},
10190   {"mips1", no_argument, NULL, OPTION_MIPS1},
10191 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10192   {"mips2", no_argument, NULL, OPTION_MIPS2},
10193 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10194   {"mips3", no_argument, NULL, OPTION_MIPS3},
10195 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10196   {"mips4", no_argument, NULL, OPTION_MIPS4},
10197 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10198   {"mips5", no_argument, NULL, OPTION_MIPS5},
10199 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10200   {"mips32", no_argument, NULL, OPTION_MIPS32},
10201 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10202   {"mips64", no_argument, NULL, OPTION_MIPS64},
10203 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10204   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10205 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10206   {"trap", no_argument, NULL, OPTION_TRAP},
10207   {"no-break", no_argument, NULL, OPTION_TRAP},
10208 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10209   {"break", no_argument, NULL, OPTION_BREAK},
10210   {"no-trap", no_argument, NULL, OPTION_BREAK},
10211 #define OPTION_EB (OPTION_MD_BASE + 11)
10212   {"EB", no_argument, NULL, OPTION_EB},
10213 #define OPTION_EL (OPTION_MD_BASE + 12)
10214   {"EL", no_argument, NULL, OPTION_EL},
10215 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10216   {"mips16", no_argument, NULL, OPTION_MIPS16},
10217 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10218   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10219 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10220   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10221 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10222   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10223   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10224 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10225   {"mfp32", no_argument, NULL, OPTION_FP32},
10226 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10227   {"mgp32", no_argument, NULL, OPTION_GP32},
10228 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10229   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10230 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10231   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10232 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10233   {"march", required_argument, NULL, OPTION_MARCH},
10234 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10235   {"mtune", required_argument, NULL, OPTION_MTUNE},
10236 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10237   {"mfp64", no_argument, NULL, OPTION_FP64},
10238 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10239   {"m4650", no_argument, NULL, OPTION_M4650},
10240 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10241   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10242 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10243   {"m4010", no_argument, NULL, OPTION_M4010},
10244 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10245   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10246 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10247   {"m4100", no_argument, NULL, OPTION_M4100},
10248 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10249   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10250 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10251   {"m3900", no_argument, NULL, OPTION_M3900},
10252 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10253   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10254 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10255   {"mgp64", no_argument, NULL, OPTION_GP64},
10256 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10257   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10258 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10259   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10260 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10261   {"mdmx", no_argument, NULL, OPTION_MDMX},
10262 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10263   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10264 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10265 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10266   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10267   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10268 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10269 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10270   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10271   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10272 #ifdef OBJ_ELF
10273 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 41)
10274 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10275   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10276   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10277 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10278   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10279 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10280   {"xgot",        no_argument, NULL, OPTION_XGOT},
10281 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10282   {"mabi", required_argument, NULL, OPTION_MABI},
10283 #define OPTION_32          (OPTION_ELF_BASE + 4)
10284   {"32",          no_argument, NULL, OPTION_32},
10285 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10286   {"n32",         no_argument, NULL, OPTION_N32},
10287 #define OPTION_64          (OPTION_ELF_BASE + 6)
10288   {"64",          no_argument, NULL, OPTION_64},
10289 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10290   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10291 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10292   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10293 #endif /* OBJ_ELF */
10294   {NULL, no_argument, NULL, 0}
10295 };
10296 size_t md_longopts_size = sizeof (md_longopts);
10297
10298 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10299    NEW_VALUE.  Warn if another value was already specified.  Note:
10300    we have to defer parsing the -march and -mtune arguments in order
10301    to handle 'from-abi' correctly, since the ABI might be specified
10302    in a later argument.  */
10303
10304 static void
10305 mips_set_option_string (string_ptr, new_value)
10306      const char **string_ptr, *new_value;
10307 {
10308   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10309     as_warn (_("A different %s was already specified, is now %s"),
10310              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10311              new_value);
10312
10313   *string_ptr = new_value;
10314 }
10315
10316 int
10317 md_parse_option (c, arg)
10318      int c;
10319      char *arg;
10320 {
10321   switch (c)
10322     {
10323     case OPTION_CONSTRUCT_FLOATS:
10324       mips_disable_float_construction = 0;
10325       break;
10326
10327     case OPTION_NO_CONSTRUCT_FLOATS:
10328       mips_disable_float_construction = 1;
10329       break;
10330
10331     case OPTION_TRAP:
10332       mips_trap = 1;
10333       break;
10334
10335     case OPTION_BREAK:
10336       mips_trap = 0;
10337       break;
10338
10339     case OPTION_EB:
10340       target_big_endian = 1;
10341       break;
10342
10343     case OPTION_EL:
10344       target_big_endian = 0;
10345       break;
10346
10347     case 'n':
10348       warn_nops = 1;
10349       break;
10350
10351     case 'O':
10352       if (arg && arg[1] == '0')
10353         mips_optimize = 1;
10354       else
10355         mips_optimize = 2;
10356       break;
10357
10358     case 'g':
10359       if (arg == NULL)
10360         mips_debug = 2;
10361       else
10362         mips_debug = atoi (arg);
10363       /* When the MIPS assembler sees -g or -g2, it does not do
10364          optimizations which limit full symbolic debugging.  We take
10365          that to be equivalent to -O0.  */
10366       if (mips_debug == 2)
10367         mips_optimize = 1;
10368       break;
10369
10370     case OPTION_MIPS1:
10371       file_mips_isa = ISA_MIPS1;
10372       break;
10373
10374     case OPTION_MIPS2:
10375       file_mips_isa = ISA_MIPS2;
10376       break;
10377
10378     case OPTION_MIPS3:
10379       file_mips_isa = ISA_MIPS3;
10380       break;
10381
10382     case OPTION_MIPS4:
10383       file_mips_isa = ISA_MIPS4;
10384       break;
10385
10386     case OPTION_MIPS5:
10387       file_mips_isa = ISA_MIPS5;
10388       break;
10389
10390     case OPTION_MIPS32:
10391       file_mips_isa = ISA_MIPS32;
10392       break;
10393
10394     case OPTION_MIPS64:
10395       file_mips_isa = ISA_MIPS64;
10396       break;
10397
10398     case OPTION_MTUNE:
10399       mips_set_option_string (&mips_tune_string, arg);
10400       break;
10401
10402     case OPTION_MARCH:
10403       mips_set_option_string (&mips_arch_string, arg);
10404       break;
10405
10406     case OPTION_M4650:
10407       mips_set_option_string (&mips_arch_string, "4650");
10408       mips_set_option_string (&mips_tune_string, "4650");
10409       break;
10410
10411     case OPTION_NO_M4650:
10412       break;
10413
10414     case OPTION_M4010:
10415       mips_set_option_string (&mips_arch_string, "4010");
10416       mips_set_option_string (&mips_tune_string, "4010");
10417       break;
10418
10419     case OPTION_NO_M4010:
10420       break;
10421
10422     case OPTION_M4100:
10423       mips_set_option_string (&mips_arch_string, "4100");
10424       mips_set_option_string (&mips_tune_string, "4100");
10425       break;
10426
10427     case OPTION_NO_M4100:
10428       break;
10429
10430     case OPTION_M3900:
10431       mips_set_option_string (&mips_arch_string, "3900");
10432       mips_set_option_string (&mips_tune_string, "3900");
10433       break;
10434
10435     case OPTION_NO_M3900:
10436       break;
10437
10438     case OPTION_MDMX:
10439       mips_opts.ase_mdmx = 1;
10440       break;
10441
10442     case OPTION_NO_MDMX:
10443       mips_opts.ase_mdmx = 0;
10444       break;
10445
10446     case OPTION_MIPS16:
10447       mips_opts.mips16 = 1;
10448       mips_no_prev_insn (false);
10449       break;
10450
10451     case OPTION_NO_MIPS16:
10452       mips_opts.mips16 = 0;
10453       mips_no_prev_insn (false);
10454       break;
10455
10456     case OPTION_MIPS3D:
10457       mips_opts.ase_mips3d = 1;
10458       break;
10459
10460     case OPTION_NO_MIPS3D:
10461       mips_opts.ase_mips3d = 0;
10462       break;
10463
10464     case OPTION_MEMBEDDED_PIC:
10465       mips_pic = EMBEDDED_PIC;
10466       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10467         {
10468           as_bad (_("-G may not be used with embedded PIC code"));
10469           return 0;
10470         }
10471       g_switch_value = 0x7fffffff;
10472       break;
10473
10474     case OPTION_FIX_VR4122:
10475       mips_fix_4122_bugs = 1;
10476       break;
10477
10478     case OPTION_NO_FIX_VR4122:
10479       mips_fix_4122_bugs = 0;
10480       break;
10481
10482     case OPTION_RELAX_BRANCH:
10483       mips_relax_branch = 1;
10484       break;
10485
10486     case OPTION_NO_RELAX_BRANCH:
10487       mips_relax_branch = 0;
10488       break;
10489
10490 #ifdef OBJ_ELF
10491       /* When generating ELF code, we permit -KPIC and -call_shared to
10492          select SVR4_PIC, and -non_shared to select no PIC.  This is
10493          intended to be compatible with Irix 5.  */
10494     case OPTION_CALL_SHARED:
10495       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10496         {
10497           as_bad (_("-call_shared is supported only for ELF format"));
10498           return 0;
10499         }
10500       mips_pic = SVR4_PIC;
10501       if (g_switch_seen && g_switch_value != 0)
10502         {
10503           as_bad (_("-G may not be used with SVR4 PIC code"));
10504           return 0;
10505         }
10506       g_switch_value = 0;
10507       break;
10508
10509     case OPTION_NON_SHARED:
10510       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10511         {
10512           as_bad (_("-non_shared is supported only for ELF format"));
10513           return 0;
10514         }
10515       mips_pic = NO_PIC;
10516       break;
10517
10518       /* The -xgot option tells the assembler to use 32 offsets when
10519          accessing the got in SVR4_PIC mode.  It is for Irix
10520          compatibility.  */
10521     case OPTION_XGOT:
10522       mips_big_got = 1;
10523       break;
10524 #endif /* OBJ_ELF */
10525
10526     case 'G':
10527       if (! USE_GLOBAL_POINTER_OPT)
10528         {
10529           as_bad (_("-G is not supported for this configuration"));
10530           return 0;
10531         }
10532       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10533         {
10534           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10535           return 0;
10536         }
10537       else
10538         g_switch_value = atoi (arg);
10539       g_switch_seen = 1;
10540       break;
10541
10542 #ifdef OBJ_ELF
10543       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10544          and -mabi=64.  */
10545     case OPTION_32:
10546       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10547         {
10548           as_bad (_("-32 is supported for ELF format only"));
10549           return 0;
10550         }
10551       mips_abi = O32_ABI;
10552       break;
10553
10554     case OPTION_N32:
10555       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10556         {
10557           as_bad (_("-n32 is supported for ELF format only"));
10558           return 0;
10559         }
10560       mips_abi = N32_ABI;
10561       break;
10562
10563     case OPTION_64:
10564       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10565         {
10566           as_bad (_("-64 is supported for ELF format only"));
10567           return 0;
10568         }
10569       mips_abi = N64_ABI;
10570       if (! support_64bit_objects())
10571         as_fatal (_("No compiled in support for 64 bit object file format"));
10572       break;
10573 #endif /* OBJ_ELF */
10574
10575     case OPTION_GP32:
10576       file_mips_gp32 = 1;
10577       break;
10578
10579     case OPTION_GP64:
10580       file_mips_gp32 = 0;
10581       break;
10582
10583     case OPTION_FP32:
10584       file_mips_fp32 = 1;
10585       break;
10586
10587     case OPTION_FP64:
10588       file_mips_fp32 = 0;
10589       break;
10590
10591 #ifdef OBJ_ELF
10592     case OPTION_MABI:
10593       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10594         {
10595           as_bad (_("-mabi is supported for ELF format only"));
10596           return 0;
10597         }
10598       if (strcmp (arg, "32") == 0)
10599         mips_abi = O32_ABI;
10600       else if (strcmp (arg, "o64") == 0)
10601         mips_abi = O64_ABI;
10602       else if (strcmp (arg, "n32") == 0)
10603         mips_abi = N32_ABI;
10604       else if (strcmp (arg, "64") == 0)
10605         {
10606           mips_abi = N64_ABI;
10607           if (! support_64bit_objects())
10608             as_fatal (_("No compiled in support for 64 bit object file "
10609                         "format"));
10610         }
10611       else if (strcmp (arg, "eabi") == 0)
10612         mips_abi = EABI_ABI;
10613       else
10614         {
10615           as_fatal (_("invalid abi -mabi=%s"), arg);
10616           return 0;
10617         }
10618       break;
10619 #endif /* OBJ_ELF */
10620
10621     case OPTION_M7000_HILO_FIX:
10622       mips_7000_hilo_fix = true;
10623       break;
10624
10625     case OPTION_MNO_7000_HILO_FIX:
10626       mips_7000_hilo_fix = false;
10627       break;
10628
10629 #ifdef OBJ_ELF
10630     case OPTION_MDEBUG:
10631       mips_flag_mdebug = true;
10632       break;
10633
10634     case OPTION_NO_MDEBUG:
10635       mips_flag_mdebug = false;
10636       break;
10637 #endif /* OBJ_ELF */
10638
10639     default:
10640       return 0;
10641     }
10642
10643   return 1;
10644 }
10645 \f
10646 /* Set up globals to generate code for the ISA or processor
10647    described by INFO.  */
10648
10649 static void
10650 mips_set_architecture (info)
10651      const struct mips_cpu_info *info;
10652 {
10653   if (info != 0)
10654     {
10655       mips_arch_info = info;
10656       mips_arch = info->cpu;
10657       mips_opts.isa = info->isa;
10658     }
10659 }
10660
10661
10662 /* Likewise for tuning.  */
10663
10664 static void
10665 mips_set_tune (info)
10666      const struct mips_cpu_info *info;
10667 {
10668   if (info != 0)
10669     {
10670       mips_tune_info = info;
10671       mips_tune = info->cpu;
10672     }
10673 }
10674
10675
10676 void
10677 mips_after_parse_args ()
10678 {
10679   /* GP relative stuff not working for PE */
10680   if (strncmp (TARGET_OS, "pe", 2) == 0
10681       && g_switch_value != 0)
10682     {
10683       if (g_switch_seen)
10684         as_bad (_("-G not supported in this configuration."));
10685       g_switch_value = 0;
10686     }
10687
10688   /* The following code determines the architecture and register size.
10689      Similar code was added to GCC 3.3 (see override_options() in
10690      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10691      as much as possible.  */
10692
10693   if (mips_arch_string != 0)
10694     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10695
10696   if (mips_tune_string != 0)
10697     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10698
10699   if (file_mips_isa != ISA_UNKNOWN)
10700     {
10701       /* Handle -mipsN.  At this point, file_mips_isa contains the
10702          ISA level specified by -mipsN, while mips_opts.isa contains
10703          the -march selection (if any).  */
10704       if (mips_arch_info != 0)
10705         {
10706           /* -march takes precedence over -mipsN, since it is more descriptive.
10707              There's no harm in specifying both as long as the ISA levels
10708              are the same.  */
10709           if (file_mips_isa != mips_opts.isa)
10710             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10711                     mips_cpu_info_from_isa (file_mips_isa)->name,
10712                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10713         }
10714       else
10715         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10716     }
10717
10718   if (mips_arch_info == 0)
10719     mips_set_architecture (mips_parse_cpu ("default CPU",
10720                                            MIPS_CPU_STRING_DEFAULT));
10721
10722   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10723     as_bad ("-march=%s is not compatible with the selected ABI",
10724             mips_arch_info->name);
10725
10726   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10727   if (mips_tune_info == 0)
10728     mips_set_tune (mips_arch_info);
10729
10730   if (file_mips_gp32 >= 0)
10731     {
10732       /* The user specified the size of the integer registers.  Make sure
10733          it agrees with the ABI and ISA.  */
10734       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10735         as_bad (_("-mgp64 used with a 32-bit processor"));
10736       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10737         as_bad (_("-mgp32 used with a 64-bit ABI"));
10738       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10739         as_bad (_("-mgp64 used with a 32-bit ABI"));
10740     }
10741   else
10742     {
10743       /* Infer the integer register size from the ABI and processor.
10744          Restrict ourselves to 32-bit registers if that's all the
10745          processor has, or if the ABI cannot handle 64-bit registers.  */
10746       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10747                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10748     }
10749
10750   /* ??? GAS treats single-float processors as though they had 64-bit
10751      float registers (although it complains when double-precision
10752      instructions are used).  As things stand, saying they have 32-bit
10753      registers would lead to spurious "register must be even" messages.
10754      So here we assume float registers are always the same size as
10755      integer ones, unless the user says otherwise.  */
10756   if (file_mips_fp32 < 0)
10757     file_mips_fp32 = file_mips_gp32;
10758
10759   /* End of GCC-shared inference code.  */
10760
10761   /* ??? When do we want this flag to be set?   Who uses it?  */
10762   if (file_mips_gp32 == 1
10763       && mips_abi == NO_ABI
10764       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10765     mips_32bitmode = 1;
10766
10767   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10768     as_bad (_("trap exception not supported at ISA 1"));
10769
10770   /* If the selected architecture includes support for ASEs, enable
10771      generation of code for them.  */
10772   if (mips_opts.mips16 == -1)
10773     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10774   if (mips_opts.ase_mips3d == -1)
10775     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10776   if (mips_opts.ase_mdmx == -1)
10777     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10778
10779   file_mips_isa = mips_opts.isa;
10780   file_ase_mips16 = mips_opts.mips16;
10781   file_ase_mips3d = mips_opts.ase_mips3d;
10782   file_ase_mdmx = mips_opts.ase_mdmx;
10783   mips_opts.gp32 = file_mips_gp32;
10784   mips_opts.fp32 = file_mips_fp32;
10785
10786   if (mips_flag_mdebug < 0)
10787     {
10788 #ifdef OBJ_MAYBE_ECOFF
10789       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10790         mips_flag_mdebug = 1;
10791       else
10792 #endif /* OBJ_MAYBE_ECOFF */
10793         mips_flag_mdebug = 0;
10794     }
10795 }
10796 \f
10797 void
10798 mips_init_after_args ()
10799 {
10800   /* initialize opcodes */
10801   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10802   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10803 }
10804
10805 long
10806 md_pcrel_from (fixP)
10807      fixS *fixP;
10808 {
10809   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10810       && fixP->fx_addsy != (symbolS *) NULL
10811       && ! S_IS_DEFINED (fixP->fx_addsy))
10812     {
10813       /* This makes a branch to an undefined symbol be a branch to the
10814          current location.  */
10815       if (mips_pic == EMBEDDED_PIC)
10816         return 4;
10817       else
10818         return 1;
10819     }
10820
10821   /* Return the address of the delay slot.  */
10822   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10823 }
10824
10825 /* This is called before the symbol table is processed.  In order to
10826    work with gcc when using mips-tfile, we must keep all local labels.
10827    However, in other cases, we want to discard them.  If we were
10828    called with -g, but we didn't see any debugging information, it may
10829    mean that gcc is smuggling debugging information through to
10830    mips-tfile, in which case we must generate all local labels.  */
10831
10832 void
10833 mips_frob_file_before_adjust ()
10834 {
10835 #ifndef NO_ECOFF_DEBUGGING
10836   if (ECOFF_DEBUGGING
10837       && mips_debug != 0
10838       && ! ecoff_debugging_seen)
10839     flag_keep_locals = 1;
10840 #endif
10841 }
10842
10843 /* Sort any unmatched HI16_S relocs so that they immediately precede
10844    the corresponding LO reloc.  This is called before md_apply_fix3 and
10845    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10846    explicit use of the %hi modifier.  */
10847
10848 void
10849 mips_frob_file ()
10850 {
10851   struct mips_hi_fixup *l;
10852
10853   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10854     {
10855       segment_info_type *seginfo;
10856       int pass;
10857
10858       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10859
10860       /* Check quickly whether the next fixup happens to be a matching
10861          %lo.  */
10862       if (l->fixp->fx_next != NULL
10863           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10864           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10865           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10866         continue;
10867
10868       /* Look through the fixups for this segment for a matching %lo.
10869          When we find one, move the %hi just in front of it.  We do
10870          this in two passes.  In the first pass, we try to find a
10871          unique %lo.  In the second pass, we permit multiple %hi
10872          relocs for a single %lo (this is a GNU extension).  */
10873       seginfo = seg_info (l->seg);
10874       for (pass = 0; pass < 2; pass++)
10875         {
10876           fixS *f, *prev;
10877
10878           prev = NULL;
10879           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10880             {
10881               /* Check whether this is a %lo fixup which matches l->fixp.  */
10882               if (f->fx_r_type == BFD_RELOC_LO16
10883                   && f->fx_addsy == l->fixp->fx_addsy
10884                   && f->fx_offset == l->fixp->fx_offset
10885                   && (pass == 1
10886                       || prev == NULL
10887                       || prev->fx_r_type != BFD_RELOC_HI16_S
10888                       || prev->fx_addsy != f->fx_addsy
10889                       || prev->fx_offset !=  f->fx_offset))
10890                 {
10891                   fixS **pf;
10892
10893                   /* Move l->fixp before f.  */
10894                   for (pf = &seginfo->fix_root;
10895                        *pf != l->fixp;
10896                        pf = &(*pf)->fx_next)
10897                     assert (*pf != NULL);
10898
10899                   *pf = l->fixp->fx_next;
10900
10901                   l->fixp->fx_next = f;
10902                   if (prev == NULL)
10903                     seginfo->fix_root = l->fixp;
10904                   else
10905                     prev->fx_next = l->fixp;
10906
10907                   break;
10908                 }
10909
10910               prev = f;
10911             }
10912
10913           if (f != NULL)
10914             break;
10915
10916 #if 0 /* GCC code motion plus incomplete dead code elimination
10917          can leave a %hi without a %lo.  */
10918           if (pass == 1)
10919             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10920                            _("Unmatched %%hi reloc"));
10921 #endif
10922         }
10923     }
10924 }
10925
10926 /* When generating embedded PIC code we need to use a special
10927    relocation to represent the difference of two symbols in the .text
10928    section (switch tables use a difference of this sort).  See
10929    include/coff/mips.h for details.  This macro checks whether this
10930    fixup requires the special reloc.  */
10931 #define SWITCH_TABLE(fixp) \
10932   ((fixp)->fx_r_type == BFD_RELOC_32 \
10933    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10934    && (fixp)->fx_addsy != NULL \
10935    && (fixp)->fx_subsy != NULL \
10936    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10937    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10938
10939 /* When generating embedded PIC code we must keep all PC relative
10940    relocations, in case the linker has to relax a call.  We also need
10941    to keep relocations for switch table entries.
10942
10943    We may have combined relocations without symbols in the N32/N64 ABI.
10944    We have to prevent gas from dropping them.  */
10945
10946 int
10947 mips_force_relocation (fixp)
10948      fixS *fixp;
10949 {
10950   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10951       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10952       || S_FORCE_RELOC (fixp->fx_addsy))
10953     return 1;
10954
10955   if (HAVE_NEWABI
10956       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10957       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10958           || fixp->fx_r_type == BFD_RELOC_HI16_S
10959           || fixp->fx_r_type == BFD_RELOC_LO16))
10960     return 1;
10961
10962   return (mips_pic == EMBEDDED_PIC
10963           && (fixp->fx_pcrel
10964               || SWITCH_TABLE (fixp)
10965               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10966               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10967 }
10968
10969 #ifdef OBJ_ELF
10970 static int
10971 mips_need_elf_addend_fixup (fixP)
10972      fixS *fixP;
10973 {
10974   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10975     return 1;
10976   if (mips_pic == EMBEDDED_PIC
10977       && S_IS_WEAK (fixP->fx_addsy))
10978     return 1;
10979   if (mips_pic != EMBEDDED_PIC
10980       && (S_IS_WEAK (fixP->fx_addsy)
10981           || S_IS_EXTERNAL (fixP->fx_addsy))
10982       && !S_IS_COMMON (fixP->fx_addsy))
10983     return 1;
10984   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10985       && (((bfd_get_section_flags (stdoutput,
10986                                    S_GET_SEGMENT (fixP->fx_addsy))
10987             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
10988           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10989                        ".gnu.linkonce",
10990                        sizeof (".gnu.linkonce") - 1)))
10991     return 1;
10992   return 0;
10993 }
10994 #endif
10995
10996 /* Apply a fixup to the object file.  */
10997
10998 void
10999 md_apply_fix3 (fixP, valP, seg)
11000      fixS *fixP;
11001      valueT *valP;
11002      segT seg ATTRIBUTE_UNUSED;
11003 {
11004   bfd_byte *buf;
11005   long insn;
11006   valueT value;
11007   static int previous_fx_r_type = 0;
11008
11009   /* FIXME: Maybe just return for all reloc types not listed below?
11010      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11011   if (fixP->fx_r_type == BFD_RELOC_8)
11012       return;
11013
11014   assert (fixP->fx_size == 4
11015           || fixP->fx_r_type == BFD_RELOC_16
11016           || fixP->fx_r_type == BFD_RELOC_32
11017           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11018           || fixP->fx_r_type == BFD_RELOC_HI16_S
11019           || fixP->fx_r_type == BFD_RELOC_LO16
11020           || fixP->fx_r_type == BFD_RELOC_GPREL16
11021           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11022           || fixP->fx_r_type == BFD_RELOC_GPREL32
11023           || fixP->fx_r_type == BFD_RELOC_64
11024           || fixP->fx_r_type == BFD_RELOC_CTOR
11025           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11026           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11027           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11028           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11029           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11030           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11031           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11032           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11033           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11034
11035   value = *valP;
11036
11037   /* If we aren't adjusting this fixup to be against the section
11038      symbol, we need to adjust the value.  */
11039 #ifdef OBJ_ELF
11040   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11041     {
11042       if (mips_need_elf_addend_fixup (fixP))
11043         {
11044           reloc_howto_type *howto;
11045           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11046
11047           value -= symval;
11048
11049           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11050           if (value != 0 && howto->partial_inplace
11051               && (! fixP->fx_pcrel || howto->pcrel_offset))
11052             {
11053               /* In this case, the bfd_install_relocation routine will
11054                  incorrectly add the symbol value back in.  We just want
11055                  the addend to appear in the object file.
11056                  
11057                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11058                  generated for code like
11059                  
11060                         globl g1 .text
11061                         .text
11062                         .space 20
11063                  g1:
11064                  x:
11065                         bal g1
11066                */
11067               value -= symval;
11068
11069               /* Make sure the addend is still non-zero.  If it became zero
11070                  after the last operation, set it to a spurious value and
11071                  subtract the same value from the object file's contents.  */
11072               if (value == 0)
11073                 {
11074                   value = 8;
11075
11076                   /* The in-place addends for LO16 relocations are signed;
11077                      leave the matching HI16 in-place addends as zero.  */
11078                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11079                     {
11080                       bfd_vma contents, mask, field;
11081
11082                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11083                                                + fixP->fx_where,
11084                                                fixP->fx_size * 8,
11085                                                target_big_endian);
11086
11087                       /* MASK has bits set where the relocation should go.
11088                          FIELD is -value, shifted into the appropriate place
11089                          for this relocation.  */
11090                       mask = 1 << (howto->bitsize - 1);
11091                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11092                       field = (-value >> howto->rightshift) << howto->bitpos;
11093
11094                       bfd_put_bits ((field & mask) | (contents & ~mask),
11095                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11096                                     fixP->fx_size * 8,
11097                                     target_big_endian);
11098                     }
11099                 }
11100             }
11101         }
11102
11103       /* This code was generated using trial and error and so is
11104          fragile and not trustworthy.  If you change it, you should
11105          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11106          they still pass.  */
11107       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11108         {
11109           value += fixP->fx_frag->fr_address + fixP->fx_where;
11110
11111           /* BFD's REL handling, for MIPS, is _very_ weird.
11112              This gives the right results, but it can't possibly
11113              be the way things are supposed to work.  */
11114           if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
11115                && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
11116               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11117             value += fixP->fx_frag->fr_address + fixP->fx_where;
11118         }
11119     }
11120 #endif
11121
11122   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11123
11124   /* We are not done if this is a composite relocation to set up gp.  */
11125   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11126       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11127            || (fixP->fx_r_type == BFD_RELOC_64
11128                && (previous_fx_r_type == BFD_RELOC_GPREL32
11129                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11130            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11131                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11132                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11133     fixP->fx_done = 1;
11134   previous_fx_r_type = fixP->fx_r_type;
11135
11136   switch (fixP->fx_r_type)
11137     {
11138     case BFD_RELOC_MIPS_JMP:
11139     case BFD_RELOC_MIPS_SHIFT5:
11140     case BFD_RELOC_MIPS_SHIFT6:
11141     case BFD_RELOC_MIPS_GOT_DISP:
11142     case BFD_RELOC_MIPS_GOT_PAGE:
11143     case BFD_RELOC_MIPS_GOT_OFST:
11144     case BFD_RELOC_MIPS_SUB:
11145     case BFD_RELOC_MIPS_INSERT_A:
11146     case BFD_RELOC_MIPS_INSERT_B:
11147     case BFD_RELOC_MIPS_DELETE:
11148     case BFD_RELOC_MIPS_HIGHEST:
11149     case BFD_RELOC_MIPS_HIGHER:
11150     case BFD_RELOC_MIPS_SCN_DISP:
11151     case BFD_RELOC_MIPS_REL16:
11152     case BFD_RELOC_MIPS_RELGOT:
11153     case BFD_RELOC_MIPS_JALR:
11154     case BFD_RELOC_HI16:
11155     case BFD_RELOC_HI16_S:
11156     case BFD_RELOC_GPREL16:
11157     case BFD_RELOC_MIPS_LITERAL:
11158     case BFD_RELOC_MIPS_CALL16:
11159     case BFD_RELOC_MIPS_GOT16:
11160     case BFD_RELOC_GPREL32:
11161     case BFD_RELOC_MIPS_GOT_HI16:
11162     case BFD_RELOC_MIPS_GOT_LO16:
11163     case BFD_RELOC_MIPS_CALL_HI16:
11164     case BFD_RELOC_MIPS_CALL_LO16:
11165     case BFD_RELOC_MIPS16_GPREL:
11166       if (fixP->fx_pcrel)
11167         as_bad_where (fixP->fx_file, fixP->fx_line,
11168                       _("Invalid PC relative reloc"));
11169       /* Nothing needed to do. The value comes from the reloc entry */
11170       break;
11171
11172     case BFD_RELOC_MIPS16_JMP:
11173       /* We currently always generate a reloc against a symbol, which
11174          means that we don't want an addend even if the symbol is
11175          defined.  */
11176       fixP->fx_addnumber = 0;
11177       break;
11178
11179     case BFD_RELOC_PCREL_HI16_S:
11180       /* The addend for this is tricky if it is internal, so we just
11181          do everything here rather than in bfd_install_relocation.  */
11182       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11183           && !fixP->fx_done
11184           && value != 0)
11185         break;
11186       if (fixP->fx_addsy
11187           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11188         {
11189           /* For an external symbol adjust by the address to make it
11190              pcrel_offset.  We use the address of the RELLO reloc
11191              which follows this one.  */
11192           value += (fixP->fx_next->fx_frag->fr_address
11193                     + fixP->fx_next->fx_where);
11194         }
11195       value = ((value + 0x8000) >> 16) & 0xffff;
11196       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11197       if (target_big_endian)
11198         buf += 2;
11199       md_number_to_chars ((char *) buf, value, 2);
11200       break;
11201
11202     case BFD_RELOC_PCREL_LO16:
11203       /* The addend for this is tricky if it is internal, so we just
11204          do everything here rather than in bfd_install_relocation.  */
11205       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11206           && !fixP->fx_done
11207           && value != 0)
11208         break;
11209       if (fixP->fx_addsy
11210           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11211         value += fixP->fx_frag->fr_address + fixP->fx_where;
11212       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11213       if (target_big_endian)
11214         buf += 2;
11215       md_number_to_chars ((char *) buf, value, 2);
11216       break;
11217
11218     case BFD_RELOC_64:
11219       /* This is handled like BFD_RELOC_32, but we output a sign
11220          extended value if we are only 32 bits.  */
11221       if (fixP->fx_done
11222           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11223         {
11224           if (8 <= sizeof (valueT))
11225             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11226                                 value, 8);
11227           else
11228             {
11229               long w1, w2;
11230               long hiv;
11231
11232               w1 = w2 = fixP->fx_where;
11233               if (target_big_endian)
11234                 w1 += 4;
11235               else
11236                 w2 += 4;
11237               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11238               if ((value & 0x80000000) != 0)
11239                 hiv = 0xffffffff;
11240               else
11241                 hiv = 0;
11242               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11243             }
11244         }
11245       break;
11246
11247     case BFD_RELOC_RVA:
11248     case BFD_RELOC_32:
11249       /* If we are deleting this reloc entry, we must fill in the
11250          value now.  This can happen if we have a .word which is not
11251          resolved when it appears but is later defined.  We also need
11252          to fill in the value if this is an embedded PIC switch table
11253          entry.  */
11254       if (fixP->fx_done
11255           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11256         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11257                             value, 4);
11258       break;
11259
11260     case BFD_RELOC_16:
11261       /* If we are deleting this reloc entry, we must fill in the
11262          value now.  */
11263       assert (fixP->fx_size == 2);
11264       if (fixP->fx_done)
11265         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11266                             value, 2);
11267       break;
11268
11269     case BFD_RELOC_LO16:
11270       /* When handling an embedded PIC switch statement, we can wind
11271          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11272       if (fixP->fx_done)
11273         {
11274           if (value + 0x8000 > 0xffff)
11275             as_bad_where (fixP->fx_file, fixP->fx_line,
11276                           _("relocation overflow"));
11277           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11278           if (target_big_endian)
11279             buf += 2;
11280           md_number_to_chars ((char *) buf, value, 2);
11281         }
11282       break;
11283
11284     case BFD_RELOC_16_PCREL_S2:
11285       if ((value & 0x3) != 0)
11286         as_bad_where (fixP->fx_file, fixP->fx_line,
11287                       _("Branch to odd address (%lx)"), (long) value);
11288
11289       /* Fall through.  */
11290
11291     case BFD_RELOC_16_PCREL:
11292       /*
11293        * We need to save the bits in the instruction since fixup_segment()
11294        * might be deleting the relocation entry (i.e., a branch within
11295        * the current segment).
11296        */
11297       if (!fixP->fx_done && value != 0)
11298         break;
11299       /* If 'value' is zero, the remaining reloc code won't actually
11300          do the store, so it must be done here.  This is probably
11301          a bug somewhere.  */
11302       if (!fixP->fx_done
11303           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11304               || fixP->fx_addsy == NULL                 /* ??? */
11305               || ! S_IS_DEFINED (fixP->fx_addsy)))
11306         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11307
11308       value = (offsetT) value >> 2;
11309
11310       /* update old instruction data */
11311       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11312       if (target_big_endian)
11313         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11314       else
11315         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11316
11317       if (value + 0x8000 <= 0xffff)
11318         insn |= value & 0xffff;
11319       else
11320         {
11321           /* The branch offset is too large.  If this is an
11322              unconditional branch, and we are not generating PIC code,
11323              we can convert it to an absolute jump instruction.  */
11324           if (mips_pic == NO_PIC
11325               && fixP->fx_done
11326               && fixP->fx_frag->fr_address >= text_section->vma
11327               && (fixP->fx_frag->fr_address
11328                   < text_section->vma + text_section->_raw_size)
11329               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11330                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11331                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11332             {
11333               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11334                 insn = 0x0c000000;      /* jal */
11335               else
11336                 insn = 0x08000000;      /* j */
11337               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11338               fixP->fx_done = 0;
11339               fixP->fx_addsy = section_symbol (text_section);
11340               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11341             }
11342           else
11343             {
11344               /* If we got here, we have branch-relaxation disabled,
11345                  and there's nothing we can do to fix this instruction
11346                  without turning it into a longer sequence.  */
11347               as_bad_where (fixP->fx_file, fixP->fx_line,
11348                             _("Branch out of range"));
11349             }
11350         }
11351
11352       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11353       break;
11354
11355     case BFD_RELOC_VTABLE_INHERIT:
11356       fixP->fx_done = 0;
11357       if (fixP->fx_addsy
11358           && !S_IS_DEFINED (fixP->fx_addsy)
11359           && !S_IS_WEAK (fixP->fx_addsy))
11360         S_SET_WEAK (fixP->fx_addsy);
11361       break;
11362
11363     case BFD_RELOC_VTABLE_ENTRY:
11364       fixP->fx_done = 0;
11365       break;
11366
11367     default:
11368       internalError ();
11369     }
11370 }
11371
11372 #if 0
11373 void
11374 printInsn (oc)
11375      unsigned long oc;
11376 {
11377   const struct mips_opcode *p;
11378   int treg, sreg, dreg, shamt;
11379   short imm;
11380   const char *args;
11381   int i;
11382
11383   for (i = 0; i < NUMOPCODES; ++i)
11384     {
11385       p = &mips_opcodes[i];
11386       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11387         {
11388           printf ("%08lx %s\t", oc, p->name);
11389           treg = (oc >> 16) & 0x1f;
11390           sreg = (oc >> 21) & 0x1f;
11391           dreg = (oc >> 11) & 0x1f;
11392           shamt = (oc >> 6) & 0x1f;
11393           imm = oc;
11394           for (args = p->args;; ++args)
11395             {
11396               switch (*args)
11397                 {
11398                 case '\0':
11399                   printf ("\n");
11400                   break;
11401
11402                 case ',':
11403                 case '(':
11404                 case ')':
11405                   printf ("%c", *args);
11406                   continue;
11407
11408                 case 'r':
11409                   assert (treg == sreg);
11410                   printf ("$%d,$%d", treg, sreg);
11411                   continue;
11412
11413                 case 'd':
11414                 case 'G':
11415                   printf ("$%d", dreg);
11416                   continue;
11417
11418                 case 't':
11419                 case 'E':
11420                   printf ("$%d", treg);
11421                   continue;
11422
11423                 case 'k':
11424                   printf ("0x%x", treg);
11425                   continue;
11426
11427                 case 'b':
11428                 case 's':
11429                   printf ("$%d", sreg);
11430                   continue;
11431
11432                 case 'a':
11433                   printf ("0x%08lx", oc & 0x1ffffff);
11434                   continue;
11435
11436                 case 'i':
11437                 case 'j':
11438                 case 'o':
11439                 case 'u':
11440                   printf ("%d", imm);
11441                   continue;
11442
11443                 case '<':
11444                 case '>':
11445                   printf ("$%d", shamt);
11446                   continue;
11447
11448                 default:
11449                   internalError ();
11450                 }
11451               break;
11452             }
11453           return;
11454         }
11455     }
11456   printf (_("%08lx  UNDEFINED\n"), oc);
11457 }
11458 #endif
11459
11460 static symbolS *
11461 get_symbol ()
11462 {
11463   int c;
11464   char *name;
11465   symbolS *p;
11466
11467   name = input_line_pointer;
11468   c = get_symbol_end ();
11469   p = (symbolS *) symbol_find_or_make (name);
11470   *input_line_pointer = c;
11471   return p;
11472 }
11473
11474 /* Align the current frag to a given power of two.  The MIPS assembler
11475    also automatically adjusts any preceding label.  */
11476
11477 static void
11478 mips_align (to, fill, label)
11479      int to;
11480      int fill;
11481      symbolS *label;
11482 {
11483   mips_emit_delays (false);
11484   frag_align (to, fill, 0);
11485   record_alignment (now_seg, to);
11486   if (label != NULL)
11487     {
11488       assert (S_GET_SEGMENT (label) == now_seg);
11489       symbol_set_frag (label, frag_now);
11490       S_SET_VALUE (label, (valueT) frag_now_fix ());
11491     }
11492 }
11493
11494 /* Align to a given power of two.  .align 0 turns off the automatic
11495    alignment used by the data creating pseudo-ops.  */
11496
11497 static void
11498 s_align (x)
11499      int x ATTRIBUTE_UNUSED;
11500 {
11501   register int temp;
11502   register long temp_fill;
11503   long max_alignment = 15;
11504
11505   /*
11506
11507     o  Note that the assembler pulls down any immediately preceeding label
11508        to the aligned address.
11509     o  It's not documented but auto alignment is reinstated by
11510        a .align pseudo instruction.
11511     o  Note also that after auto alignment is turned off the mips assembler
11512        issues an error on attempt to assemble an improperly aligned data item.
11513        We don't.
11514
11515     */
11516
11517   temp = get_absolute_expression ();
11518   if (temp > max_alignment)
11519     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11520   else if (temp < 0)
11521     {
11522       as_warn (_("Alignment negative: 0 assumed."));
11523       temp = 0;
11524     }
11525   if (*input_line_pointer == ',')
11526     {
11527       ++input_line_pointer;
11528       temp_fill = get_absolute_expression ();
11529     }
11530   else
11531     temp_fill = 0;
11532   if (temp)
11533     {
11534       auto_align = 1;
11535       mips_align (temp, (int) temp_fill,
11536                   insn_labels != NULL ? insn_labels->label : NULL);
11537     }
11538   else
11539     {
11540       auto_align = 0;
11541     }
11542
11543   demand_empty_rest_of_line ();
11544 }
11545
11546 void
11547 mips_flush_pending_output ()
11548 {
11549   mips_emit_delays (false);
11550   mips_clear_insn_labels ();
11551 }
11552
11553 static void
11554 s_change_sec (sec)
11555      int sec;
11556 {
11557   segT seg;
11558
11559   /* When generating embedded PIC code, we only use the .text, .lit8,
11560      .sdata and .sbss sections.  We change the .data and .rdata
11561      pseudo-ops to use .sdata.  */
11562   if (mips_pic == EMBEDDED_PIC
11563       && (sec == 'd' || sec == 'r'))
11564     sec = 's';
11565
11566 #ifdef OBJ_ELF
11567   /* The ELF backend needs to know that we are changing sections, so
11568      that .previous works correctly.  We could do something like check
11569      for an obj_section_change_hook macro, but that might be confusing
11570      as it would not be appropriate to use it in the section changing
11571      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11572      This should be cleaner, somehow.  */
11573   obj_elf_section_change_hook ();
11574 #endif
11575
11576   mips_emit_delays (false);
11577   switch (sec)
11578     {
11579     case 't':
11580       s_text (0);
11581       break;
11582     case 'd':
11583       s_data (0);
11584       break;
11585     case 'b':
11586       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11587       demand_empty_rest_of_line ();
11588       break;
11589
11590     case 'r':
11591       if (USE_GLOBAL_POINTER_OPT)
11592         {
11593           seg = subseg_new (RDATA_SECTION_NAME,
11594                             (subsegT) get_absolute_expression ());
11595           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11596             {
11597               bfd_set_section_flags (stdoutput, seg,
11598                                      (SEC_ALLOC
11599                                       | SEC_LOAD
11600                                       | SEC_READONLY
11601                                       | SEC_RELOC
11602                                       | SEC_DATA));
11603               if (strcmp (TARGET_OS, "elf") != 0)
11604                 record_alignment (seg, 4);
11605             }
11606           demand_empty_rest_of_line ();
11607         }
11608       else
11609         {
11610           as_bad (_("No read only data section in this object file format"));
11611           demand_empty_rest_of_line ();
11612           return;
11613         }
11614       break;
11615
11616     case 's':
11617       if (USE_GLOBAL_POINTER_OPT)
11618         {
11619           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11620           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11621             {
11622               bfd_set_section_flags (stdoutput, seg,
11623                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11624                                      | SEC_DATA);
11625               if (strcmp (TARGET_OS, "elf") != 0)
11626                 record_alignment (seg, 4);
11627             }
11628           demand_empty_rest_of_line ();
11629           break;
11630         }
11631       else
11632         {
11633           as_bad (_("Global pointers not supported; recompile -G 0"));
11634           demand_empty_rest_of_line ();
11635           return;
11636         }
11637     }
11638
11639   auto_align = 1;
11640 }
11641   
11642 void
11643 s_change_section (ignore)
11644      int ignore ATTRIBUTE_UNUSED;
11645 {
11646 #ifdef OBJ_ELF
11647   char *section_name;
11648   char c;
11649   char next_c;
11650   int section_type;
11651   int section_flag;
11652   int section_entry_size;
11653   int section_alignment;
11654   
11655   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11656     return;
11657
11658   section_name = input_line_pointer;
11659   c = get_symbol_end ();
11660   if (c)
11661     next_c = *(input_line_pointer + 1);
11662
11663   /* Do we have .section Name<,"flags">?  */
11664   if (c != ',' || (c == ',' && next_c == '"'))
11665     {
11666       /* just after name is now '\0'.  */
11667       *input_line_pointer = c;
11668       input_line_pointer = section_name;
11669       obj_elf_section (ignore);
11670       return;
11671     }
11672   input_line_pointer++;
11673
11674   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11675   if (c == ',')
11676     section_type = get_absolute_expression ();
11677   else
11678     section_type = 0;
11679   if (*input_line_pointer++ == ',')
11680     section_flag = get_absolute_expression ();
11681   else
11682     section_flag = 0;
11683   if (*input_line_pointer++ == ',')
11684     section_entry_size = get_absolute_expression ();
11685   else
11686     section_entry_size = 0;
11687   if (*input_line_pointer++ == ',')
11688     section_alignment = get_absolute_expression ();
11689   else
11690     section_alignment = 0;
11691
11692   section_name = xstrdup (section_name);
11693
11694   obj_elf_change_section (section_name, section_type, section_flag,
11695                           section_entry_size, 0, 0, 0);
11696
11697   if (now_seg->name != section_name)
11698     free (section_name);
11699 #endif /* OBJ_ELF */
11700 }
11701
11702 void
11703 mips_enable_auto_align ()
11704 {
11705   auto_align = 1;
11706 }
11707
11708 static void
11709 s_cons (log_size)
11710      int log_size;
11711 {
11712   symbolS *label;
11713
11714   label = insn_labels != NULL ? insn_labels->label : NULL;
11715   mips_emit_delays (false);
11716   if (log_size > 0 && auto_align)
11717     mips_align (log_size, 0, label);
11718   mips_clear_insn_labels ();
11719   cons (1 << log_size);
11720 }
11721
11722 static void
11723 s_float_cons (type)
11724      int type;
11725 {
11726   symbolS *label;
11727
11728   label = insn_labels != NULL ? insn_labels->label : NULL;
11729
11730   mips_emit_delays (false);
11731
11732   if (auto_align)
11733     {
11734       if (type == 'd')
11735         mips_align (3, 0, label);
11736       else
11737         mips_align (2, 0, label);
11738     }
11739
11740   mips_clear_insn_labels ();
11741
11742   float_cons (type);
11743 }
11744
11745 /* Handle .globl.  We need to override it because on Irix 5 you are
11746    permitted to say
11747        .globl foo .text
11748    where foo is an undefined symbol, to mean that foo should be
11749    considered to be the address of a function.  */
11750
11751 static void
11752 s_mips_globl (x)
11753      int x ATTRIBUTE_UNUSED;
11754 {
11755   char *name;
11756   int c;
11757   symbolS *symbolP;
11758   flagword flag;
11759
11760   name = input_line_pointer;
11761   c = get_symbol_end ();
11762   symbolP = symbol_find_or_make (name);
11763   *input_line_pointer = c;
11764   SKIP_WHITESPACE ();
11765
11766   /* On Irix 5, every global symbol that is not explicitly labelled as
11767      being a function is apparently labelled as being an object.  */
11768   flag = BSF_OBJECT;
11769
11770   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11771     {
11772       char *secname;
11773       asection *sec;
11774
11775       secname = input_line_pointer;
11776       c = get_symbol_end ();
11777       sec = bfd_get_section_by_name (stdoutput, secname);
11778       if (sec == NULL)
11779         as_bad (_("%s: no such section"), secname);
11780       *input_line_pointer = c;
11781
11782       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11783         flag = BSF_FUNCTION;
11784     }
11785
11786   symbol_get_bfdsym (symbolP)->flags |= flag;
11787
11788   S_SET_EXTERNAL (symbolP);
11789   demand_empty_rest_of_line ();
11790 }
11791
11792 static void
11793 s_option (x)
11794      int x ATTRIBUTE_UNUSED;
11795 {
11796   char *opt;
11797   char c;
11798
11799   opt = input_line_pointer;
11800   c = get_symbol_end ();
11801
11802   if (*opt == 'O')
11803     {
11804       /* FIXME: What does this mean?  */
11805     }
11806   else if (strncmp (opt, "pic", 3) == 0)
11807     {
11808       int i;
11809
11810       i = atoi (opt + 3);
11811       if (i == 0)
11812         mips_pic = NO_PIC;
11813       else if (i == 2)
11814         mips_pic = SVR4_PIC;
11815       else
11816         as_bad (_(".option pic%d not supported"), i);
11817
11818       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11819         {
11820           if (g_switch_seen && g_switch_value != 0)
11821             as_warn (_("-G may not be used with SVR4 PIC code"));
11822           g_switch_value = 0;
11823           bfd_set_gp_size (stdoutput, 0);
11824         }
11825     }
11826   else
11827     as_warn (_("Unrecognized option \"%s\""), opt);
11828
11829   *input_line_pointer = c;
11830   demand_empty_rest_of_line ();
11831 }
11832
11833 /* This structure is used to hold a stack of .set values.  */
11834
11835 struct mips_option_stack
11836 {
11837   struct mips_option_stack *next;
11838   struct mips_set_options options;
11839 };
11840
11841 static struct mips_option_stack *mips_opts_stack;
11842
11843 /* Handle the .set pseudo-op.  */
11844
11845 static void
11846 s_mipsset (x)
11847      int x ATTRIBUTE_UNUSED;
11848 {
11849   char *name = input_line_pointer, ch;
11850
11851   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11852     ++input_line_pointer;
11853   ch = *input_line_pointer;
11854   *input_line_pointer = '\0';
11855
11856   if (strcmp (name, "reorder") == 0)
11857     {
11858       if (mips_opts.noreorder && prev_nop_frag != NULL)
11859         {
11860           /* If we still have pending nops, we can discard them.  The
11861              usual nop handling will insert any that are still
11862              needed.  */
11863           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11864                                     * (mips_opts.mips16 ? 2 : 4));
11865           prev_nop_frag = NULL;
11866         }
11867       mips_opts.noreorder = 0;
11868     }
11869   else if (strcmp (name, "noreorder") == 0)
11870     {
11871       mips_emit_delays (true);
11872       mips_opts.noreorder = 1;
11873       mips_any_noreorder = 1;
11874     }
11875   else if (strcmp (name, "at") == 0)
11876     {
11877       mips_opts.noat = 0;
11878     }
11879   else if (strcmp (name, "noat") == 0)
11880     {
11881       mips_opts.noat = 1;
11882     }
11883   else if (strcmp (name, "macro") == 0)
11884     {
11885       mips_opts.warn_about_macros = 0;
11886     }
11887   else if (strcmp (name, "nomacro") == 0)
11888     {
11889       if (mips_opts.noreorder == 0)
11890         as_bad (_("`noreorder' must be set before `nomacro'"));
11891       mips_opts.warn_about_macros = 1;
11892     }
11893   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11894     {
11895       mips_opts.nomove = 0;
11896     }
11897   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11898     {
11899       mips_opts.nomove = 1;
11900     }
11901   else if (strcmp (name, "bopt") == 0)
11902     {
11903       mips_opts.nobopt = 0;
11904     }
11905   else if (strcmp (name, "nobopt") == 0)
11906     {
11907       mips_opts.nobopt = 1;
11908     }
11909   else if (strcmp (name, "mips16") == 0
11910            || strcmp (name, "MIPS-16") == 0)
11911     mips_opts.mips16 = 1;
11912   else if (strcmp (name, "nomips16") == 0
11913            || strcmp (name, "noMIPS-16") == 0)
11914     mips_opts.mips16 = 0;
11915   else if (strcmp (name, "mips3d") == 0)
11916     mips_opts.ase_mips3d = 1;
11917   else if (strcmp (name, "nomips3d") == 0)
11918     mips_opts.ase_mips3d = 0;
11919   else if (strcmp (name, "mdmx") == 0)
11920     mips_opts.ase_mdmx = 1;
11921   else if (strcmp (name, "nomdmx") == 0)
11922     mips_opts.ase_mdmx = 0;
11923   else if (strncmp (name, "mips", 4) == 0)
11924     {
11925       int isa;
11926
11927       /* Permit the user to change the ISA on the fly.  Needless to
11928          say, misuse can cause serious problems.  */
11929       isa = atoi (name + 4);
11930       switch (isa)
11931         {
11932         case  0:
11933           mips_opts.gp32 = file_mips_gp32;
11934           mips_opts.fp32 = file_mips_fp32;
11935           break;
11936         case  1:
11937         case  2:
11938         case 32:
11939           mips_opts.gp32 = 1;
11940           mips_opts.fp32 = 1;
11941           break;
11942         case  3:
11943         case  4:
11944         case  5:
11945         case 64:
11946           mips_opts.gp32 = 0;
11947           mips_opts.fp32 = 0;
11948           break;
11949         default:
11950           as_bad (_("unknown ISA level %s"), name + 4);
11951           break;
11952         }
11953
11954       switch (isa)
11955         {
11956         case  0: mips_opts.isa = file_mips_isa;   break;
11957         case  1: mips_opts.isa = ISA_MIPS1;       break;
11958         case  2: mips_opts.isa = ISA_MIPS2;       break;
11959         case  3: mips_opts.isa = ISA_MIPS3;       break;
11960         case  4: mips_opts.isa = ISA_MIPS4;       break;
11961         case  5: mips_opts.isa = ISA_MIPS5;       break;
11962         case 32: mips_opts.isa = ISA_MIPS32;      break;
11963         case 64: mips_opts.isa = ISA_MIPS64;      break;
11964         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11965         }
11966     }
11967   else if (strcmp (name, "autoextend") == 0)
11968     mips_opts.noautoextend = 0;
11969   else if (strcmp (name, "noautoextend") == 0)
11970     mips_opts.noautoextend = 1;
11971   else if (strcmp (name, "push") == 0)
11972     {
11973       struct mips_option_stack *s;
11974
11975       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11976       s->next = mips_opts_stack;
11977       s->options = mips_opts;
11978       mips_opts_stack = s;
11979     }
11980   else if (strcmp (name, "pop") == 0)
11981     {
11982       struct mips_option_stack *s;
11983
11984       s = mips_opts_stack;
11985       if (s == NULL)
11986         as_bad (_(".set pop with no .set push"));
11987       else
11988         {
11989           /* If we're changing the reorder mode we need to handle
11990              delay slots correctly.  */
11991           if (s->options.noreorder && ! mips_opts.noreorder)
11992             mips_emit_delays (true);
11993           else if (! s->options.noreorder && mips_opts.noreorder)
11994             {
11995               if (prev_nop_frag != NULL)
11996                 {
11997                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11998                                             * (mips_opts.mips16 ? 2 : 4));
11999                   prev_nop_frag = NULL;
12000                 }
12001             }
12002
12003           mips_opts = s->options;
12004           mips_opts_stack = s->next;
12005           free (s);
12006         }
12007     }
12008   else
12009     {
12010       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12011     }
12012   *input_line_pointer = ch;
12013   demand_empty_rest_of_line ();
12014 }
12015
12016 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12017    .option pic2.  It means to generate SVR4 PIC calls.  */
12018
12019 static void
12020 s_abicalls (ignore)
12021      int ignore ATTRIBUTE_UNUSED;
12022 {
12023   mips_pic = SVR4_PIC;
12024   if (USE_GLOBAL_POINTER_OPT)
12025     {
12026       if (g_switch_seen && g_switch_value != 0)
12027         as_warn (_("-G may not be used with SVR4 PIC code"));
12028       g_switch_value = 0;
12029     }
12030   bfd_set_gp_size (stdoutput, 0);
12031   demand_empty_rest_of_line ();
12032 }
12033
12034 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12035    PIC code.  It sets the $gp register for the function based on the
12036    function address, which is in the register named in the argument.
12037    This uses a relocation against _gp_disp, which is handled specially
12038    by the linker.  The result is:
12039         lui     $gp,%hi(_gp_disp)
12040         addiu   $gp,$gp,%lo(_gp_disp)
12041         addu    $gp,$gp,.cpload argument
12042    The .cpload argument is normally $25 == $t9.  */
12043
12044 static void
12045 s_cpload (ignore)
12046      int ignore ATTRIBUTE_UNUSED;
12047 {
12048   expressionS ex;
12049   int icnt = 0;
12050
12051   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12052      .cpload is ignored.  */
12053   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12054     {
12055       s_ignore (0);
12056       return;
12057     }
12058
12059   /* .cpload should be in a .set noreorder section.  */
12060   if (mips_opts.noreorder == 0)
12061     as_warn (_(".cpload not in noreorder section"));
12062
12063   ex.X_op = O_symbol;
12064   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12065   ex.X_op_symbol = NULL;
12066   ex.X_add_number = 0;
12067
12068   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12069   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12070
12071   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12072   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12073                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12074
12075   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12076                mips_gp_register, mips_gp_register, tc_get_register (0));
12077
12078   demand_empty_rest_of_line ();
12079 }
12080
12081 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12082      .cpsetup $reg1, offset|$reg2, label
12083
12084    If offset is given, this results in:
12085      sd         $gp, offset($sp)
12086      lui        $gp, %hi(%neg(%gp_rel(label)))
12087      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12088      daddu      $gp, $gp, $reg1
12089
12090    If $reg2 is given, this results in:
12091      daddu      $reg2, $gp, $0
12092      lui        $gp, %hi(%neg(%gp_rel(label)))
12093      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12094      daddu      $gp, $gp, $reg1
12095    $reg1 is normally $25 == $t9.  */
12096 static void
12097 s_cpsetup (ignore)
12098      int ignore ATTRIBUTE_UNUSED;
12099 {
12100   expressionS ex_off;
12101   expressionS ex_sym;
12102   int reg1;
12103   int icnt = 0;
12104   char *f;
12105
12106   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12107      We also need NewABI support.  */
12108   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12109     {
12110       s_ignore (0);
12111       return;
12112     }
12113
12114   reg1 = tc_get_register (0);
12115   SKIP_WHITESPACE ();
12116   if (*input_line_pointer != ',')
12117     {
12118       as_bad (_("missing argument separator ',' for .cpsetup"));
12119       return;
12120     }
12121   else
12122     ++input_line_pointer;
12123   SKIP_WHITESPACE ();
12124   if (*input_line_pointer == '$')
12125     {
12126       mips_cpreturn_register = tc_get_register (0);
12127       mips_cpreturn_offset = -1;
12128     }
12129   else
12130     {
12131       mips_cpreturn_offset = get_absolute_expression ();
12132       mips_cpreturn_register = -1;
12133     }
12134   SKIP_WHITESPACE ();
12135   if (*input_line_pointer != ',')
12136     {
12137       as_bad (_("missing argument separator ',' for .cpsetup"));
12138       return;
12139     }
12140   else
12141     ++input_line_pointer;
12142   SKIP_WHITESPACE ();
12143   expression (&ex_sym);
12144
12145   if (mips_cpreturn_register == -1)
12146     {
12147       ex_off.X_op = O_constant;
12148       ex_off.X_add_symbol = NULL;
12149       ex_off.X_op_symbol = NULL;
12150       ex_off.X_add_number = mips_cpreturn_offset;
12151
12152       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12153                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12154     }
12155   else
12156     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12157                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12158
12159   /* Ensure there's room for the next two instructions, so that `f'
12160      doesn't end up with an address in the wrong frag.  */
12161   frag_grow (8);
12162   f = frag_more (0);
12163   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12164                (int) BFD_RELOC_GPREL16);
12165   fix_new (frag_now, f - frag_now->fr_literal,
12166            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12167   fix_new (frag_now, f - frag_now->fr_literal,
12168            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12169
12170   f = frag_more (0);
12171   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12172                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12173   fix_new (frag_now, f - frag_now->fr_literal,
12174            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12175   fix_new (frag_now, f - frag_now->fr_literal,
12176            0, NULL, 0, 0, BFD_RELOC_LO16);
12177
12178   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12179                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12180                mips_gp_register, mips_gp_register, reg1);
12181
12182   demand_empty_rest_of_line ();
12183 }
12184
12185 static void
12186 s_cplocal (ignore)
12187      int ignore ATTRIBUTE_UNUSED;
12188 {
12189   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12190    .cplocal is ignored.  */
12191   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12192     {
12193       s_ignore (0);
12194       return;
12195     }
12196
12197   mips_gp_register = tc_get_register (0);
12198   demand_empty_rest_of_line ();
12199 }
12200
12201 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12202    offset from $sp.  The offset is remembered, and after making a PIC
12203    call $gp is restored from that location.  */
12204
12205 static void
12206 s_cprestore (ignore)
12207      int ignore ATTRIBUTE_UNUSED;
12208 {
12209   expressionS ex;
12210   int icnt = 0;
12211
12212   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12213      .cprestore is ignored.  */
12214   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12215     {
12216       s_ignore (0);
12217       return;
12218     }
12219
12220   mips_cprestore_offset = get_absolute_expression ();
12221   mips_cprestore_valid = 1;
12222
12223   ex.X_op = O_constant;
12224   ex.X_add_symbol = NULL;
12225   ex.X_op_symbol = NULL;
12226   ex.X_add_number = mips_cprestore_offset;
12227
12228   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12229                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12230                                 mips_gp_register, SP);
12231
12232   demand_empty_rest_of_line ();
12233 }
12234
12235 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12236    was given in the preceeding .gpsetup, it results in:
12237      ld         $gp, offset($sp)
12238
12239    If a register $reg2 was given there, it results in:
12240      daddiu     $gp, $gp, $reg2
12241  */
12242 static void
12243 s_cpreturn (ignore)
12244      int ignore ATTRIBUTE_UNUSED;
12245 {
12246   expressionS ex;
12247   int icnt = 0;
12248
12249   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12250      We also need NewABI support.  */
12251   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12252     {
12253       s_ignore (0);
12254       return;
12255     }
12256
12257   if (mips_cpreturn_register == -1)
12258     {
12259       ex.X_op = O_constant;
12260       ex.X_add_symbol = NULL;
12261       ex.X_op_symbol = NULL;
12262       ex.X_add_number = mips_cpreturn_offset;
12263
12264       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12265                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12266     }
12267   else
12268     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12269                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12270
12271   demand_empty_rest_of_line ();
12272 }
12273
12274 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12275    code.  It sets the offset to use in gp_rel relocations.  */
12276
12277 static void
12278 s_gpvalue (ignore)
12279      int ignore ATTRIBUTE_UNUSED;
12280 {
12281   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12282      We also need NewABI support.  */
12283   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12284     {
12285       s_ignore (0);
12286       return;
12287     }
12288
12289   mips_gprel_offset = get_absolute_expression ();
12290
12291   demand_empty_rest_of_line ();
12292 }
12293
12294 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12295    code.  It generates a 32 bit GP relative reloc.  */
12296
12297 static void
12298 s_gpword (ignore)
12299      int ignore ATTRIBUTE_UNUSED;
12300 {
12301   symbolS *label;
12302   expressionS ex;
12303   char *p;
12304
12305   /* When not generating PIC code, this is treated as .word.  */
12306   if (mips_pic != SVR4_PIC)
12307     {
12308       s_cons (2);
12309       return;
12310     }
12311
12312   label = insn_labels != NULL ? insn_labels->label : NULL;
12313   mips_emit_delays (true);
12314   if (auto_align)
12315     mips_align (2, 0, label);
12316   mips_clear_insn_labels ();
12317
12318   expression (&ex);
12319
12320   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12321     {
12322       as_bad (_("Unsupported use of .gpword"));
12323       ignore_rest_of_line ();
12324     }
12325
12326   p = frag_more (4);
12327   md_number_to_chars (p, (valueT) 0, 4);
12328   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
12329                BFD_RELOC_GPREL32);
12330
12331   demand_empty_rest_of_line ();
12332 }
12333
12334 static void
12335 s_gpdword (ignore)
12336      int ignore ATTRIBUTE_UNUSED;
12337 {
12338   symbolS *label;
12339   expressionS ex;
12340   char *p;
12341
12342   /* When not generating PIC code, this is treated as .dword.  */
12343   if (mips_pic != SVR4_PIC)
12344     {
12345       s_cons (3);
12346       return;
12347     }
12348
12349   label = insn_labels != NULL ? insn_labels->label : NULL;
12350   mips_emit_delays (true);
12351   if (auto_align)
12352     mips_align (3, 0, label);
12353   mips_clear_insn_labels ();
12354
12355   expression (&ex);
12356
12357   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12358     {
12359       as_bad (_("Unsupported use of .gpdword"));
12360       ignore_rest_of_line ();
12361     }
12362
12363   p = frag_more (8);
12364   md_number_to_chars (p, (valueT) 0, 8);
12365   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12366                BFD_RELOC_GPREL32);
12367
12368   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12369   ex.X_op = O_absent;
12370   ex.X_add_symbol = 0;
12371   ex.X_add_number = 0;
12372   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, false,
12373                BFD_RELOC_64);
12374
12375   demand_empty_rest_of_line ();
12376 }
12377
12378 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12379    tables in SVR4 PIC code.  */
12380
12381 static void
12382 s_cpadd (ignore)
12383      int ignore ATTRIBUTE_UNUSED;
12384 {
12385   int icnt = 0;
12386   int reg;
12387
12388   /* This is ignored when not generating SVR4 PIC code.  */
12389   if (mips_pic != SVR4_PIC)
12390     {
12391       s_ignore (0);
12392       return;
12393     }
12394
12395   /* Add $gp to the register named as an argument.  */
12396   reg = tc_get_register (0);
12397   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12398                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12399                "d,v,t", reg, reg, mips_gp_register);
12400
12401   demand_empty_rest_of_line ();
12402 }
12403
12404 /* Handle the .insn pseudo-op.  This marks instruction labels in
12405    mips16 mode.  This permits the linker to handle them specially,
12406    such as generating jalx instructions when needed.  We also make
12407    them odd for the duration of the assembly, in order to generate the
12408    right sort of code.  We will make them even in the adjust_symtab
12409    routine, while leaving them marked.  This is convenient for the
12410    debugger and the disassembler.  The linker knows to make them odd
12411    again.  */
12412
12413 static void
12414 s_insn (ignore)
12415      int ignore ATTRIBUTE_UNUSED;
12416 {
12417   mips16_mark_labels ();
12418
12419   demand_empty_rest_of_line ();
12420 }
12421
12422 /* Handle a .stabn directive.  We need these in order to mark a label
12423    as being a mips16 text label correctly.  Sometimes the compiler
12424    will emit a label, followed by a .stabn, and then switch sections.
12425    If the label and .stabn are in mips16 mode, then the label is
12426    really a mips16 text label.  */
12427
12428 static void
12429 s_mips_stab (type)
12430      int type;
12431 {
12432   if (type == 'n')
12433     mips16_mark_labels ();
12434
12435   s_stab (type);
12436 }
12437
12438 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12439  */
12440
12441 static void
12442 s_mips_weakext (ignore)
12443      int ignore ATTRIBUTE_UNUSED;
12444 {
12445   char *name;
12446   int c;
12447   symbolS *symbolP;
12448   expressionS exp;
12449
12450   name = input_line_pointer;
12451   c = get_symbol_end ();
12452   symbolP = symbol_find_or_make (name);
12453   S_SET_WEAK (symbolP);
12454   *input_line_pointer = c;
12455
12456   SKIP_WHITESPACE ();
12457
12458   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12459     {
12460       if (S_IS_DEFINED (symbolP))
12461         {
12462           as_bad ("ignoring attempt to redefine symbol %s",
12463                   S_GET_NAME (symbolP));
12464           ignore_rest_of_line ();
12465           return;
12466         }
12467
12468       if (*input_line_pointer == ',')
12469         {
12470           ++input_line_pointer;
12471           SKIP_WHITESPACE ();
12472         }
12473
12474       expression (&exp);
12475       if (exp.X_op != O_symbol)
12476         {
12477           as_bad ("bad .weakext directive");
12478           ignore_rest_of_line ();
12479           return;
12480         }
12481       symbol_set_value_expression (symbolP, &exp);
12482     }
12483
12484   demand_empty_rest_of_line ();
12485 }
12486
12487 /* Parse a register string into a number.  Called from the ECOFF code
12488    to parse .frame.  The argument is non-zero if this is the frame
12489    register, so that we can record it in mips_frame_reg.  */
12490
12491 int
12492 tc_get_register (frame)
12493      int frame;
12494 {
12495   int reg;
12496
12497   SKIP_WHITESPACE ();
12498   if (*input_line_pointer++ != '$')
12499     {
12500       as_warn (_("expected `$'"));
12501       reg = ZERO;
12502     }
12503   else if (ISDIGIT (*input_line_pointer))
12504     {
12505       reg = get_absolute_expression ();
12506       if (reg < 0 || reg >= 32)
12507         {
12508           as_warn (_("Bad register number"));
12509           reg = ZERO;
12510         }
12511     }
12512   else
12513     {
12514       if (strncmp (input_line_pointer, "ra", 2) == 0)
12515         {
12516           reg = RA;
12517           input_line_pointer += 2;
12518         }
12519       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12520         {
12521           reg = FP;
12522           input_line_pointer += 2;
12523         }
12524       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12525         {
12526           reg = SP;
12527           input_line_pointer += 2;
12528         }
12529       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12530         {
12531           reg = GP;
12532           input_line_pointer += 2;
12533         }
12534       else if (strncmp (input_line_pointer, "at", 2) == 0)
12535         {
12536           reg = AT;
12537           input_line_pointer += 2;
12538         }
12539       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12540         {
12541           reg = KT0;
12542           input_line_pointer += 3;
12543         }
12544       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12545         {
12546           reg = KT1;
12547           input_line_pointer += 3;
12548         }
12549       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12550         {
12551           reg = ZERO;
12552           input_line_pointer += 4;
12553         }
12554       else
12555         {
12556           as_warn (_("Unrecognized register name"));
12557           reg = ZERO;
12558           while (ISALNUM(*input_line_pointer))
12559            input_line_pointer++;
12560         }
12561     }
12562   if (frame)
12563     {
12564       mips_frame_reg = reg != 0 ? reg : SP;
12565       mips_frame_reg_valid = 1;
12566       mips_cprestore_valid = 0;
12567     }
12568   return reg;
12569 }
12570
12571 valueT
12572 md_section_align (seg, addr)
12573      asection *seg;
12574      valueT addr;
12575 {
12576   int align = bfd_get_section_alignment (stdoutput, seg);
12577
12578 #ifdef OBJ_ELF
12579   /* We don't need to align ELF sections to the full alignment.
12580      However, Irix 5 may prefer that we align them at least to a 16
12581      byte boundary.  We don't bother to align the sections if we are
12582      targeted for an embedded system.  */
12583   if (strcmp (TARGET_OS, "elf") == 0)
12584     return addr;
12585   if (align > 4)
12586     align = 4;
12587 #endif
12588
12589   return ((addr + (1 << align) - 1) & (-1 << align));
12590 }
12591
12592 /* Utility routine, called from above as well.  If called while the
12593    input file is still being read, it's only an approximation.  (For
12594    example, a symbol may later become defined which appeared to be
12595    undefined earlier.)  */
12596
12597 static int
12598 nopic_need_relax (sym, before_relaxing)
12599      symbolS *sym;
12600      int before_relaxing;
12601 {
12602   if (sym == 0)
12603     return 0;
12604
12605   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12606     {
12607       const char *symname;
12608       int change;
12609
12610       /* Find out whether this symbol can be referenced off the $gp
12611          register.  It can be if it is smaller than the -G size or if
12612          it is in the .sdata or .sbss section.  Certain symbols can
12613          not be referenced off the $gp, although it appears as though
12614          they can.  */
12615       symname = S_GET_NAME (sym);
12616       if (symname != (const char *) NULL
12617           && (strcmp (symname, "eprol") == 0
12618               || strcmp (symname, "etext") == 0
12619               || strcmp (symname, "_gp") == 0
12620               || strcmp (symname, "edata") == 0
12621               || strcmp (symname, "_fbss") == 0
12622               || strcmp (symname, "_fdata") == 0
12623               || strcmp (symname, "_ftext") == 0
12624               || strcmp (symname, "end") == 0
12625               || strcmp (symname, "_gp_disp") == 0))
12626         change = 1;
12627       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12628                && (0
12629 #ifndef NO_ECOFF_DEBUGGING
12630                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12631                        && (symbol_get_obj (sym)->ecoff_extern_size
12632                            <= g_switch_value))
12633 #endif
12634                    /* We must defer this decision until after the whole
12635                       file has been read, since there might be a .extern
12636                       after the first use of this symbol.  */
12637                    || (before_relaxing
12638 #ifndef NO_ECOFF_DEBUGGING
12639                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12640 #endif
12641                        && S_GET_VALUE (sym) == 0)
12642                    || (S_GET_VALUE (sym) != 0
12643                        && S_GET_VALUE (sym) <= g_switch_value)))
12644         change = 0;
12645       else
12646         {
12647           const char *segname;
12648
12649           segname = segment_name (S_GET_SEGMENT (sym));
12650           assert (strcmp (segname, ".lit8") != 0
12651                   && strcmp (segname, ".lit4") != 0);
12652           change = (strcmp (segname, ".sdata") != 0
12653                     && strcmp (segname, ".sbss") != 0
12654                     && strncmp (segname, ".sdata.", 7) != 0
12655                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12656         }
12657       return change;
12658     }
12659   else
12660     /* We are not optimizing for the $gp register.  */
12661     return 1;
12662 }
12663
12664 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12665    extended opcode.  SEC is the section the frag is in.  */
12666
12667 static int
12668 mips16_extended_frag (fragp, sec, stretch)
12669      fragS *fragp;
12670      asection *sec;
12671      long stretch;
12672 {
12673   int type;
12674   register const struct mips16_immed_operand *op;
12675   offsetT val;
12676   int mintiny, maxtiny;
12677   segT symsec;
12678   fragS *sym_frag;
12679
12680   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12681     return 0;
12682   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12683     return 1;
12684
12685   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12686   op = mips16_immed_operands;
12687   while (op->type != type)
12688     {
12689       ++op;
12690       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12691     }
12692
12693   if (op->unsp)
12694     {
12695       if (type == '<' || type == '>' || type == '[' || type == ']')
12696         {
12697           mintiny = 1;
12698           maxtiny = 1 << op->nbits;
12699         }
12700       else
12701         {
12702           mintiny = 0;
12703           maxtiny = (1 << op->nbits) - 1;
12704         }
12705     }
12706   else
12707     {
12708       mintiny = - (1 << (op->nbits - 1));
12709       maxtiny = (1 << (op->nbits - 1)) - 1;
12710     }
12711
12712   sym_frag = symbol_get_frag (fragp->fr_symbol);
12713   val = S_GET_VALUE (fragp->fr_symbol);
12714   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12715
12716   if (op->pcrel)
12717     {
12718       addressT addr;
12719
12720       /* We won't have the section when we are called from
12721          mips_relax_frag.  However, we will always have been called
12722          from md_estimate_size_before_relax first.  If this is a
12723          branch to a different section, we mark it as such.  If SEC is
12724          NULL, and the frag is not marked, then it must be a branch to
12725          the same section.  */
12726       if (sec == NULL)
12727         {
12728           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12729             return 1;
12730         }
12731       else
12732         {
12733           /* Must have been called from md_estimate_size_before_relax.  */
12734           if (symsec != sec)
12735             {
12736               fragp->fr_subtype =
12737                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12738
12739               /* FIXME: We should support this, and let the linker
12740                  catch branches and loads that are out of range.  */
12741               as_bad_where (fragp->fr_file, fragp->fr_line,
12742                             _("unsupported PC relative reference to different section"));
12743
12744               return 1;
12745             }
12746           if (fragp != sym_frag && sym_frag->fr_address == 0)
12747             /* Assume non-extended on the first relaxation pass.
12748                The address we have calculated will be bogus if this is
12749                a forward branch to another frag, as the forward frag
12750                will have fr_address == 0.  */
12751             return 0;
12752         }
12753
12754       /* In this case, we know for sure that the symbol fragment is in
12755          the same section.  If the relax_marker of the symbol fragment
12756          differs from the relax_marker of this fragment, we have not
12757          yet adjusted the symbol fragment fr_address.  We want to add
12758          in STRETCH in order to get a better estimate of the address.
12759          This particularly matters because of the shift bits.  */
12760       if (stretch != 0
12761           && sym_frag->relax_marker != fragp->relax_marker)
12762         {
12763           fragS *f;
12764
12765           /* Adjust stretch for any alignment frag.  Note that if have
12766              been expanding the earlier code, the symbol may be
12767              defined in what appears to be an earlier frag.  FIXME:
12768              This doesn't handle the fr_subtype field, which specifies
12769              a maximum number of bytes to skip when doing an
12770              alignment.  */
12771           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12772             {
12773               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12774                 {
12775                   if (stretch < 0)
12776                     stretch = - ((- stretch)
12777                                  & ~ ((1 << (int) f->fr_offset) - 1));
12778                   else
12779                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12780                   if (stretch == 0)
12781                     break;
12782                 }
12783             }
12784           if (f != NULL)
12785             val += stretch;
12786         }
12787
12788       addr = fragp->fr_address + fragp->fr_fix;
12789
12790       /* The base address rules are complicated.  The base address of
12791          a branch is the following instruction.  The base address of a
12792          PC relative load or add is the instruction itself, but if it
12793          is in a delay slot (in which case it can not be extended) use
12794          the address of the instruction whose delay slot it is in.  */
12795       if (type == 'p' || type == 'q')
12796         {
12797           addr += 2;
12798
12799           /* If we are currently assuming that this frag should be
12800              extended, then, the current address is two bytes
12801              higher.  */
12802           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12803             addr += 2;
12804
12805           /* Ignore the low bit in the target, since it will be set
12806              for a text label.  */
12807           if ((val & 1) != 0)
12808             --val;
12809         }
12810       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12811         addr -= 4;
12812       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12813         addr -= 2;
12814
12815       val -= addr & ~ ((1 << op->shift) - 1);
12816
12817       /* Branch offsets have an implicit 0 in the lowest bit.  */
12818       if (type == 'p' || type == 'q')
12819         val /= 2;
12820
12821       /* If any of the shifted bits are set, we must use an extended
12822          opcode.  If the address depends on the size of this
12823          instruction, this can lead to a loop, so we arrange to always
12824          use an extended opcode.  We only check this when we are in
12825          the main relaxation loop, when SEC is NULL.  */
12826       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12827         {
12828           fragp->fr_subtype =
12829             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12830           return 1;
12831         }
12832
12833       /* If we are about to mark a frag as extended because the value
12834          is precisely maxtiny + 1, then there is a chance of an
12835          infinite loop as in the following code:
12836              la $4,foo
12837              .skip      1020
12838              .align     2
12839            foo:
12840          In this case when the la is extended, foo is 0x3fc bytes
12841          away, so the la can be shrunk, but then foo is 0x400 away, so
12842          the la must be extended.  To avoid this loop, we mark the
12843          frag as extended if it was small, and is about to become
12844          extended with a value of maxtiny + 1.  */
12845       if (val == ((maxtiny + 1) << op->shift)
12846           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12847           && sec == NULL)
12848         {
12849           fragp->fr_subtype =
12850             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12851           return 1;
12852         }
12853     }
12854   else if (symsec != absolute_section && sec != NULL)
12855     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12856
12857   if ((val & ((1 << op->shift) - 1)) != 0
12858       || val < (mintiny << op->shift)
12859       || val > (maxtiny << op->shift))
12860     return 1;
12861   else
12862     return 0;
12863 }
12864
12865 /* Compute the length of a branch sequence, and adjust the
12866    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12867    worst-case length is computed, with UPDATE being used to indicate
12868    whether an unconditional (-1), branch-likely (+1) or regular (0)
12869    branch is to be computed.  */
12870 static int
12871 relaxed_branch_length (fragp, sec, update)
12872      fragS *fragp;
12873      asection *sec;
12874      int update;
12875 {
12876   boolean toofar;
12877   int length;
12878
12879   if (fragp
12880       && S_IS_DEFINED (fragp->fr_symbol)
12881       && sec == S_GET_SEGMENT (fragp->fr_symbol))
12882     {
12883       addressT addr;
12884       offsetT val;
12885
12886       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12887
12888       addr = fragp->fr_address + fragp->fr_fix + 4;
12889
12890       val -= addr;
12891
12892       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12893     }
12894   else if (fragp)
12895     /* If the symbol is not defined or it's in a different segment,
12896        assume the user knows what's going on and emit a short
12897        branch.  */
12898     toofar = false;
12899   else
12900     toofar = true;
12901
12902   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12903     fragp->fr_subtype
12904       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype),
12905                              RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12906                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12907                              RELAX_BRANCH_LINK (fragp->fr_subtype),
12908                              toofar);
12909
12910   length = 4;
12911   if (toofar)
12912     {
12913       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12914         length += 8;
12915
12916       if (mips_pic != NO_PIC)
12917         {
12918           /* Additional space for PIC loading of target address.  */
12919           length += 8;
12920           if (mips_opts.isa == ISA_MIPS1)
12921             /* Additional space for $at-stabilizing nop.  */
12922             length += 4;
12923         }
12924
12925       /* If branch is conditional.  */
12926       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12927         length += 8;
12928     }
12929   
12930   return length;
12931 }
12932
12933 /* Estimate the size of a frag before relaxing.  Unless this is the
12934    mips16, we are not really relaxing here, and the final size is
12935    encoded in the subtype information.  For the mips16, we have to
12936    decide whether we are using an extended opcode or not.  */
12937
12938 int
12939 md_estimate_size_before_relax (fragp, segtype)
12940      fragS *fragp;
12941      asection *segtype;
12942 {
12943   int change = 0;
12944   boolean linkonce = false;
12945
12946   if (RELAX_BRANCH_P (fragp->fr_subtype))
12947     {
12948
12949       fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
12950       
12951       return fragp->fr_var;
12952     }
12953
12954   if (RELAX_MIPS16_P (fragp->fr_subtype))
12955     /* We don't want to modify the EXTENDED bit here; it might get us
12956        into infinite loops.  We change it only in mips_relax_frag().  */
12957     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12958
12959   if (mips_pic == NO_PIC)
12960     {
12961       change = nopic_need_relax (fragp->fr_symbol, 0);
12962     }
12963   else if (mips_pic == SVR4_PIC)
12964     {
12965       symbolS *sym;
12966       asection *symsec;
12967
12968       sym = fragp->fr_symbol;
12969
12970       /* Handle the case of a symbol equated to another symbol.  */
12971       while (symbol_equated_reloc_p (sym))
12972         {
12973           symbolS *n;
12974
12975           /* It's possible to get a loop here in a badly written
12976              program.  */
12977           n = symbol_get_value_expression (sym)->X_add_symbol;
12978           if (n == sym)
12979             break;
12980           sym = n;
12981         }
12982
12983       symsec = S_GET_SEGMENT (sym);
12984
12985       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12986       if (symsec != segtype && ! S_IS_LOCAL (sym))
12987         {
12988           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12989               != 0)
12990             linkonce = true;
12991
12992           /* The GNU toolchain uses an extension for ELF: a section
12993              beginning with the magic string .gnu.linkonce is a linkonce
12994              section.  */
12995           if (strncmp (segment_name (symsec), ".gnu.linkonce",
12996                        sizeof ".gnu.linkonce" - 1) == 0)
12997             linkonce = true;
12998         }
12999
13000       /* This must duplicate the test in adjust_reloc_syms.  */
13001       change = (symsec != &bfd_und_section
13002                 && symsec != &bfd_abs_section
13003                 && ! bfd_is_com_section (symsec)
13004                 && !linkonce
13005 #ifdef OBJ_ELF
13006                 /* A global or weak symbol is treated as external.  */
13007                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13008                     || (! S_IS_WEAK (sym)
13009                         && (! S_IS_EXTERNAL (sym)
13010                             || mips_pic == EMBEDDED_PIC)))
13011 #endif
13012                 );
13013     }
13014   else
13015     abort ();
13016
13017   if (change)
13018     {
13019       /* Record the offset to the first reloc in the fr_opcode field.
13020          This lets md_convert_frag and tc_gen_reloc know that the code
13021          must be expanded.  */
13022       fragp->fr_opcode = (fragp->fr_literal
13023                           + fragp->fr_fix
13024                           - RELAX_OLD (fragp->fr_subtype)
13025                           + RELAX_RELOC1 (fragp->fr_subtype));
13026       /* FIXME: This really needs as_warn_where.  */
13027       if (RELAX_WARN (fragp->fr_subtype))
13028         as_warn (_("AT used after \".set noat\" or macro used after "
13029                    "\".set nomacro\""));
13030
13031       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13032     }
13033
13034   return 0;
13035 }
13036
13037 /* This is called to see whether a reloc against a defined symbol
13038    should be converted into a reloc against a section.  Don't adjust
13039    MIPS16 jump relocations, so we don't have to worry about the format
13040    of the offset in the .o file.  Don't adjust relocations against
13041    mips16 symbols, so that the linker can find them if it needs to set
13042    up a stub.  */
13043
13044 int
13045 mips_fix_adjustable (fixp)
13046      fixS *fixp;
13047 {
13048   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13049     return 0;
13050
13051   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13052       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13053     return 0;
13054
13055   if (fixp->fx_addsy == NULL)
13056     return 1;
13057
13058 #ifdef OBJ_ELF
13059   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13060       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13061       && fixp->fx_subsy == NULL)
13062     return 0;
13063 #endif
13064
13065   return 1;
13066 }
13067
13068 /* Translate internal representation of relocation info to BFD target
13069    format.  */
13070
13071 arelent **
13072 tc_gen_reloc (section, fixp)
13073      asection *section ATTRIBUTE_UNUSED;
13074      fixS *fixp;
13075 {
13076   static arelent *retval[4];
13077   arelent *reloc;
13078   bfd_reloc_code_real_type code;
13079
13080   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13081   retval[1] = NULL;
13082
13083   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13084   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13085   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13086
13087   if (mips_pic == EMBEDDED_PIC
13088       && SWITCH_TABLE (fixp))
13089     {
13090       /* For a switch table entry we use a special reloc.  The addend
13091          is actually the difference between the reloc address and the
13092          subtrahend.  */
13093       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13094       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13095         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13096       fixp->fx_r_type = BFD_RELOC_GPREL32;
13097     }
13098   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13099     {
13100       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13101         reloc->addend = fixp->fx_addnumber;
13102       else
13103         {
13104           /* We use a special addend for an internal RELLO reloc.  */
13105           if (symbol_section_p (fixp->fx_addsy))
13106             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13107           else
13108             reloc->addend = fixp->fx_addnumber + reloc->address;
13109         }
13110     }
13111   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13112     {
13113       assert (fixp->fx_next != NULL
13114               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13115
13116       /* The reloc is relative to the RELLO; adjust the addend
13117          accordingly.  */
13118       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13119         reloc->addend = fixp->fx_next->fx_addnumber;
13120       else
13121         {
13122           /* We use a special addend for an internal RELHI reloc.  */
13123           if (symbol_section_p (fixp->fx_addsy))
13124             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13125                              + fixp->fx_next->fx_where
13126                              - S_GET_VALUE (fixp->fx_subsy));
13127           else
13128             reloc->addend = (fixp->fx_addnumber
13129                              + fixp->fx_next->fx_frag->fr_address
13130                              + fixp->fx_next->fx_where);
13131         }
13132     }
13133   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13134     reloc->addend = fixp->fx_addnumber;
13135   else
13136     {
13137       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13138         /* A gruesome hack which is a result of the gruesome gas reloc
13139            handling.  */
13140         reloc->addend = reloc->address;
13141       else
13142         reloc->addend = -reloc->address;
13143     }
13144
13145   /* If this is a variant frag, we may need to adjust the existing
13146      reloc and generate a new one.  */
13147   if (fixp->fx_frag->fr_opcode != NULL
13148       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13149            && ! HAVE_NEWABI)
13150           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13151           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13152           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13153           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13154           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13155           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13156     )
13157     {
13158       arelent *reloc2;
13159
13160       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13161
13162       /* If this is not the last reloc in this frag, then we have two
13163          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13164          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13165          the second one handle all of them.  */
13166       if (fixp->fx_next != NULL
13167           && fixp->fx_frag == fixp->fx_next->fx_frag)
13168         {
13169           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13170                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13171                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13172                       && (fixp->fx_next->fx_r_type
13173                           == BFD_RELOC_MIPS_GOT_LO16))
13174                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13175                       && (fixp->fx_next->fx_r_type
13176                           == BFD_RELOC_MIPS_CALL_LO16)));
13177           retval[0] = NULL;
13178           return retval;
13179         }
13180
13181       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13182       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13183       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13184       retval[2] = NULL;
13185       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13186       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13187       reloc2->address = (reloc->address
13188                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13189                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13190       reloc2->addend = fixp->fx_addnumber;
13191       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13192       assert (reloc2->howto != NULL);
13193
13194       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13195         {
13196           arelent *reloc3;
13197
13198           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13199           retval[3] = NULL;
13200           *reloc3 = *reloc2;
13201           reloc3->address += 4;
13202         }
13203
13204       if (mips_pic == NO_PIC)
13205         {
13206           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13207           fixp->fx_r_type = BFD_RELOC_HI16_S;
13208         }
13209       else if (mips_pic == SVR4_PIC)
13210         {
13211           switch (fixp->fx_r_type)
13212             {
13213             default:
13214               abort ();
13215             case BFD_RELOC_MIPS_GOT16:
13216               break;
13217             case BFD_RELOC_MIPS_GOT_LO16:
13218             case BFD_RELOC_MIPS_CALL_LO16:
13219               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13220               break;
13221             case BFD_RELOC_MIPS_CALL16:
13222               if (HAVE_NEWABI)
13223                 {
13224                   /* BFD_RELOC_MIPS_GOT16;*/
13225                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13226                   reloc2->howto = bfd_reloc_type_lookup
13227                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13228                 }
13229               else
13230                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13231               break;
13232             }
13233         }
13234       else
13235         abort ();
13236
13237       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13238       if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13239         {
13240           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13241           retval[1] = NULL;
13242         }
13243     }
13244
13245   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13246      entry to be used in the relocation's section offset.  */
13247   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13248     {
13249       reloc->address = reloc->addend;
13250       reloc->addend = 0;
13251     }
13252
13253   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13254      fixup_segment converted a non-PC relative reloc into a PC
13255      relative reloc.  In such a case, we need to convert the reloc
13256      code.  */
13257   code = fixp->fx_r_type;
13258   if (fixp->fx_pcrel)
13259     {
13260       switch (code)
13261         {
13262         case BFD_RELOC_8:
13263           code = BFD_RELOC_8_PCREL;
13264           break;
13265         case BFD_RELOC_16:
13266           code = BFD_RELOC_16_PCREL;
13267           break;
13268         case BFD_RELOC_32:
13269           code = BFD_RELOC_32_PCREL;
13270           break;
13271         case BFD_RELOC_64:
13272           code = BFD_RELOC_64_PCREL;
13273           break;
13274         case BFD_RELOC_8_PCREL:
13275         case BFD_RELOC_16_PCREL:
13276         case BFD_RELOC_32_PCREL:
13277         case BFD_RELOC_64_PCREL:
13278         case BFD_RELOC_16_PCREL_S2:
13279         case BFD_RELOC_PCREL_HI16_S:
13280         case BFD_RELOC_PCREL_LO16:
13281           break;
13282         default:
13283           as_bad_where (fixp->fx_file, fixp->fx_line,
13284                         _("Cannot make %s relocation PC relative"),
13285                         bfd_get_reloc_code_name (code));
13286         }
13287     }
13288
13289 #ifdef OBJ_ELF
13290   /* md_apply_fix3 has a double-subtraction hack to get
13291      bfd_install_relocation to behave nicely.  GPREL relocations are
13292      handled correctly without this hack, so undo it here.  We can't
13293      stop md_apply_fix3 from subtracting twice in the first place since
13294      the fake addend is required for variant frags above.  */
13295   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13296       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13297       && reloc->addend != 0
13298       && mips_need_elf_addend_fixup (fixp))
13299     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13300 #endif
13301
13302   /* To support a PC relative reloc when generating embedded PIC code
13303      for ECOFF, we use a Cygnus extension.  We check for that here to
13304      make sure that we don't let such a reloc escape normally.  */
13305   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13306        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13307       && code == BFD_RELOC_16_PCREL_S2
13308       && mips_pic != EMBEDDED_PIC)
13309     reloc->howto = NULL;
13310   else
13311     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13312
13313   if (reloc->howto == NULL)
13314     {
13315       as_bad_where (fixp->fx_file, fixp->fx_line,
13316                     _("Can not represent %s relocation in this object file format"),
13317                     bfd_get_reloc_code_name (code));
13318       retval[0] = NULL;
13319     }
13320
13321   return retval;
13322 }
13323
13324 /* Relax a machine dependent frag.  This returns the amount by which
13325    the current size of the frag should change.  */
13326
13327 int
13328 mips_relax_frag (sec, fragp, stretch)
13329      asection *sec;
13330      fragS *fragp;
13331      long stretch;
13332 {
13333   if (RELAX_BRANCH_P (fragp->fr_subtype))
13334     {
13335       offsetT old_var = fragp->fr_var;
13336       
13337       fragp->fr_var = relaxed_branch_length (fragp, sec, true);
13338
13339       return fragp->fr_var - old_var;
13340     }
13341
13342   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13343     return 0;
13344
13345   if (mips16_extended_frag (fragp, NULL, stretch))
13346     {
13347       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13348         return 0;
13349       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13350       return 2;
13351     }
13352   else
13353     {
13354       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13355         return 0;
13356       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13357       return -2;
13358     }
13359
13360   return 0;
13361 }
13362
13363 /* Convert a machine dependent frag.  */
13364
13365 void
13366 md_convert_frag (abfd, asec, fragp)
13367      bfd *abfd ATTRIBUTE_UNUSED;
13368      segT asec;
13369      fragS *fragp;
13370 {
13371   int old, new;
13372   char *fixptr;
13373
13374   if (RELAX_BRANCH_P (fragp->fr_subtype))
13375     {
13376       bfd_byte *buf;
13377       unsigned long insn;
13378       expressionS exp;
13379       fixS *fixp;
13380       
13381       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13382
13383       if (target_big_endian)
13384         insn = bfd_getb32 (buf);
13385       else
13386         insn = bfd_getl32 (buf);
13387           
13388       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13389         {
13390           /* We generate a fixup instead of applying it right now
13391              because, if there are linker relaxations, we're going to
13392              need the relocations.  */
13393           exp.X_op = O_symbol;
13394           exp.X_add_symbol = fragp->fr_symbol;
13395           exp.X_add_number = fragp->fr_offset;
13396
13397           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13398                               4, &exp, 1,
13399                               RELAX_BRANCH_RELOC_S2 (fragp->fr_subtype)
13400                               ? BFD_RELOC_16_PCREL_S2
13401                               : BFD_RELOC_16_PCREL);
13402           fixp->fx_file = fragp->fr_file;
13403           fixp->fx_line = fragp->fr_line;
13404           
13405           md_number_to_chars ((char *)buf, insn, 4);
13406           buf += 4;
13407         }
13408       else
13409         {
13410           int i;
13411
13412           as_warn_where (fragp->fr_file, fragp->fr_line,
13413                          _("relaxed out-of-range branch into a jump"));
13414
13415           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13416             goto uncond;
13417
13418           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13419             {
13420               /* Reverse the branch.  */
13421               switch ((insn >> 28) & 0xf)
13422                 {
13423                 case 4:
13424                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13425                      have the condition reversed by tweaking a single
13426                      bit, and their opcodes all have 0x4???????.  */
13427                   assert ((insn & 0xf1000000) == 0x41000000);
13428                   insn ^= 0x00010000;
13429                   break;
13430
13431                 case 0:
13432                   /* bltz       0x04000000      bgez    0x04010000
13433                      bltzal     0x04100000      bgezal  0x04110000 */
13434                   assert ((insn & 0xfc0e0000) == 0x04000000);
13435                   insn ^= 0x00010000;
13436                   break;
13437                   
13438                 case 1:
13439                   /* beq        0x10000000      bne     0x14000000
13440                      blez       0x18000000      bgtz    0x1c000000 */
13441                   insn ^= 0x04000000;
13442                   break;
13443
13444                 default:
13445                   abort ();
13446                 }
13447             }
13448
13449           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13450             {
13451               /* Clear the and-link bit.  */
13452               assert ((insn & 0xfc1c0000) == 0x04100000);
13453
13454               /* bltzal 0x04100000      bgezal  0x04110000
13455                 bltzall 0x04120000     bgezall  0x04130000 */
13456               insn &= ~0x00100000;
13457             }
13458
13459           /* Branch over the branch (if the branch was likely) or the
13460              full jump (not likely case).  Compute the offset from the
13461              current instruction to branch to.  */
13462           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13463             i = 16;
13464           else
13465             {
13466               /* How many bytes in instructions we've already emitted?  */
13467               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13468               /* How many bytes in instructions from here to the end?  */
13469               i = fragp->fr_var - i;
13470             }
13471           /* Convert to instruction count.  */
13472           i >>= 2;
13473           /* Branch counts from the next instruction.  */
13474           i--; 
13475           insn |= i;
13476           /* Branch over the jump.  */
13477           md_number_to_chars ((char *)buf, insn, 4);
13478           buf += 4;
13479
13480           /* Nop */
13481           md_number_to_chars ((char*)buf, 0, 4);
13482           buf += 4;
13483
13484           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13485             {
13486               /* beql $0, $0, 2f */
13487               insn = 0x50000000;
13488               /* Compute the PC offset from the current instruction to
13489                  the end of the variable frag.  */
13490               /* How many bytes in instructions we've already emitted?  */
13491               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13492               /* How many bytes in instructions from here to the end?  */
13493               i = fragp->fr_var - i;
13494               /* Convert to instruction count.  */
13495               i >>= 2;
13496               /* Don't decrement i, because we want to branch over the
13497                  delay slot.  */
13498
13499               insn |= i;
13500               md_number_to_chars ((char *)buf, insn, 4);
13501               buf += 4;
13502
13503               md_number_to_chars ((char *)buf, 0, 4);
13504               buf += 4;
13505             }
13506
13507         uncond:
13508           if (mips_pic == NO_PIC)
13509             {
13510               /* j or jal.  */
13511               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13512                       ? 0x0c000000 : 0x08000000);
13513               exp.X_op = O_symbol;
13514               exp.X_add_symbol = fragp->fr_symbol;
13515               exp.X_add_number = fragp->fr_offset;
13516
13517               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13518                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13519               fixp->fx_file = fragp->fr_file;
13520               fixp->fx_line = fragp->fr_line;
13521
13522               md_number_to_chars ((char*)buf, insn, 4);
13523               buf += 4;
13524             }
13525           else
13526             {
13527               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13528               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13529               exp.X_op = O_symbol;
13530               exp.X_add_symbol = fragp->fr_symbol;
13531               exp.X_add_number = fragp->fr_offset;
13532
13533               if (fragp->fr_offset)
13534                 {
13535                   exp.X_add_symbol = make_expr_symbol (&exp);
13536                   exp.X_add_number = 0;
13537                 }
13538
13539               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13540                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13541               fixp->fx_file = fragp->fr_file;
13542               fixp->fx_line = fragp->fr_line;
13543
13544               md_number_to_chars ((char*)buf, insn, 4);
13545               buf += 4;
13546               
13547               if (mips_opts.isa == ISA_MIPS1)
13548                 {
13549                   /* nop */
13550                   md_number_to_chars ((char*)buf, 0, 4);
13551                   buf += 4;
13552                 }
13553
13554               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13555               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13556
13557               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13558                                   4, &exp, 0, BFD_RELOC_LO16);
13559               fixp->fx_file = fragp->fr_file;
13560               fixp->fx_line = fragp->fr_line;
13561               
13562               md_number_to_chars ((char*)buf, insn, 4);
13563               buf += 4;
13564
13565               /* j(al)r $at.  */
13566               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13567                 insn = 0x0020f809;
13568               else
13569                 insn = 0x00200008;
13570
13571               md_number_to_chars ((char*)buf, insn, 4);
13572               buf += 4;
13573             }
13574         }
13575
13576       assert (buf == (bfd_byte *)fragp->fr_literal
13577               + fragp->fr_fix + fragp->fr_var);
13578
13579       fragp->fr_fix += fragp->fr_var;
13580
13581       return;
13582     }
13583
13584   if (RELAX_MIPS16_P (fragp->fr_subtype))
13585     {
13586       int type;
13587       register const struct mips16_immed_operand *op;
13588       boolean small, ext;
13589       offsetT val;
13590       bfd_byte *buf;
13591       unsigned long insn;
13592       boolean use_extend;
13593       unsigned short extend;
13594
13595       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13596       op = mips16_immed_operands;
13597       while (op->type != type)
13598         ++op;
13599
13600       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13601         {
13602           small = false;
13603           ext = true;
13604         }
13605       else
13606         {
13607           small = true;
13608           ext = false;
13609         }
13610
13611       resolve_symbol_value (fragp->fr_symbol);
13612       val = S_GET_VALUE (fragp->fr_symbol);
13613       if (op->pcrel)
13614         {
13615           addressT addr;
13616
13617           addr = fragp->fr_address + fragp->fr_fix;
13618
13619           /* The rules for the base address of a PC relative reloc are
13620              complicated; see mips16_extended_frag.  */
13621           if (type == 'p' || type == 'q')
13622             {
13623               addr += 2;
13624               if (ext)
13625                 addr += 2;
13626               /* Ignore the low bit in the target, since it will be
13627                  set for a text label.  */
13628               if ((val & 1) != 0)
13629                 --val;
13630             }
13631           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13632             addr -= 4;
13633           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13634             addr -= 2;
13635
13636           addr &= ~ (addressT) ((1 << op->shift) - 1);
13637           val -= addr;
13638
13639           /* Make sure the section winds up with the alignment we have
13640              assumed.  */
13641           if (op->shift > 0)
13642             record_alignment (asec, op->shift);
13643         }
13644
13645       if (ext
13646           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13647               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13648         as_warn_where (fragp->fr_file, fragp->fr_line,
13649                        _("extended instruction in delay slot"));
13650
13651       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13652
13653       if (target_big_endian)
13654         insn = bfd_getb16 (buf);
13655       else
13656         insn = bfd_getl16 (buf);
13657
13658       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13659                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13660                     small, ext, &insn, &use_extend, &extend);
13661
13662       if (use_extend)
13663         {
13664           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13665           fragp->fr_fix += 2;
13666           buf += 2;
13667         }
13668
13669       md_number_to_chars ((char *) buf, insn, 2);
13670       fragp->fr_fix += 2;
13671       buf += 2;
13672     }
13673   else
13674     {
13675       if (fragp->fr_opcode == NULL)
13676         return;
13677
13678       old = RELAX_OLD (fragp->fr_subtype);
13679       new = RELAX_NEW (fragp->fr_subtype);
13680       fixptr = fragp->fr_literal + fragp->fr_fix;
13681
13682       if (new > 0)
13683         memcpy (fixptr - old, fixptr, new);
13684
13685       fragp->fr_fix += new - old;
13686     }
13687 }
13688
13689 #ifdef OBJ_ELF
13690
13691 /* This function is called after the relocs have been generated.
13692    We've been storing mips16 text labels as odd.  Here we convert them
13693    back to even for the convenience of the debugger.  */
13694
13695 void
13696 mips_frob_file_after_relocs ()
13697 {
13698   asymbol **syms;
13699   unsigned int count, i;
13700
13701   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13702     return;
13703
13704   syms = bfd_get_outsymbols (stdoutput);
13705   count = bfd_get_symcount (stdoutput);
13706   for (i = 0; i < count; i++, syms++)
13707     {
13708       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13709           && ((*syms)->value & 1) != 0)
13710         {
13711           (*syms)->value &= ~1;
13712           /* If the symbol has an odd size, it was probably computed
13713              incorrectly, so adjust that as well.  */
13714           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13715             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13716         }
13717     }
13718 }
13719
13720 #endif
13721
13722 /* This function is called whenever a label is defined.  It is used
13723    when handling branch delays; if a branch has a label, we assume we
13724    can not move it.  */
13725
13726 void
13727 mips_define_label (sym)
13728      symbolS *sym;
13729 {
13730   struct insn_label_list *l;
13731
13732   if (free_insn_labels == NULL)
13733     l = (struct insn_label_list *) xmalloc (sizeof *l);
13734   else
13735     {
13736       l = free_insn_labels;
13737       free_insn_labels = l->next;
13738     }
13739
13740   l->label = sym;
13741   l->next = insn_labels;
13742   insn_labels = l;
13743 }
13744 \f
13745 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13746
13747 /* Some special processing for a MIPS ELF file.  */
13748
13749 void
13750 mips_elf_final_processing ()
13751 {
13752   /* Write out the register information.  */
13753   if (mips_abi != N64_ABI)
13754     {
13755       Elf32_RegInfo s;
13756
13757       s.ri_gprmask = mips_gprmask;
13758       s.ri_cprmask[0] = mips_cprmask[0];
13759       s.ri_cprmask[1] = mips_cprmask[1];
13760       s.ri_cprmask[2] = mips_cprmask[2];
13761       s.ri_cprmask[3] = mips_cprmask[3];
13762       /* The gp_value field is set by the MIPS ELF backend.  */
13763
13764       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13765                                        ((Elf32_External_RegInfo *)
13766                                         mips_regmask_frag));
13767     }
13768   else
13769     {
13770       Elf64_Internal_RegInfo s;
13771
13772       s.ri_gprmask = mips_gprmask;
13773       s.ri_pad = 0;
13774       s.ri_cprmask[0] = mips_cprmask[0];
13775       s.ri_cprmask[1] = mips_cprmask[1];
13776       s.ri_cprmask[2] = mips_cprmask[2];
13777       s.ri_cprmask[3] = mips_cprmask[3];
13778       /* The gp_value field is set by the MIPS ELF backend.  */
13779
13780       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13781                                        ((Elf64_External_RegInfo *)
13782                                         mips_regmask_frag));
13783     }
13784
13785   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13786      sort of BFD interface for this.  */
13787   if (mips_any_noreorder)
13788     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13789   if (mips_pic != NO_PIC)
13790     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13791
13792   /* Set MIPS ELF flags for ASEs.  */
13793   if (file_ase_mips16)
13794     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13795 #if 0 /* XXX FIXME */
13796   if (file_ase_mips3d)
13797     elf_elfheader (stdoutput)->e_flags |= ???;
13798 #endif
13799   if (file_ase_mdmx)
13800     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13801
13802   /* Set the MIPS ELF ABI flags.  */
13803   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13804     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13805   else if (mips_abi == O64_ABI)
13806     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13807   else if (mips_abi == EABI_ABI)
13808     {
13809       if (!file_mips_gp32)
13810         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13811       else
13812         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13813     }
13814   else if (mips_abi == N32_ABI)
13815     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13816
13817   /* Nothing to do for N64_ABI.  */
13818
13819   if (mips_32bitmode)
13820     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13821 }
13822
13823 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13824 \f
13825 typedef struct proc {
13826   symbolS *isym;
13827   unsigned long reg_mask;
13828   unsigned long reg_offset;
13829   unsigned long fpreg_mask;
13830   unsigned long fpreg_offset;
13831   unsigned long frame_offset;
13832   unsigned long frame_reg;
13833   unsigned long pc_reg;
13834 } procS;
13835
13836 static procS cur_proc;
13837 static procS *cur_proc_ptr;
13838 static int numprocs;
13839
13840 /* Fill in an rs_align_code fragment.  */
13841
13842 void
13843 mips_handle_align (fragp)
13844      fragS *fragp;
13845 {
13846   if (fragp->fr_type != rs_align_code)
13847     return;
13848
13849   if (mips_opts.mips16)
13850     {
13851       static const unsigned char be_nop[] = { 0x65, 0x00 };
13852       static const unsigned char le_nop[] = { 0x00, 0x65 };
13853
13854       int bytes;
13855       char *p;
13856
13857       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13858       p = fragp->fr_literal + fragp->fr_fix;
13859
13860       if (bytes & 1)
13861         {
13862           *p++ = 0;
13863           fragp->fr_fix++;
13864         }
13865
13866       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13867       fragp->fr_var = 2;
13868     }
13869
13870   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13871 }
13872
13873 static void
13874 md_obj_begin ()
13875 {
13876 }
13877
13878 static void
13879 md_obj_end ()
13880 {
13881   /* check for premature end, nesting errors, etc */
13882   if (cur_proc_ptr)
13883     as_warn (_("missing .end at end of assembly"));
13884 }
13885
13886 static long
13887 get_number ()
13888 {
13889   int negative = 0;
13890   long val = 0;
13891
13892   if (*input_line_pointer == '-')
13893     {
13894       ++input_line_pointer;
13895       negative = 1;
13896     }
13897   if (!ISDIGIT (*input_line_pointer))
13898     as_bad (_("expected simple number"));
13899   if (input_line_pointer[0] == '0')
13900     {
13901       if (input_line_pointer[1] == 'x')
13902         {
13903           input_line_pointer += 2;
13904           while (ISXDIGIT (*input_line_pointer))
13905             {
13906               val <<= 4;
13907               val |= hex_value (*input_line_pointer++);
13908             }
13909           return negative ? -val : val;
13910         }
13911       else
13912         {
13913           ++input_line_pointer;
13914           while (ISDIGIT (*input_line_pointer))
13915             {
13916               val <<= 3;
13917               val |= *input_line_pointer++ - '0';
13918             }
13919           return negative ? -val : val;
13920         }
13921     }
13922   if (!ISDIGIT (*input_line_pointer))
13923     {
13924       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13925               *input_line_pointer, *input_line_pointer);
13926       as_warn (_("invalid number"));
13927       return -1;
13928     }
13929   while (ISDIGIT (*input_line_pointer))
13930     {
13931       val *= 10;
13932       val += *input_line_pointer++ - '0';
13933     }
13934   return negative ? -val : val;
13935 }
13936
13937 /* The .file directive; just like the usual .file directive, but there
13938    is an initial number which is the ECOFF file index.  In the non-ECOFF
13939    case .file implies DWARF-2.  */
13940
13941 static void
13942 s_mips_file (x)
13943      int x ATTRIBUTE_UNUSED;
13944 {
13945   static int first_file_directive = 0;
13946
13947   if (ECOFF_DEBUGGING)
13948     {
13949       get_number ();
13950       s_app_file (0);
13951     }
13952   else
13953     {
13954       char *filename;
13955
13956       filename = dwarf2_directive_file (0);
13957
13958       /* Versions of GCC up to 3.1 start files with a ".file"
13959          directive even for stabs output.  Make sure that this
13960          ".file" is handled.  Note that you need a version of GCC
13961          after 3.1 in order to support DWARF-2 on MIPS.  */
13962       if (filename != NULL && ! first_file_directive)
13963         {
13964           (void) new_logical_line (filename, -1);
13965           s_app_file_string (filename);
13966         }
13967       first_file_directive = 1;
13968     }
13969 }
13970
13971 /* The .loc directive, implying DWARF-2.  */
13972
13973 static void
13974 s_mips_loc (x)
13975      int x ATTRIBUTE_UNUSED;
13976 {
13977   if (!ECOFF_DEBUGGING)
13978     dwarf2_directive_loc (0);
13979 }
13980
13981 /* The .end directive.  */
13982
13983 static void
13984 s_mips_end (x)
13985      int x ATTRIBUTE_UNUSED;
13986 {
13987   symbolS *p;
13988   int maybe_text;
13989
13990   /* Following functions need their own .frame and .cprestore directives.  */
13991   mips_frame_reg_valid = 0;
13992   mips_cprestore_valid = 0;
13993
13994   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13995     {
13996       p = get_symbol ();
13997       demand_empty_rest_of_line ();
13998     }
13999   else
14000     p = NULL;
14001
14002 #ifdef BFD_ASSEMBLER
14003   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14004     maybe_text = 1;
14005   else
14006     maybe_text = 0;
14007 #else
14008   if (now_seg != data_section && now_seg != bss_section)
14009     maybe_text = 1;
14010   else
14011     maybe_text = 0;
14012 #endif
14013
14014   if (!maybe_text)
14015     as_warn (_(".end not in text section"));
14016
14017   if (!cur_proc_ptr)
14018     {
14019       as_warn (_(".end directive without a preceding .ent directive."));
14020       demand_empty_rest_of_line ();
14021       return;
14022     }
14023
14024   if (p != NULL)
14025     {
14026       assert (S_GET_NAME (p));
14027       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14028         as_warn (_(".end symbol does not match .ent symbol."));
14029
14030       if (debug_type == DEBUG_STABS)
14031         stabs_generate_asm_endfunc (S_GET_NAME (p),
14032                                     S_GET_NAME (p));
14033     }
14034   else
14035     as_warn (_(".end directive missing or unknown symbol"));
14036
14037 #ifdef OBJ_ELF
14038   /* Generate a .pdr section.  */
14039   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14040     {
14041       segT saved_seg = now_seg;
14042       subsegT saved_subseg = now_subseg;
14043       valueT dot;
14044       expressionS exp;
14045       char *fragp;
14046
14047       dot = frag_now_fix ();
14048
14049 #ifdef md_flush_pending_output
14050       md_flush_pending_output ();
14051 #endif
14052
14053       assert (pdr_seg);
14054       subseg_set (pdr_seg, 0);
14055
14056       /* Write the symbol.  */
14057       exp.X_op = O_symbol;
14058       exp.X_add_symbol = p;
14059       exp.X_add_number = 0;
14060       emit_expr (&exp, 4);
14061
14062       fragp = frag_more (7 * 4);
14063
14064       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14065       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14066       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14067       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14068       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14069       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14070       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14071
14072       subseg_set (saved_seg, saved_subseg);
14073     }
14074 #endif /* OBJ_ELF */
14075
14076   cur_proc_ptr = NULL;
14077 }
14078
14079 /* The .aent and .ent directives.  */
14080
14081 static void
14082 s_mips_ent (aent)
14083      int aent;
14084 {
14085   symbolS *symbolP;
14086   int maybe_text;
14087
14088   symbolP = get_symbol ();
14089   if (*input_line_pointer == ',')
14090     ++input_line_pointer;
14091   SKIP_WHITESPACE ();
14092   if (ISDIGIT (*input_line_pointer)
14093       || *input_line_pointer == '-')
14094     get_number ();
14095
14096 #ifdef BFD_ASSEMBLER
14097   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14098     maybe_text = 1;
14099   else
14100     maybe_text = 0;
14101 #else
14102   if (now_seg != data_section && now_seg != bss_section)
14103     maybe_text = 1;
14104   else
14105     maybe_text = 0;
14106 #endif
14107
14108   if (!maybe_text)
14109     as_warn (_(".ent or .aent not in text section."));
14110
14111   if (!aent && cur_proc_ptr)
14112     as_warn (_("missing .end"));
14113
14114   if (!aent)
14115     {
14116       /* This function needs its own .frame and .cprestore directives.  */
14117       mips_frame_reg_valid = 0;
14118       mips_cprestore_valid = 0;
14119
14120       cur_proc_ptr = &cur_proc;
14121       memset (cur_proc_ptr, '\0', sizeof (procS));
14122
14123       cur_proc_ptr->isym = symbolP;
14124
14125       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14126
14127       ++numprocs;
14128
14129       if (debug_type == DEBUG_STABS)
14130         stabs_generate_asm_func (S_GET_NAME (symbolP),
14131                                  S_GET_NAME (symbolP));
14132     }
14133
14134   demand_empty_rest_of_line ();
14135 }
14136
14137 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14138    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14139    s_mips_frame is used so that we can set the PDR information correctly.
14140    We can't use the ecoff routines because they make reference to the ecoff
14141    symbol table (in the mdebug section).  */
14142
14143 static void
14144 s_mips_frame (ignore)
14145      int ignore ATTRIBUTE_UNUSED;
14146 {
14147 #ifdef OBJ_ELF
14148   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14149     {
14150       long val;
14151
14152       if (cur_proc_ptr == (procS *) NULL)
14153         {
14154           as_warn (_(".frame outside of .ent"));
14155           demand_empty_rest_of_line ();
14156           return;
14157         }
14158
14159       cur_proc_ptr->frame_reg = tc_get_register (1);
14160
14161       SKIP_WHITESPACE ();
14162       if (*input_line_pointer++ != ','
14163           || get_absolute_expression_and_terminator (&val) != ',')
14164         {
14165           as_warn (_("Bad .frame directive"));
14166           --input_line_pointer;
14167           demand_empty_rest_of_line ();
14168           return;
14169         }
14170
14171       cur_proc_ptr->frame_offset = val;
14172       cur_proc_ptr->pc_reg = tc_get_register (0);
14173
14174       demand_empty_rest_of_line ();
14175     }
14176   else
14177 #endif /* OBJ_ELF */
14178     s_ignore (ignore);
14179 }
14180
14181 /* The .fmask and .mask directives. If the mdebug section is present
14182    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14183    embedded targets, s_mips_mask is used so that we can set the PDR
14184    information correctly. We can't use the ecoff routines because they
14185    make reference to the ecoff symbol table (in the mdebug section).  */
14186
14187 static void
14188 s_mips_mask (reg_type)
14189      char reg_type;
14190 {
14191 #ifdef OBJ_ELF
14192   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14193     {
14194       long mask, off;
14195
14196       if (cur_proc_ptr == (procS *) NULL)
14197         {
14198           as_warn (_(".mask/.fmask outside of .ent"));
14199           demand_empty_rest_of_line ();
14200           return;
14201         }
14202
14203       if (get_absolute_expression_and_terminator (&mask) != ',')
14204         {
14205           as_warn (_("Bad .mask/.fmask directive"));
14206           --input_line_pointer;
14207           demand_empty_rest_of_line ();
14208           return;
14209         }
14210
14211       off = get_absolute_expression ();
14212
14213       if (reg_type == 'F')
14214         {
14215           cur_proc_ptr->fpreg_mask = mask;
14216           cur_proc_ptr->fpreg_offset = off;
14217         }
14218       else
14219         {
14220           cur_proc_ptr->reg_mask = mask;
14221           cur_proc_ptr->reg_offset = off;
14222         }
14223
14224       demand_empty_rest_of_line ();
14225     }
14226   else
14227 #endif /* OBJ_ELF */
14228     s_ignore (reg_type);
14229 }
14230
14231 /* The .loc directive.  */
14232
14233 #if 0
14234 static void
14235 s_loc (x)
14236      int x;
14237 {
14238   symbolS *symbolP;
14239   int lineno;
14240   int addroff;
14241
14242   assert (now_seg == text_section);
14243
14244   lineno = get_number ();
14245   addroff = frag_now_fix ();
14246
14247   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14248   S_SET_TYPE (symbolP, N_SLINE);
14249   S_SET_OTHER (symbolP, 0);
14250   S_SET_DESC (symbolP, lineno);
14251   symbolP->sy_segment = now_seg;
14252 }
14253 #endif
14254
14255 /* A table describing all the processors gas knows about.  Names are
14256    matched in the order listed.
14257
14258    To ease comparison, please keep this table in the same order as
14259    gcc's mips_cpu_info_table[].  */
14260 static const struct mips_cpu_info mips_cpu_info_table[] =
14261 {
14262   /* Entries for generic ISAs */
14263   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14264   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14265   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14266   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14267   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14268   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14269   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14270
14271   /* MIPS I */
14272   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14273   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14274   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14275
14276   /* MIPS II */
14277   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14278
14279   /* MIPS III */
14280   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14281   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14282   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14283   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14284   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14285   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14286   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14287   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14288   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14289   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14290   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14291   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14292
14293   /* MIPS IV */
14294   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14295   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14296   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14297   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14298   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14299   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14300   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14301   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14302   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14303   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14304   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14305   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14306
14307   /* MIPS 32 */
14308   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14309   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14310   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14311
14312   /* MIPS 64 */
14313   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14314   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14315
14316   /* Broadcom SB-1 CPU core */
14317   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14318
14319   /* End marker */
14320   { NULL, 0, 0, 0 }
14321 };
14322
14323
14324 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14325    with a final "000" replaced by "k".  Ignore case.
14326
14327    Note: this function is shared between GCC and GAS.  */
14328
14329 static boolean
14330 mips_strict_matching_cpu_name_p (canonical, given)
14331      const char *canonical, *given;
14332 {
14333   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14334     given++, canonical++;
14335
14336   return ((*given == 0 && *canonical == 0)
14337           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14338 }
14339
14340
14341 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14342    CPU name.  We've traditionally allowed a lot of variation here.
14343
14344    Note: this function is shared between GCC and GAS.  */
14345
14346 static boolean
14347 mips_matching_cpu_name_p (canonical, given)
14348      const char *canonical, *given;
14349 {
14350   /* First see if the name matches exactly, or with a final "000"
14351      turned into "k".  */
14352   if (mips_strict_matching_cpu_name_p (canonical, given))
14353     return true;
14354
14355   /* If not, try comparing based on numerical designation alone.
14356      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14357   if (TOLOWER (*given) == 'r')
14358     given++;
14359   if (!ISDIGIT (*given))
14360     return false;
14361
14362   /* Skip over some well-known prefixes in the canonical name,
14363      hoping to find a number there too.  */
14364   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14365     canonical += 2;
14366   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14367     canonical += 2;
14368   else if (TOLOWER (canonical[0]) == 'r')
14369     canonical += 1;
14370
14371   return mips_strict_matching_cpu_name_p (canonical, given);
14372 }
14373
14374
14375 /* Parse an option that takes the name of a processor as its argument.
14376    OPTION is the name of the option and CPU_STRING is the argument.
14377    Return the corresponding processor enumeration if the CPU_STRING is
14378    recognized, otherwise report an error and return null.
14379
14380    A similar function exists in GCC.  */
14381
14382 static const struct mips_cpu_info *
14383 mips_parse_cpu (option, cpu_string)
14384      const char *option, *cpu_string;
14385 {
14386   const struct mips_cpu_info *p;
14387
14388   /* 'from-abi' selects the most compatible architecture for the given
14389      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14390      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14391      version.  Look first at the -mgp options, if given, otherwise base
14392      the choice on MIPS_DEFAULT_64BIT.
14393
14394      Treat NO_ABI like the EABIs.  One reason to do this is that the
14395      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14396      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14397      'mips64', just as we did in the days before 'from-abi'.  */
14398   if (strcasecmp (cpu_string, "from-abi") == 0)
14399     {
14400       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14401         return mips_cpu_info_from_isa (ISA_MIPS1);
14402
14403       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14404         return mips_cpu_info_from_isa (ISA_MIPS3);
14405
14406       if (file_mips_gp32 >= 0)
14407         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14408
14409       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14410                                      ? ISA_MIPS3
14411                                      : ISA_MIPS1);
14412     }
14413
14414   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14415   if (strcasecmp (cpu_string, "default") == 0)
14416     return 0;
14417
14418   for (p = mips_cpu_info_table; p->name != 0; p++)
14419     if (mips_matching_cpu_name_p (p->name, cpu_string))
14420       return p;
14421
14422   as_bad ("Bad value (%s) for %s", cpu_string, option);
14423   return 0;
14424 }
14425
14426 /* Return the canonical processor information for ISA (a member of the
14427    ISA_MIPS* enumeration).  */
14428
14429 static const struct mips_cpu_info *
14430 mips_cpu_info_from_isa (isa)
14431      int isa;
14432 {
14433   int i;
14434
14435   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14436     if (mips_cpu_info_table[i].is_isa
14437         && isa == mips_cpu_info_table[i].isa)
14438       return (&mips_cpu_info_table[i]);
14439
14440   return NULL;
14441 }
14442 \f
14443 static void
14444 show (stream, string, col_p, first_p)
14445      FILE *stream;
14446      const char *string;
14447      int *col_p;
14448      int *first_p;
14449 {
14450   if (*first_p)
14451     {
14452       fprintf (stream, "%24s", "");
14453       *col_p = 24;
14454     }
14455   else
14456     {
14457       fprintf (stream, ", ");
14458       *col_p += 2;
14459     }
14460
14461   if (*col_p + strlen (string) > 72)
14462     {
14463       fprintf (stream, "\n%24s", "");
14464       *col_p = 24;
14465     }
14466
14467   fprintf (stream, "%s", string);
14468   *col_p += strlen (string);
14469
14470   *first_p = 0;
14471 }
14472
14473 void
14474 md_show_usage (stream)
14475      FILE *stream;
14476 {
14477   int column, first;
14478   size_t i;
14479
14480   fprintf (stream, _("\
14481 MIPS options:\n\
14482 -membedded-pic          generate embedded position independent code\n\
14483 -EB                     generate big endian output\n\
14484 -EL                     generate little endian output\n\
14485 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14486 -G NUM                  allow referencing objects up to NUM bytes\n\
14487                         implicitly with the gp register [default 8]\n"));
14488   fprintf (stream, _("\
14489 -mips1                  generate MIPS ISA I instructions\n\
14490 -mips2                  generate MIPS ISA II instructions\n\
14491 -mips3                  generate MIPS ISA III instructions\n\
14492 -mips4                  generate MIPS ISA IV instructions\n\
14493 -mips5                  generate MIPS ISA V instructions\n\
14494 -mips32                 generate MIPS32 ISA instructions\n\
14495 -mips64                 generate MIPS64 ISA instructions\n\
14496 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14497
14498   first = 1;
14499
14500   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14501     show (stream, mips_cpu_info_table[i].name, &column, &first);
14502   show (stream, "from-abi", &column, &first);
14503   fputc ('\n', stream);
14504
14505   fprintf (stream, _("\
14506 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14507 -no-mCPU                don't generate code specific to CPU.\n\
14508                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14509
14510   first = 1;
14511
14512   show (stream, "3900", &column, &first);
14513   show (stream, "4010", &column, &first);
14514   show (stream, "4100", &column, &first);
14515   show (stream, "4650", &column, &first);
14516   fputc ('\n', stream);
14517
14518   fprintf (stream, _("\
14519 -mips16                 generate mips16 instructions\n\
14520 -no-mips16              do not generate mips16 instructions\n"));
14521   fprintf (stream, _("\
14522 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14523 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14524 -O0                     remove unneeded NOPs, do not swap branches\n\
14525 -O                      remove unneeded NOPs and swap branches\n\
14526 -n                      warn about NOPs generated from macros\n\
14527 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14528 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14529 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14530 #ifdef OBJ_ELF
14531   fprintf (stream, _("\
14532 -KPIC, -call_shared     generate SVR4 position independent code\n\
14533 -non_shared             do not generate position independent code\n\
14534 -xgot                   assume a 32 bit GOT\n\
14535 -mabi=ABI               create ABI conformant object file for:\n"));
14536
14537   first = 1;
14538
14539   show (stream, "32", &column, &first);
14540   show (stream, "o64", &column, &first);
14541   show (stream, "n32", &column, &first);
14542   show (stream, "64", &column, &first);
14543   show (stream, "eabi", &column, &first);
14544
14545   fputc ('\n', stream);
14546
14547   fprintf (stream, _("\
14548 -32                     create o32 ABI object file (default)\n\
14549 -n32                    create n32 ABI object file\n\
14550 -64                     create 64 ABI object file\n"));
14551 #endif
14552 }