* config/tc-ia64.c (note_register_values): Warning fix.
[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, 2003
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 /* Whether or not we have code that can call pic code.  */
135 int mips_abicalls = FALSE;
136
137 /* This is the set of options which may be modified by the .set
138    pseudo-op.  We use a struct so that .set push and .set pop are more
139    reliable.  */
140
141 struct mips_set_options
142 {
143   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
144      if it has not been initialized.  Changed by `.set mipsN', and the
145      -mipsN command line option, and the default CPU.  */
146   int isa;
147   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
148      if they have not been initialized.  Changed by `.set <asename>', by
149      command line options, and based on the default architecture.  */
150   int ase_mips3d;
151   int ase_mdmx;
152   /* Whether we are assembling for the mips16 processor.  0 if we are
153      not, 1 if we are, and -1 if the value has not been initialized.
154      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
155      -nomips16 command line options, and the default CPU.  */
156   int mips16;
157   /* Non-zero if we should not reorder instructions.  Changed by `.set
158      reorder' and `.set noreorder'.  */
159   int noreorder;
160   /* Non-zero if we should not permit the $at ($1) register to be used
161      in instructions.  Changed by `.set at' and `.set noat'.  */
162   int noat;
163   /* Non-zero if we should warn when a macro instruction expands into
164      more than one machine instruction.  Changed by `.set nomacro' and
165      `.set macro'.  */
166   int warn_about_macros;
167   /* Non-zero if we should not move instructions.  Changed by `.set
168      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
169   int nomove;
170   /* Non-zero if we should not optimize branches by moving the target
171      of the branch into the delay slot.  Actually, we don't perform
172      this optimization anyhow.  Changed by `.set bopt' and `.set
173      nobopt'.  */
174   int nobopt;
175   /* Non-zero if we should not autoextend mips16 instructions.
176      Changed by `.set autoextend' and `.set noautoextend'.  */
177   int noautoextend;
178   /* Restrict general purpose registers and floating point registers
179      to 32 bit.  This is initially determined when -mgp32 or -mfp32
180      is passed but can changed if the assembler code uses .set mipsN.  */
181   int gp32;
182   int fp32;
183   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
184      command line option, and the default CPU.  */
185   int arch;
186 };
187
188 /* True if -mgp32 was passed.  */
189 static int file_mips_gp32 = -1;
190
191 /* True if -mfp32 was passed.  */
192 static int file_mips_fp32 = -1;
193
194 /* This is the struct we use to hold the current set of options.  Note
195    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
196    -1 to indicate that they have not been initialized.  */
197
198 static struct mips_set_options mips_opts =
199 {
200   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
201 };
202
203 /* These variables are filled in with the masks of registers used.
204    The object format code reads them and puts them in the appropriate
205    place.  */
206 unsigned long mips_gprmask;
207 unsigned long mips_cprmask[4];
208
209 /* MIPS ISA we are using for this output file.  */
210 static int file_mips_isa = ISA_UNKNOWN;
211
212 /* True if -mips16 was passed or implied by arguments passed on the
213    command line (e.g., by -march).  */
214 static int file_ase_mips16;
215
216 /* True if -mips3d was passed or implied by arguments passed on the
217    command line (e.g., by -march).  */
218 static int file_ase_mips3d;
219
220 /* True if -mdmx was passed or implied by arguments passed on the
221    command line (e.g., by -march).  */
222 static int file_ase_mdmx;
223
224 /* The argument of the -march= flag.  The architecture we are assembling.  */
225 static int file_mips_arch = CPU_UNKNOWN;
226 static const char *mips_arch_string;
227
228 /* The argument of the -mtune= flag.  The architecture for which we
229    are optimizing.  */
230 static int mips_tune = CPU_UNKNOWN;
231 static const char *mips_tune_string;
232
233 /* True when generating 32-bit code for a 64-bit processor.  */
234 static int mips_32bitmode = 0;
235
236 /* Some ISA's have delay slots for instructions which read or write
237    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
238    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
239    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
240    delay slot in this ISA.  The uses of this macro assume that any
241    ISA that has delay slots for one of these, has them for all.  They
242    also assume that ISAs which don't have delays for these insns, don't
243    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
244 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
245    (ISA) == ISA_MIPS1                       \
246    || (ISA) == ISA_MIPS2                    \
247    || (ISA) == ISA_MIPS3                    \
248    )
249
250 /* True if the given ABI requires 32-bit registers.  */
251 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
252
253 /* Likewise 64-bit registers.  */
254 #define ABI_NEEDS_64BIT_REGS(ABI) \
255   ((ABI) == N32_ABI               \
256    || (ABI) == N64_ABI            \
257    || (ABI) == O64_ABI)
258
259 /*  Return true if ISA supports 64 bit gp register instructions.  */
260 #define ISA_HAS_64BIT_REGS(ISA) (    \
261    (ISA) == ISA_MIPS3                \
262    || (ISA) == ISA_MIPS4             \
263    || (ISA) == ISA_MIPS5             \
264    || (ISA) == ISA_MIPS64            \
265    )
266
267 /* Return true if ISA supports 64-bit right rotate (dror et al.)
268    instructions.  */
269 #define ISA_HAS_DROR(ISA) (     \
270    0                            \
271    )
272
273 /* Return true if ISA supports 32-bit right rotate (ror et al.)
274    instructions.  */
275 #define ISA_HAS_ROR(ISA) (      \
276    (ISA) == ISA_MIPS32R2        \
277    )
278
279 #define HAVE_32BIT_GPRS                            \
280     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
281
282 #define HAVE_32BIT_FPRS                            \
283     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
284
285 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
286 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
287
288 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
289
290 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
291
292 /* We can only have 64bit addresses if the object file format
293    supports it.  */
294 #define HAVE_32BIT_ADDRESSES                           \
295    (HAVE_32BIT_GPRS                                    \
296     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
297          || ! HAVE_64BIT_OBJECTS)                      \
298         && mips_pic != EMBEDDED_PIC))
299
300 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
301 #define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
302                                       || HAVE_64BIT_GPRS)
303
304 /* Addresses are loaded in different ways, depending on the address size
305    in use.  The n32 ABI Documentation also mandates the use of additions
306    with overflow checking, but existing implementations don't follow it.  */
307 #define ADDRESS_ADD_INSN                                                \
308    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
309
310 #define ADDRESS_ADDI_INSN                                               \
311    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
312
313 #define ADDRESS_LOAD_INSN                                               \
314    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
315
316 #define ADDRESS_STORE_INSN                                              \
317    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
318
319 /* Return true if the given CPU supports the MIPS16 ASE.  */
320 #define CPU_HAS_MIPS16(cpu)                                             \
321    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
322     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
323
324 /* Return true if the given CPU supports the MIPS3D ASE.  */
325 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
326                                  )
327
328 /* Return true if the given CPU supports the MDMX ASE.  */
329 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
330                                  )
331
332 /* True if CPU has a dror instruction.  */
333 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
334
335 /* True if CPU has a ror instruction.  */
336 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
337
338 /* Whether the processor uses hardware interlocks to protect
339    reads from the HI and LO registers, and thus does not
340    require nops to be inserted.  */
341
342 #define hilo_interlocks (mips_opts.arch == CPU_R4010                       \
343                          || mips_opts.arch == CPU_VR5500                   \
344                          || mips_opts.arch == CPU_SB1                      \
345                          )
346
347 /* Whether the processor uses hardware interlocks to protect reads
348    from the GPRs, and thus does not require nops to be inserted.  */
349 #define gpr_interlocks \
350   (mips_opts.isa != ISA_MIPS1  \
351    || mips_opts.arch == CPU_VR5400  \
352    || mips_opts.arch == CPU_VR5500  \
353    || mips_opts.arch == CPU_R3900)
354
355 /* As with other "interlocks" this is used by hardware that has FP
356    (co-processor) interlocks.  */
357 /* Itbl support may require additional care here.  */
358 #define cop_interlocks (mips_opts.arch == CPU_R4300                        \
359                         || mips_opts.arch == CPU_VR5400                    \
360                         || mips_opts.arch == CPU_VR5500                    \
361                         || mips_opts.arch == CPU_SB1                       \
362                         )
363
364 /* Is this a mfhi or mflo instruction?  */
365 #define MF_HILO_INSN(PINFO) \
366           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
367
368 /* MIPS PIC level.  */
369
370 enum mips_pic_level mips_pic;
371
372 /* Warn about all NOPS that the assembler generates.  */
373 static int warn_nops = 0;
374
375 /* 1 if we should generate 32 bit offsets from the $gp register in
376    SVR4_PIC mode.  Currently has no meaning in other modes.  */
377 static int mips_big_got = 0;
378
379 /* 1 if trap instructions should used for overflow rather than break
380    instructions.  */
381 static int mips_trap = 0;
382
383 /* 1 if double width floating point constants should not be constructed
384    by assembling two single width halves into two single width floating
385    point registers which just happen to alias the double width destination
386    register.  On some architectures this aliasing can be disabled by a bit
387    in the status register, and the setting of this bit cannot be determined
388    automatically at assemble time.  */
389 static int mips_disable_float_construction;
390
391 /* Non-zero if any .set noreorder directives were used.  */
392
393 static int mips_any_noreorder;
394
395 /* Non-zero if nops should be inserted when the register referenced in
396    an mfhi/mflo instruction is read in the next two instructions.  */
397 static int mips_7000_hilo_fix;
398
399 /* The size of the small data section.  */
400 static unsigned int g_switch_value = 8;
401 /* Whether the -G option was used.  */
402 static int g_switch_seen = 0;
403
404 #define N_RMASK 0xc4
405 #define N_VFP   0xd4
406
407 /* If we can determine in advance that GP optimization won't be
408    possible, we can skip the relaxation stuff that tries to produce
409    GP-relative references.  This makes delay slot optimization work
410    better.
411
412    This function can only provide a guess, but it seems to work for
413    gcc output.  It needs to guess right for gcc, otherwise gcc
414    will put what it thinks is a GP-relative instruction in a branch
415    delay slot.
416
417    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
418    fixed it for the non-PIC mode.  KR 95/04/07  */
419 static int nopic_need_relax PARAMS ((symbolS *, int));
420
421 /* handle of the OPCODE hash table */
422 static struct hash_control *op_hash = NULL;
423
424 /* The opcode hash table we use for the mips16.  */
425 static struct hash_control *mips16_op_hash = NULL;
426
427 /* This array holds the chars that always start a comment.  If the
428     pre-processor is disabled, these aren't very useful */
429 const char comment_chars[] = "#";
430
431 /* This array holds the chars that only start a comment at the beginning of
432    a line.  If the line seems to have the form '# 123 filename'
433    .line and .file directives will appear in the pre-processed output */
434 /* Note that input_file.c hand checks for '#' at the beginning of the
435    first line of the input file.  This is because the compiler outputs
436    #NO_APP at the beginning of its output.  */
437 /* Also note that C style comments are always supported.  */
438 const char line_comment_chars[] = "#";
439
440 /* This array holds machine specific line separator characters.  */
441 const char line_separator_chars[] = ";";
442
443 /* Chars that can be used to separate mant from exp in floating point nums */
444 const char EXP_CHARS[] = "eE";
445
446 /* Chars that mean this number is a floating point constant */
447 /* As in 0f12.456 */
448 /* or    0d1.2345e12 */
449 const char FLT_CHARS[] = "rRsSfFdDxXpP";
450
451 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
452    changed in read.c .  Ideally it shouldn't have to know about it at all,
453    but nothing is ideal around here.
454  */
455
456 static char *insn_error;
457
458 static int auto_align = 1;
459
460 /* When outputting SVR4 PIC code, the assembler needs to know the
461    offset in the stack frame from which to restore the $gp register.
462    This is set by the .cprestore pseudo-op, and saved in this
463    variable.  */
464 static offsetT mips_cprestore_offset = -1;
465
466 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
467    more optimizations, it can use a register value instead of a memory-saved
468    offset and even an other register than $gp as global pointer.  */
469 static offsetT mips_cpreturn_offset = -1;
470 static int mips_cpreturn_register = -1;
471 static int mips_gp_register = GP;
472 static int mips_gprel_offset = 0;
473
474 /* Whether mips_cprestore_offset has been set in the current function
475    (or whether it has already been warned about, if not).  */
476 static int mips_cprestore_valid = 0;
477
478 /* This is the register which holds the stack frame, as set by the
479    .frame pseudo-op.  This is needed to implement .cprestore.  */
480 static int mips_frame_reg = SP;
481
482 /* Whether mips_frame_reg has been set in the current function
483    (or whether it has already been warned about, if not).  */
484 static int mips_frame_reg_valid = 0;
485
486 /* To output NOP instructions correctly, we need to keep information
487    about the previous two instructions.  */
488
489 /* Whether we are optimizing.  The default value of 2 means to remove
490    unneeded NOPs and swap branch instructions when possible.  A value
491    of 1 means to not swap branches.  A value of 0 means to always
492    insert NOPs.  */
493 static int mips_optimize = 2;
494
495 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
496    equivalent to seeing no -g option at all.  */
497 static int mips_debug = 0;
498
499 /* The previous instruction.  */
500 static struct mips_cl_insn prev_insn;
501
502 /* The instruction before prev_insn.  */
503 static struct mips_cl_insn prev_prev_insn;
504
505 /* If we don't want information for prev_insn or prev_prev_insn, we
506    point the insn_mo field at this dummy integer.  */
507 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
508
509 /* Non-zero if prev_insn is valid.  */
510 static int prev_insn_valid;
511
512 /* The frag for the previous instruction.  */
513 static struct frag *prev_insn_frag;
514
515 /* The offset into prev_insn_frag for the previous instruction.  */
516 static long prev_insn_where;
517
518 /* The reloc type for the previous instruction, if any.  */
519 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
520
521 /* The reloc for the previous instruction, if any.  */
522 static fixS *prev_insn_fixp[3];
523
524 /* Non-zero if the previous instruction was in a delay slot.  */
525 static int prev_insn_is_delay_slot;
526
527 /* Non-zero if the previous instruction was in a .set noreorder.  */
528 static int prev_insn_unreordered;
529
530 /* Non-zero if the previous instruction uses an extend opcode (if
531    mips16).  */
532 static int prev_insn_extended;
533
534 /* Non-zero if the previous previous instruction was in a .set
535    noreorder.  */
536 static int prev_prev_insn_unreordered;
537
538 /* If this is set, it points to a frag holding nop instructions which
539    were inserted before the start of a noreorder section.  If those
540    nops turn out to be unnecessary, the size of the frag can be
541    decreased.  */
542 static fragS *prev_nop_frag;
543
544 /* The number of nop instructions we created in prev_nop_frag.  */
545 static int prev_nop_frag_holds;
546
547 /* The number of nop instructions that we know we need in
548    prev_nop_frag.  */
549 static int prev_nop_frag_required;
550
551 /* The number of instructions we've seen since prev_nop_frag.  */
552 static int prev_nop_frag_since;
553
554 /* For ECOFF and ELF, relocations against symbols are done in two
555    parts, with a HI relocation and a LO relocation.  Each relocation
556    has only 16 bits of space to store an addend.  This means that in
557    order for the linker to handle carries correctly, it must be able
558    to locate both the HI and the LO relocation.  This means that the
559    relocations must appear in order in the relocation table.
560
561    In order to implement this, we keep track of each unmatched HI
562    relocation.  We then sort them so that they immediately precede the
563    corresponding LO relocation.  */
564
565 struct mips_hi_fixup
566 {
567   /* Next HI fixup.  */
568   struct mips_hi_fixup *next;
569   /* This fixup.  */
570   fixS *fixp;
571   /* The section this fixup is in.  */
572   segT seg;
573 };
574
575 /* The list of unmatched HI relocs.  */
576
577 static struct mips_hi_fixup *mips_hi_fixup_list;
578
579 /* The frag containing the last explicit relocation operator.
580    Null if explicit relocations have not been used.  */
581
582 static fragS *prev_reloc_op_frag;
583
584 /* Map normal MIPS register numbers to mips16 register numbers.  */
585
586 #define X ILLEGAL_REG
587 static const int mips32_to_16_reg_map[] =
588 {
589   X, X, 2, 3, 4, 5, 6, 7,
590   X, X, X, X, X, X, X, X,
591   0, 1, X, X, X, X, X, X,
592   X, X, X, X, X, X, X, X
593 };
594 #undef X
595
596 /* Map mips16 register numbers to normal MIPS register numbers.  */
597
598 static const unsigned int mips16_to_32_reg_map[] =
599 {
600   16, 17, 2, 3, 4, 5, 6, 7
601 };
602
603 static int mips_fix_4122_bugs;
604
605 /* We don't relax branches by default, since this causes us to expand
606    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
607    fail to compute the offset before expanding the macro to the most
608    efficient expansion.  */
609
610 static int mips_relax_branch;
611 \f
612 /* Since the MIPS does not have multiple forms of PC relative
613    instructions, we do not have to do relaxing as is done on other
614    platforms.  However, we do have to handle GP relative addressing
615    correctly, which turns out to be a similar problem.
616
617    Every macro that refers to a symbol can occur in (at least) two
618    forms, one with GP relative addressing and one without.  For
619    example, loading a global variable into a register generally uses
620    a macro instruction like this:
621      lw $4,i
622    If i can be addressed off the GP register (this is true if it is in
623    the .sbss or .sdata section, or if it is known to be smaller than
624    the -G argument) this will generate the following instruction:
625      lw $4,i($gp)
626    This instruction will use a GPREL reloc.  If i can not be addressed
627    off the GP register, the following instruction sequence will be used:
628      lui $at,i
629      lw $4,i($at)
630    In this case the first instruction will have a HI16 reloc, and the
631    second reloc will have a LO16 reloc.  Both relocs will be against
632    the symbol i.
633
634    The issue here is that we may not know whether i is GP addressable
635    until after we see the instruction that uses it.  Therefore, we
636    want to be able to choose the final instruction sequence only at
637    the end of the assembly.  This is similar to the way other
638    platforms choose the size of a PC relative instruction only at the
639    end of assembly.
640
641    When generating position independent code we do not use GP
642    addressing in quite the same way, but the issue still arises as
643    external symbols and local symbols must be handled differently.
644
645    We handle these issues by actually generating both possible
646    instruction sequences.  The longer one is put in a frag_var with
647    type rs_machine_dependent.  We encode what to do with the frag in
648    the subtype field.  We encode (1) the number of existing bytes to
649    replace, (2) the number of new bytes to use, (3) the offset from
650    the start of the existing bytes to the first reloc we must generate
651    (that is, the offset is applied from the start of the existing
652    bytes after they are replaced by the new bytes, if any), (4) the
653    offset from the start of the existing bytes to the second reloc,
654    (5) whether a third reloc is needed (the third reloc is always four
655    bytes after the second reloc), and (6) whether to warn if this
656    variant is used (this is sometimes needed if .set nomacro or .set
657    noat is in effect).  All these numbers are reasonably small.
658
659    Generating two instruction sequences must be handled carefully to
660    ensure that delay slots are handled correctly.  Fortunately, there
661    are a limited number of cases.  When the second instruction
662    sequence is generated, append_insn is directed to maintain the
663    existing delay slot information, so it continues to apply to any
664    code after the second instruction sequence.  This means that the
665    second instruction sequence must not impose any requirements not
666    required by the first instruction sequence.
667
668    These variant frags are then handled in functions called by the
669    machine independent code.  md_estimate_size_before_relax returns
670    the final size of the frag.  md_convert_frag sets up the final form
671    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
672    one if needed.  */
673 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
674   ((relax_substateT) \
675    (((old) << 23) \
676     | ((new) << 16) \
677     | (((reloc1) + 64) << 9) \
678     | (((reloc2) + 64) << 2) \
679     | ((reloc3) ? (1 << 1) : 0) \
680     | ((warn) ? 1 : 0)))
681 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
682 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
683 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
684 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
685 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
686 #define RELAX_WARN(i) ((i) & 1)
687
688 /* Branch without likely bit.  If label is out of range, we turn:
689
690         beq reg1, reg2, label
691         delay slot
692
693    into
694
695         bne reg1, reg2, 0f
696         nop
697         j label
698      0: delay slot
699
700    with the following opcode replacements:
701
702         beq <-> bne
703         blez <-> bgtz
704         bltz <-> bgez
705         bc1f <-> bc1t
706
707         bltzal <-> bgezal  (with jal label instead of j label)
708
709    Even though keeping the delay slot instruction in the delay slot of
710    the branch would be more efficient, it would be very tricky to do
711    correctly, because we'd have to introduce a variable frag *after*
712    the delay slot instruction, and expand that instead.  Let's do it
713    the easy way for now, even if the branch-not-taken case now costs
714    one additional instruction.  Out-of-range branches are not supposed
715    to be common, anyway.
716
717    Branch likely.  If label is out of range, we turn:
718
719         beql reg1, reg2, label
720         delay slot (annulled if branch not taken)
721
722    into
723
724         beql reg1, reg2, 1f
725         nop
726         beql $0, $0, 2f
727         nop
728      1: j[al] label
729         delay slot (executed only if branch taken)
730      2:
731
732    It would be possible to generate a shorter sequence by losing the
733    likely bit, generating something like:
734
735         bne reg1, reg2, 0f
736         nop
737         j[al] label
738         delay slot (executed only if branch taken)
739      0:
740
741         beql -> bne
742         bnel -> beq
743         blezl -> bgtz
744         bgtzl -> blez
745         bltzl -> bgez
746         bgezl -> bltz
747         bc1fl -> bc1t
748         bc1tl -> bc1f
749
750         bltzall -> bgezal  (with jal label instead of j label)
751         bgezall -> bltzal  (ditto)
752
753
754    but it's not clear that it would actually improve performance.  */
755 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
756   ((relax_substateT) \
757    (0xc0000000 \
758     | ((toofar) ? 1 : 0) \
759     | ((link) ? 2 : 0) \
760     | ((likely) ? 4 : 0) \
761     | ((uncond) ? 8 : 0)))
762 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
763 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
764 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
765 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
766 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
767
768 /* For mips16 code, we use an entirely different form of relaxation.
769    mips16 supports two versions of most instructions which take
770    immediate values: a small one which takes some small value, and a
771    larger one which takes a 16 bit value.  Since branches also follow
772    this pattern, relaxing these values is required.
773
774    We can assemble both mips16 and normal MIPS code in a single
775    object.  Therefore, we need to support this type of relaxation at
776    the same time that we support the relaxation described above.  We
777    use the high bit of the subtype field to distinguish these cases.
778
779    The information we store for this type of relaxation is the
780    argument code found in the opcode file for this relocation, whether
781    the user explicitly requested a small or extended form, and whether
782    the relocation is in a jump or jal delay slot.  That tells us the
783    size of the value, and how it should be stored.  We also store
784    whether the fragment is considered to be extended or not.  We also
785    store whether this is known to be a branch to a different section,
786    whether we have tried to relax this frag yet, and whether we have
787    ever extended a PC relative fragment because of a shift count.  */
788 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
789   (0x80000000                                                   \
790    | ((type) & 0xff)                                            \
791    | ((small) ? 0x100 : 0)                                      \
792    | ((ext) ? 0x200 : 0)                                        \
793    | ((dslot) ? 0x400 : 0)                                      \
794    | ((jal_dslot) ? 0x800 : 0))
795 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
796 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
797 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
798 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
799 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
800 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
801 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
802 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
803 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
804 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
805 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
806 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
807
808 /* Is the given value a sign-extended 32-bit value?  */
809 #define IS_SEXT_32BIT_NUM(x)                                            \
810   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
811    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
812
813 /* Is the given value a sign-extended 16-bit value?  */
814 #define IS_SEXT_16BIT_NUM(x)                                            \
815   (((x) &~ (offsetT) 0x7fff) == 0                                       \
816    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
817
818 \f
819 /* Prototypes for static functions.  */
820
821 #ifdef __STDC__
822 #define internalError() \
823     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
824 #else
825 #define internalError() as_fatal (_("MIPS internal Error"));
826 #endif
827
828 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
829
830 static inline bfd_boolean reloc_needs_lo_p
831   PARAMS ((bfd_reloc_code_real_type));
832 static inline bfd_boolean fixup_has_matching_lo_p
833   PARAMS ((fixS *));
834 static int insn_uses_reg
835   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
836            enum mips_regclass class));
837 static int reg_needs_delay
838   PARAMS ((unsigned int));
839 static void mips16_mark_labels
840   PARAMS ((void));
841 static void append_insn
842   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
843            bfd_reloc_code_real_type *r));
844 static void mips_no_prev_insn
845   PARAMS ((int));
846 static void mips_emit_delays
847   PARAMS ((bfd_boolean));
848 #ifdef USE_STDARG
849 static void macro_build
850   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
851            const char *fmt, ...));
852 #else
853 static void macro_build ();
854 #endif
855 static void mips16_macro_build
856   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
857 static void macro_build_jalr
858   PARAMS ((int, expressionS *));
859 static void macro_build_lui
860   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
861 static void macro_build_ldst_constoffset
862   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
863            int valreg, int breg));
864 static void set_at
865   PARAMS ((int *counter, int reg, int unsignedp));
866 static void check_absolute_expr
867   PARAMS ((struct mips_cl_insn * ip, expressionS *));
868 static void load_register
869   PARAMS ((int *, int, expressionS *, int));
870 static void load_address
871   PARAMS ((int *, int, expressionS *, int *));
872 static void move_register
873   PARAMS ((int *, int, int));
874 static void macro
875   PARAMS ((struct mips_cl_insn * ip));
876 static void mips16_macro
877   PARAMS ((struct mips_cl_insn * ip));
878 #ifdef LOSING_COMPILER
879 static void macro2
880   PARAMS ((struct mips_cl_insn * ip));
881 #endif
882 static void mips_ip
883   PARAMS ((char *str, struct mips_cl_insn * ip));
884 static void mips16_ip
885   PARAMS ((char *str, struct mips_cl_insn * ip));
886 static void mips16_immed
887   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
888            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
889 static bfd_boolean parse_relocation
890   PARAMS ((char **, bfd_reloc_code_real_type *));
891 static size_t my_getSmallExpression
892   PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
893 static void my_getExpression
894   PARAMS ((expressionS *, char *));
895 #ifdef OBJ_ELF
896 static int support_64bit_objects
897   PARAMS((void));
898 #endif
899 static void mips_set_option_string
900   PARAMS ((const char **, const char *));
901 static symbolS *get_symbol
902   PARAMS ((void));
903 static void mips_align
904   PARAMS ((int to, int fill, symbolS *label));
905 static void s_align
906   PARAMS ((int));
907 static void s_change_sec
908   PARAMS ((int));
909 static void s_change_section
910   PARAMS ((int));
911 static void s_cons
912   PARAMS ((int));
913 static void s_float_cons
914   PARAMS ((int));
915 static void s_mips_globl
916   PARAMS ((int));
917 static void s_option
918   PARAMS ((int));
919 static void s_mipsset
920   PARAMS ((int));
921 static void s_abicalls
922   PARAMS ((int));
923 static void s_cpload
924   PARAMS ((int));
925 static void s_cpsetup
926   PARAMS ((int));
927 static void s_cplocal
928   PARAMS ((int));
929 static void s_cprestore
930   PARAMS ((int));
931 static void s_cpreturn
932   PARAMS ((int));
933 static void s_gpvalue
934   PARAMS ((int));
935 static void s_gpword
936   PARAMS ((int));
937 static void s_gpdword
938   PARAMS ((int));
939 static void s_cpadd
940   PARAMS ((int));
941 static void s_insn
942   PARAMS ((int));
943 static void md_obj_begin
944   PARAMS ((void));
945 static void md_obj_end
946   PARAMS ((void));
947 static long get_number
948   PARAMS ((void));
949 static void s_mips_ent
950   PARAMS ((int));
951 static void s_mips_end
952   PARAMS ((int));
953 static void s_mips_frame
954   PARAMS ((int));
955 static void s_mips_mask
956   PARAMS ((int));
957 static void s_mips_stab
958   PARAMS ((int));
959 static void s_mips_weakext
960   PARAMS ((int));
961 static void s_mips_file
962   PARAMS ((int));
963 static void s_mips_loc
964   PARAMS ((int));
965 static bfd_boolean pic_need_relax
966   PARAMS ((symbolS *, asection *));
967 static int mips16_extended_frag
968   PARAMS ((fragS *, asection *, long));
969 static int relaxed_branch_length (fragS *, asection *, int);
970 static int validate_mips_insn
971   PARAMS ((const struct mips_opcode *));
972 static void show
973   PARAMS ((FILE *, const char *, int *, int *));
974 #ifdef OBJ_ELF
975 static int mips_need_elf_addend_fixup
976   PARAMS ((fixS *));
977 #endif
978
979 /* Table and functions used to map between CPU/ISA names, and
980    ISA levels, and CPU numbers.  */
981
982 struct mips_cpu_info
983 {
984   const char *name;           /* CPU or ISA name.  */
985   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
986   int isa;                    /* ISA level.  */
987   int cpu;                    /* CPU number (default CPU if ISA).  */
988 };
989
990 static void mips_set_architecture
991   PARAMS ((const struct mips_cpu_info *));
992 static void mips_set_tune
993   PARAMS ((const struct mips_cpu_info *));
994 static bfd_boolean mips_strict_matching_cpu_name_p
995   PARAMS ((const char *, const char *));
996 static bfd_boolean mips_matching_cpu_name_p
997   PARAMS ((const char *, const char *));
998 static const struct mips_cpu_info *mips_parse_cpu
999   PARAMS ((const char *, const char *));
1000 static const struct mips_cpu_info *mips_cpu_info_from_isa
1001   PARAMS ((int));
1002 static const struct mips_cpu_info *mips_cpu_info_from_arch
1003   PARAMS ((int));
1004 \f
1005 /* Pseudo-op table.
1006
1007    The following pseudo-ops from the Kane and Heinrich MIPS book
1008    should be defined here, but are currently unsupported: .alias,
1009    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1010
1011    The following pseudo-ops from the Kane and Heinrich MIPS book are
1012    specific to the type of debugging information being generated, and
1013    should be defined by the object format: .aent, .begin, .bend,
1014    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1015    .vreg.
1016
1017    The following pseudo-ops from the Kane and Heinrich MIPS book are
1018    not MIPS CPU specific, but are also not specific to the object file
1019    format.  This file is probably the best place to define them, but
1020    they are not currently supported: .asm0, .endr, .lab, .repeat,
1021    .struct.  */
1022
1023 static const pseudo_typeS mips_pseudo_table[] =
1024 {
1025   /* MIPS specific pseudo-ops.  */
1026   {"option", s_option, 0},
1027   {"set", s_mipsset, 0},
1028   {"rdata", s_change_sec, 'r'},
1029   {"sdata", s_change_sec, 's'},
1030   {"livereg", s_ignore, 0},
1031   {"abicalls", s_abicalls, 0},
1032   {"cpload", s_cpload, 0},
1033   {"cpsetup", s_cpsetup, 0},
1034   {"cplocal", s_cplocal, 0},
1035   {"cprestore", s_cprestore, 0},
1036   {"cpreturn", s_cpreturn, 0},
1037   {"gpvalue", s_gpvalue, 0},
1038   {"gpword", s_gpword, 0},
1039   {"gpdword", s_gpdword, 0},
1040   {"cpadd", s_cpadd, 0},
1041   {"insn", s_insn, 0},
1042
1043   /* Relatively generic pseudo-ops that happen to be used on MIPS
1044      chips.  */
1045   {"asciiz", stringer, 1},
1046   {"bss", s_change_sec, 'b'},
1047   {"err", s_err, 0},
1048   {"half", s_cons, 1},
1049   {"dword", s_cons, 3},
1050   {"weakext", s_mips_weakext, 0},
1051
1052   /* These pseudo-ops are defined in read.c, but must be overridden
1053      here for one reason or another.  */
1054   {"align", s_align, 0},
1055   {"byte", s_cons, 0},
1056   {"data", s_change_sec, 'd'},
1057   {"double", s_float_cons, 'd'},
1058   {"float", s_float_cons, 'f'},
1059   {"globl", s_mips_globl, 0},
1060   {"global", s_mips_globl, 0},
1061   {"hword", s_cons, 1},
1062   {"int", s_cons, 2},
1063   {"long", s_cons, 2},
1064   {"octa", s_cons, 4},
1065   {"quad", s_cons, 3},
1066   {"section", s_change_section, 0},
1067   {"short", s_cons, 1},
1068   {"single", s_float_cons, 'f'},
1069   {"stabn", s_mips_stab, 'n'},
1070   {"text", s_change_sec, 't'},
1071   {"word", s_cons, 2},
1072
1073   { "extern", ecoff_directive_extern, 0},
1074
1075   { NULL, NULL, 0 },
1076 };
1077
1078 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1079 {
1080   /* These pseudo-ops should be defined by the object file format.
1081      However, a.out doesn't support them, so we have versions here.  */
1082   {"aent", s_mips_ent, 1},
1083   {"bgnb", s_ignore, 0},
1084   {"end", s_mips_end, 0},
1085   {"endb", s_ignore, 0},
1086   {"ent", s_mips_ent, 0},
1087   {"file", s_mips_file, 0},
1088   {"fmask", s_mips_mask, 'F'},
1089   {"frame", s_mips_frame, 0},
1090   {"loc", s_mips_loc, 0},
1091   {"mask", s_mips_mask, 'R'},
1092   {"verstamp", s_ignore, 0},
1093   { NULL, NULL, 0 },
1094 };
1095
1096 extern void pop_insert PARAMS ((const pseudo_typeS *));
1097
1098 void
1099 mips_pop_insert ()
1100 {
1101   pop_insert (mips_pseudo_table);
1102   if (! ECOFF_DEBUGGING)
1103     pop_insert (mips_nonecoff_pseudo_table);
1104 }
1105 \f
1106 /* Symbols labelling the current insn.  */
1107
1108 struct insn_label_list
1109 {
1110   struct insn_label_list *next;
1111   symbolS *label;
1112 };
1113
1114 static struct insn_label_list *insn_labels;
1115 static struct insn_label_list *free_insn_labels;
1116
1117 static void mips_clear_insn_labels PARAMS ((void));
1118
1119 static inline void
1120 mips_clear_insn_labels ()
1121 {
1122   register struct insn_label_list **pl;
1123
1124   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1125     ;
1126   *pl = insn_labels;
1127   insn_labels = NULL;
1128 }
1129 \f
1130 static char *expr_end;
1131
1132 /* Expressions which appear in instructions.  These are set by
1133    mips_ip.  */
1134
1135 static expressionS imm_expr;
1136 static expressionS offset_expr;
1137
1138 /* Relocs associated with imm_expr and offset_expr.  */
1139
1140 static bfd_reloc_code_real_type imm_reloc[3]
1141   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1142 static bfd_reloc_code_real_type offset_reloc[3]
1143   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1144
1145 /* These are set by mips16_ip if an explicit extension is used.  */
1146
1147 static bfd_boolean mips16_small, mips16_ext;
1148
1149 #ifdef OBJ_ELF
1150 /* The pdr segment for per procedure frame/regmask info.  Not used for
1151    ECOFF debugging.  */
1152
1153 static segT pdr_seg;
1154 #endif
1155
1156 /* The default target format to use.  */
1157
1158 const char *
1159 mips_target_format ()
1160 {
1161   switch (OUTPUT_FLAVOR)
1162     {
1163     case bfd_target_aout_flavour:
1164       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1165     case bfd_target_ecoff_flavour:
1166       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1167     case bfd_target_coff_flavour:
1168       return "pe-mips";
1169     case bfd_target_elf_flavour:
1170 #ifdef TE_TMIPS
1171       /* This is traditional mips.  */
1172       return (target_big_endian
1173               ? (HAVE_64BIT_OBJECTS
1174                  ? "elf64-tradbigmips"
1175                  : (HAVE_NEWABI
1176                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1177               : (HAVE_64BIT_OBJECTS
1178                  ? "elf64-tradlittlemips"
1179                  : (HAVE_NEWABI
1180                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1181 #else
1182       return (target_big_endian
1183               ? (HAVE_64BIT_OBJECTS
1184                  ? "elf64-bigmips"
1185                  : (HAVE_NEWABI
1186                     ? "elf32-nbigmips" : "elf32-bigmips"))
1187               : (HAVE_64BIT_OBJECTS
1188                  ? "elf64-littlemips"
1189                  : (HAVE_NEWABI
1190                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1191 #endif
1192     default:
1193       abort ();
1194       return NULL;
1195     }
1196 }
1197
1198 /* This function is called once, at assembler startup time.  It should
1199    set up all the tables, etc. that the MD part of the assembler will need.  */
1200
1201 void
1202 md_begin ()
1203 {
1204   register const char *retval = NULL;
1205   int i = 0;
1206   int broken = 0;
1207
1208   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1209     as_warn (_("Could not set architecture and machine"));
1210
1211   op_hash = hash_new ();
1212
1213   for (i = 0; i < NUMOPCODES;)
1214     {
1215       const char *name = mips_opcodes[i].name;
1216
1217       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1218       if (retval != NULL)
1219         {
1220           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1221                    mips_opcodes[i].name, retval);
1222           /* Probably a memory allocation problem?  Give up now.  */
1223           as_fatal (_("Broken assembler.  No assembly attempted."));
1224         }
1225       do
1226         {
1227           if (mips_opcodes[i].pinfo != INSN_MACRO)
1228             {
1229               if (!validate_mips_insn (&mips_opcodes[i]))
1230                 broken = 1;
1231             }
1232           ++i;
1233         }
1234       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1235     }
1236
1237   mips16_op_hash = hash_new ();
1238
1239   i = 0;
1240   while (i < bfd_mips16_num_opcodes)
1241     {
1242       const char *name = mips16_opcodes[i].name;
1243
1244       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1245       if (retval != NULL)
1246         as_fatal (_("internal: can't hash `%s': %s"),
1247                   mips16_opcodes[i].name, retval);
1248       do
1249         {
1250           if (mips16_opcodes[i].pinfo != INSN_MACRO
1251               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1252                   != mips16_opcodes[i].match))
1253             {
1254               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1255                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1256               broken = 1;
1257             }
1258           ++i;
1259         }
1260       while (i < bfd_mips16_num_opcodes
1261              && strcmp (mips16_opcodes[i].name, name) == 0);
1262     }
1263
1264   if (broken)
1265     as_fatal (_("Broken assembler.  No assembly attempted."));
1266
1267   /* We add all the general register names to the symbol table.  This
1268      helps us detect invalid uses of them.  */
1269   for (i = 0; i < 32; i++)
1270     {
1271       char buf[5];
1272
1273       sprintf (buf, "$%d", i);
1274       symbol_table_insert (symbol_new (buf, reg_section, i,
1275                                        &zero_address_frag));
1276     }
1277   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1278                                    &zero_address_frag));
1279   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1280                                    &zero_address_frag));
1281   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1282                                    &zero_address_frag));
1283   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1284                                    &zero_address_frag));
1285   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1286                                    &zero_address_frag));
1287   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1288                                    &zero_address_frag));
1289   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1290                                    &zero_address_frag));
1291   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1292                                    &zero_address_frag));
1293   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1294                                    &zero_address_frag));
1295
1296   /* If we don't add these register names to the symbol table, they
1297      may end up being added as regular symbols by operand(), and then
1298      make it to the object file as undefined in case they're not
1299      regarded as local symbols.  They're local in o32, since `$' is a
1300      local symbol prefix, but not in n32 or n64.  */
1301   for (i = 0; i < 8; i++)
1302     {
1303       char buf[6];
1304
1305       sprintf (buf, "$fcc%i", i);
1306       symbol_table_insert (symbol_new (buf, reg_section, -1,
1307                                        &zero_address_frag));
1308     }
1309
1310   mips_no_prev_insn (FALSE);
1311
1312   mips_gprmask = 0;
1313   mips_cprmask[0] = 0;
1314   mips_cprmask[1] = 0;
1315   mips_cprmask[2] = 0;
1316   mips_cprmask[3] = 0;
1317
1318   /* set the default alignment for the text section (2**2) */
1319   record_alignment (text_section, 2);
1320
1321   if (USE_GLOBAL_POINTER_OPT)
1322     bfd_set_gp_size (stdoutput, g_switch_value);
1323
1324   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1325     {
1326       /* On a native system, sections must be aligned to 16 byte
1327          boundaries.  When configured for an embedded ELF target, we
1328          don't bother.  */
1329       if (strcmp (TARGET_OS, "elf") != 0)
1330         {
1331           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1332           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1333           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1334         }
1335
1336       /* Create a .reginfo section for register masks and a .mdebug
1337          section for debugging information.  */
1338       {
1339         segT seg;
1340         subsegT subseg;
1341         flagword flags;
1342         segT sec;
1343
1344         seg = now_seg;
1345         subseg = now_subseg;
1346
1347         /* The ABI says this section should be loaded so that the
1348            running program can access it.  However, we don't load it
1349            if we are configured for an embedded target */
1350         flags = SEC_READONLY | SEC_DATA;
1351         if (strcmp (TARGET_OS, "elf") != 0)
1352           flags |= SEC_ALLOC | SEC_LOAD;
1353
1354         if (mips_abi != N64_ABI)
1355           {
1356             sec = subseg_new (".reginfo", (subsegT) 0);
1357
1358             bfd_set_section_flags (stdoutput, sec, flags);
1359             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1360
1361 #ifdef OBJ_ELF
1362             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1363 #endif
1364           }
1365         else
1366           {
1367             /* The 64-bit ABI uses a .MIPS.options section rather than
1368                .reginfo section.  */
1369             sec = subseg_new (".MIPS.options", (subsegT) 0);
1370             bfd_set_section_flags (stdoutput, sec, flags);
1371             bfd_set_section_alignment (stdoutput, sec, 3);
1372
1373 #ifdef OBJ_ELF
1374             /* Set up the option header.  */
1375             {
1376               Elf_Internal_Options opthdr;
1377               char *f;
1378
1379               opthdr.kind = ODK_REGINFO;
1380               opthdr.size = (sizeof (Elf_External_Options)
1381                              + sizeof (Elf64_External_RegInfo));
1382               opthdr.section = 0;
1383               opthdr.info = 0;
1384               f = frag_more (sizeof (Elf_External_Options));
1385               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1386                                              (Elf_External_Options *) f);
1387
1388               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1389             }
1390 #endif
1391           }
1392
1393         if (ECOFF_DEBUGGING)
1394           {
1395             sec = subseg_new (".mdebug", (subsegT) 0);
1396             (void) bfd_set_section_flags (stdoutput, sec,
1397                                           SEC_HAS_CONTENTS | SEC_READONLY);
1398             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1399           }
1400 #ifdef OBJ_ELF
1401         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1402           {
1403             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1404             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1405                                           SEC_READONLY | SEC_RELOC
1406                                           | SEC_DEBUGGING);
1407             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1408           }
1409 #endif
1410
1411         subseg_set (seg, subseg);
1412       }
1413     }
1414
1415   if (! ECOFF_DEBUGGING)
1416     md_obj_begin ();
1417 }
1418
1419 void
1420 md_mips_end ()
1421 {
1422   if (! ECOFF_DEBUGGING)
1423     md_obj_end ();
1424 }
1425
1426 void
1427 md_assemble (str)
1428      char *str;
1429 {
1430   struct mips_cl_insn insn;
1431   bfd_reloc_code_real_type unused_reloc[3]
1432     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1433
1434   imm_expr.X_op = O_absent;
1435   offset_expr.X_op = O_absent;
1436   imm_reloc[0] = BFD_RELOC_UNUSED;
1437   imm_reloc[1] = BFD_RELOC_UNUSED;
1438   imm_reloc[2] = BFD_RELOC_UNUSED;
1439   offset_reloc[0] = BFD_RELOC_UNUSED;
1440   offset_reloc[1] = BFD_RELOC_UNUSED;
1441   offset_reloc[2] = BFD_RELOC_UNUSED;
1442
1443   if (mips_opts.mips16)
1444     mips16_ip (str, &insn);
1445   else
1446     {
1447       mips_ip (str, &insn);
1448       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1449             str, insn.insn_opcode));
1450     }
1451
1452   if (insn_error)
1453     {
1454       as_bad ("%s `%s'", insn_error, str);
1455       return;
1456     }
1457
1458   if (insn.insn_mo->pinfo == INSN_MACRO)
1459     {
1460       if (mips_opts.mips16)
1461         mips16_macro (&insn);
1462       else
1463         macro (&insn);
1464     }
1465   else
1466     {
1467       if (imm_expr.X_op != O_absent)
1468         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1469       else if (offset_expr.X_op != O_absent)
1470         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1471       else
1472         append_insn (NULL, &insn, NULL, unused_reloc);
1473     }
1474 }
1475
1476 /* Return true if the given relocation might need a matching %lo().
1477    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1478    applied to local symbols.  */
1479
1480 static inline bfd_boolean
1481 reloc_needs_lo_p (reloc)
1482      bfd_reloc_code_real_type reloc;
1483 {
1484   return (reloc == BFD_RELOC_HI16_S
1485           || reloc == BFD_RELOC_MIPS_GOT16);
1486 }
1487
1488 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1489    relocation.  */
1490
1491 static inline bfd_boolean
1492 fixup_has_matching_lo_p (fixp)
1493      fixS *fixp;
1494 {
1495   return (fixp->fx_next != NULL
1496           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1497           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1498           && fixp->fx_offset == fixp->fx_next->fx_offset);
1499 }
1500
1501 /* See whether instruction IP reads register REG.  CLASS is the type
1502    of register.  */
1503
1504 static int
1505 insn_uses_reg (ip, reg, class)
1506      struct mips_cl_insn *ip;
1507      unsigned int reg;
1508      enum mips_regclass class;
1509 {
1510   if (class == MIPS16_REG)
1511     {
1512       assert (mips_opts.mips16);
1513       reg = mips16_to_32_reg_map[reg];
1514       class = MIPS_GR_REG;
1515     }
1516
1517   /* Don't report on general register ZERO, since it never changes.  */
1518   if (class == MIPS_GR_REG && reg == ZERO)
1519     return 0;
1520
1521   if (class == MIPS_FP_REG)
1522     {
1523       assert (! mips_opts.mips16);
1524       /* If we are called with either $f0 or $f1, we must check $f0.
1525          This is not optimal, because it will introduce an unnecessary
1526          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1527          need to distinguish reading both $f0 and $f1 or just one of
1528          them.  Note that we don't have to check the other way,
1529          because there is no instruction that sets both $f0 and $f1
1530          and requires a delay.  */
1531       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1532           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1533               == (reg &~ (unsigned) 1)))
1534         return 1;
1535       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1536           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1537               == (reg &~ (unsigned) 1)))
1538         return 1;
1539     }
1540   else if (! mips_opts.mips16)
1541     {
1542       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1543           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1544         return 1;
1545       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1546           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1547         return 1;
1548     }
1549   else
1550     {
1551       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1552           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1553                                     & MIPS16OP_MASK_RX)]
1554               == reg))
1555         return 1;
1556       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1557           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1558                                     & MIPS16OP_MASK_RY)]
1559               == reg))
1560         return 1;
1561       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1562           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1563                                     & MIPS16OP_MASK_MOVE32Z)]
1564               == reg))
1565         return 1;
1566       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1567         return 1;
1568       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1569         return 1;
1570       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1571         return 1;
1572       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1573           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1574               & MIPS16OP_MASK_REGR32) == reg)
1575         return 1;
1576     }
1577
1578   return 0;
1579 }
1580
1581 /* This function returns true if modifying a register requires a
1582    delay.  */
1583
1584 static int
1585 reg_needs_delay (reg)
1586      unsigned int reg;
1587 {
1588   unsigned long prev_pinfo;
1589
1590   prev_pinfo = prev_insn.insn_mo->pinfo;
1591   if (! mips_opts.noreorder
1592       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1593       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1594           || (! gpr_interlocks
1595               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1596     {
1597       /* A load from a coprocessor or from memory.  All load
1598          delays delay the use of general register rt for one
1599          instruction on the r3000.  The r6000 and r4000 use
1600          interlocks.  */
1601       /* Itbl support may require additional care here.  */
1602       know (prev_pinfo & INSN_WRITE_GPR_T);
1603       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1604         return 1;
1605     }
1606
1607   return 0;
1608 }
1609
1610 /* Mark instruction labels in mips16 mode.  This permits the linker to
1611    handle them specially, such as generating jalx instructions when
1612    needed.  We also make them odd for the duration of the assembly, in
1613    order to generate the right sort of code.  We will make them even
1614    in the adjust_symtab routine, while leaving them marked.  This is
1615    convenient for the debugger and the disassembler.  The linker knows
1616    to make them odd again.  */
1617
1618 static void
1619 mips16_mark_labels ()
1620 {
1621   if (mips_opts.mips16)
1622     {
1623       struct insn_label_list *l;
1624       valueT val;
1625
1626       for (l = insn_labels; l != NULL; l = l->next)
1627         {
1628 #ifdef OBJ_ELF
1629           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1630             S_SET_OTHER (l->label, STO_MIPS16);
1631 #endif
1632           val = S_GET_VALUE (l->label);
1633           if ((val & 1) == 0)
1634             S_SET_VALUE (l->label, val + 1);
1635         }
1636     }
1637 }
1638
1639 /* Output an instruction.  PLACE is where to put the instruction; if
1640    it is NULL, this uses frag_more to get room.  IP is the instruction
1641    information.  ADDRESS_EXPR is an operand of the instruction to be
1642    used with RELOC_TYPE.  */
1643
1644 static void
1645 append_insn (place, ip, address_expr, reloc_type)
1646      char *place;
1647      struct mips_cl_insn *ip;
1648      expressionS *address_expr;
1649      bfd_reloc_code_real_type *reloc_type;
1650 {
1651   register unsigned long prev_pinfo, pinfo;
1652   char *f;
1653   fixS *fixp[3];
1654   int nops = 0;
1655   bfd_boolean force_new_frag = FALSE;
1656
1657   /* Mark instruction labels in mips16 mode.  */
1658   mips16_mark_labels ();
1659
1660   prev_pinfo = prev_insn.insn_mo->pinfo;
1661   pinfo = ip->insn_mo->pinfo;
1662
1663   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1664     {
1665       int prev_prev_nop;
1666
1667       /* If the previous insn required any delay slots, see if we need
1668          to insert a NOP or two.  There are eight kinds of possible
1669          hazards, of which an instruction can have at most one type.
1670          (1) a load from memory delay
1671          (2) a load from a coprocessor delay
1672          (3) an unconditional branch delay
1673          (4) a conditional branch delay
1674          (5) a move to coprocessor register delay
1675          (6) a load coprocessor register from memory delay
1676          (7) a coprocessor condition code delay
1677          (8) a HI/LO special register delay
1678
1679          There are a lot of optimizations we could do that we don't.
1680          In particular, we do not, in general, reorder instructions.
1681          If you use gcc with optimization, it will reorder
1682          instructions and generally do much more optimization then we
1683          do here; repeating all that work in the assembler would only
1684          benefit hand written assembly code, and does not seem worth
1685          it.  */
1686
1687       /* This is how a NOP is emitted.  */
1688 #define emit_nop()                                      \
1689   (mips_opts.mips16                                     \
1690    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1691    : md_number_to_chars (frag_more (4), 0, 4))
1692
1693       /* The previous insn might require a delay slot, depending upon
1694          the contents of the current insn.  */
1695       if (! mips_opts.mips16
1696           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1697           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1698                && ! cop_interlocks)
1699               || (! gpr_interlocks
1700                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1701         {
1702           /* A load from a coprocessor or from memory.  All load
1703              delays delay the use of general register rt for one
1704              instruction on the r3000.  The r6000 and r4000 use
1705              interlocks.  */
1706           /* Itbl support may require additional care here.  */
1707           know (prev_pinfo & INSN_WRITE_GPR_T);
1708           if (mips_optimize == 0
1709               || insn_uses_reg (ip,
1710                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1711                                  & OP_MASK_RT),
1712                                 MIPS_GR_REG))
1713             ++nops;
1714         }
1715       else if (! mips_opts.mips16
1716                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1717                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1718                     && ! cop_interlocks)
1719                    || (mips_opts.isa == ISA_MIPS1
1720                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1721         {
1722           /* A generic coprocessor delay.  The previous instruction
1723              modified a coprocessor general or control register.  If
1724              it modified a control register, we need to avoid any
1725              coprocessor instruction (this is probably not always
1726              required, but it sometimes is).  If it modified a general
1727              register, we avoid using that register.
1728
1729              On the r6000 and r4000 loading a coprocessor register
1730              from memory is interlocked, and does not require a delay.
1731
1732              This case is not handled very well.  There is no special
1733              knowledge of CP0 handling, and the coprocessors other
1734              than the floating point unit are not distinguished at
1735              all.  */
1736           /* Itbl support may require additional care here. FIXME!
1737              Need to modify this to include knowledge about
1738              user specified delays!  */
1739           if (prev_pinfo & INSN_WRITE_FPR_T)
1740             {
1741               if (mips_optimize == 0
1742                   || insn_uses_reg (ip,
1743                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1744                                      & OP_MASK_FT),
1745                                     MIPS_FP_REG))
1746                 ++nops;
1747             }
1748           else if (prev_pinfo & INSN_WRITE_FPR_S)
1749             {
1750               if (mips_optimize == 0
1751                   || insn_uses_reg (ip,
1752                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1753                                      & OP_MASK_FS),
1754                                     MIPS_FP_REG))
1755                 ++nops;
1756             }
1757           else
1758             {
1759               /* We don't know exactly what the previous instruction
1760                  does.  If the current instruction uses a coprocessor
1761                  register, we must insert a NOP.  If previous
1762                  instruction may set the condition codes, and the
1763                  current instruction uses them, we must insert two
1764                  NOPS.  */
1765               /* Itbl support may require additional care here.  */
1766               if (mips_optimize == 0
1767                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1768                       && (pinfo & INSN_READ_COND_CODE)))
1769                 nops += 2;
1770               else if (pinfo & INSN_COP)
1771                 ++nops;
1772             }
1773         }
1774       else if (! mips_opts.mips16
1775                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1776                && (prev_pinfo & INSN_WRITE_COND_CODE)
1777                && ! cop_interlocks)
1778         {
1779           /* The previous instruction sets the coprocessor condition
1780              codes, but does not require a general coprocessor delay
1781              (this means it is a floating point comparison
1782              instruction).  If this instruction uses the condition
1783              codes, we need to insert a single NOP.  */
1784           /* Itbl support may require additional care here.  */
1785           if (mips_optimize == 0
1786               || (pinfo & INSN_READ_COND_CODE))
1787             ++nops;
1788         }
1789
1790       /* If we're fixing up mfhi/mflo for the r7000 and the
1791          previous insn was an mfhi/mflo and the current insn
1792          reads the register that the mfhi/mflo wrote to, then
1793          insert two nops.  */
1794
1795       else if (mips_7000_hilo_fix
1796                && MF_HILO_INSN (prev_pinfo)
1797                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1798                                       & OP_MASK_RD),
1799                                  MIPS_GR_REG))
1800         {
1801           nops += 2;
1802         }
1803
1804       /* If we're fixing up mfhi/mflo for the r7000 and the
1805          2nd previous insn was an mfhi/mflo and the current insn
1806          reads the register that the mfhi/mflo wrote to, then
1807          insert one nop.  */
1808
1809       else if (mips_7000_hilo_fix
1810                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1811                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1812                                        & OP_MASK_RD),
1813                                     MIPS_GR_REG))
1814
1815         {
1816           ++nops;
1817         }
1818
1819       else if (prev_pinfo & INSN_READ_LO)
1820         {
1821           /* The previous instruction reads the LO register; if the
1822              current instruction writes to the LO register, we must
1823              insert two NOPS.  Some newer processors have interlocks.
1824              Also the tx39's multiply instructions can be exectuted
1825              immediatly after a read from HI/LO (without the delay),
1826              though the tx39's divide insns still do require the
1827              delay.  */
1828           if (! (hilo_interlocks
1829                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1830               && (mips_optimize == 0
1831                   || (pinfo & INSN_WRITE_LO)))
1832             nops += 2;
1833           /* Most mips16 branch insns don't have a delay slot.
1834              If a read from LO is immediately followed by a branch
1835              to a write to LO we have a read followed by a write
1836              less than 2 insns away.  We assume the target of
1837              a branch might be a write to LO, and insert a nop
1838              between a read and an immediately following branch.  */
1839           else if (mips_opts.mips16
1840                    && (mips_optimize == 0
1841                        || (pinfo & MIPS16_INSN_BRANCH)))
1842             ++nops;
1843         }
1844       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1845         {
1846           /* The previous instruction reads the HI register; if the
1847              current instruction writes to the HI register, we must
1848              insert a NOP.  Some newer processors have interlocks.
1849              Also the note tx39's multiply above.  */
1850           if (! (hilo_interlocks
1851                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1852               && (mips_optimize == 0
1853                   || (pinfo & INSN_WRITE_HI)))
1854             nops += 2;
1855           /* Most mips16 branch insns don't have a delay slot.
1856              If a read from HI is immediately followed by a branch
1857              to a write to HI we have a read followed by a write
1858              less than 2 insns away.  We assume the target of
1859              a branch might be a write to HI, and insert a nop
1860              between a read and an immediately following branch.  */
1861           else if (mips_opts.mips16
1862                    && (mips_optimize == 0
1863                        || (pinfo & MIPS16_INSN_BRANCH)))
1864             ++nops;
1865         }
1866
1867       /* If the previous instruction was in a noreorder section, then
1868          we don't want to insert the nop after all.  */
1869       /* Itbl support may require additional care here.  */
1870       if (prev_insn_unreordered)
1871         nops = 0;
1872
1873       /* There are two cases which require two intervening
1874          instructions: 1) setting the condition codes using a move to
1875          coprocessor instruction which requires a general coprocessor
1876          delay and then reading the condition codes 2) reading the HI
1877          or LO register and then writing to it (except on processors
1878          which have interlocks).  If we are not already emitting a NOP
1879          instruction, we must check for these cases compared to the
1880          instruction previous to the previous instruction.  */
1881       if ((! mips_opts.mips16
1882            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1883            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1884            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1885            && (pinfo & INSN_READ_COND_CODE)
1886            && ! cop_interlocks)
1887           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1888               && (pinfo & INSN_WRITE_LO)
1889               && ! (hilo_interlocks
1890                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1891           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1892               && (pinfo & INSN_WRITE_HI)
1893               && ! (hilo_interlocks
1894                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1895         prev_prev_nop = 1;
1896       else
1897         prev_prev_nop = 0;
1898
1899       if (prev_prev_insn_unreordered)
1900         prev_prev_nop = 0;
1901
1902       if (prev_prev_nop && nops == 0)
1903         ++nops;
1904
1905       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1906         {
1907           /* We're out of bits in pinfo, so we must resort to string
1908              ops here.  Shortcuts are selected based on opcodes being
1909              limited to the VR4122 instruction set.  */
1910           int min_nops = 0;
1911           const char *pn = prev_insn.insn_mo->name;
1912           const char *tn = ip->insn_mo->name;
1913           if (strncmp(pn, "macc", 4) == 0
1914               || strncmp(pn, "dmacc", 5) == 0)
1915             {
1916               /* Errata 21 - [D]DIV[U] after [D]MACC */
1917               if (strstr (tn, "div"))
1918                 {
1919                   min_nops = 1;
1920                 }
1921
1922               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1923               if (pn[0] == 'd' /* dmacc */
1924                   && (strncmp(tn, "dmult", 5) == 0
1925                       || strncmp(tn, "dmacc", 5) == 0))
1926                 {
1927                   min_nops = 1;
1928                 }
1929
1930               /* Errata 24 - MT{LO,HI} after [D]MACC */
1931               if (strcmp (tn, "mtlo") == 0
1932                   || strcmp (tn, "mthi") == 0)
1933                 {
1934                   min_nops = 1;
1935                 }
1936
1937             }
1938           else if (strncmp(pn, "dmult", 5) == 0
1939                    && (strncmp(tn, "dmult", 5) == 0
1940                        || strncmp(tn, "dmacc", 5) == 0))
1941             {
1942               /* Here is the rest of errata 23.  */
1943               min_nops = 1;
1944             }
1945           if (nops < min_nops)
1946             nops = min_nops;
1947         }
1948
1949       /* If we are being given a nop instruction, don't bother with
1950          one of the nops we would otherwise output.  This will only
1951          happen when a nop instruction is used with mips_optimize set
1952          to 0.  */
1953       if (nops > 0
1954           && ! mips_opts.noreorder
1955           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1956         --nops;
1957
1958       /* Now emit the right number of NOP instructions.  */
1959       if (nops > 0 && ! mips_opts.noreorder)
1960         {
1961           fragS *old_frag;
1962           unsigned long old_frag_offset;
1963           int i;
1964           struct insn_label_list *l;
1965
1966           old_frag = frag_now;
1967           old_frag_offset = frag_now_fix ();
1968
1969           for (i = 0; i < nops; i++)
1970             emit_nop ();
1971
1972           if (listing)
1973             {
1974               listing_prev_line ();
1975               /* We may be at the start of a variant frag.  In case we
1976                  are, make sure there is enough space for the frag
1977                  after the frags created by listing_prev_line.  The
1978                  argument to frag_grow here must be at least as large
1979                  as the argument to all other calls to frag_grow in
1980                  this file.  We don't have to worry about being in the
1981                  middle of a variant frag, because the variants insert
1982                  all needed nop instructions themselves.  */
1983               frag_grow (40);
1984             }
1985
1986           for (l = insn_labels; l != NULL; l = l->next)
1987             {
1988               valueT val;
1989
1990               assert (S_GET_SEGMENT (l->label) == now_seg);
1991               symbol_set_frag (l->label, frag_now);
1992               val = (valueT) frag_now_fix ();
1993               /* mips16 text labels are stored as odd.  */
1994               if (mips_opts.mips16)
1995                 ++val;
1996               S_SET_VALUE (l->label, val);
1997             }
1998
1999 #ifndef NO_ECOFF_DEBUGGING
2000           if (ECOFF_DEBUGGING)
2001             ecoff_fix_loc (old_frag, old_frag_offset);
2002 #endif
2003         }
2004       else if (prev_nop_frag != NULL)
2005         {
2006           /* We have a frag holding nops we may be able to remove.  If
2007              we don't need any nops, we can decrease the size of
2008              prev_nop_frag by the size of one instruction.  If we do
2009              need some nops, we count them in prev_nops_required.  */
2010           if (prev_nop_frag_since == 0)
2011             {
2012               if (nops == 0)
2013                 {
2014                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2015                   --prev_nop_frag_holds;
2016                 }
2017               else
2018                 prev_nop_frag_required += nops;
2019             }
2020           else
2021             {
2022               if (prev_prev_nop == 0)
2023                 {
2024                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2025                   --prev_nop_frag_holds;
2026                 }
2027               else
2028                 ++prev_nop_frag_required;
2029             }
2030
2031           if (prev_nop_frag_holds <= prev_nop_frag_required)
2032             prev_nop_frag = NULL;
2033
2034           ++prev_nop_frag_since;
2035
2036           /* Sanity check: by the time we reach the second instruction
2037              after prev_nop_frag, we should have used up all the nops
2038              one way or another.  */
2039           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2040         }
2041     }
2042
2043   if (place == NULL
2044       && address_expr
2045       && *reloc_type == BFD_RELOC_16_PCREL_S2
2046       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2047           || pinfo & INSN_COND_BRANCH_LIKELY)
2048       && mips_relax_branch
2049       /* Don't try branch relaxation within .set nomacro, or within
2050          .set noat if we use $at for PIC computations.  If it turns
2051          out that the branch was out-of-range, we'll get an error.  */
2052       && !mips_opts.warn_about_macros
2053       && !(mips_opts.noat && mips_pic != NO_PIC)
2054       && !mips_opts.mips16)
2055     {
2056       f = frag_var (rs_machine_dependent,
2057                     relaxed_branch_length
2058                     (NULL, NULL,
2059                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2060                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2061                     RELAX_BRANCH_ENCODE
2062                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2063                      pinfo & INSN_COND_BRANCH_LIKELY,
2064                      pinfo & INSN_WRITE_GPR_31,
2065                      0),
2066                     address_expr->X_add_symbol,
2067                     address_expr->X_add_number,
2068                     0);
2069       *reloc_type = BFD_RELOC_UNUSED;
2070     }
2071   else if (*reloc_type > BFD_RELOC_UNUSED)
2072     {
2073       /* We need to set up a variant frag.  */
2074       assert (mips_opts.mips16 && address_expr != NULL);
2075       f = frag_var (rs_machine_dependent, 4, 0,
2076                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2077                                          mips16_small, mips16_ext,
2078                                          (prev_pinfo
2079                                           & INSN_UNCOND_BRANCH_DELAY),
2080                                          (*prev_insn_reloc_type
2081                                           == BFD_RELOC_MIPS16_JMP)),
2082                     make_expr_symbol (address_expr), 0, NULL);
2083     }
2084   else if (place != NULL)
2085     f = place;
2086   else if (mips_opts.mips16
2087            && ! ip->use_extend
2088            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2089     {
2090       /* Make sure there is enough room to swap this instruction with
2091          a following jump instruction.  */
2092       frag_grow (6);
2093       f = frag_more (2);
2094     }
2095   else
2096     {
2097       if (mips_opts.mips16
2098           && mips_opts.noreorder
2099           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2100         as_warn (_("extended instruction in delay slot"));
2101
2102       f = frag_more (4);
2103     }
2104
2105   fixp[0] = fixp[1] = fixp[2] = NULL;
2106   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2107     {
2108       if (address_expr->X_op == O_constant)
2109         {
2110           valueT tmp;
2111
2112           switch (*reloc_type)
2113             {
2114             case BFD_RELOC_32:
2115               ip->insn_opcode |= address_expr->X_add_number;
2116               break;
2117
2118             case BFD_RELOC_MIPS_HIGHEST:
2119               tmp = (address_expr->X_add_number
2120                      + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2121               tmp >>= 16;
2122               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2123               break;
2124
2125             case BFD_RELOC_MIPS_HIGHER:
2126               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2127               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2128               break;
2129
2130             case BFD_RELOC_HI16_S:
2131               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2132                                   >> 16) & 0xffff;
2133               break;
2134
2135             case BFD_RELOC_HI16:
2136               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2137               break;
2138
2139             case BFD_RELOC_LO16:
2140             case BFD_RELOC_MIPS_GOT_DISP:
2141               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2142               break;
2143
2144             case BFD_RELOC_MIPS_JMP:
2145               if ((address_expr->X_add_number & 3) != 0)
2146                 as_bad (_("jump to misaligned address (0x%lx)"),
2147                         (unsigned long) address_expr->X_add_number);
2148               if (address_expr->X_add_number & ~0xfffffff)
2149                 as_bad (_("jump address range overflow (0x%lx)"),
2150                         (unsigned long) address_expr->X_add_number);
2151               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2152               break;
2153
2154             case BFD_RELOC_MIPS16_JMP:
2155               if ((address_expr->X_add_number & 3) != 0)
2156                 as_bad (_("jump to misaligned address (0x%lx)"),
2157                         (unsigned long) address_expr->X_add_number);
2158               if (address_expr->X_add_number & ~0xfffffff)
2159                 as_bad (_("jump address range overflow (0x%lx)"),
2160                         (unsigned long) address_expr->X_add_number);
2161               ip->insn_opcode |=
2162                 (((address_expr->X_add_number & 0x7c0000) << 3)
2163                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2164                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2165               break;
2166
2167             case BFD_RELOC_16_PCREL_S2:
2168               goto need_reloc;
2169
2170             default:
2171               internalError ();
2172             }
2173         }
2174       else
2175         {
2176         need_reloc:
2177           /* Don't generate a reloc if we are writing into a variant frag.  */
2178           if (place == NULL)
2179             {
2180               reloc_howto_type *howto;
2181               int i;
2182
2183               /* In a compound relocation, it is the final (outermost)
2184                  operator that determines the relocated field.  */
2185               for (i = 1; i < 3; i++)
2186                 if (reloc_type[i] == BFD_RELOC_UNUSED)
2187                   break;
2188
2189               howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2190               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2191                                      bfd_get_reloc_size(howto),
2192                                      address_expr,
2193                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2194                                      reloc_type[0]);
2195
2196               /* These relocations can have an addend that won't fit in
2197                  4 octets for 64bit assembly.  */
2198               if (HAVE_64BIT_GPRS
2199                   && ! howto->partial_inplace
2200                   && (reloc_type[0] == BFD_RELOC_16
2201                       || reloc_type[0] == BFD_RELOC_32
2202                       || reloc_type[0] == BFD_RELOC_MIPS_JMP
2203                       || reloc_type[0] == BFD_RELOC_HI16_S
2204                       || reloc_type[0] == BFD_RELOC_LO16
2205                       || reloc_type[0] == BFD_RELOC_GPREL16
2206                       || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2207                       || reloc_type[0] == BFD_RELOC_GPREL32
2208                       || reloc_type[0] == BFD_RELOC_64
2209                       || reloc_type[0] == BFD_RELOC_CTOR
2210                       || reloc_type[0] == BFD_RELOC_MIPS_SUB
2211                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2212                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2213                       || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2214                       || reloc_type[0] == BFD_RELOC_MIPS_REL16
2215                       || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2216                 fixp[0]->fx_no_overflow = 1;
2217
2218               if (reloc_needs_lo_p (*reloc_type))
2219                 {
2220                   struct mips_hi_fixup *hi_fixup;
2221
2222                   /* Reuse the last entry if it already has a matching %lo.  */
2223                   hi_fixup = mips_hi_fixup_list;
2224                   if (hi_fixup == 0
2225                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2226                     {
2227                       hi_fixup = ((struct mips_hi_fixup *)
2228                                   xmalloc (sizeof (struct mips_hi_fixup)));
2229                       hi_fixup->next = mips_hi_fixup_list;
2230                       mips_hi_fixup_list = hi_fixup;
2231                     }
2232                   hi_fixup->fixp = fixp[0];
2233                   hi_fixup->seg = now_seg;
2234                 }
2235
2236               /* Add fixups for the second and third relocations, if given.
2237                  Note that the ABI allows the second relocation to be
2238                  against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2239                  moment we only use RSS_UNDEF, but we could add support
2240                  for the others if it ever becomes necessary.  */
2241               for (i = 1; i < 3; i++)
2242                 if (reloc_type[i] != BFD_RELOC_UNUSED)
2243                   {
2244                     address_expr->X_op = O_absent;
2245                     address_expr->X_add_symbol = 0;
2246                     address_expr->X_add_number = 0;
2247
2248                     fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2249                                            fixp[0]->fx_size, address_expr,
2250                                            FALSE, reloc_type[i]);
2251                   }
2252             }
2253         }
2254     }
2255
2256   if (! mips_opts.mips16)
2257     {
2258       md_number_to_chars (f, ip->insn_opcode, 4);
2259 #ifdef OBJ_ELF
2260       dwarf2_emit_insn (4);
2261 #endif
2262     }
2263   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2264     {
2265       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2266       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2267 #ifdef OBJ_ELF
2268       dwarf2_emit_insn (4);
2269 #endif
2270     }
2271   else
2272     {
2273       if (ip->use_extend)
2274         {
2275           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2276           f += 2;
2277         }
2278       md_number_to_chars (f, ip->insn_opcode, 2);
2279 #ifdef OBJ_ELF
2280       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2281 #endif
2282     }
2283
2284   /* Update the register mask information.  */
2285   if (! mips_opts.mips16)
2286     {
2287       if (pinfo & INSN_WRITE_GPR_D)
2288         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2289       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2290         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2291       if (pinfo & INSN_READ_GPR_S)
2292         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2293       if (pinfo & INSN_WRITE_GPR_31)
2294         mips_gprmask |= 1 << RA;
2295       if (pinfo & INSN_WRITE_FPR_D)
2296         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2297       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2298         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2299       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2300         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2301       if ((pinfo & INSN_READ_FPR_R) != 0)
2302         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2303       if (pinfo & INSN_COP)
2304         {
2305           /* We don't keep enough information to sort these cases out.
2306              The itbl support does keep this information however, although
2307              we currently don't support itbl fprmats as part of the cop
2308              instruction.  May want to add this support in the future.  */
2309         }
2310       /* Never set the bit for $0, which is always zero.  */
2311       mips_gprmask &= ~1 << 0;
2312     }
2313   else
2314     {
2315       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2316         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2317                               & MIPS16OP_MASK_RX);
2318       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2319         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2320                               & MIPS16OP_MASK_RY);
2321       if (pinfo & MIPS16_INSN_WRITE_Z)
2322         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2323                               & MIPS16OP_MASK_RZ);
2324       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2325         mips_gprmask |= 1 << TREG;
2326       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2327         mips_gprmask |= 1 << SP;
2328       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2329         mips_gprmask |= 1 << RA;
2330       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2331         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2332       if (pinfo & MIPS16_INSN_READ_Z)
2333         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2334                               & MIPS16OP_MASK_MOVE32Z);
2335       if (pinfo & MIPS16_INSN_READ_GPR_X)
2336         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2337                               & MIPS16OP_MASK_REGR32);
2338     }
2339
2340   if (place == NULL && ! mips_opts.noreorder)
2341     {
2342       /* Filling the branch delay slot is more complex.  We try to
2343          switch the branch with the previous instruction, which we can
2344          do if the previous instruction does not set up a condition
2345          that the branch tests and if the branch is not itself the
2346          target of any branch.  */
2347       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2348           || (pinfo & INSN_COND_BRANCH_DELAY))
2349         {
2350           if (mips_optimize < 2
2351               /* If we have seen .set volatile or .set nomove, don't
2352                  optimize.  */
2353               || mips_opts.nomove != 0
2354               /* If we had to emit any NOP instructions, then we
2355                  already know we can not swap.  */
2356               || nops != 0
2357               /* If we don't even know the previous insn, we can not
2358                  swap.  */
2359               || ! prev_insn_valid
2360               /* If the previous insn is already in a branch delay
2361                  slot, then we can not swap.  */
2362               || prev_insn_is_delay_slot
2363               /* If the previous previous insn was in a .set
2364                  noreorder, we can't swap.  Actually, the MIPS
2365                  assembler will swap in this situation.  However, gcc
2366                  configured -with-gnu-as will generate code like
2367                    .set noreorder
2368                    lw   $4,XXX
2369                    .set reorder
2370                    INSN
2371                    bne  $4,$0,foo
2372                  in which we can not swap the bne and INSN.  If gcc is
2373                  not configured -with-gnu-as, it does not output the
2374                  .set pseudo-ops.  We don't have to check
2375                  prev_insn_unreordered, because prev_insn_valid will
2376                  be 0 in that case.  We don't want to use
2377                  prev_prev_insn_valid, because we do want to be able
2378                  to swap at the start of a function.  */
2379               || prev_prev_insn_unreordered
2380               /* If the branch is itself the target of a branch, we
2381                  can not swap.  We cheat on this; all we check for is
2382                  whether there is a label on this instruction.  If
2383                  there are any branches to anything other than a
2384                  label, users must use .set noreorder.  */
2385               || insn_labels != NULL
2386               /* If the previous instruction is in a variant frag, we
2387                  can not do the swap.  This does not apply to the
2388                  mips16, which uses variant frags for different
2389                  purposes.  */
2390               || (! mips_opts.mips16
2391                   && prev_insn_frag->fr_type == rs_machine_dependent)
2392               /* If the branch reads the condition codes, we don't
2393                  even try to swap, because in the sequence
2394                    ctc1 $X,$31
2395                    INSN
2396                    INSN
2397                    bc1t LABEL
2398                  we can not swap, and I don't feel like handling that
2399                  case.  */
2400               || (! mips_opts.mips16
2401                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2402                   && (pinfo & INSN_READ_COND_CODE))
2403               /* We can not swap with an instruction that requires a
2404                  delay slot, becase the target of the branch might
2405                  interfere with that instruction.  */
2406               || (! mips_opts.mips16
2407                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2408                   && (prev_pinfo
2409               /* Itbl support may require additional care here.  */
2410                       & (INSN_LOAD_COPROC_DELAY
2411                          | INSN_COPROC_MOVE_DELAY
2412                          | INSN_WRITE_COND_CODE)))
2413               || (! (hilo_interlocks
2414                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2415                   && (prev_pinfo
2416                       & (INSN_READ_LO
2417                          | INSN_READ_HI)))
2418               || (! mips_opts.mips16
2419                   && ! gpr_interlocks
2420                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2421               || (! mips_opts.mips16
2422                   && mips_opts.isa == ISA_MIPS1
2423                   /* Itbl support may require additional care here.  */
2424                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2425               /* We can not swap with a branch instruction.  */
2426               || (prev_pinfo
2427                   & (INSN_UNCOND_BRANCH_DELAY
2428                      | INSN_COND_BRANCH_DELAY
2429                      | INSN_COND_BRANCH_LIKELY))
2430               /* We do not swap with a trap instruction, since it
2431                  complicates trap handlers to have the trap
2432                  instruction be in a delay slot.  */
2433               || (prev_pinfo & INSN_TRAP)
2434               /* If the branch reads a register that the previous
2435                  instruction sets, we can not swap.  */
2436               || (! mips_opts.mips16
2437                   && (prev_pinfo & INSN_WRITE_GPR_T)
2438                   && insn_uses_reg (ip,
2439                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2440                                      & OP_MASK_RT),
2441                                     MIPS_GR_REG))
2442               || (! mips_opts.mips16
2443                   && (prev_pinfo & INSN_WRITE_GPR_D)
2444                   && insn_uses_reg (ip,
2445                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2446                                      & OP_MASK_RD),
2447                                     MIPS_GR_REG))
2448               || (mips_opts.mips16
2449                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2450                        && insn_uses_reg (ip,
2451                                          ((prev_insn.insn_opcode
2452                                            >> MIPS16OP_SH_RX)
2453                                           & MIPS16OP_MASK_RX),
2454                                          MIPS16_REG))
2455                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2456                           && insn_uses_reg (ip,
2457                                             ((prev_insn.insn_opcode
2458                                               >> MIPS16OP_SH_RY)
2459                                              & MIPS16OP_MASK_RY),
2460                                             MIPS16_REG))
2461                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2462                           && insn_uses_reg (ip,
2463                                             ((prev_insn.insn_opcode
2464                                               >> MIPS16OP_SH_RZ)
2465                                              & MIPS16OP_MASK_RZ),
2466                                             MIPS16_REG))
2467                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2468                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2469                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2470                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2471                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2472                           && insn_uses_reg (ip,
2473                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2474                                                                      insn_opcode),
2475                                             MIPS_GR_REG))))
2476               /* If the branch writes a register that the previous
2477                  instruction sets, we can not swap (we know that
2478                  branches write only to RD or to $31).  */
2479               || (! mips_opts.mips16
2480                   && (prev_pinfo & INSN_WRITE_GPR_T)
2481                   && (((pinfo & INSN_WRITE_GPR_D)
2482                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2483                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2484                       || ((pinfo & INSN_WRITE_GPR_31)
2485                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2486                                & OP_MASK_RT)
2487                               == RA))))
2488               || (! mips_opts.mips16
2489                   && (prev_pinfo & INSN_WRITE_GPR_D)
2490                   && (((pinfo & INSN_WRITE_GPR_D)
2491                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2492                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2493                       || ((pinfo & INSN_WRITE_GPR_31)
2494                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2495                                & OP_MASK_RD)
2496                               == RA))))
2497               || (mips_opts.mips16
2498                   && (pinfo & MIPS16_INSN_WRITE_31)
2499                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2500                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2501                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2502                               == RA))))
2503               /* If the branch writes a register that the previous
2504                  instruction reads, we can not swap (we know that
2505                  branches only write to RD or to $31).  */
2506               || (! mips_opts.mips16
2507                   && (pinfo & INSN_WRITE_GPR_D)
2508                   && insn_uses_reg (&prev_insn,
2509                                     ((ip->insn_opcode >> OP_SH_RD)
2510                                      & OP_MASK_RD),
2511                                     MIPS_GR_REG))
2512               || (! mips_opts.mips16
2513                   && (pinfo & INSN_WRITE_GPR_31)
2514                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2515               || (mips_opts.mips16
2516                   && (pinfo & MIPS16_INSN_WRITE_31)
2517                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2518               /* If we are generating embedded PIC code, the branch
2519                  might be expanded into a sequence which uses $at, so
2520                  we can't swap with an instruction which reads it.  */
2521               || (mips_pic == EMBEDDED_PIC
2522                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2523               /* If the previous previous instruction has a load
2524                  delay, and sets a register that the branch reads, we
2525                  can not swap.  */
2526               || (! mips_opts.mips16
2527                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2528               /* Itbl support may require additional care here.  */
2529                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2530                       || (! gpr_interlocks
2531                           && (prev_prev_insn.insn_mo->pinfo
2532                               & INSN_LOAD_MEMORY_DELAY)))
2533                   && insn_uses_reg (ip,
2534                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2535                                      & OP_MASK_RT),
2536                                     MIPS_GR_REG))
2537               /* If one instruction sets a condition code and the
2538                  other one uses a condition code, we can not swap.  */
2539               || ((pinfo & INSN_READ_COND_CODE)
2540                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2541               || ((pinfo & INSN_WRITE_COND_CODE)
2542                   && (prev_pinfo & INSN_READ_COND_CODE))
2543               /* If the previous instruction uses the PC, we can not
2544                  swap.  */
2545               || (mips_opts.mips16
2546                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2547               /* If the previous instruction was extended, we can not
2548                  swap.  */
2549               || (mips_opts.mips16 && prev_insn_extended)
2550               /* If the previous instruction had a fixup in mips16
2551                  mode, we can not swap.  This normally means that the
2552                  previous instruction was a 4 byte branch anyhow.  */
2553               || (mips_opts.mips16 && prev_insn_fixp[0])
2554               /* If the previous instruction is a sync, sync.l, or
2555                  sync.p, we can not swap.  */
2556               || (prev_pinfo & INSN_SYNC))
2557             {
2558               /* We could do even better for unconditional branches to
2559                  portions of this object file; we could pick up the
2560                  instruction at the destination, put it in the delay
2561                  slot, and bump the destination address.  */
2562               emit_nop ();
2563               /* Update the previous insn information.  */
2564               prev_prev_insn = *ip;
2565               prev_insn.insn_mo = &dummy_opcode;
2566             }
2567           else
2568             {
2569               /* It looks like we can actually do the swap.  */
2570               if (! mips_opts.mips16)
2571                 {
2572                   char *prev_f;
2573                   char temp[4];
2574
2575                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2576                   memcpy (temp, prev_f, 4);
2577                   memcpy (prev_f, f, 4);
2578                   memcpy (f, temp, 4);
2579                   if (prev_insn_fixp[0])
2580                     {
2581                       prev_insn_fixp[0]->fx_frag = frag_now;
2582                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2583                     }
2584                   if (prev_insn_fixp[1])
2585                     {
2586                       prev_insn_fixp[1]->fx_frag = frag_now;
2587                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2588                     }
2589                   if (prev_insn_fixp[2])
2590                     {
2591                       prev_insn_fixp[2]->fx_frag = frag_now;
2592                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2593                     }
2594                   if (prev_insn_fixp[0] && HAVE_NEWABI
2595                       && prev_insn_frag != frag_now
2596                       && (prev_insn_fixp[0]->fx_r_type
2597                           == BFD_RELOC_MIPS_GOT_DISP
2598                           || (prev_insn_fixp[0]->fx_r_type
2599                               == BFD_RELOC_MIPS_CALL16)))
2600                     {
2601                       /* To avoid confusion in tc_gen_reloc, we must
2602                          ensure that this does not become a variant
2603                          frag.  */
2604                       force_new_frag = TRUE;
2605                     }
2606                   if (fixp[0])
2607                     {
2608                       fixp[0]->fx_frag = prev_insn_frag;
2609                       fixp[0]->fx_where = prev_insn_where;
2610                     }
2611                   if (fixp[1])
2612                     {
2613                       fixp[1]->fx_frag = prev_insn_frag;
2614                       fixp[1]->fx_where = prev_insn_where;
2615                     }
2616                   if (fixp[2])
2617                     {
2618                       fixp[2]->fx_frag = prev_insn_frag;
2619                       fixp[2]->fx_where = prev_insn_where;
2620                     }
2621                 }
2622               else
2623                 {
2624                   char *prev_f;
2625                   char temp[2];
2626
2627                   assert (prev_insn_fixp[0] == NULL);
2628                   assert (prev_insn_fixp[1] == NULL);
2629                   assert (prev_insn_fixp[2] == NULL);
2630                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2631                   memcpy (temp, prev_f, 2);
2632                   memcpy (prev_f, f, 2);
2633                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2634                     {
2635                       assert (*reloc_type == BFD_RELOC_UNUSED);
2636                       memcpy (f, temp, 2);
2637                     }
2638                   else
2639                     {
2640                       memcpy (f, f + 2, 2);
2641                       memcpy (f + 2, temp, 2);
2642                     }
2643                   if (fixp[0])
2644                     {
2645                       fixp[0]->fx_frag = prev_insn_frag;
2646                       fixp[0]->fx_where = prev_insn_where;
2647                     }
2648                   if (fixp[1])
2649                     {
2650                       fixp[1]->fx_frag = prev_insn_frag;
2651                       fixp[1]->fx_where = prev_insn_where;
2652                     }
2653                   if (fixp[2])
2654                     {
2655                       fixp[2]->fx_frag = prev_insn_frag;
2656                       fixp[2]->fx_where = prev_insn_where;
2657                     }
2658                 }
2659
2660               /* Update the previous insn information; leave prev_insn
2661                  unchanged.  */
2662               prev_prev_insn = *ip;
2663             }
2664           prev_insn_is_delay_slot = 1;
2665
2666           /* If that was an unconditional branch, forget the previous
2667              insn information.  */
2668           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2669             {
2670               prev_prev_insn.insn_mo = &dummy_opcode;
2671               prev_insn.insn_mo = &dummy_opcode;
2672             }
2673
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 if (pinfo & INSN_COND_BRANCH_LIKELY)
2683         {
2684           /* We don't yet optimize a branch likely.  What we should do
2685              is look at the target, copy the instruction found there
2686              into the delay slot, and increment the branch to jump to
2687              the next instruction.  */
2688           emit_nop ();
2689           /* Update the previous insn information.  */
2690           prev_prev_insn = *ip;
2691           prev_insn.insn_mo = &dummy_opcode;
2692           prev_insn_fixp[0] = NULL;
2693           prev_insn_fixp[1] = NULL;
2694           prev_insn_fixp[2] = NULL;
2695           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2696           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2697           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2698           prev_insn_extended = 0;
2699         }
2700       else
2701         {
2702           /* Update the previous insn information.  */
2703           if (nops > 0)
2704             prev_prev_insn.insn_mo = &dummy_opcode;
2705           else
2706             prev_prev_insn = prev_insn;
2707           prev_insn = *ip;
2708
2709           /* Any time we see a branch, we always fill the delay slot
2710              immediately; since this insn is not a branch, we know it
2711              is not in a delay slot.  */
2712           prev_insn_is_delay_slot = 0;
2713
2714           prev_insn_fixp[0] = fixp[0];
2715           prev_insn_fixp[1] = fixp[1];
2716           prev_insn_fixp[2] = fixp[2];
2717           prev_insn_reloc_type[0] = reloc_type[0];
2718           prev_insn_reloc_type[1] = reloc_type[1];
2719           prev_insn_reloc_type[2] = reloc_type[2];
2720           if (mips_opts.mips16)
2721             prev_insn_extended = (ip->use_extend
2722                                   || *reloc_type > BFD_RELOC_UNUSED);
2723         }
2724
2725       prev_prev_insn_unreordered = prev_insn_unreordered;
2726       prev_insn_unreordered = 0;
2727       prev_insn_frag = frag_now;
2728       prev_insn_where = f - frag_now->fr_literal;
2729       prev_insn_valid = 1;
2730     }
2731   else if (place == NULL)
2732     {
2733       /* We need to record a bit of information even when we are not
2734          reordering, in order to determine the base address for mips16
2735          PC relative relocs.  */
2736       prev_prev_insn = prev_insn;
2737       prev_insn = *ip;
2738       prev_insn_reloc_type[0] = reloc_type[0];
2739       prev_insn_reloc_type[1] = reloc_type[1];
2740       prev_insn_reloc_type[2] = reloc_type[2];
2741       prev_prev_insn_unreordered = prev_insn_unreordered;
2742       prev_insn_unreordered = 1;
2743     }
2744
2745   /* We just output an insn, so the next one doesn't have a label.  */
2746   mips_clear_insn_labels ();
2747
2748   /* We must ensure that the frag to which an instruction that was
2749      moved from a non-variant frag doesn't become a variant frag,
2750      otherwise tc_gen_reloc may get confused.  */
2751   if (force_new_frag)
2752     {
2753       frag_wane (frag_now);
2754       frag_new (0);
2755     }
2756 }
2757
2758 /* This function forgets that there was any previous instruction or
2759    label.  If PRESERVE is non-zero, it remembers enough information to
2760    know whether nops are needed before a noreorder section.  */
2761
2762 static void
2763 mips_no_prev_insn (preserve)
2764      int preserve;
2765 {
2766   if (! preserve)
2767     {
2768       prev_insn.insn_mo = &dummy_opcode;
2769       prev_prev_insn.insn_mo = &dummy_opcode;
2770       prev_nop_frag = NULL;
2771       prev_nop_frag_holds = 0;
2772       prev_nop_frag_required = 0;
2773       prev_nop_frag_since = 0;
2774     }
2775   prev_insn_valid = 0;
2776   prev_insn_is_delay_slot = 0;
2777   prev_insn_unreordered = 0;
2778   prev_insn_extended = 0;
2779   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2780   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2781   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2782   prev_prev_insn_unreordered = 0;
2783   mips_clear_insn_labels ();
2784 }
2785
2786 /* This function must be called whenever we turn on noreorder or emit
2787    something other than instructions.  It inserts any NOPS which might
2788    be needed by the previous instruction, and clears the information
2789    kept for the previous instructions.  The INSNS parameter is true if
2790    instructions are to follow.  */
2791
2792 static void
2793 mips_emit_delays (insns)
2794      bfd_boolean insns;
2795 {
2796   if (! mips_opts.noreorder)
2797     {
2798       int nops;
2799
2800       nops = 0;
2801       if ((! mips_opts.mips16
2802            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2803            && (! cop_interlocks
2804                && (prev_insn.insn_mo->pinfo
2805                    & (INSN_LOAD_COPROC_DELAY
2806                       | INSN_COPROC_MOVE_DELAY
2807                       | INSN_WRITE_COND_CODE))))
2808           || (! hilo_interlocks
2809               && (prev_insn.insn_mo->pinfo
2810                   & (INSN_READ_LO
2811                      | INSN_READ_HI)))
2812           || (! mips_opts.mips16
2813               && ! gpr_interlocks
2814               && (prev_insn.insn_mo->pinfo
2815                   & INSN_LOAD_MEMORY_DELAY))
2816           || (! mips_opts.mips16
2817               && mips_opts.isa == ISA_MIPS1
2818               && (prev_insn.insn_mo->pinfo
2819                   & INSN_COPROC_MEMORY_DELAY)))
2820         {
2821           /* Itbl support may require additional care here.  */
2822           ++nops;
2823           if ((! mips_opts.mips16
2824                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2825                && (! cop_interlocks
2826                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2827               || (! hilo_interlocks
2828                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2829                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2830             ++nops;
2831
2832           if (prev_insn_unreordered)
2833             nops = 0;
2834         }
2835       else if ((! mips_opts.mips16
2836                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2837                 && (! cop_interlocks
2838                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2839                || (! hilo_interlocks
2840                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2841                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2842         {
2843           /* Itbl support may require additional care here.  */
2844           if (! prev_prev_insn_unreordered)
2845             ++nops;
2846         }
2847
2848       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2849         {
2850           int min_nops = 0;
2851           const char *pn = prev_insn.insn_mo->name;
2852           if (strncmp(pn, "macc", 4) == 0
2853               || strncmp(pn, "dmacc", 5) == 0
2854               || strncmp(pn, "dmult", 5) == 0)
2855             {
2856               min_nops = 1;
2857             }
2858           if (nops < min_nops)
2859             nops = min_nops;
2860         }
2861
2862       if (nops > 0)
2863         {
2864           struct insn_label_list *l;
2865
2866           if (insns)
2867             {
2868               /* Record the frag which holds the nop instructions, so
2869                  that we can remove them if we don't need them.  */
2870               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2871               prev_nop_frag = frag_now;
2872               prev_nop_frag_holds = nops;
2873               prev_nop_frag_required = 0;
2874               prev_nop_frag_since = 0;
2875             }
2876
2877           for (; nops > 0; --nops)
2878             emit_nop ();
2879
2880           if (insns)
2881             {
2882               /* Move on to a new frag, so that it is safe to simply
2883                  decrease the size of prev_nop_frag.  */
2884               frag_wane (frag_now);
2885               frag_new (0);
2886             }
2887
2888           for (l = insn_labels; l != NULL; l = l->next)
2889             {
2890               valueT val;
2891
2892               assert (S_GET_SEGMENT (l->label) == now_seg);
2893               symbol_set_frag (l->label, frag_now);
2894               val = (valueT) frag_now_fix ();
2895               /* mips16 text labels are stored as odd.  */
2896               if (mips_opts.mips16)
2897                 ++val;
2898               S_SET_VALUE (l->label, val);
2899             }
2900         }
2901     }
2902
2903   /* Mark instruction labels in mips16 mode.  */
2904   if (insns)
2905     mips16_mark_labels ();
2906
2907   mips_no_prev_insn (insns);
2908 }
2909
2910 /* Build an instruction created by a macro expansion.  This is passed
2911    a pointer to the count of instructions created so far, an
2912    expression, the name of the instruction to build, an operand format
2913    string, and corresponding arguments.  */
2914
2915 #ifdef USE_STDARG
2916 static void
2917 macro_build (char *place,
2918              int *counter,
2919              expressionS * ep,
2920              const char *name,
2921              const char *fmt,
2922              ...)
2923 #else
2924 static void
2925 macro_build (place, counter, ep, name, fmt, va_alist)
2926      char *place;
2927      int *counter;
2928      expressionS *ep;
2929      const char *name;
2930      const char *fmt;
2931      va_dcl
2932 #endif
2933 {
2934   struct mips_cl_insn insn;
2935   bfd_reloc_code_real_type r[3];
2936   va_list args;
2937
2938 #ifdef USE_STDARG
2939   va_start (args, fmt);
2940 #else
2941   va_start (args);
2942 #endif
2943
2944   /*
2945    * If the macro is about to expand into a second instruction,
2946    * print a warning if needed. We need to pass ip as a parameter
2947    * to generate a better warning message here...
2948    */
2949   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2950     as_warn (_("Macro instruction expanded into multiple instructions"));
2951
2952   /*
2953    * If the macro is about to expand into a second instruction,
2954    * and it is in a delay slot, print a warning.
2955    */
2956   if (place == NULL
2957       && *counter == 1
2958       && mips_opts.noreorder
2959       && (prev_prev_insn.insn_mo->pinfo
2960           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2961              | INSN_COND_BRANCH_LIKELY)) != 0)
2962     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2963
2964   if (place == NULL)
2965     ++*counter;         /* bump instruction counter */
2966
2967   if (mips_opts.mips16)
2968     {
2969       mips16_macro_build (place, counter, ep, name, fmt, args);
2970       va_end (args);
2971       return;
2972     }
2973
2974   r[0] = BFD_RELOC_UNUSED;
2975   r[1] = BFD_RELOC_UNUSED;
2976   r[2] = BFD_RELOC_UNUSED;
2977   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2978   assert (insn.insn_mo);
2979   assert (strcmp (name, insn.insn_mo->name) == 0);
2980
2981   /* Search until we get a match for NAME.  */
2982   while (1)
2983     {
2984       /* It is assumed here that macros will never generate
2985          MDMX or MIPS-3D instructions.  */
2986       if (strcmp (fmt, insn.insn_mo->args) == 0
2987           && insn.insn_mo->pinfo != INSN_MACRO
2988           && OPCODE_IS_MEMBER (insn.insn_mo,
2989                                (mips_opts.isa
2990                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2991                                mips_opts.arch)
2992           && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2993         break;
2994
2995       ++insn.insn_mo;
2996       assert (insn.insn_mo->name);
2997       assert (strcmp (name, insn.insn_mo->name) == 0);
2998     }
2999
3000   insn.insn_opcode = insn.insn_mo->match;
3001   for (;;)
3002     {
3003       switch (*fmt++)
3004         {
3005         case '\0':
3006           break;
3007
3008         case ',':
3009         case '(':
3010         case ')':
3011           continue;
3012
3013         case 't':
3014         case 'w':
3015         case 'E':
3016           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3017           continue;
3018
3019         case 'c':
3020           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3021           continue;
3022
3023         case 'T':
3024         case 'W':
3025           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3026           continue;
3027
3028         case 'd':
3029         case 'G':
3030         case 'K':
3031           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3032           continue;
3033
3034         case 'U':
3035           {
3036             int tmp = va_arg (args, int);
3037
3038             insn.insn_opcode |= tmp << OP_SH_RT;
3039             insn.insn_opcode |= tmp << OP_SH_RD;
3040             continue;
3041           }
3042
3043         case 'V':
3044         case 'S':
3045           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3046           continue;
3047
3048         case 'z':
3049           continue;
3050
3051         case '<':
3052           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3053           continue;
3054
3055         case 'D':
3056           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3057           continue;
3058
3059         case 'B':
3060           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3061           continue;
3062
3063         case 'J':
3064           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3065           continue;
3066
3067         case 'q':
3068           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3069           continue;
3070
3071         case 'b':
3072         case 's':
3073         case 'r':
3074         case 'v':
3075           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3076           continue;
3077
3078         case 'i':
3079         case 'j':
3080         case 'o':
3081           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3082           assert (*r == BFD_RELOC_GPREL16
3083                   || *r == BFD_RELOC_MIPS_LITERAL
3084                   || *r == BFD_RELOC_MIPS_HIGHER
3085                   || *r == BFD_RELOC_HI16_S
3086                   || *r == BFD_RELOC_LO16
3087                   || *r == BFD_RELOC_MIPS_GOT16
3088                   || *r == BFD_RELOC_MIPS_CALL16
3089                   || *r == BFD_RELOC_MIPS_GOT_DISP
3090                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3091                   || *r == BFD_RELOC_MIPS_GOT_OFST
3092                   || *r == BFD_RELOC_MIPS_GOT_LO16
3093                   || *r == BFD_RELOC_MIPS_CALL_LO16
3094                   || (ep->X_op == O_subtract
3095                       && *r == BFD_RELOC_PCREL_LO16));
3096           continue;
3097
3098         case 'u':
3099           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3100           assert (ep != NULL
3101                   && (ep->X_op == O_constant
3102                       || (ep->X_op == O_symbol
3103                           && (*r == BFD_RELOC_MIPS_HIGHEST
3104                               || *r == BFD_RELOC_HI16_S
3105                               || *r == BFD_RELOC_HI16
3106                               || *r == BFD_RELOC_GPREL16
3107                               || *r == BFD_RELOC_MIPS_GOT_HI16
3108                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3109                       || (ep->X_op == O_subtract
3110                           && *r == BFD_RELOC_PCREL_HI16_S)));
3111           continue;
3112
3113         case 'p':
3114           assert (ep != NULL);
3115           /*
3116            * This allows macro() to pass an immediate expression for
3117            * creating short branches without creating a symbol.
3118            * Note that the expression still might come from the assembly
3119            * input, in which case the value is not checked for range nor
3120            * is a relocation entry generated (yuck).
3121            */
3122           if (ep->X_op == O_constant)
3123             {
3124               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3125               ep = NULL;
3126             }
3127           else
3128             *r = BFD_RELOC_16_PCREL_S2;
3129           continue;
3130
3131         case 'a':
3132           assert (ep != NULL);
3133           *r = BFD_RELOC_MIPS_JMP;
3134           continue;
3135
3136         case 'C':
3137           insn.insn_opcode |= va_arg (args, unsigned long);
3138           continue;
3139
3140         default:
3141           internalError ();
3142         }
3143       break;
3144     }
3145   va_end (args);
3146   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3147
3148   append_insn (place, &insn, ep, r);
3149 }
3150
3151 static void
3152 mips16_macro_build (place, counter, ep, name, fmt, args)
3153      char *place;
3154      int *counter ATTRIBUTE_UNUSED;
3155      expressionS *ep;
3156      const char *name;
3157      const char *fmt;
3158      va_list args;
3159 {
3160   struct mips_cl_insn insn;
3161   bfd_reloc_code_real_type r[3]
3162     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3163
3164   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3165   assert (insn.insn_mo);
3166   assert (strcmp (name, insn.insn_mo->name) == 0);
3167
3168   while (strcmp (fmt, insn.insn_mo->args) != 0
3169          || insn.insn_mo->pinfo == INSN_MACRO)
3170     {
3171       ++insn.insn_mo;
3172       assert (insn.insn_mo->name);
3173       assert (strcmp (name, insn.insn_mo->name) == 0);
3174     }
3175
3176   insn.insn_opcode = insn.insn_mo->match;
3177   insn.use_extend = FALSE;
3178
3179   for (;;)
3180     {
3181       int c;
3182
3183       c = *fmt++;
3184       switch (c)
3185         {
3186         case '\0':
3187           break;
3188
3189         case ',':
3190         case '(':
3191         case ')':
3192           continue;
3193
3194         case 'y':
3195         case 'w':
3196           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3197           continue;
3198
3199         case 'x':
3200         case 'v':
3201           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3202           continue;
3203
3204         case 'z':
3205           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3206           continue;
3207
3208         case 'Z':
3209           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3210           continue;
3211
3212         case '0':
3213         case 'S':
3214         case 'P':
3215         case 'R':
3216           continue;
3217
3218         case 'X':
3219           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3220           continue;
3221
3222         case 'Y':
3223           {
3224             int regno;
3225
3226             regno = va_arg (args, int);
3227             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3228             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3229           }
3230           continue;
3231
3232         case '<':
3233         case '>':
3234         case '4':
3235         case '5':
3236         case 'H':
3237         case 'W':
3238         case 'D':
3239         case 'j':
3240         case '8':
3241         case 'V':
3242         case 'C':
3243         case 'U':
3244         case 'k':
3245         case 'K':
3246         case 'p':
3247         case 'q':
3248           {
3249             assert (ep != NULL);
3250
3251             if (ep->X_op != O_constant)
3252               *r = (int) BFD_RELOC_UNUSED + c;
3253             else
3254               {
3255                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3256                               FALSE, &insn.insn_opcode, &insn.use_extend,
3257                               &insn.extend);
3258                 ep = NULL;
3259                 *r = BFD_RELOC_UNUSED;
3260               }
3261           }
3262           continue;
3263
3264         case '6':
3265           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3266           continue;
3267         }
3268
3269       break;
3270     }
3271
3272   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3273
3274   append_insn (place, &insn, ep, r);
3275 }
3276
3277 /*
3278  * Generate a "jalr" instruction with a relocation hint to the called
3279  * function.  This occurs in NewABI PIC code.
3280  */
3281 static void
3282 macro_build_jalr (icnt, ep)
3283      int icnt;
3284      expressionS *ep;
3285 {
3286   char *f = NULL;
3287
3288   if (HAVE_NEWABI)
3289     {
3290       frag_grow (4);
3291       f = frag_more (0);
3292     }
3293   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3294                RA, PIC_CALL_REG);
3295   if (HAVE_NEWABI)
3296     fix_new_exp (frag_now, f - frag_now->fr_literal,
3297                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3298 }
3299
3300 /*
3301  * Generate a "lui" instruction.
3302  */
3303 static void
3304 macro_build_lui (place, counter, ep, regnum)
3305      char *place;
3306      int *counter;
3307      expressionS *ep;
3308      int regnum;
3309 {
3310   expressionS high_expr;
3311   struct mips_cl_insn insn;
3312   bfd_reloc_code_real_type r[3]
3313     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3314   const char *name = "lui";
3315   const char *fmt = "t,u";
3316
3317   assert (! mips_opts.mips16);
3318
3319   if (place == NULL)
3320     high_expr = *ep;
3321   else
3322     {
3323       high_expr.X_op = O_constant;
3324       high_expr.X_add_number = ep->X_add_number;
3325     }
3326
3327   if (high_expr.X_op == O_constant)
3328     {
3329       /* we can compute the instruction now without a relocation entry */
3330       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3331                                 >> 16) & 0xffff;
3332       *r = BFD_RELOC_UNUSED;
3333     }
3334   else
3335     {
3336       assert (ep->X_op == O_symbol);
3337       /* _gp_disp is a special case, used from s_cpload.  */
3338       assert (mips_pic == NO_PIC
3339               || (! HAVE_NEWABI
3340                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3341       *r = BFD_RELOC_HI16_S;
3342     }
3343
3344   /*
3345    * If the macro is about to expand into a second instruction,
3346    * print a warning if needed. We need to pass ip as a parameter
3347    * to generate a better warning message here...
3348    */
3349   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3350     as_warn (_("Macro instruction expanded into multiple instructions"));
3351
3352   if (place == NULL)
3353     ++*counter;         /* bump instruction counter */
3354
3355   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3356   assert (insn.insn_mo);
3357   assert (strcmp (name, insn.insn_mo->name) == 0);
3358   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3359
3360   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3361   if (*r == BFD_RELOC_UNUSED)
3362     {
3363       insn.insn_opcode |= high_expr.X_add_number;
3364       append_insn (place, &insn, NULL, r);
3365     }
3366   else
3367     append_insn (place, &insn, &high_expr, r);
3368 }
3369
3370 /* Generate a sequence of instructions to do a load or store from a constant
3371    offset off of a base register (breg) into/from a target register (treg),
3372    using AT if necessary.  */
3373 static void
3374 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3375      char *place;
3376      int *counter;
3377      expressionS *ep;
3378      const char *op;
3379      int treg, breg;
3380 {
3381   assert (ep->X_op == O_constant);
3382
3383   /* Right now, this routine can only handle signed 32-bit contants.  */
3384   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3385     as_warn (_("operand overflow"));
3386
3387   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3388     {
3389       /* Signed 16-bit offset will fit in the op.  Easy!  */
3390       macro_build (place, counter, ep, op, "t,o(b)", treg,
3391                    (int) BFD_RELOC_LO16, breg);
3392     }
3393   else
3394     {
3395       /* 32-bit offset, need multiple instructions and AT, like:
3396            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3397            addu     $tempreg,$tempreg,$breg
3398            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3399          to handle the complete offset.  */
3400       macro_build_lui (place, counter, ep, AT);
3401       if (place != NULL)
3402         place += 4;
3403       macro_build (place, counter, (expressionS *) NULL, ADDRESS_ADD_INSN,
3404                    "d,v,t", AT, AT, breg);
3405       if (place != NULL)
3406         place += 4;
3407       macro_build (place, counter, ep, op, "t,o(b)", treg,
3408                    (int) BFD_RELOC_LO16, AT);
3409
3410       if (mips_opts.noat)
3411         as_warn (_("Macro used $at after \".set noat\""));
3412     }
3413 }
3414
3415 /*                      set_at()
3416  * Generates code to set the $at register to true (one)
3417  * if reg is less than the immediate expression.
3418  */
3419 static void
3420 set_at (counter, reg, unsignedp)
3421      int *counter;
3422      int reg;
3423      int unsignedp;
3424 {
3425   if (imm_expr.X_op == O_constant
3426       && imm_expr.X_add_number >= -0x8000
3427       && imm_expr.X_add_number < 0x8000)
3428     macro_build ((char *) NULL, counter, &imm_expr,
3429                  unsignedp ? "sltiu" : "slti",
3430                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3431   else
3432     {
3433       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3434       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3435                    unsignedp ? "sltu" : "slt",
3436                    "d,v,t", AT, reg, AT);
3437     }
3438 }
3439
3440 /* Warn if an expression is not a constant.  */
3441
3442 static void
3443 check_absolute_expr (ip, ex)
3444      struct mips_cl_insn *ip;
3445      expressionS *ex;
3446 {
3447   if (ex->X_op == O_big)
3448     as_bad (_("unsupported large constant"));
3449   else if (ex->X_op != O_constant)
3450     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3451 }
3452
3453 /* Count the leading zeroes by performing a binary chop. This is a
3454    bulky bit of source, but performance is a LOT better for the
3455    majority of values than a simple loop to count the bits:
3456        for (lcnt = 0; (lcnt < 32); lcnt++)
3457          if ((v) & (1 << (31 - lcnt)))
3458            break;
3459   However it is not code size friendly, and the gain will drop a bit
3460   on certain cached systems.
3461 */
3462 #define COUNT_TOP_ZEROES(v)             \
3463   (((v) & ~0xffff) == 0                 \
3464    ? ((v) & ~0xff) == 0                 \
3465      ? ((v) & ~0xf) == 0                \
3466        ? ((v) & ~0x3) == 0              \
3467          ? ((v) & ~0x1) == 0            \
3468            ? !(v)                       \
3469              ? 32                       \
3470              : 31                       \
3471            : 30                         \
3472          : ((v) & ~0x7) == 0            \
3473            ? 29                         \
3474            : 28                         \
3475        : ((v) & ~0x3f) == 0             \
3476          ? ((v) & ~0x1f) == 0           \
3477            ? 27                         \
3478            : 26                         \
3479          : ((v) & ~0x7f) == 0           \
3480            ? 25                         \
3481            : 24                         \
3482      : ((v) & ~0xfff) == 0              \
3483        ? ((v) & ~0x3ff) == 0            \
3484          ? ((v) & ~0x1ff) == 0          \
3485            ? 23                         \
3486            : 22                         \
3487          : ((v) & ~0x7ff) == 0          \
3488            ? 21                         \
3489            : 20                         \
3490        : ((v) & ~0x3fff) == 0           \
3491          ? ((v) & ~0x1fff) == 0         \
3492            ? 19                         \
3493            : 18                         \
3494          : ((v) & ~0x7fff) == 0         \
3495            ? 17                         \
3496            : 16                         \
3497    : ((v) & ~0xffffff) == 0             \
3498      ? ((v) & ~0xfffff) == 0            \
3499        ? ((v) & ~0x3ffff) == 0          \
3500          ? ((v) & ~0x1ffff) == 0        \
3501            ? 15                         \
3502            : 14                         \
3503          : ((v) & ~0x7ffff) == 0        \
3504            ? 13                         \
3505            : 12                         \
3506        : ((v) & ~0x3fffff) == 0         \
3507          ? ((v) & ~0x1fffff) == 0       \
3508            ? 11                         \
3509            : 10                         \
3510          : ((v) & ~0x7fffff) == 0       \
3511            ? 9                          \
3512            : 8                          \
3513      : ((v) & ~0xfffffff) == 0          \
3514        ? ((v) & ~0x3ffffff) == 0        \
3515          ? ((v) & ~0x1ffffff) == 0      \
3516            ? 7                          \
3517            : 6                          \
3518          : ((v) & ~0x7ffffff) == 0      \
3519            ? 5                          \
3520            : 4                          \
3521        : ((v) & ~0x3fffffff) == 0       \
3522          ? ((v) & ~0x1fffffff) == 0     \
3523            ? 3                          \
3524            : 2                          \
3525          : ((v) & ~0x7fffffff) == 0     \
3526            ? 1                          \
3527            : 0)
3528
3529 /*                      load_register()
3530  *  This routine generates the least number of instructions neccessary to load
3531  *  an absolute expression value into a register.
3532  */
3533 static void
3534 load_register (counter, reg, ep, dbl)
3535      int *counter;
3536      int reg;
3537      expressionS *ep;
3538      int dbl;
3539 {
3540   int freg;
3541   expressionS hi32, lo32;
3542
3543   if (ep->X_op != O_big)
3544     {
3545       assert (ep->X_op == O_constant);
3546       if (ep->X_add_number < 0x8000
3547           && (ep->X_add_number >= 0
3548               || (ep->X_add_number >= -0x8000
3549                   && (! dbl
3550                       || ! ep->X_unsigned
3551                       || sizeof (ep->X_add_number) > 4))))
3552         {
3553           /* We can handle 16 bit signed values with an addiu to
3554              $zero.  No need to ever use daddiu here, since $zero and
3555              the result are always correct in 32 bit mode.  */
3556           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3557                        (int) BFD_RELOC_LO16);
3558           return;
3559         }
3560       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3561         {
3562           /* We can handle 16 bit unsigned values with an ori to
3563              $zero.  */
3564           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3565                        (int) BFD_RELOC_LO16);
3566           return;
3567         }
3568       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3569                 && (! dbl
3570                     || ! ep->X_unsigned
3571                     || sizeof (ep->X_add_number) > 4
3572                     || (ep->X_add_number & 0x80000000) == 0))
3573                || ((HAVE_32BIT_GPRS || ! dbl)
3574                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3575                || (HAVE_32BIT_GPRS
3576                    && ! dbl
3577                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3578                        == ~ (offsetT) 0xffffffff)))
3579         {
3580           /* 32 bit values require an lui.  */
3581           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3582                        (int) BFD_RELOC_HI16);
3583           if ((ep->X_add_number & 0xffff) != 0)
3584             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3585                          (int) BFD_RELOC_LO16);
3586           return;
3587         }
3588     }
3589
3590   /* The value is larger than 32 bits.  */
3591
3592   if (HAVE_32BIT_GPRS)
3593     {
3594       as_bad (_("Number (0x%lx) larger than 32 bits"),
3595               (unsigned long) ep->X_add_number);
3596       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3597                    (int) BFD_RELOC_LO16);
3598       return;
3599     }
3600
3601   if (ep->X_op != O_big)
3602     {
3603       hi32 = *ep;
3604       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3605       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3606       hi32.X_add_number &= 0xffffffff;
3607       lo32 = *ep;
3608       lo32.X_add_number &= 0xffffffff;
3609     }
3610   else
3611     {
3612       assert (ep->X_add_number > 2);
3613       if (ep->X_add_number == 3)
3614         generic_bignum[3] = 0;
3615       else if (ep->X_add_number > 4)
3616         as_bad (_("Number larger than 64 bits"));
3617       lo32.X_op = O_constant;
3618       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3619       hi32.X_op = O_constant;
3620       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3621     }
3622
3623   if (hi32.X_add_number == 0)
3624     freg = 0;
3625   else
3626     {
3627       int shift, bit;
3628       unsigned long hi, lo;
3629
3630       if (hi32.X_add_number == (offsetT) 0xffffffff)
3631         {
3632           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3633             {
3634               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3635                            reg, 0, (int) BFD_RELOC_LO16);
3636               return;
3637             }
3638           if (lo32.X_add_number & 0x80000000)
3639             {
3640               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3641                            (int) BFD_RELOC_HI16);
3642               if (lo32.X_add_number & 0xffff)
3643                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3644                              reg, reg, (int) BFD_RELOC_LO16);
3645               return;
3646             }
3647         }
3648
3649       /* Check for 16bit shifted constant.  We know that hi32 is
3650          non-zero, so start the mask on the first bit of the hi32
3651          value.  */
3652       shift = 17;
3653       do
3654         {
3655           unsigned long himask, lomask;
3656
3657           if (shift < 32)
3658             {
3659               himask = 0xffff >> (32 - shift);
3660               lomask = (0xffff << shift) & 0xffffffff;
3661             }
3662           else
3663             {
3664               himask = 0xffff << (shift - 32);
3665               lomask = 0;
3666             }
3667           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3668               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3669             {
3670               expressionS tmp;
3671
3672               tmp.X_op = O_constant;
3673               if (shift < 32)
3674                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3675                                     | (lo32.X_add_number >> shift));
3676               else
3677                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3678               macro_build ((char *) NULL, counter, &tmp,
3679                            "ori", "t,r,i", reg, 0,
3680                            (int) BFD_RELOC_LO16);
3681               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3682                            (shift >= 32) ? "dsll32" : "dsll",
3683                            "d,w,<", reg, reg,
3684                            (shift >= 32) ? shift - 32 : shift);
3685               return;
3686             }
3687           ++shift;
3688         }
3689       while (shift <= (64 - 16));
3690
3691       /* Find the bit number of the lowest one bit, and store the
3692          shifted value in hi/lo.  */
3693       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3694       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3695       if (lo != 0)
3696         {
3697           bit = 0;
3698           while ((lo & 1) == 0)
3699             {
3700               lo >>= 1;
3701               ++bit;
3702             }
3703           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3704           hi >>= bit;
3705         }
3706       else
3707         {
3708           bit = 32;
3709           while ((hi & 1) == 0)
3710             {
3711               hi >>= 1;
3712               ++bit;
3713             }
3714           lo = hi;
3715           hi = 0;
3716         }
3717
3718       /* Optimize if the shifted value is a (power of 2) - 1.  */
3719       if ((hi == 0 && ((lo + 1) & lo) == 0)
3720           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3721         {
3722           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3723           if (shift != 0)
3724             {
3725               expressionS tmp;
3726
3727               /* This instruction will set the register to be all
3728                  ones.  */
3729               tmp.X_op = O_constant;
3730               tmp.X_add_number = (offsetT) -1;
3731               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3732                            reg, 0, (int) BFD_RELOC_LO16);
3733               if (bit != 0)
3734                 {
3735                   bit += shift;
3736                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3737                                (bit >= 32) ? "dsll32" : "dsll",
3738                                "d,w,<", reg, reg,
3739                                (bit >= 32) ? bit - 32 : bit);
3740                 }
3741               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3742                            (shift >= 32) ? "dsrl32" : "dsrl",
3743                            "d,w,<", reg, reg,
3744                            (shift >= 32) ? shift - 32 : shift);
3745               return;
3746             }
3747         }
3748
3749       /* Sign extend hi32 before calling load_register, because we can
3750          generally get better code when we load a sign extended value.  */
3751       if ((hi32.X_add_number & 0x80000000) != 0)
3752         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3753       load_register (counter, reg, &hi32, 0);
3754       freg = reg;
3755     }
3756   if ((lo32.X_add_number & 0xffff0000) == 0)
3757     {
3758       if (freg != 0)
3759         {
3760           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3761                        "dsll32", "d,w,<", reg, freg, 0);
3762           freg = reg;
3763         }
3764     }
3765   else
3766     {
3767       expressionS mid16;
3768
3769       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3770         {
3771           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3772                        (int) BFD_RELOC_HI16);
3773           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3774                        "dsrl32", "d,w,<", reg, reg, 0);
3775           return;
3776         }
3777
3778       if (freg != 0)
3779         {
3780           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3781                        "d,w,<", reg, freg, 16);
3782           freg = reg;
3783         }
3784       mid16 = lo32;
3785       mid16.X_add_number >>= 16;
3786       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3787                    freg, (int) BFD_RELOC_LO16);
3788       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3789                    "d,w,<", reg, reg, 16);
3790       freg = reg;
3791     }
3792   if ((lo32.X_add_number & 0xffff) != 0)
3793     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3794                  (int) BFD_RELOC_LO16);
3795 }
3796
3797 /* Load an address into a register.  */
3798
3799 static void
3800 load_address (counter, reg, ep, used_at)
3801      int *counter;
3802      int reg;
3803      expressionS *ep;
3804      int *used_at;
3805 {
3806   char *p = NULL;
3807
3808   if (ep->X_op != O_constant
3809       && ep->X_op != O_symbol)
3810     {
3811       as_bad (_("expression too complex"));
3812       ep->X_op = O_constant;
3813     }
3814
3815   if (ep->X_op == O_constant)
3816     {
3817       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3818       return;
3819     }
3820
3821   if (mips_pic == NO_PIC)
3822     {
3823       /* If this is a reference to a GP relative symbol, we want
3824            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3825          Otherwise we want
3826            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3827            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3828          If we have an addend, we always use the latter form.
3829
3830          With 64bit address space and a usable $at we want
3831            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3832            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3833            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3834            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3835            dsll32       $reg,0
3836            daddu        $reg,$reg,$at
3837
3838          If $at is already in use, we use a path which is suboptimal
3839          on superscalar processors.
3840            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3841            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3842            dsll         $reg,16
3843            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3844            dsll         $reg,16
3845            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3846        */
3847       if (HAVE_64BIT_ADDRESSES)
3848         {
3849           /* We don't do GP optimization for now because RELAX_ENCODE can't
3850              hold the data for such large chunks.  */
3851
3852           if (*used_at == 0 && ! mips_opts.noat)
3853             {
3854               macro_build (p, counter, ep, "lui", "t,u",
3855                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3856               macro_build (p, counter, ep, "lui", "t,u",
3857                            AT, (int) BFD_RELOC_HI16_S);
3858               macro_build (p, counter, ep, "daddiu", "t,r,j",
3859                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3860               macro_build (p, counter, ep, "daddiu", "t,r,j",
3861                            AT, AT, (int) BFD_RELOC_LO16);
3862               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3863                            "d,w,<", reg, reg, 0);
3864               macro_build (p, counter, (expressionS *) NULL, "daddu",
3865                            "d,v,t", reg, reg, AT);
3866               *used_at = 1;
3867             }
3868           else
3869             {
3870               macro_build (p, counter, ep, "lui", "t,u",
3871                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3872               macro_build (p, counter, ep, "daddiu", "t,r,j",
3873                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3874               macro_build (p, counter, (expressionS *) NULL, "dsll",
3875                            "d,w,<", reg, reg, 16);
3876               macro_build (p, counter, ep, "daddiu", "t,r,j",
3877                            reg, reg, (int) BFD_RELOC_HI16_S);
3878               macro_build (p, counter, (expressionS *) NULL, "dsll",
3879                            "d,w,<", reg, reg, 16);
3880               macro_build (p, counter, ep, "daddiu", "t,r,j",
3881                            reg, reg, (int) BFD_RELOC_LO16);
3882             }
3883         }
3884       else
3885         {
3886           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3887               && ! nopic_need_relax (ep->X_add_symbol, 1))
3888             {
3889               frag_grow (20);
3890               macro_build ((char *) NULL, counter, ep, ADDRESS_ADDI_INSN,
3891                            "t,r,j", reg, mips_gp_register,
3892                            (int) BFD_RELOC_GPREL16);
3893               p = frag_var (rs_machine_dependent, 8, 0,
3894                             RELAX_ENCODE (4, 8, 0, 4, 0,
3895                                           mips_opts.warn_about_macros),
3896                             ep->X_add_symbol, 0, NULL);
3897             }
3898           macro_build_lui (p, counter, ep, reg);
3899           if (p != NULL)
3900             p += 4;
3901           macro_build (p, counter, ep, ADDRESS_ADDI_INSN,
3902                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3903         }
3904     }
3905   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3906     {
3907       expressionS ex;
3908
3909       /* If this is a reference to an external symbol, we want
3910            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3911          Otherwise we want
3912            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3913            nop
3914            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3915          If there is a constant, it must be added in after.
3916
3917          If we have NewABI, we want
3918            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3919          unless we're referencing a global symbol with a non-zero
3920          offset, in which case cst must be added separately.  */
3921       if (HAVE_NEWABI)
3922         {
3923           frag_grow (12);
3924
3925           if (ep->X_add_number)
3926             {
3927               frag_now->tc_frag_data.tc_fr_offset =
3928                 ex.X_add_number = ep->X_add_number;
3929               ep->X_add_number = 0;
3930               macro_build ((char *) NULL, counter, ep, ADDRESS_LOAD_INSN,
3931                            "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_DISP,
3932                            mips_gp_register);
3933               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3934                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3935               ex.X_op = O_constant;
3936               macro_build ((char *) NULL, counter, &ex, ADDRESS_ADDI_INSN,
3937                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3938               p = frag_var (rs_machine_dependent, 8, 0,
3939                             RELAX_ENCODE (8, 4, 0, 0, 0,
3940                                           mips_opts.warn_about_macros),
3941                             ep->X_add_symbol, 0, (char *) NULL);
3942               ep->X_add_number = ex.X_add_number;
3943             }
3944
3945           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3946                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3947
3948           if (! p)
3949             {
3950               /* To avoid confusion in tc_gen_reloc, we must ensure
3951                  that this does not become a variant frag.  */
3952               frag_wane (frag_now);
3953               frag_new (0);
3954             }
3955         }
3956       else
3957         {
3958           ex.X_add_number = ep->X_add_number;
3959           ep->X_add_number = 0;
3960           frag_grow (20);
3961           macro_build ((char *) NULL, counter, ep, ADDRESS_LOAD_INSN,
3962                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16,
3963                        mips_gp_register);
3964           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3965           p = frag_var (rs_machine_dependent, 4, 0,
3966                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3967                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3968           macro_build (p, counter, ep, ADDRESS_ADDI_INSN,
3969                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3970
3971           if (ex.X_add_number != 0)
3972             {
3973               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3974                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3975               ex.X_op = O_constant;
3976               macro_build ((char *) NULL, counter, &ex, ADDRESS_ADDI_INSN,
3977                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3978             }
3979         }
3980     }
3981   else if (mips_pic == SVR4_PIC)
3982     {
3983       expressionS ex;
3984       int off;
3985
3986       /* This is the large GOT case.  If this is a reference to an
3987          external symbol, we want
3988            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3989            addu         $reg,$reg,$gp
3990            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3991
3992          Otherwise, for a reference to a local symbol in old ABI, we want
3993            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3994            nop
3995            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3996          If there is a constant, it must be added in after.
3997
3998          In the NewABI, for local symbols, with or without offsets, we want:
3999            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
4000            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
4001       */
4002       if (HAVE_NEWABI)
4003         {
4004           frag_grow (24);
4005
4006           frag_now->tc_frag_data.tc_fr_offset =
4007             ex.X_add_number = ep->X_add_number;
4008           ep->X_add_number = 0;
4009           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4010                        (int) BFD_RELOC_MIPS_GOT_HI16);
4011           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4012                        ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
4013           macro_build ((char *) NULL, counter, ep, ADDRESS_LOAD_INSN,
4014                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4015           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4016             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4017           else if (ex.X_add_number)
4018             {
4019               ex.X_op = O_constant;
4020               macro_build ((char *) NULL, counter, &ex, ADDRESS_ADDI_INSN,
4021                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4022             }
4023
4024           ep->X_add_number = ex.X_add_number;
4025           p = frag_var (rs_machine_dependent, 8, 0,
4026                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
4027                                       mips_opts.warn_about_macros),
4028                         ep->X_add_symbol, 0, (char *) NULL);
4029           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4030                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4031           macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j",
4032                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
4033         }
4034       else
4035         {
4036           ex.X_add_number = ep->X_add_number;
4037           ep->X_add_number = 0;
4038           if (reg_needs_delay (mips_gp_register))
4039             off = 4;
4040           else
4041             off = 0;
4042           frag_grow (32);
4043           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4044                        (int) BFD_RELOC_MIPS_GOT_HI16);
4045           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4046                        ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
4047           macro_build ((char *) NULL, counter, ep, ADDRESS_LOAD_INSN,
4048                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4049           p = frag_var (rs_machine_dependent, 12 + off, 0,
4050                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4051                                       mips_opts.warn_about_macros),
4052                         ep->X_add_symbol, 0, NULL);
4053           if (off > 0)
4054             {
4055               /* We need a nop before loading from $gp.  This special
4056                  check is required because the lui which starts the main
4057                  instruction stream does not refer to $gp, and so will not
4058                  insert the nop which may be required.  */
4059               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4060                 p += 4;
4061             }
4062           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4063                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4064           p += 4;
4065           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4066           p += 4;
4067           macro_build (p, counter, ep, ADDRESS_ADDI_INSN,
4068                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4069
4070           if (ex.X_add_number != 0)
4071             {
4072               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4073                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4074               ex.X_op = O_constant;
4075               macro_build ((char *) NULL, counter, &ex, ADDRESS_ADDI_INSN,
4076                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4077             }
4078         }
4079     }
4080   else if (mips_pic == EMBEDDED_PIC)
4081     {
4082       /* We always do
4083            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4084        */
4085       macro_build ((char *) NULL, counter, ep, ADDRESS_ADDI_INSN,
4086                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4087     }
4088   else
4089     abort ();
4090 }
4091
4092 /* Move the contents of register SOURCE into register DEST.  */
4093
4094 static void
4095 move_register (counter, dest, source)
4096      int *counter;
4097      int dest;
4098      int source;
4099 {
4100   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4101                HAVE_32BIT_GPRS ? "addu" : "daddu",
4102                "d,v,t", dest, source, 0);
4103 }
4104
4105 /*
4106  *                      Build macros
4107  *   This routine implements the seemingly endless macro or synthesized
4108  * instructions and addressing modes in the mips assembly language. Many
4109  * of these macros are simple and are similar to each other. These could
4110  * probably be handled by some kind of table or grammer aproach instead of
4111  * this verbose method. Others are not simple macros but are more like
4112  * optimizing code generation.
4113  *   One interesting optimization is when several store macros appear
4114  * consecutivly that would load AT with the upper half of the same address.
4115  * The ensuing load upper instructions are ommited. This implies some kind
4116  * of global optimization. We currently only optimize within a single macro.
4117  *   For many of the load and store macros if the address is specified as a
4118  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4119  * first load register 'at' with zero and use it as the base register. The
4120  * mips assembler simply uses register $zero. Just one tiny optimization
4121  * we're missing.
4122  */
4123 static void
4124 macro (ip)
4125      struct mips_cl_insn *ip;
4126 {
4127   register int treg, sreg, dreg, breg;
4128   int tempreg;
4129   int mask;
4130   int icnt = 0;
4131   int used_at = 0;
4132   expressionS expr1;
4133   const char *s;
4134   const char *s2;
4135   const char *fmt;
4136   int likely = 0;
4137   int dbl = 0;
4138   int coproc = 0;
4139   int lr = 0;
4140   int imm = 0;
4141   offsetT maxnum;
4142   int off;
4143   bfd_reloc_code_real_type r;
4144   int hold_mips_optimize;
4145
4146   assert (! mips_opts.mips16);
4147
4148   treg = (ip->insn_opcode >> 16) & 0x1f;
4149   dreg = (ip->insn_opcode >> 11) & 0x1f;
4150   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4151   mask = ip->insn_mo->mask;
4152
4153   expr1.X_op = O_constant;
4154   expr1.X_op_symbol = NULL;
4155   expr1.X_add_symbol = NULL;
4156   expr1.X_add_number = 1;
4157
4158   /* Umatched fixups should not be put in the same frag as a relaxable
4159      macro.  For example, suppose we have:
4160
4161         lui $4,%hi(l1)          # 1
4162         la $5,l2                # 2
4163         addiu $4,$4,%lo(l1)     # 3
4164
4165      If instructions 1 and 2 were put in the same frag, md_frob_file would
4166      move the fixup for #1 after the fixups for the "unrelaxed" version of
4167      #2.  This would confuse tc_gen_reloc, which expects the relocations
4168      for #2 to be the last for that frag.
4169
4170      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4171      it assumes that they belong to a relaxable macro.  We mustn't put
4172      other uses of such relocations into a variant frag.
4173
4174      To avoid both problems, finish the current frag it contains a
4175      %reloc() operator.  The macro then goes into a new frag.  */
4176   if (prev_reloc_op_frag == frag_now)
4177     {
4178       frag_wane (frag_now);
4179       frag_new (0);
4180     }
4181
4182   switch (mask)
4183     {
4184     case M_DABS:
4185       dbl = 1;
4186     case M_ABS:
4187       /* bgez $a0,.+12
4188          move v0,$a0
4189          sub v0,$zero,$a0
4190          */
4191
4192       mips_emit_delays (TRUE);
4193       ++mips_opts.noreorder;
4194       mips_any_noreorder = 1;
4195
4196       expr1.X_add_number = 8;
4197       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4198       if (dreg == sreg)
4199         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4200                      0);
4201       else
4202         move_register (&icnt, dreg, sreg);
4203       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4204                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4205
4206       --mips_opts.noreorder;
4207       return;
4208
4209     case M_ADD_I:
4210       s = "addi";
4211       s2 = "add";
4212       goto do_addi;
4213     case M_ADDU_I:
4214       s = "addiu";
4215       s2 = "addu";
4216       goto do_addi;
4217     case M_DADD_I:
4218       dbl = 1;
4219       s = "daddi";
4220       s2 = "dadd";
4221       goto do_addi;
4222     case M_DADDU_I:
4223       dbl = 1;
4224       s = "daddiu";
4225       s2 = "daddu";
4226     do_addi:
4227       if (imm_expr.X_op == O_constant
4228           && imm_expr.X_add_number >= -0x8000
4229           && imm_expr.X_add_number < 0x8000)
4230         {
4231           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4232                        (int) BFD_RELOC_LO16);
4233           return;
4234         }
4235       load_register (&icnt, AT, &imm_expr, dbl);
4236       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4237                    treg, sreg, AT);
4238       break;
4239
4240     case M_AND_I:
4241       s = "andi";
4242       s2 = "and";
4243       goto do_bit;
4244     case M_OR_I:
4245       s = "ori";
4246       s2 = "or";
4247       goto do_bit;
4248     case M_NOR_I:
4249       s = "";
4250       s2 = "nor";
4251       goto do_bit;
4252     case M_XOR_I:
4253       s = "xori";
4254       s2 = "xor";
4255     do_bit:
4256       if (imm_expr.X_op == O_constant
4257           && imm_expr.X_add_number >= 0
4258           && imm_expr.X_add_number < 0x10000)
4259         {
4260           if (mask != M_NOR_I)
4261             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4262                          sreg, (int) BFD_RELOC_LO16);
4263           else
4264             {
4265               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4266                            treg, sreg, (int) BFD_RELOC_LO16);
4267               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4268                            "d,v,t", treg, treg, 0);
4269             }
4270           return;
4271         }
4272
4273       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4274       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4275                    treg, sreg, AT);
4276       break;
4277
4278     case M_BEQ_I:
4279       s = "beq";
4280       goto beq_i;
4281     case M_BEQL_I:
4282       s = "beql";
4283       likely = 1;
4284       goto beq_i;
4285     case M_BNE_I:
4286       s = "bne";
4287       goto beq_i;
4288     case M_BNEL_I:
4289       s = "bnel";
4290       likely = 1;
4291     beq_i:
4292       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4293         {
4294           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4295                        0);
4296           return;
4297         }
4298       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4299       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4300       break;
4301
4302     case M_BGEL:
4303       likely = 1;
4304     case M_BGE:
4305       if (treg == 0)
4306         {
4307           macro_build ((char *) NULL, &icnt, &offset_expr,
4308                        likely ? "bgezl" : "bgez", "s,p", sreg);
4309           return;
4310         }
4311       if (sreg == 0)
4312         {
4313           macro_build ((char *) NULL, &icnt, &offset_expr,
4314                        likely ? "blezl" : "blez", "s,p", treg);
4315           return;
4316         }
4317       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4318                    AT, sreg, treg);
4319       macro_build ((char *) NULL, &icnt, &offset_expr,
4320                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4321       break;
4322
4323     case M_BGTL_I:
4324       likely = 1;
4325     case M_BGT_I:
4326       /* check for > max integer */
4327       maxnum = 0x7fffffff;
4328       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4329         {
4330           maxnum <<= 16;
4331           maxnum |= 0xffff;
4332           maxnum <<= 16;
4333           maxnum |= 0xffff;
4334         }
4335       if (imm_expr.X_op == O_constant
4336           && imm_expr.X_add_number >= maxnum
4337           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4338         {
4339         do_false:
4340           /* result is always false */
4341           if (! likely)
4342             {
4343               if (warn_nops)
4344                 as_warn (_("Branch %s is always false (nop)"),
4345                          ip->insn_mo->name);
4346               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4347                            "", 0);
4348             }
4349           else
4350             {
4351               if (warn_nops)
4352                 as_warn (_("Branch likely %s is always false"),
4353                          ip->insn_mo->name);
4354               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4355                            "s,t,p", 0, 0);
4356             }
4357           return;
4358         }
4359       if (imm_expr.X_op != O_constant)
4360         as_bad (_("Unsupported large constant"));
4361       ++imm_expr.X_add_number;
4362       /* FALLTHROUGH */
4363     case M_BGE_I:
4364     case M_BGEL_I:
4365       if (mask == M_BGEL_I)
4366         likely = 1;
4367       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4368         {
4369           macro_build ((char *) NULL, &icnt, &offset_expr,
4370                        likely ? "bgezl" : "bgez", "s,p", sreg);
4371           return;
4372         }
4373       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4374         {
4375           macro_build ((char *) NULL, &icnt, &offset_expr,
4376                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4377           return;
4378         }
4379       maxnum = 0x7fffffff;
4380       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4381         {
4382           maxnum <<= 16;
4383           maxnum |= 0xffff;
4384           maxnum <<= 16;
4385           maxnum |= 0xffff;
4386         }
4387       maxnum = - maxnum - 1;
4388       if (imm_expr.X_op == O_constant
4389           && imm_expr.X_add_number <= maxnum
4390           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4391         {
4392         do_true:
4393           /* result is always true */
4394           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4395           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4396           return;
4397         }
4398       set_at (&icnt, sreg, 0);
4399       macro_build ((char *) NULL, &icnt, &offset_expr,
4400                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4401       break;
4402
4403     case M_BGEUL:
4404       likely = 1;
4405     case M_BGEU:
4406       if (treg == 0)
4407         goto do_true;
4408       if (sreg == 0)
4409         {
4410           macro_build ((char *) NULL, &icnt, &offset_expr,
4411                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4412           return;
4413         }
4414       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4415                    "d,v,t", AT, sreg, treg);
4416       macro_build ((char *) NULL, &icnt, &offset_expr,
4417                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4418       break;
4419
4420     case M_BGTUL_I:
4421       likely = 1;
4422     case M_BGTU_I:
4423       if (sreg == 0
4424           || (HAVE_32BIT_GPRS
4425               && imm_expr.X_op == O_constant
4426               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4427         goto do_false;
4428       if (imm_expr.X_op != O_constant)
4429         as_bad (_("Unsupported large constant"));
4430       ++imm_expr.X_add_number;
4431       /* FALLTHROUGH */
4432     case M_BGEU_I:
4433     case M_BGEUL_I:
4434       if (mask == M_BGEUL_I)
4435         likely = 1;
4436       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4437         goto do_true;
4438       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4439         {
4440           macro_build ((char *) NULL, &icnt, &offset_expr,
4441                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4442           return;
4443         }
4444       set_at (&icnt, sreg, 1);
4445       macro_build ((char *) NULL, &icnt, &offset_expr,
4446                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4447       break;
4448
4449     case M_BGTL:
4450       likely = 1;
4451     case M_BGT:
4452       if (treg == 0)
4453         {
4454           macro_build ((char *) NULL, &icnt, &offset_expr,
4455                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4456           return;
4457         }
4458       if (sreg == 0)
4459         {
4460           macro_build ((char *) NULL, &icnt, &offset_expr,
4461                        likely ? "bltzl" : "bltz", "s,p", treg);
4462           return;
4463         }
4464       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4465                    AT, treg, sreg);
4466       macro_build ((char *) NULL, &icnt, &offset_expr,
4467                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4468       break;
4469
4470     case M_BGTUL:
4471       likely = 1;
4472     case M_BGTU:
4473       if (treg == 0)
4474         {
4475           macro_build ((char *) NULL, &icnt, &offset_expr,
4476                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4477           return;
4478         }
4479       if (sreg == 0)
4480         goto do_false;
4481       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4482                    "d,v,t", AT, treg, sreg);
4483       macro_build ((char *) NULL, &icnt, &offset_expr,
4484                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4485       break;
4486
4487     case M_BLEL:
4488       likely = 1;
4489     case M_BLE:
4490       if (treg == 0)
4491         {
4492           macro_build ((char *) NULL, &icnt, &offset_expr,
4493                        likely ? "blezl" : "blez", "s,p", sreg);
4494           return;
4495         }
4496       if (sreg == 0)
4497         {
4498           macro_build ((char *) NULL, &icnt, &offset_expr,
4499                        likely ? "bgezl" : "bgez", "s,p", treg);
4500           return;
4501         }
4502       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4503                    AT, treg, sreg);
4504       macro_build ((char *) NULL, &icnt, &offset_expr,
4505                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4506       break;
4507
4508     case M_BLEL_I:
4509       likely = 1;
4510     case M_BLE_I:
4511       maxnum = 0x7fffffff;
4512       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4513         {
4514           maxnum <<= 16;
4515           maxnum |= 0xffff;
4516           maxnum <<= 16;
4517           maxnum |= 0xffff;
4518         }
4519       if (imm_expr.X_op == O_constant
4520           && imm_expr.X_add_number >= maxnum
4521           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4522         goto do_true;
4523       if (imm_expr.X_op != O_constant)
4524         as_bad (_("Unsupported large constant"));
4525       ++imm_expr.X_add_number;
4526       /* FALLTHROUGH */
4527     case M_BLT_I:
4528     case M_BLTL_I:
4529       if (mask == M_BLTL_I)
4530         likely = 1;
4531       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4532         {
4533           macro_build ((char *) NULL, &icnt, &offset_expr,
4534                        likely ? "bltzl" : "bltz", "s,p", sreg);
4535           return;
4536         }
4537       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4538         {
4539           macro_build ((char *) NULL, &icnt, &offset_expr,
4540                        likely ? "blezl" : "blez", "s,p", sreg);
4541           return;
4542         }
4543       set_at (&icnt, sreg, 0);
4544       macro_build ((char *) NULL, &icnt, &offset_expr,
4545                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4546       break;
4547
4548     case M_BLEUL:
4549       likely = 1;
4550     case M_BLEU:
4551       if (treg == 0)
4552         {
4553           macro_build ((char *) NULL, &icnt, &offset_expr,
4554                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4555           return;
4556         }
4557       if (sreg == 0)
4558         goto do_true;
4559       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4560                    "d,v,t", AT, treg, sreg);
4561       macro_build ((char *) NULL, &icnt, &offset_expr,
4562                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4563       break;
4564
4565     case M_BLEUL_I:
4566       likely = 1;
4567     case M_BLEU_I:
4568       if (sreg == 0
4569           || (HAVE_32BIT_GPRS
4570               && imm_expr.X_op == O_constant
4571               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4572         goto do_true;
4573       if (imm_expr.X_op != O_constant)
4574         as_bad (_("Unsupported large constant"));
4575       ++imm_expr.X_add_number;
4576       /* FALLTHROUGH */
4577     case M_BLTU_I:
4578     case M_BLTUL_I:
4579       if (mask == M_BLTUL_I)
4580         likely = 1;
4581       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4582         goto do_false;
4583       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4584         {
4585           macro_build ((char *) NULL, &icnt, &offset_expr,
4586                        likely ? "beql" : "beq",
4587                        "s,t,p", sreg, 0);
4588           return;
4589         }
4590       set_at (&icnt, sreg, 1);
4591       macro_build ((char *) NULL, &icnt, &offset_expr,
4592                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4593       break;
4594
4595     case M_BLTL:
4596       likely = 1;
4597     case M_BLT:
4598       if (treg == 0)
4599         {
4600           macro_build ((char *) NULL, &icnt, &offset_expr,
4601                        likely ? "bltzl" : "bltz", "s,p", sreg);
4602           return;
4603         }
4604       if (sreg == 0)
4605         {
4606           macro_build ((char *) NULL, &icnt, &offset_expr,
4607                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4608           return;
4609         }
4610       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4611                    AT, sreg, treg);
4612       macro_build ((char *) NULL, &icnt, &offset_expr,
4613                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4614       break;
4615
4616     case M_BLTUL:
4617       likely = 1;
4618     case M_BLTU:
4619       if (treg == 0)
4620         goto do_false;
4621       if (sreg == 0)
4622         {
4623           macro_build ((char *) NULL, &icnt, &offset_expr,
4624                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4625           return;
4626         }
4627       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4628                    "d,v,t", AT, sreg,
4629                    treg);
4630       macro_build ((char *) NULL, &icnt, &offset_expr,
4631                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4632       break;
4633
4634     case M_DDIV_3:
4635       dbl = 1;
4636     case M_DIV_3:
4637       s = "mflo";
4638       goto do_div3;
4639     case M_DREM_3:
4640       dbl = 1;
4641     case M_REM_3:
4642       s = "mfhi";
4643     do_div3:
4644       if (treg == 0)
4645         {
4646           as_warn (_("Divide by zero."));
4647           if (mips_trap)
4648             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4649                          "s,t,q", 0, 0, 7);
4650           else
4651             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4652                          "c", 7);
4653           return;
4654         }
4655
4656       mips_emit_delays (TRUE);
4657       ++mips_opts.noreorder;
4658       mips_any_noreorder = 1;
4659       if (mips_trap)
4660         {
4661           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4662                        "s,t,q", treg, 0, 7);
4663           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4664                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4665         }
4666       else
4667         {
4668           expr1.X_add_number = 8;
4669           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4670           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4671                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4672           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4673                        "c", 7);
4674         }
4675       expr1.X_add_number = -1;
4676       macro_build ((char *) NULL, &icnt, &expr1,
4677                    dbl ? "daddiu" : "addiu",
4678                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4679       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4680       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4681       if (dbl)
4682         {
4683           expr1.X_add_number = 1;
4684           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4685                        (int) BFD_RELOC_LO16);
4686           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4687                        "d,w,<", AT, AT, 31);
4688         }
4689       else
4690         {
4691           expr1.X_add_number = 0x80000000;
4692           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4693                        (int) BFD_RELOC_HI16);
4694         }
4695       if (mips_trap)
4696         {
4697           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4698                        "s,t,q", sreg, AT, 6);
4699           /* We want to close the noreorder block as soon as possible, so
4700              that later insns are available for delay slot filling.  */
4701           --mips_opts.noreorder;
4702         }
4703       else
4704         {
4705           expr1.X_add_number = 8;
4706           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4707           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4708                        0);
4709
4710           /* We want to close the noreorder block as soon as possible, so
4711              that later insns are available for delay slot filling.  */
4712           --mips_opts.noreorder;
4713
4714           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4715                        "c", 6);
4716         }
4717       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4718       break;
4719
4720     case M_DIV_3I:
4721       s = "div";
4722       s2 = "mflo";
4723       goto do_divi;
4724     case M_DIVU_3I:
4725       s = "divu";
4726       s2 = "mflo";
4727       goto do_divi;
4728     case M_REM_3I:
4729       s = "div";
4730       s2 = "mfhi";
4731       goto do_divi;
4732     case M_REMU_3I:
4733       s = "divu";
4734       s2 = "mfhi";
4735       goto do_divi;
4736     case M_DDIV_3I:
4737       dbl = 1;
4738       s = "ddiv";
4739       s2 = "mflo";
4740       goto do_divi;
4741     case M_DDIVU_3I:
4742       dbl = 1;
4743       s = "ddivu";
4744       s2 = "mflo";
4745       goto do_divi;
4746     case M_DREM_3I:
4747       dbl = 1;
4748       s = "ddiv";
4749       s2 = "mfhi";
4750       goto do_divi;
4751     case M_DREMU_3I:
4752       dbl = 1;
4753       s = "ddivu";
4754       s2 = "mfhi";
4755     do_divi:
4756       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4757         {
4758           as_warn (_("Divide by zero."));
4759           if (mips_trap)
4760             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4761                          "s,t,q", 0, 0, 7);
4762           else
4763             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4764                          "c", 7);
4765           return;
4766         }
4767       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4768         {
4769           if (strcmp (s2, "mflo") == 0)
4770             move_register (&icnt, dreg, sreg);
4771           else
4772             move_register (&icnt, dreg, 0);
4773           return;
4774         }
4775       if (imm_expr.X_op == O_constant
4776           && imm_expr.X_add_number == -1
4777           && s[strlen (s) - 1] != 'u')
4778         {
4779           if (strcmp (s2, "mflo") == 0)
4780             {
4781               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4782                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4783             }
4784           else
4785             move_register (&icnt, dreg, 0);
4786           return;
4787         }
4788
4789       load_register (&icnt, AT, &imm_expr, dbl);
4790       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4791                    sreg, AT);
4792       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4793       break;
4794
4795     case M_DIVU_3:
4796       s = "divu";
4797       s2 = "mflo";
4798       goto do_divu3;
4799     case M_REMU_3:
4800       s = "divu";
4801       s2 = "mfhi";
4802       goto do_divu3;
4803     case M_DDIVU_3:
4804       s = "ddivu";
4805       s2 = "mflo";
4806       goto do_divu3;
4807     case M_DREMU_3:
4808       s = "ddivu";
4809       s2 = "mfhi";
4810     do_divu3:
4811       mips_emit_delays (TRUE);
4812       ++mips_opts.noreorder;
4813       mips_any_noreorder = 1;
4814       if (mips_trap)
4815         {
4816           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4817                        "s,t,q", treg, 0, 7);
4818           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4819                        sreg, treg);
4820           /* We want to close the noreorder block as soon as possible, so
4821              that later insns are available for delay slot filling.  */
4822           --mips_opts.noreorder;
4823         }
4824       else
4825         {
4826           expr1.X_add_number = 8;
4827           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4828           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4829                        sreg, treg);
4830
4831           /* We want to close the noreorder block as soon as possible, so
4832              that later insns are available for delay slot filling.  */
4833           --mips_opts.noreorder;
4834           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4835                        "c", 7);
4836         }
4837       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4838       return;
4839
4840     case M_DLA_AB:
4841       dbl = 1;
4842     case M_LA_AB:
4843       /* Load the address of a symbol into a register.  If breg is not
4844          zero, we then add a base register to it.  */
4845
4846       if (dbl && HAVE_32BIT_GPRS)
4847         as_warn (_("dla used to load 32-bit register"));
4848
4849       if (! dbl && HAVE_64BIT_OBJECTS)
4850         as_warn (_("la used to load 64-bit address"));
4851
4852       if (offset_expr.X_op == O_constant
4853           && offset_expr.X_add_number >= -0x8000
4854           && offset_expr.X_add_number < 0x8000)
4855         {
4856           macro_build ((char *) NULL, &icnt, &offset_expr,
4857                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4858                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4859           return;
4860         }
4861
4862       if (treg == breg)
4863         {
4864           tempreg = AT;
4865           used_at = 1;
4866         }
4867       else
4868         {
4869           tempreg = treg;
4870           used_at = 0;
4871         }
4872
4873       /* When generating embedded PIC code, we permit expressions of
4874          the form
4875            la   $treg,foo-bar
4876            la   $treg,foo-bar($breg)
4877          where bar is an address in the current section.  These are used
4878          when getting the addresses of functions.  We don't permit
4879          X_add_number to be non-zero, because if the symbol is
4880          external the relaxing code needs to know that any addend is
4881          purely the offset to X_op_symbol.  */
4882       if (mips_pic == EMBEDDED_PIC
4883           && offset_expr.X_op == O_subtract
4884           && (symbol_constant_p (offset_expr.X_op_symbol)
4885               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4886               : (symbol_equated_p (offset_expr.X_op_symbol)
4887                  && (S_GET_SEGMENT
4888                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4889                       ->X_add_symbol)
4890                      == now_seg)))
4891           && (offset_expr.X_add_number == 0
4892               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4893         {
4894           if (breg == 0)
4895             {
4896               tempreg = treg;
4897               used_at = 0;
4898               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4899                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4900             }
4901           else
4902             {
4903               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4904                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4905               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4906                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4907                            "d,v,t", tempreg, tempreg, breg);
4908             }
4909           macro_build ((char *) NULL, &icnt, &offset_expr,
4910                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4911                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4912           if (! used_at)
4913             return;
4914           break;
4915         }
4916
4917       if (offset_expr.X_op != O_symbol
4918           && offset_expr.X_op != O_constant)
4919         {
4920           as_bad (_("expression too complex"));
4921           offset_expr.X_op = O_constant;
4922         }
4923
4924       if (offset_expr.X_op == O_constant)
4925         load_register (&icnt, tempreg, &offset_expr,
4926                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4927                         ? (dbl || HAVE_64BIT_ADDRESSES)
4928                         : HAVE_64BIT_ADDRESSES));
4929       else if (mips_pic == NO_PIC)
4930         {
4931           /* If this is a reference to a GP relative symbol, we want
4932                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4933              Otherwise we want
4934                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4935                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4936              If we have a constant, we need two instructions anyhow,
4937              so we may as well always use the latter form.
4938
4939             With 64bit address space and a usable $at we want
4940               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4941               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4942               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4943               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4944               dsll32    $tempreg,0
4945               daddu     $tempreg,$tempreg,$at
4946
4947             If $at is already in use, we use a path which is suboptimal
4948             on superscalar processors.
4949               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4950               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4951               dsll      $tempreg,16
4952               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4953               dsll      $tempreg,16
4954               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4955           */
4956           char *p = NULL;
4957           if (HAVE_64BIT_ADDRESSES)
4958             {
4959               /* We don't do GP optimization for now because RELAX_ENCODE can't
4960                  hold the data for such large chunks.  */
4961
4962               if (used_at == 0 && ! mips_opts.noat)
4963                 {
4964                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4965                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4966                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4967                                AT, (int) BFD_RELOC_HI16_S);
4968                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4969                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4970                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4971                                AT, AT, (int) BFD_RELOC_LO16);
4972                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4973                                "d,w,<", tempreg, tempreg, 0);
4974                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4975                                "d,v,t", tempreg, tempreg, AT);
4976                   used_at = 1;
4977                 }
4978               else
4979                 {
4980                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4981                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4982                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4983                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4984                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4985                                tempreg, tempreg, 16);
4986                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4987                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4988                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4989                                tempreg, tempreg, 16);
4990                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4991                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4992                 }
4993             }
4994           else
4995             {
4996               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4997                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4998                 {
4999                   frag_grow (20);
5000                   macro_build ((char *) NULL, &icnt, &offset_expr,
5001                                ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5002                                mips_gp_register, (int) BFD_RELOC_GPREL16);
5003                   p = frag_var (rs_machine_dependent, 8, 0,
5004                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5005                                               mips_opts.warn_about_macros),
5006                                 offset_expr.X_add_symbol, 0, NULL);
5007                 }
5008               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5009               if (p != NULL)
5010                 p += 4;
5011               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5012                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5013             }
5014         }
5015       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5016         {
5017           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5018
5019           /* If this is a reference to an external symbol, and there
5020              is no constant, we want
5021                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5022              or if tempreg is PIC_CALL_REG
5023                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5024              For a local symbol, we want
5025                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5026                nop
5027                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5028
5029              If we have a small constant, and this is a reference to
5030              an external symbol, we want
5031                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5032                nop
5033                addiu    $tempreg,$tempreg,<constant>
5034              For a local symbol, we want the same instruction
5035              sequence, but we output a BFD_RELOC_LO16 reloc on the
5036              addiu instruction.
5037
5038              If we have a large constant, and this is a reference to
5039              an external symbol, we want
5040                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5041                lui      $at,<hiconstant>
5042                addiu    $at,$at,<loconstant>
5043                addu     $tempreg,$tempreg,$at
5044              For a local symbol, we want the same instruction
5045              sequence, but we output a BFD_RELOC_LO16 reloc on the
5046              addiu instruction.
5047            */
5048
5049           expr1.X_add_number = offset_expr.X_add_number;
5050           offset_expr.X_add_number = 0;
5051           frag_grow (32);
5052           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5053             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5054           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5055                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5056           if (expr1.X_add_number == 0)
5057             {
5058               int off;
5059               char *p;
5060
5061               if (breg == 0)
5062                 off = 0;
5063               else
5064                 {
5065                   /* We're going to put in an addu instruction using
5066                      tempreg, so we may as well insert the nop right
5067                      now.  */
5068                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5069                                "nop", "");
5070                   off = 4;
5071                 }
5072               p = frag_var (rs_machine_dependent, 8 - off, 0,
5073                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5074                                           (breg == 0
5075                                            ? mips_opts.warn_about_macros
5076                                            : 0)),
5077                             offset_expr.X_add_symbol, 0, NULL);
5078               if (breg == 0)
5079                 {
5080                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5081                   p += 4;
5082                 }
5083               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5084                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5085               /* FIXME: If breg == 0, and the next instruction uses
5086                  $tempreg, then if this variant case is used an extra
5087                  nop will be generated.  */
5088             }
5089           else if (expr1.X_add_number >= -0x8000
5090                    && expr1.X_add_number < 0x8000)
5091             {
5092               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5093                            "nop", "");
5094               macro_build ((char *) NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5095                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5096               frag_var (rs_machine_dependent, 0, 0,
5097                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5098                         offset_expr.X_add_symbol, 0, NULL);
5099             }
5100           else
5101             {
5102               int off1;
5103
5104               /* If we are going to add in a base register, and the
5105                  target register and the base register are the same,
5106                  then we are using AT as a temporary register.  Since
5107                  we want to load the constant into AT, we add our
5108                  current AT (from the global offset table) and the
5109                  register into the register now, and pretend we were
5110                  not using a base register.  */
5111               if (breg != treg)
5112                 off1 = 0;
5113               else
5114                 {
5115                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5116                                "nop", "");
5117                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5118                                ADDRESS_ADD_INSN, "d,v,t", treg, AT, breg);
5119                   breg = 0;
5120                   tempreg = treg;
5121                   off1 = -8;
5122                 }
5123
5124               /* Set mips_optimize around the lui instruction to avoid
5125                  inserting an unnecessary nop after the lw.  */
5126               hold_mips_optimize = mips_optimize;
5127               mips_optimize = 2;
5128               macro_build_lui (NULL, &icnt, &expr1, AT);
5129               mips_optimize = hold_mips_optimize;
5130
5131               macro_build ((char *) NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5132                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5133               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5134                            ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, AT);
5135               frag_var (rs_machine_dependent, 0, 0,
5136                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5137                         offset_expr.X_add_symbol, 0, NULL);
5138               used_at = 1;
5139             }
5140         }
5141       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5142         {
5143           char *p = NULL;
5144           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5145           int adj = 0;
5146
5147           /* If this is a reference to an external, and there is no
5148              constant, or local symbol (*), with or without a
5149              constant, we want
5150                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5151              or if tempreg is PIC_CALL_REG
5152                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5153
5154              If we have a small constant, and this is a reference to
5155              an external symbol, we want
5156                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5157                addiu    $tempreg,$tempreg,<constant>
5158
5159              If we have a large constant, and this is a reference to
5160              an external symbol, we want
5161                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5162                lui      $at,<hiconstant>
5163                addiu    $at,$at,<loconstant>
5164                addu     $tempreg,$tempreg,$at
5165
5166              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5167              local symbols, even though it introduces an additional
5168              instruction.  */
5169
5170           frag_grow (28);
5171           if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5172             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5173           if (offset_expr.X_add_number)
5174             {
5175               frag_now->tc_frag_data.tc_fr_offset =
5176                 expr1.X_add_number = offset_expr.X_add_number;
5177               offset_expr.X_add_number = 0;
5178
5179               macro_build ((char *) NULL, &icnt, &offset_expr,
5180                            ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5181                            lw_reloc_type, mips_gp_register);
5182
5183               if (expr1.X_add_number >= -0x8000
5184                   && expr1.X_add_number < 0x8000)
5185                 {
5186                   macro_build ((char *) NULL, &icnt, &expr1,
5187                                ADDRESS_ADDI_INSN, "t,r,j", tempreg, tempreg,
5188                                (int) BFD_RELOC_LO16);
5189                   p = frag_var (rs_machine_dependent, 4, 0,
5190                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5191                                 offset_expr.X_add_symbol, 0, NULL);
5192                 }
5193               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5194                 {
5195                   int dreg;
5196
5197                   /* If we are going to add in a base register, and the
5198                      target register and the base register are the same,
5199                      then we are using AT as a temporary register.  Since
5200                      we want to load the constant into AT, we add our
5201                      current AT (from the global offset table) and the
5202                      register into the register now, and pretend we were
5203                      not using a base register.  */
5204                   if (breg != treg)
5205                     dreg = tempreg;
5206                   else
5207                     {
5208                       assert (tempreg == AT);
5209                       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5210                                    ADDRESS_ADD_INSN, "d,v,t", treg, AT, breg);
5211                       dreg = treg;
5212                       adj = 4;
5213                     }
5214
5215                   macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5216                   macro_build ((char *) NULL, &icnt, &expr1,
5217                                ADDRESS_ADDI_INSN, "t,r,j", AT, AT,
5218                                (int) BFD_RELOC_LO16);
5219                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5220                                ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5221
5222                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
5223                                 RELAX_ENCODE (16 + adj, 4 + adj,
5224                                               0, 0, 0, 0),
5225                                 offset_expr.X_add_symbol, 0, NULL);
5226
5227                   used_at = 1;
5228                 }
5229               else
5230                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5231
5232               offset_expr.X_add_number = expr1.X_add_number;
5233
5234               macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5235                            "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_DISP,
5236                            mips_gp_register);
5237               if (adj)
5238                 {
5239                   macro_build (p + 4, &icnt, (expressionS *) NULL,
5240                                ADDRESS_ADD_INSN, "d,v,t",
5241                                treg, tempreg, breg);
5242                   breg = 0;
5243                   tempreg = treg;
5244                 }
5245             }
5246           else
5247             {
5248               macro_build ((char *) NULL, &icnt, &offset_expr,
5249                            ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5250                            lw_reloc_type, mips_gp_register);
5251               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5252                 p = frag_var (rs_machine_dependent, 0, 0,
5253                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5254                               offset_expr.X_add_symbol, 0, NULL);
5255             }
5256
5257           if (! p)
5258             {
5259               /* To avoid confusion in tc_gen_reloc, we must ensure
5260                  that this does not become a variant frag.  */
5261               frag_wane (frag_now);
5262               frag_new (0);
5263             }
5264         }
5265       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5266         {
5267           int gpdel;
5268           char *p;
5269           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5270           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5271           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5272
5273           /* This is the large GOT case.  If this is a reference to an
5274              external symbol, and there is no constant, we want
5275                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5276                addu     $tempreg,$tempreg,$gp
5277                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5278              or if tempreg is PIC_CALL_REG
5279                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5280                addu     $tempreg,$tempreg,$gp
5281                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5282              For a local symbol, we want
5283                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5284                nop
5285                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5286
5287              If we have a small constant, and this is a reference to
5288              an external symbol, we want
5289                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5290                addu     $tempreg,$tempreg,$gp
5291                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5292                nop
5293                addiu    $tempreg,$tempreg,<constant>
5294              For a local symbol, we want
5295                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5296                nop
5297                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5298
5299              If we have a large constant, and this is a reference to
5300              an external symbol, we want
5301                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5302                addu     $tempreg,$tempreg,$gp
5303                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5304                lui      $at,<hiconstant>
5305                addiu    $at,$at,<loconstant>
5306                addu     $tempreg,$tempreg,$at
5307              For a local symbol, we want
5308                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5309                lui      $at,<hiconstant>
5310                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5311                addu     $tempreg,$tempreg,$at
5312           */
5313
5314           expr1.X_add_number = offset_expr.X_add_number;
5315           offset_expr.X_add_number = 0;
5316           frag_grow (52);
5317           if (reg_needs_delay (mips_gp_register))
5318             gpdel = 4;
5319           else
5320             gpdel = 0;
5321           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5322             {
5323               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5324               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5325             }
5326           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5327                        tempreg, lui_reloc_type);
5328           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5329                        ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
5330                        mips_gp_register);
5331           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5332                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5333           if (expr1.X_add_number == 0)
5334             {
5335               int off;
5336
5337               if (breg == 0)
5338                 off = 0;
5339               else
5340                 {
5341                   /* We're going to put in an addu instruction using
5342                      tempreg, so we may as well insert the nop right
5343                      now.  */
5344                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5345                                "nop", "");
5346                   off = 4;
5347                 }
5348
5349               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5350                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5351                                           8 + gpdel, 0,
5352                                           (breg == 0
5353                                            ? mips_opts.warn_about_macros
5354                                            : 0)),
5355                             offset_expr.X_add_symbol, 0, NULL);
5356             }
5357           else if (expr1.X_add_number >= -0x8000
5358                    && expr1.X_add_number < 0x8000)
5359             {
5360               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5361                            "nop", "");
5362               macro_build ((char *) NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5363                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5364
5365               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5366                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5367                                           (breg == 0
5368                                            ? mips_opts.warn_about_macros
5369                                            : 0)),
5370                             offset_expr.X_add_symbol, 0, NULL);
5371             }
5372           else
5373             {
5374               int adj, dreg;
5375
5376               /* If we are going to add in a base register, and the
5377                  target register and the base register are the same,
5378                  then we are using AT as a temporary register.  Since
5379                  we want to load the constant into AT, we add our
5380                  current AT (from the global offset table) and the
5381                  register into the register now, and pretend we were
5382                  not using a base register.  */
5383               if (breg != treg)
5384                 {
5385                   adj = 0;
5386                   dreg = tempreg;
5387                 }
5388               else
5389                 {
5390                   assert (tempreg == AT);
5391                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5392                                "nop", "");
5393                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5394                                ADDRESS_ADD_INSN, "d,v,t", treg, AT, breg);
5395                   dreg = treg;
5396                   adj = 8;
5397                 }
5398
5399               /* Set mips_optimize around the lui instruction to avoid
5400                  inserting an unnecessary nop after the lw.  */
5401               hold_mips_optimize = mips_optimize;
5402               mips_optimize = 2;
5403               macro_build_lui (NULL, &icnt, &expr1, AT);
5404               mips_optimize = hold_mips_optimize;
5405
5406               macro_build ((char *) NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5407                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5408               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5409                            ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5410
5411               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5412                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5413                                           8 + gpdel, 0,
5414                                           (breg == 0
5415                                            ? mips_opts.warn_about_macros
5416                                            : 0)),
5417                             offset_expr.X_add_symbol, 0, NULL);
5418
5419               used_at = 1;
5420             }
5421
5422           if (gpdel > 0)
5423             {
5424               /* This is needed because this instruction uses $gp, but
5425                  the first instruction on the main stream does not.  */
5426               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5427               p += 4;
5428             }
5429
5430           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5431                        tempreg, local_reloc_type, mips_gp_register);
5432           p += 4;
5433           if (expr1.X_add_number >= -0x8000
5434               && expr1.X_add_number < 0x8000)
5435             {
5436               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5437               p += 4;
5438               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5439                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5440               /* FIXME: If add_number is 0, and there was no base
5441                  register, the external symbol case ended with a load,
5442                  so if the symbol turns out to not be external, and
5443                  the next instruction uses tempreg, an unnecessary nop
5444                  will be inserted.  */
5445             }
5446           else
5447             {
5448               if (breg == treg)
5449                 {
5450                   /* We must add in the base register now, as in the
5451                      external symbol case.  */
5452                   assert (tempreg == AT);
5453                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5454                   p += 4;
5455                   macro_build (p, &icnt, (expressionS *) NULL,
5456                                ADDRESS_ADD_INSN, "d,v,t", treg, AT, breg);
5457                   p += 4;
5458                   tempreg = treg;
5459                   /* We set breg to 0 because we have arranged to add
5460                      it in in both cases.  */
5461                   breg = 0;
5462                 }
5463
5464               macro_build_lui (p, &icnt, &expr1, AT);
5465               p += 4;
5466               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5467                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5468               p += 4;
5469               macro_build (p, &icnt, (expressionS *) NULL, ADDRESS_ADD_INSN,
5470                            "d,v,t", tempreg, tempreg, AT);
5471               p += 4;
5472             }
5473         }
5474       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5475         {
5476           char *p = NULL;
5477           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5478           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5479           int adj = 0;
5480
5481           /* This is the large GOT case.  If this is a reference to an
5482              external symbol, and there is no constant, we want
5483                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5484                add      $tempreg,$tempreg,$gp
5485                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5486              or if tempreg is PIC_CALL_REG
5487                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5488                add      $tempreg,$tempreg,$gp
5489                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5490
5491              If we have a small constant, and this is a reference to
5492              an external symbol, we want
5493                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5494                add      $tempreg,$tempreg,$gp
5495                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5496                addi     $tempreg,$tempreg,<constant>
5497
5498              If we have a large constant, and this is a reference to
5499              an external symbol, we want
5500                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5501                addu     $tempreg,$tempreg,$gp
5502                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5503                lui      $at,<hiconstant>
5504                addi     $at,$at,<loconstant>
5505                add      $tempreg,$tempreg,$at
5506
5507              If we have NewABI, and we know it's a local symbol, we want
5508                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5509                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5510              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5511
5512           frag_grow (40);
5513
5514           frag_now->tc_frag_data.tc_fr_offset =
5515             expr1.X_add_number = offset_expr.X_add_number;
5516           offset_expr.X_add_number = 0;
5517
5518           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5519             {
5520               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5521               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5522             }
5523           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5524                        tempreg, lui_reloc_type);
5525           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5526                        ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
5527                        mips_gp_register);
5528           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5529                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5530
5531           if (expr1.X_add_number == 0)
5532             {
5533               p = frag_var (rs_machine_dependent, 8, 0,
5534                             RELAX_ENCODE (12, 8, 0, 4, 0,
5535                                           mips_opts.warn_about_macros),
5536                             offset_expr.X_add_symbol, 0, NULL);
5537             }
5538           else if (expr1.X_add_number >= -0x8000
5539                    && expr1.X_add_number < 0x8000)
5540             {
5541               macro_build ((char *) NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5542                            "t,r,j", tempreg, tempreg,
5543                            (int) BFD_RELOC_LO16);
5544               p = frag_var (rs_machine_dependent, 8, 0,
5545                             RELAX_ENCODE (16, 8, 0, 4, 0,
5546                                           mips_opts.warn_about_macros),
5547                             offset_expr.X_add_symbol, 0, NULL);
5548             }
5549           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5550             {
5551               int dreg;
5552
5553               /* If we are going to add in a base register, and the
5554                  target register and the base register are the same,
5555                  then we are using AT as a temporary register.  Since
5556                  we want to load the constant into AT, we add our
5557                  current AT (from the global offset table) and the
5558                  register into the register now, and pretend we were
5559                  not using a base register.  */
5560               if (breg != treg)
5561                 dreg = tempreg;
5562               else
5563                 {
5564                   assert (tempreg == AT);
5565                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5566                                ADDRESS_ADD_INSN, "d,v,t", treg, AT, breg);
5567                   dreg = treg;
5568                   adj = 4;
5569                 }
5570
5571               /* Set mips_optimize around the lui instruction to avoid
5572                  inserting an unnecessary nop after the lw.  */
5573               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5574               macro_build ((char *) NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5575                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5576               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5577                            ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5578
5579               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5580                             RELAX_ENCODE (24 + adj, 8 + adj,
5581                                           0, 4, 0,
5582                                           (breg == 0
5583                                            ? mips_opts.warn_about_macros
5584                                            : 0)),
5585                             offset_expr.X_add_symbol, 0, NULL);
5586
5587               used_at = 1;
5588             }
5589           else
5590             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5591
5592           offset_expr.X_add_number = expr1.X_add_number;
5593           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5594                        tempreg, (int) BFD_RELOC_MIPS_GOT_PAGE,
5595                        mips_gp_register);
5596           macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5597                        tempreg, tempreg, (int) BFD_RELOC_MIPS_GOT_OFST);
5598           if (adj)
5599             {
5600               macro_build (p + 8, &icnt, (expressionS *) NULL,
5601                            ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5602               breg = 0;
5603               tempreg = treg;
5604             }
5605         }
5606       else if (mips_pic == EMBEDDED_PIC)
5607         {
5608           /* We use
5609                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5610              */
5611           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5612                         "t,r,j", tempreg, mips_gp_register,
5613                        (int) BFD_RELOC_GPREL16);
5614         }
5615       else
5616         abort ();
5617
5618       if (breg != 0)
5619         {
5620           char *s;
5621
5622           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5623             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5624           else
5625             s = ADDRESS_ADD_INSN;
5626
5627           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5628                        "d,v,t", treg, tempreg, breg);
5629         }
5630
5631       if (! used_at)
5632         return;
5633
5634       break;
5635
5636     case M_J_A:
5637       /* The j instruction may not be used in PIC code, since it
5638          requires an absolute address.  We convert it to a b
5639          instruction.  */
5640       if (mips_pic == NO_PIC)
5641         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5642       else
5643         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5644       return;
5645
5646       /* The jal instructions must be handled as macros because when
5647          generating PIC code they expand to multi-instruction
5648          sequences.  Normally they are simple instructions.  */
5649     case M_JAL_1:
5650       dreg = RA;
5651       /* Fall through.  */
5652     case M_JAL_2:
5653       if (mips_pic == NO_PIC
5654           || mips_pic == EMBEDDED_PIC)
5655         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5656                      "d,s", dreg, sreg);
5657       else if (mips_pic == SVR4_PIC)
5658         {
5659           if (sreg != PIC_CALL_REG)
5660             as_warn (_("MIPS PIC call to register other than $25"));
5661
5662           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5663                        "d,s", dreg, sreg);
5664           if (! HAVE_NEWABI)
5665             {
5666               if (mips_cprestore_offset < 0)
5667                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5668               else
5669                 {
5670                   if (! mips_frame_reg_valid)
5671                     {
5672                       as_warn (_("No .frame pseudo-op used in PIC code"));
5673                       /* Quiet this warning.  */
5674                       mips_frame_reg_valid = 1;
5675                     }
5676                   if (! mips_cprestore_valid)
5677                     {
5678                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5679                       /* Quiet this warning.  */
5680                       mips_cprestore_valid = 1;
5681                     }
5682                   expr1.X_add_number = mips_cprestore_offset;
5683                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5684                                                 ADDRESS_LOAD_INSN,
5685                                                 mips_gp_register,
5686                                                 mips_frame_reg);
5687                 }
5688             }
5689         }
5690       else
5691         abort ();
5692
5693       return;
5694
5695     case M_JAL_A:
5696       if (mips_pic == NO_PIC)
5697         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5698       else if (mips_pic == SVR4_PIC)
5699         {
5700           char *p;
5701
5702           /* If this is a reference to an external symbol, and we are
5703              using a small GOT, we want
5704                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5705                nop
5706                jalr     $ra,$25
5707                nop
5708                lw       $gp,cprestore($sp)
5709              The cprestore value is set using the .cprestore
5710              pseudo-op.  If we are using a big GOT, we want
5711                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5712                addu     $25,$25,$gp
5713                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5714                nop
5715                jalr     $ra,$25
5716                nop
5717                lw       $gp,cprestore($sp)
5718              If the symbol is not external, we want
5719                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5720                nop
5721                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5722                jalr     $ra,$25
5723                nop
5724                lw $gp,cprestore($sp)
5725
5726              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5727              sequences above, minus nops, unless the symbol is local,
5728              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5729              GOT_DISP.  */
5730           if (HAVE_NEWABI)
5731             {
5732               if (! mips_big_got)
5733                 {
5734                   frag_grow (4);
5735                   macro_build ((char *) NULL, &icnt, &offset_expr,
5736                                ADDRESS_LOAD_INSN, "t,o(b)", PIC_CALL_REG,
5737                                (int) BFD_RELOC_MIPS_CALL16,
5738                                mips_gp_register);
5739                   frag_var (rs_machine_dependent, 0, 0,
5740                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5741                             offset_expr.X_add_symbol, 0, NULL);
5742                 }
5743               else
5744                 {
5745                   frag_grow (20);
5746                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5747                                "t,u", PIC_CALL_REG,
5748                                (int) BFD_RELOC_MIPS_CALL_HI16);
5749                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5750                                ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5751                                PIC_CALL_REG, mips_gp_register);
5752                   macro_build ((char *) NULL, &icnt, &offset_expr,
5753                                ADDRESS_LOAD_INSN, "t,o(b)", PIC_CALL_REG,
5754                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5755                   p = frag_var (rs_machine_dependent, 8, 0,
5756                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5757                                 offset_expr.X_add_symbol, 0, NULL);
5758                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5759                                "t,o(b)", PIC_CALL_REG,
5760                                (int) BFD_RELOC_MIPS_GOT_PAGE,
5761                                mips_gp_register);
5762                   macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5763                                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5764                                (int) BFD_RELOC_MIPS_GOT_OFST);
5765                 }
5766
5767               macro_build_jalr (icnt, &offset_expr);
5768             }
5769           else
5770             {
5771               frag_grow (40);
5772               if (! mips_big_got)
5773                 {
5774                   macro_build ((char *) NULL, &icnt, &offset_expr,
5775                                ADDRESS_LOAD_INSN, "t,o(b)", PIC_CALL_REG,
5776                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5777                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5778                                "nop", "");
5779                   p = frag_var (rs_machine_dependent, 4, 0,
5780                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5781                                 offset_expr.X_add_symbol, 0, NULL);
5782                 }
5783               else
5784                 {
5785                   int gpdel;
5786
5787                   if (reg_needs_delay (mips_gp_register))
5788                     gpdel = 4;
5789                   else
5790                     gpdel = 0;
5791                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5792                                "t,u", PIC_CALL_REG,
5793                                (int) BFD_RELOC_MIPS_CALL_HI16);
5794                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5795                                ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5796                                PIC_CALL_REG, mips_gp_register);
5797                   macro_build ((char *) NULL, &icnt, &offset_expr,
5798                                ADDRESS_LOAD_INSN, "t,o(b)", PIC_CALL_REG,
5799                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5800                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5801                                "nop", "");
5802                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5803                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5804                                               8 + gpdel, 0, 0),
5805                                 offset_expr.X_add_symbol, 0, NULL);
5806                   if (gpdel > 0)
5807                     {
5808                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5809                       p += 4;
5810                     }
5811                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5812                                "t,o(b)", PIC_CALL_REG,
5813                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5814                   p += 4;
5815                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5816                   p += 4;
5817                 }
5818               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5819                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5820                            (int) BFD_RELOC_LO16);
5821               macro_build_jalr (icnt, &offset_expr);
5822
5823               if (mips_cprestore_offset < 0)
5824                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5825               else
5826                 {
5827                   if (! mips_frame_reg_valid)
5828                     {
5829                       as_warn (_("No .frame pseudo-op used in PIC code"));
5830                       /* Quiet this warning.  */
5831                       mips_frame_reg_valid = 1;
5832                     }
5833                   if (! mips_cprestore_valid)
5834                     {
5835                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5836                       /* Quiet this warning.  */
5837                       mips_cprestore_valid = 1;
5838                     }
5839                   if (mips_opts.noreorder)
5840                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5841                                  "nop", "");
5842                   expr1.X_add_number = mips_cprestore_offset;
5843                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5844                                                 ADDRESS_LOAD_INSN,
5845                                                 mips_gp_register,
5846                                                 mips_frame_reg);
5847                 }
5848             }
5849         }
5850       else if (mips_pic == EMBEDDED_PIC)
5851         {
5852           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5853           /* The linker may expand the call to a longer sequence which
5854              uses $at, so we must break rather than return.  */
5855           break;
5856         }
5857       else
5858         abort ();
5859
5860       return;
5861
5862     case M_LB_AB:
5863       s = "lb";
5864       goto ld;
5865     case M_LBU_AB:
5866       s = "lbu";
5867       goto ld;
5868     case M_LH_AB:
5869       s = "lh";
5870       goto ld;
5871     case M_LHU_AB:
5872       s = "lhu";
5873       goto ld;
5874     case M_LW_AB:
5875       s = "lw";
5876       goto ld;
5877     case M_LWC0_AB:
5878       s = "lwc0";
5879       /* Itbl support may require additional care here.  */
5880       coproc = 1;
5881       goto ld;
5882     case M_LWC1_AB:
5883       s = "lwc1";
5884       /* Itbl support may require additional care here.  */
5885       coproc = 1;
5886       goto ld;
5887     case M_LWC2_AB:
5888       s = "lwc2";
5889       /* Itbl support may require additional care here.  */
5890       coproc = 1;
5891       goto ld;
5892     case M_LWC3_AB:
5893       s = "lwc3";
5894       /* Itbl support may require additional care here.  */
5895       coproc = 1;
5896       goto ld;
5897     case M_LWL_AB:
5898       s = "lwl";
5899       lr = 1;
5900       goto ld;
5901     case M_LWR_AB:
5902       s = "lwr";
5903       lr = 1;
5904       goto ld;
5905     case M_LDC1_AB:
5906       if (mips_opts.arch == CPU_R4650)
5907         {
5908           as_bad (_("opcode not supported on this processor"));
5909           return;
5910         }
5911       s = "ldc1";
5912       /* Itbl support may require additional care here.  */
5913       coproc = 1;
5914       goto ld;
5915     case M_LDC2_AB:
5916       s = "ldc2";
5917       /* Itbl support may require additional care here.  */
5918       coproc = 1;
5919       goto ld;
5920     case M_LDC3_AB:
5921       s = "ldc3";
5922       /* Itbl support may require additional care here.  */
5923       coproc = 1;
5924       goto ld;
5925     case M_LDL_AB:
5926       s = "ldl";
5927       lr = 1;
5928       goto ld;
5929     case M_LDR_AB:
5930       s = "ldr";
5931       lr = 1;
5932       goto ld;
5933     case M_LL_AB:
5934       s = "ll";
5935       goto ld;
5936     case M_LLD_AB:
5937       s = "lld";
5938       goto ld;
5939     case M_LWU_AB:
5940       s = "lwu";
5941     ld:
5942       if (breg == treg || coproc || lr)
5943         {
5944           tempreg = AT;
5945           used_at = 1;
5946         }
5947       else
5948         {
5949           tempreg = treg;
5950           used_at = 0;
5951         }
5952       goto ld_st;
5953     case M_SB_AB:
5954       s = "sb";
5955       goto st;
5956     case M_SH_AB:
5957       s = "sh";
5958       goto st;
5959     case M_SW_AB:
5960       s = "sw";
5961       goto st;
5962     case M_SWC0_AB:
5963       s = "swc0";
5964       /* Itbl support may require additional care here.  */
5965       coproc = 1;
5966       goto st;
5967     case M_SWC1_AB:
5968       s = "swc1";
5969       /* Itbl support may require additional care here.  */
5970       coproc = 1;
5971       goto st;
5972     case M_SWC2_AB:
5973       s = "swc2";
5974       /* Itbl support may require additional care here.  */
5975       coproc = 1;
5976       goto st;
5977     case M_SWC3_AB:
5978       s = "swc3";
5979       /* Itbl support may require additional care here.  */
5980       coproc = 1;
5981       goto st;
5982     case M_SWL_AB:
5983       s = "swl";
5984       goto st;
5985     case M_SWR_AB:
5986       s = "swr";
5987       goto st;
5988     case M_SC_AB:
5989       s = "sc";
5990       goto st;
5991     case M_SCD_AB:
5992       s = "scd";
5993       goto st;
5994     case M_SDC1_AB:
5995       if (mips_opts.arch == CPU_R4650)
5996         {
5997           as_bad (_("opcode not supported on this processor"));
5998           return;
5999         }
6000       s = "sdc1";
6001       coproc = 1;
6002       /* Itbl support may require additional care here.  */
6003       goto st;
6004     case M_SDC2_AB:
6005       s = "sdc2";
6006       /* Itbl support may require additional care here.  */
6007       coproc = 1;
6008       goto st;
6009     case M_SDC3_AB:
6010       s = "sdc3";
6011       /* Itbl support may require additional care here.  */
6012       coproc = 1;
6013       goto st;
6014     case M_SDL_AB:
6015       s = "sdl";
6016       goto st;
6017     case M_SDR_AB:
6018       s = "sdr";
6019     st:
6020       tempreg = AT;
6021       used_at = 1;
6022     ld_st:
6023       /* Itbl support may require additional care here.  */
6024       if (mask == M_LWC1_AB
6025           || mask == M_SWC1_AB
6026           || mask == M_LDC1_AB
6027           || mask == M_SDC1_AB
6028           || mask == M_L_DAB
6029           || mask == M_S_DAB)
6030         fmt = "T,o(b)";
6031       else if (coproc)
6032         fmt = "E,o(b)";
6033       else
6034         fmt = "t,o(b)";
6035
6036       /* For embedded PIC, we allow loads where the offset is calculated
6037          by subtracting a symbol in the current segment from an unknown
6038          symbol, relative to a base register, e.g.:
6039                 <op>    $treg, <sym>-<localsym>($breg)
6040          This is used by the compiler for switch statements.  */
6041       if (mips_pic == EMBEDDED_PIC
6042           && offset_expr.X_op == O_subtract
6043           && (symbol_constant_p (offset_expr.X_op_symbol)
6044               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
6045               : (symbol_equated_p (offset_expr.X_op_symbol)
6046                  && (S_GET_SEGMENT
6047                      (symbol_get_value_expression (offset_expr.X_op_symbol)
6048                       ->X_add_symbol)
6049                      == now_seg)))
6050           && breg != 0
6051           && (offset_expr.X_add_number == 0
6052               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
6053         {
6054           /* For this case, we output the instructions:
6055                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
6056                 addiu   $tempreg,$tempreg,$breg
6057                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
6058              If the relocation would fit entirely in 16 bits, it would be
6059              nice to emit:
6060                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
6061              instead, but that seems quite difficult.  */
6062           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6063                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
6064           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6065                        ((bfd_arch_bits_per_address (stdoutput) == 32
6066                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
6067                         ? "addu" : "daddu"),
6068                        "d,v,t", tempreg, tempreg, breg);
6069           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6070                        (int) BFD_RELOC_PCREL_LO16, tempreg);
6071           if (! used_at)
6072             return;
6073           break;
6074         }
6075
6076       if (offset_expr.X_op != O_constant
6077           && offset_expr.X_op != O_symbol)
6078         {
6079           as_bad (_("expression too complex"));
6080           offset_expr.X_op = O_constant;
6081         }
6082
6083       /* A constant expression in PIC code can be handled just as it
6084          is in non PIC code.  */
6085       if (mips_pic == NO_PIC
6086           || offset_expr.X_op == O_constant)
6087         {
6088           char *p;
6089
6090           /* If this is a reference to a GP relative symbol, and there
6091              is no base register, we want
6092                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6093              Otherwise, if there is no base register, we want
6094                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6095                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6096              If we have a constant, we need two instructions anyhow,
6097              so we always use the latter form.
6098
6099              If we have a base register, and this is a reference to a
6100              GP relative symbol, we want
6101                addu     $tempreg,$breg,$gp
6102                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6103              Otherwise we want
6104                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6105                addu     $tempreg,$tempreg,$breg
6106                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6107              With a constant we always use the latter case.
6108
6109              With 64bit address space and no base register and $at usable,
6110              we want
6111                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6112                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6113                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6114                dsll32   $tempreg,0
6115                daddu    $tempreg,$at
6116                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6117              If we have a base register, we want
6118                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6119                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6120                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6121                daddu    $at,$breg
6122                dsll32   $tempreg,0
6123                daddu    $tempreg,$at
6124                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6125
6126              Without $at we can't generate the optimal path for superscalar
6127              processors here since this would require two temporary registers.
6128                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6129                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6130                dsll     $tempreg,16
6131                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6132                dsll     $tempreg,16
6133                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6134              If we have a base register, we want
6135                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6136                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6137                dsll     $tempreg,16
6138                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6139                dsll     $tempreg,16
6140                daddu    $tempreg,$tempreg,$breg
6141                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6142
6143              If we have 64-bit addresses, as an optimization, for
6144              addresses which are 32-bit constants (e.g. kseg0/kseg1
6145              addresses) we fall back to the 32-bit address generation
6146              mechanism since it is more efficient.  Note that due to
6147              the signed offset used by memory operations, the 32-bit
6148              range is shifted down by 32768 here.  This code should
6149              probably attempt to generate 64-bit constants more
6150              efficiently in general.
6151
6152              As an extension for architectures with 64-bit registers,
6153              we don't truncate 64-bit addresses given as literal
6154              constants down to 32 bits, to support existing practice
6155              in the mips64 Linux (the kernel), that compiles source
6156              files with -mabi=64, assembling them as o32 or n32 (with
6157              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
6158              the whole kernel is loaded into a memory region that is
6159              addressible with sign-extended 32-bit addresses, it is
6160              wasteful to compute the upper 32 bits of every
6161              non-literal address, that takes more space and time.
6162              Some day this should probably be implemented as an
6163              assembler option, such that the kernel doesn't have to
6164              use such ugly hacks, even though it will still have to
6165              end up converting the binary to ELF32 for a number of
6166              platforms whose boot loaders don't support ELF64
6167              binaries.  */
6168           if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
6169               || (offset_expr.X_op == O_constant
6170                   && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
6171                   && HAVE_64BIT_ADDRESS_CONSTANTS))
6172             {
6173               p = NULL;
6174
6175               /* We don't do GP optimization for now because RELAX_ENCODE can't
6176                  hold the data for such large chunks.  */
6177
6178               if (used_at == 0 && ! mips_opts.noat)
6179                 {
6180                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6181                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6182                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6183                                AT, (int) BFD_RELOC_HI16_S);
6184                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6185                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6186                   if (breg != 0)
6187                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6188                                  "d,v,t", AT, AT, breg);
6189                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
6190                                "d,w,<", tempreg, tempreg, 0);
6191                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6192                                "d,v,t", tempreg, tempreg, AT);
6193                   macro_build (p, &icnt, &offset_expr, s,
6194                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6195                   used_at = 1;
6196                 }
6197               else
6198                 {
6199                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6200                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6201                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6202                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6203                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6204                                "d,w,<", tempreg, tempreg, 16);
6205                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6206                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
6207                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6208                                "d,w,<", tempreg, tempreg, 16);
6209                   if (breg != 0)
6210                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6211                                  "d,v,t", tempreg, tempreg, breg);
6212                   macro_build (p, &icnt, &offset_expr, s,
6213                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6214                 }
6215
6216               return;
6217             }
6218           else if (offset_expr.X_op == O_constant
6219                    && !HAVE_64BIT_ADDRESS_CONSTANTS
6220                    && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6221             as_bad (_("load/store address overflow (max 32 bits)"));
6222
6223           if (breg == 0)
6224             {
6225               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6226                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6227                 p = NULL;
6228               else
6229                 {
6230                   frag_grow (20);
6231                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6232                                treg, (int) BFD_RELOC_GPREL16,
6233                                mips_gp_register);
6234                   p = frag_var (rs_machine_dependent, 8, 0,
6235                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6236                                               (mips_opts.warn_about_macros
6237                                                || (used_at
6238                                                    && mips_opts.noat))),
6239                                 offset_expr.X_add_symbol, 0, NULL);
6240                   used_at = 0;
6241                 }
6242               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6243               if (p != NULL)
6244                 p += 4;
6245               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6246                            (int) BFD_RELOC_LO16, tempreg);
6247             }
6248           else
6249             {
6250               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6251                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6252                 p = NULL;
6253               else
6254                 {
6255                   frag_grow (28);
6256                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6257                                ADDRESS_ADD_INSN, "d,v,t", tempreg, breg,
6258                                mips_gp_register);
6259                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6260                                treg, (int) BFD_RELOC_GPREL16, tempreg);
6261                   p = frag_var (rs_machine_dependent, 12, 0,
6262                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6263                                 offset_expr.X_add_symbol, 0, NULL);
6264                 }
6265               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6266               if (p != NULL)
6267                 p += 4;
6268               macro_build (p, &icnt, (expressionS *) NULL, ADDRESS_ADD_INSN,
6269                            "d,v,t", tempreg, tempreg, breg);
6270               if (p != NULL)
6271                 p += 4;
6272               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6273                            (int) BFD_RELOC_LO16, tempreg);
6274             }
6275         }
6276       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6277         {
6278           char *p;
6279           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6280
6281           /* If this is a reference to an external symbol, we want
6282                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6283                nop
6284                <op>     $treg,0($tempreg)
6285              Otherwise we want
6286                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6287                nop
6288                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6289                <op>     $treg,0($tempreg)
6290
6291              For NewABI, we want
6292                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6293                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6294
6295              If there is a base register, we add it to $tempreg before
6296              the <op>.  If there is a constant, we stick it in the
6297              <op> instruction.  We don't handle constants larger than
6298              16 bits, because we have no way to load the upper 16 bits
6299              (actually, we could handle them for the subset of cases
6300              in which we are not using $at).  */
6301           assert (offset_expr.X_op == O_symbol);
6302           if (HAVE_NEWABI)
6303             {
6304               macro_build ((char *) NULL, &icnt, &offset_expr,
6305                            ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6306                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6307               if (breg != 0)
6308                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6309                              ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6310                              breg);
6311               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6312                            (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6313
6314               if (! used_at)
6315                 return;
6316
6317               break;
6318             }
6319           expr1.X_add_number = offset_expr.X_add_number;
6320           offset_expr.X_add_number = 0;
6321           if (expr1.X_add_number < -0x8000
6322               || expr1.X_add_number >= 0x8000)
6323             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6324           frag_grow (20);
6325           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6326                        "t,o(b)", tempreg, (int) lw_reloc_type,
6327                        mips_gp_register);
6328           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6329           p = frag_var (rs_machine_dependent, 4, 0,
6330                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6331                         offset_expr.X_add_symbol, 0, NULL);
6332           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6333                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6334           if (breg != 0)
6335             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6336                          ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, breg);
6337           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6338                        (int) BFD_RELOC_LO16, tempreg);
6339         }
6340       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6341         {
6342           int gpdel;
6343           char *p;
6344
6345           /* If this is a reference to an external symbol, we want
6346                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6347                addu     $tempreg,$tempreg,$gp
6348                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6349                <op>     $treg,0($tempreg)
6350              Otherwise we want
6351                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6352                nop
6353                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6354                <op>     $treg,0($tempreg)
6355              If there is a base register, we add it to $tempreg before
6356              the <op>.  If there is a constant, we stick it in the
6357              <op> instruction.  We don't handle constants larger than
6358              16 bits, because we have no way to load the upper 16 bits
6359              (actually, we could handle them for the subset of cases
6360              in which we are not using $at).  */
6361           assert (offset_expr.X_op == O_symbol);
6362           expr1.X_add_number = offset_expr.X_add_number;
6363           offset_expr.X_add_number = 0;
6364           if (expr1.X_add_number < -0x8000
6365               || expr1.X_add_number >= 0x8000)
6366             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6367           if (reg_needs_delay (mips_gp_register))
6368             gpdel = 4;
6369           else
6370             gpdel = 0;
6371           frag_grow (36);
6372           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6373                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6374           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6375                        ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6376                        mips_gp_register);
6377           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6378                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6379                        tempreg);
6380           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6381                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6382                         offset_expr.X_add_symbol, 0, NULL);
6383           if (gpdel > 0)
6384             {
6385               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6386               p += 4;
6387             }
6388           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6389                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6390                        mips_gp_register);
6391           p += 4;
6392           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6393           p += 4;
6394           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6395                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6396           if (breg != 0)
6397             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6398                          ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, breg);
6399           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6400                        (int) BFD_RELOC_LO16, tempreg);
6401         }
6402       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6403         {
6404           char *p;
6405           int bregsz = breg != 0 ? 4 : 0;
6406
6407           /* If this is a reference to an external symbol, we want
6408                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6409                add      $tempreg,$tempreg,$gp
6410                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6411                <op>     $treg,<ofst>($tempreg)
6412              Otherwise, for local symbols, we want:
6413                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6414                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6415           assert (offset_expr.X_op == O_symbol);
6416           frag_now->tc_frag_data.tc_fr_offset =
6417             expr1.X_add_number = offset_expr.X_add_number;
6418           offset_expr.X_add_number = 0;
6419           if (expr1.X_add_number < -0x8000
6420               || expr1.X_add_number >= 0x8000)
6421             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6422           frag_grow (36);
6423           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6424                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6425           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6426                        ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6427                        mips_gp_register);
6428           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6429                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6430                        tempreg);
6431           if (breg != 0)
6432             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6433                          ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, breg);
6434           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6435                        (int) BFD_RELOC_LO16, tempreg);
6436
6437           offset_expr.X_add_number = expr1.X_add_number;
6438           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6439                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6440                                       0, 4 + bregsz, 0, 0),
6441                         offset_expr.X_add_symbol, 0, NULL);
6442           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6443                        tempreg, (int) BFD_RELOC_MIPS_GOT_PAGE,
6444                        mips_gp_register);
6445           if (breg != 0)
6446             macro_build (p + 4, &icnt, (expressionS *) NULL,
6447                          ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg, breg);
6448           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6449                        (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6450         }
6451       else if (mips_pic == EMBEDDED_PIC)
6452         {
6453           /* If there is no base register, we want
6454                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6455              If there is a base register, we want
6456                addu     $tempreg,$breg,$gp
6457                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6458              */
6459           assert (offset_expr.X_op == O_symbol);
6460           if (breg == 0)
6461             {
6462               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6463                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6464               used_at = 0;
6465             }
6466           else
6467             {
6468               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6469                            ADDRESS_ADD_INSN, "d,v,t", tempreg, breg,
6470                            mips_gp_register);
6471               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6472                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6473             }
6474         }
6475       else
6476         abort ();
6477
6478       if (! used_at)
6479         return;
6480
6481       break;
6482
6483     case M_LI:
6484     case M_LI_S:
6485       load_register (&icnt, treg, &imm_expr, 0);
6486       return;
6487
6488     case M_DLI:
6489       load_register (&icnt, treg, &imm_expr, 1);
6490       return;
6491
6492     case M_LI_SS:
6493       if (imm_expr.X_op == O_constant)
6494         {
6495           load_register (&icnt, AT, &imm_expr, 0);
6496           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6497                        "mtc1", "t,G", AT, treg);
6498           break;
6499         }
6500       else
6501         {
6502           assert (offset_expr.X_op == O_symbol
6503                   && strcmp (segment_name (S_GET_SEGMENT
6504                                            (offset_expr.X_add_symbol)),
6505                              ".lit4") == 0
6506                   && offset_expr.X_add_number == 0);
6507           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6508                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6509           return;
6510         }
6511
6512     case M_LI_D:
6513       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6514          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6515          order 32 bits of the value and the low order 32 bits are either
6516          zero or in OFFSET_EXPR.  */
6517       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6518         {
6519           if (HAVE_64BIT_GPRS)
6520             load_register (&icnt, treg, &imm_expr, 1);
6521           else
6522             {
6523               int hreg, lreg;
6524
6525               if (target_big_endian)
6526                 {
6527                   hreg = treg;
6528                   lreg = treg + 1;
6529                 }
6530               else
6531                 {
6532                   hreg = treg + 1;
6533                   lreg = treg;
6534                 }
6535
6536               if (hreg <= 31)
6537                 load_register (&icnt, hreg, &imm_expr, 0);
6538               if (lreg <= 31)
6539                 {
6540                   if (offset_expr.X_op == O_absent)
6541                     move_register (&icnt, lreg, 0);
6542                   else
6543                     {
6544                       assert (offset_expr.X_op == O_constant);
6545                       load_register (&icnt, lreg, &offset_expr, 0);
6546                     }
6547                 }
6548             }
6549           return;
6550         }
6551
6552       /* We know that sym is in the .rdata section.  First we get the
6553          upper 16 bits of the address.  */
6554       if (mips_pic == NO_PIC)
6555         {
6556           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6557         }
6558       else if (mips_pic == SVR4_PIC)
6559         {
6560           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6561                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6562                        mips_gp_register);
6563         }
6564       else if (mips_pic == EMBEDDED_PIC)
6565         {
6566           /* For embedded PIC we pick up the entire address off $gp in
6567              a single instruction.  */
6568           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6569                        "t,r,j", AT, mips_gp_register,
6570                        (int) BFD_RELOC_GPREL16);
6571           offset_expr.X_op = O_constant;
6572           offset_expr.X_add_number = 0;
6573         }
6574       else
6575         abort ();
6576
6577       /* Now we load the register(s).  */
6578       if (HAVE_64BIT_GPRS)
6579         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6580                      treg, (int) BFD_RELOC_LO16, AT);
6581       else
6582         {
6583           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6584                        treg, (int) BFD_RELOC_LO16, AT);
6585           if (treg != RA)
6586             {
6587               /* FIXME: How in the world do we deal with the possible
6588                  overflow here?  */
6589               offset_expr.X_add_number += 4;
6590               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6591                            treg + 1, (int) BFD_RELOC_LO16, AT);
6592             }
6593         }
6594
6595       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6596          does not become a variant frag.  */
6597       frag_wane (frag_now);
6598       frag_new (0);
6599
6600       break;
6601
6602     case M_LI_DD:
6603       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6604          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6605          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6606          the value and the low order 32 bits are either zero or in
6607          OFFSET_EXPR.  */
6608       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6609         {
6610           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6611           if (HAVE_64BIT_FPRS)
6612             {
6613               assert (HAVE_64BIT_GPRS);
6614               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6615                            "dmtc1", "t,S", AT, treg);
6616             }
6617           else
6618             {
6619               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6620                            "mtc1", "t,G", AT, treg + 1);
6621               if (offset_expr.X_op == O_absent)
6622                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6623                              "mtc1", "t,G", 0, treg);
6624               else
6625                 {
6626                   assert (offset_expr.X_op == O_constant);
6627                   load_register (&icnt, AT, &offset_expr, 0);
6628                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6629                                "mtc1", "t,G", AT, treg);
6630                 }
6631             }
6632           break;
6633         }
6634
6635       assert (offset_expr.X_op == O_symbol
6636               && offset_expr.X_add_number == 0);
6637       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6638       if (strcmp (s, ".lit8") == 0)
6639         {
6640           if (mips_opts.isa != ISA_MIPS1)
6641             {
6642               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6643                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6644                            mips_gp_register);
6645               return;
6646             }
6647           breg = mips_gp_register;
6648           r = BFD_RELOC_MIPS_LITERAL;
6649           goto dob;
6650         }
6651       else
6652         {
6653           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6654           if (mips_pic == SVR4_PIC)
6655             macro_build ((char *) NULL, &icnt, &offset_expr,
6656                          ADDRESS_LOAD_INSN, "t,o(b)", AT,
6657                          (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6658           else
6659             {
6660               /* FIXME: This won't work for a 64 bit address.  */
6661               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6662             }
6663
6664           if (mips_opts.isa != ISA_MIPS1)
6665             {
6666               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6667                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6668
6669               /* To avoid confusion in tc_gen_reloc, we must ensure
6670                  that this does not become a variant frag.  */
6671               frag_wane (frag_now);
6672               frag_new (0);
6673
6674               break;
6675             }
6676           breg = AT;
6677           r = BFD_RELOC_LO16;
6678           goto dob;
6679         }
6680
6681     case M_L_DOB:
6682       if (mips_opts.arch == CPU_R4650)
6683         {
6684           as_bad (_("opcode not supported on this processor"));
6685           return;
6686         }
6687       /* Even on a big endian machine $fn comes before $fn+1.  We have
6688          to adjust when loading from memory.  */
6689       r = BFD_RELOC_LO16;
6690     dob:
6691       assert (mips_opts.isa == ISA_MIPS1);
6692       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6693                    target_big_endian ? treg + 1 : treg,
6694                    (int) r, breg);
6695       /* FIXME: A possible overflow which I don't know how to deal
6696          with.  */
6697       offset_expr.X_add_number += 4;
6698       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6699                    target_big_endian ? treg : treg + 1,
6700                    (int) r, breg);
6701
6702       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6703          does not become a variant frag.  */
6704       frag_wane (frag_now);
6705       frag_new (0);
6706
6707       if (breg != AT)
6708         return;
6709       break;
6710
6711     case M_L_DAB:
6712       /*
6713        * The MIPS assembler seems to check for X_add_number not
6714        * being double aligned and generating:
6715        *        lui     at,%hi(foo+1)
6716        *        addu    at,at,v1
6717        *        addiu   at,at,%lo(foo+1)
6718        *        lwc1    f2,0(at)
6719        *        lwc1    f3,4(at)
6720        * But, the resulting address is the same after relocation so why
6721        * generate the extra instruction?
6722        */
6723       if (mips_opts.arch == CPU_R4650)
6724         {
6725           as_bad (_("opcode not supported on this processor"));
6726           return;
6727         }
6728       /* Itbl support may require additional care here.  */
6729       coproc = 1;
6730       if (mips_opts.isa != ISA_MIPS1)
6731         {
6732           s = "ldc1";
6733           goto ld;
6734         }
6735
6736       s = "lwc1";
6737       fmt = "T,o(b)";
6738       goto ldd_std;
6739
6740     case M_S_DAB:
6741       if (mips_opts.arch == CPU_R4650)
6742         {
6743           as_bad (_("opcode not supported on this processor"));
6744           return;
6745         }
6746
6747       if (mips_opts.isa != ISA_MIPS1)
6748         {
6749           s = "sdc1";
6750           goto st;
6751         }
6752
6753       s = "swc1";
6754       fmt = "T,o(b)";
6755       /* Itbl support may require additional care here.  */
6756       coproc = 1;
6757       goto ldd_std;
6758
6759     case M_LD_AB:
6760       if (HAVE_64BIT_GPRS)
6761         {
6762           s = "ld";
6763           goto ld;
6764         }
6765
6766       s = "lw";
6767       fmt = "t,o(b)";
6768       goto ldd_std;
6769
6770     case M_SD_AB:
6771       if (HAVE_64BIT_GPRS)
6772         {
6773           s = "sd";
6774           goto st;
6775         }
6776
6777       s = "sw";
6778       fmt = "t,o(b)";
6779
6780     ldd_std:
6781       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6782          loads for the case of doing a pair of loads to simulate an 'ld'.
6783          This is not currently done by the compiler, and assembly coders
6784          writing embedded-pic code can cope.  */
6785
6786       if (offset_expr.X_op != O_symbol
6787           && offset_expr.X_op != O_constant)
6788         {
6789           as_bad (_("expression too complex"));
6790           offset_expr.X_op = O_constant;
6791         }
6792
6793       /* Even on a big endian machine $fn comes before $fn+1.  We have
6794          to adjust when loading from memory.  We set coproc if we must
6795          load $fn+1 first.  */
6796       /* Itbl support may require additional care here.  */
6797       if (! target_big_endian)
6798         coproc = 0;
6799
6800       if (mips_pic == NO_PIC
6801           || offset_expr.X_op == O_constant)
6802         {
6803           char *p;
6804
6805           /* If this is a reference to a GP relative symbol, we want
6806                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6807                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6808              If we have a base register, we use this
6809                addu     $at,$breg,$gp
6810                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6811                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6812              If this is not a GP relative symbol, we want
6813                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6814                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6815                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6816              If there is a base register, we add it to $at after the
6817              lui instruction.  If there is a constant, we always use
6818              the last case.  */
6819           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6820               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6821             {
6822               p = NULL;
6823               used_at = 1;
6824             }
6825           else
6826             {
6827               int off;
6828
6829               if (breg == 0)
6830                 {
6831                   frag_grow (28);
6832                   tempreg = mips_gp_register;
6833                   off = 0;
6834                   used_at = 0;
6835                 }
6836               else
6837                 {
6838                   frag_grow (36);
6839                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6840                                ADDRESS_ADD_INSN, "d,v,t", AT, breg,
6841                                mips_gp_register);
6842                   tempreg = AT;
6843                   off = 4;
6844                   used_at = 1;
6845                 }
6846
6847               /* Itbl support may require additional care here.  */
6848               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6849                            coproc ? treg + 1 : treg,
6850                            (int) BFD_RELOC_GPREL16, tempreg);
6851               offset_expr.X_add_number += 4;
6852
6853               /* Set mips_optimize to 2 to avoid inserting an
6854                  undesired nop.  */
6855               hold_mips_optimize = mips_optimize;
6856               mips_optimize = 2;
6857               /* Itbl support may require additional care here.  */
6858               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6859                            coproc ? treg : treg + 1,
6860                            (int) BFD_RELOC_GPREL16, tempreg);
6861               mips_optimize = hold_mips_optimize;
6862
6863               p = frag_var (rs_machine_dependent, 12 + off, 0,
6864                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6865                                           used_at && mips_opts.noat),
6866                             offset_expr.X_add_symbol, 0, NULL);
6867
6868               /* We just generated two relocs.  When tc_gen_reloc
6869                  handles this case, it will skip the first reloc and
6870                  handle the second.  The second reloc already has an
6871                  extra addend of 4, which we added above.  We must
6872                  subtract it out, and then subtract another 4 to make
6873                  the first reloc come out right.  The second reloc
6874                  will come out right because we are going to add 4 to
6875                  offset_expr when we build its instruction below.
6876
6877                  If we have a symbol, then we don't want to include
6878                  the offset, because it will wind up being included
6879                  when we generate the reloc.  */
6880
6881               if (offset_expr.X_op == O_constant)
6882                 offset_expr.X_add_number -= 8;
6883               else
6884                 {
6885                   offset_expr.X_add_number = -4;
6886                   offset_expr.X_op = O_constant;
6887                 }
6888             }
6889           macro_build_lui (p, &icnt, &offset_expr, AT);
6890           if (p != NULL)
6891             p += 4;
6892           if (breg != 0)
6893             {
6894               macro_build (p, &icnt, (expressionS *) NULL, ADDRESS_ADD_INSN,
6895                            "d,v,t", AT, breg, AT);
6896               if (p != NULL)
6897                 p += 4;
6898             }
6899           /* Itbl support may require additional care here.  */
6900           macro_build (p, &icnt, &offset_expr, s, fmt,
6901                        coproc ? treg + 1 : treg,
6902                        (int) BFD_RELOC_LO16, AT);
6903           if (p != NULL)
6904             p += 4;
6905           /* FIXME: How do we handle overflow here?  */
6906           offset_expr.X_add_number += 4;
6907           /* Itbl support may require additional care here.  */
6908           macro_build (p, &icnt, &offset_expr, s, fmt,
6909                        coproc ? treg : treg + 1,
6910                        (int) BFD_RELOC_LO16, AT);
6911         }
6912       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6913         {
6914           int off;
6915
6916           /* If this is a reference to an external symbol, we want
6917                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6918                nop
6919                <op>     $treg,0($at)
6920                <op>     $treg+1,4($at)
6921              Otherwise we want
6922                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6923                nop
6924                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6925                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6926              If there is a base register we add it to $at before the
6927              lwc1 instructions.  If there is a constant we include it
6928              in the lwc1 instructions.  */
6929           used_at = 1;
6930           expr1.X_add_number = offset_expr.X_add_number;
6931           offset_expr.X_add_number = 0;
6932           if (expr1.X_add_number < -0x8000
6933               || expr1.X_add_number >= 0x8000 - 4)
6934             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6935           if (breg == 0)
6936             off = 0;
6937           else
6938             off = 4;
6939           frag_grow (24 + off);
6940           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6941                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6942                        mips_gp_register);
6943           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6944           if (breg != 0)
6945             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6946                          ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6947           /* Itbl support may require additional care here.  */
6948           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6949                        coproc ? treg + 1 : treg,
6950                        (int) BFD_RELOC_LO16, AT);
6951           expr1.X_add_number += 4;
6952
6953           /* Set mips_optimize to 2 to avoid inserting an undesired
6954              nop.  */
6955           hold_mips_optimize = mips_optimize;
6956           mips_optimize = 2;
6957           /* Itbl support may require additional care here.  */
6958           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6959                        coproc ? treg : treg + 1,
6960                        (int) BFD_RELOC_LO16, AT);
6961           mips_optimize = hold_mips_optimize;
6962
6963           (void) frag_var (rs_machine_dependent, 0, 0,
6964                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6965                            offset_expr.X_add_symbol, 0, NULL);
6966         }
6967       else if (mips_pic == SVR4_PIC)
6968         {
6969           int gpdel, off;
6970           char *p;
6971
6972           /* If this is a reference to an external symbol, we want
6973                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6974                addu     $at,$at,$gp
6975                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6976                nop
6977                <op>     $treg,0($at)
6978                <op>     $treg+1,4($at)
6979              Otherwise we want
6980                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6981                nop
6982                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6983                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6984              If there is a base register we add it to $at before the
6985              lwc1 instructions.  If there is a constant we include it
6986              in the lwc1 instructions.  */
6987           used_at = 1;
6988           expr1.X_add_number = offset_expr.X_add_number;
6989           offset_expr.X_add_number = 0;
6990           if (expr1.X_add_number < -0x8000
6991               || expr1.X_add_number >= 0x8000 - 4)
6992             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6993           if (reg_needs_delay (mips_gp_register))
6994             gpdel = 4;
6995           else
6996             gpdel = 0;
6997           if (breg == 0)
6998             off = 0;
6999           else
7000             off = 4;
7001           frag_grow (56);
7002           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
7003                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
7004           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7005                        ADDRESS_ADD_INSN, "d,v,t", AT, AT, mips_gp_register);
7006           macro_build ((char *) NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
7007                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
7008           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7009           if (breg != 0)
7010             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7011                          ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
7012           /* Itbl support may require additional care here.  */
7013           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7014                        coproc ? treg + 1 : treg,
7015                        (int) BFD_RELOC_LO16, AT);
7016           expr1.X_add_number += 4;
7017
7018           /* Set mips_optimize to 2 to avoid inserting an undesired
7019              nop.  */
7020           hold_mips_optimize = mips_optimize;
7021           mips_optimize = 2;
7022           /* Itbl support may require additional care here.  */
7023           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7024                        coproc ? treg : treg + 1,
7025                        (int) BFD_RELOC_LO16, AT);
7026           mips_optimize = hold_mips_optimize;
7027           expr1.X_add_number -= 4;
7028
7029           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
7030                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
7031                                       8 + gpdel + off, 1, 0),
7032                         offset_expr.X_add_symbol, 0, NULL);
7033           if (gpdel > 0)
7034             {
7035               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7036               p += 4;
7037             }
7038           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
7039                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
7040                        mips_gp_register);
7041           p += 4;
7042           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7043           p += 4;
7044           if (breg != 0)
7045             {
7046               macro_build (p, &icnt, (expressionS *) NULL, ADDRESS_ADD_INSN,
7047                            "d,v,t", AT, breg, AT);
7048               p += 4;
7049             }
7050           /* Itbl support may require additional care here.  */
7051           macro_build (p, &icnt, &expr1, s, fmt,
7052                        coproc ? treg + 1 : treg,
7053                        (int) BFD_RELOC_LO16, AT);
7054           p += 4;
7055           expr1.X_add_number += 4;
7056
7057           /* Set mips_optimize to 2 to avoid inserting an undesired
7058              nop.  */
7059           hold_mips_optimize = mips_optimize;
7060           mips_optimize = 2;
7061           /* Itbl support may require additional care here.  */
7062           macro_build (p, &icnt, &expr1, s, fmt,
7063                        coproc ? treg : treg + 1,
7064                        (int) BFD_RELOC_LO16, AT);
7065           mips_optimize = hold_mips_optimize;
7066         }
7067       else if (mips_pic == EMBEDDED_PIC)
7068         {
7069           /* If there is no base register, we use
7070                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
7071                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
7072              If we have a base register, we use
7073                addu     $at,$breg,$gp
7074                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
7075                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
7076              */
7077           if (breg == 0)
7078             {
7079               tempreg = mips_gp_register;
7080               used_at = 0;
7081             }
7082           else
7083             {
7084               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7085                            ADDRESS_ADD_INSN, "d,v,t", AT, breg,
7086                            mips_gp_register);
7087               tempreg = AT;
7088               used_at = 1;
7089             }
7090
7091           /* Itbl support may require additional care here.  */
7092           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7093                        coproc ? treg + 1 : treg,
7094                        (int) BFD_RELOC_GPREL16, tempreg);
7095           offset_expr.X_add_number += 4;
7096           /* Itbl support may require additional care here.  */
7097           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7098                        coproc ? treg : treg + 1,
7099                        (int) BFD_RELOC_GPREL16, tempreg);
7100         }
7101       else
7102         abort ();
7103
7104       if (! used_at)
7105         return;
7106
7107       break;
7108
7109     case M_LD_OB:
7110       s = "lw";
7111       goto sd_ob;
7112     case M_SD_OB:
7113       s = "sw";
7114     sd_ob:
7115       assert (HAVE_32BIT_ADDRESSES);
7116       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7117                    (int) BFD_RELOC_LO16, breg);
7118       offset_expr.X_add_number += 4;
7119       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7120                    (int) BFD_RELOC_LO16, breg);
7121       return;
7122
7123    /* New code added to support COPZ instructions.
7124       This code builds table entries out of the macros in mip_opcodes.
7125       R4000 uses interlocks to handle coproc delays.
7126       Other chips (like the R3000) require nops to be inserted for delays.
7127
7128       FIXME: Currently, we require that the user handle delays.
7129       In order to fill delay slots for non-interlocked chips,
7130       we must have a way to specify delays based on the coprocessor.
7131       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7132       What are the side-effects of the cop instruction?
7133       What cache support might we have and what are its effects?
7134       Both coprocessor & memory require delays. how long???
7135       What registers are read/set/modified?
7136
7137       If an itbl is provided to interpret cop instructions,
7138       this knowledge can be encoded in the itbl spec.  */
7139
7140     case M_COP0:
7141       s = "c0";
7142       goto copz;
7143     case M_COP1:
7144       s = "c1";
7145       goto copz;
7146     case M_COP2:
7147       s = "c2";
7148       goto copz;
7149     case M_COP3:
7150       s = "c3";
7151     copz:
7152       /* For now we just do C (same as Cz).  The parameter will be
7153          stored in insn_opcode by mips_ip.  */
7154       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
7155                    ip->insn_opcode);
7156       return;
7157
7158     case M_MOVE:
7159       move_register (&icnt, dreg, sreg);
7160       return;
7161
7162 #ifdef LOSING_COMPILER
7163     default:
7164       /* Try and see if this is a new itbl instruction.
7165          This code builds table entries out of the macros in mip_opcodes.
7166          FIXME: For now we just assemble the expression and pass it's
7167          value along as a 32-bit immediate.
7168          We may want to have the assembler assemble this value,
7169          so that we gain the assembler's knowledge of delay slots,
7170          symbols, etc.
7171          Would it be more efficient to use mask (id) here? */
7172       if (itbl_have_entries
7173           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7174         {
7175           s = ip->insn_mo->name;
7176           s2 = "cop3";
7177           coproc = ITBL_DECODE_PNUM (immed_expr);;
7178           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
7179           return;
7180         }
7181       macro2 (ip);
7182       return;
7183     }
7184   if (mips_opts.noat)
7185     as_warn (_("Macro used $at after \".set noat\""));
7186 }
7187
7188 static void
7189 macro2 (ip)
7190      struct mips_cl_insn *ip;
7191 {
7192   register int treg, sreg, dreg, breg;
7193   int tempreg;
7194   int mask;
7195   int icnt = 0;
7196   int used_at;
7197   expressionS expr1;
7198   const char *s;
7199   const char *s2;
7200   const char *fmt;
7201   int likely = 0;
7202   int dbl = 0;
7203   int coproc = 0;
7204   int lr = 0;
7205   int imm = 0;
7206   int off;
7207   offsetT maxnum;
7208   bfd_reloc_code_real_type r;
7209   char *p;
7210
7211   treg = (ip->insn_opcode >> 16) & 0x1f;
7212   dreg = (ip->insn_opcode >> 11) & 0x1f;
7213   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7214   mask = ip->insn_mo->mask;
7215
7216   expr1.X_op = O_constant;
7217   expr1.X_op_symbol = NULL;
7218   expr1.X_add_symbol = NULL;
7219   expr1.X_add_number = 1;
7220
7221   switch (mask)
7222     {
7223 #endif /* LOSING_COMPILER */
7224
7225     case M_DMUL:
7226       dbl = 1;
7227     case M_MUL:
7228       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7229                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7230       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7231                    dreg);
7232       return;
7233
7234     case M_DMUL_I:
7235       dbl = 1;
7236     case M_MUL_I:
7237       /* The MIPS assembler some times generates shifts and adds.  I'm
7238          not trying to be that fancy. GCC should do this for us
7239          anyway.  */
7240       load_register (&icnt, AT, &imm_expr, dbl);
7241       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7242                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
7243       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7244                    dreg);
7245       break;
7246
7247     case M_DMULO_I:
7248       dbl = 1;
7249     case M_MULO_I:
7250       imm = 1;
7251       goto do_mulo;
7252
7253     case M_DMULO:
7254       dbl = 1;
7255     case M_MULO:
7256     do_mulo:
7257       mips_emit_delays (TRUE);
7258       ++mips_opts.noreorder;
7259       mips_any_noreorder = 1;
7260       if (imm)
7261         load_register (&icnt, AT, &imm_expr, dbl);
7262       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7263                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7264       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7265                    dreg);
7266       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7267                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7268       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7269                    AT);
7270       if (mips_trap)
7271         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7272                      "s,t,q", dreg, AT, 6);
7273       else
7274         {
7275           expr1.X_add_number = 8;
7276           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
7277                        AT);
7278           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7279                        0);
7280           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7281                        "c", 6);
7282         }
7283       --mips_opts.noreorder;
7284       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
7285       break;
7286
7287     case M_DMULOU_I:
7288       dbl = 1;
7289     case M_MULOU_I:
7290       imm = 1;
7291       goto do_mulou;
7292
7293     case M_DMULOU:
7294       dbl = 1;
7295     case M_MULOU:
7296     do_mulou:
7297       mips_emit_delays (TRUE);
7298       ++mips_opts.noreorder;
7299       mips_any_noreorder = 1;
7300       if (imm)
7301         load_register (&icnt, AT, &imm_expr, dbl);
7302       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7303                    dbl ? "dmultu" : "multu",
7304                    "s,t", sreg, imm ? AT : treg);
7305       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7306                    AT);
7307       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7308                    dreg);
7309       if (mips_trap)
7310         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7311                      "s,t,q", AT, 0, 6);
7312       else
7313         {
7314           expr1.X_add_number = 8;
7315           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7316           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7317                        0);
7318           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7319                        "c", 6);
7320         }
7321       --mips_opts.noreorder;
7322       break;
7323
7324     case M_DROL:
7325       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7326         {
7327           if (dreg == sreg)
7328             {
7329               tempreg = AT;
7330               used_at = 1;
7331             }
7332           else
7333             {
7334               tempreg = dreg;
7335               used_at = 0;
7336             }
7337           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7338                        "d,w", tempreg, treg);
7339           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7340                        "d,t,s", dreg, sreg, tempreg);
7341           if (used_at)
7342             break;
7343           return;
7344         }
7345       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7346                    "d,v,t", AT, 0, treg);
7347       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7348                    "d,t,s", AT, sreg, AT);
7349       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7350                    "d,t,s", dreg, sreg, treg);
7351       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7352                    "d,v,t", dreg, dreg, AT);
7353       break;
7354
7355     case M_ROL:
7356       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7357         {
7358           if (dreg == sreg)
7359             {
7360               tempreg = AT;
7361               used_at = 1;
7362             }
7363           else
7364             {
7365               tempreg = dreg;
7366               used_at = 0;
7367             }
7368           macro_build ((char *) NULL, &icnt, NULL, "negu",
7369                        "d,w", tempreg, treg);
7370           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7371                        "d,t,s", dreg, sreg, tempreg);
7372           if (used_at)
7373             break;
7374           return;
7375         }
7376       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7377                    "d,v,t", AT, 0, treg);
7378       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7379                    "d,t,s", AT, sreg, AT);
7380       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7381                    "d,t,s", dreg, sreg, treg);
7382       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7383                    "d,v,t", dreg, dreg, AT);
7384       break;
7385
7386     case M_DROL_I:
7387       {
7388         unsigned int rot;
7389         char *l, *r;
7390
7391         if (imm_expr.X_op != O_constant)
7392           as_bad (_("Improper rotate count"));
7393         rot = imm_expr.X_add_number & 0x3f;
7394         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7395           {
7396             rot = (64 - rot) & 0x3f;
7397             if (rot >= 32)
7398               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7399                            "d,w,<", dreg, sreg, rot - 32);
7400             else
7401               macro_build ((char *) NULL, &icnt, NULL, "dror",
7402                            "d,w,<", dreg, sreg, rot);
7403             return;
7404           }
7405         if (rot == 0)
7406           {
7407             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7408                          "d,w,<", dreg, sreg, 0);
7409             return;
7410           }
7411         l = (rot < 0x20) ? "dsll" : "dsll32";
7412         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7413         rot &= 0x1f;
7414         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7415                      "d,w,<", AT, sreg, rot);
7416         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7417                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7418         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7419                      "d,v,t", dreg, dreg, AT);
7420       }
7421       break;
7422
7423     case M_ROL_I:
7424       {
7425         unsigned int rot;
7426
7427         if (imm_expr.X_op != O_constant)
7428           as_bad (_("Improper rotate count"));
7429         rot = imm_expr.X_add_number & 0x1f;
7430         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7431           {
7432             macro_build ((char *) NULL, &icnt, NULL, "ror",
7433                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7434             return;
7435           }
7436         if (rot == 0)
7437           {
7438             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7439                          "d,w,<", dreg, sreg, 0);
7440             return;
7441           }
7442         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7443                      "d,w,<", AT, sreg, rot);
7444         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7445                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7446         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7447                      "d,v,t", dreg, dreg, AT);
7448       }
7449       break;
7450
7451     case M_DROR:
7452       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7453         {
7454           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7455                        "d,t,s", dreg, sreg, treg);
7456           return;
7457         }
7458       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7459                    "d,v,t", AT, 0, treg);
7460       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7461                    "d,t,s", AT, sreg, AT);
7462       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7463                    "d,t,s", dreg, sreg, treg);
7464       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7465                    "d,v,t", dreg, dreg, AT);
7466       break;
7467
7468     case M_ROR:
7469       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7470         {
7471           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7472                        "d,t,s", dreg, sreg, treg);
7473           return;
7474         }
7475       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7476                    "d,v,t", AT, 0, treg);
7477       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7478                    "d,t,s", AT, sreg, AT);
7479       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7480                    "d,t,s", dreg, sreg, treg);
7481       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7482                    "d,v,t", dreg, dreg, AT);
7483       break;
7484
7485     case M_DROR_I:
7486       {
7487         unsigned int rot;
7488         char *l, *r;
7489
7490         if (imm_expr.X_op != O_constant)
7491           as_bad (_("Improper rotate count"));
7492         rot = imm_expr.X_add_number & 0x3f;
7493         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7494           {
7495             if (rot >= 32)
7496               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7497                            "d,w,<", dreg, sreg, rot - 32);
7498             else
7499               macro_build ((char *) NULL, &icnt, NULL, "dror",
7500                            "d,w,<", dreg, sreg, rot);
7501             return;
7502           }
7503         if (rot == 0)
7504           {
7505             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7506                          "d,w,<", dreg, sreg, 0);
7507             return;
7508           }
7509         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7510         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7511         rot &= 0x1f;
7512         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7513                      "d,w,<", AT, sreg, rot);
7514         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7515                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7516         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7517                      "d,v,t", dreg, dreg, AT);
7518       }
7519       break;
7520
7521     case M_ROR_I:
7522       {
7523         unsigned int rot;
7524
7525         if (imm_expr.X_op != O_constant)
7526           as_bad (_("Improper rotate count"));
7527         rot = imm_expr.X_add_number & 0x1f;
7528         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7529           {
7530             macro_build ((char *) NULL, &icnt, NULL, "ror",
7531                          "d,w,<", dreg, sreg, rot);
7532             return;
7533           }
7534         if (rot == 0)
7535           {
7536             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7537                          "d,w,<", dreg, sreg, 0);
7538             return;
7539           }
7540         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7541                      "d,w,<", AT, sreg, rot);
7542         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7543                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7544         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7545                      "d,v,t", dreg, dreg, AT);
7546       }
7547       break;
7548
7549     case M_S_DOB:
7550       if (mips_opts.arch == CPU_R4650)
7551         {
7552           as_bad (_("opcode not supported on this processor"));
7553           return;
7554         }
7555       assert (mips_opts.isa == ISA_MIPS1);
7556       /* Even on a big endian machine $fn comes before $fn+1.  We have
7557          to adjust when storing to memory.  */
7558       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7559                    target_big_endian ? treg + 1 : treg,
7560                    (int) BFD_RELOC_LO16, breg);
7561       offset_expr.X_add_number += 4;
7562       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7563                    target_big_endian ? treg : treg + 1,
7564                    (int) BFD_RELOC_LO16, breg);
7565       return;
7566
7567     case M_SEQ:
7568       if (sreg == 0)
7569         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7570                      treg, (int) BFD_RELOC_LO16);
7571       else if (treg == 0)
7572         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7573                      sreg, (int) BFD_RELOC_LO16);
7574       else
7575         {
7576           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7577                        "d,v,t", dreg, sreg, treg);
7578           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7579                        dreg, (int) BFD_RELOC_LO16);
7580         }
7581       return;
7582
7583     case M_SEQ_I:
7584       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7585         {
7586           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7587                        sreg, (int) BFD_RELOC_LO16);
7588           return;
7589         }
7590       if (sreg == 0)
7591         {
7592           as_warn (_("Instruction %s: result is always false"),
7593                    ip->insn_mo->name);
7594           move_register (&icnt, dreg, 0);
7595           return;
7596         }
7597       if (imm_expr.X_op == O_constant
7598           && imm_expr.X_add_number >= 0
7599           && imm_expr.X_add_number < 0x10000)
7600         {
7601           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7602                        sreg, (int) BFD_RELOC_LO16);
7603           used_at = 0;
7604         }
7605       else if (imm_expr.X_op == O_constant
7606                && imm_expr.X_add_number > -0x8000
7607                && imm_expr.X_add_number < 0)
7608         {
7609           imm_expr.X_add_number = -imm_expr.X_add_number;
7610           macro_build ((char *) NULL, &icnt, &imm_expr,
7611                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7612                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7613           used_at = 0;
7614         }
7615       else
7616         {
7617           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7618           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7619                        "d,v,t", dreg, sreg, AT);
7620           used_at = 1;
7621         }
7622       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7623                    (int) BFD_RELOC_LO16);
7624       if (used_at)
7625         break;
7626       return;
7627
7628     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7629       s = "slt";
7630       goto sge;
7631     case M_SGEU:
7632       s = "sltu";
7633     sge:
7634       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7635                    dreg, sreg, treg);
7636       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7637                    (int) BFD_RELOC_LO16);
7638       return;
7639
7640     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7641     case M_SGEU_I:
7642       if (imm_expr.X_op == O_constant
7643           && imm_expr.X_add_number >= -0x8000
7644           && imm_expr.X_add_number < 0x8000)
7645         {
7646           macro_build ((char *) NULL, &icnt, &imm_expr,
7647                        mask == M_SGE_I ? "slti" : "sltiu",
7648                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7649           used_at = 0;
7650         }
7651       else
7652         {
7653           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7654           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7655                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7656                        AT);
7657           used_at = 1;
7658         }
7659       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7660                    (int) BFD_RELOC_LO16);
7661       if (used_at)
7662         break;
7663       return;
7664
7665     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7666       s = "slt";
7667       goto sgt;
7668     case M_SGTU:
7669       s = "sltu";
7670     sgt:
7671       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7672                    dreg, treg, sreg);
7673       return;
7674
7675     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7676       s = "slt";
7677       goto sgti;
7678     case M_SGTU_I:
7679       s = "sltu";
7680     sgti:
7681       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7682       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7683                    dreg, AT, sreg);
7684       break;
7685
7686     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7687       s = "slt";
7688       goto sle;
7689     case M_SLEU:
7690       s = "sltu";
7691     sle:
7692       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7693                    dreg, treg, sreg);
7694       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7695                    (int) BFD_RELOC_LO16);
7696       return;
7697
7698     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7699       s = "slt";
7700       goto slei;
7701     case M_SLEU_I:
7702       s = "sltu";
7703     slei:
7704       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7705       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7706                    dreg, AT, sreg);
7707       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7708                    (int) BFD_RELOC_LO16);
7709       break;
7710
7711     case M_SLT_I:
7712       if (imm_expr.X_op == O_constant
7713           && imm_expr.X_add_number >= -0x8000
7714           && imm_expr.X_add_number < 0x8000)
7715         {
7716           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7717                        dreg, sreg, (int) BFD_RELOC_LO16);
7718           return;
7719         }
7720       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7721       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7722                    dreg, sreg, AT);
7723       break;
7724
7725     case M_SLTU_I:
7726       if (imm_expr.X_op == O_constant
7727           && imm_expr.X_add_number >= -0x8000
7728           && imm_expr.X_add_number < 0x8000)
7729         {
7730           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7731                        dreg, sreg, (int) BFD_RELOC_LO16);
7732           return;
7733         }
7734       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7735       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7736                    "d,v,t", dreg, sreg, AT);
7737       break;
7738
7739     case M_SNE:
7740       if (sreg == 0)
7741         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7742                      "d,v,t", dreg, 0, treg);
7743       else if (treg == 0)
7744         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7745                      "d,v,t", dreg, 0, sreg);
7746       else
7747         {
7748           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7749                        "d,v,t", dreg, sreg, treg);
7750           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7751                        "d,v,t", dreg, 0, dreg);
7752         }
7753       return;
7754
7755     case M_SNE_I:
7756       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7757         {
7758           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7759                        "d,v,t", dreg, 0, sreg);
7760           return;
7761         }
7762       if (sreg == 0)
7763         {
7764           as_warn (_("Instruction %s: result is always true"),
7765                    ip->insn_mo->name);
7766           macro_build ((char *) NULL, &icnt, &expr1,
7767                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7768                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7769           return;
7770         }
7771       if (imm_expr.X_op == O_constant
7772           && imm_expr.X_add_number >= 0
7773           && imm_expr.X_add_number < 0x10000)
7774         {
7775           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7776                        dreg, sreg, (int) BFD_RELOC_LO16);
7777           used_at = 0;
7778         }
7779       else if (imm_expr.X_op == O_constant
7780                && imm_expr.X_add_number > -0x8000
7781                && imm_expr.X_add_number < 0)
7782         {
7783           imm_expr.X_add_number = -imm_expr.X_add_number;
7784           macro_build ((char *) NULL, &icnt, &imm_expr,
7785                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7786                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7787           used_at = 0;
7788         }
7789       else
7790         {
7791           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7792           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7793                        "d,v,t", dreg, sreg, AT);
7794           used_at = 1;
7795         }
7796       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7797                    "d,v,t", dreg, 0, dreg);
7798       if (used_at)
7799         break;
7800       return;
7801
7802     case M_DSUB_I:
7803       dbl = 1;
7804     case M_SUB_I:
7805       if (imm_expr.X_op == O_constant
7806           && imm_expr.X_add_number > -0x8000
7807           && imm_expr.X_add_number <= 0x8000)
7808         {
7809           imm_expr.X_add_number = -imm_expr.X_add_number;
7810           macro_build ((char *) NULL, &icnt, &imm_expr,
7811                        dbl ? "daddi" : "addi",
7812                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7813           return;
7814         }
7815       load_register (&icnt, AT, &imm_expr, dbl);
7816       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7817                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7818       break;
7819
7820     case M_DSUBU_I:
7821       dbl = 1;
7822     case M_SUBU_I:
7823       if (imm_expr.X_op == O_constant
7824           && imm_expr.X_add_number > -0x8000
7825           && imm_expr.X_add_number <= 0x8000)
7826         {
7827           imm_expr.X_add_number = -imm_expr.X_add_number;
7828           macro_build ((char *) NULL, &icnt, &imm_expr,
7829                        dbl ? "daddiu" : "addiu",
7830                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7831           return;
7832         }
7833       load_register (&icnt, AT, &imm_expr, dbl);
7834       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7835                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7836       break;
7837
7838     case M_TEQ_I:
7839       s = "teq";
7840       goto trap;
7841     case M_TGE_I:
7842       s = "tge";
7843       goto trap;
7844     case M_TGEU_I:
7845       s = "tgeu";
7846       goto trap;
7847     case M_TLT_I:
7848       s = "tlt";
7849       goto trap;
7850     case M_TLTU_I:
7851       s = "tltu";
7852       goto trap;
7853     case M_TNE_I:
7854       s = "tne";
7855     trap:
7856       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7857       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7858                    AT);
7859       break;
7860
7861     case M_TRUNCWS:
7862     case M_TRUNCWD:
7863       assert (mips_opts.isa == ISA_MIPS1);
7864       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7865       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7866
7867       /*
7868        * Is the double cfc1 instruction a bug in the mips assembler;
7869        * or is there a reason for it?
7870        */
7871       mips_emit_delays (TRUE);
7872       ++mips_opts.noreorder;
7873       mips_any_noreorder = 1;
7874       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7875                    treg, RA);
7876       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7877                    treg, RA);
7878       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7879       expr1.X_add_number = 3;
7880       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7881                    (int) BFD_RELOC_LO16);
7882       expr1.X_add_number = 2;
7883       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7884                      (int) BFD_RELOC_LO16);
7885       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7886                    AT, RA);
7887       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7888       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7889               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7890       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7891                    treg, RA);
7892       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7893       --mips_opts.noreorder;
7894       break;
7895
7896     case M_ULH:
7897       s = "lb";
7898       goto ulh;
7899     case M_ULHU:
7900       s = "lbu";
7901     ulh:
7902       if (offset_expr.X_add_number >= 0x7fff)
7903         as_bad (_("operand overflow"));
7904       if (! target_big_endian)
7905         ++offset_expr.X_add_number;
7906       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7907                    (int) BFD_RELOC_LO16, breg);
7908       if (! target_big_endian)
7909         --offset_expr.X_add_number;
7910       else
7911         ++offset_expr.X_add_number;
7912       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7913                    (int) BFD_RELOC_LO16, breg);
7914       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7915                    AT, AT, 8);
7916       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7917                    treg, treg, AT);
7918       break;
7919
7920     case M_ULD:
7921       s = "ldl";
7922       s2 = "ldr";
7923       off = 7;
7924       goto ulw;
7925     case M_ULW:
7926       s = "lwl";
7927       s2 = "lwr";
7928       off = 3;
7929     ulw:
7930       if (offset_expr.X_add_number >= 0x8000 - off)
7931         as_bad (_("operand overflow"));
7932       if (treg != breg)
7933         tempreg = treg;
7934       else
7935         tempreg = AT;
7936       if (! target_big_endian)
7937         offset_expr.X_add_number += off;
7938       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7939                    (int) BFD_RELOC_LO16, breg);
7940       if (! target_big_endian)
7941         offset_expr.X_add_number -= off;
7942       else
7943         offset_expr.X_add_number += off;
7944       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7945                    (int) BFD_RELOC_LO16, breg);
7946
7947       /* If necessary, move the result in tempreg the final destination.  */
7948       if (treg == tempreg)
7949         return;
7950       /* Protect second load's delay slot.  */
7951       if (!gpr_interlocks)
7952         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7953       move_register (&icnt, treg, tempreg);
7954       break;
7955
7956     case M_ULD_A:
7957       s = "ldl";
7958       s2 = "ldr";
7959       off = 7;
7960       goto ulwa;
7961     case M_ULW_A:
7962       s = "lwl";
7963       s2 = "lwr";
7964       off = 3;
7965     ulwa:
7966       used_at = 1;
7967       load_address (&icnt, AT, &offset_expr, &used_at);
7968       if (breg != 0)
7969         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7970                      ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7971       if (! target_big_endian)
7972         expr1.X_add_number = off;
7973       else
7974         expr1.X_add_number = 0;
7975       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7976                    (int) BFD_RELOC_LO16, AT);
7977       if (! target_big_endian)
7978         expr1.X_add_number = 0;
7979       else
7980         expr1.X_add_number = off;
7981       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7982                    (int) BFD_RELOC_LO16, AT);
7983       break;
7984
7985     case M_ULH_A:
7986     case M_ULHU_A:
7987       used_at = 1;
7988       load_address (&icnt, AT, &offset_expr, &used_at);
7989       if (breg != 0)
7990         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7991                      ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7992       if (target_big_endian)
7993         expr1.X_add_number = 0;
7994       macro_build ((char *) NULL, &icnt, &expr1,
7995                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7996                    (int) BFD_RELOC_LO16, AT);
7997       if (target_big_endian)
7998         expr1.X_add_number = 1;
7999       else
8000         expr1.X_add_number = 0;
8001       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8002                    (int) BFD_RELOC_LO16, AT);
8003       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8004                    treg, treg, 8);
8005       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8006                    treg, treg, AT);
8007       break;
8008
8009     case M_USH:
8010       if (offset_expr.X_add_number >= 0x7fff)
8011         as_bad (_("operand overflow"));
8012       if (target_big_endian)
8013         ++offset_expr.X_add_number;
8014       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
8015                    (int) BFD_RELOC_LO16, breg);
8016       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8017                    AT, treg, 8);
8018       if (target_big_endian)
8019         --offset_expr.X_add_number;
8020       else
8021         ++offset_expr.X_add_number;
8022       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
8023                    (int) BFD_RELOC_LO16, breg);
8024       break;
8025
8026     case M_USD:
8027       s = "sdl";
8028       s2 = "sdr";
8029       off = 7;
8030       goto usw;
8031     case M_USW:
8032       s = "swl";
8033       s2 = "swr";
8034       off = 3;
8035     usw:
8036       if (offset_expr.X_add_number >= 0x8000 - off)
8037         as_bad (_("operand overflow"));
8038       if (! target_big_endian)
8039         offset_expr.X_add_number += off;
8040       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
8041                    (int) BFD_RELOC_LO16, breg);
8042       if (! target_big_endian)
8043         offset_expr.X_add_number -= off;
8044       else
8045         offset_expr.X_add_number += off;
8046       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
8047                    (int) BFD_RELOC_LO16, breg);
8048       return;
8049
8050     case M_USD_A:
8051       s = "sdl";
8052       s2 = "sdr";
8053       off = 7;
8054       goto uswa;
8055     case M_USW_A:
8056       s = "swl";
8057       s2 = "swr";
8058       off = 3;
8059     uswa:
8060       used_at = 1;
8061       load_address (&icnt, AT, &offset_expr, &used_at);
8062       if (breg != 0)
8063         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8064                      ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8065       if (! target_big_endian)
8066         expr1.X_add_number = off;
8067       else
8068         expr1.X_add_number = 0;
8069       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8070                    (int) BFD_RELOC_LO16, AT);
8071       if (! target_big_endian)
8072         expr1.X_add_number = 0;
8073       else
8074         expr1.X_add_number = off;
8075       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8076                    (int) BFD_RELOC_LO16, AT);
8077       break;
8078
8079     case M_USH_A:
8080       used_at = 1;
8081       load_address (&icnt, AT, &offset_expr, &used_at);
8082       if (breg != 0)
8083         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8084                      ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8085       if (! target_big_endian)
8086         expr1.X_add_number = 0;
8087       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8088                    (int) BFD_RELOC_LO16, AT);
8089       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8090                    treg, treg, 8);
8091       if (! target_big_endian)
8092         expr1.X_add_number = 1;
8093       else
8094         expr1.X_add_number = 0;
8095       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8096                    (int) BFD_RELOC_LO16, AT);
8097       if (! target_big_endian)
8098         expr1.X_add_number = 0;
8099       else
8100         expr1.X_add_number = 1;
8101       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8102                    (int) BFD_RELOC_LO16, AT);
8103       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8104                    treg, treg, 8);
8105       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8106                    treg, treg, AT);
8107       break;
8108
8109     default:
8110       /* FIXME: Check if this is one of the itbl macros, since they
8111          are added dynamically.  */
8112       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8113       break;
8114     }
8115   if (mips_opts.noat)
8116     as_warn (_("Macro used $at after \".set noat\""));
8117 }
8118
8119 /* Implement macros in mips16 mode.  */
8120
8121 static void
8122 mips16_macro (ip)
8123      struct mips_cl_insn *ip;
8124 {
8125   int mask;
8126   int xreg, yreg, zreg, tmp;
8127   int icnt;
8128   expressionS expr1;
8129   int dbl;
8130   const char *s, *s2, *s3;
8131
8132   mask = ip->insn_mo->mask;
8133
8134   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
8135   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
8136   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
8137
8138   icnt = 0;
8139
8140   expr1.X_op = O_constant;
8141   expr1.X_op_symbol = NULL;
8142   expr1.X_add_symbol = NULL;
8143   expr1.X_add_number = 1;
8144
8145   dbl = 0;
8146
8147   switch (mask)
8148     {
8149     default:
8150       internalError ();
8151
8152     case M_DDIV_3:
8153       dbl = 1;
8154     case M_DIV_3:
8155       s = "mflo";
8156       goto do_div3;
8157     case M_DREM_3:
8158       dbl = 1;
8159     case M_REM_3:
8160       s = "mfhi";
8161     do_div3:
8162       mips_emit_delays (TRUE);
8163       ++mips_opts.noreorder;
8164       mips_any_noreorder = 1;
8165       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8166                    dbl ? "ddiv" : "div",
8167                    "0,x,y", xreg, yreg);
8168       expr1.X_add_number = 2;
8169       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8170       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
8171                    7);
8172
8173       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8174          since that causes an overflow.  We should do that as well,
8175          but I don't see how to do the comparisons without a temporary
8176          register.  */
8177       --mips_opts.noreorder;
8178       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
8179       break;
8180
8181     case M_DIVU_3:
8182       s = "divu";
8183       s2 = "mflo";
8184       goto do_divu3;
8185     case M_REMU_3:
8186       s = "divu";
8187       s2 = "mfhi";
8188       goto do_divu3;
8189     case M_DDIVU_3:
8190       s = "ddivu";
8191       s2 = "mflo";
8192       goto do_divu3;
8193     case M_DREMU_3:
8194       s = "ddivu";
8195       s2 = "mfhi";
8196     do_divu3:
8197       mips_emit_delays (TRUE);
8198       ++mips_opts.noreorder;
8199       mips_any_noreorder = 1;
8200       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
8201                    xreg, yreg);
8202       expr1.X_add_number = 2;
8203       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8204       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
8205                    "6", 7);
8206       --mips_opts.noreorder;
8207       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
8208       break;
8209
8210     case M_DMUL:
8211       dbl = 1;
8212     case M_MUL:
8213       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8214                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8215       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
8216                    zreg);
8217       return;
8218
8219     case M_DSUBU_I:
8220       dbl = 1;
8221       goto do_subu;
8222     case M_SUBU_I:
8223     do_subu:
8224       if (imm_expr.X_op != O_constant)
8225         as_bad (_("Unsupported large constant"));
8226       imm_expr.X_add_number = -imm_expr.X_add_number;
8227       macro_build ((char *) NULL, &icnt, &imm_expr,
8228                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
8229       break;
8230
8231     case M_SUBU_I_2:
8232       if (imm_expr.X_op != O_constant)
8233         as_bad (_("Unsupported large constant"));
8234       imm_expr.X_add_number = -imm_expr.X_add_number;
8235       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
8236                    "x,k", xreg);
8237       break;
8238
8239     case M_DSUBU_I_2:
8240       if (imm_expr.X_op != O_constant)
8241         as_bad (_("Unsupported large constant"));
8242       imm_expr.X_add_number = -imm_expr.X_add_number;
8243       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
8244                    "y,j", yreg);
8245       break;
8246
8247     case M_BEQ:
8248       s = "cmp";
8249       s2 = "bteqz";
8250       goto do_branch;
8251     case M_BNE:
8252       s = "cmp";
8253       s2 = "btnez";
8254       goto do_branch;
8255     case M_BLT:
8256       s = "slt";
8257       s2 = "btnez";
8258       goto do_branch;
8259     case M_BLTU:
8260       s = "sltu";
8261       s2 = "btnez";
8262       goto do_branch;
8263     case M_BLE:
8264       s = "slt";
8265       s2 = "bteqz";
8266       goto do_reverse_branch;
8267     case M_BLEU:
8268       s = "sltu";
8269       s2 = "bteqz";
8270       goto do_reverse_branch;
8271     case M_BGE:
8272       s = "slt";
8273       s2 = "bteqz";
8274       goto do_branch;
8275     case M_BGEU:
8276       s = "sltu";
8277       s2 = "bteqz";
8278       goto do_branch;
8279     case M_BGT:
8280       s = "slt";
8281       s2 = "btnez";
8282       goto do_reverse_branch;
8283     case M_BGTU:
8284       s = "sltu";
8285       s2 = "btnez";
8286
8287     do_reverse_branch:
8288       tmp = xreg;
8289       xreg = yreg;
8290       yreg = tmp;
8291
8292     do_branch:
8293       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
8294                    xreg, yreg);
8295       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8296       break;
8297
8298     case M_BEQ_I:
8299       s = "cmpi";
8300       s2 = "bteqz";
8301       s3 = "x,U";
8302       goto do_branch_i;
8303     case M_BNE_I:
8304       s = "cmpi";
8305       s2 = "btnez";
8306       s3 = "x,U";
8307       goto do_branch_i;
8308     case M_BLT_I:
8309       s = "slti";
8310       s2 = "btnez";
8311       s3 = "x,8";
8312       goto do_branch_i;
8313     case M_BLTU_I:
8314       s = "sltiu";
8315       s2 = "btnez";
8316       s3 = "x,8";
8317       goto do_branch_i;
8318     case M_BLE_I:
8319       s = "slti";
8320       s2 = "btnez";
8321       s3 = "x,8";
8322       goto do_addone_branch_i;
8323     case M_BLEU_I:
8324       s = "sltiu";
8325       s2 = "btnez";
8326       s3 = "x,8";
8327       goto do_addone_branch_i;
8328     case M_BGE_I:
8329       s = "slti";
8330       s2 = "bteqz";
8331       s3 = "x,8";
8332       goto do_branch_i;
8333     case M_BGEU_I:
8334       s = "sltiu";
8335       s2 = "bteqz";
8336       s3 = "x,8";
8337       goto do_branch_i;
8338     case M_BGT_I:
8339       s = "slti";
8340       s2 = "bteqz";
8341       s3 = "x,8";
8342       goto do_addone_branch_i;
8343     case M_BGTU_I:
8344       s = "sltiu";
8345       s2 = "bteqz";
8346       s3 = "x,8";
8347
8348     do_addone_branch_i:
8349       if (imm_expr.X_op != O_constant)
8350         as_bad (_("Unsupported large constant"));
8351       ++imm_expr.X_add_number;
8352
8353     do_branch_i:
8354       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8355       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8356       break;
8357
8358     case M_ABS:
8359       expr1.X_add_number = 0;
8360       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8361       if (xreg != yreg)
8362         move_register (&icnt, xreg, yreg);
8363       expr1.X_add_number = 2;
8364       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8365       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8366                    "neg", "x,w", xreg, xreg);
8367     }
8368 }
8369
8370 /* For consistency checking, verify that all bits are specified either
8371    by the match/mask part of the instruction definition, or by the
8372    operand list.  */
8373 static int
8374 validate_mips_insn (opc)
8375      const struct mips_opcode *opc;
8376 {
8377   const char *p = opc->args;
8378   char c;
8379   unsigned long used_bits = opc->mask;
8380
8381   if ((used_bits & opc->match) != opc->match)
8382     {
8383       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8384               opc->name, opc->args);
8385       return 0;
8386     }
8387 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8388   while (*p)
8389     switch (c = *p++)
8390       {
8391       case ',': break;
8392       case '(': break;
8393       case ')': break;
8394       case '+':
8395         switch (c = *p++)
8396           {
8397           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8398           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8399           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8400           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8401                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8402           default:
8403             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8404                     c, opc->name, opc->args);
8405             return 0;
8406           }
8407         break;
8408       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8409       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8410       case 'A': break;
8411       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8412       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8413       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8414       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8415       case 'F': break;
8416       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8417       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8418       case 'I': break;
8419       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8420       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8421       case 'L': break;
8422       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8423       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8424       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8425       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8426                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8427       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8428       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8429       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8430       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8431       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8432       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8433       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8434       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8435       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8436       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8437       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8438       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8439       case 'f': break;
8440       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8441       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8442       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8443       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8444       case 'l': break;
8445       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8446       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8447       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8448       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8449       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8450       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8451       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8452       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8453       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8454       case 'x': break;
8455       case 'z': break;
8456       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8457       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8458                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8459       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8460       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8461       case '[': break;
8462       case ']': break;
8463       default:
8464         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8465                 c, opc->name, opc->args);
8466         return 0;
8467       }
8468 #undef USE_BITS
8469   if (used_bits != 0xffffffff)
8470     {
8471       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8472               ~used_bits & 0xffffffff, opc->name, opc->args);
8473       return 0;
8474     }
8475   return 1;
8476 }
8477
8478 /* This routine assembles an instruction into its binary format.  As a
8479    side effect, it sets one of the global variables imm_reloc or
8480    offset_reloc to the type of relocation to do if one of the operands
8481    is an address expression.  */
8482
8483 static void
8484 mips_ip (str, ip)
8485      char *str;
8486      struct mips_cl_insn *ip;
8487 {
8488   char *s;
8489   const char *args;
8490   char c = 0;
8491   struct mips_opcode *insn;
8492   char *argsStart;
8493   unsigned int regno;
8494   unsigned int lastregno = 0;
8495   unsigned int lastpos = 0;
8496   unsigned int limlo, limhi;
8497   char *s_reset;
8498   char save_c = 0;
8499
8500   insn_error = NULL;
8501
8502   /* If the instruction contains a '.', we first try to match an instruction
8503      including the '.'.  Then we try again without the '.'.  */
8504   insn = NULL;
8505   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8506     continue;
8507
8508   /* If we stopped on whitespace, then replace the whitespace with null for
8509      the call to hash_find.  Save the character we replaced just in case we
8510      have to re-parse the instruction.  */
8511   if (ISSPACE (*s))
8512     {
8513       save_c = *s;
8514       *s++ = '\0';
8515     }
8516
8517   insn = (struct mips_opcode *) hash_find (op_hash, str);
8518
8519   /* If we didn't find the instruction in the opcode table, try again, but
8520      this time with just the instruction up to, but not including the
8521      first '.'.  */
8522   if (insn == NULL)
8523     {
8524       /* Restore the character we overwrite above (if any).  */
8525       if (save_c)
8526         *(--s) = save_c;
8527
8528       /* Scan up to the first '.' or whitespace.  */
8529       for (s = str;
8530            *s != '\0' && *s != '.' && !ISSPACE (*s);
8531            ++s)
8532         continue;
8533
8534       /* If we did not find a '.', then we can quit now.  */
8535       if (*s != '.')
8536         {
8537           insn_error = "unrecognized opcode";
8538           return;
8539         }
8540
8541       /* Lookup the instruction in the hash table.  */
8542       *s++ = '\0';
8543       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8544         {
8545           insn_error = "unrecognized opcode";
8546           return;
8547         }
8548     }
8549
8550   argsStart = s;
8551   for (;;)
8552     {
8553       bfd_boolean ok;
8554
8555       assert (strcmp (insn->name, str) == 0);
8556
8557       if (OPCODE_IS_MEMBER (insn,
8558                             (mips_opts.isa
8559                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8560                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8561                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8562                             mips_opts.arch))
8563         ok = TRUE;
8564       else
8565         ok = FALSE;
8566
8567       if (insn->pinfo != INSN_MACRO)
8568         {
8569           if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8570             ok = FALSE;
8571         }
8572
8573       if (! ok)
8574         {
8575           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8576               && strcmp (insn->name, insn[1].name) == 0)
8577             {
8578               ++insn;
8579               continue;
8580             }
8581           else
8582             {
8583               if (!insn_error)
8584                 {
8585                   static char buf[100];
8586                   sprintf (buf,
8587                            _("opcode not supported on this processor: %s (%s)"),
8588                            mips_cpu_info_from_arch (mips_opts.arch)->name,
8589                            mips_cpu_info_from_isa (mips_opts.isa)->name);
8590                   insn_error = buf;
8591                 }
8592               if (save_c)
8593                 *(--s) = save_c;
8594               return;
8595             }
8596         }
8597
8598       ip->insn_mo = insn;
8599       ip->insn_opcode = insn->match;
8600       insn_error = NULL;
8601       for (args = insn->args;; ++args)
8602         {
8603           int is_mdmx;
8604
8605           s += strspn (s, " \t");
8606           is_mdmx = 0;
8607           switch (*args)
8608             {
8609             case '\0':          /* end of args */
8610               if (*s == '\0')
8611                 return;
8612               break;
8613
8614             case ',':
8615               if (*s++ == *args)
8616                 continue;
8617               s--;
8618               switch (*++args)
8619                 {
8620                 case 'r':
8621                 case 'v':
8622                   ip->insn_opcode |= lastregno << OP_SH_RS;
8623                   continue;
8624
8625                 case 'w':
8626                   ip->insn_opcode |= lastregno << OP_SH_RT;
8627                   continue;
8628
8629                 case 'W':
8630                   ip->insn_opcode |= lastregno << OP_SH_FT;
8631                   continue;
8632
8633                 case 'V':
8634                   ip->insn_opcode |= lastregno << OP_SH_FS;
8635                   continue;
8636                 }
8637               break;
8638
8639             case '(':
8640               /* Handle optional base register.
8641                  Either the base register is omitted or
8642                  we must have a left paren.  */
8643               /* This is dependent on the next operand specifier
8644                  is a base register specification.  */
8645               assert (args[1] == 'b' || args[1] == '5'
8646                       || args[1] == '-' || args[1] == '4');
8647               if (*s == '\0')
8648                 return;
8649
8650             case ')':           /* these must match exactly */
8651             case '[':
8652             case ']':
8653               if (*s++ == *args)
8654                 continue;
8655               break;
8656
8657             case '+':           /* Opcode extension character.  */
8658               switch (*++args)
8659                 {
8660                 case 'A':               /* ins/ext position, becomes LSB.  */
8661                   limlo = 0;
8662                   limhi = 31;
8663                   my_getExpression (&imm_expr, s);
8664                   check_absolute_expr (ip, &imm_expr);
8665                   if ((unsigned long) imm_expr.X_add_number < limlo
8666                       || (unsigned long) imm_expr.X_add_number > limhi)
8667                     {
8668                       as_bad (_("Improper position (%lu)"),
8669                               (unsigned long) imm_expr.X_add_number);
8670                       imm_expr.X_add_number = limlo;
8671                     }
8672                   lastpos = imm_expr.X_add_number;
8673                   ip->insn_opcode |= (imm_expr.X_add_number
8674                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8675                   imm_expr.X_op = O_absent;
8676                   s = expr_end;
8677                   continue;
8678
8679                 case 'B':               /* ins size, becomes MSB.  */
8680                   limlo = 1;
8681                   limhi = 32;
8682                   my_getExpression (&imm_expr, s);
8683                   check_absolute_expr (ip, &imm_expr);
8684                   /* Check for negative input so that small negative numbers
8685                      will not succeed incorrectly.  The checks against
8686                      (pos+size) transitively check "size" itself,
8687                      assuming that "pos" is reasonable.  */
8688                   if ((long) imm_expr.X_add_number < 0
8689                       || ((unsigned long) imm_expr.X_add_number
8690                           + lastpos) < limlo
8691                       || ((unsigned long) imm_expr.X_add_number
8692                           + lastpos) > limhi)
8693                     {
8694                       as_bad (_("Improper insert size (%lu, position %lu)"),
8695                               (unsigned long) imm_expr.X_add_number,
8696                               (unsigned long) lastpos);
8697                       imm_expr.X_add_number = limlo - lastpos;
8698                     }
8699                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8700                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8701                   imm_expr.X_op = O_absent;
8702                   s = expr_end;
8703                   continue;
8704
8705                 case 'C':               /* ext size, becomes MSBD.  */
8706                   limlo = 1;
8707                   limhi = 32;
8708                   my_getExpression (&imm_expr, s);
8709                   check_absolute_expr (ip, &imm_expr);
8710                   /* Check for negative input so that small negative numbers
8711                      will not succeed incorrectly.  The checks against
8712                      (pos+size) transitively check "size" itself,
8713                      assuming that "pos" is reasonable.  */
8714                   if ((long) imm_expr.X_add_number < 0
8715                       || ((unsigned long) imm_expr.X_add_number
8716                           + lastpos) < limlo
8717                       || ((unsigned long) imm_expr.X_add_number
8718                           + lastpos) > limhi)
8719                     {
8720                       as_bad (_("Improper extract size (%lu, position %lu)"),
8721                               (unsigned long) imm_expr.X_add_number,
8722                               (unsigned long) lastpos);
8723                       imm_expr.X_add_number = limlo - lastpos;
8724                     }
8725                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8726                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8727                   imm_expr.X_op = O_absent;
8728                   s = expr_end;
8729                   continue;
8730
8731                 case 'D':
8732                   /* +D is for disassembly only; never match.  */
8733                   break;
8734
8735                 default:
8736                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8737                     *args, insn->name, insn->args);
8738                   /* Further processing is fruitless.  */
8739                   return;
8740                 }
8741               break;
8742
8743             case '<':           /* must be at least one digit */
8744               /*
8745                * According to the manual, if the shift amount is greater
8746                * than 31 or less than 0, then the shift amount should be
8747                * mod 32.  In reality the mips assembler issues an error.
8748                * We issue a warning and mask out all but the low 5 bits.
8749                */
8750               my_getExpression (&imm_expr, s);
8751               check_absolute_expr (ip, &imm_expr);
8752               if ((unsigned long) imm_expr.X_add_number > 31)
8753                 {
8754                   as_warn (_("Improper shift amount (%lu)"),
8755                            (unsigned long) imm_expr.X_add_number);
8756                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8757                 }
8758               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8759               imm_expr.X_op = O_absent;
8760               s = expr_end;
8761               continue;
8762
8763             case '>':           /* shift amount minus 32 */
8764               my_getExpression (&imm_expr, s);
8765               check_absolute_expr (ip, &imm_expr);
8766               if ((unsigned long) imm_expr.X_add_number < 32
8767                   || (unsigned long) imm_expr.X_add_number > 63)
8768                 break;
8769               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8770               imm_expr.X_op = O_absent;
8771               s = expr_end;
8772               continue;
8773
8774             case 'k':           /* cache code */
8775             case 'h':           /* prefx code */
8776               my_getExpression (&imm_expr, s);
8777               check_absolute_expr (ip, &imm_expr);
8778               if ((unsigned long) imm_expr.X_add_number > 31)
8779                 {
8780                   as_warn (_("Invalid value for `%s' (%lu)"),
8781                            ip->insn_mo->name,
8782                            (unsigned long) imm_expr.X_add_number);
8783                   imm_expr.X_add_number &= 0x1f;
8784                 }
8785               if (*args == 'k')
8786                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8787               else
8788                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8789               imm_expr.X_op = O_absent;
8790               s = expr_end;
8791               continue;
8792
8793             case 'c':           /* break code */
8794               my_getExpression (&imm_expr, s);
8795               check_absolute_expr (ip, &imm_expr);
8796               if ((unsigned long) imm_expr.X_add_number > 1023)
8797                 {
8798                   as_warn (_("Illegal break code (%lu)"),
8799                            (unsigned long) imm_expr.X_add_number);
8800                   imm_expr.X_add_number &= OP_MASK_CODE;
8801                 }
8802               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8803               imm_expr.X_op = O_absent;
8804               s = expr_end;
8805               continue;
8806
8807             case 'q':           /* lower break code */
8808               my_getExpression (&imm_expr, s);
8809               check_absolute_expr (ip, &imm_expr);
8810               if ((unsigned long) imm_expr.X_add_number > 1023)
8811                 {
8812                   as_warn (_("Illegal lower break code (%lu)"),
8813                            (unsigned long) imm_expr.X_add_number);
8814                   imm_expr.X_add_number &= OP_MASK_CODE2;
8815                 }
8816               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8817               imm_expr.X_op = O_absent;
8818               s = expr_end;
8819               continue;
8820
8821             case 'B':           /* 20-bit syscall/break code.  */
8822               my_getExpression (&imm_expr, s);
8823               check_absolute_expr (ip, &imm_expr);
8824               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8825                 as_warn (_("Illegal 20-bit code (%lu)"),
8826                          (unsigned long) imm_expr.X_add_number);
8827               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8828               imm_expr.X_op = O_absent;
8829               s = expr_end;
8830               continue;
8831
8832             case 'C':           /* Coprocessor code */
8833               my_getExpression (&imm_expr, s);
8834               check_absolute_expr (ip, &imm_expr);
8835               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8836                 {
8837                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8838                            (unsigned long) imm_expr.X_add_number);
8839                   imm_expr.X_add_number &= ((1 << 25) - 1);
8840                 }
8841               ip->insn_opcode |= imm_expr.X_add_number;
8842               imm_expr.X_op = O_absent;
8843               s = expr_end;
8844               continue;
8845
8846             case 'J':           /* 19-bit wait code.  */
8847               my_getExpression (&imm_expr, s);
8848               check_absolute_expr (ip, &imm_expr);
8849               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8850                 as_warn (_("Illegal 19-bit code (%lu)"),
8851                          (unsigned long) imm_expr.X_add_number);
8852               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8853               imm_expr.X_op = O_absent;
8854               s = expr_end;
8855               continue;
8856
8857             case 'P':           /* Performance register */
8858               my_getExpression (&imm_expr, s);
8859               check_absolute_expr (ip, &imm_expr);
8860               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8861                 {
8862                   as_warn (_("Invalid performance register (%lu)"),
8863                            (unsigned long) imm_expr.X_add_number);
8864                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8865                 }
8866               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8867               imm_expr.X_op = O_absent;
8868               s = expr_end;
8869               continue;
8870
8871             case 'b':           /* base register */
8872             case 'd':           /* destination register */
8873             case 's':           /* source register */
8874             case 't':           /* target register */
8875             case 'r':           /* both target and source */
8876             case 'v':           /* both dest and source */
8877             case 'w':           /* both dest and target */
8878             case 'E':           /* coprocessor target register */
8879             case 'G':           /* coprocessor destination register */
8880             case 'K':           /* 'rdhwr' destination register */
8881             case 'x':           /* ignore register name */
8882             case 'z':           /* must be zero register */
8883             case 'U':           /* destination register (clo/clz).  */
8884               s_reset = s;
8885               if (s[0] == '$')
8886                 {
8887
8888                   if (ISDIGIT (s[1]))
8889                     {
8890                       ++s;
8891                       regno = 0;
8892                       do
8893                         {
8894                           regno *= 10;
8895                           regno += *s - '0';
8896                           ++s;
8897                         }
8898                       while (ISDIGIT (*s));
8899                       if (regno > 31)
8900                         as_bad (_("Invalid register number (%d)"), regno);
8901                     }
8902                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8903                     goto notreg;
8904                   else
8905                     {
8906                       if (s[1] == 'r' && s[2] == 'a')
8907                         {
8908                           s += 3;
8909                           regno = RA;
8910                         }
8911                       else if (s[1] == 'f' && s[2] == 'p')
8912                         {
8913                           s += 3;
8914                           regno = FP;
8915                         }
8916                       else if (s[1] == 's' && s[2] == 'p')
8917                         {
8918                           s += 3;
8919                           regno = SP;
8920                         }
8921                       else if (s[1] == 'g' && s[2] == 'p')
8922                         {
8923                           s += 3;
8924                           regno = GP;
8925                         }
8926                       else if (s[1] == 'a' && s[2] == 't')
8927                         {
8928                           s += 3;
8929                           regno = AT;
8930                         }
8931                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8932                         {
8933                           s += 4;
8934                           regno = KT0;
8935                         }
8936                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8937                         {
8938                           s += 4;
8939                           regno = KT1;
8940                         }
8941                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8942                         {
8943                           s += 5;
8944                           regno = ZERO;
8945                         }
8946                       else if (itbl_have_entries)
8947                         {
8948                           char *p, *n;
8949                           unsigned long r;
8950
8951                           p = s + 1;    /* advance past '$' */
8952                           n = itbl_get_field (&p);  /* n is name */
8953
8954                           /* See if this is a register defined in an
8955                              itbl entry.  */
8956                           if (itbl_get_reg_val (n, &r))
8957                             {
8958                               /* Get_field advances to the start of
8959                                  the next field, so we need to back
8960                                  rack to the end of the last field.  */
8961                               if (p)
8962                                 s = p - 1;
8963                               else
8964                                 s = strchr (s, '\0');
8965                               regno = r;
8966                             }
8967                           else
8968                             goto notreg;
8969                         }
8970                       else
8971                         goto notreg;
8972                     }
8973                   if (regno == AT
8974                       && ! mips_opts.noat
8975                       && *args != 'E'
8976                       && *args != 'G'
8977                       && *args != 'K')
8978                     as_warn (_("Used $at without \".set noat\""));
8979                   c = *args;
8980                   if (*s == ' ')
8981                     ++s;
8982                   if (args[1] != *s)
8983                     {
8984                       if (c == 'r' || c == 'v' || c == 'w')
8985                         {
8986                           regno = lastregno;
8987                           s = s_reset;
8988                           ++args;
8989                         }
8990                     }
8991                   /* 'z' only matches $0.  */
8992                   if (c == 'z' && regno != 0)
8993                     break;
8994
8995         /* Now that we have assembled one operand, we use the args string
8996          * to figure out where it goes in the instruction.  */
8997                   switch (c)
8998                     {
8999                     case 'r':
9000                     case 's':
9001                     case 'v':
9002                     case 'b':
9003                       ip->insn_opcode |= regno << OP_SH_RS;
9004                       break;
9005                     case 'd':
9006                     case 'G':
9007                     case 'K':
9008                       ip->insn_opcode |= regno << OP_SH_RD;
9009                       break;
9010                     case 'U':
9011                       ip->insn_opcode |= regno << OP_SH_RD;
9012                       ip->insn_opcode |= regno << OP_SH_RT;
9013                       break;
9014                     case 'w':
9015                     case 't':
9016                     case 'E':
9017                       ip->insn_opcode |= regno << OP_SH_RT;
9018                       break;
9019                     case 'x':
9020                       /* This case exists because on the r3000 trunc
9021                          expands into a macro which requires a gp
9022                          register.  On the r6000 or r4000 it is
9023                          assembled into a single instruction which
9024                          ignores the register.  Thus the insn version
9025                          is MIPS_ISA2 and uses 'x', and the macro
9026                          version is MIPS_ISA1 and uses 't'.  */
9027                       break;
9028                     case 'z':
9029                       /* This case is for the div instruction, which
9030                          acts differently if the destination argument
9031                          is $0.  This only matches $0, and is checked
9032                          outside the switch.  */
9033                       break;
9034                     case 'D':
9035                       /* Itbl operand; not yet implemented. FIXME ?? */
9036                       break;
9037                       /* What about all other operands like 'i', which
9038                          can be specified in the opcode table? */
9039                     }
9040                   lastregno = regno;
9041                   continue;
9042                 }
9043             notreg:
9044               switch (*args++)
9045                 {
9046                 case 'r':
9047                 case 'v':
9048                   ip->insn_opcode |= lastregno << OP_SH_RS;
9049                   continue;
9050                 case 'w':
9051                   ip->insn_opcode |= lastregno << OP_SH_RT;
9052                   continue;
9053                 }
9054               break;
9055
9056             case 'O':           /* MDMX alignment immediate constant.  */
9057               my_getExpression (&imm_expr, s);
9058               check_absolute_expr (ip, &imm_expr);
9059               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9060                 {
9061                   as_warn ("Improper align amount (%ld), using low bits",
9062                            (long) imm_expr.X_add_number);
9063                   imm_expr.X_add_number &= OP_MASK_ALN;
9064                 }
9065               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
9066               imm_expr.X_op = O_absent;
9067               s = expr_end;
9068               continue;
9069
9070             case 'Q':           /* MDMX vector, element sel, or const.  */
9071               if (s[0] != '$')
9072                 {
9073                   /* MDMX Immediate.  */
9074                   my_getExpression (&imm_expr, s);
9075                   check_absolute_expr (ip, &imm_expr);
9076                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9077                     {
9078                       as_warn (_("Invalid MDMX Immediate (%ld)"),
9079                                (long) imm_expr.X_add_number);
9080                       imm_expr.X_add_number &= OP_MASK_FT;
9081                     }
9082                   imm_expr.X_add_number &= OP_MASK_FT;
9083                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9084                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9085                   else
9086                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9087                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
9088                   imm_expr.X_op = O_absent;
9089                   s = expr_end;
9090                   continue;
9091                 }
9092               /* Not MDMX Immediate.  Fall through.  */
9093             case 'X':           /* MDMX destination register.  */
9094             case 'Y':           /* MDMX source register.  */
9095             case 'Z':           /* MDMX target register.  */
9096               is_mdmx = 1;
9097             case 'D':           /* floating point destination register */
9098             case 'S':           /* floating point source register */
9099             case 'T':           /* floating point target register */
9100             case 'R':           /* floating point source register */
9101             case 'V':
9102             case 'W':
9103               s_reset = s;
9104               /* Accept $fN for FP and MDMX register numbers, and in
9105                  addition accept $vN for MDMX register numbers.  */
9106               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
9107                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
9108                       && ISDIGIT (s[2])))
9109                 {
9110                   s += 2;
9111                   regno = 0;
9112                   do
9113                     {
9114                       regno *= 10;
9115                       regno += *s - '0';
9116                       ++s;
9117                     }
9118                   while (ISDIGIT (*s));
9119
9120                   if (regno > 31)
9121                     as_bad (_("Invalid float register number (%d)"), regno);
9122
9123                   if ((regno & 1) != 0
9124                       && HAVE_32BIT_FPRS
9125                       && ! (strcmp (str, "mtc1") == 0
9126                             || strcmp (str, "mfc1") == 0
9127                             || strcmp (str, "lwc1") == 0
9128                             || strcmp (str, "swc1") == 0
9129                             || strcmp (str, "l.s") == 0
9130                             || strcmp (str, "s.s") == 0))
9131                     as_warn (_("Float register should be even, was %d"),
9132                              regno);
9133
9134                   c = *args;
9135                   if (*s == ' ')
9136                     ++s;
9137                   if (args[1] != *s)
9138                     {
9139                       if (c == 'V' || c == 'W')
9140                         {
9141                           regno = lastregno;
9142                           s = s_reset;
9143                           ++args;
9144                         }
9145                     }
9146                   switch (c)
9147                     {
9148                     case 'D':
9149                     case 'X':
9150                       ip->insn_opcode |= regno << OP_SH_FD;
9151                       break;
9152                     case 'V':
9153                     case 'S':
9154                     case 'Y':
9155                       ip->insn_opcode |= regno << OP_SH_FS;
9156                       break;
9157                     case 'Q':
9158                       /* This is like 'Z', but also needs to fix the MDMX
9159                          vector/scalar select bits.  Note that the
9160                          scalar immediate case is handled above.  */
9161                       if (*s == '[')
9162                         {
9163                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9164                           int max_el = (is_qh ? 3 : 7);
9165                           s++;
9166                           my_getExpression(&imm_expr, s);
9167                           check_absolute_expr (ip, &imm_expr);
9168                           s = expr_end;
9169                           if (imm_expr.X_add_number > max_el)
9170                             as_bad(_("Bad element selector %ld"),
9171                                    (long) imm_expr.X_add_number);
9172                           imm_expr.X_add_number &= max_el;
9173                           ip->insn_opcode |= (imm_expr.X_add_number
9174                                               << (OP_SH_VSEL +
9175                                                   (is_qh ? 2 : 1)));
9176                           if (*s != ']')
9177                             as_warn(_("Expecting ']' found '%s'"), s);
9178                           else
9179                             s++;
9180                         }
9181                       else
9182                         {
9183                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9184                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9185                                                 << OP_SH_VSEL);
9186                           else
9187                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9188                                                 OP_SH_VSEL);
9189                         }
9190                       /* Fall through */
9191                     case 'W':
9192                     case 'T':
9193                     case 'Z':
9194                       ip->insn_opcode |= regno << OP_SH_FT;
9195                       break;
9196                     case 'R':
9197                       ip->insn_opcode |= regno << OP_SH_FR;
9198                       break;
9199                     }
9200                   lastregno = regno;
9201                   continue;
9202                 }
9203
9204               switch (*args++)
9205                 {
9206                 case 'V':
9207                   ip->insn_opcode |= lastregno << OP_SH_FS;
9208                   continue;
9209                 case 'W':
9210                   ip->insn_opcode |= lastregno << OP_SH_FT;
9211                   continue;
9212                 }
9213               break;
9214
9215             case 'I':
9216               my_getExpression (&imm_expr, s);
9217               if (imm_expr.X_op != O_big
9218                   && imm_expr.X_op != O_constant)
9219                 insn_error = _("absolute expression required");
9220               s = expr_end;
9221               continue;
9222
9223             case 'A':
9224               my_getExpression (&offset_expr, s);
9225               *imm_reloc = BFD_RELOC_32;
9226               s = expr_end;
9227               continue;
9228
9229             case 'F':
9230             case 'L':
9231             case 'f':
9232             case 'l':
9233               {
9234                 int f64;
9235                 int using_gprs;
9236                 char *save_in;
9237                 char *err;
9238                 unsigned char temp[8];
9239                 int len;
9240                 unsigned int length;
9241                 segT seg;
9242                 subsegT subseg;
9243                 char *p;
9244
9245                 /* These only appear as the last operand in an
9246                    instruction, and every instruction that accepts
9247                    them in any variant accepts them in all variants.
9248                    This means we don't have to worry about backing out
9249                    any changes if the instruction does not match.
9250
9251                    The difference between them is the size of the
9252                    floating point constant and where it goes.  For 'F'
9253                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9254                    is 32 bits.  Where the constant is placed is based
9255                    on how the MIPS assembler does things:
9256                     F -- .rdata
9257                     L -- .lit8
9258                     f -- immediate value
9259                     l -- .lit4
9260
9261                     The .lit4 and .lit8 sections are only used if
9262                     permitted by the -G argument.
9263
9264                     When generating embedded PIC code, we use the
9265                     .lit8 section but not the .lit4 section (we can do
9266                     .lit4 inline easily; we need to put .lit8
9267                     somewhere in the data segment, and using .lit8
9268                     permits the linker to eventually combine identical
9269                     .lit8 entries).
9270
9271                     The code below needs to know whether the target register
9272                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9273                     'F' are used with GPR-based instructions and 'l' and
9274                     'L' are used with FPR-based instructions.  */
9275
9276                 f64 = *args == 'F' || *args == 'L';
9277                 using_gprs = *args == 'F' || *args == 'f';
9278
9279                 save_in = input_line_pointer;
9280                 input_line_pointer = s;
9281                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9282                 length = len;
9283                 s = input_line_pointer;
9284                 input_line_pointer = save_in;
9285                 if (err != NULL && *err != '\0')
9286                   {
9287                     as_bad (_("Bad floating point constant: %s"), err);
9288                     memset (temp, '\0', sizeof temp);
9289                     length = f64 ? 8 : 4;
9290                   }
9291
9292                 assert (length == (unsigned) (f64 ? 8 : 4));
9293
9294                 if (*args == 'f'
9295                     || (*args == 'l'
9296                         && (! USE_GLOBAL_POINTER_OPT
9297                             || mips_pic == EMBEDDED_PIC
9298                             || g_switch_value < 4
9299                             || (temp[0] == 0 && temp[1] == 0)
9300                             || (temp[2] == 0 && temp[3] == 0))))
9301                   {
9302                     imm_expr.X_op = O_constant;
9303                     if (! target_big_endian)
9304                       imm_expr.X_add_number = bfd_getl32 (temp);
9305                     else
9306                       imm_expr.X_add_number = bfd_getb32 (temp);
9307                   }
9308                 else if (length > 4
9309                          && ! mips_disable_float_construction
9310                          /* Constants can only be constructed in GPRs and
9311                             copied to FPRs if the GPRs are at least as wide
9312                             as the FPRs.  Force the constant into memory if
9313                             we are using 64-bit FPRs but the GPRs are only
9314                             32 bits wide.  */
9315                          && (using_gprs
9316                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9317                          && ((temp[0] == 0 && temp[1] == 0)
9318                              || (temp[2] == 0 && temp[3] == 0))
9319                          && ((temp[4] == 0 && temp[5] == 0)
9320                              || (temp[6] == 0 && temp[7] == 0)))
9321                   {
9322                     /* The value is simple enough to load with a couple of
9323                        instructions.  If using 32-bit registers, set
9324                        imm_expr to the high order 32 bits and offset_expr to
9325                        the low order 32 bits.  Otherwise, set imm_expr to
9326                        the entire 64 bit constant.  */
9327                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9328                       {
9329                         imm_expr.X_op = O_constant;
9330                         offset_expr.X_op = O_constant;
9331                         if (! target_big_endian)
9332                           {
9333                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9334                             offset_expr.X_add_number = bfd_getl32 (temp);
9335                           }
9336                         else
9337                           {
9338                             imm_expr.X_add_number = bfd_getb32 (temp);
9339                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9340                           }
9341                         if (offset_expr.X_add_number == 0)
9342                           offset_expr.X_op = O_absent;
9343                       }
9344                     else if (sizeof (imm_expr.X_add_number) > 4)
9345                       {
9346                         imm_expr.X_op = O_constant;
9347                         if (! target_big_endian)
9348                           imm_expr.X_add_number = bfd_getl64 (temp);
9349                         else
9350                           imm_expr.X_add_number = bfd_getb64 (temp);
9351                       }
9352                     else
9353                       {
9354                         imm_expr.X_op = O_big;
9355                         imm_expr.X_add_number = 4;
9356                         if (! target_big_endian)
9357                           {
9358                             generic_bignum[0] = bfd_getl16 (temp);
9359                             generic_bignum[1] = bfd_getl16 (temp + 2);
9360                             generic_bignum[2] = bfd_getl16 (temp + 4);
9361                             generic_bignum[3] = bfd_getl16 (temp + 6);
9362                           }
9363                         else
9364                           {
9365                             generic_bignum[0] = bfd_getb16 (temp + 6);
9366                             generic_bignum[1] = bfd_getb16 (temp + 4);
9367                             generic_bignum[2] = bfd_getb16 (temp + 2);
9368                             generic_bignum[3] = bfd_getb16 (temp);
9369                           }
9370                       }
9371                   }
9372                 else
9373                   {
9374                     const char *newname;
9375                     segT new_seg;
9376
9377                     /* Switch to the right section.  */
9378                     seg = now_seg;
9379                     subseg = now_subseg;
9380                     switch (*args)
9381                       {
9382                       default: /* unused default case avoids warnings.  */
9383                       case 'L':
9384                         newname = RDATA_SECTION_NAME;
9385                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9386                             || mips_pic == EMBEDDED_PIC)
9387                           newname = ".lit8";
9388                         break;
9389                       case 'F':
9390                         if (mips_pic == EMBEDDED_PIC)
9391                           newname = ".lit8";
9392                         else
9393                           newname = RDATA_SECTION_NAME;
9394                         break;
9395                       case 'l':
9396                         assert (!USE_GLOBAL_POINTER_OPT
9397                                 || g_switch_value >= 4);
9398                         newname = ".lit4";
9399                         break;
9400                       }
9401                     new_seg = subseg_new (newname, (subsegT) 0);
9402                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9403                       bfd_set_section_flags (stdoutput, new_seg,
9404                                              (SEC_ALLOC
9405                                               | SEC_LOAD
9406                                               | SEC_READONLY
9407                                               | SEC_DATA));
9408                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9409                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9410                         && strcmp (TARGET_OS, "elf") != 0)
9411                       record_alignment (new_seg, 4);
9412                     else
9413                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9414                     if (seg == now_seg)
9415                       as_bad (_("Can't use floating point insn in this section"));
9416
9417                     /* Set the argument to the current address in the
9418                        section.  */
9419                     offset_expr.X_op = O_symbol;
9420                     offset_expr.X_add_symbol =
9421                       symbol_new ("L0\001", now_seg,
9422                                   (valueT) frag_now_fix (), frag_now);
9423                     offset_expr.X_add_number = 0;
9424
9425                     /* Put the floating point number into the section.  */
9426                     p = frag_more ((int) length);
9427                     memcpy (p, temp, length);
9428
9429                     /* Switch back to the original section.  */
9430                     subseg_set (seg, subseg);
9431                   }
9432               }
9433               continue;
9434
9435             case 'i':           /* 16 bit unsigned immediate */
9436             case 'j':           /* 16 bit signed immediate */
9437               *imm_reloc = BFD_RELOC_LO16;
9438               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9439                 {
9440                   int more;
9441                   offsetT minval, maxval;
9442
9443                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9444                           && strcmp (insn->name, insn[1].name) == 0);
9445
9446                   /* If the expression was written as an unsigned number,
9447                      only treat it as signed if there are no more
9448                      alternatives.  */
9449                   if (more
9450                       && *args == 'j'
9451                       && sizeof (imm_expr.X_add_number) <= 4
9452                       && imm_expr.X_op == O_constant
9453                       && imm_expr.X_add_number < 0
9454                       && imm_expr.X_unsigned
9455                       && HAVE_64BIT_GPRS)
9456                     break;
9457
9458                   /* For compatibility with older assemblers, we accept
9459                      0x8000-0xffff as signed 16-bit numbers when only
9460                      signed numbers are allowed.  */
9461                   if (*args == 'i')
9462                     minval = 0, maxval = 0xffff;
9463                   else if (more)
9464                     minval = -0x8000, maxval = 0x7fff;
9465                   else
9466                     minval = -0x8000, maxval = 0xffff;
9467
9468                   if (imm_expr.X_op != O_constant
9469                       || imm_expr.X_add_number < minval
9470                       || imm_expr.X_add_number > maxval)
9471                     {
9472                       if (more)
9473                         break;
9474                       if (imm_expr.X_op == O_constant
9475                           || imm_expr.X_op == O_big)
9476                         as_bad (_("expression out of range"));
9477                     }
9478                 }
9479               s = expr_end;
9480               continue;
9481
9482             case 'o':           /* 16 bit offset */
9483               /* Check whether there is only a single bracketed expression
9484                  left.  If so, it must be the base register and the
9485                  constant must be zero.  */
9486               if (*s == '(' && strchr (s + 1, '(') == 0)
9487                 {
9488                   offset_expr.X_op = O_constant;
9489                   offset_expr.X_add_number = 0;
9490                   continue;
9491                 }
9492
9493               /* If this value won't fit into a 16 bit offset, then go
9494                  find a macro that will generate the 32 bit offset
9495                  code pattern.  */
9496               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9497                   && (offset_expr.X_op != O_constant
9498                       || offset_expr.X_add_number >= 0x8000
9499                       || offset_expr.X_add_number < -0x8000))
9500                 break;
9501
9502               s = expr_end;
9503               continue;
9504
9505             case 'p':           /* pc relative offset */
9506               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9507               my_getExpression (&offset_expr, s);
9508               s = expr_end;
9509               continue;
9510
9511             case 'u':           /* upper 16 bits */
9512               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9513                   && imm_expr.X_op == O_constant
9514                   && (imm_expr.X_add_number < 0
9515                       || imm_expr.X_add_number >= 0x10000))
9516                 as_bad (_("lui expression not in range 0..65535"));
9517               s = expr_end;
9518               continue;
9519
9520             case 'a':           /* 26 bit address */
9521               my_getExpression (&offset_expr, s);
9522               s = expr_end;
9523               *offset_reloc = BFD_RELOC_MIPS_JMP;
9524               continue;
9525
9526             case 'N':           /* 3 bit branch condition code */
9527             case 'M':           /* 3 bit compare condition code */
9528               if (strncmp (s, "$fcc", 4) != 0)
9529                 break;
9530               s += 4;
9531               regno = 0;
9532               do
9533                 {
9534                   regno *= 10;
9535                   regno += *s - '0';
9536                   ++s;
9537                 }
9538               while (ISDIGIT (*s));
9539               if (regno > 7)
9540                 as_bad (_("invalid condition code register $fcc%d"), regno);
9541               if (*args == 'N')
9542                 ip->insn_opcode |= regno << OP_SH_BCC;
9543               else
9544                 ip->insn_opcode |= regno << OP_SH_CCC;
9545               continue;
9546
9547             case 'H':
9548               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9549                 s += 2;
9550               if (ISDIGIT (*s))
9551                 {
9552                   c = 0;
9553                   do
9554                     {
9555                       c *= 10;
9556                       c += *s - '0';
9557                       ++s;
9558                     }
9559                   while (ISDIGIT (*s));
9560                 }
9561               else
9562                 c = 8; /* Invalid sel value.  */
9563
9564               if (c > 7)
9565                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9566               ip->insn_opcode |= c;
9567               continue;
9568
9569             case 'e':
9570               /* Must be at least one digit.  */
9571               my_getExpression (&imm_expr, s);
9572               check_absolute_expr (ip, &imm_expr);
9573
9574               if ((unsigned long) imm_expr.X_add_number
9575                   > (unsigned long) OP_MASK_VECBYTE)
9576                 {
9577                   as_bad (_("bad byte vector index (%ld)"),
9578                            (long) imm_expr.X_add_number);
9579                   imm_expr.X_add_number = 0;
9580                 }
9581
9582               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9583               imm_expr.X_op = O_absent;
9584               s = expr_end;
9585               continue;
9586
9587             case '%':
9588               my_getExpression (&imm_expr, s);
9589               check_absolute_expr (ip, &imm_expr);
9590
9591               if ((unsigned long) imm_expr.X_add_number
9592                   > (unsigned long) OP_MASK_VECALIGN)
9593                 {
9594                   as_bad (_("bad byte vector index (%ld)"),
9595                            (long) imm_expr.X_add_number);
9596                   imm_expr.X_add_number = 0;
9597                 }
9598
9599               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9600               imm_expr.X_op = O_absent;
9601               s = expr_end;
9602               continue;
9603
9604             default:
9605               as_bad (_("bad char = '%c'\n"), *args);
9606               internalError ();
9607             }
9608           break;
9609         }
9610       /* Args don't match.  */
9611       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9612           !strcmp (insn->name, insn[1].name))
9613         {
9614           ++insn;
9615           s = argsStart;
9616           insn_error = _("illegal operands");
9617           continue;
9618         }
9619       if (save_c)
9620         *(--s) = save_c;
9621       insn_error = _("illegal operands");
9622       return;
9623     }
9624 }
9625
9626 /* This routine assembles an instruction into its binary format when
9627    assembling for the mips16.  As a side effect, it sets one of the
9628    global variables imm_reloc or offset_reloc to the type of
9629    relocation to do if one of the operands is an address expression.
9630    It also sets mips16_small and mips16_ext if the user explicitly
9631    requested a small or extended instruction.  */
9632
9633 static void
9634 mips16_ip (str, ip)
9635      char *str;
9636      struct mips_cl_insn *ip;
9637 {
9638   char *s;
9639   const char *args;
9640   struct mips_opcode *insn;
9641   char *argsstart;
9642   unsigned int regno;
9643   unsigned int lastregno = 0;
9644   char *s_reset;
9645
9646   insn_error = NULL;
9647
9648   mips16_small = FALSE;
9649   mips16_ext = FALSE;
9650
9651   for (s = str; ISLOWER (*s); ++s)
9652     ;
9653   switch (*s)
9654     {
9655     case '\0':
9656       break;
9657
9658     case ' ':
9659       *s++ = '\0';
9660       break;
9661
9662     case '.':
9663       if (s[1] == 't' && s[2] == ' ')
9664         {
9665           *s = '\0';
9666           mips16_small = TRUE;
9667           s += 3;
9668           break;
9669         }
9670       else if (s[1] == 'e' && s[2] == ' ')
9671         {
9672           *s = '\0';
9673           mips16_ext = TRUE;
9674           s += 3;
9675           break;
9676         }
9677       /* Fall through.  */
9678     default:
9679       insn_error = _("unknown opcode");
9680       return;
9681     }
9682
9683   if (mips_opts.noautoextend && ! mips16_ext)
9684     mips16_small = TRUE;
9685
9686   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9687     {
9688       insn_error = _("unrecognized opcode");
9689       return;
9690     }
9691
9692   argsstart = s;
9693   for (;;)
9694     {
9695       assert (strcmp (insn->name, str) == 0);
9696
9697       ip->insn_mo = insn;
9698       ip->insn_opcode = insn->match;
9699       ip->use_extend = FALSE;
9700       imm_expr.X_op = O_absent;
9701       imm_reloc[0] = BFD_RELOC_UNUSED;
9702       imm_reloc[1] = BFD_RELOC_UNUSED;
9703       imm_reloc[2] = BFD_RELOC_UNUSED;
9704       offset_expr.X_op = O_absent;
9705       offset_reloc[0] = BFD_RELOC_UNUSED;
9706       offset_reloc[1] = BFD_RELOC_UNUSED;
9707       offset_reloc[2] = BFD_RELOC_UNUSED;
9708       for (args = insn->args; 1; ++args)
9709         {
9710           int c;
9711
9712           if (*s == ' ')
9713             ++s;
9714
9715           /* In this switch statement we call break if we did not find
9716              a match, continue if we did find a match, or return if we
9717              are done.  */
9718
9719           c = *args;
9720           switch (c)
9721             {
9722             case '\0':
9723               if (*s == '\0')
9724                 {
9725                   /* Stuff the immediate value in now, if we can.  */
9726                   if (imm_expr.X_op == O_constant
9727                       && *imm_reloc > BFD_RELOC_UNUSED
9728                       && insn->pinfo != INSN_MACRO)
9729                     {
9730                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9731                                     imm_expr.X_add_number, TRUE, mips16_small,
9732                                     mips16_ext, &ip->insn_opcode,
9733                                     &ip->use_extend, &ip->extend);
9734                       imm_expr.X_op = O_absent;
9735                       *imm_reloc = BFD_RELOC_UNUSED;
9736                     }
9737
9738                   return;
9739                 }
9740               break;
9741
9742             case ',':
9743               if (*s++ == c)
9744                 continue;
9745               s--;
9746               switch (*++args)
9747                 {
9748                 case 'v':
9749                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9750                   continue;
9751                 case 'w':
9752                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9753                   continue;
9754                 }
9755               break;
9756
9757             case '(':
9758             case ')':
9759               if (*s++ == c)
9760                 continue;
9761               break;
9762
9763             case 'v':
9764             case 'w':
9765               if (s[0] != '$')
9766                 {
9767                   if (c == 'v')
9768                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9769                   else
9770                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9771                   ++args;
9772                   continue;
9773                 }
9774               /* Fall through.  */
9775             case 'x':
9776             case 'y':
9777             case 'z':
9778             case 'Z':
9779             case '0':
9780             case 'S':
9781             case 'R':
9782             case 'X':
9783             case 'Y':
9784               if (s[0] != '$')
9785                 break;
9786               s_reset = s;
9787               if (ISDIGIT (s[1]))
9788                 {
9789                   ++s;
9790                   regno = 0;
9791                   do
9792                     {
9793                       regno *= 10;
9794                       regno += *s - '0';
9795                       ++s;
9796                     }
9797                   while (ISDIGIT (*s));
9798                   if (regno > 31)
9799                     {
9800                       as_bad (_("invalid register number (%d)"), regno);
9801                       regno = 2;
9802                     }
9803                 }
9804               else
9805                 {
9806                   if (s[1] == 'r' && s[2] == 'a')
9807                     {
9808                       s += 3;
9809                       regno = RA;
9810                     }
9811                   else if (s[1] == 'f' && s[2] == 'p')
9812                     {
9813                       s += 3;
9814                       regno = FP;
9815                     }
9816                   else if (s[1] == 's' && s[2] == 'p')
9817                     {
9818                       s += 3;
9819                       regno = SP;
9820                     }
9821                   else if (s[1] == 'g' && s[2] == 'p')
9822                     {
9823                       s += 3;
9824                       regno = GP;
9825                     }
9826                   else if (s[1] == 'a' && s[2] == 't')
9827                     {
9828                       s += 3;
9829                       regno = AT;
9830                     }
9831                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9832                     {
9833                       s += 4;
9834                       regno = KT0;
9835                     }
9836                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9837                     {
9838                       s += 4;
9839                       regno = KT1;
9840                     }
9841                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9842                     {
9843                       s += 5;
9844                       regno = ZERO;
9845                     }
9846                   else
9847                     break;
9848                 }
9849
9850               if (*s == ' ')
9851                 ++s;
9852               if (args[1] != *s)
9853                 {
9854                   if (c == 'v' || c == 'w')
9855                     {
9856                       regno = mips16_to_32_reg_map[lastregno];
9857                       s = s_reset;
9858                       ++args;
9859                     }
9860                 }
9861
9862               switch (c)
9863                 {
9864                 case 'x':
9865                 case 'y':
9866                 case 'z':
9867                 case 'v':
9868                 case 'w':
9869                 case 'Z':
9870                   regno = mips32_to_16_reg_map[regno];
9871                   break;
9872
9873                 case '0':
9874                   if (regno != 0)
9875                     regno = ILLEGAL_REG;
9876                   break;
9877
9878                 case 'S':
9879                   if (regno != SP)
9880                     regno = ILLEGAL_REG;
9881                   break;
9882
9883                 case 'R':
9884                   if (regno != RA)
9885                     regno = ILLEGAL_REG;
9886                   break;
9887
9888                 case 'X':
9889                 case 'Y':
9890                   if (regno == AT && ! mips_opts.noat)
9891                     as_warn (_("used $at without \".set noat\""));
9892                   break;
9893
9894                 default:
9895                   internalError ();
9896                 }
9897
9898               if (regno == ILLEGAL_REG)
9899                 break;
9900
9901               switch (c)
9902                 {
9903                 case 'x':
9904                 case 'v':
9905                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9906                   break;
9907                 case 'y':
9908                 case 'w':
9909                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9910                   break;
9911                 case 'z':
9912                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9913                   break;
9914                 case 'Z':
9915                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9916                 case '0':
9917                 case 'S':
9918                 case 'R':
9919                   break;
9920                 case 'X':
9921                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9922                   break;
9923                 case 'Y':
9924                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9925                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9926                   break;
9927                 default:
9928                   internalError ();
9929                 }
9930
9931               lastregno = regno;
9932               continue;
9933
9934             case 'P':
9935               if (strncmp (s, "$pc", 3) == 0)
9936                 {
9937                   s += 3;
9938                   continue;
9939                 }
9940               break;
9941
9942             case '<':
9943             case '>':
9944             case '[':
9945             case ']':
9946             case '4':
9947             case '5':
9948             case 'H':
9949             case 'W':
9950             case 'D':
9951             case 'j':
9952             case '8':
9953             case 'V':
9954             case 'C':
9955             case 'U':
9956             case 'k':
9957             case 'K':
9958               if (s[0] == '%'
9959                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9960                 {
9961                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9962                      and generate the appropriate reloc.  If the text
9963                      inside %gprel is not a symbol name with an
9964                      optional offset, then we generate a normal reloc
9965                      and will probably fail later.  */
9966                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9967                   if (imm_expr.X_op == O_symbol)
9968                     {
9969                       mips16_ext = TRUE;
9970                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9971                       s = expr_end;
9972                       ip->use_extend = TRUE;
9973                       ip->extend = 0;
9974                       continue;
9975                     }
9976                 }
9977               else
9978                 {
9979                   /* Just pick up a normal expression.  */
9980                   my_getExpression (&imm_expr, s);
9981                 }
9982
9983               if (imm_expr.X_op == O_register)
9984                 {
9985                   /* What we thought was an expression turned out to
9986                      be a register.  */
9987
9988                   if (s[0] == '(' && args[1] == '(')
9989                     {
9990                       /* It looks like the expression was omitted
9991                          before a register indirection, which means
9992                          that the expression is implicitly zero.  We
9993                          still set up imm_expr, so that we handle
9994                          explicit extensions correctly.  */
9995                       imm_expr.X_op = O_constant;
9996                       imm_expr.X_add_number = 0;
9997                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9998                       continue;
9999                     }
10000
10001                   break;
10002                 }
10003
10004               /* We need to relax this instruction.  */
10005               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10006               s = expr_end;
10007               continue;
10008
10009             case 'p':
10010             case 'q':
10011             case 'A':
10012             case 'B':
10013             case 'E':
10014               /* We use offset_reloc rather than imm_reloc for the PC
10015                  relative operands.  This lets macros with both
10016                  immediate and address operands work correctly.  */
10017               my_getExpression (&offset_expr, s);
10018
10019               if (offset_expr.X_op == O_register)
10020                 break;
10021
10022               /* We need to relax this instruction.  */
10023               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10024               s = expr_end;
10025               continue;
10026
10027             case '6':           /* break code */
10028               my_getExpression (&imm_expr, s);
10029               check_absolute_expr (ip, &imm_expr);
10030               if ((unsigned long) imm_expr.X_add_number > 63)
10031                 {
10032                   as_warn (_("Invalid value for `%s' (%lu)"),
10033                            ip->insn_mo->name,
10034                            (unsigned long) imm_expr.X_add_number);
10035                   imm_expr.X_add_number &= 0x3f;
10036                 }
10037               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
10038               imm_expr.X_op = O_absent;
10039               s = expr_end;
10040               continue;
10041
10042             case 'a':           /* 26 bit address */
10043               my_getExpression (&offset_expr, s);
10044               s = expr_end;
10045               *offset_reloc = BFD_RELOC_MIPS16_JMP;
10046               ip->insn_opcode <<= 16;
10047               continue;
10048
10049             case 'l':           /* register list for entry macro */
10050             case 'L':           /* register list for exit macro */
10051               {
10052                 int mask;
10053
10054                 if (c == 'l')
10055                   mask = 0;
10056                 else
10057                   mask = 7 << 3;
10058                 while (*s != '\0')
10059                   {
10060                     int freg, reg1, reg2;
10061
10062                     while (*s == ' ' || *s == ',')
10063                       ++s;
10064                     if (*s != '$')
10065                       {
10066                         as_bad (_("can't parse register list"));
10067                         break;
10068                       }
10069                     ++s;
10070                     if (*s != 'f')
10071                       freg = 0;
10072                     else
10073                       {
10074                         freg = 1;
10075                         ++s;
10076                       }
10077                     reg1 = 0;
10078                     while (ISDIGIT (*s))
10079                       {
10080                         reg1 *= 10;
10081                         reg1 += *s - '0';
10082                         ++s;
10083                       }
10084                     if (*s == ' ')
10085                       ++s;
10086                     if (*s != '-')
10087                       reg2 = reg1;
10088                     else
10089                       {
10090                         ++s;
10091                         if (*s != '$')
10092                           break;
10093                         ++s;
10094                         if (freg)
10095                           {
10096                             if (*s == 'f')
10097                               ++s;
10098                             else
10099                               {
10100                                 as_bad (_("invalid register list"));
10101                                 break;
10102                               }
10103                           }
10104                         reg2 = 0;
10105                         while (ISDIGIT (*s))
10106                           {
10107                             reg2 *= 10;
10108                             reg2 += *s - '0';
10109                             ++s;
10110                           }
10111                       }
10112                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10113                       {
10114                         mask &= ~ (7 << 3);
10115                         mask |= 5 << 3;
10116                       }
10117                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10118                       {
10119                         mask &= ~ (7 << 3);
10120                         mask |= 6 << 3;
10121                       }
10122                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10123                       mask |= (reg2 - 3) << 3;
10124                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10125                       mask |= (reg2 - 15) << 1;
10126                     else if (reg1 == RA && reg2 == RA)
10127                       mask |= 1;
10128                     else
10129                       {
10130                         as_bad (_("invalid register list"));
10131                         break;
10132                       }
10133                   }
10134                 /* The mask is filled in in the opcode table for the
10135                    benefit of the disassembler.  We remove it before
10136                    applying the actual mask.  */
10137                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10138                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10139               }
10140             continue;
10141
10142             case 'e':           /* extend code */
10143               my_getExpression (&imm_expr, s);
10144               check_absolute_expr (ip, &imm_expr);
10145               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10146                 {
10147                   as_warn (_("Invalid value for `%s' (%lu)"),
10148                            ip->insn_mo->name,
10149                            (unsigned long) imm_expr.X_add_number);
10150                   imm_expr.X_add_number &= 0x7ff;
10151                 }
10152               ip->insn_opcode |= imm_expr.X_add_number;
10153               imm_expr.X_op = O_absent;
10154               s = expr_end;
10155               continue;
10156
10157             default:
10158               internalError ();
10159             }
10160           break;
10161         }
10162
10163       /* Args don't match.  */
10164       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10165           strcmp (insn->name, insn[1].name) == 0)
10166         {
10167           ++insn;
10168           s = argsstart;
10169           continue;
10170         }
10171
10172       insn_error = _("illegal operands");
10173
10174       return;
10175     }
10176 }
10177
10178 /* This structure holds information we know about a mips16 immediate
10179    argument type.  */
10180
10181 struct mips16_immed_operand
10182 {
10183   /* The type code used in the argument string in the opcode table.  */
10184   int type;
10185   /* The number of bits in the short form of the opcode.  */
10186   int nbits;
10187   /* The number of bits in the extended form of the opcode.  */
10188   int extbits;
10189   /* The amount by which the short form is shifted when it is used;
10190      for example, the sw instruction has a shift count of 2.  */
10191   int shift;
10192   /* The amount by which the short form is shifted when it is stored
10193      into the instruction code.  */
10194   int op_shift;
10195   /* Non-zero if the short form is unsigned.  */
10196   int unsp;
10197   /* Non-zero if the extended form is unsigned.  */
10198   int extu;
10199   /* Non-zero if the value is PC relative.  */
10200   int pcrel;
10201 };
10202
10203 /* The mips16 immediate operand types.  */
10204
10205 static const struct mips16_immed_operand mips16_immed_operands[] =
10206 {
10207   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10208   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10209   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10210   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10211   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10212   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10213   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10214   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10215   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10216   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10217   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10218   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10219   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10220   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10221   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10222   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10223   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10224   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10225   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10226   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10227   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10228 };
10229
10230 #define MIPS16_NUM_IMMED \
10231   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10232
10233 /* Handle a mips16 instruction with an immediate value.  This or's the
10234    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10235    whether an extended value is needed; if one is needed, it sets
10236    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10237    If SMALL is true, an unextended opcode was explicitly requested.
10238    If EXT is true, an extended opcode was explicitly requested.  If
10239    WARN is true, warn if EXT does not match reality.  */
10240
10241 static void
10242 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
10243               extend)
10244      char *file;
10245      unsigned int line;
10246      int type;
10247      offsetT val;
10248      bfd_boolean warn;
10249      bfd_boolean small;
10250      bfd_boolean ext;
10251      unsigned long *insn;
10252      bfd_boolean *use_extend;
10253      unsigned short *extend;
10254 {
10255   register const struct mips16_immed_operand *op;
10256   int mintiny, maxtiny;
10257   bfd_boolean needext;
10258
10259   op = mips16_immed_operands;
10260   while (op->type != type)
10261     {
10262       ++op;
10263       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10264     }
10265
10266   if (op->unsp)
10267     {
10268       if (type == '<' || type == '>' || type == '[' || type == ']')
10269         {
10270           mintiny = 1;
10271           maxtiny = 1 << op->nbits;
10272         }
10273       else
10274         {
10275           mintiny = 0;
10276           maxtiny = (1 << op->nbits) - 1;
10277         }
10278     }
10279   else
10280     {
10281       mintiny = - (1 << (op->nbits - 1));
10282       maxtiny = (1 << (op->nbits - 1)) - 1;
10283     }
10284
10285   /* Branch offsets have an implicit 0 in the lowest bit.  */
10286   if (type == 'p' || type == 'q')
10287     val /= 2;
10288
10289   if ((val & ((1 << op->shift) - 1)) != 0
10290       || val < (mintiny << op->shift)
10291       || val > (maxtiny << op->shift))
10292     needext = TRUE;
10293   else
10294     needext = FALSE;
10295
10296   if (warn && ext && ! needext)
10297     as_warn_where (file, line,
10298                    _("extended operand requested but not required"));
10299   if (small && needext)
10300     as_bad_where (file, line, _("invalid unextended operand value"));
10301
10302   if (small || (! ext && ! needext))
10303     {
10304       int insnval;
10305
10306       *use_extend = FALSE;
10307       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10308       insnval <<= op->op_shift;
10309       *insn |= insnval;
10310     }
10311   else
10312     {
10313       long minext, maxext;
10314       int extval;
10315
10316       if (op->extu)
10317         {
10318           minext = 0;
10319           maxext = (1 << op->extbits) - 1;
10320         }
10321       else
10322         {
10323           minext = - (1 << (op->extbits - 1));
10324           maxext = (1 << (op->extbits - 1)) - 1;
10325         }
10326       if (val < minext || val > maxext)
10327         as_bad_where (file, line,
10328                       _("operand value out of range for instruction"));
10329
10330       *use_extend = TRUE;
10331       if (op->extbits == 16)
10332         {
10333           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10334           val &= 0x1f;
10335         }
10336       else if (op->extbits == 15)
10337         {
10338           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10339           val &= 0xf;
10340         }
10341       else
10342         {
10343           extval = ((val & 0x1f) << 6) | (val & 0x20);
10344           val = 0;
10345         }
10346
10347       *extend = (unsigned short) extval;
10348       *insn |= val;
10349     }
10350 }
10351 \f
10352 static const struct percent_op_match
10353 {
10354   const char *str;
10355   bfd_reloc_code_real_type reloc;
10356 } percent_op[] =
10357 {
10358   {"%lo", BFD_RELOC_LO16},
10359 #ifdef OBJ_ELF
10360   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10361   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10362   {"%call16", BFD_RELOC_MIPS_CALL16},
10363   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10364   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10365   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10366   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10367   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10368   {"%got", BFD_RELOC_MIPS_GOT16},
10369   {"%gp_rel", BFD_RELOC_GPREL16},
10370   {"%half", BFD_RELOC_16},
10371   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10372   {"%higher", BFD_RELOC_MIPS_HIGHER},
10373   {"%neg", BFD_RELOC_MIPS_SUB},
10374 #endif
10375   {"%hi", BFD_RELOC_HI16_S}
10376 };
10377
10378
10379 /* Return true if *STR points to a relocation operator.  When returning true,
10380    move *STR over the operator and store its relocation code in *RELOC.
10381    Leave both *STR and *RELOC alone when returning false.  */
10382
10383 static bfd_boolean
10384 parse_relocation (str, reloc)
10385      char **str;
10386      bfd_reloc_code_real_type *reloc;
10387 {
10388   size_t i;
10389
10390   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10391     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10392       {
10393         *str += strlen (percent_op[i].str);
10394         *reloc = percent_op[i].reloc;
10395
10396         /* Check whether the output BFD supports this relocation.
10397            If not, issue an error and fall back on something safe.  */
10398         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10399           {
10400             as_bad ("relocation %s isn't supported by the current ABI",
10401                     percent_op[i].str);
10402             *reloc = BFD_RELOC_LO16;
10403           }
10404         return TRUE;
10405       }
10406   return FALSE;
10407 }
10408
10409
10410 /* Parse string STR as a 16-bit relocatable operand.  Store the
10411    expression in *EP and the relocations in the array starting
10412    at RELOC.  Return the number of relocation operators used.
10413
10414    On exit, EXPR_END points to the first character after the expression.
10415    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10416
10417 static size_t
10418 my_getSmallExpression (ep, reloc, str)
10419      expressionS *ep;
10420      bfd_reloc_code_real_type *reloc;
10421      char *str;
10422 {
10423   bfd_reloc_code_real_type reversed_reloc[3];
10424   size_t reloc_index, i;
10425   int crux_depth, str_depth;
10426   char *crux;
10427
10428   /* Search for the start of the main expression, recoding relocations
10429      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10430      of the main expression and with CRUX_DEPTH containing the number
10431      of open brackets at that point.  */
10432   reloc_index = -1;
10433   str_depth = 0;
10434   do
10435     {
10436       reloc_index++;
10437       crux = str;
10438       crux_depth = str_depth;
10439
10440       /* Skip over whitespace and brackets, keeping count of the number
10441          of brackets.  */
10442       while (*str == ' ' || *str == '\t' || *str == '(')
10443         if (*str++ == '(')
10444           str_depth++;
10445     }
10446   while (*str == '%'
10447          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10448          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10449
10450   my_getExpression (ep, crux);
10451   str = expr_end;
10452
10453   /* Match every open bracket.  */
10454   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10455     if (*str++ == ')')
10456       crux_depth--;
10457
10458   if (crux_depth > 0)
10459     as_bad ("unclosed '('");
10460
10461   expr_end = str;
10462
10463   if (reloc_index == 0)
10464     reloc[0] = BFD_RELOC_LO16;
10465   else
10466     {
10467       prev_reloc_op_frag = frag_now;
10468       for (i = 0; i < reloc_index; i++)
10469         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10470     }
10471
10472   return reloc_index;
10473 }
10474
10475 static void
10476 my_getExpression (ep, str)
10477      expressionS *ep;
10478      char *str;
10479 {
10480   char *save_in;
10481   valueT val;
10482
10483   save_in = input_line_pointer;
10484   input_line_pointer = str;
10485   expression (ep);
10486   expr_end = input_line_pointer;
10487   input_line_pointer = save_in;
10488
10489   /* If we are in mips16 mode, and this is an expression based on `.',
10490      then we bump the value of the symbol by 1 since that is how other
10491      text symbols are handled.  We don't bother to handle complex
10492      expressions, just `.' plus or minus a constant.  */
10493   if (mips_opts.mips16
10494       && ep->X_op == O_symbol
10495       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10496       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10497       && symbol_get_frag (ep->X_add_symbol) == frag_now
10498       && symbol_constant_p (ep->X_add_symbol)
10499       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10500     S_SET_VALUE (ep->X_add_symbol, val + 1);
10501 }
10502
10503 /* Turn a string in input_line_pointer into a floating point constant
10504    of type TYPE, and store the appropriate bytes in *LITP.  The number
10505    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10506    returned, or NULL on OK.  */
10507
10508 char *
10509 md_atof (type, litP, sizeP)
10510      int type;
10511      char *litP;
10512      int *sizeP;
10513 {
10514   int prec;
10515   LITTLENUM_TYPE words[4];
10516   char *t;
10517   int i;
10518
10519   switch (type)
10520     {
10521     case 'f':
10522       prec = 2;
10523       break;
10524
10525     case 'd':
10526       prec = 4;
10527       break;
10528
10529     default:
10530       *sizeP = 0;
10531       return _("bad call to md_atof");
10532     }
10533
10534   t = atof_ieee (input_line_pointer, type, words);
10535   if (t)
10536     input_line_pointer = t;
10537
10538   *sizeP = prec * 2;
10539
10540   if (! target_big_endian)
10541     {
10542       for (i = prec - 1; i >= 0; i--)
10543         {
10544           md_number_to_chars (litP, (valueT) words[i], 2);
10545           litP += 2;
10546         }
10547     }
10548   else
10549     {
10550       for (i = 0; i < prec; i++)
10551         {
10552           md_number_to_chars (litP, (valueT) words[i], 2);
10553           litP += 2;
10554         }
10555     }
10556
10557   return NULL;
10558 }
10559
10560 void
10561 md_number_to_chars (buf, val, n)
10562      char *buf;
10563      valueT val;
10564      int n;
10565 {
10566   if (target_big_endian)
10567     number_to_chars_bigendian (buf, val, n);
10568   else
10569     number_to_chars_littleendian (buf, val, n);
10570 }
10571 \f
10572 #ifdef OBJ_ELF
10573 static int support_64bit_objects(void)
10574 {
10575   const char **list, **l;
10576   int yes;
10577
10578   list = bfd_target_list ();
10579   for (l = list; *l != NULL; l++)
10580 #ifdef TE_TMIPS
10581     /* This is traditional mips */
10582     if (strcmp (*l, "elf64-tradbigmips") == 0
10583         || strcmp (*l, "elf64-tradlittlemips") == 0)
10584 #else
10585     if (strcmp (*l, "elf64-bigmips") == 0
10586         || strcmp (*l, "elf64-littlemips") == 0)
10587 #endif
10588       break;
10589   yes = (*l != NULL);
10590   free (list);
10591   return yes;
10592 }
10593 #endif /* OBJ_ELF */
10594
10595 const char *md_shortopts = "nO::g::G:";
10596
10597 struct option md_longopts[] =
10598 {
10599   /* Options which specify architecture.  */
10600 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
10601 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10602   {"march", required_argument, NULL, OPTION_MARCH},
10603 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10604   {"mtune", required_argument, NULL, OPTION_MTUNE},
10605 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10606   {"mips0", no_argument, NULL, OPTION_MIPS1},
10607   {"mips1", no_argument, NULL, OPTION_MIPS1},
10608 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10609   {"mips2", no_argument, NULL, OPTION_MIPS2},
10610 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10611   {"mips3", no_argument, NULL, OPTION_MIPS3},
10612 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10613   {"mips4", no_argument, NULL, OPTION_MIPS4},
10614 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10615   {"mips5", no_argument, NULL, OPTION_MIPS5},
10616 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10617   {"mips32", no_argument, NULL, OPTION_MIPS32},
10618 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10619   {"mips64", no_argument, NULL, OPTION_MIPS64},
10620 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10621   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10622
10623   /* Options which specify Application Specific Extensions (ASEs).  */
10624 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 10)
10625 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10626   {"mips16", no_argument, NULL, OPTION_MIPS16},
10627 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10628   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10629 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10630   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10631 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10632   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10633 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10634   {"mdmx", no_argument, NULL, OPTION_MDMX},
10635 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10636   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10637
10638   /* Old-style architecture options.  Don't add more of these.  */
10639 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10640 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10641   {"m4650", no_argument, NULL, OPTION_M4650},
10642 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10643   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10644 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10645   {"m4010", no_argument, NULL, OPTION_M4010},
10646 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10647   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10648 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10649   {"m4100", no_argument, NULL, OPTION_M4100},
10650 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10651   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10652 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10653   {"m3900", no_argument, NULL, OPTION_M3900},
10654 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10655   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10656
10657   /* Options which enable bug fixes.  */
10658 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10659 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10660   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10661 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10662   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10663   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10664 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10665 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10666   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10667   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10668
10669   /* Miscellaneous options.  */
10670 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10671 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10672   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10673 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10674   {"trap", no_argument, NULL, OPTION_TRAP},
10675   {"no-break", no_argument, NULL, OPTION_TRAP},
10676 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10677   {"break", no_argument, NULL, OPTION_BREAK},
10678   {"no-trap", no_argument, NULL, OPTION_BREAK},
10679 #define OPTION_EB (OPTION_MISC_BASE + 3)
10680   {"EB", no_argument, NULL, OPTION_EB},
10681 #define OPTION_EL (OPTION_MISC_BASE + 4)
10682   {"EL", no_argument, NULL, OPTION_EL},
10683 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10684   {"mfp32", no_argument, NULL, OPTION_FP32},
10685 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10686   {"mgp32", no_argument, NULL, OPTION_GP32},
10687 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10688   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10689 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10690   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10691 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10692   {"mfp64", no_argument, NULL, OPTION_FP64},
10693 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10694   {"mgp64", no_argument, NULL, OPTION_GP64},
10695 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10696 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10697   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10698   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10699
10700   /* ELF-specific options.  */
10701 #ifdef OBJ_ELF
10702 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 13)
10703 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10704   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10705   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10706 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10707   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10708 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10709   {"xgot",        no_argument, NULL, OPTION_XGOT},
10710 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10711   {"mabi", required_argument, NULL, OPTION_MABI},
10712 #define OPTION_32          (OPTION_ELF_BASE + 4)
10713   {"32",          no_argument, NULL, OPTION_32},
10714 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10715   {"n32",         no_argument, NULL, OPTION_N32},
10716 #define OPTION_64          (OPTION_ELF_BASE + 6)
10717   {"64",          no_argument, NULL, OPTION_64},
10718 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10719   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10720 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10721   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10722 #endif /* OBJ_ELF */
10723
10724   {NULL, no_argument, NULL, 0}
10725 };
10726 size_t md_longopts_size = sizeof (md_longopts);
10727
10728 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10729    NEW_VALUE.  Warn if another value was already specified.  Note:
10730    we have to defer parsing the -march and -mtune arguments in order
10731    to handle 'from-abi' correctly, since the ABI might be specified
10732    in a later argument.  */
10733
10734 static void
10735 mips_set_option_string (string_ptr, new_value)
10736      const char **string_ptr, *new_value;
10737 {
10738   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10739     as_warn (_("A different %s was already specified, is now %s"),
10740              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10741              new_value);
10742
10743   *string_ptr = new_value;
10744 }
10745
10746 int
10747 md_parse_option (c, arg)
10748      int c;
10749      char *arg;
10750 {
10751   switch (c)
10752     {
10753     case OPTION_CONSTRUCT_FLOATS:
10754       mips_disable_float_construction = 0;
10755       break;
10756
10757     case OPTION_NO_CONSTRUCT_FLOATS:
10758       mips_disable_float_construction = 1;
10759       break;
10760
10761     case OPTION_TRAP:
10762       mips_trap = 1;
10763       break;
10764
10765     case OPTION_BREAK:
10766       mips_trap = 0;
10767       break;
10768
10769     case OPTION_EB:
10770       target_big_endian = 1;
10771       break;
10772
10773     case OPTION_EL:
10774       target_big_endian = 0;
10775       break;
10776
10777     case 'n':
10778       warn_nops = 1;
10779       break;
10780
10781     case 'O':
10782       if (arg && arg[1] == '0')
10783         mips_optimize = 1;
10784       else
10785         mips_optimize = 2;
10786       break;
10787
10788     case 'g':
10789       if (arg == NULL)
10790         mips_debug = 2;
10791       else
10792         mips_debug = atoi (arg);
10793       /* When the MIPS assembler sees -g or -g2, it does not do
10794          optimizations which limit full symbolic debugging.  We take
10795          that to be equivalent to -O0.  */
10796       if (mips_debug == 2)
10797         mips_optimize = 1;
10798       break;
10799
10800     case OPTION_MIPS1:
10801       file_mips_isa = ISA_MIPS1;
10802       break;
10803
10804     case OPTION_MIPS2:
10805       file_mips_isa = ISA_MIPS2;
10806       break;
10807
10808     case OPTION_MIPS3:
10809       file_mips_isa = ISA_MIPS3;
10810       break;
10811
10812     case OPTION_MIPS4:
10813       file_mips_isa = ISA_MIPS4;
10814       break;
10815
10816     case OPTION_MIPS5:
10817       file_mips_isa = ISA_MIPS5;
10818       break;
10819
10820     case OPTION_MIPS32:
10821       file_mips_isa = ISA_MIPS32;
10822       break;
10823
10824     case OPTION_MIPS32R2:
10825       file_mips_isa = ISA_MIPS32R2;
10826       break;
10827
10828     case OPTION_MIPS64:
10829       file_mips_isa = ISA_MIPS64;
10830       break;
10831
10832     case OPTION_MTUNE:
10833       mips_set_option_string (&mips_tune_string, arg);
10834       break;
10835
10836     case OPTION_MARCH:
10837       mips_set_option_string (&mips_arch_string, arg);
10838       break;
10839
10840     case OPTION_M4650:
10841       mips_set_option_string (&mips_arch_string, "4650");
10842       mips_set_option_string (&mips_tune_string, "4650");
10843       break;
10844
10845     case OPTION_NO_M4650:
10846       break;
10847
10848     case OPTION_M4010:
10849       mips_set_option_string (&mips_arch_string, "4010");
10850       mips_set_option_string (&mips_tune_string, "4010");
10851       break;
10852
10853     case OPTION_NO_M4010:
10854       break;
10855
10856     case OPTION_M4100:
10857       mips_set_option_string (&mips_arch_string, "4100");
10858       mips_set_option_string (&mips_tune_string, "4100");
10859       break;
10860
10861     case OPTION_NO_M4100:
10862       break;
10863
10864     case OPTION_M3900:
10865       mips_set_option_string (&mips_arch_string, "3900");
10866       mips_set_option_string (&mips_tune_string, "3900");
10867       break;
10868
10869     case OPTION_NO_M3900:
10870       break;
10871
10872     case OPTION_MDMX:
10873       mips_opts.ase_mdmx = 1;
10874       break;
10875
10876     case OPTION_NO_MDMX:
10877       mips_opts.ase_mdmx = 0;
10878       break;
10879
10880     case OPTION_MIPS16:
10881       mips_opts.mips16 = 1;
10882       mips_no_prev_insn (FALSE);
10883       break;
10884
10885     case OPTION_NO_MIPS16:
10886       mips_opts.mips16 = 0;
10887       mips_no_prev_insn (FALSE);
10888       break;
10889
10890     case OPTION_MIPS3D:
10891       mips_opts.ase_mips3d = 1;
10892       break;
10893
10894     case OPTION_NO_MIPS3D:
10895       mips_opts.ase_mips3d = 0;
10896       break;
10897
10898     case OPTION_MEMBEDDED_PIC:
10899       mips_pic = EMBEDDED_PIC;
10900       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10901         {
10902           as_bad (_("-G may not be used with embedded PIC code"));
10903           return 0;
10904         }
10905       g_switch_value = 0x7fffffff;
10906       break;
10907
10908     case OPTION_FIX_VR4122:
10909       mips_fix_4122_bugs = 1;
10910       break;
10911
10912     case OPTION_NO_FIX_VR4122:
10913       mips_fix_4122_bugs = 0;
10914       break;
10915
10916     case OPTION_RELAX_BRANCH:
10917       mips_relax_branch = 1;
10918       break;
10919
10920     case OPTION_NO_RELAX_BRANCH:
10921       mips_relax_branch = 0;
10922       break;
10923
10924 #ifdef OBJ_ELF
10925       /* When generating ELF code, we permit -KPIC and -call_shared to
10926          select SVR4_PIC, and -non_shared to select no PIC.  This is
10927          intended to be compatible with Irix 5.  */
10928     case OPTION_CALL_SHARED:
10929       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10930         {
10931           as_bad (_("-call_shared is supported only for ELF format"));
10932           return 0;
10933         }
10934       mips_pic = SVR4_PIC;
10935       mips_abicalls = TRUE;
10936       if (g_switch_seen && g_switch_value != 0)
10937         {
10938           as_bad (_("-G may not be used with SVR4 PIC code"));
10939           return 0;
10940         }
10941       g_switch_value = 0;
10942       break;
10943
10944     case OPTION_NON_SHARED:
10945       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10946         {
10947           as_bad (_("-non_shared is supported only for ELF format"));
10948           return 0;
10949         }
10950       mips_pic = NO_PIC;
10951       mips_abicalls = FALSE;
10952       break;
10953
10954       /* The -xgot option tells the assembler to use 32 offsets when
10955          accessing the got in SVR4_PIC mode.  It is for Irix
10956          compatibility.  */
10957     case OPTION_XGOT:
10958       mips_big_got = 1;
10959       break;
10960 #endif /* OBJ_ELF */
10961
10962     case 'G':
10963       if (! USE_GLOBAL_POINTER_OPT)
10964         {
10965           as_bad (_("-G is not supported for this configuration"));
10966           return 0;
10967         }
10968       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10969         {
10970           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10971           return 0;
10972         }
10973       else
10974         g_switch_value = atoi (arg);
10975       g_switch_seen = 1;
10976       break;
10977
10978 #ifdef OBJ_ELF
10979       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10980          and -mabi=64.  */
10981     case OPTION_32:
10982       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10983         {
10984           as_bad (_("-32 is supported for ELF format only"));
10985           return 0;
10986         }
10987       mips_abi = O32_ABI;
10988       break;
10989
10990     case OPTION_N32:
10991       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10992         {
10993           as_bad (_("-n32 is supported for ELF format only"));
10994           return 0;
10995         }
10996       mips_abi = N32_ABI;
10997       break;
10998
10999     case OPTION_64:
11000       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11001         {
11002           as_bad (_("-64 is supported for ELF format only"));
11003           return 0;
11004         }
11005       mips_abi = N64_ABI;
11006       if (! support_64bit_objects())
11007         as_fatal (_("No compiled in support for 64 bit object file format"));
11008       break;
11009 #endif /* OBJ_ELF */
11010
11011     case OPTION_GP32:
11012       file_mips_gp32 = 1;
11013       break;
11014
11015     case OPTION_GP64:
11016       file_mips_gp32 = 0;
11017       break;
11018
11019     case OPTION_FP32:
11020       file_mips_fp32 = 1;
11021       break;
11022
11023     case OPTION_FP64:
11024       file_mips_fp32 = 0;
11025       break;
11026
11027 #ifdef OBJ_ELF
11028     case OPTION_MABI:
11029       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11030         {
11031           as_bad (_("-mabi is supported for ELF format only"));
11032           return 0;
11033         }
11034       if (strcmp (arg, "32") == 0)
11035         mips_abi = O32_ABI;
11036       else if (strcmp (arg, "o64") == 0)
11037         mips_abi = O64_ABI;
11038       else if (strcmp (arg, "n32") == 0)
11039         mips_abi = N32_ABI;
11040       else if (strcmp (arg, "64") == 0)
11041         {
11042           mips_abi = N64_ABI;
11043           if (! support_64bit_objects())
11044             as_fatal (_("No compiled in support for 64 bit object file "
11045                         "format"));
11046         }
11047       else if (strcmp (arg, "eabi") == 0)
11048         mips_abi = EABI_ABI;
11049       else
11050         {
11051           as_fatal (_("invalid abi -mabi=%s"), arg);
11052           return 0;
11053         }
11054       break;
11055 #endif /* OBJ_ELF */
11056
11057     case OPTION_M7000_HILO_FIX:
11058       mips_7000_hilo_fix = TRUE;
11059       break;
11060
11061     case OPTION_MNO_7000_HILO_FIX:
11062       mips_7000_hilo_fix = FALSE;
11063       break;
11064
11065 #ifdef OBJ_ELF
11066     case OPTION_MDEBUG:
11067       mips_flag_mdebug = TRUE;
11068       break;
11069
11070     case OPTION_NO_MDEBUG:
11071       mips_flag_mdebug = FALSE;
11072       break;
11073 #endif /* OBJ_ELF */
11074
11075     default:
11076       return 0;
11077     }
11078
11079   return 1;
11080 }
11081 \f
11082 /* Set up globals to generate code for the ISA or processor
11083    described by INFO.  */
11084
11085 static void
11086 mips_set_architecture (info)
11087      const struct mips_cpu_info *info;
11088 {
11089   if (info != 0)
11090     {
11091       file_mips_arch = info->cpu;
11092       mips_opts.arch = info->cpu;
11093       mips_opts.isa = info->isa;
11094     }
11095 }
11096
11097
11098 /* Likewise for tuning.  */
11099
11100 static void
11101 mips_set_tune (info)
11102      const struct mips_cpu_info *info;
11103 {
11104   if (info != 0)
11105     mips_tune = info->cpu;
11106 }
11107
11108
11109 void
11110 mips_after_parse_args ()
11111 {
11112   const struct mips_cpu_info *arch_info = 0;
11113   const struct mips_cpu_info *tune_info = 0;
11114
11115   /* GP relative stuff not working for PE */
11116   if (strncmp (TARGET_OS, "pe", 2) == 0
11117       && g_switch_value != 0)
11118     {
11119       if (g_switch_seen)
11120         as_bad (_("-G not supported in this configuration."));
11121       g_switch_value = 0;
11122     }
11123
11124   if (mips_abi == NO_ABI)
11125     mips_abi = MIPS_DEFAULT_ABI;
11126
11127   /* The following code determines the architecture and register size.
11128      Similar code was added to GCC 3.3 (see override_options() in
11129      config/mips/mips.c).  The GAS and GCC code should be kept in sync
11130      as much as possible.  */
11131
11132   if (mips_arch_string != 0)
11133     arch_info = mips_parse_cpu ("-march", mips_arch_string);
11134
11135   if (file_mips_isa != ISA_UNKNOWN)
11136     {
11137       /* Handle -mipsN.  At this point, file_mips_isa contains the
11138          ISA level specified by -mipsN, while arch_info->isa contains
11139          the -march selection (if any).  */
11140       if (arch_info != 0)
11141         {
11142           /* -march takes precedence over -mipsN, since it is more descriptive.
11143              There's no harm in specifying both as long as the ISA levels
11144              are the same.  */
11145           if (file_mips_isa != arch_info->isa)
11146             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11147                     mips_cpu_info_from_isa (file_mips_isa)->name,
11148                     mips_cpu_info_from_isa (arch_info->isa)->name);
11149         }
11150       else
11151         arch_info = mips_cpu_info_from_isa (file_mips_isa);
11152     }
11153
11154   if (arch_info == 0)
11155     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11156
11157   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11158     as_bad ("-march=%s is not compatible with the selected ABI",
11159             arch_info->name);
11160
11161   mips_set_architecture (arch_info);
11162
11163   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
11164   if (mips_tune_string != 0)
11165     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11166
11167   if (tune_info == 0)
11168     mips_set_tune (arch_info);
11169   else
11170     mips_set_tune (tune_info);
11171
11172   if (file_mips_gp32 >= 0)
11173     {
11174       /* The user specified the size of the integer registers.  Make sure
11175          it agrees with the ABI and ISA.  */
11176       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11177         as_bad (_("-mgp64 used with a 32-bit processor"));
11178       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11179         as_bad (_("-mgp32 used with a 64-bit ABI"));
11180       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11181         as_bad (_("-mgp64 used with a 32-bit ABI"));
11182     }
11183   else
11184     {
11185       /* Infer the integer register size from the ABI and processor.
11186          Restrict ourselves to 32-bit registers if that's all the
11187          processor has, or if the ABI cannot handle 64-bit registers.  */
11188       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11189                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11190     }
11191
11192   /* ??? GAS treats single-float processors as though they had 64-bit
11193      float registers (although it complains when double-precision
11194      instructions are used).  As things stand, saying they have 32-bit
11195      registers would lead to spurious "register must be even" messages.
11196      So here we assume float registers are always the same size as
11197      integer ones, unless the user says otherwise.  */
11198   if (file_mips_fp32 < 0)
11199     file_mips_fp32 = file_mips_gp32;
11200
11201   /* End of GCC-shared inference code.  */
11202
11203   /* ??? When do we want this flag to be set?   Who uses it?  */
11204   if (file_mips_gp32 == 1
11205       && mips_abi == NO_ABI
11206       && ISA_HAS_64BIT_REGS (mips_opts.isa))
11207     mips_32bitmode = 1;
11208
11209   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11210     as_bad (_("trap exception not supported at ISA 1"));
11211
11212   /* If the selected architecture includes support for ASEs, enable
11213      generation of code for them.  */
11214   if (mips_opts.mips16 == -1)
11215     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11216   if (mips_opts.ase_mips3d == -1)
11217     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
11218   if (mips_opts.ase_mdmx == -1)
11219     mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
11220
11221   file_mips_isa = mips_opts.isa;
11222   file_ase_mips16 = mips_opts.mips16;
11223   file_ase_mips3d = mips_opts.ase_mips3d;
11224   file_ase_mdmx = mips_opts.ase_mdmx;
11225   mips_opts.gp32 = file_mips_gp32;
11226   mips_opts.fp32 = file_mips_fp32;
11227
11228   if (mips_flag_mdebug < 0)
11229     {
11230 #ifdef OBJ_MAYBE_ECOFF
11231       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11232         mips_flag_mdebug = 1;
11233       else
11234 #endif /* OBJ_MAYBE_ECOFF */
11235         mips_flag_mdebug = 0;
11236     }
11237 }
11238 \f
11239 void
11240 mips_init_after_args ()
11241 {
11242   /* initialize opcodes */
11243   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11244   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11245 }
11246
11247 long
11248 md_pcrel_from (fixP)
11249      fixS *fixP;
11250 {
11251   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11252   switch (fixP->fx_r_type)
11253     {
11254     case BFD_RELOC_16_PCREL_S2:
11255     case BFD_RELOC_MIPS_JMP:
11256       /* Return the address of the delay slot.  */
11257       return addr + 4;
11258     default:
11259       return addr;
11260     }
11261 }
11262
11263 /* This is called before the symbol table is processed.  In order to
11264    work with gcc when using mips-tfile, we must keep all local labels.
11265    However, in other cases, we want to discard them.  If we were
11266    called with -g, but we didn't see any debugging information, it may
11267    mean that gcc is smuggling debugging information through to
11268    mips-tfile, in which case we must generate all local labels.  */
11269
11270 void
11271 mips_frob_file_before_adjust ()
11272 {
11273 #ifndef NO_ECOFF_DEBUGGING
11274   if (ECOFF_DEBUGGING
11275       && mips_debug != 0
11276       && ! ecoff_debugging_seen)
11277     flag_keep_locals = 1;
11278 #endif
11279 }
11280
11281 /* Sort any unmatched HI16_S relocs so that they immediately precede
11282    the corresponding LO reloc.  This is called before md_apply_fix3 and
11283    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11284    explicit use of the %hi modifier.  */
11285
11286 void
11287 mips_frob_file ()
11288 {
11289   struct mips_hi_fixup *l;
11290
11291   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11292     {
11293       segment_info_type *seginfo;
11294       int pass;
11295
11296       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11297
11298       /* If a GOT16 relocation turns out to be against a global symbol,
11299          there isn't supposed to be a matching LO.  */
11300       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11301           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11302         continue;
11303
11304       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11305       if (fixup_has_matching_lo_p (l->fixp))
11306         continue;
11307
11308       /* Look through the fixups for this segment for a matching %lo.
11309          When we find one, move the %hi just in front of it.  We do
11310          this in two passes.  In the first pass, we try to find a
11311          unique %lo.  In the second pass, we permit multiple %hi
11312          relocs for a single %lo (this is a GNU extension).  */
11313       seginfo = seg_info (l->seg);
11314       for (pass = 0; pass < 2; pass++)
11315         {
11316           fixS *f, *prev;
11317
11318           prev = NULL;
11319           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11320             {
11321               /* Check whether this is a %lo fixup which matches l->fixp.  */
11322               if (f->fx_r_type == BFD_RELOC_LO16
11323                   && f->fx_addsy == l->fixp->fx_addsy
11324                   && f->fx_offset == l->fixp->fx_offset
11325                   && (pass == 1
11326                       || prev == NULL
11327                       || !reloc_needs_lo_p (prev->fx_r_type)
11328                       || !fixup_has_matching_lo_p (prev)))
11329                 {
11330                   fixS **pf;
11331
11332                   /* Move l->fixp before f.  */
11333                   for (pf = &seginfo->fix_root;
11334                        *pf != l->fixp;
11335                        pf = &(*pf)->fx_next)
11336                     assert (*pf != NULL);
11337
11338                   *pf = l->fixp->fx_next;
11339
11340                   l->fixp->fx_next = f;
11341                   if (prev == NULL)
11342                     seginfo->fix_root = l->fixp;
11343                   else
11344                     prev->fx_next = l->fixp;
11345
11346                   break;
11347                 }
11348
11349               prev = f;
11350             }
11351
11352           if (f != NULL)
11353             break;
11354
11355 #if 0 /* GCC code motion plus incomplete dead code elimination
11356          can leave a %hi without a %lo.  */
11357           if (pass == 1)
11358             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11359                            _("Unmatched %%hi reloc"));
11360 #endif
11361         }
11362     }
11363 }
11364
11365 /* When generating embedded PIC code we need to use a special
11366    relocation to represent the difference of two symbols in the .text
11367    section (switch tables use a difference of this sort).  See
11368    include/coff/mips.h for details.  This macro checks whether this
11369    fixup requires the special reloc.  */
11370 #define SWITCH_TABLE(fixp) \
11371   ((fixp)->fx_r_type == BFD_RELOC_32 \
11372    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11373    && (fixp)->fx_addsy != NULL \
11374    && (fixp)->fx_subsy != NULL \
11375    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11376    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11377
11378 /* When generating embedded PIC code we must keep all PC relative
11379    relocations, in case the linker has to relax a call.  We also need
11380    to keep relocations for switch table entries.
11381
11382    We may have combined relocations without symbols in the N32/N64 ABI.
11383    We have to prevent gas from dropping them.  */
11384
11385 int
11386 mips_force_relocation (fixp)
11387      fixS *fixp;
11388 {
11389   if (generic_force_reloc (fixp))
11390     return 1;
11391
11392   if (HAVE_NEWABI
11393       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11394       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11395           || fixp->fx_r_type == BFD_RELOC_HI16_S
11396           || fixp->fx_r_type == BFD_RELOC_LO16))
11397     return 1;
11398
11399   return (mips_pic == EMBEDDED_PIC
11400           && (fixp->fx_pcrel
11401               || SWITCH_TABLE (fixp)
11402               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11403               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11404 }
11405
11406 /* This hook is called before a fix is simplified.  We don't really
11407    decide whether to skip a fix here.  Rather, we turn global symbols
11408    used as branch targets into local symbols, such that they undergo
11409    simplification.  We can only do this if the symbol is defined and
11410    it is in the same section as the branch.  If this doesn't hold, we
11411    emit a better error message than just saying the relocation is not
11412    valid for the selected object format.
11413
11414    FIXP is the fix-up we're going to try to simplify, SEG is the
11415    segment in which the fix up occurs.  The return value should be
11416    non-zero to indicate the fix-up is valid for further
11417    simplifications.  */
11418
11419 int
11420 mips_validate_fix (fixP, seg)
11421      struct fix *fixP;
11422      asection *seg;
11423 {
11424   /* There's a lot of discussion on whether it should be possible to
11425      use R_MIPS_PC16 to represent branch relocations.  The outcome
11426      seems to be that it can, but gas/bfd are very broken in creating
11427      RELA relocations for this, so for now we only accept branches to
11428      symbols in the same section.  Anything else is of dubious value,
11429      since there's no guarantee that at link time the symbol would be
11430      in range.  Even for branches to local symbols this is arguably
11431      wrong, since it we assume the symbol is not going to be
11432      overridden, which should be possible per ELF library semantics,
11433      but then, there isn't a dynamic relocation that could be used to
11434      this effect, and the target would likely be out of range as well.
11435
11436      Unfortunately, it seems that there is too much code out there
11437      that relies on branches to symbols that are global to be resolved
11438      as if they were local, like the IRIX tools do, so we do it as
11439      well, but with a warning so that people are reminded to fix their
11440      code.  If we ever get back to using R_MIPS_PC16 for branch
11441      targets, this entire block should go away (and probably the
11442      whole function).  */
11443
11444   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11445       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11446             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11447            && mips_pic != EMBEDDED_PIC)
11448           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11449       && fixP->fx_addsy)
11450     {
11451       if (! S_IS_DEFINED (fixP->fx_addsy))
11452         {
11453           as_bad_where (fixP->fx_file, fixP->fx_line,
11454                         _("Cannot branch to undefined symbol."));
11455           /* Avoid any further errors about this fixup.  */
11456           fixP->fx_done = 1;
11457         }
11458       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11459         {
11460           as_bad_where (fixP->fx_file, fixP->fx_line,
11461                         _("Cannot branch to symbol in another section."));
11462           fixP->fx_done = 1;
11463         }
11464       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11465         {
11466           symbolS *sym = fixP->fx_addsy;
11467
11468           as_warn_where (fixP->fx_file, fixP->fx_line,
11469                          _("Pretending global symbol used as branch target is local."));
11470
11471           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11472                                           S_GET_SEGMENT (sym),
11473                                           S_GET_VALUE (sym),
11474                                           symbol_get_frag (sym));
11475           copy_symbol_attributes (fixP->fx_addsy, sym);
11476           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11477           assert (symbol_resolved_p (sym));
11478           symbol_mark_resolved (fixP->fx_addsy);
11479         }
11480     }
11481
11482   return 1;
11483 }
11484
11485 #ifdef OBJ_ELF
11486 static int
11487 mips_need_elf_addend_fixup (fixP)
11488      fixS *fixP;
11489 {
11490   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11491     return 1;
11492   if (mips_pic == EMBEDDED_PIC
11493       && S_IS_WEAK (fixP->fx_addsy))
11494     return 1;
11495   if (mips_pic != EMBEDDED_PIC
11496       && (S_IS_WEAK (fixP->fx_addsy)
11497           || S_IS_EXTERNAL (fixP->fx_addsy))
11498       && !S_IS_COMMON (fixP->fx_addsy))
11499     return 1;
11500   if (((bfd_get_section_flags (stdoutput,
11501                                S_GET_SEGMENT (fixP->fx_addsy))
11502         & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11503       || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11504                    ".gnu.linkonce",
11505                    sizeof (".gnu.linkonce") - 1))
11506     return 1;
11507   return 0;
11508 }
11509 #endif
11510
11511 /* Apply a fixup to the object file.  */
11512
11513 void
11514 md_apply_fix3 (fixP, valP, seg)
11515      fixS *fixP;
11516      valueT *valP;
11517      segT seg ATTRIBUTE_UNUSED;
11518 {
11519   bfd_byte *buf;
11520   long insn;
11521   static int previous_fx_r_type = 0;
11522   reloc_howto_type *howto;
11523
11524   /* We ignore generic BFD relocations we don't know about.  */
11525   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11526   if (! howto)
11527     return;
11528
11529   assert (fixP->fx_size == 4
11530           || fixP->fx_r_type == BFD_RELOC_16
11531           || fixP->fx_r_type == BFD_RELOC_64
11532           || fixP->fx_r_type == BFD_RELOC_CTOR
11533           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11534           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11535           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11536
11537   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11538
11539   /* If we aren't adjusting this fixup to be against the section
11540      symbol, we need to adjust the value.  */
11541 #ifdef OBJ_ELF
11542   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11543     {
11544       if (mips_need_elf_addend_fixup (fixP)
11545           && howto->partial_inplace
11546           && fixP->fx_r_type != BFD_RELOC_GPREL16
11547           && fixP->fx_r_type != BFD_RELOC_GPREL32
11548           && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11549         {
11550           /* In this case, the bfd_install_relocation routine will
11551              incorrectly add the symbol value back in.  We just want
11552              the addend to appear in the object file.
11553
11554              The condition above used to include
11555              "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11556
11557              However, howto can't be trusted here, because we
11558              might change the reloc type in tc_gen_reloc.  We can
11559              check howto->partial_inplace because that conversion
11560              happens to preserve howto->partial_inplace; but it
11561              does not preserve howto->pcrel_offset.  I've just
11562              eliminated the check, because all MIPS PC-relative
11563              relocations are marked howto->pcrel_offset.
11564
11565              howto->pcrel_offset was originally added for
11566              R_MIPS_PC16, which is generated for code like
11567
11568                     globl g1 .text
11569                     .text
11570                     .space 20
11571              g1:
11572              x:
11573                     bal g1
11574            */
11575           *valP -= S_GET_VALUE (fixP->fx_addsy);
11576         }
11577
11578       /* This code was generated using trial and error and so is
11579          fragile and not trustworthy.  If you change it, you should
11580          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11581          they still pass.  */
11582       if (fixP->fx_pcrel)
11583         {
11584           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11585
11586           /* BFD's REL handling, for MIPS, is _very_ weird.
11587              This gives the right results, but it can't possibly
11588              be the way things are supposed to work.  */
11589           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11590         }
11591     }
11592 #endif
11593
11594   /* We are not done if this is a composite relocation to set up gp.  */
11595   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11596       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11597            || (fixP->fx_r_type == BFD_RELOC_64
11598                && (previous_fx_r_type == BFD_RELOC_GPREL32
11599                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11600            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11601                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11602                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11603     fixP->fx_done = 1;
11604   previous_fx_r_type = fixP->fx_r_type;
11605
11606   switch (fixP->fx_r_type)
11607     {
11608     case BFD_RELOC_MIPS_JMP:
11609     case BFD_RELOC_MIPS_SHIFT5:
11610     case BFD_RELOC_MIPS_SHIFT6:
11611     case BFD_RELOC_MIPS_GOT_DISP:
11612     case BFD_RELOC_MIPS_GOT_PAGE:
11613     case BFD_RELOC_MIPS_GOT_OFST:
11614     case BFD_RELOC_MIPS_SUB:
11615     case BFD_RELOC_MIPS_INSERT_A:
11616     case BFD_RELOC_MIPS_INSERT_B:
11617     case BFD_RELOC_MIPS_DELETE:
11618     case BFD_RELOC_MIPS_HIGHEST:
11619     case BFD_RELOC_MIPS_HIGHER:
11620     case BFD_RELOC_MIPS_SCN_DISP:
11621     case BFD_RELOC_MIPS_REL16:
11622     case BFD_RELOC_MIPS_RELGOT:
11623     case BFD_RELOC_MIPS_JALR:
11624     case BFD_RELOC_HI16:
11625     case BFD_RELOC_HI16_S:
11626     case BFD_RELOC_GPREL16:
11627     case BFD_RELOC_MIPS_LITERAL:
11628     case BFD_RELOC_MIPS_CALL16:
11629     case BFD_RELOC_MIPS_GOT16:
11630     case BFD_RELOC_GPREL32:
11631     case BFD_RELOC_MIPS_GOT_HI16:
11632     case BFD_RELOC_MIPS_GOT_LO16:
11633     case BFD_RELOC_MIPS_CALL_HI16:
11634     case BFD_RELOC_MIPS_CALL_LO16:
11635     case BFD_RELOC_MIPS16_GPREL:
11636       if (fixP->fx_pcrel)
11637         as_bad_where (fixP->fx_file, fixP->fx_line,
11638                       _("Invalid PC relative reloc"));
11639       /* Nothing needed to do. The value comes from the reloc entry */
11640       break;
11641
11642     case BFD_RELOC_MIPS16_JMP:
11643       /* We currently always generate a reloc against a symbol, which
11644          means that we don't want an addend even if the symbol is
11645          defined.  */
11646       *valP = 0;
11647       break;
11648
11649     case BFD_RELOC_PCREL_HI16_S:
11650       /* The addend for this is tricky if it is internal, so we just
11651          do everything here rather than in bfd_install_relocation.  */
11652       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11653         break;
11654       if (fixP->fx_addsy
11655           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11656         {
11657           /* For an external symbol adjust by the address to make it
11658              pcrel_offset.  We use the address of the RELLO reloc
11659              which follows this one.  */
11660           *valP += (fixP->fx_next->fx_frag->fr_address
11661                     + fixP->fx_next->fx_where);
11662         }
11663       *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11664       if (target_big_endian)
11665         buf += 2;
11666       md_number_to_chars ((char *) buf, *valP, 2);
11667       break;
11668
11669     case BFD_RELOC_PCREL_LO16:
11670       /* The addend for this is tricky if it is internal, so we just
11671          do everything here rather than in bfd_install_relocation.  */
11672       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11673         break;
11674       if (fixP->fx_addsy
11675           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11676         *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11677       if (target_big_endian)
11678         buf += 2;
11679       md_number_to_chars ((char *) buf, *valP, 2);
11680       break;
11681
11682     case BFD_RELOC_64:
11683       /* This is handled like BFD_RELOC_32, but we output a sign
11684          extended value if we are only 32 bits.  */
11685       if (fixP->fx_done
11686           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11687         {
11688           if (8 <= sizeof (valueT))
11689             md_number_to_chars (buf, *valP, 8);
11690           else
11691             {
11692               valueT hiv;
11693
11694               if ((*valP & 0x80000000) != 0)
11695                 hiv = 0xffffffff;
11696               else
11697                 hiv = 0;
11698               md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11699                                   *valP, 4);
11700               md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11701                                   hiv, 4);
11702             }
11703         }
11704       break;
11705
11706     case BFD_RELOC_RVA:
11707     case BFD_RELOC_32:
11708       /* If we are deleting this reloc entry, we must fill in the
11709          value now.  This can happen if we have a .word which is not
11710          resolved when it appears but is later defined.  We also need
11711          to fill in the value if this is an embedded PIC switch table
11712          entry.  */
11713       if (fixP->fx_done
11714           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11715         md_number_to_chars (buf, *valP, 4);
11716       break;
11717
11718     case BFD_RELOC_16:
11719       /* If we are deleting this reloc entry, we must fill in the
11720          value now.  */
11721       assert (fixP->fx_size == 2);
11722       if (fixP->fx_done)
11723         md_number_to_chars (buf, *valP, 2);
11724       break;
11725
11726     case BFD_RELOC_LO16:
11727       /* When handling an embedded PIC switch statement, we can wind
11728          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11729       if (fixP->fx_done)
11730         {
11731           if (*valP + 0x8000 > 0xffff)
11732             as_bad_where (fixP->fx_file, fixP->fx_line,
11733                           _("relocation overflow"));
11734           if (target_big_endian)
11735             buf += 2;
11736           md_number_to_chars ((char *) buf, *valP, 2);
11737         }
11738       break;
11739
11740     case BFD_RELOC_16_PCREL_S2:
11741       if ((*valP & 0x3) != 0)
11742         as_bad_where (fixP->fx_file, fixP->fx_line,
11743                       _("Branch to odd address (%lx)"), (long) *valP);
11744
11745       /*
11746        * We need to save the bits in the instruction since fixup_segment()
11747        * might be deleting the relocation entry (i.e., a branch within
11748        * the current segment).
11749        */
11750       if (! fixP->fx_done)
11751         break;
11752
11753       /* update old instruction data */
11754       if (target_big_endian)
11755         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11756       else
11757         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11758
11759       if (*valP + 0x20000 <= 0x3ffff)
11760         {
11761           insn |= (*valP >> 2) & 0xffff;
11762           md_number_to_chars ((char *) buf, (valueT) insn, 4);
11763         }
11764       else if (mips_pic == NO_PIC
11765                && fixP->fx_done
11766                && fixP->fx_frag->fr_address >= text_section->vma
11767                && (fixP->fx_frag->fr_address
11768                    < text_section->vma + text_section->_raw_size)
11769                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
11770                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
11771                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11772         {
11773           /* The branch offset is too large.  If this is an
11774              unconditional branch, and we are not generating PIC code,
11775              we can convert it to an absolute jump instruction.  */
11776           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
11777             insn = 0x0c000000;  /* jal */
11778           else
11779             insn = 0x08000000;  /* j */
11780           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11781           fixP->fx_done = 0;
11782           fixP->fx_addsy = section_symbol (text_section);
11783           *valP += md_pcrel_from (fixP);
11784           md_number_to_chars ((char *) buf, (valueT) insn, 4);
11785         }
11786       else
11787         {
11788           /* If we got here, we have branch-relaxation disabled,
11789              and there's nothing we can do to fix this instruction
11790              without turning it into a longer sequence.  */
11791           as_bad_where (fixP->fx_file, fixP->fx_line,
11792                         _("Branch out of range"));
11793         }
11794       break;
11795
11796     case BFD_RELOC_VTABLE_INHERIT:
11797       fixP->fx_done = 0;
11798       if (fixP->fx_addsy
11799           && !S_IS_DEFINED (fixP->fx_addsy)
11800           && !S_IS_WEAK (fixP->fx_addsy))
11801         S_SET_WEAK (fixP->fx_addsy);
11802       break;
11803
11804     case BFD_RELOC_VTABLE_ENTRY:
11805       fixP->fx_done = 0;
11806       break;
11807
11808     default:
11809       internalError ();
11810     }
11811
11812   /* Remember value for tc_gen_reloc.  */
11813   fixP->fx_addnumber = *valP;
11814 }
11815
11816 #if 0
11817 void
11818 printInsn (oc)
11819      unsigned long oc;
11820 {
11821   const struct mips_opcode *p;
11822   int treg, sreg, dreg, shamt;
11823   short imm;
11824   const char *args;
11825   int i;
11826
11827   for (i = 0; i < NUMOPCODES; ++i)
11828     {
11829       p = &mips_opcodes[i];
11830       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11831         {
11832           printf ("%08lx %s\t", oc, p->name);
11833           treg = (oc >> 16) & 0x1f;
11834           sreg = (oc >> 21) & 0x1f;
11835           dreg = (oc >> 11) & 0x1f;
11836           shamt = (oc >> 6) & 0x1f;
11837           imm = oc;
11838           for (args = p->args;; ++args)
11839             {
11840               switch (*args)
11841                 {
11842                 case '\0':
11843                   printf ("\n");
11844                   break;
11845
11846                 case ',':
11847                 case '(':
11848                 case ')':
11849                   printf ("%c", *args);
11850                   continue;
11851
11852                 case 'r':
11853                   assert (treg == sreg);
11854                   printf ("$%d,$%d", treg, sreg);
11855                   continue;
11856
11857                 case 'd':
11858                 case 'G':
11859                   printf ("$%d", dreg);
11860                   continue;
11861
11862                 case 't':
11863                 case 'E':
11864                   printf ("$%d", treg);
11865                   continue;
11866
11867                 case 'k':
11868                   printf ("0x%x", treg);
11869                   continue;
11870
11871                 case 'b':
11872                 case 's':
11873                   printf ("$%d", sreg);
11874                   continue;
11875
11876                 case 'a':
11877                   printf ("0x%08lx", oc & 0x1ffffff);
11878                   continue;
11879
11880                 case 'i':
11881                 case 'j':
11882                 case 'o':
11883                 case 'u':
11884                   printf ("%d", imm);
11885                   continue;
11886
11887                 case '<':
11888                 case '>':
11889                   printf ("$%d", shamt);
11890                   continue;
11891
11892                 default:
11893                   internalError ();
11894                 }
11895               break;
11896             }
11897           return;
11898         }
11899     }
11900   printf (_("%08lx  UNDEFINED\n"), oc);
11901 }
11902 #endif
11903
11904 static symbolS *
11905 get_symbol ()
11906 {
11907   int c;
11908   char *name;
11909   symbolS *p;
11910
11911   name = input_line_pointer;
11912   c = get_symbol_end ();
11913   p = (symbolS *) symbol_find_or_make (name);
11914   *input_line_pointer = c;
11915   return p;
11916 }
11917
11918 /* Align the current frag to a given power of two.  The MIPS assembler
11919    also automatically adjusts any preceding label.  */
11920
11921 static void
11922 mips_align (to, fill, label)
11923      int to;
11924      int fill;
11925      symbolS *label;
11926 {
11927   mips_emit_delays (FALSE);
11928   frag_align (to, fill, 0);
11929   record_alignment (now_seg, to);
11930   if (label != NULL)
11931     {
11932       assert (S_GET_SEGMENT (label) == now_seg);
11933       symbol_set_frag (label, frag_now);
11934       S_SET_VALUE (label, (valueT) frag_now_fix ());
11935     }
11936 }
11937
11938 /* Align to a given power of two.  .align 0 turns off the automatic
11939    alignment used by the data creating pseudo-ops.  */
11940
11941 static void
11942 s_align (x)
11943      int x ATTRIBUTE_UNUSED;
11944 {
11945   register int temp;
11946   register long temp_fill;
11947   long max_alignment = 15;
11948
11949   /*
11950
11951     o  Note that the assembler pulls down any immediately preceeding label
11952        to the aligned address.
11953     o  It's not documented but auto alignment is reinstated by
11954        a .align pseudo instruction.
11955     o  Note also that after auto alignment is turned off the mips assembler
11956        issues an error on attempt to assemble an improperly aligned data item.
11957        We don't.
11958
11959     */
11960
11961   temp = get_absolute_expression ();
11962   if (temp > max_alignment)
11963     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11964   else if (temp < 0)
11965     {
11966       as_warn (_("Alignment negative: 0 assumed."));
11967       temp = 0;
11968     }
11969   if (*input_line_pointer == ',')
11970     {
11971       ++input_line_pointer;
11972       temp_fill = get_absolute_expression ();
11973     }
11974   else
11975     temp_fill = 0;
11976   if (temp)
11977     {
11978       auto_align = 1;
11979       mips_align (temp, (int) temp_fill,
11980                   insn_labels != NULL ? insn_labels->label : NULL);
11981     }
11982   else
11983     {
11984       auto_align = 0;
11985     }
11986
11987   demand_empty_rest_of_line ();
11988 }
11989
11990 void
11991 mips_flush_pending_output ()
11992 {
11993   mips_emit_delays (FALSE);
11994   mips_clear_insn_labels ();
11995 }
11996
11997 static void
11998 s_change_sec (sec)
11999      int sec;
12000 {
12001   segT seg;
12002
12003   /* When generating embedded PIC code, we only use the .text, .lit8,
12004      .sdata and .sbss sections.  We change the .data and .rdata
12005      pseudo-ops to use .sdata.  */
12006   if (mips_pic == EMBEDDED_PIC
12007       && (sec == 'd' || sec == 'r'))
12008     sec = 's';
12009
12010 #ifdef OBJ_ELF
12011   /* The ELF backend needs to know that we are changing sections, so
12012      that .previous works correctly.  We could do something like check
12013      for an obj_section_change_hook macro, but that might be confusing
12014      as it would not be appropriate to use it in the section changing
12015      functions in read.c, since obj-elf.c intercepts those.  FIXME:
12016      This should be cleaner, somehow.  */
12017   obj_elf_section_change_hook ();
12018 #endif
12019
12020   mips_emit_delays (FALSE);
12021   switch (sec)
12022     {
12023     case 't':
12024       s_text (0);
12025       break;
12026     case 'd':
12027       s_data (0);
12028       break;
12029     case 'b':
12030       subseg_set (bss_section, (subsegT) get_absolute_expression ());
12031       demand_empty_rest_of_line ();
12032       break;
12033
12034     case 'r':
12035       if (USE_GLOBAL_POINTER_OPT)
12036         {
12037           seg = subseg_new (RDATA_SECTION_NAME,
12038                             (subsegT) get_absolute_expression ());
12039           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12040             {
12041               bfd_set_section_flags (stdoutput, seg,
12042                                      (SEC_ALLOC
12043                                       | SEC_LOAD
12044                                       | SEC_READONLY
12045                                       | SEC_RELOC
12046                                       | SEC_DATA));
12047               if (strcmp (TARGET_OS, "elf") != 0)
12048                 record_alignment (seg, 4);
12049             }
12050           demand_empty_rest_of_line ();
12051         }
12052       else
12053         {
12054           as_bad (_("No read only data section in this object file format"));
12055           demand_empty_rest_of_line ();
12056           return;
12057         }
12058       break;
12059
12060     case 's':
12061       if (USE_GLOBAL_POINTER_OPT)
12062         {
12063           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12064           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12065             {
12066               bfd_set_section_flags (stdoutput, seg,
12067                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
12068                                      | SEC_DATA);
12069               if (strcmp (TARGET_OS, "elf") != 0)
12070                 record_alignment (seg, 4);
12071             }
12072           demand_empty_rest_of_line ();
12073           break;
12074         }
12075       else
12076         {
12077           as_bad (_("Global pointers not supported; recompile -G 0"));
12078           demand_empty_rest_of_line ();
12079           return;
12080         }
12081     }
12082
12083   auto_align = 1;
12084 }
12085
12086 void
12087 s_change_section (ignore)
12088      int ignore ATTRIBUTE_UNUSED;
12089 {
12090 #ifdef OBJ_ELF
12091   char *section_name;
12092   char c;
12093   char next_c = 0;
12094   int section_type;
12095   int section_flag;
12096   int section_entry_size;
12097   int section_alignment;
12098
12099   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12100     return;
12101
12102   section_name = input_line_pointer;
12103   c = get_symbol_end ();
12104   if (c)
12105     next_c = *(input_line_pointer + 1);
12106
12107   /* Do we have .section Name<,"flags">?  */
12108   if (c != ',' || (c == ',' && next_c == '"'))
12109     {
12110       /* just after name is now '\0'.  */
12111       *input_line_pointer = c;
12112       input_line_pointer = section_name;
12113       obj_elf_section (ignore);
12114       return;
12115     }
12116   input_line_pointer++;
12117
12118   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
12119   if (c == ',')
12120     section_type = get_absolute_expression ();
12121   else
12122     section_type = 0;
12123   if (*input_line_pointer++ == ',')
12124     section_flag = get_absolute_expression ();
12125   else
12126     section_flag = 0;
12127   if (*input_line_pointer++ == ',')
12128     section_entry_size = get_absolute_expression ();
12129   else
12130     section_entry_size = 0;
12131   if (*input_line_pointer++ == ',')
12132     section_alignment = get_absolute_expression ();
12133   else
12134     section_alignment = 0;
12135
12136   section_name = xstrdup (section_name);
12137
12138   obj_elf_change_section (section_name, section_type, section_flag,
12139                           section_entry_size, 0, 0, 0);
12140
12141   if (now_seg->name != section_name)
12142     free (section_name);
12143 #endif /* OBJ_ELF */
12144 }
12145
12146 void
12147 mips_enable_auto_align ()
12148 {
12149   auto_align = 1;
12150 }
12151
12152 static void
12153 s_cons (log_size)
12154      int log_size;
12155 {
12156   symbolS *label;
12157
12158   label = insn_labels != NULL ? insn_labels->label : NULL;
12159   mips_emit_delays (FALSE);
12160   if (log_size > 0 && auto_align)
12161     mips_align (log_size, 0, label);
12162   mips_clear_insn_labels ();
12163   cons (1 << log_size);
12164 }
12165
12166 static void
12167 s_float_cons (type)
12168      int type;
12169 {
12170   symbolS *label;
12171
12172   label = insn_labels != NULL ? insn_labels->label : NULL;
12173
12174   mips_emit_delays (FALSE);
12175
12176   if (auto_align)
12177     {
12178       if (type == 'd')
12179         mips_align (3, 0, label);
12180       else
12181         mips_align (2, 0, label);
12182     }
12183
12184   mips_clear_insn_labels ();
12185
12186   float_cons (type);
12187 }
12188
12189 /* Handle .globl.  We need to override it because on Irix 5 you are
12190    permitted to say
12191        .globl foo .text
12192    where foo is an undefined symbol, to mean that foo should be
12193    considered to be the address of a function.  */
12194
12195 static void
12196 s_mips_globl (x)
12197      int x ATTRIBUTE_UNUSED;
12198 {
12199   char *name;
12200   int c;
12201   symbolS *symbolP;
12202   flagword flag;
12203
12204   name = input_line_pointer;
12205   c = get_symbol_end ();
12206   symbolP = symbol_find_or_make (name);
12207   *input_line_pointer = c;
12208   SKIP_WHITESPACE ();
12209
12210   /* On Irix 5, every global symbol that is not explicitly labelled as
12211      being a function is apparently labelled as being an object.  */
12212   flag = BSF_OBJECT;
12213
12214   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12215     {
12216       char *secname;
12217       asection *sec;
12218
12219       secname = input_line_pointer;
12220       c = get_symbol_end ();
12221       sec = bfd_get_section_by_name (stdoutput, secname);
12222       if (sec == NULL)
12223         as_bad (_("%s: no such section"), secname);
12224       *input_line_pointer = c;
12225
12226       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12227         flag = BSF_FUNCTION;
12228     }
12229
12230   symbol_get_bfdsym (symbolP)->flags |= flag;
12231
12232   S_SET_EXTERNAL (symbolP);
12233   demand_empty_rest_of_line ();
12234 }
12235
12236 static void
12237 s_option (x)
12238      int x ATTRIBUTE_UNUSED;
12239 {
12240   char *opt;
12241   char c;
12242
12243   opt = input_line_pointer;
12244   c = get_symbol_end ();
12245
12246   if (*opt == 'O')
12247     {
12248       /* FIXME: What does this mean?  */
12249     }
12250   else if (strncmp (opt, "pic", 3) == 0)
12251     {
12252       int i;
12253
12254       i = atoi (opt + 3);
12255       if (i == 0)
12256         mips_pic = NO_PIC;
12257       else if (i == 2)
12258         {
12259         mips_pic = SVR4_PIC;
12260           mips_abicalls = TRUE;
12261         }
12262       else
12263         as_bad (_(".option pic%d not supported"), i);
12264
12265       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12266         {
12267           if (g_switch_seen && g_switch_value != 0)
12268             as_warn (_("-G may not be used with SVR4 PIC code"));
12269           g_switch_value = 0;
12270           bfd_set_gp_size (stdoutput, 0);
12271         }
12272     }
12273   else
12274     as_warn (_("Unrecognized option \"%s\""), opt);
12275
12276   *input_line_pointer = c;
12277   demand_empty_rest_of_line ();
12278 }
12279
12280 /* This structure is used to hold a stack of .set values.  */
12281
12282 struct mips_option_stack
12283 {
12284   struct mips_option_stack *next;
12285   struct mips_set_options options;
12286 };
12287
12288 static struct mips_option_stack *mips_opts_stack;
12289
12290 /* Handle the .set pseudo-op.  */
12291
12292 static void
12293 s_mipsset (x)
12294      int x ATTRIBUTE_UNUSED;
12295 {
12296   char *name = input_line_pointer, ch;
12297
12298   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12299     ++input_line_pointer;
12300   ch = *input_line_pointer;
12301   *input_line_pointer = '\0';
12302
12303   if (strcmp (name, "reorder") == 0)
12304     {
12305       if (mips_opts.noreorder && prev_nop_frag != NULL)
12306         {
12307           /* If we still have pending nops, we can discard them.  The
12308              usual nop handling will insert any that are still
12309              needed.  */
12310           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12311                                     * (mips_opts.mips16 ? 2 : 4));
12312           prev_nop_frag = NULL;
12313         }
12314       mips_opts.noreorder = 0;
12315     }
12316   else if (strcmp (name, "noreorder") == 0)
12317     {
12318       mips_emit_delays (TRUE);
12319       mips_opts.noreorder = 1;
12320       mips_any_noreorder = 1;
12321     }
12322   else if (strcmp (name, "at") == 0)
12323     {
12324       mips_opts.noat = 0;
12325     }
12326   else if (strcmp (name, "noat") == 0)
12327     {
12328       mips_opts.noat = 1;
12329     }
12330   else if (strcmp (name, "macro") == 0)
12331     {
12332       mips_opts.warn_about_macros = 0;
12333     }
12334   else if (strcmp (name, "nomacro") == 0)
12335     {
12336       if (mips_opts.noreorder == 0)
12337         as_bad (_("`noreorder' must be set before `nomacro'"));
12338       mips_opts.warn_about_macros = 1;
12339     }
12340   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12341     {
12342       mips_opts.nomove = 0;
12343     }
12344   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12345     {
12346       mips_opts.nomove = 1;
12347     }
12348   else if (strcmp (name, "bopt") == 0)
12349     {
12350       mips_opts.nobopt = 0;
12351     }
12352   else if (strcmp (name, "nobopt") == 0)
12353     {
12354       mips_opts.nobopt = 1;
12355     }
12356   else if (strcmp (name, "mips16") == 0
12357            || strcmp (name, "MIPS-16") == 0)
12358     mips_opts.mips16 = 1;
12359   else if (strcmp (name, "nomips16") == 0
12360            || strcmp (name, "noMIPS-16") == 0)
12361     mips_opts.mips16 = 0;
12362   else if (strcmp (name, "mips3d") == 0)
12363     mips_opts.ase_mips3d = 1;
12364   else if (strcmp (name, "nomips3d") == 0)
12365     mips_opts.ase_mips3d = 0;
12366   else if (strcmp (name, "mdmx") == 0)
12367     mips_opts.ase_mdmx = 1;
12368   else if (strcmp (name, "nomdmx") == 0)
12369     mips_opts.ase_mdmx = 0;
12370   else if (strncmp (name, "mips", 4) == 0)
12371     {
12372       int reset = 0;
12373
12374       /* Permit the user to change the ISA on the fly.  Needless to
12375          say, misuse can cause serious problems.  */
12376       if (strcmp (name, "mips0") == 0)
12377         {
12378           reset = 1;
12379           mips_opts.isa = file_mips_isa;
12380         }
12381       else if (strcmp (name, "mips1") == 0)
12382         mips_opts.isa = ISA_MIPS1;
12383       else if (strcmp (name, "mips2") == 0)
12384         mips_opts.isa = ISA_MIPS2;
12385       else if (strcmp (name, "mips3") == 0)
12386         mips_opts.isa = ISA_MIPS3;
12387       else if (strcmp (name, "mips4") == 0)
12388         mips_opts.isa = ISA_MIPS4;
12389       else if (strcmp (name, "mips5") == 0)
12390         mips_opts.isa = ISA_MIPS5;
12391       else if (strcmp (name, "mips32") == 0)
12392         mips_opts.isa = ISA_MIPS32;
12393       else if (strcmp (name, "mips32r2") == 0)
12394         mips_opts.isa = ISA_MIPS32R2;
12395       else if (strcmp (name, "mips64") == 0)
12396         mips_opts.isa = ISA_MIPS64;
12397       else
12398         as_bad (_("unknown ISA level %s"), name + 4);
12399
12400       switch (mips_opts.isa)
12401         {
12402         case  0:
12403           break;
12404         case ISA_MIPS1:
12405         case ISA_MIPS2:
12406         case ISA_MIPS32:
12407         case ISA_MIPS32R2:
12408           mips_opts.gp32 = 1;
12409           mips_opts.fp32 = 1;
12410           break;
12411         case ISA_MIPS3:
12412         case ISA_MIPS4:
12413         case ISA_MIPS5:
12414         case ISA_MIPS64:
12415           mips_opts.gp32 = 0;
12416           mips_opts.fp32 = 0;
12417           break;
12418         default:
12419           as_bad (_("unknown ISA level %s"), name + 4);
12420           break;
12421         }
12422       if (reset)
12423         {
12424           mips_opts.gp32 = file_mips_gp32;
12425           mips_opts.fp32 = file_mips_fp32;
12426         }
12427     }
12428   else if (strcmp (name, "autoextend") == 0)
12429     mips_opts.noautoextend = 0;
12430   else if (strcmp (name, "noautoextend") == 0)
12431     mips_opts.noautoextend = 1;
12432   else if (strcmp (name, "push") == 0)
12433     {
12434       struct mips_option_stack *s;
12435
12436       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12437       s->next = mips_opts_stack;
12438       s->options = mips_opts;
12439       mips_opts_stack = s;
12440     }
12441   else if (strcmp (name, "pop") == 0)
12442     {
12443       struct mips_option_stack *s;
12444
12445       s = mips_opts_stack;
12446       if (s == NULL)
12447         as_bad (_(".set pop with no .set push"));
12448       else
12449         {
12450           /* If we're changing the reorder mode we need to handle
12451              delay slots correctly.  */
12452           if (s->options.noreorder && ! mips_opts.noreorder)
12453             mips_emit_delays (TRUE);
12454           else if (! s->options.noreorder && mips_opts.noreorder)
12455             {
12456               if (prev_nop_frag != NULL)
12457                 {
12458                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12459                                             * (mips_opts.mips16 ? 2 : 4));
12460                   prev_nop_frag = NULL;
12461                 }
12462             }
12463
12464           mips_opts = s->options;
12465           mips_opts_stack = s->next;
12466           free (s);
12467         }
12468     }
12469   else if (strncmp (name, "arch=", 5) == 0)
12470     {
12471       /* Permit the user to change the architecture on the fly.  Needless
12472          to say, misuse can cause serious problems.  */
12473       if (strcmp (name + 5, "default") == 0)
12474         {
12475           mips_opts.arch = file_mips_arch;
12476           mips_opts.isa = file_mips_isa;
12477           mips_opts.gp32 = file_mips_gp32;
12478           mips_opts.fp32 = file_mips_fp32;
12479         }
12480       else
12481         {
12482           const struct mips_cpu_info *p;
12483
12484           p = mips_parse_cpu("internal use", name + 5);
12485           if (!p)
12486             as_bad (_("unknown architecture %s"), name + 5);
12487           else
12488             {
12489               mips_opts.arch = p->cpu;
12490               mips_opts.isa = p->isa;
12491             }
12492
12493           switch (mips_opts.arch)
12494             {
12495               case CPU_R3000:
12496               case CPU_R3900:
12497               case CPU_R6000:
12498               case CPU_MIPS32:
12499               case CPU_MIPS32R2:
12500                 mips_opts.gp32 = 1;
12501                 mips_opts.fp32 = 1;
12502                 break;
12503               default:
12504                 mips_opts.gp32 = 0;
12505                 mips_opts.fp32 = 0;
12506                 break;
12507             }
12508         }
12509     }
12510   else
12511     {
12512       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12513     }
12514   *input_line_pointer = ch;
12515   demand_empty_rest_of_line ();
12516 }
12517
12518 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12519    .option pic2.  It means to generate SVR4 PIC calls.  */
12520
12521 static void
12522 s_abicalls (ignore)
12523      int ignore ATTRIBUTE_UNUSED;
12524 {
12525   mips_pic = SVR4_PIC;
12526   mips_abicalls = TRUE;
12527   if (USE_GLOBAL_POINTER_OPT)
12528     {
12529       if (g_switch_seen && g_switch_value != 0)
12530         as_warn (_("-G may not be used with SVR4 PIC code"));
12531       g_switch_value = 0;
12532     }
12533   bfd_set_gp_size (stdoutput, 0);
12534   demand_empty_rest_of_line ();
12535 }
12536
12537 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12538    PIC code.  It sets the $gp register for the function based on the
12539    function address, which is in the register named in the argument.
12540    This uses a relocation against _gp_disp, which is handled specially
12541    by the linker.  The result is:
12542         lui     $gp,%hi(_gp_disp)
12543         addiu   $gp,$gp,%lo(_gp_disp)
12544         addu    $gp,$gp,.cpload argument
12545    The .cpload argument is normally $25 == $t9.  */
12546
12547 static void
12548 s_cpload (ignore)
12549      int ignore ATTRIBUTE_UNUSED;
12550 {
12551   expressionS ex;
12552   int icnt = 0;
12553
12554   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12555      .cpload is ignored.  */
12556   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12557     {
12558       s_ignore (0);
12559       return;
12560     }
12561
12562   /* .cpload should be in a .set noreorder section.  */
12563   if (mips_opts.noreorder == 0)
12564     as_warn (_(".cpload not in noreorder section"));
12565
12566   ex.X_op = O_symbol;
12567   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12568   ex.X_op_symbol = NULL;
12569   ex.X_add_number = 0;
12570
12571   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12572   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12573
12574   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12575   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12576                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12577
12578   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12579                mips_gp_register, mips_gp_register, tc_get_register (0));
12580
12581   demand_empty_rest_of_line ();
12582 }
12583
12584 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12585      .cpsetup $reg1, offset|$reg2, label
12586
12587    If offset is given, this results in:
12588      sd         $gp, offset($sp)
12589      lui        $gp, %hi(%neg(%gp_rel(label)))
12590      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12591      daddu      $gp, $gp, $reg1
12592
12593    If $reg2 is given, this results in:
12594      daddu      $reg2, $gp, $0
12595      lui        $gp, %hi(%neg(%gp_rel(label)))
12596      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12597      daddu      $gp, $gp, $reg1
12598    $reg1 is normally $25 == $t9.  */
12599 static void
12600 s_cpsetup (ignore)
12601      int ignore ATTRIBUTE_UNUSED;
12602 {
12603   expressionS ex_off;
12604   expressionS ex_sym;
12605   int reg1;
12606   int icnt = 0;
12607   char *f;
12608
12609   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12610      We also need NewABI support.  */
12611   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12612     {
12613       s_ignore (0);
12614       return;
12615     }
12616
12617   reg1 = tc_get_register (0);
12618   SKIP_WHITESPACE ();
12619   if (*input_line_pointer != ',')
12620     {
12621       as_bad (_("missing argument separator ',' for .cpsetup"));
12622       return;
12623     }
12624   else
12625     ++input_line_pointer;
12626   SKIP_WHITESPACE ();
12627   if (*input_line_pointer == '$')
12628     {
12629       mips_cpreturn_register = tc_get_register (0);
12630       mips_cpreturn_offset = -1;
12631     }
12632   else
12633     {
12634       mips_cpreturn_offset = get_absolute_expression ();
12635       mips_cpreturn_register = -1;
12636     }
12637   SKIP_WHITESPACE ();
12638   if (*input_line_pointer != ',')
12639     {
12640       as_bad (_("missing argument separator ',' for .cpsetup"));
12641       return;
12642     }
12643   else
12644     ++input_line_pointer;
12645   SKIP_WHITESPACE ();
12646   expression (&ex_sym);
12647
12648   if (mips_cpreturn_register == -1)
12649     {
12650       ex_off.X_op = O_constant;
12651       ex_off.X_add_symbol = NULL;
12652       ex_off.X_op_symbol = NULL;
12653       ex_off.X_add_number = mips_cpreturn_offset;
12654
12655       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12656                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12657     }
12658   else
12659     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12660                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12661
12662   /* Ensure there's room for the next two instructions, so that `f'
12663      doesn't end up with an address in the wrong frag.  */
12664   frag_grow (8);
12665   f = frag_more (0);
12666   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12667                (int) BFD_RELOC_GPREL16);
12668   fix_new (frag_now, f - frag_now->fr_literal,
12669            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12670   fix_new (frag_now, f - frag_now->fr_literal,
12671            4, NULL, 0, 0, BFD_RELOC_HI16_S);
12672
12673   f = frag_more (0);
12674   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12675                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12676   fix_new (frag_now, f - frag_now->fr_literal,
12677            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12678   fix_new (frag_now, f - frag_now->fr_literal,
12679            4, NULL, 0, 0, BFD_RELOC_LO16);
12680
12681   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, ADDRESS_ADD_INSN,
12682                "d,v,t", mips_gp_register, mips_gp_register, reg1);
12683
12684   demand_empty_rest_of_line ();
12685 }
12686
12687 static void
12688 s_cplocal (ignore)
12689      int ignore ATTRIBUTE_UNUSED;
12690 {
12691   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12692    .cplocal is ignored.  */
12693   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12694     {
12695       s_ignore (0);
12696       return;
12697     }
12698
12699   mips_gp_register = tc_get_register (0);
12700   demand_empty_rest_of_line ();
12701 }
12702
12703 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12704    offset from $sp.  The offset is remembered, and after making a PIC
12705    call $gp is restored from that location.  */
12706
12707 static void
12708 s_cprestore (ignore)
12709      int ignore ATTRIBUTE_UNUSED;
12710 {
12711   expressionS ex;
12712   int icnt = 0;
12713
12714   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12715      .cprestore is ignored.  */
12716   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12717     {
12718       s_ignore (0);
12719       return;
12720     }
12721
12722   mips_cprestore_offset = get_absolute_expression ();
12723   mips_cprestore_valid = 1;
12724
12725   ex.X_op = O_constant;
12726   ex.X_add_symbol = NULL;
12727   ex.X_op_symbol = NULL;
12728   ex.X_add_number = mips_cprestore_offset;
12729
12730   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex, ADDRESS_STORE_INSN,
12731                                 mips_gp_register, SP);
12732
12733   demand_empty_rest_of_line ();
12734 }
12735
12736 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12737    was given in the preceeding .gpsetup, it results in:
12738      ld         $gp, offset($sp)
12739
12740    If a register $reg2 was given there, it results in:
12741      daddiu     $gp, $gp, $reg2
12742  */
12743 static void
12744 s_cpreturn (ignore)
12745      int ignore ATTRIBUTE_UNUSED;
12746 {
12747   expressionS ex;
12748   int icnt = 0;
12749
12750   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12751      We also need NewABI support.  */
12752   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12753     {
12754       s_ignore (0);
12755       return;
12756     }
12757
12758   if (mips_cpreturn_register == -1)
12759     {
12760       ex.X_op = O_constant;
12761       ex.X_add_symbol = NULL;
12762       ex.X_op_symbol = NULL;
12763       ex.X_add_number = mips_cpreturn_offset;
12764
12765       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12766                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12767     }
12768   else
12769     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12770                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12771
12772   demand_empty_rest_of_line ();
12773 }
12774
12775 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12776    code.  It sets the offset to use in gp_rel relocations.  */
12777
12778 static void
12779 s_gpvalue (ignore)
12780      int ignore ATTRIBUTE_UNUSED;
12781 {
12782   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12783      We also need NewABI support.  */
12784   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12785     {
12786       s_ignore (0);
12787       return;
12788     }
12789
12790   mips_gprel_offset = get_absolute_expression ();
12791
12792   demand_empty_rest_of_line ();
12793 }
12794
12795 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12796    code.  It generates a 32 bit GP relative reloc.  */
12797
12798 static void
12799 s_gpword (ignore)
12800      int ignore ATTRIBUTE_UNUSED;
12801 {
12802   symbolS *label;
12803   expressionS ex;
12804   char *p;
12805
12806   /* When not generating PIC code, this is treated as .word.  */
12807   if (mips_pic != SVR4_PIC)
12808     {
12809       s_cons (2);
12810       return;
12811     }
12812
12813   label = insn_labels != NULL ? insn_labels->label : NULL;
12814   mips_emit_delays (TRUE);
12815   if (auto_align)
12816     mips_align (2, 0, label);
12817   mips_clear_insn_labels ();
12818
12819   expression (&ex);
12820
12821   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12822     {
12823       as_bad (_("Unsupported use of .gpword"));
12824       ignore_rest_of_line ();
12825     }
12826
12827   p = frag_more (4);
12828   md_number_to_chars (p, (valueT) 0, 4);
12829   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12830                BFD_RELOC_GPREL32);
12831
12832   demand_empty_rest_of_line ();
12833 }
12834
12835 static void
12836 s_gpdword (ignore)
12837      int ignore ATTRIBUTE_UNUSED;
12838 {
12839   symbolS *label;
12840   expressionS ex;
12841   char *p;
12842
12843   /* When not generating PIC code, this is treated as .dword.  */
12844   if (mips_pic != SVR4_PIC)
12845     {
12846       s_cons (3);
12847       return;
12848     }
12849
12850   label = insn_labels != NULL ? insn_labels->label : NULL;
12851   mips_emit_delays (TRUE);
12852   if (auto_align)
12853     mips_align (3, 0, label);
12854   mips_clear_insn_labels ();
12855
12856   expression (&ex);
12857
12858   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12859     {
12860       as_bad (_("Unsupported use of .gpdword"));
12861       ignore_rest_of_line ();
12862     }
12863
12864   p = frag_more (8);
12865   md_number_to_chars (p, (valueT) 0, 8);
12866   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12867                BFD_RELOC_GPREL32);
12868
12869   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12870   ex.X_op = O_absent;
12871   ex.X_add_symbol = 0;
12872   ex.X_add_number = 0;
12873   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12874                BFD_RELOC_64);
12875
12876   demand_empty_rest_of_line ();
12877 }
12878
12879 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12880    tables in SVR4 PIC code.  */
12881
12882 static void
12883 s_cpadd (ignore)
12884      int ignore ATTRIBUTE_UNUSED;
12885 {
12886   int icnt = 0;
12887   int reg;
12888
12889   /* This is ignored when not generating SVR4 PIC code.  */
12890   if (mips_pic != SVR4_PIC)
12891     {
12892       s_ignore (0);
12893       return;
12894     }
12895
12896   /* Add $gp to the register named as an argument.  */
12897   reg = tc_get_register (0);
12898   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, ADDRESS_ADD_INSN,
12899                "d,v,t", reg, reg, mips_gp_register);
12900
12901   demand_empty_rest_of_line ();
12902 }
12903
12904 /* Handle the .insn pseudo-op.  This marks instruction labels in
12905    mips16 mode.  This permits the linker to handle them specially,
12906    such as generating jalx instructions when needed.  We also make
12907    them odd for the duration of the assembly, in order to generate the
12908    right sort of code.  We will make them even in the adjust_symtab
12909    routine, while leaving them marked.  This is convenient for the
12910    debugger and the disassembler.  The linker knows to make them odd
12911    again.  */
12912
12913 static void
12914 s_insn (ignore)
12915      int ignore ATTRIBUTE_UNUSED;
12916 {
12917   mips16_mark_labels ();
12918
12919   demand_empty_rest_of_line ();
12920 }
12921
12922 /* Handle a .stabn directive.  We need these in order to mark a label
12923    as being a mips16 text label correctly.  Sometimes the compiler
12924    will emit a label, followed by a .stabn, and then switch sections.
12925    If the label and .stabn are in mips16 mode, then the label is
12926    really a mips16 text label.  */
12927
12928 static void
12929 s_mips_stab (type)
12930      int type;
12931 {
12932   if (type == 'n')
12933     mips16_mark_labels ();
12934
12935   s_stab (type);
12936 }
12937
12938 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12939  */
12940
12941 static void
12942 s_mips_weakext (ignore)
12943      int ignore ATTRIBUTE_UNUSED;
12944 {
12945   char *name;
12946   int c;
12947   symbolS *symbolP;
12948   expressionS exp;
12949
12950   name = input_line_pointer;
12951   c = get_symbol_end ();
12952   symbolP = symbol_find_or_make (name);
12953   S_SET_WEAK (symbolP);
12954   *input_line_pointer = c;
12955
12956   SKIP_WHITESPACE ();
12957
12958   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12959     {
12960       if (S_IS_DEFINED (symbolP))
12961         {
12962           as_bad ("ignoring attempt to redefine symbol %s",
12963                   S_GET_NAME (symbolP));
12964           ignore_rest_of_line ();
12965           return;
12966         }
12967
12968       if (*input_line_pointer == ',')
12969         {
12970           ++input_line_pointer;
12971           SKIP_WHITESPACE ();
12972         }
12973
12974       expression (&exp);
12975       if (exp.X_op != O_symbol)
12976         {
12977           as_bad ("bad .weakext directive");
12978           ignore_rest_of_line ();
12979           return;
12980         }
12981       symbol_set_value_expression (symbolP, &exp);
12982     }
12983
12984   demand_empty_rest_of_line ();
12985 }
12986
12987 /* Parse a register string into a number.  Called from the ECOFF code
12988    to parse .frame.  The argument is non-zero if this is the frame
12989    register, so that we can record it in mips_frame_reg.  */
12990
12991 int
12992 tc_get_register (frame)
12993      int frame;
12994 {
12995   int reg;
12996
12997   SKIP_WHITESPACE ();
12998   if (*input_line_pointer++ != '$')
12999     {
13000       as_warn (_("expected `$'"));
13001       reg = ZERO;
13002     }
13003   else if (ISDIGIT (*input_line_pointer))
13004     {
13005       reg = get_absolute_expression ();
13006       if (reg < 0 || reg >= 32)
13007         {
13008           as_warn (_("Bad register number"));
13009           reg = ZERO;
13010         }
13011     }
13012   else
13013     {
13014       if (strncmp (input_line_pointer, "ra", 2) == 0)
13015         {
13016           reg = RA;
13017           input_line_pointer += 2;
13018         }
13019       else if (strncmp (input_line_pointer, "fp", 2) == 0)
13020         {
13021           reg = FP;
13022           input_line_pointer += 2;
13023         }
13024       else if (strncmp (input_line_pointer, "sp", 2) == 0)
13025         {
13026           reg = SP;
13027           input_line_pointer += 2;
13028         }
13029       else if (strncmp (input_line_pointer, "gp", 2) == 0)
13030         {
13031           reg = GP;
13032           input_line_pointer += 2;
13033         }
13034       else if (strncmp (input_line_pointer, "at", 2) == 0)
13035         {
13036           reg = AT;
13037           input_line_pointer += 2;
13038         }
13039       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13040         {
13041           reg = KT0;
13042           input_line_pointer += 3;
13043         }
13044       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13045         {
13046           reg = KT1;
13047           input_line_pointer += 3;
13048         }
13049       else if (strncmp (input_line_pointer, "zero", 4) == 0)
13050         {
13051           reg = ZERO;
13052           input_line_pointer += 4;
13053         }
13054       else
13055         {
13056           as_warn (_("Unrecognized register name"));
13057           reg = ZERO;
13058           while (ISALNUM(*input_line_pointer))
13059            input_line_pointer++;
13060         }
13061     }
13062   if (frame)
13063     {
13064       mips_frame_reg = reg != 0 ? reg : SP;
13065       mips_frame_reg_valid = 1;
13066       mips_cprestore_valid = 0;
13067     }
13068   return reg;
13069 }
13070
13071 valueT
13072 md_section_align (seg, addr)
13073      asection *seg;
13074      valueT addr;
13075 {
13076   int align = bfd_get_section_alignment (stdoutput, seg);
13077
13078 #ifdef OBJ_ELF
13079   /* We don't need to align ELF sections to the full alignment.
13080      However, Irix 5 may prefer that we align them at least to a 16
13081      byte boundary.  We don't bother to align the sections if we are
13082      targeted for an embedded system.  */
13083   if (strcmp (TARGET_OS, "elf") == 0)
13084     return addr;
13085   if (align > 4)
13086     align = 4;
13087 #endif
13088
13089   return ((addr + (1 << align) - 1) & (-1 << align));
13090 }
13091
13092 /* Utility routine, called from above as well.  If called while the
13093    input file is still being read, it's only an approximation.  (For
13094    example, a symbol may later become defined which appeared to be
13095    undefined earlier.)  */
13096
13097 static int
13098 nopic_need_relax (sym, before_relaxing)
13099      symbolS *sym;
13100      int before_relaxing;
13101 {
13102   if (sym == 0)
13103     return 0;
13104
13105   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
13106     {
13107       const char *symname;
13108       int change;
13109
13110       /* Find out whether this symbol can be referenced off the $gp
13111          register.  It can be if it is smaller than the -G size or if
13112          it is in the .sdata or .sbss section.  Certain symbols can
13113          not be referenced off the $gp, although it appears as though
13114          they can.  */
13115       symname = S_GET_NAME (sym);
13116       if (symname != (const char *) NULL
13117           && (strcmp (symname, "eprol") == 0
13118               || strcmp (symname, "etext") == 0
13119               || strcmp (symname, "_gp") == 0
13120               || strcmp (symname, "edata") == 0
13121               || strcmp (symname, "_fbss") == 0
13122               || strcmp (symname, "_fdata") == 0
13123               || strcmp (symname, "_ftext") == 0
13124               || strcmp (symname, "end") == 0
13125               || strcmp (symname, "_gp_disp") == 0))
13126         change = 1;
13127       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13128                && (0
13129 #ifndef NO_ECOFF_DEBUGGING
13130                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
13131                        && (symbol_get_obj (sym)->ecoff_extern_size
13132                            <= g_switch_value))
13133 #endif
13134                    /* We must defer this decision until after the whole
13135                       file has been read, since there might be a .extern
13136                       after the first use of this symbol.  */
13137                    || (before_relaxing
13138 #ifndef NO_ECOFF_DEBUGGING
13139                        && symbol_get_obj (sym)->ecoff_extern_size == 0
13140 #endif
13141                        && S_GET_VALUE (sym) == 0)
13142                    || (S_GET_VALUE (sym) != 0
13143                        && S_GET_VALUE (sym) <= g_switch_value)))
13144         change = 0;
13145       else
13146         {
13147           const char *segname;
13148
13149           segname = segment_name (S_GET_SEGMENT (sym));
13150           assert (strcmp (segname, ".lit8") != 0
13151                   && strcmp (segname, ".lit4") != 0);
13152           change = (strcmp (segname, ".sdata") != 0
13153                     && strcmp (segname, ".sbss") != 0
13154                     && strncmp (segname, ".sdata.", 7) != 0
13155                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13156         }
13157       return change;
13158     }
13159   else
13160     /* We are not optimizing for the $gp register.  */
13161     return 1;
13162 }
13163
13164
13165 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
13166
13167 static bfd_boolean
13168 pic_need_relax (sym, segtype)
13169      symbolS *sym;
13170      asection *segtype;
13171 {
13172   asection *symsec;
13173   bfd_boolean linkonce;
13174
13175   /* Handle the case of a symbol equated to another symbol.  */
13176   while (symbol_equated_reloc_p (sym))
13177     {
13178       symbolS *n;
13179
13180       /* It's possible to get a loop here in a badly written
13181          program.  */
13182       n = symbol_get_value_expression (sym)->X_add_symbol;
13183       if (n == sym)
13184         break;
13185       sym = n;
13186     }
13187
13188   symsec = S_GET_SEGMENT (sym);
13189
13190   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13191   linkonce = FALSE;
13192   if (symsec != segtype && ! S_IS_LOCAL (sym))
13193     {
13194       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13195           != 0)
13196         linkonce = TRUE;
13197
13198       /* The GNU toolchain uses an extension for ELF: a section
13199          beginning with the magic string .gnu.linkonce is a linkonce
13200          section.  */
13201       if (strncmp (segment_name (symsec), ".gnu.linkonce",
13202                    sizeof ".gnu.linkonce" - 1) == 0)
13203         linkonce = TRUE;
13204     }
13205
13206   /* This must duplicate the test in adjust_reloc_syms.  */
13207   return (symsec != &bfd_und_section
13208           && symsec != &bfd_abs_section
13209           && ! bfd_is_com_section (symsec)
13210           && !linkonce
13211 #ifdef OBJ_ELF
13212           /* A global or weak symbol is treated as external.  */
13213           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13214               || (! S_IS_WEAK (sym)
13215                   && (! S_IS_EXTERNAL (sym)
13216                       || mips_pic == EMBEDDED_PIC)))
13217 #endif
13218           );
13219 }
13220
13221
13222 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13223    extended opcode.  SEC is the section the frag is in.  */
13224
13225 static int
13226 mips16_extended_frag (fragp, sec, stretch)
13227      fragS *fragp;
13228      asection *sec;
13229      long stretch;
13230 {
13231   int type;
13232   register const struct mips16_immed_operand *op;
13233   offsetT val;
13234   int mintiny, maxtiny;
13235   segT symsec;
13236   fragS *sym_frag;
13237
13238   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13239     return 0;
13240   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13241     return 1;
13242
13243   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13244   op = mips16_immed_operands;
13245   while (op->type != type)
13246     {
13247       ++op;
13248       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13249     }
13250
13251   if (op->unsp)
13252     {
13253       if (type == '<' || type == '>' || type == '[' || type == ']')
13254         {
13255           mintiny = 1;
13256           maxtiny = 1 << op->nbits;
13257         }
13258       else
13259         {
13260           mintiny = 0;
13261           maxtiny = (1 << op->nbits) - 1;
13262         }
13263     }
13264   else
13265     {
13266       mintiny = - (1 << (op->nbits - 1));
13267       maxtiny = (1 << (op->nbits - 1)) - 1;
13268     }
13269
13270   sym_frag = symbol_get_frag (fragp->fr_symbol);
13271   val = S_GET_VALUE (fragp->fr_symbol);
13272   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13273
13274   if (op->pcrel)
13275     {
13276       addressT addr;
13277
13278       /* We won't have the section when we are called from
13279          mips_relax_frag.  However, we will always have been called
13280          from md_estimate_size_before_relax first.  If this is a
13281          branch to a different section, we mark it as such.  If SEC is
13282          NULL, and the frag is not marked, then it must be a branch to
13283          the same section.  */
13284       if (sec == NULL)
13285         {
13286           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13287             return 1;
13288         }
13289       else
13290         {
13291           /* Must have been called from md_estimate_size_before_relax.  */
13292           if (symsec != sec)
13293             {
13294               fragp->fr_subtype =
13295                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13296
13297               /* FIXME: We should support this, and let the linker
13298                  catch branches and loads that are out of range.  */
13299               as_bad_where (fragp->fr_file, fragp->fr_line,
13300                             _("unsupported PC relative reference to different section"));
13301
13302               return 1;
13303             }
13304           if (fragp != sym_frag && sym_frag->fr_address == 0)
13305             /* Assume non-extended on the first relaxation pass.
13306                The address we have calculated will be bogus if this is
13307                a forward branch to another frag, as the forward frag
13308                will have fr_address == 0.  */
13309             return 0;
13310         }
13311
13312       /* In this case, we know for sure that the symbol fragment is in
13313          the same section.  If the relax_marker of the symbol fragment
13314          differs from the relax_marker of this fragment, we have not
13315          yet adjusted the symbol fragment fr_address.  We want to add
13316          in STRETCH in order to get a better estimate of the address.
13317          This particularly matters because of the shift bits.  */
13318       if (stretch != 0
13319           && sym_frag->relax_marker != fragp->relax_marker)
13320         {
13321           fragS *f;
13322
13323           /* Adjust stretch for any alignment frag.  Note that if have
13324              been expanding the earlier code, the symbol may be
13325              defined in what appears to be an earlier frag.  FIXME:
13326              This doesn't handle the fr_subtype field, which specifies
13327              a maximum number of bytes to skip when doing an
13328              alignment.  */
13329           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13330             {
13331               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13332                 {
13333                   if (stretch < 0)
13334                     stretch = - ((- stretch)
13335                                  & ~ ((1 << (int) f->fr_offset) - 1));
13336                   else
13337                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13338                   if (stretch == 0)
13339                     break;
13340                 }
13341             }
13342           if (f != NULL)
13343             val += stretch;
13344         }
13345
13346       addr = fragp->fr_address + fragp->fr_fix;
13347
13348       /* The base address rules are complicated.  The base address of
13349          a branch is the following instruction.  The base address of a
13350          PC relative load or add is the instruction itself, but if it
13351          is in a delay slot (in which case it can not be extended) use
13352          the address of the instruction whose delay slot it is in.  */
13353       if (type == 'p' || type == 'q')
13354         {
13355           addr += 2;
13356
13357           /* If we are currently assuming that this frag should be
13358              extended, then, the current address is two bytes
13359              higher.  */
13360           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13361             addr += 2;
13362
13363           /* Ignore the low bit in the target, since it will be set
13364              for a text label.  */
13365           if ((val & 1) != 0)
13366             --val;
13367         }
13368       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13369         addr -= 4;
13370       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13371         addr -= 2;
13372
13373       val -= addr & ~ ((1 << op->shift) - 1);
13374
13375       /* Branch offsets have an implicit 0 in the lowest bit.  */
13376       if (type == 'p' || type == 'q')
13377         val /= 2;
13378
13379       /* If any of the shifted bits are set, we must use an extended
13380          opcode.  If the address depends on the size of this
13381          instruction, this can lead to a loop, so we arrange to always
13382          use an extended opcode.  We only check this when we are in
13383          the main relaxation loop, when SEC is NULL.  */
13384       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13385         {
13386           fragp->fr_subtype =
13387             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13388           return 1;
13389         }
13390
13391       /* If we are about to mark a frag as extended because the value
13392          is precisely maxtiny + 1, then there is a chance of an
13393          infinite loop as in the following code:
13394              la $4,foo
13395              .skip      1020
13396              .align     2
13397            foo:
13398          In this case when the la is extended, foo is 0x3fc bytes
13399          away, so the la can be shrunk, but then foo is 0x400 away, so
13400          the la must be extended.  To avoid this loop, we mark the
13401          frag as extended if it was small, and is about to become
13402          extended with a value of maxtiny + 1.  */
13403       if (val == ((maxtiny + 1) << op->shift)
13404           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13405           && sec == NULL)
13406         {
13407           fragp->fr_subtype =
13408             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13409           return 1;
13410         }
13411     }
13412   else if (symsec != absolute_section && sec != NULL)
13413     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13414
13415   if ((val & ((1 << op->shift) - 1)) != 0
13416       || val < (mintiny << op->shift)
13417       || val > (maxtiny << op->shift))
13418     return 1;
13419   else
13420     return 0;
13421 }
13422
13423 /* Compute the length of a branch sequence, and adjust the
13424    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13425    worst-case length is computed, with UPDATE being used to indicate
13426    whether an unconditional (-1), branch-likely (+1) or regular (0)
13427    branch is to be computed.  */
13428 static int
13429 relaxed_branch_length (fragp, sec, update)
13430      fragS *fragp;
13431      asection *sec;
13432      int update;
13433 {
13434   bfd_boolean toofar;
13435   int length;
13436
13437   if (fragp
13438       && S_IS_DEFINED (fragp->fr_symbol)
13439       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13440     {
13441       addressT addr;
13442       offsetT val;
13443
13444       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13445
13446       addr = fragp->fr_address + fragp->fr_fix + 4;
13447
13448       val -= addr;
13449
13450       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13451     }
13452   else if (fragp)
13453     /* If the symbol is not defined or it's in a different segment,
13454        assume the user knows what's going on and emit a short
13455        branch.  */
13456     toofar = FALSE;
13457   else
13458     toofar = TRUE;
13459
13460   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13461     fragp->fr_subtype
13462       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13463                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13464                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13465                              toofar);
13466
13467   length = 4;
13468   if (toofar)
13469     {
13470       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13471         length += 8;
13472
13473       if (mips_pic != NO_PIC)
13474         {
13475           /* Additional space for PIC loading of target address.  */
13476           length += 8;
13477           if (mips_opts.isa == ISA_MIPS1)
13478             /* Additional space for $at-stabilizing nop.  */
13479             length += 4;
13480         }
13481
13482       /* If branch is conditional.  */
13483       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13484         length += 8;
13485     }
13486
13487   return length;
13488 }
13489
13490 /* Estimate the size of a frag before relaxing.  Unless this is the
13491    mips16, we are not really relaxing here, and the final size is
13492    encoded in the subtype information.  For the mips16, we have to
13493    decide whether we are using an extended opcode or not.  */
13494
13495 int
13496 md_estimate_size_before_relax (fragp, segtype)
13497      fragS *fragp;
13498      asection *segtype;
13499 {
13500   int change;
13501
13502   if (RELAX_BRANCH_P (fragp->fr_subtype))
13503     {
13504
13505       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13506
13507       return fragp->fr_var;
13508     }
13509
13510   if (RELAX_MIPS16_P (fragp->fr_subtype))
13511     /* We don't want to modify the EXTENDED bit here; it might get us
13512        into infinite loops.  We change it only in mips_relax_frag().  */
13513     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13514
13515   if (mips_pic == NO_PIC)
13516     change = nopic_need_relax (fragp->fr_symbol, 0);
13517   else if (mips_pic == SVR4_PIC)
13518     change = pic_need_relax (fragp->fr_symbol, segtype);
13519   else
13520     abort ();
13521
13522   if (change)
13523     {
13524       /* Record the offset to the first reloc in the fr_opcode field.
13525          This lets md_convert_frag and tc_gen_reloc know that the code
13526          must be expanded.  */
13527       fragp->fr_opcode = (fragp->fr_literal
13528                           + fragp->fr_fix
13529                           - RELAX_OLD (fragp->fr_subtype)
13530                           + RELAX_RELOC1 (fragp->fr_subtype));
13531       /* FIXME: This really needs as_warn_where.  */
13532       if (RELAX_WARN (fragp->fr_subtype))
13533         as_warn (_("AT used after \".set noat\" or macro used after "
13534                    "\".set nomacro\""));
13535
13536       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13537     }
13538
13539   return 0;
13540 }
13541
13542 /* This is called to see whether a reloc against a defined symbol
13543    should be converted into a reloc against a section.  Don't adjust
13544    MIPS16 jump relocations, so we don't have to worry about the format
13545    of the offset in the .o file.  Don't adjust relocations against
13546    mips16 symbols, so that the linker can find them if it needs to set
13547    up a stub.  */
13548
13549 int
13550 mips_fix_adjustable (fixp)
13551      fixS *fixp;
13552 {
13553   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13554     return 0;
13555
13556   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13557       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13558     return 0;
13559
13560   if (fixp->fx_addsy == NULL)
13561     return 1;
13562
13563 #ifdef OBJ_ELF
13564   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13565       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13566       && fixp->fx_subsy == NULL)
13567     return 0;
13568 #endif
13569
13570   return 1;
13571 }
13572
13573 /* Translate internal representation of relocation info to BFD target
13574    format.  */
13575
13576 arelent **
13577 tc_gen_reloc (section, fixp)
13578      asection *section ATTRIBUTE_UNUSED;
13579      fixS *fixp;
13580 {
13581   static arelent *retval[4];
13582   arelent *reloc;
13583   bfd_reloc_code_real_type code;
13584
13585   memset (retval, 0, sizeof(retval));
13586   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13587   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13588   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13589   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13590
13591   if (mips_pic == EMBEDDED_PIC
13592       && SWITCH_TABLE (fixp))
13593     {
13594       /* For a switch table entry we use a special reloc.  The addend
13595          is actually the difference between the reloc address and the
13596          subtrahend.  */
13597       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13598       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13599         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13600       fixp->fx_r_type = BFD_RELOC_GPREL32;
13601     }
13602   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13603     {
13604       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13605         reloc->addend = fixp->fx_addnumber;
13606       else
13607         {
13608           /* We use a special addend for an internal RELLO reloc.  */
13609           if (symbol_section_p (fixp->fx_addsy))
13610             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13611           else
13612             reloc->addend = fixp->fx_addnumber + reloc->address;
13613         }
13614     }
13615   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13616     {
13617       assert (fixp->fx_next != NULL
13618               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13619
13620       /* The reloc is relative to the RELLO; adjust the addend
13621          accordingly.  */
13622       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13623         reloc->addend = fixp->fx_next->fx_addnumber;
13624       else
13625         {
13626           /* We use a special addend for an internal RELHI reloc.  */
13627           if (symbol_section_p (fixp->fx_addsy))
13628             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13629                              + fixp->fx_next->fx_where
13630                              - S_GET_VALUE (fixp->fx_subsy));
13631           else
13632             reloc->addend = (fixp->fx_addnumber
13633                              + fixp->fx_next->fx_frag->fr_address
13634                              + fixp->fx_next->fx_where);
13635         }
13636     }
13637   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13638     reloc->addend = fixp->fx_addnumber;
13639   else
13640     {
13641       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13642         /* A gruesome hack which is a result of the gruesome gas reloc
13643            handling.  */
13644         reloc->addend = reloc->address;
13645       else
13646         reloc->addend = -reloc->address;
13647     }
13648
13649   /* If this is a variant frag, we may need to adjust the existing
13650      reloc and generate a new one.  */
13651   if (fixp->fx_frag->fr_opcode != NULL
13652       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13653            && ! HAVE_NEWABI)
13654           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13655               && HAVE_NEWABI)
13656           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13657           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13658           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13659           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13660           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13661           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13662     )
13663     {
13664       arelent *reloc2;
13665
13666       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13667
13668       /* If this is not the last reloc in this frag, then we have two
13669          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13670          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13671          the second one handle all of them.  */
13672       if (fixp->fx_next != NULL
13673           && fixp->fx_frag == fixp->fx_next->fx_frag)
13674         {
13675           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13676                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13677                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13678                       && (fixp->fx_next->fx_r_type
13679                           == BFD_RELOC_MIPS_GOT_LO16))
13680                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13681                       && (fixp->fx_next->fx_r_type
13682                           == BFD_RELOC_MIPS_CALL_LO16)));
13683           retval[0] = NULL;
13684           return retval;
13685         }
13686
13687       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13688       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13689       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13690       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13691       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13692       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13693       reloc2->address = (reloc->address
13694                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13695                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13696       reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13697         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13698       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13699       assert (reloc2->howto != NULL);
13700
13701       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13702         {
13703           arelent *reloc3;
13704
13705           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13706           *reloc3 = *reloc2;
13707           reloc3->address += 4;
13708         }
13709
13710       if (mips_pic == NO_PIC)
13711         {
13712           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13713           fixp->fx_r_type = BFD_RELOC_HI16_S;
13714         }
13715       else if (mips_pic == SVR4_PIC)
13716         {
13717           switch (fixp->fx_r_type)
13718             {
13719             default:
13720               abort ();
13721             case BFD_RELOC_MIPS_GOT16:
13722               break;
13723             case BFD_RELOC_MIPS_GOT_LO16:
13724             case BFD_RELOC_MIPS_CALL_LO16:
13725               if (HAVE_NEWABI)
13726                 {
13727                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13728                   reloc2->howto = bfd_reloc_type_lookup
13729                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13730                 }
13731               else
13732                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13733               break;
13734             case BFD_RELOC_MIPS_CALL16:
13735             case BFD_RELOC_MIPS_GOT_OFST:
13736             case BFD_RELOC_MIPS_GOT_DISP:
13737               if (HAVE_NEWABI)
13738                 {
13739                   /* It may seem nonsensical to relax GOT_DISP to
13740                      GOT_DISP, but we're actually turning a GOT_DISP
13741                      without offset into a GOT_DISP with an offset,
13742                      getting rid of the separate addition, which we can
13743                      do when the symbol is found to be local.  */
13744                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13745                   retval[1] = NULL;
13746                 }
13747               else
13748                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13749               break;
13750             }
13751         }
13752       else
13753         abort ();
13754     }
13755
13756   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13757      entry to be used in the relocation's section offset.  */
13758   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13759     {
13760       reloc->address = reloc->addend;
13761       reloc->addend = 0;
13762     }
13763
13764   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13765      fixup_segment converted a non-PC relative reloc into a PC
13766      relative reloc.  In such a case, we need to convert the reloc
13767      code.  */
13768   code = fixp->fx_r_type;
13769   if (fixp->fx_pcrel)
13770     {
13771       switch (code)
13772         {
13773         case BFD_RELOC_8:
13774           code = BFD_RELOC_8_PCREL;
13775           break;
13776         case BFD_RELOC_16:
13777           code = BFD_RELOC_16_PCREL;
13778           break;
13779         case BFD_RELOC_32:
13780           code = BFD_RELOC_32_PCREL;
13781           break;
13782         case BFD_RELOC_64:
13783           code = BFD_RELOC_64_PCREL;
13784           break;
13785         case BFD_RELOC_8_PCREL:
13786         case BFD_RELOC_16_PCREL:
13787         case BFD_RELOC_32_PCREL:
13788         case BFD_RELOC_64_PCREL:
13789         case BFD_RELOC_16_PCREL_S2:
13790         case BFD_RELOC_PCREL_HI16_S:
13791         case BFD_RELOC_PCREL_LO16:
13792           break;
13793         default:
13794           as_bad_where (fixp->fx_file, fixp->fx_line,
13795                         _("Cannot make %s relocation PC relative"),
13796                         bfd_get_reloc_code_name (code));
13797         }
13798     }
13799
13800   /* To support a PC relative reloc when generating embedded PIC code
13801      for ECOFF, we use a Cygnus extension.  We check for that here to
13802      make sure that we don't let such a reloc escape normally.  */
13803   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13804        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13805       && code == BFD_RELOC_16_PCREL_S2
13806       && mips_pic != EMBEDDED_PIC)
13807     reloc->howto = NULL;
13808   else
13809     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13810
13811   if (reloc->howto == NULL)
13812     {
13813       as_bad_where (fixp->fx_file, fixp->fx_line,
13814                     _("Can not represent %s relocation in this object file format"),
13815                     bfd_get_reloc_code_name (code));
13816       retval[0] = NULL;
13817     }
13818
13819   return retval;
13820 }
13821
13822 /* Relax a machine dependent frag.  This returns the amount by which
13823    the current size of the frag should change.  */
13824
13825 int
13826 mips_relax_frag (sec, fragp, stretch)
13827      asection *sec;
13828      fragS *fragp;
13829      long stretch;
13830 {
13831   if (RELAX_BRANCH_P (fragp->fr_subtype))
13832     {
13833       offsetT old_var = fragp->fr_var;
13834
13835       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13836
13837       return fragp->fr_var - old_var;
13838     }
13839
13840   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13841     return 0;
13842
13843   if (mips16_extended_frag (fragp, NULL, stretch))
13844     {
13845       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13846         return 0;
13847       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13848       return 2;
13849     }
13850   else
13851     {
13852       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13853         return 0;
13854       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13855       return -2;
13856     }
13857
13858   return 0;
13859 }
13860
13861 /* Convert a machine dependent frag.  */
13862
13863 void
13864 md_convert_frag (abfd, asec, fragp)
13865      bfd *abfd ATTRIBUTE_UNUSED;
13866      segT asec;
13867      fragS *fragp;
13868 {
13869   int old, new;
13870   char *fixptr;
13871
13872   if (RELAX_BRANCH_P (fragp->fr_subtype))
13873     {
13874       bfd_byte *buf;
13875       unsigned long insn;
13876       expressionS exp;
13877       fixS *fixp;
13878
13879       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13880
13881       if (target_big_endian)
13882         insn = bfd_getb32 (buf);
13883       else
13884         insn = bfd_getl32 (buf);
13885
13886       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13887         {
13888           /* We generate a fixup instead of applying it right now
13889              because, if there are linker relaxations, we're going to
13890              need the relocations.  */
13891           exp.X_op = O_symbol;
13892           exp.X_add_symbol = fragp->fr_symbol;
13893           exp.X_add_number = fragp->fr_offset;
13894
13895           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13896                               4, &exp, 1,
13897                               BFD_RELOC_16_PCREL_S2);
13898           fixp->fx_file = fragp->fr_file;
13899           fixp->fx_line = fragp->fr_line;
13900
13901           md_number_to_chars ((char *)buf, insn, 4);
13902           buf += 4;
13903         }
13904       else
13905         {
13906           int i;
13907
13908           as_warn_where (fragp->fr_file, fragp->fr_line,
13909                          _("relaxed out-of-range branch into a jump"));
13910
13911           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13912             goto uncond;
13913
13914           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13915             {
13916               /* Reverse the branch.  */
13917               switch ((insn >> 28) & 0xf)
13918                 {
13919                 case 4:
13920                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13921                      have the condition reversed by tweaking a single
13922                      bit, and their opcodes all have 0x4???????.  */
13923                   assert ((insn & 0xf1000000) == 0x41000000);
13924                   insn ^= 0x00010000;
13925                   break;
13926
13927                 case 0:
13928                   /* bltz       0x04000000      bgez    0x04010000
13929                      bltzal     0x04100000      bgezal  0x04110000 */
13930                   assert ((insn & 0xfc0e0000) == 0x04000000);
13931                   insn ^= 0x00010000;
13932                   break;
13933
13934                 case 1:
13935                   /* beq        0x10000000      bne     0x14000000
13936                      blez       0x18000000      bgtz    0x1c000000 */
13937                   insn ^= 0x04000000;
13938                   break;
13939
13940                 default:
13941                   abort ();
13942                 }
13943             }
13944
13945           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13946             {
13947               /* Clear the and-link bit.  */
13948               assert ((insn & 0xfc1c0000) == 0x04100000);
13949
13950               /* bltzal 0x04100000      bgezal  0x04110000
13951                 bltzall 0x04120000     bgezall  0x04130000 */
13952               insn &= ~0x00100000;
13953             }
13954
13955           /* Branch over the branch (if the branch was likely) or the
13956              full jump (not likely case).  Compute the offset from the
13957              current instruction to branch to.  */
13958           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13959             i = 16;
13960           else
13961             {
13962               /* How many bytes in instructions we've already emitted?  */
13963               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13964               /* How many bytes in instructions from here to the end?  */
13965               i = fragp->fr_var - i;
13966             }
13967           /* Convert to instruction count.  */
13968           i >>= 2;
13969           /* Branch counts from the next instruction.  */
13970           i--;
13971           insn |= i;
13972           /* Branch over the jump.  */
13973           md_number_to_chars ((char *)buf, insn, 4);
13974           buf += 4;
13975
13976           /* Nop */
13977           md_number_to_chars ((char*)buf, 0, 4);
13978           buf += 4;
13979
13980           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13981             {
13982               /* beql $0, $0, 2f */
13983               insn = 0x50000000;
13984               /* Compute the PC offset from the current instruction to
13985                  the end of the variable frag.  */
13986               /* How many bytes in instructions we've already emitted?  */
13987               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13988               /* How many bytes in instructions from here to the end?  */
13989               i = fragp->fr_var - i;
13990               /* Convert to instruction count.  */
13991               i >>= 2;
13992               /* Don't decrement i, because we want to branch over the
13993                  delay slot.  */
13994
13995               insn |= i;
13996               md_number_to_chars ((char *)buf, insn, 4);
13997               buf += 4;
13998
13999               md_number_to_chars ((char *)buf, 0, 4);
14000               buf += 4;
14001             }
14002
14003         uncond:
14004           if (mips_pic == NO_PIC)
14005             {
14006               /* j or jal.  */
14007               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14008                       ? 0x0c000000 : 0x08000000);
14009               exp.X_op = O_symbol;
14010               exp.X_add_symbol = fragp->fr_symbol;
14011               exp.X_add_number = fragp->fr_offset;
14012
14013               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14014                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
14015               fixp->fx_file = fragp->fr_file;
14016               fixp->fx_line = fragp->fr_line;
14017
14018               md_number_to_chars ((char*)buf, insn, 4);
14019               buf += 4;
14020             }
14021           else
14022             {
14023               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
14024               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14025               exp.X_op = O_symbol;
14026               exp.X_add_symbol = fragp->fr_symbol;
14027               exp.X_add_number = fragp->fr_offset;
14028
14029               if (fragp->fr_offset)
14030                 {
14031                   exp.X_add_symbol = make_expr_symbol (&exp);
14032                   exp.X_add_number = 0;
14033                 }
14034
14035               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14036                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14037               fixp->fx_file = fragp->fr_file;
14038               fixp->fx_line = fragp->fr_line;
14039
14040               md_number_to_chars ((char*)buf, insn, 4);
14041               buf += 4;
14042
14043               if (mips_opts.isa == ISA_MIPS1)
14044                 {
14045                   /* nop */
14046                   md_number_to_chars ((char*)buf, 0, 4);
14047                   buf += 4;
14048                 }
14049
14050               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
14051               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14052
14053               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14054                                   4, &exp, 0, BFD_RELOC_LO16);
14055               fixp->fx_file = fragp->fr_file;
14056               fixp->fx_line = fragp->fr_line;
14057
14058               md_number_to_chars ((char*)buf, insn, 4);
14059               buf += 4;
14060
14061               /* j(al)r $at.  */
14062               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14063                 insn = 0x0020f809;
14064               else
14065                 insn = 0x00200008;
14066
14067               md_number_to_chars ((char*)buf, insn, 4);
14068               buf += 4;
14069             }
14070         }
14071
14072       assert (buf == (bfd_byte *)fragp->fr_literal
14073               + fragp->fr_fix + fragp->fr_var);
14074
14075       fragp->fr_fix += fragp->fr_var;
14076
14077       return;
14078     }
14079
14080   if (RELAX_MIPS16_P (fragp->fr_subtype))
14081     {
14082       int type;
14083       register const struct mips16_immed_operand *op;
14084       bfd_boolean small, ext;
14085       offsetT val;
14086       bfd_byte *buf;
14087       unsigned long insn;
14088       bfd_boolean use_extend;
14089       unsigned short extend;
14090
14091       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14092       op = mips16_immed_operands;
14093       while (op->type != type)
14094         ++op;
14095
14096       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14097         {
14098           small = FALSE;
14099           ext = TRUE;
14100         }
14101       else
14102         {
14103           small = TRUE;
14104           ext = FALSE;
14105         }
14106
14107       resolve_symbol_value (fragp->fr_symbol);
14108       val = S_GET_VALUE (fragp->fr_symbol);
14109       if (op->pcrel)
14110         {
14111           addressT addr;
14112
14113           addr = fragp->fr_address + fragp->fr_fix;
14114
14115           /* The rules for the base address of a PC relative reloc are
14116              complicated; see mips16_extended_frag.  */
14117           if (type == 'p' || type == 'q')
14118             {
14119               addr += 2;
14120               if (ext)
14121                 addr += 2;
14122               /* Ignore the low bit in the target, since it will be
14123                  set for a text label.  */
14124               if ((val & 1) != 0)
14125                 --val;
14126             }
14127           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14128             addr -= 4;
14129           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14130             addr -= 2;
14131
14132           addr &= ~ (addressT) ((1 << op->shift) - 1);
14133           val -= addr;
14134
14135           /* Make sure the section winds up with the alignment we have
14136              assumed.  */
14137           if (op->shift > 0)
14138             record_alignment (asec, op->shift);
14139         }
14140
14141       if (ext
14142           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14143               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14144         as_warn_where (fragp->fr_file, fragp->fr_line,
14145                        _("extended instruction in delay slot"));
14146
14147       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14148
14149       if (target_big_endian)
14150         insn = bfd_getb16 (buf);
14151       else
14152         insn = bfd_getl16 (buf);
14153
14154       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14155                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14156                     small, ext, &insn, &use_extend, &extend);
14157
14158       if (use_extend)
14159         {
14160           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
14161           fragp->fr_fix += 2;
14162           buf += 2;
14163         }
14164
14165       md_number_to_chars ((char *) buf, insn, 2);
14166       fragp->fr_fix += 2;
14167       buf += 2;
14168     }
14169   else
14170     {
14171       if (fragp->fr_opcode == NULL)
14172         return;
14173
14174       old = RELAX_OLD (fragp->fr_subtype);
14175       new = RELAX_NEW (fragp->fr_subtype);
14176       fixptr = fragp->fr_literal + fragp->fr_fix;
14177
14178       if (new > 0)
14179         memmove (fixptr - old, fixptr, new);
14180
14181       fragp->fr_fix += new - old;
14182     }
14183 }
14184
14185 #ifdef OBJ_ELF
14186
14187 /* This function is called after the relocs have been generated.
14188    We've been storing mips16 text labels as odd.  Here we convert them
14189    back to even for the convenience of the debugger.  */
14190
14191 void
14192 mips_frob_file_after_relocs ()
14193 {
14194   asymbol **syms;
14195   unsigned int count, i;
14196
14197   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14198     return;
14199
14200   syms = bfd_get_outsymbols (stdoutput);
14201   count = bfd_get_symcount (stdoutput);
14202   for (i = 0; i < count; i++, syms++)
14203     {
14204       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14205           && ((*syms)->value & 1) != 0)
14206         {
14207           (*syms)->value &= ~1;
14208           /* If the symbol has an odd size, it was probably computed
14209              incorrectly, so adjust that as well.  */
14210           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14211             ++elf_symbol (*syms)->internal_elf_sym.st_size;
14212         }
14213     }
14214 }
14215
14216 #endif
14217
14218 /* This function is called whenever a label is defined.  It is used
14219    when handling branch delays; if a branch has a label, we assume we
14220    can not move it.  */
14221
14222 void
14223 mips_define_label (sym)
14224      symbolS *sym;
14225 {
14226   struct insn_label_list *l;
14227
14228   if (free_insn_labels == NULL)
14229     l = (struct insn_label_list *) xmalloc (sizeof *l);
14230   else
14231     {
14232       l = free_insn_labels;
14233       free_insn_labels = l->next;
14234     }
14235
14236   l->label = sym;
14237   l->next = insn_labels;
14238   insn_labels = l;
14239 }
14240 \f
14241 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14242
14243 /* Some special processing for a MIPS ELF file.  */
14244
14245 void
14246 mips_elf_final_processing ()
14247 {
14248   /* Write out the register information.  */
14249   if (mips_abi != N64_ABI)
14250     {
14251       Elf32_RegInfo s;
14252
14253       s.ri_gprmask = mips_gprmask;
14254       s.ri_cprmask[0] = mips_cprmask[0];
14255       s.ri_cprmask[1] = mips_cprmask[1];
14256       s.ri_cprmask[2] = mips_cprmask[2];
14257       s.ri_cprmask[3] = mips_cprmask[3];
14258       /* The gp_value field is set by the MIPS ELF backend.  */
14259
14260       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14261                                        ((Elf32_External_RegInfo *)
14262                                         mips_regmask_frag));
14263     }
14264   else
14265     {
14266       Elf64_Internal_RegInfo s;
14267
14268       s.ri_gprmask = mips_gprmask;
14269       s.ri_pad = 0;
14270       s.ri_cprmask[0] = mips_cprmask[0];
14271       s.ri_cprmask[1] = mips_cprmask[1];
14272       s.ri_cprmask[2] = mips_cprmask[2];
14273       s.ri_cprmask[3] = mips_cprmask[3];
14274       /* The gp_value field is set by the MIPS ELF backend.  */
14275
14276       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14277                                        ((Elf64_External_RegInfo *)
14278                                         mips_regmask_frag));
14279     }
14280
14281   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14282      sort of BFD interface for this.  */
14283   if (mips_any_noreorder)
14284     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14285   if (mips_pic != NO_PIC)
14286     {
14287     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14288       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14289     }
14290   if (mips_abicalls)
14291     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14292
14293   /* Set MIPS ELF flags for ASEs.  */
14294   if (file_ase_mips16)
14295     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14296 #if 0 /* XXX FIXME */
14297   if (file_ase_mips3d)
14298     elf_elfheader (stdoutput)->e_flags |= ???;
14299 #endif
14300   if (file_ase_mdmx)
14301     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14302
14303   /* Set the MIPS ELF ABI flags.  */
14304   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14305     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14306   else if (mips_abi == O64_ABI)
14307     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14308   else if (mips_abi == EABI_ABI)
14309     {
14310       if (!file_mips_gp32)
14311         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14312       else
14313         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14314     }
14315   else if (mips_abi == N32_ABI)
14316     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14317
14318   /* Nothing to do for N64_ABI.  */
14319
14320   if (mips_32bitmode)
14321     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14322 }
14323
14324 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14325 \f
14326 typedef struct proc {
14327   symbolS *isym;
14328   unsigned long reg_mask;
14329   unsigned long reg_offset;
14330   unsigned long fpreg_mask;
14331   unsigned long fpreg_offset;
14332   unsigned long frame_offset;
14333   unsigned long frame_reg;
14334   unsigned long pc_reg;
14335 } procS;
14336
14337 static procS cur_proc;
14338 static procS *cur_proc_ptr;
14339 static int numprocs;
14340
14341 /* Fill in an rs_align_code fragment.  */
14342
14343 void
14344 mips_handle_align (fragp)
14345      fragS *fragp;
14346 {
14347   if (fragp->fr_type != rs_align_code)
14348     return;
14349
14350   if (mips_opts.mips16)
14351     {
14352       static const unsigned char be_nop[] = { 0x65, 0x00 };
14353       static const unsigned char le_nop[] = { 0x00, 0x65 };
14354
14355       int bytes;
14356       char *p;
14357
14358       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14359       p = fragp->fr_literal + fragp->fr_fix;
14360
14361       if (bytes & 1)
14362         {
14363           *p++ = 0;
14364           fragp->fr_fix++;
14365         }
14366
14367       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14368       fragp->fr_var = 2;
14369     }
14370
14371   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14372 }
14373
14374 static void
14375 md_obj_begin ()
14376 {
14377 }
14378
14379 static void
14380 md_obj_end ()
14381 {
14382   /* check for premature end, nesting errors, etc */
14383   if (cur_proc_ptr)
14384     as_warn (_("missing .end at end of assembly"));
14385 }
14386
14387 static long
14388 get_number ()
14389 {
14390   int negative = 0;
14391   long val = 0;
14392
14393   if (*input_line_pointer == '-')
14394     {
14395       ++input_line_pointer;
14396       negative = 1;
14397     }
14398   if (!ISDIGIT (*input_line_pointer))
14399     as_bad (_("expected simple number"));
14400   if (input_line_pointer[0] == '0')
14401     {
14402       if (input_line_pointer[1] == 'x')
14403         {
14404           input_line_pointer += 2;
14405           while (ISXDIGIT (*input_line_pointer))
14406             {
14407               val <<= 4;
14408               val |= hex_value (*input_line_pointer++);
14409             }
14410           return negative ? -val : val;
14411         }
14412       else
14413         {
14414           ++input_line_pointer;
14415           while (ISDIGIT (*input_line_pointer))
14416             {
14417               val <<= 3;
14418               val |= *input_line_pointer++ - '0';
14419             }
14420           return negative ? -val : val;
14421         }
14422     }
14423   if (!ISDIGIT (*input_line_pointer))
14424     {
14425       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14426               *input_line_pointer, *input_line_pointer);
14427       as_warn (_("invalid number"));
14428       return -1;
14429     }
14430   while (ISDIGIT (*input_line_pointer))
14431     {
14432       val *= 10;
14433       val += *input_line_pointer++ - '0';
14434     }
14435   return negative ? -val : val;
14436 }
14437
14438 /* The .file directive; just like the usual .file directive, but there
14439    is an initial number which is the ECOFF file index.  In the non-ECOFF
14440    case .file implies DWARF-2.  */
14441
14442 static void
14443 s_mips_file (x)
14444      int x ATTRIBUTE_UNUSED;
14445 {
14446   static int first_file_directive = 0;
14447
14448   if (ECOFF_DEBUGGING)
14449     {
14450       get_number ();
14451       s_app_file (0);
14452     }
14453   else
14454     {
14455       char *filename;
14456
14457       filename = dwarf2_directive_file (0);
14458
14459       /* Versions of GCC up to 3.1 start files with a ".file"
14460          directive even for stabs output.  Make sure that this
14461          ".file" is handled.  Note that you need a version of GCC
14462          after 3.1 in order to support DWARF-2 on MIPS.  */
14463       if (filename != NULL && ! first_file_directive)
14464         {
14465           (void) new_logical_line (filename, -1);
14466           s_app_file_string (filename);
14467         }
14468       first_file_directive = 1;
14469     }
14470 }
14471
14472 /* The .loc directive, implying DWARF-2.  */
14473
14474 static void
14475 s_mips_loc (x)
14476      int x ATTRIBUTE_UNUSED;
14477 {
14478   if (!ECOFF_DEBUGGING)
14479     dwarf2_directive_loc (0);
14480 }
14481
14482 /* The .end directive.  */
14483
14484 static void
14485 s_mips_end (x)
14486      int x ATTRIBUTE_UNUSED;
14487 {
14488   symbolS *p;
14489
14490   /* Following functions need their own .frame and .cprestore directives.  */
14491   mips_frame_reg_valid = 0;
14492   mips_cprestore_valid = 0;
14493
14494   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14495     {
14496       p = get_symbol ();
14497       demand_empty_rest_of_line ();
14498     }
14499   else
14500     p = NULL;
14501
14502   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14503     as_warn (_(".end not in text section"));
14504
14505   if (!cur_proc_ptr)
14506     {
14507       as_warn (_(".end directive without a preceding .ent directive."));
14508       demand_empty_rest_of_line ();
14509       return;
14510     }
14511
14512   if (p != NULL)
14513     {
14514       assert (S_GET_NAME (p));
14515       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14516         as_warn (_(".end symbol does not match .ent symbol."));
14517
14518       if (debug_type == DEBUG_STABS)
14519         stabs_generate_asm_endfunc (S_GET_NAME (p),
14520                                     S_GET_NAME (p));
14521     }
14522   else
14523     as_warn (_(".end directive missing or unknown symbol"));
14524
14525 #ifdef OBJ_ELF
14526   /* Generate a .pdr section.  */
14527   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14528     {
14529       segT saved_seg = now_seg;
14530       subsegT saved_subseg = now_subseg;
14531       valueT dot;
14532       expressionS exp;
14533       char *fragp;
14534
14535       dot = frag_now_fix ();
14536
14537 #ifdef md_flush_pending_output
14538       md_flush_pending_output ();
14539 #endif
14540
14541       assert (pdr_seg);
14542       subseg_set (pdr_seg, 0);
14543
14544       /* Write the symbol.  */
14545       exp.X_op = O_symbol;
14546       exp.X_add_symbol = p;
14547       exp.X_add_number = 0;
14548       emit_expr (&exp, 4);
14549
14550       fragp = frag_more (7 * 4);
14551
14552       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14553       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14554       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14555       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14556       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14557       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14558       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14559
14560       subseg_set (saved_seg, saved_subseg);
14561     }
14562 #endif /* OBJ_ELF */
14563
14564   cur_proc_ptr = NULL;
14565 }
14566
14567 /* The .aent and .ent directives.  */
14568
14569 static void
14570 s_mips_ent (aent)
14571      int aent;
14572 {
14573   symbolS *symbolP;
14574
14575   symbolP = get_symbol ();
14576   if (*input_line_pointer == ',')
14577     ++input_line_pointer;
14578   SKIP_WHITESPACE ();
14579   if (ISDIGIT (*input_line_pointer)
14580       || *input_line_pointer == '-')
14581     get_number ();
14582
14583   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14584     as_warn (_(".ent or .aent not in text section."));
14585
14586   if (!aent && cur_proc_ptr)
14587     as_warn (_("missing .end"));
14588
14589   if (!aent)
14590     {
14591       /* This function needs its own .frame and .cprestore directives.  */
14592       mips_frame_reg_valid = 0;
14593       mips_cprestore_valid = 0;
14594
14595       cur_proc_ptr = &cur_proc;
14596       memset (cur_proc_ptr, '\0', sizeof (procS));
14597
14598       cur_proc_ptr->isym = symbolP;
14599
14600       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14601
14602       ++numprocs;
14603
14604       if (debug_type == DEBUG_STABS)
14605         stabs_generate_asm_func (S_GET_NAME (symbolP),
14606                                  S_GET_NAME (symbolP));
14607     }
14608
14609   demand_empty_rest_of_line ();
14610 }
14611
14612 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14613    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14614    s_mips_frame is used so that we can set the PDR information correctly.
14615    We can't use the ecoff routines because they make reference to the ecoff
14616    symbol table (in the mdebug section).  */
14617
14618 static void
14619 s_mips_frame (ignore)
14620      int ignore ATTRIBUTE_UNUSED;
14621 {
14622 #ifdef OBJ_ELF
14623   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14624     {
14625       long val;
14626
14627       if (cur_proc_ptr == (procS *) NULL)
14628         {
14629           as_warn (_(".frame outside of .ent"));
14630           demand_empty_rest_of_line ();
14631           return;
14632         }
14633
14634       cur_proc_ptr->frame_reg = tc_get_register (1);
14635
14636       SKIP_WHITESPACE ();
14637       if (*input_line_pointer++ != ','
14638           || get_absolute_expression_and_terminator (&val) != ',')
14639         {
14640           as_warn (_("Bad .frame directive"));
14641           --input_line_pointer;
14642           demand_empty_rest_of_line ();
14643           return;
14644         }
14645
14646       cur_proc_ptr->frame_offset = val;
14647       cur_proc_ptr->pc_reg = tc_get_register (0);
14648
14649       demand_empty_rest_of_line ();
14650     }
14651   else
14652 #endif /* OBJ_ELF */
14653     s_ignore (ignore);
14654 }
14655
14656 /* The .fmask and .mask directives. If the mdebug section is present
14657    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14658    embedded targets, s_mips_mask is used so that we can set the PDR
14659    information correctly. We can't use the ecoff routines because they
14660    make reference to the ecoff symbol table (in the mdebug section).  */
14661
14662 static void
14663 s_mips_mask (reg_type)
14664      char reg_type;
14665 {
14666 #ifdef OBJ_ELF
14667   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14668     {
14669       long mask, off;
14670
14671       if (cur_proc_ptr == (procS *) NULL)
14672         {
14673           as_warn (_(".mask/.fmask outside of .ent"));
14674           demand_empty_rest_of_line ();
14675           return;
14676         }
14677
14678       if (get_absolute_expression_and_terminator (&mask) != ',')
14679         {
14680           as_warn (_("Bad .mask/.fmask directive"));
14681           --input_line_pointer;
14682           demand_empty_rest_of_line ();
14683           return;
14684         }
14685
14686       off = get_absolute_expression ();
14687
14688       if (reg_type == 'F')
14689         {
14690           cur_proc_ptr->fpreg_mask = mask;
14691           cur_proc_ptr->fpreg_offset = off;
14692         }
14693       else
14694         {
14695           cur_proc_ptr->reg_mask = mask;
14696           cur_proc_ptr->reg_offset = off;
14697         }
14698
14699       demand_empty_rest_of_line ();
14700     }
14701   else
14702 #endif /* OBJ_ELF */
14703     s_ignore (reg_type);
14704 }
14705
14706 /* The .loc directive.  */
14707
14708 #if 0
14709 static void
14710 s_loc (x)
14711      int x;
14712 {
14713   symbolS *symbolP;
14714   int lineno;
14715   int addroff;
14716
14717   assert (now_seg == text_section);
14718
14719   lineno = get_number ();
14720   addroff = frag_now_fix ();
14721
14722   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14723   S_SET_TYPE (symbolP, N_SLINE);
14724   S_SET_OTHER (symbolP, 0);
14725   S_SET_DESC (symbolP, lineno);
14726   symbolP->sy_segment = now_seg;
14727 }
14728 #endif
14729
14730 /* A table describing all the processors gas knows about.  Names are
14731    matched in the order listed.
14732
14733    To ease comparison, please keep this table in the same order as
14734    gcc's mips_cpu_info_table[].  */
14735 static const struct mips_cpu_info mips_cpu_info_table[] =
14736 {
14737   /* Entries for generic ISAs */
14738   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14739   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14740   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14741   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14742   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14743   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14744   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14745   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14746
14747   /* MIPS I */
14748   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14749   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14750   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14751
14752   /* MIPS II */
14753   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14754
14755   /* MIPS III */
14756   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14757   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14758   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14759   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14760   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14761   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14762   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14763   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14764   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14765   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14766   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14767   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14768
14769   /* MIPS IV */
14770   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14771   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14772   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14773   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14774   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14775   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14776   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14777   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14778   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14779   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14780   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14781   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14782
14783   /* MIPS 32 */
14784   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32 },
14785   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14786   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14787
14788   /* MIPS 64 */
14789   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14790   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14791
14792   /* Broadcom SB-1 CPU core */
14793   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14794
14795   /* End marker */
14796   { NULL, 0, 0, 0 }
14797 };
14798
14799
14800 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14801    with a final "000" replaced by "k".  Ignore case.
14802
14803    Note: this function is shared between GCC and GAS.  */
14804
14805 static bfd_boolean
14806 mips_strict_matching_cpu_name_p (canonical, given)
14807      const char *canonical, *given;
14808 {
14809   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14810     given++, canonical++;
14811
14812   return ((*given == 0 && *canonical == 0)
14813           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14814 }
14815
14816
14817 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14818    CPU name.  We've traditionally allowed a lot of variation here.
14819
14820    Note: this function is shared between GCC and GAS.  */
14821
14822 static bfd_boolean
14823 mips_matching_cpu_name_p (canonical, given)
14824      const char *canonical, *given;
14825 {
14826   /* First see if the name matches exactly, or with a final "000"
14827      turned into "k".  */
14828   if (mips_strict_matching_cpu_name_p (canonical, given))
14829     return TRUE;
14830
14831   /* If not, try comparing based on numerical designation alone.
14832      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14833   if (TOLOWER (*given) == 'r')
14834     given++;
14835   if (!ISDIGIT (*given))
14836     return FALSE;
14837
14838   /* Skip over some well-known prefixes in the canonical name,
14839      hoping to find a number there too.  */
14840   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14841     canonical += 2;
14842   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14843     canonical += 2;
14844   else if (TOLOWER (canonical[0]) == 'r')
14845     canonical += 1;
14846
14847   return mips_strict_matching_cpu_name_p (canonical, given);
14848 }
14849
14850
14851 /* Parse an option that takes the name of a processor as its argument.
14852    OPTION is the name of the option and CPU_STRING is the argument.
14853    Return the corresponding processor enumeration if the CPU_STRING is
14854    recognized, otherwise report an error and return null.
14855
14856    A similar function exists in GCC.  */
14857
14858 static const struct mips_cpu_info *
14859 mips_parse_cpu (option, cpu_string)
14860      const char *option, *cpu_string;
14861 {
14862   const struct mips_cpu_info *p;
14863
14864   /* 'from-abi' selects the most compatible architecture for the given
14865      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14866      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14867      version.  Look first at the -mgp options, if given, otherwise base
14868      the choice on MIPS_DEFAULT_64BIT.
14869
14870      Treat NO_ABI like the EABIs.  One reason to do this is that the
14871      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14872      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14873      'mips64', just as we did in the days before 'from-abi'.  */
14874   if (strcasecmp (cpu_string, "from-abi") == 0)
14875     {
14876       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14877         return mips_cpu_info_from_isa (ISA_MIPS1);
14878
14879       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14880         return mips_cpu_info_from_isa (ISA_MIPS3);
14881
14882       if (file_mips_gp32 >= 0)
14883         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14884
14885       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14886                                      ? ISA_MIPS3
14887                                      : ISA_MIPS1);
14888     }
14889
14890   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14891   if (strcasecmp (cpu_string, "default") == 0)
14892     return 0;
14893
14894   for (p = mips_cpu_info_table; p->name != 0; p++)
14895     if (mips_matching_cpu_name_p (p->name, cpu_string))
14896       return p;
14897
14898   as_bad ("Bad value (%s) for %s", cpu_string, option);
14899   return 0;
14900 }
14901
14902 /* Return the canonical processor information for ISA (a member of the
14903    ISA_MIPS* enumeration).  */
14904
14905 static const struct mips_cpu_info *
14906 mips_cpu_info_from_isa (isa)
14907      int isa;
14908 {
14909   int i;
14910
14911   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14912     if (mips_cpu_info_table[i].is_isa
14913         && isa == mips_cpu_info_table[i].isa)
14914       return (&mips_cpu_info_table[i]);
14915
14916   return NULL;
14917 }
14918
14919 static const struct mips_cpu_info *
14920 mips_cpu_info_from_arch (arch)
14921      int arch;
14922 {
14923   int i;
14924
14925   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14926     if (arch == mips_cpu_info_table[i].cpu)
14927       return (&mips_cpu_info_table[i]);
14928
14929   return NULL;
14930 }
14931 \f
14932 static void
14933 show (stream, string, col_p, first_p)
14934      FILE *stream;
14935      const char *string;
14936      int *col_p;
14937      int *first_p;
14938 {
14939   if (*first_p)
14940     {
14941       fprintf (stream, "%24s", "");
14942       *col_p = 24;
14943     }
14944   else
14945     {
14946       fprintf (stream, ", ");
14947       *col_p += 2;
14948     }
14949
14950   if (*col_p + strlen (string) > 72)
14951     {
14952       fprintf (stream, "\n%24s", "");
14953       *col_p = 24;
14954     }
14955
14956   fprintf (stream, "%s", string);
14957   *col_p += strlen (string);
14958
14959   *first_p = 0;
14960 }
14961
14962 void
14963 md_show_usage (stream)
14964      FILE *stream;
14965 {
14966   int column, first;
14967   size_t i;
14968
14969   fprintf (stream, _("\
14970 MIPS options:\n\
14971 -membedded-pic          generate embedded position independent code\n\
14972 -EB                     generate big endian output\n\
14973 -EL                     generate little endian output\n\
14974 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14975 -G NUM                  allow referencing objects up to NUM bytes\n\
14976                         implicitly with the gp register [default 8]\n"));
14977   fprintf (stream, _("\
14978 -mips1                  generate MIPS ISA I instructions\n\
14979 -mips2                  generate MIPS ISA II instructions\n\
14980 -mips3                  generate MIPS ISA III instructions\n\
14981 -mips4                  generate MIPS ISA IV instructions\n\
14982 -mips5                  generate MIPS ISA V instructions\n\
14983 -mips32                 generate MIPS32 ISA instructions\n\
14984 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14985 -mips64                 generate MIPS64 ISA instructions\n\
14986 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14987
14988   first = 1;
14989
14990   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14991     show (stream, mips_cpu_info_table[i].name, &column, &first);
14992   show (stream, "from-abi", &column, &first);
14993   fputc ('\n', stream);
14994
14995   fprintf (stream, _("\
14996 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14997 -no-mCPU                don't generate code specific to CPU.\n\
14998                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14999
15000   first = 1;
15001
15002   show (stream, "3900", &column, &first);
15003   show (stream, "4010", &column, &first);
15004   show (stream, "4100", &column, &first);
15005   show (stream, "4650", &column, &first);
15006   fputc ('\n', stream);
15007
15008   fprintf (stream, _("\
15009 -mips16                 generate mips16 instructions\n\
15010 -no-mips16              do not generate mips16 instructions\n"));
15011   fprintf (stream, _("\
15012 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
15013 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
15014 -O0                     remove unneeded NOPs, do not swap branches\n\
15015 -O                      remove unneeded NOPs and swap branches\n\
15016 -n                      warn about NOPs generated from macros\n\
15017 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
15018 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
15019 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
15020 #ifdef OBJ_ELF
15021   fprintf (stream, _("\
15022 -KPIC, -call_shared     generate SVR4 position independent code\n\
15023 -non_shared             do not generate position independent code\n\
15024 -xgot                   assume a 32 bit GOT\n\
15025 -mabi=ABI               create ABI conformant object file for:\n"));
15026
15027   first = 1;
15028
15029   show (stream, "32", &column, &first);
15030   show (stream, "o64", &column, &first);
15031   show (stream, "n32", &column, &first);
15032   show (stream, "64", &column, &first);
15033   show (stream, "eabi", &column, &first);
15034
15035   fputc ('\n', stream);
15036
15037   fprintf (stream, _("\
15038 -32                     create o32 ABI object file (default)\n\
15039 -n32                    create n32 ABI object file\n\
15040 -64                     create 64 ABI object file\n"));
15041 #endif
15042 }
15043
15044 enum dwarf2_format
15045 mips_dwarf2_format ()
15046 {
15047   if (mips_abi == N64_ABI)
15048     {
15049 #ifdef TE_IRIX
15050       return dwarf2_format_64bit_irix;
15051 #else
15052       return dwarf2_format_64bit;
15053 #endif
15054     }
15055   else
15056     return dwarf2_format_32bit;
15057 }