gas/
[external/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 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 /* This is the set of options which may be modified by the .set
135    pseudo-op.  We use a struct so that .set push and .set pop are more
136    reliable.  */
137
138 struct mips_set_options
139 {
140   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
141      if it has not been initialized.  Changed by `.set mipsN', and the
142      -mipsN command line option, and the default CPU.  */
143   int isa;
144   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
145      if they have not been initialized.  Changed by `.set <asename>', by
146      command line options, and based on the default architecture.  */
147   int ase_mips3d;
148   int ase_mdmx;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175   /* Restrict general purpose registers and floating point registers
176      to 32 bit.  This is initially determined when -mgp32 or -mfp32
177      is passed but can changed if the assembler code uses .set mipsN.  */
178   int gp32;
179   int fp32;
180 };
181
182 /* True if -mgp32 was passed.  */
183 static int file_mips_gp32 = -1;
184
185 /* True if -mfp32 was passed.  */
186 static int file_mips_fp32 = -1;
187
188 /* This is the struct we use to hold the current set of options.  Note
189    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
190    -1 to indicate that they have not been initialized.  */
191
192 static struct mips_set_options mips_opts =
193 {
194   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
195 };
196
197 /* These variables are filled in with the masks of registers used.
198    The object format code reads them and puts them in the appropriate
199    place.  */
200 unsigned long mips_gprmask;
201 unsigned long mips_cprmask[4];
202
203 /* MIPS ISA we are using for this output file.  */
204 static int file_mips_isa = ISA_UNKNOWN;
205
206 /* True if -mips16 was passed or implied by arguments passed on the
207    command line (e.g., by -march).  */
208 static int file_ase_mips16;
209
210 /* True if -mips3d was passed or implied by arguments passed on the
211    command line (e.g., by -march).  */
212 static int file_ase_mips3d;
213
214 /* True if -mdmx was passed or implied by arguments passed on the
215    command line (e.g., by -march).  */
216 static int file_ase_mdmx;
217
218 /* The argument of the -march= flag.  The architecture we are assembling.  */
219 static int mips_arch = CPU_UNKNOWN;
220 static const char *mips_arch_string;
221 static const struct mips_cpu_info *mips_arch_info;
222
223 /* The argument of the -mtune= flag.  The architecture for which we
224    are optimizing.  */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227 static const struct mips_cpu_info *mips_tune_info;
228
229 /* True when generating 32-bit code for a 64-bit processor.  */
230 static int mips_32bitmode = 0;
231
232 /* Some ISA's have delay slots for instructions which read or write
233    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
234    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
235    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236    delay slot in this ISA.  The uses of this macro assume that any
237    ISA that has delay slots for one of these, has them for all.  They
238    also assume that ISAs which don't have delays for these insns, don't
239    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
240 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
241    (ISA) == ISA_MIPS1                       \
242    || (ISA) == ISA_MIPS2                    \
243    || (ISA) == ISA_MIPS3                    \
244    )
245
246 /* True if the given ABI requires 32-bit registers.  */
247 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249 /* Likewise 64-bit registers.  */
250 #define ABI_NEEDS_64BIT_REGS(ABI) \
251   ((ABI) == N32_ABI               \
252    || (ABI) == N64_ABI            \
253    || (ABI) == O64_ABI)
254
255 /*  Return true if ISA supports 64 bit gp register instructions.  */
256 #define ISA_HAS_64BIT_REGS(ISA) (    \
257    (ISA) == ISA_MIPS3                \
258    || (ISA) == ISA_MIPS4             \
259    || (ISA) == ISA_MIPS5             \
260    || (ISA) == ISA_MIPS64            \
261    )
262
263 /* Return true if ISA supports 64-bit right rotate (dror et al.)
264    instructions.  */
265 #define ISA_HAS_DROR(ISA) (     \
266    0                            \
267    )
268
269 /* Return true if ISA supports 32-bit right rotate (ror et al.)
270    instructions.  */
271 #define ISA_HAS_ROR(ISA) (      \
272    (ISA) == ISA_MIPS32R2        \
273    )
274
275 #define HAVE_32BIT_GPRS                            \
276     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
277
278 #define HAVE_32BIT_FPRS                            \
279     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
284 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
285
286 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
287
288 /* We can only have 64bit addresses if the object file format
289    supports it.  */
290 #define HAVE_32BIT_ADDRESSES                           \
291    (HAVE_32BIT_GPRS                                    \
292     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
293          || ! HAVE_64BIT_OBJECTS)                      \
294         && mips_pic != EMBEDDED_PIC))
295
296 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
297
298 /* Return true if the given CPU supports the MIPS16 ASE.  */
299 #define CPU_HAS_MIPS16(cpu)                                             \
300    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
301     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
302
303 /* Return true if the given CPU supports the MIPS3D ASE.  */
304 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
305                                  )
306
307 /* Return true if the given CPU supports the MDMX ASE.  */
308 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
309                                  )
310
311 /* True if CPU has a dror instruction.  */
312 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
313
314 /* True if CPU has a ror instruction.  */
315 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
316
317 /* Whether the processor uses hardware interlocks to protect
318    reads from the HI and LO registers, and thus does not
319    require nops to be inserted.  */
320
321 #define hilo_interlocks (mips_arch == CPU_R4010                       \
322                          || mips_arch == CPU_VR5500                   \
323                          || mips_arch == CPU_SB1                      \
324                          )
325
326 /* Whether the processor uses hardware interlocks to protect reads
327    from the GPRs, and thus does not require nops to be inserted.  */
328 #define gpr_interlocks \
329   (mips_opts.isa != ISA_MIPS1  \
330    || mips_arch == CPU_VR5400  \
331    || mips_arch == CPU_VR5500  \
332    || mips_arch == CPU_R3900)
333
334 /* As with other "interlocks" this is used by hardware that has FP
335    (co-processor) interlocks.  */
336 /* Itbl support may require additional care here.  */
337 #define cop_interlocks (mips_arch == CPU_R4300                        \
338                         || mips_arch == CPU_VR5400                    \
339                         || mips_arch == CPU_VR5500                    \
340                         || mips_arch == CPU_SB1                       \
341                         )
342
343 /* Is this a mfhi or mflo instruction?  */
344 #define MF_HILO_INSN(PINFO) \
345           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
346
347 /* MIPS PIC level.  */
348
349 enum mips_pic_level mips_pic;
350
351 /* Warn about all NOPS that the assembler generates.  */
352 static int warn_nops = 0;
353
354 /* 1 if we should generate 32 bit offsets from the $gp register in
355    SVR4_PIC mode.  Currently has no meaning in other modes.  */
356 static int mips_big_got = 0;
357
358 /* 1 if trap instructions should used for overflow rather than break
359    instructions.  */
360 static int mips_trap = 0;
361
362 /* 1 if double width floating point constants should not be constructed
363    by assembling two single width halves into two single width floating
364    point registers which just happen to alias the double width destination
365    register.  On some architectures this aliasing can be disabled by a bit
366    in the status register, and the setting of this bit cannot be determined
367    automatically at assemble time.  */
368 static int mips_disable_float_construction;
369
370 /* Non-zero if any .set noreorder directives were used.  */
371
372 static int mips_any_noreorder;
373
374 /* Non-zero if nops should be inserted when the register referenced in
375    an mfhi/mflo instruction is read in the next two instructions.  */
376 static int mips_7000_hilo_fix;
377
378 /* The size of the small data section.  */
379 static unsigned int g_switch_value = 8;
380 /* Whether the -G option was used.  */
381 static int g_switch_seen = 0;
382
383 #define N_RMASK 0xc4
384 #define N_VFP   0xd4
385
386 /* If we can determine in advance that GP optimization won't be
387    possible, we can skip the relaxation stuff that tries to produce
388    GP-relative references.  This makes delay slot optimization work
389    better.
390
391    This function can only provide a guess, but it seems to work for
392    gcc output.  It needs to guess right for gcc, otherwise gcc
393    will put what it thinks is a GP-relative instruction in a branch
394    delay slot.
395
396    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
397    fixed it for the non-PIC mode.  KR 95/04/07  */
398 static int nopic_need_relax PARAMS ((symbolS *, int));
399
400 /* handle of the OPCODE hash table */
401 static struct hash_control *op_hash = NULL;
402
403 /* The opcode hash table we use for the mips16.  */
404 static struct hash_control *mips16_op_hash = NULL;
405
406 /* This array holds the chars that always start a comment.  If the
407     pre-processor is disabled, these aren't very useful */
408 const char comment_chars[] = "#";
409
410 /* This array holds the chars that only start a comment at the beginning of
411    a line.  If the line seems to have the form '# 123 filename'
412    .line and .file directives will appear in the pre-processed output */
413 /* Note that input_file.c hand checks for '#' at the beginning of the
414    first line of the input file.  This is because the compiler outputs
415    #NO_APP at the beginning of its output.  */
416 /* Also note that C style comments are always supported.  */
417 const char line_comment_chars[] = "#";
418
419 /* This array holds machine specific line separator characters.  */
420 const char line_separator_chars[] = ";";
421
422 /* Chars that can be used to separate mant from exp in floating point nums */
423 const char EXP_CHARS[] = "eE";
424
425 /* Chars that mean this number is a floating point constant */
426 /* As in 0f12.456 */
427 /* or    0d1.2345e12 */
428 const char FLT_CHARS[] = "rRsSfFdDxXpP";
429
430 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
431    changed in read.c .  Ideally it shouldn't have to know about it at all,
432    but nothing is ideal around here.
433  */
434
435 static char *insn_error;
436
437 static int auto_align = 1;
438
439 /* When outputting SVR4 PIC code, the assembler needs to know the
440    offset in the stack frame from which to restore the $gp register.
441    This is set by the .cprestore pseudo-op, and saved in this
442    variable.  */
443 static offsetT mips_cprestore_offset = -1;
444
445 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
446    more optimizations, it can use a register value instead of a memory-saved
447    offset and even an other register than $gp as global pointer.  */
448 static offsetT mips_cpreturn_offset = -1;
449 static int mips_cpreturn_register = -1;
450 static int mips_gp_register = GP;
451 static int mips_gprel_offset = 0;
452
453 /* Whether mips_cprestore_offset has been set in the current function
454    (or whether it has already been warned about, if not).  */
455 static int mips_cprestore_valid = 0;
456
457 /* This is the register which holds the stack frame, as set by the
458    .frame pseudo-op.  This is needed to implement .cprestore.  */
459 static int mips_frame_reg = SP;
460
461 /* Whether mips_frame_reg has been set in the current function
462    (or whether it has already been warned about, if not).  */
463 static int mips_frame_reg_valid = 0;
464
465 /* To output NOP instructions correctly, we need to keep information
466    about the previous two instructions.  */
467
468 /* Whether we are optimizing.  The default value of 2 means to remove
469    unneeded NOPs and swap branch instructions when possible.  A value
470    of 1 means to not swap branches.  A value of 0 means to always
471    insert NOPs.  */
472 static int mips_optimize = 2;
473
474 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
475    equivalent to seeing no -g option at all.  */
476 static int mips_debug = 0;
477
478 /* The previous instruction.  */
479 static struct mips_cl_insn prev_insn;
480
481 /* The instruction before prev_insn.  */
482 static struct mips_cl_insn prev_prev_insn;
483
484 /* If we don't want information for prev_insn or prev_prev_insn, we
485    point the insn_mo field at this dummy integer.  */
486 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
487
488 /* Non-zero if prev_insn is valid.  */
489 static int prev_insn_valid;
490
491 /* The frag for the previous instruction.  */
492 static struct frag *prev_insn_frag;
493
494 /* The offset into prev_insn_frag for the previous instruction.  */
495 static long prev_insn_where;
496
497 /* The reloc type for the previous instruction, if any.  */
498 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
499
500 /* The reloc for the previous instruction, if any.  */
501 static fixS *prev_insn_fixp[3];
502
503 /* Non-zero if the previous instruction was in a delay slot.  */
504 static int prev_insn_is_delay_slot;
505
506 /* Non-zero if the previous instruction was in a .set noreorder.  */
507 static int prev_insn_unreordered;
508
509 /* Non-zero if the previous instruction uses an extend opcode (if
510    mips16).  */
511 static int prev_insn_extended;
512
513 /* Non-zero if the previous previous instruction was in a .set
514    noreorder.  */
515 static int prev_prev_insn_unreordered;
516
517 /* If this is set, it points to a frag holding nop instructions which
518    were inserted before the start of a noreorder section.  If those
519    nops turn out to be unnecessary, the size of the frag can be
520    decreased.  */
521 static fragS *prev_nop_frag;
522
523 /* The number of nop instructions we created in prev_nop_frag.  */
524 static int prev_nop_frag_holds;
525
526 /* The number of nop instructions that we know we need in
527    prev_nop_frag.  */
528 static int prev_nop_frag_required;
529
530 /* The number of instructions we've seen since prev_nop_frag.  */
531 static int prev_nop_frag_since;
532
533 /* For ECOFF and ELF, relocations against symbols are done in two
534    parts, with a HI relocation and a LO relocation.  Each relocation
535    has only 16 bits of space to store an addend.  This means that in
536    order for the linker to handle carries correctly, it must be able
537    to locate both the HI and the LO relocation.  This means that the
538    relocations must appear in order in the relocation table.
539
540    In order to implement this, we keep track of each unmatched HI
541    relocation.  We then sort them so that they immediately precede the
542    corresponding LO relocation.  */
543
544 struct mips_hi_fixup
545 {
546   /* Next HI fixup.  */
547   struct mips_hi_fixup *next;
548   /* This fixup.  */
549   fixS *fixp;
550   /* The section this fixup is in.  */
551   segT seg;
552 };
553
554 /* The list of unmatched HI relocs.  */
555
556 static struct mips_hi_fixup *mips_hi_fixup_list;
557
558 /* Map normal MIPS register numbers to mips16 register numbers.  */
559
560 #define X ILLEGAL_REG
561 static const int mips32_to_16_reg_map[] =
562 {
563   X, X, 2, 3, 4, 5, 6, 7,
564   X, X, X, X, X, X, X, X,
565   0, 1, X, X, X, X, X, X,
566   X, X, X, X, X, X, X, X
567 };
568 #undef X
569
570 /* Map mips16 register numbers to normal MIPS register numbers.  */
571
572 static const unsigned int mips16_to_32_reg_map[] =
573 {
574   16, 17, 2, 3, 4, 5, 6, 7
575 };
576
577 static int mips_fix_4122_bugs;
578
579 /* We don't relax branches by default, since this causes us to expand
580    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
581    fail to compute the offset before expanding the macro to the most
582    efficient expansion.  */
583
584 static int mips_relax_branch;
585 \f
586 /* Since the MIPS does not have multiple forms of PC relative
587    instructions, we do not have to do relaxing as is done on other
588    platforms.  However, we do have to handle GP relative addressing
589    correctly, which turns out to be a similar problem.
590
591    Every macro that refers to a symbol can occur in (at least) two
592    forms, one with GP relative addressing and one without.  For
593    example, loading a global variable into a register generally uses
594    a macro instruction like this:
595      lw $4,i
596    If i can be addressed off the GP register (this is true if it is in
597    the .sbss or .sdata section, or if it is known to be smaller than
598    the -G argument) this will generate the following instruction:
599      lw $4,i($gp)
600    This instruction will use a GPREL reloc.  If i can not be addressed
601    off the GP register, the following instruction sequence will be used:
602      lui $at,i
603      lw $4,i($at)
604    In this case the first instruction will have a HI16 reloc, and the
605    second reloc will have a LO16 reloc.  Both relocs will be against
606    the symbol i.
607
608    The issue here is that we may not know whether i is GP addressable
609    until after we see the instruction that uses it.  Therefore, we
610    want to be able to choose the final instruction sequence only at
611    the end of the assembly.  This is similar to the way other
612    platforms choose the size of a PC relative instruction only at the
613    end of assembly.
614
615    When generating position independent code we do not use GP
616    addressing in quite the same way, but the issue still arises as
617    external symbols and local symbols must be handled differently.
618
619    We handle these issues by actually generating both possible
620    instruction sequences.  The longer one is put in a frag_var with
621    type rs_machine_dependent.  We encode what to do with the frag in
622    the subtype field.  We encode (1) the number of existing bytes to
623    replace, (2) the number of new bytes to use, (3) the offset from
624    the start of the existing bytes to the first reloc we must generate
625    (that is, the offset is applied from the start of the existing
626    bytes after they are replaced by the new bytes, if any), (4) the
627    offset from the start of the existing bytes to the second reloc,
628    (5) whether a third reloc is needed (the third reloc is always four
629    bytes after the second reloc), and (6) whether to warn if this
630    variant is used (this is sometimes needed if .set nomacro or .set
631    noat is in effect).  All these numbers are reasonably small.
632
633    Generating two instruction sequences must be handled carefully to
634    ensure that delay slots are handled correctly.  Fortunately, there
635    are a limited number of cases.  When the second instruction
636    sequence is generated, append_insn is directed to maintain the
637    existing delay slot information, so it continues to apply to any
638    code after the second instruction sequence.  This means that the
639    second instruction sequence must not impose any requirements not
640    required by the first instruction sequence.
641
642    These variant frags are then handled in functions called by the
643    machine independent code.  md_estimate_size_before_relax returns
644    the final size of the frag.  md_convert_frag sets up the final form
645    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
646    one if needed.  */
647 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
648   ((relax_substateT) \
649    (((old) << 23) \
650     | ((new) << 16) \
651     | (((reloc1) + 64) << 9) \
652     | (((reloc2) + 64) << 2) \
653     | ((reloc3) ? (1 << 1) : 0) \
654     | ((warn) ? 1 : 0)))
655 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
656 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
657 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
658 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
659 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
660 #define RELAX_WARN(i) ((i) & 1)
661
662 /* Branch without likely bit.  If label is out of range, we turn:
663
664         beq reg1, reg2, label
665         delay slot
666
667    into
668
669         bne reg1, reg2, 0f
670         nop
671         j label
672      0: delay slot
673
674    with the following opcode replacements:
675
676         beq <-> bne
677         blez <-> bgtz
678         bltz <-> bgez
679         bc1f <-> bc1t
680
681         bltzal <-> bgezal  (with jal label instead of j label)
682
683    Even though keeping the delay slot instruction in the delay slot of
684    the branch would be more efficient, it would be very tricky to do
685    correctly, because we'd have to introduce a variable frag *after*
686    the delay slot instruction, and expand that instead.  Let's do it
687    the easy way for now, even if the branch-not-taken case now costs
688    one additional instruction.  Out-of-range branches are not supposed
689    to be common, anyway.
690
691    Branch likely.  If label is out of range, we turn:
692
693         beql reg1, reg2, label
694         delay slot (annulled if branch not taken)
695
696    into
697
698         beql reg1, reg2, 1f
699         nop
700         beql $0, $0, 2f
701         nop
702      1: j[al] label
703         delay slot (executed only if branch taken)
704      2:
705
706    It would be possible to generate a shorter sequence by losing the
707    likely bit, generating something like:
708
709         bne reg1, reg2, 0f
710         nop
711         j[al] label
712         delay slot (executed only if branch taken)
713      0:
714
715         beql -> bne
716         bnel -> beq
717         blezl -> bgtz
718         bgtzl -> blez
719         bltzl -> bgez
720         bgezl -> bltz
721         bc1fl -> bc1t
722         bc1tl -> bc1f
723
724         bltzall -> bgezal  (with jal label instead of j label)
725         bgezall -> bltzal  (ditto)
726
727
728    but it's not clear that it would actually improve performance.  */
729 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
730   ((relax_substateT) \
731    (0xc0000000 \
732     | ((toofar) ? 1 : 0) \
733     | ((link) ? 2 : 0) \
734     | ((likely) ? 4 : 0) \
735     | ((uncond) ? 8 : 0)))
736 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
737 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
738 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
739 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
740 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
741
742 /* For mips16 code, we use an entirely different form of relaxation.
743    mips16 supports two versions of most instructions which take
744    immediate values: a small one which takes some small value, and a
745    larger one which takes a 16 bit value.  Since branches also follow
746    this pattern, relaxing these values is required.
747
748    We can assemble both mips16 and normal MIPS code in a single
749    object.  Therefore, we need to support this type of relaxation at
750    the same time that we support the relaxation described above.  We
751    use the high bit of the subtype field to distinguish these cases.
752
753    The information we store for this type of relaxation is the
754    argument code found in the opcode file for this relocation, whether
755    the user explicitly requested a small or extended form, and whether
756    the relocation is in a jump or jal delay slot.  That tells us the
757    size of the value, and how it should be stored.  We also store
758    whether the fragment is considered to be extended or not.  We also
759    store whether this is known to be a branch to a different section,
760    whether we have tried to relax this frag yet, and whether we have
761    ever extended a PC relative fragment because of a shift count.  */
762 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
763   (0x80000000                                                   \
764    | ((type) & 0xff)                                            \
765    | ((small) ? 0x100 : 0)                                      \
766    | ((ext) ? 0x200 : 0)                                        \
767    | ((dslot) ? 0x400 : 0)                                      \
768    | ((jal_dslot) ? 0x800 : 0))
769 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
770 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
771 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
772 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
773 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
774 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
775 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
776 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
777 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
778 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
779 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
780 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
781
782 /* Is the given value a sign-extended 32-bit value?  */
783 #define IS_SEXT_32BIT_NUM(x)                                            \
784   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
785    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
786
787 /* Is the given value a sign-extended 16-bit value?  */
788 #define IS_SEXT_16BIT_NUM(x)                                            \
789   (((x) &~ (offsetT) 0x7fff) == 0                                       \
790    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
791
792 \f
793 /* Prototypes for static functions.  */
794
795 #ifdef __STDC__
796 #define internalError() \
797     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
798 #else
799 #define internalError() as_fatal (_("MIPS internal Error"));
800 #endif
801
802 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
803
804 static int insn_uses_reg
805   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
806            enum mips_regclass class));
807 static int reg_needs_delay
808   PARAMS ((unsigned int));
809 static void mips16_mark_labels
810   PARAMS ((void));
811 static void append_insn
812   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
813            bfd_reloc_code_real_type *r));
814 static void mips_no_prev_insn
815   PARAMS ((int));
816 static void mips_emit_delays
817   PARAMS ((bfd_boolean));
818 #ifdef USE_STDARG
819 static void macro_build
820   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
821            const char *fmt, ...));
822 #else
823 static void macro_build ();
824 #endif
825 static void mips16_macro_build
826   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
827 static void macro_build_jalr
828   PARAMS ((int, expressionS *));
829 static void macro_build_lui
830   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
831 static void macro_build_ldst_constoffset
832   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
833            int valreg, int breg));
834 static void set_at
835   PARAMS ((int *counter, int reg, int unsignedp));
836 static void check_absolute_expr
837   PARAMS ((struct mips_cl_insn * ip, expressionS *));
838 static void load_register
839   PARAMS ((int *, int, expressionS *, int));
840 static void load_address
841   PARAMS ((int *, int, expressionS *, int *));
842 static void move_register
843   PARAMS ((int *, int, int));
844 static void macro
845   PARAMS ((struct mips_cl_insn * ip));
846 static void mips16_macro
847   PARAMS ((struct mips_cl_insn * ip));
848 #ifdef LOSING_COMPILER
849 static void macro2
850   PARAMS ((struct mips_cl_insn * ip));
851 #endif
852 static void mips_ip
853   PARAMS ((char *str, struct mips_cl_insn * ip));
854 static void mips16_ip
855   PARAMS ((char *str, struct mips_cl_insn * ip));
856 static void mips16_immed
857   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
858            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
859 static bfd_boolean parse_relocation
860   PARAMS ((char **, bfd_reloc_code_real_type *));
861 static size_t my_getSmallExpression
862   PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
863 static void my_getExpression
864   PARAMS ((expressionS *, char *));
865 #ifdef OBJ_ELF
866 static int support_64bit_objects
867   PARAMS((void));
868 #endif
869 static void mips_set_option_string
870   PARAMS ((const char **, const char *));
871 static symbolS *get_symbol
872   PARAMS ((void));
873 static void mips_align
874   PARAMS ((int to, int fill, symbolS *label));
875 static void s_align
876   PARAMS ((int));
877 static void s_change_sec
878   PARAMS ((int));
879 static void s_change_section
880   PARAMS ((int));
881 static void s_cons
882   PARAMS ((int));
883 static void s_float_cons
884   PARAMS ((int));
885 static void s_mips_globl
886   PARAMS ((int));
887 static void s_option
888   PARAMS ((int));
889 static void s_mipsset
890   PARAMS ((int));
891 static void s_abicalls
892   PARAMS ((int));
893 static void s_cpload
894   PARAMS ((int));
895 static void s_cpsetup
896   PARAMS ((int));
897 static void s_cplocal
898   PARAMS ((int));
899 static void s_cprestore
900   PARAMS ((int));
901 static void s_cpreturn
902   PARAMS ((int));
903 static void s_gpvalue
904   PARAMS ((int));
905 static void s_gpword
906   PARAMS ((int));
907 static void s_gpdword
908   PARAMS ((int));
909 static void s_cpadd
910   PARAMS ((int));
911 static void s_insn
912   PARAMS ((int));
913 static void md_obj_begin
914   PARAMS ((void));
915 static void md_obj_end
916   PARAMS ((void));
917 static long get_number
918   PARAMS ((void));
919 static void s_mips_ent
920   PARAMS ((int));
921 static void s_mips_end
922   PARAMS ((int));
923 static void s_mips_frame
924   PARAMS ((int));
925 static void s_mips_mask
926   PARAMS ((int));
927 static void s_mips_stab
928   PARAMS ((int));
929 static void s_mips_weakext
930   PARAMS ((int));
931 static void s_mips_file
932   PARAMS ((int));
933 static void s_mips_loc
934   PARAMS ((int));
935 static int mips16_extended_frag
936   PARAMS ((fragS *, asection *, long));
937 static int relaxed_branch_length (fragS *, asection *, int);
938 static int validate_mips_insn
939   PARAMS ((const struct mips_opcode *));
940 static void show
941   PARAMS ((FILE *, const char *, int *, int *));
942 #ifdef OBJ_ELF
943 static int mips_need_elf_addend_fixup
944   PARAMS ((fixS *));
945 #endif
946
947 /* Table and functions used to map between CPU/ISA names, and
948    ISA levels, and CPU numbers.  */
949
950 struct mips_cpu_info
951 {
952   const char *name;           /* CPU or ISA name.  */
953   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
954   int isa;                    /* ISA level.  */
955   int cpu;                    /* CPU number (default CPU if ISA).  */
956 };
957
958 static void mips_set_architecture
959   PARAMS ((const struct mips_cpu_info *));
960 static void mips_set_tune
961   PARAMS ((const struct mips_cpu_info *));
962 static bfd_boolean mips_strict_matching_cpu_name_p
963   PARAMS ((const char *, const char *));
964 static bfd_boolean mips_matching_cpu_name_p
965   PARAMS ((const char *, const char *));
966 static const struct mips_cpu_info *mips_parse_cpu
967   PARAMS ((const char *, const char *));
968 static const struct mips_cpu_info *mips_cpu_info_from_isa
969   PARAMS ((int));
970 \f
971 /* Pseudo-op table.
972
973    The following pseudo-ops from the Kane and Heinrich MIPS book
974    should be defined here, but are currently unsupported: .alias,
975    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
976
977    The following pseudo-ops from the Kane and Heinrich MIPS book are
978    specific to the type of debugging information being generated, and
979    should be defined by the object format: .aent, .begin, .bend,
980    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
981    .vreg.
982
983    The following pseudo-ops from the Kane and Heinrich MIPS book are
984    not MIPS CPU specific, but are also not specific to the object file
985    format.  This file is probably the best place to define them, but
986    they are not currently supported: .asm0, .endr, .lab, .repeat,
987    .struct.  */
988
989 static const pseudo_typeS mips_pseudo_table[] =
990 {
991   /* MIPS specific pseudo-ops.  */
992   {"option", s_option, 0},
993   {"set", s_mipsset, 0},
994   {"rdata", s_change_sec, 'r'},
995   {"sdata", s_change_sec, 's'},
996   {"livereg", s_ignore, 0},
997   {"abicalls", s_abicalls, 0},
998   {"cpload", s_cpload, 0},
999   {"cpsetup", s_cpsetup, 0},
1000   {"cplocal", s_cplocal, 0},
1001   {"cprestore", s_cprestore, 0},
1002   {"cpreturn", s_cpreturn, 0},
1003   {"gpvalue", s_gpvalue, 0},
1004   {"gpword", s_gpword, 0},
1005   {"gpdword", s_gpdword, 0},
1006   {"cpadd", s_cpadd, 0},
1007   {"insn", s_insn, 0},
1008
1009   /* Relatively generic pseudo-ops that happen to be used on MIPS
1010      chips.  */
1011   {"asciiz", stringer, 1},
1012   {"bss", s_change_sec, 'b'},
1013   {"err", s_err, 0},
1014   {"half", s_cons, 1},
1015   {"dword", s_cons, 3},
1016   {"weakext", s_mips_weakext, 0},
1017
1018   /* These pseudo-ops are defined in read.c, but must be overridden
1019      here for one reason or another.  */
1020   {"align", s_align, 0},
1021   {"byte", s_cons, 0},
1022   {"data", s_change_sec, 'd'},
1023   {"double", s_float_cons, 'd'},
1024   {"float", s_float_cons, 'f'},
1025   {"globl", s_mips_globl, 0},
1026   {"global", s_mips_globl, 0},
1027   {"hword", s_cons, 1},
1028   {"int", s_cons, 2},
1029   {"long", s_cons, 2},
1030   {"octa", s_cons, 4},
1031   {"quad", s_cons, 3},
1032   {"section", s_change_section, 0},
1033   {"short", s_cons, 1},
1034   {"single", s_float_cons, 'f'},
1035   {"stabn", s_mips_stab, 'n'},
1036   {"text", s_change_sec, 't'},
1037   {"word", s_cons, 2},
1038
1039   { "extern", ecoff_directive_extern, 0},
1040
1041   { NULL, NULL, 0 },
1042 };
1043
1044 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1045 {
1046   /* These pseudo-ops should be defined by the object file format.
1047      However, a.out doesn't support them, so we have versions here.  */
1048   {"aent", s_mips_ent, 1},
1049   {"bgnb", s_ignore, 0},
1050   {"end", s_mips_end, 0},
1051   {"endb", s_ignore, 0},
1052   {"ent", s_mips_ent, 0},
1053   {"file", s_mips_file, 0},
1054   {"fmask", s_mips_mask, 'F'},
1055   {"frame", s_mips_frame, 0},
1056   {"loc", s_mips_loc, 0},
1057   {"mask", s_mips_mask, 'R'},
1058   {"verstamp", s_ignore, 0},
1059   { NULL, NULL, 0 },
1060 };
1061
1062 extern void pop_insert PARAMS ((const pseudo_typeS *));
1063
1064 void
1065 mips_pop_insert ()
1066 {
1067   pop_insert (mips_pseudo_table);
1068   if (! ECOFF_DEBUGGING)
1069     pop_insert (mips_nonecoff_pseudo_table);
1070 }
1071 \f
1072 /* Symbols labelling the current insn.  */
1073
1074 struct insn_label_list
1075 {
1076   struct insn_label_list *next;
1077   symbolS *label;
1078 };
1079
1080 static struct insn_label_list *insn_labels;
1081 static struct insn_label_list *free_insn_labels;
1082
1083 static void mips_clear_insn_labels PARAMS ((void));
1084
1085 static inline void
1086 mips_clear_insn_labels ()
1087 {
1088   register struct insn_label_list **pl;
1089
1090   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1091     ;
1092   *pl = insn_labels;
1093   insn_labels = NULL;
1094 }
1095 \f
1096 static char *expr_end;
1097
1098 /* Expressions which appear in instructions.  These are set by
1099    mips_ip.  */
1100
1101 static expressionS imm_expr;
1102 static expressionS offset_expr;
1103
1104 /* Relocs associated with imm_expr and offset_expr.  */
1105
1106 static bfd_reloc_code_real_type imm_reloc[3]
1107   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1108 static bfd_reloc_code_real_type offset_reloc[3]
1109   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1110
1111 /* These are set by mips16_ip if an explicit extension is used.  */
1112
1113 static bfd_boolean mips16_small, mips16_ext;
1114
1115 #ifdef OBJ_ELF
1116 /* The pdr segment for per procedure frame/regmask info.  Not used for
1117    ECOFF debugging.  */
1118
1119 static segT pdr_seg;
1120 #endif
1121
1122 /* The default target format to use.  */
1123
1124 const char *
1125 mips_target_format ()
1126 {
1127   switch (OUTPUT_FLAVOR)
1128     {
1129     case bfd_target_aout_flavour:
1130       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1131     case bfd_target_ecoff_flavour:
1132       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1133     case bfd_target_coff_flavour:
1134       return "pe-mips";
1135     case bfd_target_elf_flavour:
1136 #ifdef TE_TMIPS
1137       /* This is traditional mips.  */
1138       return (target_big_endian
1139               ? (HAVE_64BIT_OBJECTS
1140                  ? "elf64-tradbigmips"
1141                  : (HAVE_NEWABI
1142                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1143               : (HAVE_64BIT_OBJECTS
1144                  ? "elf64-tradlittlemips"
1145                  : (HAVE_NEWABI
1146                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1147 #else
1148       return (target_big_endian
1149               ? (HAVE_64BIT_OBJECTS
1150                  ? "elf64-bigmips"
1151                  : (HAVE_NEWABI
1152                     ? "elf32-nbigmips" : "elf32-bigmips"))
1153               : (HAVE_64BIT_OBJECTS
1154                  ? "elf64-littlemips"
1155                  : (HAVE_NEWABI
1156                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1157 #endif
1158     default:
1159       abort ();
1160       return NULL;
1161     }
1162 }
1163
1164 /* This function is called once, at assembler startup time.  It should
1165    set up all the tables, etc. that the MD part of the assembler will need.  */
1166
1167 void
1168 md_begin ()
1169 {
1170   register const char *retval = NULL;
1171   int i = 0;
1172   int broken = 0;
1173
1174   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1175     as_warn (_("Could not set architecture and machine"));
1176
1177   op_hash = hash_new ();
1178
1179   for (i = 0; i < NUMOPCODES;)
1180     {
1181       const char *name = mips_opcodes[i].name;
1182
1183       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1184       if (retval != NULL)
1185         {
1186           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1187                    mips_opcodes[i].name, retval);
1188           /* Probably a memory allocation problem?  Give up now.  */
1189           as_fatal (_("Broken assembler.  No assembly attempted."));
1190         }
1191       do
1192         {
1193           if (mips_opcodes[i].pinfo != INSN_MACRO)
1194             {
1195               if (!validate_mips_insn (&mips_opcodes[i]))
1196                 broken = 1;
1197             }
1198           ++i;
1199         }
1200       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1201     }
1202
1203   mips16_op_hash = hash_new ();
1204
1205   i = 0;
1206   while (i < bfd_mips16_num_opcodes)
1207     {
1208       const char *name = mips16_opcodes[i].name;
1209
1210       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1211       if (retval != NULL)
1212         as_fatal (_("internal: can't hash `%s': %s"),
1213                   mips16_opcodes[i].name, retval);
1214       do
1215         {
1216           if (mips16_opcodes[i].pinfo != INSN_MACRO
1217               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1218                   != mips16_opcodes[i].match))
1219             {
1220               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1221                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1222               broken = 1;
1223             }
1224           ++i;
1225         }
1226       while (i < bfd_mips16_num_opcodes
1227              && strcmp (mips16_opcodes[i].name, name) == 0);
1228     }
1229
1230   if (broken)
1231     as_fatal (_("Broken assembler.  No assembly attempted."));
1232
1233   /* We add all the general register names to the symbol table.  This
1234      helps us detect invalid uses of them.  */
1235   for (i = 0; i < 32; i++)
1236     {
1237       char buf[5];
1238
1239       sprintf (buf, "$%d", i);
1240       symbol_table_insert (symbol_new (buf, reg_section, i,
1241                                        &zero_address_frag));
1242     }
1243   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1244                                    &zero_address_frag));
1245   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1246                                    &zero_address_frag));
1247   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1248                                    &zero_address_frag));
1249   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1250                                    &zero_address_frag));
1251   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1252                                    &zero_address_frag));
1253   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1254                                    &zero_address_frag));
1255   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1256                                    &zero_address_frag));
1257   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1258                                    &zero_address_frag));
1259   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1260                                    &zero_address_frag));
1261
1262   /* If we don't add these register names to the symbol table, they
1263      may end up being added as regular symbols by operand(), and then
1264      make it to the object file as undefined in case they're not
1265      regarded as local symbols.  They're local in o32, since `$' is a
1266      local symbol prefix, but not in n32 or n64.  */
1267   for (i = 0; i < 8; i++)
1268     {
1269       char buf[6];
1270
1271       sprintf (buf, "$fcc%i", i);
1272       symbol_table_insert (symbol_new (buf, reg_section, -1,
1273                                        &zero_address_frag));
1274     }
1275
1276   mips_no_prev_insn (FALSE);
1277
1278   mips_gprmask = 0;
1279   mips_cprmask[0] = 0;
1280   mips_cprmask[1] = 0;
1281   mips_cprmask[2] = 0;
1282   mips_cprmask[3] = 0;
1283
1284   /* set the default alignment for the text section (2**2) */
1285   record_alignment (text_section, 2);
1286
1287   if (USE_GLOBAL_POINTER_OPT)
1288     bfd_set_gp_size (stdoutput, g_switch_value);
1289
1290   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1291     {
1292       /* On a native system, sections must be aligned to 16 byte
1293          boundaries.  When configured for an embedded ELF target, we
1294          don't bother.  */
1295       if (strcmp (TARGET_OS, "elf") != 0)
1296         {
1297           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1298           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1299           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1300         }
1301
1302       /* Create a .reginfo section for register masks and a .mdebug
1303          section for debugging information.  */
1304       {
1305         segT seg;
1306         subsegT subseg;
1307         flagword flags;
1308         segT sec;
1309
1310         seg = now_seg;
1311         subseg = now_subseg;
1312
1313         /* The ABI says this section should be loaded so that the
1314            running program can access it.  However, we don't load it
1315            if we are configured for an embedded target */
1316         flags = SEC_READONLY | SEC_DATA;
1317         if (strcmp (TARGET_OS, "elf") != 0)
1318           flags |= SEC_ALLOC | SEC_LOAD;
1319
1320         if (mips_abi != N64_ABI)
1321           {
1322             sec = subseg_new (".reginfo", (subsegT) 0);
1323
1324             bfd_set_section_flags (stdoutput, sec, flags);
1325             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1326
1327 #ifdef OBJ_ELF
1328             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1329 #endif
1330           }
1331         else
1332           {
1333             /* The 64-bit ABI uses a .MIPS.options section rather than
1334                .reginfo section.  */
1335             sec = subseg_new (".MIPS.options", (subsegT) 0);
1336             bfd_set_section_flags (stdoutput, sec, flags);
1337             bfd_set_section_alignment (stdoutput, sec, 3);
1338
1339 #ifdef OBJ_ELF
1340             /* Set up the option header.  */
1341             {
1342               Elf_Internal_Options opthdr;
1343               char *f;
1344
1345               opthdr.kind = ODK_REGINFO;
1346               opthdr.size = (sizeof (Elf_External_Options)
1347                              + sizeof (Elf64_External_RegInfo));
1348               opthdr.section = 0;
1349               opthdr.info = 0;
1350               f = frag_more (sizeof (Elf_External_Options));
1351               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1352                                              (Elf_External_Options *) f);
1353
1354               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1355             }
1356 #endif
1357           }
1358
1359         if (ECOFF_DEBUGGING)
1360           {
1361             sec = subseg_new (".mdebug", (subsegT) 0);
1362             (void) bfd_set_section_flags (stdoutput, sec,
1363                                           SEC_HAS_CONTENTS | SEC_READONLY);
1364             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1365           }
1366 #ifdef OBJ_ELF
1367         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1368           {
1369             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1370             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1371                                           SEC_READONLY | SEC_RELOC
1372                                           | SEC_DEBUGGING);
1373             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1374           }
1375 #endif
1376
1377         subseg_set (seg, subseg);
1378       }
1379     }
1380
1381   if (! ECOFF_DEBUGGING)
1382     md_obj_begin ();
1383 }
1384
1385 void
1386 md_mips_end ()
1387 {
1388   if (! ECOFF_DEBUGGING)
1389     md_obj_end ();
1390 }
1391
1392 void
1393 md_assemble (str)
1394      char *str;
1395 {
1396   struct mips_cl_insn insn;
1397   bfd_reloc_code_real_type unused_reloc[3]
1398     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1399
1400   imm_expr.X_op = O_absent;
1401   offset_expr.X_op = O_absent;
1402   imm_reloc[0] = BFD_RELOC_UNUSED;
1403   imm_reloc[1] = BFD_RELOC_UNUSED;
1404   imm_reloc[2] = BFD_RELOC_UNUSED;
1405   offset_reloc[0] = BFD_RELOC_UNUSED;
1406   offset_reloc[1] = BFD_RELOC_UNUSED;
1407   offset_reloc[2] = BFD_RELOC_UNUSED;
1408
1409   if (mips_opts.mips16)
1410     mips16_ip (str, &insn);
1411   else
1412     {
1413       mips_ip (str, &insn);
1414       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1415             str, insn.insn_opcode));
1416     }
1417
1418   if (insn_error)
1419     {
1420       as_bad ("%s `%s'", insn_error, str);
1421       return;
1422     }
1423
1424   if (insn.insn_mo->pinfo == INSN_MACRO)
1425     {
1426       if (mips_opts.mips16)
1427         mips16_macro (&insn);
1428       else
1429         macro (&insn);
1430     }
1431   else
1432     {
1433       if (imm_expr.X_op != O_absent)
1434         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1435       else if (offset_expr.X_op != O_absent)
1436         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1437       else
1438         append_insn (NULL, &insn, NULL, unused_reloc);
1439     }
1440 }
1441
1442 /* See whether instruction IP reads register REG.  CLASS is the type
1443    of register.  */
1444
1445 static int
1446 insn_uses_reg (ip, reg, class)
1447      struct mips_cl_insn *ip;
1448      unsigned int reg;
1449      enum mips_regclass class;
1450 {
1451   if (class == MIPS16_REG)
1452     {
1453       assert (mips_opts.mips16);
1454       reg = mips16_to_32_reg_map[reg];
1455       class = MIPS_GR_REG;
1456     }
1457
1458   /* Don't report on general register ZERO, since it never changes.  */
1459   if (class == MIPS_GR_REG && reg == ZERO)
1460     return 0;
1461
1462   if (class == MIPS_FP_REG)
1463     {
1464       assert (! mips_opts.mips16);
1465       /* If we are called with either $f0 or $f1, we must check $f0.
1466          This is not optimal, because it will introduce an unnecessary
1467          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1468          need to distinguish reading both $f0 and $f1 or just one of
1469          them.  Note that we don't have to check the other way,
1470          because there is no instruction that sets both $f0 and $f1
1471          and requires a delay.  */
1472       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1473           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1474               == (reg &~ (unsigned) 1)))
1475         return 1;
1476       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1477           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1478               == (reg &~ (unsigned) 1)))
1479         return 1;
1480     }
1481   else if (! mips_opts.mips16)
1482     {
1483       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1484           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1485         return 1;
1486       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1487           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1488         return 1;
1489     }
1490   else
1491     {
1492       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1493           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1494                                     & MIPS16OP_MASK_RX)]
1495               == reg))
1496         return 1;
1497       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1498           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1499                                     & MIPS16OP_MASK_RY)]
1500               == reg))
1501         return 1;
1502       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1503           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1504                                     & MIPS16OP_MASK_MOVE32Z)]
1505               == reg))
1506         return 1;
1507       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1508         return 1;
1509       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1510         return 1;
1511       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1512         return 1;
1513       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1514           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1515               & MIPS16OP_MASK_REGR32) == reg)
1516         return 1;
1517     }
1518
1519   return 0;
1520 }
1521
1522 /* This function returns true if modifying a register requires a
1523    delay.  */
1524
1525 static int
1526 reg_needs_delay (reg)
1527      unsigned int reg;
1528 {
1529   unsigned long prev_pinfo;
1530
1531   prev_pinfo = prev_insn.insn_mo->pinfo;
1532   if (! mips_opts.noreorder
1533       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1534       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1535           || (! gpr_interlocks
1536               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1537     {
1538       /* A load from a coprocessor or from memory.  All load
1539          delays delay the use of general register rt for one
1540          instruction on the r3000.  The r6000 and r4000 use
1541          interlocks.  */
1542       /* Itbl support may require additional care here.  */
1543       know (prev_pinfo & INSN_WRITE_GPR_T);
1544       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1545         return 1;
1546     }
1547
1548   return 0;
1549 }
1550
1551 /* Mark instruction labels in mips16 mode.  This permits the linker to
1552    handle them specially, such as generating jalx instructions when
1553    needed.  We also make them odd for the duration of the assembly, in
1554    order to generate the right sort of code.  We will make them even
1555    in the adjust_symtab routine, while leaving them marked.  This is
1556    convenient for the debugger and the disassembler.  The linker knows
1557    to make them odd again.  */
1558
1559 static void
1560 mips16_mark_labels ()
1561 {
1562   if (mips_opts.mips16)
1563     {
1564       struct insn_label_list *l;
1565       valueT val;
1566
1567       for (l = insn_labels; l != NULL; l = l->next)
1568         {
1569 #ifdef OBJ_ELF
1570           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1571             S_SET_OTHER (l->label, STO_MIPS16);
1572 #endif
1573           val = S_GET_VALUE (l->label);
1574           if ((val & 1) == 0)
1575             S_SET_VALUE (l->label, val + 1);
1576         }
1577     }
1578 }
1579
1580 /* Output an instruction.  PLACE is where to put the instruction; if
1581    it is NULL, this uses frag_more to get room.  IP is the instruction
1582    information.  ADDRESS_EXPR is an operand of the instruction to be
1583    used with RELOC_TYPE.  */
1584
1585 static void
1586 append_insn (place, ip, address_expr, reloc_type)
1587      char *place;
1588      struct mips_cl_insn *ip;
1589      expressionS *address_expr;
1590      bfd_reloc_code_real_type *reloc_type;
1591 {
1592   register unsigned long prev_pinfo, pinfo;
1593   char *f;
1594   fixS *fixp[3];
1595   int nops = 0;
1596   bfd_boolean unmatched_reloc_p;
1597
1598   /* Mark instruction labels in mips16 mode.  */
1599   mips16_mark_labels ();
1600
1601   prev_pinfo = prev_insn.insn_mo->pinfo;
1602   pinfo = ip->insn_mo->pinfo;
1603   unmatched_reloc_p = FALSE;
1604
1605   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1606     {
1607       int prev_prev_nop;
1608
1609       /* If the previous insn required any delay slots, see if we need
1610          to insert a NOP or two.  There are eight kinds of possible
1611          hazards, of which an instruction can have at most one type.
1612          (1) a load from memory delay
1613          (2) a load from a coprocessor delay
1614          (3) an unconditional branch delay
1615          (4) a conditional branch delay
1616          (5) a move to coprocessor register delay
1617          (6) a load coprocessor register from memory delay
1618          (7) a coprocessor condition code delay
1619          (8) a HI/LO special register delay
1620
1621          There are a lot of optimizations we could do that we don't.
1622          In particular, we do not, in general, reorder instructions.
1623          If you use gcc with optimization, it will reorder
1624          instructions and generally do much more optimization then we
1625          do here; repeating all that work in the assembler would only
1626          benefit hand written assembly code, and does not seem worth
1627          it.  */
1628
1629       /* This is how a NOP is emitted.  */
1630 #define emit_nop()                                      \
1631   (mips_opts.mips16                                     \
1632    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1633    : md_number_to_chars (frag_more (4), 0, 4))
1634
1635       /* The previous insn might require a delay slot, depending upon
1636          the contents of the current insn.  */
1637       if (! mips_opts.mips16
1638           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1639           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1640                && ! cop_interlocks)
1641               || (! gpr_interlocks
1642                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1643         {
1644           /* A load from a coprocessor or from memory.  All load
1645              delays delay the use of general register rt for one
1646              instruction on the r3000.  The r6000 and r4000 use
1647              interlocks.  */
1648           /* Itbl support may require additional care here.  */
1649           know (prev_pinfo & INSN_WRITE_GPR_T);
1650           if (mips_optimize == 0
1651               || insn_uses_reg (ip,
1652                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1653                                  & OP_MASK_RT),
1654                                 MIPS_GR_REG))
1655             ++nops;
1656         }
1657       else if (! mips_opts.mips16
1658                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1659                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1660                     && ! cop_interlocks)
1661                    || (mips_opts.isa == ISA_MIPS1
1662                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1663         {
1664           /* A generic coprocessor delay.  The previous instruction
1665              modified a coprocessor general or control register.  If
1666              it modified a control register, we need to avoid any
1667              coprocessor instruction (this is probably not always
1668              required, but it sometimes is).  If it modified a general
1669              register, we avoid using that register.
1670
1671              On the r6000 and r4000 loading a coprocessor register
1672              from memory is interlocked, and does not require a delay.
1673
1674              This case is not handled very well.  There is no special
1675              knowledge of CP0 handling, and the coprocessors other
1676              than the floating point unit are not distinguished at
1677              all.  */
1678           /* Itbl support may require additional care here. FIXME!
1679              Need to modify this to include knowledge about
1680              user specified delays!  */
1681           if (prev_pinfo & INSN_WRITE_FPR_T)
1682             {
1683               if (mips_optimize == 0
1684                   || insn_uses_reg (ip,
1685                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1686                                      & OP_MASK_FT),
1687                                     MIPS_FP_REG))
1688                 ++nops;
1689             }
1690           else if (prev_pinfo & INSN_WRITE_FPR_S)
1691             {
1692               if (mips_optimize == 0
1693                   || insn_uses_reg (ip,
1694                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1695                                      & OP_MASK_FS),
1696                                     MIPS_FP_REG))
1697                 ++nops;
1698             }
1699           else
1700             {
1701               /* We don't know exactly what the previous instruction
1702                  does.  If the current instruction uses a coprocessor
1703                  register, we must insert a NOP.  If previous
1704                  instruction may set the condition codes, and the
1705                  current instruction uses them, we must insert two
1706                  NOPS.  */
1707               /* Itbl support may require additional care here.  */
1708               if (mips_optimize == 0
1709                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1710                       && (pinfo & INSN_READ_COND_CODE)))
1711                 nops += 2;
1712               else if (pinfo & INSN_COP)
1713                 ++nops;
1714             }
1715         }
1716       else if (! mips_opts.mips16
1717                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1718                && (prev_pinfo & INSN_WRITE_COND_CODE)
1719                && ! cop_interlocks)
1720         {
1721           /* The previous instruction sets the coprocessor condition
1722              codes, but does not require a general coprocessor delay
1723              (this means it is a floating point comparison
1724              instruction).  If this instruction uses the condition
1725              codes, we need to insert a single NOP.  */
1726           /* Itbl support may require additional care here.  */
1727           if (mips_optimize == 0
1728               || (pinfo & INSN_READ_COND_CODE))
1729             ++nops;
1730         }
1731
1732       /* If we're fixing up mfhi/mflo for the r7000 and the
1733          previous insn was an mfhi/mflo and the current insn
1734          reads the register that the mfhi/mflo wrote to, then
1735          insert two nops.  */
1736
1737       else if (mips_7000_hilo_fix
1738                && MF_HILO_INSN (prev_pinfo)
1739                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1740                                       & OP_MASK_RD),
1741                                  MIPS_GR_REG))
1742         {
1743           nops += 2;
1744         }
1745
1746       /* If we're fixing up mfhi/mflo for the r7000 and the
1747          2nd previous insn was an mfhi/mflo and the current insn
1748          reads the register that the mfhi/mflo wrote to, then
1749          insert one nop.  */
1750
1751       else if (mips_7000_hilo_fix
1752                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1753                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1754                                        & OP_MASK_RD),
1755                                     MIPS_GR_REG))
1756
1757         {
1758           ++nops;
1759         }
1760
1761       else if (prev_pinfo & INSN_READ_LO)
1762         {
1763           /* The previous instruction reads the LO register; if the
1764              current instruction writes to the LO register, we must
1765              insert two NOPS.  Some newer processors have interlocks.
1766              Also the tx39's multiply instructions can be exectuted
1767              immediatly after a read from HI/LO (without the delay),
1768              though the tx39's divide insns still do require the
1769              delay.  */
1770           if (! (hilo_interlocks
1771                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1772               && (mips_optimize == 0
1773                   || (pinfo & INSN_WRITE_LO)))
1774             nops += 2;
1775           /* Most mips16 branch insns don't have a delay slot.
1776              If a read from LO is immediately followed by a branch
1777              to a write to LO we have a read followed by a write
1778              less than 2 insns away.  We assume the target of
1779              a branch might be a write to LO, and insert a nop
1780              between a read and an immediately following branch.  */
1781           else if (mips_opts.mips16
1782                    && (mips_optimize == 0
1783                        || (pinfo & MIPS16_INSN_BRANCH)))
1784             ++nops;
1785         }
1786       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1787         {
1788           /* The previous instruction reads the HI register; if the
1789              current instruction writes to the HI register, we must
1790              insert a NOP.  Some newer processors have interlocks.
1791              Also the note tx39's multiply above.  */
1792           if (! (hilo_interlocks
1793                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1794               && (mips_optimize == 0
1795                   || (pinfo & INSN_WRITE_HI)))
1796             nops += 2;
1797           /* Most mips16 branch insns don't have a delay slot.
1798              If a read from HI is immediately followed by a branch
1799              to a write to HI we have a read followed by a write
1800              less than 2 insns away.  We assume the target of
1801              a branch might be a write to HI, and insert a nop
1802              between a read and an immediately following branch.  */
1803           else if (mips_opts.mips16
1804                    && (mips_optimize == 0
1805                        || (pinfo & MIPS16_INSN_BRANCH)))
1806             ++nops;
1807         }
1808
1809       /* If the previous instruction was in a noreorder section, then
1810          we don't want to insert the nop after all.  */
1811       /* Itbl support may require additional care here.  */
1812       if (prev_insn_unreordered)
1813         nops = 0;
1814
1815       /* There are two cases which require two intervening
1816          instructions: 1) setting the condition codes using a move to
1817          coprocessor instruction which requires a general coprocessor
1818          delay and then reading the condition codes 2) reading the HI
1819          or LO register and then writing to it (except on processors
1820          which have interlocks).  If we are not already emitting a NOP
1821          instruction, we must check for these cases compared to the
1822          instruction previous to the previous instruction.  */
1823       if ((! mips_opts.mips16
1824            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1825            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1826            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1827            && (pinfo & INSN_READ_COND_CODE)
1828            && ! cop_interlocks)
1829           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1830               && (pinfo & INSN_WRITE_LO)
1831               && ! (hilo_interlocks
1832                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1833           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1834               && (pinfo & INSN_WRITE_HI)
1835               && ! (hilo_interlocks
1836                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1837         prev_prev_nop = 1;
1838       else
1839         prev_prev_nop = 0;
1840
1841       if (prev_prev_insn_unreordered)
1842         prev_prev_nop = 0;
1843
1844       if (prev_prev_nop && nops == 0)
1845         ++nops;
1846
1847       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1848         {
1849           /* We're out of bits in pinfo, so we must resort to string
1850              ops here.  Shortcuts are selected based on opcodes being
1851              limited to the VR4122 instruction set.  */
1852           int min_nops = 0;
1853           const char *pn = prev_insn.insn_mo->name;
1854           const char *tn = ip->insn_mo->name;
1855           if (strncmp(pn, "macc", 4) == 0
1856               || strncmp(pn, "dmacc", 5) == 0)
1857             {
1858               /* Errata 21 - [D]DIV[U] after [D]MACC */
1859               if (strstr (tn, "div"))
1860                 {
1861                   min_nops = 1;
1862                 }
1863
1864               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1865               if (pn[0] == 'd' /* dmacc */
1866                   && (strncmp(tn, "dmult", 5) == 0
1867                       || strncmp(tn, "dmacc", 5) == 0))
1868                 {
1869                   min_nops = 1;
1870                 }
1871
1872               /* Errata 24 - MT{LO,HI} after [D]MACC */
1873               if (strcmp (tn, "mtlo") == 0
1874                   || strcmp (tn, "mthi") == 0)
1875                 {
1876                   min_nops = 1;
1877                 }
1878
1879             }
1880           else if (strncmp(pn, "dmult", 5) == 0
1881                    && (strncmp(tn, "dmult", 5) == 0
1882                        || strncmp(tn, "dmacc", 5) == 0))
1883             {
1884               /* Here is the rest of errata 23.  */
1885               min_nops = 1;
1886             }
1887           if (nops < min_nops)
1888             nops = min_nops;
1889         }
1890
1891       /* If we are being given a nop instruction, don't bother with
1892          one of the nops we would otherwise output.  This will only
1893          happen when a nop instruction is used with mips_optimize set
1894          to 0.  */
1895       if (nops > 0
1896           && ! mips_opts.noreorder
1897           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1898         --nops;
1899
1900       /* Now emit the right number of NOP instructions.  */
1901       if (nops > 0 && ! mips_opts.noreorder)
1902         {
1903           fragS *old_frag;
1904           unsigned long old_frag_offset;
1905           int i;
1906           struct insn_label_list *l;
1907
1908           old_frag = frag_now;
1909           old_frag_offset = frag_now_fix ();
1910
1911           for (i = 0; i < nops; i++)
1912             emit_nop ();
1913
1914           if (listing)
1915             {
1916               listing_prev_line ();
1917               /* We may be at the start of a variant frag.  In case we
1918                  are, make sure there is enough space for the frag
1919                  after the frags created by listing_prev_line.  The
1920                  argument to frag_grow here must be at least as large
1921                  as the argument to all other calls to frag_grow in
1922                  this file.  We don't have to worry about being in the
1923                  middle of a variant frag, because the variants insert
1924                  all needed nop instructions themselves.  */
1925               frag_grow (40);
1926             }
1927
1928           for (l = insn_labels; l != NULL; l = l->next)
1929             {
1930               valueT val;
1931
1932               assert (S_GET_SEGMENT (l->label) == now_seg);
1933               symbol_set_frag (l->label, frag_now);
1934               val = (valueT) frag_now_fix ();
1935               /* mips16 text labels are stored as odd.  */
1936               if (mips_opts.mips16)
1937                 ++val;
1938               S_SET_VALUE (l->label, val);
1939             }
1940
1941 #ifndef NO_ECOFF_DEBUGGING
1942           if (ECOFF_DEBUGGING)
1943             ecoff_fix_loc (old_frag, old_frag_offset);
1944 #endif
1945         }
1946       else if (prev_nop_frag != NULL)
1947         {
1948           /* We have a frag holding nops we may be able to remove.  If
1949              we don't need any nops, we can decrease the size of
1950              prev_nop_frag by the size of one instruction.  If we do
1951              need some nops, we count them in prev_nops_required.  */
1952           if (prev_nop_frag_since == 0)
1953             {
1954               if (nops == 0)
1955                 {
1956                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1957                   --prev_nop_frag_holds;
1958                 }
1959               else
1960                 prev_nop_frag_required += nops;
1961             }
1962           else
1963             {
1964               if (prev_prev_nop == 0)
1965                 {
1966                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1967                   --prev_nop_frag_holds;
1968                 }
1969               else
1970                 ++prev_nop_frag_required;
1971             }
1972
1973           if (prev_nop_frag_holds <= prev_nop_frag_required)
1974             prev_nop_frag = NULL;
1975
1976           ++prev_nop_frag_since;
1977
1978           /* Sanity check: by the time we reach the second instruction
1979              after prev_nop_frag, we should have used up all the nops
1980              one way or another.  */
1981           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1982         }
1983     }
1984
1985   if (place == NULL
1986       && address_expr
1987       && *reloc_type == BFD_RELOC_16_PCREL_S2
1988       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1989           || pinfo & INSN_COND_BRANCH_LIKELY)
1990       && mips_relax_branch
1991       /* Don't try branch relaxation within .set nomacro, or within
1992          .set noat if we use $at for PIC computations.  If it turns
1993          out that the branch was out-of-range, we'll get an error.  */
1994       && !mips_opts.warn_about_macros
1995       && !(mips_opts.noat && mips_pic != NO_PIC)
1996       && !mips_opts.mips16)
1997     {
1998       f = frag_var (rs_machine_dependent,
1999                     relaxed_branch_length
2000                     (NULL, NULL,
2001                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2002                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2003                     RELAX_BRANCH_ENCODE
2004                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2005                      pinfo & INSN_COND_BRANCH_LIKELY,
2006                      pinfo & INSN_WRITE_GPR_31,
2007                      0),
2008                     address_expr->X_add_symbol,
2009                     address_expr->X_add_number,
2010                     0);
2011       *reloc_type = BFD_RELOC_UNUSED;
2012     }
2013   else if (*reloc_type > BFD_RELOC_UNUSED)
2014     {
2015       /* We need to set up a variant frag.  */
2016       assert (mips_opts.mips16 && address_expr != NULL);
2017       f = frag_var (rs_machine_dependent, 4, 0,
2018                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2019                                          mips16_small, mips16_ext,
2020                                          (prev_pinfo
2021                                           & INSN_UNCOND_BRANCH_DELAY),
2022                                          (*prev_insn_reloc_type
2023                                           == BFD_RELOC_MIPS16_JMP)),
2024                     make_expr_symbol (address_expr), 0, NULL);
2025     }
2026   else if (place != NULL)
2027     f = place;
2028   else if (mips_opts.mips16
2029            && ! ip->use_extend
2030            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2031     {
2032       /* Make sure there is enough room to swap this instruction with
2033          a following jump instruction.  */
2034       frag_grow (6);
2035       f = frag_more (2);
2036     }
2037   else
2038     {
2039       if (mips_opts.mips16
2040           && mips_opts.noreorder
2041           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2042         as_warn (_("extended instruction in delay slot"));
2043
2044       f = frag_more (4);
2045     }
2046
2047   fixp[0] = fixp[1] = fixp[2] = NULL;
2048   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2049     {
2050       if (address_expr->X_op == O_constant)
2051         {
2052           valueT tmp;
2053
2054           switch (*reloc_type)
2055             {
2056             case BFD_RELOC_32:
2057               ip->insn_opcode |= address_expr->X_add_number;
2058               break;
2059
2060             case BFD_RELOC_MIPS_HIGHEST:
2061               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2062               tmp >>= 16;
2063               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2064               break;
2065
2066             case BFD_RELOC_MIPS_HIGHER:
2067               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2068               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2069               break;
2070
2071             case BFD_RELOC_HI16_S:
2072               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2073                                   >> 16) & 0xffff;
2074               break;
2075
2076             case BFD_RELOC_HI16:
2077               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2078               break;
2079
2080             case BFD_RELOC_LO16:
2081             case BFD_RELOC_MIPS_GOT_DISP:
2082               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2083               break;
2084
2085             case BFD_RELOC_MIPS_JMP:
2086               if ((address_expr->X_add_number & 3) != 0)
2087                 as_bad (_("jump to misaligned address (0x%lx)"),
2088                         (unsigned long) address_expr->X_add_number);
2089               if (address_expr->X_add_number & ~0xfffffff)
2090                 as_bad (_("jump address range overflow (0x%lx)"),
2091                         (unsigned long) address_expr->X_add_number);
2092               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2093               break;
2094
2095             case BFD_RELOC_MIPS16_JMP:
2096               if ((address_expr->X_add_number & 3) != 0)
2097                 as_bad (_("jump to misaligned address (0x%lx)"),
2098                         (unsigned long) address_expr->X_add_number);
2099               if (address_expr->X_add_number & ~0xfffffff)
2100                 as_bad (_("jump address range overflow (0x%lx)"),
2101                         (unsigned long) address_expr->X_add_number);
2102               ip->insn_opcode |=
2103                 (((address_expr->X_add_number & 0x7c0000) << 3)
2104                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2105                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2106               break;
2107
2108             case BFD_RELOC_16_PCREL_S2:
2109               goto need_reloc;
2110
2111             default:
2112               internalError ();
2113             }
2114         }
2115       else
2116         {
2117         need_reloc:
2118           /* Don't generate a reloc if we are writing into a variant frag.  */
2119           if (place == NULL)
2120             {
2121               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2122                                      address_expr,
2123                                      *reloc_type == BFD_RELOC_16_PCREL_S2,
2124                                      reloc_type[0]);
2125
2126               /* These relocations can have an addend that won't fit in
2127                  4 octets for 64bit assembly.  */
2128               if (HAVE_64BIT_GPRS &&
2129                   (*reloc_type == BFD_RELOC_16
2130                    || *reloc_type == BFD_RELOC_32
2131                    || *reloc_type == BFD_RELOC_MIPS_JMP
2132                    || *reloc_type == BFD_RELOC_HI16_S
2133                    || *reloc_type == BFD_RELOC_LO16
2134                    || *reloc_type == BFD_RELOC_GPREL16
2135                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2136                    || *reloc_type == BFD_RELOC_GPREL32
2137                    || *reloc_type == BFD_RELOC_64
2138                    || *reloc_type == BFD_RELOC_CTOR
2139                    || *reloc_type == BFD_RELOC_MIPS_SUB
2140                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2141                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2142                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2143                    || *reloc_type == BFD_RELOC_MIPS_REL16
2144                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2145                 fixp[0]->fx_no_overflow = 1;
2146
2147               if (reloc_type[0] == BFD_RELOC_HI16_S)
2148                 {
2149                   struct mips_hi_fixup *hi_fixup;
2150
2151                   hi_fixup = ((struct mips_hi_fixup *)
2152                               xmalloc (sizeof (struct mips_hi_fixup)));
2153                   hi_fixup->fixp = fixp[0];
2154                   hi_fixup->seg = now_seg;
2155                   hi_fixup->next = mips_hi_fixup_list;
2156                   mips_hi_fixup_list = hi_fixup;
2157                   unmatched_reloc_p = TRUE;
2158                 }
2159
2160               if (reloc_type[1] != BFD_RELOC_UNUSED)
2161                 {
2162                   /* FIXME: This symbol can be one of
2163                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2164                   address_expr->X_op = O_absent;
2165                   address_expr->X_add_symbol = 0;
2166                   address_expr->X_add_number = 0;
2167
2168                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2169                                          4, address_expr, FALSE,
2170                                          reloc_type[1]);
2171
2172                   /* These relocations can have an addend that won't fit in
2173                      4 octets for 64bit assembly.  */
2174                   if (HAVE_64BIT_GPRS &&
2175                       (*reloc_type == BFD_RELOC_16
2176                        || *reloc_type == BFD_RELOC_32
2177                        || *reloc_type == BFD_RELOC_MIPS_JMP
2178                        || *reloc_type == BFD_RELOC_HI16_S
2179                        || *reloc_type == BFD_RELOC_LO16
2180                        || *reloc_type == BFD_RELOC_GPREL16
2181                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2182                        || *reloc_type == BFD_RELOC_GPREL32
2183                        || *reloc_type == BFD_RELOC_64
2184                        || *reloc_type == BFD_RELOC_CTOR
2185                        || *reloc_type == BFD_RELOC_MIPS_SUB
2186                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2187                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2188                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2189                        || *reloc_type == BFD_RELOC_MIPS_REL16
2190                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2191                     fixp[1]->fx_no_overflow = 1;
2192
2193                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2194                     {
2195                       address_expr->X_op = O_absent;
2196                       address_expr->X_add_symbol = 0;
2197                       address_expr->X_add_number = 0;
2198
2199                       fixp[2] = fix_new_exp (frag_now,
2200                                              f - frag_now->fr_literal, 4,
2201                                              address_expr, FALSE,
2202                                              reloc_type[2]);
2203
2204                       /* These relocations can have an addend that won't fit in
2205                          4 octets for 64bit assembly.  */
2206                       if (HAVE_64BIT_GPRS &&
2207                           (*reloc_type == BFD_RELOC_16
2208                            || *reloc_type == BFD_RELOC_32
2209                            || *reloc_type == BFD_RELOC_MIPS_JMP
2210                            || *reloc_type == BFD_RELOC_HI16_S
2211                            || *reloc_type == BFD_RELOC_LO16
2212                            || *reloc_type == BFD_RELOC_GPREL16
2213                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2214                            || *reloc_type == BFD_RELOC_GPREL32
2215                            || *reloc_type == BFD_RELOC_64
2216                            || *reloc_type == BFD_RELOC_CTOR
2217                            || *reloc_type == BFD_RELOC_MIPS_SUB
2218                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2219                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2220                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2221                            || *reloc_type == BFD_RELOC_MIPS_REL16
2222                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2223                         fixp[2]->fx_no_overflow = 1;
2224                     }
2225                 }
2226             }
2227         }
2228     }
2229
2230   if (! mips_opts.mips16)
2231     {
2232       md_number_to_chars (f, ip->insn_opcode, 4);
2233 #ifdef OBJ_ELF
2234       dwarf2_emit_insn (4);
2235 #endif
2236     }
2237   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2238     {
2239       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2240       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2241 #ifdef OBJ_ELF
2242       dwarf2_emit_insn (4);
2243 #endif
2244     }
2245   else
2246     {
2247       if (ip->use_extend)
2248         {
2249           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2250           f += 2;
2251         }
2252       md_number_to_chars (f, ip->insn_opcode, 2);
2253 #ifdef OBJ_ELF
2254       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2255 #endif
2256     }
2257
2258   /* Update the register mask information.  */
2259   if (! mips_opts.mips16)
2260     {
2261       if (pinfo & INSN_WRITE_GPR_D)
2262         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2263       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2264         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2265       if (pinfo & INSN_READ_GPR_S)
2266         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2267       if (pinfo & INSN_WRITE_GPR_31)
2268         mips_gprmask |= 1 << RA;
2269       if (pinfo & INSN_WRITE_FPR_D)
2270         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2271       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2272         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2273       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2274         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2275       if ((pinfo & INSN_READ_FPR_R) != 0)
2276         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2277       if (pinfo & INSN_COP)
2278         {
2279           /* We don't keep enough information to sort these cases out.
2280              The itbl support does keep this information however, although
2281              we currently don't support itbl fprmats as part of the cop
2282              instruction.  May want to add this support in the future.  */
2283         }
2284       /* Never set the bit for $0, which is always zero.  */
2285       mips_gprmask &= ~1 << 0;
2286     }
2287   else
2288     {
2289       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2290         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2291                               & MIPS16OP_MASK_RX);
2292       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2293         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2294                               & MIPS16OP_MASK_RY);
2295       if (pinfo & MIPS16_INSN_WRITE_Z)
2296         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2297                               & MIPS16OP_MASK_RZ);
2298       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2299         mips_gprmask |= 1 << TREG;
2300       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2301         mips_gprmask |= 1 << SP;
2302       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2303         mips_gprmask |= 1 << RA;
2304       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2305         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2306       if (pinfo & MIPS16_INSN_READ_Z)
2307         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2308                               & MIPS16OP_MASK_MOVE32Z);
2309       if (pinfo & MIPS16_INSN_READ_GPR_X)
2310         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2311                               & MIPS16OP_MASK_REGR32);
2312     }
2313
2314   if (place == NULL && ! mips_opts.noreorder)
2315     {
2316       /* Filling the branch delay slot is more complex.  We try to
2317          switch the branch with the previous instruction, which we can
2318          do if the previous instruction does not set up a condition
2319          that the branch tests and if the branch is not itself the
2320          target of any branch.  */
2321       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2322           || (pinfo & INSN_COND_BRANCH_DELAY))
2323         {
2324           if (mips_optimize < 2
2325               /* If we have seen .set volatile or .set nomove, don't
2326                  optimize.  */
2327               || mips_opts.nomove != 0
2328               /* If we had to emit any NOP instructions, then we
2329                  already know we can not swap.  */
2330               || nops != 0
2331               /* If we don't even know the previous insn, we can not
2332                  swap.  */
2333               || ! prev_insn_valid
2334               /* If the previous insn is already in a branch delay
2335                  slot, then we can not swap.  */
2336               || prev_insn_is_delay_slot
2337               /* If the previous previous insn was in a .set
2338                  noreorder, we can't swap.  Actually, the MIPS
2339                  assembler will swap in this situation.  However, gcc
2340                  configured -with-gnu-as will generate code like
2341                    .set noreorder
2342                    lw   $4,XXX
2343                    .set reorder
2344                    INSN
2345                    bne  $4,$0,foo
2346                  in which we can not swap the bne and INSN.  If gcc is
2347                  not configured -with-gnu-as, it does not output the
2348                  .set pseudo-ops.  We don't have to check
2349                  prev_insn_unreordered, because prev_insn_valid will
2350                  be 0 in that case.  We don't want to use
2351                  prev_prev_insn_valid, because we do want to be able
2352                  to swap at the start of a function.  */
2353               || prev_prev_insn_unreordered
2354               /* If the branch is itself the target of a branch, we
2355                  can not swap.  We cheat on this; all we check for is
2356                  whether there is a label on this instruction.  If
2357                  there are any branches to anything other than a
2358                  label, users must use .set noreorder.  */
2359               || insn_labels != NULL
2360               /* If the previous instruction is in a variant frag, we
2361                  can not do the swap.  This does not apply to the
2362                  mips16, which uses variant frags for different
2363                  purposes.  */
2364               || (! mips_opts.mips16
2365                   && prev_insn_frag->fr_type == rs_machine_dependent)
2366               /* If the branch reads the condition codes, we don't
2367                  even try to swap, because in the sequence
2368                    ctc1 $X,$31
2369                    INSN
2370                    INSN
2371                    bc1t LABEL
2372                  we can not swap, and I don't feel like handling that
2373                  case.  */
2374               || (! mips_opts.mips16
2375                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2376                   && (pinfo & INSN_READ_COND_CODE))
2377               /* We can not swap with an instruction that requires a
2378                  delay slot, becase the target of the branch might
2379                  interfere with that instruction.  */
2380               || (! mips_opts.mips16
2381                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2382                   && (prev_pinfo
2383               /* Itbl support may require additional care here.  */
2384                       & (INSN_LOAD_COPROC_DELAY
2385                          | INSN_COPROC_MOVE_DELAY
2386                          | INSN_WRITE_COND_CODE)))
2387               || (! (hilo_interlocks
2388                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2389                   && (prev_pinfo
2390                       & (INSN_READ_LO
2391                          | INSN_READ_HI)))
2392               || (! mips_opts.mips16
2393                   && ! gpr_interlocks
2394                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2395               || (! mips_opts.mips16
2396                   && mips_opts.isa == ISA_MIPS1
2397                   /* Itbl support may require additional care here.  */
2398                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2399               /* We can not swap with a branch instruction.  */
2400               || (prev_pinfo
2401                   & (INSN_UNCOND_BRANCH_DELAY
2402                      | INSN_COND_BRANCH_DELAY
2403                      | INSN_COND_BRANCH_LIKELY))
2404               /* We do not swap with a trap instruction, since it
2405                  complicates trap handlers to have the trap
2406                  instruction be in a delay slot.  */
2407               || (prev_pinfo & INSN_TRAP)
2408               /* If the branch reads a register that the previous
2409                  instruction sets, we can not swap.  */
2410               || (! mips_opts.mips16
2411                   && (prev_pinfo & INSN_WRITE_GPR_T)
2412                   && insn_uses_reg (ip,
2413                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2414                                      & OP_MASK_RT),
2415                                     MIPS_GR_REG))
2416               || (! mips_opts.mips16
2417                   && (prev_pinfo & INSN_WRITE_GPR_D)
2418                   && insn_uses_reg (ip,
2419                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2420                                      & OP_MASK_RD),
2421                                     MIPS_GR_REG))
2422               || (mips_opts.mips16
2423                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2424                        && insn_uses_reg (ip,
2425                                          ((prev_insn.insn_opcode
2426                                            >> MIPS16OP_SH_RX)
2427                                           & MIPS16OP_MASK_RX),
2428                                          MIPS16_REG))
2429                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2430                           && insn_uses_reg (ip,
2431                                             ((prev_insn.insn_opcode
2432                                               >> MIPS16OP_SH_RY)
2433                                              & MIPS16OP_MASK_RY),
2434                                             MIPS16_REG))
2435                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2436                           && insn_uses_reg (ip,
2437                                             ((prev_insn.insn_opcode
2438                                               >> MIPS16OP_SH_RZ)
2439                                              & MIPS16OP_MASK_RZ),
2440                                             MIPS16_REG))
2441                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2442                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2443                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2444                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2445                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2446                           && insn_uses_reg (ip,
2447                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2448                                                                      insn_opcode),
2449                                             MIPS_GR_REG))))
2450               /* If the branch writes a register that the previous
2451                  instruction sets, we can not swap (we know that
2452                  branches write only to RD or to $31).  */
2453               || (! mips_opts.mips16
2454                   && (prev_pinfo & INSN_WRITE_GPR_T)
2455                   && (((pinfo & INSN_WRITE_GPR_D)
2456                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2457                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2458                       || ((pinfo & INSN_WRITE_GPR_31)
2459                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2460                                & OP_MASK_RT)
2461                               == RA))))
2462               || (! mips_opts.mips16
2463                   && (prev_pinfo & INSN_WRITE_GPR_D)
2464                   && (((pinfo & INSN_WRITE_GPR_D)
2465                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2466                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2467                       || ((pinfo & INSN_WRITE_GPR_31)
2468                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2469                                & OP_MASK_RD)
2470                               == RA))))
2471               || (mips_opts.mips16
2472                   && (pinfo & MIPS16_INSN_WRITE_31)
2473                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2474                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2475                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2476                               == RA))))
2477               /* If the branch writes a register that the previous
2478                  instruction reads, we can not swap (we know that
2479                  branches only write to RD or to $31).  */
2480               || (! mips_opts.mips16
2481                   && (pinfo & INSN_WRITE_GPR_D)
2482                   && insn_uses_reg (&prev_insn,
2483                                     ((ip->insn_opcode >> OP_SH_RD)
2484                                      & OP_MASK_RD),
2485                                     MIPS_GR_REG))
2486               || (! mips_opts.mips16
2487                   && (pinfo & INSN_WRITE_GPR_31)
2488                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2489               || (mips_opts.mips16
2490                   && (pinfo & MIPS16_INSN_WRITE_31)
2491                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2492               /* If we are generating embedded PIC code, the branch
2493                  might be expanded into a sequence which uses $at, so
2494                  we can't swap with an instruction which reads it.  */
2495               || (mips_pic == EMBEDDED_PIC
2496                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2497               /* If the previous previous instruction has a load
2498                  delay, and sets a register that the branch reads, we
2499                  can not swap.  */
2500               || (! mips_opts.mips16
2501                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2502               /* Itbl support may require additional care here.  */
2503                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2504                       || (! gpr_interlocks
2505                           && (prev_prev_insn.insn_mo->pinfo
2506                               & INSN_LOAD_MEMORY_DELAY)))
2507                   && insn_uses_reg (ip,
2508                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2509                                      & OP_MASK_RT),
2510                                     MIPS_GR_REG))
2511               /* If one instruction sets a condition code and the
2512                  other one uses a condition code, we can not swap.  */
2513               || ((pinfo & INSN_READ_COND_CODE)
2514                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2515               || ((pinfo & INSN_WRITE_COND_CODE)
2516                   && (prev_pinfo & INSN_READ_COND_CODE))
2517               /* If the previous instruction uses the PC, we can not
2518                  swap.  */
2519               || (mips_opts.mips16
2520                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2521               /* If the previous instruction was extended, we can not
2522                  swap.  */
2523               || (mips_opts.mips16 && prev_insn_extended)
2524               /* If the previous instruction had a fixup in mips16
2525                  mode, we can not swap.  This normally means that the
2526                  previous instruction was a 4 byte branch anyhow.  */
2527               || (mips_opts.mips16 && prev_insn_fixp[0])
2528               /* If the previous instruction is a sync, sync.l, or
2529                  sync.p, we can not swap.  */
2530               || (prev_pinfo & INSN_SYNC))
2531             {
2532               /* We could do even better for unconditional branches to
2533                  portions of this object file; we could pick up the
2534                  instruction at the destination, put it in the delay
2535                  slot, and bump the destination address.  */
2536               emit_nop ();
2537               /* Update the previous insn information.  */
2538               prev_prev_insn = *ip;
2539               prev_insn.insn_mo = &dummy_opcode;
2540             }
2541           else
2542             {
2543               /* It looks like we can actually do the swap.  */
2544               if (! mips_opts.mips16)
2545                 {
2546                   char *prev_f;
2547                   char temp[4];
2548
2549                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2550                   memcpy (temp, prev_f, 4);
2551                   memcpy (prev_f, f, 4);
2552                   memcpy (f, temp, 4);
2553                   if (prev_insn_fixp[0])
2554                     {
2555                       prev_insn_fixp[0]->fx_frag = frag_now;
2556                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2557                     }
2558                   if (prev_insn_fixp[1])
2559                     {
2560                       prev_insn_fixp[1]->fx_frag = frag_now;
2561                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2562                     }
2563                   if (prev_insn_fixp[2])
2564                     {
2565                       prev_insn_fixp[2]->fx_frag = frag_now;
2566                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2567                     }
2568                   if (fixp[0])
2569                     {
2570                       fixp[0]->fx_frag = prev_insn_frag;
2571                       fixp[0]->fx_where = prev_insn_where;
2572                     }
2573                   if (fixp[1])
2574                     {
2575                       fixp[1]->fx_frag = prev_insn_frag;
2576                       fixp[1]->fx_where = prev_insn_where;
2577                     }
2578                   if (fixp[2])
2579                     {
2580                       fixp[2]->fx_frag = prev_insn_frag;
2581                       fixp[2]->fx_where = prev_insn_where;
2582                     }
2583                 }
2584               else
2585                 {
2586                   char *prev_f;
2587                   char temp[2];
2588
2589                   assert (prev_insn_fixp[0] == NULL);
2590                   assert (prev_insn_fixp[1] == NULL);
2591                   assert (prev_insn_fixp[2] == NULL);
2592                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2593                   memcpy (temp, prev_f, 2);
2594                   memcpy (prev_f, f, 2);
2595                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2596                     {
2597                       assert (*reloc_type == BFD_RELOC_UNUSED);
2598                       memcpy (f, temp, 2);
2599                     }
2600                   else
2601                     {
2602                       memcpy (f, f + 2, 2);
2603                       memcpy (f + 2, temp, 2);
2604                     }
2605                   if (fixp[0])
2606                     {
2607                       fixp[0]->fx_frag = prev_insn_frag;
2608                       fixp[0]->fx_where = prev_insn_where;
2609                     }
2610                   if (fixp[1])
2611                     {
2612                       fixp[1]->fx_frag = prev_insn_frag;
2613                       fixp[1]->fx_where = prev_insn_where;
2614                     }
2615                   if (fixp[2])
2616                     {
2617                       fixp[2]->fx_frag = prev_insn_frag;
2618                       fixp[2]->fx_where = prev_insn_where;
2619                     }
2620                 }
2621
2622               /* Update the previous insn information; leave prev_insn
2623                  unchanged.  */
2624               prev_prev_insn = *ip;
2625             }
2626           prev_insn_is_delay_slot = 1;
2627
2628           /* If that was an unconditional branch, forget the previous
2629              insn information.  */
2630           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2631             {
2632               prev_prev_insn.insn_mo = &dummy_opcode;
2633               prev_insn.insn_mo = &dummy_opcode;
2634             }
2635
2636           prev_insn_fixp[0] = NULL;
2637           prev_insn_fixp[1] = NULL;
2638           prev_insn_fixp[2] = NULL;
2639           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2640           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2641           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2642           prev_insn_extended = 0;
2643         }
2644       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2645         {
2646           /* We don't yet optimize a branch likely.  What we should do
2647              is look at the target, copy the instruction found there
2648              into the delay slot, and increment the branch to jump to
2649              the next instruction.  */
2650           emit_nop ();
2651           /* Update the previous insn information.  */
2652           prev_prev_insn = *ip;
2653           prev_insn.insn_mo = &dummy_opcode;
2654           prev_insn_fixp[0] = NULL;
2655           prev_insn_fixp[1] = NULL;
2656           prev_insn_fixp[2] = NULL;
2657           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2658           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2659           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2660           prev_insn_extended = 0;
2661         }
2662       else
2663         {
2664           /* Update the previous insn information.  */
2665           if (nops > 0)
2666             prev_prev_insn.insn_mo = &dummy_opcode;
2667           else
2668             prev_prev_insn = prev_insn;
2669           prev_insn = *ip;
2670
2671           /* Any time we see a branch, we always fill the delay slot
2672              immediately; since this insn is not a branch, we know it
2673              is not in a delay slot.  */
2674           prev_insn_is_delay_slot = 0;
2675
2676           prev_insn_fixp[0] = fixp[0];
2677           prev_insn_fixp[1] = fixp[1];
2678           prev_insn_fixp[2] = fixp[2];
2679           prev_insn_reloc_type[0] = reloc_type[0];
2680           prev_insn_reloc_type[1] = reloc_type[1];
2681           prev_insn_reloc_type[2] = reloc_type[2];
2682           if (mips_opts.mips16)
2683             prev_insn_extended = (ip->use_extend
2684                                   || *reloc_type > BFD_RELOC_UNUSED);
2685         }
2686
2687       prev_prev_insn_unreordered = prev_insn_unreordered;
2688       prev_insn_unreordered = 0;
2689       prev_insn_frag = frag_now;
2690       prev_insn_where = f - frag_now->fr_literal;
2691       prev_insn_valid = 1;
2692     }
2693   else if (place == NULL)
2694     {
2695       /* We need to record a bit of information even when we are not
2696          reordering, in order to determine the base address for mips16
2697          PC relative relocs.  */
2698       prev_prev_insn = prev_insn;
2699       prev_insn = *ip;
2700       prev_insn_reloc_type[0] = reloc_type[0];
2701       prev_insn_reloc_type[1] = reloc_type[1];
2702       prev_insn_reloc_type[2] = reloc_type[2];
2703       prev_prev_insn_unreordered = prev_insn_unreordered;
2704       prev_insn_unreordered = 1;
2705     }
2706
2707   /* We just output an insn, so the next one doesn't have a label.  */
2708   mips_clear_insn_labels ();
2709
2710   /* We must ensure that a fixup associated with an unmatched %hi
2711      reloc does not become a variant frag.  Otherwise, the
2712      rearrangement of %hi relocs in frob_file may confuse
2713      tc_gen_reloc.  */
2714   if (unmatched_reloc_p)
2715     {
2716       frag_wane (frag_now);
2717       frag_new (0);
2718     }
2719 }
2720
2721 /* This function forgets that there was any previous instruction or
2722    label.  If PRESERVE is non-zero, it remembers enough information to
2723    know whether nops are needed before a noreorder section.  */
2724
2725 static void
2726 mips_no_prev_insn (preserve)
2727      int preserve;
2728 {
2729   if (! preserve)
2730     {
2731       prev_insn.insn_mo = &dummy_opcode;
2732       prev_prev_insn.insn_mo = &dummy_opcode;
2733       prev_nop_frag = NULL;
2734       prev_nop_frag_holds = 0;
2735       prev_nop_frag_required = 0;
2736       prev_nop_frag_since = 0;
2737     }
2738   prev_insn_valid = 0;
2739   prev_insn_is_delay_slot = 0;
2740   prev_insn_unreordered = 0;
2741   prev_insn_extended = 0;
2742   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2743   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2744   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2745   prev_prev_insn_unreordered = 0;
2746   mips_clear_insn_labels ();
2747 }
2748
2749 /* This function must be called whenever we turn on noreorder or emit
2750    something other than instructions.  It inserts any NOPS which might
2751    be needed by the previous instruction, and clears the information
2752    kept for the previous instructions.  The INSNS parameter is true if
2753    instructions are to follow.  */
2754
2755 static void
2756 mips_emit_delays (insns)
2757      bfd_boolean insns;
2758 {
2759   if (! mips_opts.noreorder)
2760     {
2761       int nops;
2762
2763       nops = 0;
2764       if ((! mips_opts.mips16
2765            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2766            && (! cop_interlocks
2767                && (prev_insn.insn_mo->pinfo
2768                    & (INSN_LOAD_COPROC_DELAY
2769                       | INSN_COPROC_MOVE_DELAY
2770                       | INSN_WRITE_COND_CODE))))
2771           || (! hilo_interlocks
2772               && (prev_insn.insn_mo->pinfo
2773                   & (INSN_READ_LO
2774                      | INSN_READ_HI)))
2775           || (! mips_opts.mips16
2776               && ! gpr_interlocks
2777               && (prev_insn.insn_mo->pinfo
2778                   & INSN_LOAD_MEMORY_DELAY))
2779           || (! mips_opts.mips16
2780               && mips_opts.isa == ISA_MIPS1
2781               && (prev_insn.insn_mo->pinfo
2782                   & INSN_COPROC_MEMORY_DELAY)))
2783         {
2784           /* Itbl support may require additional care here.  */
2785           ++nops;
2786           if ((! mips_opts.mips16
2787                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2788                && (! cop_interlocks
2789                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2790               || (! hilo_interlocks
2791                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2792                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2793             ++nops;
2794
2795           if (prev_insn_unreordered)
2796             nops = 0;
2797         }
2798       else if ((! mips_opts.mips16
2799                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2800                 && (! cop_interlocks
2801                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2802                || (! hilo_interlocks
2803                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2804                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2805         {
2806           /* Itbl support may require additional care here.  */
2807           if (! prev_prev_insn_unreordered)
2808             ++nops;
2809         }
2810
2811       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2812         {
2813           int min_nops = 0;
2814           const char *pn = prev_insn.insn_mo->name;
2815           if (strncmp(pn, "macc", 4) == 0
2816               || strncmp(pn, "dmacc", 5) == 0
2817               || strncmp(pn, "dmult", 5) == 0)
2818             {
2819               min_nops = 1;
2820             }
2821           if (nops < min_nops)
2822             nops = min_nops;
2823         }
2824
2825       if (nops > 0)
2826         {
2827           struct insn_label_list *l;
2828
2829           if (insns)
2830             {
2831               /* Record the frag which holds the nop instructions, so
2832                  that we can remove them if we don't need them.  */
2833               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2834               prev_nop_frag = frag_now;
2835               prev_nop_frag_holds = nops;
2836               prev_nop_frag_required = 0;
2837               prev_nop_frag_since = 0;
2838             }
2839
2840           for (; nops > 0; --nops)
2841             emit_nop ();
2842
2843           if (insns)
2844             {
2845               /* Move on to a new frag, so that it is safe to simply
2846                  decrease the size of prev_nop_frag.  */
2847               frag_wane (frag_now);
2848               frag_new (0);
2849             }
2850
2851           for (l = insn_labels; l != NULL; l = l->next)
2852             {
2853               valueT val;
2854
2855               assert (S_GET_SEGMENT (l->label) == now_seg);
2856               symbol_set_frag (l->label, frag_now);
2857               val = (valueT) frag_now_fix ();
2858               /* mips16 text labels are stored as odd.  */
2859               if (mips_opts.mips16)
2860                 ++val;
2861               S_SET_VALUE (l->label, val);
2862             }
2863         }
2864     }
2865
2866   /* Mark instruction labels in mips16 mode.  */
2867   if (insns)
2868     mips16_mark_labels ();
2869
2870   mips_no_prev_insn (insns);
2871 }
2872
2873 /* Build an instruction created by a macro expansion.  This is passed
2874    a pointer to the count of instructions created so far, an
2875    expression, the name of the instruction to build, an operand format
2876    string, and corresponding arguments.  */
2877
2878 #ifdef USE_STDARG
2879 static void
2880 macro_build (char *place,
2881              int *counter,
2882              expressionS * ep,
2883              const char *name,
2884              const char *fmt,
2885              ...)
2886 #else
2887 static void
2888 macro_build (place, counter, ep, name, fmt, va_alist)
2889      char *place;
2890      int *counter;
2891      expressionS *ep;
2892      const char *name;
2893      const char *fmt;
2894      va_dcl
2895 #endif
2896 {
2897   struct mips_cl_insn insn;
2898   bfd_reloc_code_real_type r[3];
2899   va_list args;
2900
2901 #ifdef USE_STDARG
2902   va_start (args, fmt);
2903 #else
2904   va_start (args);
2905 #endif
2906
2907   /*
2908    * If the macro is about to expand into a second instruction,
2909    * print a warning if needed. We need to pass ip as a parameter
2910    * to generate a better warning message here...
2911    */
2912   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2913     as_warn (_("Macro instruction expanded into multiple instructions"));
2914
2915   /*
2916    * If the macro is about to expand into a second instruction,
2917    * and it is in a delay slot, print a warning.
2918    */
2919   if (place == NULL
2920       && *counter == 1
2921       && mips_opts.noreorder
2922       && (prev_prev_insn.insn_mo->pinfo
2923           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2924              | INSN_COND_BRANCH_LIKELY)) != 0)
2925     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2926
2927   if (place == NULL)
2928     ++*counter;         /* bump instruction counter */
2929
2930   if (mips_opts.mips16)
2931     {
2932       mips16_macro_build (place, counter, ep, name, fmt, args);
2933       va_end (args);
2934       return;
2935     }
2936
2937   r[0] = BFD_RELOC_UNUSED;
2938   r[1] = BFD_RELOC_UNUSED;
2939   r[2] = BFD_RELOC_UNUSED;
2940   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2941   assert (insn.insn_mo);
2942   assert (strcmp (name, insn.insn_mo->name) == 0);
2943
2944   /* Search until we get a match for NAME.  */
2945   while (1)
2946     {
2947       /* It is assumed here that macros will never generate
2948          MDMX or MIPS-3D instructions.  */
2949       if (strcmp (fmt, insn.insn_mo->args) == 0
2950           && insn.insn_mo->pinfo != INSN_MACRO
2951           && OPCODE_IS_MEMBER (insn.insn_mo,
2952                                (mips_opts.isa
2953                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2954                                mips_arch)
2955           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2956         break;
2957
2958       ++insn.insn_mo;
2959       assert (insn.insn_mo->name);
2960       assert (strcmp (name, insn.insn_mo->name) == 0);
2961     }
2962
2963   insn.insn_opcode = insn.insn_mo->match;
2964   for (;;)
2965     {
2966       switch (*fmt++)
2967         {
2968         case '\0':
2969           break;
2970
2971         case ',':
2972         case '(':
2973         case ')':
2974           continue;
2975
2976         case 't':
2977         case 'w':
2978         case 'E':
2979           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2980           continue;
2981
2982         case 'c':
2983           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2984           continue;
2985
2986         case 'T':
2987         case 'W':
2988           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2989           continue;
2990
2991         case 'd':
2992         case 'G':
2993         case 'K':
2994           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2995           continue;
2996
2997         case 'U':
2998           {
2999             int tmp = va_arg (args, int);
3000
3001             insn.insn_opcode |= tmp << OP_SH_RT;
3002             insn.insn_opcode |= tmp << OP_SH_RD;
3003             continue;
3004           }
3005
3006         case 'V':
3007         case 'S':
3008           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3009           continue;
3010
3011         case 'z':
3012           continue;
3013
3014         case '<':
3015           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3016           continue;
3017
3018         case 'D':
3019           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3020           continue;
3021
3022         case 'B':
3023           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3024           continue;
3025
3026         case 'J':
3027           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3028           continue;
3029
3030         case 'q':
3031           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3032           continue;
3033
3034         case 'b':
3035         case 's':
3036         case 'r':
3037         case 'v':
3038           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3039           continue;
3040
3041         case 'i':
3042         case 'j':
3043         case 'o':
3044           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3045           assert (*r == BFD_RELOC_GPREL16
3046                   || *r == BFD_RELOC_MIPS_LITERAL
3047                   || *r == BFD_RELOC_MIPS_HIGHER
3048                   || *r == BFD_RELOC_HI16_S
3049                   || *r == BFD_RELOC_LO16
3050                   || *r == BFD_RELOC_MIPS_GOT16
3051                   || *r == BFD_RELOC_MIPS_CALL16
3052                   || *r == BFD_RELOC_MIPS_GOT_DISP
3053                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3054                   || *r == BFD_RELOC_MIPS_GOT_OFST
3055                   || *r == BFD_RELOC_MIPS_GOT_LO16
3056                   || *r == BFD_RELOC_MIPS_CALL_LO16
3057                   || (ep->X_op == O_subtract
3058                       && *r == BFD_RELOC_PCREL_LO16));
3059           continue;
3060
3061         case 'u':
3062           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3063           assert (ep != NULL
3064                   && (ep->X_op == O_constant
3065                       || (ep->X_op == O_symbol
3066                           && (*r == BFD_RELOC_MIPS_HIGHEST
3067                               || *r == BFD_RELOC_HI16_S
3068                               || *r == BFD_RELOC_HI16
3069                               || *r == BFD_RELOC_GPREL16
3070                               || *r == BFD_RELOC_MIPS_GOT_HI16
3071                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3072                       || (ep->X_op == O_subtract
3073                           && *r == BFD_RELOC_PCREL_HI16_S)));
3074           continue;
3075
3076         case 'p':
3077           assert (ep != NULL);
3078           /*
3079            * This allows macro() to pass an immediate expression for
3080            * creating short branches without creating a symbol.
3081            * Note that the expression still might come from the assembly
3082            * input, in which case the value is not checked for range nor
3083            * is a relocation entry generated (yuck).
3084            */
3085           if (ep->X_op == O_constant)
3086             {
3087               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3088               ep = NULL;
3089             }
3090           else
3091             *r = BFD_RELOC_16_PCREL_S2;
3092           continue;
3093
3094         case 'a':
3095           assert (ep != NULL);
3096           *r = BFD_RELOC_MIPS_JMP;
3097           continue;
3098
3099         case 'C':
3100           insn.insn_opcode |= va_arg (args, unsigned long);
3101           continue;
3102
3103         default:
3104           internalError ();
3105         }
3106       break;
3107     }
3108   va_end (args);
3109   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3110
3111   append_insn (place, &insn, ep, r);
3112 }
3113
3114 static void
3115 mips16_macro_build (place, counter, ep, name, fmt, args)
3116      char *place;
3117      int *counter ATTRIBUTE_UNUSED;
3118      expressionS *ep;
3119      const char *name;
3120      const char *fmt;
3121      va_list args;
3122 {
3123   struct mips_cl_insn insn;
3124   bfd_reloc_code_real_type r[3]
3125     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3126
3127   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3128   assert (insn.insn_mo);
3129   assert (strcmp (name, insn.insn_mo->name) == 0);
3130
3131   while (strcmp (fmt, insn.insn_mo->args) != 0
3132          || insn.insn_mo->pinfo == INSN_MACRO)
3133     {
3134       ++insn.insn_mo;
3135       assert (insn.insn_mo->name);
3136       assert (strcmp (name, insn.insn_mo->name) == 0);
3137     }
3138
3139   insn.insn_opcode = insn.insn_mo->match;
3140   insn.use_extend = FALSE;
3141
3142   for (;;)
3143     {
3144       int c;
3145
3146       c = *fmt++;
3147       switch (c)
3148         {
3149         case '\0':
3150           break;
3151
3152         case ',':
3153         case '(':
3154         case ')':
3155           continue;
3156
3157         case 'y':
3158         case 'w':
3159           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3160           continue;
3161
3162         case 'x':
3163         case 'v':
3164           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3165           continue;
3166
3167         case 'z':
3168           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3169           continue;
3170
3171         case 'Z':
3172           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3173           continue;
3174
3175         case '0':
3176         case 'S':
3177         case 'P':
3178         case 'R':
3179           continue;
3180
3181         case 'X':
3182           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3183           continue;
3184
3185         case 'Y':
3186           {
3187             int regno;
3188
3189             regno = va_arg (args, int);
3190             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3191             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3192           }
3193           continue;
3194
3195         case '<':
3196         case '>':
3197         case '4':
3198         case '5':
3199         case 'H':
3200         case 'W':
3201         case 'D':
3202         case 'j':
3203         case '8':
3204         case 'V':
3205         case 'C':
3206         case 'U':
3207         case 'k':
3208         case 'K':
3209         case 'p':
3210         case 'q':
3211           {
3212             assert (ep != NULL);
3213
3214             if (ep->X_op != O_constant)
3215               *r = (int) BFD_RELOC_UNUSED + c;
3216             else
3217               {
3218                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3219                               FALSE, &insn.insn_opcode, &insn.use_extend,
3220                               &insn.extend);
3221                 ep = NULL;
3222                 *r = BFD_RELOC_UNUSED;
3223               }
3224           }
3225           continue;
3226
3227         case '6':
3228           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3229           continue;
3230         }
3231
3232       break;
3233     }
3234
3235   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3236
3237   append_insn (place, &insn, ep, r);
3238 }
3239
3240 /*
3241  * Generate a "jalr" instruction with a relocation hint to the called
3242  * function.  This occurs in NewABI PIC code.
3243  */
3244 static void
3245 macro_build_jalr (icnt, ep)
3246      int icnt;
3247      expressionS *ep;
3248 {
3249   char *f;
3250
3251   if (HAVE_NEWABI)
3252     {
3253       frag_grow (4);
3254       f = frag_more (0);
3255     }
3256   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3257                RA, PIC_CALL_REG);
3258   if (HAVE_NEWABI)
3259     fix_new_exp (frag_now, f - frag_now->fr_literal,
3260                  0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3261 }
3262
3263 /*
3264  * Generate a "lui" instruction.
3265  */
3266 static void
3267 macro_build_lui (place, counter, ep, regnum)
3268      char *place;
3269      int *counter;
3270      expressionS *ep;
3271      int regnum;
3272 {
3273   expressionS high_expr;
3274   struct mips_cl_insn insn;
3275   bfd_reloc_code_real_type r[3]
3276     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3277   const char *name = "lui";
3278   const char *fmt = "t,u";
3279
3280   assert (! mips_opts.mips16);
3281
3282   if (place == NULL)
3283     high_expr = *ep;
3284   else
3285     {
3286       high_expr.X_op = O_constant;
3287       high_expr.X_add_number = ep->X_add_number;
3288     }
3289
3290   if (high_expr.X_op == O_constant)
3291     {
3292       /* we can compute the instruction now without a relocation entry */
3293       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3294                                 >> 16) & 0xffff;
3295       *r = BFD_RELOC_UNUSED;
3296     }
3297   else
3298     {
3299       assert (ep->X_op == O_symbol);
3300       /* _gp_disp is a special case, used from s_cpload.  */
3301       assert (mips_pic == NO_PIC
3302               || (! HAVE_NEWABI
3303                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3304       *r = BFD_RELOC_HI16_S;
3305     }
3306
3307   /*
3308    * If the macro is about to expand into a second instruction,
3309    * print a warning if needed. We need to pass ip as a parameter
3310    * to generate a better warning message here...
3311    */
3312   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3313     as_warn (_("Macro instruction expanded into multiple instructions"));
3314
3315   if (place == NULL)
3316     ++*counter;         /* bump instruction counter */
3317
3318   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3319   assert (insn.insn_mo);
3320   assert (strcmp (name, insn.insn_mo->name) == 0);
3321   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3322
3323   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3324   if (*r == BFD_RELOC_UNUSED)
3325     {
3326       insn.insn_opcode |= high_expr.X_add_number;
3327       append_insn (place, &insn, NULL, r);
3328     }
3329   else
3330     append_insn (place, &insn, &high_expr, r);
3331 }
3332
3333 /* Generate a sequence of instructions to do a load or store from a constant
3334    offset off of a base register (breg) into/from a target register (treg),
3335    using AT if necessary.  */
3336 static void
3337 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3338      char *place;
3339      int *counter;
3340      expressionS *ep;
3341      const char *op;
3342      int treg, breg;
3343 {
3344   assert (ep->X_op == O_constant);
3345
3346   /* Right now, this routine can only handle signed 32-bit contants.  */
3347   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3348     as_warn (_("operand overflow"));
3349
3350   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3351     {
3352       /* Signed 16-bit offset will fit in the op.  Easy!  */
3353       macro_build (place, counter, ep, op, "t,o(b)", treg,
3354                    (int) BFD_RELOC_LO16, breg);
3355     }
3356   else
3357     {
3358       /* 32-bit offset, need multiple instructions and AT, like:
3359            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3360            addu     $tempreg,$tempreg,$breg
3361            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3362          to handle the complete offset.  */
3363       macro_build_lui (place, counter, ep, AT);
3364       if (place != NULL)
3365         place += 4;
3366       macro_build (place, counter, (expressionS *) NULL,
3367                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3368                    "d,v,t", AT, AT, breg);
3369       if (place != NULL)
3370         place += 4;
3371       macro_build (place, counter, ep, op, "t,o(b)", treg,
3372                    (int) BFD_RELOC_LO16, AT);
3373
3374       if (mips_opts.noat)
3375         as_warn (_("Macro used $at after \".set noat\""));
3376     }
3377 }
3378
3379 /*                      set_at()
3380  * Generates code to set the $at register to true (one)
3381  * if reg is less than the immediate expression.
3382  */
3383 static void
3384 set_at (counter, reg, unsignedp)
3385      int *counter;
3386      int reg;
3387      int unsignedp;
3388 {
3389   if (imm_expr.X_op == O_constant
3390       && imm_expr.X_add_number >= -0x8000
3391       && imm_expr.X_add_number < 0x8000)
3392     macro_build ((char *) NULL, counter, &imm_expr,
3393                  unsignedp ? "sltiu" : "slti",
3394                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3395   else
3396     {
3397       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3398       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3399                    unsignedp ? "sltu" : "slt",
3400                    "d,v,t", AT, reg, AT);
3401     }
3402 }
3403
3404 /* Warn if an expression is not a constant.  */
3405
3406 static void
3407 check_absolute_expr (ip, ex)
3408      struct mips_cl_insn *ip;
3409      expressionS *ex;
3410 {
3411   if (ex->X_op == O_big)
3412     as_bad (_("unsupported large constant"));
3413   else if (ex->X_op != O_constant)
3414     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3415 }
3416
3417 /* Count the leading zeroes by performing a binary chop. This is a
3418    bulky bit of source, but performance is a LOT better for the
3419    majority of values than a simple loop to count the bits:
3420        for (lcnt = 0; (lcnt < 32); lcnt++)
3421          if ((v) & (1 << (31 - lcnt)))
3422            break;
3423   However it is not code size friendly, and the gain will drop a bit
3424   on certain cached systems.
3425 */
3426 #define COUNT_TOP_ZEROES(v)             \
3427   (((v) & ~0xffff) == 0                 \
3428    ? ((v) & ~0xff) == 0                 \
3429      ? ((v) & ~0xf) == 0                \
3430        ? ((v) & ~0x3) == 0              \
3431          ? ((v) & ~0x1) == 0            \
3432            ? !(v)                       \
3433              ? 32                       \
3434              : 31                       \
3435            : 30                         \
3436          : ((v) & ~0x7) == 0            \
3437            ? 29                         \
3438            : 28                         \
3439        : ((v) & ~0x3f) == 0             \
3440          ? ((v) & ~0x1f) == 0           \
3441            ? 27                         \
3442            : 26                         \
3443          : ((v) & ~0x7f) == 0           \
3444            ? 25                         \
3445            : 24                         \
3446      : ((v) & ~0xfff) == 0              \
3447        ? ((v) & ~0x3ff) == 0            \
3448          ? ((v) & ~0x1ff) == 0          \
3449            ? 23                         \
3450            : 22                         \
3451          : ((v) & ~0x7ff) == 0          \
3452            ? 21                         \
3453            : 20                         \
3454        : ((v) & ~0x3fff) == 0           \
3455          ? ((v) & ~0x1fff) == 0         \
3456            ? 19                         \
3457            : 18                         \
3458          : ((v) & ~0x7fff) == 0         \
3459            ? 17                         \
3460            : 16                         \
3461    : ((v) & ~0xffffff) == 0             \
3462      ? ((v) & ~0xfffff) == 0            \
3463        ? ((v) & ~0x3ffff) == 0          \
3464          ? ((v) & ~0x1ffff) == 0        \
3465            ? 15                         \
3466            : 14                         \
3467          : ((v) & ~0x7ffff) == 0        \
3468            ? 13                         \
3469            : 12                         \
3470        : ((v) & ~0x3fffff) == 0         \
3471          ? ((v) & ~0x1fffff) == 0       \
3472            ? 11                         \
3473            : 10                         \
3474          : ((v) & ~0x7fffff) == 0       \
3475            ? 9                          \
3476            : 8                          \
3477      : ((v) & ~0xfffffff) == 0          \
3478        ? ((v) & ~0x3ffffff) == 0        \
3479          ? ((v) & ~0x1ffffff) == 0      \
3480            ? 7                          \
3481            : 6                          \
3482          : ((v) & ~0x7ffffff) == 0      \
3483            ? 5                          \
3484            : 4                          \
3485        : ((v) & ~0x3fffffff) == 0       \
3486          ? ((v) & ~0x1fffffff) == 0     \
3487            ? 3                          \
3488            : 2                          \
3489          : ((v) & ~0x7fffffff) == 0     \
3490            ? 1                          \
3491            : 0)
3492
3493 /*                      load_register()
3494  *  This routine generates the least number of instructions neccessary to load
3495  *  an absolute expression value into a register.
3496  */
3497 static void
3498 load_register (counter, reg, ep, dbl)
3499      int *counter;
3500      int reg;
3501      expressionS *ep;
3502      int dbl;
3503 {
3504   int freg;
3505   expressionS hi32, lo32;
3506
3507   if (ep->X_op != O_big)
3508     {
3509       assert (ep->X_op == O_constant);
3510       if (ep->X_add_number < 0x8000
3511           && (ep->X_add_number >= 0
3512               || (ep->X_add_number >= -0x8000
3513                   && (! dbl
3514                       || ! ep->X_unsigned
3515                       || sizeof (ep->X_add_number) > 4))))
3516         {
3517           /* We can handle 16 bit signed values with an addiu to
3518              $zero.  No need to ever use daddiu here, since $zero and
3519              the result are always correct in 32 bit mode.  */
3520           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3521                        (int) BFD_RELOC_LO16);
3522           return;
3523         }
3524       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3525         {
3526           /* We can handle 16 bit unsigned values with an ori to
3527              $zero.  */
3528           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3529                        (int) BFD_RELOC_LO16);
3530           return;
3531         }
3532       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3533                 && (! dbl
3534                     || ! ep->X_unsigned
3535                     || sizeof (ep->X_add_number) > 4
3536                     || (ep->X_add_number & 0x80000000) == 0))
3537                || ((HAVE_32BIT_GPRS || ! dbl)
3538                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3539                || (HAVE_32BIT_GPRS
3540                    && ! dbl
3541                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3542                        == ~ (offsetT) 0xffffffff)))
3543         {
3544           /* 32 bit values require an lui.  */
3545           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3546                        (int) BFD_RELOC_HI16);
3547           if ((ep->X_add_number & 0xffff) != 0)
3548             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3549                          (int) BFD_RELOC_LO16);
3550           return;
3551         }
3552     }
3553
3554   /* The value is larger than 32 bits.  */
3555
3556   if (HAVE_32BIT_GPRS)
3557     {
3558       as_bad (_("Number (0x%lx) larger than 32 bits"),
3559               (unsigned long) ep->X_add_number);
3560       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3561                    (int) BFD_RELOC_LO16);
3562       return;
3563     }
3564
3565   if (ep->X_op != O_big)
3566     {
3567       hi32 = *ep;
3568       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3569       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3570       hi32.X_add_number &= 0xffffffff;
3571       lo32 = *ep;
3572       lo32.X_add_number &= 0xffffffff;
3573     }
3574   else
3575     {
3576       assert (ep->X_add_number > 2);
3577       if (ep->X_add_number == 3)
3578         generic_bignum[3] = 0;
3579       else if (ep->X_add_number > 4)
3580         as_bad (_("Number larger than 64 bits"));
3581       lo32.X_op = O_constant;
3582       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3583       hi32.X_op = O_constant;
3584       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3585     }
3586
3587   if (hi32.X_add_number == 0)
3588     freg = 0;
3589   else
3590     {
3591       int shift, bit;
3592       unsigned long hi, lo;
3593
3594       if (hi32.X_add_number == (offsetT) 0xffffffff)
3595         {
3596           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3597             {
3598               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3599                            reg, 0, (int) BFD_RELOC_LO16);
3600               return;
3601             }
3602           if (lo32.X_add_number & 0x80000000)
3603             {
3604               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3605                            (int) BFD_RELOC_HI16);
3606               if (lo32.X_add_number & 0xffff)
3607                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3608                              reg, reg, (int) BFD_RELOC_LO16);
3609               return;
3610             }
3611         }
3612
3613       /* Check for 16bit shifted constant.  We know that hi32 is
3614          non-zero, so start the mask on the first bit of the hi32
3615          value.  */
3616       shift = 17;
3617       do
3618         {
3619           unsigned long himask, lomask;
3620
3621           if (shift < 32)
3622             {
3623               himask = 0xffff >> (32 - shift);
3624               lomask = (0xffff << shift) & 0xffffffff;
3625             }
3626           else
3627             {
3628               himask = 0xffff << (shift - 32);
3629               lomask = 0;
3630             }
3631           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3632               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3633             {
3634               expressionS tmp;
3635
3636               tmp.X_op = O_constant;
3637               if (shift < 32)
3638                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3639                                     | (lo32.X_add_number >> shift));
3640               else
3641                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3642               macro_build ((char *) NULL, counter, &tmp,
3643                            "ori", "t,r,i", reg, 0,
3644                            (int) BFD_RELOC_LO16);
3645               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3646                            (shift >= 32) ? "dsll32" : "dsll",
3647                            "d,w,<", reg, reg,
3648                            (shift >= 32) ? shift - 32 : shift);
3649               return;
3650             }
3651           ++shift;
3652         }
3653       while (shift <= (64 - 16));
3654
3655       /* Find the bit number of the lowest one bit, and store the
3656          shifted value in hi/lo.  */
3657       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3658       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3659       if (lo != 0)
3660         {
3661           bit = 0;
3662           while ((lo & 1) == 0)
3663             {
3664               lo >>= 1;
3665               ++bit;
3666             }
3667           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3668           hi >>= bit;
3669         }
3670       else
3671         {
3672           bit = 32;
3673           while ((hi & 1) == 0)
3674             {
3675               hi >>= 1;
3676               ++bit;
3677             }
3678           lo = hi;
3679           hi = 0;
3680         }
3681
3682       /* Optimize if the shifted value is a (power of 2) - 1.  */
3683       if ((hi == 0 && ((lo + 1) & lo) == 0)
3684           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3685         {
3686           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3687           if (shift != 0)
3688             {
3689               expressionS tmp;
3690
3691               /* This instruction will set the register to be all
3692                  ones.  */
3693               tmp.X_op = O_constant;
3694               tmp.X_add_number = (offsetT) -1;
3695               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3696                            reg, 0, (int) BFD_RELOC_LO16);
3697               if (bit != 0)
3698                 {
3699                   bit += shift;
3700                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3701                                (bit >= 32) ? "dsll32" : "dsll",
3702                                "d,w,<", reg, reg,
3703                                (bit >= 32) ? bit - 32 : bit);
3704                 }
3705               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3706                            (shift >= 32) ? "dsrl32" : "dsrl",
3707                            "d,w,<", reg, reg,
3708                            (shift >= 32) ? shift - 32 : shift);
3709               return;
3710             }
3711         }
3712
3713       /* Sign extend hi32 before calling load_register, because we can
3714          generally get better code when we load a sign extended value.  */
3715       if ((hi32.X_add_number & 0x80000000) != 0)
3716         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3717       load_register (counter, reg, &hi32, 0);
3718       freg = reg;
3719     }
3720   if ((lo32.X_add_number & 0xffff0000) == 0)
3721     {
3722       if (freg != 0)
3723         {
3724           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3725                        "dsll32", "d,w,<", reg, freg, 0);
3726           freg = reg;
3727         }
3728     }
3729   else
3730     {
3731       expressionS mid16;
3732
3733       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3734         {
3735           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3736                        (int) BFD_RELOC_HI16);
3737           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3738                        "dsrl32", "d,w,<", reg, reg, 0);
3739           return;
3740         }
3741
3742       if (freg != 0)
3743         {
3744           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3745                        "d,w,<", reg, freg, 16);
3746           freg = reg;
3747         }
3748       mid16 = lo32;
3749       mid16.X_add_number >>= 16;
3750       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3751                    freg, (int) BFD_RELOC_LO16);
3752       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3753                    "d,w,<", reg, reg, 16);
3754       freg = reg;
3755     }
3756   if ((lo32.X_add_number & 0xffff) != 0)
3757     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3758                  (int) BFD_RELOC_LO16);
3759 }
3760
3761 /* Load an address into a register.  */
3762
3763 static void
3764 load_address (counter, reg, ep, used_at)
3765      int *counter;
3766      int reg;
3767      expressionS *ep;
3768      int *used_at;
3769 {
3770   char *p = NULL;
3771
3772   if (ep->X_op != O_constant
3773       && ep->X_op != O_symbol)
3774     {
3775       as_bad (_("expression too complex"));
3776       ep->X_op = O_constant;
3777     }
3778
3779   if (ep->X_op == O_constant)
3780     {
3781       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3782       return;
3783     }
3784
3785   if (mips_pic == NO_PIC)
3786     {
3787       /* If this is a reference to a GP relative symbol, we want
3788            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3789          Otherwise we want
3790            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3791            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3792          If we have an addend, we always use the latter form.
3793
3794          With 64bit address space and a usable $at we want
3795            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3796            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3797            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3798            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3799            dsll32       $reg,0
3800            daddu        $reg,$reg,$at
3801
3802          If $at is already in use, we use a path which is suboptimal
3803          on superscalar processors.
3804            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3805            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3806            dsll         $reg,16
3807            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3808            dsll         $reg,16
3809            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3810        */
3811       if (HAVE_64BIT_ADDRESSES)
3812         {
3813           /* We don't do GP optimization for now because RELAX_ENCODE can't
3814              hold the data for such large chunks.  */
3815
3816           if (*used_at == 0 && ! mips_opts.noat)
3817             {
3818               macro_build (p, counter, ep, "lui", "t,u",
3819                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3820               macro_build (p, counter, ep, "lui", "t,u",
3821                            AT, (int) BFD_RELOC_HI16_S);
3822               macro_build (p, counter, ep, "daddiu", "t,r,j",
3823                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3824               macro_build (p, counter, ep, "daddiu", "t,r,j",
3825                            AT, AT, (int) BFD_RELOC_LO16);
3826               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3827                            "d,w,<", reg, reg, 0);
3828               macro_build (p, counter, (expressionS *) NULL, "daddu",
3829                            "d,v,t", reg, reg, AT);
3830               *used_at = 1;
3831             }
3832           else
3833             {
3834               macro_build (p, counter, ep, "lui", "t,u",
3835                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3836               macro_build (p, counter, ep, "daddiu", "t,r,j",
3837                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3838               macro_build (p, counter, (expressionS *) NULL, "dsll",
3839                            "d,w,<", reg, reg, 16);
3840               macro_build (p, counter, ep, "daddiu", "t,r,j",
3841                            reg, reg, (int) BFD_RELOC_HI16_S);
3842               macro_build (p, counter, (expressionS *) NULL, "dsll",
3843                            "d,w,<", reg, reg, 16);
3844               macro_build (p, counter, ep, "daddiu", "t,r,j",
3845                            reg, reg, (int) BFD_RELOC_LO16);
3846             }
3847         }
3848       else
3849         {
3850           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3851               && ! nopic_need_relax (ep->X_add_symbol, 1))
3852             {
3853               frag_grow (20);
3854               macro_build ((char *) NULL, counter, ep,
3855                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3856                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3857               p = frag_var (rs_machine_dependent, 8, 0,
3858                             RELAX_ENCODE (4, 8, 0, 4, 0,
3859                                           mips_opts.warn_about_macros),
3860                             ep->X_add_symbol, 0, NULL);
3861             }
3862           macro_build_lui (p, counter, ep, reg);
3863           if (p != NULL)
3864             p += 4;
3865           macro_build (p, counter, ep,
3866                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3867                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3868         }
3869     }
3870   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3871     {
3872       expressionS ex;
3873
3874       /* If this is a reference to an external symbol, we want
3875            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3876          Otherwise we want
3877            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3878            nop
3879            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3880          If we have NewABI, we want
3881            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3882          If there is a constant, it must be added in after.  */
3883       ex.X_add_number = ep->X_add_number;
3884       ep->X_add_number = 0;
3885       frag_grow (20);
3886       if (HAVE_NEWABI)
3887         {
3888           macro_build ((char *) NULL, counter, ep,
3889                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3890                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3891         }
3892       else
3893         {
3894           macro_build ((char *) NULL, counter, ep,
3895                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3896                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3897           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3898           p = frag_var (rs_machine_dependent, 4, 0,
3899                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3900                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3901           macro_build (p, counter, ep,
3902                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3903                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3904         }
3905
3906       if (ex.X_add_number != 0)
3907         {
3908           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3909             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3910           ex.X_op = O_constant;
3911           macro_build ((char *) NULL, counter, &ex,
3912                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3913                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3914         }
3915     }
3916   else if (mips_pic == SVR4_PIC)
3917     {
3918       expressionS ex;
3919       int off;
3920
3921       /* This is the large GOT case.  If this is a reference to an
3922          external symbol, we want
3923            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3924            addu         $reg,$reg,$gp
3925            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3926          Otherwise, for a reference to a local symbol, we want
3927            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3928            nop
3929            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3930          If we have NewABI, we want
3931            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3932            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3933          If there is a constant, it must be added in after.  */
3934       ex.X_add_number = ep->X_add_number;
3935       ep->X_add_number = 0;
3936       if (HAVE_NEWABI)
3937         {
3938           macro_build ((char *) NULL, counter, ep,
3939                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3940                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3941           macro_build (p, counter, ep,
3942                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3943                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3944         }
3945       else
3946         {
3947           if (reg_needs_delay (mips_gp_register))
3948             off = 4;
3949           else
3950             off = 0;
3951           frag_grow (32);
3952           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3953                        (int) BFD_RELOC_MIPS_GOT_HI16);
3954           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3955                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3956                        reg, mips_gp_register);
3957           macro_build ((char *) NULL, counter, ep,
3958                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3959                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3960           p = frag_var (rs_machine_dependent, 12 + off, 0,
3961                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3962                                       mips_opts.warn_about_macros),
3963                         ep->X_add_symbol, 0, NULL);
3964           if (off > 0)
3965             {
3966               /* We need a nop before loading from $gp.  This special
3967                  check is required because the lui which starts the main
3968                  instruction stream does not refer to $gp, and so will not
3969                  insert the nop which may be required.  */
3970               macro_build (p, counter, (expressionS *) NULL, "nop", "");
3971                 p += 4;
3972             }
3973           macro_build (p, counter, ep,
3974                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3975                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3976           p += 4;
3977           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3978           p += 4;
3979           macro_build (p, counter, ep,
3980                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3981                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3982         }
3983
3984       if (ex.X_add_number != 0)
3985         {
3986           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3987             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3988           ex.X_op = O_constant;
3989           macro_build ((char *) NULL, counter, &ex,
3990                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3991                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3992         }
3993     }
3994   else if (mips_pic == EMBEDDED_PIC)
3995     {
3996       /* We always do
3997            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3998        */
3999       macro_build ((char *) NULL, counter, ep,
4000                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4001                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4002     }
4003   else
4004     abort ();
4005 }
4006
4007 /* Move the contents of register SOURCE into register DEST.  */
4008
4009 static void
4010 move_register (counter, dest, source)
4011      int *counter;
4012      int dest;
4013      int source;
4014 {
4015   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4016                HAVE_32BIT_GPRS ? "addu" : "daddu",
4017                "d,v,t", dest, source, 0);
4018 }
4019
4020 /*
4021  *                      Build macros
4022  *   This routine implements the seemingly endless macro or synthesized
4023  * instructions and addressing modes in the mips assembly language. Many
4024  * of these macros are simple and are similar to each other. These could
4025  * probably be handled by some kind of table or grammer aproach instead of
4026  * this verbose method. Others are not simple macros but are more like
4027  * optimizing code generation.
4028  *   One interesting optimization is when several store macros appear
4029  * consecutivly that would load AT with the upper half of the same address.
4030  * The ensuing load upper instructions are ommited. This implies some kind
4031  * of global optimization. We currently only optimize within a single macro.
4032  *   For many of the load and store macros if the address is specified as a
4033  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4034  * first load register 'at' with zero and use it as the base register. The
4035  * mips assembler simply uses register $zero. Just one tiny optimization
4036  * we're missing.
4037  */
4038 static void
4039 macro (ip)
4040      struct mips_cl_insn *ip;
4041 {
4042   register int treg, sreg, dreg, breg;
4043   int tempreg;
4044   int mask;
4045   int icnt = 0;
4046   int used_at = 0;
4047   expressionS expr1;
4048   const char *s;
4049   const char *s2;
4050   const char *fmt;
4051   int likely = 0;
4052   int dbl = 0;
4053   int coproc = 0;
4054   int lr = 0;
4055   int imm = 0;
4056   offsetT maxnum;
4057   int off;
4058   bfd_reloc_code_real_type r;
4059   int hold_mips_optimize;
4060
4061   assert (! mips_opts.mips16);
4062
4063   treg = (ip->insn_opcode >> 16) & 0x1f;
4064   dreg = (ip->insn_opcode >> 11) & 0x1f;
4065   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4066   mask = ip->insn_mo->mask;
4067
4068   expr1.X_op = O_constant;
4069   expr1.X_op_symbol = NULL;
4070   expr1.X_add_symbol = NULL;
4071   expr1.X_add_number = 1;
4072
4073   switch (mask)
4074     {
4075     case M_DABS:
4076       dbl = 1;
4077     case M_ABS:
4078       /* bgez $a0,.+12
4079          move v0,$a0
4080          sub v0,$zero,$a0
4081          */
4082
4083       mips_emit_delays (TRUE);
4084       ++mips_opts.noreorder;
4085       mips_any_noreorder = 1;
4086
4087       expr1.X_add_number = 8;
4088       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4089       if (dreg == sreg)
4090         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4091                      0);
4092       else
4093         move_register (&icnt, dreg, sreg);
4094       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4095                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4096
4097       --mips_opts.noreorder;
4098       return;
4099
4100     case M_ADD_I:
4101       s = "addi";
4102       s2 = "add";
4103       goto do_addi;
4104     case M_ADDU_I:
4105       s = "addiu";
4106       s2 = "addu";
4107       goto do_addi;
4108     case M_DADD_I:
4109       dbl = 1;
4110       s = "daddi";
4111       s2 = "dadd";
4112       goto do_addi;
4113     case M_DADDU_I:
4114       dbl = 1;
4115       s = "daddiu";
4116       s2 = "daddu";
4117     do_addi:
4118       if (imm_expr.X_op == O_constant
4119           && imm_expr.X_add_number >= -0x8000
4120           && imm_expr.X_add_number < 0x8000)
4121         {
4122           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4123                        (int) BFD_RELOC_LO16);
4124           return;
4125         }
4126       load_register (&icnt, AT, &imm_expr, dbl);
4127       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4128                    treg, sreg, AT);
4129       break;
4130
4131     case M_AND_I:
4132       s = "andi";
4133       s2 = "and";
4134       goto do_bit;
4135     case M_OR_I:
4136       s = "ori";
4137       s2 = "or";
4138       goto do_bit;
4139     case M_NOR_I:
4140       s = "";
4141       s2 = "nor";
4142       goto do_bit;
4143     case M_XOR_I:
4144       s = "xori";
4145       s2 = "xor";
4146     do_bit:
4147       if (imm_expr.X_op == O_constant
4148           && imm_expr.X_add_number >= 0
4149           && imm_expr.X_add_number < 0x10000)
4150         {
4151           if (mask != M_NOR_I)
4152             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4153                          sreg, (int) BFD_RELOC_LO16);
4154           else
4155             {
4156               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4157                            treg, sreg, (int) BFD_RELOC_LO16);
4158               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4159                            "d,v,t", treg, treg, 0);
4160             }
4161           return;
4162         }
4163
4164       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4165       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4166                    treg, sreg, AT);
4167       break;
4168
4169     case M_BEQ_I:
4170       s = "beq";
4171       goto beq_i;
4172     case M_BEQL_I:
4173       s = "beql";
4174       likely = 1;
4175       goto beq_i;
4176     case M_BNE_I:
4177       s = "bne";
4178       goto beq_i;
4179     case M_BNEL_I:
4180       s = "bnel";
4181       likely = 1;
4182     beq_i:
4183       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4184         {
4185           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4186                        0);
4187           return;
4188         }
4189       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4190       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4191       break;
4192
4193     case M_BGEL:
4194       likely = 1;
4195     case M_BGE:
4196       if (treg == 0)
4197         {
4198           macro_build ((char *) NULL, &icnt, &offset_expr,
4199                        likely ? "bgezl" : "bgez", "s,p", sreg);
4200           return;
4201         }
4202       if (sreg == 0)
4203         {
4204           macro_build ((char *) NULL, &icnt, &offset_expr,
4205                        likely ? "blezl" : "blez", "s,p", treg);
4206           return;
4207         }
4208       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4209                    AT, sreg, treg);
4210       macro_build ((char *) NULL, &icnt, &offset_expr,
4211                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4212       break;
4213
4214     case M_BGTL_I:
4215       likely = 1;
4216     case M_BGT_I:
4217       /* check for > max integer */
4218       maxnum = 0x7fffffff;
4219       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4220         {
4221           maxnum <<= 16;
4222           maxnum |= 0xffff;
4223           maxnum <<= 16;
4224           maxnum |= 0xffff;
4225         }
4226       if (imm_expr.X_op == O_constant
4227           && imm_expr.X_add_number >= maxnum
4228           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4229         {
4230         do_false:
4231           /* result is always false */
4232           if (! likely)
4233             {
4234               if (warn_nops)
4235                 as_warn (_("Branch %s is always false (nop)"),
4236                          ip->insn_mo->name);
4237               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4238                            "", 0);
4239             }
4240           else
4241             {
4242               if (warn_nops)
4243                 as_warn (_("Branch likely %s is always false"),
4244                          ip->insn_mo->name);
4245               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4246                            "s,t,p", 0, 0);
4247             }
4248           return;
4249         }
4250       if (imm_expr.X_op != O_constant)
4251         as_bad (_("Unsupported large constant"));
4252       ++imm_expr.X_add_number;
4253       /* FALLTHROUGH */
4254     case M_BGE_I:
4255     case M_BGEL_I:
4256       if (mask == M_BGEL_I)
4257         likely = 1;
4258       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4259         {
4260           macro_build ((char *) NULL, &icnt, &offset_expr,
4261                        likely ? "bgezl" : "bgez", "s,p", sreg);
4262           return;
4263         }
4264       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4265         {
4266           macro_build ((char *) NULL, &icnt, &offset_expr,
4267                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4268           return;
4269         }
4270       maxnum = 0x7fffffff;
4271       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4272         {
4273           maxnum <<= 16;
4274           maxnum |= 0xffff;
4275           maxnum <<= 16;
4276           maxnum |= 0xffff;
4277         }
4278       maxnum = - maxnum - 1;
4279       if (imm_expr.X_op == O_constant
4280           && imm_expr.X_add_number <= maxnum
4281           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4282         {
4283         do_true:
4284           /* result is always true */
4285           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4286           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4287           return;
4288         }
4289       set_at (&icnt, sreg, 0);
4290       macro_build ((char *) NULL, &icnt, &offset_expr,
4291                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4292       break;
4293
4294     case M_BGEUL:
4295       likely = 1;
4296     case M_BGEU:
4297       if (treg == 0)
4298         goto do_true;
4299       if (sreg == 0)
4300         {
4301           macro_build ((char *) NULL, &icnt, &offset_expr,
4302                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4303           return;
4304         }
4305       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4306                    "d,v,t", AT, sreg, treg);
4307       macro_build ((char *) NULL, &icnt, &offset_expr,
4308                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4309       break;
4310
4311     case M_BGTUL_I:
4312       likely = 1;
4313     case M_BGTU_I:
4314       if (sreg == 0
4315           || (HAVE_32BIT_GPRS
4316               && imm_expr.X_op == O_constant
4317               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4318         goto do_false;
4319       if (imm_expr.X_op != O_constant)
4320         as_bad (_("Unsupported large constant"));
4321       ++imm_expr.X_add_number;
4322       /* FALLTHROUGH */
4323     case M_BGEU_I:
4324     case M_BGEUL_I:
4325       if (mask == M_BGEUL_I)
4326         likely = 1;
4327       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4328         goto do_true;
4329       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4330         {
4331           macro_build ((char *) NULL, &icnt, &offset_expr,
4332                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4333           return;
4334         }
4335       set_at (&icnt, sreg, 1);
4336       macro_build ((char *) NULL, &icnt, &offset_expr,
4337                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4338       break;
4339
4340     case M_BGTL:
4341       likely = 1;
4342     case M_BGT:
4343       if (treg == 0)
4344         {
4345           macro_build ((char *) NULL, &icnt, &offset_expr,
4346                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4347           return;
4348         }
4349       if (sreg == 0)
4350         {
4351           macro_build ((char *) NULL, &icnt, &offset_expr,
4352                        likely ? "bltzl" : "bltz", "s,p", treg);
4353           return;
4354         }
4355       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4356                    AT, treg, sreg);
4357       macro_build ((char *) NULL, &icnt, &offset_expr,
4358                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4359       break;
4360
4361     case M_BGTUL:
4362       likely = 1;
4363     case M_BGTU:
4364       if (treg == 0)
4365         {
4366           macro_build ((char *) NULL, &icnt, &offset_expr,
4367                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4368           return;
4369         }
4370       if (sreg == 0)
4371         goto do_false;
4372       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4373                    "d,v,t", AT, treg, sreg);
4374       macro_build ((char *) NULL, &icnt, &offset_expr,
4375                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4376       break;
4377
4378     case M_BLEL:
4379       likely = 1;
4380     case M_BLE:
4381       if (treg == 0)
4382         {
4383           macro_build ((char *) NULL, &icnt, &offset_expr,
4384                        likely ? "blezl" : "blez", "s,p", sreg);
4385           return;
4386         }
4387       if (sreg == 0)
4388         {
4389           macro_build ((char *) NULL, &icnt, &offset_expr,
4390                        likely ? "bgezl" : "bgez", "s,p", treg);
4391           return;
4392         }
4393       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4394                    AT, treg, sreg);
4395       macro_build ((char *) NULL, &icnt, &offset_expr,
4396                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4397       break;
4398
4399     case M_BLEL_I:
4400       likely = 1;
4401     case M_BLE_I:
4402       maxnum = 0x7fffffff;
4403       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4404         {
4405           maxnum <<= 16;
4406           maxnum |= 0xffff;
4407           maxnum <<= 16;
4408           maxnum |= 0xffff;
4409         }
4410       if (imm_expr.X_op == O_constant
4411           && imm_expr.X_add_number >= maxnum
4412           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4413         goto do_true;
4414       if (imm_expr.X_op != O_constant)
4415         as_bad (_("Unsupported large constant"));
4416       ++imm_expr.X_add_number;
4417       /* FALLTHROUGH */
4418     case M_BLT_I:
4419     case M_BLTL_I:
4420       if (mask == M_BLTL_I)
4421         likely = 1;
4422       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4423         {
4424           macro_build ((char *) NULL, &icnt, &offset_expr,
4425                        likely ? "bltzl" : "bltz", "s,p", sreg);
4426           return;
4427         }
4428       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4429         {
4430           macro_build ((char *) NULL, &icnt, &offset_expr,
4431                        likely ? "blezl" : "blez", "s,p", sreg);
4432           return;
4433         }
4434       set_at (&icnt, sreg, 0);
4435       macro_build ((char *) NULL, &icnt, &offset_expr,
4436                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4437       break;
4438
4439     case M_BLEUL:
4440       likely = 1;
4441     case M_BLEU:
4442       if (treg == 0)
4443         {
4444           macro_build ((char *) NULL, &icnt, &offset_expr,
4445                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4446           return;
4447         }
4448       if (sreg == 0)
4449         goto do_true;
4450       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4451                    "d,v,t", AT, treg, sreg);
4452       macro_build ((char *) NULL, &icnt, &offset_expr,
4453                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4454       break;
4455
4456     case M_BLEUL_I:
4457       likely = 1;
4458     case M_BLEU_I:
4459       if (sreg == 0
4460           || (HAVE_32BIT_GPRS
4461               && imm_expr.X_op == O_constant
4462               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4463         goto do_true;
4464       if (imm_expr.X_op != O_constant)
4465         as_bad (_("Unsupported large constant"));
4466       ++imm_expr.X_add_number;
4467       /* FALLTHROUGH */
4468     case M_BLTU_I:
4469     case M_BLTUL_I:
4470       if (mask == M_BLTUL_I)
4471         likely = 1;
4472       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4473         goto do_false;
4474       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4475         {
4476           macro_build ((char *) NULL, &icnt, &offset_expr,
4477                        likely ? "beql" : "beq",
4478                        "s,t,p", sreg, 0);
4479           return;
4480         }
4481       set_at (&icnt, sreg, 1);
4482       macro_build ((char *) NULL, &icnt, &offset_expr,
4483                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4484       break;
4485
4486     case M_BLTL:
4487       likely = 1;
4488     case M_BLT:
4489       if (treg == 0)
4490         {
4491           macro_build ((char *) NULL, &icnt, &offset_expr,
4492                        likely ? "bltzl" : "bltz", "s,p", sreg);
4493           return;
4494         }
4495       if (sreg == 0)
4496         {
4497           macro_build ((char *) NULL, &icnt, &offset_expr,
4498                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4499           return;
4500         }
4501       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4502                    AT, sreg, treg);
4503       macro_build ((char *) NULL, &icnt, &offset_expr,
4504                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4505       break;
4506
4507     case M_BLTUL:
4508       likely = 1;
4509     case M_BLTU:
4510       if (treg == 0)
4511         goto do_false;
4512       if (sreg == 0)
4513         {
4514           macro_build ((char *) NULL, &icnt, &offset_expr,
4515                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4516           return;
4517         }
4518       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4519                    "d,v,t", AT, sreg,
4520                    treg);
4521       macro_build ((char *) NULL, &icnt, &offset_expr,
4522                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4523       break;
4524
4525     case M_DDIV_3:
4526       dbl = 1;
4527     case M_DIV_3:
4528       s = "mflo";
4529       goto do_div3;
4530     case M_DREM_3:
4531       dbl = 1;
4532     case M_REM_3:
4533       s = "mfhi";
4534     do_div3:
4535       if (treg == 0)
4536         {
4537           as_warn (_("Divide by zero."));
4538           if (mips_trap)
4539             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4540                          "s,t,q", 0, 0, 7);
4541           else
4542             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4543                          "c", 7);
4544           return;
4545         }
4546
4547       mips_emit_delays (TRUE);
4548       ++mips_opts.noreorder;
4549       mips_any_noreorder = 1;
4550       if (mips_trap)
4551         {
4552           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4553                        "s,t,q", treg, 0, 7);
4554           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4555                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4556         }
4557       else
4558         {
4559           expr1.X_add_number = 8;
4560           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4561           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4562                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4563           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4564                        "c", 7);
4565         }
4566       expr1.X_add_number = -1;
4567       macro_build ((char *) NULL, &icnt, &expr1,
4568                    dbl ? "daddiu" : "addiu",
4569                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4570       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4571       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4572       if (dbl)
4573         {
4574           expr1.X_add_number = 1;
4575           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4576                        (int) BFD_RELOC_LO16);
4577           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4578                        "d,w,<", AT, AT, 31);
4579         }
4580       else
4581         {
4582           expr1.X_add_number = 0x80000000;
4583           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4584                        (int) BFD_RELOC_HI16);
4585         }
4586       if (mips_trap)
4587         {
4588           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4589                        "s,t,q", sreg, AT, 6);
4590           /* We want to close the noreorder block as soon as possible, so
4591              that later insns are available for delay slot filling.  */
4592           --mips_opts.noreorder;
4593         }
4594       else
4595         {
4596           expr1.X_add_number = 8;
4597           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4598           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4599                        0);
4600
4601           /* We want to close the noreorder block as soon as possible, so
4602              that later insns are available for delay slot filling.  */
4603           --mips_opts.noreorder;
4604
4605           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4606                        "c", 6);
4607         }
4608       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4609       break;
4610
4611     case M_DIV_3I:
4612       s = "div";
4613       s2 = "mflo";
4614       goto do_divi;
4615     case M_DIVU_3I:
4616       s = "divu";
4617       s2 = "mflo";
4618       goto do_divi;
4619     case M_REM_3I:
4620       s = "div";
4621       s2 = "mfhi";
4622       goto do_divi;
4623     case M_REMU_3I:
4624       s = "divu";
4625       s2 = "mfhi";
4626       goto do_divi;
4627     case M_DDIV_3I:
4628       dbl = 1;
4629       s = "ddiv";
4630       s2 = "mflo";
4631       goto do_divi;
4632     case M_DDIVU_3I:
4633       dbl = 1;
4634       s = "ddivu";
4635       s2 = "mflo";
4636       goto do_divi;
4637     case M_DREM_3I:
4638       dbl = 1;
4639       s = "ddiv";
4640       s2 = "mfhi";
4641       goto do_divi;
4642     case M_DREMU_3I:
4643       dbl = 1;
4644       s = "ddivu";
4645       s2 = "mfhi";
4646     do_divi:
4647       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4648         {
4649           as_warn (_("Divide by zero."));
4650           if (mips_trap)
4651             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4652                          "s,t,q", 0, 0, 7);
4653           else
4654             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4655                          "c", 7);
4656           return;
4657         }
4658       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4659         {
4660           if (strcmp (s2, "mflo") == 0)
4661             move_register (&icnt, dreg, sreg);
4662           else
4663             move_register (&icnt, dreg, 0);
4664           return;
4665         }
4666       if (imm_expr.X_op == O_constant
4667           && imm_expr.X_add_number == -1
4668           && s[strlen (s) - 1] != 'u')
4669         {
4670           if (strcmp (s2, "mflo") == 0)
4671             {
4672               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4673                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4674             }
4675           else
4676             move_register (&icnt, dreg, 0);
4677           return;
4678         }
4679
4680       load_register (&icnt, AT, &imm_expr, dbl);
4681       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4682                    sreg, AT);
4683       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4684       break;
4685
4686     case M_DIVU_3:
4687       s = "divu";
4688       s2 = "mflo";
4689       goto do_divu3;
4690     case M_REMU_3:
4691       s = "divu";
4692       s2 = "mfhi";
4693       goto do_divu3;
4694     case M_DDIVU_3:
4695       s = "ddivu";
4696       s2 = "mflo";
4697       goto do_divu3;
4698     case M_DREMU_3:
4699       s = "ddivu";
4700       s2 = "mfhi";
4701     do_divu3:
4702       mips_emit_delays (TRUE);
4703       ++mips_opts.noreorder;
4704       mips_any_noreorder = 1;
4705       if (mips_trap)
4706         {
4707           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4708                        "s,t,q", treg, 0, 7);
4709           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4710                        sreg, treg);
4711           /* We want to close the noreorder block as soon as possible, so
4712              that later insns are available for delay slot filling.  */
4713           --mips_opts.noreorder;
4714         }
4715       else
4716         {
4717           expr1.X_add_number = 8;
4718           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4719           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4720                        sreg, treg);
4721
4722           /* We want to close the noreorder block as soon as possible, so
4723              that later insns are available for delay slot filling.  */
4724           --mips_opts.noreorder;
4725           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4726                        "c", 7);
4727         }
4728       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4729       return;
4730
4731     case M_DLA_AB:
4732       dbl = 1;
4733     case M_LA_AB:
4734       /* Load the address of a symbol into a register.  If breg is not
4735          zero, we then add a base register to it.  */
4736
4737       if (dbl && HAVE_32BIT_GPRS)
4738         as_warn (_("dla used to load 32-bit register"));
4739
4740       if (! dbl && HAVE_64BIT_OBJECTS)
4741         as_warn (_("la used to load 64-bit address"));
4742
4743       if (offset_expr.X_op == O_constant
4744           && offset_expr.X_add_number >= -0x8000
4745           && offset_expr.X_add_number < 0x8000)
4746         {
4747           macro_build ((char *) NULL, &icnt, &offset_expr,
4748                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4749                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4750           return;
4751         }
4752
4753       if (treg == breg)
4754         {
4755           tempreg = AT;
4756           used_at = 1;
4757         }
4758       else
4759         {
4760           tempreg = treg;
4761           used_at = 0;
4762         }
4763
4764       /* When generating embedded PIC code, we permit expressions of
4765          the form
4766            la   $treg,foo-bar
4767            la   $treg,foo-bar($breg)
4768          where bar is an address in the current section.  These are used
4769          when getting the addresses of functions.  We don't permit
4770          X_add_number to be non-zero, because if the symbol is
4771          external the relaxing code needs to know that any addend is
4772          purely the offset to X_op_symbol.  */
4773       if (mips_pic == EMBEDDED_PIC
4774           && offset_expr.X_op == O_subtract
4775           && (symbol_constant_p (offset_expr.X_op_symbol)
4776               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4777               : (symbol_equated_p (offset_expr.X_op_symbol)
4778                  && (S_GET_SEGMENT
4779                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4780                       ->X_add_symbol)
4781                      == now_seg)))
4782           && (offset_expr.X_add_number == 0
4783               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4784         {
4785           if (breg == 0)
4786             {
4787               tempreg = treg;
4788               used_at = 0;
4789               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4790                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4791             }
4792           else
4793             {
4794               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4795                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4796               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4797                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4798                            "d,v,t", tempreg, tempreg, breg);
4799             }
4800           macro_build ((char *) NULL, &icnt, &offset_expr,
4801                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4802                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4803           if (! used_at)
4804             return;
4805           break;
4806         }
4807
4808       if (offset_expr.X_op != O_symbol
4809           && offset_expr.X_op != O_constant)
4810         {
4811           as_bad (_("expression too complex"));
4812           offset_expr.X_op = O_constant;
4813         }
4814
4815       if (offset_expr.X_op == O_constant)
4816         load_register (&icnt, tempreg, &offset_expr,
4817                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4818                         ? (dbl || HAVE_64BIT_ADDRESSES)
4819                         : HAVE_64BIT_ADDRESSES));
4820       else if (mips_pic == NO_PIC)
4821         {
4822           /* If this is a reference to a GP relative symbol, we want
4823                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4824              Otherwise we want
4825                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4826                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4827              If we have a constant, we need two instructions anyhow,
4828              so we may as well always use the latter form.
4829
4830             With 64bit address space and a usable $at we want
4831               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4832               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4833               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4834               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4835               dsll32    $tempreg,0
4836               daddu     $tempreg,$tempreg,$at
4837
4838             If $at is already in use, we use a path which is suboptimal
4839             on superscalar processors.
4840               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4841               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4842               dsll      $tempreg,16
4843               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4844               dsll      $tempreg,16
4845               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4846           */
4847           char *p = NULL;
4848           if (HAVE_64BIT_ADDRESSES)
4849             {
4850               /* We don't do GP optimization for now because RELAX_ENCODE can't
4851                  hold the data for such large chunks.  */
4852
4853               if (used_at == 0 && ! mips_opts.noat)
4854                 {
4855                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4856                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4857                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4858                                AT, (int) BFD_RELOC_HI16_S);
4859                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4860                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4861                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4862                                AT, AT, (int) BFD_RELOC_LO16);
4863                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4864                                "d,w,<", tempreg, tempreg, 0);
4865                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4866                                "d,v,t", tempreg, tempreg, AT);
4867                   used_at = 1;
4868                 }
4869               else
4870                 {
4871                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4872                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4873                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4874                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4875                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4876                                tempreg, tempreg, 16);
4877                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4878                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4879                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4880                                tempreg, tempreg, 16);
4881                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4882                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4883                 }
4884             }
4885           else
4886             {
4887               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4888                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4889                 {
4890                   frag_grow (20);
4891                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4892                                "t,r,j", tempreg, mips_gp_register,
4893                                (int) BFD_RELOC_GPREL16);
4894                   p = frag_var (rs_machine_dependent, 8, 0,
4895                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4896                                               mips_opts.warn_about_macros),
4897                                 offset_expr.X_add_symbol, 0, NULL);
4898                 }
4899               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4900               if (p != NULL)
4901                 p += 4;
4902               macro_build (p, &icnt, &offset_expr, "addiu",
4903                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4904             }
4905         }
4906       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4907         {
4908           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4909
4910           /* If this is a reference to an external symbol, and there
4911              is no constant, we want
4912                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4913              or if tempreg is PIC_CALL_REG
4914                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4915              For a local symbol, we want
4916                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4917                nop
4918                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4919
4920              If we have a small constant, and this is a reference to
4921              an external symbol, we want
4922                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4923                nop
4924                addiu    $tempreg,$tempreg,<constant>
4925              For a local symbol, we want the same instruction
4926              sequence, but we output a BFD_RELOC_LO16 reloc on the
4927              addiu instruction.
4928
4929              If we have a large constant, and this is a reference to
4930              an external symbol, we want
4931                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4932                lui      $at,<hiconstant>
4933                addiu    $at,$at,<loconstant>
4934                addu     $tempreg,$tempreg,$at
4935              For a local symbol, we want the same instruction
4936              sequence, but we output a BFD_RELOC_LO16 reloc on the
4937              addiu instruction.
4938
4939              For NewABI, we want for local or external data addresses
4940                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4941              For a local function symbol, we want
4942                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4943                nop
4944                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4945            */
4946
4947           expr1.X_add_number = offset_expr.X_add_number;
4948           offset_expr.X_add_number = 0;
4949           frag_grow (32);
4950           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4951             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4952           else if (HAVE_NEWABI)
4953             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4954           macro_build ((char *) NULL, &icnt, &offset_expr,
4955                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4956                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
4957           if (expr1.X_add_number == 0)
4958             {
4959               int off;
4960               char *p;
4961
4962               if (breg == 0)
4963                 off = 0;
4964               else
4965                 {
4966                   /* We're going to put in an addu instruction using
4967                      tempreg, so we may as well insert the nop right
4968                      now.  */
4969                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4970                                "nop", "");
4971                   off = 4;
4972                 }
4973               p = frag_var (rs_machine_dependent, 8 - off, 0,
4974                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4975                                           (breg == 0
4976                                            ? mips_opts.warn_about_macros
4977                                            : 0)),
4978                             offset_expr.X_add_symbol, 0, NULL);
4979               if (breg == 0)
4980                 {
4981                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4982                   p += 4;
4983                 }
4984               macro_build (p, &icnt, &expr1,
4985                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4986                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4987               /* FIXME: If breg == 0, and the next instruction uses
4988                  $tempreg, then if this variant case is used an extra
4989                  nop will be generated.  */
4990             }
4991           else if (expr1.X_add_number >= -0x8000
4992                    && expr1.X_add_number < 0x8000)
4993             {
4994               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4995                            "nop", "");
4996               macro_build ((char *) NULL, &icnt, &expr1,
4997                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4998                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4999               frag_var (rs_machine_dependent, 0, 0,
5000                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5001                         offset_expr.X_add_symbol, 0, NULL);
5002             }
5003           else
5004             {
5005               int off1;
5006
5007               /* If we are going to add in a base register, and the
5008                  target register and the base register are the same,
5009                  then we are using AT as a temporary register.  Since
5010                  we want to load the constant into AT, we add our
5011                  current AT (from the global offset table) and the
5012                  register into the register now, and pretend we were
5013                  not using a base register.  */
5014               if (breg != treg)
5015                 off1 = 0;
5016               else
5017                 {
5018                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5019                                "nop", "");
5020                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5021                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5022                                "d,v,t", treg, AT, breg);
5023                   breg = 0;
5024                   tempreg = treg;
5025                   off1 = -8;
5026                 }
5027
5028               /* Set mips_optimize around the lui instruction to avoid
5029                  inserting an unnecessary nop after the lw.  */
5030               hold_mips_optimize = mips_optimize;
5031               mips_optimize = 2;
5032               macro_build_lui (NULL, &icnt, &expr1, AT);
5033               mips_optimize = hold_mips_optimize;
5034
5035               macro_build ((char *) NULL, &icnt, &expr1,
5036                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5037                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5038               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5039                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5040                            "d,v,t", tempreg, tempreg, AT);
5041               frag_var (rs_machine_dependent, 0, 0,
5042                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5043                         offset_expr.X_add_symbol, 0, NULL);
5044               used_at = 1;
5045             }
5046         }
5047       else if (mips_pic == SVR4_PIC)
5048         {
5049           int gpdel;
5050           char *p;
5051           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5052           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5053           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5054
5055           /* This is the large GOT case.  If this is a reference to an
5056              external symbol, and there is no constant, we want
5057                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5058                addu     $tempreg,$tempreg,$gp
5059                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5060              or if tempreg is PIC_CALL_REG
5061                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5062                addu     $tempreg,$tempreg,$gp
5063                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5064              For a local symbol, we want
5065                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5066                nop
5067                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5068
5069              If we have a small constant, and this is a reference to
5070              an external symbol, we want
5071                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5072                addu     $tempreg,$tempreg,$gp
5073                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5074                nop
5075                addiu    $tempreg,$tempreg,<constant>
5076              For a local symbol, we want
5077                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5078                nop
5079                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5080
5081              If we have a large constant, and this is a reference to
5082              an external symbol, we want
5083                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5084                addu     $tempreg,$tempreg,$gp
5085                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5086                lui      $at,<hiconstant>
5087                addiu    $at,$at,<loconstant>
5088                addu     $tempreg,$tempreg,$at
5089              For a local symbol, we want
5090                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5091                lui      $at,<hiconstant>
5092                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5093                addu     $tempreg,$tempreg,$at
5094
5095              For NewABI, we want for local data addresses
5096               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5097            */
5098
5099           expr1.X_add_number = offset_expr.X_add_number;
5100           offset_expr.X_add_number = 0;
5101           frag_grow (52);
5102           if (reg_needs_delay (mips_gp_register))
5103             gpdel = 4;
5104           else
5105             gpdel = 0;
5106           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5107             {
5108               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5109               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5110             }
5111           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5112                        tempreg, lui_reloc_type);
5113           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5114                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5115                        "d,v,t", tempreg, tempreg, mips_gp_register);
5116           macro_build ((char *) NULL, &icnt, &offset_expr,
5117                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5118                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5119           if (expr1.X_add_number == 0)
5120             {
5121               int off;
5122
5123               if (breg == 0)
5124                 off = 0;
5125               else
5126                 {
5127                   /* We're going to put in an addu instruction using
5128                      tempreg, so we may as well insert the nop right
5129                      now.  */
5130                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5131                                "nop", "");
5132                   off = 4;
5133                 }
5134
5135               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5136                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5137                                           8 + gpdel, 0,
5138                                           (breg == 0
5139                                            ? mips_opts.warn_about_macros
5140                                            : 0)),
5141                             offset_expr.X_add_symbol, 0, NULL);
5142             }
5143           else if (expr1.X_add_number >= -0x8000
5144                    && expr1.X_add_number < 0x8000)
5145             {
5146               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147                            "nop", "");
5148               macro_build ((char *) NULL, &icnt, &expr1,
5149                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5150                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5151
5152               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5153                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5154                                           (breg == 0
5155                                            ? mips_opts.warn_about_macros
5156                                            : 0)),
5157                             offset_expr.X_add_symbol, 0, NULL);
5158             }
5159           else
5160             {
5161               int adj, dreg;
5162
5163               /* If we are going to add in a base register, and the
5164                  target register and the base register are the same,
5165                  then we are using AT as a temporary register.  Since
5166                  we want to load the constant into AT, we add our
5167                  current AT (from the global offset table) and the
5168                  register into the register now, and pretend we were
5169                  not using a base register.  */
5170               if (breg != treg)
5171                 {
5172                   adj = 0;
5173                   dreg = tempreg;
5174                 }
5175               else
5176                 {
5177                   assert (tempreg == AT);
5178                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5179                                "nop", "");
5180                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5181                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5182                                "d,v,t", treg, AT, breg);
5183                   dreg = treg;
5184                   adj = 8;
5185                 }
5186
5187               /* Set mips_optimize around the lui instruction to avoid
5188                  inserting an unnecessary nop after the lw.  */
5189               hold_mips_optimize = mips_optimize;
5190               mips_optimize = 2;
5191               macro_build_lui (NULL, &icnt, &expr1, AT);
5192               mips_optimize = hold_mips_optimize;
5193
5194               macro_build ((char *) NULL, &icnt, &expr1,
5195                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5196                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5197               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5198                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5199                            "d,v,t", dreg, dreg, AT);
5200
5201               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5202                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5203                                           8 + gpdel, 0,
5204                                           (breg == 0
5205                                            ? mips_opts.warn_about_macros
5206                                            : 0)),
5207                             offset_expr.X_add_symbol, 0, NULL);
5208
5209               used_at = 1;
5210             }
5211
5212           if (gpdel > 0)
5213             {
5214               /* This is needed because this instruction uses $gp, but
5215                  the first instruction on the main stream does not.  */
5216               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5217               p += 4;
5218             }
5219
5220           if (HAVE_NEWABI)
5221             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5222           macro_build (p, &icnt, &offset_expr,
5223                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5224                        "t,o(b)", tempreg,
5225                        local_reloc_type,
5226                        mips_gp_register);
5227           p += 4;
5228           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5229             {
5230               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5231             }
5232          else
5233            if (expr1.X_add_number >= -0x8000
5234               && expr1.X_add_number < 0x8000)
5235             {
5236               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5237               p += 4;
5238               macro_build (p, &icnt, &expr1,
5239                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5240                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5241               /* FIXME: If add_number is 0, and there was no base
5242                  register, the external symbol case ended with a load,
5243                  so if the symbol turns out to not be external, and
5244                  the next instruction uses tempreg, an unnecessary nop
5245                  will be inserted.  */
5246             }
5247           else
5248             {
5249               if (breg == treg)
5250                 {
5251                   /* We must add in the base register now, as in the
5252                      external symbol case.  */
5253                   assert (tempreg == AT);
5254                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5255                   p += 4;
5256                   macro_build (p, &icnt, (expressionS *) NULL,
5257                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5258                                "d,v,t", treg, AT, breg);
5259                   p += 4;
5260                   tempreg = treg;
5261                   /* We set breg to 0 because we have arranged to add
5262                      it in in both cases.  */
5263                   breg = 0;
5264                 }
5265
5266               macro_build_lui (p, &icnt, &expr1, AT);
5267               p += 4;
5268               macro_build (p, &icnt, &expr1,
5269                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5270                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5271               p += 4;
5272               macro_build (p, &icnt, (expressionS *) NULL,
5273                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5274                            "d,v,t", tempreg, tempreg, AT);
5275               p += 4;
5276             }
5277         }
5278       else if (mips_pic == EMBEDDED_PIC)
5279         {
5280           /* We use
5281                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5282              */
5283           macro_build ((char *) NULL, &icnt, &offset_expr,
5284                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5285                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5286         }
5287       else
5288         abort ();
5289
5290       if (breg != 0)
5291         {
5292           char *s;
5293
5294           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5295             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5296           else
5297             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5298
5299           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5300                        "d,v,t", treg, tempreg, breg);
5301         }
5302
5303       if (! used_at)
5304         return;
5305
5306       break;
5307
5308     case M_J_A:
5309       /* The j instruction may not be used in PIC code, since it
5310          requires an absolute address.  We convert it to a b
5311          instruction.  */
5312       if (mips_pic == NO_PIC)
5313         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5314       else
5315         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5316       return;
5317
5318       /* The jal instructions must be handled as macros because when
5319          generating PIC code they expand to multi-instruction
5320          sequences.  Normally they are simple instructions.  */
5321     case M_JAL_1:
5322       dreg = RA;
5323       /* Fall through.  */
5324     case M_JAL_2:
5325       if (mips_pic == NO_PIC
5326           || mips_pic == EMBEDDED_PIC)
5327         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5328                      "d,s", dreg, sreg);
5329       else if (mips_pic == SVR4_PIC)
5330         {
5331           if (sreg != PIC_CALL_REG)
5332             as_warn (_("MIPS PIC call to register other than $25"));
5333
5334           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5335                        "d,s", dreg, sreg);
5336           if (! HAVE_NEWABI)
5337             {
5338               if (mips_cprestore_offset < 0)
5339                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5340               else
5341                 {
5342                   if (! mips_frame_reg_valid)
5343                     {
5344                       as_warn (_("No .frame pseudo-op used in PIC code"));
5345                       /* Quiet this warning.  */
5346                       mips_frame_reg_valid = 1;
5347                     }
5348                   if (! mips_cprestore_valid)
5349                     {
5350                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5351                       /* Quiet this warning.  */
5352                       mips_cprestore_valid = 1;
5353                     }
5354                   expr1.X_add_number = mips_cprestore_offset;
5355                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5356                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5357                                                 mips_gp_register, mips_frame_reg);
5358                 }
5359             }
5360         }
5361       else
5362         abort ();
5363
5364       return;
5365
5366     case M_JAL_A:
5367       if (mips_pic == NO_PIC)
5368         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5369       else if (mips_pic == SVR4_PIC)
5370         {
5371           char *p;
5372
5373           /* If this is a reference to an external symbol, and we are
5374              using a small GOT, we want
5375                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5376                nop
5377                jalr     $ra,$25
5378                nop
5379                lw       $gp,cprestore($sp)
5380              The cprestore value is set using the .cprestore
5381              pseudo-op.  If we are using a big GOT, we want
5382                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5383                addu     $25,$25,$gp
5384                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5385                nop
5386                jalr     $ra,$25
5387                nop
5388                lw       $gp,cprestore($sp)
5389              If the symbol is not external, we want
5390                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5391                nop
5392                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5393                jalr     $ra,$25
5394                nop
5395                lw $gp,cprestore($sp)
5396              For NewABI, we want
5397                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5398                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5399            */
5400           if (HAVE_NEWABI)
5401             {
5402               macro_build ((char *) NULL, &icnt, &offset_expr,
5403                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5404                            "t,o(b)", PIC_CALL_REG,
5405                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5406               macro_build_jalr (icnt, &offset_expr);
5407             }
5408           else
5409             {
5410               frag_grow (40);
5411               if (! mips_big_got)
5412                 {
5413                   macro_build ((char *) NULL, &icnt, &offset_expr,
5414                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5415                                "t,o(b)", PIC_CALL_REG,
5416                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5417                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5418                                "nop", "");
5419                   p = frag_var (rs_machine_dependent, 4, 0,
5420                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5421                                 offset_expr.X_add_symbol, 0, NULL);
5422                 }
5423               else
5424                 {
5425                   int gpdel;
5426
5427                   if (reg_needs_delay (mips_gp_register))
5428                     gpdel = 4;
5429                   else
5430                     gpdel = 0;
5431                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5432                                "t,u", PIC_CALL_REG,
5433                                (int) BFD_RELOC_MIPS_CALL_HI16);
5434                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5435                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5436                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5437                                mips_gp_register);
5438                   macro_build ((char *) NULL, &icnt, &offset_expr,
5439                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5440                                "t,o(b)", PIC_CALL_REG,
5441                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5442                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5443                                "nop", "");
5444                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5445                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5446                                               8 + gpdel, 0, 0),
5447                                 offset_expr.X_add_symbol, 0, NULL);
5448                   if (gpdel > 0)
5449                     {
5450                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5451                       p += 4;
5452                     }
5453                   macro_build (p, &icnt, &offset_expr,
5454                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5455                                "t,o(b)", PIC_CALL_REG,
5456                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5457                   p += 4;
5458                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5459                   p += 4;
5460                 }
5461               macro_build (p, &icnt, &offset_expr,
5462                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5463                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5464                            (int) BFD_RELOC_LO16);
5465               macro_build_jalr (icnt, &offset_expr);
5466
5467               if (mips_cprestore_offset < 0)
5468                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5469               else
5470                 {
5471                   if (! mips_frame_reg_valid)
5472                     {
5473                       as_warn (_("No .frame pseudo-op used in PIC code"));
5474                       /* Quiet this warning.  */
5475                       mips_frame_reg_valid = 1;
5476                     }
5477                   if (! mips_cprestore_valid)
5478                     {
5479                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5480                       /* Quiet this warning.  */
5481                       mips_cprestore_valid = 1;
5482                     }
5483                   if (mips_opts.noreorder)
5484                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5485                                  "nop", "");
5486                   expr1.X_add_number = mips_cprestore_offset;
5487                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5488                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5489                                                 mips_gp_register, mips_frame_reg);
5490                 }
5491             }
5492         }
5493       else if (mips_pic == EMBEDDED_PIC)
5494         {
5495           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5496           /* The linker may expand the call to a longer sequence which
5497              uses $at, so we must break rather than return.  */
5498           break;
5499         }
5500       else
5501         abort ();
5502
5503       return;
5504
5505     case M_LB_AB:
5506       s = "lb";
5507       goto ld;
5508     case M_LBU_AB:
5509       s = "lbu";
5510       goto ld;
5511     case M_LH_AB:
5512       s = "lh";
5513       goto ld;
5514     case M_LHU_AB:
5515       s = "lhu";
5516       goto ld;
5517     case M_LW_AB:
5518       s = "lw";
5519       goto ld;
5520     case M_LWC0_AB:
5521       s = "lwc0";
5522       /* Itbl support may require additional care here.  */
5523       coproc = 1;
5524       goto ld;
5525     case M_LWC1_AB:
5526       s = "lwc1";
5527       /* Itbl support may require additional care here.  */
5528       coproc = 1;
5529       goto ld;
5530     case M_LWC2_AB:
5531       s = "lwc2";
5532       /* Itbl support may require additional care here.  */
5533       coproc = 1;
5534       goto ld;
5535     case M_LWC3_AB:
5536       s = "lwc3";
5537       /* Itbl support may require additional care here.  */
5538       coproc = 1;
5539       goto ld;
5540     case M_LWL_AB:
5541       s = "lwl";
5542       lr = 1;
5543       goto ld;
5544     case M_LWR_AB:
5545       s = "lwr";
5546       lr = 1;
5547       goto ld;
5548     case M_LDC1_AB:
5549       if (mips_arch == CPU_R4650)
5550         {
5551           as_bad (_("opcode not supported on this processor"));
5552           return;
5553         }
5554       s = "ldc1";
5555       /* Itbl support may require additional care here.  */
5556       coproc = 1;
5557       goto ld;
5558     case M_LDC2_AB:
5559       s = "ldc2";
5560       /* Itbl support may require additional care here.  */
5561       coproc = 1;
5562       goto ld;
5563     case M_LDC3_AB:
5564       s = "ldc3";
5565       /* Itbl support may require additional care here.  */
5566       coproc = 1;
5567       goto ld;
5568     case M_LDL_AB:
5569       s = "ldl";
5570       lr = 1;
5571       goto ld;
5572     case M_LDR_AB:
5573       s = "ldr";
5574       lr = 1;
5575       goto ld;
5576     case M_LL_AB:
5577       s = "ll";
5578       goto ld;
5579     case M_LLD_AB:
5580       s = "lld";
5581       goto ld;
5582     case M_LWU_AB:
5583       s = "lwu";
5584     ld:
5585       if (breg == treg || coproc || lr)
5586         {
5587           tempreg = AT;
5588           used_at = 1;
5589         }
5590       else
5591         {
5592           tempreg = treg;
5593           used_at = 0;
5594         }
5595       goto ld_st;
5596     case M_SB_AB:
5597       s = "sb";
5598       goto st;
5599     case M_SH_AB:
5600       s = "sh";
5601       goto st;
5602     case M_SW_AB:
5603       s = "sw";
5604       goto st;
5605     case M_SWC0_AB:
5606       s = "swc0";
5607       /* Itbl support may require additional care here.  */
5608       coproc = 1;
5609       goto st;
5610     case M_SWC1_AB:
5611       s = "swc1";
5612       /* Itbl support may require additional care here.  */
5613       coproc = 1;
5614       goto st;
5615     case M_SWC2_AB:
5616       s = "swc2";
5617       /* Itbl support may require additional care here.  */
5618       coproc = 1;
5619       goto st;
5620     case M_SWC3_AB:
5621       s = "swc3";
5622       /* Itbl support may require additional care here.  */
5623       coproc = 1;
5624       goto st;
5625     case M_SWL_AB:
5626       s = "swl";
5627       goto st;
5628     case M_SWR_AB:
5629       s = "swr";
5630       goto st;
5631     case M_SC_AB:
5632       s = "sc";
5633       goto st;
5634     case M_SCD_AB:
5635       s = "scd";
5636       goto st;
5637     case M_SDC1_AB:
5638       if (mips_arch == CPU_R4650)
5639         {
5640           as_bad (_("opcode not supported on this processor"));
5641           return;
5642         }
5643       s = "sdc1";
5644       coproc = 1;
5645       /* Itbl support may require additional care here.  */
5646       goto st;
5647     case M_SDC2_AB:
5648       s = "sdc2";
5649       /* Itbl support may require additional care here.  */
5650       coproc = 1;
5651       goto st;
5652     case M_SDC3_AB:
5653       s = "sdc3";
5654       /* Itbl support may require additional care here.  */
5655       coproc = 1;
5656       goto st;
5657     case M_SDL_AB:
5658       s = "sdl";
5659       goto st;
5660     case M_SDR_AB:
5661       s = "sdr";
5662     st:
5663       tempreg = AT;
5664       used_at = 1;
5665     ld_st:
5666       /* Itbl support may require additional care here.  */
5667       if (mask == M_LWC1_AB
5668           || mask == M_SWC1_AB
5669           || mask == M_LDC1_AB
5670           || mask == M_SDC1_AB
5671           || mask == M_L_DAB
5672           || mask == M_S_DAB)
5673         fmt = "T,o(b)";
5674       else if (coproc)
5675         fmt = "E,o(b)";
5676       else
5677         fmt = "t,o(b)";
5678
5679       /* For embedded PIC, we allow loads where the offset is calculated
5680          by subtracting a symbol in the current segment from an unknown
5681          symbol, relative to a base register, e.g.:
5682                 <op>    $treg, <sym>-<localsym>($breg)
5683          This is used by the compiler for switch statements.  */
5684       if (mips_pic == EMBEDDED_PIC
5685           && offset_expr.X_op == O_subtract
5686           && (symbol_constant_p (offset_expr.X_op_symbol)
5687               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5688               : (symbol_equated_p (offset_expr.X_op_symbol)
5689                  && (S_GET_SEGMENT
5690                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5691                       ->X_add_symbol)
5692                      == now_seg)))
5693           && breg != 0
5694           && (offset_expr.X_add_number == 0
5695               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5696         {
5697           /* For this case, we output the instructions:
5698                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5699                 addiu   $tempreg,$tempreg,$breg
5700                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5701              If the relocation would fit entirely in 16 bits, it would be
5702              nice to emit:
5703                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5704              instead, but that seems quite difficult.  */
5705           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5706                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5707           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5708                        ((bfd_arch_bits_per_address (stdoutput) == 32
5709                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5710                         ? "addu" : "daddu"),
5711                        "d,v,t", tempreg, tempreg, breg);
5712           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5713                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5714           if (! used_at)
5715             return;
5716           break;
5717         }
5718
5719       if (offset_expr.X_op != O_constant
5720           && offset_expr.X_op != O_symbol)
5721         {
5722           as_bad (_("expression too complex"));
5723           offset_expr.X_op = O_constant;
5724         }
5725
5726       /* A constant expression in PIC code can be handled just as it
5727          is in non PIC code.  */
5728       if (mips_pic == NO_PIC
5729           || offset_expr.X_op == O_constant)
5730         {
5731           char *p;
5732
5733           /* If this is a reference to a GP relative symbol, and there
5734              is no base register, we want
5735                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5736              Otherwise, if there is no base register, we want
5737                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5738                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5739              If we have a constant, we need two instructions anyhow,
5740              so we always use the latter form.
5741
5742              If we have a base register, and this is a reference to a
5743              GP relative symbol, we want
5744                addu     $tempreg,$breg,$gp
5745                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5746              Otherwise we want
5747                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5748                addu     $tempreg,$tempreg,$breg
5749                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5750              With a constant we always use the latter case.
5751
5752              With 64bit address space and no base register and $at usable,
5753              we want
5754                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5755                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5756                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5757                dsll32   $tempreg,0
5758                daddu    $tempreg,$at
5759                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5760              If we have a base register, we want
5761                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5762                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5763                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5764                daddu    $at,$breg
5765                dsll32   $tempreg,0
5766                daddu    $tempreg,$at
5767                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5768
5769              Without $at we can't generate the optimal path for superscalar
5770              processors here since this would require two temporary registers.
5771                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5772                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5773                dsll     $tempreg,16
5774                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5775                dsll     $tempreg,16
5776                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5777              If we have a base register, we want
5778                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5779                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5780                dsll     $tempreg,16
5781                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5782                dsll     $tempreg,16
5783                daddu    $tempreg,$tempreg,$breg
5784                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5785
5786              If we have 64-bit addresses, as an optimization, for
5787              addresses which are 32-bit constants (e.g. kseg0/kseg1
5788              addresses) we fall back to the 32-bit address generation
5789              mechanism since it is more efficient.  Note that due to
5790              the signed offset used by memory operations, the 32-bit
5791              range is shifted down by 32768 here.  This code should
5792              probably attempt to generate 64-bit constants more
5793              efficiently in general.
5794            */
5795           if (HAVE_64BIT_ADDRESSES
5796               && !(offset_expr.X_op == O_constant
5797                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5798             {
5799               p = NULL;
5800
5801               /* We don't do GP optimization for now because RELAX_ENCODE can't
5802                  hold the data for such large chunks.  */
5803
5804               if (used_at == 0 && ! mips_opts.noat)
5805                 {
5806                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5807                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5808                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5809                                AT, (int) BFD_RELOC_HI16_S);
5810                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5811                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5812                   if (breg != 0)
5813                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5814                                  "d,v,t", AT, AT, breg);
5815                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5816                                "d,w,<", tempreg, tempreg, 0);
5817                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5818                                "d,v,t", tempreg, tempreg, AT);
5819                   macro_build (p, &icnt, &offset_expr, s,
5820                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5821                   used_at = 1;
5822                 }
5823               else
5824                 {
5825                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5826                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5827                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5828                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5829                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5830                                "d,w,<", tempreg, tempreg, 16);
5831                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5832                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5833                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5834                                "d,w,<", tempreg, tempreg, 16);
5835                   if (breg != 0)
5836                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5837                                  "d,v,t", tempreg, tempreg, breg);
5838                   macro_build (p, &icnt, &offset_expr, s,
5839                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5840                 }
5841
5842               return;
5843             }
5844
5845           if (breg == 0)
5846             {
5847               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5848                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5849                 p = NULL;
5850               else
5851                 {
5852                   frag_grow (20);
5853                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5854                                treg, (int) BFD_RELOC_GPREL16,
5855                                mips_gp_register);
5856                   p = frag_var (rs_machine_dependent, 8, 0,
5857                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5858                                               (mips_opts.warn_about_macros
5859                                                || (used_at
5860                                                    && mips_opts.noat))),
5861                                 offset_expr.X_add_symbol, 0, NULL);
5862                   used_at = 0;
5863                 }
5864               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5865               if (p != NULL)
5866                 p += 4;
5867               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5868                            (int) BFD_RELOC_LO16, tempreg);
5869             }
5870           else
5871             {
5872               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5873                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5874                 p = NULL;
5875               else
5876                 {
5877                   frag_grow (28);
5878                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5879                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5880                                "d,v,t", tempreg, breg, mips_gp_register);
5881                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5882                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5883                   p = frag_var (rs_machine_dependent, 12, 0,
5884                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5885                                 offset_expr.X_add_symbol, 0, NULL);
5886                 }
5887               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5888               if (p != NULL)
5889                 p += 4;
5890               macro_build (p, &icnt, (expressionS *) NULL,
5891                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5892                            "d,v,t", tempreg, tempreg, breg);
5893               if (p != NULL)
5894                 p += 4;
5895               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5896                            (int) BFD_RELOC_LO16, tempreg);
5897             }
5898         }
5899       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5900         {
5901           char *p;
5902           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5903
5904           /* If this is a reference to an external symbol, we want
5905                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5906                nop
5907                <op>     $treg,0($tempreg)
5908              Otherwise we want
5909                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5910                nop
5911                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5912                <op>     $treg,0($tempreg)
5913              If we have NewABI, we want
5914                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5915              If there is a base register, we add it to $tempreg before
5916              the <op>.  If there is a constant, we stick it in the
5917              <op> instruction.  We don't handle constants larger than
5918              16 bits, because we have no way to load the upper 16 bits
5919              (actually, we could handle them for the subset of cases
5920              in which we are not using $at).  */
5921           assert (offset_expr.X_op == O_symbol);
5922           expr1.X_add_number = offset_expr.X_add_number;
5923           offset_expr.X_add_number = 0;
5924           if (HAVE_NEWABI)
5925             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5926           if (expr1.X_add_number < -0x8000
5927               || expr1.X_add_number >= 0x8000)
5928             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5929           frag_grow (20);
5930           macro_build ((char *) NULL, &icnt, &offset_expr,
5931                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5932                        (int) lw_reloc_type, mips_gp_register);
5933           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5934           p = frag_var (rs_machine_dependent, 4, 0,
5935                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5936                         offset_expr.X_add_symbol, 0, NULL);
5937           macro_build (p, &icnt, &offset_expr,
5938                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5939                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5940           if (breg != 0)
5941             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5942                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5943                          "d,v,t", tempreg, tempreg, breg);
5944           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5945                        (int) BFD_RELOC_LO16, tempreg);
5946         }
5947       else if (mips_pic == SVR4_PIC)
5948         {
5949           int gpdel;
5950           char *p;
5951
5952           /* If this is a reference to an external symbol, we want
5953                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5954                addu     $tempreg,$tempreg,$gp
5955                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5956                <op>     $treg,0($tempreg)
5957              Otherwise we want
5958                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5959                nop
5960                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5961                <op>     $treg,0($tempreg)
5962              If there is a base register, we add it to $tempreg before
5963              the <op>.  If there is a constant, we stick it in the
5964              <op> instruction.  We don't handle constants larger than
5965              16 bits, because we have no way to load the upper 16 bits
5966              (actually, we could handle them for the subset of cases
5967              in which we are not using $at).
5968
5969              For NewABI, we want
5970                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
5971                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5972                <op>     $treg,0($tempreg)
5973            */
5974           assert (offset_expr.X_op == O_symbol);
5975           expr1.X_add_number = offset_expr.X_add_number;
5976           offset_expr.X_add_number = 0;
5977           if (expr1.X_add_number < -0x8000
5978               || expr1.X_add_number >= 0x8000)
5979             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5980           if (HAVE_NEWABI)
5981             {
5982               macro_build ((char *) NULL, &icnt, &offset_expr,
5983                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5984                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5985                            mips_gp_register);
5986               macro_build ((char *) NULL, &icnt, &offset_expr,
5987                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5988                            "t,r,j", tempreg, tempreg,
5989                            BFD_RELOC_MIPS_GOT_OFST);
5990               if (breg != 0)
5991                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5992                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5993                              "d,v,t", tempreg, tempreg, breg);
5994               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5995                            (int) BFD_RELOC_LO16, tempreg);
5996
5997               if (! used_at)
5998                 return;
5999
6000               break;
6001             }
6002           if (reg_needs_delay (mips_gp_register))
6003             gpdel = 4;
6004           else
6005             gpdel = 0;
6006           frag_grow (36);
6007           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6008                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6009           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6010                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6011                        "d,v,t", tempreg, tempreg, mips_gp_register);
6012           macro_build ((char *) NULL, &icnt, &offset_expr,
6013                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6014                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6015                        tempreg);
6016           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6017                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6018                         offset_expr.X_add_symbol, 0, NULL);
6019           if (gpdel > 0)
6020             {
6021               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6022               p += 4;
6023             }
6024           macro_build (p, &icnt, &offset_expr,
6025                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6026                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6027                        mips_gp_register);
6028           p += 4;
6029           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6030           p += 4;
6031           macro_build (p, &icnt, &offset_expr,
6032                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6033                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6034           if (breg != 0)
6035             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6036                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6037                          "d,v,t", tempreg, tempreg, breg);
6038           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6039                        (int) BFD_RELOC_LO16, tempreg);
6040         }
6041       else if (mips_pic == EMBEDDED_PIC)
6042         {
6043           /* If there is no base register, we want
6044                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6045              If there is a base register, we want
6046                addu     $tempreg,$breg,$gp
6047                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6048              */
6049           assert (offset_expr.X_op == O_symbol);
6050           if (breg == 0)
6051             {
6052               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6053                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6054               used_at = 0;
6055             }
6056           else
6057             {
6058               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6059                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6060                            "d,v,t", tempreg, breg, mips_gp_register);
6061               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6062                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6063             }
6064         }
6065       else
6066         abort ();
6067
6068       if (! used_at)
6069         return;
6070
6071       break;
6072
6073     case M_LI:
6074     case M_LI_S:
6075       load_register (&icnt, treg, &imm_expr, 0);
6076       return;
6077
6078     case M_DLI:
6079       load_register (&icnt, treg, &imm_expr, 1);
6080       return;
6081
6082     case M_LI_SS:
6083       if (imm_expr.X_op == O_constant)
6084         {
6085           load_register (&icnt, AT, &imm_expr, 0);
6086           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6087                        "mtc1", "t,G", AT, treg);
6088           break;
6089         }
6090       else
6091         {
6092           assert (offset_expr.X_op == O_symbol
6093                   && strcmp (segment_name (S_GET_SEGMENT
6094                                            (offset_expr.X_add_symbol)),
6095                              ".lit4") == 0
6096                   && offset_expr.X_add_number == 0);
6097           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6098                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6099           return;
6100         }
6101
6102     case M_LI_D:
6103       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6104          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6105          order 32 bits of the value and the low order 32 bits are either
6106          zero or in OFFSET_EXPR.  */
6107       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6108         {
6109           if (HAVE_64BIT_GPRS)
6110             load_register (&icnt, treg, &imm_expr, 1);
6111           else
6112             {
6113               int hreg, lreg;
6114
6115               if (target_big_endian)
6116                 {
6117                   hreg = treg;
6118                   lreg = treg + 1;
6119                 }
6120               else
6121                 {
6122                   hreg = treg + 1;
6123                   lreg = treg;
6124                 }
6125
6126               if (hreg <= 31)
6127                 load_register (&icnt, hreg, &imm_expr, 0);
6128               if (lreg <= 31)
6129                 {
6130                   if (offset_expr.X_op == O_absent)
6131                     move_register (&icnt, lreg, 0);
6132                   else
6133                     {
6134                       assert (offset_expr.X_op == O_constant);
6135                       load_register (&icnt, lreg, &offset_expr, 0);
6136                     }
6137                 }
6138             }
6139           return;
6140         }
6141
6142       /* We know that sym is in the .rdata section.  First we get the
6143          upper 16 bits of the address.  */
6144       if (mips_pic == NO_PIC)
6145         {
6146           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6147         }
6148       else if (mips_pic == SVR4_PIC)
6149         {
6150           macro_build ((char *) NULL, &icnt, &offset_expr,
6151                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6152                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6153                        mips_gp_register);
6154         }
6155       else if (mips_pic == EMBEDDED_PIC)
6156         {
6157           /* For embedded PIC we pick up the entire address off $gp in
6158              a single instruction.  */
6159           macro_build ((char *) NULL, &icnt, &offset_expr,
6160                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6161                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6162           offset_expr.X_op = O_constant;
6163           offset_expr.X_add_number = 0;
6164         }
6165       else
6166         abort ();
6167
6168       /* Now we load the register(s).  */
6169       if (HAVE_64BIT_GPRS)
6170         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6171                      treg, (int) BFD_RELOC_LO16, AT);
6172       else
6173         {
6174           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6175                        treg, (int) BFD_RELOC_LO16, AT);
6176           if (treg != RA)
6177             {
6178               /* FIXME: How in the world do we deal with the possible
6179                  overflow here?  */
6180               offset_expr.X_add_number += 4;
6181               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6182                            treg + 1, (int) BFD_RELOC_LO16, AT);
6183             }
6184         }
6185
6186       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6187          does not become a variant frag.  */
6188       frag_wane (frag_now);
6189       frag_new (0);
6190
6191       break;
6192
6193     case M_LI_DD:
6194       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6195          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6196          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6197          the value and the low order 32 bits are either zero or in
6198          OFFSET_EXPR.  */
6199       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6200         {
6201           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6202           if (HAVE_64BIT_FPRS)
6203             {
6204               assert (HAVE_64BIT_GPRS);
6205               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6206                            "dmtc1", "t,S", AT, treg);
6207             }
6208           else
6209             {
6210               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6211                            "mtc1", "t,G", AT, treg + 1);
6212               if (offset_expr.X_op == O_absent)
6213                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6214                              "mtc1", "t,G", 0, treg);
6215               else
6216                 {
6217                   assert (offset_expr.X_op == O_constant);
6218                   load_register (&icnt, AT, &offset_expr, 0);
6219                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6220                                "mtc1", "t,G", AT, treg);
6221                 }
6222             }
6223           break;
6224         }
6225
6226       assert (offset_expr.X_op == O_symbol
6227               && offset_expr.X_add_number == 0);
6228       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6229       if (strcmp (s, ".lit8") == 0)
6230         {
6231           if (mips_opts.isa != ISA_MIPS1)
6232             {
6233               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6234                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6235                            mips_gp_register);
6236               return;
6237             }
6238           breg = mips_gp_register;
6239           r = BFD_RELOC_MIPS_LITERAL;
6240           goto dob;
6241         }
6242       else
6243         {
6244           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6245           if (mips_pic == SVR4_PIC)
6246             macro_build ((char *) NULL, &icnt, &offset_expr,
6247                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6248                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6249                          mips_gp_register);
6250           else
6251             {
6252               /* FIXME: This won't work for a 64 bit address.  */
6253               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6254             }
6255
6256           if (mips_opts.isa != ISA_MIPS1)
6257             {
6258               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6259                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6260
6261               /* To avoid confusion in tc_gen_reloc, we must ensure
6262                  that this does not become a variant frag.  */
6263               frag_wane (frag_now);
6264               frag_new (0);
6265
6266               break;
6267             }
6268           breg = AT;
6269           r = BFD_RELOC_LO16;
6270           goto dob;
6271         }
6272
6273     case M_L_DOB:
6274       if (mips_arch == CPU_R4650)
6275         {
6276           as_bad (_("opcode not supported on this processor"));
6277           return;
6278         }
6279       /* Even on a big endian machine $fn comes before $fn+1.  We have
6280          to adjust when loading from memory.  */
6281       r = BFD_RELOC_LO16;
6282     dob:
6283       assert (mips_opts.isa == ISA_MIPS1);
6284       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6285                    target_big_endian ? treg + 1 : treg,
6286                    (int) r, breg);
6287       /* FIXME: A possible overflow which I don't know how to deal
6288          with.  */
6289       offset_expr.X_add_number += 4;
6290       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6291                    target_big_endian ? treg : treg + 1,
6292                    (int) r, breg);
6293
6294       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6295          does not become a variant frag.  */
6296       frag_wane (frag_now);
6297       frag_new (0);
6298
6299       if (breg != AT)
6300         return;
6301       break;
6302
6303     case M_L_DAB:
6304       /*
6305        * The MIPS assembler seems to check for X_add_number not
6306        * being double aligned and generating:
6307        *        lui     at,%hi(foo+1)
6308        *        addu    at,at,v1
6309        *        addiu   at,at,%lo(foo+1)
6310        *        lwc1    f2,0(at)
6311        *        lwc1    f3,4(at)
6312        * But, the resulting address is the same after relocation so why
6313        * generate the extra instruction?
6314        */
6315       if (mips_arch == CPU_R4650)
6316         {
6317           as_bad (_("opcode not supported on this processor"));
6318           return;
6319         }
6320       /* Itbl support may require additional care here.  */
6321       coproc = 1;
6322       if (mips_opts.isa != ISA_MIPS1)
6323         {
6324           s = "ldc1";
6325           goto ld;
6326         }
6327
6328       s = "lwc1";
6329       fmt = "T,o(b)";
6330       goto ldd_std;
6331
6332     case M_S_DAB:
6333       if (mips_arch == CPU_R4650)
6334         {
6335           as_bad (_("opcode not supported on this processor"));
6336           return;
6337         }
6338
6339       if (mips_opts.isa != ISA_MIPS1)
6340         {
6341           s = "sdc1";
6342           goto st;
6343         }
6344
6345       s = "swc1";
6346       fmt = "T,o(b)";
6347       /* Itbl support may require additional care here.  */
6348       coproc = 1;
6349       goto ldd_std;
6350
6351     case M_LD_AB:
6352       if (HAVE_64BIT_GPRS)
6353         {
6354           s = "ld";
6355           goto ld;
6356         }
6357
6358       s = "lw";
6359       fmt = "t,o(b)";
6360       goto ldd_std;
6361
6362     case M_SD_AB:
6363       if (HAVE_64BIT_GPRS)
6364         {
6365           s = "sd";
6366           goto st;
6367         }
6368
6369       s = "sw";
6370       fmt = "t,o(b)";
6371
6372     ldd_std:
6373       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6374          loads for the case of doing a pair of loads to simulate an 'ld'.
6375          This is not currently done by the compiler, and assembly coders
6376          writing embedded-pic code can cope.  */
6377
6378       if (offset_expr.X_op != O_symbol
6379           && offset_expr.X_op != O_constant)
6380         {
6381           as_bad (_("expression too complex"));
6382           offset_expr.X_op = O_constant;
6383         }
6384
6385       /* Even on a big endian machine $fn comes before $fn+1.  We have
6386          to adjust when loading from memory.  We set coproc if we must
6387          load $fn+1 first.  */
6388       /* Itbl support may require additional care here.  */
6389       if (! target_big_endian)
6390         coproc = 0;
6391
6392       if (mips_pic == NO_PIC
6393           || offset_expr.X_op == O_constant)
6394         {
6395           char *p;
6396
6397           /* If this is a reference to a GP relative symbol, we want
6398                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6399                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6400              If we have a base register, we use this
6401                addu     $at,$breg,$gp
6402                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6403                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6404              If this is not a GP relative symbol, we want
6405                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6406                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6407                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6408              If there is a base register, we add it to $at after the
6409              lui instruction.  If there is a constant, we always use
6410              the last case.  */
6411           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6412               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6413             {
6414               p = NULL;
6415               used_at = 1;
6416             }
6417           else
6418             {
6419               int off;
6420
6421               if (breg == 0)
6422                 {
6423                   frag_grow (28);
6424                   tempreg = mips_gp_register;
6425                   off = 0;
6426                   used_at = 0;
6427                 }
6428               else
6429                 {
6430                   frag_grow (36);
6431                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6432                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6433                                "d,v,t", AT, breg, mips_gp_register);
6434                   tempreg = AT;
6435                   off = 4;
6436                   used_at = 1;
6437                 }
6438
6439               /* Itbl support may require additional care here.  */
6440               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6441                            coproc ? treg + 1 : treg,
6442                            (int) BFD_RELOC_GPREL16, tempreg);
6443               offset_expr.X_add_number += 4;
6444
6445               /* Set mips_optimize to 2 to avoid inserting an
6446                  undesired nop.  */
6447               hold_mips_optimize = mips_optimize;
6448               mips_optimize = 2;
6449               /* Itbl support may require additional care here.  */
6450               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6451                            coproc ? treg : treg + 1,
6452                            (int) BFD_RELOC_GPREL16, tempreg);
6453               mips_optimize = hold_mips_optimize;
6454
6455               p = frag_var (rs_machine_dependent, 12 + off, 0,
6456                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6457                                           used_at && mips_opts.noat),
6458                             offset_expr.X_add_symbol, 0, NULL);
6459
6460               /* We just generated two relocs.  When tc_gen_reloc
6461                  handles this case, it will skip the first reloc and
6462                  handle the second.  The second reloc already has an
6463                  extra addend of 4, which we added above.  We must
6464                  subtract it out, and then subtract another 4 to make
6465                  the first reloc come out right.  The second reloc
6466                  will come out right because we are going to add 4 to
6467                  offset_expr when we build its instruction below.
6468
6469                  If we have a symbol, then we don't want to include
6470                  the offset, because it will wind up being included
6471                  when we generate the reloc.  */
6472
6473               if (offset_expr.X_op == O_constant)
6474                 offset_expr.X_add_number -= 8;
6475               else
6476                 {
6477                   offset_expr.X_add_number = -4;
6478                   offset_expr.X_op = O_constant;
6479                 }
6480             }
6481           macro_build_lui (p, &icnt, &offset_expr, AT);
6482           if (p != NULL)
6483             p += 4;
6484           if (breg != 0)
6485             {
6486               macro_build (p, &icnt, (expressionS *) NULL,
6487                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6488                            "d,v,t", AT, breg, AT);
6489               if (p != NULL)
6490                 p += 4;
6491             }
6492           /* Itbl support may require additional care here.  */
6493           macro_build (p, &icnt, &offset_expr, s, fmt,
6494                        coproc ? treg + 1 : treg,
6495                        (int) BFD_RELOC_LO16, AT);
6496           if (p != NULL)
6497             p += 4;
6498           /* FIXME: How do we handle overflow here?  */
6499           offset_expr.X_add_number += 4;
6500           /* Itbl support may require additional care here.  */
6501           macro_build (p, &icnt, &offset_expr, s, fmt,
6502                        coproc ? treg : treg + 1,
6503                        (int) BFD_RELOC_LO16, AT);
6504         }
6505       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6506         {
6507           int off;
6508
6509           /* If this is a reference to an external symbol, we want
6510                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6511                nop
6512                <op>     $treg,0($at)
6513                <op>     $treg+1,4($at)
6514              Otherwise we want
6515                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6516                nop
6517                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6518                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6519              If there is a base register we add it to $at before the
6520              lwc1 instructions.  If there is a constant we include it
6521              in the lwc1 instructions.  */
6522           used_at = 1;
6523           expr1.X_add_number = offset_expr.X_add_number;
6524           offset_expr.X_add_number = 0;
6525           if (expr1.X_add_number < -0x8000
6526               || expr1.X_add_number >= 0x8000 - 4)
6527             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6528           if (breg == 0)
6529             off = 0;
6530           else
6531             off = 4;
6532           frag_grow (24 + off);
6533           macro_build ((char *) NULL, &icnt, &offset_expr,
6534                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6535                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6536           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6537           if (breg != 0)
6538             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6539                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6540                          "d,v,t", AT, breg, AT);
6541           /* Itbl support may require additional care here.  */
6542           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6543                        coproc ? treg + 1 : treg,
6544                        (int) BFD_RELOC_LO16, AT);
6545           expr1.X_add_number += 4;
6546
6547           /* Set mips_optimize to 2 to avoid inserting an undesired
6548              nop.  */
6549           hold_mips_optimize = mips_optimize;
6550           mips_optimize = 2;
6551           /* Itbl support may require additional care here.  */
6552           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6553                        coproc ? treg : treg + 1,
6554                        (int) BFD_RELOC_LO16, AT);
6555           mips_optimize = hold_mips_optimize;
6556
6557           (void) frag_var (rs_machine_dependent, 0, 0,
6558                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6559                            offset_expr.X_add_symbol, 0, NULL);
6560         }
6561       else if (mips_pic == SVR4_PIC)
6562         {
6563           int gpdel, off;
6564           char *p;
6565
6566           /* If this is a reference to an external symbol, we want
6567                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6568                addu     $at,$at,$gp
6569                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6570                nop
6571                <op>     $treg,0($at)
6572                <op>     $treg+1,4($at)
6573              Otherwise we want
6574                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6575                nop
6576                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6577                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6578              If there is a base register we add it to $at before the
6579              lwc1 instructions.  If there is a constant we include it
6580              in the lwc1 instructions.  */
6581           used_at = 1;
6582           expr1.X_add_number = offset_expr.X_add_number;
6583           offset_expr.X_add_number = 0;
6584           if (expr1.X_add_number < -0x8000
6585               || expr1.X_add_number >= 0x8000 - 4)
6586             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6587           if (reg_needs_delay (mips_gp_register))
6588             gpdel = 4;
6589           else
6590             gpdel = 0;
6591           if (breg == 0)
6592             off = 0;
6593           else
6594             off = 4;
6595           frag_grow (56);
6596           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6597                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6598           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6599                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6600                        "d,v,t", AT, AT, mips_gp_register);
6601           macro_build ((char *) NULL, &icnt, &offset_expr,
6602                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6603                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6604           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6605           if (breg != 0)
6606             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6607                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6608                          "d,v,t", AT, breg, AT);
6609           /* Itbl support may require additional care here.  */
6610           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6611                        coproc ? treg + 1 : treg,
6612                        (int) BFD_RELOC_LO16, AT);
6613           expr1.X_add_number += 4;
6614
6615           /* Set mips_optimize to 2 to avoid inserting an undesired
6616              nop.  */
6617           hold_mips_optimize = mips_optimize;
6618           mips_optimize = 2;
6619           /* Itbl support may require additional care here.  */
6620           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6621                        coproc ? treg : treg + 1,
6622                        (int) BFD_RELOC_LO16, AT);
6623           mips_optimize = hold_mips_optimize;
6624           expr1.X_add_number -= 4;
6625
6626           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6627                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6628                                       8 + gpdel + off, 1, 0),
6629                         offset_expr.X_add_symbol, 0, NULL);
6630           if (gpdel > 0)
6631             {
6632               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6633               p += 4;
6634             }
6635           macro_build (p, &icnt, &offset_expr,
6636                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6637                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6638                        mips_gp_register);
6639           p += 4;
6640           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6641           p += 4;
6642           if (breg != 0)
6643             {
6644               macro_build (p, &icnt, (expressionS *) NULL,
6645                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6646                            "d,v,t", AT, breg, AT);
6647               p += 4;
6648             }
6649           /* Itbl support may require additional care here.  */
6650           macro_build (p, &icnt, &expr1, s, fmt,
6651                        coproc ? treg + 1 : treg,
6652                        (int) BFD_RELOC_LO16, AT);
6653           p += 4;
6654           expr1.X_add_number += 4;
6655
6656           /* Set mips_optimize to 2 to avoid inserting an undesired
6657              nop.  */
6658           hold_mips_optimize = mips_optimize;
6659           mips_optimize = 2;
6660           /* Itbl support may require additional care here.  */
6661           macro_build (p, &icnt, &expr1, s, fmt,
6662                        coproc ? treg : treg + 1,
6663                        (int) BFD_RELOC_LO16, AT);
6664           mips_optimize = hold_mips_optimize;
6665         }
6666       else if (mips_pic == EMBEDDED_PIC)
6667         {
6668           /* If there is no base register, we use
6669                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6670                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6671              If we have a base register, we use
6672                addu     $at,$breg,$gp
6673                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6674                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6675              */
6676           if (breg == 0)
6677             {
6678               tempreg = mips_gp_register;
6679               used_at = 0;
6680             }
6681           else
6682             {
6683               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6684                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6685                            "d,v,t", AT, breg, mips_gp_register);
6686               tempreg = AT;
6687               used_at = 1;
6688             }
6689
6690           /* Itbl support may require additional care here.  */
6691           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6692                        coproc ? treg + 1 : treg,
6693                        (int) BFD_RELOC_GPREL16, tempreg);
6694           offset_expr.X_add_number += 4;
6695           /* Itbl support may require additional care here.  */
6696           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6697                        coproc ? treg : treg + 1,
6698                        (int) BFD_RELOC_GPREL16, tempreg);
6699         }
6700       else
6701         abort ();
6702
6703       if (! used_at)
6704         return;
6705
6706       break;
6707
6708     case M_LD_OB:
6709       s = "lw";
6710       goto sd_ob;
6711     case M_SD_OB:
6712       s = "sw";
6713     sd_ob:
6714       assert (HAVE_32BIT_ADDRESSES);
6715       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6716                    (int) BFD_RELOC_LO16, breg);
6717       offset_expr.X_add_number += 4;
6718       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6719                    (int) BFD_RELOC_LO16, breg);
6720       return;
6721
6722    /* New code added to support COPZ instructions.
6723       This code builds table entries out of the macros in mip_opcodes.
6724       R4000 uses interlocks to handle coproc delays.
6725       Other chips (like the R3000) require nops to be inserted for delays.
6726
6727       FIXME: Currently, we require that the user handle delays.
6728       In order to fill delay slots for non-interlocked chips,
6729       we must have a way to specify delays based on the coprocessor.
6730       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6731       What are the side-effects of the cop instruction?
6732       What cache support might we have and what are its effects?
6733       Both coprocessor & memory require delays. how long???
6734       What registers are read/set/modified?
6735
6736       If an itbl is provided to interpret cop instructions,
6737       this knowledge can be encoded in the itbl spec.  */
6738
6739     case M_COP0:
6740       s = "c0";
6741       goto copz;
6742     case M_COP1:
6743       s = "c1";
6744       goto copz;
6745     case M_COP2:
6746       s = "c2";
6747       goto copz;
6748     case M_COP3:
6749       s = "c3";
6750     copz:
6751       /* For now we just do C (same as Cz).  The parameter will be
6752          stored in insn_opcode by mips_ip.  */
6753       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6754                    ip->insn_opcode);
6755       return;
6756
6757     case M_MOVE:
6758       move_register (&icnt, dreg, sreg);
6759       return;
6760
6761 #ifdef LOSING_COMPILER
6762     default:
6763       /* Try and see if this is a new itbl instruction.
6764          This code builds table entries out of the macros in mip_opcodes.
6765          FIXME: For now we just assemble the expression and pass it's
6766          value along as a 32-bit immediate.
6767          We may want to have the assembler assemble this value,
6768          so that we gain the assembler's knowledge of delay slots,
6769          symbols, etc.
6770          Would it be more efficient to use mask (id) here? */
6771       if (itbl_have_entries
6772           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6773         {
6774           s = ip->insn_mo->name;
6775           s2 = "cop3";
6776           coproc = ITBL_DECODE_PNUM (immed_expr);;
6777           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6778           return;
6779         }
6780       macro2 (ip);
6781       return;
6782     }
6783   if (mips_opts.noat)
6784     as_warn (_("Macro used $at after \".set noat\""));
6785 }
6786
6787 static void
6788 macro2 (ip)
6789      struct mips_cl_insn *ip;
6790 {
6791   register int treg, sreg, dreg, breg;
6792   int tempreg;
6793   int mask;
6794   int icnt = 0;
6795   int used_at;
6796   expressionS expr1;
6797   const char *s;
6798   const char *s2;
6799   const char *fmt;
6800   int likely = 0;
6801   int dbl = 0;
6802   int coproc = 0;
6803   int lr = 0;
6804   int imm = 0;
6805   int off;
6806   offsetT maxnum;
6807   bfd_reloc_code_real_type r;
6808   char *p;
6809
6810   treg = (ip->insn_opcode >> 16) & 0x1f;
6811   dreg = (ip->insn_opcode >> 11) & 0x1f;
6812   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6813   mask = ip->insn_mo->mask;
6814
6815   expr1.X_op = O_constant;
6816   expr1.X_op_symbol = NULL;
6817   expr1.X_add_symbol = NULL;
6818   expr1.X_add_number = 1;
6819
6820   switch (mask)
6821     {
6822 #endif /* LOSING_COMPILER */
6823
6824     case M_DMUL:
6825       dbl = 1;
6826     case M_MUL:
6827       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6828                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6829       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6830                    dreg);
6831       return;
6832
6833     case M_DMUL_I:
6834       dbl = 1;
6835     case M_MUL_I:
6836       /* The MIPS assembler some times generates shifts and adds.  I'm
6837          not trying to be that fancy. GCC should do this for us
6838          anyway.  */
6839       load_register (&icnt, AT, &imm_expr, dbl);
6840       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6841                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6842       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6843                    dreg);
6844       break;
6845
6846     case M_DMULO_I:
6847       dbl = 1;
6848     case M_MULO_I:
6849       imm = 1;
6850       goto do_mulo;
6851
6852     case M_DMULO:
6853       dbl = 1;
6854     case M_MULO:
6855     do_mulo:
6856       mips_emit_delays (TRUE);
6857       ++mips_opts.noreorder;
6858       mips_any_noreorder = 1;
6859       if (imm)
6860         load_register (&icnt, AT, &imm_expr, dbl);
6861       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6862                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6863       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6864                    dreg);
6865       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6866                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6867       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6868                    AT);
6869       if (mips_trap)
6870         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6871                      "s,t,q", dreg, AT, 6);
6872       else
6873         {
6874           expr1.X_add_number = 8;
6875           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6876                        AT);
6877           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6878                        0);
6879           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6880                        "c", 6);
6881         }
6882       --mips_opts.noreorder;
6883       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6884       break;
6885
6886     case M_DMULOU_I:
6887       dbl = 1;
6888     case M_MULOU_I:
6889       imm = 1;
6890       goto do_mulou;
6891
6892     case M_DMULOU:
6893       dbl = 1;
6894     case M_MULOU:
6895     do_mulou:
6896       mips_emit_delays (TRUE);
6897       ++mips_opts.noreorder;
6898       mips_any_noreorder = 1;
6899       if (imm)
6900         load_register (&icnt, AT, &imm_expr, dbl);
6901       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6902                    dbl ? "dmultu" : "multu",
6903                    "s,t", sreg, imm ? AT : treg);
6904       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6905                    AT);
6906       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6907                    dreg);
6908       if (mips_trap)
6909         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6910                      "s,t,q", AT, 0, 6);
6911       else
6912         {
6913           expr1.X_add_number = 8;
6914           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6915           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6916                        0);
6917           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6918                        "c", 6);
6919         }
6920       --mips_opts.noreorder;
6921       break;
6922
6923     case M_DROL:
6924       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
6925         {
6926           if (dreg == sreg)
6927             {
6928               tempreg = AT;
6929               used_at = 1;
6930             }
6931           else
6932             {
6933               tempreg = dreg;
6934               used_at = 0;
6935             }
6936           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
6937                        "d,w", tempreg, treg);
6938           macro_build ((char *) NULL, &icnt, NULL, "drorv",
6939                        "d,t,s", dreg, sreg, tempreg);
6940           if (used_at)
6941             break;
6942           return;
6943         }
6944       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6945                    "d,v,t", AT, 0, treg);
6946       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6947                    "d,t,s", AT, sreg, AT);
6948       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6949                    "d,t,s", dreg, sreg, treg);
6950       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6951                    "d,v,t", dreg, dreg, AT);
6952       break;
6953
6954     case M_ROL:
6955       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
6956         {
6957           if (dreg == sreg)
6958             {
6959               tempreg = AT;
6960               used_at = 1;
6961             }
6962           else
6963             {
6964               tempreg = dreg;
6965               used_at = 0;
6966             }
6967           macro_build ((char *) NULL, &icnt, NULL, "negu",
6968                        "d,w", tempreg, treg);
6969           macro_build ((char *) NULL, &icnt, NULL, "rorv",
6970                        "d,t,s", dreg, sreg, tempreg);
6971           if (used_at)
6972             break;
6973           return;
6974         }
6975       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6976                    "d,v,t", AT, 0, treg);
6977       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6978                    "d,t,s", AT, sreg, AT);
6979       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6980                    "d,t,s", dreg, sreg, treg);
6981       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6982                    "d,v,t", dreg, dreg, AT);
6983       break;
6984
6985     case M_DROL_I:
6986       {
6987         unsigned int rot;
6988         char *l, *r;
6989
6990         if (imm_expr.X_op != O_constant)
6991           as_bad (_("Improper rotate count"));
6992         rot = imm_expr.X_add_number & 0x3f;
6993         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
6994           {
6995             rot = (64 - rot) & 0x3f;
6996             if (rot >= 32)
6997               macro_build ((char *) NULL, &icnt, NULL, "dror32",
6998                            "d,w,<", dreg, sreg, rot - 32);
6999             else
7000               macro_build ((char *) NULL, &icnt, NULL, "dror",
7001                            "d,w,<", dreg, sreg, rot);
7002             return;
7003           }
7004         if (rot == 0)
7005           {
7006             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7007                          "d,w,<", dreg, sreg, 0);
7008             return;
7009           }
7010         l = (rot < 0x20) ? "dsll" : "dsll32";
7011         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7012         rot &= 0x1f;
7013         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7014                      "d,w,<", AT, sreg, rot);
7015         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7016                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7017         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7018                      "d,v,t", dreg, dreg, AT);
7019       }
7020       break;
7021
7022     case M_ROL_I:
7023       {
7024         unsigned int rot;
7025
7026         if (imm_expr.X_op != O_constant)
7027           as_bad (_("Improper rotate count"));
7028         rot = imm_expr.X_add_number & 0x1f;
7029         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7030           {
7031             macro_build ((char *) NULL, &icnt, NULL, "ror",
7032                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7033             return;
7034           }
7035         if (rot == 0)
7036           {
7037             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7038                          "d,w,<", dreg, sreg, 0);
7039             return;
7040           }
7041         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7042                      "d,w,<", AT, sreg, rot);
7043         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7044                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7045         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7046                      "d,v,t", dreg, dreg, AT);
7047       }
7048       break;
7049
7050     case M_DROR:
7051       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7052         {
7053           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7054                        "d,t,s", dreg, sreg, treg);
7055           return;
7056         }
7057       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7058                    "d,v,t", AT, 0, treg);
7059       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7060                    "d,t,s", AT, sreg, AT);
7061       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7062                    "d,t,s", dreg, sreg, treg);
7063       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7064                    "d,v,t", dreg, dreg, AT);
7065       break;
7066
7067     case M_ROR:
7068       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7069         {
7070           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7071                        "d,t,s", dreg, sreg, treg);
7072           return;
7073         }
7074       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7075                    "d,v,t", AT, 0, treg);
7076       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7077                    "d,t,s", AT, sreg, AT);
7078       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7079                    "d,t,s", dreg, sreg, treg);
7080       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7081                    "d,v,t", dreg, dreg, AT);
7082       break;
7083
7084     case M_DROR_I:
7085       {
7086         unsigned int rot;
7087         char *l, *r;
7088
7089         if (imm_expr.X_op != O_constant)
7090           as_bad (_("Improper rotate count"));
7091         rot = imm_expr.X_add_number & 0x3f;
7092         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7093           {
7094             if (rot >= 32)
7095               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7096                            "d,w,<", dreg, sreg, rot - 32);
7097             else
7098               macro_build ((char *) NULL, &icnt, NULL, "dror",
7099                            "d,w,<", dreg, sreg, rot);
7100             return;
7101           }
7102         if (rot == 0)
7103           {
7104             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7105                          "d,w,<", dreg, sreg, 0);
7106             return;
7107           }
7108         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7109         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7110         rot &= 0x1f;
7111         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7112                      "d,w,<", AT, sreg, rot);
7113         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7114                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7115         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7116                      "d,v,t", dreg, dreg, AT);
7117       }
7118       break;
7119
7120     case M_ROR_I:
7121       {
7122         unsigned int rot;
7123
7124         if (imm_expr.X_op != O_constant)
7125           as_bad (_("Improper rotate count"));
7126         rot = imm_expr.X_add_number & 0x1f;
7127         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7128           {
7129             macro_build ((char *) NULL, &icnt, NULL, "ror",
7130                          "d,w,<", dreg, sreg, rot);
7131             return;
7132           }
7133         if (rot == 0)
7134           {
7135             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7136                          "d,w,<", dreg, sreg, 0);
7137             return;
7138           }
7139         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7140                      "d,w,<", AT, sreg, rot);
7141         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7142                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7143         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7144                      "d,v,t", dreg, dreg, AT);
7145       }
7146       break;
7147
7148     case M_S_DOB:
7149       if (mips_arch == CPU_R4650)
7150         {
7151           as_bad (_("opcode not supported on this processor"));
7152           return;
7153         }
7154       assert (mips_opts.isa == ISA_MIPS1);
7155       /* Even on a big endian machine $fn comes before $fn+1.  We have
7156          to adjust when storing to memory.  */
7157       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7158                    target_big_endian ? treg + 1 : treg,
7159                    (int) BFD_RELOC_LO16, breg);
7160       offset_expr.X_add_number += 4;
7161       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7162                    target_big_endian ? treg : treg + 1,
7163                    (int) BFD_RELOC_LO16, breg);
7164       return;
7165
7166     case M_SEQ:
7167       if (sreg == 0)
7168         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7169                      treg, (int) BFD_RELOC_LO16);
7170       else if (treg == 0)
7171         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7172                      sreg, (int) BFD_RELOC_LO16);
7173       else
7174         {
7175           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7176                        "d,v,t", dreg, sreg, treg);
7177           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7178                        dreg, (int) BFD_RELOC_LO16);
7179         }
7180       return;
7181
7182     case M_SEQ_I:
7183       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7184         {
7185           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7186                        sreg, (int) BFD_RELOC_LO16);
7187           return;
7188         }
7189       if (sreg == 0)
7190         {
7191           as_warn (_("Instruction %s: result is always false"),
7192                    ip->insn_mo->name);
7193           move_register (&icnt, dreg, 0);
7194           return;
7195         }
7196       if (imm_expr.X_op == O_constant
7197           && imm_expr.X_add_number >= 0
7198           && imm_expr.X_add_number < 0x10000)
7199         {
7200           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7201                        sreg, (int) BFD_RELOC_LO16);
7202           used_at = 0;
7203         }
7204       else if (imm_expr.X_op == O_constant
7205                && imm_expr.X_add_number > -0x8000
7206                && imm_expr.X_add_number < 0)
7207         {
7208           imm_expr.X_add_number = -imm_expr.X_add_number;
7209           macro_build ((char *) NULL, &icnt, &imm_expr,
7210                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7211                        "t,r,j", dreg, sreg,
7212                        (int) BFD_RELOC_LO16);
7213           used_at = 0;
7214         }
7215       else
7216         {
7217           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7218           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7219                        "d,v,t", dreg, sreg, AT);
7220           used_at = 1;
7221         }
7222       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7223                    (int) BFD_RELOC_LO16);
7224       if (used_at)
7225         break;
7226       return;
7227
7228     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7229       s = "slt";
7230       goto sge;
7231     case M_SGEU:
7232       s = "sltu";
7233     sge:
7234       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7235                    dreg, sreg, treg);
7236       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7237                    (int) BFD_RELOC_LO16);
7238       return;
7239
7240     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7241     case M_SGEU_I:
7242       if (imm_expr.X_op == O_constant
7243           && imm_expr.X_add_number >= -0x8000
7244           && imm_expr.X_add_number < 0x8000)
7245         {
7246           macro_build ((char *) NULL, &icnt, &imm_expr,
7247                        mask == M_SGE_I ? "slti" : "sltiu",
7248                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7249           used_at = 0;
7250         }
7251       else
7252         {
7253           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7254           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7255                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7256                        AT);
7257           used_at = 1;
7258         }
7259       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7260                    (int) BFD_RELOC_LO16);
7261       if (used_at)
7262         break;
7263       return;
7264
7265     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7266       s = "slt";
7267       goto sgt;
7268     case M_SGTU:
7269       s = "sltu";
7270     sgt:
7271       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7272                    dreg, treg, sreg);
7273       return;
7274
7275     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7276       s = "slt";
7277       goto sgti;
7278     case M_SGTU_I:
7279       s = "sltu";
7280     sgti:
7281       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7282       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7283                    dreg, AT, sreg);
7284       break;
7285
7286     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7287       s = "slt";
7288       goto sle;
7289     case M_SLEU:
7290       s = "sltu";
7291     sle:
7292       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7293                    dreg, treg, sreg);
7294       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7295                    (int) BFD_RELOC_LO16);
7296       return;
7297
7298     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7299       s = "slt";
7300       goto slei;
7301     case M_SLEU_I:
7302       s = "sltu";
7303     slei:
7304       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7305       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7306                    dreg, AT, sreg);
7307       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7308                    (int) BFD_RELOC_LO16);
7309       break;
7310
7311     case M_SLT_I:
7312       if (imm_expr.X_op == O_constant
7313           && imm_expr.X_add_number >= -0x8000
7314           && imm_expr.X_add_number < 0x8000)
7315         {
7316           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7317                        dreg, sreg, (int) BFD_RELOC_LO16);
7318           return;
7319         }
7320       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7321       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7322                    dreg, sreg, AT);
7323       break;
7324
7325     case M_SLTU_I:
7326       if (imm_expr.X_op == O_constant
7327           && imm_expr.X_add_number >= -0x8000
7328           && imm_expr.X_add_number < 0x8000)
7329         {
7330           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7331                        dreg, sreg, (int) BFD_RELOC_LO16);
7332           return;
7333         }
7334       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7335       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7336                    "d,v,t", dreg, sreg, AT);
7337       break;
7338
7339     case M_SNE:
7340       if (sreg == 0)
7341         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7342                      "d,v,t", dreg, 0, treg);
7343       else if (treg == 0)
7344         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7345                      "d,v,t", dreg, 0, sreg);
7346       else
7347         {
7348           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7349                        "d,v,t", dreg, sreg, treg);
7350           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7351                        "d,v,t", dreg, 0, dreg);
7352         }
7353       return;
7354
7355     case M_SNE_I:
7356       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7357         {
7358           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7359                        "d,v,t", dreg, 0, sreg);
7360           return;
7361         }
7362       if (sreg == 0)
7363         {
7364           as_warn (_("Instruction %s: result is always true"),
7365                    ip->insn_mo->name);
7366           macro_build ((char *) NULL, &icnt, &expr1,
7367                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7368                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7369           return;
7370         }
7371       if (imm_expr.X_op == O_constant
7372           && imm_expr.X_add_number >= 0
7373           && imm_expr.X_add_number < 0x10000)
7374         {
7375           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7376                        dreg, sreg, (int) BFD_RELOC_LO16);
7377           used_at = 0;
7378         }
7379       else if (imm_expr.X_op == O_constant
7380                && imm_expr.X_add_number > -0x8000
7381                && imm_expr.X_add_number < 0)
7382         {
7383           imm_expr.X_add_number = -imm_expr.X_add_number;
7384           macro_build ((char *) NULL, &icnt, &imm_expr,
7385                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7386                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7387           used_at = 0;
7388         }
7389       else
7390         {
7391           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7392           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7393                        "d,v,t", dreg, sreg, AT);
7394           used_at = 1;
7395         }
7396       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7397                    "d,v,t", dreg, 0, dreg);
7398       if (used_at)
7399         break;
7400       return;
7401
7402     case M_DSUB_I:
7403       dbl = 1;
7404     case M_SUB_I:
7405       if (imm_expr.X_op == O_constant
7406           && imm_expr.X_add_number > -0x8000
7407           && imm_expr.X_add_number <= 0x8000)
7408         {
7409           imm_expr.X_add_number = -imm_expr.X_add_number;
7410           macro_build ((char *) NULL, &icnt, &imm_expr,
7411                        dbl ? "daddi" : "addi",
7412                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7413           return;
7414         }
7415       load_register (&icnt, AT, &imm_expr, dbl);
7416       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7417                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7418       break;
7419
7420     case M_DSUBU_I:
7421       dbl = 1;
7422     case M_SUBU_I:
7423       if (imm_expr.X_op == O_constant
7424           && imm_expr.X_add_number > -0x8000
7425           && imm_expr.X_add_number <= 0x8000)
7426         {
7427           imm_expr.X_add_number = -imm_expr.X_add_number;
7428           macro_build ((char *) NULL, &icnt, &imm_expr,
7429                        dbl ? "daddiu" : "addiu",
7430                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7431           return;
7432         }
7433       load_register (&icnt, AT, &imm_expr, dbl);
7434       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7435                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7436       break;
7437
7438     case M_TEQ_I:
7439       s = "teq";
7440       goto trap;
7441     case M_TGE_I:
7442       s = "tge";
7443       goto trap;
7444     case M_TGEU_I:
7445       s = "tgeu";
7446       goto trap;
7447     case M_TLT_I:
7448       s = "tlt";
7449       goto trap;
7450     case M_TLTU_I:
7451       s = "tltu";
7452       goto trap;
7453     case M_TNE_I:
7454       s = "tne";
7455     trap:
7456       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7457       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7458                    AT);
7459       break;
7460
7461     case M_TRUNCWS:
7462     case M_TRUNCWD:
7463       assert (mips_opts.isa == ISA_MIPS1);
7464       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7465       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7466
7467       /*
7468        * Is the double cfc1 instruction a bug in the mips assembler;
7469        * or is there a reason for it?
7470        */
7471       mips_emit_delays (TRUE);
7472       ++mips_opts.noreorder;
7473       mips_any_noreorder = 1;
7474       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7475                    treg, RA);
7476       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7477                    treg, RA);
7478       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7479       expr1.X_add_number = 3;
7480       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7481                    (int) BFD_RELOC_LO16);
7482       expr1.X_add_number = 2;
7483       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7484                      (int) BFD_RELOC_LO16);
7485       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7486                    AT, RA);
7487       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7488       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7489               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7490       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7491                    treg, RA);
7492       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7493       --mips_opts.noreorder;
7494       break;
7495
7496     case M_ULH:
7497       s = "lb";
7498       goto ulh;
7499     case M_ULHU:
7500       s = "lbu";
7501     ulh:
7502       if (offset_expr.X_add_number >= 0x7fff)
7503         as_bad (_("operand overflow"));
7504       /* avoid load delay */
7505       if (! target_big_endian)
7506         ++offset_expr.X_add_number;
7507       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7508                    (int) BFD_RELOC_LO16, breg);
7509       if (! target_big_endian)
7510         --offset_expr.X_add_number;
7511       else
7512         ++offset_expr.X_add_number;
7513       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7514                    (int) BFD_RELOC_LO16, breg);
7515       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7516                    treg, treg, 8);
7517       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7518                    treg, treg, AT);
7519       break;
7520
7521     case M_ULD:
7522       s = "ldl";
7523       s2 = "ldr";
7524       off = 7;
7525       goto ulw;
7526     case M_ULW:
7527       s = "lwl";
7528       s2 = "lwr";
7529       off = 3;
7530     ulw:
7531       if (offset_expr.X_add_number >= 0x8000 - off)
7532         as_bad (_("operand overflow"));
7533       if (! target_big_endian)
7534         offset_expr.X_add_number += off;
7535       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7536                    (int) BFD_RELOC_LO16, breg);
7537       if (! target_big_endian)
7538         offset_expr.X_add_number -= off;
7539       else
7540         offset_expr.X_add_number += off;
7541       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7542                    (int) BFD_RELOC_LO16, breg);
7543       return;
7544
7545     case M_ULD_A:
7546       s = "ldl";
7547       s2 = "ldr";
7548       off = 7;
7549       goto ulwa;
7550     case M_ULW_A:
7551       s = "lwl";
7552       s2 = "lwr";
7553       off = 3;
7554     ulwa:
7555       used_at = 1;
7556       load_address (&icnt, AT, &offset_expr, &used_at);
7557       if (breg != 0)
7558         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7559                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7560                      "d,v,t", AT, AT, breg);
7561       if (! target_big_endian)
7562         expr1.X_add_number = off;
7563       else
7564         expr1.X_add_number = 0;
7565       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7566                    (int) BFD_RELOC_LO16, AT);
7567       if (! target_big_endian)
7568         expr1.X_add_number = 0;
7569       else
7570         expr1.X_add_number = off;
7571       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7572                    (int) BFD_RELOC_LO16, AT);
7573       break;
7574
7575     case M_ULH_A:
7576     case M_ULHU_A:
7577       used_at = 1;
7578       load_address (&icnt, AT, &offset_expr, &used_at);
7579       if (breg != 0)
7580         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7581                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7582                      "d,v,t", AT, AT, breg);
7583       if (target_big_endian)
7584         expr1.X_add_number = 0;
7585       macro_build ((char *) NULL, &icnt, &expr1,
7586                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7587                    (int) BFD_RELOC_LO16, AT);
7588       if (target_big_endian)
7589         expr1.X_add_number = 1;
7590       else
7591         expr1.X_add_number = 0;
7592       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7593                    (int) BFD_RELOC_LO16, AT);
7594       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7595                    treg, treg, 8);
7596       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7597                    treg, treg, AT);
7598       break;
7599
7600     case M_USH:
7601       if (offset_expr.X_add_number >= 0x7fff)
7602         as_bad (_("operand overflow"));
7603       if (target_big_endian)
7604         ++offset_expr.X_add_number;
7605       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7606                    (int) BFD_RELOC_LO16, breg);
7607       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7608                    AT, treg, 8);
7609       if (target_big_endian)
7610         --offset_expr.X_add_number;
7611       else
7612         ++offset_expr.X_add_number;
7613       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7614                    (int) BFD_RELOC_LO16, breg);
7615       break;
7616
7617     case M_USD:
7618       s = "sdl";
7619       s2 = "sdr";
7620       off = 7;
7621       goto usw;
7622     case M_USW:
7623       s = "swl";
7624       s2 = "swr";
7625       off = 3;
7626     usw:
7627       if (offset_expr.X_add_number >= 0x8000 - off)
7628         as_bad (_("operand overflow"));
7629       if (! target_big_endian)
7630         offset_expr.X_add_number += off;
7631       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7632                    (int) BFD_RELOC_LO16, breg);
7633       if (! target_big_endian)
7634         offset_expr.X_add_number -= off;
7635       else
7636         offset_expr.X_add_number += off;
7637       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7638                    (int) BFD_RELOC_LO16, breg);
7639       return;
7640
7641     case M_USD_A:
7642       s = "sdl";
7643       s2 = "sdr";
7644       off = 7;
7645       goto uswa;
7646     case M_USW_A:
7647       s = "swl";
7648       s2 = "swr";
7649       off = 3;
7650     uswa:
7651       used_at = 1;
7652       load_address (&icnt, AT, &offset_expr, &used_at);
7653       if (breg != 0)
7654         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7655                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7656                      "d,v,t", AT, AT, breg);
7657       if (! target_big_endian)
7658         expr1.X_add_number = off;
7659       else
7660         expr1.X_add_number = 0;
7661       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7662                    (int) BFD_RELOC_LO16, AT);
7663       if (! target_big_endian)
7664         expr1.X_add_number = 0;
7665       else
7666         expr1.X_add_number = off;
7667       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7668                    (int) BFD_RELOC_LO16, AT);
7669       break;
7670
7671     case M_USH_A:
7672       used_at = 1;
7673       load_address (&icnt, AT, &offset_expr, &used_at);
7674       if (breg != 0)
7675         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7676                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7677                      "d,v,t", AT, AT, breg);
7678       if (! target_big_endian)
7679         expr1.X_add_number = 0;
7680       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7681                    (int) BFD_RELOC_LO16, AT);
7682       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7683                    treg, treg, 8);
7684       if (! target_big_endian)
7685         expr1.X_add_number = 1;
7686       else
7687         expr1.X_add_number = 0;
7688       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7689                    (int) BFD_RELOC_LO16, AT);
7690       if (! target_big_endian)
7691         expr1.X_add_number = 0;
7692       else
7693         expr1.X_add_number = 1;
7694       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7695                    (int) BFD_RELOC_LO16, AT);
7696       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7697                    treg, treg, 8);
7698       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7699                    treg, treg, AT);
7700       break;
7701
7702     default:
7703       /* FIXME: Check if this is one of the itbl macros, since they
7704          are added dynamically.  */
7705       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7706       break;
7707     }
7708   if (mips_opts.noat)
7709     as_warn (_("Macro used $at after \".set noat\""));
7710 }
7711
7712 /* Implement macros in mips16 mode.  */
7713
7714 static void
7715 mips16_macro (ip)
7716      struct mips_cl_insn *ip;
7717 {
7718   int mask;
7719   int xreg, yreg, zreg, tmp;
7720   int icnt;
7721   expressionS expr1;
7722   int dbl;
7723   const char *s, *s2, *s3;
7724
7725   mask = ip->insn_mo->mask;
7726
7727   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7728   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7729   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7730
7731   icnt = 0;
7732
7733   expr1.X_op = O_constant;
7734   expr1.X_op_symbol = NULL;
7735   expr1.X_add_symbol = NULL;
7736   expr1.X_add_number = 1;
7737
7738   dbl = 0;
7739
7740   switch (mask)
7741     {
7742     default:
7743       internalError ();
7744
7745     case M_DDIV_3:
7746       dbl = 1;
7747     case M_DIV_3:
7748       s = "mflo";
7749       goto do_div3;
7750     case M_DREM_3:
7751       dbl = 1;
7752     case M_REM_3:
7753       s = "mfhi";
7754     do_div3:
7755       mips_emit_delays (TRUE);
7756       ++mips_opts.noreorder;
7757       mips_any_noreorder = 1;
7758       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7759                    dbl ? "ddiv" : "div",
7760                    "0,x,y", xreg, yreg);
7761       expr1.X_add_number = 2;
7762       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7763       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7764                    7);
7765
7766       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7767          since that causes an overflow.  We should do that as well,
7768          but I don't see how to do the comparisons without a temporary
7769          register.  */
7770       --mips_opts.noreorder;
7771       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7772       break;
7773
7774     case M_DIVU_3:
7775       s = "divu";
7776       s2 = "mflo";
7777       goto do_divu3;
7778     case M_REMU_3:
7779       s = "divu";
7780       s2 = "mfhi";
7781       goto do_divu3;
7782     case M_DDIVU_3:
7783       s = "ddivu";
7784       s2 = "mflo";
7785       goto do_divu3;
7786     case M_DREMU_3:
7787       s = "ddivu";
7788       s2 = "mfhi";
7789     do_divu3:
7790       mips_emit_delays (TRUE);
7791       ++mips_opts.noreorder;
7792       mips_any_noreorder = 1;
7793       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7794                    xreg, yreg);
7795       expr1.X_add_number = 2;
7796       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7797       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7798                    "6", 7);
7799       --mips_opts.noreorder;
7800       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7801       break;
7802
7803     case M_DMUL:
7804       dbl = 1;
7805     case M_MUL:
7806       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7807                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7808       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7809                    zreg);
7810       return;
7811
7812     case M_DSUBU_I:
7813       dbl = 1;
7814       goto do_subu;
7815     case M_SUBU_I:
7816     do_subu:
7817       if (imm_expr.X_op != O_constant)
7818         as_bad (_("Unsupported large constant"));
7819       imm_expr.X_add_number = -imm_expr.X_add_number;
7820       macro_build ((char *) NULL, &icnt, &imm_expr,
7821                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7822       break;
7823
7824     case M_SUBU_I_2:
7825       if (imm_expr.X_op != O_constant)
7826         as_bad (_("Unsupported large constant"));
7827       imm_expr.X_add_number = -imm_expr.X_add_number;
7828       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7829                    "x,k", xreg);
7830       break;
7831
7832     case M_DSUBU_I_2:
7833       if (imm_expr.X_op != O_constant)
7834         as_bad (_("Unsupported large constant"));
7835       imm_expr.X_add_number = -imm_expr.X_add_number;
7836       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7837                    "y,j", yreg);
7838       break;
7839
7840     case M_BEQ:
7841       s = "cmp";
7842       s2 = "bteqz";
7843       goto do_branch;
7844     case M_BNE:
7845       s = "cmp";
7846       s2 = "btnez";
7847       goto do_branch;
7848     case M_BLT:
7849       s = "slt";
7850       s2 = "btnez";
7851       goto do_branch;
7852     case M_BLTU:
7853       s = "sltu";
7854       s2 = "btnez";
7855       goto do_branch;
7856     case M_BLE:
7857       s = "slt";
7858       s2 = "bteqz";
7859       goto do_reverse_branch;
7860     case M_BLEU:
7861       s = "sltu";
7862       s2 = "bteqz";
7863       goto do_reverse_branch;
7864     case M_BGE:
7865       s = "slt";
7866       s2 = "bteqz";
7867       goto do_branch;
7868     case M_BGEU:
7869       s = "sltu";
7870       s2 = "bteqz";
7871       goto do_branch;
7872     case M_BGT:
7873       s = "slt";
7874       s2 = "btnez";
7875       goto do_reverse_branch;
7876     case M_BGTU:
7877       s = "sltu";
7878       s2 = "btnez";
7879
7880     do_reverse_branch:
7881       tmp = xreg;
7882       xreg = yreg;
7883       yreg = tmp;
7884
7885     do_branch:
7886       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7887                    xreg, yreg);
7888       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7889       break;
7890
7891     case M_BEQ_I:
7892       s = "cmpi";
7893       s2 = "bteqz";
7894       s3 = "x,U";
7895       goto do_branch_i;
7896     case M_BNE_I:
7897       s = "cmpi";
7898       s2 = "btnez";
7899       s3 = "x,U";
7900       goto do_branch_i;
7901     case M_BLT_I:
7902       s = "slti";
7903       s2 = "btnez";
7904       s3 = "x,8";
7905       goto do_branch_i;
7906     case M_BLTU_I:
7907       s = "sltiu";
7908       s2 = "btnez";
7909       s3 = "x,8";
7910       goto do_branch_i;
7911     case M_BLE_I:
7912       s = "slti";
7913       s2 = "btnez";
7914       s3 = "x,8";
7915       goto do_addone_branch_i;
7916     case M_BLEU_I:
7917       s = "sltiu";
7918       s2 = "btnez";
7919       s3 = "x,8";
7920       goto do_addone_branch_i;
7921     case M_BGE_I:
7922       s = "slti";
7923       s2 = "bteqz";
7924       s3 = "x,8";
7925       goto do_branch_i;
7926     case M_BGEU_I:
7927       s = "sltiu";
7928       s2 = "bteqz";
7929       s3 = "x,8";
7930       goto do_branch_i;
7931     case M_BGT_I:
7932       s = "slti";
7933       s2 = "bteqz";
7934       s3 = "x,8";
7935       goto do_addone_branch_i;
7936     case M_BGTU_I:
7937       s = "sltiu";
7938       s2 = "bteqz";
7939       s3 = "x,8";
7940
7941     do_addone_branch_i:
7942       if (imm_expr.X_op != O_constant)
7943         as_bad (_("Unsupported large constant"));
7944       ++imm_expr.X_add_number;
7945
7946     do_branch_i:
7947       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7948       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7949       break;
7950
7951     case M_ABS:
7952       expr1.X_add_number = 0;
7953       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7954       if (xreg != yreg)
7955         move_register (&icnt, xreg, yreg);
7956       expr1.X_add_number = 2;
7957       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7958       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7959                    "neg", "x,w", xreg, xreg);
7960     }
7961 }
7962
7963 /* For consistency checking, verify that all bits are specified either
7964    by the match/mask part of the instruction definition, or by the
7965    operand list.  */
7966 static int
7967 validate_mips_insn (opc)
7968      const struct mips_opcode *opc;
7969 {
7970   const char *p = opc->args;
7971   char c;
7972   unsigned long used_bits = opc->mask;
7973
7974   if ((used_bits & opc->match) != opc->match)
7975     {
7976       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7977               opc->name, opc->args);
7978       return 0;
7979     }
7980 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
7981   while (*p)
7982     switch (c = *p++)
7983       {
7984       case ',': break;
7985       case '(': break;
7986       case ')': break;
7987       case '+':
7988         switch (c = *p++)
7989           {
7990           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
7991           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
7992           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
7993           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
7994                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
7995           default:
7996             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7997                     c, opc->name, opc->args);
7998             return 0;
7999           }
8000         break;
8001       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8002       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8003       case 'A': break;
8004       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8005       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8006       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8007       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8008       case 'F': break;
8009       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8010       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8011       case 'I': break;
8012       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8013       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8014       case 'L': break;
8015       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8016       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8017       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8018       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8019                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8020       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8021       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8022       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8023       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8024       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8025       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8026       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8027       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8028       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8029       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8030       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8031       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8032       case 'f': break;
8033       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8034       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8035       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8036       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8037       case 'l': break;
8038       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8039       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8040       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8041       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8042       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8043       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8044       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8045       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8046       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8047       case 'x': break;
8048       case 'z': break;
8049       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8050       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8051                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8052       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8053       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8054       case '[': break;
8055       case ']': break;
8056       default:
8057         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8058                 c, opc->name, opc->args);
8059         return 0;
8060       }
8061 #undef USE_BITS
8062   if (used_bits != 0xffffffff)
8063     {
8064       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8065               ~used_bits & 0xffffffff, opc->name, opc->args);
8066       return 0;
8067     }
8068   return 1;
8069 }
8070
8071 /* This routine assembles an instruction into its binary format.  As a
8072    side effect, it sets one of the global variables imm_reloc or
8073    offset_reloc to the type of relocation to do if one of the operands
8074    is an address expression.  */
8075
8076 static void
8077 mips_ip (str, ip)
8078      char *str;
8079      struct mips_cl_insn *ip;
8080 {
8081   char *s;
8082   const char *args;
8083   char c = 0;
8084   struct mips_opcode *insn;
8085   char *argsStart;
8086   unsigned int regno;
8087   unsigned int lastregno = 0;
8088   unsigned int lastpos = 0;
8089   unsigned int limlo, limhi;
8090   char *s_reset;
8091   char save_c = 0;
8092
8093   insn_error = NULL;
8094
8095   /* If the instruction contains a '.', we first try to match an instruction
8096      including the '.'.  Then we try again without the '.'.  */
8097   insn = NULL;
8098   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8099     continue;
8100
8101   /* If we stopped on whitespace, then replace the whitespace with null for
8102      the call to hash_find.  Save the character we replaced just in case we
8103      have to re-parse the instruction.  */
8104   if (ISSPACE (*s))
8105     {
8106       save_c = *s;
8107       *s++ = '\0';
8108     }
8109
8110   insn = (struct mips_opcode *) hash_find (op_hash, str);
8111
8112   /* If we didn't find the instruction in the opcode table, try again, but
8113      this time with just the instruction up to, but not including the
8114      first '.'.  */
8115   if (insn == NULL)
8116     {
8117       /* Restore the character we overwrite above (if any).  */
8118       if (save_c)
8119         *(--s) = save_c;
8120
8121       /* Scan up to the first '.' or whitespace.  */
8122       for (s = str;
8123            *s != '\0' && *s != '.' && !ISSPACE (*s);
8124            ++s)
8125         continue;
8126
8127       /* If we did not find a '.', then we can quit now.  */
8128       if (*s != '.')
8129         {
8130           insn_error = "unrecognized opcode";
8131           return;
8132         }
8133
8134       /* Lookup the instruction in the hash table.  */
8135       *s++ = '\0';
8136       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8137         {
8138           insn_error = "unrecognized opcode";
8139           return;
8140         }
8141     }
8142
8143   argsStart = s;
8144   for (;;)
8145     {
8146       bfd_boolean ok;
8147
8148       assert (strcmp (insn->name, str) == 0);
8149
8150       if (OPCODE_IS_MEMBER (insn,
8151                             (mips_opts.isa
8152                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8153                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8154                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8155                             mips_arch))
8156         ok = TRUE;
8157       else
8158         ok = FALSE;
8159
8160       if (insn->pinfo != INSN_MACRO)
8161         {
8162           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8163             ok = FALSE;
8164         }
8165
8166       if (! ok)
8167         {
8168           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8169               && strcmp (insn->name, insn[1].name) == 0)
8170             {
8171               ++insn;
8172               continue;
8173             }
8174           else
8175             {
8176               if (!insn_error)
8177                 {
8178                   static char buf[100];
8179                   if (mips_arch_info->is_isa)
8180                     sprintf (buf,
8181                              _("opcode not supported at this ISA level (%s)"),
8182                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8183                   else
8184                     sprintf (buf,
8185                              _("opcode not supported on this processor: %s (%s)"),
8186                              mips_arch_info->name,
8187                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8188                   insn_error = buf;
8189                 }
8190               if (save_c)
8191                 *(--s) = save_c;
8192               return;
8193             }
8194         }
8195
8196       ip->insn_mo = insn;
8197       ip->insn_opcode = insn->match;
8198       insn_error = NULL;
8199       for (args = insn->args;; ++args)
8200         {
8201           int is_mdmx;
8202
8203           s += strspn (s, " \t");
8204           is_mdmx = 0;
8205           switch (*args)
8206             {
8207             case '\0':          /* end of args */
8208               if (*s == '\0')
8209                 return;
8210               break;
8211
8212             case ',':
8213               if (*s++ == *args)
8214                 continue;
8215               s--;
8216               switch (*++args)
8217                 {
8218                 case 'r':
8219                 case 'v':
8220                   ip->insn_opcode |= lastregno << OP_SH_RS;
8221                   continue;
8222
8223                 case 'w':
8224                   ip->insn_opcode |= lastregno << OP_SH_RT;
8225                   continue;
8226
8227                 case 'W':
8228                   ip->insn_opcode |= lastregno << OP_SH_FT;
8229                   continue;
8230
8231                 case 'V':
8232                   ip->insn_opcode |= lastregno << OP_SH_FS;
8233                   continue;
8234                 }
8235               break;
8236
8237             case '(':
8238               /* Handle optional base register.
8239                  Either the base register is omitted or
8240                  we must have a left paren.  */
8241               /* This is dependent on the next operand specifier
8242                  is a base register specification.  */
8243               assert (args[1] == 'b' || args[1] == '5'
8244                       || args[1] == '-' || args[1] == '4');
8245               if (*s == '\0')
8246                 return;
8247
8248             case ')':           /* these must match exactly */
8249             case '[':
8250             case ']':
8251               if (*s++ == *args)
8252                 continue;
8253               break;
8254
8255             case '+':           /* Opcode extension character.  */
8256               switch (*++args)
8257                 {
8258                 case 'A':               /* ins/ext position, becomes LSB.  */
8259                   limlo = 0;
8260                   limhi = 31;
8261                   my_getExpression (&imm_expr, s);
8262                   check_absolute_expr (ip, &imm_expr);
8263                   if ((unsigned long) imm_expr.X_add_number < limlo
8264                       || (unsigned long) imm_expr.X_add_number > limhi)
8265                     {
8266                       as_bad (_("Improper position (%lu)"),
8267                               (unsigned long) imm_expr.X_add_number);
8268                       imm_expr.X_add_number = limlo;
8269                     }
8270                   lastpos = imm_expr.X_add_number;
8271                   ip->insn_opcode |= (imm_expr.X_add_number
8272                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8273                   imm_expr.X_op = O_absent;
8274                   s = expr_end;
8275                   continue;
8276
8277                 case 'B':               /* ins size, becomes MSB.  */
8278                   limlo = 1;
8279                   limhi = 32;
8280                   my_getExpression (&imm_expr, s);
8281                   check_absolute_expr (ip, &imm_expr);
8282                   /* Check for negative input so that small negative numbers
8283                      will not succeed incorrectly.  The checks against
8284                      (pos+size) transitively check "size" itself,
8285                      assuming that "pos" is reasonable.  */
8286                   if ((long) imm_expr.X_add_number < 0
8287                       || ((unsigned long) imm_expr.X_add_number
8288                           + lastpos) < limlo
8289                       || ((unsigned long) imm_expr.X_add_number
8290                           + lastpos) > limhi)
8291                     {
8292                       as_bad (_("Improper insert size (%lu, position %lu)"),
8293                               (unsigned long) imm_expr.X_add_number,
8294                               (unsigned long) lastpos);
8295                       imm_expr.X_add_number = limlo - lastpos;
8296                     }
8297                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8298                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8299                   imm_expr.X_op = O_absent;
8300                   s = expr_end;
8301                   continue;
8302
8303                 case 'C':               /* ext size, becomes MSBD.  */
8304                   limlo = 1;
8305                   limhi = 32;
8306                   my_getExpression (&imm_expr, s);
8307                   check_absolute_expr (ip, &imm_expr);
8308                   /* Check for negative input so that small negative numbers
8309                      will not succeed incorrectly.  The checks against
8310                      (pos+size) transitively check "size" itself,
8311                      assuming that "pos" is reasonable.  */
8312                   if ((long) imm_expr.X_add_number < 0
8313                       || ((unsigned long) imm_expr.X_add_number
8314                           + lastpos) < limlo
8315                       || ((unsigned long) imm_expr.X_add_number
8316                           + lastpos) > limhi)
8317                     {
8318                       as_bad (_("Improper extract size (%lu, position %lu)"),
8319                               (unsigned long) imm_expr.X_add_number,
8320                               (unsigned long) lastpos);
8321                       imm_expr.X_add_number = limlo - lastpos;
8322                     }
8323                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8324                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8325                   imm_expr.X_op = O_absent;
8326                   s = expr_end;
8327                   continue;
8328
8329                 case 'D':
8330                   /* +D is for disassembly only; never match.  */
8331                   break;
8332
8333                 default:
8334                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8335                     *args, insn->name, insn->args);
8336                   /* Further processing is fruitless.  */
8337                   return;
8338                 }
8339               break;
8340
8341             case '<':           /* must be at least one digit */
8342               /*
8343                * According to the manual, if the shift amount is greater
8344                * than 31 or less than 0, then the shift amount should be
8345                * mod 32.  In reality the mips assembler issues an error.
8346                * We issue a warning and mask out all but the low 5 bits.
8347                */
8348               my_getExpression (&imm_expr, s);
8349               check_absolute_expr (ip, &imm_expr);
8350               if ((unsigned long) imm_expr.X_add_number > 31)
8351                 {
8352                   as_warn (_("Improper shift amount (%lu)"),
8353                            (unsigned long) imm_expr.X_add_number);
8354                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8355                 }
8356               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8357               imm_expr.X_op = O_absent;
8358               s = expr_end;
8359               continue;
8360
8361             case '>':           /* shift amount minus 32 */
8362               my_getExpression (&imm_expr, s);
8363               check_absolute_expr (ip, &imm_expr);
8364               if ((unsigned long) imm_expr.X_add_number < 32
8365                   || (unsigned long) imm_expr.X_add_number > 63)
8366                 break;
8367               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8368               imm_expr.X_op = O_absent;
8369               s = expr_end;
8370               continue;
8371
8372             case 'k':           /* cache code */
8373             case 'h':           /* prefx code */
8374               my_getExpression (&imm_expr, s);
8375               check_absolute_expr (ip, &imm_expr);
8376               if ((unsigned long) imm_expr.X_add_number > 31)
8377                 {
8378                   as_warn (_("Invalid value for `%s' (%lu)"),
8379                            ip->insn_mo->name,
8380                            (unsigned long) imm_expr.X_add_number);
8381                   imm_expr.X_add_number &= 0x1f;
8382                 }
8383               if (*args == 'k')
8384                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8385               else
8386                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8387               imm_expr.X_op = O_absent;
8388               s = expr_end;
8389               continue;
8390
8391             case 'c':           /* break code */
8392               my_getExpression (&imm_expr, s);
8393               check_absolute_expr (ip, &imm_expr);
8394               if ((unsigned long) imm_expr.X_add_number > 1023)
8395                 {
8396                   as_warn (_("Illegal break code (%lu)"),
8397                            (unsigned long) imm_expr.X_add_number);
8398                   imm_expr.X_add_number &= OP_MASK_CODE;
8399                 }
8400               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8401               imm_expr.X_op = O_absent;
8402               s = expr_end;
8403               continue;
8404
8405             case 'q':           /* lower break code */
8406               my_getExpression (&imm_expr, s);
8407               check_absolute_expr (ip, &imm_expr);
8408               if ((unsigned long) imm_expr.X_add_number > 1023)
8409                 {
8410                   as_warn (_("Illegal lower break code (%lu)"),
8411                            (unsigned long) imm_expr.X_add_number);
8412                   imm_expr.X_add_number &= OP_MASK_CODE2;
8413                 }
8414               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8415               imm_expr.X_op = O_absent;
8416               s = expr_end;
8417               continue;
8418
8419             case 'B':           /* 20-bit syscall/break code.  */
8420               my_getExpression (&imm_expr, s);
8421               check_absolute_expr (ip, &imm_expr);
8422               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8423                 as_warn (_("Illegal 20-bit code (%lu)"),
8424                          (unsigned long) imm_expr.X_add_number);
8425               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8426               imm_expr.X_op = O_absent;
8427               s = expr_end;
8428               continue;
8429
8430             case 'C':           /* Coprocessor code */
8431               my_getExpression (&imm_expr, s);
8432               check_absolute_expr (ip, &imm_expr);
8433               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8434                 {
8435                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8436                            (unsigned long) imm_expr.X_add_number);
8437                   imm_expr.X_add_number &= ((1 << 25) - 1);
8438                 }
8439               ip->insn_opcode |= imm_expr.X_add_number;
8440               imm_expr.X_op = O_absent;
8441               s = expr_end;
8442               continue;
8443
8444             case 'J':           /* 19-bit wait code.  */
8445               my_getExpression (&imm_expr, s);
8446               check_absolute_expr (ip, &imm_expr);
8447               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8448                 as_warn (_("Illegal 19-bit code (%lu)"),
8449                          (unsigned long) imm_expr.X_add_number);
8450               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8451               imm_expr.X_op = O_absent;
8452               s = expr_end;
8453               continue;
8454
8455             case 'P':           /* Performance register */
8456               my_getExpression (&imm_expr, s);
8457               check_absolute_expr (ip, &imm_expr);
8458               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8459                 {
8460                   as_warn (_("Invalid performance register (%lu)"),
8461                            (unsigned long) imm_expr.X_add_number);
8462                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8463                 }
8464               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8465               imm_expr.X_op = O_absent;
8466               s = expr_end;
8467               continue;
8468
8469             case 'b':           /* base register */
8470             case 'd':           /* destination register */
8471             case 's':           /* source register */
8472             case 't':           /* target register */
8473             case 'r':           /* both target and source */
8474             case 'v':           /* both dest and source */
8475             case 'w':           /* both dest and target */
8476             case 'E':           /* coprocessor target register */
8477             case 'G':           /* coprocessor destination register */
8478             case 'K':           /* 'rdhwr' destination register */
8479             case 'x':           /* ignore register name */
8480             case 'z':           /* must be zero register */
8481             case 'U':           /* destination register (clo/clz).  */
8482               s_reset = s;
8483               if (s[0] == '$')
8484                 {
8485
8486                   if (ISDIGIT (s[1]))
8487                     {
8488                       ++s;
8489                       regno = 0;
8490                       do
8491                         {
8492                           regno *= 10;
8493                           regno += *s - '0';
8494                           ++s;
8495                         }
8496                       while (ISDIGIT (*s));
8497                       if (regno > 31)
8498                         as_bad (_("Invalid register number (%d)"), regno);
8499                     }
8500                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8501                     goto notreg;
8502                   else
8503                     {
8504                       if (s[1] == 'r' && s[2] == 'a')
8505                         {
8506                           s += 3;
8507                           regno = RA;
8508                         }
8509                       else if (s[1] == 'f' && s[2] == 'p')
8510                         {
8511                           s += 3;
8512                           regno = FP;
8513                         }
8514                       else if (s[1] == 's' && s[2] == 'p')
8515                         {
8516                           s += 3;
8517                           regno = SP;
8518                         }
8519                       else if (s[1] == 'g' && s[2] == 'p')
8520                         {
8521                           s += 3;
8522                           regno = GP;
8523                         }
8524                       else if (s[1] == 'a' && s[2] == 't')
8525                         {
8526                           s += 3;
8527                           regno = AT;
8528                         }
8529                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8530                         {
8531                           s += 4;
8532                           regno = KT0;
8533                         }
8534                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8535                         {
8536                           s += 4;
8537                           regno = KT1;
8538                         }
8539                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8540                         {
8541                           s += 5;
8542                           regno = ZERO;
8543                         }
8544                       else if (itbl_have_entries)
8545                         {
8546                           char *p, *n;
8547                           unsigned long r;
8548
8549                           p = s + 1;    /* advance past '$' */
8550                           n = itbl_get_field (&p);  /* n is name */
8551
8552                           /* See if this is a register defined in an
8553                              itbl entry.  */
8554                           if (itbl_get_reg_val (n, &r))
8555                             {
8556                               /* Get_field advances to the start of
8557                                  the next field, so we need to back
8558                                  rack to the end of the last field.  */
8559                               if (p)
8560                                 s = p - 1;
8561                               else
8562                                 s = strchr (s, '\0');
8563                               regno = r;
8564                             }
8565                           else
8566                             goto notreg;
8567                         }
8568                       else
8569                         goto notreg;
8570                     }
8571                   if (regno == AT
8572                       && ! mips_opts.noat
8573                       && *args != 'E'
8574                       && *args != 'G'
8575                       && *args != 'K')
8576                     as_warn (_("Used $at without \".set noat\""));
8577                   c = *args;
8578                   if (*s == ' ')
8579                     ++s;
8580                   if (args[1] != *s)
8581                     {
8582                       if (c == 'r' || c == 'v' || c == 'w')
8583                         {
8584                           regno = lastregno;
8585                           s = s_reset;
8586                           ++args;
8587                         }
8588                     }
8589                   /* 'z' only matches $0.  */
8590                   if (c == 'z' && regno != 0)
8591                     break;
8592
8593         /* Now that we have assembled one operand, we use the args string
8594          * to figure out where it goes in the instruction.  */
8595                   switch (c)
8596                     {
8597                     case 'r':
8598                     case 's':
8599                     case 'v':
8600                     case 'b':
8601                       ip->insn_opcode |= regno << OP_SH_RS;
8602                       break;
8603                     case 'd':
8604                     case 'G':
8605                     case 'K':
8606                       ip->insn_opcode |= regno << OP_SH_RD;
8607                       break;
8608                     case 'U':
8609                       ip->insn_opcode |= regno << OP_SH_RD;
8610                       ip->insn_opcode |= regno << OP_SH_RT;
8611                       break;
8612                     case 'w':
8613                     case 't':
8614                     case 'E':
8615                       ip->insn_opcode |= regno << OP_SH_RT;
8616                       break;
8617                     case 'x':
8618                       /* This case exists because on the r3000 trunc
8619                          expands into a macro which requires a gp
8620                          register.  On the r6000 or r4000 it is
8621                          assembled into a single instruction which
8622                          ignores the register.  Thus the insn version
8623                          is MIPS_ISA2 and uses 'x', and the macro
8624                          version is MIPS_ISA1 and uses 't'.  */
8625                       break;
8626                     case 'z':
8627                       /* This case is for the div instruction, which
8628                          acts differently if the destination argument
8629                          is $0.  This only matches $0, and is checked
8630                          outside the switch.  */
8631                       break;
8632                     case 'D':
8633                       /* Itbl operand; not yet implemented. FIXME ?? */
8634                       break;
8635                       /* What about all other operands like 'i', which
8636                          can be specified in the opcode table? */
8637                     }
8638                   lastregno = regno;
8639                   continue;
8640                 }
8641             notreg:
8642               switch (*args++)
8643                 {
8644                 case 'r':
8645                 case 'v':
8646                   ip->insn_opcode |= lastregno << OP_SH_RS;
8647                   continue;
8648                 case 'w':
8649                   ip->insn_opcode |= lastregno << OP_SH_RT;
8650                   continue;
8651                 }
8652               break;
8653
8654             case 'O':           /* MDMX alignment immediate constant.  */
8655               my_getExpression (&imm_expr, s);
8656               check_absolute_expr (ip, &imm_expr);
8657               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8658                 {
8659                   as_warn ("Improper align amount (%ld), using low bits",
8660                            (long) imm_expr.X_add_number);
8661                   imm_expr.X_add_number &= OP_MASK_ALN;
8662                 }
8663               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8664               imm_expr.X_op = O_absent;
8665               s = expr_end;
8666               continue;
8667
8668             case 'Q':           /* MDMX vector, element sel, or const.  */
8669               if (s[0] != '$')
8670                 {
8671                   /* MDMX Immediate.  */
8672                   my_getExpression (&imm_expr, s);
8673                   check_absolute_expr (ip, &imm_expr);
8674                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8675                     {
8676                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8677                                (long) imm_expr.X_add_number);
8678                       imm_expr.X_add_number &= OP_MASK_FT;
8679                     }
8680                   imm_expr.X_add_number &= OP_MASK_FT;
8681                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8682                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8683                   else
8684                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8685                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8686                   imm_expr.X_op = O_absent;
8687                   s = expr_end;
8688                   continue;
8689                 }
8690               /* Not MDMX Immediate.  Fall through.  */
8691             case 'X':           /* MDMX destination register.  */
8692             case 'Y':           /* MDMX source register.  */
8693             case 'Z':           /* MDMX target register.  */
8694               is_mdmx = 1;
8695             case 'D':           /* floating point destination register */
8696             case 'S':           /* floating point source register */
8697             case 'T':           /* floating point target register */
8698             case 'R':           /* floating point source register */
8699             case 'V':
8700             case 'W':
8701               s_reset = s;
8702               /* Accept $fN for FP and MDMX register numbers, and in
8703                  addition accept $vN for MDMX register numbers.  */
8704               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8705                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8706                       && ISDIGIT (s[2])))
8707                 {
8708                   s += 2;
8709                   regno = 0;
8710                   do
8711                     {
8712                       regno *= 10;
8713                       regno += *s - '0';
8714                       ++s;
8715                     }
8716                   while (ISDIGIT (*s));
8717
8718                   if (regno > 31)
8719                     as_bad (_("Invalid float register number (%d)"), regno);
8720
8721                   if ((regno & 1) != 0
8722                       && HAVE_32BIT_FPRS
8723                       && ! (strcmp (str, "mtc1") == 0
8724                             || strcmp (str, "mfc1") == 0
8725                             || strcmp (str, "lwc1") == 0
8726                             || strcmp (str, "swc1") == 0
8727                             || strcmp (str, "l.s") == 0
8728                             || strcmp (str, "s.s") == 0))
8729                     as_warn (_("Float register should be even, was %d"),
8730                              regno);
8731
8732                   c = *args;
8733                   if (*s == ' ')
8734                     ++s;
8735                   if (args[1] != *s)
8736                     {
8737                       if (c == 'V' || c == 'W')
8738                         {
8739                           regno = lastregno;
8740                           s = s_reset;
8741                           ++args;
8742                         }
8743                     }
8744                   switch (c)
8745                     {
8746                     case 'D':
8747                     case 'X':
8748                       ip->insn_opcode |= regno << OP_SH_FD;
8749                       break;
8750                     case 'V':
8751                     case 'S':
8752                     case 'Y':
8753                       ip->insn_opcode |= regno << OP_SH_FS;
8754                       break;
8755                     case 'Q':
8756                       /* This is like 'Z', but also needs to fix the MDMX
8757                          vector/scalar select bits.  Note that the
8758                          scalar immediate case is handled above.  */
8759                       if (*s == '[')
8760                         {
8761                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8762                           int max_el = (is_qh ? 3 : 7);
8763                           s++;
8764                           my_getExpression(&imm_expr, s);
8765                           check_absolute_expr (ip, &imm_expr);
8766                           s = expr_end;
8767                           if (imm_expr.X_add_number > max_el)
8768                             as_bad(_("Bad element selector %ld"),
8769                                    (long) imm_expr.X_add_number);
8770                           imm_expr.X_add_number &= max_el;
8771                           ip->insn_opcode |= (imm_expr.X_add_number
8772                                               << (OP_SH_VSEL +
8773                                                   (is_qh ? 2 : 1)));
8774                           if (*s != ']')
8775                             as_warn(_("Expecting ']' found '%s'"), s);
8776                           else
8777                             s++;
8778                         }
8779                       else
8780                         {
8781                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8782                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8783                                                 << OP_SH_VSEL);
8784                           else
8785                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8786                                                 OP_SH_VSEL);
8787                         }
8788                       /* Fall through */
8789                     case 'W':
8790                     case 'T':
8791                     case 'Z':
8792                       ip->insn_opcode |= regno << OP_SH_FT;
8793                       break;
8794                     case 'R':
8795                       ip->insn_opcode |= regno << OP_SH_FR;
8796                       break;
8797                     }
8798                   lastregno = regno;
8799                   continue;
8800                 }
8801
8802               switch (*args++)
8803                 {
8804                 case 'V':
8805                   ip->insn_opcode |= lastregno << OP_SH_FS;
8806                   continue;
8807                 case 'W':
8808                   ip->insn_opcode |= lastregno << OP_SH_FT;
8809                   continue;
8810                 }
8811               break;
8812
8813             case 'I':
8814               my_getExpression (&imm_expr, s);
8815               if (imm_expr.X_op != O_big
8816                   && imm_expr.X_op != O_constant)
8817                 insn_error = _("absolute expression required");
8818               s = expr_end;
8819               continue;
8820
8821             case 'A':
8822               my_getExpression (&offset_expr, s);
8823               *imm_reloc = BFD_RELOC_32;
8824               s = expr_end;
8825               continue;
8826
8827             case 'F':
8828             case 'L':
8829             case 'f':
8830             case 'l':
8831               {
8832                 int f64;
8833                 int using_gprs;
8834                 char *save_in;
8835                 char *err;
8836                 unsigned char temp[8];
8837                 int len;
8838                 unsigned int length;
8839                 segT seg;
8840                 subsegT subseg;
8841                 char *p;
8842
8843                 /* These only appear as the last operand in an
8844                    instruction, and every instruction that accepts
8845                    them in any variant accepts them in all variants.
8846                    This means we don't have to worry about backing out
8847                    any changes if the instruction does not match.
8848
8849                    The difference between them is the size of the
8850                    floating point constant and where it goes.  For 'F'
8851                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8852                    is 32 bits.  Where the constant is placed is based
8853                    on how the MIPS assembler does things:
8854                     F -- .rdata
8855                     L -- .lit8
8856                     f -- immediate value
8857                     l -- .lit4
8858
8859                     The .lit4 and .lit8 sections are only used if
8860                     permitted by the -G argument.
8861
8862                     When generating embedded PIC code, we use the
8863                     .lit8 section but not the .lit4 section (we can do
8864                     .lit4 inline easily; we need to put .lit8
8865                     somewhere in the data segment, and using .lit8
8866                     permits the linker to eventually combine identical
8867                     .lit8 entries).
8868
8869                     The code below needs to know whether the target register
8870                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8871                     'F' are used with GPR-based instructions and 'l' and
8872                     'L' are used with FPR-based instructions.  */
8873
8874                 f64 = *args == 'F' || *args == 'L';
8875                 using_gprs = *args == 'F' || *args == 'f';
8876
8877                 save_in = input_line_pointer;
8878                 input_line_pointer = s;
8879                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8880                 length = len;
8881                 s = input_line_pointer;
8882                 input_line_pointer = save_in;
8883                 if (err != NULL && *err != '\0')
8884                   {
8885                     as_bad (_("Bad floating point constant: %s"), err);
8886                     memset (temp, '\0', sizeof temp);
8887                     length = f64 ? 8 : 4;
8888                   }
8889
8890                 assert (length == (unsigned) (f64 ? 8 : 4));
8891
8892                 if (*args == 'f'
8893                     || (*args == 'l'
8894                         && (! USE_GLOBAL_POINTER_OPT
8895                             || mips_pic == EMBEDDED_PIC
8896                             || g_switch_value < 4
8897                             || (temp[0] == 0 && temp[1] == 0)
8898                             || (temp[2] == 0 && temp[3] == 0))))
8899                   {
8900                     imm_expr.X_op = O_constant;
8901                     if (! target_big_endian)
8902                       imm_expr.X_add_number = bfd_getl32 (temp);
8903                     else
8904                       imm_expr.X_add_number = bfd_getb32 (temp);
8905                   }
8906                 else if (length > 4
8907                          && ! mips_disable_float_construction
8908                          /* Constants can only be constructed in GPRs and
8909                             copied to FPRs if the GPRs are at least as wide
8910                             as the FPRs.  Force the constant into memory if
8911                             we are using 64-bit FPRs but the GPRs are only
8912                             32 bits wide.  */
8913                          && (using_gprs
8914                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8915                          && ((temp[0] == 0 && temp[1] == 0)
8916                              || (temp[2] == 0 && temp[3] == 0))
8917                          && ((temp[4] == 0 && temp[5] == 0)
8918                              || (temp[6] == 0 && temp[7] == 0)))
8919                   {
8920                     /* The value is simple enough to load with a couple of
8921                        instructions.  If using 32-bit registers, set
8922                        imm_expr to the high order 32 bits and offset_expr to
8923                        the low order 32 bits.  Otherwise, set imm_expr to
8924                        the entire 64 bit constant.  */
8925                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8926                       {
8927                         imm_expr.X_op = O_constant;
8928                         offset_expr.X_op = O_constant;
8929                         if (! target_big_endian)
8930                           {
8931                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8932                             offset_expr.X_add_number = bfd_getl32 (temp);
8933                           }
8934                         else
8935                           {
8936                             imm_expr.X_add_number = bfd_getb32 (temp);
8937                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
8938                           }
8939                         if (offset_expr.X_add_number == 0)
8940                           offset_expr.X_op = O_absent;
8941                       }
8942                     else if (sizeof (imm_expr.X_add_number) > 4)
8943                       {
8944                         imm_expr.X_op = O_constant;
8945                         if (! target_big_endian)
8946                           imm_expr.X_add_number = bfd_getl64 (temp);
8947                         else
8948                           imm_expr.X_add_number = bfd_getb64 (temp);
8949                       }
8950                     else
8951                       {
8952                         imm_expr.X_op = O_big;
8953                         imm_expr.X_add_number = 4;
8954                         if (! target_big_endian)
8955                           {
8956                             generic_bignum[0] = bfd_getl16 (temp);
8957                             generic_bignum[1] = bfd_getl16 (temp + 2);
8958                             generic_bignum[2] = bfd_getl16 (temp + 4);
8959                             generic_bignum[3] = bfd_getl16 (temp + 6);
8960                           }
8961                         else
8962                           {
8963                             generic_bignum[0] = bfd_getb16 (temp + 6);
8964                             generic_bignum[1] = bfd_getb16 (temp + 4);
8965                             generic_bignum[2] = bfd_getb16 (temp + 2);
8966                             generic_bignum[3] = bfd_getb16 (temp);
8967                           }
8968                       }
8969                   }
8970                 else
8971                   {
8972                     const char *newname;
8973                     segT new_seg;
8974
8975                     /* Switch to the right section.  */
8976                     seg = now_seg;
8977                     subseg = now_subseg;
8978                     switch (*args)
8979                       {
8980                       default: /* unused default case avoids warnings.  */
8981                       case 'L':
8982                         newname = RDATA_SECTION_NAME;
8983                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8984                             || mips_pic == EMBEDDED_PIC)
8985                           newname = ".lit8";
8986                         break;
8987                       case 'F':
8988                         if (mips_pic == EMBEDDED_PIC)
8989                           newname = ".lit8";
8990                         else
8991                           newname = RDATA_SECTION_NAME;
8992                         break;
8993                       case 'l':
8994                         assert (!USE_GLOBAL_POINTER_OPT
8995                                 || g_switch_value >= 4);
8996                         newname = ".lit4";
8997                         break;
8998                       }
8999                     new_seg = subseg_new (newname, (subsegT) 0);
9000                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9001                       bfd_set_section_flags (stdoutput, new_seg,
9002                                              (SEC_ALLOC
9003                                               | SEC_LOAD
9004                                               | SEC_READONLY
9005                                               | SEC_DATA));
9006                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9007                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9008                         && strcmp (TARGET_OS, "elf") != 0)
9009                       record_alignment (new_seg, 4);
9010                     else
9011                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9012                     if (seg == now_seg)
9013                       as_bad (_("Can't use floating point insn in this section"));
9014
9015                     /* Set the argument to the current address in the
9016                        section.  */
9017                     offset_expr.X_op = O_symbol;
9018                     offset_expr.X_add_symbol =
9019                       symbol_new ("L0\001", now_seg,
9020                                   (valueT) frag_now_fix (), frag_now);
9021                     offset_expr.X_add_number = 0;
9022
9023                     /* Put the floating point number into the section.  */
9024                     p = frag_more ((int) length);
9025                     memcpy (p, temp, length);
9026
9027                     /* Switch back to the original section.  */
9028                     subseg_set (seg, subseg);
9029                   }
9030               }
9031               continue;
9032
9033             case 'i':           /* 16 bit unsigned immediate */
9034             case 'j':           /* 16 bit signed immediate */
9035               *imm_reloc = BFD_RELOC_LO16;
9036               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9037                 {
9038                   int more;
9039                   offsetT minval, maxval;
9040
9041                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9042                           && strcmp (insn->name, insn[1].name) == 0);
9043
9044                   /* If the expression was written as an unsigned number,
9045                      only treat it as signed if there are no more
9046                      alternatives.  */
9047                   if (more
9048                       && *args == 'j'
9049                       && sizeof (imm_expr.X_add_number) <= 4
9050                       && imm_expr.X_op == O_constant
9051                       && imm_expr.X_add_number < 0
9052                       && imm_expr.X_unsigned
9053                       && HAVE_64BIT_GPRS)
9054                     break;
9055
9056                   /* For compatibility with older assemblers, we accept
9057                      0x8000-0xffff as signed 16-bit numbers when only
9058                      signed numbers are allowed.  */
9059                   if (*args == 'i')
9060                     minval = 0, maxval = 0xffff;
9061                   else if (more)
9062                     minval = -0x8000, maxval = 0x7fff;
9063                   else
9064                     minval = -0x8000, maxval = 0xffff;
9065
9066                   if (imm_expr.X_op != O_constant
9067                       || imm_expr.X_add_number < minval
9068                       || imm_expr.X_add_number > maxval)
9069                     {
9070                       if (more)
9071                         break;
9072                       if (imm_expr.X_op == O_constant
9073                           || imm_expr.X_op == O_big)
9074                         as_bad (_("expression out of range"));
9075                     }
9076                 }
9077               s = expr_end;
9078               continue;
9079
9080             case 'o':           /* 16 bit offset */
9081               /* Check whether there is only a single bracketed expression
9082                  left.  If so, it must be the base register and the
9083                  constant must be zero.  */
9084               if (*s == '(' && strchr (s + 1, '(') == 0)
9085                 {
9086                   offset_expr.X_op = O_constant;
9087                   offset_expr.X_add_number = 0;
9088                   continue;
9089                 }
9090
9091               /* If this value won't fit into a 16 bit offset, then go
9092                  find a macro that will generate the 32 bit offset
9093                  code pattern.  */
9094               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9095                   && (offset_expr.X_op != O_constant
9096                       || offset_expr.X_add_number >= 0x8000
9097                       || offset_expr.X_add_number < -0x8000))
9098                 break;
9099
9100               s = expr_end;
9101               continue;
9102
9103             case 'p':           /* pc relative offset */
9104               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9105               my_getExpression (&offset_expr, s);
9106               s = expr_end;
9107               continue;
9108
9109             case 'u':           /* upper 16 bits */
9110               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9111                   && imm_expr.X_op == O_constant
9112                   && (imm_expr.X_add_number < 0
9113                       || imm_expr.X_add_number >= 0x10000))
9114                 as_bad (_("lui expression not in range 0..65535"));
9115               s = expr_end;
9116               continue;
9117
9118             case 'a':           /* 26 bit address */
9119               my_getExpression (&offset_expr, s);
9120               s = expr_end;
9121               *offset_reloc = BFD_RELOC_MIPS_JMP;
9122               continue;
9123
9124             case 'N':           /* 3 bit branch condition code */
9125             case 'M':           /* 3 bit compare condition code */
9126               if (strncmp (s, "$fcc", 4) != 0)
9127                 break;
9128               s += 4;
9129               regno = 0;
9130               do
9131                 {
9132                   regno *= 10;
9133                   regno += *s - '0';
9134                   ++s;
9135                 }
9136               while (ISDIGIT (*s));
9137               if (regno > 7)
9138                 as_bad (_("invalid condition code register $fcc%d"), regno);
9139               if (*args == 'N')
9140                 ip->insn_opcode |= regno << OP_SH_BCC;
9141               else
9142                 ip->insn_opcode |= regno << OP_SH_CCC;
9143               continue;
9144
9145             case 'H':
9146               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9147                 s += 2;
9148               if (ISDIGIT (*s))
9149                 {
9150                   c = 0;
9151                   do
9152                     {
9153                       c *= 10;
9154                       c += *s - '0';
9155                       ++s;
9156                     }
9157                   while (ISDIGIT (*s));
9158                 }
9159               else
9160                 c = 8; /* Invalid sel value.  */
9161
9162               if (c > 7)
9163                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9164               ip->insn_opcode |= c;
9165               continue;
9166
9167             case 'e':
9168               /* Must be at least one digit.  */
9169               my_getExpression (&imm_expr, s);
9170               check_absolute_expr (ip, &imm_expr);
9171
9172               if ((unsigned long) imm_expr.X_add_number
9173                   > (unsigned long) OP_MASK_VECBYTE)
9174                 {
9175                   as_bad (_("bad byte vector index (%ld)"),
9176                            (long) imm_expr.X_add_number);
9177                   imm_expr.X_add_number = 0;
9178                 }
9179
9180               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9181               imm_expr.X_op = O_absent;
9182               s = expr_end;
9183               continue;
9184
9185             case '%':
9186               my_getExpression (&imm_expr, s);
9187               check_absolute_expr (ip, &imm_expr);
9188
9189               if ((unsigned long) imm_expr.X_add_number
9190                   > (unsigned long) OP_MASK_VECALIGN)
9191                 {
9192                   as_bad (_("bad byte vector index (%ld)"),
9193                            (long) imm_expr.X_add_number);
9194                   imm_expr.X_add_number = 0;
9195                 }
9196
9197               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9198               imm_expr.X_op = O_absent;
9199               s = expr_end;
9200               continue;
9201
9202             default:
9203               as_bad (_("bad char = '%c'\n"), *args);
9204               internalError ();
9205             }
9206           break;
9207         }
9208       /* Args don't match.  */
9209       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9210           !strcmp (insn->name, insn[1].name))
9211         {
9212           ++insn;
9213           s = argsStart;
9214           insn_error = _("illegal operands");
9215           continue;
9216         }
9217       if (save_c)
9218         *(--s) = save_c;
9219       insn_error = _("illegal operands");
9220       return;
9221     }
9222 }
9223
9224 /* This routine assembles an instruction into its binary format when
9225    assembling for the mips16.  As a side effect, it sets one of the
9226    global variables imm_reloc or offset_reloc to the type of
9227    relocation to do if one of the operands is an address expression.
9228    It also sets mips16_small and mips16_ext if the user explicitly
9229    requested a small or extended instruction.  */
9230
9231 static void
9232 mips16_ip (str, ip)
9233      char *str;
9234      struct mips_cl_insn *ip;
9235 {
9236   char *s;
9237   const char *args;
9238   struct mips_opcode *insn;
9239   char *argsstart;
9240   unsigned int regno;
9241   unsigned int lastregno = 0;
9242   char *s_reset;
9243
9244   insn_error = NULL;
9245
9246   mips16_small = FALSE;
9247   mips16_ext = FALSE;
9248
9249   for (s = str; ISLOWER (*s); ++s)
9250     ;
9251   switch (*s)
9252     {
9253     case '\0':
9254       break;
9255
9256     case ' ':
9257       *s++ = '\0';
9258       break;
9259
9260     case '.':
9261       if (s[1] == 't' && s[2] == ' ')
9262         {
9263           *s = '\0';
9264           mips16_small = TRUE;
9265           s += 3;
9266           break;
9267         }
9268       else if (s[1] == 'e' && s[2] == ' ')
9269         {
9270           *s = '\0';
9271           mips16_ext = TRUE;
9272           s += 3;
9273           break;
9274         }
9275       /* Fall through.  */
9276     default:
9277       insn_error = _("unknown opcode");
9278       return;
9279     }
9280
9281   if (mips_opts.noautoextend && ! mips16_ext)
9282     mips16_small = TRUE;
9283
9284   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9285     {
9286       insn_error = _("unrecognized opcode");
9287       return;
9288     }
9289
9290   argsstart = s;
9291   for (;;)
9292     {
9293       assert (strcmp (insn->name, str) == 0);
9294
9295       ip->insn_mo = insn;
9296       ip->insn_opcode = insn->match;
9297       ip->use_extend = FALSE;
9298       imm_expr.X_op = O_absent;
9299       imm_reloc[0] = BFD_RELOC_UNUSED;
9300       imm_reloc[1] = BFD_RELOC_UNUSED;
9301       imm_reloc[2] = BFD_RELOC_UNUSED;
9302       offset_expr.X_op = O_absent;
9303       offset_reloc[0] = BFD_RELOC_UNUSED;
9304       offset_reloc[1] = BFD_RELOC_UNUSED;
9305       offset_reloc[2] = BFD_RELOC_UNUSED;
9306       for (args = insn->args; 1; ++args)
9307         {
9308           int c;
9309
9310           if (*s == ' ')
9311             ++s;
9312
9313           /* In this switch statement we call break if we did not find
9314              a match, continue if we did find a match, or return if we
9315              are done.  */
9316
9317           c = *args;
9318           switch (c)
9319             {
9320             case '\0':
9321               if (*s == '\0')
9322                 {
9323                   /* Stuff the immediate value in now, if we can.  */
9324                   if (imm_expr.X_op == O_constant
9325                       && *imm_reloc > BFD_RELOC_UNUSED
9326                       && insn->pinfo != INSN_MACRO)
9327                     {
9328                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9329                                     imm_expr.X_add_number, TRUE, mips16_small,
9330                                     mips16_ext, &ip->insn_opcode,
9331                                     &ip->use_extend, &ip->extend);
9332                       imm_expr.X_op = O_absent;
9333                       *imm_reloc = BFD_RELOC_UNUSED;
9334                     }
9335
9336                   return;
9337                 }
9338               break;
9339
9340             case ',':
9341               if (*s++ == c)
9342                 continue;
9343               s--;
9344               switch (*++args)
9345                 {
9346                 case 'v':
9347                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9348                   continue;
9349                 case 'w':
9350                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9351                   continue;
9352                 }
9353               break;
9354
9355             case '(':
9356             case ')':
9357               if (*s++ == c)
9358                 continue;
9359               break;
9360
9361             case 'v':
9362             case 'w':
9363               if (s[0] != '$')
9364                 {
9365                   if (c == 'v')
9366                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9367                   else
9368                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9369                   ++args;
9370                   continue;
9371                 }
9372               /* Fall through.  */
9373             case 'x':
9374             case 'y':
9375             case 'z':
9376             case 'Z':
9377             case '0':
9378             case 'S':
9379             case 'R':
9380             case 'X':
9381             case 'Y':
9382               if (s[0] != '$')
9383                 break;
9384               s_reset = s;
9385               if (ISDIGIT (s[1]))
9386                 {
9387                   ++s;
9388                   regno = 0;
9389                   do
9390                     {
9391                       regno *= 10;
9392                       regno += *s - '0';
9393                       ++s;
9394                     }
9395                   while (ISDIGIT (*s));
9396                   if (regno > 31)
9397                     {
9398                       as_bad (_("invalid register number (%d)"), regno);
9399                       regno = 2;
9400                     }
9401                 }
9402               else
9403                 {
9404                   if (s[1] == 'r' && s[2] == 'a')
9405                     {
9406                       s += 3;
9407                       regno = RA;
9408                     }
9409                   else if (s[1] == 'f' && s[2] == 'p')
9410                     {
9411                       s += 3;
9412                       regno = FP;
9413                     }
9414                   else if (s[1] == 's' && s[2] == 'p')
9415                     {
9416                       s += 3;
9417                       regno = SP;
9418                     }
9419                   else if (s[1] == 'g' && s[2] == 'p')
9420                     {
9421                       s += 3;
9422                       regno = GP;
9423                     }
9424                   else if (s[1] == 'a' && s[2] == 't')
9425                     {
9426                       s += 3;
9427                       regno = AT;
9428                     }
9429                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9430                     {
9431                       s += 4;
9432                       regno = KT0;
9433                     }
9434                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9435                     {
9436                       s += 4;
9437                       regno = KT1;
9438                     }
9439                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9440                     {
9441                       s += 5;
9442                       regno = ZERO;
9443                     }
9444                   else
9445                     break;
9446                 }
9447
9448               if (*s == ' ')
9449                 ++s;
9450               if (args[1] != *s)
9451                 {
9452                   if (c == 'v' || c == 'w')
9453                     {
9454                       regno = mips16_to_32_reg_map[lastregno];
9455                       s = s_reset;
9456                       ++args;
9457                     }
9458                 }
9459
9460               switch (c)
9461                 {
9462                 case 'x':
9463                 case 'y':
9464                 case 'z':
9465                 case 'v':
9466                 case 'w':
9467                 case 'Z':
9468                   regno = mips32_to_16_reg_map[regno];
9469                   break;
9470
9471                 case '0':
9472                   if (regno != 0)
9473                     regno = ILLEGAL_REG;
9474                   break;
9475
9476                 case 'S':
9477                   if (regno != SP)
9478                     regno = ILLEGAL_REG;
9479                   break;
9480
9481                 case 'R':
9482                   if (regno != RA)
9483                     regno = ILLEGAL_REG;
9484                   break;
9485
9486                 case 'X':
9487                 case 'Y':
9488                   if (regno == AT && ! mips_opts.noat)
9489                     as_warn (_("used $at without \".set noat\""));
9490                   break;
9491
9492                 default:
9493                   internalError ();
9494                 }
9495
9496               if (regno == ILLEGAL_REG)
9497                 break;
9498
9499               switch (c)
9500                 {
9501                 case 'x':
9502                 case 'v':
9503                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9504                   break;
9505                 case 'y':
9506                 case 'w':
9507                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9508                   break;
9509                 case 'z':
9510                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9511                   break;
9512                 case 'Z':
9513                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9514                 case '0':
9515                 case 'S':
9516                 case 'R':
9517                   break;
9518                 case 'X':
9519                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9520                   break;
9521                 case 'Y':
9522                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9523                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9524                   break;
9525                 default:
9526                   internalError ();
9527                 }
9528
9529               lastregno = regno;
9530               continue;
9531
9532             case 'P':
9533               if (strncmp (s, "$pc", 3) == 0)
9534                 {
9535                   s += 3;
9536                   continue;
9537                 }
9538               break;
9539
9540             case '<':
9541             case '>':
9542             case '[':
9543             case ']':
9544             case '4':
9545             case '5':
9546             case 'H':
9547             case 'W':
9548             case 'D':
9549             case 'j':
9550             case '8':
9551             case 'V':
9552             case 'C':
9553             case 'U':
9554             case 'k':
9555             case 'K':
9556               if (s[0] == '%'
9557                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9558                 {
9559                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9560                      and generate the appropriate reloc.  If the text
9561                      inside %gprel is not a symbol name with an
9562                      optional offset, then we generate a normal reloc
9563                      and will probably fail later.  */
9564                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9565                   if (imm_expr.X_op == O_symbol)
9566                     {
9567                       mips16_ext = TRUE;
9568                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9569                       s = expr_end;
9570                       ip->use_extend = TRUE;
9571                       ip->extend = 0;
9572                       continue;
9573                     }
9574                 }
9575               else
9576                 {
9577                   /* Just pick up a normal expression.  */
9578                   my_getExpression (&imm_expr, s);
9579                 }
9580
9581               if (imm_expr.X_op == O_register)
9582                 {
9583                   /* What we thought was an expression turned out to
9584                      be a register.  */
9585
9586                   if (s[0] == '(' && args[1] == '(')
9587                     {
9588                       /* It looks like the expression was omitted
9589                          before a register indirection, which means
9590                          that the expression is implicitly zero.  We
9591                          still set up imm_expr, so that we handle
9592                          explicit extensions correctly.  */
9593                       imm_expr.X_op = O_constant;
9594                       imm_expr.X_add_number = 0;
9595                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9596                       continue;
9597                     }
9598
9599                   break;
9600                 }
9601
9602               /* We need to relax this instruction.  */
9603               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9604               s = expr_end;
9605               continue;
9606
9607             case 'p':
9608             case 'q':
9609             case 'A':
9610             case 'B':
9611             case 'E':
9612               /* We use offset_reloc rather than imm_reloc for the PC
9613                  relative operands.  This lets macros with both
9614                  immediate and address operands work correctly.  */
9615               my_getExpression (&offset_expr, s);
9616
9617               if (offset_expr.X_op == O_register)
9618                 break;
9619
9620               /* We need to relax this instruction.  */
9621               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9622               s = expr_end;
9623               continue;
9624
9625             case '6':           /* break code */
9626               my_getExpression (&imm_expr, s);
9627               check_absolute_expr (ip, &imm_expr);
9628               if ((unsigned long) imm_expr.X_add_number > 63)
9629                 {
9630                   as_warn (_("Invalid value for `%s' (%lu)"),
9631                            ip->insn_mo->name,
9632                            (unsigned long) imm_expr.X_add_number);
9633                   imm_expr.X_add_number &= 0x3f;
9634                 }
9635               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9636               imm_expr.X_op = O_absent;
9637               s = expr_end;
9638               continue;
9639
9640             case 'a':           /* 26 bit address */
9641               my_getExpression (&offset_expr, s);
9642               s = expr_end;
9643               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9644               ip->insn_opcode <<= 16;
9645               continue;
9646
9647             case 'l':           /* register list for entry macro */
9648             case 'L':           /* register list for exit macro */
9649               {
9650                 int mask;
9651
9652                 if (c == 'l')
9653                   mask = 0;
9654                 else
9655                   mask = 7 << 3;
9656                 while (*s != '\0')
9657                   {
9658                     int freg, reg1, reg2;
9659
9660                     while (*s == ' ' || *s == ',')
9661                       ++s;
9662                     if (*s != '$')
9663                       {
9664                         as_bad (_("can't parse register list"));
9665                         break;
9666                       }
9667                     ++s;
9668                     if (*s != 'f')
9669                       freg = 0;
9670                     else
9671                       {
9672                         freg = 1;
9673                         ++s;
9674                       }
9675                     reg1 = 0;
9676                     while (ISDIGIT (*s))
9677                       {
9678                         reg1 *= 10;
9679                         reg1 += *s - '0';
9680                         ++s;
9681                       }
9682                     if (*s == ' ')
9683                       ++s;
9684                     if (*s != '-')
9685                       reg2 = reg1;
9686                     else
9687                       {
9688                         ++s;
9689                         if (*s != '$')
9690                           break;
9691                         ++s;
9692                         if (freg)
9693                           {
9694                             if (*s == 'f')
9695                               ++s;
9696                             else
9697                               {
9698                                 as_bad (_("invalid register list"));
9699                                 break;
9700                               }
9701                           }
9702                         reg2 = 0;
9703                         while (ISDIGIT (*s))
9704                           {
9705                             reg2 *= 10;
9706                             reg2 += *s - '0';
9707                             ++s;
9708                           }
9709                       }
9710                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9711                       {
9712                         mask &= ~ (7 << 3);
9713                         mask |= 5 << 3;
9714                       }
9715                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9716                       {
9717                         mask &= ~ (7 << 3);
9718                         mask |= 6 << 3;
9719                       }
9720                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9721                       mask |= (reg2 - 3) << 3;
9722                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9723                       mask |= (reg2 - 15) << 1;
9724                     else if (reg1 == RA && reg2 == RA)
9725                       mask |= 1;
9726                     else
9727                       {
9728                         as_bad (_("invalid register list"));
9729                         break;
9730                       }
9731                   }
9732                 /* The mask is filled in in the opcode table for the
9733                    benefit of the disassembler.  We remove it before
9734                    applying the actual mask.  */
9735                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9736                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9737               }
9738             continue;
9739
9740             case 'e':           /* extend code */
9741               my_getExpression (&imm_expr, s);
9742               check_absolute_expr (ip, &imm_expr);
9743               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9744                 {
9745                   as_warn (_("Invalid value for `%s' (%lu)"),
9746                            ip->insn_mo->name,
9747                            (unsigned long) imm_expr.X_add_number);
9748                   imm_expr.X_add_number &= 0x7ff;
9749                 }
9750               ip->insn_opcode |= imm_expr.X_add_number;
9751               imm_expr.X_op = O_absent;
9752               s = expr_end;
9753               continue;
9754
9755             default:
9756               internalError ();
9757             }
9758           break;
9759         }
9760
9761       /* Args don't match.  */
9762       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9763           strcmp (insn->name, insn[1].name) == 0)
9764         {
9765           ++insn;
9766           s = argsstart;
9767           continue;
9768         }
9769
9770       insn_error = _("illegal operands");
9771
9772       return;
9773     }
9774 }
9775
9776 /* This structure holds information we know about a mips16 immediate
9777    argument type.  */
9778
9779 struct mips16_immed_operand
9780 {
9781   /* The type code used in the argument string in the opcode table.  */
9782   int type;
9783   /* The number of bits in the short form of the opcode.  */
9784   int nbits;
9785   /* The number of bits in the extended form of the opcode.  */
9786   int extbits;
9787   /* The amount by which the short form is shifted when it is used;
9788      for example, the sw instruction has a shift count of 2.  */
9789   int shift;
9790   /* The amount by which the short form is shifted when it is stored
9791      into the instruction code.  */
9792   int op_shift;
9793   /* Non-zero if the short form is unsigned.  */
9794   int unsp;
9795   /* Non-zero if the extended form is unsigned.  */
9796   int extu;
9797   /* Non-zero if the value is PC relative.  */
9798   int pcrel;
9799 };
9800
9801 /* The mips16 immediate operand types.  */
9802
9803 static const struct mips16_immed_operand mips16_immed_operands[] =
9804 {
9805   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9806   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9807   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9808   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9809   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9810   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9811   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9812   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9813   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9814   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9815   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9816   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9817   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9818   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9819   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9820   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9821   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9822   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9823   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9824   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9825   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9826 };
9827
9828 #define MIPS16_NUM_IMMED \
9829   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9830
9831 /* Handle a mips16 instruction with an immediate value.  This or's the
9832    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9833    whether an extended value is needed; if one is needed, it sets
9834    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9835    If SMALL is true, an unextended opcode was explicitly requested.
9836    If EXT is true, an extended opcode was explicitly requested.  If
9837    WARN is true, warn if EXT does not match reality.  */
9838
9839 static void
9840 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9841               extend)
9842      char *file;
9843      unsigned int line;
9844      int type;
9845      offsetT val;
9846      bfd_boolean warn;
9847      bfd_boolean small;
9848      bfd_boolean ext;
9849      unsigned long *insn;
9850      bfd_boolean *use_extend;
9851      unsigned short *extend;
9852 {
9853   register const struct mips16_immed_operand *op;
9854   int mintiny, maxtiny;
9855   bfd_boolean needext;
9856
9857   op = mips16_immed_operands;
9858   while (op->type != type)
9859     {
9860       ++op;
9861       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9862     }
9863
9864   if (op->unsp)
9865     {
9866       if (type == '<' || type == '>' || type == '[' || type == ']')
9867         {
9868           mintiny = 1;
9869           maxtiny = 1 << op->nbits;
9870         }
9871       else
9872         {
9873           mintiny = 0;
9874           maxtiny = (1 << op->nbits) - 1;
9875         }
9876     }
9877   else
9878     {
9879       mintiny = - (1 << (op->nbits - 1));
9880       maxtiny = (1 << (op->nbits - 1)) - 1;
9881     }
9882
9883   /* Branch offsets have an implicit 0 in the lowest bit.  */
9884   if (type == 'p' || type == 'q')
9885     val /= 2;
9886
9887   if ((val & ((1 << op->shift) - 1)) != 0
9888       || val < (mintiny << op->shift)
9889       || val > (maxtiny << op->shift))
9890     needext = TRUE;
9891   else
9892     needext = FALSE;
9893
9894   if (warn && ext && ! needext)
9895     as_warn_where (file, line,
9896                    _("extended operand requested but not required"));
9897   if (small && needext)
9898     as_bad_where (file, line, _("invalid unextended operand value"));
9899
9900   if (small || (! ext && ! needext))
9901     {
9902       int insnval;
9903
9904       *use_extend = FALSE;
9905       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9906       insnval <<= op->op_shift;
9907       *insn |= insnval;
9908     }
9909   else
9910     {
9911       long minext, maxext;
9912       int extval;
9913
9914       if (op->extu)
9915         {
9916           minext = 0;
9917           maxext = (1 << op->extbits) - 1;
9918         }
9919       else
9920         {
9921           minext = - (1 << (op->extbits - 1));
9922           maxext = (1 << (op->extbits - 1)) - 1;
9923         }
9924       if (val < minext || val > maxext)
9925         as_bad_where (file, line,
9926                       _("operand value out of range for instruction"));
9927
9928       *use_extend = TRUE;
9929       if (op->extbits == 16)
9930         {
9931           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9932           val &= 0x1f;
9933         }
9934       else if (op->extbits == 15)
9935         {
9936           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9937           val &= 0xf;
9938         }
9939       else
9940         {
9941           extval = ((val & 0x1f) << 6) | (val & 0x20);
9942           val = 0;
9943         }
9944
9945       *extend = (unsigned short) extval;
9946       *insn |= val;
9947     }
9948 }
9949 \f
9950 static const struct percent_op_match
9951 {
9952   const char *str;
9953   bfd_reloc_code_real_type reloc;
9954 } percent_op[] =
9955 {
9956   {"%lo", BFD_RELOC_LO16},
9957 #ifdef OBJ_ELF
9958   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9959   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9960   {"%call16", BFD_RELOC_MIPS_CALL16},
9961   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9962   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9963   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9964   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9965   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9966   {"%got", BFD_RELOC_MIPS_GOT16},
9967   {"%gp_rel", BFD_RELOC_GPREL16},
9968   {"%half", BFD_RELOC_16},
9969   {"%highest", BFD_RELOC_MIPS_HIGHEST},
9970   {"%higher", BFD_RELOC_MIPS_HIGHER},
9971   {"%neg", BFD_RELOC_MIPS_SUB},
9972 #endif
9973   {"%hi", BFD_RELOC_HI16_S}
9974 };
9975
9976
9977 /* Return true if *STR points to a relocation operator.  When returning true,
9978    move *STR over the operator and store its relocation code in *RELOC.
9979    Leave both *STR and *RELOC alone when returning false.  */
9980
9981 static bfd_boolean
9982 parse_relocation (str, reloc)
9983      char **str;
9984      bfd_reloc_code_real_type *reloc;
9985 {
9986   size_t i;
9987
9988   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9989     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9990       {
9991         *str += strlen (percent_op[i].str);
9992         *reloc = percent_op[i].reloc;
9993
9994         /* Check whether the output BFD supports this relocation.
9995            If not, issue an error and fall back on something safe.  */
9996         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9997           {
9998             as_bad ("relocation %s isn't supported by the current ABI",
9999                     percent_op[i].str);
10000             *reloc = BFD_RELOC_LO16;
10001           }
10002         return TRUE;
10003       }
10004   return FALSE;
10005 }
10006
10007
10008 /* Parse string STR as a 16-bit relocatable operand.  Store the
10009    expression in *EP and the relocations in the array starting
10010    at RELOC.  Return the number of relocation operators used.
10011
10012    On exit, EXPR_END points to the first character after the expression.
10013    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10014
10015 static size_t
10016 my_getSmallExpression (ep, reloc, str)
10017      expressionS *ep;
10018      bfd_reloc_code_real_type *reloc;
10019      char *str;
10020 {
10021   bfd_reloc_code_real_type reversed_reloc[3];
10022   size_t reloc_index, i;
10023   int bracket_depth;
10024
10025   reloc_index = 0;
10026   bracket_depth = 0;
10027
10028   /* Search for the start of the main expression, recoding relocations
10029      in REVERSED_RELOC.  */
10030   for (;;)
10031     {
10032       if (*str == '(')
10033         bracket_depth++, str++;
10034       else if (*str == ' ' || *str == '\t')
10035         str++;
10036       else if (*str == '%'
10037                && reloc_index < (HAVE_NEWABI ? 3 : 1)
10038                && parse_relocation (&str, &reversed_reloc[reloc_index]))
10039         reloc_index++;
10040       else
10041         break;
10042     }
10043
10044   my_getExpression (ep, str);
10045   str = expr_end;
10046
10047   /* Match every open bracket.  */
10048   while (bracket_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10049     if (*str++ == ')')
10050       bracket_depth--;
10051
10052   if (bracket_depth > 0)
10053     as_bad ("unclosed '('");
10054
10055   expr_end = str;
10056
10057   reloc[0] = BFD_RELOC_LO16;
10058   for (i = 0; i < reloc_index; i++)
10059     reloc[i] = reversed_reloc[reloc_index - 1 - i];
10060
10061   return reloc_index;
10062 }
10063
10064 static void
10065 my_getExpression (ep, str)
10066      expressionS *ep;
10067      char *str;
10068 {
10069   char *save_in;
10070   valueT val;
10071
10072   save_in = input_line_pointer;
10073   input_line_pointer = str;
10074   expression (ep);
10075   expr_end = input_line_pointer;
10076   input_line_pointer = save_in;
10077
10078   /* If we are in mips16 mode, and this is an expression based on `.',
10079      then we bump the value of the symbol by 1 since that is how other
10080      text symbols are handled.  We don't bother to handle complex
10081      expressions, just `.' plus or minus a constant.  */
10082   if (mips_opts.mips16
10083       && ep->X_op == O_symbol
10084       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10085       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10086       && symbol_get_frag (ep->X_add_symbol) == frag_now
10087       && symbol_constant_p (ep->X_add_symbol)
10088       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10089     S_SET_VALUE (ep->X_add_symbol, val + 1);
10090 }
10091
10092 /* Turn a string in input_line_pointer into a floating point constant
10093    of type TYPE, and store the appropriate bytes in *LITP.  The number
10094    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10095    returned, or NULL on OK.  */
10096
10097 char *
10098 md_atof (type, litP, sizeP)
10099      int type;
10100      char *litP;
10101      int *sizeP;
10102 {
10103   int prec;
10104   LITTLENUM_TYPE words[4];
10105   char *t;
10106   int i;
10107
10108   switch (type)
10109     {
10110     case 'f':
10111       prec = 2;
10112       break;
10113
10114     case 'd':
10115       prec = 4;
10116       break;
10117
10118     default:
10119       *sizeP = 0;
10120       return _("bad call to md_atof");
10121     }
10122
10123   t = atof_ieee (input_line_pointer, type, words);
10124   if (t)
10125     input_line_pointer = t;
10126
10127   *sizeP = prec * 2;
10128
10129   if (! target_big_endian)
10130     {
10131       for (i = prec - 1; i >= 0; i--)
10132         {
10133           md_number_to_chars (litP, (valueT) words[i], 2);
10134           litP += 2;
10135         }
10136     }
10137   else
10138     {
10139       for (i = 0; i < prec; i++)
10140         {
10141           md_number_to_chars (litP, (valueT) words[i], 2);
10142           litP += 2;
10143         }
10144     }
10145
10146   return NULL;
10147 }
10148
10149 void
10150 md_number_to_chars (buf, val, n)
10151      char *buf;
10152      valueT val;
10153      int n;
10154 {
10155   if (target_big_endian)
10156     number_to_chars_bigendian (buf, val, n);
10157   else
10158     number_to_chars_littleendian (buf, val, n);
10159 }
10160 \f
10161 #ifdef OBJ_ELF
10162 static int support_64bit_objects(void)
10163 {
10164   const char **list, **l;
10165   int yes;
10166
10167   list = bfd_target_list ();
10168   for (l = list; *l != NULL; l++)
10169 #ifdef TE_TMIPS
10170     /* This is traditional mips */
10171     if (strcmp (*l, "elf64-tradbigmips") == 0
10172         || strcmp (*l, "elf64-tradlittlemips") == 0)
10173 #else
10174     if (strcmp (*l, "elf64-bigmips") == 0
10175         || strcmp (*l, "elf64-littlemips") == 0)
10176 #endif
10177       break;
10178   yes = (*l != NULL);
10179   free (list);
10180   return yes;
10181 }
10182 #endif /* OBJ_ELF */
10183
10184 const char *md_shortopts = "nO::g::G:";
10185
10186 struct option md_longopts[] =
10187 {
10188 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10189   {"mips0", no_argument, NULL, OPTION_MIPS1},
10190   {"mips1", no_argument, NULL, OPTION_MIPS1},
10191 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10192   {"mips2", no_argument, NULL, OPTION_MIPS2},
10193 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10194   {"mips3", no_argument, NULL, OPTION_MIPS3},
10195 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10196   {"mips4", no_argument, NULL, OPTION_MIPS4},
10197 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10198   {"mips5", no_argument, NULL, OPTION_MIPS5},
10199 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10200   {"mips32", no_argument, NULL, OPTION_MIPS32},
10201 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10202   {"mips64", no_argument, NULL, OPTION_MIPS64},
10203 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10204   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10205 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10206   {"trap", no_argument, NULL, OPTION_TRAP},
10207   {"no-break", no_argument, NULL, OPTION_TRAP},
10208 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10209   {"break", no_argument, NULL, OPTION_BREAK},
10210   {"no-trap", no_argument, NULL, OPTION_BREAK},
10211 #define OPTION_EB (OPTION_MD_BASE + 11)
10212   {"EB", no_argument, NULL, OPTION_EB},
10213 #define OPTION_EL (OPTION_MD_BASE + 12)
10214   {"EL", no_argument, NULL, OPTION_EL},
10215 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10216   {"mips16", no_argument, NULL, OPTION_MIPS16},
10217 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10218   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10219 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10220   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10221 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10222   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10223   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10224 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10225   {"mfp32", no_argument, NULL, OPTION_FP32},
10226 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10227   {"mgp32", no_argument, NULL, OPTION_GP32},
10228 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10229   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10230 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10231   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10232 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10233   {"march", required_argument, NULL, OPTION_MARCH},
10234 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10235   {"mtune", required_argument, NULL, OPTION_MTUNE},
10236 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10237   {"mfp64", no_argument, NULL, OPTION_FP64},
10238 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10239   {"m4650", no_argument, NULL, OPTION_M4650},
10240 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10241   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10242 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10243   {"m4010", no_argument, NULL, OPTION_M4010},
10244 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10245   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10246 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10247   {"m4100", no_argument, NULL, OPTION_M4100},
10248 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10249   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10250 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10251   {"m3900", no_argument, NULL, OPTION_M3900},
10252 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10253   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10254 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10255   {"mgp64", no_argument, NULL, OPTION_GP64},
10256 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10257   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10258 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10259   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10260 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10261   {"mdmx", no_argument, NULL, OPTION_MDMX},
10262 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10263   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10264 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10265 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10266   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10267   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10268 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10269 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10270   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10271   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10272 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10273   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10274 #ifdef OBJ_ELF
10275 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 42)
10276 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10277   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10278   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10279 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10280   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10281 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10282   {"xgot",        no_argument, NULL, OPTION_XGOT},
10283 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10284   {"mabi", required_argument, NULL, OPTION_MABI},
10285 #define OPTION_32          (OPTION_ELF_BASE + 4)
10286   {"32",          no_argument, NULL, OPTION_32},
10287 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10288   {"n32",         no_argument, NULL, OPTION_N32},
10289 #define OPTION_64          (OPTION_ELF_BASE + 6)
10290   {"64",          no_argument, NULL, OPTION_64},
10291 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10292   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10293 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10294   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10295 #endif /* OBJ_ELF */
10296   {NULL, no_argument, NULL, 0}
10297 };
10298 size_t md_longopts_size = sizeof (md_longopts);
10299
10300 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10301    NEW_VALUE.  Warn if another value was already specified.  Note:
10302    we have to defer parsing the -march and -mtune arguments in order
10303    to handle 'from-abi' correctly, since the ABI might be specified
10304    in a later argument.  */
10305
10306 static void
10307 mips_set_option_string (string_ptr, new_value)
10308      const char **string_ptr, *new_value;
10309 {
10310   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10311     as_warn (_("A different %s was already specified, is now %s"),
10312              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10313              new_value);
10314
10315   *string_ptr = new_value;
10316 }
10317
10318 int
10319 md_parse_option (c, arg)
10320      int c;
10321      char *arg;
10322 {
10323   switch (c)
10324     {
10325     case OPTION_CONSTRUCT_FLOATS:
10326       mips_disable_float_construction = 0;
10327       break;
10328
10329     case OPTION_NO_CONSTRUCT_FLOATS:
10330       mips_disable_float_construction = 1;
10331       break;
10332
10333     case OPTION_TRAP:
10334       mips_trap = 1;
10335       break;
10336
10337     case OPTION_BREAK:
10338       mips_trap = 0;
10339       break;
10340
10341     case OPTION_EB:
10342       target_big_endian = 1;
10343       break;
10344
10345     case OPTION_EL:
10346       target_big_endian = 0;
10347       break;
10348
10349     case 'n':
10350       warn_nops = 1;
10351       break;
10352
10353     case 'O':
10354       if (arg && arg[1] == '0')
10355         mips_optimize = 1;
10356       else
10357         mips_optimize = 2;
10358       break;
10359
10360     case 'g':
10361       if (arg == NULL)
10362         mips_debug = 2;
10363       else
10364         mips_debug = atoi (arg);
10365       /* When the MIPS assembler sees -g or -g2, it does not do
10366          optimizations which limit full symbolic debugging.  We take
10367          that to be equivalent to -O0.  */
10368       if (mips_debug == 2)
10369         mips_optimize = 1;
10370       break;
10371
10372     case OPTION_MIPS1:
10373       file_mips_isa = ISA_MIPS1;
10374       break;
10375
10376     case OPTION_MIPS2:
10377       file_mips_isa = ISA_MIPS2;
10378       break;
10379
10380     case OPTION_MIPS3:
10381       file_mips_isa = ISA_MIPS3;
10382       break;
10383
10384     case OPTION_MIPS4:
10385       file_mips_isa = ISA_MIPS4;
10386       break;
10387
10388     case OPTION_MIPS5:
10389       file_mips_isa = ISA_MIPS5;
10390       break;
10391
10392     case OPTION_MIPS32:
10393       file_mips_isa = ISA_MIPS32;
10394       break;
10395
10396     case OPTION_MIPS32R2:
10397       file_mips_isa = ISA_MIPS32R2;
10398       break;
10399
10400     case OPTION_MIPS64:
10401       file_mips_isa = ISA_MIPS64;
10402       break;
10403
10404     case OPTION_MTUNE:
10405       mips_set_option_string (&mips_tune_string, arg);
10406       break;
10407
10408     case OPTION_MARCH:
10409       mips_set_option_string (&mips_arch_string, arg);
10410       break;
10411
10412     case OPTION_M4650:
10413       mips_set_option_string (&mips_arch_string, "4650");
10414       mips_set_option_string (&mips_tune_string, "4650");
10415       break;
10416
10417     case OPTION_NO_M4650:
10418       break;
10419
10420     case OPTION_M4010:
10421       mips_set_option_string (&mips_arch_string, "4010");
10422       mips_set_option_string (&mips_tune_string, "4010");
10423       break;
10424
10425     case OPTION_NO_M4010:
10426       break;
10427
10428     case OPTION_M4100:
10429       mips_set_option_string (&mips_arch_string, "4100");
10430       mips_set_option_string (&mips_tune_string, "4100");
10431       break;
10432
10433     case OPTION_NO_M4100:
10434       break;
10435
10436     case OPTION_M3900:
10437       mips_set_option_string (&mips_arch_string, "3900");
10438       mips_set_option_string (&mips_tune_string, "3900");
10439       break;
10440
10441     case OPTION_NO_M3900:
10442       break;
10443
10444     case OPTION_MDMX:
10445       mips_opts.ase_mdmx = 1;
10446       break;
10447
10448     case OPTION_NO_MDMX:
10449       mips_opts.ase_mdmx = 0;
10450       break;
10451
10452     case OPTION_MIPS16:
10453       mips_opts.mips16 = 1;
10454       mips_no_prev_insn (FALSE);
10455       break;
10456
10457     case OPTION_NO_MIPS16:
10458       mips_opts.mips16 = 0;
10459       mips_no_prev_insn (FALSE);
10460       break;
10461
10462     case OPTION_MIPS3D:
10463       mips_opts.ase_mips3d = 1;
10464       break;
10465
10466     case OPTION_NO_MIPS3D:
10467       mips_opts.ase_mips3d = 0;
10468       break;
10469
10470     case OPTION_MEMBEDDED_PIC:
10471       mips_pic = EMBEDDED_PIC;
10472       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10473         {
10474           as_bad (_("-G may not be used with embedded PIC code"));
10475           return 0;
10476         }
10477       g_switch_value = 0x7fffffff;
10478       break;
10479
10480     case OPTION_FIX_VR4122:
10481       mips_fix_4122_bugs = 1;
10482       break;
10483
10484     case OPTION_NO_FIX_VR4122:
10485       mips_fix_4122_bugs = 0;
10486       break;
10487
10488     case OPTION_RELAX_BRANCH:
10489       mips_relax_branch = 1;
10490       break;
10491
10492     case OPTION_NO_RELAX_BRANCH:
10493       mips_relax_branch = 0;
10494       break;
10495
10496 #ifdef OBJ_ELF
10497       /* When generating ELF code, we permit -KPIC and -call_shared to
10498          select SVR4_PIC, and -non_shared to select no PIC.  This is
10499          intended to be compatible with Irix 5.  */
10500     case OPTION_CALL_SHARED:
10501       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10502         {
10503           as_bad (_("-call_shared is supported only for ELF format"));
10504           return 0;
10505         }
10506       mips_pic = SVR4_PIC;
10507       if (g_switch_seen && g_switch_value != 0)
10508         {
10509           as_bad (_("-G may not be used with SVR4 PIC code"));
10510           return 0;
10511         }
10512       g_switch_value = 0;
10513       break;
10514
10515     case OPTION_NON_SHARED:
10516       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10517         {
10518           as_bad (_("-non_shared is supported only for ELF format"));
10519           return 0;
10520         }
10521       mips_pic = NO_PIC;
10522       break;
10523
10524       /* The -xgot option tells the assembler to use 32 offsets when
10525          accessing the got in SVR4_PIC mode.  It is for Irix
10526          compatibility.  */
10527     case OPTION_XGOT:
10528       mips_big_got = 1;
10529       break;
10530 #endif /* OBJ_ELF */
10531
10532     case 'G':
10533       if (! USE_GLOBAL_POINTER_OPT)
10534         {
10535           as_bad (_("-G is not supported for this configuration"));
10536           return 0;
10537         }
10538       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10539         {
10540           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10541           return 0;
10542         }
10543       else
10544         g_switch_value = atoi (arg);
10545       g_switch_seen = 1;
10546       break;
10547
10548 #ifdef OBJ_ELF
10549       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10550          and -mabi=64.  */
10551     case OPTION_32:
10552       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10553         {
10554           as_bad (_("-32 is supported for ELF format only"));
10555           return 0;
10556         }
10557       mips_abi = O32_ABI;
10558       break;
10559
10560     case OPTION_N32:
10561       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10562         {
10563           as_bad (_("-n32 is supported for ELF format only"));
10564           return 0;
10565         }
10566       mips_abi = N32_ABI;
10567       break;
10568
10569     case OPTION_64:
10570       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10571         {
10572           as_bad (_("-64 is supported for ELF format only"));
10573           return 0;
10574         }
10575       mips_abi = N64_ABI;
10576       if (! support_64bit_objects())
10577         as_fatal (_("No compiled in support for 64 bit object file format"));
10578       break;
10579 #endif /* OBJ_ELF */
10580
10581     case OPTION_GP32:
10582       file_mips_gp32 = 1;
10583       break;
10584
10585     case OPTION_GP64:
10586       file_mips_gp32 = 0;
10587       break;
10588
10589     case OPTION_FP32:
10590       file_mips_fp32 = 1;
10591       break;
10592
10593     case OPTION_FP64:
10594       file_mips_fp32 = 0;
10595       break;
10596
10597 #ifdef OBJ_ELF
10598     case OPTION_MABI:
10599       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10600         {
10601           as_bad (_("-mabi is supported for ELF format only"));
10602           return 0;
10603         }
10604       if (strcmp (arg, "32") == 0)
10605         mips_abi = O32_ABI;
10606       else if (strcmp (arg, "o64") == 0)
10607         mips_abi = O64_ABI;
10608       else if (strcmp (arg, "n32") == 0)
10609         mips_abi = N32_ABI;
10610       else if (strcmp (arg, "64") == 0)
10611         {
10612           mips_abi = N64_ABI;
10613           if (! support_64bit_objects())
10614             as_fatal (_("No compiled in support for 64 bit object file "
10615                         "format"));
10616         }
10617       else if (strcmp (arg, "eabi") == 0)
10618         mips_abi = EABI_ABI;
10619       else
10620         {
10621           as_fatal (_("invalid abi -mabi=%s"), arg);
10622           return 0;
10623         }
10624       break;
10625 #endif /* OBJ_ELF */
10626
10627     case OPTION_M7000_HILO_FIX:
10628       mips_7000_hilo_fix = TRUE;
10629       break;
10630
10631     case OPTION_MNO_7000_HILO_FIX:
10632       mips_7000_hilo_fix = FALSE;
10633       break;
10634
10635 #ifdef OBJ_ELF
10636     case OPTION_MDEBUG:
10637       mips_flag_mdebug = TRUE;
10638       break;
10639
10640     case OPTION_NO_MDEBUG:
10641       mips_flag_mdebug = FALSE;
10642       break;
10643 #endif /* OBJ_ELF */
10644
10645     default:
10646       return 0;
10647     }
10648
10649   return 1;
10650 }
10651 \f
10652 /* Set up globals to generate code for the ISA or processor
10653    described by INFO.  */
10654
10655 static void
10656 mips_set_architecture (info)
10657      const struct mips_cpu_info *info;
10658 {
10659   if (info != 0)
10660     {
10661       mips_arch_info = info;
10662       mips_arch = info->cpu;
10663       mips_opts.isa = info->isa;
10664     }
10665 }
10666
10667
10668 /* Likewise for tuning.  */
10669
10670 static void
10671 mips_set_tune (info)
10672      const struct mips_cpu_info *info;
10673 {
10674   if (info != 0)
10675     {
10676       mips_tune_info = info;
10677       mips_tune = info->cpu;
10678     }
10679 }
10680
10681
10682 void
10683 mips_after_parse_args ()
10684 {
10685   /* GP relative stuff not working for PE */
10686   if (strncmp (TARGET_OS, "pe", 2) == 0
10687       && g_switch_value != 0)
10688     {
10689       if (g_switch_seen)
10690         as_bad (_("-G not supported in this configuration."));
10691       g_switch_value = 0;
10692     }
10693
10694   /* The following code determines the architecture and register size.
10695      Similar code was added to GCC 3.3 (see override_options() in
10696      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10697      as much as possible.  */
10698
10699   if (mips_arch_string != 0)
10700     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10701
10702   if (mips_tune_string != 0)
10703     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10704
10705   if (file_mips_isa != ISA_UNKNOWN)
10706     {
10707       /* Handle -mipsN.  At this point, file_mips_isa contains the
10708          ISA level specified by -mipsN, while mips_opts.isa contains
10709          the -march selection (if any).  */
10710       if (mips_arch_info != 0)
10711         {
10712           /* -march takes precedence over -mipsN, since it is more descriptive.
10713              There's no harm in specifying both as long as the ISA levels
10714              are the same.  */
10715           if (file_mips_isa != mips_opts.isa)
10716             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10717                     mips_cpu_info_from_isa (file_mips_isa)->name,
10718                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10719         }
10720       else
10721         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10722     }
10723
10724   if (mips_arch_info == 0)
10725     mips_set_architecture (mips_parse_cpu ("default CPU",
10726                                            MIPS_CPU_STRING_DEFAULT));
10727
10728   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10729     as_bad ("-march=%s is not compatible with the selected ABI",
10730             mips_arch_info->name);
10731
10732   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10733   if (mips_tune_info == 0)
10734     mips_set_tune (mips_arch_info);
10735
10736   if (file_mips_gp32 >= 0)
10737     {
10738       /* The user specified the size of the integer registers.  Make sure
10739          it agrees with the ABI and ISA.  */
10740       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10741         as_bad (_("-mgp64 used with a 32-bit processor"));
10742       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10743         as_bad (_("-mgp32 used with a 64-bit ABI"));
10744       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10745         as_bad (_("-mgp64 used with a 32-bit ABI"));
10746     }
10747   else
10748     {
10749       /* Infer the integer register size from the ABI and processor.
10750          Restrict ourselves to 32-bit registers if that's all the
10751          processor has, or if the ABI cannot handle 64-bit registers.  */
10752       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10753                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10754     }
10755
10756   /* ??? GAS treats single-float processors as though they had 64-bit
10757      float registers (although it complains when double-precision
10758      instructions are used).  As things stand, saying they have 32-bit
10759      registers would lead to spurious "register must be even" messages.
10760      So here we assume float registers are always the same size as
10761      integer ones, unless the user says otherwise.  */
10762   if (file_mips_fp32 < 0)
10763     file_mips_fp32 = file_mips_gp32;
10764
10765   /* End of GCC-shared inference code.  */
10766
10767   /* ??? When do we want this flag to be set?   Who uses it?  */
10768   if (file_mips_gp32 == 1
10769       && mips_abi == NO_ABI
10770       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10771     mips_32bitmode = 1;
10772
10773   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10774     as_bad (_("trap exception not supported at ISA 1"));
10775
10776   /* If the selected architecture includes support for ASEs, enable
10777      generation of code for them.  */
10778   if (mips_opts.mips16 == -1)
10779     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10780   if (mips_opts.ase_mips3d == -1)
10781     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10782   if (mips_opts.ase_mdmx == -1)
10783     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10784
10785   file_mips_isa = mips_opts.isa;
10786   file_ase_mips16 = mips_opts.mips16;
10787   file_ase_mips3d = mips_opts.ase_mips3d;
10788   file_ase_mdmx = mips_opts.ase_mdmx;
10789   mips_opts.gp32 = file_mips_gp32;
10790   mips_opts.fp32 = file_mips_fp32;
10791
10792   if (mips_flag_mdebug < 0)
10793     {
10794 #ifdef OBJ_MAYBE_ECOFF
10795       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10796         mips_flag_mdebug = 1;
10797       else
10798 #endif /* OBJ_MAYBE_ECOFF */
10799         mips_flag_mdebug = 0;
10800     }
10801 }
10802 \f
10803 void
10804 mips_init_after_args ()
10805 {
10806   /* initialize opcodes */
10807   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10808   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10809 }
10810
10811 long
10812 md_pcrel_from (fixP)
10813      fixS *fixP;
10814 {
10815   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10816       && fixP->fx_addsy != (symbolS *) NULL
10817       && ! S_IS_DEFINED (fixP->fx_addsy))
10818     return 4;
10819
10820   /* Return the address of the delay slot.  */
10821   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10822 }
10823
10824 /* This is called before the symbol table is processed.  In order to
10825    work with gcc when using mips-tfile, we must keep all local labels.
10826    However, in other cases, we want to discard them.  If we were
10827    called with -g, but we didn't see any debugging information, it may
10828    mean that gcc is smuggling debugging information through to
10829    mips-tfile, in which case we must generate all local labels.  */
10830
10831 void
10832 mips_frob_file_before_adjust ()
10833 {
10834 #ifndef NO_ECOFF_DEBUGGING
10835   if (ECOFF_DEBUGGING
10836       && mips_debug != 0
10837       && ! ecoff_debugging_seen)
10838     flag_keep_locals = 1;
10839 #endif
10840 }
10841
10842 /* Sort any unmatched HI16_S relocs so that they immediately precede
10843    the corresponding LO reloc.  This is called before md_apply_fix3 and
10844    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10845    explicit use of the %hi modifier.  */
10846
10847 void
10848 mips_frob_file ()
10849 {
10850   struct mips_hi_fixup *l;
10851
10852   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10853     {
10854       segment_info_type *seginfo;
10855       int pass;
10856
10857       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10858
10859       /* Check quickly whether the next fixup happens to be a matching
10860          %lo.  */
10861       if (l->fixp->fx_next != NULL
10862           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10863           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10864           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10865         continue;
10866
10867       /* Look through the fixups for this segment for a matching %lo.
10868          When we find one, move the %hi just in front of it.  We do
10869          this in two passes.  In the first pass, we try to find a
10870          unique %lo.  In the second pass, we permit multiple %hi
10871          relocs for a single %lo (this is a GNU extension).  */
10872       seginfo = seg_info (l->seg);
10873       for (pass = 0; pass < 2; pass++)
10874         {
10875           fixS *f, *prev;
10876
10877           prev = NULL;
10878           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10879             {
10880               /* Check whether this is a %lo fixup which matches l->fixp.  */
10881               if (f->fx_r_type == BFD_RELOC_LO16
10882                   && f->fx_addsy == l->fixp->fx_addsy
10883                   && f->fx_offset == l->fixp->fx_offset
10884                   && (pass == 1
10885                       || prev == NULL
10886                       || prev->fx_r_type != BFD_RELOC_HI16_S
10887                       || prev->fx_addsy != f->fx_addsy
10888                       || prev->fx_offset !=  f->fx_offset))
10889                 {
10890                   fixS **pf;
10891
10892                   /* Move l->fixp before f.  */
10893                   for (pf = &seginfo->fix_root;
10894                        *pf != l->fixp;
10895                        pf = &(*pf)->fx_next)
10896                     assert (*pf != NULL);
10897
10898                   *pf = l->fixp->fx_next;
10899
10900                   l->fixp->fx_next = f;
10901                   if (prev == NULL)
10902                     seginfo->fix_root = l->fixp;
10903                   else
10904                     prev->fx_next = l->fixp;
10905
10906                   break;
10907                 }
10908
10909               prev = f;
10910             }
10911
10912           if (f != NULL)
10913             break;
10914
10915 #if 0 /* GCC code motion plus incomplete dead code elimination
10916          can leave a %hi without a %lo.  */
10917           if (pass == 1)
10918             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10919                            _("Unmatched %%hi reloc"));
10920 #endif
10921         }
10922     }
10923 }
10924
10925 /* When generating embedded PIC code we need to use a special
10926    relocation to represent the difference of two symbols in the .text
10927    section (switch tables use a difference of this sort).  See
10928    include/coff/mips.h for details.  This macro checks whether this
10929    fixup requires the special reloc.  */
10930 #define SWITCH_TABLE(fixp) \
10931   ((fixp)->fx_r_type == BFD_RELOC_32 \
10932    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10933    && (fixp)->fx_addsy != NULL \
10934    && (fixp)->fx_subsy != NULL \
10935    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10936    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10937
10938 /* When generating embedded PIC code we must keep all PC relative
10939    relocations, in case the linker has to relax a call.  We also need
10940    to keep relocations for switch table entries.
10941
10942    We may have combined relocations without symbols in the N32/N64 ABI.
10943    We have to prevent gas from dropping them.  */
10944
10945 int
10946 mips_force_relocation (fixp)
10947      fixS *fixp;
10948 {
10949   if (generic_force_reloc (fixp))
10950     return 1;
10951
10952   if (HAVE_NEWABI
10953       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10954       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10955           || fixp->fx_r_type == BFD_RELOC_HI16_S
10956           || fixp->fx_r_type == BFD_RELOC_LO16))
10957     return 1;
10958
10959   return (mips_pic == EMBEDDED_PIC
10960           && (fixp->fx_pcrel
10961               || SWITCH_TABLE (fixp)
10962               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10963               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10964 }
10965
10966 #ifdef OBJ_ELF
10967 static int
10968 mips_need_elf_addend_fixup (fixP)
10969      fixS *fixP;
10970 {
10971   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10972     return 1;
10973   if (mips_pic == EMBEDDED_PIC
10974       && S_IS_WEAK (fixP->fx_addsy))
10975     return 1;
10976   if (mips_pic != EMBEDDED_PIC
10977       && (S_IS_WEAK (fixP->fx_addsy)
10978           || S_IS_EXTERNAL (fixP->fx_addsy))
10979       && !S_IS_COMMON (fixP->fx_addsy))
10980     return 1;
10981   if (symbol_used_in_reloc_p (fixP->fx_addsy)
10982       && (((bfd_get_section_flags (stdoutput,
10983                                    S_GET_SEGMENT (fixP->fx_addsy))
10984             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
10985           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10986                        ".gnu.linkonce",
10987                        sizeof (".gnu.linkonce") - 1)))
10988     return 1;
10989   return 0;
10990 }
10991 #endif
10992
10993 /* Apply a fixup to the object file.  */
10994
10995 void
10996 md_apply_fix3 (fixP, valP, seg)
10997      fixS *fixP;
10998      valueT *valP;
10999      segT seg ATTRIBUTE_UNUSED;
11000 {
11001   bfd_byte *buf;
11002   long insn;
11003   valueT value;
11004   static int previous_fx_r_type = 0;
11005
11006   /* FIXME: Maybe just return for all reloc types not listed below?
11007      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11008   if (fixP->fx_r_type == BFD_RELOC_8)
11009       return;
11010
11011   assert (fixP->fx_size == 4
11012           || fixP->fx_r_type == BFD_RELOC_16
11013           || fixP->fx_r_type == BFD_RELOC_32
11014           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11015           || fixP->fx_r_type == BFD_RELOC_HI16_S
11016           || fixP->fx_r_type == BFD_RELOC_LO16
11017           || fixP->fx_r_type == BFD_RELOC_GPREL16
11018           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11019           || fixP->fx_r_type == BFD_RELOC_GPREL32
11020           || fixP->fx_r_type == BFD_RELOC_64
11021           || fixP->fx_r_type == BFD_RELOC_CTOR
11022           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11023           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11024           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11025           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11026           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11027           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11028           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11029           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11030           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11031
11032   value = *valP;
11033
11034   /* If we aren't adjusting this fixup to be against the section
11035      symbol, we need to adjust the value.  */
11036 #ifdef OBJ_ELF
11037   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11038     {
11039       if (mips_need_elf_addend_fixup (fixP))
11040         {
11041           reloc_howto_type *howto;
11042           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11043
11044           value -= symval;
11045
11046           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11047           if (value != 0 && howto && howto->partial_inplace
11048               && (! fixP->fx_pcrel || howto->pcrel_offset))
11049             {
11050               /* In this case, the bfd_install_relocation routine will
11051                  incorrectly add the symbol value back in.  We just want
11052                  the addend to appear in the object file.
11053
11054                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11055                  generated for code like
11056
11057                         globl g1 .text
11058                         .text
11059                         .space 20
11060                  g1:
11061                  x:
11062                         bal g1
11063                */
11064               value -= symval;
11065
11066               /* Make sure the addend is still non-zero.  If it became zero
11067                  after the last operation, set it to a spurious value and
11068                  subtract the same value from the object file's contents.  */
11069               if (value == 0)
11070                 {
11071                   value = 8;
11072
11073                   /* The in-place addends for LO16 relocations are signed;
11074                      leave the matching HI16 in-place addends as zero.  */
11075                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11076                     {
11077                       bfd_vma contents, mask, field;
11078
11079                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11080                                                + fixP->fx_where,
11081                                                fixP->fx_size * 8,
11082                                                target_big_endian);
11083
11084                       /* MASK has bits set where the relocation should go.
11085                          FIELD is -value, shifted into the appropriate place
11086                          for this relocation.  */
11087                       mask = 1 << (howto->bitsize - 1);
11088                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11089                       field = (-value >> howto->rightshift) << howto->bitpos;
11090
11091                       bfd_put_bits ((field & mask) | (contents & ~mask),
11092                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11093                                     fixP->fx_size * 8,
11094                                     target_big_endian);
11095                     }
11096                 }
11097             }
11098         }
11099
11100       /* This code was generated using trial and error and so is
11101          fragile and not trustworthy.  If you change it, you should
11102          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11103          they still pass.  */
11104       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11105         {
11106           value += fixP->fx_frag->fr_address + fixP->fx_where;
11107
11108           /* BFD's REL handling, for MIPS, is _very_ weird.
11109              This gives the right results, but it can't possibly
11110              be the way things are supposed to work.  */
11111           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11112               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11113             value += fixP->fx_frag->fr_address + fixP->fx_where;
11114         }
11115     }
11116 #endif
11117
11118   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11119
11120   /* We are not done if this is a composite relocation to set up gp.  */
11121   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11122       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11123            || (fixP->fx_r_type == BFD_RELOC_64
11124                && (previous_fx_r_type == BFD_RELOC_GPREL32
11125                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11126            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11127                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11128                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11129     fixP->fx_done = 1;
11130   previous_fx_r_type = fixP->fx_r_type;
11131
11132   switch (fixP->fx_r_type)
11133     {
11134     case BFD_RELOC_MIPS_JMP:
11135     case BFD_RELOC_MIPS_SHIFT5:
11136     case BFD_RELOC_MIPS_SHIFT6:
11137     case BFD_RELOC_MIPS_GOT_DISP:
11138     case BFD_RELOC_MIPS_GOT_PAGE:
11139     case BFD_RELOC_MIPS_GOT_OFST:
11140     case BFD_RELOC_MIPS_SUB:
11141     case BFD_RELOC_MIPS_INSERT_A:
11142     case BFD_RELOC_MIPS_INSERT_B:
11143     case BFD_RELOC_MIPS_DELETE:
11144     case BFD_RELOC_MIPS_HIGHEST:
11145     case BFD_RELOC_MIPS_HIGHER:
11146     case BFD_RELOC_MIPS_SCN_DISP:
11147     case BFD_RELOC_MIPS_REL16:
11148     case BFD_RELOC_MIPS_RELGOT:
11149     case BFD_RELOC_MIPS_JALR:
11150     case BFD_RELOC_HI16:
11151     case BFD_RELOC_HI16_S:
11152     case BFD_RELOC_GPREL16:
11153     case BFD_RELOC_MIPS_LITERAL:
11154     case BFD_RELOC_MIPS_CALL16:
11155     case BFD_RELOC_MIPS_GOT16:
11156     case BFD_RELOC_GPREL32:
11157     case BFD_RELOC_MIPS_GOT_HI16:
11158     case BFD_RELOC_MIPS_GOT_LO16:
11159     case BFD_RELOC_MIPS_CALL_HI16:
11160     case BFD_RELOC_MIPS_CALL_LO16:
11161     case BFD_RELOC_MIPS16_GPREL:
11162       if (fixP->fx_pcrel)
11163         as_bad_where (fixP->fx_file, fixP->fx_line,
11164                       _("Invalid PC relative reloc"));
11165       /* Nothing needed to do. The value comes from the reloc entry */
11166       break;
11167
11168     case BFD_RELOC_MIPS16_JMP:
11169       /* We currently always generate a reloc against a symbol, which
11170          means that we don't want an addend even if the symbol is
11171          defined.  */
11172       fixP->fx_addnumber = 0;
11173       break;
11174
11175     case BFD_RELOC_PCREL_HI16_S:
11176       /* The addend for this is tricky if it is internal, so we just
11177          do everything here rather than in bfd_install_relocation.  */
11178       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11179           && !fixP->fx_done
11180           && value != 0)
11181         break;
11182       if (fixP->fx_addsy
11183           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11184         {
11185           /* For an external symbol adjust by the address to make it
11186              pcrel_offset.  We use the address of the RELLO reloc
11187              which follows this one.  */
11188           value += (fixP->fx_next->fx_frag->fr_address
11189                     + fixP->fx_next->fx_where);
11190         }
11191       value = ((value + 0x8000) >> 16) & 0xffff;
11192       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11193       if (target_big_endian)
11194         buf += 2;
11195       md_number_to_chars ((char *) buf, value, 2);
11196       break;
11197
11198     case BFD_RELOC_PCREL_LO16:
11199       /* The addend for this is tricky if it is internal, so we just
11200          do everything here rather than in bfd_install_relocation.  */
11201       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11202           && !fixP->fx_done
11203           && value != 0)
11204         break;
11205       if (fixP->fx_addsy
11206           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11207         value += fixP->fx_frag->fr_address + fixP->fx_where;
11208       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11209       if (target_big_endian)
11210         buf += 2;
11211       md_number_to_chars ((char *) buf, value, 2);
11212       break;
11213
11214     case BFD_RELOC_64:
11215       /* This is handled like BFD_RELOC_32, but we output a sign
11216          extended value if we are only 32 bits.  */
11217       if (fixP->fx_done
11218           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11219         {
11220           if (8 <= sizeof (valueT))
11221             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11222                                 value, 8);
11223           else
11224             {
11225               long w1, w2;
11226               long hiv;
11227
11228               w1 = w2 = fixP->fx_where;
11229               if (target_big_endian)
11230                 w1 += 4;
11231               else
11232                 w2 += 4;
11233               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11234               if ((value & 0x80000000) != 0)
11235                 hiv = 0xffffffff;
11236               else
11237                 hiv = 0;
11238               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11239             }
11240         }
11241       break;
11242
11243     case BFD_RELOC_RVA:
11244     case BFD_RELOC_32:
11245       /* If we are deleting this reloc entry, we must fill in the
11246          value now.  This can happen if we have a .word which is not
11247          resolved when it appears but is later defined.  We also need
11248          to fill in the value if this is an embedded PIC switch table
11249          entry.  */
11250       if (fixP->fx_done
11251           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11252         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11253                             value, 4);
11254       break;
11255
11256     case BFD_RELOC_16:
11257       /* If we are deleting this reloc entry, we must fill in the
11258          value now.  */
11259       assert (fixP->fx_size == 2);
11260       if (fixP->fx_done)
11261         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11262                             value, 2);
11263       break;
11264
11265     case BFD_RELOC_LO16:
11266       /* When handling an embedded PIC switch statement, we can wind
11267          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11268       if (fixP->fx_done)
11269         {
11270           if (value + 0x8000 > 0xffff)
11271             as_bad_where (fixP->fx_file, fixP->fx_line,
11272                           _("relocation overflow"));
11273           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11274           if (target_big_endian)
11275             buf += 2;
11276           md_number_to_chars ((char *) buf, value, 2);
11277         }
11278       break;
11279
11280     case BFD_RELOC_16_PCREL_S2:
11281       if ((value & 0x3) != 0)
11282         as_bad_where (fixP->fx_file, fixP->fx_line,
11283                       _("Branch to odd address (%lx)"), (long) value);
11284
11285       /*
11286        * We need to save the bits in the instruction since fixup_segment()
11287        * might be deleting the relocation entry (i.e., a branch within
11288        * the current segment).
11289        */
11290       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11291         break;
11292       /* If 'value' is zero, the remaining reloc code won't actually
11293          do the store, so it must be done here.  This is probably
11294          a bug somewhere.  */
11295       if (!fixP->fx_done
11296           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11297               || fixP->fx_addsy == NULL                 /* ??? */
11298               || ! S_IS_DEFINED (fixP->fx_addsy)))
11299         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11300
11301       value = (offsetT) value >> 2;
11302
11303       /* update old instruction data */
11304       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11305       if (target_big_endian)
11306         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11307       else
11308         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11309
11310       if (value + 0x8000 <= 0xffff)
11311         insn |= value & 0xffff;
11312       else
11313         {
11314           /* The branch offset is too large.  If this is an
11315              unconditional branch, and we are not generating PIC code,
11316              we can convert it to an absolute jump instruction.  */
11317           if (mips_pic == NO_PIC
11318               && fixP->fx_done
11319               && fixP->fx_frag->fr_address >= text_section->vma
11320               && (fixP->fx_frag->fr_address
11321                   < text_section->vma + text_section->_raw_size)
11322               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11323                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11324                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11325             {
11326               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11327                 insn = 0x0c000000;      /* jal */
11328               else
11329                 insn = 0x08000000;      /* j */
11330               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11331               fixP->fx_done = 0;
11332               fixP->fx_addsy = section_symbol (text_section);
11333               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11334             }
11335           else
11336             {
11337               /* If we got here, we have branch-relaxation disabled,
11338                  and there's nothing we can do to fix this instruction
11339                  without turning it into a longer sequence.  */
11340               as_bad_where (fixP->fx_file, fixP->fx_line,
11341                             _("Branch out of range"));
11342             }
11343         }
11344
11345       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11346       break;
11347
11348     case BFD_RELOC_VTABLE_INHERIT:
11349       fixP->fx_done = 0;
11350       if (fixP->fx_addsy
11351           && !S_IS_DEFINED (fixP->fx_addsy)
11352           && !S_IS_WEAK (fixP->fx_addsy))
11353         S_SET_WEAK (fixP->fx_addsy);
11354       break;
11355
11356     case BFD_RELOC_VTABLE_ENTRY:
11357       fixP->fx_done = 0;
11358       break;
11359
11360     default:
11361       internalError ();
11362     }
11363 }
11364
11365 #if 0
11366 void
11367 printInsn (oc)
11368      unsigned long oc;
11369 {
11370   const struct mips_opcode *p;
11371   int treg, sreg, dreg, shamt;
11372   short imm;
11373   const char *args;
11374   int i;
11375
11376   for (i = 0; i < NUMOPCODES; ++i)
11377     {
11378       p = &mips_opcodes[i];
11379       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11380         {
11381           printf ("%08lx %s\t", oc, p->name);
11382           treg = (oc >> 16) & 0x1f;
11383           sreg = (oc >> 21) & 0x1f;
11384           dreg = (oc >> 11) & 0x1f;
11385           shamt = (oc >> 6) & 0x1f;
11386           imm = oc;
11387           for (args = p->args;; ++args)
11388             {
11389               switch (*args)
11390                 {
11391                 case '\0':
11392                   printf ("\n");
11393                   break;
11394
11395                 case ',':
11396                 case '(':
11397                 case ')':
11398                   printf ("%c", *args);
11399                   continue;
11400
11401                 case 'r':
11402                   assert (treg == sreg);
11403                   printf ("$%d,$%d", treg, sreg);
11404                   continue;
11405
11406                 case 'd':
11407                 case 'G':
11408                   printf ("$%d", dreg);
11409                   continue;
11410
11411                 case 't':
11412                 case 'E':
11413                   printf ("$%d", treg);
11414                   continue;
11415
11416                 case 'k':
11417                   printf ("0x%x", treg);
11418                   continue;
11419
11420                 case 'b':
11421                 case 's':
11422                   printf ("$%d", sreg);
11423                   continue;
11424
11425                 case 'a':
11426                   printf ("0x%08lx", oc & 0x1ffffff);
11427                   continue;
11428
11429                 case 'i':
11430                 case 'j':
11431                 case 'o':
11432                 case 'u':
11433                   printf ("%d", imm);
11434                   continue;
11435
11436                 case '<':
11437                 case '>':
11438                   printf ("$%d", shamt);
11439                   continue;
11440
11441                 default:
11442                   internalError ();
11443                 }
11444               break;
11445             }
11446           return;
11447         }
11448     }
11449   printf (_("%08lx  UNDEFINED\n"), oc);
11450 }
11451 #endif
11452
11453 static symbolS *
11454 get_symbol ()
11455 {
11456   int c;
11457   char *name;
11458   symbolS *p;
11459
11460   name = input_line_pointer;
11461   c = get_symbol_end ();
11462   p = (symbolS *) symbol_find_or_make (name);
11463   *input_line_pointer = c;
11464   return p;
11465 }
11466
11467 /* Align the current frag to a given power of two.  The MIPS assembler
11468    also automatically adjusts any preceding label.  */
11469
11470 static void
11471 mips_align (to, fill, label)
11472      int to;
11473      int fill;
11474      symbolS *label;
11475 {
11476   mips_emit_delays (FALSE);
11477   frag_align (to, fill, 0);
11478   record_alignment (now_seg, to);
11479   if (label != NULL)
11480     {
11481       assert (S_GET_SEGMENT (label) == now_seg);
11482       symbol_set_frag (label, frag_now);
11483       S_SET_VALUE (label, (valueT) frag_now_fix ());
11484     }
11485 }
11486
11487 /* Align to a given power of two.  .align 0 turns off the automatic
11488    alignment used by the data creating pseudo-ops.  */
11489
11490 static void
11491 s_align (x)
11492      int x ATTRIBUTE_UNUSED;
11493 {
11494   register int temp;
11495   register long temp_fill;
11496   long max_alignment = 15;
11497
11498   /*
11499
11500     o  Note that the assembler pulls down any immediately preceeding label
11501        to the aligned address.
11502     o  It's not documented but auto alignment is reinstated by
11503        a .align pseudo instruction.
11504     o  Note also that after auto alignment is turned off the mips assembler
11505        issues an error on attempt to assemble an improperly aligned data item.
11506        We don't.
11507
11508     */
11509
11510   temp = get_absolute_expression ();
11511   if (temp > max_alignment)
11512     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11513   else if (temp < 0)
11514     {
11515       as_warn (_("Alignment negative: 0 assumed."));
11516       temp = 0;
11517     }
11518   if (*input_line_pointer == ',')
11519     {
11520       ++input_line_pointer;
11521       temp_fill = get_absolute_expression ();
11522     }
11523   else
11524     temp_fill = 0;
11525   if (temp)
11526     {
11527       auto_align = 1;
11528       mips_align (temp, (int) temp_fill,
11529                   insn_labels != NULL ? insn_labels->label : NULL);
11530     }
11531   else
11532     {
11533       auto_align = 0;
11534     }
11535
11536   demand_empty_rest_of_line ();
11537 }
11538
11539 void
11540 mips_flush_pending_output ()
11541 {
11542   mips_emit_delays (FALSE);
11543   mips_clear_insn_labels ();
11544 }
11545
11546 static void
11547 s_change_sec (sec)
11548      int sec;
11549 {
11550   segT seg;
11551
11552   /* When generating embedded PIC code, we only use the .text, .lit8,
11553      .sdata and .sbss sections.  We change the .data and .rdata
11554      pseudo-ops to use .sdata.  */
11555   if (mips_pic == EMBEDDED_PIC
11556       && (sec == 'd' || sec == 'r'))
11557     sec = 's';
11558
11559 #ifdef OBJ_ELF
11560   /* The ELF backend needs to know that we are changing sections, so
11561      that .previous works correctly.  We could do something like check
11562      for an obj_section_change_hook macro, but that might be confusing
11563      as it would not be appropriate to use it in the section changing
11564      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11565      This should be cleaner, somehow.  */
11566   obj_elf_section_change_hook ();
11567 #endif
11568
11569   mips_emit_delays (FALSE);
11570   switch (sec)
11571     {
11572     case 't':
11573       s_text (0);
11574       break;
11575     case 'd':
11576       s_data (0);
11577       break;
11578     case 'b':
11579       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11580       demand_empty_rest_of_line ();
11581       break;
11582
11583     case 'r':
11584       if (USE_GLOBAL_POINTER_OPT)
11585         {
11586           seg = subseg_new (RDATA_SECTION_NAME,
11587                             (subsegT) get_absolute_expression ());
11588           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11589             {
11590               bfd_set_section_flags (stdoutput, seg,
11591                                      (SEC_ALLOC
11592                                       | SEC_LOAD
11593                                       | SEC_READONLY
11594                                       | SEC_RELOC
11595                                       | SEC_DATA));
11596               if (strcmp (TARGET_OS, "elf") != 0)
11597                 record_alignment (seg, 4);
11598             }
11599           demand_empty_rest_of_line ();
11600         }
11601       else
11602         {
11603           as_bad (_("No read only data section in this object file format"));
11604           demand_empty_rest_of_line ();
11605           return;
11606         }
11607       break;
11608
11609     case 's':
11610       if (USE_GLOBAL_POINTER_OPT)
11611         {
11612           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11613           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11614             {
11615               bfd_set_section_flags (stdoutput, seg,
11616                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11617                                      | SEC_DATA);
11618               if (strcmp (TARGET_OS, "elf") != 0)
11619                 record_alignment (seg, 4);
11620             }
11621           demand_empty_rest_of_line ();
11622           break;
11623         }
11624       else
11625         {
11626           as_bad (_("Global pointers not supported; recompile -G 0"));
11627           demand_empty_rest_of_line ();
11628           return;
11629         }
11630     }
11631
11632   auto_align = 1;
11633 }
11634
11635 void
11636 s_change_section (ignore)
11637      int ignore ATTRIBUTE_UNUSED;
11638 {
11639 #ifdef OBJ_ELF
11640   char *section_name;
11641   char c;
11642   char next_c;
11643   int section_type;
11644   int section_flag;
11645   int section_entry_size;
11646   int section_alignment;
11647
11648   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11649     return;
11650
11651   section_name = input_line_pointer;
11652   c = get_symbol_end ();
11653   if (c)
11654     next_c = *(input_line_pointer + 1);
11655
11656   /* Do we have .section Name<,"flags">?  */
11657   if (c != ',' || (c == ',' && next_c == '"'))
11658     {
11659       /* just after name is now '\0'.  */
11660       *input_line_pointer = c;
11661       input_line_pointer = section_name;
11662       obj_elf_section (ignore);
11663       return;
11664     }
11665   input_line_pointer++;
11666
11667   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11668   if (c == ',')
11669     section_type = get_absolute_expression ();
11670   else
11671     section_type = 0;
11672   if (*input_line_pointer++ == ',')
11673     section_flag = get_absolute_expression ();
11674   else
11675     section_flag = 0;
11676   if (*input_line_pointer++ == ',')
11677     section_entry_size = get_absolute_expression ();
11678   else
11679     section_entry_size = 0;
11680   if (*input_line_pointer++ == ',')
11681     section_alignment = get_absolute_expression ();
11682   else
11683     section_alignment = 0;
11684
11685   section_name = xstrdup (section_name);
11686
11687   obj_elf_change_section (section_name, section_type, section_flag,
11688                           section_entry_size, 0, 0, 0);
11689
11690   if (now_seg->name != section_name)
11691     free (section_name);
11692 #endif /* OBJ_ELF */
11693 }
11694
11695 void
11696 mips_enable_auto_align ()
11697 {
11698   auto_align = 1;
11699 }
11700
11701 static void
11702 s_cons (log_size)
11703      int log_size;
11704 {
11705   symbolS *label;
11706
11707   label = insn_labels != NULL ? insn_labels->label : NULL;
11708   mips_emit_delays (FALSE);
11709   if (log_size > 0 && auto_align)
11710     mips_align (log_size, 0, label);
11711   mips_clear_insn_labels ();
11712   cons (1 << log_size);
11713 }
11714
11715 static void
11716 s_float_cons (type)
11717      int type;
11718 {
11719   symbolS *label;
11720
11721   label = insn_labels != NULL ? insn_labels->label : NULL;
11722
11723   mips_emit_delays (FALSE);
11724
11725   if (auto_align)
11726     {
11727       if (type == 'd')
11728         mips_align (3, 0, label);
11729       else
11730         mips_align (2, 0, label);
11731     }
11732
11733   mips_clear_insn_labels ();
11734
11735   float_cons (type);
11736 }
11737
11738 /* Handle .globl.  We need to override it because on Irix 5 you are
11739    permitted to say
11740        .globl foo .text
11741    where foo is an undefined symbol, to mean that foo should be
11742    considered to be the address of a function.  */
11743
11744 static void
11745 s_mips_globl (x)
11746      int x ATTRIBUTE_UNUSED;
11747 {
11748   char *name;
11749   int c;
11750   symbolS *symbolP;
11751   flagword flag;
11752
11753   name = input_line_pointer;
11754   c = get_symbol_end ();
11755   symbolP = symbol_find_or_make (name);
11756   *input_line_pointer = c;
11757   SKIP_WHITESPACE ();
11758
11759   /* On Irix 5, every global symbol that is not explicitly labelled as
11760      being a function is apparently labelled as being an object.  */
11761   flag = BSF_OBJECT;
11762
11763   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11764     {
11765       char *secname;
11766       asection *sec;
11767
11768       secname = input_line_pointer;
11769       c = get_symbol_end ();
11770       sec = bfd_get_section_by_name (stdoutput, secname);
11771       if (sec == NULL)
11772         as_bad (_("%s: no such section"), secname);
11773       *input_line_pointer = c;
11774
11775       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11776         flag = BSF_FUNCTION;
11777     }
11778
11779   symbol_get_bfdsym (symbolP)->flags |= flag;
11780
11781   S_SET_EXTERNAL (symbolP);
11782   demand_empty_rest_of_line ();
11783 }
11784
11785 static void
11786 s_option (x)
11787      int x ATTRIBUTE_UNUSED;
11788 {
11789   char *opt;
11790   char c;
11791
11792   opt = input_line_pointer;
11793   c = get_symbol_end ();
11794
11795   if (*opt == 'O')
11796     {
11797       /* FIXME: What does this mean?  */
11798     }
11799   else if (strncmp (opt, "pic", 3) == 0)
11800     {
11801       int i;
11802
11803       i = atoi (opt + 3);
11804       if (i == 0)
11805         mips_pic = NO_PIC;
11806       else if (i == 2)
11807         mips_pic = SVR4_PIC;
11808       else
11809         as_bad (_(".option pic%d not supported"), i);
11810
11811       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11812         {
11813           if (g_switch_seen && g_switch_value != 0)
11814             as_warn (_("-G may not be used with SVR4 PIC code"));
11815           g_switch_value = 0;
11816           bfd_set_gp_size (stdoutput, 0);
11817         }
11818     }
11819   else
11820     as_warn (_("Unrecognized option \"%s\""), opt);
11821
11822   *input_line_pointer = c;
11823   demand_empty_rest_of_line ();
11824 }
11825
11826 /* This structure is used to hold a stack of .set values.  */
11827
11828 struct mips_option_stack
11829 {
11830   struct mips_option_stack *next;
11831   struct mips_set_options options;
11832 };
11833
11834 static struct mips_option_stack *mips_opts_stack;
11835
11836 /* Handle the .set pseudo-op.  */
11837
11838 static void
11839 s_mipsset (x)
11840      int x ATTRIBUTE_UNUSED;
11841 {
11842   char *name = input_line_pointer, ch;
11843
11844   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11845     ++input_line_pointer;
11846   ch = *input_line_pointer;
11847   *input_line_pointer = '\0';
11848
11849   if (strcmp (name, "reorder") == 0)
11850     {
11851       if (mips_opts.noreorder && prev_nop_frag != NULL)
11852         {
11853           /* If we still have pending nops, we can discard them.  The
11854              usual nop handling will insert any that are still
11855              needed.  */
11856           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11857                                     * (mips_opts.mips16 ? 2 : 4));
11858           prev_nop_frag = NULL;
11859         }
11860       mips_opts.noreorder = 0;
11861     }
11862   else if (strcmp (name, "noreorder") == 0)
11863     {
11864       mips_emit_delays (TRUE);
11865       mips_opts.noreorder = 1;
11866       mips_any_noreorder = 1;
11867     }
11868   else if (strcmp (name, "at") == 0)
11869     {
11870       mips_opts.noat = 0;
11871     }
11872   else if (strcmp (name, "noat") == 0)
11873     {
11874       mips_opts.noat = 1;
11875     }
11876   else if (strcmp (name, "macro") == 0)
11877     {
11878       mips_opts.warn_about_macros = 0;
11879     }
11880   else if (strcmp (name, "nomacro") == 0)
11881     {
11882       if (mips_opts.noreorder == 0)
11883         as_bad (_("`noreorder' must be set before `nomacro'"));
11884       mips_opts.warn_about_macros = 1;
11885     }
11886   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11887     {
11888       mips_opts.nomove = 0;
11889     }
11890   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11891     {
11892       mips_opts.nomove = 1;
11893     }
11894   else if (strcmp (name, "bopt") == 0)
11895     {
11896       mips_opts.nobopt = 0;
11897     }
11898   else if (strcmp (name, "nobopt") == 0)
11899     {
11900       mips_opts.nobopt = 1;
11901     }
11902   else if (strcmp (name, "mips16") == 0
11903            || strcmp (name, "MIPS-16") == 0)
11904     mips_opts.mips16 = 1;
11905   else if (strcmp (name, "nomips16") == 0
11906            || strcmp (name, "noMIPS-16") == 0)
11907     mips_opts.mips16 = 0;
11908   else if (strcmp (name, "mips3d") == 0)
11909     mips_opts.ase_mips3d = 1;
11910   else if (strcmp (name, "nomips3d") == 0)
11911     mips_opts.ase_mips3d = 0;
11912   else if (strcmp (name, "mdmx") == 0)
11913     mips_opts.ase_mdmx = 1;
11914   else if (strcmp (name, "nomdmx") == 0)
11915     mips_opts.ase_mdmx = 0;
11916   else if (strncmp (name, "mips", 4) == 0)
11917     {
11918       int reset = 0;
11919
11920       /* Permit the user to change the ISA on the fly.  Needless to
11921          say, misuse can cause serious problems.  */
11922       if (strcmp (name, "mips0") == 0)
11923         {
11924           reset = 1;
11925           mips_opts.isa = file_mips_isa;
11926         }
11927       else if (strcmp (name, "mips1") == 0)
11928         mips_opts.isa = ISA_MIPS1;
11929       else if (strcmp (name, "mips2") == 0)
11930         mips_opts.isa = ISA_MIPS2;
11931       else if (strcmp (name, "mips3") == 0)
11932         mips_opts.isa = ISA_MIPS3;
11933       else if (strcmp (name, "mips4") == 0)
11934         mips_opts.isa = ISA_MIPS4;
11935       else if (strcmp (name, "mips5") == 0)
11936         mips_opts.isa = ISA_MIPS5;
11937       else if (strcmp (name, "mips32") == 0)
11938         mips_opts.isa = ISA_MIPS32;
11939       else if (strcmp (name, "mips32r2") == 0)
11940         mips_opts.isa = ISA_MIPS32R2;
11941       else if (strcmp (name, "mips64") == 0)
11942         mips_opts.isa = ISA_MIPS64;
11943       else
11944         as_bad (_("unknown ISA level %s"), name + 4);
11945
11946       switch (mips_opts.isa)
11947         {
11948         case  0:
11949           break;
11950         case ISA_MIPS1:
11951         case ISA_MIPS2:
11952         case ISA_MIPS32:
11953         case ISA_MIPS32R2:
11954           mips_opts.gp32 = 1;
11955           mips_opts.fp32 = 1;
11956           break;
11957         case ISA_MIPS3:
11958         case ISA_MIPS4:
11959         case ISA_MIPS5:
11960         case ISA_MIPS64:
11961           mips_opts.gp32 = 0;
11962           mips_opts.fp32 = 0;
11963           break;
11964         default:
11965           as_bad (_("unknown ISA level %s"), name + 4);
11966           break;
11967         }
11968       if (reset)
11969         {
11970           mips_opts.gp32 = file_mips_gp32;
11971           mips_opts.fp32 = file_mips_fp32;
11972         }
11973     }
11974   else if (strcmp (name, "autoextend") == 0)
11975     mips_opts.noautoextend = 0;
11976   else if (strcmp (name, "noautoextend") == 0)
11977     mips_opts.noautoextend = 1;
11978   else if (strcmp (name, "push") == 0)
11979     {
11980       struct mips_option_stack *s;
11981
11982       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11983       s->next = mips_opts_stack;
11984       s->options = mips_opts;
11985       mips_opts_stack = s;
11986     }
11987   else if (strcmp (name, "pop") == 0)
11988     {
11989       struct mips_option_stack *s;
11990
11991       s = mips_opts_stack;
11992       if (s == NULL)
11993         as_bad (_(".set pop with no .set push"));
11994       else
11995         {
11996           /* If we're changing the reorder mode we need to handle
11997              delay slots correctly.  */
11998           if (s->options.noreorder && ! mips_opts.noreorder)
11999             mips_emit_delays (TRUE);
12000           else if (! s->options.noreorder && mips_opts.noreorder)
12001             {
12002               if (prev_nop_frag != NULL)
12003                 {
12004                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12005                                             * (mips_opts.mips16 ? 2 : 4));
12006                   prev_nop_frag = NULL;
12007                 }
12008             }
12009
12010           mips_opts = s->options;
12011           mips_opts_stack = s->next;
12012           free (s);
12013         }
12014     }
12015   else
12016     {
12017       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12018     }
12019   *input_line_pointer = ch;
12020   demand_empty_rest_of_line ();
12021 }
12022
12023 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12024    .option pic2.  It means to generate SVR4 PIC calls.  */
12025
12026 static void
12027 s_abicalls (ignore)
12028      int ignore ATTRIBUTE_UNUSED;
12029 {
12030   mips_pic = SVR4_PIC;
12031   if (USE_GLOBAL_POINTER_OPT)
12032     {
12033       if (g_switch_seen && g_switch_value != 0)
12034         as_warn (_("-G may not be used with SVR4 PIC code"));
12035       g_switch_value = 0;
12036     }
12037   bfd_set_gp_size (stdoutput, 0);
12038   demand_empty_rest_of_line ();
12039 }
12040
12041 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12042    PIC code.  It sets the $gp register for the function based on the
12043    function address, which is in the register named in the argument.
12044    This uses a relocation against _gp_disp, which is handled specially
12045    by the linker.  The result is:
12046         lui     $gp,%hi(_gp_disp)
12047         addiu   $gp,$gp,%lo(_gp_disp)
12048         addu    $gp,$gp,.cpload argument
12049    The .cpload argument is normally $25 == $t9.  */
12050
12051 static void
12052 s_cpload (ignore)
12053      int ignore ATTRIBUTE_UNUSED;
12054 {
12055   expressionS ex;
12056   int icnt = 0;
12057
12058   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12059      .cpload is ignored.  */
12060   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12061     {
12062       s_ignore (0);
12063       return;
12064     }
12065
12066   /* .cpload should be in a .set noreorder section.  */
12067   if (mips_opts.noreorder == 0)
12068     as_warn (_(".cpload not in noreorder section"));
12069
12070   ex.X_op = O_symbol;
12071   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12072   ex.X_op_symbol = NULL;
12073   ex.X_add_number = 0;
12074
12075   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12076   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12077
12078   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12079   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12080                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12081
12082   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12083                mips_gp_register, mips_gp_register, tc_get_register (0));
12084
12085   demand_empty_rest_of_line ();
12086 }
12087
12088 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12089      .cpsetup $reg1, offset|$reg2, label
12090
12091    If offset is given, this results in:
12092      sd         $gp, offset($sp)
12093      lui        $gp, %hi(%neg(%gp_rel(label)))
12094      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12095      daddu      $gp, $gp, $reg1
12096
12097    If $reg2 is given, this results in:
12098      daddu      $reg2, $gp, $0
12099      lui        $gp, %hi(%neg(%gp_rel(label)))
12100      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12101      daddu      $gp, $gp, $reg1
12102    $reg1 is normally $25 == $t9.  */
12103 static void
12104 s_cpsetup (ignore)
12105      int ignore ATTRIBUTE_UNUSED;
12106 {
12107   expressionS ex_off;
12108   expressionS ex_sym;
12109   int reg1;
12110   int icnt = 0;
12111   char *f;
12112
12113   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12114      We also need NewABI support.  */
12115   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12116     {
12117       s_ignore (0);
12118       return;
12119     }
12120
12121   reg1 = tc_get_register (0);
12122   SKIP_WHITESPACE ();
12123   if (*input_line_pointer != ',')
12124     {
12125       as_bad (_("missing argument separator ',' for .cpsetup"));
12126       return;
12127     }
12128   else
12129     ++input_line_pointer;
12130   SKIP_WHITESPACE ();
12131   if (*input_line_pointer == '$')
12132     {
12133       mips_cpreturn_register = tc_get_register (0);
12134       mips_cpreturn_offset = -1;
12135     }
12136   else
12137     {
12138       mips_cpreturn_offset = get_absolute_expression ();
12139       mips_cpreturn_register = -1;
12140     }
12141   SKIP_WHITESPACE ();
12142   if (*input_line_pointer != ',')
12143     {
12144       as_bad (_("missing argument separator ',' for .cpsetup"));
12145       return;
12146     }
12147   else
12148     ++input_line_pointer;
12149   SKIP_WHITESPACE ();
12150   expression (&ex_sym);
12151
12152   if (mips_cpreturn_register == -1)
12153     {
12154       ex_off.X_op = O_constant;
12155       ex_off.X_add_symbol = NULL;
12156       ex_off.X_op_symbol = NULL;
12157       ex_off.X_add_number = mips_cpreturn_offset;
12158
12159       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12160                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12161     }
12162   else
12163     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12164                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12165
12166   /* Ensure there's room for the next two instructions, so that `f'
12167      doesn't end up with an address in the wrong frag.  */
12168   frag_grow (8);
12169   f = frag_more (0);
12170   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12171                (int) BFD_RELOC_GPREL16);
12172   fix_new (frag_now, f - frag_now->fr_literal,
12173            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12174   fix_new (frag_now, f - frag_now->fr_literal,
12175            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12176
12177   f = frag_more (0);
12178   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12179                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12180   fix_new (frag_now, f - frag_now->fr_literal,
12181            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12182   fix_new (frag_now, f - frag_now->fr_literal,
12183            0, NULL, 0, 0, BFD_RELOC_LO16);
12184
12185   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12186                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12187                mips_gp_register, mips_gp_register, reg1);
12188
12189   demand_empty_rest_of_line ();
12190 }
12191
12192 static void
12193 s_cplocal (ignore)
12194      int ignore ATTRIBUTE_UNUSED;
12195 {
12196   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12197    .cplocal is ignored.  */
12198   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12199     {
12200       s_ignore (0);
12201       return;
12202     }
12203
12204   mips_gp_register = tc_get_register (0);
12205   demand_empty_rest_of_line ();
12206 }
12207
12208 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12209    offset from $sp.  The offset is remembered, and after making a PIC
12210    call $gp is restored from that location.  */
12211
12212 static void
12213 s_cprestore (ignore)
12214      int ignore ATTRIBUTE_UNUSED;
12215 {
12216   expressionS ex;
12217   int icnt = 0;
12218
12219   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12220      .cprestore is ignored.  */
12221   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12222     {
12223       s_ignore (0);
12224       return;
12225     }
12226
12227   mips_cprestore_offset = get_absolute_expression ();
12228   mips_cprestore_valid = 1;
12229
12230   ex.X_op = O_constant;
12231   ex.X_add_symbol = NULL;
12232   ex.X_op_symbol = NULL;
12233   ex.X_add_number = mips_cprestore_offset;
12234
12235   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12236                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12237                                 mips_gp_register, SP);
12238
12239   demand_empty_rest_of_line ();
12240 }
12241
12242 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12243    was given in the preceeding .gpsetup, it results in:
12244      ld         $gp, offset($sp)
12245
12246    If a register $reg2 was given there, it results in:
12247      daddiu     $gp, $gp, $reg2
12248  */
12249 static void
12250 s_cpreturn (ignore)
12251      int ignore ATTRIBUTE_UNUSED;
12252 {
12253   expressionS ex;
12254   int icnt = 0;
12255
12256   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12257      We also need NewABI support.  */
12258   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12259     {
12260       s_ignore (0);
12261       return;
12262     }
12263
12264   if (mips_cpreturn_register == -1)
12265     {
12266       ex.X_op = O_constant;
12267       ex.X_add_symbol = NULL;
12268       ex.X_op_symbol = NULL;
12269       ex.X_add_number = mips_cpreturn_offset;
12270
12271       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12272                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12273     }
12274   else
12275     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12276                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12277
12278   demand_empty_rest_of_line ();
12279 }
12280
12281 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12282    code.  It sets the offset to use in gp_rel relocations.  */
12283
12284 static void
12285 s_gpvalue (ignore)
12286      int ignore ATTRIBUTE_UNUSED;
12287 {
12288   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12289      We also need NewABI support.  */
12290   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12291     {
12292       s_ignore (0);
12293       return;
12294     }
12295
12296   mips_gprel_offset = get_absolute_expression ();
12297
12298   demand_empty_rest_of_line ();
12299 }
12300
12301 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12302    code.  It generates a 32 bit GP relative reloc.  */
12303
12304 static void
12305 s_gpword (ignore)
12306      int ignore ATTRIBUTE_UNUSED;
12307 {
12308   symbolS *label;
12309   expressionS ex;
12310   char *p;
12311
12312   /* When not generating PIC code, this is treated as .word.  */
12313   if (mips_pic != SVR4_PIC)
12314     {
12315       s_cons (2);
12316       return;
12317     }
12318
12319   label = insn_labels != NULL ? insn_labels->label : NULL;
12320   mips_emit_delays (TRUE);
12321   if (auto_align)
12322     mips_align (2, 0, label);
12323   mips_clear_insn_labels ();
12324
12325   expression (&ex);
12326
12327   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12328     {
12329       as_bad (_("Unsupported use of .gpword"));
12330       ignore_rest_of_line ();
12331     }
12332
12333   p = frag_more (4);
12334   md_number_to_chars (p, (valueT) 0, 4);
12335   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12336                BFD_RELOC_GPREL32);
12337
12338   demand_empty_rest_of_line ();
12339 }
12340
12341 static void
12342 s_gpdword (ignore)
12343      int ignore ATTRIBUTE_UNUSED;
12344 {
12345   symbolS *label;
12346   expressionS ex;
12347   char *p;
12348
12349   /* When not generating PIC code, this is treated as .dword.  */
12350   if (mips_pic != SVR4_PIC)
12351     {
12352       s_cons (3);
12353       return;
12354     }
12355
12356   label = insn_labels != NULL ? insn_labels->label : NULL;
12357   mips_emit_delays (TRUE);
12358   if (auto_align)
12359     mips_align (3, 0, label);
12360   mips_clear_insn_labels ();
12361
12362   expression (&ex);
12363
12364   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12365     {
12366       as_bad (_("Unsupported use of .gpdword"));
12367       ignore_rest_of_line ();
12368     }
12369
12370   p = frag_more (8);
12371   md_number_to_chars (p, (valueT) 0, 8);
12372   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12373                BFD_RELOC_GPREL32);
12374
12375   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12376   ex.X_op = O_absent;
12377   ex.X_add_symbol = 0;
12378   ex.X_add_number = 0;
12379   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12380                BFD_RELOC_64);
12381
12382   demand_empty_rest_of_line ();
12383 }
12384
12385 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12386    tables in SVR4 PIC code.  */
12387
12388 static void
12389 s_cpadd (ignore)
12390      int ignore ATTRIBUTE_UNUSED;
12391 {
12392   int icnt = 0;
12393   int reg;
12394
12395   /* This is ignored when not generating SVR4 PIC code.  */
12396   if (mips_pic != SVR4_PIC)
12397     {
12398       s_ignore (0);
12399       return;
12400     }
12401
12402   /* Add $gp to the register named as an argument.  */
12403   reg = tc_get_register (0);
12404   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12405                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12406                "d,v,t", reg, reg, mips_gp_register);
12407
12408   demand_empty_rest_of_line ();
12409 }
12410
12411 /* Handle the .insn pseudo-op.  This marks instruction labels in
12412    mips16 mode.  This permits the linker to handle them specially,
12413    such as generating jalx instructions when needed.  We also make
12414    them odd for the duration of the assembly, in order to generate the
12415    right sort of code.  We will make them even in the adjust_symtab
12416    routine, while leaving them marked.  This is convenient for the
12417    debugger and the disassembler.  The linker knows to make them odd
12418    again.  */
12419
12420 static void
12421 s_insn (ignore)
12422      int ignore ATTRIBUTE_UNUSED;
12423 {
12424   mips16_mark_labels ();
12425
12426   demand_empty_rest_of_line ();
12427 }
12428
12429 /* Handle a .stabn directive.  We need these in order to mark a label
12430    as being a mips16 text label correctly.  Sometimes the compiler
12431    will emit a label, followed by a .stabn, and then switch sections.
12432    If the label and .stabn are in mips16 mode, then the label is
12433    really a mips16 text label.  */
12434
12435 static void
12436 s_mips_stab (type)
12437      int type;
12438 {
12439   if (type == 'n')
12440     mips16_mark_labels ();
12441
12442   s_stab (type);
12443 }
12444
12445 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12446  */
12447
12448 static void
12449 s_mips_weakext (ignore)
12450      int ignore ATTRIBUTE_UNUSED;
12451 {
12452   char *name;
12453   int c;
12454   symbolS *symbolP;
12455   expressionS exp;
12456
12457   name = input_line_pointer;
12458   c = get_symbol_end ();
12459   symbolP = symbol_find_or_make (name);
12460   S_SET_WEAK (symbolP);
12461   *input_line_pointer = c;
12462
12463   SKIP_WHITESPACE ();
12464
12465   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12466     {
12467       if (S_IS_DEFINED (symbolP))
12468         {
12469           as_bad ("ignoring attempt to redefine symbol %s",
12470                   S_GET_NAME (symbolP));
12471           ignore_rest_of_line ();
12472           return;
12473         }
12474
12475       if (*input_line_pointer == ',')
12476         {
12477           ++input_line_pointer;
12478           SKIP_WHITESPACE ();
12479         }
12480
12481       expression (&exp);
12482       if (exp.X_op != O_symbol)
12483         {
12484           as_bad ("bad .weakext directive");
12485           ignore_rest_of_line ();
12486           return;
12487         }
12488       symbol_set_value_expression (symbolP, &exp);
12489     }
12490
12491   demand_empty_rest_of_line ();
12492 }
12493
12494 /* Parse a register string into a number.  Called from the ECOFF code
12495    to parse .frame.  The argument is non-zero if this is the frame
12496    register, so that we can record it in mips_frame_reg.  */
12497
12498 int
12499 tc_get_register (frame)
12500      int frame;
12501 {
12502   int reg;
12503
12504   SKIP_WHITESPACE ();
12505   if (*input_line_pointer++ != '$')
12506     {
12507       as_warn (_("expected `$'"));
12508       reg = ZERO;
12509     }
12510   else if (ISDIGIT (*input_line_pointer))
12511     {
12512       reg = get_absolute_expression ();
12513       if (reg < 0 || reg >= 32)
12514         {
12515           as_warn (_("Bad register number"));
12516           reg = ZERO;
12517         }
12518     }
12519   else
12520     {
12521       if (strncmp (input_line_pointer, "ra", 2) == 0)
12522         {
12523           reg = RA;
12524           input_line_pointer += 2;
12525         }
12526       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12527         {
12528           reg = FP;
12529           input_line_pointer += 2;
12530         }
12531       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12532         {
12533           reg = SP;
12534           input_line_pointer += 2;
12535         }
12536       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12537         {
12538           reg = GP;
12539           input_line_pointer += 2;
12540         }
12541       else if (strncmp (input_line_pointer, "at", 2) == 0)
12542         {
12543           reg = AT;
12544           input_line_pointer += 2;
12545         }
12546       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12547         {
12548           reg = KT0;
12549           input_line_pointer += 3;
12550         }
12551       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12552         {
12553           reg = KT1;
12554           input_line_pointer += 3;
12555         }
12556       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12557         {
12558           reg = ZERO;
12559           input_line_pointer += 4;
12560         }
12561       else
12562         {
12563           as_warn (_("Unrecognized register name"));
12564           reg = ZERO;
12565           while (ISALNUM(*input_line_pointer))
12566            input_line_pointer++;
12567         }
12568     }
12569   if (frame)
12570     {
12571       mips_frame_reg = reg != 0 ? reg : SP;
12572       mips_frame_reg_valid = 1;
12573       mips_cprestore_valid = 0;
12574     }
12575   return reg;
12576 }
12577
12578 valueT
12579 md_section_align (seg, addr)
12580      asection *seg;
12581      valueT addr;
12582 {
12583   int align = bfd_get_section_alignment (stdoutput, seg);
12584
12585 #ifdef OBJ_ELF
12586   /* We don't need to align ELF sections to the full alignment.
12587      However, Irix 5 may prefer that we align them at least to a 16
12588      byte boundary.  We don't bother to align the sections if we are
12589      targeted for an embedded system.  */
12590   if (strcmp (TARGET_OS, "elf") == 0)
12591     return addr;
12592   if (align > 4)
12593     align = 4;
12594 #endif
12595
12596   return ((addr + (1 << align) - 1) & (-1 << align));
12597 }
12598
12599 /* Utility routine, called from above as well.  If called while the
12600    input file is still being read, it's only an approximation.  (For
12601    example, a symbol may later become defined which appeared to be
12602    undefined earlier.)  */
12603
12604 static int
12605 nopic_need_relax (sym, before_relaxing)
12606      symbolS *sym;
12607      int before_relaxing;
12608 {
12609   if (sym == 0)
12610     return 0;
12611
12612   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12613     {
12614       const char *symname;
12615       int change;
12616
12617       /* Find out whether this symbol can be referenced off the $gp
12618          register.  It can be if it is smaller than the -G size or if
12619          it is in the .sdata or .sbss section.  Certain symbols can
12620          not be referenced off the $gp, although it appears as though
12621          they can.  */
12622       symname = S_GET_NAME (sym);
12623       if (symname != (const char *) NULL
12624           && (strcmp (symname, "eprol") == 0
12625               || strcmp (symname, "etext") == 0
12626               || strcmp (symname, "_gp") == 0
12627               || strcmp (symname, "edata") == 0
12628               || strcmp (symname, "_fbss") == 0
12629               || strcmp (symname, "_fdata") == 0
12630               || strcmp (symname, "_ftext") == 0
12631               || strcmp (symname, "end") == 0
12632               || strcmp (symname, "_gp_disp") == 0))
12633         change = 1;
12634       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12635                && (0
12636 #ifndef NO_ECOFF_DEBUGGING
12637                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12638                        && (symbol_get_obj (sym)->ecoff_extern_size
12639                            <= g_switch_value))
12640 #endif
12641                    /* We must defer this decision until after the whole
12642                       file has been read, since there might be a .extern
12643                       after the first use of this symbol.  */
12644                    || (before_relaxing
12645 #ifndef NO_ECOFF_DEBUGGING
12646                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12647 #endif
12648                        && S_GET_VALUE (sym) == 0)
12649                    || (S_GET_VALUE (sym) != 0
12650                        && S_GET_VALUE (sym) <= g_switch_value)))
12651         change = 0;
12652       else
12653         {
12654           const char *segname;
12655
12656           segname = segment_name (S_GET_SEGMENT (sym));
12657           assert (strcmp (segname, ".lit8") != 0
12658                   && strcmp (segname, ".lit4") != 0);
12659           change = (strcmp (segname, ".sdata") != 0
12660                     && strcmp (segname, ".sbss") != 0
12661                     && strncmp (segname, ".sdata.", 7) != 0
12662                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12663         }
12664       return change;
12665     }
12666   else
12667     /* We are not optimizing for the $gp register.  */
12668     return 1;
12669 }
12670
12671 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12672    extended opcode.  SEC is the section the frag is in.  */
12673
12674 static int
12675 mips16_extended_frag (fragp, sec, stretch)
12676      fragS *fragp;
12677      asection *sec;
12678      long stretch;
12679 {
12680   int type;
12681   register const struct mips16_immed_operand *op;
12682   offsetT val;
12683   int mintiny, maxtiny;
12684   segT symsec;
12685   fragS *sym_frag;
12686
12687   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12688     return 0;
12689   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12690     return 1;
12691
12692   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12693   op = mips16_immed_operands;
12694   while (op->type != type)
12695     {
12696       ++op;
12697       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12698     }
12699
12700   if (op->unsp)
12701     {
12702       if (type == '<' || type == '>' || type == '[' || type == ']')
12703         {
12704           mintiny = 1;
12705           maxtiny = 1 << op->nbits;
12706         }
12707       else
12708         {
12709           mintiny = 0;
12710           maxtiny = (1 << op->nbits) - 1;
12711         }
12712     }
12713   else
12714     {
12715       mintiny = - (1 << (op->nbits - 1));
12716       maxtiny = (1 << (op->nbits - 1)) - 1;
12717     }
12718
12719   sym_frag = symbol_get_frag (fragp->fr_symbol);
12720   val = S_GET_VALUE (fragp->fr_symbol);
12721   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12722
12723   if (op->pcrel)
12724     {
12725       addressT addr;
12726
12727       /* We won't have the section when we are called from
12728          mips_relax_frag.  However, we will always have been called
12729          from md_estimate_size_before_relax first.  If this is a
12730          branch to a different section, we mark it as such.  If SEC is
12731          NULL, and the frag is not marked, then it must be a branch to
12732          the same section.  */
12733       if (sec == NULL)
12734         {
12735           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12736             return 1;
12737         }
12738       else
12739         {
12740           /* Must have been called from md_estimate_size_before_relax.  */
12741           if (symsec != sec)
12742             {
12743               fragp->fr_subtype =
12744                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12745
12746               /* FIXME: We should support this, and let the linker
12747                  catch branches and loads that are out of range.  */
12748               as_bad_where (fragp->fr_file, fragp->fr_line,
12749                             _("unsupported PC relative reference to different section"));
12750
12751               return 1;
12752             }
12753           if (fragp != sym_frag && sym_frag->fr_address == 0)
12754             /* Assume non-extended on the first relaxation pass.
12755                The address we have calculated will be bogus if this is
12756                a forward branch to another frag, as the forward frag
12757                will have fr_address == 0.  */
12758             return 0;
12759         }
12760
12761       /* In this case, we know for sure that the symbol fragment is in
12762          the same section.  If the relax_marker of the symbol fragment
12763          differs from the relax_marker of this fragment, we have not
12764          yet adjusted the symbol fragment fr_address.  We want to add
12765          in STRETCH in order to get a better estimate of the address.
12766          This particularly matters because of the shift bits.  */
12767       if (stretch != 0
12768           && sym_frag->relax_marker != fragp->relax_marker)
12769         {
12770           fragS *f;
12771
12772           /* Adjust stretch for any alignment frag.  Note that if have
12773              been expanding the earlier code, the symbol may be
12774              defined in what appears to be an earlier frag.  FIXME:
12775              This doesn't handle the fr_subtype field, which specifies
12776              a maximum number of bytes to skip when doing an
12777              alignment.  */
12778           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12779             {
12780               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12781                 {
12782                   if (stretch < 0)
12783                     stretch = - ((- stretch)
12784                                  & ~ ((1 << (int) f->fr_offset) - 1));
12785                   else
12786                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12787                   if (stretch == 0)
12788                     break;
12789                 }
12790             }
12791           if (f != NULL)
12792             val += stretch;
12793         }
12794
12795       addr = fragp->fr_address + fragp->fr_fix;
12796
12797       /* The base address rules are complicated.  The base address of
12798          a branch is the following instruction.  The base address of a
12799          PC relative load or add is the instruction itself, but if it
12800          is in a delay slot (in which case it can not be extended) use
12801          the address of the instruction whose delay slot it is in.  */
12802       if (type == 'p' || type == 'q')
12803         {
12804           addr += 2;
12805
12806           /* If we are currently assuming that this frag should be
12807              extended, then, the current address is two bytes
12808              higher.  */
12809           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12810             addr += 2;
12811
12812           /* Ignore the low bit in the target, since it will be set
12813              for a text label.  */
12814           if ((val & 1) != 0)
12815             --val;
12816         }
12817       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12818         addr -= 4;
12819       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12820         addr -= 2;
12821
12822       val -= addr & ~ ((1 << op->shift) - 1);
12823
12824       /* Branch offsets have an implicit 0 in the lowest bit.  */
12825       if (type == 'p' || type == 'q')
12826         val /= 2;
12827
12828       /* If any of the shifted bits are set, we must use an extended
12829          opcode.  If the address depends on the size of this
12830          instruction, this can lead to a loop, so we arrange to always
12831          use an extended opcode.  We only check this when we are in
12832          the main relaxation loop, when SEC is NULL.  */
12833       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12834         {
12835           fragp->fr_subtype =
12836             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12837           return 1;
12838         }
12839
12840       /* If we are about to mark a frag as extended because the value
12841          is precisely maxtiny + 1, then there is a chance of an
12842          infinite loop as in the following code:
12843              la $4,foo
12844              .skip      1020
12845              .align     2
12846            foo:
12847          In this case when the la is extended, foo is 0x3fc bytes
12848          away, so the la can be shrunk, but then foo is 0x400 away, so
12849          the la must be extended.  To avoid this loop, we mark the
12850          frag as extended if it was small, and is about to become
12851          extended with a value of maxtiny + 1.  */
12852       if (val == ((maxtiny + 1) << op->shift)
12853           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12854           && sec == NULL)
12855         {
12856           fragp->fr_subtype =
12857             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12858           return 1;
12859         }
12860     }
12861   else if (symsec != absolute_section && sec != NULL)
12862     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12863
12864   if ((val & ((1 << op->shift) - 1)) != 0
12865       || val < (mintiny << op->shift)
12866       || val > (maxtiny << op->shift))
12867     return 1;
12868   else
12869     return 0;
12870 }
12871
12872 /* Compute the length of a branch sequence, and adjust the
12873    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12874    worst-case length is computed, with UPDATE being used to indicate
12875    whether an unconditional (-1), branch-likely (+1) or regular (0)
12876    branch is to be computed.  */
12877 static int
12878 relaxed_branch_length (fragp, sec, update)
12879      fragS *fragp;
12880      asection *sec;
12881      int update;
12882 {
12883   bfd_boolean toofar;
12884   int length;
12885
12886   if (fragp
12887       && S_IS_DEFINED (fragp->fr_symbol)
12888       && sec == S_GET_SEGMENT (fragp->fr_symbol))
12889     {
12890       addressT addr;
12891       offsetT val;
12892
12893       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12894
12895       addr = fragp->fr_address + fragp->fr_fix + 4;
12896
12897       val -= addr;
12898
12899       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12900     }
12901   else if (fragp)
12902     /* If the symbol is not defined or it's in a different segment,
12903        assume the user knows what's going on and emit a short
12904        branch.  */
12905     toofar = FALSE;
12906   else
12907     toofar = TRUE;
12908
12909   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12910     fragp->fr_subtype
12911       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12912                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12913                              RELAX_BRANCH_LINK (fragp->fr_subtype),
12914                              toofar);
12915
12916   length = 4;
12917   if (toofar)
12918     {
12919       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12920         length += 8;
12921
12922       if (mips_pic != NO_PIC)
12923         {
12924           /* Additional space for PIC loading of target address.  */
12925           length += 8;
12926           if (mips_opts.isa == ISA_MIPS1)
12927             /* Additional space for $at-stabilizing nop.  */
12928             length += 4;
12929         }
12930
12931       /* If branch is conditional.  */
12932       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12933         length += 8;
12934     }
12935
12936   return length;
12937 }
12938
12939 /* Estimate the size of a frag before relaxing.  Unless this is the
12940    mips16, we are not really relaxing here, and the final size is
12941    encoded in the subtype information.  For the mips16, we have to
12942    decide whether we are using an extended opcode or not.  */
12943
12944 int
12945 md_estimate_size_before_relax (fragp, segtype)
12946      fragS *fragp;
12947      asection *segtype;
12948 {
12949   int change = 0;
12950   bfd_boolean linkonce = FALSE;
12951
12952   if (RELAX_BRANCH_P (fragp->fr_subtype))
12953     {
12954
12955       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12956
12957       return fragp->fr_var;
12958     }
12959
12960   if (RELAX_MIPS16_P (fragp->fr_subtype))
12961     /* We don't want to modify the EXTENDED bit here; it might get us
12962        into infinite loops.  We change it only in mips_relax_frag().  */
12963     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12964
12965   if (mips_pic == NO_PIC)
12966     {
12967       change = nopic_need_relax (fragp->fr_symbol, 0);
12968     }
12969   else if (mips_pic == SVR4_PIC)
12970     {
12971       symbolS *sym;
12972       asection *symsec;
12973
12974       sym = fragp->fr_symbol;
12975
12976       /* Handle the case of a symbol equated to another symbol.  */
12977       while (symbol_equated_reloc_p (sym))
12978         {
12979           symbolS *n;
12980
12981           /* It's possible to get a loop here in a badly written
12982              program.  */
12983           n = symbol_get_value_expression (sym)->X_add_symbol;
12984           if (n == sym)
12985             break;
12986           sym = n;
12987         }
12988
12989       symsec = S_GET_SEGMENT (sym);
12990
12991       /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12992       if (symsec != segtype && ! S_IS_LOCAL (sym))
12993         {
12994           if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12995               != 0)
12996             linkonce = TRUE;
12997
12998           /* The GNU toolchain uses an extension for ELF: a section
12999              beginning with the magic string .gnu.linkonce is a linkonce
13000              section.  */
13001           if (strncmp (segment_name (symsec), ".gnu.linkonce",
13002                        sizeof ".gnu.linkonce" - 1) == 0)
13003             linkonce = TRUE;
13004         }
13005
13006       /* This must duplicate the test in adjust_reloc_syms.  */
13007       change = (symsec != &bfd_und_section
13008                 && symsec != &bfd_abs_section
13009                 && ! bfd_is_com_section (symsec)
13010                 && !linkonce
13011 #ifdef OBJ_ELF
13012                 /* A global or weak symbol is treated as external.  */
13013                 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13014                     || (! S_IS_WEAK (sym)
13015                         && (! S_IS_EXTERNAL (sym)
13016                             || mips_pic == EMBEDDED_PIC)))
13017 #endif
13018                 );
13019     }
13020   else
13021     abort ();
13022
13023   if (change)
13024     {
13025       /* Record the offset to the first reloc in the fr_opcode field.
13026          This lets md_convert_frag and tc_gen_reloc know that the code
13027          must be expanded.  */
13028       fragp->fr_opcode = (fragp->fr_literal
13029                           + fragp->fr_fix
13030                           - RELAX_OLD (fragp->fr_subtype)
13031                           + RELAX_RELOC1 (fragp->fr_subtype));
13032       /* FIXME: This really needs as_warn_where.  */
13033       if (RELAX_WARN (fragp->fr_subtype))
13034         as_warn (_("AT used after \".set noat\" or macro used after "
13035                    "\".set nomacro\""));
13036
13037       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13038     }
13039
13040   return 0;
13041 }
13042
13043 /* This is called to see whether a reloc against a defined symbol
13044    should be converted into a reloc against a section.  Don't adjust
13045    MIPS16 jump relocations, so we don't have to worry about the format
13046    of the offset in the .o file.  Don't adjust relocations against
13047    mips16 symbols, so that the linker can find them if it needs to set
13048    up a stub.  */
13049
13050 int
13051 mips_fix_adjustable (fixp)
13052      fixS *fixp;
13053 {
13054   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13055     return 0;
13056
13057   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13058       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13059     return 0;
13060
13061   if (fixp->fx_addsy == NULL)
13062     return 1;
13063
13064 #ifdef OBJ_ELF
13065   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13066       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13067       && fixp->fx_subsy == NULL)
13068     return 0;
13069 #endif
13070
13071   return 1;
13072 }
13073
13074 /* Translate internal representation of relocation info to BFD target
13075    format.  */
13076
13077 arelent **
13078 tc_gen_reloc (section, fixp)
13079      asection *section ATTRIBUTE_UNUSED;
13080      fixS *fixp;
13081 {
13082   static arelent *retval[4];
13083   arelent *reloc;
13084   bfd_reloc_code_real_type code;
13085
13086   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13087   retval[1] = NULL;
13088
13089   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13090   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13091   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13092
13093   if (mips_pic == EMBEDDED_PIC
13094       && SWITCH_TABLE (fixp))
13095     {
13096       /* For a switch table entry we use a special reloc.  The addend
13097          is actually the difference between the reloc address and the
13098          subtrahend.  */
13099       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13100       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13101         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13102       fixp->fx_r_type = BFD_RELOC_GPREL32;
13103     }
13104   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13105     {
13106       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13107         reloc->addend = fixp->fx_addnumber;
13108       else
13109         {
13110           /* We use a special addend for an internal RELLO reloc.  */
13111           if (symbol_section_p (fixp->fx_addsy))
13112             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13113           else
13114             reloc->addend = fixp->fx_addnumber + reloc->address;
13115         }
13116     }
13117   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13118     {
13119       assert (fixp->fx_next != NULL
13120               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13121
13122       /* The reloc is relative to the RELLO; adjust the addend
13123          accordingly.  */
13124       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13125         reloc->addend = fixp->fx_next->fx_addnumber;
13126       else
13127         {
13128           /* We use a special addend for an internal RELHI reloc.  */
13129           if (symbol_section_p (fixp->fx_addsy))
13130             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13131                              + fixp->fx_next->fx_where
13132                              - S_GET_VALUE (fixp->fx_subsy));
13133           else
13134             reloc->addend = (fixp->fx_addnumber
13135                              + fixp->fx_next->fx_frag->fr_address
13136                              + fixp->fx_next->fx_where);
13137         }
13138     }
13139   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13140     reloc->addend = fixp->fx_addnumber;
13141   else
13142     {
13143       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13144         /* A gruesome hack which is a result of the gruesome gas reloc
13145            handling.  */
13146         reloc->addend = reloc->address;
13147       else
13148         reloc->addend = -reloc->address;
13149     }
13150
13151   /* If this is a variant frag, we may need to adjust the existing
13152      reloc and generate a new one.  */
13153   if (fixp->fx_frag->fr_opcode != NULL
13154       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13155            && ! HAVE_NEWABI)
13156           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13157           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13158           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13159           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13160           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13161           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13162     )
13163     {
13164       arelent *reloc2;
13165
13166       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13167
13168       /* If this is not the last reloc in this frag, then we have two
13169          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13170          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13171          the second one handle all of them.  */
13172       if (fixp->fx_next != NULL
13173           && fixp->fx_frag == fixp->fx_next->fx_frag)
13174         {
13175           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13176                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13177                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13178                       && (fixp->fx_next->fx_r_type
13179                           == BFD_RELOC_MIPS_GOT_LO16))
13180                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13181                       && (fixp->fx_next->fx_r_type
13182                           == BFD_RELOC_MIPS_CALL_LO16)));
13183           retval[0] = NULL;
13184           return retval;
13185         }
13186
13187       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13188       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13189       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13190       retval[2] = NULL;
13191       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13192       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13193       reloc2->address = (reloc->address
13194                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13195                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13196       reloc2->addend = fixp->fx_addnumber;
13197       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13198       assert (reloc2->howto != NULL);
13199
13200       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13201         {
13202           arelent *reloc3;
13203
13204           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13205           retval[3] = NULL;
13206           *reloc3 = *reloc2;
13207           reloc3->address += 4;
13208         }
13209
13210       if (mips_pic == NO_PIC)
13211         {
13212           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13213           fixp->fx_r_type = BFD_RELOC_HI16_S;
13214         }
13215       else if (mips_pic == SVR4_PIC)
13216         {
13217           switch (fixp->fx_r_type)
13218             {
13219             default:
13220               abort ();
13221             case BFD_RELOC_MIPS_GOT16:
13222               break;
13223             case BFD_RELOC_MIPS_GOT_LO16:
13224             case BFD_RELOC_MIPS_CALL_LO16:
13225               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13226               break;
13227             case BFD_RELOC_MIPS_CALL16:
13228               if (HAVE_NEWABI)
13229                 {
13230                   /* BFD_RELOC_MIPS_GOT16;*/
13231                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13232                   reloc2->howto = bfd_reloc_type_lookup
13233                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13234                 }
13235               else
13236                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13237               break;
13238             }
13239         }
13240       else
13241         abort ();
13242
13243       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13244       if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13245         {
13246           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13247           retval[1] = NULL;
13248         }
13249     }
13250
13251   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13252      entry to be used in the relocation's section offset.  */
13253   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13254     {
13255       reloc->address = reloc->addend;
13256       reloc->addend = 0;
13257     }
13258
13259   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13260      fixup_segment converted a non-PC relative reloc into a PC
13261      relative reloc.  In such a case, we need to convert the reloc
13262      code.  */
13263   code = fixp->fx_r_type;
13264   if (fixp->fx_pcrel)
13265     {
13266       switch (code)
13267         {
13268         case BFD_RELOC_8:
13269           code = BFD_RELOC_8_PCREL;
13270           break;
13271         case BFD_RELOC_16:
13272           code = BFD_RELOC_16_PCREL;
13273           break;
13274         case BFD_RELOC_32:
13275           code = BFD_RELOC_32_PCREL;
13276           break;
13277         case BFD_RELOC_64:
13278           code = BFD_RELOC_64_PCREL;
13279           break;
13280         case BFD_RELOC_8_PCREL:
13281         case BFD_RELOC_16_PCREL:
13282         case BFD_RELOC_32_PCREL:
13283         case BFD_RELOC_64_PCREL:
13284         case BFD_RELOC_16_PCREL_S2:
13285         case BFD_RELOC_PCREL_HI16_S:
13286         case BFD_RELOC_PCREL_LO16:
13287           break;
13288         default:
13289           as_bad_where (fixp->fx_file, fixp->fx_line,
13290                         _("Cannot make %s relocation PC relative"),
13291                         bfd_get_reloc_code_name (code));
13292         }
13293     }
13294
13295 #ifdef OBJ_ELF
13296   /* md_apply_fix3 has a double-subtraction hack to get
13297      bfd_install_relocation to behave nicely.  GPREL relocations are
13298      handled correctly without this hack, so undo it here.  We can't
13299      stop md_apply_fix3 from subtracting twice in the first place since
13300      the fake addend is required for variant frags above.  */
13301   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13302       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13303       && reloc->addend != 0
13304       && mips_need_elf_addend_fixup (fixp))
13305     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13306 #endif
13307
13308   /* To support a PC relative reloc when generating embedded PIC code
13309      for ECOFF, we use a Cygnus extension.  We check for that here to
13310      make sure that we don't let such a reloc escape normally.  */
13311   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13312        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13313       && code == BFD_RELOC_16_PCREL_S2
13314       && mips_pic != EMBEDDED_PIC)
13315     reloc->howto = NULL;
13316   else
13317     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13318
13319   if (reloc->howto == NULL)
13320     {
13321       as_bad_where (fixp->fx_file, fixp->fx_line,
13322                     _("Can not represent %s relocation in this object file format"),
13323                     bfd_get_reloc_code_name (code));
13324       retval[0] = NULL;
13325     }
13326
13327   return retval;
13328 }
13329
13330 /* Relax a machine dependent frag.  This returns the amount by which
13331    the current size of the frag should change.  */
13332
13333 int
13334 mips_relax_frag (sec, fragp, stretch)
13335      asection *sec;
13336      fragS *fragp;
13337      long stretch;
13338 {
13339   if (RELAX_BRANCH_P (fragp->fr_subtype))
13340     {
13341       offsetT old_var = fragp->fr_var;
13342
13343       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13344
13345       return fragp->fr_var - old_var;
13346     }
13347
13348   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13349     return 0;
13350
13351   if (mips16_extended_frag (fragp, NULL, stretch))
13352     {
13353       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13354         return 0;
13355       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13356       return 2;
13357     }
13358   else
13359     {
13360       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13361         return 0;
13362       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13363       return -2;
13364     }
13365
13366   return 0;
13367 }
13368
13369 /* Convert a machine dependent frag.  */
13370
13371 void
13372 md_convert_frag (abfd, asec, fragp)
13373      bfd *abfd ATTRIBUTE_UNUSED;
13374      segT asec;
13375      fragS *fragp;
13376 {
13377   int old, new;
13378   char *fixptr;
13379
13380   if (RELAX_BRANCH_P (fragp->fr_subtype))
13381     {
13382       bfd_byte *buf;
13383       unsigned long insn;
13384       expressionS exp;
13385       fixS *fixp;
13386
13387       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13388
13389       if (target_big_endian)
13390         insn = bfd_getb32 (buf);
13391       else
13392         insn = bfd_getl32 (buf);
13393
13394       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13395         {
13396           /* We generate a fixup instead of applying it right now
13397              because, if there are linker relaxations, we're going to
13398              need the relocations.  */
13399           exp.X_op = O_symbol;
13400           exp.X_add_symbol = fragp->fr_symbol;
13401           exp.X_add_number = fragp->fr_offset;
13402
13403           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13404                               4, &exp, 1,
13405                               BFD_RELOC_16_PCREL_S2);
13406           fixp->fx_file = fragp->fr_file;
13407           fixp->fx_line = fragp->fr_line;
13408
13409           md_number_to_chars ((char *)buf, insn, 4);
13410           buf += 4;
13411         }
13412       else
13413         {
13414           int i;
13415
13416           as_warn_where (fragp->fr_file, fragp->fr_line,
13417                          _("relaxed out-of-range branch into a jump"));
13418
13419           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13420             goto uncond;
13421
13422           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13423             {
13424               /* Reverse the branch.  */
13425               switch ((insn >> 28) & 0xf)
13426                 {
13427                 case 4:
13428                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13429                      have the condition reversed by tweaking a single
13430                      bit, and their opcodes all have 0x4???????.  */
13431                   assert ((insn & 0xf1000000) == 0x41000000);
13432                   insn ^= 0x00010000;
13433                   break;
13434
13435                 case 0:
13436                   /* bltz       0x04000000      bgez    0x04010000
13437                      bltzal     0x04100000      bgezal  0x04110000 */
13438                   assert ((insn & 0xfc0e0000) == 0x04000000);
13439                   insn ^= 0x00010000;
13440                   break;
13441
13442                 case 1:
13443                   /* beq        0x10000000      bne     0x14000000
13444                      blez       0x18000000      bgtz    0x1c000000 */
13445                   insn ^= 0x04000000;
13446                   break;
13447
13448                 default:
13449                   abort ();
13450                 }
13451             }
13452
13453           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13454             {
13455               /* Clear the and-link bit.  */
13456               assert ((insn & 0xfc1c0000) == 0x04100000);
13457
13458               /* bltzal 0x04100000      bgezal  0x04110000
13459                 bltzall 0x04120000     bgezall  0x04130000 */
13460               insn &= ~0x00100000;
13461             }
13462
13463           /* Branch over the branch (if the branch was likely) or the
13464              full jump (not likely case).  Compute the offset from the
13465              current instruction to branch to.  */
13466           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13467             i = 16;
13468           else
13469             {
13470               /* How many bytes in instructions we've already emitted?  */
13471               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13472               /* How many bytes in instructions from here to the end?  */
13473               i = fragp->fr_var - i;
13474             }
13475           /* Convert to instruction count.  */
13476           i >>= 2;
13477           /* Branch counts from the next instruction.  */
13478           i--;
13479           insn |= i;
13480           /* Branch over the jump.  */
13481           md_number_to_chars ((char *)buf, insn, 4);
13482           buf += 4;
13483
13484           /* Nop */
13485           md_number_to_chars ((char*)buf, 0, 4);
13486           buf += 4;
13487
13488           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13489             {
13490               /* beql $0, $0, 2f */
13491               insn = 0x50000000;
13492               /* Compute the PC offset from the current instruction to
13493                  the end of the variable frag.  */
13494               /* How many bytes in instructions we've already emitted?  */
13495               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13496               /* How many bytes in instructions from here to the end?  */
13497               i = fragp->fr_var - i;
13498               /* Convert to instruction count.  */
13499               i >>= 2;
13500               /* Don't decrement i, because we want to branch over the
13501                  delay slot.  */
13502
13503               insn |= i;
13504               md_number_to_chars ((char *)buf, insn, 4);
13505               buf += 4;
13506
13507               md_number_to_chars ((char *)buf, 0, 4);
13508               buf += 4;
13509             }
13510
13511         uncond:
13512           if (mips_pic == NO_PIC)
13513             {
13514               /* j or jal.  */
13515               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13516                       ? 0x0c000000 : 0x08000000);
13517               exp.X_op = O_symbol;
13518               exp.X_add_symbol = fragp->fr_symbol;
13519               exp.X_add_number = fragp->fr_offset;
13520
13521               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13522                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13523               fixp->fx_file = fragp->fr_file;
13524               fixp->fx_line = fragp->fr_line;
13525
13526               md_number_to_chars ((char*)buf, insn, 4);
13527               buf += 4;
13528             }
13529           else
13530             {
13531               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13532               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13533               exp.X_op = O_symbol;
13534               exp.X_add_symbol = fragp->fr_symbol;
13535               exp.X_add_number = fragp->fr_offset;
13536
13537               if (fragp->fr_offset)
13538                 {
13539                   exp.X_add_symbol = make_expr_symbol (&exp);
13540                   exp.X_add_number = 0;
13541                 }
13542
13543               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13544                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13545               fixp->fx_file = fragp->fr_file;
13546               fixp->fx_line = fragp->fr_line;
13547
13548               md_number_to_chars ((char*)buf, insn, 4);
13549               buf += 4;
13550
13551               if (mips_opts.isa == ISA_MIPS1)
13552                 {
13553                   /* nop */
13554                   md_number_to_chars ((char*)buf, 0, 4);
13555                   buf += 4;
13556                 }
13557
13558               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13559               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13560
13561               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13562                                   4, &exp, 0, BFD_RELOC_LO16);
13563               fixp->fx_file = fragp->fr_file;
13564               fixp->fx_line = fragp->fr_line;
13565
13566               md_number_to_chars ((char*)buf, insn, 4);
13567               buf += 4;
13568
13569               /* j(al)r $at.  */
13570               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13571                 insn = 0x0020f809;
13572               else
13573                 insn = 0x00200008;
13574
13575               md_number_to_chars ((char*)buf, insn, 4);
13576               buf += 4;
13577             }
13578         }
13579
13580       assert (buf == (bfd_byte *)fragp->fr_literal
13581               + fragp->fr_fix + fragp->fr_var);
13582
13583       fragp->fr_fix += fragp->fr_var;
13584
13585       return;
13586     }
13587
13588   if (RELAX_MIPS16_P (fragp->fr_subtype))
13589     {
13590       int type;
13591       register const struct mips16_immed_operand *op;
13592       bfd_boolean small, ext;
13593       offsetT val;
13594       bfd_byte *buf;
13595       unsigned long insn;
13596       bfd_boolean use_extend;
13597       unsigned short extend;
13598
13599       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13600       op = mips16_immed_operands;
13601       while (op->type != type)
13602         ++op;
13603
13604       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13605         {
13606           small = FALSE;
13607           ext = TRUE;
13608         }
13609       else
13610         {
13611           small = TRUE;
13612           ext = FALSE;
13613         }
13614
13615       resolve_symbol_value (fragp->fr_symbol);
13616       val = S_GET_VALUE (fragp->fr_symbol);
13617       if (op->pcrel)
13618         {
13619           addressT addr;
13620
13621           addr = fragp->fr_address + fragp->fr_fix;
13622
13623           /* The rules for the base address of a PC relative reloc are
13624              complicated; see mips16_extended_frag.  */
13625           if (type == 'p' || type == 'q')
13626             {
13627               addr += 2;
13628               if (ext)
13629                 addr += 2;
13630               /* Ignore the low bit in the target, since it will be
13631                  set for a text label.  */
13632               if ((val & 1) != 0)
13633                 --val;
13634             }
13635           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13636             addr -= 4;
13637           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13638             addr -= 2;
13639
13640           addr &= ~ (addressT) ((1 << op->shift) - 1);
13641           val -= addr;
13642
13643           /* Make sure the section winds up with the alignment we have
13644              assumed.  */
13645           if (op->shift > 0)
13646             record_alignment (asec, op->shift);
13647         }
13648
13649       if (ext
13650           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13651               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13652         as_warn_where (fragp->fr_file, fragp->fr_line,
13653                        _("extended instruction in delay slot"));
13654
13655       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13656
13657       if (target_big_endian)
13658         insn = bfd_getb16 (buf);
13659       else
13660         insn = bfd_getl16 (buf);
13661
13662       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13663                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13664                     small, ext, &insn, &use_extend, &extend);
13665
13666       if (use_extend)
13667         {
13668           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13669           fragp->fr_fix += 2;
13670           buf += 2;
13671         }
13672
13673       md_number_to_chars ((char *) buf, insn, 2);
13674       fragp->fr_fix += 2;
13675       buf += 2;
13676     }
13677   else
13678     {
13679       if (fragp->fr_opcode == NULL)
13680         return;
13681
13682       old = RELAX_OLD (fragp->fr_subtype);
13683       new = RELAX_NEW (fragp->fr_subtype);
13684       fixptr = fragp->fr_literal + fragp->fr_fix;
13685
13686       if (new > 0)
13687         memcpy (fixptr - old, fixptr, new);
13688
13689       fragp->fr_fix += new - old;
13690     }
13691 }
13692
13693 #ifdef OBJ_ELF
13694
13695 /* This function is called after the relocs have been generated.
13696    We've been storing mips16 text labels as odd.  Here we convert them
13697    back to even for the convenience of the debugger.  */
13698
13699 void
13700 mips_frob_file_after_relocs ()
13701 {
13702   asymbol **syms;
13703   unsigned int count, i;
13704
13705   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13706     return;
13707
13708   syms = bfd_get_outsymbols (stdoutput);
13709   count = bfd_get_symcount (stdoutput);
13710   for (i = 0; i < count; i++, syms++)
13711     {
13712       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13713           && ((*syms)->value & 1) != 0)
13714         {
13715           (*syms)->value &= ~1;
13716           /* If the symbol has an odd size, it was probably computed
13717              incorrectly, so adjust that as well.  */
13718           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13719             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13720         }
13721     }
13722 }
13723
13724 #endif
13725
13726 /* This function is called whenever a label is defined.  It is used
13727    when handling branch delays; if a branch has a label, we assume we
13728    can not move it.  */
13729
13730 void
13731 mips_define_label (sym)
13732      symbolS *sym;
13733 {
13734   struct insn_label_list *l;
13735
13736   if (free_insn_labels == NULL)
13737     l = (struct insn_label_list *) xmalloc (sizeof *l);
13738   else
13739     {
13740       l = free_insn_labels;
13741       free_insn_labels = l->next;
13742     }
13743
13744   l->label = sym;
13745   l->next = insn_labels;
13746   insn_labels = l;
13747 }
13748 \f
13749 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13750
13751 /* Some special processing for a MIPS ELF file.  */
13752
13753 void
13754 mips_elf_final_processing ()
13755 {
13756   /* Write out the register information.  */
13757   if (mips_abi != N64_ABI)
13758     {
13759       Elf32_RegInfo s;
13760
13761       s.ri_gprmask = mips_gprmask;
13762       s.ri_cprmask[0] = mips_cprmask[0];
13763       s.ri_cprmask[1] = mips_cprmask[1];
13764       s.ri_cprmask[2] = mips_cprmask[2];
13765       s.ri_cprmask[3] = mips_cprmask[3];
13766       /* The gp_value field is set by the MIPS ELF backend.  */
13767
13768       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13769                                        ((Elf32_External_RegInfo *)
13770                                         mips_regmask_frag));
13771     }
13772   else
13773     {
13774       Elf64_Internal_RegInfo s;
13775
13776       s.ri_gprmask = mips_gprmask;
13777       s.ri_pad = 0;
13778       s.ri_cprmask[0] = mips_cprmask[0];
13779       s.ri_cprmask[1] = mips_cprmask[1];
13780       s.ri_cprmask[2] = mips_cprmask[2];
13781       s.ri_cprmask[3] = mips_cprmask[3];
13782       /* The gp_value field is set by the MIPS ELF backend.  */
13783
13784       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13785                                        ((Elf64_External_RegInfo *)
13786                                         mips_regmask_frag));
13787     }
13788
13789   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13790      sort of BFD interface for this.  */
13791   if (mips_any_noreorder)
13792     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13793   if (mips_pic != NO_PIC)
13794     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13795
13796   /* Set MIPS ELF flags for ASEs.  */
13797   if (file_ase_mips16)
13798     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13799 #if 0 /* XXX FIXME */
13800   if (file_ase_mips3d)
13801     elf_elfheader (stdoutput)->e_flags |= ???;
13802 #endif
13803   if (file_ase_mdmx)
13804     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13805
13806   /* Set the MIPS ELF ABI flags.  */
13807   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13808     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13809   else if (mips_abi == O64_ABI)
13810     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13811   else if (mips_abi == EABI_ABI)
13812     {
13813       if (!file_mips_gp32)
13814         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13815       else
13816         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13817     }
13818   else if (mips_abi == N32_ABI)
13819     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13820
13821   /* Nothing to do for N64_ABI.  */
13822
13823   if (mips_32bitmode)
13824     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13825 }
13826
13827 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13828 \f
13829 typedef struct proc {
13830   symbolS *isym;
13831   unsigned long reg_mask;
13832   unsigned long reg_offset;
13833   unsigned long fpreg_mask;
13834   unsigned long fpreg_offset;
13835   unsigned long frame_offset;
13836   unsigned long frame_reg;
13837   unsigned long pc_reg;
13838 } procS;
13839
13840 static procS cur_proc;
13841 static procS *cur_proc_ptr;
13842 static int numprocs;
13843
13844 /* Fill in an rs_align_code fragment.  */
13845
13846 void
13847 mips_handle_align (fragp)
13848      fragS *fragp;
13849 {
13850   if (fragp->fr_type != rs_align_code)
13851     return;
13852
13853   if (mips_opts.mips16)
13854     {
13855       static const unsigned char be_nop[] = { 0x65, 0x00 };
13856       static const unsigned char le_nop[] = { 0x00, 0x65 };
13857
13858       int bytes;
13859       char *p;
13860
13861       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13862       p = fragp->fr_literal + fragp->fr_fix;
13863
13864       if (bytes & 1)
13865         {
13866           *p++ = 0;
13867           fragp->fr_fix++;
13868         }
13869
13870       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13871       fragp->fr_var = 2;
13872     }
13873
13874   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13875 }
13876
13877 static void
13878 md_obj_begin ()
13879 {
13880 }
13881
13882 static void
13883 md_obj_end ()
13884 {
13885   /* check for premature end, nesting errors, etc */
13886   if (cur_proc_ptr)
13887     as_warn (_("missing .end at end of assembly"));
13888 }
13889
13890 static long
13891 get_number ()
13892 {
13893   int negative = 0;
13894   long val = 0;
13895
13896   if (*input_line_pointer == '-')
13897     {
13898       ++input_line_pointer;
13899       negative = 1;
13900     }
13901   if (!ISDIGIT (*input_line_pointer))
13902     as_bad (_("expected simple number"));
13903   if (input_line_pointer[0] == '0')
13904     {
13905       if (input_line_pointer[1] == 'x')
13906         {
13907           input_line_pointer += 2;
13908           while (ISXDIGIT (*input_line_pointer))
13909             {
13910               val <<= 4;
13911               val |= hex_value (*input_line_pointer++);
13912             }
13913           return negative ? -val : val;
13914         }
13915       else
13916         {
13917           ++input_line_pointer;
13918           while (ISDIGIT (*input_line_pointer))
13919             {
13920               val <<= 3;
13921               val |= *input_line_pointer++ - '0';
13922             }
13923           return negative ? -val : val;
13924         }
13925     }
13926   if (!ISDIGIT (*input_line_pointer))
13927     {
13928       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13929               *input_line_pointer, *input_line_pointer);
13930       as_warn (_("invalid number"));
13931       return -1;
13932     }
13933   while (ISDIGIT (*input_line_pointer))
13934     {
13935       val *= 10;
13936       val += *input_line_pointer++ - '0';
13937     }
13938   return negative ? -val : val;
13939 }
13940
13941 /* The .file directive; just like the usual .file directive, but there
13942    is an initial number which is the ECOFF file index.  In the non-ECOFF
13943    case .file implies DWARF-2.  */
13944
13945 static void
13946 s_mips_file (x)
13947      int x ATTRIBUTE_UNUSED;
13948 {
13949   static int first_file_directive = 0;
13950
13951   if (ECOFF_DEBUGGING)
13952     {
13953       get_number ();
13954       s_app_file (0);
13955     }
13956   else
13957     {
13958       char *filename;
13959
13960       filename = dwarf2_directive_file (0);
13961
13962       /* Versions of GCC up to 3.1 start files with a ".file"
13963          directive even for stabs output.  Make sure that this
13964          ".file" is handled.  Note that you need a version of GCC
13965          after 3.1 in order to support DWARF-2 on MIPS.  */
13966       if (filename != NULL && ! first_file_directive)
13967         {
13968           (void) new_logical_line (filename, -1);
13969           s_app_file_string (filename);
13970         }
13971       first_file_directive = 1;
13972     }
13973 }
13974
13975 /* The .loc directive, implying DWARF-2.  */
13976
13977 static void
13978 s_mips_loc (x)
13979      int x ATTRIBUTE_UNUSED;
13980 {
13981   if (!ECOFF_DEBUGGING)
13982     dwarf2_directive_loc (0);
13983 }
13984
13985 /* The .end directive.  */
13986
13987 static void
13988 s_mips_end (x)
13989      int x ATTRIBUTE_UNUSED;
13990 {
13991   symbolS *p;
13992   int maybe_text;
13993
13994   /* Following functions need their own .frame and .cprestore directives.  */
13995   mips_frame_reg_valid = 0;
13996   mips_cprestore_valid = 0;
13997
13998   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13999     {
14000       p = get_symbol ();
14001       demand_empty_rest_of_line ();
14002     }
14003   else
14004     p = NULL;
14005
14006 #ifdef BFD_ASSEMBLER
14007   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14008     maybe_text = 1;
14009   else
14010     maybe_text = 0;
14011 #else
14012   if (now_seg != data_section && now_seg != bss_section)
14013     maybe_text = 1;
14014   else
14015     maybe_text = 0;
14016 #endif
14017
14018   if (!maybe_text)
14019     as_warn (_(".end not in text section"));
14020
14021   if (!cur_proc_ptr)
14022     {
14023       as_warn (_(".end directive without a preceding .ent directive."));
14024       demand_empty_rest_of_line ();
14025       return;
14026     }
14027
14028   if (p != NULL)
14029     {
14030       assert (S_GET_NAME (p));
14031       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14032         as_warn (_(".end symbol does not match .ent symbol."));
14033
14034       if (debug_type == DEBUG_STABS)
14035         stabs_generate_asm_endfunc (S_GET_NAME (p),
14036                                     S_GET_NAME (p));
14037     }
14038   else
14039     as_warn (_(".end directive missing or unknown symbol"));
14040
14041 #ifdef OBJ_ELF
14042   /* Generate a .pdr section.  */
14043   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14044     {
14045       segT saved_seg = now_seg;
14046       subsegT saved_subseg = now_subseg;
14047       valueT dot;
14048       expressionS exp;
14049       char *fragp;
14050
14051       dot = frag_now_fix ();
14052
14053 #ifdef md_flush_pending_output
14054       md_flush_pending_output ();
14055 #endif
14056
14057       assert (pdr_seg);
14058       subseg_set (pdr_seg, 0);
14059
14060       /* Write the symbol.  */
14061       exp.X_op = O_symbol;
14062       exp.X_add_symbol = p;
14063       exp.X_add_number = 0;
14064       emit_expr (&exp, 4);
14065
14066       fragp = frag_more (7 * 4);
14067
14068       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14069       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14070       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14071       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14072       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14073       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14074       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14075
14076       subseg_set (saved_seg, saved_subseg);
14077     }
14078 #endif /* OBJ_ELF */
14079
14080   cur_proc_ptr = NULL;
14081 }
14082
14083 /* The .aent and .ent directives.  */
14084
14085 static void
14086 s_mips_ent (aent)
14087      int aent;
14088 {
14089   symbolS *symbolP;
14090   int maybe_text;
14091
14092   symbolP = get_symbol ();
14093   if (*input_line_pointer == ',')
14094     ++input_line_pointer;
14095   SKIP_WHITESPACE ();
14096   if (ISDIGIT (*input_line_pointer)
14097       || *input_line_pointer == '-')
14098     get_number ();
14099
14100 #ifdef BFD_ASSEMBLER
14101   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14102     maybe_text = 1;
14103   else
14104     maybe_text = 0;
14105 #else
14106   if (now_seg != data_section && now_seg != bss_section)
14107     maybe_text = 1;
14108   else
14109     maybe_text = 0;
14110 #endif
14111
14112   if (!maybe_text)
14113     as_warn (_(".ent or .aent not in text section."));
14114
14115   if (!aent && cur_proc_ptr)
14116     as_warn (_("missing .end"));
14117
14118   if (!aent)
14119     {
14120       /* This function needs its own .frame and .cprestore directives.  */
14121       mips_frame_reg_valid = 0;
14122       mips_cprestore_valid = 0;
14123
14124       cur_proc_ptr = &cur_proc;
14125       memset (cur_proc_ptr, '\0', sizeof (procS));
14126
14127       cur_proc_ptr->isym = symbolP;
14128
14129       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14130
14131       ++numprocs;
14132
14133       if (debug_type == DEBUG_STABS)
14134         stabs_generate_asm_func (S_GET_NAME (symbolP),
14135                                  S_GET_NAME (symbolP));
14136     }
14137
14138   demand_empty_rest_of_line ();
14139 }
14140
14141 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14142    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14143    s_mips_frame is used so that we can set the PDR information correctly.
14144    We can't use the ecoff routines because they make reference to the ecoff
14145    symbol table (in the mdebug section).  */
14146
14147 static void
14148 s_mips_frame (ignore)
14149      int ignore ATTRIBUTE_UNUSED;
14150 {
14151 #ifdef OBJ_ELF
14152   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14153     {
14154       long val;
14155
14156       if (cur_proc_ptr == (procS *) NULL)
14157         {
14158           as_warn (_(".frame outside of .ent"));
14159           demand_empty_rest_of_line ();
14160           return;
14161         }
14162
14163       cur_proc_ptr->frame_reg = tc_get_register (1);
14164
14165       SKIP_WHITESPACE ();
14166       if (*input_line_pointer++ != ','
14167           || get_absolute_expression_and_terminator (&val) != ',')
14168         {
14169           as_warn (_("Bad .frame directive"));
14170           --input_line_pointer;
14171           demand_empty_rest_of_line ();
14172           return;
14173         }
14174
14175       cur_proc_ptr->frame_offset = val;
14176       cur_proc_ptr->pc_reg = tc_get_register (0);
14177
14178       demand_empty_rest_of_line ();
14179     }
14180   else
14181 #endif /* OBJ_ELF */
14182     s_ignore (ignore);
14183 }
14184
14185 /* The .fmask and .mask directives. If the mdebug section is present
14186    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14187    embedded targets, s_mips_mask is used so that we can set the PDR
14188    information correctly. We can't use the ecoff routines because they
14189    make reference to the ecoff symbol table (in the mdebug section).  */
14190
14191 static void
14192 s_mips_mask (reg_type)
14193      char reg_type;
14194 {
14195 #ifdef OBJ_ELF
14196   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14197     {
14198       long mask, off;
14199
14200       if (cur_proc_ptr == (procS *) NULL)
14201         {
14202           as_warn (_(".mask/.fmask outside of .ent"));
14203           demand_empty_rest_of_line ();
14204           return;
14205         }
14206
14207       if (get_absolute_expression_and_terminator (&mask) != ',')
14208         {
14209           as_warn (_("Bad .mask/.fmask directive"));
14210           --input_line_pointer;
14211           demand_empty_rest_of_line ();
14212           return;
14213         }
14214
14215       off = get_absolute_expression ();
14216
14217       if (reg_type == 'F')
14218         {
14219           cur_proc_ptr->fpreg_mask = mask;
14220           cur_proc_ptr->fpreg_offset = off;
14221         }
14222       else
14223         {
14224           cur_proc_ptr->reg_mask = mask;
14225           cur_proc_ptr->reg_offset = off;
14226         }
14227
14228       demand_empty_rest_of_line ();
14229     }
14230   else
14231 #endif /* OBJ_ELF */
14232     s_ignore (reg_type);
14233 }
14234
14235 /* The .loc directive.  */
14236
14237 #if 0
14238 static void
14239 s_loc (x)
14240      int x;
14241 {
14242   symbolS *symbolP;
14243   int lineno;
14244   int addroff;
14245
14246   assert (now_seg == text_section);
14247
14248   lineno = get_number ();
14249   addroff = frag_now_fix ();
14250
14251   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14252   S_SET_TYPE (symbolP, N_SLINE);
14253   S_SET_OTHER (symbolP, 0);
14254   S_SET_DESC (symbolP, lineno);
14255   symbolP->sy_segment = now_seg;
14256 }
14257 #endif
14258
14259 /* A table describing all the processors gas knows about.  Names are
14260    matched in the order listed.
14261
14262    To ease comparison, please keep this table in the same order as
14263    gcc's mips_cpu_info_table[].  */
14264 static const struct mips_cpu_info mips_cpu_info_table[] =
14265 {
14266   /* Entries for generic ISAs */
14267   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14268   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14269   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14270   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14271   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14272   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14273   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14274   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14275
14276   /* MIPS I */
14277   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14278   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14279   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14280
14281   /* MIPS II */
14282   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14283
14284   /* MIPS III */
14285   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14286   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14287   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14288   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14289   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14290   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14291   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14292   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14293   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14294   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14295   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14296   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14297
14298   /* MIPS IV */
14299   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14300   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14301   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14302   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14303   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14304   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14305   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14306   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14307   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14308   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14309   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14310   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14311
14312   /* MIPS 32 */
14313   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14314   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14315   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14316
14317   /* MIPS 64 */
14318   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14319   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14320
14321   /* Broadcom SB-1 CPU core */
14322   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14323
14324   /* End marker */
14325   { NULL, 0, 0, 0 }
14326 };
14327
14328
14329 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14330    with a final "000" replaced by "k".  Ignore case.
14331
14332    Note: this function is shared between GCC and GAS.  */
14333
14334 static bfd_boolean
14335 mips_strict_matching_cpu_name_p (canonical, given)
14336      const char *canonical, *given;
14337 {
14338   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14339     given++, canonical++;
14340
14341   return ((*given == 0 && *canonical == 0)
14342           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14343 }
14344
14345
14346 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14347    CPU name.  We've traditionally allowed a lot of variation here.
14348
14349    Note: this function is shared between GCC and GAS.  */
14350
14351 static bfd_boolean
14352 mips_matching_cpu_name_p (canonical, given)
14353      const char *canonical, *given;
14354 {
14355   /* First see if the name matches exactly, or with a final "000"
14356      turned into "k".  */
14357   if (mips_strict_matching_cpu_name_p (canonical, given))
14358     return TRUE;
14359
14360   /* If not, try comparing based on numerical designation alone.
14361      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14362   if (TOLOWER (*given) == 'r')
14363     given++;
14364   if (!ISDIGIT (*given))
14365     return FALSE;
14366
14367   /* Skip over some well-known prefixes in the canonical name,
14368      hoping to find a number there too.  */
14369   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14370     canonical += 2;
14371   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14372     canonical += 2;
14373   else if (TOLOWER (canonical[0]) == 'r')
14374     canonical += 1;
14375
14376   return mips_strict_matching_cpu_name_p (canonical, given);
14377 }
14378
14379
14380 /* Parse an option that takes the name of a processor as its argument.
14381    OPTION is the name of the option and CPU_STRING is the argument.
14382    Return the corresponding processor enumeration if the CPU_STRING is
14383    recognized, otherwise report an error and return null.
14384
14385    A similar function exists in GCC.  */
14386
14387 static const struct mips_cpu_info *
14388 mips_parse_cpu (option, cpu_string)
14389      const char *option, *cpu_string;
14390 {
14391   const struct mips_cpu_info *p;
14392
14393   /* 'from-abi' selects the most compatible architecture for the given
14394      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14395      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14396      version.  Look first at the -mgp options, if given, otherwise base
14397      the choice on MIPS_DEFAULT_64BIT.
14398
14399      Treat NO_ABI like the EABIs.  One reason to do this is that the
14400      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14401      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14402      'mips64', just as we did in the days before 'from-abi'.  */
14403   if (strcasecmp (cpu_string, "from-abi") == 0)
14404     {
14405       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14406         return mips_cpu_info_from_isa (ISA_MIPS1);
14407
14408       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14409         return mips_cpu_info_from_isa (ISA_MIPS3);
14410
14411       if (file_mips_gp32 >= 0)
14412         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14413
14414       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14415                                      ? ISA_MIPS3
14416                                      : ISA_MIPS1);
14417     }
14418
14419   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14420   if (strcasecmp (cpu_string, "default") == 0)
14421     return 0;
14422
14423   for (p = mips_cpu_info_table; p->name != 0; p++)
14424     if (mips_matching_cpu_name_p (p->name, cpu_string))
14425       return p;
14426
14427   as_bad ("Bad value (%s) for %s", cpu_string, option);
14428   return 0;
14429 }
14430
14431 /* Return the canonical processor information for ISA (a member of the
14432    ISA_MIPS* enumeration).  */
14433
14434 static const struct mips_cpu_info *
14435 mips_cpu_info_from_isa (isa)
14436      int isa;
14437 {
14438   int i;
14439
14440   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14441     if (mips_cpu_info_table[i].is_isa
14442         && isa == mips_cpu_info_table[i].isa)
14443       return (&mips_cpu_info_table[i]);
14444
14445   return NULL;
14446 }
14447 \f
14448 static void
14449 show (stream, string, col_p, first_p)
14450      FILE *stream;
14451      const char *string;
14452      int *col_p;
14453      int *first_p;
14454 {
14455   if (*first_p)
14456     {
14457       fprintf (stream, "%24s", "");
14458       *col_p = 24;
14459     }
14460   else
14461     {
14462       fprintf (stream, ", ");
14463       *col_p += 2;
14464     }
14465
14466   if (*col_p + strlen (string) > 72)
14467     {
14468       fprintf (stream, "\n%24s", "");
14469       *col_p = 24;
14470     }
14471
14472   fprintf (stream, "%s", string);
14473   *col_p += strlen (string);
14474
14475   *first_p = 0;
14476 }
14477
14478 void
14479 md_show_usage (stream)
14480      FILE *stream;
14481 {
14482   int column, first;
14483   size_t i;
14484
14485   fprintf (stream, _("\
14486 MIPS options:\n\
14487 -membedded-pic          generate embedded position independent code\n\
14488 -EB                     generate big endian output\n\
14489 -EL                     generate little endian output\n\
14490 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14491 -G NUM                  allow referencing objects up to NUM bytes\n\
14492                         implicitly with the gp register [default 8]\n"));
14493   fprintf (stream, _("\
14494 -mips1                  generate MIPS ISA I instructions\n\
14495 -mips2                  generate MIPS ISA II instructions\n\
14496 -mips3                  generate MIPS ISA III instructions\n\
14497 -mips4                  generate MIPS ISA IV instructions\n\
14498 -mips5                  generate MIPS ISA V instructions\n\
14499 -mips32                 generate MIPS32 ISA instructions\n\
14500 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14501 -mips64                 generate MIPS64 ISA instructions\n\
14502 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14503
14504   first = 1;
14505
14506   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14507     show (stream, mips_cpu_info_table[i].name, &column, &first);
14508   show (stream, "from-abi", &column, &first);
14509   fputc ('\n', stream);
14510
14511   fprintf (stream, _("\
14512 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14513 -no-mCPU                don't generate code specific to CPU.\n\
14514                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14515
14516   first = 1;
14517
14518   show (stream, "3900", &column, &first);
14519   show (stream, "4010", &column, &first);
14520   show (stream, "4100", &column, &first);
14521   show (stream, "4650", &column, &first);
14522   fputc ('\n', stream);
14523
14524   fprintf (stream, _("\
14525 -mips16                 generate mips16 instructions\n\
14526 -no-mips16              do not generate mips16 instructions\n"));
14527   fprintf (stream, _("\
14528 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14529 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14530 -O0                     remove unneeded NOPs, do not swap branches\n\
14531 -O                      remove unneeded NOPs and swap branches\n\
14532 -n                      warn about NOPs generated from macros\n\
14533 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14534 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14535 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14536 #ifdef OBJ_ELF
14537   fprintf (stream, _("\
14538 -KPIC, -call_shared     generate SVR4 position independent code\n\
14539 -non_shared             do not generate position independent code\n\
14540 -xgot                   assume a 32 bit GOT\n\
14541 -mabi=ABI               create ABI conformant object file for:\n"));
14542
14543   first = 1;
14544
14545   show (stream, "32", &column, &first);
14546   show (stream, "o64", &column, &first);
14547   show (stream, "n32", &column, &first);
14548   show (stream, "64", &column, &first);
14549   show (stream, "eabi", &column, &first);
14550
14551   fputc ('\n', stream);
14552
14553   fprintf (stream, _("\
14554 -32                     create o32 ABI object file (default)\n\
14555 -n32                    create n32 ABI object file\n\
14556 -64                     create 64 ABI object file\n"));
14557 #endif
14558 }
14559
14560 enum dwarf2_format
14561 mips_dwarf2_format ()
14562 {
14563   if (mips_abi == N64_ABI)
14564     {
14565 #ifdef TE_IRIX
14566       return dwarf2_format_64bit_irix;
14567 #else
14568       return dwarf2_format_64bit;
14569 #endif
14570     }
14571   else
14572     return dwarf2_format_32bit;
14573 }