* tc-mips.c (RELAX_BRANCH_ENCODE): Remove reloc_s2 argument.
[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(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 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
725 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
726 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
727 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
728 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1))
729
730 /* For mips16 code, we use an entirely different form of relaxation.
731    mips16 supports two versions of most instructions which take
732    immediate values: a small one which takes some small value, and a
733    larger one which takes a 16 bit value.  Since branches also follow
734    this pattern, relaxing these values is required.
735
736    We can assemble both mips16 and normal MIPS code in a single
737    object.  Therefore, we need to support this type of relaxation at
738    the same time that we support the relaxation described above.  We
739    use the high bit of the subtype field to distinguish these cases.
740
741    The information we store for this type of relaxation is the
742    argument code found in the opcode file for this relocation, whether
743    the user explicitly requested a small or extended form, and whether
744    the relocation is in a jump or jal delay slot.  That tells us the
745    size of the value, and how it should be stored.  We also store
746    whether the fragment is considered to be extended or not.  We also
747    store whether this is known to be a branch to a different section,
748    whether we have tried to relax this frag yet, and whether we have
749    ever extended a PC relative fragment because of a shift count.  */
750 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
751   (0x80000000                                                   \
752    | ((type) & 0xff)                                            \
753    | ((small) ? 0x100 : 0)                                      \
754    | ((ext) ? 0x200 : 0)                                        \
755    | ((dslot) ? 0x400 : 0)                                      \
756    | ((jal_dslot) ? 0x800 : 0))
757 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
758 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
759 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
760 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
761 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
762 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
763 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
764 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
765 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
766 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
767 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
768 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
769
770 /* Is the given value a sign-extended 32-bit value?  */
771 #define IS_SEXT_32BIT_NUM(x)                                            \
772   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
773    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
774
775 /* Is the given value a sign-extended 16-bit value?  */
776 #define IS_SEXT_16BIT_NUM(x)                                            \
777   (((x) &~ (offsetT) 0x7fff) == 0                                       \
778    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
779
780 \f
781 /* Prototypes for static functions.  */
782
783 #ifdef __STDC__
784 #define internalError() \
785     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
786 #else
787 #define internalError() as_fatal (_("MIPS internal Error"));
788 #endif
789
790 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
791
792 static int insn_uses_reg
793   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
794            enum mips_regclass class));
795 static int reg_needs_delay
796   PARAMS ((unsigned int));
797 static void mips16_mark_labels
798   PARAMS ((void));
799 static void append_insn
800   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
801            bfd_reloc_code_real_type *r, bfd_boolean));
802 static void mips_no_prev_insn
803   PARAMS ((int));
804 static void mips_emit_delays
805   PARAMS ((bfd_boolean));
806 #ifdef USE_STDARG
807 static void macro_build
808   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
809            const char *fmt, ...));
810 #else
811 static void macro_build ();
812 #endif
813 static void mips16_macro_build
814   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
815 static void macro_build_jalr
816   PARAMS ((int, expressionS *));
817 static void macro_build_lui
818   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
819 static void macro_build_ldst_constoffset
820   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
821            int valreg, int breg));
822 static void set_at
823   PARAMS ((int *counter, int reg, int unsignedp));
824 static void check_absolute_expr
825   PARAMS ((struct mips_cl_insn * ip, expressionS *));
826 static void load_register
827   PARAMS ((int *, int, expressionS *, int));
828 static void load_address
829   PARAMS ((int *, int, expressionS *, int *));
830 static void move_register
831   PARAMS ((int *, int, int));
832 static void macro
833   PARAMS ((struct mips_cl_insn * ip));
834 static void mips16_macro
835   PARAMS ((struct mips_cl_insn * ip));
836 #ifdef LOSING_COMPILER
837 static void macro2
838   PARAMS ((struct mips_cl_insn * ip));
839 #endif
840 static void mips_ip
841   PARAMS ((char *str, struct mips_cl_insn * ip));
842 static void mips16_ip
843   PARAMS ((char *str, struct mips_cl_insn * ip));
844 static void mips16_immed
845   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
846            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
847 static int my_getPercentOp
848   PARAMS ((char **, unsigned int *, int *));
849 static int my_getSmallParser
850   PARAMS ((char **, unsigned int *, int *));
851 static int my_getSmallExpression
852   PARAMS ((expressionS *, char *));
853 static void my_getExpression
854   PARAMS ((expressionS *, char *));
855 #ifdef OBJ_ELF
856 static int support_64bit_objects
857   PARAMS((void));
858 #endif
859 static void mips_set_option_string
860   PARAMS ((const char **, const char *));
861 static symbolS *get_symbol
862   PARAMS ((void));
863 static void mips_align
864   PARAMS ((int to, int fill, symbolS *label));
865 static void s_align
866   PARAMS ((int));
867 static void s_change_sec
868   PARAMS ((int));
869 static void s_change_section
870   PARAMS ((int));
871 static void s_cons
872   PARAMS ((int));
873 static void s_float_cons
874   PARAMS ((int));
875 static void s_mips_globl
876   PARAMS ((int));
877 static void s_option
878   PARAMS ((int));
879 static void s_mipsset
880   PARAMS ((int));
881 static void s_abicalls
882   PARAMS ((int));
883 static void s_cpload
884   PARAMS ((int));
885 static void s_cpsetup
886   PARAMS ((int));
887 static void s_cplocal
888   PARAMS ((int));
889 static void s_cprestore
890   PARAMS ((int));
891 static void s_cpreturn
892   PARAMS ((int));
893 static void s_gpvalue
894   PARAMS ((int));
895 static void s_gpword
896   PARAMS ((int));
897 static void s_gpdword
898   PARAMS ((int));
899 static void s_cpadd
900   PARAMS ((int));
901 static void s_insn
902   PARAMS ((int));
903 static void md_obj_begin
904   PARAMS ((void));
905 static void md_obj_end
906   PARAMS ((void));
907 static long get_number
908   PARAMS ((void));
909 static void s_mips_ent
910   PARAMS ((int));
911 static void s_mips_end
912   PARAMS ((int));
913 static void s_mips_frame
914   PARAMS ((int));
915 static void s_mips_mask
916   PARAMS ((int));
917 static void s_mips_stab
918   PARAMS ((int));
919 static void s_mips_weakext
920   PARAMS ((int));
921 static void s_mips_file
922   PARAMS ((int));
923 static void s_mips_loc
924   PARAMS ((int));
925 static int mips16_extended_frag
926   PARAMS ((fragS *, asection *, long));
927 static int relaxed_branch_length (fragS *, asection *, int);
928 static int validate_mips_insn
929   PARAMS ((const struct mips_opcode *));
930 static void show
931   PARAMS ((FILE *, const char *, int *, int *));
932 #ifdef OBJ_ELF
933 static int mips_need_elf_addend_fixup
934   PARAMS ((fixS *));
935 #endif
936
937 /* Return values of my_getSmallExpression().  */
938
939 enum small_ex_type
940 {
941   S_EX_NONE = 0,
942   S_EX_REGISTER,
943
944   /* Direct relocation creation by %percent_op().  */
945   S_EX_HALF,
946   S_EX_HI,
947   S_EX_LO,
948   S_EX_GP_REL,
949   S_EX_GOT,
950   S_EX_CALL16,
951   S_EX_GOT_DISP,
952   S_EX_GOT_PAGE,
953   S_EX_GOT_OFST,
954   S_EX_GOT_HI,
955   S_EX_GOT_LO,
956   S_EX_NEG,
957   S_EX_HIGHER,
958   S_EX_HIGHEST,
959   S_EX_CALL_HI,
960   S_EX_CALL_LO
961 };
962
963 /* Table and functions used to map between CPU/ISA names, and
964    ISA levels, and CPU numbers.  */
965
966 struct mips_cpu_info
967 {
968   const char *name;           /* CPU or ISA name.  */
969   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
970   int isa;                    /* ISA level.  */
971   int cpu;                    /* CPU number (default CPU if ISA).  */
972 };
973
974 static void mips_set_architecture
975   PARAMS ((const struct mips_cpu_info *));
976 static void mips_set_tune
977   PARAMS ((const struct mips_cpu_info *));
978 static bfd_boolean mips_strict_matching_cpu_name_p
979   PARAMS ((const char *, const char *));
980 static bfd_boolean mips_matching_cpu_name_p
981   PARAMS ((const char *, const char *));
982 static const struct mips_cpu_info *mips_parse_cpu
983   PARAMS ((const char *, const char *));
984 static const struct mips_cpu_info *mips_cpu_info_from_isa
985   PARAMS ((int));
986 \f
987 /* Pseudo-op table.
988
989    The following pseudo-ops from the Kane and Heinrich MIPS book
990    should be defined here, but are currently unsupported: .alias,
991    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
992
993    The following pseudo-ops from the Kane and Heinrich MIPS book are
994    specific to the type of debugging information being generated, and
995    should be defined by the object format: .aent, .begin, .bend,
996    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
997    .vreg.
998
999    The following pseudo-ops from the Kane and Heinrich MIPS book are
1000    not MIPS CPU specific, but are also not specific to the object file
1001    format.  This file is probably the best place to define them, but
1002    they are not currently supported: .asm0, .endr, .lab, .repeat,
1003    .struct.  */
1004
1005 static const pseudo_typeS mips_pseudo_table[] =
1006 {
1007   /* MIPS specific pseudo-ops.  */
1008   {"option", s_option, 0},
1009   {"set", s_mipsset, 0},
1010   {"rdata", s_change_sec, 'r'},
1011   {"sdata", s_change_sec, 's'},
1012   {"livereg", s_ignore, 0},
1013   {"abicalls", s_abicalls, 0},
1014   {"cpload", s_cpload, 0},
1015   {"cpsetup", s_cpsetup, 0},
1016   {"cplocal", s_cplocal, 0},
1017   {"cprestore", s_cprestore, 0},
1018   {"cpreturn", s_cpreturn, 0},
1019   {"gpvalue", s_gpvalue, 0},
1020   {"gpword", s_gpword, 0},
1021   {"gpdword", s_gpdword, 0},
1022   {"cpadd", s_cpadd, 0},
1023   {"insn", s_insn, 0},
1024
1025   /* Relatively generic pseudo-ops that happen to be used on MIPS
1026      chips.  */
1027   {"asciiz", stringer, 1},
1028   {"bss", s_change_sec, 'b'},
1029   {"err", s_err, 0},
1030   {"half", s_cons, 1},
1031   {"dword", s_cons, 3},
1032   {"weakext", s_mips_weakext, 0},
1033
1034   /* These pseudo-ops are defined in read.c, but must be overridden
1035      here for one reason or another.  */
1036   {"align", s_align, 0},
1037   {"byte", s_cons, 0},
1038   {"data", s_change_sec, 'd'},
1039   {"double", s_float_cons, 'd'},
1040   {"float", s_float_cons, 'f'},
1041   {"globl", s_mips_globl, 0},
1042   {"global", s_mips_globl, 0},
1043   {"hword", s_cons, 1},
1044   {"int", s_cons, 2},
1045   {"long", s_cons, 2},
1046   {"octa", s_cons, 4},
1047   {"quad", s_cons, 3},
1048   {"section", s_change_section, 0},
1049   {"short", s_cons, 1},
1050   {"single", s_float_cons, 'f'},
1051   {"stabn", s_mips_stab, 'n'},
1052   {"text", s_change_sec, 't'},
1053   {"word", s_cons, 2},
1054
1055   { "extern", ecoff_directive_extern, 0},
1056
1057   { NULL, NULL, 0 },
1058 };
1059
1060 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1061 {
1062   /* These pseudo-ops should be defined by the object file format.
1063      However, a.out doesn't support them, so we have versions here.  */
1064   {"aent", s_mips_ent, 1},
1065   {"bgnb", s_ignore, 0},
1066   {"end", s_mips_end, 0},
1067   {"endb", s_ignore, 0},
1068   {"ent", s_mips_ent, 0},
1069   {"file", s_mips_file, 0},
1070   {"fmask", s_mips_mask, 'F'},
1071   {"frame", s_mips_frame, 0},
1072   {"loc", s_mips_loc, 0},
1073   {"mask", s_mips_mask, 'R'},
1074   {"verstamp", s_ignore, 0},
1075   { NULL, NULL, 0 },
1076 };
1077
1078 extern void pop_insert PARAMS ((const pseudo_typeS *));
1079
1080 void
1081 mips_pop_insert ()
1082 {
1083   pop_insert (mips_pseudo_table);
1084   if (! ECOFF_DEBUGGING)
1085     pop_insert (mips_nonecoff_pseudo_table);
1086 }
1087 \f
1088 /* Symbols labelling the current insn.  */
1089
1090 struct insn_label_list
1091 {
1092   struct insn_label_list *next;
1093   symbolS *label;
1094 };
1095
1096 static struct insn_label_list *insn_labels;
1097 static struct insn_label_list *free_insn_labels;
1098
1099 static void mips_clear_insn_labels PARAMS ((void));
1100
1101 static inline void
1102 mips_clear_insn_labels ()
1103 {
1104   register struct insn_label_list **pl;
1105
1106   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1107     ;
1108   *pl = insn_labels;
1109   insn_labels = NULL;
1110 }
1111 \f
1112 static char *expr_end;
1113
1114 /* Expressions which appear in instructions.  These are set by
1115    mips_ip.  */
1116
1117 static expressionS imm_expr;
1118 static expressionS offset_expr;
1119
1120 /* Relocs associated with imm_expr and offset_expr.  */
1121
1122 static bfd_reloc_code_real_type imm_reloc[3]
1123   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1124 static bfd_reloc_code_real_type offset_reloc[3]
1125   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1126
1127 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
1128
1129 static bfd_boolean imm_unmatched_hi;
1130
1131 /* These are set by mips16_ip if an explicit extension is used.  */
1132
1133 static bfd_boolean mips16_small, mips16_ext;
1134
1135 #ifdef OBJ_ELF
1136 /* The pdr segment for per procedure frame/regmask info.  Not used for
1137    ECOFF debugging.  */
1138
1139 static segT pdr_seg;
1140 #endif
1141
1142 /* The default target format to use.  */
1143
1144 const char *
1145 mips_target_format ()
1146 {
1147   switch (OUTPUT_FLAVOR)
1148     {
1149     case bfd_target_aout_flavour:
1150       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1151     case bfd_target_ecoff_flavour:
1152       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1153     case bfd_target_coff_flavour:
1154       return "pe-mips";
1155     case bfd_target_elf_flavour:
1156 #ifdef TE_TMIPS
1157       /* This is traditional mips.  */
1158       return (target_big_endian
1159               ? (HAVE_64BIT_OBJECTS
1160                  ? "elf64-tradbigmips"
1161                  : (HAVE_NEWABI
1162                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1163               : (HAVE_64BIT_OBJECTS
1164                  ? "elf64-tradlittlemips"
1165                  : (HAVE_NEWABI
1166                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1167 #else
1168       return (target_big_endian
1169               ? (HAVE_64BIT_OBJECTS
1170                  ? "elf64-bigmips"
1171                  : (HAVE_NEWABI
1172                     ? "elf32-nbigmips" : "elf32-bigmips"))
1173               : (HAVE_64BIT_OBJECTS
1174                  ? "elf64-littlemips"
1175                  : (HAVE_NEWABI
1176                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1177 #endif
1178     default:
1179       abort ();
1180       return NULL;
1181     }
1182 }
1183
1184 /* This function is called once, at assembler startup time.  It should
1185    set up all the tables, etc. that the MD part of the assembler will need.  */
1186
1187 void
1188 md_begin ()
1189 {
1190   register const char *retval = NULL;
1191   int i = 0;
1192   int broken = 0;
1193
1194   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1195     as_warn (_("Could not set architecture and machine"));
1196
1197   op_hash = hash_new ();
1198
1199   for (i = 0; i < NUMOPCODES;)
1200     {
1201       const char *name = mips_opcodes[i].name;
1202
1203       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1204       if (retval != NULL)
1205         {
1206           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1207                    mips_opcodes[i].name, retval);
1208           /* Probably a memory allocation problem?  Give up now.  */
1209           as_fatal (_("Broken assembler.  No assembly attempted."));
1210         }
1211       do
1212         {
1213           if (mips_opcodes[i].pinfo != INSN_MACRO)
1214             {
1215               if (!validate_mips_insn (&mips_opcodes[i]))
1216                 broken = 1;
1217             }
1218           ++i;
1219         }
1220       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1221     }
1222
1223   mips16_op_hash = hash_new ();
1224
1225   i = 0;
1226   while (i < bfd_mips16_num_opcodes)
1227     {
1228       const char *name = mips16_opcodes[i].name;
1229
1230       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1231       if (retval != NULL)
1232         as_fatal (_("internal: can't hash `%s': %s"),
1233                   mips16_opcodes[i].name, retval);
1234       do
1235         {
1236           if (mips16_opcodes[i].pinfo != INSN_MACRO
1237               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1238                   != mips16_opcodes[i].match))
1239             {
1240               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1241                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1242               broken = 1;
1243             }
1244           ++i;
1245         }
1246       while (i < bfd_mips16_num_opcodes
1247              && strcmp (mips16_opcodes[i].name, name) == 0);
1248     }
1249
1250   if (broken)
1251     as_fatal (_("Broken assembler.  No assembly attempted."));
1252
1253   /* We add all the general register names to the symbol table.  This
1254      helps us detect invalid uses of them.  */
1255   for (i = 0; i < 32; i++)
1256     {
1257       char buf[5];
1258
1259       sprintf (buf, "$%d", i);
1260       symbol_table_insert (symbol_new (buf, reg_section, i,
1261                                        &zero_address_frag));
1262     }
1263   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1264                                    &zero_address_frag));
1265   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1266                                    &zero_address_frag));
1267   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1268                                    &zero_address_frag));
1269   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1270                                    &zero_address_frag));
1271   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1272                                    &zero_address_frag));
1273   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1274                                    &zero_address_frag));
1275   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1276                                    &zero_address_frag));
1277   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1278                                    &zero_address_frag));
1279   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1280                                    &zero_address_frag));
1281
1282   /* If we don't add these register names to the symbol table, they
1283      may end up being added as regular symbols by operand(), and then
1284      make it to the object file as undefined in case they're not
1285      regarded as local symbols.  They're local in o32, since `$' is a
1286      local symbol prefix, but not in n32 or n64.  */
1287   for (i = 0; i < 8; i++)
1288     {
1289       char buf[6];
1290
1291       sprintf (buf, "$fcc%i", i);
1292       symbol_table_insert (symbol_new (buf, reg_section, -1,
1293                                        &zero_address_frag));
1294     }
1295
1296   mips_no_prev_insn (FALSE);
1297
1298   mips_gprmask = 0;
1299   mips_cprmask[0] = 0;
1300   mips_cprmask[1] = 0;
1301   mips_cprmask[2] = 0;
1302   mips_cprmask[3] = 0;
1303
1304   /* set the default alignment for the text section (2**2) */
1305   record_alignment (text_section, 2);
1306
1307   if (USE_GLOBAL_POINTER_OPT)
1308     bfd_set_gp_size (stdoutput, g_switch_value);
1309
1310   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1311     {
1312       /* On a native system, sections must be aligned to 16 byte
1313          boundaries.  When configured for an embedded ELF target, we
1314          don't bother.  */
1315       if (strcmp (TARGET_OS, "elf") != 0)
1316         {
1317           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1318           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1319           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1320         }
1321
1322       /* Create a .reginfo section for register masks and a .mdebug
1323          section for debugging information.  */
1324       {
1325         segT seg;
1326         subsegT subseg;
1327         flagword flags;
1328         segT sec;
1329
1330         seg = now_seg;
1331         subseg = now_subseg;
1332
1333         /* The ABI says this section should be loaded so that the
1334            running program can access it.  However, we don't load it
1335            if we are configured for an embedded target */
1336         flags = SEC_READONLY | SEC_DATA;
1337         if (strcmp (TARGET_OS, "elf") != 0)
1338           flags |= SEC_ALLOC | SEC_LOAD;
1339
1340         if (mips_abi != N64_ABI)
1341           {
1342             sec = subseg_new (".reginfo", (subsegT) 0);
1343
1344             bfd_set_section_flags (stdoutput, sec, flags);
1345             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1346
1347 #ifdef OBJ_ELF
1348             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1349 #endif
1350           }
1351         else
1352           {
1353             /* The 64-bit ABI uses a .MIPS.options section rather than
1354                .reginfo section.  */
1355             sec = subseg_new (".MIPS.options", (subsegT) 0);
1356             bfd_set_section_flags (stdoutput, sec, flags);
1357             bfd_set_section_alignment (stdoutput, sec, 3);
1358
1359 #ifdef OBJ_ELF
1360             /* Set up the option header.  */
1361             {
1362               Elf_Internal_Options opthdr;
1363               char *f;
1364
1365               opthdr.kind = ODK_REGINFO;
1366               opthdr.size = (sizeof (Elf_External_Options)
1367                              + sizeof (Elf64_External_RegInfo));
1368               opthdr.section = 0;
1369               opthdr.info = 0;
1370               f = frag_more (sizeof (Elf_External_Options));
1371               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1372                                              (Elf_External_Options *) f);
1373
1374               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1375             }
1376 #endif
1377           }
1378
1379         if (ECOFF_DEBUGGING)
1380           {
1381             sec = subseg_new (".mdebug", (subsegT) 0);
1382             (void) bfd_set_section_flags (stdoutput, sec,
1383                                           SEC_HAS_CONTENTS | SEC_READONLY);
1384             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1385           }
1386 #ifdef OBJ_ELF
1387         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1388           {
1389             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1390             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1391                                           SEC_READONLY | SEC_RELOC
1392                                           | SEC_DEBUGGING);
1393             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1394           }
1395 #endif
1396
1397         subseg_set (seg, subseg);
1398       }
1399     }
1400
1401   if (! ECOFF_DEBUGGING)
1402     md_obj_begin ();
1403 }
1404
1405 void
1406 md_mips_end ()
1407 {
1408   if (! ECOFF_DEBUGGING)
1409     md_obj_end ();
1410 }
1411
1412 void
1413 md_assemble (str)
1414      char *str;
1415 {
1416   struct mips_cl_insn insn;
1417   bfd_reloc_code_real_type unused_reloc[3]
1418     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1419
1420   imm_expr.X_op = O_absent;
1421   imm_unmatched_hi = FALSE;
1422   offset_expr.X_op = O_absent;
1423   imm_reloc[0] = BFD_RELOC_UNUSED;
1424   imm_reloc[1] = BFD_RELOC_UNUSED;
1425   imm_reloc[2] = BFD_RELOC_UNUSED;
1426   offset_reloc[0] = BFD_RELOC_UNUSED;
1427   offset_reloc[1] = BFD_RELOC_UNUSED;
1428   offset_reloc[2] = BFD_RELOC_UNUSED;
1429
1430   if (mips_opts.mips16)
1431     mips16_ip (str, &insn);
1432   else
1433     {
1434       mips_ip (str, &insn);
1435       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1436             str, insn.insn_opcode));
1437     }
1438
1439   if (insn_error)
1440     {
1441       as_bad ("%s `%s'", insn_error, str);
1442       return;
1443     }
1444
1445   if (insn.insn_mo->pinfo == INSN_MACRO)
1446     {
1447       if (mips_opts.mips16)
1448         mips16_macro (&insn);
1449       else
1450         macro (&insn);
1451     }
1452   else
1453     {
1454       if (imm_expr.X_op != O_absent)
1455         append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1456       else if (offset_expr.X_op != O_absent)
1457         append_insn (NULL, &insn, &offset_expr, offset_reloc, FALSE);
1458       else
1459         append_insn (NULL, &insn, NULL, unused_reloc, FALSE);
1460     }
1461 }
1462
1463 /* See whether instruction IP reads register REG.  CLASS is the type
1464    of register.  */
1465
1466 static int
1467 insn_uses_reg (ip, reg, class)
1468      struct mips_cl_insn *ip;
1469      unsigned int reg;
1470      enum mips_regclass class;
1471 {
1472   if (class == MIPS16_REG)
1473     {
1474       assert (mips_opts.mips16);
1475       reg = mips16_to_32_reg_map[reg];
1476       class = MIPS_GR_REG;
1477     }
1478
1479   /* Don't report on general register ZERO, since it never changes.  */
1480   if (class == MIPS_GR_REG && reg == ZERO)
1481     return 0;
1482
1483   if (class == MIPS_FP_REG)
1484     {
1485       assert (! mips_opts.mips16);
1486       /* If we are called with either $f0 or $f1, we must check $f0.
1487          This is not optimal, because it will introduce an unnecessary
1488          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1489          need to distinguish reading both $f0 and $f1 or just one of
1490          them.  Note that we don't have to check the other way,
1491          because there is no instruction that sets both $f0 and $f1
1492          and requires a delay.  */
1493       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1494           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1495               == (reg &~ (unsigned) 1)))
1496         return 1;
1497       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1498           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1499               == (reg &~ (unsigned) 1)))
1500         return 1;
1501     }
1502   else if (! mips_opts.mips16)
1503     {
1504       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1505           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1506         return 1;
1507       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1508           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1509         return 1;
1510     }
1511   else
1512     {
1513       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1514           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1515                                     & MIPS16OP_MASK_RX)]
1516               == reg))
1517         return 1;
1518       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1519           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1520                                     & MIPS16OP_MASK_RY)]
1521               == reg))
1522         return 1;
1523       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1524           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1525                                     & MIPS16OP_MASK_MOVE32Z)]
1526               == reg))
1527         return 1;
1528       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1529         return 1;
1530       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1531         return 1;
1532       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1533         return 1;
1534       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1535           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1536               & MIPS16OP_MASK_REGR32) == reg)
1537         return 1;
1538     }
1539
1540   return 0;
1541 }
1542
1543 /* This function returns true if modifying a register requires a
1544    delay.  */
1545
1546 static int
1547 reg_needs_delay (reg)
1548      unsigned int reg;
1549 {
1550   unsigned long prev_pinfo;
1551
1552   prev_pinfo = prev_insn.insn_mo->pinfo;
1553   if (! mips_opts.noreorder
1554       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1555       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1556           || (! gpr_interlocks
1557               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1558     {
1559       /* A load from a coprocessor or from memory.  All load
1560          delays delay the use of general register rt for one
1561          instruction on the r3000.  The r6000 and r4000 use
1562          interlocks.  */
1563       /* Itbl support may require additional care here.  */
1564       know (prev_pinfo & INSN_WRITE_GPR_T);
1565       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1566         return 1;
1567     }
1568
1569   return 0;
1570 }
1571
1572 /* Mark instruction labels in mips16 mode.  This permits the linker to
1573    handle them specially, such as generating jalx instructions when
1574    needed.  We also make them odd for the duration of the assembly, in
1575    order to generate the right sort of code.  We will make them even
1576    in the adjust_symtab routine, while leaving them marked.  This is
1577    convenient for the debugger and the disassembler.  The linker knows
1578    to make them odd again.  */
1579
1580 static void
1581 mips16_mark_labels ()
1582 {
1583   if (mips_opts.mips16)
1584     {
1585       struct insn_label_list *l;
1586       valueT val;
1587
1588       for (l = insn_labels; l != NULL; l = l->next)
1589         {
1590 #ifdef OBJ_ELF
1591           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1592             S_SET_OTHER (l->label, STO_MIPS16);
1593 #endif
1594           val = S_GET_VALUE (l->label);
1595           if ((val & 1) == 0)
1596             S_SET_VALUE (l->label, val + 1);
1597         }
1598     }
1599 }
1600
1601 /* Output an instruction.  PLACE is where to put the instruction; if
1602    it is NULL, this uses frag_more to get room.  IP is the instruction
1603    information.  ADDRESS_EXPR is an operand of the instruction to be
1604    used with RELOC_TYPE.  */
1605
1606 static void
1607 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1608      char *place;
1609      struct mips_cl_insn *ip;
1610      expressionS *address_expr;
1611      bfd_reloc_code_real_type *reloc_type;
1612      bfd_boolean unmatched_hi;
1613 {
1614   register unsigned long prev_pinfo, pinfo;
1615   char *f;
1616   fixS *fixp[3];
1617   int nops = 0;
1618
1619   /* Mark instruction labels in mips16 mode.  */
1620   mips16_mark_labels ();
1621
1622   prev_pinfo = prev_insn.insn_mo->pinfo;
1623   pinfo = ip->insn_mo->pinfo;
1624
1625   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1626     {
1627       int prev_prev_nop;
1628
1629       /* If the previous insn required any delay slots, see if we need
1630          to insert a NOP or two.  There are eight kinds of possible
1631          hazards, of which an instruction can have at most one type.
1632          (1) a load from memory delay
1633          (2) a load from a coprocessor delay
1634          (3) an unconditional branch delay
1635          (4) a conditional branch delay
1636          (5) a move to coprocessor register delay
1637          (6) a load coprocessor register from memory delay
1638          (7) a coprocessor condition code delay
1639          (8) a HI/LO special register delay
1640
1641          There are a lot of optimizations we could do that we don't.
1642          In particular, we do not, in general, reorder instructions.
1643          If you use gcc with optimization, it will reorder
1644          instructions and generally do much more optimization then we
1645          do here; repeating all that work in the assembler would only
1646          benefit hand written assembly code, and does not seem worth
1647          it.  */
1648
1649       /* This is how a NOP is emitted.  */
1650 #define emit_nop()                                      \
1651   (mips_opts.mips16                                     \
1652    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1653    : md_number_to_chars (frag_more (4), 0, 4))
1654
1655       /* The previous insn might require a delay slot, depending upon
1656          the contents of the current insn.  */
1657       if (! mips_opts.mips16
1658           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1659           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1660                && ! cop_interlocks)
1661               || (! gpr_interlocks
1662                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1663         {
1664           /* A load from a coprocessor or from memory.  All load
1665              delays delay the use of general register rt for one
1666              instruction on the r3000.  The r6000 and r4000 use
1667              interlocks.  */
1668           /* Itbl support may require additional care here.  */
1669           know (prev_pinfo & INSN_WRITE_GPR_T);
1670           if (mips_optimize == 0
1671               || insn_uses_reg (ip,
1672                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1673                                  & OP_MASK_RT),
1674                                 MIPS_GR_REG))
1675             ++nops;
1676         }
1677       else if (! mips_opts.mips16
1678                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1679                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1680                     && ! cop_interlocks)
1681                    || (mips_opts.isa == ISA_MIPS1
1682                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1683         {
1684           /* A generic coprocessor delay.  The previous instruction
1685              modified a coprocessor general or control register.  If
1686              it modified a control register, we need to avoid any
1687              coprocessor instruction (this is probably not always
1688              required, but it sometimes is).  If it modified a general
1689              register, we avoid using that register.
1690
1691              On the r6000 and r4000 loading a coprocessor register
1692              from memory is interlocked, and does not require a delay.
1693
1694              This case is not handled very well.  There is no special
1695              knowledge of CP0 handling, and the coprocessors other
1696              than the floating point unit are not distinguished at
1697              all.  */
1698           /* Itbl support may require additional care here. FIXME!
1699              Need to modify this to include knowledge about
1700              user specified delays!  */
1701           if (prev_pinfo & INSN_WRITE_FPR_T)
1702             {
1703               if (mips_optimize == 0
1704                   || insn_uses_reg (ip,
1705                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1706                                      & OP_MASK_FT),
1707                                     MIPS_FP_REG))
1708                 ++nops;
1709             }
1710           else if (prev_pinfo & INSN_WRITE_FPR_S)
1711             {
1712               if (mips_optimize == 0
1713                   || insn_uses_reg (ip,
1714                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1715                                      & OP_MASK_FS),
1716                                     MIPS_FP_REG))
1717                 ++nops;
1718             }
1719           else
1720             {
1721               /* We don't know exactly what the previous instruction
1722                  does.  If the current instruction uses a coprocessor
1723                  register, we must insert a NOP.  If previous
1724                  instruction may set the condition codes, and the
1725                  current instruction uses them, we must insert two
1726                  NOPS.  */
1727               /* Itbl support may require additional care here.  */
1728               if (mips_optimize == 0
1729                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1730                       && (pinfo & INSN_READ_COND_CODE)))
1731                 nops += 2;
1732               else if (pinfo & INSN_COP)
1733                 ++nops;
1734             }
1735         }
1736       else if (! mips_opts.mips16
1737                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1738                && (prev_pinfo & INSN_WRITE_COND_CODE)
1739                && ! cop_interlocks)
1740         {
1741           /* The previous instruction sets the coprocessor condition
1742              codes, but does not require a general coprocessor delay
1743              (this means it is a floating point comparison
1744              instruction).  If this instruction uses the condition
1745              codes, we need to insert a single NOP.  */
1746           /* Itbl support may require additional care here.  */
1747           if (mips_optimize == 0
1748               || (pinfo & INSN_READ_COND_CODE))
1749             ++nops;
1750         }
1751
1752       /* If we're fixing up mfhi/mflo for the r7000 and the
1753          previous insn was an mfhi/mflo and the current insn
1754          reads the register that the mfhi/mflo wrote to, then
1755          insert two nops.  */
1756
1757       else if (mips_7000_hilo_fix
1758                && MF_HILO_INSN (prev_pinfo)
1759                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1760                                       & OP_MASK_RD),
1761                                  MIPS_GR_REG))
1762         {
1763           nops += 2;
1764         }
1765
1766       /* If we're fixing up mfhi/mflo for the r7000 and the
1767          2nd previous insn was an mfhi/mflo and the current insn
1768          reads the register that the mfhi/mflo wrote to, then
1769          insert one nop.  */
1770
1771       else if (mips_7000_hilo_fix
1772                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1773                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1774                                        & OP_MASK_RD),
1775                                     MIPS_GR_REG))
1776
1777         {
1778           ++nops;
1779         }
1780
1781       else if (prev_pinfo & INSN_READ_LO)
1782         {
1783           /* The previous instruction reads the LO register; if the
1784              current instruction writes to the LO register, we must
1785              insert two NOPS.  Some newer processors have interlocks.
1786              Also the tx39's multiply instructions can be exectuted
1787              immediatly after a read from HI/LO (without the delay),
1788              though the tx39's divide insns still do require the
1789              delay.  */
1790           if (! (hilo_interlocks
1791                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1792               && (mips_optimize == 0
1793                   || (pinfo & INSN_WRITE_LO)))
1794             nops += 2;
1795           /* Most mips16 branch insns don't have a delay slot.
1796              If a read from LO is immediately followed by a branch
1797              to a write to LO we have a read followed by a write
1798              less than 2 insns away.  We assume the target of
1799              a branch might be a write to LO, and insert a nop
1800              between a read and an immediately following branch.  */
1801           else if (mips_opts.mips16
1802                    && (mips_optimize == 0
1803                        || (pinfo & MIPS16_INSN_BRANCH)))
1804             ++nops;
1805         }
1806       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1807         {
1808           /* The previous instruction reads the HI register; if the
1809              current instruction writes to the HI register, we must
1810              insert a NOP.  Some newer processors have interlocks.
1811              Also the note tx39's multiply above.  */
1812           if (! (hilo_interlocks
1813                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1814               && (mips_optimize == 0
1815                   || (pinfo & INSN_WRITE_HI)))
1816             nops += 2;
1817           /* Most mips16 branch insns don't have a delay slot.
1818              If a read from HI is immediately followed by a branch
1819              to a write to HI we have a read followed by a write
1820              less than 2 insns away.  We assume the target of
1821              a branch might be a write to HI, and insert a nop
1822              between a read and an immediately following branch.  */
1823           else if (mips_opts.mips16
1824                    && (mips_optimize == 0
1825                        || (pinfo & MIPS16_INSN_BRANCH)))
1826             ++nops;
1827         }
1828
1829       /* If the previous instruction was in a noreorder section, then
1830          we don't want to insert the nop after all.  */
1831       /* Itbl support may require additional care here.  */
1832       if (prev_insn_unreordered)
1833         nops = 0;
1834
1835       /* There are two cases which require two intervening
1836          instructions: 1) setting the condition codes using a move to
1837          coprocessor instruction which requires a general coprocessor
1838          delay and then reading the condition codes 2) reading the HI
1839          or LO register and then writing to it (except on processors
1840          which have interlocks).  If we are not already emitting a NOP
1841          instruction, we must check for these cases compared to the
1842          instruction previous to the previous instruction.  */
1843       if ((! mips_opts.mips16
1844            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1845            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1846            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1847            && (pinfo & INSN_READ_COND_CODE)
1848            && ! cop_interlocks)
1849           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1850               && (pinfo & INSN_WRITE_LO)
1851               && ! (hilo_interlocks
1852                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1853           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1854               && (pinfo & INSN_WRITE_HI)
1855               && ! (hilo_interlocks
1856                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1857         prev_prev_nop = 1;
1858       else
1859         prev_prev_nop = 0;
1860
1861       if (prev_prev_insn_unreordered)
1862         prev_prev_nop = 0;
1863
1864       if (prev_prev_nop && nops == 0)
1865         ++nops;
1866
1867       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1868         {
1869           /* We're out of bits in pinfo, so we must resort to string
1870              ops here.  Shortcuts are selected based on opcodes being
1871              limited to the VR4122 instruction set.  */
1872           int min_nops = 0;
1873           const char *pn = prev_insn.insn_mo->name;
1874           const char *tn = ip->insn_mo->name;
1875           if (strncmp(pn, "macc", 4) == 0
1876               || strncmp(pn, "dmacc", 5) == 0)
1877             {
1878               /* Errata 21 - [D]DIV[U] after [D]MACC */
1879               if (strstr (tn, "div"))
1880                 {
1881                   min_nops = 1;
1882                 }
1883
1884               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1885               if (pn[0] == 'd' /* dmacc */
1886                   && (strncmp(tn, "dmult", 5) == 0
1887                       || strncmp(tn, "dmacc", 5) == 0))
1888                 {
1889                   min_nops = 1;
1890                 }
1891
1892               /* Errata 24 - MT{LO,HI} after [D]MACC */
1893               if (strcmp (tn, "mtlo") == 0
1894                   || strcmp (tn, "mthi") == 0)
1895                 {
1896                   min_nops = 1;
1897                 }
1898
1899             }
1900           else if (strncmp(pn, "dmult", 5) == 0
1901                    && (strncmp(tn, "dmult", 5) == 0
1902                        || strncmp(tn, "dmacc", 5) == 0))
1903             {
1904               /* Here is the rest of errata 23.  */
1905               min_nops = 1;
1906             }
1907           if (nops < min_nops)
1908             nops = min_nops;
1909         }
1910
1911       /* If we are being given a nop instruction, don't bother with
1912          one of the nops we would otherwise output.  This will only
1913          happen when a nop instruction is used with mips_optimize set
1914          to 0.  */
1915       if (nops > 0
1916           && ! mips_opts.noreorder
1917           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1918         --nops;
1919
1920       /* Now emit the right number of NOP instructions.  */
1921       if (nops > 0 && ! mips_opts.noreorder)
1922         {
1923           fragS *old_frag;
1924           unsigned long old_frag_offset;
1925           int i;
1926           struct insn_label_list *l;
1927
1928           old_frag = frag_now;
1929           old_frag_offset = frag_now_fix ();
1930
1931           for (i = 0; i < nops; i++)
1932             emit_nop ();
1933
1934           if (listing)
1935             {
1936               listing_prev_line ();
1937               /* We may be at the start of a variant frag.  In case we
1938                  are, make sure there is enough space for the frag
1939                  after the frags created by listing_prev_line.  The
1940                  argument to frag_grow here must be at least as large
1941                  as the argument to all other calls to frag_grow in
1942                  this file.  We don't have to worry about being in the
1943                  middle of a variant frag, because the variants insert
1944                  all needed nop instructions themselves.  */
1945               frag_grow (40);
1946             }
1947
1948           for (l = insn_labels; l != NULL; l = l->next)
1949             {
1950               valueT val;
1951
1952               assert (S_GET_SEGMENT (l->label) == now_seg);
1953               symbol_set_frag (l->label, frag_now);
1954               val = (valueT) frag_now_fix ();
1955               /* mips16 text labels are stored as odd.  */
1956               if (mips_opts.mips16)
1957                 ++val;
1958               S_SET_VALUE (l->label, val);
1959             }
1960
1961 #ifndef NO_ECOFF_DEBUGGING
1962           if (ECOFF_DEBUGGING)
1963             ecoff_fix_loc (old_frag, old_frag_offset);
1964 #endif
1965         }
1966       else if (prev_nop_frag != NULL)
1967         {
1968           /* We have a frag holding nops we may be able to remove.  If
1969              we don't need any nops, we can decrease the size of
1970              prev_nop_frag by the size of one instruction.  If we do
1971              need some nops, we count them in prev_nops_required.  */
1972           if (prev_nop_frag_since == 0)
1973             {
1974               if (nops == 0)
1975                 {
1976                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1977                   --prev_nop_frag_holds;
1978                 }
1979               else
1980                 prev_nop_frag_required += nops;
1981             }
1982           else
1983             {
1984               if (prev_prev_nop == 0)
1985                 {
1986                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1987                   --prev_nop_frag_holds;
1988                 }
1989               else
1990                 ++prev_nop_frag_required;
1991             }
1992
1993           if (prev_nop_frag_holds <= prev_nop_frag_required)
1994             prev_nop_frag = NULL;
1995
1996           ++prev_nop_frag_since;
1997
1998           /* Sanity check: by the time we reach the second instruction
1999              after prev_nop_frag, we should have used up all the nops
2000              one way or another.  */
2001           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2002         }
2003     }
2004
2005   if (place == NULL
2006       && address_expr
2007       && *reloc_type == BFD_RELOC_16_PCREL_S2
2008       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2009           || pinfo & INSN_COND_BRANCH_LIKELY)
2010       && mips_relax_branch
2011       /* Don't try branch relaxation within .set nomacro, or within
2012          .set noat if we use $at for PIC computations.  If it turns
2013          out that the branch was out-of-range, we'll get an error.  */
2014       && !mips_opts.warn_about_macros
2015       && !(mips_opts.noat && mips_pic != NO_PIC)
2016       && !mips_opts.mips16)
2017     {
2018       f = frag_var (rs_machine_dependent,
2019                     relaxed_branch_length
2020                     (NULL, NULL,
2021                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2022                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2023                     RELAX_BRANCH_ENCODE
2024                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2025                      pinfo & INSN_COND_BRANCH_LIKELY,
2026                      pinfo & INSN_WRITE_GPR_31,
2027                      0),
2028                     address_expr->X_add_symbol,
2029                     address_expr->X_add_number,
2030                     0);
2031       *reloc_type = BFD_RELOC_UNUSED;
2032     }
2033   else if (*reloc_type > BFD_RELOC_UNUSED)
2034     {
2035       /* We need to set up a variant frag.  */
2036       assert (mips_opts.mips16 && address_expr != NULL);
2037       f = frag_var (rs_machine_dependent, 4, 0,
2038                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2039                                          mips16_small, mips16_ext,
2040                                          (prev_pinfo
2041                                           & INSN_UNCOND_BRANCH_DELAY),
2042                                          (*prev_insn_reloc_type
2043                                           == BFD_RELOC_MIPS16_JMP)),
2044                     make_expr_symbol (address_expr), 0, NULL);
2045     }
2046   else if (place != NULL)
2047     f = place;
2048   else if (mips_opts.mips16
2049            && ! ip->use_extend
2050            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2051     {
2052       /* Make sure there is enough room to swap this instruction with
2053          a following jump instruction.  */
2054       frag_grow (6);
2055       f = frag_more (2);
2056     }
2057   else
2058     {
2059       if (mips_opts.mips16
2060           && mips_opts.noreorder
2061           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2062         as_warn (_("extended instruction in delay slot"));
2063
2064       f = frag_more (4);
2065     }
2066
2067   fixp[0] = fixp[1] = fixp[2] = NULL;
2068   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2069     {
2070       if (address_expr->X_op == O_constant)
2071         {
2072           valueT tmp;
2073
2074           switch (*reloc_type)
2075             {
2076             case BFD_RELOC_32:
2077               ip->insn_opcode |= address_expr->X_add_number;
2078               break;
2079
2080             case BFD_RELOC_MIPS_HIGHEST:
2081               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2082               tmp >>= 16;
2083               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2084               break;
2085
2086             case BFD_RELOC_MIPS_HIGHER:
2087               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2088               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2089               break;
2090
2091             case BFD_RELOC_HI16_S:
2092               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2093                                   >> 16) & 0xffff;
2094               break;
2095
2096             case BFD_RELOC_HI16:
2097               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2098               break;
2099
2100             case BFD_RELOC_LO16:
2101             case BFD_RELOC_MIPS_GOT_DISP:
2102               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2103               break;
2104
2105             case BFD_RELOC_MIPS_JMP:
2106               if ((address_expr->X_add_number & 3) != 0)
2107                 as_bad (_("jump to misaligned address (0x%lx)"),
2108                         (unsigned long) address_expr->X_add_number);
2109               if (address_expr->X_add_number & ~0xfffffff)
2110                 as_bad (_("jump address range overflow (0x%lx)"),
2111                         (unsigned long) address_expr->X_add_number);
2112               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2113               break;
2114
2115             case BFD_RELOC_MIPS16_JMP:
2116               if ((address_expr->X_add_number & 3) != 0)
2117                 as_bad (_("jump to misaligned address (0x%lx)"),
2118                         (unsigned long) address_expr->X_add_number);
2119               if (address_expr->X_add_number & ~0xfffffff)
2120                 as_bad (_("jump address range overflow (0x%lx)"),
2121                         (unsigned long) address_expr->X_add_number);
2122               ip->insn_opcode |=
2123                 (((address_expr->X_add_number & 0x7c0000) << 3)
2124                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2125                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2126               break;
2127
2128             case BFD_RELOC_16_PCREL_S2:
2129               goto need_reloc;
2130
2131             default:
2132               internalError ();
2133             }
2134         }
2135       else
2136         {
2137         need_reloc:
2138           /* Don't generate a reloc if we are writing into a variant frag.  */
2139           if (place == NULL)
2140             {
2141               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2142                                      address_expr,
2143                                      *reloc_type == BFD_RELOC_16_PCREL_S2,
2144                                      reloc_type[0]);
2145
2146               /* These relocations can have an addend that won't fit in
2147                  4 octets for 64bit assembly.  */
2148               if (HAVE_64BIT_GPRS &&
2149                   (*reloc_type == BFD_RELOC_16
2150                    || *reloc_type == BFD_RELOC_32
2151                    || *reloc_type == BFD_RELOC_MIPS_JMP
2152                    || *reloc_type == BFD_RELOC_HI16_S
2153                    || *reloc_type == BFD_RELOC_LO16
2154                    || *reloc_type == BFD_RELOC_GPREL16
2155                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2156                    || *reloc_type == BFD_RELOC_GPREL32
2157                    || *reloc_type == BFD_RELOC_64
2158                    || *reloc_type == BFD_RELOC_CTOR
2159                    || *reloc_type == BFD_RELOC_MIPS_SUB
2160                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2161                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2162                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2163                    || *reloc_type == BFD_RELOC_MIPS_REL16
2164                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2165                 fixp[0]->fx_no_overflow = 1;
2166
2167               if (unmatched_hi)
2168                 {
2169                   struct mips_hi_fixup *hi_fixup;
2170
2171                   assert (*reloc_type == BFD_RELOC_HI16_S);
2172                   hi_fixup = ((struct mips_hi_fixup *)
2173                               xmalloc (sizeof (struct mips_hi_fixup)));
2174                   hi_fixup->fixp = fixp[0];
2175                   hi_fixup->seg = now_seg;
2176                   hi_fixup->next = mips_hi_fixup_list;
2177                   mips_hi_fixup_list = hi_fixup;
2178                 }
2179
2180               if (reloc_type[1] != BFD_RELOC_UNUSED)
2181                 {
2182                   /* FIXME: This symbol can be one of
2183                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2184                   address_expr->X_op = O_absent;
2185                   address_expr->X_add_symbol = 0;
2186                   address_expr->X_add_number = 0;
2187
2188                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2189                                          4, address_expr, FALSE,
2190                                          reloc_type[1]);
2191
2192                   /* These relocations can have an addend that won't fit in
2193                      4 octets for 64bit assembly.  */
2194                   if (HAVE_64BIT_GPRS &&
2195                       (*reloc_type == BFD_RELOC_16
2196                        || *reloc_type == BFD_RELOC_32
2197                        || *reloc_type == BFD_RELOC_MIPS_JMP
2198                        || *reloc_type == BFD_RELOC_HI16_S
2199                        || *reloc_type == BFD_RELOC_LO16
2200                        || *reloc_type == BFD_RELOC_GPREL16
2201                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2202                        || *reloc_type == BFD_RELOC_GPREL32
2203                        || *reloc_type == BFD_RELOC_64
2204                        || *reloc_type == BFD_RELOC_CTOR
2205                        || *reloc_type == BFD_RELOC_MIPS_SUB
2206                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2207                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2208                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2209                        || *reloc_type == BFD_RELOC_MIPS_REL16
2210                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2211                     fixp[1]->fx_no_overflow = 1;
2212
2213                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2214                     {
2215                       address_expr->X_op = O_absent;
2216                       address_expr->X_add_symbol = 0;
2217                       address_expr->X_add_number = 0;
2218
2219                       fixp[2] = fix_new_exp (frag_now,
2220                                              f - frag_now->fr_literal, 4,
2221                                              address_expr, FALSE,
2222                                              reloc_type[2]);
2223
2224                       /* These relocations can have an addend that won't fit in
2225                          4 octets for 64bit assembly.  */
2226                       if (HAVE_64BIT_GPRS &&
2227                           (*reloc_type == BFD_RELOC_16
2228                            || *reloc_type == BFD_RELOC_32
2229                            || *reloc_type == BFD_RELOC_MIPS_JMP
2230                            || *reloc_type == BFD_RELOC_HI16_S
2231                            || *reloc_type == BFD_RELOC_LO16
2232                            || *reloc_type == BFD_RELOC_GPREL16
2233                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2234                            || *reloc_type == BFD_RELOC_GPREL32
2235                            || *reloc_type == BFD_RELOC_64
2236                            || *reloc_type == BFD_RELOC_CTOR
2237                            || *reloc_type == BFD_RELOC_MIPS_SUB
2238                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2239                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2240                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2241                            || *reloc_type == BFD_RELOC_MIPS_REL16
2242                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2243                         fixp[2]->fx_no_overflow = 1;
2244                     }
2245                 }
2246             }
2247         }
2248     }
2249
2250   if (! mips_opts.mips16)
2251     {
2252       md_number_to_chars (f, ip->insn_opcode, 4);
2253 #ifdef OBJ_ELF
2254       dwarf2_emit_insn (4);
2255 #endif
2256     }
2257   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2258     {
2259       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2260       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2261 #ifdef OBJ_ELF
2262       dwarf2_emit_insn (4);
2263 #endif
2264     }
2265   else
2266     {
2267       if (ip->use_extend)
2268         {
2269           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2270           f += 2;
2271         }
2272       md_number_to_chars (f, ip->insn_opcode, 2);
2273 #ifdef OBJ_ELF
2274       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2275 #endif
2276     }
2277
2278   /* Update the register mask information.  */
2279   if (! mips_opts.mips16)
2280     {
2281       if (pinfo & INSN_WRITE_GPR_D)
2282         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2283       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2284         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2285       if (pinfo & INSN_READ_GPR_S)
2286         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2287       if (pinfo & INSN_WRITE_GPR_31)
2288         mips_gprmask |= 1 << RA;
2289       if (pinfo & INSN_WRITE_FPR_D)
2290         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2291       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2292         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2293       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2294         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2295       if ((pinfo & INSN_READ_FPR_R) != 0)
2296         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2297       if (pinfo & INSN_COP)
2298         {
2299           /* We don't keep enough information to sort these cases out.
2300              The itbl support does keep this information however, although
2301              we currently don't support itbl fprmats as part of the cop
2302              instruction.  May want to add this support in the future.  */
2303         }
2304       /* Never set the bit for $0, which is always zero.  */
2305       mips_gprmask &= ~1 << 0;
2306     }
2307   else
2308     {
2309       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2310         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2311                               & MIPS16OP_MASK_RX);
2312       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2313         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2314                               & MIPS16OP_MASK_RY);
2315       if (pinfo & MIPS16_INSN_WRITE_Z)
2316         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2317                               & MIPS16OP_MASK_RZ);
2318       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2319         mips_gprmask |= 1 << TREG;
2320       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2321         mips_gprmask |= 1 << SP;
2322       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2323         mips_gprmask |= 1 << RA;
2324       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2325         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2326       if (pinfo & MIPS16_INSN_READ_Z)
2327         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2328                               & MIPS16OP_MASK_MOVE32Z);
2329       if (pinfo & MIPS16_INSN_READ_GPR_X)
2330         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2331                               & MIPS16OP_MASK_REGR32);
2332     }
2333
2334   if (place == NULL && ! mips_opts.noreorder)
2335     {
2336       /* Filling the branch delay slot is more complex.  We try to
2337          switch the branch with the previous instruction, which we can
2338          do if the previous instruction does not set up a condition
2339          that the branch tests and if the branch is not itself the
2340          target of any branch.  */
2341       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2342           || (pinfo & INSN_COND_BRANCH_DELAY))
2343         {
2344           if (mips_optimize < 2
2345               /* If we have seen .set volatile or .set nomove, don't
2346                  optimize.  */
2347               || mips_opts.nomove != 0
2348               /* If we had to emit any NOP instructions, then we
2349                  already know we can not swap.  */
2350               || nops != 0
2351               /* If we don't even know the previous insn, we can not
2352                  swap.  */
2353               || ! prev_insn_valid
2354               /* If the previous insn is already in a branch delay
2355                  slot, then we can not swap.  */
2356               || prev_insn_is_delay_slot
2357               /* If the previous previous insn was in a .set
2358                  noreorder, we can't swap.  Actually, the MIPS
2359                  assembler will swap in this situation.  However, gcc
2360                  configured -with-gnu-as will generate code like
2361                    .set noreorder
2362                    lw   $4,XXX
2363                    .set reorder
2364                    INSN
2365                    bne  $4,$0,foo
2366                  in which we can not swap the bne and INSN.  If gcc is
2367                  not configured -with-gnu-as, it does not output the
2368                  .set pseudo-ops.  We don't have to check
2369                  prev_insn_unreordered, because prev_insn_valid will
2370                  be 0 in that case.  We don't want to use
2371                  prev_prev_insn_valid, because we do want to be able
2372                  to swap at the start of a function.  */
2373               || prev_prev_insn_unreordered
2374               /* If the branch is itself the target of a branch, we
2375                  can not swap.  We cheat on this; all we check for is
2376                  whether there is a label on this instruction.  If
2377                  there are any branches to anything other than a
2378                  label, users must use .set noreorder.  */
2379               || insn_labels != NULL
2380               /* If the previous instruction is in a variant frag, we
2381                  can not do the swap.  This does not apply to the
2382                  mips16, which uses variant frags for different
2383                  purposes.  */
2384               || (! mips_opts.mips16
2385                   && prev_insn_frag->fr_type == rs_machine_dependent)
2386               /* If the branch reads the condition codes, we don't
2387                  even try to swap, because in the sequence
2388                    ctc1 $X,$31
2389                    INSN
2390                    INSN
2391                    bc1t LABEL
2392                  we can not swap, and I don't feel like handling that
2393                  case.  */
2394               || (! mips_opts.mips16
2395                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2396                   && (pinfo & INSN_READ_COND_CODE))
2397               /* We can not swap with an instruction that requires a
2398                  delay slot, becase the target of the branch might
2399                  interfere with that instruction.  */
2400               || (! mips_opts.mips16
2401                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2402                   && (prev_pinfo
2403               /* Itbl support may require additional care here.  */
2404                       & (INSN_LOAD_COPROC_DELAY
2405                          | INSN_COPROC_MOVE_DELAY
2406                          | INSN_WRITE_COND_CODE)))
2407               || (! (hilo_interlocks
2408                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2409                   && (prev_pinfo
2410                       & (INSN_READ_LO
2411                          | INSN_READ_HI)))
2412               || (! mips_opts.mips16
2413                   && ! gpr_interlocks
2414                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2415               || (! mips_opts.mips16
2416                   && mips_opts.isa == ISA_MIPS1
2417                   /* Itbl support may require additional care here.  */
2418                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2419               /* We can not swap with a branch instruction.  */
2420               || (prev_pinfo
2421                   & (INSN_UNCOND_BRANCH_DELAY
2422                      | INSN_COND_BRANCH_DELAY
2423                      | INSN_COND_BRANCH_LIKELY))
2424               /* We do not swap with a trap instruction, since it
2425                  complicates trap handlers to have the trap
2426                  instruction be in a delay slot.  */
2427               || (prev_pinfo & INSN_TRAP)
2428               /* If the branch reads a register that the previous
2429                  instruction sets, we can not swap.  */
2430               || (! mips_opts.mips16
2431                   && (prev_pinfo & INSN_WRITE_GPR_T)
2432                   && insn_uses_reg (ip,
2433                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2434                                      & OP_MASK_RT),
2435                                     MIPS_GR_REG))
2436               || (! mips_opts.mips16
2437                   && (prev_pinfo & INSN_WRITE_GPR_D)
2438                   && insn_uses_reg (ip,
2439                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2440                                      & OP_MASK_RD),
2441                                     MIPS_GR_REG))
2442               || (mips_opts.mips16
2443                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2444                        && insn_uses_reg (ip,
2445                                          ((prev_insn.insn_opcode
2446                                            >> MIPS16OP_SH_RX)
2447                                           & MIPS16OP_MASK_RX),
2448                                          MIPS16_REG))
2449                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2450                           && insn_uses_reg (ip,
2451                                             ((prev_insn.insn_opcode
2452                                               >> MIPS16OP_SH_RY)
2453                                              & MIPS16OP_MASK_RY),
2454                                             MIPS16_REG))
2455                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2456                           && insn_uses_reg (ip,
2457                                             ((prev_insn.insn_opcode
2458                                               >> MIPS16OP_SH_RZ)
2459                                              & MIPS16OP_MASK_RZ),
2460                                             MIPS16_REG))
2461                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2462                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2463                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2464                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2465                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2466                           && insn_uses_reg (ip,
2467                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2468                                                                      insn_opcode),
2469                                             MIPS_GR_REG))))
2470               /* If the branch writes a register that the previous
2471                  instruction sets, we can not swap (we know that
2472                  branches write only to RD or to $31).  */
2473               || (! mips_opts.mips16
2474                   && (prev_pinfo & INSN_WRITE_GPR_T)
2475                   && (((pinfo & INSN_WRITE_GPR_D)
2476                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2477                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2478                       || ((pinfo & INSN_WRITE_GPR_31)
2479                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2480                                & OP_MASK_RT)
2481                               == RA))))
2482               || (! mips_opts.mips16
2483                   && (prev_pinfo & INSN_WRITE_GPR_D)
2484                   && (((pinfo & INSN_WRITE_GPR_D)
2485                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2486                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2487                       || ((pinfo & INSN_WRITE_GPR_31)
2488                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2489                                & OP_MASK_RD)
2490                               == RA))))
2491               || (mips_opts.mips16
2492                   && (pinfo & MIPS16_INSN_WRITE_31)
2493                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2494                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2495                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2496                               == RA))))
2497               /* If the branch writes a register that the previous
2498                  instruction reads, we can not swap (we know that
2499                  branches only write to RD or to $31).  */
2500               || (! mips_opts.mips16
2501                   && (pinfo & INSN_WRITE_GPR_D)
2502                   && insn_uses_reg (&prev_insn,
2503                                     ((ip->insn_opcode >> OP_SH_RD)
2504                                      & OP_MASK_RD),
2505                                     MIPS_GR_REG))
2506               || (! mips_opts.mips16
2507                   && (pinfo & INSN_WRITE_GPR_31)
2508                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2509               || (mips_opts.mips16
2510                   && (pinfo & MIPS16_INSN_WRITE_31)
2511                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2512               /* If we are generating embedded PIC code, the branch
2513                  might be expanded into a sequence which uses $at, so
2514                  we can't swap with an instruction which reads it.  */
2515               || (mips_pic == EMBEDDED_PIC
2516                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2517               /* If the previous previous instruction has a load
2518                  delay, and sets a register that the branch reads, we
2519                  can not swap.  */
2520               || (! mips_opts.mips16
2521                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2522               /* Itbl support may require additional care here.  */
2523                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2524                       || (! gpr_interlocks
2525                           && (prev_prev_insn.insn_mo->pinfo
2526                               & INSN_LOAD_MEMORY_DELAY)))
2527                   && insn_uses_reg (ip,
2528                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2529                                      & OP_MASK_RT),
2530                                     MIPS_GR_REG))
2531               /* If one instruction sets a condition code and the
2532                  other one uses a condition code, we can not swap.  */
2533               || ((pinfo & INSN_READ_COND_CODE)
2534                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2535               || ((pinfo & INSN_WRITE_COND_CODE)
2536                   && (prev_pinfo & INSN_READ_COND_CODE))
2537               /* If the previous instruction uses the PC, we can not
2538                  swap.  */
2539               || (mips_opts.mips16
2540                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2541               /* If the previous instruction was extended, we can not
2542                  swap.  */
2543               || (mips_opts.mips16 && prev_insn_extended)
2544               /* If the previous instruction had a fixup in mips16
2545                  mode, we can not swap.  This normally means that the
2546                  previous instruction was a 4 byte branch anyhow.  */
2547               || (mips_opts.mips16 && prev_insn_fixp[0])
2548               /* If the previous instruction is a sync, sync.l, or
2549                  sync.p, we can not swap.  */
2550               || (prev_pinfo & INSN_SYNC))
2551             {
2552               /* We could do even better for unconditional branches to
2553                  portions of this object file; we could pick up the
2554                  instruction at the destination, put it in the delay
2555                  slot, and bump the destination address.  */
2556               emit_nop ();
2557               /* Update the previous insn information.  */
2558               prev_prev_insn = *ip;
2559               prev_insn.insn_mo = &dummy_opcode;
2560             }
2561           else
2562             {
2563               /* It looks like we can actually do the swap.  */
2564               if (! mips_opts.mips16)
2565                 {
2566                   char *prev_f;
2567                   char temp[4];
2568
2569                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2570                   memcpy (temp, prev_f, 4);
2571                   memcpy (prev_f, f, 4);
2572                   memcpy (f, temp, 4);
2573                   if (prev_insn_fixp[0])
2574                     {
2575                       prev_insn_fixp[0]->fx_frag = frag_now;
2576                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2577                     }
2578                   if (prev_insn_fixp[1])
2579                     {
2580                       prev_insn_fixp[1]->fx_frag = frag_now;
2581                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2582                     }
2583                   if (prev_insn_fixp[2])
2584                     {
2585                       prev_insn_fixp[2]->fx_frag = frag_now;
2586                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2587                     }
2588                   if (fixp[0])
2589                     {
2590                       fixp[0]->fx_frag = prev_insn_frag;
2591                       fixp[0]->fx_where = prev_insn_where;
2592                     }
2593                   if (fixp[1])
2594                     {
2595                       fixp[1]->fx_frag = prev_insn_frag;
2596                       fixp[1]->fx_where = prev_insn_where;
2597                     }
2598                   if (fixp[2])
2599                     {
2600                       fixp[2]->fx_frag = prev_insn_frag;
2601                       fixp[2]->fx_where = prev_insn_where;
2602                     }
2603                 }
2604               else
2605                 {
2606                   char *prev_f;
2607                   char temp[2];
2608
2609                   assert (prev_insn_fixp[0] == NULL);
2610                   assert (prev_insn_fixp[1] == NULL);
2611                   assert (prev_insn_fixp[2] == NULL);
2612                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2613                   memcpy (temp, prev_f, 2);
2614                   memcpy (prev_f, f, 2);
2615                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2616                     {
2617                       assert (*reloc_type == BFD_RELOC_UNUSED);
2618                       memcpy (f, temp, 2);
2619                     }
2620                   else
2621                     {
2622                       memcpy (f, f + 2, 2);
2623                       memcpy (f + 2, temp, 2);
2624                     }
2625                   if (fixp[0])
2626                     {
2627                       fixp[0]->fx_frag = prev_insn_frag;
2628                       fixp[0]->fx_where = prev_insn_where;
2629                     }
2630                   if (fixp[1])
2631                     {
2632                       fixp[1]->fx_frag = prev_insn_frag;
2633                       fixp[1]->fx_where = prev_insn_where;
2634                     }
2635                   if (fixp[2])
2636                     {
2637                       fixp[2]->fx_frag = prev_insn_frag;
2638                       fixp[2]->fx_where = prev_insn_where;
2639                     }
2640                 }
2641
2642               /* Update the previous insn information; leave prev_insn
2643                  unchanged.  */
2644               prev_prev_insn = *ip;
2645             }
2646           prev_insn_is_delay_slot = 1;
2647
2648           /* If that was an unconditional branch, forget the previous
2649              insn information.  */
2650           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2651             {
2652               prev_prev_insn.insn_mo = &dummy_opcode;
2653               prev_insn.insn_mo = &dummy_opcode;
2654             }
2655
2656           prev_insn_fixp[0] = NULL;
2657           prev_insn_fixp[1] = NULL;
2658           prev_insn_fixp[2] = NULL;
2659           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2660           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2661           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2662           prev_insn_extended = 0;
2663         }
2664       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2665         {
2666           /* We don't yet optimize a branch likely.  What we should do
2667              is look at the target, copy the instruction found there
2668              into the delay slot, and increment the branch to jump to
2669              the next instruction.  */
2670           emit_nop ();
2671           /* Update the previous insn information.  */
2672           prev_prev_insn = *ip;
2673           prev_insn.insn_mo = &dummy_opcode;
2674           prev_insn_fixp[0] = NULL;
2675           prev_insn_fixp[1] = NULL;
2676           prev_insn_fixp[2] = NULL;
2677           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2678           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2679           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2680           prev_insn_extended = 0;
2681         }
2682       else
2683         {
2684           /* Update the previous insn information.  */
2685           if (nops > 0)
2686             prev_prev_insn.insn_mo = &dummy_opcode;
2687           else
2688             prev_prev_insn = prev_insn;
2689           prev_insn = *ip;
2690
2691           /* Any time we see a branch, we always fill the delay slot
2692              immediately; since this insn is not a branch, we know it
2693              is not in a delay slot.  */
2694           prev_insn_is_delay_slot = 0;
2695
2696           prev_insn_fixp[0] = fixp[0];
2697           prev_insn_fixp[1] = fixp[1];
2698           prev_insn_fixp[2] = fixp[2];
2699           prev_insn_reloc_type[0] = reloc_type[0];
2700           prev_insn_reloc_type[1] = reloc_type[1];
2701           prev_insn_reloc_type[2] = reloc_type[2];
2702           if (mips_opts.mips16)
2703             prev_insn_extended = (ip->use_extend
2704                                   || *reloc_type > BFD_RELOC_UNUSED);
2705         }
2706
2707       prev_prev_insn_unreordered = prev_insn_unreordered;
2708       prev_insn_unreordered = 0;
2709       prev_insn_frag = frag_now;
2710       prev_insn_where = f - frag_now->fr_literal;
2711       prev_insn_valid = 1;
2712     }
2713   else if (place == NULL)
2714     {
2715       /* We need to record a bit of information even when we are not
2716          reordering, in order to determine the base address for mips16
2717          PC relative relocs.  */
2718       prev_prev_insn = prev_insn;
2719       prev_insn = *ip;
2720       prev_insn_reloc_type[0] = reloc_type[0];
2721       prev_insn_reloc_type[1] = reloc_type[1];
2722       prev_insn_reloc_type[2] = reloc_type[2];
2723       prev_prev_insn_unreordered = prev_insn_unreordered;
2724       prev_insn_unreordered = 1;
2725     }
2726
2727   /* We just output an insn, so the next one doesn't have a label.  */
2728   mips_clear_insn_labels ();
2729
2730   /* We must ensure that a fixup associated with an unmatched %hi
2731      reloc does not become a variant frag.  Otherwise, the
2732      rearrangement of %hi relocs in frob_file may confuse
2733      tc_gen_reloc.  */
2734   if (unmatched_hi)
2735     {
2736       frag_wane (frag_now);
2737       frag_new (0);
2738     }
2739 }
2740
2741 /* This function forgets that there was any previous instruction or
2742    label.  If PRESERVE is non-zero, it remembers enough information to
2743    know whether nops are needed before a noreorder section.  */
2744
2745 static void
2746 mips_no_prev_insn (preserve)
2747      int preserve;
2748 {
2749   if (! preserve)
2750     {
2751       prev_insn.insn_mo = &dummy_opcode;
2752       prev_prev_insn.insn_mo = &dummy_opcode;
2753       prev_nop_frag = NULL;
2754       prev_nop_frag_holds = 0;
2755       prev_nop_frag_required = 0;
2756       prev_nop_frag_since = 0;
2757     }
2758   prev_insn_valid = 0;
2759   prev_insn_is_delay_slot = 0;
2760   prev_insn_unreordered = 0;
2761   prev_insn_extended = 0;
2762   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2763   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2764   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2765   prev_prev_insn_unreordered = 0;
2766   mips_clear_insn_labels ();
2767 }
2768
2769 /* This function must be called whenever we turn on noreorder or emit
2770    something other than instructions.  It inserts any NOPS which might
2771    be needed by the previous instruction, and clears the information
2772    kept for the previous instructions.  The INSNS parameter is true if
2773    instructions are to follow.  */
2774
2775 static void
2776 mips_emit_delays (insns)
2777      bfd_boolean insns;
2778 {
2779   if (! mips_opts.noreorder)
2780     {
2781       int nops;
2782
2783       nops = 0;
2784       if ((! mips_opts.mips16
2785            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2786            && (! cop_interlocks
2787                && (prev_insn.insn_mo->pinfo
2788                    & (INSN_LOAD_COPROC_DELAY
2789                       | INSN_COPROC_MOVE_DELAY
2790                       | INSN_WRITE_COND_CODE))))
2791           || (! hilo_interlocks
2792               && (prev_insn.insn_mo->pinfo
2793                   & (INSN_READ_LO
2794                      | INSN_READ_HI)))
2795           || (! mips_opts.mips16
2796               && ! gpr_interlocks
2797               && (prev_insn.insn_mo->pinfo
2798                   & INSN_LOAD_MEMORY_DELAY))
2799           || (! mips_opts.mips16
2800               && mips_opts.isa == ISA_MIPS1
2801               && (prev_insn.insn_mo->pinfo
2802                   & INSN_COPROC_MEMORY_DELAY)))
2803         {
2804           /* Itbl support may require additional care here.  */
2805           ++nops;
2806           if ((! mips_opts.mips16
2807                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2808                && (! cop_interlocks
2809                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2810               || (! hilo_interlocks
2811                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2812                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2813             ++nops;
2814
2815           if (prev_insn_unreordered)
2816             nops = 0;
2817         }
2818       else if ((! mips_opts.mips16
2819                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2820                 && (! cop_interlocks
2821                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2822                || (! hilo_interlocks
2823                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2824                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2825         {
2826           /* Itbl support may require additional care here.  */
2827           if (! prev_prev_insn_unreordered)
2828             ++nops;
2829         }
2830
2831       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2832         {
2833           int min_nops = 0;
2834           const char *pn = prev_insn.insn_mo->name;
2835           if (strncmp(pn, "macc", 4) == 0
2836               || strncmp(pn, "dmacc", 5) == 0
2837               || strncmp(pn, "dmult", 5) == 0)
2838             {
2839               min_nops = 1;
2840             }
2841           if (nops < min_nops)
2842             nops = min_nops;
2843         }
2844
2845       if (nops > 0)
2846         {
2847           struct insn_label_list *l;
2848
2849           if (insns)
2850             {
2851               /* Record the frag which holds the nop instructions, so
2852                  that we can remove them if we don't need them.  */
2853               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2854               prev_nop_frag = frag_now;
2855               prev_nop_frag_holds = nops;
2856               prev_nop_frag_required = 0;
2857               prev_nop_frag_since = 0;
2858             }
2859
2860           for (; nops > 0; --nops)
2861             emit_nop ();
2862
2863           if (insns)
2864             {
2865               /* Move on to a new frag, so that it is safe to simply
2866                  decrease the size of prev_nop_frag.  */
2867               frag_wane (frag_now);
2868               frag_new (0);
2869             }
2870
2871           for (l = insn_labels; l != NULL; l = l->next)
2872             {
2873               valueT val;
2874
2875               assert (S_GET_SEGMENT (l->label) == now_seg);
2876               symbol_set_frag (l->label, frag_now);
2877               val = (valueT) frag_now_fix ();
2878               /* mips16 text labels are stored as odd.  */
2879               if (mips_opts.mips16)
2880                 ++val;
2881               S_SET_VALUE (l->label, val);
2882             }
2883         }
2884     }
2885
2886   /* Mark instruction labels in mips16 mode.  */
2887   if (insns)
2888     mips16_mark_labels ();
2889
2890   mips_no_prev_insn (insns);
2891 }
2892
2893 /* Build an instruction created by a macro expansion.  This is passed
2894    a pointer to the count of instructions created so far, an
2895    expression, the name of the instruction to build, an operand format
2896    string, and corresponding arguments.  */
2897
2898 #ifdef USE_STDARG
2899 static void
2900 macro_build (char *place,
2901              int *counter,
2902              expressionS * ep,
2903              const char *name,
2904              const char *fmt,
2905              ...)
2906 #else
2907 static void
2908 macro_build (place, counter, ep, name, fmt, va_alist)
2909      char *place;
2910      int *counter;
2911      expressionS *ep;
2912      const char *name;
2913      const char *fmt;
2914      va_dcl
2915 #endif
2916 {
2917   struct mips_cl_insn insn;
2918   bfd_reloc_code_real_type r[3];
2919   va_list args;
2920
2921 #ifdef USE_STDARG
2922   va_start (args, fmt);
2923 #else
2924   va_start (args);
2925 #endif
2926
2927   /*
2928    * If the macro is about to expand into a second instruction,
2929    * print a warning if needed. We need to pass ip as a parameter
2930    * to generate a better warning message here...
2931    */
2932   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2933     as_warn (_("Macro instruction expanded into multiple instructions"));
2934
2935   /*
2936    * If the macro is about to expand into a second instruction,
2937    * and it is in a delay slot, print a warning.
2938    */
2939   if (place == NULL
2940       && *counter == 1
2941       && mips_opts.noreorder
2942       && (prev_prev_insn.insn_mo->pinfo
2943           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2944              | INSN_COND_BRANCH_LIKELY)) != 0)
2945     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2946
2947   if (place == NULL)
2948     ++*counter;         /* bump instruction counter */
2949
2950   if (mips_opts.mips16)
2951     {
2952       mips16_macro_build (place, counter, ep, name, fmt, args);
2953       va_end (args);
2954       return;
2955     }
2956
2957   r[0] = BFD_RELOC_UNUSED;
2958   r[1] = BFD_RELOC_UNUSED;
2959   r[2] = BFD_RELOC_UNUSED;
2960   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2961   assert (insn.insn_mo);
2962   assert (strcmp (name, insn.insn_mo->name) == 0);
2963
2964   /* Search until we get a match for NAME.  */
2965   while (1)
2966     {
2967       /* It is assumed here that macros will never generate
2968          MDMX or MIPS-3D instructions.  */
2969       if (strcmp (fmt, insn.insn_mo->args) == 0
2970           && insn.insn_mo->pinfo != INSN_MACRO
2971           && OPCODE_IS_MEMBER (insn.insn_mo,
2972                                (mips_opts.isa
2973                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2974                                mips_arch)
2975           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2976         break;
2977
2978       ++insn.insn_mo;
2979       assert (insn.insn_mo->name);
2980       assert (strcmp (name, insn.insn_mo->name) == 0);
2981     }
2982
2983   insn.insn_opcode = insn.insn_mo->match;
2984   for (;;)
2985     {
2986       switch (*fmt++)
2987         {
2988         case '\0':
2989           break;
2990
2991         case ',':
2992         case '(':
2993         case ')':
2994           continue;
2995
2996         case 't':
2997         case 'w':
2998         case 'E':
2999           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3000           continue;
3001
3002         case 'c':
3003           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3004           continue;
3005
3006         case 'T':
3007         case 'W':
3008           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3009           continue;
3010
3011         case 'd':
3012         case 'G':
3013           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3014           continue;
3015
3016         case 'U':
3017           {
3018             int tmp = va_arg (args, int);
3019
3020             insn.insn_opcode |= tmp << OP_SH_RT;
3021             insn.insn_opcode |= tmp << OP_SH_RD;
3022             continue;
3023           }
3024
3025         case 'V':
3026         case 'S':
3027           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3028           continue;
3029
3030         case 'z':
3031           continue;
3032
3033         case '<':
3034           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3035           continue;
3036
3037         case 'D':
3038           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3039           continue;
3040
3041         case 'B':
3042           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3043           continue;
3044
3045         case 'J':
3046           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3047           continue;
3048
3049         case 'q':
3050           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3051           continue;
3052
3053         case 'b':
3054         case 's':
3055         case 'r':
3056         case 'v':
3057           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3058           continue;
3059
3060         case 'i':
3061         case 'j':
3062         case 'o':
3063           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3064           assert (*r == BFD_RELOC_GPREL16
3065                   || *r == BFD_RELOC_MIPS_LITERAL
3066                   || *r == BFD_RELOC_MIPS_HIGHER
3067                   || *r == BFD_RELOC_HI16_S
3068                   || *r == BFD_RELOC_LO16
3069                   || *r == BFD_RELOC_MIPS_GOT16
3070                   || *r == BFD_RELOC_MIPS_CALL16
3071                   || *r == BFD_RELOC_MIPS_GOT_DISP
3072                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3073                   || *r == BFD_RELOC_MIPS_GOT_OFST
3074                   || *r == BFD_RELOC_MIPS_GOT_LO16
3075                   || *r == BFD_RELOC_MIPS_CALL_LO16
3076                   || (ep->X_op == O_subtract
3077                       && *r == BFD_RELOC_PCREL_LO16));
3078           continue;
3079
3080         case 'u':
3081           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3082           assert (ep != NULL
3083                   && (ep->X_op == O_constant
3084                       || (ep->X_op == O_symbol
3085                           && (*r == BFD_RELOC_MIPS_HIGHEST
3086                               || *r == BFD_RELOC_HI16_S
3087                               || *r == BFD_RELOC_HI16
3088                               || *r == BFD_RELOC_GPREL16
3089                               || *r == BFD_RELOC_MIPS_GOT_HI16
3090                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3091                       || (ep->X_op == O_subtract
3092                           && *r == BFD_RELOC_PCREL_HI16_S)));
3093           continue;
3094
3095         case 'p':
3096           assert (ep != NULL);
3097           /*
3098            * This allows macro() to pass an immediate expression for
3099            * creating short branches without creating a symbol.
3100            * Note that the expression still might come from the assembly
3101            * input, in which case the value is not checked for range nor
3102            * is a relocation entry generated (yuck).
3103            */
3104           if (ep->X_op == O_constant)
3105             {
3106               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3107               ep = NULL;
3108             }
3109           else
3110             *r = BFD_RELOC_16_PCREL_S2;
3111           continue;
3112
3113         case 'a':
3114           assert (ep != NULL);
3115           *r = BFD_RELOC_MIPS_JMP;
3116           continue;
3117
3118         case 'C':
3119           insn.insn_opcode |= va_arg (args, unsigned long);
3120           continue;
3121
3122         default:
3123           internalError ();
3124         }
3125       break;
3126     }
3127   va_end (args);
3128   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3129
3130   append_insn (place, &insn, ep, r, FALSE);
3131 }
3132
3133 static void
3134 mips16_macro_build (place, counter, ep, name, fmt, args)
3135      char *place;
3136      int *counter ATTRIBUTE_UNUSED;
3137      expressionS *ep;
3138      const char *name;
3139      const char *fmt;
3140      va_list args;
3141 {
3142   struct mips_cl_insn insn;
3143   bfd_reloc_code_real_type r[3]
3144     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3145
3146   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3147   assert (insn.insn_mo);
3148   assert (strcmp (name, insn.insn_mo->name) == 0);
3149
3150   while (strcmp (fmt, insn.insn_mo->args) != 0
3151          || insn.insn_mo->pinfo == INSN_MACRO)
3152     {
3153       ++insn.insn_mo;
3154       assert (insn.insn_mo->name);
3155       assert (strcmp (name, insn.insn_mo->name) == 0);
3156     }
3157
3158   insn.insn_opcode = insn.insn_mo->match;
3159   insn.use_extend = FALSE;
3160
3161   for (;;)
3162     {
3163       int c;
3164
3165       c = *fmt++;
3166       switch (c)
3167         {
3168         case '\0':
3169           break;
3170
3171         case ',':
3172         case '(':
3173         case ')':
3174           continue;
3175
3176         case 'y':
3177         case 'w':
3178           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3179           continue;
3180
3181         case 'x':
3182         case 'v':
3183           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3184           continue;
3185
3186         case 'z':
3187           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3188           continue;
3189
3190         case 'Z':
3191           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3192           continue;
3193
3194         case '0':
3195         case 'S':
3196         case 'P':
3197         case 'R':
3198           continue;
3199
3200         case 'X':
3201           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3202           continue;
3203
3204         case 'Y':
3205           {
3206             int regno;
3207
3208             regno = va_arg (args, int);
3209             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3210             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3211           }
3212           continue;
3213
3214         case '<':
3215         case '>':
3216         case '4':
3217         case '5':
3218         case 'H':
3219         case 'W':
3220         case 'D':
3221         case 'j':
3222         case '8':
3223         case 'V':
3224         case 'C':
3225         case 'U':
3226         case 'k':
3227         case 'K':
3228         case 'p':
3229         case 'q':
3230           {
3231             assert (ep != NULL);
3232
3233             if (ep->X_op != O_constant)
3234               *r = (int) BFD_RELOC_UNUSED + c;
3235             else
3236               {
3237                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3238                               FALSE, &insn.insn_opcode, &insn.use_extend,
3239                               &insn.extend);
3240                 ep = NULL;
3241                 *r = BFD_RELOC_UNUSED;
3242               }
3243           }
3244           continue;
3245
3246         case '6':
3247           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3248           continue;
3249         }
3250
3251       break;
3252     }
3253
3254   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3255
3256   append_insn (place, &insn, ep, r, FALSE);
3257 }
3258
3259 /*
3260  * Generate a "jalr" instruction with a relocation hint to the called
3261  * function.  This occurs in NewABI PIC code.
3262  */
3263 static void
3264 macro_build_jalr (icnt, ep)
3265      int icnt;
3266      expressionS *ep;
3267 {
3268   char *f;
3269
3270   if (HAVE_NEWABI)
3271     {
3272       frag_grow (4);
3273       f = frag_more (0);
3274     }
3275   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3276                RA, PIC_CALL_REG);
3277   if (HAVE_NEWABI)
3278     fix_new_exp (frag_now, f - frag_now->fr_literal,
3279                  0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3280 }
3281
3282 /*
3283  * Generate a "lui" instruction.
3284  */
3285 static void
3286 macro_build_lui (place, counter, ep, regnum)
3287      char *place;
3288      int *counter;
3289      expressionS *ep;
3290      int regnum;
3291 {
3292   expressionS high_expr;
3293   struct mips_cl_insn insn;
3294   bfd_reloc_code_real_type r[3]
3295     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3296   const char *name = "lui";
3297   const char *fmt = "t,u";
3298
3299   assert (! mips_opts.mips16);
3300
3301   if (place == NULL)
3302     high_expr = *ep;
3303   else
3304     {
3305       high_expr.X_op = O_constant;
3306       high_expr.X_add_number = ep->X_add_number;
3307     }
3308
3309   if (high_expr.X_op == O_constant)
3310     {
3311       /* we can compute the instruction now without a relocation entry */
3312       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3313                                 >> 16) & 0xffff;
3314       *r = BFD_RELOC_UNUSED;
3315     }
3316   else
3317     {
3318       assert (ep->X_op == O_symbol);
3319       /* _gp_disp is a special case, used from s_cpload.  */
3320       assert (mips_pic == NO_PIC
3321               || (! HAVE_NEWABI
3322                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3323       *r = BFD_RELOC_HI16_S;
3324     }
3325
3326   /*
3327    * If the macro is about to expand into a second instruction,
3328    * print a warning if needed. We need to pass ip as a parameter
3329    * to generate a better warning message here...
3330    */
3331   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3332     as_warn (_("Macro instruction expanded into multiple instructions"));
3333
3334   if (place == NULL)
3335     ++*counter;         /* bump instruction counter */
3336
3337   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3338   assert (insn.insn_mo);
3339   assert (strcmp (name, insn.insn_mo->name) == 0);
3340   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3341
3342   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3343   if (*r == BFD_RELOC_UNUSED)
3344     {
3345       insn.insn_opcode |= high_expr.X_add_number;
3346       append_insn (place, &insn, NULL, r, FALSE);
3347     }
3348   else
3349     append_insn (place, &insn, &high_expr, r, FALSE);
3350 }
3351
3352 /* Generate a sequence of instructions to do a load or store from a constant
3353    offset off of a base register (breg) into/from a target register (treg),
3354    using AT if necessary.  */
3355 static void
3356 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3357      char *place;
3358      int *counter;
3359      expressionS *ep;
3360      const char *op;
3361      int treg, breg;
3362 {
3363   assert (ep->X_op == O_constant);
3364
3365   /* Right now, this routine can only handle signed 32-bit contants.  */
3366   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3367     as_warn (_("operand overflow"));
3368
3369   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3370     {
3371       /* Signed 16-bit offset will fit in the op.  Easy!  */
3372       macro_build (place, counter, ep, op, "t,o(b)", treg,
3373                    (int) BFD_RELOC_LO16, breg);
3374     }
3375   else
3376     {
3377       /* 32-bit offset, need multiple instructions and AT, like:
3378            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3379            addu     $tempreg,$tempreg,$breg
3380            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3381          to handle the complete offset.  */
3382       macro_build_lui (place, counter, ep, AT);
3383       if (place != NULL)
3384         place += 4;
3385       macro_build (place, counter, (expressionS *) NULL,
3386                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3387                    "d,v,t", AT, AT, breg);
3388       if (place != NULL)
3389         place += 4;
3390       macro_build (place, counter, ep, op, "t,o(b)", treg,
3391                    (int) BFD_RELOC_LO16, AT);
3392
3393       if (mips_opts.noat)
3394         as_warn (_("Macro used $at after \".set noat\""));
3395     }
3396 }
3397
3398 /*                      set_at()
3399  * Generates code to set the $at register to true (one)
3400  * if reg is less than the immediate expression.
3401  */
3402 static void
3403 set_at (counter, reg, unsignedp)
3404      int *counter;
3405      int reg;
3406      int unsignedp;
3407 {
3408   if (imm_expr.X_op == O_constant
3409       && imm_expr.X_add_number >= -0x8000
3410       && imm_expr.X_add_number < 0x8000)
3411     macro_build ((char *) NULL, counter, &imm_expr,
3412                  unsignedp ? "sltiu" : "slti",
3413                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3414   else
3415     {
3416       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3417       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3418                    unsignedp ? "sltu" : "slt",
3419                    "d,v,t", AT, reg, AT);
3420     }
3421 }
3422
3423 /* Warn if an expression is not a constant.  */
3424
3425 static void
3426 check_absolute_expr (ip, ex)
3427      struct mips_cl_insn *ip;
3428      expressionS *ex;
3429 {
3430   if (ex->X_op == O_big)
3431     as_bad (_("unsupported large constant"));
3432   else if (ex->X_op != O_constant)
3433     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3434 }
3435
3436 /* Count the leading zeroes by performing a binary chop. This is a
3437    bulky bit of source, but performance is a LOT better for the
3438    majority of values than a simple loop to count the bits:
3439        for (lcnt = 0; (lcnt < 32); lcnt++)
3440          if ((v) & (1 << (31 - lcnt)))
3441            break;
3442   However it is not code size friendly, and the gain will drop a bit
3443   on certain cached systems.
3444 */
3445 #define COUNT_TOP_ZEROES(v)             \
3446   (((v) & ~0xffff) == 0                 \
3447    ? ((v) & ~0xff) == 0                 \
3448      ? ((v) & ~0xf) == 0                \
3449        ? ((v) & ~0x3) == 0              \
3450          ? ((v) & ~0x1) == 0            \
3451            ? !(v)                       \
3452              ? 32                       \
3453              : 31                       \
3454            : 30                         \
3455          : ((v) & ~0x7) == 0            \
3456            ? 29                         \
3457            : 28                         \
3458        : ((v) & ~0x3f) == 0             \
3459          ? ((v) & ~0x1f) == 0           \
3460            ? 27                         \
3461            : 26                         \
3462          : ((v) & ~0x7f) == 0           \
3463            ? 25                         \
3464            : 24                         \
3465      : ((v) & ~0xfff) == 0              \
3466        ? ((v) & ~0x3ff) == 0            \
3467          ? ((v) & ~0x1ff) == 0          \
3468            ? 23                         \
3469            : 22                         \
3470          : ((v) & ~0x7ff) == 0          \
3471            ? 21                         \
3472            : 20                         \
3473        : ((v) & ~0x3fff) == 0           \
3474          ? ((v) & ~0x1fff) == 0         \
3475            ? 19                         \
3476            : 18                         \
3477          : ((v) & ~0x7fff) == 0         \
3478            ? 17                         \
3479            : 16                         \
3480    : ((v) & ~0xffffff) == 0             \
3481      ? ((v) & ~0xfffff) == 0            \
3482        ? ((v) & ~0x3ffff) == 0          \
3483          ? ((v) & ~0x1ffff) == 0        \
3484            ? 15                         \
3485            : 14                         \
3486          : ((v) & ~0x7ffff) == 0        \
3487            ? 13                         \
3488            : 12                         \
3489        : ((v) & ~0x3fffff) == 0         \
3490          ? ((v) & ~0x1fffff) == 0       \
3491            ? 11                         \
3492            : 10                         \
3493          : ((v) & ~0x7fffff) == 0       \
3494            ? 9                          \
3495            : 8                          \
3496      : ((v) & ~0xfffffff) == 0          \
3497        ? ((v) & ~0x3ffffff) == 0        \
3498          ? ((v) & ~0x1ffffff) == 0      \
3499            ? 7                          \
3500            : 6                          \
3501          : ((v) & ~0x7ffffff) == 0      \
3502            ? 5                          \
3503            : 4                          \
3504        : ((v) & ~0x3fffffff) == 0       \
3505          ? ((v) & ~0x1fffffff) == 0     \
3506            ? 3                          \
3507            : 2                          \
3508          : ((v) & ~0x7fffffff) == 0     \
3509            ? 1                          \
3510            : 0)
3511
3512 /*                      load_register()
3513  *  This routine generates the least number of instructions neccessary to load
3514  *  an absolute expression value into a register.
3515  */
3516 static void
3517 load_register (counter, reg, ep, dbl)
3518      int *counter;
3519      int reg;
3520      expressionS *ep;
3521      int dbl;
3522 {
3523   int freg;
3524   expressionS hi32, lo32;
3525
3526   if (ep->X_op != O_big)
3527     {
3528       assert (ep->X_op == O_constant);
3529       if (ep->X_add_number < 0x8000
3530           && (ep->X_add_number >= 0
3531               || (ep->X_add_number >= -0x8000
3532                   && (! dbl
3533                       || ! ep->X_unsigned
3534                       || sizeof (ep->X_add_number) > 4))))
3535         {
3536           /* We can handle 16 bit signed values with an addiu to
3537              $zero.  No need to ever use daddiu here, since $zero and
3538              the result are always correct in 32 bit mode.  */
3539           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3540                        (int) BFD_RELOC_LO16);
3541           return;
3542         }
3543       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3544         {
3545           /* We can handle 16 bit unsigned values with an ori to
3546              $zero.  */
3547           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3548                        (int) BFD_RELOC_LO16);
3549           return;
3550         }
3551       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3552                 && (! dbl
3553                     || ! ep->X_unsigned
3554                     || sizeof (ep->X_add_number) > 4
3555                     || (ep->X_add_number & 0x80000000) == 0))
3556                || ((HAVE_32BIT_GPRS || ! dbl)
3557                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3558                || (HAVE_32BIT_GPRS
3559                    && ! dbl
3560                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3561                        == ~ (offsetT) 0xffffffff)))
3562         {
3563           /* 32 bit values require an lui.  */
3564           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3565                        (int) BFD_RELOC_HI16);
3566           if ((ep->X_add_number & 0xffff) != 0)
3567             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3568                          (int) BFD_RELOC_LO16);
3569           return;
3570         }
3571     }
3572
3573   /* The value is larger than 32 bits.  */
3574
3575   if (HAVE_32BIT_GPRS)
3576     {
3577       as_bad (_("Number (0x%lx) larger than 32 bits"),
3578               (unsigned long) ep->X_add_number);
3579       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3580                    (int) BFD_RELOC_LO16);
3581       return;
3582     }
3583
3584   if (ep->X_op != O_big)
3585     {
3586       hi32 = *ep;
3587       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3588       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3589       hi32.X_add_number &= 0xffffffff;
3590       lo32 = *ep;
3591       lo32.X_add_number &= 0xffffffff;
3592     }
3593   else
3594     {
3595       assert (ep->X_add_number > 2);
3596       if (ep->X_add_number == 3)
3597         generic_bignum[3] = 0;
3598       else if (ep->X_add_number > 4)
3599         as_bad (_("Number larger than 64 bits"));
3600       lo32.X_op = O_constant;
3601       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3602       hi32.X_op = O_constant;
3603       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3604     }
3605
3606   if (hi32.X_add_number == 0)
3607     freg = 0;
3608   else
3609     {
3610       int shift, bit;
3611       unsigned long hi, lo;
3612
3613       if (hi32.X_add_number == (offsetT) 0xffffffff)
3614         {
3615           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3616             {
3617               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3618                            reg, 0, (int) BFD_RELOC_LO16);
3619               return;
3620             }
3621           if (lo32.X_add_number & 0x80000000)
3622             {
3623               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3624                            (int) BFD_RELOC_HI16);
3625               if (lo32.X_add_number & 0xffff)
3626                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3627                              reg, reg, (int) BFD_RELOC_LO16);
3628               return;
3629             }
3630         }
3631
3632       /* Check for 16bit shifted constant.  We know that hi32 is
3633          non-zero, so start the mask on the first bit of the hi32
3634          value.  */
3635       shift = 17;
3636       do
3637         {
3638           unsigned long himask, lomask;
3639
3640           if (shift < 32)
3641             {
3642               himask = 0xffff >> (32 - shift);
3643               lomask = (0xffff << shift) & 0xffffffff;
3644             }
3645           else
3646             {
3647               himask = 0xffff << (shift - 32);
3648               lomask = 0;
3649             }
3650           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3651               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3652             {
3653               expressionS tmp;
3654
3655               tmp.X_op = O_constant;
3656               if (shift < 32)
3657                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3658                                     | (lo32.X_add_number >> shift));
3659               else
3660                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3661               macro_build ((char *) NULL, counter, &tmp,
3662                            "ori", "t,r,i", reg, 0,
3663                            (int) BFD_RELOC_LO16);
3664               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3665                            (shift >= 32) ? "dsll32" : "dsll",
3666                            "d,w,<", reg, reg,
3667                            (shift >= 32) ? shift - 32 : shift);
3668               return;
3669             }
3670           ++shift;
3671         }
3672       while (shift <= (64 - 16));
3673
3674       /* Find the bit number of the lowest one bit, and store the
3675          shifted value in hi/lo.  */
3676       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3677       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3678       if (lo != 0)
3679         {
3680           bit = 0;
3681           while ((lo & 1) == 0)
3682             {
3683               lo >>= 1;
3684               ++bit;
3685             }
3686           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3687           hi >>= bit;
3688         }
3689       else
3690         {
3691           bit = 32;
3692           while ((hi & 1) == 0)
3693             {
3694               hi >>= 1;
3695               ++bit;
3696             }
3697           lo = hi;
3698           hi = 0;
3699         }
3700
3701       /* Optimize if the shifted value is a (power of 2) - 1.  */
3702       if ((hi == 0 && ((lo + 1) & lo) == 0)
3703           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3704         {
3705           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3706           if (shift != 0)
3707             {
3708               expressionS tmp;
3709
3710               /* This instruction will set the register to be all
3711                  ones.  */
3712               tmp.X_op = O_constant;
3713               tmp.X_add_number = (offsetT) -1;
3714               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3715                            reg, 0, (int) BFD_RELOC_LO16);
3716               if (bit != 0)
3717                 {
3718                   bit += shift;
3719                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3720                                (bit >= 32) ? "dsll32" : "dsll",
3721                                "d,w,<", reg, reg,
3722                                (bit >= 32) ? bit - 32 : bit);
3723                 }
3724               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3725                            (shift >= 32) ? "dsrl32" : "dsrl",
3726                            "d,w,<", reg, reg,
3727                            (shift >= 32) ? shift - 32 : shift);
3728               return;
3729             }
3730         }
3731
3732       /* Sign extend hi32 before calling load_register, because we can
3733          generally get better code when we load a sign extended value.  */
3734       if ((hi32.X_add_number & 0x80000000) != 0)
3735         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3736       load_register (counter, reg, &hi32, 0);
3737       freg = reg;
3738     }
3739   if ((lo32.X_add_number & 0xffff0000) == 0)
3740     {
3741       if (freg != 0)
3742         {
3743           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3744                        "dsll32", "d,w,<", reg, freg, 0);
3745           freg = reg;
3746         }
3747     }
3748   else
3749     {
3750       expressionS mid16;
3751
3752       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3753         {
3754           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3755                        (int) BFD_RELOC_HI16);
3756           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3757                        "dsrl32", "d,w,<", reg, reg, 0);
3758           return;
3759         }
3760
3761       if (freg != 0)
3762         {
3763           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3764                        "d,w,<", reg, freg, 16);
3765           freg = reg;
3766         }
3767       mid16 = lo32;
3768       mid16.X_add_number >>= 16;
3769       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3770                    freg, (int) BFD_RELOC_LO16);
3771       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3772                    "d,w,<", reg, reg, 16);
3773       freg = reg;
3774     }
3775   if ((lo32.X_add_number & 0xffff) != 0)
3776     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3777                  (int) BFD_RELOC_LO16);
3778 }
3779
3780 /* Load an address into a register.  */
3781
3782 static void
3783 load_address (counter, reg, ep, used_at)
3784      int *counter;
3785      int reg;
3786      expressionS *ep;
3787      int *used_at;
3788 {
3789   char *p = NULL;
3790
3791   if (ep->X_op != O_constant
3792       && ep->X_op != O_symbol)
3793     {
3794       as_bad (_("expression too complex"));
3795       ep->X_op = O_constant;
3796     }
3797
3798   if (ep->X_op == O_constant)
3799     {
3800       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3801       return;
3802     }
3803
3804   if (mips_pic == NO_PIC)
3805     {
3806       /* If this is a reference to a GP relative symbol, we want
3807            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3808          Otherwise we want
3809            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3810            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3811          If we have an addend, we always use the latter form.
3812
3813          With 64bit address space and a usable $at we want
3814            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3815            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3816            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3817            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3818            dsll32       $reg,0
3819            daddu        $reg,$reg,$at
3820
3821          If $at is already in use, we use an path which is suboptimal
3822          on superscalar processors.
3823            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3824            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3825            dsll         $reg,16
3826            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3827            dsll         $reg,16
3828            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3829        */
3830       if (HAVE_64BIT_ADDRESSES)
3831         {
3832           /* We don't do GP optimization for now because RELAX_ENCODE can't
3833              hold the data for such large chunks.  */
3834
3835           if (*used_at == 0 && ! mips_opts.noat)
3836             {
3837               macro_build (p, counter, ep, "lui", "t,u",
3838                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3839               macro_build (p, counter, ep, "lui", "t,u",
3840                            AT, (int) BFD_RELOC_HI16_S);
3841               macro_build (p, counter, ep, "daddiu", "t,r,j",
3842                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3843               macro_build (p, counter, ep, "daddiu", "t,r,j",
3844                            AT, AT, (int) BFD_RELOC_LO16);
3845               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3846                            "d,w,<", reg, reg, 0);
3847               macro_build (p, counter, (expressionS *) NULL, "daddu",
3848                            "d,v,t", reg, reg, AT);
3849               *used_at = 1;
3850             }
3851           else
3852             {
3853               macro_build (p, counter, ep, "lui", "t,u",
3854                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3855               macro_build (p, counter, ep, "daddiu", "t,r,j",
3856                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3857               macro_build (p, counter, (expressionS *) NULL, "dsll",
3858                            "d,w,<", reg, reg, 16);
3859               macro_build (p, counter, ep, "daddiu", "t,r,j",
3860                            reg, reg, (int) BFD_RELOC_HI16_S);
3861               macro_build (p, counter, (expressionS *) NULL, "dsll",
3862                            "d,w,<", reg, reg, 16);
3863               macro_build (p, counter, ep, "daddiu", "t,r,j",
3864                            reg, reg, (int) BFD_RELOC_LO16);
3865             }
3866         }
3867       else
3868         {
3869           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3870               && ! nopic_need_relax (ep->X_add_symbol, 1))
3871             {
3872               frag_grow (20);
3873               macro_build ((char *) NULL, counter, ep,
3874                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3875                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3876               p = frag_var (rs_machine_dependent, 8, 0,
3877                             RELAX_ENCODE (4, 8, 0, 4, 0,
3878                                           mips_opts.warn_about_macros),
3879                             ep->X_add_symbol, 0, NULL);
3880             }
3881           macro_build_lui (p, counter, ep, reg);
3882           if (p != NULL)
3883             p += 4;
3884           macro_build (p, counter, ep,
3885                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3886                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3887         }
3888     }
3889   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3890     {
3891       expressionS ex;
3892
3893       /* If this is a reference to an external symbol, we want
3894            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3895          Otherwise we want
3896            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3897            nop
3898            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3899          If we have NewABI, we want
3900            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3901          If there is a constant, it must be added in after.  */
3902       ex.X_add_number = ep->X_add_number;
3903       ep->X_add_number = 0;
3904       frag_grow (20);
3905       if (HAVE_NEWABI)
3906         {
3907           macro_build ((char *) NULL, counter, ep,
3908                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3909                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3910         }
3911       else
3912         {
3913           macro_build ((char *) NULL, counter, ep,
3914                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3915                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3916           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3917           p = frag_var (rs_machine_dependent, 4, 0,
3918                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3919                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3920           macro_build (p, counter, ep,
3921                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3922                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3923         }
3924
3925       if (ex.X_add_number != 0)
3926         {
3927           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3928             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3929           ex.X_op = O_constant;
3930           macro_build ((char *) NULL, counter, &ex,
3931                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3932                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3933         }
3934     }
3935   else if (mips_pic == SVR4_PIC)
3936     {
3937       expressionS ex;
3938       int off;
3939
3940       /* This is the large GOT case.  If this is a reference to an
3941          external symbol, we want
3942            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3943            addu         $reg,$reg,$gp
3944            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3945          Otherwise, for a reference to a local symbol, we want
3946            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3947            nop
3948            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3949          If we have NewABI, we want
3950            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3951            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3952          If there is a constant, it must be added in after.  */
3953       ex.X_add_number = ep->X_add_number;
3954       ep->X_add_number = 0;
3955       if (HAVE_NEWABI)
3956         {
3957           macro_build ((char *) NULL, counter, ep,
3958                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3959                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3960           macro_build (p, counter, ep,
3961                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3962                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3963         }
3964       else
3965         {
3966           if (reg_needs_delay (mips_gp_register))
3967             off = 4;
3968           else
3969             off = 0;
3970           frag_grow (32);
3971           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3972                        (int) BFD_RELOC_MIPS_GOT_HI16);
3973           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3974                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3975                        reg, mips_gp_register);
3976           macro_build ((char *) NULL, counter, ep,
3977                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3978                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3979           p = frag_var (rs_machine_dependent, 12 + off, 0,
3980                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3981                                       mips_opts.warn_about_macros),
3982                         ep->X_add_symbol, 0, NULL);
3983           if (off > 0)
3984             {
3985               /* We need a nop before loading from $gp.  This special
3986                  check is required because the lui which starts the main
3987                  instruction stream does not refer to $gp, and so will not
3988                  insert the nop which may be required.  */
3989               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3990                 p += 4;
3991             }
3992           macro_build (p, counter, ep,
3993                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3994                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3995           p += 4;
3996           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3997           p += 4;
3998           macro_build (p, counter, ep,
3999                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4000                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4001         }
4002
4003       if (ex.X_add_number != 0)
4004         {
4005           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4006             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4007           ex.X_op = O_constant;
4008           macro_build ((char *) NULL, counter, &ex,
4009                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4010                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4011         }
4012     }
4013   else if (mips_pic == EMBEDDED_PIC)
4014     {
4015       /* We always do
4016            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4017        */
4018       macro_build ((char *) NULL, counter, ep,
4019                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4020                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4021     }
4022   else
4023     abort ();
4024 }
4025
4026 /* Move the contents of register SOURCE into register DEST.  */
4027
4028 static void
4029 move_register (counter, dest, source)
4030      int *counter;
4031      int dest;
4032      int source;
4033 {
4034   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4035                HAVE_32BIT_GPRS ? "addu" : "daddu",
4036                "d,v,t", dest, source, 0);
4037 }
4038
4039 /*
4040  *                      Build macros
4041  *   This routine implements the seemingly endless macro or synthesized
4042  * instructions and addressing modes in the mips assembly language. Many
4043  * of these macros are simple and are similar to each other. These could
4044  * probably be handled by some kind of table or grammer aproach instead of
4045  * this verbose method. Others are not simple macros but are more like
4046  * optimizing code generation.
4047  *   One interesting optimization is when several store macros appear
4048  * consecutivly that would load AT with the upper half of the same address.
4049  * The ensuing load upper instructions are ommited. This implies some kind
4050  * of global optimization. We currently only optimize within a single macro.
4051  *   For many of the load and store macros if the address is specified as a
4052  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4053  * first load register 'at' with zero and use it as the base register. The
4054  * mips assembler simply uses register $zero. Just one tiny optimization
4055  * we're missing.
4056  */
4057 static void
4058 macro (ip)
4059      struct mips_cl_insn *ip;
4060 {
4061   register int treg, sreg, dreg, breg;
4062   int tempreg;
4063   int mask;
4064   int icnt = 0;
4065   int used_at = 0;
4066   expressionS expr1;
4067   const char *s;
4068   const char *s2;
4069   const char *fmt;
4070   int likely = 0;
4071   int dbl = 0;
4072   int coproc = 0;
4073   int lr = 0;
4074   int imm = 0;
4075   offsetT maxnum;
4076   int off;
4077   bfd_reloc_code_real_type r;
4078   int hold_mips_optimize;
4079
4080   assert (! mips_opts.mips16);
4081
4082   treg = (ip->insn_opcode >> 16) & 0x1f;
4083   dreg = (ip->insn_opcode >> 11) & 0x1f;
4084   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4085   mask = ip->insn_mo->mask;
4086
4087   expr1.X_op = O_constant;
4088   expr1.X_op_symbol = NULL;
4089   expr1.X_add_symbol = NULL;
4090   expr1.X_add_number = 1;
4091
4092   switch (mask)
4093     {
4094     case M_DABS:
4095       dbl = 1;
4096     case M_ABS:
4097       /* bgez $a0,.+12
4098          move v0,$a0
4099          sub v0,$zero,$a0
4100          */
4101
4102       mips_emit_delays (TRUE);
4103       ++mips_opts.noreorder;
4104       mips_any_noreorder = 1;
4105
4106       expr1.X_add_number = 8;
4107       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4108       if (dreg == sreg)
4109         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4110                      0);
4111       else
4112         move_register (&icnt, dreg, sreg);
4113       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4114                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4115
4116       --mips_opts.noreorder;
4117       return;
4118
4119     case M_ADD_I:
4120       s = "addi";
4121       s2 = "add";
4122       goto do_addi;
4123     case M_ADDU_I:
4124       s = "addiu";
4125       s2 = "addu";
4126       goto do_addi;
4127     case M_DADD_I:
4128       dbl = 1;
4129       s = "daddi";
4130       s2 = "dadd";
4131       goto do_addi;
4132     case M_DADDU_I:
4133       dbl = 1;
4134       s = "daddiu";
4135       s2 = "daddu";
4136     do_addi:
4137       if (imm_expr.X_op == O_constant
4138           && imm_expr.X_add_number >= -0x8000
4139           && imm_expr.X_add_number < 0x8000)
4140         {
4141           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4142                        (int) BFD_RELOC_LO16);
4143           return;
4144         }
4145       load_register (&icnt, AT, &imm_expr, dbl);
4146       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4147                    treg, sreg, AT);
4148       break;
4149
4150     case M_AND_I:
4151       s = "andi";
4152       s2 = "and";
4153       goto do_bit;
4154     case M_OR_I:
4155       s = "ori";
4156       s2 = "or";
4157       goto do_bit;
4158     case M_NOR_I:
4159       s = "";
4160       s2 = "nor";
4161       goto do_bit;
4162     case M_XOR_I:
4163       s = "xori";
4164       s2 = "xor";
4165     do_bit:
4166       if (imm_expr.X_op == O_constant
4167           && imm_expr.X_add_number >= 0
4168           && imm_expr.X_add_number < 0x10000)
4169         {
4170           if (mask != M_NOR_I)
4171             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4172                          sreg, (int) BFD_RELOC_LO16);
4173           else
4174             {
4175               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4176                            treg, sreg, (int) BFD_RELOC_LO16);
4177               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4178                            "d,v,t", treg, treg, 0);
4179             }
4180           return;
4181         }
4182
4183       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4184       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4185                    treg, sreg, AT);
4186       break;
4187
4188     case M_BEQ_I:
4189       s = "beq";
4190       goto beq_i;
4191     case M_BEQL_I:
4192       s = "beql";
4193       likely = 1;
4194       goto beq_i;
4195     case M_BNE_I:
4196       s = "bne";
4197       goto beq_i;
4198     case M_BNEL_I:
4199       s = "bnel";
4200       likely = 1;
4201     beq_i:
4202       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4203         {
4204           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4205                        0);
4206           return;
4207         }
4208       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4209       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4210       break;
4211
4212     case M_BGEL:
4213       likely = 1;
4214     case M_BGE:
4215       if (treg == 0)
4216         {
4217           macro_build ((char *) NULL, &icnt, &offset_expr,
4218                        likely ? "bgezl" : "bgez", "s,p", sreg);
4219           return;
4220         }
4221       if (sreg == 0)
4222         {
4223           macro_build ((char *) NULL, &icnt, &offset_expr,
4224                        likely ? "blezl" : "blez", "s,p", treg);
4225           return;
4226         }
4227       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4228                    AT, sreg, treg);
4229       macro_build ((char *) NULL, &icnt, &offset_expr,
4230                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4231       break;
4232
4233     case M_BGTL_I:
4234       likely = 1;
4235     case M_BGT_I:
4236       /* check for > max integer */
4237       maxnum = 0x7fffffff;
4238       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4239         {
4240           maxnum <<= 16;
4241           maxnum |= 0xffff;
4242           maxnum <<= 16;
4243           maxnum |= 0xffff;
4244         }
4245       if (imm_expr.X_op == O_constant
4246           && imm_expr.X_add_number >= maxnum
4247           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4248         {
4249         do_false:
4250           /* result is always false */
4251           if (! likely)
4252             {
4253               if (warn_nops)
4254                 as_warn (_("Branch %s is always false (nop)"),
4255                          ip->insn_mo->name);
4256               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4257                            "", 0);
4258             }
4259           else
4260             {
4261               if (warn_nops)
4262                 as_warn (_("Branch likely %s is always false"),
4263                          ip->insn_mo->name);
4264               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4265                            "s,t,p", 0, 0);
4266             }
4267           return;
4268         }
4269       if (imm_expr.X_op != O_constant)
4270         as_bad (_("Unsupported large constant"));
4271       ++imm_expr.X_add_number;
4272       /* FALLTHROUGH */
4273     case M_BGE_I:
4274     case M_BGEL_I:
4275       if (mask == M_BGEL_I)
4276         likely = 1;
4277       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4278         {
4279           macro_build ((char *) NULL, &icnt, &offset_expr,
4280                        likely ? "bgezl" : "bgez", "s,p", sreg);
4281           return;
4282         }
4283       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4284         {
4285           macro_build ((char *) NULL, &icnt, &offset_expr,
4286                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4287           return;
4288         }
4289       maxnum = 0x7fffffff;
4290       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4291         {
4292           maxnum <<= 16;
4293           maxnum |= 0xffff;
4294           maxnum <<= 16;
4295           maxnum |= 0xffff;
4296         }
4297       maxnum = - maxnum - 1;
4298       if (imm_expr.X_op == O_constant
4299           && imm_expr.X_add_number <= maxnum
4300           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4301         {
4302         do_true:
4303           /* result is always true */
4304           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4305           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4306           return;
4307         }
4308       set_at (&icnt, sreg, 0);
4309       macro_build ((char *) NULL, &icnt, &offset_expr,
4310                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4311       break;
4312
4313     case M_BGEUL:
4314       likely = 1;
4315     case M_BGEU:
4316       if (treg == 0)
4317         goto do_true;
4318       if (sreg == 0)
4319         {
4320           macro_build ((char *) NULL, &icnt, &offset_expr,
4321                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4322           return;
4323         }
4324       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4325                    "d,v,t", AT, sreg, treg);
4326       macro_build ((char *) NULL, &icnt, &offset_expr,
4327                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4328       break;
4329
4330     case M_BGTUL_I:
4331       likely = 1;
4332     case M_BGTU_I:
4333       if (sreg == 0
4334           || (HAVE_32BIT_GPRS
4335               && imm_expr.X_op == O_constant
4336               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4337         goto do_false;
4338       if (imm_expr.X_op != O_constant)
4339         as_bad (_("Unsupported large constant"));
4340       ++imm_expr.X_add_number;
4341       /* FALLTHROUGH */
4342     case M_BGEU_I:
4343     case M_BGEUL_I:
4344       if (mask == M_BGEUL_I)
4345         likely = 1;
4346       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4347         goto do_true;
4348       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4349         {
4350           macro_build ((char *) NULL, &icnt, &offset_expr,
4351                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4352           return;
4353         }
4354       set_at (&icnt, sreg, 1);
4355       macro_build ((char *) NULL, &icnt, &offset_expr,
4356                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4357       break;
4358
4359     case M_BGTL:
4360       likely = 1;
4361     case M_BGT:
4362       if (treg == 0)
4363         {
4364           macro_build ((char *) NULL, &icnt, &offset_expr,
4365                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4366           return;
4367         }
4368       if (sreg == 0)
4369         {
4370           macro_build ((char *) NULL, &icnt, &offset_expr,
4371                        likely ? "bltzl" : "bltz", "s,p", treg);
4372           return;
4373         }
4374       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4375                    AT, treg, sreg);
4376       macro_build ((char *) NULL, &icnt, &offset_expr,
4377                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4378       break;
4379
4380     case M_BGTUL:
4381       likely = 1;
4382     case M_BGTU:
4383       if (treg == 0)
4384         {
4385           macro_build ((char *) NULL, &icnt, &offset_expr,
4386                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4387           return;
4388         }
4389       if (sreg == 0)
4390         goto do_false;
4391       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4392                    "d,v,t", AT, treg, sreg);
4393       macro_build ((char *) NULL, &icnt, &offset_expr,
4394                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4395       break;
4396
4397     case M_BLEL:
4398       likely = 1;
4399     case M_BLE:
4400       if (treg == 0)
4401         {
4402           macro_build ((char *) NULL, &icnt, &offset_expr,
4403                        likely ? "blezl" : "blez", "s,p", sreg);
4404           return;
4405         }
4406       if (sreg == 0)
4407         {
4408           macro_build ((char *) NULL, &icnt, &offset_expr,
4409                        likely ? "bgezl" : "bgez", "s,p", treg);
4410           return;
4411         }
4412       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4413                    AT, treg, sreg);
4414       macro_build ((char *) NULL, &icnt, &offset_expr,
4415                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4416       break;
4417
4418     case M_BLEL_I:
4419       likely = 1;
4420     case M_BLE_I:
4421       maxnum = 0x7fffffff;
4422       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4423         {
4424           maxnum <<= 16;
4425           maxnum |= 0xffff;
4426           maxnum <<= 16;
4427           maxnum |= 0xffff;
4428         }
4429       if (imm_expr.X_op == O_constant
4430           && imm_expr.X_add_number >= maxnum
4431           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4432         goto do_true;
4433       if (imm_expr.X_op != O_constant)
4434         as_bad (_("Unsupported large constant"));
4435       ++imm_expr.X_add_number;
4436       /* FALLTHROUGH */
4437     case M_BLT_I:
4438     case M_BLTL_I:
4439       if (mask == M_BLTL_I)
4440         likely = 1;
4441       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4442         {
4443           macro_build ((char *) NULL, &icnt, &offset_expr,
4444                        likely ? "bltzl" : "bltz", "s,p", sreg);
4445           return;
4446         }
4447       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4448         {
4449           macro_build ((char *) NULL, &icnt, &offset_expr,
4450                        likely ? "blezl" : "blez", "s,p", sreg);
4451           return;
4452         }
4453       set_at (&icnt, sreg, 0);
4454       macro_build ((char *) NULL, &icnt, &offset_expr,
4455                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4456       break;
4457
4458     case M_BLEUL:
4459       likely = 1;
4460     case M_BLEU:
4461       if (treg == 0)
4462         {
4463           macro_build ((char *) NULL, &icnt, &offset_expr,
4464                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4465           return;
4466         }
4467       if (sreg == 0)
4468         goto do_true;
4469       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4470                    "d,v,t", AT, treg, sreg);
4471       macro_build ((char *) NULL, &icnt, &offset_expr,
4472                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4473       break;
4474
4475     case M_BLEUL_I:
4476       likely = 1;
4477     case M_BLEU_I:
4478       if (sreg == 0
4479           || (HAVE_32BIT_GPRS
4480               && imm_expr.X_op == O_constant
4481               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4482         goto do_true;
4483       if (imm_expr.X_op != O_constant)
4484         as_bad (_("Unsupported large constant"));
4485       ++imm_expr.X_add_number;
4486       /* FALLTHROUGH */
4487     case M_BLTU_I:
4488     case M_BLTUL_I:
4489       if (mask == M_BLTUL_I)
4490         likely = 1;
4491       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4492         goto do_false;
4493       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4494         {
4495           macro_build ((char *) NULL, &icnt, &offset_expr,
4496                        likely ? "beql" : "beq",
4497                        "s,t,p", sreg, 0);
4498           return;
4499         }
4500       set_at (&icnt, sreg, 1);
4501       macro_build ((char *) NULL, &icnt, &offset_expr,
4502                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4503       break;
4504
4505     case M_BLTL:
4506       likely = 1;
4507     case M_BLT:
4508       if (treg == 0)
4509         {
4510           macro_build ((char *) NULL, &icnt, &offset_expr,
4511                        likely ? "bltzl" : "bltz", "s,p", sreg);
4512           return;
4513         }
4514       if (sreg == 0)
4515         {
4516           macro_build ((char *) NULL, &icnt, &offset_expr,
4517                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4518           return;
4519         }
4520       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4521                    AT, sreg, treg);
4522       macro_build ((char *) NULL, &icnt, &offset_expr,
4523                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4524       break;
4525
4526     case M_BLTUL:
4527       likely = 1;
4528     case M_BLTU:
4529       if (treg == 0)
4530         goto do_false;
4531       if (sreg == 0)
4532         {
4533           macro_build ((char *) NULL, &icnt, &offset_expr,
4534                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4535           return;
4536         }
4537       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4538                    "d,v,t", AT, sreg,
4539                    treg);
4540       macro_build ((char *) NULL, &icnt, &offset_expr,
4541                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4542       break;
4543
4544     case M_DDIV_3:
4545       dbl = 1;
4546     case M_DIV_3:
4547       s = "mflo";
4548       goto do_div3;
4549     case M_DREM_3:
4550       dbl = 1;
4551     case M_REM_3:
4552       s = "mfhi";
4553     do_div3:
4554       if (treg == 0)
4555         {
4556           as_warn (_("Divide by zero."));
4557           if (mips_trap)
4558             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4559                          "s,t,q", 0, 0, 7);
4560           else
4561             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4562                          "c", 7);
4563           return;
4564         }
4565
4566       mips_emit_delays (TRUE);
4567       ++mips_opts.noreorder;
4568       mips_any_noreorder = 1;
4569       if (mips_trap)
4570         {
4571           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4572                        "s,t,q", treg, 0, 7);
4573           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4574                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4575         }
4576       else
4577         {
4578           expr1.X_add_number = 8;
4579           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4580           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4581                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4582           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4583                        "c", 7);
4584         }
4585       expr1.X_add_number = -1;
4586       macro_build ((char *) NULL, &icnt, &expr1,
4587                    dbl ? "daddiu" : "addiu",
4588                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4589       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4590       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4591       if (dbl)
4592         {
4593           expr1.X_add_number = 1;
4594           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4595                        (int) BFD_RELOC_LO16);
4596           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4597                        "d,w,<", AT, AT, 31);
4598         }
4599       else
4600         {
4601           expr1.X_add_number = 0x80000000;
4602           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4603                        (int) BFD_RELOC_HI16);
4604         }
4605       if (mips_trap)
4606         {
4607           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4608                        "s,t,q", sreg, AT, 6);
4609           /* We want to close the noreorder block as soon as possible, so
4610              that later insns are available for delay slot filling.  */
4611           --mips_opts.noreorder;
4612         }
4613       else
4614         {
4615           expr1.X_add_number = 8;
4616           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4617           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4618                        0);
4619
4620           /* We want to close the noreorder block as soon as possible, so
4621              that later insns are available for delay slot filling.  */
4622           --mips_opts.noreorder;
4623
4624           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4625                        "c", 6);
4626         }
4627       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4628       break;
4629
4630     case M_DIV_3I:
4631       s = "div";
4632       s2 = "mflo";
4633       goto do_divi;
4634     case M_DIVU_3I:
4635       s = "divu";
4636       s2 = "mflo";
4637       goto do_divi;
4638     case M_REM_3I:
4639       s = "div";
4640       s2 = "mfhi";
4641       goto do_divi;
4642     case M_REMU_3I:
4643       s = "divu";
4644       s2 = "mfhi";
4645       goto do_divi;
4646     case M_DDIV_3I:
4647       dbl = 1;
4648       s = "ddiv";
4649       s2 = "mflo";
4650       goto do_divi;
4651     case M_DDIVU_3I:
4652       dbl = 1;
4653       s = "ddivu";
4654       s2 = "mflo";
4655       goto do_divi;
4656     case M_DREM_3I:
4657       dbl = 1;
4658       s = "ddiv";
4659       s2 = "mfhi";
4660       goto do_divi;
4661     case M_DREMU_3I:
4662       dbl = 1;
4663       s = "ddivu";
4664       s2 = "mfhi";
4665     do_divi:
4666       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4667         {
4668           as_warn (_("Divide by zero."));
4669           if (mips_trap)
4670             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4671                          "s,t,q", 0, 0, 7);
4672           else
4673             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4674                          "c", 7);
4675           return;
4676         }
4677       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4678         {
4679           if (strcmp (s2, "mflo") == 0)
4680             move_register (&icnt, dreg, sreg);
4681           else
4682             move_register (&icnt, dreg, 0);
4683           return;
4684         }
4685       if (imm_expr.X_op == O_constant
4686           && imm_expr.X_add_number == -1
4687           && s[strlen (s) - 1] != 'u')
4688         {
4689           if (strcmp (s2, "mflo") == 0)
4690             {
4691               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4692                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4693             }
4694           else
4695             move_register (&icnt, dreg, 0);
4696           return;
4697         }
4698
4699       load_register (&icnt, AT, &imm_expr, dbl);
4700       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4701                    sreg, AT);
4702       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4703       break;
4704
4705     case M_DIVU_3:
4706       s = "divu";
4707       s2 = "mflo";
4708       goto do_divu3;
4709     case M_REMU_3:
4710       s = "divu";
4711       s2 = "mfhi";
4712       goto do_divu3;
4713     case M_DDIVU_3:
4714       s = "ddivu";
4715       s2 = "mflo";
4716       goto do_divu3;
4717     case M_DREMU_3:
4718       s = "ddivu";
4719       s2 = "mfhi";
4720     do_divu3:
4721       mips_emit_delays (TRUE);
4722       ++mips_opts.noreorder;
4723       mips_any_noreorder = 1;
4724       if (mips_trap)
4725         {
4726           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4727                        "s,t,q", treg, 0, 7);
4728           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4729                        sreg, treg);
4730           /* We want to close the noreorder block as soon as possible, so
4731              that later insns are available for delay slot filling.  */
4732           --mips_opts.noreorder;
4733         }
4734       else
4735         {
4736           expr1.X_add_number = 8;
4737           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4738           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4739                        sreg, treg);
4740
4741           /* We want to close the noreorder block as soon as possible, so
4742              that later insns are available for delay slot filling.  */
4743           --mips_opts.noreorder;
4744           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4745                        "c", 7);
4746         }
4747       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4748       return;
4749
4750     case M_DLA_AB:
4751       dbl = 1;
4752     case M_LA_AB:
4753       /* Load the address of a symbol into a register.  If breg is not
4754          zero, we then add a base register to it.  */
4755
4756       if (dbl && HAVE_32BIT_GPRS)
4757         as_warn (_("dla used to load 32-bit register"));
4758
4759       if (! dbl && HAVE_64BIT_OBJECTS)
4760         as_warn (_("la used to load 64-bit address"));
4761
4762       if (offset_expr.X_op == O_constant
4763           && offset_expr.X_add_number >= -0x8000
4764           && offset_expr.X_add_number < 0x8000)
4765         {
4766           macro_build ((char *) NULL, &icnt, &offset_expr,
4767                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4768                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4769           return;
4770         }
4771
4772       if (treg == breg)
4773         {
4774           tempreg = AT;
4775           used_at = 1;
4776         }
4777       else
4778         {
4779           tempreg = treg;
4780           used_at = 0;
4781         }
4782
4783       /* When generating embedded PIC code, we permit expressions of
4784          the form
4785            la   $treg,foo-bar
4786            la   $treg,foo-bar($breg)
4787          where bar is an address in the current section.  These are used
4788          when getting the addresses of functions.  We don't permit
4789          X_add_number to be non-zero, because if the symbol is
4790          external the relaxing code needs to know that any addend is
4791          purely the offset to X_op_symbol.  */
4792       if (mips_pic == EMBEDDED_PIC
4793           && offset_expr.X_op == O_subtract
4794           && (symbol_constant_p (offset_expr.X_op_symbol)
4795               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4796               : (symbol_equated_p (offset_expr.X_op_symbol)
4797                  && (S_GET_SEGMENT
4798                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4799                       ->X_add_symbol)
4800                      == now_seg)))
4801           && (offset_expr.X_add_number == 0
4802               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4803         {
4804           if (breg == 0)
4805             {
4806               tempreg = treg;
4807               used_at = 0;
4808               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4809                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4810             }
4811           else
4812             {
4813               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4814                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4815               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4816                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4817                            "d,v,t", tempreg, tempreg, breg);
4818             }
4819           macro_build ((char *) NULL, &icnt, &offset_expr,
4820                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4821                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4822           if (! used_at)
4823             return;
4824           break;
4825         }
4826
4827       if (offset_expr.X_op != O_symbol
4828           && offset_expr.X_op != O_constant)
4829         {
4830           as_bad (_("expression too complex"));
4831           offset_expr.X_op = O_constant;
4832         }
4833
4834       if (offset_expr.X_op == O_constant)
4835         load_register (&icnt, tempreg, &offset_expr,
4836                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4837                         ? (dbl || HAVE_64BIT_ADDRESSES)
4838                         : HAVE_64BIT_ADDRESSES));
4839       else if (mips_pic == NO_PIC)
4840         {
4841           /* If this is a reference to a GP relative symbol, we want
4842                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4843              Otherwise we want
4844                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4845                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4846              If we have a constant, we need two instructions anyhow,
4847              so we may as well always use the latter form.
4848
4849             With 64bit address space and a usable $at we want
4850               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4851               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4852               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4853               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4854               dsll32    $tempreg,0
4855               daddu     $tempreg,$tempreg,$at
4856
4857             If $at is already in use, we use an path which is suboptimal
4858             on superscalar processors.
4859               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4860               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4861               dsll      $tempreg,16
4862               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4863               dsll      $tempreg,16
4864               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4865           */
4866           char *p = NULL;
4867           if (HAVE_64BIT_ADDRESSES)
4868             {
4869               /* We don't do GP optimization for now because RELAX_ENCODE can't
4870                  hold the data for such large chunks.  */
4871
4872               if (used_at == 0 && ! mips_opts.noat)
4873                 {
4874                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4875                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4876                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4877                                AT, (int) BFD_RELOC_HI16_S);
4878                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4879                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4880                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4881                                AT, AT, (int) BFD_RELOC_LO16);
4882                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4883                                "d,w,<", tempreg, tempreg, 0);
4884                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4885                                "d,v,t", tempreg, tempreg, AT);
4886                   used_at = 1;
4887                 }
4888               else
4889                 {
4890                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4891                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4892                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4893                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4894                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4895                                tempreg, tempreg, 16);
4896                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4897                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4898                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4899                                tempreg, tempreg, 16);
4900                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4901                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4902                 }
4903             }
4904           else
4905             {
4906               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4907                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4908                 {
4909                   frag_grow (20);
4910                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4911                                "t,r,j", tempreg, mips_gp_register,
4912                                (int) BFD_RELOC_GPREL16);
4913                   p = frag_var (rs_machine_dependent, 8, 0,
4914                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4915                                               mips_opts.warn_about_macros),
4916                                 offset_expr.X_add_symbol, 0, NULL);
4917                 }
4918               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4919               if (p != NULL)
4920                 p += 4;
4921               macro_build (p, &icnt, &offset_expr, "addiu",
4922                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4923             }
4924         }
4925       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4926         {
4927           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4928
4929           /* If this is a reference to an external symbol, and there
4930              is no constant, we want
4931                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4932              or if tempreg is PIC_CALL_REG
4933                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4934              For a local symbol, we want
4935                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4936                nop
4937                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4938
4939              If we have a small constant, and this is a reference to
4940              an external symbol, we want
4941                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4942                nop
4943                addiu    $tempreg,$tempreg,<constant>
4944              For a local symbol, we want the same instruction
4945              sequence, but we output a BFD_RELOC_LO16 reloc on the
4946              addiu instruction.
4947
4948              If we have a large constant, and this is a reference to
4949              an external symbol, we want
4950                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4951                lui      $at,<hiconstant>
4952                addiu    $at,$at,<loconstant>
4953                addu     $tempreg,$tempreg,$at
4954              For a local symbol, we want the same instruction
4955              sequence, but we output a BFD_RELOC_LO16 reloc on the
4956              addiu instruction.
4957
4958              For NewABI, we want for local or external data addresses
4959                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4960              For a local function symbol, we want
4961                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4962                nop
4963                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4964            */
4965
4966           expr1.X_add_number = offset_expr.X_add_number;
4967           offset_expr.X_add_number = 0;
4968           frag_grow (32);
4969           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4970             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4971           else if (HAVE_NEWABI)
4972             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4973           macro_build ((char *) NULL, &icnt, &offset_expr,
4974                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4975                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4976           if (expr1.X_add_number == 0)
4977             {
4978               int off;
4979               char *p;
4980
4981               if (breg == 0)
4982                 off = 0;
4983               else
4984                 {
4985                   /* We're going to put in an addu instruction using
4986                      tempreg, so we may as well insert the nop right
4987                      now.  */
4988                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4989                                "nop", "");
4990                   off = 4;
4991                 }
4992               p = frag_var (rs_machine_dependent, 8 - off, 0,
4993                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4994                                           (breg == 0
4995                                            ? mips_opts.warn_about_macros
4996                                            : 0)),
4997                             offset_expr.X_add_symbol, 0, NULL);
4998               if (breg == 0)
4999                 {
5000                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5001                   p += 4;
5002                 }
5003               macro_build (p, &icnt, &expr1,
5004                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5005                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5006               /* FIXME: If breg == 0, and the next instruction uses
5007                  $tempreg, then if this variant case is used an extra
5008                  nop will be generated.  */
5009             }
5010           else if (expr1.X_add_number >= -0x8000
5011                    && expr1.X_add_number < 0x8000)
5012             {
5013               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5014                            "nop", "");
5015               macro_build ((char *) NULL, &icnt, &expr1,
5016                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5017                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5018               frag_var (rs_machine_dependent, 0, 0,
5019                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5020                         offset_expr.X_add_symbol, 0, NULL);
5021             }
5022           else
5023             {
5024               int off1;
5025
5026               /* If we are going to add in a base register, and the
5027                  target register and the base register are the same,
5028                  then we are using AT as a temporary register.  Since
5029                  we want to load the constant into AT, we add our
5030                  current AT (from the global offset table) and the
5031                  register into the register now, and pretend we were
5032                  not using a base register.  */
5033               if (breg != treg)
5034                 off1 = 0;
5035               else
5036                 {
5037                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5038                                "nop", "");
5039                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5040                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5041                                "d,v,t", treg, AT, breg);
5042                   breg = 0;
5043                   tempreg = treg;
5044                   off1 = -8;
5045                 }
5046
5047               /* Set mips_optimize around the lui instruction to avoid
5048                  inserting an unnecessary nop after the lw.  */
5049               hold_mips_optimize = mips_optimize;
5050               mips_optimize = 2;
5051               macro_build_lui (NULL, &icnt, &expr1, AT);
5052               mips_optimize = hold_mips_optimize;
5053
5054               macro_build ((char *) NULL, &icnt, &expr1,
5055                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5056                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5057               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5058                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5059                            "d,v,t", tempreg, tempreg, AT);
5060               frag_var (rs_machine_dependent, 0, 0,
5061                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5062                         offset_expr.X_add_symbol, 0, NULL);
5063               used_at = 1;
5064             }
5065         }
5066       else if (mips_pic == SVR4_PIC)
5067         {
5068           int gpdel;
5069           char *p;
5070           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5071           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5072           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5073
5074           /* This is the large GOT case.  If this is a reference to an
5075              external symbol, and there is no constant, we want
5076                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5077                addu     $tempreg,$tempreg,$gp
5078                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5079              or if tempreg is PIC_CALL_REG
5080                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5081                addu     $tempreg,$tempreg,$gp
5082                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5083              For a local symbol, we want
5084                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5085                nop
5086                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5087
5088              If we have a small constant, and this is a reference to
5089              an external symbol, we want
5090                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5091                addu     $tempreg,$tempreg,$gp
5092                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5093                nop
5094                addiu    $tempreg,$tempreg,<constant>
5095              For a local symbol, we want
5096                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5097                nop
5098                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5099
5100              If we have a large constant, and this is a reference to
5101              an external symbol, we want
5102                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5103                addu     $tempreg,$tempreg,$gp
5104                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5105                lui      $at,<hiconstant>
5106                addiu    $at,$at,<loconstant>
5107                addu     $tempreg,$tempreg,$at
5108              For a local symbol, we want
5109                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5110                lui      $at,<hiconstant>
5111                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5112                addu     $tempreg,$tempreg,$at
5113
5114              For NewABI, we want for local data addresses
5115               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5116            */
5117
5118           expr1.X_add_number = offset_expr.X_add_number;
5119           offset_expr.X_add_number = 0;
5120           frag_grow (52);
5121           if (reg_needs_delay (mips_gp_register))
5122             gpdel = 4;
5123           else
5124             gpdel = 0;
5125           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5126             {
5127               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5128               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5129             }
5130           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5131                        tempreg, lui_reloc_type);
5132           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5133                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5134                        "d,v,t", tempreg, tempreg, mips_gp_register);
5135           macro_build ((char *) NULL, &icnt, &offset_expr,
5136                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5137                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5138           if (expr1.X_add_number == 0)
5139             {
5140               int off;
5141
5142               if (breg == 0)
5143                 off = 0;
5144               else
5145                 {
5146                   /* We're going to put in an addu instruction using
5147                      tempreg, so we may as well insert the nop right
5148                      now.  */
5149                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5150                                "nop", "");
5151                   off = 4;
5152                 }
5153
5154               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5155                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5156                                           8 + gpdel, 0,
5157                                           (breg == 0
5158                                            ? mips_opts.warn_about_macros
5159                                            : 0)),
5160                             offset_expr.X_add_symbol, 0, NULL);
5161             }
5162           else if (expr1.X_add_number >= -0x8000
5163                    && expr1.X_add_number < 0x8000)
5164             {
5165               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5166                            "nop", "");
5167               macro_build ((char *) NULL, &icnt, &expr1,
5168                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5169                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5170
5171               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5172                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5173                                           (breg == 0
5174                                            ? mips_opts.warn_about_macros
5175                                            : 0)),
5176                             offset_expr.X_add_symbol, 0, NULL);
5177             }
5178           else
5179             {
5180               int adj, dreg;
5181
5182               /* If we are going to add in a base register, and the
5183                  target register and the base register are the same,
5184                  then we are using AT as a temporary register.  Since
5185                  we want to load the constant into AT, we add our
5186                  current AT (from the global offset table) and the
5187                  register into the register now, and pretend we were
5188                  not using a base register.  */
5189               if (breg != treg)
5190                 {
5191                   adj = 0;
5192                   dreg = tempreg;
5193                 }
5194               else
5195                 {
5196                   assert (tempreg == AT);
5197                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5198                                "nop", "");
5199                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5200                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5201                                "d,v,t", treg, AT, breg);
5202                   dreg = treg;
5203                   adj = 8;
5204                 }
5205
5206               /* Set mips_optimize around the lui instruction to avoid
5207                  inserting an unnecessary nop after the lw.  */
5208               hold_mips_optimize = mips_optimize;
5209               mips_optimize = 2;
5210               macro_build_lui (NULL, &icnt, &expr1, AT);
5211               mips_optimize = hold_mips_optimize;
5212
5213               macro_build ((char *) NULL, &icnt, &expr1,
5214                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5215                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5216               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5217                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5218                            "d,v,t", dreg, dreg, AT);
5219
5220               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5221                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5222                                           8 + gpdel, 0,
5223                                           (breg == 0
5224                                            ? mips_opts.warn_about_macros
5225                                            : 0)),
5226                             offset_expr.X_add_symbol, 0, NULL);
5227
5228               used_at = 1;
5229             }
5230
5231           if (gpdel > 0)
5232             {
5233               /* This is needed because this instruction uses $gp, but
5234                  the first instruction on the main stream does not.  */
5235               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5236               p += 4;
5237             }
5238
5239           if (HAVE_NEWABI)
5240             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5241           macro_build (p, &icnt, &offset_expr,
5242                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5243                        "t,o(b)", tempreg,
5244                        local_reloc_type,
5245                        mips_gp_register);
5246           p += 4;
5247           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5248             {
5249               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5250             }
5251          else
5252            if (expr1.X_add_number >= -0x8000
5253               && expr1.X_add_number < 0x8000)
5254             {
5255               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5256               p += 4;
5257               macro_build (p, &icnt, &expr1,
5258                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5259                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5260               /* FIXME: If add_number is 0, and there was no base
5261                  register, the external symbol case ended with a load,
5262                  so if the symbol turns out to not be external, and
5263                  the next instruction uses tempreg, an unnecessary nop
5264                  will be inserted.  */
5265             }
5266           else
5267             {
5268               if (breg == treg)
5269                 {
5270                   /* We must add in the base register now, as in the
5271                      external symbol case.  */
5272                   assert (tempreg == AT);
5273                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5274                   p += 4;
5275                   macro_build (p, &icnt, (expressionS *) NULL,
5276                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5277                                "d,v,t", treg, AT, breg);
5278                   p += 4;
5279                   tempreg = treg;
5280                   /* We set breg to 0 because we have arranged to add
5281                      it in in both cases.  */
5282                   breg = 0;
5283                 }
5284
5285               macro_build_lui (p, &icnt, &expr1, AT);
5286               p += 4;
5287               macro_build (p, &icnt, &expr1,
5288                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5289                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5290               p += 4;
5291               macro_build (p, &icnt, (expressionS *) NULL,
5292                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5293                            "d,v,t", tempreg, tempreg, AT);
5294               p += 4;
5295             }
5296         }
5297       else if (mips_pic == EMBEDDED_PIC)
5298         {
5299           /* We use
5300                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5301              */
5302           macro_build ((char *) NULL, &icnt, &offset_expr,
5303                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5304                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5305         }
5306       else
5307         abort ();
5308
5309       if (breg != 0)
5310         {
5311           char *s;
5312
5313           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5314             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5315           else
5316             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5317
5318           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5319                        "d,v,t", treg, tempreg, breg);
5320         }
5321
5322       if (! used_at)
5323         return;
5324
5325       break;
5326
5327     case M_J_A:
5328       /* The j instruction may not be used in PIC code, since it
5329          requires an absolute address.  We convert it to a b
5330          instruction.  */
5331       if (mips_pic == NO_PIC)
5332         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5333       else
5334         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5335       return;
5336
5337       /* The jal instructions must be handled as macros because when
5338          generating PIC code they expand to multi-instruction
5339          sequences.  Normally they are simple instructions.  */
5340     case M_JAL_1:
5341       dreg = RA;
5342       /* Fall through.  */
5343     case M_JAL_2:
5344       if (mips_pic == NO_PIC
5345           || mips_pic == EMBEDDED_PIC)
5346         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5347                      "d,s", dreg, sreg);
5348       else if (mips_pic == SVR4_PIC)
5349         {
5350           if (sreg != PIC_CALL_REG)
5351             as_warn (_("MIPS PIC call to register other than $25"));
5352
5353           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5354                        "d,s", dreg, sreg);
5355           if (! HAVE_NEWABI)
5356             {
5357               if (mips_cprestore_offset < 0)
5358                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5359               else
5360                 {
5361                   if (! mips_frame_reg_valid)
5362                     {
5363                       as_warn (_("No .frame pseudo-op used in PIC code"));
5364                       /* Quiet this warning.  */
5365                       mips_frame_reg_valid = 1;
5366                     }
5367                   if (! mips_cprestore_valid)
5368                     {
5369                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5370                       /* Quiet this warning.  */
5371                       mips_cprestore_valid = 1;
5372                     }
5373                   expr1.X_add_number = mips_cprestore_offset;
5374                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5375                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5376                                                 mips_gp_register, mips_frame_reg);
5377                 }
5378             }
5379         }
5380       else
5381         abort ();
5382
5383       return;
5384
5385     case M_JAL_A:
5386       if (mips_pic == NO_PIC)
5387         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5388       else if (mips_pic == SVR4_PIC)
5389         {
5390           char *p;
5391
5392           /* If this is a reference to an external symbol, and we are
5393              using a small GOT, we want
5394                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5395                nop
5396                jalr     $ra,$25
5397                nop
5398                lw       $gp,cprestore($sp)
5399              The cprestore value is set using the .cprestore
5400              pseudo-op.  If we are using a big GOT, we want
5401                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5402                addu     $25,$25,$gp
5403                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5404                nop
5405                jalr     $ra,$25
5406                nop
5407                lw       $gp,cprestore($sp)
5408              If the symbol is not external, we want
5409                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5410                nop
5411                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5412                jalr     $ra,$25
5413                nop
5414                lw $gp,cprestore($sp)
5415              For NewABI, we want
5416                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5417                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5418            */
5419           if (HAVE_NEWABI)
5420             {
5421               macro_build ((char *) NULL, &icnt, &offset_expr,
5422                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5423                            "t,o(b)", PIC_CALL_REG,
5424                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5425               macro_build_jalr (icnt, &offset_expr);
5426             }
5427           else
5428             {
5429               frag_grow (40);
5430               if (! mips_big_got)
5431                 {
5432                   macro_build ((char *) NULL, &icnt, &offset_expr,
5433                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5434                                "t,o(b)", PIC_CALL_REG,
5435                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5436                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5437                                "nop", "");
5438                   p = frag_var (rs_machine_dependent, 4, 0,
5439                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5440                                 offset_expr.X_add_symbol, 0, NULL);
5441                 }
5442               else
5443                 {
5444                   int gpdel;
5445
5446                   if (reg_needs_delay (mips_gp_register))
5447                     gpdel = 4;
5448                   else
5449                     gpdel = 0;
5450                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5451                                "t,u", PIC_CALL_REG,
5452                                (int) BFD_RELOC_MIPS_CALL_HI16);
5453                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5454                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5455                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5456                                mips_gp_register);
5457                   macro_build ((char *) NULL, &icnt, &offset_expr,
5458                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5459                                "t,o(b)", PIC_CALL_REG,
5460                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5461                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5462                                "nop", "");
5463                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5464                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5465                                               8 + gpdel, 0, 0),
5466                                 offset_expr.X_add_symbol, 0, NULL);
5467                   if (gpdel > 0)
5468                     {
5469                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5470                       p += 4;
5471                     }
5472                   macro_build (p, &icnt, &offset_expr,
5473                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5474                                "t,o(b)", PIC_CALL_REG,
5475                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5476                   p += 4;
5477                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5478                   p += 4;
5479                 }
5480               macro_build (p, &icnt, &offset_expr,
5481                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5482                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5483                            (int) BFD_RELOC_LO16);
5484               macro_build_jalr (icnt, &offset_expr);
5485
5486               if (mips_cprestore_offset < 0)
5487                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5488               else
5489                 {
5490                   if (! mips_frame_reg_valid)
5491                     {
5492                       as_warn (_("No .frame pseudo-op used in PIC code"));
5493                       /* Quiet this warning.  */
5494                       mips_frame_reg_valid = 1;
5495                     }
5496                   if (! mips_cprestore_valid)
5497                     {
5498                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5499                       /* Quiet this warning.  */
5500                       mips_cprestore_valid = 1;
5501                     }
5502                   if (mips_opts.noreorder)
5503                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5504                                  "nop", "");
5505                   expr1.X_add_number = mips_cprestore_offset;
5506                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5507                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5508                                                 mips_gp_register, mips_frame_reg);
5509                 }
5510             }
5511         }
5512       else if (mips_pic == EMBEDDED_PIC)
5513         {
5514           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5515           /* The linker may expand the call to a longer sequence which
5516              uses $at, so we must break rather than return.  */
5517           break;
5518         }
5519       else
5520         abort ();
5521
5522       return;
5523
5524     case M_LB_AB:
5525       s = "lb";
5526       goto ld;
5527     case M_LBU_AB:
5528       s = "lbu";
5529       goto ld;
5530     case M_LH_AB:
5531       s = "lh";
5532       goto ld;
5533     case M_LHU_AB:
5534       s = "lhu";
5535       goto ld;
5536     case M_LW_AB:
5537       s = "lw";
5538       goto ld;
5539     case M_LWC0_AB:
5540       s = "lwc0";
5541       /* Itbl support may require additional care here.  */
5542       coproc = 1;
5543       goto ld;
5544     case M_LWC1_AB:
5545       s = "lwc1";
5546       /* Itbl support may require additional care here.  */
5547       coproc = 1;
5548       goto ld;
5549     case M_LWC2_AB:
5550       s = "lwc2";
5551       /* Itbl support may require additional care here.  */
5552       coproc = 1;
5553       goto ld;
5554     case M_LWC3_AB:
5555       s = "lwc3";
5556       /* Itbl support may require additional care here.  */
5557       coproc = 1;
5558       goto ld;
5559     case M_LWL_AB:
5560       s = "lwl";
5561       lr = 1;
5562       goto ld;
5563     case M_LWR_AB:
5564       s = "lwr";
5565       lr = 1;
5566       goto ld;
5567     case M_LDC1_AB:
5568       if (mips_arch == CPU_R4650)
5569         {
5570           as_bad (_("opcode not supported on this processor"));
5571           return;
5572         }
5573       s = "ldc1";
5574       /* Itbl support may require additional care here.  */
5575       coproc = 1;
5576       goto ld;
5577     case M_LDC2_AB:
5578       s = "ldc2";
5579       /* Itbl support may require additional care here.  */
5580       coproc = 1;
5581       goto ld;
5582     case M_LDC3_AB:
5583       s = "ldc3";
5584       /* Itbl support may require additional care here.  */
5585       coproc = 1;
5586       goto ld;
5587     case M_LDL_AB:
5588       s = "ldl";
5589       lr = 1;
5590       goto ld;
5591     case M_LDR_AB:
5592       s = "ldr";
5593       lr = 1;
5594       goto ld;
5595     case M_LL_AB:
5596       s = "ll";
5597       goto ld;
5598     case M_LLD_AB:
5599       s = "lld";
5600       goto ld;
5601     case M_LWU_AB:
5602       s = "lwu";
5603     ld:
5604       if (breg == treg || coproc || lr)
5605         {
5606           tempreg = AT;
5607           used_at = 1;
5608         }
5609       else
5610         {
5611           tempreg = treg;
5612           used_at = 0;
5613         }
5614       goto ld_st;
5615     case M_SB_AB:
5616       s = "sb";
5617       goto st;
5618     case M_SH_AB:
5619       s = "sh";
5620       goto st;
5621     case M_SW_AB:
5622       s = "sw";
5623       goto st;
5624     case M_SWC0_AB:
5625       s = "swc0";
5626       /* Itbl support may require additional care here.  */
5627       coproc = 1;
5628       goto st;
5629     case M_SWC1_AB:
5630       s = "swc1";
5631       /* Itbl support may require additional care here.  */
5632       coproc = 1;
5633       goto st;
5634     case M_SWC2_AB:
5635       s = "swc2";
5636       /* Itbl support may require additional care here.  */
5637       coproc = 1;
5638       goto st;
5639     case M_SWC3_AB:
5640       s = "swc3";
5641       /* Itbl support may require additional care here.  */
5642       coproc = 1;
5643       goto st;
5644     case M_SWL_AB:
5645       s = "swl";
5646       goto st;
5647     case M_SWR_AB:
5648       s = "swr";
5649       goto st;
5650     case M_SC_AB:
5651       s = "sc";
5652       goto st;
5653     case M_SCD_AB:
5654       s = "scd";
5655       goto st;
5656     case M_SDC1_AB:
5657       if (mips_arch == CPU_R4650)
5658         {
5659           as_bad (_("opcode not supported on this processor"));
5660           return;
5661         }
5662       s = "sdc1";
5663       coproc = 1;
5664       /* Itbl support may require additional care here.  */
5665       goto st;
5666     case M_SDC2_AB:
5667       s = "sdc2";
5668       /* Itbl support may require additional care here.  */
5669       coproc = 1;
5670       goto st;
5671     case M_SDC3_AB:
5672       s = "sdc3";
5673       /* Itbl support may require additional care here.  */
5674       coproc = 1;
5675       goto st;
5676     case M_SDL_AB:
5677       s = "sdl";
5678       goto st;
5679     case M_SDR_AB:
5680       s = "sdr";
5681     st:
5682       tempreg = AT;
5683       used_at = 1;
5684     ld_st:
5685       /* Itbl support may require additional care here.  */
5686       if (mask == M_LWC1_AB
5687           || mask == M_SWC1_AB
5688           || mask == M_LDC1_AB
5689           || mask == M_SDC1_AB
5690           || mask == M_L_DAB
5691           || mask == M_S_DAB)
5692         fmt = "T,o(b)";
5693       else if (coproc)
5694         fmt = "E,o(b)";
5695       else
5696         fmt = "t,o(b)";
5697
5698       /* For embedded PIC, we allow loads where the offset is calculated
5699          by subtracting a symbol in the current segment from an unknown
5700          symbol, relative to a base register, e.g.:
5701                 <op>    $treg, <sym>-<localsym>($breg)
5702          This is used by the compiler for switch statements.  */
5703       if (mips_pic == EMBEDDED_PIC
5704           && offset_expr.X_op == O_subtract
5705           && (symbol_constant_p (offset_expr.X_op_symbol)
5706               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5707               : (symbol_equated_p (offset_expr.X_op_symbol)
5708                  && (S_GET_SEGMENT
5709                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5710                       ->X_add_symbol)
5711                      == now_seg)))
5712           && breg != 0
5713           && (offset_expr.X_add_number == 0
5714               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5715         {
5716           /* For this case, we output the instructions:
5717                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5718                 addiu   $tempreg,$tempreg,$breg
5719                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5720              If the relocation would fit entirely in 16 bits, it would be
5721              nice to emit:
5722                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5723              instead, but that seems quite difficult.  */
5724           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5725                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5726           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5727                        ((bfd_arch_bits_per_address (stdoutput) == 32
5728                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5729                         ? "addu" : "daddu"),
5730                        "d,v,t", tempreg, tempreg, breg);
5731           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5732                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5733           if (! used_at)
5734             return;
5735           break;
5736         }
5737
5738       if (offset_expr.X_op != O_constant
5739           && offset_expr.X_op != O_symbol)
5740         {
5741           as_bad (_("expression too complex"));
5742           offset_expr.X_op = O_constant;
5743         }
5744
5745       /* A constant expression in PIC code can be handled just as it
5746          is in non PIC code.  */
5747       if (mips_pic == NO_PIC
5748           || offset_expr.X_op == O_constant)
5749         {
5750           char *p;
5751
5752           /* If this is a reference to a GP relative symbol, and there
5753              is no base register, we want
5754                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5755              Otherwise, if there is no base register, we want
5756                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5757                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5758              If we have a constant, we need two instructions anyhow,
5759              so we always use the latter form.
5760
5761              If we have a base register, and this is a reference to a
5762              GP relative symbol, we want
5763                addu     $tempreg,$breg,$gp
5764                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5765              Otherwise we want
5766                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5767                addu     $tempreg,$tempreg,$breg
5768                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5769              With a constant we always use the latter case.
5770
5771              With 64bit address space and no base register and $at usable,
5772              we want
5773                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5774                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5775                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5776                dsll32   $tempreg,0
5777                daddu    $tempreg,$at
5778                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5779              If we have a base register, we want
5780                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5781                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5782                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5783                daddu    $at,$breg
5784                dsll32   $tempreg,0
5785                daddu    $tempreg,$at
5786                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5787
5788              Without $at we can't generate the optimal path for superscalar
5789              processors here since this would require two temporary registers.
5790                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5791                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5792                dsll     $tempreg,16
5793                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5794                dsll     $tempreg,16
5795                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5796              If we have a base register, we want
5797                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5798                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5799                dsll     $tempreg,16
5800                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5801                dsll     $tempreg,16
5802                daddu    $tempreg,$tempreg,$breg
5803                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5804
5805              If we have 64-bit addresses, as an optimization, for
5806              addresses which are 32-bit constants (e.g. kseg0/kseg1
5807              addresses) we fall back to the 32-bit address generation
5808              mechanism since it is more efficient.  Note that due to
5809              the signed offset used by memory operations, the 32-bit
5810              range is shifted down by 32768 here.  This code should
5811              probably attempt to generate 64-bit constants more
5812              efficiently in general.
5813            */
5814           if (HAVE_64BIT_ADDRESSES
5815               && !(offset_expr.X_op == O_constant
5816                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5817             {
5818               p = NULL;
5819
5820               /* We don't do GP optimization for now because RELAX_ENCODE can't
5821                  hold the data for such large chunks.  */
5822
5823               if (used_at == 0 && ! mips_opts.noat)
5824                 {
5825                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5826                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5827                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5828                                AT, (int) BFD_RELOC_HI16_S);
5829                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5830                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5831                   if (breg != 0)
5832                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5833                                  "d,v,t", AT, AT, breg);
5834                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5835                                "d,w,<", tempreg, tempreg, 0);
5836                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5837                                "d,v,t", tempreg, tempreg, AT);
5838                   macro_build (p, &icnt, &offset_expr, s,
5839                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5840                   used_at = 1;
5841                 }
5842               else
5843                 {
5844                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5845                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5846                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5847                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5848                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5849                                "d,w,<", tempreg, tempreg, 16);
5850                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5851                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5852                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5853                                "d,w,<", tempreg, tempreg, 16);
5854                   if (breg != 0)
5855                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5856                                  "d,v,t", tempreg, tempreg, breg);
5857                   macro_build (p, &icnt, &offset_expr, s,
5858                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5859                 }
5860
5861               return;
5862             }
5863
5864           if (breg == 0)
5865             {
5866               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5867                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5868                 p = NULL;
5869               else
5870                 {
5871                   frag_grow (20);
5872                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5873                                treg, (int) BFD_RELOC_GPREL16,
5874                                mips_gp_register);
5875                   p = frag_var (rs_machine_dependent, 8, 0,
5876                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5877                                               (mips_opts.warn_about_macros
5878                                                || (used_at
5879                                                    && mips_opts.noat))),
5880                                 offset_expr.X_add_symbol, 0, NULL);
5881                   used_at = 0;
5882                 }
5883               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5884               if (p != NULL)
5885                 p += 4;
5886               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5887                            (int) BFD_RELOC_LO16, tempreg);
5888             }
5889           else
5890             {
5891               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5892                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5893                 p = NULL;
5894               else
5895                 {
5896                   frag_grow (28);
5897                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5898                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5899                                "d,v,t", tempreg, breg, mips_gp_register);
5900                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5901                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5902                   p = frag_var (rs_machine_dependent, 12, 0,
5903                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5904                                 offset_expr.X_add_symbol, 0, NULL);
5905                 }
5906               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5907               if (p != NULL)
5908                 p += 4;
5909               macro_build (p, &icnt, (expressionS *) NULL,
5910                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5911                            "d,v,t", tempreg, tempreg, breg);
5912               if (p != NULL)
5913                 p += 4;
5914               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5915                            (int) BFD_RELOC_LO16, tempreg);
5916             }
5917         }
5918       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5919         {
5920           char *p;
5921           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5922
5923           /* If this is a reference to an external symbol, we want
5924                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5925                nop
5926                <op>     $treg,0($tempreg)
5927              Otherwise we want
5928                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5929                nop
5930                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5931                <op>     $treg,0($tempreg)
5932              If we have NewABI, we want
5933                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5934              If there is a base register, we add it to $tempreg before
5935              the <op>.  If there is a constant, we stick it in the
5936              <op> instruction.  We don't handle constants larger than
5937              16 bits, because we have no way to load the upper 16 bits
5938              (actually, we could handle them for the subset of cases
5939              in which we are not using $at).  */
5940           assert (offset_expr.X_op == O_symbol);
5941           expr1.X_add_number = offset_expr.X_add_number;
5942           offset_expr.X_add_number = 0;
5943           if (HAVE_NEWABI)
5944             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5945           if (expr1.X_add_number < -0x8000
5946               || expr1.X_add_number >= 0x8000)
5947             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5948           frag_grow (20);
5949           macro_build ((char *) NULL, &icnt, &offset_expr,
5950                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5951                        (int) lw_reloc_type, mips_gp_register);
5952           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5953           p = frag_var (rs_machine_dependent, 4, 0,
5954                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5955                         offset_expr.X_add_symbol, 0, NULL);
5956           macro_build (p, &icnt, &offset_expr,
5957                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5958                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5959           if (breg != 0)
5960             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5961                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5962                          "d,v,t", tempreg, tempreg, breg);
5963           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5964                        (int) BFD_RELOC_LO16, tempreg);
5965         }
5966       else if (mips_pic == SVR4_PIC)
5967         {
5968           int gpdel;
5969           char *p;
5970
5971           /* If this is a reference to an external symbol, we want
5972                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5973                addu     $tempreg,$tempreg,$gp
5974                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5975                <op>     $treg,0($tempreg)
5976              Otherwise we want
5977                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5978                nop
5979                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5980                <op>     $treg,0($tempreg)
5981              If there is a base register, we add it to $tempreg before
5982              the <op>.  If there is a constant, we stick it in the
5983              <op> instruction.  We don't handle constants larger than
5984              16 bits, because we have no way to load the upper 16 bits
5985              (actually, we could handle them for the subset of cases
5986              in which we are not using $at).
5987
5988              For NewABI, we want
5989                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5990                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5991                <op>     $treg,0($tempreg)
5992            */
5993           assert (offset_expr.X_op == O_symbol);
5994           expr1.X_add_number = offset_expr.X_add_number;
5995           offset_expr.X_add_number = 0;
5996           if (expr1.X_add_number < -0x8000
5997               || expr1.X_add_number >= 0x8000)
5998             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5999           if (HAVE_NEWABI)
6000             {
6001               macro_build ((char *) NULL, &icnt, &offset_expr,
6002                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6003                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6004                            mips_gp_register);
6005               macro_build ((char *) NULL, &icnt, &offset_expr,
6006                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6007                            "t,r,j", tempreg, tempreg,
6008                            BFD_RELOC_MIPS_GOT_OFST);
6009               if (breg != 0)
6010                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6011                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6012                              "d,v,t", tempreg, tempreg, breg);
6013               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6014                            (int) BFD_RELOC_LO16, tempreg);
6015
6016               if (! used_at)
6017                 return;
6018
6019               break;
6020             }
6021           if (reg_needs_delay (mips_gp_register))
6022             gpdel = 4;
6023           else
6024             gpdel = 0;
6025           frag_grow (36);
6026           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6027                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6028           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6029                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6030                        "d,v,t", tempreg, tempreg, mips_gp_register);
6031           macro_build ((char *) NULL, &icnt, &offset_expr,
6032                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6033                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6034                        tempreg);
6035           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6036                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6037                         offset_expr.X_add_symbol, 0, NULL);
6038           if (gpdel > 0)
6039             {
6040               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6041               p += 4;
6042             }
6043           macro_build (p, &icnt, &offset_expr,
6044                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6045                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6046                        mips_gp_register);
6047           p += 4;
6048           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6049           p += 4;
6050           macro_build (p, &icnt, &offset_expr,
6051                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6052                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6053           if (breg != 0)
6054             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6055                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6056                          "d,v,t", tempreg, tempreg, breg);
6057           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6058                        (int) BFD_RELOC_LO16, tempreg);
6059         }
6060       else if (mips_pic == EMBEDDED_PIC)
6061         {
6062           /* If there is no base register, we want
6063                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6064              If there is a base register, we want
6065                addu     $tempreg,$breg,$gp
6066                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6067              */
6068           assert (offset_expr.X_op == O_symbol);
6069           if (breg == 0)
6070             {
6071               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6072                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6073               used_at = 0;
6074             }
6075           else
6076             {
6077               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6078                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6079                            "d,v,t", tempreg, breg, mips_gp_register);
6080               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6081                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6082             }
6083         }
6084       else
6085         abort ();
6086
6087       if (! used_at)
6088         return;
6089
6090       break;
6091
6092     case M_LI:
6093     case M_LI_S:
6094       load_register (&icnt, treg, &imm_expr, 0);
6095       return;
6096
6097     case M_DLI:
6098       load_register (&icnt, treg, &imm_expr, 1);
6099       return;
6100
6101     case M_LI_SS:
6102       if (imm_expr.X_op == O_constant)
6103         {
6104           load_register (&icnt, AT, &imm_expr, 0);
6105           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6106                        "mtc1", "t,G", AT, treg);
6107           break;
6108         }
6109       else
6110         {
6111           assert (offset_expr.X_op == O_symbol
6112                   && strcmp (segment_name (S_GET_SEGMENT
6113                                            (offset_expr.X_add_symbol)),
6114                              ".lit4") == 0
6115                   && offset_expr.X_add_number == 0);
6116           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6117                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6118           return;
6119         }
6120
6121     case M_LI_D:
6122       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6123          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6124          order 32 bits of the value and the low order 32 bits are either
6125          zero or in OFFSET_EXPR.  */
6126       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6127         {
6128           if (HAVE_64BIT_GPRS)
6129             load_register (&icnt, treg, &imm_expr, 1);
6130           else
6131             {
6132               int hreg, lreg;
6133
6134               if (target_big_endian)
6135                 {
6136                   hreg = treg;
6137                   lreg = treg + 1;
6138                 }
6139               else
6140                 {
6141                   hreg = treg + 1;
6142                   lreg = treg;
6143                 }
6144
6145               if (hreg <= 31)
6146                 load_register (&icnt, hreg, &imm_expr, 0);
6147               if (lreg <= 31)
6148                 {
6149                   if (offset_expr.X_op == O_absent)
6150                     move_register (&icnt, lreg, 0);
6151                   else
6152                     {
6153                       assert (offset_expr.X_op == O_constant);
6154                       load_register (&icnt, lreg, &offset_expr, 0);
6155                     }
6156                 }
6157             }
6158           return;
6159         }
6160
6161       /* We know that sym is in the .rdata section.  First we get the
6162          upper 16 bits of the address.  */
6163       if (mips_pic == NO_PIC)
6164         {
6165           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6166         }
6167       else if (mips_pic == SVR4_PIC)
6168         {
6169           macro_build ((char *) NULL, &icnt, &offset_expr,
6170                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6171                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6172                        mips_gp_register);
6173         }
6174       else if (mips_pic == EMBEDDED_PIC)
6175         {
6176           /* For embedded PIC we pick up the entire address off $gp in
6177              a single instruction.  */
6178           macro_build ((char *) NULL, &icnt, &offset_expr,
6179                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6180                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6181           offset_expr.X_op = O_constant;
6182           offset_expr.X_add_number = 0;
6183         }
6184       else
6185         abort ();
6186
6187       /* Now we load the register(s).  */
6188       if (HAVE_64BIT_GPRS)
6189         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6190                      treg, (int) BFD_RELOC_LO16, AT);
6191       else
6192         {
6193           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6194                        treg, (int) BFD_RELOC_LO16, AT);
6195           if (treg != RA)
6196             {
6197               /* FIXME: How in the world do we deal with the possible
6198                  overflow here?  */
6199               offset_expr.X_add_number += 4;
6200               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6201                            treg + 1, (int) BFD_RELOC_LO16, AT);
6202             }
6203         }
6204
6205       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6206          does not become a variant frag.  */
6207       frag_wane (frag_now);
6208       frag_new (0);
6209
6210       break;
6211
6212     case M_LI_DD:
6213       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6214          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6215          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6216          the value and the low order 32 bits are either zero or in
6217          OFFSET_EXPR.  */
6218       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6219         {
6220           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6221           if (HAVE_64BIT_FPRS)
6222             {
6223               assert (HAVE_64BIT_GPRS);
6224               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6225                            "dmtc1", "t,S", AT, treg);
6226             }
6227           else
6228             {
6229               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6230                            "mtc1", "t,G", AT, treg + 1);
6231               if (offset_expr.X_op == O_absent)
6232                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6233                              "mtc1", "t,G", 0, treg);
6234               else
6235                 {
6236                   assert (offset_expr.X_op == O_constant);
6237                   load_register (&icnt, AT, &offset_expr, 0);
6238                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6239                                "mtc1", "t,G", AT, treg);
6240                 }
6241             }
6242           break;
6243         }
6244
6245       assert (offset_expr.X_op == O_symbol
6246               && offset_expr.X_add_number == 0);
6247       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6248       if (strcmp (s, ".lit8") == 0)
6249         {
6250           if (mips_opts.isa != ISA_MIPS1)
6251             {
6252               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6253                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6254                            mips_gp_register);
6255               return;
6256             }
6257           breg = mips_gp_register;
6258           r = BFD_RELOC_MIPS_LITERAL;
6259           goto dob;
6260         }
6261       else
6262         {
6263           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6264           if (mips_pic == SVR4_PIC)
6265             macro_build ((char *) NULL, &icnt, &offset_expr,
6266                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6267                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6268                          mips_gp_register);
6269           else
6270             {
6271               /* FIXME: This won't work for a 64 bit address.  */
6272               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6273             }
6274
6275           if (mips_opts.isa != ISA_MIPS1)
6276             {
6277               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6278                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6279
6280               /* To avoid confusion in tc_gen_reloc, we must ensure
6281                  that this does not become a variant frag.  */
6282               frag_wane (frag_now);
6283               frag_new (0);
6284
6285               break;
6286             }
6287           breg = AT;
6288           r = BFD_RELOC_LO16;
6289           goto dob;
6290         }
6291
6292     case M_L_DOB:
6293       if (mips_arch == CPU_R4650)
6294         {
6295           as_bad (_("opcode not supported on this processor"));
6296           return;
6297         }
6298       /* Even on a big endian machine $fn comes before $fn+1.  We have
6299          to adjust when loading from memory.  */
6300       r = BFD_RELOC_LO16;
6301     dob:
6302       assert (mips_opts.isa == ISA_MIPS1);
6303       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6304                    target_big_endian ? treg + 1 : treg,
6305                    (int) r, breg);
6306       /* FIXME: A possible overflow which I don't know how to deal
6307          with.  */
6308       offset_expr.X_add_number += 4;
6309       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6310                    target_big_endian ? treg : treg + 1,
6311                    (int) r, breg);
6312
6313       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6314          does not become a variant frag.  */
6315       frag_wane (frag_now);
6316       frag_new (0);
6317
6318       if (breg != AT)
6319         return;
6320       break;
6321
6322     case M_L_DAB:
6323       /*
6324        * The MIPS assembler seems to check for X_add_number not
6325        * being double aligned and generating:
6326        *        lui     at,%hi(foo+1)
6327        *        addu    at,at,v1
6328        *        addiu   at,at,%lo(foo+1)
6329        *        lwc1    f2,0(at)
6330        *        lwc1    f3,4(at)
6331        * But, the resulting address is the same after relocation so why
6332        * generate the extra instruction?
6333        */
6334       if (mips_arch == CPU_R4650)
6335         {
6336           as_bad (_("opcode not supported on this processor"));
6337           return;
6338         }
6339       /* Itbl support may require additional care here.  */
6340       coproc = 1;
6341       if (mips_opts.isa != ISA_MIPS1)
6342         {
6343           s = "ldc1";
6344           goto ld;
6345         }
6346
6347       s = "lwc1";
6348       fmt = "T,o(b)";
6349       goto ldd_std;
6350
6351     case M_S_DAB:
6352       if (mips_arch == CPU_R4650)
6353         {
6354           as_bad (_("opcode not supported on this processor"));
6355           return;
6356         }
6357
6358       if (mips_opts.isa != ISA_MIPS1)
6359         {
6360           s = "sdc1";
6361           goto st;
6362         }
6363
6364       s = "swc1";
6365       fmt = "T,o(b)";
6366       /* Itbl support may require additional care here.  */
6367       coproc = 1;
6368       goto ldd_std;
6369
6370     case M_LD_AB:
6371       if (HAVE_64BIT_GPRS)
6372         {
6373           s = "ld";
6374           goto ld;
6375         }
6376
6377       s = "lw";
6378       fmt = "t,o(b)";
6379       goto ldd_std;
6380
6381     case M_SD_AB:
6382       if (HAVE_64BIT_GPRS)
6383         {
6384           s = "sd";
6385           goto st;
6386         }
6387
6388       s = "sw";
6389       fmt = "t,o(b)";
6390
6391     ldd_std:
6392       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6393          loads for the case of doing a pair of loads to simulate an 'ld'.
6394          This is not currently done by the compiler, and assembly coders
6395          writing embedded-pic code can cope.  */
6396
6397       if (offset_expr.X_op != O_symbol
6398           && offset_expr.X_op != O_constant)
6399         {
6400           as_bad (_("expression too complex"));
6401           offset_expr.X_op = O_constant;
6402         }
6403
6404       /* Even on a big endian machine $fn comes before $fn+1.  We have
6405          to adjust when loading from memory.  We set coproc if we must
6406          load $fn+1 first.  */
6407       /* Itbl support may require additional care here.  */
6408       if (! target_big_endian)
6409         coproc = 0;
6410
6411       if (mips_pic == NO_PIC
6412           || offset_expr.X_op == O_constant)
6413         {
6414           char *p;
6415
6416           /* If this is a reference to a GP relative symbol, we want
6417                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6418                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6419              If we have a base register, we use this
6420                addu     $at,$breg,$gp
6421                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6422                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6423              If this is not a GP relative symbol, we want
6424                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6425                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6426                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6427              If there is a base register, we add it to $at after the
6428              lui instruction.  If there is a constant, we always use
6429              the last case.  */
6430           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6431               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6432             {
6433               p = NULL;
6434               used_at = 1;
6435             }
6436           else
6437             {
6438               int off;
6439
6440               if (breg == 0)
6441                 {
6442                   frag_grow (28);
6443                   tempreg = mips_gp_register;
6444                   off = 0;
6445                   used_at = 0;
6446                 }
6447               else
6448                 {
6449                   frag_grow (36);
6450                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6451                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6452                                "d,v,t", AT, breg, mips_gp_register);
6453                   tempreg = AT;
6454                   off = 4;
6455                   used_at = 1;
6456                 }
6457
6458               /* Itbl support may require additional care here.  */
6459               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6460                            coproc ? treg + 1 : treg,
6461                            (int) BFD_RELOC_GPREL16, tempreg);
6462               offset_expr.X_add_number += 4;
6463
6464               /* Set mips_optimize to 2 to avoid inserting an
6465                  undesired nop.  */
6466               hold_mips_optimize = mips_optimize;
6467               mips_optimize = 2;
6468               /* Itbl support may require additional care here.  */
6469               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6470                            coproc ? treg : treg + 1,
6471                            (int) BFD_RELOC_GPREL16, tempreg);
6472               mips_optimize = hold_mips_optimize;
6473
6474               p = frag_var (rs_machine_dependent, 12 + off, 0,
6475                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6476                                           used_at && mips_opts.noat),
6477                             offset_expr.X_add_symbol, 0, NULL);
6478
6479               /* We just generated two relocs.  When tc_gen_reloc
6480                  handles this case, it will skip the first reloc and
6481                  handle the second.  The second reloc already has an
6482                  extra addend of 4, which we added above.  We must
6483                  subtract it out, and then subtract another 4 to make
6484                  the first reloc come out right.  The second reloc
6485                  will come out right because we are going to add 4 to
6486                  offset_expr when we build its instruction below.
6487
6488                  If we have a symbol, then we don't want to include
6489                  the offset, because it will wind up being included
6490                  when we generate the reloc.  */
6491
6492               if (offset_expr.X_op == O_constant)
6493                 offset_expr.X_add_number -= 8;
6494               else
6495                 {
6496                   offset_expr.X_add_number = -4;
6497                   offset_expr.X_op = O_constant;
6498                 }
6499             }
6500           macro_build_lui (p, &icnt, &offset_expr, AT);
6501           if (p != NULL)
6502             p += 4;
6503           if (breg != 0)
6504             {
6505               macro_build (p, &icnt, (expressionS *) NULL,
6506                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6507                            "d,v,t", AT, breg, AT);
6508               if (p != NULL)
6509                 p += 4;
6510             }
6511           /* Itbl support may require additional care here.  */
6512           macro_build (p, &icnt, &offset_expr, s, fmt,
6513                        coproc ? treg + 1 : treg,
6514                        (int) BFD_RELOC_LO16, AT);
6515           if (p != NULL)
6516             p += 4;
6517           /* FIXME: How do we handle overflow here?  */
6518           offset_expr.X_add_number += 4;
6519           /* Itbl support may require additional care here.  */
6520           macro_build (p, &icnt, &offset_expr, s, fmt,
6521                        coproc ? treg : treg + 1,
6522                        (int) BFD_RELOC_LO16, AT);
6523         }
6524       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6525         {
6526           int off;
6527
6528           /* If this is a reference to an external symbol, we want
6529                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6530                nop
6531                <op>     $treg,0($at)
6532                <op>     $treg+1,4($at)
6533              Otherwise we want
6534                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6535                nop
6536                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6537                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6538              If there is a base register we add it to $at before the
6539              lwc1 instructions.  If there is a constant we include it
6540              in the lwc1 instructions.  */
6541           used_at = 1;
6542           expr1.X_add_number = offset_expr.X_add_number;
6543           offset_expr.X_add_number = 0;
6544           if (expr1.X_add_number < -0x8000
6545               || expr1.X_add_number >= 0x8000 - 4)
6546             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6547           if (breg == 0)
6548             off = 0;
6549           else
6550             off = 4;
6551           frag_grow (24 + off);
6552           macro_build ((char *) NULL, &icnt, &offset_expr,
6553                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6554                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6555           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6556           if (breg != 0)
6557             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6558                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6559                          "d,v,t", AT, breg, AT);
6560           /* Itbl support may require additional care here.  */
6561           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6562                        coproc ? treg + 1 : treg,
6563                        (int) BFD_RELOC_LO16, AT);
6564           expr1.X_add_number += 4;
6565
6566           /* Set mips_optimize to 2 to avoid inserting an undesired
6567              nop.  */
6568           hold_mips_optimize = mips_optimize;
6569           mips_optimize = 2;
6570           /* Itbl support may require additional care here.  */
6571           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6572                        coproc ? treg : treg + 1,
6573                        (int) BFD_RELOC_LO16, AT);
6574           mips_optimize = hold_mips_optimize;
6575
6576           (void) frag_var (rs_machine_dependent, 0, 0,
6577                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6578                            offset_expr.X_add_symbol, 0, NULL);
6579         }
6580       else if (mips_pic == SVR4_PIC)
6581         {
6582           int gpdel, off;
6583           char *p;
6584
6585           /* If this is a reference to an external symbol, we want
6586                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6587                addu     $at,$at,$gp
6588                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6589                nop
6590                <op>     $treg,0($at)
6591                <op>     $treg+1,4($at)
6592              Otherwise we want
6593                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6594                nop
6595                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6596                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6597              If there is a base register we add it to $at before the
6598              lwc1 instructions.  If there is a constant we include it
6599              in the lwc1 instructions.  */
6600           used_at = 1;
6601           expr1.X_add_number = offset_expr.X_add_number;
6602           offset_expr.X_add_number = 0;
6603           if (expr1.X_add_number < -0x8000
6604               || expr1.X_add_number >= 0x8000 - 4)
6605             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6606           if (reg_needs_delay (mips_gp_register))
6607             gpdel = 4;
6608           else
6609             gpdel = 0;
6610           if (breg == 0)
6611             off = 0;
6612           else
6613             off = 4;
6614           frag_grow (56);
6615           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6616                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6617           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6618                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6619                        "d,v,t", AT, AT, mips_gp_register);
6620           macro_build ((char *) NULL, &icnt, &offset_expr,
6621                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6622                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6623           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6624           if (breg != 0)
6625             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6626                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6627                          "d,v,t", AT, breg, AT);
6628           /* Itbl support may require additional care here.  */
6629           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6630                        coproc ? treg + 1 : treg,
6631                        (int) BFD_RELOC_LO16, AT);
6632           expr1.X_add_number += 4;
6633
6634           /* Set mips_optimize to 2 to avoid inserting an undesired
6635              nop.  */
6636           hold_mips_optimize = mips_optimize;
6637           mips_optimize = 2;
6638           /* Itbl support may require additional care here.  */
6639           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6640                        coproc ? treg : treg + 1,
6641                        (int) BFD_RELOC_LO16, AT);
6642           mips_optimize = hold_mips_optimize;
6643           expr1.X_add_number -= 4;
6644
6645           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6646                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6647                                       8 + gpdel + off, 1, 0),
6648                         offset_expr.X_add_symbol, 0, NULL);
6649           if (gpdel > 0)
6650             {
6651               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6652               p += 4;
6653             }
6654           macro_build (p, &icnt, &offset_expr,
6655                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6656                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6657                        mips_gp_register);
6658           p += 4;
6659           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6660           p += 4;
6661           if (breg != 0)
6662             {
6663               macro_build (p, &icnt, (expressionS *) NULL,
6664                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6665                            "d,v,t", AT, breg, AT);
6666               p += 4;
6667             }
6668           /* Itbl support may require additional care here.  */
6669           macro_build (p, &icnt, &expr1, s, fmt,
6670                        coproc ? treg + 1 : treg,
6671                        (int) BFD_RELOC_LO16, AT);
6672           p += 4;
6673           expr1.X_add_number += 4;
6674
6675           /* Set mips_optimize to 2 to avoid inserting an undesired
6676              nop.  */
6677           hold_mips_optimize = mips_optimize;
6678           mips_optimize = 2;
6679           /* Itbl support may require additional care here.  */
6680           macro_build (p, &icnt, &expr1, s, fmt,
6681                        coproc ? treg : treg + 1,
6682                        (int) BFD_RELOC_LO16, AT);
6683           mips_optimize = hold_mips_optimize;
6684         }
6685       else if (mips_pic == EMBEDDED_PIC)
6686         {
6687           /* If there is no base register, we use
6688                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6689                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6690              If we have a base register, we use
6691                addu     $at,$breg,$gp
6692                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6693                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6694              */
6695           if (breg == 0)
6696             {
6697               tempreg = mips_gp_register;
6698               used_at = 0;
6699             }
6700           else
6701             {
6702               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6703                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6704                            "d,v,t", AT, breg, mips_gp_register);
6705               tempreg = AT;
6706               used_at = 1;
6707             }
6708
6709           /* Itbl support may require additional care here.  */
6710           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6711                        coproc ? treg + 1 : treg,
6712                        (int) BFD_RELOC_GPREL16, tempreg);
6713           offset_expr.X_add_number += 4;
6714           /* Itbl support may require additional care here.  */
6715           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6716                        coproc ? treg : treg + 1,
6717                        (int) BFD_RELOC_GPREL16, tempreg);
6718         }
6719       else
6720         abort ();
6721
6722       if (! used_at)
6723         return;
6724
6725       break;
6726
6727     case M_LD_OB:
6728       s = "lw";
6729       goto sd_ob;
6730     case M_SD_OB:
6731       s = "sw";
6732     sd_ob:
6733       assert (HAVE_32BIT_ADDRESSES);
6734       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6735                    (int) BFD_RELOC_LO16, breg);
6736       offset_expr.X_add_number += 4;
6737       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6738                    (int) BFD_RELOC_LO16, breg);
6739       return;
6740
6741    /* New code added to support COPZ instructions.
6742       This code builds table entries out of the macros in mip_opcodes.
6743       R4000 uses interlocks to handle coproc delays.
6744       Other chips (like the R3000) require nops to be inserted for delays.
6745
6746       FIXME: Currently, we require that the user handle delays.
6747       In order to fill delay slots for non-interlocked chips,
6748       we must have a way to specify delays based on the coprocessor.
6749       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6750       What are the side-effects of the cop instruction?
6751       What cache support might we have and what are its effects?
6752       Both coprocessor & memory require delays. how long???
6753       What registers are read/set/modified?
6754
6755       If an itbl is provided to interpret cop instructions,
6756       this knowledge can be encoded in the itbl spec.  */
6757
6758     case M_COP0:
6759       s = "c0";
6760       goto copz;
6761     case M_COP1:
6762       s = "c1";
6763       goto copz;
6764     case M_COP2:
6765       s = "c2";
6766       goto copz;
6767     case M_COP3:
6768       s = "c3";
6769     copz:
6770       /* For now we just do C (same as Cz).  The parameter will be
6771          stored in insn_opcode by mips_ip.  */
6772       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6773                    ip->insn_opcode);
6774       return;
6775
6776     case M_MOVE:
6777       move_register (&icnt, dreg, sreg);
6778       return;
6779
6780 #ifdef LOSING_COMPILER
6781     default:
6782       /* Try and see if this is a new itbl instruction.
6783          This code builds table entries out of the macros in mip_opcodes.
6784          FIXME: For now we just assemble the expression and pass it's
6785          value along as a 32-bit immediate.
6786          We may want to have the assembler assemble this value,
6787          so that we gain the assembler's knowledge of delay slots,
6788          symbols, etc.
6789          Would it be more efficient to use mask (id) here? */
6790       if (itbl_have_entries
6791           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6792         {
6793           s = ip->insn_mo->name;
6794           s2 = "cop3";
6795           coproc = ITBL_DECODE_PNUM (immed_expr);;
6796           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6797           return;
6798         }
6799       macro2 (ip);
6800       return;
6801     }
6802   if (mips_opts.noat)
6803     as_warn (_("Macro used $at after \".set noat\""));
6804 }
6805
6806 static void
6807 macro2 (ip)
6808      struct mips_cl_insn *ip;
6809 {
6810   register int treg, sreg, dreg, breg;
6811   int tempreg;
6812   int mask;
6813   int icnt = 0;
6814   int used_at;
6815   expressionS expr1;
6816   const char *s;
6817   const char *s2;
6818   const char *fmt;
6819   int likely = 0;
6820   int dbl = 0;
6821   int coproc = 0;
6822   int lr = 0;
6823   int imm = 0;
6824   int off;
6825   offsetT maxnum;
6826   bfd_reloc_code_real_type r;
6827   char *p;
6828
6829   treg = (ip->insn_opcode >> 16) & 0x1f;
6830   dreg = (ip->insn_opcode >> 11) & 0x1f;
6831   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6832   mask = ip->insn_mo->mask;
6833
6834   expr1.X_op = O_constant;
6835   expr1.X_op_symbol = NULL;
6836   expr1.X_add_symbol = NULL;
6837   expr1.X_add_number = 1;
6838
6839   switch (mask)
6840     {
6841 #endif /* LOSING_COMPILER */
6842
6843     case M_DMUL:
6844       dbl = 1;
6845     case M_MUL:
6846       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6847                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6848       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6849                    dreg);
6850       return;
6851
6852     case M_DMUL_I:
6853       dbl = 1;
6854     case M_MUL_I:
6855       /* The MIPS assembler some times generates shifts and adds.  I'm
6856          not trying to be that fancy. GCC should do this for us
6857          anyway.  */
6858       load_register (&icnt, AT, &imm_expr, dbl);
6859       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6860                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6861       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6862                    dreg);
6863       break;
6864
6865     case M_DMULO_I:
6866       dbl = 1;
6867     case M_MULO_I:
6868       imm = 1;
6869       goto do_mulo;
6870
6871     case M_DMULO:
6872       dbl = 1;
6873     case M_MULO:
6874     do_mulo:
6875       mips_emit_delays (TRUE);
6876       ++mips_opts.noreorder;
6877       mips_any_noreorder = 1;
6878       if (imm)
6879         load_register (&icnt, AT, &imm_expr, dbl);
6880       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6881                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6882       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6883                    dreg);
6884       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6885                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6886       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6887                    AT);
6888       if (mips_trap)
6889         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6890                      "s,t,q", dreg, AT, 6);
6891       else
6892         {
6893           expr1.X_add_number = 8;
6894           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6895                        AT);
6896           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6897                        0);
6898           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6899                        "c", 6);
6900         }
6901       --mips_opts.noreorder;
6902       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6903       break;
6904
6905     case M_DMULOU_I:
6906       dbl = 1;
6907     case M_MULOU_I:
6908       imm = 1;
6909       goto do_mulou;
6910
6911     case M_DMULOU:
6912       dbl = 1;
6913     case M_MULOU:
6914     do_mulou:
6915       mips_emit_delays (TRUE);
6916       ++mips_opts.noreorder;
6917       mips_any_noreorder = 1;
6918       if (imm)
6919         load_register (&icnt, AT, &imm_expr, dbl);
6920       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6921                    dbl ? "dmultu" : "multu",
6922                    "s,t", sreg, imm ? AT : treg);
6923       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6924                    AT);
6925       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6926                    dreg);
6927       if (mips_trap)
6928         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6929                      "s,t,q", AT, 0, 6);
6930       else
6931         {
6932           expr1.X_add_number = 8;
6933           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6934           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6935                        0);
6936           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6937                        "c", 6);
6938         }
6939       --mips_opts.noreorder;
6940       break;
6941
6942     case M_DROL:
6943       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6944                    "d,v,t", AT, 0, treg);
6945       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6946                    "d,t,s", AT, sreg, AT);
6947       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6948                    "d,t,s", dreg, sreg, treg);
6949       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6950                    "d,v,t", dreg, dreg, AT);
6951       break;
6952
6953     case M_ROL:
6954       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6955                    "d,v,t", AT, 0, treg);
6956       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6957                    "d,t,s", AT, sreg, AT);
6958       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6959                    "d,t,s", dreg, sreg, treg);
6960       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6961                    "d,v,t", dreg, dreg, AT);
6962       break;
6963
6964     case M_DROL_I:
6965       {
6966         unsigned int rot;
6967
6968         if (imm_expr.X_op != O_constant)
6969           as_bad (_("rotate count too large"));
6970         rot = imm_expr.X_add_number & 0x3f;
6971         if (CPU_HAS_DROR (mips_arch))
6972           {
6973             rot = (64 - rot) & 0x3f;
6974             if (rot >= 32)
6975               macro_build ((char *) NULL, &icnt, NULL, "dror32",
6976                            "d,w,<", dreg, sreg, rot - 32);
6977             else
6978               macro_build ((char *) NULL, &icnt, NULL, "dror",
6979                            "d,w,<", dreg, sreg, rot);
6980             break;
6981           }
6982         if (rot == 0)
6983           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6984                        "d,w,<", dreg, sreg, 0);
6985         else
6986           {
6987             char *l, *r;
6988
6989             l = (rot < 0x20) ? "dsll" : "dsll32";
6990             r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6991             rot &= 0x1f;
6992             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6993                          "d,w,<", AT, sreg, rot);
6994             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6995                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6996             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6997                          "d,v,t", dreg, dreg, AT);
6998           }
6999       }
7000       break;
7001
7002     case M_ROL_I:
7003       {
7004         unsigned int rot;
7005
7006         if (imm_expr.X_op != O_constant)
7007           as_bad (_("rotate count too large"));
7008         rot = imm_expr.X_add_number & 0x1f;
7009         if (CPU_HAS_ROR (mips_arch))
7010           {
7011             macro_build ((char *) NULL, &icnt, NULL, "ror",
7012                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7013             break;
7014           }
7015         if (rot == 0)
7016           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7017                        "d,w,<", dreg, sreg, 0);
7018         else
7019           {
7020             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7021                          "d,w,<", AT, sreg, rot);
7022             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7023                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7024             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7025                          "d,v,t", dreg, dreg, AT);
7026           }
7027       }
7028       break;
7029
7030     case M_DROR:
7031       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7032                    "d,v,t", AT, 0, treg);
7033       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7034                    "d,t,s", AT, sreg, AT);
7035       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7036                    "d,t,s", dreg, sreg, treg);
7037       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7038                    "d,v,t", dreg, dreg, AT);
7039       break;
7040
7041     case M_ROR:
7042       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7043                    "d,v,t", AT, 0, treg);
7044       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7045                    "d,t,s", AT, sreg, AT);
7046       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7047                    "d,t,s", dreg, sreg, treg);
7048       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7049                    "d,v,t", dreg, dreg, AT);
7050       break;
7051
7052     case M_DROR_I:
7053       {
7054         unsigned int rot;
7055
7056         if (imm_expr.X_op != O_constant)
7057           as_bad (_("rotate count too large"));
7058         rot = imm_expr.X_add_number & 0x3f;
7059         if (rot == 0)
7060           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7061                        "d,w,<", dreg, sreg, 0);
7062         else
7063           {
7064             char *l, *r;
7065
7066             r = (rot < 0x20) ? "dsrl" : "dsrl32";
7067             l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7068             rot &= 0x1f;
7069             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7070                          "d,w,<", AT, sreg, rot);
7071             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7072                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7073             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7074                          "d,v,t", dreg, dreg, AT);
7075           }
7076       }
7077       break;
7078
7079     case M_ROR_I:
7080       {
7081         unsigned int rot;
7082
7083         if (imm_expr.X_op != O_constant)
7084           as_bad (_("rotate count too large"));
7085         rot = imm_expr.X_add_number & 0x1f;
7086         if (rot == 0)
7087           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7088                        "d,w,<", dreg, sreg, 0);
7089         else
7090           {
7091             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7092                          "d,w,<", AT, sreg, rot);
7093             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7094                          "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7095             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7096                          "d,v,t", dreg, dreg, AT);
7097           }
7098       }
7099       break;
7100
7101     case M_S_DOB:
7102       if (mips_arch == CPU_R4650)
7103         {
7104           as_bad (_("opcode not supported on this processor"));
7105           return;
7106         }
7107       assert (mips_opts.isa == ISA_MIPS1);
7108       /* Even on a big endian machine $fn comes before $fn+1.  We have
7109          to adjust when storing to memory.  */
7110       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7111                    target_big_endian ? treg + 1 : treg,
7112                    (int) BFD_RELOC_LO16, breg);
7113       offset_expr.X_add_number += 4;
7114       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7115                    target_big_endian ? treg : treg + 1,
7116                    (int) BFD_RELOC_LO16, breg);
7117       return;
7118
7119     case M_SEQ:
7120       if (sreg == 0)
7121         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7122                      treg, (int) BFD_RELOC_LO16);
7123       else if (treg == 0)
7124         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7125                      sreg, (int) BFD_RELOC_LO16);
7126       else
7127         {
7128           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7129                        "d,v,t", dreg, sreg, treg);
7130           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7131                        dreg, (int) BFD_RELOC_LO16);
7132         }
7133       return;
7134
7135     case M_SEQ_I:
7136       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7137         {
7138           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7139                        sreg, (int) BFD_RELOC_LO16);
7140           return;
7141         }
7142       if (sreg == 0)
7143         {
7144           as_warn (_("Instruction %s: result is always false"),
7145                    ip->insn_mo->name);
7146           move_register (&icnt, dreg, 0);
7147           return;
7148         }
7149       if (imm_expr.X_op == O_constant
7150           && imm_expr.X_add_number >= 0
7151           && imm_expr.X_add_number < 0x10000)
7152         {
7153           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7154                        sreg, (int) BFD_RELOC_LO16);
7155           used_at = 0;
7156         }
7157       else if (imm_expr.X_op == O_constant
7158                && imm_expr.X_add_number > -0x8000
7159                && imm_expr.X_add_number < 0)
7160         {
7161           imm_expr.X_add_number = -imm_expr.X_add_number;
7162           macro_build ((char *) NULL, &icnt, &imm_expr,
7163                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7164                        "t,r,j", dreg, sreg,
7165                        (int) BFD_RELOC_LO16);
7166           used_at = 0;
7167         }
7168       else
7169         {
7170           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7171           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7172                        "d,v,t", dreg, sreg, AT);
7173           used_at = 1;
7174         }
7175       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7176                    (int) BFD_RELOC_LO16);
7177       if (used_at)
7178         break;
7179       return;
7180
7181     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7182       s = "slt";
7183       goto sge;
7184     case M_SGEU:
7185       s = "sltu";
7186     sge:
7187       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7188                    dreg, sreg, treg);
7189       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7190                    (int) BFD_RELOC_LO16);
7191       return;
7192
7193     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7194     case M_SGEU_I:
7195       if (imm_expr.X_op == O_constant
7196           && imm_expr.X_add_number >= -0x8000
7197           && imm_expr.X_add_number < 0x8000)
7198         {
7199           macro_build ((char *) NULL, &icnt, &imm_expr,
7200                        mask == M_SGE_I ? "slti" : "sltiu",
7201                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7202           used_at = 0;
7203         }
7204       else
7205         {
7206           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7207           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7208                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7209                        AT);
7210           used_at = 1;
7211         }
7212       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7213                    (int) BFD_RELOC_LO16);
7214       if (used_at)
7215         break;
7216       return;
7217
7218     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7219       s = "slt";
7220       goto sgt;
7221     case M_SGTU:
7222       s = "sltu";
7223     sgt:
7224       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7225                    dreg, treg, sreg);
7226       return;
7227
7228     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7229       s = "slt";
7230       goto sgti;
7231     case M_SGTU_I:
7232       s = "sltu";
7233     sgti:
7234       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7235       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7236                    dreg, AT, sreg);
7237       break;
7238
7239     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7240       s = "slt";
7241       goto sle;
7242     case M_SLEU:
7243       s = "sltu";
7244     sle:
7245       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7246                    dreg, treg, sreg);
7247       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7248                    (int) BFD_RELOC_LO16);
7249       return;
7250
7251     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7252       s = "slt";
7253       goto slei;
7254     case M_SLEU_I:
7255       s = "sltu";
7256     slei:
7257       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7258       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7259                    dreg, AT, sreg);
7260       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7261                    (int) BFD_RELOC_LO16);
7262       break;
7263
7264     case M_SLT_I:
7265       if (imm_expr.X_op == O_constant
7266           && imm_expr.X_add_number >= -0x8000
7267           && imm_expr.X_add_number < 0x8000)
7268         {
7269           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7270                        dreg, sreg, (int) BFD_RELOC_LO16);
7271           return;
7272         }
7273       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7274       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7275                    dreg, sreg, AT);
7276       break;
7277
7278     case M_SLTU_I:
7279       if (imm_expr.X_op == O_constant
7280           && imm_expr.X_add_number >= -0x8000
7281           && imm_expr.X_add_number < 0x8000)
7282         {
7283           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7284                        dreg, sreg, (int) BFD_RELOC_LO16);
7285           return;
7286         }
7287       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7288       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7289                    "d,v,t", dreg, sreg, AT);
7290       break;
7291
7292     case M_SNE:
7293       if (sreg == 0)
7294         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7295                      "d,v,t", dreg, 0, treg);
7296       else if (treg == 0)
7297         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7298                      "d,v,t", dreg, 0, sreg);
7299       else
7300         {
7301           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7302                        "d,v,t", dreg, sreg, treg);
7303           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7304                        "d,v,t", dreg, 0, dreg);
7305         }
7306       return;
7307
7308     case M_SNE_I:
7309       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7310         {
7311           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7312                        "d,v,t", dreg, 0, sreg);
7313           return;
7314         }
7315       if (sreg == 0)
7316         {
7317           as_warn (_("Instruction %s: result is always true"),
7318                    ip->insn_mo->name);
7319           macro_build ((char *) NULL, &icnt, &expr1,
7320                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7321                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7322           return;
7323         }
7324       if (imm_expr.X_op == O_constant
7325           && imm_expr.X_add_number >= 0
7326           && imm_expr.X_add_number < 0x10000)
7327         {
7328           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7329                        dreg, sreg, (int) BFD_RELOC_LO16);
7330           used_at = 0;
7331         }
7332       else if (imm_expr.X_op == O_constant
7333                && imm_expr.X_add_number > -0x8000
7334                && imm_expr.X_add_number < 0)
7335         {
7336           imm_expr.X_add_number = -imm_expr.X_add_number;
7337           macro_build ((char *) NULL, &icnt, &imm_expr,
7338                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7339                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7340           used_at = 0;
7341         }
7342       else
7343         {
7344           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7345           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7346                        "d,v,t", dreg, sreg, AT);
7347           used_at = 1;
7348         }
7349       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7350                    "d,v,t", dreg, 0, dreg);
7351       if (used_at)
7352         break;
7353       return;
7354
7355     case M_DSUB_I:
7356       dbl = 1;
7357     case M_SUB_I:
7358       if (imm_expr.X_op == O_constant
7359           && imm_expr.X_add_number > -0x8000
7360           && imm_expr.X_add_number <= 0x8000)
7361         {
7362           imm_expr.X_add_number = -imm_expr.X_add_number;
7363           macro_build ((char *) NULL, &icnt, &imm_expr,
7364                        dbl ? "daddi" : "addi",
7365                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7366           return;
7367         }
7368       load_register (&icnt, AT, &imm_expr, dbl);
7369       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7370                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7371       break;
7372
7373     case M_DSUBU_I:
7374       dbl = 1;
7375     case M_SUBU_I:
7376       if (imm_expr.X_op == O_constant
7377           && imm_expr.X_add_number > -0x8000
7378           && imm_expr.X_add_number <= 0x8000)
7379         {
7380           imm_expr.X_add_number = -imm_expr.X_add_number;
7381           macro_build ((char *) NULL, &icnt, &imm_expr,
7382                        dbl ? "daddiu" : "addiu",
7383                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7384           return;
7385         }
7386       load_register (&icnt, AT, &imm_expr, dbl);
7387       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7388                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7389       break;
7390
7391     case M_TEQ_I:
7392       s = "teq";
7393       goto trap;
7394     case M_TGE_I:
7395       s = "tge";
7396       goto trap;
7397     case M_TGEU_I:
7398       s = "tgeu";
7399       goto trap;
7400     case M_TLT_I:
7401       s = "tlt";
7402       goto trap;
7403     case M_TLTU_I:
7404       s = "tltu";
7405       goto trap;
7406     case M_TNE_I:
7407       s = "tne";
7408     trap:
7409       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7410       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7411                    AT);
7412       break;
7413
7414     case M_TRUNCWS:
7415     case M_TRUNCWD:
7416       assert (mips_opts.isa == ISA_MIPS1);
7417       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7418       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7419
7420       /*
7421        * Is the double cfc1 instruction a bug in the mips assembler;
7422        * or is there a reason for it?
7423        */
7424       mips_emit_delays (TRUE);
7425       ++mips_opts.noreorder;
7426       mips_any_noreorder = 1;
7427       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7428                    treg, RA);
7429       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7430                    treg, RA);
7431       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7432       expr1.X_add_number = 3;
7433       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7434                    (int) BFD_RELOC_LO16);
7435       expr1.X_add_number = 2;
7436       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7437                      (int) BFD_RELOC_LO16);
7438       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7439                    AT, RA);
7440       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7441       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7442               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7443       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7444                    treg, RA);
7445       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7446       --mips_opts.noreorder;
7447       break;
7448
7449     case M_ULH:
7450       s = "lb";
7451       goto ulh;
7452     case M_ULHU:
7453       s = "lbu";
7454     ulh:
7455       if (offset_expr.X_add_number >= 0x7fff)
7456         as_bad (_("operand overflow"));
7457       /* avoid load delay */
7458       if (! target_big_endian)
7459         ++offset_expr.X_add_number;
7460       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7461                    (int) BFD_RELOC_LO16, breg);
7462       if (! target_big_endian)
7463         --offset_expr.X_add_number;
7464       else
7465         ++offset_expr.X_add_number;
7466       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7467                    (int) BFD_RELOC_LO16, breg);
7468       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7469                    treg, treg, 8);
7470       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7471                    treg, treg, AT);
7472       break;
7473
7474     case M_ULD:
7475       s = "ldl";
7476       s2 = "ldr";
7477       off = 7;
7478       goto ulw;
7479     case M_ULW:
7480       s = "lwl";
7481       s2 = "lwr";
7482       off = 3;
7483     ulw:
7484       if (offset_expr.X_add_number >= 0x8000 - off)
7485         as_bad (_("operand overflow"));
7486       if (! target_big_endian)
7487         offset_expr.X_add_number += off;
7488       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7489                    (int) BFD_RELOC_LO16, breg);
7490       if (! target_big_endian)
7491         offset_expr.X_add_number -= off;
7492       else
7493         offset_expr.X_add_number += off;
7494       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7495                    (int) BFD_RELOC_LO16, breg);
7496       return;
7497
7498     case M_ULD_A:
7499       s = "ldl";
7500       s2 = "ldr";
7501       off = 7;
7502       goto ulwa;
7503     case M_ULW_A:
7504       s = "lwl";
7505       s2 = "lwr";
7506       off = 3;
7507     ulwa:
7508       used_at = 1;
7509       load_address (&icnt, AT, &offset_expr, &used_at);
7510       if (breg != 0)
7511         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7512                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7513                      "d,v,t", AT, AT, breg);
7514       if (! target_big_endian)
7515         expr1.X_add_number = off;
7516       else
7517         expr1.X_add_number = 0;
7518       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7519                    (int) BFD_RELOC_LO16, AT);
7520       if (! target_big_endian)
7521         expr1.X_add_number = 0;
7522       else
7523         expr1.X_add_number = off;
7524       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7525                    (int) BFD_RELOC_LO16, AT);
7526       break;
7527
7528     case M_ULH_A:
7529     case M_ULHU_A:
7530       used_at = 1;
7531       load_address (&icnt, AT, &offset_expr, &used_at);
7532       if (breg != 0)
7533         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7534                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7535                      "d,v,t", AT, AT, breg);
7536       if (target_big_endian)
7537         expr1.X_add_number = 0;
7538       macro_build ((char *) NULL, &icnt, &expr1,
7539                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7540                    (int) BFD_RELOC_LO16, AT);
7541       if (target_big_endian)
7542         expr1.X_add_number = 1;
7543       else
7544         expr1.X_add_number = 0;
7545       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7546                    (int) BFD_RELOC_LO16, AT);
7547       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7548                    treg, treg, 8);
7549       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7550                    treg, treg, AT);
7551       break;
7552
7553     case M_USH:
7554       if (offset_expr.X_add_number >= 0x7fff)
7555         as_bad (_("operand overflow"));
7556       if (target_big_endian)
7557         ++offset_expr.X_add_number;
7558       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7559                    (int) BFD_RELOC_LO16, breg);
7560       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7561                    AT, treg, 8);
7562       if (target_big_endian)
7563         --offset_expr.X_add_number;
7564       else
7565         ++offset_expr.X_add_number;
7566       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7567                    (int) BFD_RELOC_LO16, breg);
7568       break;
7569
7570     case M_USD:
7571       s = "sdl";
7572       s2 = "sdr";
7573       off = 7;
7574       goto usw;
7575     case M_USW:
7576       s = "swl";
7577       s2 = "swr";
7578       off = 3;
7579     usw:
7580       if (offset_expr.X_add_number >= 0x8000 - off)
7581         as_bad (_("operand overflow"));
7582       if (! target_big_endian)
7583         offset_expr.X_add_number += off;
7584       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7585                    (int) BFD_RELOC_LO16, breg);
7586       if (! target_big_endian)
7587         offset_expr.X_add_number -= off;
7588       else
7589         offset_expr.X_add_number += off;
7590       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7591                    (int) BFD_RELOC_LO16, breg);
7592       return;
7593
7594     case M_USD_A:
7595       s = "sdl";
7596       s2 = "sdr";
7597       off = 7;
7598       goto uswa;
7599     case M_USW_A:
7600       s = "swl";
7601       s2 = "swr";
7602       off = 3;
7603     uswa:
7604       used_at = 1;
7605       load_address (&icnt, AT, &offset_expr, &used_at);
7606       if (breg != 0)
7607         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7608                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7609                      "d,v,t", AT, AT, breg);
7610       if (! target_big_endian)
7611         expr1.X_add_number = off;
7612       else
7613         expr1.X_add_number = 0;
7614       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7615                    (int) BFD_RELOC_LO16, AT);
7616       if (! target_big_endian)
7617         expr1.X_add_number = 0;
7618       else
7619         expr1.X_add_number = off;
7620       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7621                    (int) BFD_RELOC_LO16, AT);
7622       break;
7623
7624     case M_USH_A:
7625       used_at = 1;
7626       load_address (&icnt, AT, &offset_expr, &used_at);
7627       if (breg != 0)
7628         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7629                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7630                      "d,v,t", AT, AT, breg);
7631       if (! target_big_endian)
7632         expr1.X_add_number = 0;
7633       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7634                    (int) BFD_RELOC_LO16, AT);
7635       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7636                    treg, treg, 8);
7637       if (! target_big_endian)
7638         expr1.X_add_number = 1;
7639       else
7640         expr1.X_add_number = 0;
7641       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7642                    (int) BFD_RELOC_LO16, AT);
7643       if (! target_big_endian)
7644         expr1.X_add_number = 0;
7645       else
7646         expr1.X_add_number = 1;
7647       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7648                    (int) BFD_RELOC_LO16, AT);
7649       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7650                    treg, treg, 8);
7651       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7652                    treg, treg, AT);
7653       break;
7654
7655     default:
7656       /* FIXME: Check if this is one of the itbl macros, since they
7657          are added dynamically.  */
7658       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7659       break;
7660     }
7661   if (mips_opts.noat)
7662     as_warn (_("Macro used $at after \".set noat\""));
7663 }
7664
7665 /* Implement macros in mips16 mode.  */
7666
7667 static void
7668 mips16_macro (ip)
7669      struct mips_cl_insn *ip;
7670 {
7671   int mask;
7672   int xreg, yreg, zreg, tmp;
7673   int icnt;
7674   expressionS expr1;
7675   int dbl;
7676   const char *s, *s2, *s3;
7677
7678   mask = ip->insn_mo->mask;
7679
7680   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7681   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7682   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7683
7684   icnt = 0;
7685
7686   expr1.X_op = O_constant;
7687   expr1.X_op_symbol = NULL;
7688   expr1.X_add_symbol = NULL;
7689   expr1.X_add_number = 1;
7690
7691   dbl = 0;
7692
7693   switch (mask)
7694     {
7695     default:
7696       internalError ();
7697
7698     case M_DDIV_3:
7699       dbl = 1;
7700     case M_DIV_3:
7701       s = "mflo";
7702       goto do_div3;
7703     case M_DREM_3:
7704       dbl = 1;
7705     case M_REM_3:
7706       s = "mfhi";
7707     do_div3:
7708       mips_emit_delays (TRUE);
7709       ++mips_opts.noreorder;
7710       mips_any_noreorder = 1;
7711       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7712                    dbl ? "ddiv" : "div",
7713                    "0,x,y", xreg, yreg);
7714       expr1.X_add_number = 2;
7715       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7716       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7717                    7);
7718
7719       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7720          since that causes an overflow.  We should do that as well,
7721          but I don't see how to do the comparisons without a temporary
7722          register.  */
7723       --mips_opts.noreorder;
7724       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7725       break;
7726
7727     case M_DIVU_3:
7728       s = "divu";
7729       s2 = "mflo";
7730       goto do_divu3;
7731     case M_REMU_3:
7732       s = "divu";
7733       s2 = "mfhi";
7734       goto do_divu3;
7735     case M_DDIVU_3:
7736       s = "ddivu";
7737       s2 = "mflo";
7738       goto do_divu3;
7739     case M_DREMU_3:
7740       s = "ddivu";
7741       s2 = "mfhi";
7742     do_divu3:
7743       mips_emit_delays (TRUE);
7744       ++mips_opts.noreorder;
7745       mips_any_noreorder = 1;
7746       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7747                    xreg, yreg);
7748       expr1.X_add_number = 2;
7749       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7750       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7751                    "6", 7);
7752       --mips_opts.noreorder;
7753       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7754       break;
7755
7756     case M_DMUL:
7757       dbl = 1;
7758     case M_MUL:
7759       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7760                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7761       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7762                    zreg);
7763       return;
7764
7765     case M_DSUBU_I:
7766       dbl = 1;
7767       goto do_subu;
7768     case M_SUBU_I:
7769     do_subu:
7770       if (imm_expr.X_op != O_constant)
7771         as_bad (_("Unsupported large constant"));
7772       imm_expr.X_add_number = -imm_expr.X_add_number;
7773       macro_build ((char *) NULL, &icnt, &imm_expr,
7774                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7775       break;
7776
7777     case M_SUBU_I_2:
7778       if (imm_expr.X_op != O_constant)
7779         as_bad (_("Unsupported large constant"));
7780       imm_expr.X_add_number = -imm_expr.X_add_number;
7781       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7782                    "x,k", xreg);
7783       break;
7784
7785     case M_DSUBU_I_2:
7786       if (imm_expr.X_op != O_constant)
7787         as_bad (_("Unsupported large constant"));
7788       imm_expr.X_add_number = -imm_expr.X_add_number;
7789       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7790                    "y,j", yreg);
7791       break;
7792
7793     case M_BEQ:
7794       s = "cmp";
7795       s2 = "bteqz";
7796       goto do_branch;
7797     case M_BNE:
7798       s = "cmp";
7799       s2 = "btnez";
7800       goto do_branch;
7801     case M_BLT:
7802       s = "slt";
7803       s2 = "btnez";
7804       goto do_branch;
7805     case M_BLTU:
7806       s = "sltu";
7807       s2 = "btnez";
7808       goto do_branch;
7809     case M_BLE:
7810       s = "slt";
7811       s2 = "bteqz";
7812       goto do_reverse_branch;
7813     case M_BLEU:
7814       s = "sltu";
7815       s2 = "bteqz";
7816       goto do_reverse_branch;
7817     case M_BGE:
7818       s = "slt";
7819       s2 = "bteqz";
7820       goto do_branch;
7821     case M_BGEU:
7822       s = "sltu";
7823       s2 = "bteqz";
7824       goto do_branch;
7825     case M_BGT:
7826       s = "slt";
7827       s2 = "btnez";
7828       goto do_reverse_branch;
7829     case M_BGTU:
7830       s = "sltu";
7831       s2 = "btnez";
7832
7833     do_reverse_branch:
7834       tmp = xreg;
7835       xreg = yreg;
7836       yreg = tmp;
7837
7838     do_branch:
7839       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7840                    xreg, yreg);
7841       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7842       break;
7843
7844     case M_BEQ_I:
7845       s = "cmpi";
7846       s2 = "bteqz";
7847       s3 = "x,U";
7848       goto do_branch_i;
7849     case M_BNE_I:
7850       s = "cmpi";
7851       s2 = "btnez";
7852       s3 = "x,U";
7853       goto do_branch_i;
7854     case M_BLT_I:
7855       s = "slti";
7856       s2 = "btnez";
7857       s3 = "x,8";
7858       goto do_branch_i;
7859     case M_BLTU_I:
7860       s = "sltiu";
7861       s2 = "btnez";
7862       s3 = "x,8";
7863       goto do_branch_i;
7864     case M_BLE_I:
7865       s = "slti";
7866       s2 = "btnez";
7867       s3 = "x,8";
7868       goto do_addone_branch_i;
7869     case M_BLEU_I:
7870       s = "sltiu";
7871       s2 = "btnez";
7872       s3 = "x,8";
7873       goto do_addone_branch_i;
7874     case M_BGE_I:
7875       s = "slti";
7876       s2 = "bteqz";
7877       s3 = "x,8";
7878       goto do_branch_i;
7879     case M_BGEU_I:
7880       s = "sltiu";
7881       s2 = "bteqz";
7882       s3 = "x,8";
7883       goto do_branch_i;
7884     case M_BGT_I:
7885       s = "slti";
7886       s2 = "bteqz";
7887       s3 = "x,8";
7888       goto do_addone_branch_i;
7889     case M_BGTU_I:
7890       s = "sltiu";
7891       s2 = "bteqz";
7892       s3 = "x,8";
7893
7894     do_addone_branch_i:
7895       if (imm_expr.X_op != O_constant)
7896         as_bad (_("Unsupported large constant"));
7897       ++imm_expr.X_add_number;
7898
7899     do_branch_i:
7900       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7901       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7902       break;
7903
7904     case M_ABS:
7905       expr1.X_add_number = 0;
7906       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7907       if (xreg != yreg)
7908         move_register (&icnt, xreg, yreg);
7909       expr1.X_add_number = 2;
7910       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7911       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7912                    "neg", "x,w", xreg, xreg);
7913     }
7914 }
7915
7916 /* For consistency checking, verify that all bits are specified either
7917    by the match/mask part of the instruction definition, or by the
7918    operand list.  */
7919 static int
7920 validate_mips_insn (opc)
7921      const struct mips_opcode *opc;
7922 {
7923   const char *p = opc->args;
7924   char c;
7925   unsigned long used_bits = opc->mask;
7926
7927   if ((used_bits & opc->match) != opc->match)
7928     {
7929       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7930               opc->name, opc->args);
7931       return 0;
7932     }
7933 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7934   while (*p)
7935     switch (c = *p++)
7936       {
7937       case ',': break;
7938       case '(': break;
7939       case ')': break;
7940       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7941       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
7942       case 'A': break;
7943       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7944       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
7945       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7946       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7947       case 'F': break;
7948       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7949       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
7950       case 'I': break;
7951       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7952       case 'L': break;
7953       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
7954       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
7955       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
7956       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
7957                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7958       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
7959       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7960       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7961       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7962       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7963       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
7964       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
7965       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
7966       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
7967       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7968       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
7969       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
7970       case 'f': break;
7971       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
7972       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7973       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7974       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
7975       case 'l': break;
7976       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7977       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
7978       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
7979       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7980       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7981       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7982       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
7983       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
7984       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7985       case 'x': break;
7986       case 'z': break;
7987       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
7988       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7989                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
7990       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
7991       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
7992       case '[': break;
7993       case ']': break;
7994       default:
7995         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7996                 c, opc->name, opc->args);
7997         return 0;
7998       }
7999 #undef USE_BITS
8000   if (used_bits != 0xffffffff)
8001     {
8002       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8003               ~used_bits & 0xffffffff, opc->name, opc->args);
8004       return 0;
8005     }
8006   return 1;
8007 }
8008
8009 /* This routine assembles an instruction into its binary format.  As a
8010    side effect, it sets one of the global variables imm_reloc or
8011    offset_reloc to the type of relocation to do if one of the operands
8012    is an address expression.  */
8013
8014 static void
8015 mips_ip (str, ip)
8016      char *str;
8017      struct mips_cl_insn *ip;
8018 {
8019   char *s;
8020   const char *args;
8021   char c = 0;
8022   struct mips_opcode *insn;
8023   char *argsStart;
8024   unsigned int regno;
8025   unsigned int lastregno = 0;
8026   char *s_reset;
8027   char save_c = 0;
8028
8029   insn_error = NULL;
8030
8031   /* If the instruction contains a '.', we first try to match an instruction
8032      including the '.'.  Then we try again without the '.'.  */
8033   insn = NULL;
8034   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8035     continue;
8036
8037   /* If we stopped on whitespace, then replace the whitespace with null for
8038      the call to hash_find.  Save the character we replaced just in case we
8039      have to re-parse the instruction.  */
8040   if (ISSPACE (*s))
8041     {
8042       save_c = *s;
8043       *s++ = '\0';
8044     }
8045
8046   insn = (struct mips_opcode *) hash_find (op_hash, str);
8047
8048   /* If we didn't find the instruction in the opcode table, try again, but
8049      this time with just the instruction up to, but not including the
8050      first '.'.  */
8051   if (insn == NULL)
8052     {
8053       /* Restore the character we overwrite above (if any).  */
8054       if (save_c)
8055         *(--s) = save_c;
8056
8057       /* Scan up to the first '.' or whitespace.  */
8058       for (s = str;
8059            *s != '\0' && *s != '.' && !ISSPACE (*s);
8060            ++s)
8061         continue;
8062
8063       /* If we did not find a '.', then we can quit now.  */
8064       if (*s != '.')
8065         {
8066           insn_error = "unrecognized opcode";
8067           return;
8068         }
8069
8070       /* Lookup the instruction in the hash table.  */
8071       *s++ = '\0';
8072       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8073         {
8074           insn_error = "unrecognized opcode";
8075           return;
8076         }
8077     }
8078
8079   argsStart = s;
8080   for (;;)
8081     {
8082       bfd_boolean ok;
8083
8084       assert (strcmp (insn->name, str) == 0);
8085
8086       if (OPCODE_IS_MEMBER (insn,
8087                             (mips_opts.isa
8088                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8089                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8090                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8091                             mips_arch))
8092         ok = TRUE;
8093       else
8094         ok = FALSE;
8095
8096       if (insn->pinfo != INSN_MACRO)
8097         {
8098           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8099             ok = FALSE;
8100         }
8101
8102       if (! ok)
8103         {
8104           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8105               && strcmp (insn->name, insn[1].name) == 0)
8106             {
8107               ++insn;
8108               continue;
8109             }
8110           else
8111             {
8112               if (!insn_error)
8113                 {
8114                   static char buf[100];
8115                   if (mips_arch_info->is_isa)
8116                     sprintf (buf,
8117                              _("opcode not supported at this ISA level (%s)"),
8118                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8119                   else
8120                     sprintf (buf,
8121                              _("opcode not supported on this processor: %s (%s)"),
8122                              mips_arch_info->name,
8123                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8124                   insn_error = buf;
8125                 }
8126               if (save_c)
8127                 *(--s) = save_c;
8128               return;
8129             }
8130         }
8131
8132       ip->insn_mo = insn;
8133       ip->insn_opcode = insn->match;
8134       insn_error = NULL;
8135       for (args = insn->args;; ++args)
8136         {
8137           int is_mdmx;
8138
8139           s += strspn (s, " \t");
8140           is_mdmx = 0;
8141           switch (*args)
8142             {
8143             case '\0':          /* end of args */
8144               if (*s == '\0')
8145                 return;
8146               break;
8147
8148             case ',':
8149               if (*s++ == *args)
8150                 continue;
8151               s--;
8152               switch (*++args)
8153                 {
8154                 case 'r':
8155                 case 'v':
8156                   ip->insn_opcode |= lastregno << OP_SH_RS;
8157                   continue;
8158
8159                 case 'w':
8160                   ip->insn_opcode |= lastregno << OP_SH_RT;
8161                   continue;
8162
8163                 case 'W':
8164                   ip->insn_opcode |= lastregno << OP_SH_FT;
8165                   continue;
8166
8167                 case 'V':
8168                   ip->insn_opcode |= lastregno << OP_SH_FS;
8169                   continue;
8170                 }
8171               break;
8172
8173             case '(':
8174               /* Handle optional base register.
8175                  Either the base register is omitted or
8176                  we must have a left paren.  */
8177               /* This is dependent on the next operand specifier
8178                  is a base register specification.  */
8179               assert (args[1] == 'b' || args[1] == '5'
8180                       || args[1] == '-' || args[1] == '4');
8181               if (*s == '\0')
8182                 return;
8183
8184             case ')':           /* these must match exactly */
8185             case '[':
8186             case ']':
8187               if (*s++ == *args)
8188                 continue;
8189               break;
8190
8191             case '<':           /* must be at least one digit */
8192               /*
8193                * According to the manual, if the shift amount is greater
8194                * than 31 or less than 0, then the shift amount should be
8195                * mod 32.  In reality the mips assembler issues an error.
8196                * We issue a warning and mask out all but the low 5 bits.
8197                */
8198               my_getExpression (&imm_expr, s);
8199               check_absolute_expr (ip, &imm_expr);
8200               if ((unsigned long) imm_expr.X_add_number > 31)
8201                 {
8202                   as_warn (_("Improper shift amount (%lu)"),
8203                            (unsigned long) imm_expr.X_add_number);
8204                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8205                 }
8206               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8207               imm_expr.X_op = O_absent;
8208               s = expr_end;
8209               continue;
8210
8211             case '>':           /* shift amount minus 32 */
8212               my_getExpression (&imm_expr, s);
8213               check_absolute_expr (ip, &imm_expr);
8214               if ((unsigned long) imm_expr.X_add_number < 32
8215                   || (unsigned long) imm_expr.X_add_number > 63)
8216                 break;
8217               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8218               imm_expr.X_op = O_absent;
8219               s = expr_end;
8220               continue;
8221
8222             case 'k':           /* cache code */
8223             case 'h':           /* prefx code */
8224               my_getExpression (&imm_expr, s);
8225               check_absolute_expr (ip, &imm_expr);
8226               if ((unsigned long) imm_expr.X_add_number > 31)
8227                 {
8228                   as_warn (_("Invalid value for `%s' (%lu)"),
8229                            ip->insn_mo->name,
8230                            (unsigned long) imm_expr.X_add_number);
8231                   imm_expr.X_add_number &= 0x1f;
8232                 }
8233               if (*args == 'k')
8234                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8235               else
8236                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8237               imm_expr.X_op = O_absent;
8238               s = expr_end;
8239               continue;
8240
8241             case 'c':           /* break code */
8242               my_getExpression (&imm_expr, s);
8243               check_absolute_expr (ip, &imm_expr);
8244               if ((unsigned long) imm_expr.X_add_number > 1023)
8245                 {
8246                   as_warn (_("Illegal break code (%lu)"),
8247                            (unsigned long) imm_expr.X_add_number);
8248                   imm_expr.X_add_number &= OP_MASK_CODE;
8249                 }
8250               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8251               imm_expr.X_op = O_absent;
8252               s = expr_end;
8253               continue;
8254
8255             case 'q':           /* lower break code */
8256               my_getExpression (&imm_expr, s);
8257               check_absolute_expr (ip, &imm_expr);
8258               if ((unsigned long) imm_expr.X_add_number > 1023)
8259                 {
8260                   as_warn (_("Illegal lower break code (%lu)"),
8261                            (unsigned long) imm_expr.X_add_number);
8262                   imm_expr.X_add_number &= OP_MASK_CODE2;
8263                 }
8264               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8265               imm_expr.X_op = O_absent;
8266               s = expr_end;
8267               continue;
8268
8269             case 'B':           /* 20-bit syscall/break code.  */
8270               my_getExpression (&imm_expr, s);
8271               check_absolute_expr (ip, &imm_expr);
8272               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8273                 as_warn (_("Illegal 20-bit code (%lu)"),
8274                          (unsigned long) imm_expr.X_add_number);
8275               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8276               imm_expr.X_op = O_absent;
8277               s = expr_end;
8278               continue;
8279
8280             case 'C':           /* Coprocessor code */
8281               my_getExpression (&imm_expr, s);
8282               check_absolute_expr (ip, &imm_expr);
8283               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8284                 {
8285                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8286                            (unsigned long) imm_expr.X_add_number);
8287                   imm_expr.X_add_number &= ((1 << 25) - 1);
8288                 }
8289               ip->insn_opcode |= imm_expr.X_add_number;
8290               imm_expr.X_op = O_absent;
8291               s = expr_end;
8292               continue;
8293
8294             case 'J':           /* 19-bit wait code.  */
8295               my_getExpression (&imm_expr, s);
8296               check_absolute_expr (ip, &imm_expr);
8297               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8298                 as_warn (_("Illegal 19-bit code (%lu)"),
8299                          (unsigned long) imm_expr.X_add_number);
8300               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8301               imm_expr.X_op = O_absent;
8302               s = expr_end;
8303               continue;
8304
8305             case 'P':           /* Performance register */
8306               my_getExpression (&imm_expr, s);
8307               check_absolute_expr (ip, &imm_expr);
8308               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8309                 {
8310                   as_warn (_("Invalid performance register (%lu)"),
8311                            (unsigned long) imm_expr.X_add_number);
8312                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8313                 }
8314               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8315               imm_expr.X_op = O_absent;
8316               s = expr_end;
8317               continue;
8318
8319             case 'b':           /* base register */
8320             case 'd':           /* destination register */
8321             case 's':           /* source register */
8322             case 't':           /* target register */
8323             case 'r':           /* both target and source */
8324             case 'v':           /* both dest and source */
8325             case 'w':           /* both dest and target */
8326             case 'E':           /* coprocessor target register */
8327             case 'G':           /* coprocessor destination register */
8328             case 'x':           /* ignore register name */
8329             case 'z':           /* must be zero register */
8330             case 'U':           /* destination register (clo/clz).  */
8331               s_reset = s;
8332               if (s[0] == '$')
8333                 {
8334
8335                   if (ISDIGIT (s[1]))
8336                     {
8337                       ++s;
8338                       regno = 0;
8339                       do
8340                         {
8341                           regno *= 10;
8342                           regno += *s - '0';
8343                           ++s;
8344                         }
8345                       while (ISDIGIT (*s));
8346                       if (regno > 31)
8347                         as_bad (_("Invalid register number (%d)"), regno);
8348                     }
8349                   else if (*args == 'E' || *args == 'G')
8350                     goto notreg;
8351                   else
8352                     {
8353                       if (s[1] == 'r' && s[2] == 'a')
8354                         {
8355                           s += 3;
8356                           regno = RA;
8357                         }
8358                       else if (s[1] == 'f' && s[2] == 'p')
8359                         {
8360                           s += 3;
8361                           regno = FP;
8362                         }
8363                       else if (s[1] == 's' && s[2] == 'p')
8364                         {
8365                           s += 3;
8366                           regno = SP;
8367                         }
8368                       else if (s[1] == 'g' && s[2] == 'p')
8369                         {
8370                           s += 3;
8371                           regno = GP;
8372                         }
8373                       else if (s[1] == 'a' && s[2] == 't')
8374                         {
8375                           s += 3;
8376                           regno = AT;
8377                         }
8378                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8379                         {
8380                           s += 4;
8381                           regno = KT0;
8382                         }
8383                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8384                         {
8385                           s += 4;
8386                           regno = KT1;
8387                         }
8388                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8389                         {
8390                           s += 5;
8391                           regno = ZERO;
8392                         }
8393                       else if (itbl_have_entries)
8394                         {
8395                           char *p, *n;
8396                           unsigned long r;
8397
8398                           p = s + 1;    /* advance past '$' */
8399                           n = itbl_get_field (&p);  /* n is name */
8400
8401                           /* See if this is a register defined in an
8402                              itbl entry.  */
8403                           if (itbl_get_reg_val (n, &r))
8404                             {
8405                               /* Get_field advances to the start of
8406                                  the next field, so we need to back
8407                                  rack to the end of the last field.  */
8408                               if (p)
8409                                 s = p - 1;
8410                               else
8411                                 s = strchr (s, '\0');
8412                               regno = r;
8413                             }
8414                           else
8415                             goto notreg;
8416                         }
8417                       else
8418                         goto notreg;
8419                     }
8420                   if (regno == AT
8421                       && ! mips_opts.noat
8422                       && *args != 'E'
8423                       && *args != 'G')
8424                     as_warn (_("Used $at without \".set noat\""));
8425                   c = *args;
8426                   if (*s == ' ')
8427                     ++s;
8428                   if (args[1] != *s)
8429                     {
8430                       if (c == 'r' || c == 'v' || c == 'w')
8431                         {
8432                           regno = lastregno;
8433                           s = s_reset;
8434                           ++args;
8435                         }
8436                     }
8437                   /* 'z' only matches $0.  */
8438                   if (c == 'z' && regno != 0)
8439                     break;
8440
8441         /* Now that we have assembled one operand, we use the args string
8442          * to figure out where it goes in the instruction.  */
8443                   switch (c)
8444                     {
8445                     case 'r':
8446                     case 's':
8447                     case 'v':
8448                     case 'b':
8449                       ip->insn_opcode |= regno << OP_SH_RS;
8450                       break;
8451                     case 'd':
8452                     case 'G':
8453                       ip->insn_opcode |= regno << OP_SH_RD;
8454                       break;
8455                     case 'U':
8456                       ip->insn_opcode |= regno << OP_SH_RD;
8457                       ip->insn_opcode |= regno << OP_SH_RT;
8458                       break;
8459                     case 'w':
8460                     case 't':
8461                     case 'E':
8462                       ip->insn_opcode |= regno << OP_SH_RT;
8463                       break;
8464                     case 'x':
8465                       /* This case exists because on the r3000 trunc
8466                          expands into a macro which requires a gp
8467                          register.  On the r6000 or r4000 it is
8468                          assembled into a single instruction which
8469                          ignores the register.  Thus the insn version
8470                          is MIPS_ISA2 and uses 'x', and the macro
8471                          version is MIPS_ISA1 and uses 't'.  */
8472                       break;
8473                     case 'z':
8474                       /* This case is for the div instruction, which
8475                          acts differently if the destination argument
8476                          is $0.  This only matches $0, and is checked
8477                          outside the switch.  */
8478                       break;
8479                     case 'D':
8480                       /* Itbl operand; not yet implemented. FIXME ?? */
8481                       break;
8482                       /* What about all other operands like 'i', which
8483                          can be specified in the opcode table? */
8484                     }
8485                   lastregno = regno;
8486                   continue;
8487                 }
8488             notreg:
8489               switch (*args++)
8490                 {
8491                 case 'r':
8492                 case 'v':
8493                   ip->insn_opcode |= lastregno << OP_SH_RS;
8494                   continue;
8495                 case 'w':
8496                   ip->insn_opcode |= lastregno << OP_SH_RT;
8497                   continue;
8498                 }
8499               break;
8500
8501             case 'O':           /* MDMX alignment immediate constant.  */
8502               my_getExpression (&imm_expr, s);
8503               check_absolute_expr (ip, &imm_expr);
8504               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8505                 {
8506                   as_warn ("Improper align amount (%ld), using low bits",
8507                            (long) imm_expr.X_add_number);
8508                   imm_expr.X_add_number &= OP_MASK_ALN;
8509                 }
8510               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8511               imm_expr.X_op = O_absent;
8512               s = expr_end;
8513               continue;
8514
8515             case 'Q':           /* MDMX vector, element sel, or const.  */
8516               if (s[0] != '$')
8517                 {
8518                   /* MDMX Immediate.  */
8519                   my_getExpression (&imm_expr, s);
8520                   check_absolute_expr (ip, &imm_expr);
8521                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8522                     {
8523                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8524                                (long) imm_expr.X_add_number);
8525                       imm_expr.X_add_number &= OP_MASK_FT;
8526                     }
8527                   imm_expr.X_add_number &= OP_MASK_FT;
8528                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8529                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8530                   else
8531                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8532                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8533                   imm_expr.X_op = O_absent;
8534                   s = expr_end;
8535                   continue;
8536                 }
8537               /* Not MDMX Immediate.  Fall through.  */
8538             case 'X':           /* MDMX destination register.  */
8539             case 'Y':           /* MDMX source register.  */
8540             case 'Z':           /* MDMX target register.  */
8541               is_mdmx = 1;
8542             case 'D':           /* floating point destination register */
8543             case 'S':           /* floating point source register */
8544             case 'T':           /* floating point target register */
8545             case 'R':           /* floating point source register */
8546             case 'V':
8547             case 'W':
8548               s_reset = s;
8549               /* Accept $fN for FP and MDMX register numbers, and in
8550                  addition accept $vN for MDMX register numbers.  */
8551               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8552                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8553                       && ISDIGIT (s[2])))
8554                 {
8555                   s += 2;
8556                   regno = 0;
8557                   do
8558                     {
8559                       regno *= 10;
8560                       regno += *s - '0';
8561                       ++s;
8562                     }
8563                   while (ISDIGIT (*s));
8564
8565                   if (regno > 31)
8566                     as_bad (_("Invalid float register number (%d)"), regno);
8567
8568                   if ((regno & 1) != 0
8569                       && HAVE_32BIT_FPRS
8570                       && ! (strcmp (str, "mtc1") == 0
8571                             || strcmp (str, "mfc1") == 0
8572                             || strcmp (str, "lwc1") == 0
8573                             || strcmp (str, "swc1") == 0
8574                             || strcmp (str, "l.s") == 0
8575                             || strcmp (str, "s.s") == 0))
8576                     as_warn (_("Float register should be even, was %d"),
8577                              regno);
8578
8579                   c = *args;
8580                   if (*s == ' ')
8581                     ++s;
8582                   if (args[1] != *s)
8583                     {
8584                       if (c == 'V' || c == 'W')
8585                         {
8586                           regno = lastregno;
8587                           s = s_reset;
8588                           ++args;
8589                         }
8590                     }
8591                   switch (c)
8592                     {
8593                     case 'D':
8594                     case 'X':
8595                       ip->insn_opcode |= regno << OP_SH_FD;
8596                       break;
8597                     case 'V':
8598                     case 'S':
8599                     case 'Y':
8600                       ip->insn_opcode |= regno << OP_SH_FS;
8601                       break;
8602                     case 'Q':
8603                       /* This is like 'Z', but also needs to fix the MDMX
8604                          vector/scalar select bits.  Note that the
8605                          scalar immediate case is handled above.  */
8606                       if (*s == '[')
8607                         {
8608                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8609                           int max_el = (is_qh ? 3 : 7);
8610                           s++;
8611                           my_getExpression(&imm_expr, s);
8612                           check_absolute_expr (ip, &imm_expr);
8613                           s = expr_end;
8614                           if (imm_expr.X_add_number > max_el)
8615                             as_bad(_("Bad element selector %ld"),
8616                                    (long) imm_expr.X_add_number);
8617                           imm_expr.X_add_number &= max_el;
8618                           ip->insn_opcode |= (imm_expr.X_add_number
8619                                               << (OP_SH_VSEL +
8620                                                   (is_qh ? 2 : 1)));
8621                           if (*s != ']')
8622                             as_warn(_("Expecting ']' found '%s'"), s);
8623                           else
8624                             s++;
8625                         }
8626                       else
8627                         {
8628                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8629                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8630                                                 << OP_SH_VSEL);
8631                           else
8632                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8633                                                 OP_SH_VSEL);
8634                         }
8635                       /* Fall through */
8636                     case 'W':
8637                     case 'T':
8638                     case 'Z':
8639                       ip->insn_opcode |= regno << OP_SH_FT;
8640                       break;
8641                     case 'R':
8642                       ip->insn_opcode |= regno << OP_SH_FR;
8643                       break;
8644                     }
8645                   lastregno = regno;
8646                   continue;
8647                 }
8648
8649               switch (*args++)
8650                 {
8651                 case 'V':
8652                   ip->insn_opcode |= lastregno << OP_SH_FS;
8653                   continue;
8654                 case 'W':
8655                   ip->insn_opcode |= lastregno << OP_SH_FT;
8656                   continue;
8657                 }
8658               break;
8659
8660             case 'I':
8661               my_getExpression (&imm_expr, s);
8662               if (imm_expr.X_op != O_big
8663                   && imm_expr.X_op != O_constant)
8664                 insn_error = _("absolute expression required");
8665               s = expr_end;
8666               continue;
8667
8668             case 'A':
8669               my_getExpression (&offset_expr, s);
8670               *imm_reloc = BFD_RELOC_32;
8671               s = expr_end;
8672               continue;
8673
8674             case 'F':
8675             case 'L':
8676             case 'f':
8677             case 'l':
8678               {
8679                 int f64;
8680                 int using_gprs;
8681                 char *save_in;
8682                 char *err;
8683                 unsigned char temp[8];
8684                 int len;
8685                 unsigned int length;
8686                 segT seg;
8687                 subsegT subseg;
8688                 char *p;
8689
8690                 /* These only appear as the last operand in an
8691                    instruction, and every instruction that accepts
8692                    them in any variant accepts them in all variants.
8693                    This means we don't have to worry about backing out
8694                    any changes if the instruction does not match.
8695
8696                    The difference between them is the size of the
8697                    floating point constant and where it goes.  For 'F'
8698                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8699                    is 32 bits.  Where the constant is placed is based
8700                    on how the MIPS assembler does things:
8701                     F -- .rdata
8702                     L -- .lit8
8703                     f -- immediate value
8704                     l -- .lit4
8705
8706                     The .lit4 and .lit8 sections are only used if
8707                     permitted by the -G argument.
8708
8709                     When generating embedded PIC code, we use the
8710                     .lit8 section but not the .lit4 section (we can do
8711                     .lit4 inline easily; we need to put .lit8
8712                     somewhere in the data segment, and using .lit8
8713                     permits the linker to eventually combine identical
8714                     .lit8 entries).
8715
8716                     The code below needs to know whether the target register
8717                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8718                     'F' are used with GPR-based instructions and 'l' and
8719                     'L' are used with FPR-based instructions.  */
8720
8721                 f64 = *args == 'F' || *args == 'L';
8722                 using_gprs = *args == 'F' || *args == 'f';
8723
8724                 save_in = input_line_pointer;
8725                 input_line_pointer = s;
8726                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8727                 length = len;
8728                 s = input_line_pointer;
8729                 input_line_pointer = save_in;
8730                 if (err != NULL && *err != '\0')
8731                   {
8732                     as_bad (_("Bad floating point constant: %s"), err);
8733                     memset (temp, '\0', sizeof temp);
8734                     length = f64 ? 8 : 4;
8735                   }
8736
8737                 assert (length == (unsigned) (f64 ? 8 : 4));
8738
8739                 if (*args == 'f'
8740                     || (*args == 'l'
8741                         && (! USE_GLOBAL_POINTER_OPT
8742                             || mips_pic == EMBEDDED_PIC
8743                             || g_switch_value < 4
8744                             || (temp[0] == 0 && temp[1] == 0)
8745                             || (temp[2] == 0 && temp[3] == 0))))
8746                   {
8747                     imm_expr.X_op = O_constant;
8748                     if (! target_big_endian)
8749                       imm_expr.X_add_number = bfd_getl32 (temp);
8750                     else
8751                       imm_expr.X_add_number = bfd_getb32 (temp);
8752                   }
8753                 else if (length > 4
8754                          && ! mips_disable_float_construction
8755                          /* Constants can only be constructed in GPRs and
8756                             copied to FPRs if the GPRs are at least as wide
8757                             as the FPRs.  Force the constant into memory if
8758                             we are using 64-bit FPRs but the GPRs are only
8759                             32 bits wide.  */
8760                          && (using_gprs
8761                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8762                          && ((temp[0] == 0 && temp[1] == 0)
8763                              || (temp[2] == 0 && temp[3] == 0))
8764                          && ((temp[4] == 0 && temp[5] == 0)
8765                              || (temp[6] == 0 && temp[7] == 0)))
8766                   {
8767                     /* The value is simple enough to load with a couple of
8768                        instructions.  If using 32-bit registers, set
8769                        imm_expr to the high order 32 bits and offset_expr to
8770                        the low order 32 bits.  Otherwise, set imm_expr to
8771                        the entire 64 bit constant.  */
8772                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8773                       {
8774                         imm_expr.X_op = O_constant;
8775                         offset_expr.X_op = O_constant;
8776                         if (! target_big_endian)
8777                           {
8778                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8779                             offset_expr.X_add_number = bfd_getl32 (temp);
8780                           }
8781                         else
8782                           {
8783                             imm_expr.X_add_number = bfd_getb32 (temp);
8784                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8785                           }
8786                         if (offset_expr.X_add_number == 0)
8787                           offset_expr.X_op = O_absent;
8788                       }
8789                     else if (sizeof (imm_expr.X_add_number) > 4)
8790                       {
8791                         imm_expr.X_op = O_constant;
8792                         if (! target_big_endian)
8793                           imm_expr.X_add_number = bfd_getl64 (temp);
8794                         else
8795                           imm_expr.X_add_number = bfd_getb64 (temp);
8796                       }
8797                     else
8798                       {
8799                         imm_expr.X_op = O_big;
8800                         imm_expr.X_add_number = 4;
8801                         if (! target_big_endian)
8802                           {
8803                             generic_bignum[0] = bfd_getl16 (temp);
8804                             generic_bignum[1] = bfd_getl16 (temp + 2);
8805                             generic_bignum[2] = bfd_getl16 (temp + 4);
8806                             generic_bignum[3] = bfd_getl16 (temp + 6);
8807                           }
8808                         else
8809                           {
8810                             generic_bignum[0] = bfd_getb16 (temp + 6);
8811                             generic_bignum[1] = bfd_getb16 (temp + 4);
8812                             generic_bignum[2] = bfd_getb16 (temp + 2);
8813                             generic_bignum[3] = bfd_getb16 (temp);
8814                           }
8815                       }
8816                   }
8817                 else
8818                   {
8819                     const char *newname;
8820                     segT new_seg;
8821
8822                     /* Switch to the right section.  */
8823                     seg = now_seg;
8824                     subseg = now_subseg;
8825                     switch (*args)
8826                       {
8827                       default: /* unused default case avoids warnings.  */
8828                       case 'L':
8829                         newname = RDATA_SECTION_NAME;
8830                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8831                             || mips_pic == EMBEDDED_PIC)
8832                           newname = ".lit8";
8833                         break;
8834                       case 'F':
8835                         if (mips_pic == EMBEDDED_PIC)
8836                           newname = ".lit8";
8837                         else
8838                           newname = RDATA_SECTION_NAME;
8839                         break;
8840                       case 'l':
8841                         assert (!USE_GLOBAL_POINTER_OPT
8842                                 || g_switch_value >= 4);
8843                         newname = ".lit4";
8844                         break;
8845                       }
8846                     new_seg = subseg_new (newname, (subsegT) 0);
8847                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8848                       bfd_set_section_flags (stdoutput, new_seg,
8849                                              (SEC_ALLOC
8850                                               | SEC_LOAD
8851                                               | SEC_READONLY
8852                                               | SEC_DATA));
8853                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
8854                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8855                         && strcmp (TARGET_OS, "elf") != 0)
8856                       record_alignment (new_seg, 4);
8857                     else
8858                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
8859                     if (seg == now_seg)
8860                       as_bad (_("Can't use floating point insn in this section"));
8861
8862                     /* Set the argument to the current address in the
8863                        section.  */
8864                     offset_expr.X_op = O_symbol;
8865                     offset_expr.X_add_symbol =
8866                       symbol_new ("L0\001", now_seg,
8867                                   (valueT) frag_now_fix (), frag_now);
8868                     offset_expr.X_add_number = 0;
8869
8870                     /* Put the floating point number into the section.  */
8871                     p = frag_more ((int) length);
8872                     memcpy (p, temp, length);
8873
8874                     /* Switch back to the original section.  */
8875                     subseg_set (seg, subseg);
8876                   }
8877               }
8878               continue;
8879
8880             case 'i':           /* 16 bit unsigned immediate */
8881             case 'j':           /* 16 bit signed immediate */
8882               *imm_reloc = BFD_RELOC_LO16;
8883               c = my_getSmallExpression (&imm_expr, s);
8884               if (c != S_EX_NONE)
8885                 {
8886                   if (c != S_EX_LO)
8887                     {
8888                       if (c == S_EX_HI)
8889                         {
8890                           *imm_reloc = BFD_RELOC_HI16_S;
8891                           imm_unmatched_hi = TRUE;
8892                         }
8893 #ifdef OBJ_ELF
8894                       else if (c == S_EX_HIGHEST)
8895                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8896                       else if (c == S_EX_HIGHER)
8897                         *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8898                       else if (c == S_EX_GP_REL)
8899                         {
8900                           /* This occurs in NewABI only.  */
8901                           c = my_getSmallExpression (&imm_expr, s);
8902                           if (c != S_EX_NEG)
8903                             as_bad (_("bad composition of relocations"));
8904                           else
8905                             {
8906                               c = my_getSmallExpression (&imm_expr, s);
8907                               if (c != S_EX_LO)
8908                                 as_bad (_("bad composition of relocations"));
8909                               else
8910                                 {
8911                                   imm_reloc[0] = BFD_RELOC_GPREL16;
8912                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8913                                   imm_reloc[2] = BFD_RELOC_LO16;
8914                                 }
8915                             }
8916                         }
8917 #endif
8918                       else
8919                         *imm_reloc = BFD_RELOC_HI16;
8920                     }
8921                   else if (imm_expr.X_op == O_constant)
8922                     imm_expr.X_add_number &= 0xffff;
8923                 }
8924               if (*args == 'i')
8925                 {
8926                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8927                       || ((imm_expr.X_add_number < 0
8928                            || imm_expr.X_add_number >= 0x10000)
8929                           && imm_expr.X_op == O_constant))
8930                     {
8931                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8932                           !strcmp (insn->name, insn[1].name))
8933                         break;
8934                       if (imm_expr.X_op == O_constant
8935                           || imm_expr.X_op == O_big)
8936                         as_bad (_("16 bit expression not in range 0..65535"));
8937                     }
8938                 }
8939               else
8940                 {
8941                   int more;
8942                   offsetT max;
8943
8944                   /* The upper bound should be 0x8000, but
8945                      unfortunately the MIPS assembler accepts numbers
8946                      from 0x8000 to 0xffff and sign extends them, and
8947                      we want to be compatible.  We only permit this
8948                      extended range for an instruction which does not
8949                      provide any further alternates, since those
8950                      alternates may handle other cases.  People should
8951                      use the numbers they mean, rather than relying on
8952                      a mysterious sign extension.  */
8953                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8954                           strcmp (insn->name, insn[1].name) == 0);
8955                   if (more)
8956                     max = 0x8000;
8957                   else
8958                     max = 0x10000;
8959                   if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8960                       || ((imm_expr.X_add_number < -0x8000
8961                            || imm_expr.X_add_number >= max)
8962                           && imm_expr.X_op == O_constant)
8963                       || (more
8964                           && imm_expr.X_add_number < 0
8965                           && HAVE_64BIT_GPRS
8966                           && imm_expr.X_unsigned
8967                           && sizeof (imm_expr.X_add_number) <= 4))
8968                     {
8969                       if (more)
8970                         break;
8971                       if (imm_expr.X_op == O_constant
8972                           || imm_expr.X_op == O_big)
8973                         as_bad (_("16 bit expression not in range -32768..32767"));
8974                     }
8975                 }
8976               s = expr_end;
8977               continue;
8978
8979             case 'o':           /* 16 bit offset */
8980               c = my_getSmallExpression (&offset_expr, s);
8981
8982               /* If this value won't fit into a 16 bit offset, then go
8983                  find a macro that will generate the 32 bit offset
8984                  code pattern.  */
8985               if (c == S_EX_NONE
8986                   && (offset_expr.X_op != O_constant
8987                       || offset_expr.X_add_number >= 0x8000
8988                       || offset_expr.X_add_number < -0x8000))
8989                 break;
8990
8991               if (c == S_EX_HI)
8992                 {
8993                   if (offset_expr.X_op != O_constant)
8994                     break;
8995                   offset_expr.X_add_number =
8996                     (offset_expr.X_add_number >> 16) & 0xffff;
8997                 }
8998               *offset_reloc = BFD_RELOC_LO16;
8999               s = expr_end;
9000               continue;
9001
9002             case 'p':           /* pc relative offset */
9003               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9004               my_getExpression (&offset_expr, s);
9005               s = expr_end;
9006               continue;
9007
9008             case 'u':           /* upper 16 bits */
9009               c = my_getSmallExpression (&imm_expr, s);
9010               *imm_reloc = BFD_RELOC_LO16;
9011               if (c != S_EX_NONE)
9012                 {
9013                   if (c != S_EX_LO)
9014                     {
9015                       if (c == S_EX_HI)
9016                         {
9017                           *imm_reloc = BFD_RELOC_HI16_S;
9018                           imm_unmatched_hi = TRUE;
9019                         }
9020 #ifdef OBJ_ELF
9021                       else if (c == S_EX_HIGHEST)
9022                         *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
9023                       else if (c == S_EX_GP_REL)
9024                         {
9025                           /* This occurs in NewABI only.  */
9026                           c = my_getSmallExpression (&imm_expr, s);
9027                           if (c != S_EX_NEG)
9028                             as_bad (_("bad composition of relocations"));
9029                           else
9030                             {
9031                               c = my_getSmallExpression (&imm_expr, s);
9032                               if (c != S_EX_HI)
9033                                 as_bad (_("bad composition of relocations"));
9034                               else
9035                                 {
9036                                   imm_reloc[0] = BFD_RELOC_GPREL16;
9037                                   imm_reloc[1] = BFD_RELOC_MIPS_SUB;
9038                                   imm_reloc[2] = BFD_RELOC_HI16_S;
9039                                 }
9040                             }
9041                         }
9042 #endif
9043                       else
9044                         *imm_reloc = BFD_RELOC_HI16;
9045                     }
9046                   else if (imm_expr.X_op == O_constant)
9047                     imm_expr.X_add_number &= 0xffff;
9048                 }
9049               else if (imm_expr.X_op == O_constant
9050                        && (imm_expr.X_add_number < 0
9051                            || imm_expr.X_add_number >= 0x10000))
9052                 as_bad (_("lui expression not in range 0..65535"));
9053               s = expr_end;
9054               continue;
9055
9056             case 'a':           /* 26 bit address */
9057               my_getExpression (&offset_expr, s);
9058               s = expr_end;
9059               *offset_reloc = BFD_RELOC_MIPS_JMP;
9060               continue;
9061
9062             case 'N':           /* 3 bit branch condition code */
9063             case 'M':           /* 3 bit compare condition code */
9064               if (strncmp (s, "$fcc", 4) != 0)
9065                 break;
9066               s += 4;
9067               regno = 0;
9068               do
9069                 {
9070                   regno *= 10;
9071                   regno += *s - '0';
9072                   ++s;
9073                 }
9074               while (ISDIGIT (*s));
9075               if (regno > 7)
9076                 as_bad (_("invalid condition code register $fcc%d"), regno);
9077               if (*args == 'N')
9078                 ip->insn_opcode |= regno << OP_SH_BCC;
9079               else
9080                 ip->insn_opcode |= regno << OP_SH_CCC;
9081               continue;
9082
9083             case 'H':
9084               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9085                 s += 2;
9086               if (ISDIGIT (*s))
9087                 {
9088                   c = 0;
9089                   do
9090                     {
9091                       c *= 10;
9092                       c += *s - '0';
9093                       ++s;
9094                     }
9095                   while (ISDIGIT (*s));
9096                 }
9097               else
9098                 c = 8; /* Invalid sel value.  */
9099
9100               if (c > 7)
9101                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9102               ip->insn_opcode |= c;
9103               continue;
9104
9105             case 'e':
9106               /* Must be at least one digit.  */
9107               my_getExpression (&imm_expr, s);
9108               check_absolute_expr (ip, &imm_expr);
9109
9110               if ((unsigned long) imm_expr.X_add_number
9111                   > (unsigned long) OP_MASK_VECBYTE)
9112                 {
9113                   as_bad (_("bad byte vector index (%ld)"),
9114                            (long) imm_expr.X_add_number);
9115                   imm_expr.X_add_number = 0;
9116                 }
9117
9118               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9119               imm_expr.X_op = O_absent;
9120               s = expr_end;
9121               continue;
9122
9123             case '%':
9124               my_getExpression (&imm_expr, s);
9125               check_absolute_expr (ip, &imm_expr);
9126
9127               if ((unsigned long) imm_expr.X_add_number
9128                   > (unsigned long) OP_MASK_VECALIGN)
9129                 {
9130                   as_bad (_("bad byte vector index (%ld)"),
9131                            (long) imm_expr.X_add_number);
9132                   imm_expr.X_add_number = 0;
9133                 }
9134
9135               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9136               imm_expr.X_op = O_absent;
9137               s = expr_end;
9138               continue;
9139
9140             default:
9141               as_bad (_("bad char = '%c'\n"), *args);
9142               internalError ();
9143             }
9144           break;
9145         }
9146       /* Args don't match.  */
9147       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9148           !strcmp (insn->name, insn[1].name))
9149         {
9150           ++insn;
9151           s = argsStart;
9152           insn_error = _("illegal operands");
9153           continue;
9154         }
9155       if (save_c)
9156         *(--s) = save_c;
9157       insn_error = _("illegal operands");
9158       return;
9159     }
9160 }
9161
9162 /* This routine assembles an instruction into its binary format when
9163    assembling for the mips16.  As a side effect, it sets one of the
9164    global variables imm_reloc or offset_reloc to the type of
9165    relocation to do if one of the operands is an address expression.
9166    It also sets mips16_small and mips16_ext if the user explicitly
9167    requested a small or extended instruction.  */
9168
9169 static void
9170 mips16_ip (str, ip)
9171      char *str;
9172      struct mips_cl_insn *ip;
9173 {
9174   char *s;
9175   const char *args;
9176   struct mips_opcode *insn;
9177   char *argsstart;
9178   unsigned int regno;
9179   unsigned int lastregno = 0;
9180   char *s_reset;
9181
9182   insn_error = NULL;
9183
9184   mips16_small = FALSE;
9185   mips16_ext = FALSE;
9186
9187   for (s = str; ISLOWER (*s); ++s)
9188     ;
9189   switch (*s)
9190     {
9191     case '\0':
9192       break;
9193
9194     case ' ':
9195       *s++ = '\0';
9196       break;
9197
9198     case '.':
9199       if (s[1] == 't' && s[2] == ' ')
9200         {
9201           *s = '\0';
9202           mips16_small = TRUE;
9203           s += 3;
9204           break;
9205         }
9206       else if (s[1] == 'e' && s[2] == ' ')
9207         {
9208           *s = '\0';
9209           mips16_ext = TRUE;
9210           s += 3;
9211           break;
9212         }
9213       /* Fall through.  */
9214     default:
9215       insn_error = _("unknown opcode");
9216       return;
9217     }
9218
9219   if (mips_opts.noautoextend && ! mips16_ext)
9220     mips16_small = TRUE;
9221
9222   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9223     {
9224       insn_error = _("unrecognized opcode");
9225       return;
9226     }
9227
9228   argsstart = s;
9229   for (;;)
9230     {
9231       assert (strcmp (insn->name, str) == 0);
9232
9233       ip->insn_mo = insn;
9234       ip->insn_opcode = insn->match;
9235       ip->use_extend = FALSE;
9236       imm_expr.X_op = O_absent;
9237       imm_reloc[0] = BFD_RELOC_UNUSED;
9238       imm_reloc[1] = BFD_RELOC_UNUSED;
9239       imm_reloc[2] = BFD_RELOC_UNUSED;
9240       offset_expr.X_op = O_absent;
9241       offset_reloc[0] = BFD_RELOC_UNUSED;
9242       offset_reloc[1] = BFD_RELOC_UNUSED;
9243       offset_reloc[2] = BFD_RELOC_UNUSED;
9244       for (args = insn->args; 1; ++args)
9245         {
9246           int c;
9247
9248           if (*s == ' ')
9249             ++s;
9250
9251           /* In this switch statement we call break if we did not find
9252              a match, continue if we did find a match, or return if we
9253              are done.  */
9254
9255           c = *args;
9256           switch (c)
9257             {
9258             case '\0':
9259               if (*s == '\0')
9260                 {
9261                   /* Stuff the immediate value in now, if we can.  */
9262                   if (imm_expr.X_op == O_constant
9263                       && *imm_reloc > BFD_RELOC_UNUSED
9264                       && insn->pinfo != INSN_MACRO)
9265                     {
9266                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9267                                     imm_expr.X_add_number, TRUE, mips16_small,
9268                                     mips16_ext, &ip->insn_opcode,
9269                                     &ip->use_extend, &ip->extend);
9270                       imm_expr.X_op = O_absent;
9271                       *imm_reloc = BFD_RELOC_UNUSED;
9272                     }
9273
9274                   return;
9275                 }
9276               break;
9277
9278             case ',':
9279               if (*s++ == c)
9280                 continue;
9281               s--;
9282               switch (*++args)
9283                 {
9284                 case 'v':
9285                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9286                   continue;
9287                 case 'w':
9288                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9289                   continue;
9290                 }
9291               break;
9292
9293             case '(':
9294             case ')':
9295               if (*s++ == c)
9296                 continue;
9297               break;
9298
9299             case 'v':
9300             case 'w':
9301               if (s[0] != '$')
9302                 {
9303                   if (c == 'v')
9304                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9305                   else
9306                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9307                   ++args;
9308                   continue;
9309                 }
9310               /* Fall through.  */
9311             case 'x':
9312             case 'y':
9313             case 'z':
9314             case 'Z':
9315             case '0':
9316             case 'S':
9317             case 'R':
9318             case 'X':
9319             case 'Y':
9320               if (s[0] != '$')
9321                 break;
9322               s_reset = s;
9323               if (ISDIGIT (s[1]))
9324                 {
9325                   ++s;
9326                   regno = 0;
9327                   do
9328                     {
9329                       regno *= 10;
9330                       regno += *s - '0';
9331                       ++s;
9332                     }
9333                   while (ISDIGIT (*s));
9334                   if (regno > 31)
9335                     {
9336                       as_bad (_("invalid register number (%d)"), regno);
9337                       regno = 2;
9338                     }
9339                 }
9340               else
9341                 {
9342                   if (s[1] == 'r' && s[2] == 'a')
9343                     {
9344                       s += 3;
9345                       regno = RA;
9346                     }
9347                   else if (s[1] == 'f' && s[2] == 'p')
9348                     {
9349                       s += 3;
9350                       regno = FP;
9351                     }
9352                   else if (s[1] == 's' && s[2] == 'p')
9353                     {
9354                       s += 3;
9355                       regno = SP;
9356                     }
9357                   else if (s[1] == 'g' && s[2] == 'p')
9358                     {
9359                       s += 3;
9360                       regno = GP;
9361                     }
9362                   else if (s[1] == 'a' && s[2] == 't')
9363                     {
9364                       s += 3;
9365                       regno = AT;
9366                     }
9367                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9368                     {
9369                       s += 4;
9370                       regno = KT0;
9371                     }
9372                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9373                     {
9374                       s += 4;
9375                       regno = KT1;
9376                     }
9377                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9378                     {
9379                       s += 5;
9380                       regno = ZERO;
9381                     }
9382                   else
9383                     break;
9384                 }
9385
9386               if (*s == ' ')
9387                 ++s;
9388               if (args[1] != *s)
9389                 {
9390                   if (c == 'v' || c == 'w')
9391                     {
9392                       regno = mips16_to_32_reg_map[lastregno];
9393                       s = s_reset;
9394                       ++args;
9395                     }
9396                 }
9397
9398               switch (c)
9399                 {
9400                 case 'x':
9401                 case 'y':
9402                 case 'z':
9403                 case 'v':
9404                 case 'w':
9405                 case 'Z':
9406                   regno = mips32_to_16_reg_map[regno];
9407                   break;
9408
9409                 case '0':
9410                   if (regno != 0)
9411                     regno = ILLEGAL_REG;
9412                   break;
9413
9414                 case 'S':
9415                   if (regno != SP)
9416                     regno = ILLEGAL_REG;
9417                   break;
9418
9419                 case 'R':
9420                   if (regno != RA)
9421                     regno = ILLEGAL_REG;
9422                   break;
9423
9424                 case 'X':
9425                 case 'Y':
9426                   if (regno == AT && ! mips_opts.noat)
9427                     as_warn (_("used $at without \".set noat\""));
9428                   break;
9429
9430                 default:
9431                   internalError ();
9432                 }
9433
9434               if (regno == ILLEGAL_REG)
9435                 break;
9436
9437               switch (c)
9438                 {
9439                 case 'x':
9440                 case 'v':
9441                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9442                   break;
9443                 case 'y':
9444                 case 'w':
9445                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9446                   break;
9447                 case 'z':
9448                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9449                   break;
9450                 case 'Z':
9451                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9452                 case '0':
9453                 case 'S':
9454                 case 'R':
9455                   break;
9456                 case 'X':
9457                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9458                   break;
9459                 case 'Y':
9460                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9461                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9462                   break;
9463                 default:
9464                   internalError ();
9465                 }
9466
9467               lastregno = regno;
9468               continue;
9469
9470             case 'P':
9471               if (strncmp (s, "$pc", 3) == 0)
9472                 {
9473                   s += 3;
9474                   continue;
9475                 }
9476               break;
9477
9478             case '<':
9479             case '>':
9480             case '[':
9481             case ']':
9482             case '4':
9483             case '5':
9484             case 'H':
9485             case 'W':
9486             case 'D':
9487             case 'j':
9488             case '8':
9489             case 'V':
9490             case 'C':
9491             case 'U':
9492             case 'k':
9493             case 'K':
9494               if (s[0] == '%'
9495                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9496                 {
9497                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9498                      and generate the appropriate reloc.  If the text
9499                      inside %gprel is not a symbol name with an
9500                      optional offset, then we generate a normal reloc
9501                      and will probably fail later.  */
9502                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9503                   if (imm_expr.X_op == O_symbol)
9504                     {
9505                       mips16_ext = TRUE;
9506                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9507                       s = expr_end;
9508                       ip->use_extend = TRUE;
9509                       ip->extend = 0;
9510                       continue;
9511                     }
9512                 }
9513               else
9514                 {
9515                   /* Just pick up a normal expression.  */
9516                   my_getExpression (&imm_expr, s);
9517                 }
9518
9519               if (imm_expr.X_op == O_register)
9520                 {
9521                   /* What we thought was an expression turned out to
9522                      be a register.  */
9523
9524                   if (s[0] == '(' && args[1] == '(')
9525                     {
9526                       /* It looks like the expression was omitted
9527                          before a register indirection, which means
9528                          that the expression is implicitly zero.  We
9529                          still set up imm_expr, so that we handle
9530                          explicit extensions correctly.  */
9531                       imm_expr.X_op = O_constant;
9532                       imm_expr.X_add_number = 0;
9533                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9534                       continue;
9535                     }
9536
9537                   break;
9538                 }
9539
9540               /* We need to relax this instruction.  */
9541               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9542               s = expr_end;
9543               continue;
9544
9545             case 'p':
9546             case 'q':
9547             case 'A':
9548             case 'B':
9549             case 'E':
9550               /* We use offset_reloc rather than imm_reloc for the PC
9551                  relative operands.  This lets macros with both
9552                  immediate and address operands work correctly.  */
9553               my_getExpression (&offset_expr, s);
9554
9555               if (offset_expr.X_op == O_register)
9556                 break;
9557
9558               /* We need to relax this instruction.  */
9559               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9560               s = expr_end;
9561               continue;
9562
9563             case '6':           /* break code */
9564               my_getExpression (&imm_expr, s);
9565               check_absolute_expr (ip, &imm_expr);
9566               if ((unsigned long) imm_expr.X_add_number > 63)
9567                 {
9568                   as_warn (_("Invalid value for `%s' (%lu)"),
9569                            ip->insn_mo->name,
9570                            (unsigned long) imm_expr.X_add_number);
9571                   imm_expr.X_add_number &= 0x3f;
9572                 }
9573               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9574               imm_expr.X_op = O_absent;
9575               s = expr_end;
9576               continue;
9577
9578             case 'a':           /* 26 bit address */
9579               my_getExpression (&offset_expr, s);
9580               s = expr_end;
9581               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9582               ip->insn_opcode <<= 16;
9583               continue;
9584
9585             case 'l':           /* register list for entry macro */
9586             case 'L':           /* register list for exit macro */
9587               {
9588                 int mask;
9589
9590                 if (c == 'l')
9591                   mask = 0;
9592                 else
9593                   mask = 7 << 3;
9594                 while (*s != '\0')
9595                   {
9596                     int freg, reg1, reg2;
9597
9598                     while (*s == ' ' || *s == ',')
9599                       ++s;
9600                     if (*s != '$')
9601                       {
9602                         as_bad (_("can't parse register list"));
9603                         break;
9604                       }
9605                     ++s;
9606                     if (*s != 'f')
9607                       freg = 0;
9608                     else
9609                       {
9610                         freg = 1;
9611                         ++s;
9612                       }
9613                     reg1 = 0;
9614                     while (ISDIGIT (*s))
9615                       {
9616                         reg1 *= 10;
9617                         reg1 += *s - '0';
9618                         ++s;
9619                       }
9620                     if (*s == ' ')
9621                       ++s;
9622                     if (*s != '-')
9623                       reg2 = reg1;
9624                     else
9625                       {
9626                         ++s;
9627                         if (*s != '$')
9628                           break;
9629                         ++s;
9630                         if (freg)
9631                           {
9632                             if (*s == 'f')
9633                               ++s;
9634                             else
9635                               {
9636                                 as_bad (_("invalid register list"));
9637                                 break;
9638                               }
9639                           }
9640                         reg2 = 0;
9641                         while (ISDIGIT (*s))
9642                           {
9643                             reg2 *= 10;
9644                             reg2 += *s - '0';
9645                             ++s;
9646                           }
9647                       }
9648                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9649                       {
9650                         mask &= ~ (7 << 3);
9651                         mask |= 5 << 3;
9652                       }
9653                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9654                       {
9655                         mask &= ~ (7 << 3);
9656                         mask |= 6 << 3;
9657                       }
9658                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9659                       mask |= (reg2 - 3) << 3;
9660                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9661                       mask |= (reg2 - 15) << 1;
9662                     else if (reg1 == RA && reg2 == RA)
9663                       mask |= 1;
9664                     else
9665                       {
9666                         as_bad (_("invalid register list"));
9667                         break;
9668                       }
9669                   }
9670                 /* The mask is filled in in the opcode table for the
9671                    benefit of the disassembler.  We remove it before
9672                    applying the actual mask.  */
9673                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9674                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9675               }
9676             continue;
9677
9678             case 'e':           /* extend code */
9679               my_getExpression (&imm_expr, s);
9680               check_absolute_expr (ip, &imm_expr);
9681               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9682                 {
9683                   as_warn (_("Invalid value for `%s' (%lu)"),
9684                            ip->insn_mo->name,
9685                            (unsigned long) imm_expr.X_add_number);
9686                   imm_expr.X_add_number &= 0x7ff;
9687                 }
9688               ip->insn_opcode |= imm_expr.X_add_number;
9689               imm_expr.X_op = O_absent;
9690               s = expr_end;
9691               continue;
9692
9693             default:
9694               internalError ();
9695             }
9696           break;
9697         }
9698
9699       /* Args don't match.  */
9700       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9701           strcmp (insn->name, insn[1].name) == 0)
9702         {
9703           ++insn;
9704           s = argsstart;
9705           continue;
9706         }
9707
9708       insn_error = _("illegal operands");
9709
9710       return;
9711     }
9712 }
9713
9714 /* This structure holds information we know about a mips16 immediate
9715    argument type.  */
9716
9717 struct mips16_immed_operand
9718 {
9719   /* The type code used in the argument string in the opcode table.  */
9720   int type;
9721   /* The number of bits in the short form of the opcode.  */
9722   int nbits;
9723   /* The number of bits in the extended form of the opcode.  */
9724   int extbits;
9725   /* The amount by which the short form is shifted when it is used;
9726      for example, the sw instruction has a shift count of 2.  */
9727   int shift;
9728   /* The amount by which the short form is shifted when it is stored
9729      into the instruction code.  */
9730   int op_shift;
9731   /* Non-zero if the short form is unsigned.  */
9732   int unsp;
9733   /* Non-zero if the extended form is unsigned.  */
9734   int extu;
9735   /* Non-zero if the value is PC relative.  */
9736   int pcrel;
9737 };
9738
9739 /* The mips16 immediate operand types.  */
9740
9741 static const struct mips16_immed_operand mips16_immed_operands[] =
9742 {
9743   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9744   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9745   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9746   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9747   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9748   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9749   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9750   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9751   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9752   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9753   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9754   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9755   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9756   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9757   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9758   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9759   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9760   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9761   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9762   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9763   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9764 };
9765
9766 #define MIPS16_NUM_IMMED \
9767   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9768
9769 /* Handle a mips16 instruction with an immediate value.  This or's the
9770    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9771    whether an extended value is needed; if one is needed, it sets
9772    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9773    If SMALL is true, an unextended opcode was explicitly requested.
9774    If EXT is true, an extended opcode was explicitly requested.  If
9775    WARN is true, warn if EXT does not match reality.  */
9776
9777 static void
9778 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9779               extend)
9780      char *file;
9781      unsigned int line;
9782      int type;
9783      offsetT val;
9784      bfd_boolean warn;
9785      bfd_boolean small;
9786      bfd_boolean ext;
9787      unsigned long *insn;
9788      bfd_boolean *use_extend;
9789      unsigned short *extend;
9790 {
9791   register const struct mips16_immed_operand *op;
9792   int mintiny, maxtiny;
9793   bfd_boolean needext;
9794
9795   op = mips16_immed_operands;
9796   while (op->type != type)
9797     {
9798       ++op;
9799       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9800     }
9801
9802   if (op->unsp)
9803     {
9804       if (type == '<' || type == '>' || type == '[' || type == ']')
9805         {
9806           mintiny = 1;
9807           maxtiny = 1 << op->nbits;
9808         }
9809       else
9810         {
9811           mintiny = 0;
9812           maxtiny = (1 << op->nbits) - 1;
9813         }
9814     }
9815   else
9816     {
9817       mintiny = - (1 << (op->nbits - 1));
9818       maxtiny = (1 << (op->nbits - 1)) - 1;
9819     }
9820
9821   /* Branch offsets have an implicit 0 in the lowest bit.  */
9822   if (type == 'p' || type == 'q')
9823     val /= 2;
9824
9825   if ((val & ((1 << op->shift) - 1)) != 0
9826       || val < (mintiny << op->shift)
9827       || val > (maxtiny << op->shift))
9828     needext = TRUE;
9829   else
9830     needext = FALSE;
9831
9832   if (warn && ext && ! needext)
9833     as_warn_where (file, line,
9834                    _("extended operand requested but not required"));
9835   if (small && needext)
9836     as_bad_where (file, line, _("invalid unextended operand value"));
9837
9838   if (small || (! ext && ! needext))
9839     {
9840       int insnval;
9841
9842       *use_extend = FALSE;
9843       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9844       insnval <<= op->op_shift;
9845       *insn |= insnval;
9846     }
9847   else
9848     {
9849       long minext, maxext;
9850       int extval;
9851
9852       if (op->extu)
9853         {
9854           minext = 0;
9855           maxext = (1 << op->extbits) - 1;
9856         }
9857       else
9858         {
9859           minext = - (1 << (op->extbits - 1));
9860           maxext = (1 << (op->extbits - 1)) - 1;
9861         }
9862       if (val < minext || val > maxext)
9863         as_bad_where (file, line,
9864                       _("operand value out of range for instruction"));
9865
9866       *use_extend = TRUE;
9867       if (op->extbits == 16)
9868         {
9869           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9870           val &= 0x1f;
9871         }
9872       else if (op->extbits == 15)
9873         {
9874           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9875           val &= 0xf;
9876         }
9877       else
9878         {
9879           extval = ((val & 0x1f) << 6) | (val & 0x20);
9880           val = 0;
9881         }
9882
9883       *extend = (unsigned short) extval;
9884       *insn |= val;
9885     }
9886 }
9887 \f
9888 static struct percent_op_match
9889 {
9890    const char *str;
9891    const enum small_ex_type type;
9892 } percent_op[] =
9893 {
9894   {"%lo", S_EX_LO},
9895 #ifdef OBJ_ELF
9896   {"%call_hi", S_EX_CALL_HI},
9897   {"%call_lo", S_EX_CALL_LO},
9898   {"%call16", S_EX_CALL16},
9899   {"%got_disp", S_EX_GOT_DISP},
9900   {"%got_page", S_EX_GOT_PAGE},
9901   {"%got_ofst", S_EX_GOT_OFST},
9902   {"%got_hi", S_EX_GOT_HI},
9903   {"%got_lo", S_EX_GOT_LO},
9904   {"%got", S_EX_GOT},
9905   {"%gp_rel", S_EX_GP_REL},
9906   {"%half", S_EX_HALF},
9907   {"%highest", S_EX_HIGHEST},
9908   {"%higher", S_EX_HIGHER},
9909   {"%neg", S_EX_NEG},
9910 #endif
9911   {"%hi", S_EX_HI}
9912 };
9913
9914 /* Parse small expression input.  STR gets adjusted to eat up whitespace.
9915    It detects valid "%percent_op(...)" and "($reg)" strings.  Percent_op's
9916    can be nested, this is handled by blanking the innermost, parsing the
9917    rest by subsequent calls.  */
9918
9919 static int
9920 my_getSmallParser (str, len, nestlevel)
9921      char **str;
9922      unsigned int *len;
9923      int *nestlevel;
9924 {
9925   *len = 0;
9926   *str += strspn (*str, " \t");
9927   /* Check for expression in parentheses.  */
9928   if (**str == '(')
9929     {
9930       char *b = *str + 1 + strspn (*str + 1, " \t");
9931       char *e;
9932
9933       /* Check for base register.  */
9934       if (b[0] == '$')
9935         {
9936           if (strchr (b, ')')
9937               && (e = b + strcspn (b, ") \t"))
9938               && e - b > 1 && e - b < 4)
9939             {
9940               if ((e - b == 3
9941                    && ((b[1] == 'f' && b[2] == 'p')
9942                        || (b[1] == 's' && b[2] == 'p')
9943                        || (b[1] == 'g' && b[2] == 'p')
9944                        || (b[1] == 'a' && b[2] == 't')
9945                        || (ISDIGIT (b[1])
9946                            && ISDIGIT (b[2]))))
9947                   || (ISDIGIT (b[1])))
9948                 {
9949                   *len = strcspn (*str, ")") + 1;
9950                   return S_EX_REGISTER;
9951                 }
9952             }
9953         }
9954       /* Check for percent_op (in parentheses).  */
9955       else if (b[0] == '%')
9956         {
9957           *str = b;
9958           return my_getPercentOp (str, len, nestlevel);
9959         }
9960
9961       /* Some other expression in the parentheses, which can contain
9962          parentheses itself. Attempt to find the matching one.  */
9963       {
9964         int pcnt = 1;
9965         char *s;
9966
9967         *len = 1;
9968         for (s = *str + 1; *s && pcnt; s++, (*len)++)
9969           {
9970             if (*s == '(')
9971               ++pcnt;
9972             else if (*s == ')')
9973               --pcnt;
9974           }
9975       }
9976     }
9977   /* Check for percent_op (outside of parentheses).  */
9978   else if (*str[0] == '%')
9979     return my_getPercentOp (str, len, nestlevel);
9980
9981   /* Any other expression.  */
9982   return S_EX_NONE;
9983 }
9984
9985 static int
9986 my_getPercentOp (str, len, nestlevel)
9987      char **str;
9988      unsigned int *len;
9989      int *nestlevel;
9990 {
9991   char *tmp = *str + 1;
9992   unsigned int i = 0;
9993
9994   while (ISALPHA (*tmp) || *tmp == '_')
9995     {
9996       *tmp = TOLOWER (*tmp);
9997       tmp++;
9998     }
9999   while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
10000     {
10001       if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
10002         i++;
10003       else
10004         {
10005           int type = percent_op[i].type;
10006
10007           /* Only %hi and %lo are allowed for OldABI.  */
10008           if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
10009             return S_EX_NONE;
10010
10011           *len = strlen (percent_op[i].str);
10012           ++(*nestlevel);
10013           return type;
10014         }
10015     }
10016   return S_EX_NONE;
10017 }
10018
10019 static int
10020 my_getSmallExpression (ep, str)
10021      expressionS *ep;
10022      char *str;
10023 {
10024   static char *oldstr = NULL;
10025   int c = S_EX_NONE;
10026   int oldc;
10027   int nestlevel = -1;
10028   unsigned int len;
10029
10030   /* Don't update oldstr if the last call had nested percent_op's. We need
10031      it to parse the outer ones later.  */
10032   if (! oldstr)
10033     oldstr = str;
10034
10035   do
10036     {
10037       oldc = c;
10038       c = my_getSmallParser (&str, &len, &nestlevel);
10039       if (c != S_EX_NONE && c != S_EX_REGISTER)
10040         str += len;
10041     }
10042   while (c != S_EX_NONE && c != S_EX_REGISTER);
10043
10044   if (nestlevel >= 0)
10045     {
10046       /* A percent_op was encountered.  Don't try to get an expression if
10047          it is already blanked out.  */
10048       if (*(str + strspn (str + 1, " )")) != ')')
10049         {
10050           char save;
10051
10052           /* Let my_getExpression() stop at the closing parenthesis.  */
10053           save = *(str + len);
10054           *(str + len) = '\0';
10055           my_getExpression (ep, str);
10056           *(str + len) = save;
10057         }
10058       if (nestlevel > 0)
10059         {
10060           /* Blank out including the % sign and the proper matching
10061              parenthesis.  */
10062           int pcnt = 1;
10063           char *s = strrchr (oldstr, '%');
10064           char *end;
10065
10066           for (end = strchr (s, '(') + 1; *end && pcnt; end++)
10067             {
10068               if (*end == '(')
10069                 ++pcnt;
10070               else if (*end == ')')
10071                 --pcnt;
10072             }
10073
10074           memset (s, ' ', end - s);
10075           str = oldstr;
10076         }
10077       else
10078         expr_end = str + len;
10079
10080       c = oldc;
10081     }
10082   else if (c == S_EX_NONE)
10083     {
10084       my_getExpression (ep, str);
10085     }
10086   else if (c == S_EX_REGISTER)
10087     {
10088       ep->X_op = O_constant;
10089       expr_end = str;
10090       ep->X_add_symbol = NULL;
10091       ep->X_op_symbol = NULL;
10092       ep->X_add_number = 0;
10093     }
10094   else
10095     {
10096       as_fatal (_("internal error"));
10097     }
10098
10099   if (nestlevel <= 0)
10100     /* All percent_op's have been handled.  */
10101     oldstr = NULL;
10102
10103   return c;
10104 }
10105
10106 static void
10107 my_getExpression (ep, str)
10108      expressionS *ep;
10109      char *str;
10110 {
10111   char *save_in;
10112   valueT val;
10113
10114   save_in = input_line_pointer;
10115   input_line_pointer = str;
10116   expression (ep);
10117   expr_end = input_line_pointer;
10118   input_line_pointer = save_in;
10119
10120   /* If we are in mips16 mode, and this is an expression based on `.',
10121      then we bump the value of the symbol by 1 since that is how other
10122      text symbols are handled.  We don't bother to handle complex
10123      expressions, just `.' plus or minus a constant.  */
10124   if (mips_opts.mips16
10125       && ep->X_op == O_symbol
10126       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10127       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10128       && symbol_get_frag (ep->X_add_symbol) == frag_now
10129       && symbol_constant_p (ep->X_add_symbol)
10130       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10131     S_SET_VALUE (ep->X_add_symbol, val + 1);
10132 }
10133
10134 /* Turn a string in input_line_pointer into a floating point constant
10135    of type TYPE, and store the appropriate bytes in *LITP.  The number
10136    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10137    returned, or NULL on OK.  */
10138
10139 char *
10140 md_atof (type, litP, sizeP)
10141      int type;
10142      char *litP;
10143      int *sizeP;
10144 {
10145   int prec;
10146   LITTLENUM_TYPE words[4];
10147   char *t;
10148   int i;
10149
10150   switch (type)
10151     {
10152     case 'f':
10153       prec = 2;
10154       break;
10155
10156     case 'd':
10157       prec = 4;
10158       break;
10159
10160     default:
10161       *sizeP = 0;
10162       return _("bad call to md_atof");
10163     }
10164
10165   t = atof_ieee (input_line_pointer, type, words);
10166   if (t)
10167     input_line_pointer = t;
10168
10169   *sizeP = prec * 2;
10170
10171   if (! target_big_endian)
10172     {
10173       for (i = prec - 1; i >= 0; i--)
10174         {
10175           md_number_to_chars (litP, (valueT) words[i], 2);
10176           litP += 2;
10177         }
10178     }
10179   else
10180     {
10181       for (i = 0; i < prec; i++)
10182         {
10183           md_number_to_chars (litP, (valueT) words[i], 2);
10184           litP += 2;
10185         }
10186     }
10187
10188   return NULL;
10189 }
10190
10191 void
10192 md_number_to_chars (buf, val, n)
10193      char *buf;
10194      valueT val;
10195      int n;
10196 {
10197   if (target_big_endian)
10198     number_to_chars_bigendian (buf, val, n);
10199   else
10200     number_to_chars_littleendian (buf, val, n);
10201 }
10202 \f
10203 #ifdef OBJ_ELF
10204 static int support_64bit_objects(void)
10205 {
10206   const char **list, **l;
10207   int yes;
10208
10209   list = bfd_target_list ();
10210   for (l = list; *l != NULL; l++)
10211 #ifdef TE_TMIPS
10212     /* This is traditional mips */
10213     if (strcmp (*l, "elf64-tradbigmips") == 0
10214         || strcmp (*l, "elf64-tradlittlemips") == 0)
10215 #else
10216     if (strcmp (*l, "elf64-bigmips") == 0
10217         || strcmp (*l, "elf64-littlemips") == 0)
10218 #endif
10219       break;
10220   yes = (*l != NULL);
10221   free (list);
10222   return yes;
10223 }
10224 #endif /* OBJ_ELF */
10225
10226 const char *md_shortopts = "nO::g::G:";
10227
10228 struct option md_longopts[] =
10229 {
10230 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10231   {"mips0", no_argument, NULL, OPTION_MIPS1},
10232   {"mips1", no_argument, NULL, OPTION_MIPS1},
10233 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10234   {"mips2", no_argument, NULL, OPTION_MIPS2},
10235 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10236   {"mips3", no_argument, NULL, OPTION_MIPS3},
10237 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10238   {"mips4", no_argument, NULL, OPTION_MIPS4},
10239 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10240   {"mips5", no_argument, NULL, OPTION_MIPS5},
10241 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10242   {"mips32", no_argument, NULL, OPTION_MIPS32},
10243 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10244   {"mips64", no_argument, NULL, OPTION_MIPS64},
10245 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10246   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10247 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10248   {"trap", no_argument, NULL, OPTION_TRAP},
10249   {"no-break", no_argument, NULL, OPTION_TRAP},
10250 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10251   {"break", no_argument, NULL, OPTION_BREAK},
10252   {"no-trap", no_argument, NULL, OPTION_BREAK},
10253 #define OPTION_EB (OPTION_MD_BASE + 11)
10254   {"EB", no_argument, NULL, OPTION_EB},
10255 #define OPTION_EL (OPTION_MD_BASE + 12)
10256   {"EL", no_argument, NULL, OPTION_EL},
10257 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10258   {"mips16", no_argument, NULL, OPTION_MIPS16},
10259 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10260   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10261 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10262   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10263 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10264   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10265   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10266 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10267   {"mfp32", no_argument, NULL, OPTION_FP32},
10268 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10269   {"mgp32", no_argument, NULL, OPTION_GP32},
10270 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10271   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10272 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10273   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10274 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10275   {"march", required_argument, NULL, OPTION_MARCH},
10276 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10277   {"mtune", required_argument, NULL, OPTION_MTUNE},
10278 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10279   {"mfp64", no_argument, NULL, OPTION_FP64},
10280 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10281   {"m4650", no_argument, NULL, OPTION_M4650},
10282 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10283   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10284 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10285   {"m4010", no_argument, NULL, OPTION_M4010},
10286 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10287   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10288 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10289   {"m4100", no_argument, NULL, OPTION_M4100},
10290 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10291   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10292 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10293   {"m3900", no_argument, NULL, OPTION_M3900},
10294 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10295   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10296 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10297   {"mgp64", no_argument, NULL, OPTION_GP64},
10298 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10299   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10300 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10301   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10302 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10303   {"mdmx", no_argument, NULL, OPTION_MDMX},
10304 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10305   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10306 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10307 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10308   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10309   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10310 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10311 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10312   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10313   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10314 #ifdef OBJ_ELF
10315 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 41)
10316 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10317   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10318   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10319 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10320   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10321 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10322   {"xgot",        no_argument, NULL, OPTION_XGOT},
10323 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10324   {"mabi", required_argument, NULL, OPTION_MABI},
10325 #define OPTION_32          (OPTION_ELF_BASE + 4)
10326   {"32",          no_argument, NULL, OPTION_32},
10327 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10328   {"n32",         no_argument, NULL, OPTION_N32},
10329 #define OPTION_64          (OPTION_ELF_BASE + 6)
10330   {"64",          no_argument, NULL, OPTION_64},
10331 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10332   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10333 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10334   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10335 #endif /* OBJ_ELF */
10336   {NULL, no_argument, NULL, 0}
10337 };
10338 size_t md_longopts_size = sizeof (md_longopts);
10339
10340 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10341    NEW_VALUE.  Warn if another value was already specified.  Note:
10342    we have to defer parsing the -march and -mtune arguments in order
10343    to handle 'from-abi' correctly, since the ABI might be specified
10344    in a later argument.  */
10345
10346 static void
10347 mips_set_option_string (string_ptr, new_value)
10348      const char **string_ptr, *new_value;
10349 {
10350   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10351     as_warn (_("A different %s was already specified, is now %s"),
10352              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10353              new_value);
10354
10355   *string_ptr = new_value;
10356 }
10357
10358 int
10359 md_parse_option (c, arg)
10360      int c;
10361      char *arg;
10362 {
10363   switch (c)
10364     {
10365     case OPTION_CONSTRUCT_FLOATS:
10366       mips_disable_float_construction = 0;
10367       break;
10368
10369     case OPTION_NO_CONSTRUCT_FLOATS:
10370       mips_disable_float_construction = 1;
10371       break;
10372
10373     case OPTION_TRAP:
10374       mips_trap = 1;
10375       break;
10376
10377     case OPTION_BREAK:
10378       mips_trap = 0;
10379       break;
10380
10381     case OPTION_EB:
10382       target_big_endian = 1;
10383       break;
10384
10385     case OPTION_EL:
10386       target_big_endian = 0;
10387       break;
10388
10389     case 'n':
10390       warn_nops = 1;
10391       break;
10392
10393     case 'O':
10394       if (arg && arg[1] == '0')
10395         mips_optimize = 1;
10396       else
10397         mips_optimize = 2;
10398       break;
10399
10400     case 'g':
10401       if (arg == NULL)
10402         mips_debug = 2;
10403       else
10404         mips_debug = atoi (arg);
10405       /* When the MIPS assembler sees -g or -g2, it does not do
10406          optimizations which limit full symbolic debugging.  We take
10407          that to be equivalent to -O0.  */
10408       if (mips_debug == 2)
10409         mips_optimize = 1;
10410       break;
10411
10412     case OPTION_MIPS1:
10413       file_mips_isa = ISA_MIPS1;
10414       break;
10415
10416     case OPTION_MIPS2:
10417       file_mips_isa = ISA_MIPS2;
10418       break;
10419
10420     case OPTION_MIPS3:
10421       file_mips_isa = ISA_MIPS3;
10422       break;
10423
10424     case OPTION_MIPS4:
10425       file_mips_isa = ISA_MIPS4;
10426       break;
10427
10428     case OPTION_MIPS5:
10429       file_mips_isa = ISA_MIPS5;
10430       break;
10431
10432     case OPTION_MIPS32:
10433       file_mips_isa = ISA_MIPS32;
10434       break;
10435
10436     case OPTION_MIPS64:
10437       file_mips_isa = ISA_MIPS64;
10438       break;
10439
10440     case OPTION_MTUNE:
10441       mips_set_option_string (&mips_tune_string, arg);
10442       break;
10443
10444     case OPTION_MARCH:
10445       mips_set_option_string (&mips_arch_string, arg);
10446       break;
10447
10448     case OPTION_M4650:
10449       mips_set_option_string (&mips_arch_string, "4650");
10450       mips_set_option_string (&mips_tune_string, "4650");
10451       break;
10452
10453     case OPTION_NO_M4650:
10454       break;
10455
10456     case OPTION_M4010:
10457       mips_set_option_string (&mips_arch_string, "4010");
10458       mips_set_option_string (&mips_tune_string, "4010");
10459       break;
10460
10461     case OPTION_NO_M4010:
10462       break;
10463
10464     case OPTION_M4100:
10465       mips_set_option_string (&mips_arch_string, "4100");
10466       mips_set_option_string (&mips_tune_string, "4100");
10467       break;
10468
10469     case OPTION_NO_M4100:
10470       break;
10471
10472     case OPTION_M3900:
10473       mips_set_option_string (&mips_arch_string, "3900");
10474       mips_set_option_string (&mips_tune_string, "3900");
10475       break;
10476
10477     case OPTION_NO_M3900:
10478       break;
10479
10480     case OPTION_MDMX:
10481       mips_opts.ase_mdmx = 1;
10482       break;
10483
10484     case OPTION_NO_MDMX:
10485       mips_opts.ase_mdmx = 0;
10486       break;
10487
10488     case OPTION_MIPS16:
10489       mips_opts.mips16 = 1;
10490       mips_no_prev_insn (FALSE);
10491       break;
10492
10493     case OPTION_NO_MIPS16:
10494       mips_opts.mips16 = 0;
10495       mips_no_prev_insn (FALSE);
10496       break;
10497
10498     case OPTION_MIPS3D:
10499       mips_opts.ase_mips3d = 1;
10500       break;
10501
10502     case OPTION_NO_MIPS3D:
10503       mips_opts.ase_mips3d = 0;
10504       break;
10505
10506     case OPTION_MEMBEDDED_PIC:
10507       mips_pic = EMBEDDED_PIC;
10508       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10509         {
10510           as_bad (_("-G may not be used with embedded PIC code"));
10511           return 0;
10512         }
10513       g_switch_value = 0x7fffffff;
10514       break;
10515
10516     case OPTION_FIX_VR4122:
10517       mips_fix_4122_bugs = 1;
10518       break;
10519
10520     case OPTION_NO_FIX_VR4122:
10521       mips_fix_4122_bugs = 0;
10522       break;
10523
10524     case OPTION_RELAX_BRANCH:
10525       mips_relax_branch = 1;
10526       break;
10527
10528     case OPTION_NO_RELAX_BRANCH:
10529       mips_relax_branch = 0;
10530       break;
10531
10532 #ifdef OBJ_ELF
10533       /* When generating ELF code, we permit -KPIC and -call_shared to
10534          select SVR4_PIC, and -non_shared to select no PIC.  This is
10535          intended to be compatible with Irix 5.  */
10536     case OPTION_CALL_SHARED:
10537       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10538         {
10539           as_bad (_("-call_shared is supported only for ELF format"));
10540           return 0;
10541         }
10542       mips_pic = SVR4_PIC;
10543       if (g_switch_seen && g_switch_value != 0)
10544         {
10545           as_bad (_("-G may not be used with SVR4 PIC code"));
10546           return 0;
10547         }
10548       g_switch_value = 0;
10549       break;
10550
10551     case OPTION_NON_SHARED:
10552       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10553         {
10554           as_bad (_("-non_shared is supported only for ELF format"));
10555           return 0;
10556         }
10557       mips_pic = NO_PIC;
10558       break;
10559
10560       /* The -xgot option tells the assembler to use 32 offsets when
10561          accessing the got in SVR4_PIC mode.  It is for Irix
10562          compatibility.  */
10563     case OPTION_XGOT:
10564       mips_big_got = 1;
10565       break;
10566 #endif /* OBJ_ELF */
10567
10568     case 'G':
10569       if (! USE_GLOBAL_POINTER_OPT)
10570         {
10571           as_bad (_("-G is not supported for this configuration"));
10572           return 0;
10573         }
10574       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10575         {
10576           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10577           return 0;
10578         }
10579       else
10580         g_switch_value = atoi (arg);
10581       g_switch_seen = 1;
10582       break;
10583
10584 #ifdef OBJ_ELF
10585       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10586          and -mabi=64.  */
10587     case OPTION_32:
10588       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10589         {
10590           as_bad (_("-32 is supported for ELF format only"));
10591           return 0;
10592         }
10593       mips_abi = O32_ABI;
10594       break;
10595
10596     case OPTION_N32:
10597       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10598         {
10599           as_bad (_("-n32 is supported for ELF format only"));
10600           return 0;
10601         }
10602       mips_abi = N32_ABI;
10603       break;
10604
10605     case OPTION_64:
10606       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10607         {
10608           as_bad (_("-64 is supported for ELF format only"));
10609           return 0;
10610         }
10611       mips_abi = N64_ABI;
10612       if (! support_64bit_objects())
10613         as_fatal (_("No compiled in support for 64 bit object file format"));
10614       break;
10615 #endif /* OBJ_ELF */
10616
10617     case OPTION_GP32:
10618       file_mips_gp32 = 1;
10619       break;
10620
10621     case OPTION_GP64:
10622       file_mips_gp32 = 0;
10623       break;
10624
10625     case OPTION_FP32:
10626       file_mips_fp32 = 1;
10627       break;
10628
10629     case OPTION_FP64:
10630       file_mips_fp32 = 0;
10631       break;
10632
10633 #ifdef OBJ_ELF
10634     case OPTION_MABI:
10635       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10636         {
10637           as_bad (_("-mabi is supported for ELF format only"));
10638           return 0;
10639         }
10640       if (strcmp (arg, "32") == 0)
10641         mips_abi = O32_ABI;
10642       else if (strcmp (arg, "o64") == 0)
10643         mips_abi = O64_ABI;
10644       else if (strcmp (arg, "n32") == 0)
10645         mips_abi = N32_ABI;
10646       else if (strcmp (arg, "64") == 0)
10647         {
10648           mips_abi = N64_ABI;
10649           if (! support_64bit_objects())
10650             as_fatal (_("No compiled in support for 64 bit object file "
10651                         "format"));
10652         }
10653       else if (strcmp (arg, "eabi") == 0)
10654         mips_abi = EABI_ABI;
10655       else
10656         {
10657           as_fatal (_("invalid abi -mabi=%s"), arg);
10658           return 0;
10659         }
10660       break;
10661 #endif /* OBJ_ELF */
10662
10663     case OPTION_M7000_HILO_FIX:
10664       mips_7000_hilo_fix = TRUE;
10665       break;
10666
10667     case OPTION_MNO_7000_HILO_FIX:
10668       mips_7000_hilo_fix = FALSE;
10669       break;
10670
10671 #ifdef OBJ_ELF
10672     case OPTION_MDEBUG:
10673       mips_flag_mdebug = TRUE;
10674       break;
10675
10676     case OPTION_NO_MDEBUG:
10677       mips_flag_mdebug = FALSE;
10678       break;
10679 #endif /* OBJ_ELF */
10680
10681     default:
10682       return 0;
10683     }
10684
10685   return 1;
10686 }
10687 \f
10688 /* Set up globals to generate code for the ISA or processor
10689    described by INFO.  */
10690
10691 static void
10692 mips_set_architecture (info)
10693      const struct mips_cpu_info *info;
10694 {
10695   if (info != 0)
10696     {
10697       mips_arch_info = info;
10698       mips_arch = info->cpu;
10699       mips_opts.isa = info->isa;
10700     }
10701 }
10702
10703
10704 /* Likewise for tuning.  */
10705
10706 static void
10707 mips_set_tune (info)
10708      const struct mips_cpu_info *info;
10709 {
10710   if (info != 0)
10711     {
10712       mips_tune_info = info;
10713       mips_tune = info->cpu;
10714     }
10715 }
10716
10717
10718 void
10719 mips_after_parse_args ()
10720 {
10721   /* GP relative stuff not working for PE */
10722   if (strncmp (TARGET_OS, "pe", 2) == 0
10723       && g_switch_value != 0)
10724     {
10725       if (g_switch_seen)
10726         as_bad (_("-G not supported in this configuration."));
10727       g_switch_value = 0;
10728     }
10729
10730   /* The following code determines the architecture and register size.
10731      Similar code was added to GCC 3.3 (see override_options() in
10732      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10733      as much as possible.  */
10734
10735   if (mips_arch_string != 0)
10736     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10737
10738   if (mips_tune_string != 0)
10739     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10740
10741   if (file_mips_isa != ISA_UNKNOWN)
10742     {
10743       /* Handle -mipsN.  At this point, file_mips_isa contains the
10744          ISA level specified by -mipsN, while mips_opts.isa contains
10745          the -march selection (if any).  */
10746       if (mips_arch_info != 0)
10747         {
10748           /* -march takes precedence over -mipsN, since it is more descriptive.
10749              There's no harm in specifying both as long as the ISA levels
10750              are the same.  */
10751           if (file_mips_isa != mips_opts.isa)
10752             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10753                     mips_cpu_info_from_isa (file_mips_isa)->name,
10754                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10755         }
10756       else
10757         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10758     }
10759
10760   if (mips_arch_info == 0)
10761     mips_set_architecture (mips_parse_cpu ("default CPU",
10762                                            MIPS_CPU_STRING_DEFAULT));
10763
10764   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10765     as_bad ("-march=%s is not compatible with the selected ABI",
10766             mips_arch_info->name);
10767
10768   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10769   if (mips_tune_info == 0)
10770     mips_set_tune (mips_arch_info);
10771
10772   if (file_mips_gp32 >= 0)
10773     {
10774       /* The user specified the size of the integer registers.  Make sure
10775          it agrees with the ABI and ISA.  */
10776       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10777         as_bad (_("-mgp64 used with a 32-bit processor"));
10778       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10779         as_bad (_("-mgp32 used with a 64-bit ABI"));
10780       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10781         as_bad (_("-mgp64 used with a 32-bit ABI"));
10782     }
10783   else
10784     {
10785       /* Infer the integer register size from the ABI and processor.
10786          Restrict ourselves to 32-bit registers if that's all the
10787          processor has, or if the ABI cannot handle 64-bit registers.  */
10788       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10789                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10790     }
10791
10792   /* ??? GAS treats single-float processors as though they had 64-bit
10793      float registers (although it complains when double-precision
10794      instructions are used).  As things stand, saying they have 32-bit
10795      registers would lead to spurious "register must be even" messages.
10796      So here we assume float registers are always the same size as
10797      integer ones, unless the user says otherwise.  */
10798   if (file_mips_fp32 < 0)
10799     file_mips_fp32 = file_mips_gp32;
10800
10801   /* End of GCC-shared inference code.  */
10802
10803   /* ??? When do we want this flag to be set?   Who uses it?  */
10804   if (file_mips_gp32 == 1
10805       && mips_abi == NO_ABI
10806       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10807     mips_32bitmode = 1;
10808
10809   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10810     as_bad (_("trap exception not supported at ISA 1"));
10811
10812   /* If the selected architecture includes support for ASEs, enable
10813      generation of code for them.  */
10814   if (mips_opts.mips16 == -1)
10815     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10816   if (mips_opts.ase_mips3d == -1)
10817     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10818   if (mips_opts.ase_mdmx == -1)
10819     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10820
10821   file_mips_isa = mips_opts.isa;
10822   file_ase_mips16 = mips_opts.mips16;
10823   file_ase_mips3d = mips_opts.ase_mips3d;
10824   file_ase_mdmx = mips_opts.ase_mdmx;
10825   mips_opts.gp32 = file_mips_gp32;
10826   mips_opts.fp32 = file_mips_fp32;
10827
10828   if (mips_flag_mdebug < 0)
10829     {
10830 #ifdef OBJ_MAYBE_ECOFF
10831       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10832         mips_flag_mdebug = 1;
10833       else
10834 #endif /* OBJ_MAYBE_ECOFF */
10835         mips_flag_mdebug = 0;
10836     }
10837 }
10838 \f
10839 void
10840 mips_init_after_args ()
10841 {
10842   /* initialize opcodes */
10843   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10844   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10845 }
10846
10847 long
10848 md_pcrel_from (fixP)
10849      fixS *fixP;
10850 {
10851   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10852       && fixP->fx_addsy != (symbolS *) NULL
10853       && ! S_IS_DEFINED (fixP->fx_addsy))
10854     return 4;
10855
10856   /* Return the address of the delay slot.  */
10857   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10858 }
10859
10860 /* This is called before the symbol table is processed.  In order to
10861    work with gcc when using mips-tfile, we must keep all local labels.
10862    However, in other cases, we want to discard them.  If we were
10863    called with -g, but we didn't see any debugging information, it may
10864    mean that gcc is smuggling debugging information through to
10865    mips-tfile, in which case we must generate all local labels.  */
10866
10867 void
10868 mips_frob_file_before_adjust ()
10869 {
10870 #ifndef NO_ECOFF_DEBUGGING
10871   if (ECOFF_DEBUGGING
10872       && mips_debug != 0
10873       && ! ecoff_debugging_seen)
10874     flag_keep_locals = 1;
10875 #endif
10876 }
10877
10878 /* Sort any unmatched HI16_S relocs so that they immediately precede
10879    the corresponding LO reloc.  This is called before md_apply_fix3 and
10880    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10881    explicit use of the %hi modifier.  */
10882
10883 void
10884 mips_frob_file ()
10885 {
10886   struct mips_hi_fixup *l;
10887
10888   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10889     {
10890       segment_info_type *seginfo;
10891       int pass;
10892
10893       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10894
10895       /* Check quickly whether the next fixup happens to be a matching
10896          %lo.  */
10897       if (l->fixp->fx_next != NULL
10898           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10899           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10900           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10901         continue;
10902
10903       /* Look through the fixups for this segment for a matching %lo.
10904          When we find one, move the %hi just in front of it.  We do
10905          this in two passes.  In the first pass, we try to find a
10906          unique %lo.  In the second pass, we permit multiple %hi
10907          relocs for a single %lo (this is a GNU extension).  */
10908       seginfo = seg_info (l->seg);
10909       for (pass = 0; pass < 2; pass++)
10910         {
10911           fixS *f, *prev;
10912
10913           prev = NULL;
10914           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10915             {
10916               /* Check whether this is a %lo fixup which matches l->fixp.  */
10917               if (f->fx_r_type == BFD_RELOC_LO16
10918                   && f->fx_addsy == l->fixp->fx_addsy
10919                   && f->fx_offset == l->fixp->fx_offset
10920                   && (pass == 1
10921                       || prev == NULL
10922                       || prev->fx_r_type != BFD_RELOC_HI16_S
10923                       || prev->fx_addsy != f->fx_addsy
10924                       || prev->fx_offset !=  f->fx_offset))
10925                 {
10926                   fixS **pf;
10927
10928                   /* Move l->fixp before f.  */
10929                   for (pf = &seginfo->fix_root;
10930                        *pf != l->fixp;
10931                        pf = &(*pf)->fx_next)
10932                     assert (*pf != NULL);
10933
10934                   *pf = l->fixp->fx_next;
10935
10936                   l->fixp->fx_next = f;
10937                   if (prev == NULL)
10938                     seginfo->fix_root = l->fixp;
10939                   else
10940                     prev->fx_next = l->fixp;
10941
10942                   break;
10943                 }
10944
10945               prev = f;
10946             }
10947
10948           if (f != NULL)
10949             break;
10950
10951 #if 0 /* GCC code motion plus incomplete dead code elimination
10952          can leave a %hi without a %lo.  */
10953           if (pass == 1)
10954             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10955                            _("Unmatched %%hi reloc"));
10956 #endif
10957         }
10958     }
10959 }
10960
10961 /* When generating embedded PIC code we need to use a special
10962    relocation to represent the difference of two symbols in the .text
10963    section (switch tables use a difference of this sort).  See
10964    include/coff/mips.h for details.  This macro checks whether this
10965    fixup requires the special reloc.  */
10966 #define SWITCH_TABLE(fixp) \
10967   ((fixp)->fx_r_type == BFD_RELOC_32 \
10968    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10969    && (fixp)->fx_addsy != NULL \
10970    && (fixp)->fx_subsy != NULL \
10971    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10972    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10973
10974 /* When generating embedded PIC code we must keep all PC relative
10975    relocations, in case the linker has to relax a call.  We also need
10976    to keep relocations for switch table entries.
10977
10978    We may have combined relocations without symbols in the N32/N64 ABI.
10979    We have to prevent gas from dropping them.  */
10980
10981 int
10982 mips_force_relocation (fixp)
10983      fixS *fixp;
10984 {
10985   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10986       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10987       || S_FORCE_RELOC (fixp->fx_addsy))
10988     return 1;
10989
10990   if (HAVE_NEWABI
10991       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10992       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10993           || fixp->fx_r_type == BFD_RELOC_HI16_S
10994           || fixp->fx_r_type == BFD_RELOC_LO16))
10995     return 1;
10996
10997   return (mips_pic == EMBEDDED_PIC
10998           && (fixp->fx_pcrel
10999               || SWITCH_TABLE (fixp)
11000               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11001               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11002 }
11003
11004 #ifdef OBJ_ELF
11005 static int
11006 mips_need_elf_addend_fixup (fixP)
11007      fixS *fixP;
11008 {
11009   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11010     return 1;
11011   if (mips_pic == EMBEDDED_PIC
11012       && S_IS_WEAK (fixP->fx_addsy))
11013     return 1;
11014   if (mips_pic != EMBEDDED_PIC
11015       && (S_IS_WEAK (fixP->fx_addsy)
11016           || S_IS_EXTERNAL (fixP->fx_addsy))
11017       && !S_IS_COMMON (fixP->fx_addsy))
11018     return 1;
11019   if (symbol_used_in_reloc_p (fixP->fx_addsy)
11020       && (((bfd_get_section_flags (stdoutput,
11021                                    S_GET_SEGMENT (fixP->fx_addsy))
11022             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11023           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11024                        ".gnu.linkonce",
11025                        sizeof (".gnu.linkonce") - 1)))
11026     return 1;
11027   return 0;
11028 }
11029 #endif
11030
11031 /* Apply a fixup to the object file.  */
11032
11033 void
11034 md_apply_fix3 (fixP, valP, seg)
11035      fixS *fixP;
11036      valueT *valP;
11037      segT seg ATTRIBUTE_UNUSED;
11038 {
11039   bfd_byte *buf;
11040   long insn;
11041   valueT value;
11042   static int previous_fx_r_type = 0;
11043
11044   /* FIXME: Maybe just return for all reloc types not listed below?
11045      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11046   if (fixP->fx_r_type == BFD_RELOC_8)
11047       return;
11048
11049   assert (fixP->fx_size == 4
11050           || fixP->fx_r_type == BFD_RELOC_16
11051           || fixP->fx_r_type == BFD_RELOC_32
11052           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11053           || fixP->fx_r_type == BFD_RELOC_HI16_S
11054           || fixP->fx_r_type == BFD_RELOC_LO16
11055           || fixP->fx_r_type == BFD_RELOC_GPREL16
11056           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11057           || fixP->fx_r_type == BFD_RELOC_GPREL32
11058           || fixP->fx_r_type == BFD_RELOC_64
11059           || fixP->fx_r_type == BFD_RELOC_CTOR
11060           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11061           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11062           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11063           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11064           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11065           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11066           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11067           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11068           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11069
11070   value = *valP;
11071
11072   /* If we aren't adjusting this fixup to be against the section
11073      symbol, we need to adjust the value.  */
11074 #ifdef OBJ_ELF
11075   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11076     {
11077       if (mips_need_elf_addend_fixup (fixP))
11078         {
11079           reloc_howto_type *howto;
11080           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11081
11082           value -= symval;
11083
11084           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11085           if (value != 0 && howto && howto->partial_inplace
11086               && (! fixP->fx_pcrel || howto->pcrel_offset))
11087             {
11088               /* In this case, the bfd_install_relocation routine will
11089                  incorrectly add the symbol value back in.  We just want
11090                  the addend to appear in the object file.
11091
11092                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11093                  generated for code like
11094
11095                         globl g1 .text
11096                         .text
11097                         .space 20
11098                  g1:
11099                  x:
11100                         bal g1
11101                */
11102               value -= symval;
11103
11104               /* Make sure the addend is still non-zero.  If it became zero
11105                  after the last operation, set it to a spurious value and
11106                  subtract the same value from the object file's contents.  */
11107               if (value == 0)
11108                 {
11109                   value = 8;
11110
11111                   /* The in-place addends for LO16 relocations are signed;
11112                      leave the matching HI16 in-place addends as zero.  */
11113                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11114                     {
11115                       bfd_vma contents, mask, field;
11116
11117                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11118                                                + fixP->fx_where,
11119                                                fixP->fx_size * 8,
11120                                                target_big_endian);
11121
11122                       /* MASK has bits set where the relocation should go.
11123                          FIELD is -value, shifted into the appropriate place
11124                          for this relocation.  */
11125                       mask = 1 << (howto->bitsize - 1);
11126                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11127                       field = (-value >> howto->rightshift) << howto->bitpos;
11128
11129                       bfd_put_bits ((field & mask) | (contents & ~mask),
11130                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11131                                     fixP->fx_size * 8,
11132                                     target_big_endian);
11133                     }
11134                 }
11135             }
11136         }
11137
11138       /* This code was generated using trial and error and so is
11139          fragile and not trustworthy.  If you change it, you should
11140          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11141          they still pass.  */
11142       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11143         {
11144           value += fixP->fx_frag->fr_address + fixP->fx_where;
11145
11146           /* BFD's REL handling, for MIPS, is _very_ weird.
11147              This gives the right results, but it can't possibly
11148              be the way things are supposed to work.  */
11149           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11150               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11151             value += fixP->fx_frag->fr_address + fixP->fx_where;
11152         }
11153     }
11154 #endif
11155
11156   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11157
11158   /* We are not done if this is a composite relocation to set up gp.  */
11159   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11160       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11161            || (fixP->fx_r_type == BFD_RELOC_64
11162                && (previous_fx_r_type == BFD_RELOC_GPREL32
11163                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11164            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11165                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11166                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11167     fixP->fx_done = 1;
11168   previous_fx_r_type = fixP->fx_r_type;
11169
11170   switch (fixP->fx_r_type)
11171     {
11172     case BFD_RELOC_MIPS_JMP:
11173     case BFD_RELOC_MIPS_SHIFT5:
11174     case BFD_RELOC_MIPS_SHIFT6:
11175     case BFD_RELOC_MIPS_GOT_DISP:
11176     case BFD_RELOC_MIPS_GOT_PAGE:
11177     case BFD_RELOC_MIPS_GOT_OFST:
11178     case BFD_RELOC_MIPS_SUB:
11179     case BFD_RELOC_MIPS_INSERT_A:
11180     case BFD_RELOC_MIPS_INSERT_B:
11181     case BFD_RELOC_MIPS_DELETE:
11182     case BFD_RELOC_MIPS_HIGHEST:
11183     case BFD_RELOC_MIPS_HIGHER:
11184     case BFD_RELOC_MIPS_SCN_DISP:
11185     case BFD_RELOC_MIPS_REL16:
11186     case BFD_RELOC_MIPS_RELGOT:
11187     case BFD_RELOC_MIPS_JALR:
11188     case BFD_RELOC_HI16:
11189     case BFD_RELOC_HI16_S:
11190     case BFD_RELOC_GPREL16:
11191     case BFD_RELOC_MIPS_LITERAL:
11192     case BFD_RELOC_MIPS_CALL16:
11193     case BFD_RELOC_MIPS_GOT16:
11194     case BFD_RELOC_GPREL32:
11195     case BFD_RELOC_MIPS_GOT_HI16:
11196     case BFD_RELOC_MIPS_GOT_LO16:
11197     case BFD_RELOC_MIPS_CALL_HI16:
11198     case BFD_RELOC_MIPS_CALL_LO16:
11199     case BFD_RELOC_MIPS16_GPREL:
11200       if (fixP->fx_pcrel)
11201         as_bad_where (fixP->fx_file, fixP->fx_line,
11202                       _("Invalid PC relative reloc"));
11203       /* Nothing needed to do. The value comes from the reloc entry */
11204       break;
11205
11206     case BFD_RELOC_MIPS16_JMP:
11207       /* We currently always generate a reloc against a symbol, which
11208          means that we don't want an addend even if the symbol is
11209          defined.  */
11210       fixP->fx_addnumber = 0;
11211       break;
11212
11213     case BFD_RELOC_PCREL_HI16_S:
11214       /* The addend for this is tricky if it is internal, so we just
11215          do everything here rather than in bfd_install_relocation.  */
11216       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11217           && !fixP->fx_done
11218           && value != 0)
11219         break;
11220       if (fixP->fx_addsy
11221           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11222         {
11223           /* For an external symbol adjust by the address to make it
11224              pcrel_offset.  We use the address of the RELLO reloc
11225              which follows this one.  */
11226           value += (fixP->fx_next->fx_frag->fr_address
11227                     + fixP->fx_next->fx_where);
11228         }
11229       value = ((value + 0x8000) >> 16) & 0xffff;
11230       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11231       if (target_big_endian)
11232         buf += 2;
11233       md_number_to_chars ((char *) buf, value, 2);
11234       break;
11235
11236     case BFD_RELOC_PCREL_LO16:
11237       /* The addend for this is tricky if it is internal, so we just
11238          do everything here rather than in bfd_install_relocation.  */
11239       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11240           && !fixP->fx_done
11241           && value != 0)
11242         break;
11243       if (fixP->fx_addsy
11244           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11245         value += fixP->fx_frag->fr_address + fixP->fx_where;
11246       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11247       if (target_big_endian)
11248         buf += 2;
11249       md_number_to_chars ((char *) buf, value, 2);
11250       break;
11251
11252     case BFD_RELOC_64:
11253       /* This is handled like BFD_RELOC_32, but we output a sign
11254          extended value if we are only 32 bits.  */
11255       if (fixP->fx_done
11256           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11257         {
11258           if (8 <= sizeof (valueT))
11259             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11260                                 value, 8);
11261           else
11262             {
11263               long w1, w2;
11264               long hiv;
11265
11266               w1 = w2 = fixP->fx_where;
11267               if (target_big_endian)
11268                 w1 += 4;
11269               else
11270                 w2 += 4;
11271               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11272               if ((value & 0x80000000) != 0)
11273                 hiv = 0xffffffff;
11274               else
11275                 hiv = 0;
11276               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11277             }
11278         }
11279       break;
11280
11281     case BFD_RELOC_RVA:
11282     case BFD_RELOC_32:
11283       /* If we are deleting this reloc entry, we must fill in the
11284          value now.  This can happen if we have a .word which is not
11285          resolved when it appears but is later defined.  We also need
11286          to fill in the value if this is an embedded PIC switch table
11287          entry.  */
11288       if (fixP->fx_done
11289           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11290         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11291                             value, 4);
11292       break;
11293
11294     case BFD_RELOC_16:
11295       /* If we are deleting this reloc entry, we must fill in the
11296          value now.  */
11297       assert (fixP->fx_size == 2);
11298       if (fixP->fx_done)
11299         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11300                             value, 2);
11301       break;
11302
11303     case BFD_RELOC_LO16:
11304       /* When handling an embedded PIC switch statement, we can wind
11305          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11306       if (fixP->fx_done)
11307         {
11308           if (value + 0x8000 > 0xffff)
11309             as_bad_where (fixP->fx_file, fixP->fx_line,
11310                           _("relocation overflow"));
11311           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11312           if (target_big_endian)
11313             buf += 2;
11314           md_number_to_chars ((char *) buf, value, 2);
11315         }
11316       break;
11317
11318     case BFD_RELOC_16_PCREL_S2:
11319       if ((value & 0x3) != 0)
11320         as_bad_where (fixP->fx_file, fixP->fx_line,
11321                       _("Branch to odd address (%lx)"), (long) value);
11322
11323       /*
11324        * We need to save the bits in the instruction since fixup_segment()
11325        * might be deleting the relocation entry (i.e., a branch within
11326        * the current segment).
11327        */
11328       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11329         break;
11330       /* If 'value' is zero, the remaining reloc code won't actually
11331          do the store, so it must be done here.  This is probably
11332          a bug somewhere.  */
11333       if (!fixP->fx_done
11334           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11335               || fixP->fx_addsy == NULL                 /* ??? */
11336               || ! S_IS_DEFINED (fixP->fx_addsy)))
11337         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11338
11339       value = (offsetT) value >> 2;
11340
11341       /* update old instruction data */
11342       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11343       if (target_big_endian)
11344         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11345       else
11346         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11347
11348       if (value + 0x8000 <= 0xffff)
11349         insn |= value & 0xffff;
11350       else
11351         {
11352           /* The branch offset is too large.  If this is an
11353              unconditional branch, and we are not generating PIC code,
11354              we can convert it to an absolute jump instruction.  */
11355           if (mips_pic == NO_PIC
11356               && fixP->fx_done
11357               && fixP->fx_frag->fr_address >= text_section->vma
11358               && (fixP->fx_frag->fr_address
11359                   < text_section->vma + text_section->_raw_size)
11360               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11361                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11362                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11363             {
11364               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11365                 insn = 0x0c000000;      /* jal */
11366               else
11367                 insn = 0x08000000;      /* j */
11368               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11369               fixP->fx_done = 0;
11370               fixP->fx_addsy = section_symbol (text_section);
11371               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11372             }
11373           else
11374             {
11375               /* If we got here, we have branch-relaxation disabled,
11376                  and there's nothing we can do to fix this instruction
11377                  without turning it into a longer sequence.  */
11378               as_bad_where (fixP->fx_file, fixP->fx_line,
11379                             _("Branch out of range"));
11380             }
11381         }
11382
11383       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11384       break;
11385
11386     case BFD_RELOC_VTABLE_INHERIT:
11387       fixP->fx_done = 0;
11388       if (fixP->fx_addsy
11389           && !S_IS_DEFINED (fixP->fx_addsy)
11390           && !S_IS_WEAK (fixP->fx_addsy))
11391         S_SET_WEAK (fixP->fx_addsy);
11392       break;
11393
11394     case BFD_RELOC_VTABLE_ENTRY:
11395       fixP->fx_done = 0;
11396       break;
11397
11398     default:
11399       internalError ();
11400     }
11401 }
11402
11403 #if 0
11404 void
11405 printInsn (oc)
11406      unsigned long oc;
11407 {
11408   const struct mips_opcode *p;
11409   int treg, sreg, dreg, shamt;
11410   short imm;
11411   const char *args;
11412   int i;
11413
11414   for (i = 0; i < NUMOPCODES; ++i)
11415     {
11416       p = &mips_opcodes[i];
11417       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11418         {
11419           printf ("%08lx %s\t", oc, p->name);
11420           treg = (oc >> 16) & 0x1f;
11421           sreg = (oc >> 21) & 0x1f;
11422           dreg = (oc >> 11) & 0x1f;
11423           shamt = (oc >> 6) & 0x1f;
11424           imm = oc;
11425           for (args = p->args;; ++args)
11426             {
11427               switch (*args)
11428                 {
11429                 case '\0':
11430                   printf ("\n");
11431                   break;
11432
11433                 case ',':
11434                 case '(':
11435                 case ')':
11436                   printf ("%c", *args);
11437                   continue;
11438
11439                 case 'r':
11440                   assert (treg == sreg);
11441                   printf ("$%d,$%d", treg, sreg);
11442                   continue;
11443
11444                 case 'd':
11445                 case 'G':
11446                   printf ("$%d", dreg);
11447                   continue;
11448
11449                 case 't':
11450                 case 'E':
11451                   printf ("$%d", treg);
11452                   continue;
11453
11454                 case 'k':
11455                   printf ("0x%x", treg);
11456                   continue;
11457
11458                 case 'b':
11459                 case 's':
11460                   printf ("$%d", sreg);
11461                   continue;
11462
11463                 case 'a':
11464                   printf ("0x%08lx", oc & 0x1ffffff);
11465                   continue;
11466
11467                 case 'i':
11468                 case 'j':
11469                 case 'o':
11470                 case 'u':
11471                   printf ("%d", imm);
11472                   continue;
11473
11474                 case '<':
11475                 case '>':
11476                   printf ("$%d", shamt);
11477                   continue;
11478
11479                 default:
11480                   internalError ();
11481                 }
11482               break;
11483             }
11484           return;
11485         }
11486     }
11487   printf (_("%08lx  UNDEFINED\n"), oc);
11488 }
11489 #endif
11490
11491 static symbolS *
11492 get_symbol ()
11493 {
11494   int c;
11495   char *name;
11496   symbolS *p;
11497
11498   name = input_line_pointer;
11499   c = get_symbol_end ();
11500   p = (symbolS *) symbol_find_or_make (name);
11501   *input_line_pointer = c;
11502   return p;
11503 }
11504
11505 /* Align the current frag to a given power of two.  The MIPS assembler
11506    also automatically adjusts any preceding label.  */
11507
11508 static void
11509 mips_align (to, fill, label)
11510      int to;
11511      int fill;
11512      symbolS *label;
11513 {
11514   mips_emit_delays (FALSE);
11515   frag_align (to, fill, 0);
11516   record_alignment (now_seg, to);
11517   if (label != NULL)
11518     {
11519       assert (S_GET_SEGMENT (label) == now_seg);
11520       symbol_set_frag (label, frag_now);
11521       S_SET_VALUE (label, (valueT) frag_now_fix ());
11522     }
11523 }
11524
11525 /* Align to a given power of two.  .align 0 turns off the automatic
11526    alignment used by the data creating pseudo-ops.  */
11527
11528 static void
11529 s_align (x)
11530      int x ATTRIBUTE_UNUSED;
11531 {
11532   register int temp;
11533   register long temp_fill;
11534   long max_alignment = 15;
11535
11536   /*
11537
11538     o  Note that the assembler pulls down any immediately preceeding label
11539        to the aligned address.
11540     o  It's not documented but auto alignment is reinstated by
11541        a .align pseudo instruction.
11542     o  Note also that after auto alignment is turned off the mips assembler
11543        issues an error on attempt to assemble an improperly aligned data item.
11544        We don't.
11545
11546     */
11547
11548   temp = get_absolute_expression ();
11549   if (temp > max_alignment)
11550     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11551   else if (temp < 0)
11552     {
11553       as_warn (_("Alignment negative: 0 assumed."));
11554       temp = 0;
11555     }
11556   if (*input_line_pointer == ',')
11557     {
11558       ++input_line_pointer;
11559       temp_fill = get_absolute_expression ();
11560     }
11561   else
11562     temp_fill = 0;
11563   if (temp)
11564     {
11565       auto_align = 1;
11566       mips_align (temp, (int) temp_fill,
11567                   insn_labels != NULL ? insn_labels->label : NULL);
11568     }
11569   else
11570     {
11571       auto_align = 0;
11572     }
11573
11574   demand_empty_rest_of_line ();
11575 }
11576
11577 void
11578 mips_flush_pending_output ()
11579 {
11580   mips_emit_delays (FALSE);
11581   mips_clear_insn_labels ();
11582 }
11583
11584 static void
11585 s_change_sec (sec)
11586      int sec;
11587 {
11588   segT seg;
11589
11590   /* When generating embedded PIC code, we only use the .text, .lit8,
11591      .sdata and .sbss sections.  We change the .data and .rdata
11592      pseudo-ops to use .sdata.  */
11593   if (mips_pic == EMBEDDED_PIC
11594       && (sec == 'd' || sec == 'r'))
11595     sec = 's';
11596
11597 #ifdef OBJ_ELF
11598   /* The ELF backend needs to know that we are changing sections, so
11599      that .previous works correctly.  We could do something like check
11600      for an obj_section_change_hook macro, but that might be confusing
11601      as it would not be appropriate to use it in the section changing
11602      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11603      This should be cleaner, somehow.  */
11604   obj_elf_section_change_hook ();
11605 #endif
11606
11607   mips_emit_delays (FALSE);
11608   switch (sec)
11609     {
11610     case 't':
11611       s_text (0);
11612       break;
11613     case 'd':
11614       s_data (0);
11615       break;
11616     case 'b':
11617       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11618       demand_empty_rest_of_line ();
11619       break;
11620
11621     case 'r':
11622       if (USE_GLOBAL_POINTER_OPT)
11623         {
11624           seg = subseg_new (RDATA_SECTION_NAME,
11625                             (subsegT) get_absolute_expression ());
11626           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11627             {
11628               bfd_set_section_flags (stdoutput, seg,
11629                                      (SEC_ALLOC
11630                                       | SEC_LOAD
11631                                       | SEC_READONLY
11632                                       | SEC_RELOC
11633                                       | SEC_DATA));
11634               if (strcmp (TARGET_OS, "elf") != 0)
11635                 record_alignment (seg, 4);
11636             }
11637           demand_empty_rest_of_line ();
11638         }
11639       else
11640         {
11641           as_bad (_("No read only data section in this object file format"));
11642           demand_empty_rest_of_line ();
11643           return;
11644         }
11645       break;
11646
11647     case 's':
11648       if (USE_GLOBAL_POINTER_OPT)
11649         {
11650           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11651           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11652             {
11653               bfd_set_section_flags (stdoutput, seg,
11654                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11655                                      | SEC_DATA);
11656               if (strcmp (TARGET_OS, "elf") != 0)
11657                 record_alignment (seg, 4);
11658             }
11659           demand_empty_rest_of_line ();
11660           break;
11661         }
11662       else
11663         {
11664           as_bad (_("Global pointers not supported; recompile -G 0"));
11665           demand_empty_rest_of_line ();
11666           return;
11667         }
11668     }
11669
11670   auto_align = 1;
11671 }
11672
11673 void
11674 s_change_section (ignore)
11675      int ignore ATTRIBUTE_UNUSED;
11676 {
11677 #ifdef OBJ_ELF
11678   char *section_name;
11679   char c;
11680   char next_c;
11681   int section_type;
11682   int section_flag;
11683   int section_entry_size;
11684   int section_alignment;
11685
11686   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11687     return;
11688
11689   section_name = input_line_pointer;
11690   c = get_symbol_end ();
11691   if (c)
11692     next_c = *(input_line_pointer + 1);
11693
11694   /* Do we have .section Name<,"flags">?  */
11695   if (c != ',' || (c == ',' && next_c == '"'))
11696     {
11697       /* just after name is now '\0'.  */
11698       *input_line_pointer = c;
11699       input_line_pointer = section_name;
11700       obj_elf_section (ignore);
11701       return;
11702     }
11703   input_line_pointer++;
11704
11705   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11706   if (c == ',')
11707     section_type = get_absolute_expression ();
11708   else
11709     section_type = 0;
11710   if (*input_line_pointer++ == ',')
11711     section_flag = get_absolute_expression ();
11712   else
11713     section_flag = 0;
11714   if (*input_line_pointer++ == ',')
11715     section_entry_size = get_absolute_expression ();
11716   else
11717     section_entry_size = 0;
11718   if (*input_line_pointer++ == ',')
11719     section_alignment = get_absolute_expression ();
11720   else
11721     section_alignment = 0;
11722
11723   section_name = xstrdup (section_name);
11724
11725   obj_elf_change_section (section_name, section_type, section_flag,
11726                           section_entry_size, 0, 0, 0);
11727
11728   if (now_seg->name != section_name)
11729     free (section_name);
11730 #endif /* OBJ_ELF */
11731 }
11732
11733 void
11734 mips_enable_auto_align ()
11735 {
11736   auto_align = 1;
11737 }
11738
11739 static void
11740 s_cons (log_size)
11741      int log_size;
11742 {
11743   symbolS *label;
11744
11745   label = insn_labels != NULL ? insn_labels->label : NULL;
11746   mips_emit_delays (FALSE);
11747   if (log_size > 0 && auto_align)
11748     mips_align (log_size, 0, label);
11749   mips_clear_insn_labels ();
11750   cons (1 << log_size);
11751 }
11752
11753 static void
11754 s_float_cons (type)
11755      int type;
11756 {
11757   symbolS *label;
11758
11759   label = insn_labels != NULL ? insn_labels->label : NULL;
11760
11761   mips_emit_delays (FALSE);
11762
11763   if (auto_align)
11764     {
11765       if (type == 'd')
11766         mips_align (3, 0, label);
11767       else
11768         mips_align (2, 0, label);
11769     }
11770
11771   mips_clear_insn_labels ();
11772
11773   float_cons (type);
11774 }
11775
11776 /* Handle .globl.  We need to override it because on Irix 5 you are
11777    permitted to say
11778        .globl foo .text
11779    where foo is an undefined symbol, to mean that foo should be
11780    considered to be the address of a function.  */
11781
11782 static void
11783 s_mips_globl (x)
11784      int x ATTRIBUTE_UNUSED;
11785 {
11786   char *name;
11787   int c;
11788   symbolS *symbolP;
11789   flagword flag;
11790
11791   name = input_line_pointer;
11792   c = get_symbol_end ();
11793   symbolP = symbol_find_or_make (name);
11794   *input_line_pointer = c;
11795   SKIP_WHITESPACE ();
11796
11797   /* On Irix 5, every global symbol that is not explicitly labelled as
11798      being a function is apparently labelled as being an object.  */
11799   flag = BSF_OBJECT;
11800
11801   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11802     {
11803       char *secname;
11804       asection *sec;
11805
11806       secname = input_line_pointer;
11807       c = get_symbol_end ();
11808       sec = bfd_get_section_by_name (stdoutput, secname);
11809       if (sec == NULL)
11810         as_bad (_("%s: no such section"), secname);
11811       *input_line_pointer = c;
11812
11813       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11814         flag = BSF_FUNCTION;
11815     }
11816
11817   symbol_get_bfdsym (symbolP)->flags |= flag;
11818
11819   S_SET_EXTERNAL (symbolP);
11820   demand_empty_rest_of_line ();
11821 }
11822
11823 static void
11824 s_option (x)
11825      int x ATTRIBUTE_UNUSED;
11826 {
11827   char *opt;
11828   char c;
11829
11830   opt = input_line_pointer;
11831   c = get_symbol_end ();
11832
11833   if (*opt == 'O')
11834     {
11835       /* FIXME: What does this mean?  */
11836     }
11837   else if (strncmp (opt, "pic", 3) == 0)
11838     {
11839       int i;
11840
11841       i = atoi (opt + 3);
11842       if (i == 0)
11843         mips_pic = NO_PIC;
11844       else if (i == 2)
11845         mips_pic = SVR4_PIC;
11846       else
11847         as_bad (_(".option pic%d not supported"), i);
11848
11849       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11850         {
11851           if (g_switch_seen && g_switch_value != 0)
11852             as_warn (_("-G may not be used with SVR4 PIC code"));
11853           g_switch_value = 0;
11854           bfd_set_gp_size (stdoutput, 0);
11855         }
11856     }
11857   else
11858     as_warn (_("Unrecognized option \"%s\""), opt);
11859
11860   *input_line_pointer = c;
11861   demand_empty_rest_of_line ();
11862 }
11863
11864 /* This structure is used to hold a stack of .set values.  */
11865
11866 struct mips_option_stack
11867 {
11868   struct mips_option_stack *next;
11869   struct mips_set_options options;
11870 };
11871
11872 static struct mips_option_stack *mips_opts_stack;
11873
11874 /* Handle the .set pseudo-op.  */
11875
11876 static void
11877 s_mipsset (x)
11878      int x ATTRIBUTE_UNUSED;
11879 {
11880   char *name = input_line_pointer, ch;
11881
11882   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11883     ++input_line_pointer;
11884   ch = *input_line_pointer;
11885   *input_line_pointer = '\0';
11886
11887   if (strcmp (name, "reorder") == 0)
11888     {
11889       if (mips_opts.noreorder && prev_nop_frag != NULL)
11890         {
11891           /* If we still have pending nops, we can discard them.  The
11892              usual nop handling will insert any that are still
11893              needed.  */
11894           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11895                                     * (mips_opts.mips16 ? 2 : 4));
11896           prev_nop_frag = NULL;
11897         }
11898       mips_opts.noreorder = 0;
11899     }
11900   else if (strcmp (name, "noreorder") == 0)
11901     {
11902       mips_emit_delays (TRUE);
11903       mips_opts.noreorder = 1;
11904       mips_any_noreorder = 1;
11905     }
11906   else if (strcmp (name, "at") == 0)
11907     {
11908       mips_opts.noat = 0;
11909     }
11910   else if (strcmp (name, "noat") == 0)
11911     {
11912       mips_opts.noat = 1;
11913     }
11914   else if (strcmp (name, "macro") == 0)
11915     {
11916       mips_opts.warn_about_macros = 0;
11917     }
11918   else if (strcmp (name, "nomacro") == 0)
11919     {
11920       if (mips_opts.noreorder == 0)
11921         as_bad (_("`noreorder' must be set before `nomacro'"));
11922       mips_opts.warn_about_macros = 1;
11923     }
11924   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11925     {
11926       mips_opts.nomove = 0;
11927     }
11928   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11929     {
11930       mips_opts.nomove = 1;
11931     }
11932   else if (strcmp (name, "bopt") == 0)
11933     {
11934       mips_opts.nobopt = 0;
11935     }
11936   else if (strcmp (name, "nobopt") == 0)
11937     {
11938       mips_opts.nobopt = 1;
11939     }
11940   else if (strcmp (name, "mips16") == 0
11941            || strcmp (name, "MIPS-16") == 0)
11942     mips_opts.mips16 = 1;
11943   else if (strcmp (name, "nomips16") == 0
11944            || strcmp (name, "noMIPS-16") == 0)
11945     mips_opts.mips16 = 0;
11946   else if (strcmp (name, "mips3d") == 0)
11947     mips_opts.ase_mips3d = 1;
11948   else if (strcmp (name, "nomips3d") == 0)
11949     mips_opts.ase_mips3d = 0;
11950   else if (strcmp (name, "mdmx") == 0)
11951     mips_opts.ase_mdmx = 1;
11952   else if (strcmp (name, "nomdmx") == 0)
11953     mips_opts.ase_mdmx = 0;
11954   else if (strncmp (name, "mips", 4) == 0)
11955     {
11956       int isa;
11957
11958       /* Permit the user to change the ISA on the fly.  Needless to
11959          say, misuse can cause serious problems.  */
11960       isa = atoi (name + 4);
11961       switch (isa)
11962         {
11963         case  0:
11964           mips_opts.gp32 = file_mips_gp32;
11965           mips_opts.fp32 = file_mips_fp32;
11966           break;
11967         case  1:
11968         case  2:
11969         case 32:
11970           mips_opts.gp32 = 1;
11971           mips_opts.fp32 = 1;
11972           break;
11973         case  3:
11974         case  4:
11975         case  5:
11976         case 64:
11977           mips_opts.gp32 = 0;
11978           mips_opts.fp32 = 0;
11979           break;
11980         default:
11981           as_bad (_("unknown ISA level %s"), name + 4);
11982           break;
11983         }
11984
11985       switch (isa)
11986         {
11987         case  0: mips_opts.isa = file_mips_isa;   break;
11988         case  1: mips_opts.isa = ISA_MIPS1;       break;
11989         case  2: mips_opts.isa = ISA_MIPS2;       break;
11990         case  3: mips_opts.isa = ISA_MIPS3;       break;
11991         case  4: mips_opts.isa = ISA_MIPS4;       break;
11992         case  5: mips_opts.isa = ISA_MIPS5;       break;
11993         case 32: mips_opts.isa = ISA_MIPS32;      break;
11994         case 64: mips_opts.isa = ISA_MIPS64;      break;
11995         default: as_bad (_("unknown ISA level %s"), name + 4); break;
11996         }
11997     }
11998   else if (strcmp (name, "autoextend") == 0)
11999     mips_opts.noautoextend = 0;
12000   else if (strcmp (name, "noautoextend") == 0)
12001     mips_opts.noautoextend = 1;
12002   else if (strcmp (name, "push") == 0)
12003     {
12004       struct mips_option_stack *s;
12005
12006       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12007       s->next = mips_opts_stack;
12008       s->options = mips_opts;
12009       mips_opts_stack = s;
12010     }
12011   else if (strcmp (name, "pop") == 0)
12012     {
12013       struct mips_option_stack *s;
12014
12015       s = mips_opts_stack;
12016       if (s == NULL)
12017         as_bad (_(".set pop with no .set push"));
12018       else
12019         {
12020           /* If we're changing the reorder mode we need to handle
12021              delay slots correctly.  */
12022           if (s->options.noreorder && ! mips_opts.noreorder)
12023             mips_emit_delays (TRUE);
12024           else if (! s->options.noreorder && mips_opts.noreorder)
12025             {
12026               if (prev_nop_frag != NULL)
12027                 {
12028                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12029                                             * (mips_opts.mips16 ? 2 : 4));
12030                   prev_nop_frag = NULL;
12031                 }
12032             }
12033
12034           mips_opts = s->options;
12035           mips_opts_stack = s->next;
12036           free (s);
12037         }
12038     }
12039   else
12040     {
12041       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12042     }
12043   *input_line_pointer = ch;
12044   demand_empty_rest_of_line ();
12045 }
12046
12047 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12048    .option pic2.  It means to generate SVR4 PIC calls.  */
12049
12050 static void
12051 s_abicalls (ignore)
12052      int ignore ATTRIBUTE_UNUSED;
12053 {
12054   mips_pic = SVR4_PIC;
12055   if (USE_GLOBAL_POINTER_OPT)
12056     {
12057       if (g_switch_seen && g_switch_value != 0)
12058         as_warn (_("-G may not be used with SVR4 PIC code"));
12059       g_switch_value = 0;
12060     }
12061   bfd_set_gp_size (stdoutput, 0);
12062   demand_empty_rest_of_line ();
12063 }
12064
12065 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12066    PIC code.  It sets the $gp register for the function based on the
12067    function address, which is in the register named in the argument.
12068    This uses a relocation against _gp_disp, which is handled specially
12069    by the linker.  The result is:
12070         lui     $gp,%hi(_gp_disp)
12071         addiu   $gp,$gp,%lo(_gp_disp)
12072         addu    $gp,$gp,.cpload argument
12073    The .cpload argument is normally $25 == $t9.  */
12074
12075 static void
12076 s_cpload (ignore)
12077      int ignore ATTRIBUTE_UNUSED;
12078 {
12079   expressionS ex;
12080   int icnt = 0;
12081
12082   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12083      .cpload is ignored.  */
12084   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12085     {
12086       s_ignore (0);
12087       return;
12088     }
12089
12090   /* .cpload should be in a .set noreorder section.  */
12091   if (mips_opts.noreorder == 0)
12092     as_warn (_(".cpload not in noreorder section"));
12093
12094   ex.X_op = O_symbol;
12095   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12096   ex.X_op_symbol = NULL;
12097   ex.X_add_number = 0;
12098
12099   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12100   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12101
12102   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12103   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12104                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12105
12106   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12107                mips_gp_register, mips_gp_register, tc_get_register (0));
12108
12109   demand_empty_rest_of_line ();
12110 }
12111
12112 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12113      .cpsetup $reg1, offset|$reg2, label
12114
12115    If offset is given, this results in:
12116      sd         $gp, offset($sp)
12117      lui        $gp, %hi(%neg(%gp_rel(label)))
12118      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12119      daddu      $gp, $gp, $reg1
12120
12121    If $reg2 is given, this results in:
12122      daddu      $reg2, $gp, $0
12123      lui        $gp, %hi(%neg(%gp_rel(label)))
12124      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12125      daddu      $gp, $gp, $reg1
12126    $reg1 is normally $25 == $t9.  */
12127 static void
12128 s_cpsetup (ignore)
12129      int ignore ATTRIBUTE_UNUSED;
12130 {
12131   expressionS ex_off;
12132   expressionS ex_sym;
12133   int reg1;
12134   int icnt = 0;
12135   char *f;
12136
12137   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12138      We also need NewABI support.  */
12139   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12140     {
12141       s_ignore (0);
12142       return;
12143     }
12144
12145   reg1 = tc_get_register (0);
12146   SKIP_WHITESPACE ();
12147   if (*input_line_pointer != ',')
12148     {
12149       as_bad (_("missing argument separator ',' for .cpsetup"));
12150       return;
12151     }
12152   else
12153     ++input_line_pointer;
12154   SKIP_WHITESPACE ();
12155   if (*input_line_pointer == '$')
12156     {
12157       mips_cpreturn_register = tc_get_register (0);
12158       mips_cpreturn_offset = -1;
12159     }
12160   else
12161     {
12162       mips_cpreturn_offset = get_absolute_expression ();
12163       mips_cpreturn_register = -1;
12164     }
12165   SKIP_WHITESPACE ();
12166   if (*input_line_pointer != ',')
12167     {
12168       as_bad (_("missing argument separator ',' for .cpsetup"));
12169       return;
12170     }
12171   else
12172     ++input_line_pointer;
12173   SKIP_WHITESPACE ();
12174   expression (&ex_sym);
12175
12176   if (mips_cpreturn_register == -1)
12177     {
12178       ex_off.X_op = O_constant;
12179       ex_off.X_add_symbol = NULL;
12180       ex_off.X_op_symbol = NULL;
12181       ex_off.X_add_number = mips_cpreturn_offset;
12182
12183       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12184                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12185     }
12186   else
12187     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12188                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12189
12190   /* Ensure there's room for the next two instructions, so that `f'
12191      doesn't end up with an address in the wrong frag.  */
12192   frag_grow (8);
12193   f = frag_more (0);
12194   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12195                (int) BFD_RELOC_GPREL16);
12196   fix_new (frag_now, f - frag_now->fr_literal,
12197            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12198   fix_new (frag_now, f - frag_now->fr_literal,
12199            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12200
12201   f = frag_more (0);
12202   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12203                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12204   fix_new (frag_now, f - frag_now->fr_literal,
12205            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12206   fix_new (frag_now, f - frag_now->fr_literal,
12207            0, NULL, 0, 0, BFD_RELOC_LO16);
12208
12209   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12210                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12211                mips_gp_register, mips_gp_register, reg1);
12212
12213   demand_empty_rest_of_line ();
12214 }
12215
12216 static void
12217 s_cplocal (ignore)
12218      int ignore ATTRIBUTE_UNUSED;
12219 {
12220   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12221    .cplocal is ignored.  */
12222   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12223     {
12224       s_ignore (0);
12225       return;
12226     }
12227
12228   mips_gp_register = tc_get_register (0);
12229   demand_empty_rest_of_line ();
12230 }
12231
12232 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12233    offset from $sp.  The offset is remembered, and after making a PIC
12234    call $gp is restored from that location.  */
12235
12236 static void
12237 s_cprestore (ignore)
12238      int ignore ATTRIBUTE_UNUSED;
12239 {
12240   expressionS ex;
12241   int icnt = 0;
12242
12243   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12244      .cprestore is ignored.  */
12245   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12246     {
12247       s_ignore (0);
12248       return;
12249     }
12250
12251   mips_cprestore_offset = get_absolute_expression ();
12252   mips_cprestore_valid = 1;
12253
12254   ex.X_op = O_constant;
12255   ex.X_add_symbol = NULL;
12256   ex.X_op_symbol = NULL;
12257   ex.X_add_number = mips_cprestore_offset;
12258
12259   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12260                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12261                                 mips_gp_register, SP);
12262
12263   demand_empty_rest_of_line ();
12264 }
12265
12266 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12267    was given in the preceeding .gpsetup, it results in:
12268      ld         $gp, offset($sp)
12269
12270    If a register $reg2 was given there, it results in:
12271      daddiu     $gp, $gp, $reg2
12272  */
12273 static void
12274 s_cpreturn (ignore)
12275      int ignore ATTRIBUTE_UNUSED;
12276 {
12277   expressionS ex;
12278   int icnt = 0;
12279
12280   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12281      We also need NewABI support.  */
12282   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12283     {
12284       s_ignore (0);
12285       return;
12286     }
12287
12288   if (mips_cpreturn_register == -1)
12289     {
12290       ex.X_op = O_constant;
12291       ex.X_add_symbol = NULL;
12292       ex.X_op_symbol = NULL;
12293       ex.X_add_number = mips_cpreturn_offset;
12294
12295       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12296                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12297     }
12298   else
12299     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12300                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12301
12302   demand_empty_rest_of_line ();
12303 }
12304
12305 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12306    code.  It sets the offset to use in gp_rel relocations.  */
12307
12308 static void
12309 s_gpvalue (ignore)
12310      int ignore ATTRIBUTE_UNUSED;
12311 {
12312   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12313      We also need NewABI support.  */
12314   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12315     {
12316       s_ignore (0);
12317       return;
12318     }
12319
12320   mips_gprel_offset = get_absolute_expression ();
12321
12322   demand_empty_rest_of_line ();
12323 }
12324
12325 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12326    code.  It generates a 32 bit GP relative reloc.  */
12327
12328 static void
12329 s_gpword (ignore)
12330      int ignore ATTRIBUTE_UNUSED;
12331 {
12332   symbolS *label;
12333   expressionS ex;
12334   char *p;
12335
12336   /* When not generating PIC code, this is treated as .word.  */
12337   if (mips_pic != SVR4_PIC)
12338     {
12339       s_cons (2);
12340       return;
12341     }
12342
12343   label = insn_labels != NULL ? insn_labels->label : NULL;
12344   mips_emit_delays (TRUE);
12345   if (auto_align)
12346     mips_align (2, 0, label);
12347   mips_clear_insn_labels ();
12348
12349   expression (&ex);
12350
12351   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12352     {
12353       as_bad (_("Unsupported use of .gpword"));
12354       ignore_rest_of_line ();
12355     }
12356
12357   p = frag_more (4);
12358   md_number_to_chars (p, (valueT) 0, 4);
12359   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12360                BFD_RELOC_GPREL32);
12361
12362   demand_empty_rest_of_line ();
12363 }
12364
12365 static void
12366 s_gpdword (ignore)
12367      int ignore ATTRIBUTE_UNUSED;
12368 {
12369   symbolS *label;
12370   expressionS ex;
12371   char *p;
12372
12373   /* When not generating PIC code, this is treated as .dword.  */
12374   if (mips_pic != SVR4_PIC)
12375     {
12376       s_cons (3);
12377       return;
12378     }
12379
12380   label = insn_labels != NULL ? insn_labels->label : NULL;
12381   mips_emit_delays (TRUE);
12382   if (auto_align)
12383     mips_align (3, 0, label);
12384   mips_clear_insn_labels ();
12385
12386   expression (&ex);
12387
12388   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12389     {
12390       as_bad (_("Unsupported use of .gpdword"));
12391       ignore_rest_of_line ();
12392     }
12393
12394   p = frag_more (8);
12395   md_number_to_chars (p, (valueT) 0, 8);
12396   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12397                BFD_RELOC_GPREL32);
12398
12399   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12400   ex.X_op = O_absent;
12401   ex.X_add_symbol = 0;
12402   ex.X_add_number = 0;
12403   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12404                BFD_RELOC_64);
12405
12406   demand_empty_rest_of_line ();
12407 }
12408
12409 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12410    tables in SVR4 PIC code.  */
12411
12412 static void
12413 s_cpadd (ignore)
12414      int ignore ATTRIBUTE_UNUSED;
12415 {
12416   int icnt = 0;
12417   int reg;
12418
12419   /* This is ignored when not generating SVR4 PIC code.  */
12420   if (mips_pic != SVR4_PIC)
12421     {
12422       s_ignore (0);
12423       return;
12424     }
12425
12426   /* Add $gp to the register named as an argument.  */
12427   reg = tc_get_register (0);
12428   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12429                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12430                "d,v,t", reg, reg, mips_gp_register);
12431
12432   demand_empty_rest_of_line ();
12433 }
12434
12435 /* Handle the .insn pseudo-op.  This marks instruction labels in
12436    mips16 mode.  This permits the linker to handle them specially,
12437    such as generating jalx instructions when needed.  We also make
12438    them odd for the duration of the assembly, in order to generate the
12439    right sort of code.  We will make them even in the adjust_symtab
12440    routine, while leaving them marked.  This is convenient for the
12441    debugger and the disassembler.  The linker knows to make them odd
12442    again.  */
12443
12444 static void
12445 s_insn (ignore)
12446      int ignore ATTRIBUTE_UNUSED;
12447 {
12448   mips16_mark_labels ();
12449
12450   demand_empty_rest_of_line ();
12451 }
12452
12453 /* Handle a .stabn directive.  We need these in order to mark a label
12454    as being a mips16 text label correctly.  Sometimes the compiler
12455    will emit a label, followed by a .stabn, and then switch sections.
12456    If the label and .stabn are in mips16 mode, then the label is
12457    really a mips16 text label.  */
12458
12459 static void
12460 s_mips_stab (type)
12461      int type;
12462 {
12463   if (type == 'n')
12464     mips16_mark_labels ();
12465
12466   s_stab (type);
12467 }
12468
12469 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12470  */
12471
12472 static void
12473 s_mips_weakext (ignore)
12474      int ignore ATTRIBUTE_UNUSED;
12475 {
12476   char *name;
12477   int c;
12478   symbolS *symbolP;
12479   expressionS exp;
12480
12481   name = input_line_pointer;
12482   c = get_symbol_end ();
12483   symbolP = symbol_find_or_make (name);
12484   S_SET_WEAK (symbolP);
12485   *input_line_pointer = c;
12486
12487   SKIP_WHITESPACE ();
12488
12489   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12490     {
12491       if (S_IS_DEFINED (symbolP))
12492         {
12493           as_bad ("ignoring attempt to redefine symbol %s",
12494                   S_GET_NAME (symbolP));
12495           ignore_rest_of_line ();
12496           return;
12497         }
12498
12499       if (*input_line_pointer == ',')
12500         {
12501           ++input_line_pointer;
12502           SKIP_WHITESPACE ();
12503         }
12504
12505       expression (&exp);
12506       if (exp.X_op != O_symbol)
12507         {
12508           as_bad ("bad .weakext directive");
12509           ignore_rest_of_line ();
12510           return;
12511         }
12512       symbol_set_value_expression (symbolP, &exp);
12513     }
12514
12515   demand_empty_rest_of_line ();
12516 }
12517
12518 /* Parse a register string into a number.  Called from the ECOFF code
12519    to parse .frame.  The argument is non-zero if this is the frame
12520    register, so that we can record it in mips_frame_reg.  */
12521
12522 int
12523 tc_get_register (frame)
12524      int frame;
12525 {
12526   int reg;
12527
12528   SKIP_WHITESPACE ();
12529   if (*input_line_pointer++ != '$')
12530     {
12531       as_warn (_("expected `$'"));
12532       reg = ZERO;
12533     }
12534   else if (ISDIGIT (*input_line_pointer))
12535     {
12536       reg = get_absolute_expression ();
12537       if (reg < 0 || reg >= 32)
12538         {
12539           as_warn (_("Bad register number"));
12540           reg = ZERO;
12541         }
12542     }
12543   else
12544     {
12545       if (strncmp (input_line_pointer, "ra", 2) == 0)
12546         {
12547           reg = RA;
12548           input_line_pointer += 2;
12549         }
12550       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12551         {
12552           reg = FP;
12553           input_line_pointer += 2;
12554         }
12555       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12556         {
12557           reg = SP;
12558           input_line_pointer += 2;
12559         }
12560       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12561         {
12562           reg = GP;
12563           input_line_pointer += 2;
12564         }
12565       else if (strncmp (input_line_pointer, "at", 2) == 0)
12566         {
12567           reg = AT;
12568           input_line_pointer += 2;
12569         }
12570       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12571         {
12572           reg = KT0;
12573           input_line_pointer += 3;
12574         }
12575       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12576         {
12577           reg = KT1;
12578           input_line_pointer += 3;
12579         }
12580       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12581         {
12582           reg = ZERO;
12583           input_line_pointer += 4;
12584         }
12585       else
12586         {
12587           as_warn (_("Unrecognized register name"));
12588           reg = ZERO;
12589           while (ISALNUM(*input_line_pointer))
12590            input_line_pointer++;
12591         }
12592     }
12593   if (frame)
12594     {
12595       mips_frame_reg = reg != 0 ? reg : SP;
12596       mips_frame_reg_valid = 1;
12597       mips_cprestore_valid = 0;
12598     }
12599   return reg;
12600 }
12601
12602 valueT
12603 md_section_align (seg, addr)
12604      asection *seg;
12605      valueT addr;
12606 {
12607   int align = bfd_get_section_alignment (stdoutput, seg);
12608
12609 #ifdef OBJ_ELF
12610   /* We don't need to align ELF sections to the full alignment.
12611      However, Irix 5 may prefer that we align them at least to a 16
12612      byte boundary.  We don't bother to align the sections if we are
12613      targeted for an embedded system.  */
12614   if (strcmp (TARGET_OS, "elf") == 0)
12615     return addr;
12616   if (align > 4)
12617     align = 4;
12618 #endif
12619
12620   return ((addr + (1 << align) - 1) & (-1 << align));
12621 }
12622
12623 /* Utility routine, called from above as well.  If called while the
12624    input file is still being read, it's only an approximation.  (For
12625    example, a symbol may later become defined which appeared to be
12626    undefined earlier.)  */
12627
12628 static int
12629 nopic_need_relax (sym, before_relaxing)
12630      symbolS *sym;
12631      int before_relaxing;
12632 {
12633   if (sym == 0)
12634     return 0;
12635
12636   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12637     {
12638       const char *symname;
12639       int change;
12640
12641       /* Find out whether this symbol can be referenced off the $gp
12642          register.  It can be if it is smaller than the -G size or if
12643          it is in the .sdata or .sbss section.  Certain symbols can
12644          not be referenced off the $gp, although it appears as though
12645          they can.  */
12646       symname = S_GET_NAME (sym);
12647       if (symname != (const char *) NULL
12648           && (strcmp (symname, "eprol") == 0
12649               || strcmp (symname, "etext") == 0
12650               || strcmp (symname, "_gp") == 0
12651               || strcmp (symname, "edata") == 0
12652               || strcmp (symname, "_fbss") == 0
12653               || strcmp (symname, "_fdata") == 0
12654               || strcmp (symname, "_ftext") == 0
12655               || strcmp (symname, "end") == 0
12656               || strcmp (symname, "_gp_disp") == 0))
12657         change = 1;
12658       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12659                && (0
12660 #ifndef NO_ECOFF_DEBUGGING
12661                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12662                        && (symbol_get_obj (sym)->ecoff_extern_size
12663                            <= g_switch_value))
12664 #endif
12665                    /* We must defer this decision until after the whole
12666                       file has been read, since there might be a .extern
12667                       after the first use of this symbol.  */
12668                    || (before_relaxing
12669 #ifndef NO_ECOFF_DEBUGGING
12670                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12671 #endif
12672                        && S_GET_VALUE (sym) == 0)
12673                    || (S_GET_VALUE (sym) != 0
12674                        && S_GET_VALUE (sym) <= g_switch_value)))
12675         change = 0;
12676       else
12677         {
12678           const char *segname;
12679
12680           segname = segment_name (S_GET_SEGMENT (sym));
12681           assert (strcmp (segname, ".lit8") != 0
12682                   && strcmp (segname, ".lit4") != 0);
12683           change = (strcmp (segname, ".sdata") != 0
12684                     && strcmp (segname, ".sbss") != 0
12685                     && strncmp (segname, ".sdata.", 7) != 0
12686                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12687         }
12688       return change;
12689     }
12690   else
12691     /* We are not optimizing for the $gp register.  */
12692     return 1;
12693 }
12694
12695 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12696    extended opcode.  SEC is the section the frag is in.  */
12697
12698 static int
12699 mips16_extended_frag (fragp, sec, stretch)
12700      fragS *fragp;
12701      asection *sec;
12702      long stretch;
12703 {
12704   int type;
12705   register const struct mips16_immed_operand *op;
12706   offsetT val;
12707   int mintiny, maxtiny;
12708   segT symsec;
12709   fragS *sym_frag;
12710
12711   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12712     return 0;
12713   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12714     return 1;
12715
12716   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12717   op = mips16_immed_operands;
12718   while (op->type != type)
12719     {
12720       ++op;
12721       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12722     }
12723
12724   if (op->unsp)
12725     {
12726       if (type == '<' || type == '>' || type == '[' || type == ']')
12727         {
12728           mintiny = 1;
12729           maxtiny = 1 << op->nbits;
12730         }
12731       else
12732         {
12733           mintiny = 0;
12734           maxtiny = (1 << op->nbits) - 1;
12735         }
12736     }
12737   else
12738     {
12739       mintiny = - (1 << (op->nbits - 1));
12740       maxtiny = (1 << (op->nbits - 1)) - 1;
12741     }
12742
12743   sym_frag = symbol_get_frag (fragp->fr_symbol);
12744   val = S_GET_VALUE (fragp->fr_symbol);
12745   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12746
12747   if (op->pcrel)
12748     {
12749       addressT addr;
12750
12751       /* We won't have the section when we are called from
12752          mips_relax_frag.  However, we will always have been called
12753          from md_estimate_size_before_relax first.  If this is a
12754          branch to a different section, we mark it as such.  If SEC is
12755          NULL, and the frag is not marked, then it must be a branch to
12756          the same section.  */
12757       if (sec == NULL)
12758         {
12759           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12760             return 1;
12761         }
12762       else
12763         {
12764           /* Must have been called from md_estimate_size_before_relax.  */
12765           if (symsec != sec)
12766             {
12767               fragp->fr_subtype =
12768                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12769
12770               /* FIXME: We should support this, and let the linker
12771                  catch branches and loads that are out of range.  */
12772               as_bad_where (fragp->fr_file, fragp->fr_line,
12773                             _("unsupported PC relative reference to different section"));
12774
12775               return 1;
12776             }
12777           if (fragp != sym_frag && sym_frag->fr_address == 0)
12778             /* Assume non-extended on the first relaxation pass.
12779                The address we have calculated will be bogus if this is
12780                a forward branch to another frag, as the forward frag
12781                will have fr_address == 0.  */
12782             return 0;
12783         }
12784
12785       /* In this case, we know for sure that the symbol fragment is in
12786          the same section.  If the relax_marker of the symbol fragment
12787          differs from the relax_marker of this fragment, we have not
12788          yet adjusted the symbol fragment fr_address.  We want to add
12789          in STRETCH in order to get a better estimate of the address.
12790          This particularly matters because of the shift bits.  */
12791       if (stretch != 0
12792           && sym_frag->relax_marker != fragp->relax_marker)
12793         {
12794           fragS *f;
12795
12796           /* Adjust stretch for any alignment frag.  Note that if have
12797              been expanding the earlier code, the symbol may be
12798              defined in what appears to be an earlier frag.  FIXME:
12799              This doesn't handle the fr_subtype field, which specifies
12800              a maximum number of bytes to skip when doing an
12801              alignment.  */
12802           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12803             {
12804               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12805                 {
12806                   if (stretch < 0)
12807                     stretch = - ((- stretch)
12808                                  & ~ ((1 << (int) f->fr_offset) - 1));
12809                   else
12810                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12811                   if (stretch == 0)
12812                     break;
12813                 }
12814             }
12815           if (f != NULL)
12816             val += stretch;
12817         }
12818
12819       addr = fragp->fr_address + fragp->fr_fix;
12820
12821       /* The base address rules are complicated.  The base address of
12822          a branch is the following instruction.  The base address of a
12823          PC relative load or add is the instruction itself, but if it
12824          is in a delay slot (in which case it can not be extended) use
12825          the address of the instruction whose delay slot it is in.  */
12826       if (type == 'p' || type == 'q')
12827         {
12828           addr += 2;
12829
12830           /* If we are currently assuming that this frag should be
12831              extended, then, the current address is two bytes
12832              higher.  */
12833           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12834             addr += 2;
12835
12836           /* Ignore the low bit in the target, since it will be set
12837              for a text label.  */
12838           if ((val & 1) != 0)
12839             --val;
12840         }
12841       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12842         addr -= 4;
12843       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12844         addr -= 2;
12845
12846       val -= addr & ~ ((1 << op->shift) - 1);
12847
12848       /* Branch offsets have an implicit 0 in the lowest bit.  */
12849       if (type == 'p' || type == 'q')
12850         val /= 2;
12851
12852       /* If any of the shifted bits are set, we must use an extended
12853          opcode.  If the address depends on the size of this
12854          instruction, this can lead to a loop, so we arrange to always
12855          use an extended opcode.  We only check this when we are in
12856          the main relaxation loop, when SEC is NULL.  */
12857       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12858         {
12859           fragp->fr_subtype =
12860             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12861           return 1;
12862         }
12863
12864       /* If we are about to mark a frag as extended because the value
12865          is precisely maxtiny + 1, then there is a chance of an
12866          infinite loop as in the following code:
12867              la $4,foo
12868              .skip      1020
12869              .align     2
12870            foo:
12871          In this case when the la is extended, foo is 0x3fc bytes
12872          away, so the la can be shrunk, but then foo is 0x400 away, so
12873          the la must be extended.  To avoid this loop, we mark the
12874          frag as extended if it was small, and is about to become
12875          extended with a value of maxtiny + 1.  */
12876       if (val == ((maxtiny + 1) << op->shift)
12877           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12878           && sec == NULL)
12879         {
12880           fragp->fr_subtype =
12881             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12882           return 1;
12883         }
12884     }
12885   else if (symsec != absolute_section && sec != NULL)
12886     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12887
12888   if ((val & ((1 << op->shift) - 1)) != 0
12889       || val < (mintiny << op->shift)
12890       || val > (maxtiny << op->shift))
12891     return 1;
12892   else
12893     return 0;
12894 }
12895
12896 /* Compute the length of a branch sequence, and adjust the
12897    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12898    worst-case length is computed, with UPDATE being used to indicate
12899    whether an unconditional (-1), branch-likely (+1) or regular (0)
12900    branch is to be computed.  */
12901 static int
12902 relaxed_branch_length (fragp, sec, update)
12903      fragS *fragp;
12904      asection *sec;
12905      int update;
12906 {
12907   bfd_boolean toofar;
12908   int length;
12909
12910   if (fragp
12911       && S_IS_DEFINED (fragp->fr_symbol)
12912       && sec == S_GET_SEGMENT (fragp->fr_symbol))
12913     {
12914       addressT addr;
12915       offsetT val;
12916
12917       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12918
12919       addr = fragp->fr_address + fragp->fr_fix + 4;
12920
12921       val -= addr;
12922
12923       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12924     }
12925   else if (fragp)
12926     /* If the symbol is not defined or it's in a different segment,
12927        assume the user knows what's going on and emit a short
12928        branch.  */
12929     toofar = FALSE;
12930   else
12931     toofar = TRUE;
12932
12933   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12934     fragp->fr_subtype
12935       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12936                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12937                              RELAX_BRANCH_LINK (fragp->fr_subtype),
12938                              toofar);
12939
12940   length = 4;
12941   if (toofar)
12942     {
12943       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12944         length += 8;
12945
12946       if (mips_pic != NO_PIC)
12947         {
12948           /* Additional space for PIC loading of target address.  */
12949           length += 8;
12950           if (mips_opts.isa == ISA_MIPS1)
12951             /* Additional space for $at-stabilizing nop.  */
12952             length += 4;
12953         }
12954
12955       /* If branch is conditional.  */
12956       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12957         length += 8;
12958     }
12959
12960   return length;
12961 }
12962
12963 /* Estimate the size of a frag before relaxing.  Unless this is the
12964    mips16, we are not really relaxing here, and the final size is
12965    encoded in the subtype information.  For the mips16, we have to
12966    decide whether we are using an extended opcode or not.  */
12967
12968 int
12969 md_estimate_size_before_relax (fragp, segtype)
12970      fragS *fragp;
12971      asection *segtype;
12972 {
12973   int change = 0;
12974   bfd_boolean linkonce = FALSE;
12975
12976   if (RELAX_BRANCH_P (fragp->fr_subtype))
12977     {
12978
12979       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12980
12981       return fragp->fr_var;
12982     }
12983
12984   if (RELAX_MIPS16_P (fragp->fr_subtype))
12985     /* We don't want to modify the EXTENDED bit here; it might get us
12986        into infinite loops.  We change it only in mips_relax_frag().  */
12987     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12988
12989   if (mips_pic == NO_PIC)
12990     {
12991       change = nopic_need_relax (fragp->fr_symbol, 0);
12992     }
12993   else if (mips_pic == SVR4_PIC)
12994     {
12995       symbolS *sym;
12996       asection *symsec;
12997
12998       sym = fragp->fr_symbol;
12999
13000       /* Handle the case of a symbol equated to another symbol.  */
13001       while (symbol_equated_reloc_p (sym))
13002         {
13003           symbolS *n;
13004
13005           /* It's possible to get a loop here in a badly written
13006              program.  */
13007           n = symbol_get_value_expression (sym)->X_add_symbol;
13008           if (n == sym)
13009             break;
13010           sym = n;
13011         }
13012
13013       symsec = S_GET_SEGMENT (sym);
13014
13015       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13016       if (symsec != segtype && ! S_IS_LOCAL (sym))
13017         {
13018           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13019               != 0)
13020             linkonce = TRUE;
13021
13022           /* The GNU toolchain uses an extension for ELF: a section
13023              beginning with the magic string .gnu.linkonce is a linkonce
13024              section.  */
13025           if (strncmp (segment_name (symsec), ".gnu.linkonce",
13026                        sizeof ".gnu.linkonce" - 1) == 0)
13027             linkonce = TRUE;
13028         }
13029
13030       /* This must duplicate the test in adjust_reloc_syms.  */
13031       change = (symsec != &bfd_und_section
13032                 && symsec != &bfd_abs_section
13033                 && ! bfd_is_com_section (symsec)
13034                 && !linkonce
13035 #ifdef OBJ_ELF
13036                 /* A global or weak symbol is treated as external.  */
13037                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13038                     || (! S_IS_WEAK (sym)
13039                         && (! S_IS_EXTERNAL (sym)
13040                             || mips_pic == EMBEDDED_PIC)))
13041 #endif
13042                 );
13043     }
13044   else
13045     abort ();
13046
13047   if (change)
13048     {
13049       /* Record the offset to the first reloc in the fr_opcode field.
13050          This lets md_convert_frag and tc_gen_reloc know that the code
13051          must be expanded.  */
13052       fragp->fr_opcode = (fragp->fr_literal
13053                           + fragp->fr_fix
13054                           - RELAX_OLD (fragp->fr_subtype)
13055                           + RELAX_RELOC1 (fragp->fr_subtype));
13056       /* FIXME: This really needs as_warn_where.  */
13057       if (RELAX_WARN (fragp->fr_subtype))
13058         as_warn (_("AT used after \".set noat\" or macro used after "
13059                    "\".set nomacro\""));
13060
13061       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13062     }
13063
13064   return 0;
13065 }
13066
13067 /* This is called to see whether a reloc against a defined symbol
13068    should be converted into a reloc against a section.  Don't adjust
13069    MIPS16 jump relocations, so we don't have to worry about the format
13070    of the offset in the .o file.  Don't adjust relocations against
13071    mips16 symbols, so that the linker can find them if it needs to set
13072    up a stub.  */
13073
13074 int
13075 mips_fix_adjustable (fixp)
13076      fixS *fixp;
13077 {
13078   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13079     return 0;
13080
13081   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13082       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13083     return 0;
13084
13085   if (fixp->fx_addsy == NULL)
13086     return 1;
13087
13088 #ifdef OBJ_ELF
13089   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13090       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13091       && fixp->fx_subsy == NULL)
13092     return 0;
13093 #endif
13094
13095   return 1;
13096 }
13097
13098 /* Translate internal representation of relocation info to BFD target
13099    format.  */
13100
13101 arelent **
13102 tc_gen_reloc (section, fixp)
13103      asection *section ATTRIBUTE_UNUSED;
13104      fixS *fixp;
13105 {
13106   static arelent *retval[4];
13107   arelent *reloc;
13108   bfd_reloc_code_real_type code;
13109
13110   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13111   retval[1] = NULL;
13112
13113   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13114   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13115   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13116
13117   if (mips_pic == EMBEDDED_PIC
13118       && SWITCH_TABLE (fixp))
13119     {
13120       /* For a switch table entry we use a special reloc.  The addend
13121          is actually the difference between the reloc address and the
13122          subtrahend.  */
13123       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13124       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13125         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13126       fixp->fx_r_type = BFD_RELOC_GPREL32;
13127     }
13128   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13129     {
13130       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13131         reloc->addend = fixp->fx_addnumber;
13132       else
13133         {
13134           /* We use a special addend for an internal RELLO reloc.  */
13135           if (symbol_section_p (fixp->fx_addsy))
13136             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13137           else
13138             reloc->addend = fixp->fx_addnumber + reloc->address;
13139         }
13140     }
13141   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13142     {
13143       assert (fixp->fx_next != NULL
13144               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13145
13146       /* The reloc is relative to the RELLO; adjust the addend
13147          accordingly.  */
13148       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13149         reloc->addend = fixp->fx_next->fx_addnumber;
13150       else
13151         {
13152           /* We use a special addend for an internal RELHI reloc.  */
13153           if (symbol_section_p (fixp->fx_addsy))
13154             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13155                              + fixp->fx_next->fx_where
13156                              - S_GET_VALUE (fixp->fx_subsy));
13157           else
13158             reloc->addend = (fixp->fx_addnumber
13159                              + fixp->fx_next->fx_frag->fr_address
13160                              + fixp->fx_next->fx_where);
13161         }
13162     }
13163   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13164     reloc->addend = fixp->fx_addnumber;
13165   else
13166     {
13167       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13168         /* A gruesome hack which is a result of the gruesome gas reloc
13169            handling.  */
13170         reloc->addend = reloc->address;
13171       else
13172         reloc->addend = -reloc->address;
13173     }
13174
13175   /* If this is a variant frag, we may need to adjust the existing
13176      reloc and generate a new one.  */
13177   if (fixp->fx_frag->fr_opcode != NULL
13178       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13179            && ! HAVE_NEWABI)
13180           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13181           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13182           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13183           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13184           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13185           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13186     )
13187     {
13188       arelent *reloc2;
13189
13190       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13191
13192       /* If this is not the last reloc in this frag, then we have two
13193          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13194          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13195          the second one handle all of them.  */
13196       if (fixp->fx_next != NULL
13197           && fixp->fx_frag == fixp->fx_next->fx_frag)
13198         {
13199           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13200                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13201                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13202                       && (fixp->fx_next->fx_r_type
13203                           == BFD_RELOC_MIPS_GOT_LO16))
13204                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13205                       && (fixp->fx_next->fx_r_type
13206                           == BFD_RELOC_MIPS_CALL_LO16)));
13207           retval[0] = NULL;
13208           return retval;
13209         }
13210
13211       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13212       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13213       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13214       retval[2] = NULL;
13215       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13216       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13217       reloc2->address = (reloc->address
13218                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13219                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13220       reloc2->addend = fixp->fx_addnumber;
13221       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13222       assert (reloc2->howto != NULL);
13223
13224       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13225         {
13226           arelent *reloc3;
13227
13228           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13229           retval[3] = NULL;
13230           *reloc3 = *reloc2;
13231           reloc3->address += 4;
13232         }
13233
13234       if (mips_pic == NO_PIC)
13235         {
13236           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13237           fixp->fx_r_type = BFD_RELOC_HI16_S;
13238         }
13239       else if (mips_pic == SVR4_PIC)
13240         {
13241           switch (fixp->fx_r_type)
13242             {
13243             default:
13244               abort ();
13245             case BFD_RELOC_MIPS_GOT16:
13246               break;
13247             case BFD_RELOC_MIPS_GOT_LO16:
13248             case BFD_RELOC_MIPS_CALL_LO16:
13249               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13250               break;
13251             case BFD_RELOC_MIPS_CALL16:
13252               if (HAVE_NEWABI)
13253                 {
13254                   /* BFD_RELOC_MIPS_GOT16;*/
13255                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13256                   reloc2->howto = bfd_reloc_type_lookup
13257                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13258                 }
13259               else
13260                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13261               break;
13262             }
13263         }
13264       else
13265         abort ();
13266
13267       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13268       if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13269         {
13270           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13271           retval[1] = NULL;
13272         }
13273     }
13274
13275   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13276      entry to be used in the relocation's section offset.  */
13277   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13278     {
13279       reloc->address = reloc->addend;
13280       reloc->addend = 0;
13281     }
13282
13283   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13284      fixup_segment converted a non-PC relative reloc into a PC
13285      relative reloc.  In such a case, we need to convert the reloc
13286      code.  */
13287   code = fixp->fx_r_type;
13288   if (fixp->fx_pcrel)
13289     {
13290       switch (code)
13291         {
13292         case BFD_RELOC_8:
13293           code = BFD_RELOC_8_PCREL;
13294           break;
13295         case BFD_RELOC_16:
13296           code = BFD_RELOC_16_PCREL;
13297           break;
13298         case BFD_RELOC_32:
13299           code = BFD_RELOC_32_PCREL;
13300           break;
13301         case BFD_RELOC_64:
13302           code = BFD_RELOC_64_PCREL;
13303           break;
13304         case BFD_RELOC_8_PCREL:
13305         case BFD_RELOC_16_PCREL:
13306         case BFD_RELOC_32_PCREL:
13307         case BFD_RELOC_64_PCREL:
13308         case BFD_RELOC_16_PCREL_S2:
13309         case BFD_RELOC_PCREL_HI16_S:
13310         case BFD_RELOC_PCREL_LO16:
13311           break;
13312         default:
13313           as_bad_where (fixp->fx_file, fixp->fx_line,
13314                         _("Cannot make %s relocation PC relative"),
13315                         bfd_get_reloc_code_name (code));
13316         }
13317     }
13318
13319 #ifdef OBJ_ELF
13320   /* md_apply_fix3 has a double-subtraction hack to get
13321      bfd_install_relocation to behave nicely.  GPREL relocations are
13322      handled correctly without this hack, so undo it here.  We can't
13323      stop md_apply_fix3 from subtracting twice in the first place since
13324      the fake addend is required for variant frags above.  */
13325   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13326       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13327       && reloc->addend != 0
13328       && mips_need_elf_addend_fixup (fixp))
13329     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13330 #endif
13331
13332   /* To support a PC relative reloc when generating embedded PIC code
13333      for ECOFF, we use a Cygnus extension.  We check for that here to
13334      make sure that we don't let such a reloc escape normally.  */
13335   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13336        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13337       && code == BFD_RELOC_16_PCREL_S2
13338       && mips_pic != EMBEDDED_PIC)
13339     reloc->howto = NULL;
13340   else
13341     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13342
13343   if (reloc->howto == NULL)
13344     {
13345       as_bad_where (fixp->fx_file, fixp->fx_line,
13346                     _("Can not represent %s relocation in this object file format"),
13347                     bfd_get_reloc_code_name (code));
13348       retval[0] = NULL;
13349     }
13350
13351   return retval;
13352 }
13353
13354 /* Relax a machine dependent frag.  This returns the amount by which
13355    the current size of the frag should change.  */
13356
13357 int
13358 mips_relax_frag (sec, fragp, stretch)
13359      asection *sec;
13360      fragS *fragp;
13361      long stretch;
13362 {
13363   if (RELAX_BRANCH_P (fragp->fr_subtype))
13364     {
13365       offsetT old_var = fragp->fr_var;
13366
13367       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13368
13369       return fragp->fr_var - old_var;
13370     }
13371
13372   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13373     return 0;
13374
13375   if (mips16_extended_frag (fragp, NULL, stretch))
13376     {
13377       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13378         return 0;
13379       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13380       return 2;
13381     }
13382   else
13383     {
13384       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13385         return 0;
13386       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13387       return -2;
13388     }
13389
13390   return 0;
13391 }
13392
13393 /* Convert a machine dependent frag.  */
13394
13395 void
13396 md_convert_frag (abfd, asec, fragp)
13397      bfd *abfd ATTRIBUTE_UNUSED;
13398      segT asec;
13399      fragS *fragp;
13400 {
13401   int old, new;
13402   char *fixptr;
13403
13404   if (RELAX_BRANCH_P (fragp->fr_subtype))
13405     {
13406       bfd_byte *buf;
13407       unsigned long insn;
13408       expressionS exp;
13409       fixS *fixp;
13410
13411       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13412
13413       if (target_big_endian)
13414         insn = bfd_getb32 (buf);
13415       else
13416         insn = bfd_getl32 (buf);
13417
13418       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13419         {
13420           /* We generate a fixup instead of applying it right now
13421              because, if there are linker relaxations, we're going to
13422              need the relocations.  */
13423           exp.X_op = O_symbol;
13424           exp.X_add_symbol = fragp->fr_symbol;
13425           exp.X_add_number = fragp->fr_offset;
13426
13427           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13428                               4, &exp, 1,
13429                               BFD_RELOC_16_PCREL_S2);
13430           fixp->fx_file = fragp->fr_file;
13431           fixp->fx_line = fragp->fr_line;
13432
13433           md_number_to_chars ((char *)buf, insn, 4);
13434           buf += 4;
13435         }
13436       else
13437         {
13438           int i;
13439
13440           as_warn_where (fragp->fr_file, fragp->fr_line,
13441                          _("relaxed out-of-range branch into a jump"));
13442
13443           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13444             goto uncond;
13445
13446           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13447             {
13448               /* Reverse the branch.  */
13449               switch ((insn >> 28) & 0xf)
13450                 {
13451                 case 4:
13452                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13453                      have the condition reversed by tweaking a single
13454                      bit, and their opcodes all have 0x4???????.  */
13455                   assert ((insn & 0xf1000000) == 0x41000000);
13456                   insn ^= 0x00010000;
13457                   break;
13458
13459                 case 0:
13460                   /* bltz       0x04000000      bgez    0x04010000
13461                      bltzal     0x04100000      bgezal  0x04110000 */
13462                   assert ((insn & 0xfc0e0000) == 0x04000000);
13463                   insn ^= 0x00010000;
13464                   break;
13465
13466                 case 1:
13467                   /* beq        0x10000000      bne     0x14000000
13468                      blez       0x18000000      bgtz    0x1c000000 */
13469                   insn ^= 0x04000000;
13470                   break;
13471
13472                 default:
13473                   abort ();
13474                 }
13475             }
13476
13477           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13478             {
13479               /* Clear the and-link bit.  */
13480               assert ((insn & 0xfc1c0000) == 0x04100000);
13481
13482               /* bltzal 0x04100000      bgezal  0x04110000
13483                 bltzall 0x04120000     bgezall  0x04130000 */
13484               insn &= ~0x00100000;
13485             }
13486
13487           /* Branch over the branch (if the branch was likely) or the
13488              full jump (not likely case).  Compute the offset from the
13489              current instruction to branch to.  */
13490           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13491             i = 16;
13492           else
13493             {
13494               /* How many bytes in instructions we've already emitted?  */
13495               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13496               /* How many bytes in instructions from here to the end?  */
13497               i = fragp->fr_var - i;
13498             }
13499           /* Convert to instruction count.  */
13500           i >>= 2;
13501           /* Branch counts from the next instruction.  */
13502           i--;
13503           insn |= i;
13504           /* Branch over the jump.  */
13505           md_number_to_chars ((char *)buf, insn, 4);
13506           buf += 4;
13507
13508           /* Nop */
13509           md_number_to_chars ((char*)buf, 0, 4);
13510           buf += 4;
13511
13512           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13513             {
13514               /* beql $0, $0, 2f */
13515               insn = 0x50000000;
13516               /* Compute the PC offset from the current instruction to
13517                  the end of the variable frag.  */
13518               /* How many bytes in instructions we've already emitted?  */
13519               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13520               /* How many bytes in instructions from here to the end?  */
13521               i = fragp->fr_var - i;
13522               /* Convert to instruction count.  */
13523               i >>= 2;
13524               /* Don't decrement i, because we want to branch over the
13525                  delay slot.  */
13526
13527               insn |= i;
13528               md_number_to_chars ((char *)buf, insn, 4);
13529               buf += 4;
13530
13531               md_number_to_chars ((char *)buf, 0, 4);
13532               buf += 4;
13533             }
13534
13535         uncond:
13536           if (mips_pic == NO_PIC)
13537             {
13538               /* j or jal.  */
13539               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13540                       ? 0x0c000000 : 0x08000000);
13541               exp.X_op = O_symbol;
13542               exp.X_add_symbol = fragp->fr_symbol;
13543               exp.X_add_number = fragp->fr_offset;
13544
13545               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13546                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13547               fixp->fx_file = fragp->fr_file;
13548               fixp->fx_line = fragp->fr_line;
13549
13550               md_number_to_chars ((char*)buf, insn, 4);
13551               buf += 4;
13552             }
13553           else
13554             {
13555               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13556               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13557               exp.X_op = O_symbol;
13558               exp.X_add_symbol = fragp->fr_symbol;
13559               exp.X_add_number = fragp->fr_offset;
13560
13561               if (fragp->fr_offset)
13562                 {
13563                   exp.X_add_symbol = make_expr_symbol (&exp);
13564                   exp.X_add_number = 0;
13565                 }
13566
13567               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13568                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13569               fixp->fx_file = fragp->fr_file;
13570               fixp->fx_line = fragp->fr_line;
13571
13572               md_number_to_chars ((char*)buf, insn, 4);
13573               buf += 4;
13574
13575               if (mips_opts.isa == ISA_MIPS1)
13576                 {
13577                   /* nop */
13578                   md_number_to_chars ((char*)buf, 0, 4);
13579                   buf += 4;
13580                 }
13581
13582               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13583               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13584
13585               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13586                                   4, &exp, 0, BFD_RELOC_LO16);
13587               fixp->fx_file = fragp->fr_file;
13588               fixp->fx_line = fragp->fr_line;
13589
13590               md_number_to_chars ((char*)buf, insn, 4);
13591               buf += 4;
13592
13593               /* j(al)r $at.  */
13594               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13595                 insn = 0x0020f809;
13596               else
13597                 insn = 0x00200008;
13598
13599               md_number_to_chars ((char*)buf, insn, 4);
13600               buf += 4;
13601             }
13602         }
13603
13604       assert (buf == (bfd_byte *)fragp->fr_literal
13605               + fragp->fr_fix + fragp->fr_var);
13606
13607       fragp->fr_fix += fragp->fr_var;
13608
13609       return;
13610     }
13611
13612   if (RELAX_MIPS16_P (fragp->fr_subtype))
13613     {
13614       int type;
13615       register const struct mips16_immed_operand *op;
13616       bfd_boolean small, ext;
13617       offsetT val;
13618       bfd_byte *buf;
13619       unsigned long insn;
13620       bfd_boolean use_extend;
13621       unsigned short extend;
13622
13623       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13624       op = mips16_immed_operands;
13625       while (op->type != type)
13626         ++op;
13627
13628       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13629         {
13630           small = FALSE;
13631           ext = TRUE;
13632         }
13633       else
13634         {
13635           small = TRUE;
13636           ext = FALSE;
13637         }
13638
13639       resolve_symbol_value (fragp->fr_symbol);
13640       val = S_GET_VALUE (fragp->fr_symbol);
13641       if (op->pcrel)
13642         {
13643           addressT addr;
13644
13645           addr = fragp->fr_address + fragp->fr_fix;
13646
13647           /* The rules for the base address of a PC relative reloc are
13648              complicated; see mips16_extended_frag.  */
13649           if (type == 'p' || type == 'q')
13650             {
13651               addr += 2;
13652               if (ext)
13653                 addr += 2;
13654               /* Ignore the low bit in the target, since it will be
13655                  set for a text label.  */
13656               if ((val & 1) != 0)
13657                 --val;
13658             }
13659           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13660             addr -= 4;
13661           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13662             addr -= 2;
13663
13664           addr &= ~ (addressT) ((1 << op->shift) - 1);
13665           val -= addr;
13666
13667           /* Make sure the section winds up with the alignment we have
13668              assumed.  */
13669           if (op->shift > 0)
13670             record_alignment (asec, op->shift);
13671         }
13672
13673       if (ext
13674           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13675               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13676         as_warn_where (fragp->fr_file, fragp->fr_line,
13677                        _("extended instruction in delay slot"));
13678
13679       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13680
13681       if (target_big_endian)
13682         insn = bfd_getb16 (buf);
13683       else
13684         insn = bfd_getl16 (buf);
13685
13686       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13687                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13688                     small, ext, &insn, &use_extend, &extend);
13689
13690       if (use_extend)
13691         {
13692           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13693           fragp->fr_fix += 2;
13694           buf += 2;
13695         }
13696
13697       md_number_to_chars ((char *) buf, insn, 2);
13698       fragp->fr_fix += 2;
13699       buf += 2;
13700     }
13701   else
13702     {
13703       if (fragp->fr_opcode == NULL)
13704         return;
13705
13706       old = RELAX_OLD (fragp->fr_subtype);
13707       new = RELAX_NEW (fragp->fr_subtype);
13708       fixptr = fragp->fr_literal + fragp->fr_fix;
13709
13710       if (new > 0)
13711         memcpy (fixptr - old, fixptr, new);
13712
13713       fragp->fr_fix += new - old;
13714     }
13715 }
13716
13717 #ifdef OBJ_ELF
13718
13719 /* This function is called after the relocs have been generated.
13720    We've been storing mips16 text labels as odd.  Here we convert them
13721    back to even for the convenience of the debugger.  */
13722
13723 void
13724 mips_frob_file_after_relocs ()
13725 {
13726   asymbol **syms;
13727   unsigned int count, i;
13728
13729   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13730     return;
13731
13732   syms = bfd_get_outsymbols (stdoutput);
13733   count = bfd_get_symcount (stdoutput);
13734   for (i = 0; i < count; i++, syms++)
13735     {
13736       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13737           && ((*syms)->value & 1) != 0)
13738         {
13739           (*syms)->value &= ~1;
13740           /* If the symbol has an odd size, it was probably computed
13741              incorrectly, so adjust that as well.  */
13742           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13743             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13744         }
13745     }
13746 }
13747
13748 #endif
13749
13750 /* This function is called whenever a label is defined.  It is used
13751    when handling branch delays; if a branch has a label, we assume we
13752    can not move it.  */
13753
13754 void
13755 mips_define_label (sym)
13756      symbolS *sym;
13757 {
13758   struct insn_label_list *l;
13759
13760   if (free_insn_labels == NULL)
13761     l = (struct insn_label_list *) xmalloc (sizeof *l);
13762   else
13763     {
13764       l = free_insn_labels;
13765       free_insn_labels = l->next;
13766     }
13767
13768   l->label = sym;
13769   l->next = insn_labels;
13770   insn_labels = l;
13771 }
13772 \f
13773 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13774
13775 /* Some special processing for a MIPS ELF file.  */
13776
13777 void
13778 mips_elf_final_processing ()
13779 {
13780   /* Write out the register information.  */
13781   if (mips_abi != N64_ABI)
13782     {
13783       Elf32_RegInfo s;
13784
13785       s.ri_gprmask = mips_gprmask;
13786       s.ri_cprmask[0] = mips_cprmask[0];
13787       s.ri_cprmask[1] = mips_cprmask[1];
13788       s.ri_cprmask[2] = mips_cprmask[2];
13789       s.ri_cprmask[3] = mips_cprmask[3];
13790       /* The gp_value field is set by the MIPS ELF backend.  */
13791
13792       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13793                                        ((Elf32_External_RegInfo *)
13794                                         mips_regmask_frag));
13795     }
13796   else
13797     {
13798       Elf64_Internal_RegInfo s;
13799
13800       s.ri_gprmask = mips_gprmask;
13801       s.ri_pad = 0;
13802       s.ri_cprmask[0] = mips_cprmask[0];
13803       s.ri_cprmask[1] = mips_cprmask[1];
13804       s.ri_cprmask[2] = mips_cprmask[2];
13805       s.ri_cprmask[3] = mips_cprmask[3];
13806       /* The gp_value field is set by the MIPS ELF backend.  */
13807
13808       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13809                                        ((Elf64_External_RegInfo *)
13810                                         mips_regmask_frag));
13811     }
13812
13813   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13814      sort of BFD interface for this.  */
13815   if (mips_any_noreorder)
13816     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13817   if (mips_pic != NO_PIC)
13818     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13819
13820   /* Set MIPS ELF flags for ASEs.  */
13821   if (file_ase_mips16)
13822     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13823 #if 0 /* XXX FIXME */
13824   if (file_ase_mips3d)
13825     elf_elfheader (stdoutput)->e_flags |= ???;
13826 #endif
13827   if (file_ase_mdmx)
13828     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13829
13830   /* Set the MIPS ELF ABI flags.  */
13831   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13832     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13833   else if (mips_abi == O64_ABI)
13834     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13835   else if (mips_abi == EABI_ABI)
13836     {
13837       if (!file_mips_gp32)
13838         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13839       else
13840         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13841     }
13842   else if (mips_abi == N32_ABI)
13843     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13844
13845   /* Nothing to do for N64_ABI.  */
13846
13847   if (mips_32bitmode)
13848     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13849 }
13850
13851 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13852 \f
13853 typedef struct proc {
13854   symbolS *isym;
13855   unsigned long reg_mask;
13856   unsigned long reg_offset;
13857   unsigned long fpreg_mask;
13858   unsigned long fpreg_offset;
13859   unsigned long frame_offset;
13860   unsigned long frame_reg;
13861   unsigned long pc_reg;
13862 } procS;
13863
13864 static procS cur_proc;
13865 static procS *cur_proc_ptr;
13866 static int numprocs;
13867
13868 /* Fill in an rs_align_code fragment.  */
13869
13870 void
13871 mips_handle_align (fragp)
13872      fragS *fragp;
13873 {
13874   if (fragp->fr_type != rs_align_code)
13875     return;
13876
13877   if (mips_opts.mips16)
13878     {
13879       static const unsigned char be_nop[] = { 0x65, 0x00 };
13880       static const unsigned char le_nop[] = { 0x00, 0x65 };
13881
13882       int bytes;
13883       char *p;
13884
13885       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13886       p = fragp->fr_literal + fragp->fr_fix;
13887
13888       if (bytes & 1)
13889         {
13890           *p++ = 0;
13891           fragp->fr_fix++;
13892         }
13893
13894       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13895       fragp->fr_var = 2;
13896     }
13897
13898   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13899 }
13900
13901 static void
13902 md_obj_begin ()
13903 {
13904 }
13905
13906 static void
13907 md_obj_end ()
13908 {
13909   /* check for premature end, nesting errors, etc */
13910   if (cur_proc_ptr)
13911     as_warn (_("missing .end at end of assembly"));
13912 }
13913
13914 static long
13915 get_number ()
13916 {
13917   int negative = 0;
13918   long val = 0;
13919
13920   if (*input_line_pointer == '-')
13921     {
13922       ++input_line_pointer;
13923       negative = 1;
13924     }
13925   if (!ISDIGIT (*input_line_pointer))
13926     as_bad (_("expected simple number"));
13927   if (input_line_pointer[0] == '0')
13928     {
13929       if (input_line_pointer[1] == 'x')
13930         {
13931           input_line_pointer += 2;
13932           while (ISXDIGIT (*input_line_pointer))
13933             {
13934               val <<= 4;
13935               val |= hex_value (*input_line_pointer++);
13936             }
13937           return negative ? -val : val;
13938         }
13939       else
13940         {
13941           ++input_line_pointer;
13942           while (ISDIGIT (*input_line_pointer))
13943             {
13944               val <<= 3;
13945               val |= *input_line_pointer++ - '0';
13946             }
13947           return negative ? -val : val;
13948         }
13949     }
13950   if (!ISDIGIT (*input_line_pointer))
13951     {
13952       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13953               *input_line_pointer, *input_line_pointer);
13954       as_warn (_("invalid number"));
13955       return -1;
13956     }
13957   while (ISDIGIT (*input_line_pointer))
13958     {
13959       val *= 10;
13960       val += *input_line_pointer++ - '0';
13961     }
13962   return negative ? -val : val;
13963 }
13964
13965 /* The .file directive; just like the usual .file directive, but there
13966    is an initial number which is the ECOFF file index.  In the non-ECOFF
13967    case .file implies DWARF-2.  */
13968
13969 static void
13970 s_mips_file (x)
13971      int x ATTRIBUTE_UNUSED;
13972 {
13973   static int first_file_directive = 0;
13974
13975   if (ECOFF_DEBUGGING)
13976     {
13977       get_number ();
13978       s_app_file (0);
13979     }
13980   else
13981     {
13982       char *filename;
13983
13984       filename = dwarf2_directive_file (0);
13985
13986       /* Versions of GCC up to 3.1 start files with a ".file"
13987          directive even for stabs output.  Make sure that this
13988          ".file" is handled.  Note that you need a version of GCC
13989          after 3.1 in order to support DWARF-2 on MIPS.  */
13990       if (filename != NULL && ! first_file_directive)
13991         {
13992           (void) new_logical_line (filename, -1);
13993           s_app_file_string (filename);
13994         }
13995       first_file_directive = 1;
13996     }
13997 }
13998
13999 /* The .loc directive, implying DWARF-2.  */
14000
14001 static void
14002 s_mips_loc (x)
14003      int x ATTRIBUTE_UNUSED;
14004 {
14005   if (!ECOFF_DEBUGGING)
14006     dwarf2_directive_loc (0);
14007 }
14008
14009 /* The .end directive.  */
14010
14011 static void
14012 s_mips_end (x)
14013      int x ATTRIBUTE_UNUSED;
14014 {
14015   symbolS *p;
14016   int maybe_text;
14017
14018   /* Following functions need their own .frame and .cprestore directives.  */
14019   mips_frame_reg_valid = 0;
14020   mips_cprestore_valid = 0;
14021
14022   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14023     {
14024       p = get_symbol ();
14025       demand_empty_rest_of_line ();
14026     }
14027   else
14028     p = NULL;
14029
14030 #ifdef BFD_ASSEMBLER
14031   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14032     maybe_text = 1;
14033   else
14034     maybe_text = 0;
14035 #else
14036   if (now_seg != data_section && now_seg != bss_section)
14037     maybe_text = 1;
14038   else
14039     maybe_text = 0;
14040 #endif
14041
14042   if (!maybe_text)
14043     as_warn (_(".end not in text section"));
14044
14045   if (!cur_proc_ptr)
14046     {
14047       as_warn (_(".end directive without a preceding .ent directive."));
14048       demand_empty_rest_of_line ();
14049       return;
14050     }
14051
14052   if (p != NULL)
14053     {
14054       assert (S_GET_NAME (p));
14055       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14056         as_warn (_(".end symbol does not match .ent symbol."));
14057
14058       if (debug_type == DEBUG_STABS)
14059         stabs_generate_asm_endfunc (S_GET_NAME (p),
14060                                     S_GET_NAME (p));
14061     }
14062   else
14063     as_warn (_(".end directive missing or unknown symbol"));
14064
14065 #ifdef OBJ_ELF
14066   /* Generate a .pdr section.  */
14067   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14068     {
14069       segT saved_seg = now_seg;
14070       subsegT saved_subseg = now_subseg;
14071       valueT dot;
14072       expressionS exp;
14073       char *fragp;
14074
14075       dot = frag_now_fix ();
14076
14077 #ifdef md_flush_pending_output
14078       md_flush_pending_output ();
14079 #endif
14080
14081       assert (pdr_seg);
14082       subseg_set (pdr_seg, 0);
14083
14084       /* Write the symbol.  */
14085       exp.X_op = O_symbol;
14086       exp.X_add_symbol = p;
14087       exp.X_add_number = 0;
14088       emit_expr (&exp, 4);
14089
14090       fragp = frag_more (7 * 4);
14091
14092       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14093       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14094       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14095       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14096       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14097       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14098       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14099
14100       subseg_set (saved_seg, saved_subseg);
14101     }
14102 #endif /* OBJ_ELF */
14103
14104   cur_proc_ptr = NULL;
14105 }
14106
14107 /* The .aent and .ent directives.  */
14108
14109 static void
14110 s_mips_ent (aent)
14111      int aent;
14112 {
14113   symbolS *symbolP;
14114   int maybe_text;
14115
14116   symbolP = get_symbol ();
14117   if (*input_line_pointer == ',')
14118     ++input_line_pointer;
14119   SKIP_WHITESPACE ();
14120   if (ISDIGIT (*input_line_pointer)
14121       || *input_line_pointer == '-')
14122     get_number ();
14123
14124 #ifdef BFD_ASSEMBLER
14125   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14126     maybe_text = 1;
14127   else
14128     maybe_text = 0;
14129 #else
14130   if (now_seg != data_section && now_seg != bss_section)
14131     maybe_text = 1;
14132   else
14133     maybe_text = 0;
14134 #endif
14135
14136   if (!maybe_text)
14137     as_warn (_(".ent or .aent not in text section."));
14138
14139   if (!aent && cur_proc_ptr)
14140     as_warn (_("missing .end"));
14141
14142   if (!aent)
14143     {
14144       /* This function needs its own .frame and .cprestore directives.  */
14145       mips_frame_reg_valid = 0;
14146       mips_cprestore_valid = 0;
14147
14148       cur_proc_ptr = &cur_proc;
14149       memset (cur_proc_ptr, '\0', sizeof (procS));
14150
14151       cur_proc_ptr->isym = symbolP;
14152
14153       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14154
14155       ++numprocs;
14156
14157       if (debug_type == DEBUG_STABS)
14158         stabs_generate_asm_func (S_GET_NAME (symbolP),
14159                                  S_GET_NAME (symbolP));
14160     }
14161
14162   demand_empty_rest_of_line ();
14163 }
14164
14165 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14166    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14167    s_mips_frame is used so that we can set the PDR information correctly.
14168    We can't use the ecoff routines because they make reference to the ecoff
14169    symbol table (in the mdebug section).  */
14170
14171 static void
14172 s_mips_frame (ignore)
14173      int ignore ATTRIBUTE_UNUSED;
14174 {
14175 #ifdef OBJ_ELF
14176   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14177     {
14178       long val;
14179
14180       if (cur_proc_ptr == (procS *) NULL)
14181         {
14182           as_warn (_(".frame outside of .ent"));
14183           demand_empty_rest_of_line ();
14184           return;
14185         }
14186
14187       cur_proc_ptr->frame_reg = tc_get_register (1);
14188
14189       SKIP_WHITESPACE ();
14190       if (*input_line_pointer++ != ','
14191           || get_absolute_expression_and_terminator (&val) != ',')
14192         {
14193           as_warn (_("Bad .frame directive"));
14194           --input_line_pointer;
14195           demand_empty_rest_of_line ();
14196           return;
14197         }
14198
14199       cur_proc_ptr->frame_offset = val;
14200       cur_proc_ptr->pc_reg = tc_get_register (0);
14201
14202       demand_empty_rest_of_line ();
14203     }
14204   else
14205 #endif /* OBJ_ELF */
14206     s_ignore (ignore);
14207 }
14208
14209 /* The .fmask and .mask directives. If the mdebug section is present
14210    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14211    embedded targets, s_mips_mask is used so that we can set the PDR
14212    information correctly. We can't use the ecoff routines because they
14213    make reference to the ecoff symbol table (in the mdebug section).  */
14214
14215 static void
14216 s_mips_mask (reg_type)
14217      char reg_type;
14218 {
14219 #ifdef OBJ_ELF
14220   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14221     {
14222       long mask, off;
14223
14224       if (cur_proc_ptr == (procS *) NULL)
14225         {
14226           as_warn (_(".mask/.fmask outside of .ent"));
14227           demand_empty_rest_of_line ();
14228           return;
14229         }
14230
14231       if (get_absolute_expression_and_terminator (&mask) != ',')
14232         {
14233           as_warn (_("Bad .mask/.fmask directive"));
14234           --input_line_pointer;
14235           demand_empty_rest_of_line ();
14236           return;
14237         }
14238
14239       off = get_absolute_expression ();
14240
14241       if (reg_type == 'F')
14242         {
14243           cur_proc_ptr->fpreg_mask = mask;
14244           cur_proc_ptr->fpreg_offset = off;
14245         }
14246       else
14247         {
14248           cur_proc_ptr->reg_mask = mask;
14249           cur_proc_ptr->reg_offset = off;
14250         }
14251
14252       demand_empty_rest_of_line ();
14253     }
14254   else
14255 #endif /* OBJ_ELF */
14256     s_ignore (reg_type);
14257 }
14258
14259 /* The .loc directive.  */
14260
14261 #if 0
14262 static void
14263 s_loc (x)
14264      int x;
14265 {
14266   symbolS *symbolP;
14267   int lineno;
14268   int addroff;
14269
14270   assert (now_seg == text_section);
14271
14272   lineno = get_number ();
14273   addroff = frag_now_fix ();
14274
14275   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14276   S_SET_TYPE (symbolP, N_SLINE);
14277   S_SET_OTHER (symbolP, 0);
14278   S_SET_DESC (symbolP, lineno);
14279   symbolP->sy_segment = now_seg;
14280 }
14281 #endif
14282
14283 /* A table describing all the processors gas knows about.  Names are
14284    matched in the order listed.
14285
14286    To ease comparison, please keep this table in the same order as
14287    gcc's mips_cpu_info_table[].  */
14288 static const struct mips_cpu_info mips_cpu_info_table[] =
14289 {
14290   /* Entries for generic ISAs */
14291   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14292   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14293   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14294   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14295   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14296   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14297   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14298
14299   /* MIPS I */
14300   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14301   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14302   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14303
14304   /* MIPS II */
14305   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14306
14307   /* MIPS III */
14308   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14309   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14310   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14311   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14312   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14313   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14314   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14315   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14316   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14317   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14318   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14319   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14320
14321   /* MIPS IV */
14322   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14323   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14324   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14325   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14326   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14327   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14328   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14329   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14330   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14331   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14332   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14333   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14334
14335   /* MIPS 32 */
14336   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14337   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14338   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14339
14340   /* MIPS 64 */
14341   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14342   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14343
14344   /* Broadcom SB-1 CPU core */
14345   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14346
14347   /* End marker */
14348   { NULL, 0, 0, 0 }
14349 };
14350
14351
14352 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14353    with a final "000" replaced by "k".  Ignore case.
14354
14355    Note: this function is shared between GCC and GAS.  */
14356
14357 static bfd_boolean
14358 mips_strict_matching_cpu_name_p (canonical, given)
14359      const char *canonical, *given;
14360 {
14361   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14362     given++, canonical++;
14363
14364   return ((*given == 0 && *canonical == 0)
14365           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14366 }
14367
14368
14369 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14370    CPU name.  We've traditionally allowed a lot of variation here.
14371
14372    Note: this function is shared between GCC and GAS.  */
14373
14374 static bfd_boolean
14375 mips_matching_cpu_name_p (canonical, given)
14376      const char *canonical, *given;
14377 {
14378   /* First see if the name matches exactly, or with a final "000"
14379      turned into "k".  */
14380   if (mips_strict_matching_cpu_name_p (canonical, given))
14381     return TRUE;
14382
14383   /* If not, try comparing based on numerical designation alone.
14384      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14385   if (TOLOWER (*given) == 'r')
14386     given++;
14387   if (!ISDIGIT (*given))
14388     return FALSE;
14389
14390   /* Skip over some well-known prefixes in the canonical name,
14391      hoping to find a number there too.  */
14392   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14393     canonical += 2;
14394   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14395     canonical += 2;
14396   else if (TOLOWER (canonical[0]) == 'r')
14397     canonical += 1;
14398
14399   return mips_strict_matching_cpu_name_p (canonical, given);
14400 }
14401
14402
14403 /* Parse an option that takes the name of a processor as its argument.
14404    OPTION is the name of the option and CPU_STRING is the argument.
14405    Return the corresponding processor enumeration if the CPU_STRING is
14406    recognized, otherwise report an error and return null.
14407
14408    A similar function exists in GCC.  */
14409
14410 static const struct mips_cpu_info *
14411 mips_parse_cpu (option, cpu_string)
14412      const char *option, *cpu_string;
14413 {
14414   const struct mips_cpu_info *p;
14415
14416   /* 'from-abi' selects the most compatible architecture for the given
14417      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14418      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14419      version.  Look first at the -mgp options, if given, otherwise base
14420      the choice on MIPS_DEFAULT_64BIT.
14421
14422      Treat NO_ABI like the EABIs.  One reason to do this is that the
14423      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14424      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14425      'mips64', just as we did in the days before 'from-abi'.  */
14426   if (strcasecmp (cpu_string, "from-abi") == 0)
14427     {
14428       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14429         return mips_cpu_info_from_isa (ISA_MIPS1);
14430
14431       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14432         return mips_cpu_info_from_isa (ISA_MIPS3);
14433
14434       if (file_mips_gp32 >= 0)
14435         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14436
14437       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14438                                      ? ISA_MIPS3
14439                                      : ISA_MIPS1);
14440     }
14441
14442   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14443   if (strcasecmp (cpu_string, "default") == 0)
14444     return 0;
14445
14446   for (p = mips_cpu_info_table; p->name != 0; p++)
14447     if (mips_matching_cpu_name_p (p->name, cpu_string))
14448       return p;
14449
14450   as_bad ("Bad value (%s) for %s", cpu_string, option);
14451   return 0;
14452 }
14453
14454 /* Return the canonical processor information for ISA (a member of the
14455    ISA_MIPS* enumeration).  */
14456
14457 static const struct mips_cpu_info *
14458 mips_cpu_info_from_isa (isa)
14459      int isa;
14460 {
14461   int i;
14462
14463   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14464     if (mips_cpu_info_table[i].is_isa
14465         && isa == mips_cpu_info_table[i].isa)
14466       return (&mips_cpu_info_table[i]);
14467
14468   return NULL;
14469 }
14470 \f
14471 static void
14472 show (stream, string, col_p, first_p)
14473      FILE *stream;
14474      const char *string;
14475      int *col_p;
14476      int *first_p;
14477 {
14478   if (*first_p)
14479     {
14480       fprintf (stream, "%24s", "");
14481       *col_p = 24;
14482     }
14483   else
14484     {
14485       fprintf (stream, ", ");
14486       *col_p += 2;
14487     }
14488
14489   if (*col_p + strlen (string) > 72)
14490     {
14491       fprintf (stream, "\n%24s", "");
14492       *col_p = 24;
14493     }
14494
14495   fprintf (stream, "%s", string);
14496   *col_p += strlen (string);
14497
14498   *first_p = 0;
14499 }
14500
14501 void
14502 md_show_usage (stream)
14503      FILE *stream;
14504 {
14505   int column, first;
14506   size_t i;
14507
14508   fprintf (stream, _("\
14509 MIPS options:\n\
14510 -membedded-pic          generate embedded position independent code\n\
14511 -EB                     generate big endian output\n\
14512 -EL                     generate little endian output\n\
14513 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14514 -G NUM                  allow referencing objects up to NUM bytes\n\
14515                         implicitly with the gp register [default 8]\n"));
14516   fprintf (stream, _("\
14517 -mips1                  generate MIPS ISA I instructions\n\
14518 -mips2                  generate MIPS ISA II instructions\n\
14519 -mips3                  generate MIPS ISA III instructions\n\
14520 -mips4                  generate MIPS ISA IV instructions\n\
14521 -mips5                  generate MIPS ISA V instructions\n\
14522 -mips32                 generate MIPS32 ISA instructions\n\
14523 -mips64                 generate MIPS64 ISA instructions\n\
14524 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14525
14526   first = 1;
14527
14528   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14529     show (stream, mips_cpu_info_table[i].name, &column, &first);
14530   show (stream, "from-abi", &column, &first);
14531   fputc ('\n', stream);
14532
14533   fprintf (stream, _("\
14534 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14535 -no-mCPU                don't generate code specific to CPU.\n\
14536                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14537
14538   first = 1;
14539
14540   show (stream, "3900", &column, &first);
14541   show (stream, "4010", &column, &first);
14542   show (stream, "4100", &column, &first);
14543   show (stream, "4650", &column, &first);
14544   fputc ('\n', stream);
14545
14546   fprintf (stream, _("\
14547 -mips16                 generate mips16 instructions\n\
14548 -no-mips16              do not generate mips16 instructions\n"));
14549   fprintf (stream, _("\
14550 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14551 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14552 -O0                     remove unneeded NOPs, do not swap branches\n\
14553 -O                      remove unneeded NOPs and swap branches\n\
14554 -n                      warn about NOPs generated from macros\n\
14555 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14556 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14557 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14558 #ifdef OBJ_ELF
14559   fprintf (stream, _("\
14560 -KPIC, -call_shared     generate SVR4 position independent code\n\
14561 -non_shared             do not generate position independent code\n\
14562 -xgot                   assume a 32 bit GOT\n\
14563 -mabi=ABI               create ABI conformant object file for:\n"));
14564
14565   first = 1;
14566
14567   show (stream, "32", &column, &first);
14568   show (stream, "o64", &column, &first);
14569   show (stream, "n32", &column, &first);
14570   show (stream, "64", &column, &first);
14571   show (stream, "eabi", &column, &first);
14572
14573   fputc ('\n', stream);
14574
14575   fprintf (stream, _("\
14576 -32                     create o32 ABI object file (default)\n\
14577 -n32                    create n32 ABI object file\n\
14578 -64                     create 64 ABI object file\n"));
14579 #endif
14580 }
14581
14582 enum dwarf2_format
14583 mips_dwarf2_format ()
14584 {
14585   if (mips_abi == N64_ABI)
14586     return dwarf2_format_64bit_irix;
14587   else
14588     return dwarf2_format_32bit;
14589 }