[ gas/ChangeLog ]
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too.  */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT  1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP  28
95 #define SP  29
96 #define FP  30
97 #define RA  31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format.  */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section.  */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111                             ? ".data" \
112                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113                             ? ".rdata" \
114                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115                             ? ".rdata" \
116                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117                             ? ".rodata" \
118                             : (abort (), ""))
119
120 /* The ABI to use.  */
121 enum mips_abi_level
122 {
123   NO_ABI = 0,
124   O32_ABI,
125   O64_ABI,
126   N32_ABI,
127   N64_ABI,
128   EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file.  */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* 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 /* The frag containing the last explicit relocation operator.
559    Null if explicit relocations have not been used.  */
560
561 static fragS *prev_reloc_op_frag;
562
563 /* Map normal MIPS register numbers to mips16 register numbers.  */
564
565 #define X ILLEGAL_REG
566 static const int mips32_to_16_reg_map[] =
567 {
568   X, X, 2, 3, 4, 5, 6, 7,
569   X, X, X, X, X, X, X, X,
570   0, 1, X, X, X, X, X, X,
571   X, X, X, X, X, X, X, X
572 };
573 #undef X
574
575 /* Map mips16 register numbers to normal MIPS register numbers.  */
576
577 static const unsigned int mips16_to_32_reg_map[] =
578 {
579   16, 17, 2, 3, 4, 5, 6, 7
580 };
581
582 static int mips_fix_4122_bugs;
583
584 /* We don't relax branches by default, since this causes us to expand
585    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
586    fail to compute the offset before expanding the macro to the most
587    efficient expansion.  */
588
589 static int mips_relax_branch;
590 \f
591 /* Since the MIPS does not have multiple forms of PC relative
592    instructions, we do not have to do relaxing as is done on other
593    platforms.  However, we do have to handle GP relative addressing
594    correctly, which turns out to be a similar problem.
595
596    Every macro that refers to a symbol can occur in (at least) two
597    forms, one with GP relative addressing and one without.  For
598    example, loading a global variable into a register generally uses
599    a macro instruction like this:
600      lw $4,i
601    If i can be addressed off the GP register (this is true if it is in
602    the .sbss or .sdata section, or if it is known to be smaller than
603    the -G argument) this will generate the following instruction:
604      lw $4,i($gp)
605    This instruction will use a GPREL reloc.  If i can not be addressed
606    off the GP register, the following instruction sequence will be used:
607      lui $at,i
608      lw $4,i($at)
609    In this case the first instruction will have a HI16 reloc, and the
610    second reloc will have a LO16 reloc.  Both relocs will be against
611    the symbol i.
612
613    The issue here is that we may not know whether i is GP addressable
614    until after we see the instruction that uses it.  Therefore, we
615    want to be able to choose the final instruction sequence only at
616    the end of the assembly.  This is similar to the way other
617    platforms choose the size of a PC relative instruction only at the
618    end of assembly.
619
620    When generating position independent code we do not use GP
621    addressing in quite the same way, but the issue still arises as
622    external symbols and local symbols must be handled differently.
623
624    We handle these issues by actually generating both possible
625    instruction sequences.  The longer one is put in a frag_var with
626    type rs_machine_dependent.  We encode what to do with the frag in
627    the subtype field.  We encode (1) the number of existing bytes to
628    replace, (2) the number of new bytes to use, (3) the offset from
629    the start of the existing bytes to the first reloc we must generate
630    (that is, the offset is applied from the start of the existing
631    bytes after they are replaced by the new bytes, if any), (4) the
632    offset from the start of the existing bytes to the second reloc,
633    (5) whether a third reloc is needed (the third reloc is always four
634    bytes after the second reloc), and (6) whether to warn if this
635    variant is used (this is sometimes needed if .set nomacro or .set
636    noat is in effect).  All these numbers are reasonably small.
637
638    Generating two instruction sequences must be handled carefully to
639    ensure that delay slots are handled correctly.  Fortunately, there
640    are a limited number of cases.  When the second instruction
641    sequence is generated, append_insn is directed to maintain the
642    existing delay slot information, so it continues to apply to any
643    code after the second instruction sequence.  This means that the
644    second instruction sequence must not impose any requirements not
645    required by the first instruction sequence.
646
647    These variant frags are then handled in functions called by the
648    machine independent code.  md_estimate_size_before_relax returns
649    the final size of the frag.  md_convert_frag sets up the final form
650    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
651    one if needed.  */
652 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
653   ((relax_substateT) \
654    (((old) << 23) \
655     | ((new) << 16) \
656     | (((reloc1) + 64) << 9) \
657     | (((reloc2) + 64) << 2) \
658     | ((reloc3) ? (1 << 1) : 0) \
659     | ((warn) ? 1 : 0)))
660 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
661 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
662 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
663 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
664 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
665 #define RELAX_WARN(i) ((i) & 1)
666
667 /* Branch without likely bit.  If label is out of range, we turn:
668
669         beq reg1, reg2, label
670         delay slot
671
672    into
673
674         bne reg1, reg2, 0f
675         nop
676         j label
677      0: delay slot
678
679    with the following opcode replacements:
680
681         beq <-> bne
682         blez <-> bgtz
683         bltz <-> bgez
684         bc1f <-> bc1t
685
686         bltzal <-> bgezal  (with jal label instead of j label)
687
688    Even though keeping the delay slot instruction in the delay slot of
689    the branch would be more efficient, it would be very tricky to do
690    correctly, because we'd have to introduce a variable frag *after*
691    the delay slot instruction, and expand that instead.  Let's do it
692    the easy way for now, even if the branch-not-taken case now costs
693    one additional instruction.  Out-of-range branches are not supposed
694    to be common, anyway.
695
696    Branch likely.  If label is out of range, we turn:
697
698         beql reg1, reg2, label
699         delay slot (annulled if branch not taken)
700
701    into
702
703         beql reg1, reg2, 1f
704         nop
705         beql $0, $0, 2f
706         nop
707      1: j[al] label
708         delay slot (executed only if branch taken)
709      2:
710
711    It would be possible to generate a shorter sequence by losing the
712    likely bit, generating something like:
713
714         bne reg1, reg2, 0f
715         nop
716         j[al] label
717         delay slot (executed only if branch taken)
718      0:
719
720         beql -> bne
721         bnel -> beq
722         blezl -> bgtz
723         bgtzl -> blez
724         bltzl -> bgez
725         bgezl -> bltz
726         bc1fl -> bc1t
727         bc1tl -> bc1f
728
729         bltzall -> bgezal  (with jal label instead of j label)
730         bgezall -> bltzal  (ditto)
731
732
733    but it's not clear that it would actually improve performance.  */
734 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
735   ((relax_substateT) \
736    (0xc0000000 \
737     | ((toofar) ? 1 : 0) \
738     | ((link) ? 2 : 0) \
739     | ((likely) ? 4 : 0) \
740     | ((uncond) ? 8 : 0)))
741 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
742 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
743 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
744 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
745 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
746
747 /* For mips16 code, we use an entirely different form of relaxation.
748    mips16 supports two versions of most instructions which take
749    immediate values: a small one which takes some small value, and a
750    larger one which takes a 16 bit value.  Since branches also follow
751    this pattern, relaxing these values is required.
752
753    We can assemble both mips16 and normal MIPS code in a single
754    object.  Therefore, we need to support this type of relaxation at
755    the same time that we support the relaxation described above.  We
756    use the high bit of the subtype field to distinguish these cases.
757
758    The information we store for this type of relaxation is the
759    argument code found in the opcode file for this relocation, whether
760    the user explicitly requested a small or extended form, and whether
761    the relocation is in a jump or jal delay slot.  That tells us the
762    size of the value, and how it should be stored.  We also store
763    whether the fragment is considered to be extended or not.  We also
764    store whether this is known to be a branch to a different section,
765    whether we have tried to relax this frag yet, and whether we have
766    ever extended a PC relative fragment because of a shift count.  */
767 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
768   (0x80000000                                                   \
769    | ((type) & 0xff)                                            \
770    | ((small) ? 0x100 : 0)                                      \
771    | ((ext) ? 0x200 : 0)                                        \
772    | ((dslot) ? 0x400 : 0)                                      \
773    | ((jal_dslot) ? 0x800 : 0))
774 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
775 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
776 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
777 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
778 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
779 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
780 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
781 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
782 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
783 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
784 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
785 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
786
787 /* Is the given value a sign-extended 32-bit value?  */
788 #define IS_SEXT_32BIT_NUM(x)                                            \
789   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
790    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
791
792 /* Is the given value a sign-extended 16-bit value?  */
793 #define IS_SEXT_16BIT_NUM(x)                                            \
794   (((x) &~ (offsetT) 0x7fff) == 0                                       \
795    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
796
797 \f
798 /* Prototypes for static functions.  */
799
800 #ifdef __STDC__
801 #define internalError() \
802     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
803 #else
804 #define internalError() as_fatal (_("MIPS internal Error"));
805 #endif
806
807 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
808
809 static inline bfd_boolean reloc_needs_lo_p
810   PARAMS ((bfd_reloc_code_real_type));
811 static inline bfd_boolean fixup_has_matching_lo_p
812   PARAMS ((fixS *));
813 static int insn_uses_reg
814   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
815            enum mips_regclass class));
816 static int reg_needs_delay
817   PARAMS ((unsigned int));
818 static void mips16_mark_labels
819   PARAMS ((void));
820 static void append_insn
821   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
822            bfd_reloc_code_real_type *r));
823 static void mips_no_prev_insn
824   PARAMS ((int));
825 static void mips_emit_delays
826   PARAMS ((bfd_boolean));
827 #ifdef USE_STDARG
828 static void macro_build
829   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
830            const char *fmt, ...));
831 #else
832 static void macro_build ();
833 #endif
834 static void mips16_macro_build
835   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
836 static void macro_build_jalr
837   PARAMS ((int, expressionS *));
838 static void macro_build_lui
839   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
840 static void macro_build_ldst_constoffset
841   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
842            int valreg, int breg));
843 static void set_at
844   PARAMS ((int *counter, int reg, int unsignedp));
845 static void check_absolute_expr
846   PARAMS ((struct mips_cl_insn * ip, expressionS *));
847 static void load_register
848   PARAMS ((int *, int, expressionS *, int));
849 static void load_address
850   PARAMS ((int *, int, expressionS *, int *));
851 static void move_register
852   PARAMS ((int *, int, int));
853 static void macro
854   PARAMS ((struct mips_cl_insn * ip));
855 static void mips16_macro
856   PARAMS ((struct mips_cl_insn * ip));
857 #ifdef LOSING_COMPILER
858 static void macro2
859   PARAMS ((struct mips_cl_insn * ip));
860 #endif
861 static void mips_ip
862   PARAMS ((char *str, struct mips_cl_insn * ip));
863 static void mips16_ip
864   PARAMS ((char *str, struct mips_cl_insn * ip));
865 static void mips16_immed
866   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
867            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
868 static bfd_boolean parse_relocation
869   PARAMS ((char **, bfd_reloc_code_real_type *));
870 static size_t my_getSmallExpression
871   PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
872 static void my_getExpression
873   PARAMS ((expressionS *, char *));
874 #ifdef OBJ_ELF
875 static int support_64bit_objects
876   PARAMS((void));
877 #endif
878 static void mips_set_option_string
879   PARAMS ((const char **, const char *));
880 static symbolS *get_symbol
881   PARAMS ((void));
882 static void mips_align
883   PARAMS ((int to, int fill, symbolS *label));
884 static void s_align
885   PARAMS ((int));
886 static void s_change_sec
887   PARAMS ((int));
888 static void s_change_section
889   PARAMS ((int));
890 static void s_cons
891   PARAMS ((int));
892 static void s_float_cons
893   PARAMS ((int));
894 static void s_mips_globl
895   PARAMS ((int));
896 static void s_option
897   PARAMS ((int));
898 static void s_mipsset
899   PARAMS ((int));
900 static void s_abicalls
901   PARAMS ((int));
902 static void s_cpload
903   PARAMS ((int));
904 static void s_cpsetup
905   PARAMS ((int));
906 static void s_cplocal
907   PARAMS ((int));
908 static void s_cprestore
909   PARAMS ((int));
910 static void s_cpreturn
911   PARAMS ((int));
912 static void s_gpvalue
913   PARAMS ((int));
914 static void s_gpword
915   PARAMS ((int));
916 static void s_gpdword
917   PARAMS ((int));
918 static void s_cpadd
919   PARAMS ((int));
920 static void s_insn
921   PARAMS ((int));
922 static void md_obj_begin
923   PARAMS ((void));
924 static void md_obj_end
925   PARAMS ((void));
926 static long get_number
927   PARAMS ((void));
928 static void s_mips_ent
929   PARAMS ((int));
930 static void s_mips_end
931   PARAMS ((int));
932 static void s_mips_frame
933   PARAMS ((int));
934 static void s_mips_mask
935   PARAMS ((int));
936 static void s_mips_stab
937   PARAMS ((int));
938 static void s_mips_weakext
939   PARAMS ((int));
940 static void s_mips_file
941   PARAMS ((int));
942 static void s_mips_loc
943   PARAMS ((int));
944 static bfd_boolean pic_need_relax
945   PARAMS ((symbolS *, asection *));
946 static int mips16_extended_frag
947   PARAMS ((fragS *, asection *, long));
948 static int relaxed_branch_length (fragS *, asection *, int);
949 static int validate_mips_insn
950   PARAMS ((const struct mips_opcode *));
951 static void show
952   PARAMS ((FILE *, const char *, int *, int *));
953 #ifdef OBJ_ELF
954 static int mips_need_elf_addend_fixup
955   PARAMS ((fixS *));
956 #endif
957
958 /* Table and functions used to map between CPU/ISA names, and
959    ISA levels, and CPU numbers.  */
960
961 struct mips_cpu_info
962 {
963   const char *name;           /* CPU or ISA name.  */
964   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
965   int isa;                    /* ISA level.  */
966   int cpu;                    /* CPU number (default CPU if ISA).  */
967 };
968
969 static void mips_set_architecture
970   PARAMS ((const struct mips_cpu_info *));
971 static void mips_set_tune
972   PARAMS ((const struct mips_cpu_info *));
973 static bfd_boolean mips_strict_matching_cpu_name_p
974   PARAMS ((const char *, const char *));
975 static bfd_boolean mips_matching_cpu_name_p
976   PARAMS ((const char *, const char *));
977 static const struct mips_cpu_info *mips_parse_cpu
978   PARAMS ((const char *, const char *));
979 static const struct mips_cpu_info *mips_cpu_info_from_isa
980   PARAMS ((int));
981 \f
982 /* Pseudo-op table.
983
984    The following pseudo-ops from the Kane and Heinrich MIPS book
985    should be defined here, but are currently unsupported: .alias,
986    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
987
988    The following pseudo-ops from the Kane and Heinrich MIPS book are
989    specific to the type of debugging information being generated, and
990    should be defined by the object format: .aent, .begin, .bend,
991    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
992    .vreg.
993
994    The following pseudo-ops from the Kane and Heinrich MIPS book are
995    not MIPS CPU specific, but are also not specific to the object file
996    format.  This file is probably the best place to define them, but
997    they are not currently supported: .asm0, .endr, .lab, .repeat,
998    .struct.  */
999
1000 static const pseudo_typeS mips_pseudo_table[] =
1001 {
1002   /* MIPS specific pseudo-ops.  */
1003   {"option", s_option, 0},
1004   {"set", s_mipsset, 0},
1005   {"rdata", s_change_sec, 'r'},
1006   {"sdata", s_change_sec, 's'},
1007   {"livereg", s_ignore, 0},
1008   {"abicalls", s_abicalls, 0},
1009   {"cpload", s_cpload, 0},
1010   {"cpsetup", s_cpsetup, 0},
1011   {"cplocal", s_cplocal, 0},
1012   {"cprestore", s_cprestore, 0},
1013   {"cpreturn", s_cpreturn, 0},
1014   {"gpvalue", s_gpvalue, 0},
1015   {"gpword", s_gpword, 0},
1016   {"gpdword", s_gpdword, 0},
1017   {"cpadd", s_cpadd, 0},
1018   {"insn", s_insn, 0},
1019
1020   /* Relatively generic pseudo-ops that happen to be used on MIPS
1021      chips.  */
1022   {"asciiz", stringer, 1},
1023   {"bss", s_change_sec, 'b'},
1024   {"err", s_err, 0},
1025   {"half", s_cons, 1},
1026   {"dword", s_cons, 3},
1027   {"weakext", s_mips_weakext, 0},
1028
1029   /* These pseudo-ops are defined in read.c, but must be overridden
1030      here for one reason or another.  */
1031   {"align", s_align, 0},
1032   {"byte", s_cons, 0},
1033   {"data", s_change_sec, 'd'},
1034   {"double", s_float_cons, 'd'},
1035   {"float", s_float_cons, 'f'},
1036   {"globl", s_mips_globl, 0},
1037   {"global", s_mips_globl, 0},
1038   {"hword", s_cons, 1},
1039   {"int", s_cons, 2},
1040   {"long", s_cons, 2},
1041   {"octa", s_cons, 4},
1042   {"quad", s_cons, 3},
1043   {"section", s_change_section, 0},
1044   {"short", s_cons, 1},
1045   {"single", s_float_cons, 'f'},
1046   {"stabn", s_mips_stab, 'n'},
1047   {"text", s_change_sec, 't'},
1048   {"word", s_cons, 2},
1049
1050   { "extern", ecoff_directive_extern, 0},
1051
1052   { NULL, NULL, 0 },
1053 };
1054
1055 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1056 {
1057   /* These pseudo-ops should be defined by the object file format.
1058      However, a.out doesn't support them, so we have versions here.  */
1059   {"aent", s_mips_ent, 1},
1060   {"bgnb", s_ignore, 0},
1061   {"end", s_mips_end, 0},
1062   {"endb", s_ignore, 0},
1063   {"ent", s_mips_ent, 0},
1064   {"file", s_mips_file, 0},
1065   {"fmask", s_mips_mask, 'F'},
1066   {"frame", s_mips_frame, 0},
1067   {"loc", s_mips_loc, 0},
1068   {"mask", s_mips_mask, 'R'},
1069   {"verstamp", s_ignore, 0},
1070   { NULL, NULL, 0 },
1071 };
1072
1073 extern void pop_insert PARAMS ((const pseudo_typeS *));
1074
1075 void
1076 mips_pop_insert ()
1077 {
1078   pop_insert (mips_pseudo_table);
1079   if (! ECOFF_DEBUGGING)
1080     pop_insert (mips_nonecoff_pseudo_table);
1081 }
1082 \f
1083 /* Symbols labelling the current insn.  */
1084
1085 struct insn_label_list
1086 {
1087   struct insn_label_list *next;
1088   symbolS *label;
1089 };
1090
1091 static struct insn_label_list *insn_labels;
1092 static struct insn_label_list *free_insn_labels;
1093
1094 static void mips_clear_insn_labels PARAMS ((void));
1095
1096 static inline void
1097 mips_clear_insn_labels ()
1098 {
1099   register struct insn_label_list **pl;
1100
1101   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1102     ;
1103   *pl = insn_labels;
1104   insn_labels = NULL;
1105 }
1106 \f
1107 static char *expr_end;
1108
1109 /* Expressions which appear in instructions.  These are set by
1110    mips_ip.  */
1111
1112 static expressionS imm_expr;
1113 static expressionS offset_expr;
1114
1115 /* Relocs associated with imm_expr and offset_expr.  */
1116
1117 static bfd_reloc_code_real_type imm_reloc[3]
1118   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1119 static bfd_reloc_code_real_type offset_reloc[3]
1120   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1121
1122 /* These are set by mips16_ip if an explicit extension is used.  */
1123
1124 static bfd_boolean mips16_small, mips16_ext;
1125
1126 #ifdef OBJ_ELF
1127 /* The pdr segment for per procedure frame/regmask info.  Not used for
1128    ECOFF debugging.  */
1129
1130 static segT pdr_seg;
1131 #endif
1132
1133 /* The default target format to use.  */
1134
1135 const char *
1136 mips_target_format ()
1137 {
1138   switch (OUTPUT_FLAVOR)
1139     {
1140     case bfd_target_aout_flavour:
1141       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1142     case bfd_target_ecoff_flavour:
1143       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1144     case bfd_target_coff_flavour:
1145       return "pe-mips";
1146     case bfd_target_elf_flavour:
1147 #ifdef TE_TMIPS
1148       /* This is traditional mips.  */
1149       return (target_big_endian
1150               ? (HAVE_64BIT_OBJECTS
1151                  ? "elf64-tradbigmips"
1152                  : (HAVE_NEWABI
1153                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1154               : (HAVE_64BIT_OBJECTS
1155                  ? "elf64-tradlittlemips"
1156                  : (HAVE_NEWABI
1157                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1158 #else
1159       return (target_big_endian
1160               ? (HAVE_64BIT_OBJECTS
1161                  ? "elf64-bigmips"
1162                  : (HAVE_NEWABI
1163                     ? "elf32-nbigmips" : "elf32-bigmips"))
1164               : (HAVE_64BIT_OBJECTS
1165                  ? "elf64-littlemips"
1166                  : (HAVE_NEWABI
1167                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1168 #endif
1169     default:
1170       abort ();
1171       return NULL;
1172     }
1173 }
1174
1175 /* This function is called once, at assembler startup time.  It should
1176    set up all the tables, etc. that the MD part of the assembler will need.  */
1177
1178 void
1179 md_begin ()
1180 {
1181   register const char *retval = NULL;
1182   int i = 0;
1183   int broken = 0;
1184
1185   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1186     as_warn (_("Could not set architecture and machine"));
1187
1188   op_hash = hash_new ();
1189
1190   for (i = 0; i < NUMOPCODES;)
1191     {
1192       const char *name = mips_opcodes[i].name;
1193
1194       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1195       if (retval != NULL)
1196         {
1197           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1198                    mips_opcodes[i].name, retval);
1199           /* Probably a memory allocation problem?  Give up now.  */
1200           as_fatal (_("Broken assembler.  No assembly attempted."));
1201         }
1202       do
1203         {
1204           if (mips_opcodes[i].pinfo != INSN_MACRO)
1205             {
1206               if (!validate_mips_insn (&mips_opcodes[i]))
1207                 broken = 1;
1208             }
1209           ++i;
1210         }
1211       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1212     }
1213
1214   mips16_op_hash = hash_new ();
1215
1216   i = 0;
1217   while (i < bfd_mips16_num_opcodes)
1218     {
1219       const char *name = mips16_opcodes[i].name;
1220
1221       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1222       if (retval != NULL)
1223         as_fatal (_("internal: can't hash `%s': %s"),
1224                   mips16_opcodes[i].name, retval);
1225       do
1226         {
1227           if (mips16_opcodes[i].pinfo != INSN_MACRO
1228               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1229                   != mips16_opcodes[i].match))
1230             {
1231               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1232                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1233               broken = 1;
1234             }
1235           ++i;
1236         }
1237       while (i < bfd_mips16_num_opcodes
1238              && strcmp (mips16_opcodes[i].name, name) == 0);
1239     }
1240
1241   if (broken)
1242     as_fatal (_("Broken assembler.  No assembly attempted."));
1243
1244   /* We add all the general register names to the symbol table.  This
1245      helps us detect invalid uses of them.  */
1246   for (i = 0; i < 32; i++)
1247     {
1248       char buf[5];
1249
1250       sprintf (buf, "$%d", i);
1251       symbol_table_insert (symbol_new (buf, reg_section, i,
1252                                        &zero_address_frag));
1253     }
1254   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1255                                    &zero_address_frag));
1256   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1257                                    &zero_address_frag));
1258   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1259                                    &zero_address_frag));
1260   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1261                                    &zero_address_frag));
1262   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1263                                    &zero_address_frag));
1264   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1265                                    &zero_address_frag));
1266   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1267                                    &zero_address_frag));
1268   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1269                                    &zero_address_frag));
1270   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1271                                    &zero_address_frag));
1272
1273   /* If we don't add these register names to the symbol table, they
1274      may end up being added as regular symbols by operand(), and then
1275      make it to the object file as undefined in case they're not
1276      regarded as local symbols.  They're local in o32, since `$' is a
1277      local symbol prefix, but not in n32 or n64.  */
1278   for (i = 0; i < 8; i++)
1279     {
1280       char buf[6];
1281
1282       sprintf (buf, "$fcc%i", i);
1283       symbol_table_insert (symbol_new (buf, reg_section, -1,
1284                                        &zero_address_frag));
1285     }
1286
1287   mips_no_prev_insn (FALSE);
1288
1289   mips_gprmask = 0;
1290   mips_cprmask[0] = 0;
1291   mips_cprmask[1] = 0;
1292   mips_cprmask[2] = 0;
1293   mips_cprmask[3] = 0;
1294
1295   /* set the default alignment for the text section (2**2) */
1296   record_alignment (text_section, 2);
1297
1298   if (USE_GLOBAL_POINTER_OPT)
1299     bfd_set_gp_size (stdoutput, g_switch_value);
1300
1301   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1302     {
1303       /* On a native system, sections must be aligned to 16 byte
1304          boundaries.  When configured for an embedded ELF target, we
1305          don't bother.  */
1306       if (strcmp (TARGET_OS, "elf") != 0)
1307         {
1308           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1309           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1310           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1311         }
1312
1313       /* Create a .reginfo section for register masks and a .mdebug
1314          section for debugging information.  */
1315       {
1316         segT seg;
1317         subsegT subseg;
1318         flagword flags;
1319         segT sec;
1320
1321         seg = now_seg;
1322         subseg = now_subseg;
1323
1324         /* The ABI says this section should be loaded so that the
1325            running program can access it.  However, we don't load it
1326            if we are configured for an embedded target */
1327         flags = SEC_READONLY | SEC_DATA;
1328         if (strcmp (TARGET_OS, "elf") != 0)
1329           flags |= SEC_ALLOC | SEC_LOAD;
1330
1331         if (mips_abi != N64_ABI)
1332           {
1333             sec = subseg_new (".reginfo", (subsegT) 0);
1334
1335             bfd_set_section_flags (stdoutput, sec, flags);
1336             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1337
1338 #ifdef OBJ_ELF
1339             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1340 #endif
1341           }
1342         else
1343           {
1344             /* The 64-bit ABI uses a .MIPS.options section rather than
1345                .reginfo section.  */
1346             sec = subseg_new (".MIPS.options", (subsegT) 0);
1347             bfd_set_section_flags (stdoutput, sec, flags);
1348             bfd_set_section_alignment (stdoutput, sec, 3);
1349
1350 #ifdef OBJ_ELF
1351             /* Set up the option header.  */
1352             {
1353               Elf_Internal_Options opthdr;
1354               char *f;
1355
1356               opthdr.kind = ODK_REGINFO;
1357               opthdr.size = (sizeof (Elf_External_Options)
1358                              + sizeof (Elf64_External_RegInfo));
1359               opthdr.section = 0;
1360               opthdr.info = 0;
1361               f = frag_more (sizeof (Elf_External_Options));
1362               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1363                                              (Elf_External_Options *) f);
1364
1365               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1366             }
1367 #endif
1368           }
1369
1370         if (ECOFF_DEBUGGING)
1371           {
1372             sec = subseg_new (".mdebug", (subsegT) 0);
1373             (void) bfd_set_section_flags (stdoutput, sec,
1374                                           SEC_HAS_CONTENTS | SEC_READONLY);
1375             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1376           }
1377 #ifdef OBJ_ELF
1378         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1379           {
1380             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1381             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1382                                           SEC_READONLY | SEC_RELOC
1383                                           | SEC_DEBUGGING);
1384             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1385           }
1386 #endif
1387
1388         subseg_set (seg, subseg);
1389       }
1390     }
1391
1392   if (! ECOFF_DEBUGGING)
1393     md_obj_begin ();
1394 }
1395
1396 void
1397 md_mips_end ()
1398 {
1399   if (! ECOFF_DEBUGGING)
1400     md_obj_end ();
1401 }
1402
1403 void
1404 md_assemble (str)
1405      char *str;
1406 {
1407   struct mips_cl_insn insn;
1408   bfd_reloc_code_real_type unused_reloc[3]
1409     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1410
1411   imm_expr.X_op = O_absent;
1412   offset_expr.X_op = O_absent;
1413   imm_reloc[0] = BFD_RELOC_UNUSED;
1414   imm_reloc[1] = BFD_RELOC_UNUSED;
1415   imm_reloc[2] = BFD_RELOC_UNUSED;
1416   offset_reloc[0] = BFD_RELOC_UNUSED;
1417   offset_reloc[1] = BFD_RELOC_UNUSED;
1418   offset_reloc[2] = BFD_RELOC_UNUSED;
1419
1420   if (mips_opts.mips16)
1421     mips16_ip (str, &insn);
1422   else
1423     {
1424       mips_ip (str, &insn);
1425       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1426             str, insn.insn_opcode));
1427     }
1428
1429   if (insn_error)
1430     {
1431       as_bad ("%s `%s'", insn_error, str);
1432       return;
1433     }
1434
1435   if (insn.insn_mo->pinfo == INSN_MACRO)
1436     {
1437       if (mips_opts.mips16)
1438         mips16_macro (&insn);
1439       else
1440         macro (&insn);
1441     }
1442   else
1443     {
1444       if (imm_expr.X_op != O_absent)
1445         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1446       else if (offset_expr.X_op != O_absent)
1447         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1448       else
1449         append_insn (NULL, &insn, NULL, unused_reloc);
1450     }
1451 }
1452
1453 /* Return true if the given relocation might need a matching %lo().
1454    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1455    applied to local symbols.  */
1456
1457 static inline bfd_boolean
1458 reloc_needs_lo_p (reloc)
1459      bfd_reloc_code_real_type reloc;
1460 {
1461   return (reloc == BFD_RELOC_HI16_S
1462           || reloc == BFD_RELOC_MIPS_GOT16);
1463 }
1464
1465 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1466    relocation.  */
1467
1468 static inline bfd_boolean
1469 fixup_has_matching_lo_p (fixp)
1470      fixS *fixp;
1471 {
1472   return (fixp->fx_next != NULL
1473           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1474           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1475           && fixp->fx_offset == fixp->fx_next->fx_offset);
1476 }
1477
1478 /* See whether instruction IP reads register REG.  CLASS is the type
1479    of register.  */
1480
1481 static int
1482 insn_uses_reg (ip, reg, class)
1483      struct mips_cl_insn *ip;
1484      unsigned int reg;
1485      enum mips_regclass class;
1486 {
1487   if (class == MIPS16_REG)
1488     {
1489       assert (mips_opts.mips16);
1490       reg = mips16_to_32_reg_map[reg];
1491       class = MIPS_GR_REG;
1492     }
1493
1494   /* Don't report on general register ZERO, since it never changes.  */
1495   if (class == MIPS_GR_REG && reg == ZERO)
1496     return 0;
1497
1498   if (class == MIPS_FP_REG)
1499     {
1500       assert (! mips_opts.mips16);
1501       /* If we are called with either $f0 or $f1, we must check $f0.
1502          This is not optimal, because it will introduce an unnecessary
1503          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1504          need to distinguish reading both $f0 and $f1 or just one of
1505          them.  Note that we don't have to check the other way,
1506          because there is no instruction that sets both $f0 and $f1
1507          and requires a delay.  */
1508       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1509           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1510               == (reg &~ (unsigned) 1)))
1511         return 1;
1512       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1513           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1514               == (reg &~ (unsigned) 1)))
1515         return 1;
1516     }
1517   else if (! mips_opts.mips16)
1518     {
1519       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1520           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1521         return 1;
1522       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1523           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1524         return 1;
1525     }
1526   else
1527     {
1528       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1529           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1530                                     & MIPS16OP_MASK_RX)]
1531               == reg))
1532         return 1;
1533       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1534           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1535                                     & MIPS16OP_MASK_RY)]
1536               == reg))
1537         return 1;
1538       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1539           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1540                                     & MIPS16OP_MASK_MOVE32Z)]
1541               == reg))
1542         return 1;
1543       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1544         return 1;
1545       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1546         return 1;
1547       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1548         return 1;
1549       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1550           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1551               & MIPS16OP_MASK_REGR32) == reg)
1552         return 1;
1553     }
1554
1555   return 0;
1556 }
1557
1558 /* This function returns true if modifying a register requires a
1559    delay.  */
1560
1561 static int
1562 reg_needs_delay (reg)
1563      unsigned int reg;
1564 {
1565   unsigned long prev_pinfo;
1566
1567   prev_pinfo = prev_insn.insn_mo->pinfo;
1568   if (! mips_opts.noreorder
1569       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1570       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1571           || (! gpr_interlocks
1572               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1573     {
1574       /* A load from a coprocessor or from memory.  All load
1575          delays delay the use of general register rt for one
1576          instruction on the r3000.  The r6000 and r4000 use
1577          interlocks.  */
1578       /* Itbl support may require additional care here.  */
1579       know (prev_pinfo & INSN_WRITE_GPR_T);
1580       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1581         return 1;
1582     }
1583
1584   return 0;
1585 }
1586
1587 /* Mark instruction labels in mips16 mode.  This permits the linker to
1588    handle them specially, such as generating jalx instructions when
1589    needed.  We also make them odd for the duration of the assembly, in
1590    order to generate the right sort of code.  We will make them even
1591    in the adjust_symtab routine, while leaving them marked.  This is
1592    convenient for the debugger and the disassembler.  The linker knows
1593    to make them odd again.  */
1594
1595 static void
1596 mips16_mark_labels ()
1597 {
1598   if (mips_opts.mips16)
1599     {
1600       struct insn_label_list *l;
1601       valueT val;
1602
1603       for (l = insn_labels; l != NULL; l = l->next)
1604         {
1605 #ifdef OBJ_ELF
1606           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1607             S_SET_OTHER (l->label, STO_MIPS16);
1608 #endif
1609           val = S_GET_VALUE (l->label);
1610           if ((val & 1) == 0)
1611             S_SET_VALUE (l->label, val + 1);
1612         }
1613     }
1614 }
1615
1616 /* Output an instruction.  PLACE is where to put the instruction; if
1617    it is NULL, this uses frag_more to get room.  IP is the instruction
1618    information.  ADDRESS_EXPR is an operand of the instruction to be
1619    used with RELOC_TYPE.  */
1620
1621 static void
1622 append_insn (place, ip, address_expr, reloc_type)
1623      char *place;
1624      struct mips_cl_insn *ip;
1625      expressionS *address_expr;
1626      bfd_reloc_code_real_type *reloc_type;
1627 {
1628   register unsigned long prev_pinfo, pinfo;
1629   char *f;
1630   fixS *fixp[3];
1631   int nops = 0;
1632
1633   /* Mark instruction labels in mips16 mode.  */
1634   mips16_mark_labels ();
1635
1636   prev_pinfo = prev_insn.insn_mo->pinfo;
1637   pinfo = ip->insn_mo->pinfo;
1638
1639   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1640     {
1641       int prev_prev_nop;
1642
1643       /* If the previous insn required any delay slots, see if we need
1644          to insert a NOP or two.  There are eight kinds of possible
1645          hazards, of which an instruction can have at most one type.
1646          (1) a load from memory delay
1647          (2) a load from a coprocessor delay
1648          (3) an unconditional branch delay
1649          (4) a conditional branch delay
1650          (5) a move to coprocessor register delay
1651          (6) a load coprocessor register from memory delay
1652          (7) a coprocessor condition code delay
1653          (8) a HI/LO special register delay
1654
1655          There are a lot of optimizations we could do that we don't.
1656          In particular, we do not, in general, reorder instructions.
1657          If you use gcc with optimization, it will reorder
1658          instructions and generally do much more optimization then we
1659          do here; repeating all that work in the assembler would only
1660          benefit hand written assembly code, and does not seem worth
1661          it.  */
1662
1663       /* This is how a NOP is emitted.  */
1664 #define emit_nop()                                      \
1665   (mips_opts.mips16                                     \
1666    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1667    : md_number_to_chars (frag_more (4), 0, 4))
1668
1669       /* The previous insn might require a delay slot, depending upon
1670          the contents of the current insn.  */
1671       if (! mips_opts.mips16
1672           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1673           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1674                && ! cop_interlocks)
1675               || (! gpr_interlocks
1676                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1677         {
1678           /* A load from a coprocessor or from memory.  All load
1679              delays delay the use of general register rt for one
1680              instruction on the r3000.  The r6000 and r4000 use
1681              interlocks.  */
1682           /* Itbl support may require additional care here.  */
1683           know (prev_pinfo & INSN_WRITE_GPR_T);
1684           if (mips_optimize == 0
1685               || insn_uses_reg (ip,
1686                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1687                                  & OP_MASK_RT),
1688                                 MIPS_GR_REG))
1689             ++nops;
1690         }
1691       else if (! mips_opts.mips16
1692                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1693                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1694                     && ! cop_interlocks)
1695                    || (mips_opts.isa == ISA_MIPS1
1696                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1697         {
1698           /* A generic coprocessor delay.  The previous instruction
1699              modified a coprocessor general or control register.  If
1700              it modified a control register, we need to avoid any
1701              coprocessor instruction (this is probably not always
1702              required, but it sometimes is).  If it modified a general
1703              register, we avoid using that register.
1704
1705              On the r6000 and r4000 loading a coprocessor register
1706              from memory is interlocked, and does not require a delay.
1707
1708              This case is not handled very well.  There is no special
1709              knowledge of CP0 handling, and the coprocessors other
1710              than the floating point unit are not distinguished at
1711              all.  */
1712           /* Itbl support may require additional care here. FIXME!
1713              Need to modify this to include knowledge about
1714              user specified delays!  */
1715           if (prev_pinfo & INSN_WRITE_FPR_T)
1716             {
1717               if (mips_optimize == 0
1718                   || insn_uses_reg (ip,
1719                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1720                                      & OP_MASK_FT),
1721                                     MIPS_FP_REG))
1722                 ++nops;
1723             }
1724           else if (prev_pinfo & INSN_WRITE_FPR_S)
1725             {
1726               if (mips_optimize == 0
1727                   || insn_uses_reg (ip,
1728                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1729                                      & OP_MASK_FS),
1730                                     MIPS_FP_REG))
1731                 ++nops;
1732             }
1733           else
1734             {
1735               /* We don't know exactly what the previous instruction
1736                  does.  If the current instruction uses a coprocessor
1737                  register, we must insert a NOP.  If previous
1738                  instruction may set the condition codes, and the
1739                  current instruction uses them, we must insert two
1740                  NOPS.  */
1741               /* Itbl support may require additional care here.  */
1742               if (mips_optimize == 0
1743                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1744                       && (pinfo & INSN_READ_COND_CODE)))
1745                 nops += 2;
1746               else if (pinfo & INSN_COP)
1747                 ++nops;
1748             }
1749         }
1750       else if (! mips_opts.mips16
1751                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1752                && (prev_pinfo & INSN_WRITE_COND_CODE)
1753                && ! cop_interlocks)
1754         {
1755           /* The previous instruction sets the coprocessor condition
1756              codes, but does not require a general coprocessor delay
1757              (this means it is a floating point comparison
1758              instruction).  If this instruction uses the condition
1759              codes, we need to insert a single NOP.  */
1760           /* Itbl support may require additional care here.  */
1761           if (mips_optimize == 0
1762               || (pinfo & INSN_READ_COND_CODE))
1763             ++nops;
1764         }
1765
1766       /* If we're fixing up mfhi/mflo for the r7000 and the
1767          previous insn was an mfhi/mflo and the current insn
1768          reads the register that the mfhi/mflo wrote to, then
1769          insert two nops.  */
1770
1771       else if (mips_7000_hilo_fix
1772                && MF_HILO_INSN (prev_pinfo)
1773                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1774                                       & OP_MASK_RD),
1775                                  MIPS_GR_REG))
1776         {
1777           nops += 2;
1778         }
1779
1780       /* If we're fixing up mfhi/mflo for the r7000 and the
1781          2nd previous insn was an mfhi/mflo and the current insn
1782          reads the register that the mfhi/mflo wrote to, then
1783          insert one nop.  */
1784
1785       else if (mips_7000_hilo_fix
1786                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1787                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1788                                        & OP_MASK_RD),
1789                                     MIPS_GR_REG))
1790
1791         {
1792           ++nops;
1793         }
1794
1795       else if (prev_pinfo & INSN_READ_LO)
1796         {
1797           /* The previous instruction reads the LO register; if the
1798              current instruction writes to the LO register, we must
1799              insert two NOPS.  Some newer processors have interlocks.
1800              Also the tx39's multiply instructions can be exectuted
1801              immediatly after a read from HI/LO (without the delay),
1802              though the tx39's divide insns still do require the
1803              delay.  */
1804           if (! (hilo_interlocks
1805                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1806               && (mips_optimize == 0
1807                   || (pinfo & INSN_WRITE_LO)))
1808             nops += 2;
1809           /* Most mips16 branch insns don't have a delay slot.
1810              If a read from LO is immediately followed by a branch
1811              to a write to LO we have a read followed by a write
1812              less than 2 insns away.  We assume the target of
1813              a branch might be a write to LO, and insert a nop
1814              between a read and an immediately following branch.  */
1815           else if (mips_opts.mips16
1816                    && (mips_optimize == 0
1817                        || (pinfo & MIPS16_INSN_BRANCH)))
1818             ++nops;
1819         }
1820       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1821         {
1822           /* The previous instruction reads the HI register; if the
1823              current instruction writes to the HI register, we must
1824              insert a NOP.  Some newer processors have interlocks.
1825              Also the note tx39's multiply above.  */
1826           if (! (hilo_interlocks
1827                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1828               && (mips_optimize == 0
1829                   || (pinfo & INSN_WRITE_HI)))
1830             nops += 2;
1831           /* Most mips16 branch insns don't have a delay slot.
1832              If a read from HI is immediately followed by a branch
1833              to a write to HI we have a read followed by a write
1834              less than 2 insns away.  We assume the target of
1835              a branch might be a write to HI, and insert a nop
1836              between a read and an immediately following branch.  */
1837           else if (mips_opts.mips16
1838                    && (mips_optimize == 0
1839                        || (pinfo & MIPS16_INSN_BRANCH)))
1840             ++nops;
1841         }
1842
1843       /* If the previous instruction was in a noreorder section, then
1844          we don't want to insert the nop after all.  */
1845       /* Itbl support may require additional care here.  */
1846       if (prev_insn_unreordered)
1847         nops = 0;
1848
1849       /* There are two cases which require two intervening
1850          instructions: 1) setting the condition codes using a move to
1851          coprocessor instruction which requires a general coprocessor
1852          delay and then reading the condition codes 2) reading the HI
1853          or LO register and then writing to it (except on processors
1854          which have interlocks).  If we are not already emitting a NOP
1855          instruction, we must check for these cases compared to the
1856          instruction previous to the previous instruction.  */
1857       if ((! mips_opts.mips16
1858            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1859            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1860            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1861            && (pinfo & INSN_READ_COND_CODE)
1862            && ! cop_interlocks)
1863           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1864               && (pinfo & INSN_WRITE_LO)
1865               && ! (hilo_interlocks
1866                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1867           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1868               && (pinfo & INSN_WRITE_HI)
1869               && ! (hilo_interlocks
1870                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1871         prev_prev_nop = 1;
1872       else
1873         prev_prev_nop = 0;
1874
1875       if (prev_prev_insn_unreordered)
1876         prev_prev_nop = 0;
1877
1878       if (prev_prev_nop && nops == 0)
1879         ++nops;
1880
1881       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1882         {
1883           /* We're out of bits in pinfo, so we must resort to string
1884              ops here.  Shortcuts are selected based on opcodes being
1885              limited to the VR4122 instruction set.  */
1886           int min_nops = 0;
1887           const char *pn = prev_insn.insn_mo->name;
1888           const char *tn = ip->insn_mo->name;
1889           if (strncmp(pn, "macc", 4) == 0
1890               || strncmp(pn, "dmacc", 5) == 0)
1891             {
1892               /* Errata 21 - [D]DIV[U] after [D]MACC */
1893               if (strstr (tn, "div"))
1894                 {
1895                   min_nops = 1;
1896                 }
1897
1898               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1899               if (pn[0] == 'd' /* dmacc */
1900                   && (strncmp(tn, "dmult", 5) == 0
1901                       || strncmp(tn, "dmacc", 5) == 0))
1902                 {
1903                   min_nops = 1;
1904                 }
1905
1906               /* Errata 24 - MT{LO,HI} after [D]MACC */
1907               if (strcmp (tn, "mtlo") == 0
1908                   || strcmp (tn, "mthi") == 0)
1909                 {
1910                   min_nops = 1;
1911                 }
1912
1913             }
1914           else if (strncmp(pn, "dmult", 5) == 0
1915                    && (strncmp(tn, "dmult", 5) == 0
1916                        || strncmp(tn, "dmacc", 5) == 0))
1917             {
1918               /* Here is the rest of errata 23.  */
1919               min_nops = 1;
1920             }
1921           if (nops < min_nops)
1922             nops = min_nops;
1923         }
1924
1925       /* If we are being given a nop instruction, don't bother with
1926          one of the nops we would otherwise output.  This will only
1927          happen when a nop instruction is used with mips_optimize set
1928          to 0.  */
1929       if (nops > 0
1930           && ! mips_opts.noreorder
1931           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1932         --nops;
1933
1934       /* Now emit the right number of NOP instructions.  */
1935       if (nops > 0 && ! mips_opts.noreorder)
1936         {
1937           fragS *old_frag;
1938           unsigned long old_frag_offset;
1939           int i;
1940           struct insn_label_list *l;
1941
1942           old_frag = frag_now;
1943           old_frag_offset = frag_now_fix ();
1944
1945           for (i = 0; i < nops; i++)
1946             emit_nop ();
1947
1948           if (listing)
1949             {
1950               listing_prev_line ();
1951               /* We may be at the start of a variant frag.  In case we
1952                  are, make sure there is enough space for the frag
1953                  after the frags created by listing_prev_line.  The
1954                  argument to frag_grow here must be at least as large
1955                  as the argument to all other calls to frag_grow in
1956                  this file.  We don't have to worry about being in the
1957                  middle of a variant frag, because the variants insert
1958                  all needed nop instructions themselves.  */
1959               frag_grow (40);
1960             }
1961
1962           for (l = insn_labels; l != NULL; l = l->next)
1963             {
1964               valueT val;
1965
1966               assert (S_GET_SEGMENT (l->label) == now_seg);
1967               symbol_set_frag (l->label, frag_now);
1968               val = (valueT) frag_now_fix ();
1969               /* mips16 text labels are stored as odd.  */
1970               if (mips_opts.mips16)
1971                 ++val;
1972               S_SET_VALUE (l->label, val);
1973             }
1974
1975 #ifndef NO_ECOFF_DEBUGGING
1976           if (ECOFF_DEBUGGING)
1977             ecoff_fix_loc (old_frag, old_frag_offset);
1978 #endif
1979         }
1980       else if (prev_nop_frag != NULL)
1981         {
1982           /* We have a frag holding nops we may be able to remove.  If
1983              we don't need any nops, we can decrease the size of
1984              prev_nop_frag by the size of one instruction.  If we do
1985              need some nops, we count them in prev_nops_required.  */
1986           if (prev_nop_frag_since == 0)
1987             {
1988               if (nops == 0)
1989                 {
1990                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1991                   --prev_nop_frag_holds;
1992                 }
1993               else
1994                 prev_nop_frag_required += nops;
1995             }
1996           else
1997             {
1998               if (prev_prev_nop == 0)
1999                 {
2000                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2001                   --prev_nop_frag_holds;
2002                 }
2003               else
2004                 ++prev_nop_frag_required;
2005             }
2006
2007           if (prev_nop_frag_holds <= prev_nop_frag_required)
2008             prev_nop_frag = NULL;
2009
2010           ++prev_nop_frag_since;
2011
2012           /* Sanity check: by the time we reach the second instruction
2013              after prev_nop_frag, we should have used up all the nops
2014              one way or another.  */
2015           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2016         }
2017     }
2018
2019   if (place == NULL
2020       && address_expr
2021       && *reloc_type == BFD_RELOC_16_PCREL_S2
2022       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2023           || pinfo & INSN_COND_BRANCH_LIKELY)
2024       && mips_relax_branch
2025       /* Don't try branch relaxation within .set nomacro, or within
2026          .set noat if we use $at for PIC computations.  If it turns
2027          out that the branch was out-of-range, we'll get an error.  */
2028       && !mips_opts.warn_about_macros
2029       && !(mips_opts.noat && mips_pic != NO_PIC)
2030       && !mips_opts.mips16)
2031     {
2032       f = frag_var (rs_machine_dependent,
2033                     relaxed_branch_length
2034                     (NULL, NULL,
2035                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2036                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2037                     RELAX_BRANCH_ENCODE
2038                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2039                      pinfo & INSN_COND_BRANCH_LIKELY,
2040                      pinfo & INSN_WRITE_GPR_31,
2041                      0),
2042                     address_expr->X_add_symbol,
2043                     address_expr->X_add_number,
2044                     0);
2045       *reloc_type = BFD_RELOC_UNUSED;
2046     }
2047   else if (*reloc_type > BFD_RELOC_UNUSED)
2048     {
2049       /* We need to set up a variant frag.  */
2050       assert (mips_opts.mips16 && address_expr != NULL);
2051       f = frag_var (rs_machine_dependent, 4, 0,
2052                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2053                                          mips16_small, mips16_ext,
2054                                          (prev_pinfo
2055                                           & INSN_UNCOND_BRANCH_DELAY),
2056                                          (*prev_insn_reloc_type
2057                                           == BFD_RELOC_MIPS16_JMP)),
2058                     make_expr_symbol (address_expr), 0, NULL);
2059     }
2060   else if (place != NULL)
2061     f = place;
2062   else if (mips_opts.mips16
2063            && ! ip->use_extend
2064            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2065     {
2066       /* Make sure there is enough room to swap this instruction with
2067          a following jump instruction.  */
2068       frag_grow (6);
2069       f = frag_more (2);
2070     }
2071   else
2072     {
2073       if (mips_opts.mips16
2074           && mips_opts.noreorder
2075           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2076         as_warn (_("extended instruction in delay slot"));
2077
2078       f = frag_more (4);
2079     }
2080
2081   fixp[0] = fixp[1] = fixp[2] = NULL;
2082   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2083     {
2084       if (address_expr->X_op == O_constant)
2085         {
2086           valueT tmp;
2087
2088           switch (*reloc_type)
2089             {
2090             case BFD_RELOC_32:
2091               ip->insn_opcode |= address_expr->X_add_number;
2092               break;
2093
2094             case BFD_RELOC_MIPS_HIGHEST:
2095               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2096               tmp >>= 16;
2097               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2098               break;
2099
2100             case BFD_RELOC_MIPS_HIGHER:
2101               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2102               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2103               break;
2104
2105             case BFD_RELOC_HI16_S:
2106               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2107                                   >> 16) & 0xffff;
2108               break;
2109
2110             case BFD_RELOC_HI16:
2111               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2112               break;
2113
2114             case BFD_RELOC_LO16:
2115             case BFD_RELOC_MIPS_GOT_DISP:
2116               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2117               break;
2118
2119             case BFD_RELOC_MIPS_JMP:
2120               if ((address_expr->X_add_number & 3) != 0)
2121                 as_bad (_("jump to misaligned address (0x%lx)"),
2122                         (unsigned long) address_expr->X_add_number);
2123               if (address_expr->X_add_number & ~0xfffffff)
2124                 as_bad (_("jump address range overflow (0x%lx)"),
2125                         (unsigned long) address_expr->X_add_number);
2126               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2127               break;
2128
2129             case BFD_RELOC_MIPS16_JMP:
2130               if ((address_expr->X_add_number & 3) != 0)
2131                 as_bad (_("jump to misaligned address (0x%lx)"),
2132                         (unsigned long) address_expr->X_add_number);
2133               if (address_expr->X_add_number & ~0xfffffff)
2134                 as_bad (_("jump address range overflow (0x%lx)"),
2135                         (unsigned long) address_expr->X_add_number);
2136               ip->insn_opcode |=
2137                 (((address_expr->X_add_number & 0x7c0000) << 3)
2138                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2139                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2140               break;
2141
2142             case BFD_RELOC_16_PCREL_S2:
2143               goto need_reloc;
2144
2145             default:
2146               internalError ();
2147             }
2148         }
2149       else
2150         {
2151         need_reloc:
2152           /* Don't generate a reloc if we are writing into a variant frag.  */
2153           if (place == NULL)
2154             {
2155               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2156                                      address_expr,
2157                                      *reloc_type == BFD_RELOC_16_PCREL_S2,
2158                                      reloc_type[0]);
2159
2160               /* These relocations can have an addend that won't fit in
2161                  4 octets for 64bit assembly.  */
2162               if (HAVE_64BIT_GPRS &&
2163                   (*reloc_type == BFD_RELOC_16
2164                    || *reloc_type == BFD_RELOC_32
2165                    || *reloc_type == BFD_RELOC_MIPS_JMP
2166                    || *reloc_type == BFD_RELOC_HI16_S
2167                    || *reloc_type == BFD_RELOC_LO16
2168                    || *reloc_type == BFD_RELOC_GPREL16
2169                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2170                    || *reloc_type == BFD_RELOC_GPREL32
2171                    || *reloc_type == BFD_RELOC_64
2172                    || *reloc_type == BFD_RELOC_CTOR
2173                    || *reloc_type == BFD_RELOC_MIPS_SUB
2174                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2175                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2176                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2177                    || *reloc_type == BFD_RELOC_MIPS_REL16
2178                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2179                 fixp[0]->fx_no_overflow = 1;
2180
2181               if (reloc_needs_lo_p (*reloc_type))
2182                 {
2183                   struct mips_hi_fixup *hi_fixup;
2184
2185                   /* Reuse the last entry if it already has a matching %lo.  */
2186                   hi_fixup = mips_hi_fixup_list;
2187                   if (hi_fixup == 0
2188                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2189                     {
2190                       hi_fixup = ((struct mips_hi_fixup *)
2191                                   xmalloc (sizeof (struct mips_hi_fixup)));
2192                       hi_fixup->next = mips_hi_fixup_list;
2193                       mips_hi_fixup_list = hi_fixup;
2194                     }
2195                   hi_fixup->fixp = fixp[0];
2196                   hi_fixup->seg = now_seg;
2197                 }
2198
2199               if (reloc_type[1] != BFD_RELOC_UNUSED)
2200                 {
2201                   /* FIXME: This symbol can be one of
2202                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2203                   address_expr->X_op = O_absent;
2204                   address_expr->X_add_symbol = 0;
2205                   address_expr->X_add_number = 0;
2206
2207                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2208                                          4, address_expr, FALSE,
2209                                          reloc_type[1]);
2210
2211                   /* These relocations can have an addend that won't fit in
2212                      4 octets for 64bit assembly.  */
2213                   if (HAVE_64BIT_GPRS &&
2214                       (*reloc_type == BFD_RELOC_16
2215                        || *reloc_type == BFD_RELOC_32
2216                        || *reloc_type == BFD_RELOC_MIPS_JMP
2217                        || *reloc_type == BFD_RELOC_HI16_S
2218                        || *reloc_type == BFD_RELOC_LO16
2219                        || *reloc_type == BFD_RELOC_GPREL16
2220                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2221                        || *reloc_type == BFD_RELOC_GPREL32
2222                        || *reloc_type == BFD_RELOC_64
2223                        || *reloc_type == BFD_RELOC_CTOR
2224                        || *reloc_type == BFD_RELOC_MIPS_SUB
2225                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2226                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2227                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2228                        || *reloc_type == BFD_RELOC_MIPS_REL16
2229                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2230                     fixp[1]->fx_no_overflow = 1;
2231
2232                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2233                     {
2234                       address_expr->X_op = O_absent;
2235                       address_expr->X_add_symbol = 0;
2236                       address_expr->X_add_number = 0;
2237
2238                       fixp[2] = fix_new_exp (frag_now,
2239                                              f - frag_now->fr_literal, 4,
2240                                              address_expr, FALSE,
2241                                              reloc_type[2]);
2242
2243                       /* These relocations can have an addend that won't fit in
2244                          4 octets for 64bit assembly.  */
2245                       if (HAVE_64BIT_GPRS &&
2246                           (*reloc_type == BFD_RELOC_16
2247                            || *reloc_type == BFD_RELOC_32
2248                            || *reloc_type == BFD_RELOC_MIPS_JMP
2249                            || *reloc_type == BFD_RELOC_HI16_S
2250                            || *reloc_type == BFD_RELOC_LO16
2251                            || *reloc_type == BFD_RELOC_GPREL16
2252                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2253                            || *reloc_type == BFD_RELOC_GPREL32
2254                            || *reloc_type == BFD_RELOC_64
2255                            || *reloc_type == BFD_RELOC_CTOR
2256                            || *reloc_type == BFD_RELOC_MIPS_SUB
2257                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2258                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2259                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2260                            || *reloc_type == BFD_RELOC_MIPS_REL16
2261                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2262                         fixp[2]->fx_no_overflow = 1;
2263                     }
2264                 }
2265             }
2266         }
2267     }
2268
2269   if (! mips_opts.mips16)
2270     {
2271       md_number_to_chars (f, ip->insn_opcode, 4);
2272 #ifdef OBJ_ELF
2273       dwarf2_emit_insn (4);
2274 #endif
2275     }
2276   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2277     {
2278       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2279       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2280 #ifdef OBJ_ELF
2281       dwarf2_emit_insn (4);
2282 #endif
2283     }
2284   else
2285     {
2286       if (ip->use_extend)
2287         {
2288           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2289           f += 2;
2290         }
2291       md_number_to_chars (f, ip->insn_opcode, 2);
2292 #ifdef OBJ_ELF
2293       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2294 #endif
2295     }
2296
2297   /* Update the register mask information.  */
2298   if (! mips_opts.mips16)
2299     {
2300       if (pinfo & INSN_WRITE_GPR_D)
2301         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2302       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2303         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2304       if (pinfo & INSN_READ_GPR_S)
2305         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2306       if (pinfo & INSN_WRITE_GPR_31)
2307         mips_gprmask |= 1 << RA;
2308       if (pinfo & INSN_WRITE_FPR_D)
2309         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2310       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2311         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2312       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2313         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2314       if ((pinfo & INSN_READ_FPR_R) != 0)
2315         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2316       if (pinfo & INSN_COP)
2317         {
2318           /* We don't keep enough information to sort these cases out.
2319              The itbl support does keep this information however, although
2320              we currently don't support itbl fprmats as part of the cop
2321              instruction.  May want to add this support in the future.  */
2322         }
2323       /* Never set the bit for $0, which is always zero.  */
2324       mips_gprmask &= ~1 << 0;
2325     }
2326   else
2327     {
2328       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2329         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2330                               & MIPS16OP_MASK_RX);
2331       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2332         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2333                               & MIPS16OP_MASK_RY);
2334       if (pinfo & MIPS16_INSN_WRITE_Z)
2335         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2336                               & MIPS16OP_MASK_RZ);
2337       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2338         mips_gprmask |= 1 << TREG;
2339       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2340         mips_gprmask |= 1 << SP;
2341       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2342         mips_gprmask |= 1 << RA;
2343       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2344         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2345       if (pinfo & MIPS16_INSN_READ_Z)
2346         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2347                               & MIPS16OP_MASK_MOVE32Z);
2348       if (pinfo & MIPS16_INSN_READ_GPR_X)
2349         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2350                               & MIPS16OP_MASK_REGR32);
2351     }
2352
2353   if (place == NULL && ! mips_opts.noreorder)
2354     {
2355       /* Filling the branch delay slot is more complex.  We try to
2356          switch the branch with the previous instruction, which we can
2357          do if the previous instruction does not set up a condition
2358          that the branch tests and if the branch is not itself the
2359          target of any branch.  */
2360       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2361           || (pinfo & INSN_COND_BRANCH_DELAY))
2362         {
2363           if (mips_optimize < 2
2364               /* If we have seen .set volatile or .set nomove, don't
2365                  optimize.  */
2366               || mips_opts.nomove != 0
2367               /* If we had to emit any NOP instructions, then we
2368                  already know we can not swap.  */
2369               || nops != 0
2370               /* If we don't even know the previous insn, we can not
2371                  swap.  */
2372               || ! prev_insn_valid
2373               /* If the previous insn is already in a branch delay
2374                  slot, then we can not swap.  */
2375               || prev_insn_is_delay_slot
2376               /* If the previous previous insn was in a .set
2377                  noreorder, we can't swap.  Actually, the MIPS
2378                  assembler will swap in this situation.  However, gcc
2379                  configured -with-gnu-as will generate code like
2380                    .set noreorder
2381                    lw   $4,XXX
2382                    .set reorder
2383                    INSN
2384                    bne  $4,$0,foo
2385                  in which we can not swap the bne and INSN.  If gcc is
2386                  not configured -with-gnu-as, it does not output the
2387                  .set pseudo-ops.  We don't have to check
2388                  prev_insn_unreordered, because prev_insn_valid will
2389                  be 0 in that case.  We don't want to use
2390                  prev_prev_insn_valid, because we do want to be able
2391                  to swap at the start of a function.  */
2392               || prev_prev_insn_unreordered
2393               /* If the branch is itself the target of a branch, we
2394                  can not swap.  We cheat on this; all we check for is
2395                  whether there is a label on this instruction.  If
2396                  there are any branches to anything other than a
2397                  label, users must use .set noreorder.  */
2398               || insn_labels != NULL
2399               /* If the previous instruction is in a variant frag, we
2400                  can not do the swap.  This does not apply to the
2401                  mips16, which uses variant frags for different
2402                  purposes.  */
2403               || (! mips_opts.mips16
2404                   && prev_insn_frag->fr_type == rs_machine_dependent)
2405               /* If the branch reads the condition codes, we don't
2406                  even try to swap, because in the sequence
2407                    ctc1 $X,$31
2408                    INSN
2409                    INSN
2410                    bc1t LABEL
2411                  we can not swap, and I don't feel like handling that
2412                  case.  */
2413               || (! mips_opts.mips16
2414                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2415                   && (pinfo & INSN_READ_COND_CODE))
2416               /* We can not swap with an instruction that requires a
2417                  delay slot, becase the target of the branch might
2418                  interfere with that instruction.  */
2419               || (! mips_opts.mips16
2420                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2421                   && (prev_pinfo
2422               /* Itbl support may require additional care here.  */
2423                       & (INSN_LOAD_COPROC_DELAY
2424                          | INSN_COPROC_MOVE_DELAY
2425                          | INSN_WRITE_COND_CODE)))
2426               || (! (hilo_interlocks
2427                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2428                   && (prev_pinfo
2429                       & (INSN_READ_LO
2430                          | INSN_READ_HI)))
2431               || (! mips_opts.mips16
2432                   && ! gpr_interlocks
2433                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2434               || (! mips_opts.mips16
2435                   && mips_opts.isa == ISA_MIPS1
2436                   /* Itbl support may require additional care here.  */
2437                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2438               /* We can not swap with a branch instruction.  */
2439               || (prev_pinfo
2440                   & (INSN_UNCOND_BRANCH_DELAY
2441                      | INSN_COND_BRANCH_DELAY
2442                      | INSN_COND_BRANCH_LIKELY))
2443               /* We do not swap with a trap instruction, since it
2444                  complicates trap handlers to have the trap
2445                  instruction be in a delay slot.  */
2446               || (prev_pinfo & INSN_TRAP)
2447               /* If the branch reads a register that the previous
2448                  instruction sets, we can not swap.  */
2449               || (! mips_opts.mips16
2450                   && (prev_pinfo & INSN_WRITE_GPR_T)
2451                   && insn_uses_reg (ip,
2452                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2453                                      & OP_MASK_RT),
2454                                     MIPS_GR_REG))
2455               || (! mips_opts.mips16
2456                   && (prev_pinfo & INSN_WRITE_GPR_D)
2457                   && insn_uses_reg (ip,
2458                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2459                                      & OP_MASK_RD),
2460                                     MIPS_GR_REG))
2461               || (mips_opts.mips16
2462                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2463                        && insn_uses_reg (ip,
2464                                          ((prev_insn.insn_opcode
2465                                            >> MIPS16OP_SH_RX)
2466                                           & MIPS16OP_MASK_RX),
2467                                          MIPS16_REG))
2468                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2469                           && insn_uses_reg (ip,
2470                                             ((prev_insn.insn_opcode
2471                                               >> MIPS16OP_SH_RY)
2472                                              & MIPS16OP_MASK_RY),
2473                                             MIPS16_REG))
2474                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2475                           && insn_uses_reg (ip,
2476                                             ((prev_insn.insn_opcode
2477                                               >> MIPS16OP_SH_RZ)
2478                                              & MIPS16OP_MASK_RZ),
2479                                             MIPS16_REG))
2480                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2481                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2482                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2483                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2484                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2485                           && insn_uses_reg (ip,
2486                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2487                                                                      insn_opcode),
2488                                             MIPS_GR_REG))))
2489               /* If the branch writes a register that the previous
2490                  instruction sets, we can not swap (we know that
2491                  branches write only to RD or to $31).  */
2492               || (! mips_opts.mips16
2493                   && (prev_pinfo & INSN_WRITE_GPR_T)
2494                   && (((pinfo & INSN_WRITE_GPR_D)
2495                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2496                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2497                       || ((pinfo & INSN_WRITE_GPR_31)
2498                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2499                                & OP_MASK_RT)
2500                               == RA))))
2501               || (! mips_opts.mips16
2502                   && (prev_pinfo & INSN_WRITE_GPR_D)
2503                   && (((pinfo & INSN_WRITE_GPR_D)
2504                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2505                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2506                       || ((pinfo & INSN_WRITE_GPR_31)
2507                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2508                                & OP_MASK_RD)
2509                               == RA))))
2510               || (mips_opts.mips16
2511                   && (pinfo & MIPS16_INSN_WRITE_31)
2512                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2513                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2514                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2515                               == RA))))
2516               /* If the branch writes a register that the previous
2517                  instruction reads, we can not swap (we know that
2518                  branches only write to RD or to $31).  */
2519               || (! mips_opts.mips16
2520                   && (pinfo & INSN_WRITE_GPR_D)
2521                   && insn_uses_reg (&prev_insn,
2522                                     ((ip->insn_opcode >> OP_SH_RD)
2523                                      & OP_MASK_RD),
2524                                     MIPS_GR_REG))
2525               || (! mips_opts.mips16
2526                   && (pinfo & INSN_WRITE_GPR_31)
2527                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2528               || (mips_opts.mips16
2529                   && (pinfo & MIPS16_INSN_WRITE_31)
2530                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2531               /* If we are generating embedded PIC code, the branch
2532                  might be expanded into a sequence which uses $at, so
2533                  we can't swap with an instruction which reads it.  */
2534               || (mips_pic == EMBEDDED_PIC
2535                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2536               /* If the previous previous instruction has a load
2537                  delay, and sets a register that the branch reads, we
2538                  can not swap.  */
2539               || (! mips_opts.mips16
2540                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2541               /* Itbl support may require additional care here.  */
2542                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2543                       || (! gpr_interlocks
2544                           && (prev_prev_insn.insn_mo->pinfo
2545                               & INSN_LOAD_MEMORY_DELAY)))
2546                   && insn_uses_reg (ip,
2547                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2548                                      & OP_MASK_RT),
2549                                     MIPS_GR_REG))
2550               /* If one instruction sets a condition code and the
2551                  other one uses a condition code, we can not swap.  */
2552               || ((pinfo & INSN_READ_COND_CODE)
2553                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2554               || ((pinfo & INSN_WRITE_COND_CODE)
2555                   && (prev_pinfo & INSN_READ_COND_CODE))
2556               /* If the previous instruction uses the PC, we can not
2557                  swap.  */
2558               || (mips_opts.mips16
2559                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2560               /* If the previous instruction was extended, we can not
2561                  swap.  */
2562               || (mips_opts.mips16 && prev_insn_extended)
2563               /* If the previous instruction had a fixup in mips16
2564                  mode, we can not swap.  This normally means that the
2565                  previous instruction was a 4 byte branch anyhow.  */
2566               || (mips_opts.mips16 && prev_insn_fixp[0])
2567               /* If the previous instruction is a sync, sync.l, or
2568                  sync.p, we can not swap.  */
2569               || (prev_pinfo & INSN_SYNC))
2570             {
2571               /* We could do even better for unconditional branches to
2572                  portions of this object file; we could pick up the
2573                  instruction at the destination, put it in the delay
2574                  slot, and bump the destination address.  */
2575               emit_nop ();
2576               /* Update the previous insn information.  */
2577               prev_prev_insn = *ip;
2578               prev_insn.insn_mo = &dummy_opcode;
2579             }
2580           else
2581             {
2582               /* It looks like we can actually do the swap.  */
2583               if (! mips_opts.mips16)
2584                 {
2585                   char *prev_f;
2586                   char temp[4];
2587
2588                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2589                   memcpy (temp, prev_f, 4);
2590                   memcpy (prev_f, f, 4);
2591                   memcpy (f, temp, 4);
2592                   if (prev_insn_fixp[0])
2593                     {
2594                       prev_insn_fixp[0]->fx_frag = frag_now;
2595                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2596                     }
2597                   if (prev_insn_fixp[1])
2598                     {
2599                       prev_insn_fixp[1]->fx_frag = frag_now;
2600                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2601                     }
2602                   if (prev_insn_fixp[2])
2603                     {
2604                       prev_insn_fixp[2]->fx_frag = frag_now;
2605                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2606                     }
2607                   if (fixp[0])
2608                     {
2609                       fixp[0]->fx_frag = prev_insn_frag;
2610                       fixp[0]->fx_where = prev_insn_where;
2611                     }
2612                   if (fixp[1])
2613                     {
2614                       fixp[1]->fx_frag = prev_insn_frag;
2615                       fixp[1]->fx_where = prev_insn_where;
2616                     }
2617                   if (fixp[2])
2618                     {
2619                       fixp[2]->fx_frag = prev_insn_frag;
2620                       fixp[2]->fx_where = prev_insn_where;
2621                     }
2622                 }
2623               else
2624                 {
2625                   char *prev_f;
2626                   char temp[2];
2627
2628                   assert (prev_insn_fixp[0] == NULL);
2629                   assert (prev_insn_fixp[1] == NULL);
2630                   assert (prev_insn_fixp[2] == NULL);
2631                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2632                   memcpy (temp, prev_f, 2);
2633                   memcpy (prev_f, f, 2);
2634                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2635                     {
2636                       assert (*reloc_type == BFD_RELOC_UNUSED);
2637                       memcpy (f, temp, 2);
2638                     }
2639                   else
2640                     {
2641                       memcpy (f, f + 2, 2);
2642                       memcpy (f + 2, temp, 2);
2643                     }
2644                   if (fixp[0])
2645                     {
2646                       fixp[0]->fx_frag = prev_insn_frag;
2647                       fixp[0]->fx_where = prev_insn_where;
2648                     }
2649                   if (fixp[1])
2650                     {
2651                       fixp[1]->fx_frag = prev_insn_frag;
2652                       fixp[1]->fx_where = prev_insn_where;
2653                     }
2654                   if (fixp[2])
2655                     {
2656                       fixp[2]->fx_frag = prev_insn_frag;
2657                       fixp[2]->fx_where = prev_insn_where;
2658                     }
2659                 }
2660
2661               /* Update the previous insn information; leave prev_insn
2662                  unchanged.  */
2663               prev_prev_insn = *ip;
2664             }
2665           prev_insn_is_delay_slot = 1;
2666
2667           /* If that was an unconditional branch, forget the previous
2668              insn information.  */
2669           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2670             {
2671               prev_prev_insn.insn_mo = &dummy_opcode;
2672               prev_insn.insn_mo = &dummy_opcode;
2673             }
2674
2675           prev_insn_fixp[0] = NULL;
2676           prev_insn_fixp[1] = NULL;
2677           prev_insn_fixp[2] = NULL;
2678           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2679           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2680           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2681           prev_insn_extended = 0;
2682         }
2683       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2684         {
2685           /* We don't yet optimize a branch likely.  What we should do
2686              is look at the target, copy the instruction found there
2687              into the delay slot, and increment the branch to jump to
2688              the next instruction.  */
2689           emit_nop ();
2690           /* Update the previous insn information.  */
2691           prev_prev_insn = *ip;
2692           prev_insn.insn_mo = &dummy_opcode;
2693           prev_insn_fixp[0] = NULL;
2694           prev_insn_fixp[1] = NULL;
2695           prev_insn_fixp[2] = NULL;
2696           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2697           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2698           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2699           prev_insn_extended = 0;
2700         }
2701       else
2702         {
2703           /* Update the previous insn information.  */
2704           if (nops > 0)
2705             prev_prev_insn.insn_mo = &dummy_opcode;
2706           else
2707             prev_prev_insn = prev_insn;
2708           prev_insn = *ip;
2709
2710           /* Any time we see a branch, we always fill the delay slot
2711              immediately; since this insn is not a branch, we know it
2712              is not in a delay slot.  */
2713           prev_insn_is_delay_slot = 0;
2714
2715           prev_insn_fixp[0] = fixp[0];
2716           prev_insn_fixp[1] = fixp[1];
2717           prev_insn_fixp[2] = fixp[2];
2718           prev_insn_reloc_type[0] = reloc_type[0];
2719           prev_insn_reloc_type[1] = reloc_type[1];
2720           prev_insn_reloc_type[2] = reloc_type[2];
2721           if (mips_opts.mips16)
2722             prev_insn_extended = (ip->use_extend
2723                                   || *reloc_type > BFD_RELOC_UNUSED);
2724         }
2725
2726       prev_prev_insn_unreordered = prev_insn_unreordered;
2727       prev_insn_unreordered = 0;
2728       prev_insn_frag = frag_now;
2729       prev_insn_where = f - frag_now->fr_literal;
2730       prev_insn_valid = 1;
2731     }
2732   else if (place == NULL)
2733     {
2734       /* We need to record a bit of information even when we are not
2735          reordering, in order to determine the base address for mips16
2736          PC relative relocs.  */
2737       prev_prev_insn = prev_insn;
2738       prev_insn = *ip;
2739       prev_insn_reloc_type[0] = reloc_type[0];
2740       prev_insn_reloc_type[1] = reloc_type[1];
2741       prev_insn_reloc_type[2] = reloc_type[2];
2742       prev_prev_insn_unreordered = prev_insn_unreordered;
2743       prev_insn_unreordered = 1;
2744     }
2745
2746   /* We just output an insn, so the next one doesn't have a label.  */
2747   mips_clear_insn_labels ();
2748 }
2749
2750 /* This function forgets that there was any previous instruction or
2751    label.  If PRESERVE is non-zero, it remembers enough information to
2752    know whether nops are needed before a noreorder section.  */
2753
2754 static void
2755 mips_no_prev_insn (preserve)
2756      int preserve;
2757 {
2758   if (! preserve)
2759     {
2760       prev_insn.insn_mo = &dummy_opcode;
2761       prev_prev_insn.insn_mo = &dummy_opcode;
2762       prev_nop_frag = NULL;
2763       prev_nop_frag_holds = 0;
2764       prev_nop_frag_required = 0;
2765       prev_nop_frag_since = 0;
2766     }
2767   prev_insn_valid = 0;
2768   prev_insn_is_delay_slot = 0;
2769   prev_insn_unreordered = 0;
2770   prev_insn_extended = 0;
2771   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2772   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2773   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2774   prev_prev_insn_unreordered = 0;
2775   mips_clear_insn_labels ();
2776 }
2777
2778 /* This function must be called whenever we turn on noreorder or emit
2779    something other than instructions.  It inserts any NOPS which might
2780    be needed by the previous instruction, and clears the information
2781    kept for the previous instructions.  The INSNS parameter is true if
2782    instructions are to follow.  */
2783
2784 static void
2785 mips_emit_delays (insns)
2786      bfd_boolean insns;
2787 {
2788   if (! mips_opts.noreorder)
2789     {
2790       int nops;
2791
2792       nops = 0;
2793       if ((! mips_opts.mips16
2794            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2795            && (! cop_interlocks
2796                && (prev_insn.insn_mo->pinfo
2797                    & (INSN_LOAD_COPROC_DELAY
2798                       | INSN_COPROC_MOVE_DELAY
2799                       | INSN_WRITE_COND_CODE))))
2800           || (! hilo_interlocks
2801               && (prev_insn.insn_mo->pinfo
2802                   & (INSN_READ_LO
2803                      | INSN_READ_HI)))
2804           || (! mips_opts.mips16
2805               && ! gpr_interlocks
2806               && (prev_insn.insn_mo->pinfo
2807                   & INSN_LOAD_MEMORY_DELAY))
2808           || (! mips_opts.mips16
2809               && mips_opts.isa == ISA_MIPS1
2810               && (prev_insn.insn_mo->pinfo
2811                   & INSN_COPROC_MEMORY_DELAY)))
2812         {
2813           /* Itbl support may require additional care here.  */
2814           ++nops;
2815           if ((! mips_opts.mips16
2816                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2817                && (! cop_interlocks
2818                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2819               || (! hilo_interlocks
2820                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2821                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2822             ++nops;
2823
2824           if (prev_insn_unreordered)
2825             nops = 0;
2826         }
2827       else if ((! mips_opts.mips16
2828                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2829                 && (! cop_interlocks
2830                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2831                || (! hilo_interlocks
2832                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2833                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2834         {
2835           /* Itbl support may require additional care here.  */
2836           if (! prev_prev_insn_unreordered)
2837             ++nops;
2838         }
2839
2840       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2841         {
2842           int min_nops = 0;
2843           const char *pn = prev_insn.insn_mo->name;
2844           if (strncmp(pn, "macc", 4) == 0
2845               || strncmp(pn, "dmacc", 5) == 0
2846               || strncmp(pn, "dmult", 5) == 0)
2847             {
2848               min_nops = 1;
2849             }
2850           if (nops < min_nops)
2851             nops = min_nops;
2852         }
2853
2854       if (nops > 0)
2855         {
2856           struct insn_label_list *l;
2857
2858           if (insns)
2859             {
2860               /* Record the frag which holds the nop instructions, so
2861                  that we can remove them if we don't need them.  */
2862               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2863               prev_nop_frag = frag_now;
2864               prev_nop_frag_holds = nops;
2865               prev_nop_frag_required = 0;
2866               prev_nop_frag_since = 0;
2867             }
2868
2869           for (; nops > 0; --nops)
2870             emit_nop ();
2871
2872           if (insns)
2873             {
2874               /* Move on to a new frag, so that it is safe to simply
2875                  decrease the size of prev_nop_frag.  */
2876               frag_wane (frag_now);
2877               frag_new (0);
2878             }
2879
2880           for (l = insn_labels; l != NULL; l = l->next)
2881             {
2882               valueT val;
2883
2884               assert (S_GET_SEGMENT (l->label) == now_seg);
2885               symbol_set_frag (l->label, frag_now);
2886               val = (valueT) frag_now_fix ();
2887               /* mips16 text labels are stored as odd.  */
2888               if (mips_opts.mips16)
2889                 ++val;
2890               S_SET_VALUE (l->label, val);
2891             }
2892         }
2893     }
2894
2895   /* Mark instruction labels in mips16 mode.  */
2896   if (insns)
2897     mips16_mark_labels ();
2898
2899   mips_no_prev_insn (insns);
2900 }
2901
2902 /* Build an instruction created by a macro expansion.  This is passed
2903    a pointer to the count of instructions created so far, an
2904    expression, the name of the instruction to build, an operand format
2905    string, and corresponding arguments.  */
2906
2907 #ifdef USE_STDARG
2908 static void
2909 macro_build (char *place,
2910              int *counter,
2911              expressionS * ep,
2912              const char *name,
2913              const char *fmt,
2914              ...)
2915 #else
2916 static void
2917 macro_build (place, counter, ep, name, fmt, va_alist)
2918      char *place;
2919      int *counter;
2920      expressionS *ep;
2921      const char *name;
2922      const char *fmt;
2923      va_dcl
2924 #endif
2925 {
2926   struct mips_cl_insn insn;
2927   bfd_reloc_code_real_type r[3];
2928   va_list args;
2929
2930 #ifdef USE_STDARG
2931   va_start (args, fmt);
2932 #else
2933   va_start (args);
2934 #endif
2935
2936   /*
2937    * If the macro is about to expand into a second instruction,
2938    * print a warning if needed. We need to pass ip as a parameter
2939    * to generate a better warning message here...
2940    */
2941   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2942     as_warn (_("Macro instruction expanded into multiple instructions"));
2943
2944   /*
2945    * If the macro is about to expand into a second instruction,
2946    * and it is in a delay slot, print a warning.
2947    */
2948   if (place == NULL
2949       && *counter == 1
2950       && mips_opts.noreorder
2951       && (prev_prev_insn.insn_mo->pinfo
2952           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2953              | INSN_COND_BRANCH_LIKELY)) != 0)
2954     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2955
2956   if (place == NULL)
2957     ++*counter;         /* bump instruction counter */
2958
2959   if (mips_opts.mips16)
2960     {
2961       mips16_macro_build (place, counter, ep, name, fmt, args);
2962       va_end (args);
2963       return;
2964     }
2965
2966   r[0] = BFD_RELOC_UNUSED;
2967   r[1] = BFD_RELOC_UNUSED;
2968   r[2] = BFD_RELOC_UNUSED;
2969   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2970   assert (insn.insn_mo);
2971   assert (strcmp (name, insn.insn_mo->name) == 0);
2972
2973   /* Search until we get a match for NAME.  */
2974   while (1)
2975     {
2976       /* It is assumed here that macros will never generate
2977          MDMX or MIPS-3D instructions.  */
2978       if (strcmp (fmt, insn.insn_mo->args) == 0
2979           && insn.insn_mo->pinfo != INSN_MACRO
2980           && OPCODE_IS_MEMBER (insn.insn_mo,
2981                                (mips_opts.isa
2982                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2983                                mips_arch)
2984           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2985         break;
2986
2987       ++insn.insn_mo;
2988       assert (insn.insn_mo->name);
2989       assert (strcmp (name, insn.insn_mo->name) == 0);
2990     }
2991
2992   insn.insn_opcode = insn.insn_mo->match;
2993   for (;;)
2994     {
2995       switch (*fmt++)
2996         {
2997         case '\0':
2998           break;
2999
3000         case ',':
3001         case '(':
3002         case ')':
3003           continue;
3004
3005         case 't':
3006         case 'w':
3007         case 'E':
3008           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3009           continue;
3010
3011         case 'c':
3012           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3013           continue;
3014
3015         case 'T':
3016         case 'W':
3017           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3018           continue;
3019
3020         case 'd':
3021         case 'G':
3022         case 'K':
3023           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3024           continue;
3025
3026         case 'U':
3027           {
3028             int tmp = va_arg (args, int);
3029
3030             insn.insn_opcode |= tmp << OP_SH_RT;
3031             insn.insn_opcode |= tmp << OP_SH_RD;
3032             continue;
3033           }
3034
3035         case 'V':
3036         case 'S':
3037           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3038           continue;
3039
3040         case 'z':
3041           continue;
3042
3043         case '<':
3044           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3045           continue;
3046
3047         case 'D':
3048           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3049           continue;
3050
3051         case 'B':
3052           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3053           continue;
3054
3055         case 'J':
3056           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3057           continue;
3058
3059         case 'q':
3060           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3061           continue;
3062
3063         case 'b':
3064         case 's':
3065         case 'r':
3066         case 'v':
3067           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3068           continue;
3069
3070         case 'i':
3071         case 'j':
3072         case 'o':
3073           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3074           assert (*r == BFD_RELOC_GPREL16
3075                   || *r == BFD_RELOC_MIPS_LITERAL
3076                   || *r == BFD_RELOC_MIPS_HIGHER
3077                   || *r == BFD_RELOC_HI16_S
3078                   || *r == BFD_RELOC_LO16
3079                   || *r == BFD_RELOC_MIPS_GOT16
3080                   || *r == BFD_RELOC_MIPS_CALL16
3081                   || *r == BFD_RELOC_MIPS_GOT_DISP
3082                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3083                   || *r == BFD_RELOC_MIPS_GOT_OFST
3084                   || *r == BFD_RELOC_MIPS_GOT_LO16
3085                   || *r == BFD_RELOC_MIPS_CALL_LO16
3086                   || (ep->X_op == O_subtract
3087                       && *r == BFD_RELOC_PCREL_LO16));
3088           continue;
3089
3090         case 'u':
3091           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3092           assert (ep != NULL
3093                   && (ep->X_op == O_constant
3094                       || (ep->X_op == O_symbol
3095                           && (*r == BFD_RELOC_MIPS_HIGHEST
3096                               || *r == BFD_RELOC_HI16_S
3097                               || *r == BFD_RELOC_HI16
3098                               || *r == BFD_RELOC_GPREL16
3099                               || *r == BFD_RELOC_MIPS_GOT_HI16
3100                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3101                       || (ep->X_op == O_subtract
3102                           && *r == BFD_RELOC_PCREL_HI16_S)));
3103           continue;
3104
3105         case 'p':
3106           assert (ep != NULL);
3107           /*
3108            * This allows macro() to pass an immediate expression for
3109            * creating short branches without creating a symbol.
3110            * Note that the expression still might come from the assembly
3111            * input, in which case the value is not checked for range nor
3112            * is a relocation entry generated (yuck).
3113            */
3114           if (ep->X_op == O_constant)
3115             {
3116               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3117               ep = NULL;
3118             }
3119           else
3120             *r = BFD_RELOC_16_PCREL_S2;
3121           continue;
3122
3123         case 'a':
3124           assert (ep != NULL);
3125           *r = BFD_RELOC_MIPS_JMP;
3126           continue;
3127
3128         case 'C':
3129           insn.insn_opcode |= va_arg (args, unsigned long);
3130           continue;
3131
3132         default:
3133           internalError ();
3134         }
3135       break;
3136     }
3137   va_end (args);
3138   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3139
3140   append_insn (place, &insn, ep, r);
3141 }
3142
3143 static void
3144 mips16_macro_build (place, counter, ep, name, fmt, args)
3145      char *place;
3146      int *counter ATTRIBUTE_UNUSED;
3147      expressionS *ep;
3148      const char *name;
3149      const char *fmt;
3150      va_list args;
3151 {
3152   struct mips_cl_insn insn;
3153   bfd_reloc_code_real_type r[3]
3154     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3155
3156   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3157   assert (insn.insn_mo);
3158   assert (strcmp (name, insn.insn_mo->name) == 0);
3159
3160   while (strcmp (fmt, insn.insn_mo->args) != 0
3161          || insn.insn_mo->pinfo == INSN_MACRO)
3162     {
3163       ++insn.insn_mo;
3164       assert (insn.insn_mo->name);
3165       assert (strcmp (name, insn.insn_mo->name) == 0);
3166     }
3167
3168   insn.insn_opcode = insn.insn_mo->match;
3169   insn.use_extend = FALSE;
3170
3171   for (;;)
3172     {
3173       int c;
3174
3175       c = *fmt++;
3176       switch (c)
3177         {
3178         case '\0':
3179           break;
3180
3181         case ',':
3182         case '(':
3183         case ')':
3184           continue;
3185
3186         case 'y':
3187         case 'w':
3188           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3189           continue;
3190
3191         case 'x':
3192         case 'v':
3193           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3194           continue;
3195
3196         case 'z':
3197           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3198           continue;
3199
3200         case 'Z':
3201           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3202           continue;
3203
3204         case '0':
3205         case 'S':
3206         case 'P':
3207         case 'R':
3208           continue;
3209
3210         case 'X':
3211           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3212           continue;
3213
3214         case 'Y':
3215           {
3216             int regno;
3217
3218             regno = va_arg (args, int);
3219             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3220             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3221           }
3222           continue;
3223
3224         case '<':
3225         case '>':
3226         case '4':
3227         case '5':
3228         case 'H':
3229         case 'W':
3230         case 'D':
3231         case 'j':
3232         case '8':
3233         case 'V':
3234         case 'C':
3235         case 'U':
3236         case 'k':
3237         case 'K':
3238         case 'p':
3239         case 'q':
3240           {
3241             assert (ep != NULL);
3242
3243             if (ep->X_op != O_constant)
3244               *r = (int) BFD_RELOC_UNUSED + c;
3245             else
3246               {
3247                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3248                               FALSE, &insn.insn_opcode, &insn.use_extend,
3249                               &insn.extend);
3250                 ep = NULL;
3251                 *r = BFD_RELOC_UNUSED;
3252               }
3253           }
3254           continue;
3255
3256         case '6':
3257           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3258           continue;
3259         }
3260
3261       break;
3262     }
3263
3264   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3265
3266   append_insn (place, &insn, ep, r);
3267 }
3268
3269 /*
3270  * Generate a "jalr" instruction with a relocation hint to the called
3271  * function.  This occurs in NewABI PIC code.
3272  */
3273 static void
3274 macro_build_jalr (icnt, ep)
3275      int icnt;
3276      expressionS *ep;
3277 {
3278   char *f;
3279
3280   if (HAVE_NEWABI)
3281     {
3282       frag_grow (4);
3283       f = frag_more (0);
3284     }
3285   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3286                RA, PIC_CALL_REG);
3287   if (HAVE_NEWABI)
3288     fix_new_exp (frag_now, f - frag_now->fr_literal,
3289                  0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3290 }
3291
3292 /*
3293  * Generate a "lui" instruction.
3294  */
3295 static void
3296 macro_build_lui (place, counter, ep, regnum)
3297      char *place;
3298      int *counter;
3299      expressionS *ep;
3300      int regnum;
3301 {
3302   expressionS high_expr;
3303   struct mips_cl_insn insn;
3304   bfd_reloc_code_real_type r[3]
3305     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3306   const char *name = "lui";
3307   const char *fmt = "t,u";
3308
3309   assert (! mips_opts.mips16);
3310
3311   if (place == NULL)
3312     high_expr = *ep;
3313   else
3314     {
3315       high_expr.X_op = O_constant;
3316       high_expr.X_add_number = ep->X_add_number;
3317     }
3318
3319   if (high_expr.X_op == O_constant)
3320     {
3321       /* we can compute the instruction now without a relocation entry */
3322       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3323                                 >> 16) & 0xffff;
3324       *r = BFD_RELOC_UNUSED;
3325     }
3326   else
3327     {
3328       assert (ep->X_op == O_symbol);
3329       /* _gp_disp is a special case, used from s_cpload.  */
3330       assert (mips_pic == NO_PIC
3331               || (! HAVE_NEWABI
3332                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3333       *r = BFD_RELOC_HI16_S;
3334     }
3335
3336   /*
3337    * If the macro is about to expand into a second instruction,
3338    * print a warning if needed. We need to pass ip as a parameter
3339    * to generate a better warning message here...
3340    */
3341   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3342     as_warn (_("Macro instruction expanded into multiple instructions"));
3343
3344   if (place == NULL)
3345     ++*counter;         /* bump instruction counter */
3346
3347   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3348   assert (insn.insn_mo);
3349   assert (strcmp (name, insn.insn_mo->name) == 0);
3350   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3351
3352   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3353   if (*r == BFD_RELOC_UNUSED)
3354     {
3355       insn.insn_opcode |= high_expr.X_add_number;
3356       append_insn (place, &insn, NULL, r);
3357     }
3358   else
3359     append_insn (place, &insn, &high_expr, r);
3360 }
3361
3362 /* Generate a sequence of instructions to do a load or store from a constant
3363    offset off of a base register (breg) into/from a target register (treg),
3364    using AT if necessary.  */
3365 static void
3366 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3367      char *place;
3368      int *counter;
3369      expressionS *ep;
3370      const char *op;
3371      int treg, breg;
3372 {
3373   assert (ep->X_op == O_constant);
3374
3375   /* Right now, this routine can only handle signed 32-bit contants.  */
3376   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3377     as_warn (_("operand overflow"));
3378
3379   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3380     {
3381       /* Signed 16-bit offset will fit in the op.  Easy!  */
3382       macro_build (place, counter, ep, op, "t,o(b)", treg,
3383                    (int) BFD_RELOC_LO16, breg);
3384     }
3385   else
3386     {
3387       /* 32-bit offset, need multiple instructions and AT, like:
3388            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3389            addu     $tempreg,$tempreg,$breg
3390            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3391          to handle the complete offset.  */
3392       macro_build_lui (place, counter, ep, AT);
3393       if (place != NULL)
3394         place += 4;
3395       macro_build (place, counter, (expressionS *) NULL,
3396                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3397                    "d,v,t", AT, AT, breg);
3398       if (place != NULL)
3399         place += 4;
3400       macro_build (place, counter, ep, op, "t,o(b)", treg,
3401                    (int) BFD_RELOC_LO16, AT);
3402
3403       if (mips_opts.noat)
3404         as_warn (_("Macro used $at after \".set noat\""));
3405     }
3406 }
3407
3408 /*                      set_at()
3409  * Generates code to set the $at register to true (one)
3410  * if reg is less than the immediate expression.
3411  */
3412 static void
3413 set_at (counter, reg, unsignedp)
3414      int *counter;
3415      int reg;
3416      int unsignedp;
3417 {
3418   if (imm_expr.X_op == O_constant
3419       && imm_expr.X_add_number >= -0x8000
3420       && imm_expr.X_add_number < 0x8000)
3421     macro_build ((char *) NULL, counter, &imm_expr,
3422                  unsignedp ? "sltiu" : "slti",
3423                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3424   else
3425     {
3426       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3427       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3428                    unsignedp ? "sltu" : "slt",
3429                    "d,v,t", AT, reg, AT);
3430     }
3431 }
3432
3433 /* Warn if an expression is not a constant.  */
3434
3435 static void
3436 check_absolute_expr (ip, ex)
3437      struct mips_cl_insn *ip;
3438      expressionS *ex;
3439 {
3440   if (ex->X_op == O_big)
3441     as_bad (_("unsupported large constant"));
3442   else if (ex->X_op != O_constant)
3443     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3444 }
3445
3446 /* Count the leading zeroes by performing a binary chop. This is a
3447    bulky bit of source, but performance is a LOT better for the
3448    majority of values than a simple loop to count the bits:
3449        for (lcnt = 0; (lcnt < 32); lcnt++)
3450          if ((v) & (1 << (31 - lcnt)))
3451            break;
3452   However it is not code size friendly, and the gain will drop a bit
3453   on certain cached systems.
3454 */
3455 #define COUNT_TOP_ZEROES(v)             \
3456   (((v) & ~0xffff) == 0                 \
3457    ? ((v) & ~0xff) == 0                 \
3458      ? ((v) & ~0xf) == 0                \
3459        ? ((v) & ~0x3) == 0              \
3460          ? ((v) & ~0x1) == 0            \
3461            ? !(v)                       \
3462              ? 32                       \
3463              : 31                       \
3464            : 30                         \
3465          : ((v) & ~0x7) == 0            \
3466            ? 29                         \
3467            : 28                         \
3468        : ((v) & ~0x3f) == 0             \
3469          ? ((v) & ~0x1f) == 0           \
3470            ? 27                         \
3471            : 26                         \
3472          : ((v) & ~0x7f) == 0           \
3473            ? 25                         \
3474            : 24                         \
3475      : ((v) & ~0xfff) == 0              \
3476        ? ((v) & ~0x3ff) == 0            \
3477          ? ((v) & ~0x1ff) == 0          \
3478            ? 23                         \
3479            : 22                         \
3480          : ((v) & ~0x7ff) == 0          \
3481            ? 21                         \
3482            : 20                         \
3483        : ((v) & ~0x3fff) == 0           \
3484          ? ((v) & ~0x1fff) == 0         \
3485            ? 19                         \
3486            : 18                         \
3487          : ((v) & ~0x7fff) == 0         \
3488            ? 17                         \
3489            : 16                         \
3490    : ((v) & ~0xffffff) == 0             \
3491      ? ((v) & ~0xfffff) == 0            \
3492        ? ((v) & ~0x3ffff) == 0          \
3493          ? ((v) & ~0x1ffff) == 0        \
3494            ? 15                         \
3495            : 14                         \
3496          : ((v) & ~0x7ffff) == 0        \
3497            ? 13                         \
3498            : 12                         \
3499        : ((v) & ~0x3fffff) == 0         \
3500          ? ((v) & ~0x1fffff) == 0       \
3501            ? 11                         \
3502            : 10                         \
3503          : ((v) & ~0x7fffff) == 0       \
3504            ? 9                          \
3505            : 8                          \
3506      : ((v) & ~0xfffffff) == 0          \
3507        ? ((v) & ~0x3ffffff) == 0        \
3508          ? ((v) & ~0x1ffffff) == 0      \
3509            ? 7                          \
3510            : 6                          \
3511          : ((v) & ~0x7ffffff) == 0      \
3512            ? 5                          \
3513            : 4                          \
3514        : ((v) & ~0x3fffffff) == 0       \
3515          ? ((v) & ~0x1fffffff) == 0     \
3516            ? 3                          \
3517            : 2                          \
3518          : ((v) & ~0x7fffffff) == 0     \
3519            ? 1                          \
3520            : 0)
3521
3522 /*                      load_register()
3523  *  This routine generates the least number of instructions neccessary to load
3524  *  an absolute expression value into a register.
3525  */
3526 static void
3527 load_register (counter, reg, ep, dbl)
3528      int *counter;
3529      int reg;
3530      expressionS *ep;
3531      int dbl;
3532 {
3533   int freg;
3534   expressionS hi32, lo32;
3535
3536   if (ep->X_op != O_big)
3537     {
3538       assert (ep->X_op == O_constant);
3539       if (ep->X_add_number < 0x8000
3540           && (ep->X_add_number >= 0
3541               || (ep->X_add_number >= -0x8000
3542                   && (! dbl
3543                       || ! ep->X_unsigned
3544                       || sizeof (ep->X_add_number) > 4))))
3545         {
3546           /* We can handle 16 bit signed values with an addiu to
3547              $zero.  No need to ever use daddiu here, since $zero and
3548              the result are always correct in 32 bit mode.  */
3549           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3550                        (int) BFD_RELOC_LO16);
3551           return;
3552         }
3553       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3554         {
3555           /* We can handle 16 bit unsigned values with an ori to
3556              $zero.  */
3557           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3558                        (int) BFD_RELOC_LO16);
3559           return;
3560         }
3561       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3562                 && (! dbl
3563                     || ! ep->X_unsigned
3564                     || sizeof (ep->X_add_number) > 4
3565                     || (ep->X_add_number & 0x80000000) == 0))
3566                || ((HAVE_32BIT_GPRS || ! dbl)
3567                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3568                || (HAVE_32BIT_GPRS
3569                    && ! dbl
3570                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3571                        == ~ (offsetT) 0xffffffff)))
3572         {
3573           /* 32 bit values require an lui.  */
3574           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3575                        (int) BFD_RELOC_HI16);
3576           if ((ep->X_add_number & 0xffff) != 0)
3577             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3578                          (int) BFD_RELOC_LO16);
3579           return;
3580         }
3581     }
3582
3583   /* The value is larger than 32 bits.  */
3584
3585   if (HAVE_32BIT_GPRS)
3586     {
3587       as_bad (_("Number (0x%lx) larger than 32 bits"),
3588               (unsigned long) ep->X_add_number);
3589       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3590                    (int) BFD_RELOC_LO16);
3591       return;
3592     }
3593
3594   if (ep->X_op != O_big)
3595     {
3596       hi32 = *ep;
3597       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3598       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3599       hi32.X_add_number &= 0xffffffff;
3600       lo32 = *ep;
3601       lo32.X_add_number &= 0xffffffff;
3602     }
3603   else
3604     {
3605       assert (ep->X_add_number > 2);
3606       if (ep->X_add_number == 3)
3607         generic_bignum[3] = 0;
3608       else if (ep->X_add_number > 4)
3609         as_bad (_("Number larger than 64 bits"));
3610       lo32.X_op = O_constant;
3611       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3612       hi32.X_op = O_constant;
3613       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3614     }
3615
3616   if (hi32.X_add_number == 0)
3617     freg = 0;
3618   else
3619     {
3620       int shift, bit;
3621       unsigned long hi, lo;
3622
3623       if (hi32.X_add_number == (offsetT) 0xffffffff)
3624         {
3625           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3626             {
3627               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3628                            reg, 0, (int) BFD_RELOC_LO16);
3629               return;
3630             }
3631           if (lo32.X_add_number & 0x80000000)
3632             {
3633               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3634                            (int) BFD_RELOC_HI16);
3635               if (lo32.X_add_number & 0xffff)
3636                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3637                              reg, reg, (int) BFD_RELOC_LO16);
3638               return;
3639             }
3640         }
3641
3642       /* Check for 16bit shifted constant.  We know that hi32 is
3643          non-zero, so start the mask on the first bit of the hi32
3644          value.  */
3645       shift = 17;
3646       do
3647         {
3648           unsigned long himask, lomask;
3649
3650           if (shift < 32)
3651             {
3652               himask = 0xffff >> (32 - shift);
3653               lomask = (0xffff << shift) & 0xffffffff;
3654             }
3655           else
3656             {
3657               himask = 0xffff << (shift - 32);
3658               lomask = 0;
3659             }
3660           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3661               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3662             {
3663               expressionS tmp;
3664
3665               tmp.X_op = O_constant;
3666               if (shift < 32)
3667                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3668                                     | (lo32.X_add_number >> shift));
3669               else
3670                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3671               macro_build ((char *) NULL, counter, &tmp,
3672                            "ori", "t,r,i", reg, 0,
3673                            (int) BFD_RELOC_LO16);
3674               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3675                            (shift >= 32) ? "dsll32" : "dsll",
3676                            "d,w,<", reg, reg,
3677                            (shift >= 32) ? shift - 32 : shift);
3678               return;
3679             }
3680           ++shift;
3681         }
3682       while (shift <= (64 - 16));
3683
3684       /* Find the bit number of the lowest one bit, and store the
3685          shifted value in hi/lo.  */
3686       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3687       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3688       if (lo != 0)
3689         {
3690           bit = 0;
3691           while ((lo & 1) == 0)
3692             {
3693               lo >>= 1;
3694               ++bit;
3695             }
3696           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3697           hi >>= bit;
3698         }
3699       else
3700         {
3701           bit = 32;
3702           while ((hi & 1) == 0)
3703             {
3704               hi >>= 1;
3705               ++bit;
3706             }
3707           lo = hi;
3708           hi = 0;
3709         }
3710
3711       /* Optimize if the shifted value is a (power of 2) - 1.  */
3712       if ((hi == 0 && ((lo + 1) & lo) == 0)
3713           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3714         {
3715           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3716           if (shift != 0)
3717             {
3718               expressionS tmp;
3719
3720               /* This instruction will set the register to be all
3721                  ones.  */
3722               tmp.X_op = O_constant;
3723               tmp.X_add_number = (offsetT) -1;
3724               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3725                            reg, 0, (int) BFD_RELOC_LO16);
3726               if (bit != 0)
3727                 {
3728                   bit += shift;
3729                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3730                                (bit >= 32) ? "dsll32" : "dsll",
3731                                "d,w,<", reg, reg,
3732                                (bit >= 32) ? bit - 32 : bit);
3733                 }
3734               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3735                            (shift >= 32) ? "dsrl32" : "dsrl",
3736                            "d,w,<", reg, reg,
3737                            (shift >= 32) ? shift - 32 : shift);
3738               return;
3739             }
3740         }
3741
3742       /* Sign extend hi32 before calling load_register, because we can
3743          generally get better code when we load a sign extended value.  */
3744       if ((hi32.X_add_number & 0x80000000) != 0)
3745         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3746       load_register (counter, reg, &hi32, 0);
3747       freg = reg;
3748     }
3749   if ((lo32.X_add_number & 0xffff0000) == 0)
3750     {
3751       if (freg != 0)
3752         {
3753           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3754                        "dsll32", "d,w,<", reg, freg, 0);
3755           freg = reg;
3756         }
3757     }
3758   else
3759     {
3760       expressionS mid16;
3761
3762       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3763         {
3764           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3765                        (int) BFD_RELOC_HI16);
3766           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3767                        "dsrl32", "d,w,<", reg, reg, 0);
3768           return;
3769         }
3770
3771       if (freg != 0)
3772         {
3773           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3774                        "d,w,<", reg, freg, 16);
3775           freg = reg;
3776         }
3777       mid16 = lo32;
3778       mid16.X_add_number >>= 16;
3779       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3780                    freg, (int) BFD_RELOC_LO16);
3781       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3782                    "d,w,<", reg, reg, 16);
3783       freg = reg;
3784     }
3785   if ((lo32.X_add_number & 0xffff) != 0)
3786     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3787                  (int) BFD_RELOC_LO16);
3788 }
3789
3790 /* Load an address into a register.  */
3791
3792 static void
3793 load_address (counter, reg, ep, used_at)
3794      int *counter;
3795      int reg;
3796      expressionS *ep;
3797      int *used_at;
3798 {
3799   char *p = NULL;
3800
3801   if (ep->X_op != O_constant
3802       && ep->X_op != O_symbol)
3803     {
3804       as_bad (_("expression too complex"));
3805       ep->X_op = O_constant;
3806     }
3807
3808   if (ep->X_op == O_constant)
3809     {
3810       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3811       return;
3812     }
3813
3814   if (mips_pic == NO_PIC)
3815     {
3816       /* If this is a reference to a GP relative symbol, we want
3817            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3818          Otherwise we want
3819            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3820            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3821          If we have an addend, we always use the latter form.
3822
3823          With 64bit address space and a usable $at we want
3824            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3825            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3826            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3827            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3828            dsll32       $reg,0
3829            daddu        $reg,$reg,$at
3830
3831          If $at is already in use, we use a path which is suboptimal
3832          on superscalar processors.
3833            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3834            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3835            dsll         $reg,16
3836            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3837            dsll         $reg,16
3838            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3839        */
3840       if (HAVE_64BIT_ADDRESSES)
3841         {
3842           /* We don't do GP optimization for now because RELAX_ENCODE can't
3843              hold the data for such large chunks.  */
3844
3845           if (*used_at == 0 && ! mips_opts.noat)
3846             {
3847               macro_build (p, counter, ep, "lui", "t,u",
3848                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3849               macro_build (p, counter, ep, "lui", "t,u",
3850                            AT, (int) BFD_RELOC_HI16_S);
3851               macro_build (p, counter, ep, "daddiu", "t,r,j",
3852                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3853               macro_build (p, counter, ep, "daddiu", "t,r,j",
3854                            AT, AT, (int) BFD_RELOC_LO16);
3855               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3856                            "d,w,<", reg, reg, 0);
3857               macro_build (p, counter, (expressionS *) NULL, "daddu",
3858                            "d,v,t", reg, reg, AT);
3859               *used_at = 1;
3860             }
3861           else
3862             {
3863               macro_build (p, counter, ep, "lui", "t,u",
3864                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3865               macro_build (p, counter, ep, "daddiu", "t,r,j",
3866                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3867               macro_build (p, counter, (expressionS *) NULL, "dsll",
3868                            "d,w,<", reg, reg, 16);
3869               macro_build (p, counter, ep, "daddiu", "t,r,j",
3870                            reg, reg, (int) BFD_RELOC_HI16_S);
3871               macro_build (p, counter, (expressionS *) NULL, "dsll",
3872                            "d,w,<", reg, reg, 16);
3873               macro_build (p, counter, ep, "daddiu", "t,r,j",
3874                            reg, reg, (int) BFD_RELOC_LO16);
3875             }
3876         }
3877       else
3878         {
3879           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3880               && ! nopic_need_relax (ep->X_add_symbol, 1))
3881             {
3882               frag_grow (20);
3883               macro_build ((char *) NULL, counter, ep,
3884                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3885                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3886               p = frag_var (rs_machine_dependent, 8, 0,
3887                             RELAX_ENCODE (4, 8, 0, 4, 0,
3888                                           mips_opts.warn_about_macros),
3889                             ep->X_add_symbol, 0, NULL);
3890             }
3891           macro_build_lui (p, counter, ep, reg);
3892           if (p != NULL)
3893             p += 4;
3894           macro_build (p, counter, ep,
3895                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3896                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3897         }
3898     }
3899   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3900     {
3901       expressionS ex;
3902
3903       /* If this is a reference to an external symbol, we want
3904            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3905          Otherwise we want
3906            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3907            nop
3908            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3909          If we have NewABI, we want
3910            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3911          If there is a constant, it must be added in after.  */
3912       ex.X_add_number = ep->X_add_number;
3913       ep->X_add_number = 0;
3914       frag_grow (20);
3915       if (HAVE_NEWABI)
3916         {
3917           macro_build ((char *) NULL, counter, ep,
3918                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3919                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3920         }
3921       else
3922         {
3923           macro_build ((char *) NULL, counter, ep,
3924                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3925                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3926           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3927           p = frag_var (rs_machine_dependent, 4, 0,
3928                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3929                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3930           macro_build (p, counter, ep,
3931                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3932                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3933         }
3934
3935       if (ex.X_add_number != 0)
3936         {
3937           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3938             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3939           ex.X_op = O_constant;
3940           macro_build ((char *) NULL, counter, &ex,
3941                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3942                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3943         }
3944     }
3945   else if (mips_pic == SVR4_PIC)
3946     {
3947       expressionS ex;
3948       int off;
3949
3950       /* This is the large GOT case.  If this is a reference to an
3951          external symbol, we want
3952            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3953            addu         $reg,$reg,$gp
3954            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3955          Otherwise, for a reference to a local symbol, we want
3956            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3957            nop
3958            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3959          If we have NewABI, we want
3960            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3961            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3962          If there is a constant, it must be added in after.  */
3963       ex.X_add_number = ep->X_add_number;
3964       ep->X_add_number = 0;
3965       if (HAVE_NEWABI)
3966         {
3967           macro_build ((char *) NULL, counter, ep,
3968                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3969                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3970           macro_build (p, counter, ep,
3971                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3972                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3973         }
3974       else
3975         {
3976           if (reg_needs_delay (mips_gp_register))
3977             off = 4;
3978           else
3979             off = 0;
3980           frag_grow (32);
3981           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3982                        (int) BFD_RELOC_MIPS_GOT_HI16);
3983           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3984                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3985                        reg, mips_gp_register);
3986           macro_build ((char *) NULL, counter, ep,
3987                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3988                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3989           p = frag_var (rs_machine_dependent, 12 + off, 0,
3990                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3991                                       mips_opts.warn_about_macros),
3992                         ep->X_add_symbol, 0, NULL);
3993           if (off > 0)
3994             {
3995               /* We need a nop before loading from $gp.  This special
3996                  check is required because the lui which starts the main
3997                  instruction stream does not refer to $gp, and so will not
3998                  insert the nop which may be required.  */
3999               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4000                 p += 4;
4001             }
4002           macro_build (p, counter, ep,
4003                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4004                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4005           p += 4;
4006           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4007           p += 4;
4008           macro_build (p, counter, ep,
4009                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4010                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4011         }
4012
4013       if (ex.X_add_number != 0)
4014         {
4015           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4016             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4017           ex.X_op = O_constant;
4018           macro_build ((char *) NULL, counter, &ex,
4019                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4020                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4021         }
4022     }
4023   else if (mips_pic == EMBEDDED_PIC)
4024     {
4025       /* We always do
4026            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4027        */
4028       macro_build ((char *) NULL, counter, ep,
4029                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4030                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4031     }
4032   else
4033     abort ();
4034 }
4035
4036 /* Move the contents of register SOURCE into register DEST.  */
4037
4038 static void
4039 move_register (counter, dest, source)
4040      int *counter;
4041      int dest;
4042      int source;
4043 {
4044   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4045                HAVE_32BIT_GPRS ? "addu" : "daddu",
4046                "d,v,t", dest, source, 0);
4047 }
4048
4049 /*
4050  *                      Build macros
4051  *   This routine implements the seemingly endless macro or synthesized
4052  * instructions and addressing modes in the mips assembly language. Many
4053  * of these macros are simple and are similar to each other. These could
4054  * probably be handled by some kind of table or grammer aproach instead of
4055  * this verbose method. Others are not simple macros but are more like
4056  * optimizing code generation.
4057  *   One interesting optimization is when several store macros appear
4058  * consecutivly that would load AT with the upper half of the same address.
4059  * The ensuing load upper instructions are ommited. This implies some kind
4060  * of global optimization. We currently only optimize within a single macro.
4061  *   For many of the load and store macros if the address is specified as a
4062  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4063  * first load register 'at' with zero and use it as the base register. The
4064  * mips assembler simply uses register $zero. Just one tiny optimization
4065  * we're missing.
4066  */
4067 static void
4068 macro (ip)
4069      struct mips_cl_insn *ip;
4070 {
4071   register int treg, sreg, dreg, breg;
4072   int tempreg;
4073   int mask;
4074   int icnt = 0;
4075   int used_at = 0;
4076   expressionS expr1;
4077   const char *s;
4078   const char *s2;
4079   const char *fmt;
4080   int likely = 0;
4081   int dbl = 0;
4082   int coproc = 0;
4083   int lr = 0;
4084   int imm = 0;
4085   offsetT maxnum;
4086   int off;
4087   bfd_reloc_code_real_type r;
4088   int hold_mips_optimize;
4089
4090   assert (! mips_opts.mips16);
4091
4092   treg = (ip->insn_opcode >> 16) & 0x1f;
4093   dreg = (ip->insn_opcode >> 11) & 0x1f;
4094   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4095   mask = ip->insn_mo->mask;
4096
4097   expr1.X_op = O_constant;
4098   expr1.X_op_symbol = NULL;
4099   expr1.X_add_symbol = NULL;
4100   expr1.X_add_number = 1;
4101
4102   /* Umatched fixups should not be put in the same frag as a relaxable
4103      macro.  For example, suppose we have:
4104
4105         lui $4,%hi(l1)          # 1
4106         la $5,l2                # 2
4107         addiu $4,$4,%lo(l1)     # 3
4108
4109      If instructions 1 and 2 were put in the same frag, md_frob_file would
4110      move the fixup for #1 after the fixups for the "unrelaxed" version of
4111      #2.  This would confuse tc_gen_reloc, which expects the relocations
4112      for #2 to be the last for that frag.
4113
4114      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4115      it assumes that they belong to a relaxable macro.  We mustn't put
4116      other uses of such relocations into a variant frag.
4117
4118      To avoid both problems, finish the current frag it contains a
4119      %reloc() operator.  The macro then goes into a new frag.  */
4120   if (prev_reloc_op_frag == frag_now)
4121     {
4122       frag_wane (frag_now);
4123       frag_new (0);
4124     }
4125
4126   switch (mask)
4127     {
4128     case M_DABS:
4129       dbl = 1;
4130     case M_ABS:
4131       /* bgez $a0,.+12
4132          move v0,$a0
4133          sub v0,$zero,$a0
4134          */
4135
4136       mips_emit_delays (TRUE);
4137       ++mips_opts.noreorder;
4138       mips_any_noreorder = 1;
4139
4140       expr1.X_add_number = 8;
4141       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4142       if (dreg == sreg)
4143         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4144                      0);
4145       else
4146         move_register (&icnt, dreg, sreg);
4147       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4148                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4149
4150       --mips_opts.noreorder;
4151       return;
4152
4153     case M_ADD_I:
4154       s = "addi";
4155       s2 = "add";
4156       goto do_addi;
4157     case M_ADDU_I:
4158       s = "addiu";
4159       s2 = "addu";
4160       goto do_addi;
4161     case M_DADD_I:
4162       dbl = 1;
4163       s = "daddi";
4164       s2 = "dadd";
4165       goto do_addi;
4166     case M_DADDU_I:
4167       dbl = 1;
4168       s = "daddiu";
4169       s2 = "daddu";
4170     do_addi:
4171       if (imm_expr.X_op == O_constant
4172           && imm_expr.X_add_number >= -0x8000
4173           && imm_expr.X_add_number < 0x8000)
4174         {
4175           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4176                        (int) BFD_RELOC_LO16);
4177           return;
4178         }
4179       load_register (&icnt, AT, &imm_expr, dbl);
4180       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4181                    treg, sreg, AT);
4182       break;
4183
4184     case M_AND_I:
4185       s = "andi";
4186       s2 = "and";
4187       goto do_bit;
4188     case M_OR_I:
4189       s = "ori";
4190       s2 = "or";
4191       goto do_bit;
4192     case M_NOR_I:
4193       s = "";
4194       s2 = "nor";
4195       goto do_bit;
4196     case M_XOR_I:
4197       s = "xori";
4198       s2 = "xor";
4199     do_bit:
4200       if (imm_expr.X_op == O_constant
4201           && imm_expr.X_add_number >= 0
4202           && imm_expr.X_add_number < 0x10000)
4203         {
4204           if (mask != M_NOR_I)
4205             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4206                          sreg, (int) BFD_RELOC_LO16);
4207           else
4208             {
4209               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4210                            treg, sreg, (int) BFD_RELOC_LO16);
4211               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4212                            "d,v,t", treg, treg, 0);
4213             }
4214           return;
4215         }
4216
4217       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4218       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4219                    treg, sreg, AT);
4220       break;
4221
4222     case M_BEQ_I:
4223       s = "beq";
4224       goto beq_i;
4225     case M_BEQL_I:
4226       s = "beql";
4227       likely = 1;
4228       goto beq_i;
4229     case M_BNE_I:
4230       s = "bne";
4231       goto beq_i;
4232     case M_BNEL_I:
4233       s = "bnel";
4234       likely = 1;
4235     beq_i:
4236       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4237         {
4238           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4239                        0);
4240           return;
4241         }
4242       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4243       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4244       break;
4245
4246     case M_BGEL:
4247       likely = 1;
4248     case M_BGE:
4249       if (treg == 0)
4250         {
4251           macro_build ((char *) NULL, &icnt, &offset_expr,
4252                        likely ? "bgezl" : "bgez", "s,p", sreg);
4253           return;
4254         }
4255       if (sreg == 0)
4256         {
4257           macro_build ((char *) NULL, &icnt, &offset_expr,
4258                        likely ? "blezl" : "blez", "s,p", treg);
4259           return;
4260         }
4261       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4262                    AT, sreg, treg);
4263       macro_build ((char *) NULL, &icnt, &offset_expr,
4264                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4265       break;
4266
4267     case M_BGTL_I:
4268       likely = 1;
4269     case M_BGT_I:
4270       /* check for > max integer */
4271       maxnum = 0x7fffffff;
4272       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4273         {
4274           maxnum <<= 16;
4275           maxnum |= 0xffff;
4276           maxnum <<= 16;
4277           maxnum |= 0xffff;
4278         }
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_false:
4284           /* result is always false */
4285           if (! likely)
4286             {
4287               if (warn_nops)
4288                 as_warn (_("Branch %s is always false (nop)"),
4289                          ip->insn_mo->name);
4290               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4291                            "", 0);
4292             }
4293           else
4294             {
4295               if (warn_nops)
4296                 as_warn (_("Branch likely %s is always false"),
4297                          ip->insn_mo->name);
4298               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4299                            "s,t,p", 0, 0);
4300             }
4301           return;
4302         }
4303       if (imm_expr.X_op != O_constant)
4304         as_bad (_("Unsupported large constant"));
4305       ++imm_expr.X_add_number;
4306       /* FALLTHROUGH */
4307     case M_BGE_I:
4308     case M_BGEL_I:
4309       if (mask == M_BGEL_I)
4310         likely = 1;
4311       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4312         {
4313           macro_build ((char *) NULL, &icnt, &offset_expr,
4314                        likely ? "bgezl" : "bgez", "s,p", sreg);
4315           return;
4316         }
4317       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4318         {
4319           macro_build ((char *) NULL, &icnt, &offset_expr,
4320                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4321           return;
4322         }
4323       maxnum = 0x7fffffff;
4324       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4325         {
4326           maxnum <<= 16;
4327           maxnum |= 0xffff;
4328           maxnum <<= 16;
4329           maxnum |= 0xffff;
4330         }
4331       maxnum = - maxnum - 1;
4332       if (imm_expr.X_op == O_constant
4333           && imm_expr.X_add_number <= maxnum
4334           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4335         {
4336         do_true:
4337           /* result is always true */
4338           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4339           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4340           return;
4341         }
4342       set_at (&icnt, sreg, 0);
4343       macro_build ((char *) NULL, &icnt, &offset_expr,
4344                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4345       break;
4346
4347     case M_BGEUL:
4348       likely = 1;
4349     case M_BGEU:
4350       if (treg == 0)
4351         goto do_true;
4352       if (sreg == 0)
4353         {
4354           macro_build ((char *) NULL, &icnt, &offset_expr,
4355                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4356           return;
4357         }
4358       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4359                    "d,v,t", AT, sreg, treg);
4360       macro_build ((char *) NULL, &icnt, &offset_expr,
4361                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4362       break;
4363
4364     case M_BGTUL_I:
4365       likely = 1;
4366     case M_BGTU_I:
4367       if (sreg == 0
4368           || (HAVE_32BIT_GPRS
4369               && imm_expr.X_op == O_constant
4370               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4371         goto do_false;
4372       if (imm_expr.X_op != O_constant)
4373         as_bad (_("Unsupported large constant"));
4374       ++imm_expr.X_add_number;
4375       /* FALLTHROUGH */
4376     case M_BGEU_I:
4377     case M_BGEUL_I:
4378       if (mask == M_BGEUL_I)
4379         likely = 1;
4380       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4381         goto do_true;
4382       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4383         {
4384           macro_build ((char *) NULL, &icnt, &offset_expr,
4385                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4386           return;
4387         }
4388       set_at (&icnt, sreg, 1);
4389       macro_build ((char *) NULL, &icnt, &offset_expr,
4390                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4391       break;
4392
4393     case M_BGTL:
4394       likely = 1;
4395     case M_BGT:
4396       if (treg == 0)
4397         {
4398           macro_build ((char *) NULL, &icnt, &offset_expr,
4399                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4400           return;
4401         }
4402       if (sreg == 0)
4403         {
4404           macro_build ((char *) NULL, &icnt, &offset_expr,
4405                        likely ? "bltzl" : "bltz", "s,p", treg);
4406           return;
4407         }
4408       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4409                    AT, treg, sreg);
4410       macro_build ((char *) NULL, &icnt, &offset_expr,
4411                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4412       break;
4413
4414     case M_BGTUL:
4415       likely = 1;
4416     case M_BGTU:
4417       if (treg == 0)
4418         {
4419           macro_build ((char *) NULL, &icnt, &offset_expr,
4420                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4421           return;
4422         }
4423       if (sreg == 0)
4424         goto do_false;
4425       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4426                    "d,v,t", AT, treg, sreg);
4427       macro_build ((char *) NULL, &icnt, &offset_expr,
4428                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4429       break;
4430
4431     case M_BLEL:
4432       likely = 1;
4433     case M_BLE:
4434       if (treg == 0)
4435         {
4436           macro_build ((char *) NULL, &icnt, &offset_expr,
4437                        likely ? "blezl" : "blez", "s,p", sreg);
4438           return;
4439         }
4440       if (sreg == 0)
4441         {
4442           macro_build ((char *) NULL, &icnt, &offset_expr,
4443                        likely ? "bgezl" : "bgez", "s,p", treg);
4444           return;
4445         }
4446       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4447                    AT, treg, sreg);
4448       macro_build ((char *) NULL, &icnt, &offset_expr,
4449                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4450       break;
4451
4452     case M_BLEL_I:
4453       likely = 1;
4454     case M_BLE_I:
4455       maxnum = 0x7fffffff;
4456       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4457         {
4458           maxnum <<= 16;
4459           maxnum |= 0xffff;
4460           maxnum <<= 16;
4461           maxnum |= 0xffff;
4462         }
4463       if (imm_expr.X_op == O_constant
4464           && imm_expr.X_add_number >= maxnum
4465           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4466         goto do_true;
4467       if (imm_expr.X_op != O_constant)
4468         as_bad (_("Unsupported large constant"));
4469       ++imm_expr.X_add_number;
4470       /* FALLTHROUGH */
4471     case M_BLT_I:
4472     case M_BLTL_I:
4473       if (mask == M_BLTL_I)
4474         likely = 1;
4475       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4476         {
4477           macro_build ((char *) NULL, &icnt, &offset_expr,
4478                        likely ? "bltzl" : "bltz", "s,p", sreg);
4479           return;
4480         }
4481       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4482         {
4483           macro_build ((char *) NULL, &icnt, &offset_expr,
4484                        likely ? "blezl" : "blez", "s,p", sreg);
4485           return;
4486         }
4487       set_at (&icnt, sreg, 0);
4488       macro_build ((char *) NULL, &icnt, &offset_expr,
4489                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4490       break;
4491
4492     case M_BLEUL:
4493       likely = 1;
4494     case M_BLEU:
4495       if (treg == 0)
4496         {
4497           macro_build ((char *) NULL, &icnt, &offset_expr,
4498                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4499           return;
4500         }
4501       if (sreg == 0)
4502         goto do_true;
4503       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4504                    "d,v,t", AT, treg, sreg);
4505       macro_build ((char *) NULL, &icnt, &offset_expr,
4506                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4507       break;
4508
4509     case M_BLEUL_I:
4510       likely = 1;
4511     case M_BLEU_I:
4512       if (sreg == 0
4513           || (HAVE_32BIT_GPRS
4514               && imm_expr.X_op == O_constant
4515               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4516         goto do_true;
4517       if (imm_expr.X_op != O_constant)
4518         as_bad (_("Unsupported large constant"));
4519       ++imm_expr.X_add_number;
4520       /* FALLTHROUGH */
4521     case M_BLTU_I:
4522     case M_BLTUL_I:
4523       if (mask == M_BLTUL_I)
4524         likely = 1;
4525       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4526         goto do_false;
4527       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4528         {
4529           macro_build ((char *) NULL, &icnt, &offset_expr,
4530                        likely ? "beql" : "beq",
4531                        "s,t,p", sreg, 0);
4532           return;
4533         }
4534       set_at (&icnt, sreg, 1);
4535       macro_build ((char *) NULL, &icnt, &offset_expr,
4536                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4537       break;
4538
4539     case M_BLTL:
4540       likely = 1;
4541     case M_BLT:
4542       if (treg == 0)
4543         {
4544           macro_build ((char *) NULL, &icnt, &offset_expr,
4545                        likely ? "bltzl" : "bltz", "s,p", sreg);
4546           return;
4547         }
4548       if (sreg == 0)
4549         {
4550           macro_build ((char *) NULL, &icnt, &offset_expr,
4551                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4552           return;
4553         }
4554       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4555                    AT, sreg, treg);
4556       macro_build ((char *) NULL, &icnt, &offset_expr,
4557                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4558       break;
4559
4560     case M_BLTUL:
4561       likely = 1;
4562     case M_BLTU:
4563       if (treg == 0)
4564         goto do_false;
4565       if (sreg == 0)
4566         {
4567           macro_build ((char *) NULL, &icnt, &offset_expr,
4568                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4569           return;
4570         }
4571       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4572                    "d,v,t", AT, sreg,
4573                    treg);
4574       macro_build ((char *) NULL, &icnt, &offset_expr,
4575                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4576       break;
4577
4578     case M_DDIV_3:
4579       dbl = 1;
4580     case M_DIV_3:
4581       s = "mflo";
4582       goto do_div3;
4583     case M_DREM_3:
4584       dbl = 1;
4585     case M_REM_3:
4586       s = "mfhi";
4587     do_div3:
4588       if (treg == 0)
4589         {
4590           as_warn (_("Divide by zero."));
4591           if (mips_trap)
4592             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4593                          "s,t,q", 0, 0, 7);
4594           else
4595             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4596                          "c", 7);
4597           return;
4598         }
4599
4600       mips_emit_delays (TRUE);
4601       ++mips_opts.noreorder;
4602       mips_any_noreorder = 1;
4603       if (mips_trap)
4604         {
4605           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4606                        "s,t,q", treg, 0, 7);
4607           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4608                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4609         }
4610       else
4611         {
4612           expr1.X_add_number = 8;
4613           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4614           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4615                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4616           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4617                        "c", 7);
4618         }
4619       expr1.X_add_number = -1;
4620       macro_build ((char *) NULL, &icnt, &expr1,
4621                    dbl ? "daddiu" : "addiu",
4622                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4623       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4624       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4625       if (dbl)
4626         {
4627           expr1.X_add_number = 1;
4628           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4629                        (int) BFD_RELOC_LO16);
4630           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4631                        "d,w,<", AT, AT, 31);
4632         }
4633       else
4634         {
4635           expr1.X_add_number = 0x80000000;
4636           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4637                        (int) BFD_RELOC_HI16);
4638         }
4639       if (mips_trap)
4640         {
4641           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4642                        "s,t,q", sreg, AT, 6);
4643           /* We want to close the noreorder block as soon as possible, so
4644              that later insns are available for delay slot filling.  */
4645           --mips_opts.noreorder;
4646         }
4647       else
4648         {
4649           expr1.X_add_number = 8;
4650           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4651           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4652                        0);
4653
4654           /* We want to close the noreorder block as soon as possible, so
4655              that later insns are available for delay slot filling.  */
4656           --mips_opts.noreorder;
4657
4658           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4659                        "c", 6);
4660         }
4661       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4662       break;
4663
4664     case M_DIV_3I:
4665       s = "div";
4666       s2 = "mflo";
4667       goto do_divi;
4668     case M_DIVU_3I:
4669       s = "divu";
4670       s2 = "mflo";
4671       goto do_divi;
4672     case M_REM_3I:
4673       s = "div";
4674       s2 = "mfhi";
4675       goto do_divi;
4676     case M_REMU_3I:
4677       s = "divu";
4678       s2 = "mfhi";
4679       goto do_divi;
4680     case M_DDIV_3I:
4681       dbl = 1;
4682       s = "ddiv";
4683       s2 = "mflo";
4684       goto do_divi;
4685     case M_DDIVU_3I:
4686       dbl = 1;
4687       s = "ddivu";
4688       s2 = "mflo";
4689       goto do_divi;
4690     case M_DREM_3I:
4691       dbl = 1;
4692       s = "ddiv";
4693       s2 = "mfhi";
4694       goto do_divi;
4695     case M_DREMU_3I:
4696       dbl = 1;
4697       s = "ddivu";
4698       s2 = "mfhi";
4699     do_divi:
4700       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4701         {
4702           as_warn (_("Divide by zero."));
4703           if (mips_trap)
4704             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4705                          "s,t,q", 0, 0, 7);
4706           else
4707             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4708                          "c", 7);
4709           return;
4710         }
4711       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4712         {
4713           if (strcmp (s2, "mflo") == 0)
4714             move_register (&icnt, dreg, sreg);
4715           else
4716             move_register (&icnt, dreg, 0);
4717           return;
4718         }
4719       if (imm_expr.X_op == O_constant
4720           && imm_expr.X_add_number == -1
4721           && s[strlen (s) - 1] != 'u')
4722         {
4723           if (strcmp (s2, "mflo") == 0)
4724             {
4725               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4726                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4727             }
4728           else
4729             move_register (&icnt, dreg, 0);
4730           return;
4731         }
4732
4733       load_register (&icnt, AT, &imm_expr, dbl);
4734       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4735                    sreg, AT);
4736       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4737       break;
4738
4739     case M_DIVU_3:
4740       s = "divu";
4741       s2 = "mflo";
4742       goto do_divu3;
4743     case M_REMU_3:
4744       s = "divu";
4745       s2 = "mfhi";
4746       goto do_divu3;
4747     case M_DDIVU_3:
4748       s = "ddivu";
4749       s2 = "mflo";
4750       goto do_divu3;
4751     case M_DREMU_3:
4752       s = "ddivu";
4753       s2 = "mfhi";
4754     do_divu3:
4755       mips_emit_delays (TRUE);
4756       ++mips_opts.noreorder;
4757       mips_any_noreorder = 1;
4758       if (mips_trap)
4759         {
4760           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4761                        "s,t,q", treg, 0, 7);
4762           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4763                        sreg, treg);
4764           /* We want to close the noreorder block as soon as possible, so
4765              that later insns are available for delay slot filling.  */
4766           --mips_opts.noreorder;
4767         }
4768       else
4769         {
4770           expr1.X_add_number = 8;
4771           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4772           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4773                        sreg, treg);
4774
4775           /* We want to close the noreorder block as soon as possible, so
4776              that later insns are available for delay slot filling.  */
4777           --mips_opts.noreorder;
4778           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4779                        "c", 7);
4780         }
4781       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4782       return;
4783
4784     case M_DLA_AB:
4785       dbl = 1;
4786     case M_LA_AB:
4787       /* Load the address of a symbol into a register.  If breg is not
4788          zero, we then add a base register to it.  */
4789
4790       if (dbl && HAVE_32BIT_GPRS)
4791         as_warn (_("dla used to load 32-bit register"));
4792
4793       if (! dbl && HAVE_64BIT_OBJECTS)
4794         as_warn (_("la used to load 64-bit address"));
4795
4796       if (offset_expr.X_op == O_constant
4797           && offset_expr.X_add_number >= -0x8000
4798           && offset_expr.X_add_number < 0x8000)
4799         {
4800           macro_build ((char *) NULL, &icnt, &offset_expr,
4801                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4802                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4803           return;
4804         }
4805
4806       if (treg == breg)
4807         {
4808           tempreg = AT;
4809           used_at = 1;
4810         }
4811       else
4812         {
4813           tempreg = treg;
4814           used_at = 0;
4815         }
4816
4817       /* When generating embedded PIC code, we permit expressions of
4818          the form
4819            la   $treg,foo-bar
4820            la   $treg,foo-bar($breg)
4821          where bar is an address in the current section.  These are used
4822          when getting the addresses of functions.  We don't permit
4823          X_add_number to be non-zero, because if the symbol is
4824          external the relaxing code needs to know that any addend is
4825          purely the offset to X_op_symbol.  */
4826       if (mips_pic == EMBEDDED_PIC
4827           && offset_expr.X_op == O_subtract
4828           && (symbol_constant_p (offset_expr.X_op_symbol)
4829               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4830               : (symbol_equated_p (offset_expr.X_op_symbol)
4831                  && (S_GET_SEGMENT
4832                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4833                       ->X_add_symbol)
4834                      == now_seg)))
4835           && (offset_expr.X_add_number == 0
4836               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4837         {
4838           if (breg == 0)
4839             {
4840               tempreg = treg;
4841               used_at = 0;
4842               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4843                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4844             }
4845           else
4846             {
4847               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4848                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4849               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4850                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4851                            "d,v,t", tempreg, tempreg, breg);
4852             }
4853           macro_build ((char *) NULL, &icnt, &offset_expr,
4854                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4855                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4856           if (! used_at)
4857             return;
4858           break;
4859         }
4860
4861       if (offset_expr.X_op != O_symbol
4862           && offset_expr.X_op != O_constant)
4863         {
4864           as_bad (_("expression too complex"));
4865           offset_expr.X_op = O_constant;
4866         }
4867
4868       if (offset_expr.X_op == O_constant)
4869         load_register (&icnt, tempreg, &offset_expr,
4870                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4871                         ? (dbl || HAVE_64BIT_ADDRESSES)
4872                         : HAVE_64BIT_ADDRESSES));
4873       else if (mips_pic == NO_PIC)
4874         {
4875           /* If this is a reference to a GP relative symbol, we want
4876                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4877              Otherwise we want
4878                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4879                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4880              If we have a constant, we need two instructions anyhow,
4881              so we may as well always use the latter form.
4882
4883             With 64bit address space and a usable $at we want
4884               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4885               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4886               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4887               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4888               dsll32    $tempreg,0
4889               daddu     $tempreg,$tempreg,$at
4890
4891             If $at is already in use, we use a path which is suboptimal
4892             on superscalar processors.
4893               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4894               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4895               dsll      $tempreg,16
4896               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4897               dsll      $tempreg,16
4898               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4899           */
4900           char *p = NULL;
4901           if (HAVE_64BIT_ADDRESSES)
4902             {
4903               /* We don't do GP optimization for now because RELAX_ENCODE can't
4904                  hold the data for such large chunks.  */
4905
4906               if (used_at == 0 && ! mips_opts.noat)
4907                 {
4908                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4909                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4910                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4911                                AT, (int) BFD_RELOC_HI16_S);
4912                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4913                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4914                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4915                                AT, AT, (int) BFD_RELOC_LO16);
4916                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4917                                "d,w,<", tempreg, tempreg, 0);
4918                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4919                                "d,v,t", tempreg, tempreg, AT);
4920                   used_at = 1;
4921                 }
4922               else
4923                 {
4924                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4925                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4926                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4927                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4928                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4929                                tempreg, tempreg, 16);
4930                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4931                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4932                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4933                                tempreg, tempreg, 16);
4934                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4935                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4936                 }
4937             }
4938           else
4939             {
4940               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4941                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4942                 {
4943                   frag_grow (20);
4944                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4945                                "t,r,j", tempreg, mips_gp_register,
4946                                (int) BFD_RELOC_GPREL16);
4947                   p = frag_var (rs_machine_dependent, 8, 0,
4948                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4949                                               mips_opts.warn_about_macros),
4950                                 offset_expr.X_add_symbol, 0, NULL);
4951                 }
4952               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4953               if (p != NULL)
4954                 p += 4;
4955               macro_build (p, &icnt, &offset_expr, "addiu",
4956                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4957             }
4958         }
4959       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4960         {
4961           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4962
4963           /* If this is a reference to an external symbol, and there
4964              is no constant, we want
4965                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4966              or if tempreg is PIC_CALL_REG
4967                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4968              For a local symbol, we want
4969                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4970                nop
4971                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4972
4973              If we have a small constant, and this is a reference to
4974              an external symbol, we want
4975                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4976                nop
4977                addiu    $tempreg,$tempreg,<constant>
4978              For a local symbol, we want the same instruction
4979              sequence, but we output a BFD_RELOC_LO16 reloc on the
4980              addiu instruction.
4981
4982              If we have a large constant, and this is a reference to
4983              an external symbol, we want
4984                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4985                lui      $at,<hiconstant>
4986                addiu    $at,$at,<loconstant>
4987                addu     $tempreg,$tempreg,$at
4988              For a local symbol, we want the same instruction
4989              sequence, but we output a BFD_RELOC_LO16 reloc on the
4990              addiu instruction.
4991
4992              For NewABI, we want for local or external data addresses
4993                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4994              For a local function symbol, we want
4995                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4996                nop
4997                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4998            */
4999
5000           expr1.X_add_number = offset_expr.X_add_number;
5001           offset_expr.X_add_number = 0;
5002           frag_grow (32);
5003           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5004             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5005           else if (HAVE_NEWABI)
5006             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5007           macro_build ((char *) NULL, &icnt, &offset_expr,
5008                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5009                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5010           if (expr1.X_add_number == 0)
5011             {
5012               int off;
5013               char *p;
5014
5015               if (breg == 0)
5016                 off = 0;
5017               else
5018                 {
5019                   /* We're going to put in an addu instruction using
5020                      tempreg, so we may as well insert the nop right
5021                      now.  */
5022                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5023                                "nop", "");
5024                   off = 4;
5025                 }
5026               p = frag_var (rs_machine_dependent, 8 - off, 0,
5027                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5028                                           (breg == 0
5029                                            ? mips_opts.warn_about_macros
5030                                            : 0)),
5031                             offset_expr.X_add_symbol, 0, NULL);
5032               if (breg == 0)
5033                 {
5034                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5035                   p += 4;
5036                 }
5037               macro_build (p, &icnt, &expr1,
5038                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5039                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5040               /* FIXME: If breg == 0, and the next instruction uses
5041                  $tempreg, then if this variant case is used an extra
5042                  nop will be generated.  */
5043             }
5044           else if (expr1.X_add_number >= -0x8000
5045                    && expr1.X_add_number < 0x8000)
5046             {
5047               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5048                            "nop", "");
5049               macro_build ((char *) NULL, &icnt, &expr1,
5050                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5051                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5052               frag_var (rs_machine_dependent, 0, 0,
5053                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5054                         offset_expr.X_add_symbol, 0, NULL);
5055             }
5056           else
5057             {
5058               int off1;
5059
5060               /* If we are going to add in a base register, and the
5061                  target register and the base register are the same,
5062                  then we are using AT as a temporary register.  Since
5063                  we want to load the constant into AT, we add our
5064                  current AT (from the global offset table) and the
5065                  register into the register now, and pretend we were
5066                  not using a base register.  */
5067               if (breg != treg)
5068                 off1 = 0;
5069               else
5070                 {
5071                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5072                                "nop", "");
5073                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5075                                "d,v,t", treg, AT, breg);
5076                   breg = 0;
5077                   tempreg = treg;
5078                   off1 = -8;
5079                 }
5080
5081               /* Set mips_optimize around the lui instruction to avoid
5082                  inserting an unnecessary nop after the lw.  */
5083               hold_mips_optimize = mips_optimize;
5084               mips_optimize = 2;
5085               macro_build_lui (NULL, &icnt, &expr1, AT);
5086               mips_optimize = hold_mips_optimize;
5087
5088               macro_build ((char *) NULL, &icnt, &expr1,
5089                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5090                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5091               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5092                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5093                            "d,v,t", tempreg, tempreg, AT);
5094               frag_var (rs_machine_dependent, 0, 0,
5095                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5096                         offset_expr.X_add_symbol, 0, NULL);
5097               used_at = 1;
5098             }
5099         }
5100       else if (mips_pic == SVR4_PIC)
5101         {
5102           int gpdel;
5103           char *p;
5104           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5105           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5106           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5107
5108           /* This is the large GOT case.  If this is a reference to an
5109              external symbol, and there is no constant, we want
5110                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5111                addu     $tempreg,$tempreg,$gp
5112                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5113              or if tempreg is PIC_CALL_REG
5114                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5115                addu     $tempreg,$tempreg,$gp
5116                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5117              For a local symbol, we want
5118                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5119                nop
5120                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5121
5122              If we have a small constant, and this is a reference to
5123              an external symbol, we want
5124                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5125                addu     $tempreg,$tempreg,$gp
5126                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5127                nop
5128                addiu    $tempreg,$tempreg,<constant>
5129              For a local symbol, we want
5130                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5131                nop
5132                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5133
5134              If we have a large constant, and this is a reference to
5135              an external symbol, we want
5136                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5137                addu     $tempreg,$tempreg,$gp
5138                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5139                lui      $at,<hiconstant>
5140                addiu    $at,$at,<loconstant>
5141                addu     $tempreg,$tempreg,$at
5142              For a local symbol, we want
5143                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5144                lui      $at,<hiconstant>
5145                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5146                addu     $tempreg,$tempreg,$at
5147
5148              For NewABI, we want for local data addresses
5149               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5150            */
5151
5152           expr1.X_add_number = offset_expr.X_add_number;
5153           offset_expr.X_add_number = 0;
5154           frag_grow (52);
5155           if (reg_needs_delay (mips_gp_register))
5156             gpdel = 4;
5157           else
5158             gpdel = 0;
5159           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5160             {
5161               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5162               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5163             }
5164           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5165                        tempreg, lui_reloc_type);
5166           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5167                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5168                        "d,v,t", tempreg, tempreg, mips_gp_register);
5169           macro_build ((char *) NULL, &icnt, &offset_expr,
5170                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5171                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5172           if (expr1.X_add_number == 0)
5173             {
5174               int off;
5175
5176               if (breg == 0)
5177                 off = 0;
5178               else
5179                 {
5180                   /* We're going to put in an addu instruction using
5181                      tempreg, so we may as well insert the nop right
5182                      now.  */
5183                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5184                                "nop", "");
5185                   off = 4;
5186                 }
5187
5188               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5189                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5190                                           8 + gpdel, 0,
5191                                           (breg == 0
5192                                            ? mips_opts.warn_about_macros
5193                                            : 0)),
5194                             offset_expr.X_add_symbol, 0, NULL);
5195             }
5196           else if (expr1.X_add_number >= -0x8000
5197                    && expr1.X_add_number < 0x8000)
5198             {
5199               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5200                            "nop", "");
5201               macro_build ((char *) NULL, &icnt, &expr1,
5202                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5203                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5204
5205               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5206                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5207                                           (breg == 0
5208                                            ? mips_opts.warn_about_macros
5209                                            : 0)),
5210                             offset_expr.X_add_symbol, 0, NULL);
5211             }
5212           else
5213             {
5214               int adj, dreg;
5215
5216               /* If we are going to add in a base register, and the
5217                  target register and the base register are the same,
5218                  then we are using AT as a temporary register.  Since
5219                  we want to load the constant into AT, we add our
5220                  current AT (from the global offset table) and the
5221                  register into the register now, and pretend we were
5222                  not using a base register.  */
5223               if (breg != treg)
5224                 {
5225                   adj = 0;
5226                   dreg = tempreg;
5227                 }
5228               else
5229                 {
5230                   assert (tempreg == AT);
5231                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5232                                "nop", "");
5233                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5234                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5235                                "d,v,t", treg, AT, breg);
5236                   dreg = treg;
5237                   adj = 8;
5238                 }
5239
5240               /* Set mips_optimize around the lui instruction to avoid
5241                  inserting an unnecessary nop after the lw.  */
5242               hold_mips_optimize = mips_optimize;
5243               mips_optimize = 2;
5244               macro_build_lui (NULL, &icnt, &expr1, AT);
5245               mips_optimize = hold_mips_optimize;
5246
5247               macro_build ((char *) NULL, &icnt, &expr1,
5248                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5249                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5250               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5251                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5252                            "d,v,t", dreg, dreg, AT);
5253
5254               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5255                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5256                                           8 + gpdel, 0,
5257                                           (breg == 0
5258                                            ? mips_opts.warn_about_macros
5259                                            : 0)),
5260                             offset_expr.X_add_symbol, 0, NULL);
5261
5262               used_at = 1;
5263             }
5264
5265           if (gpdel > 0)
5266             {
5267               /* This is needed because this instruction uses $gp, but
5268                  the first instruction on the main stream does not.  */
5269               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5270               p += 4;
5271             }
5272
5273           if (HAVE_NEWABI)
5274             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5275           macro_build (p, &icnt, &offset_expr,
5276                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5277                        "t,o(b)", tempreg,
5278                        local_reloc_type,
5279                        mips_gp_register);
5280           p += 4;
5281           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5282             {
5283               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5284             }
5285          else
5286            if (expr1.X_add_number >= -0x8000
5287               && expr1.X_add_number < 0x8000)
5288             {
5289               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5290               p += 4;
5291               macro_build (p, &icnt, &expr1,
5292                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5293                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5294               /* FIXME: If add_number is 0, and there was no base
5295                  register, the external symbol case ended with a load,
5296                  so if the symbol turns out to not be external, and
5297                  the next instruction uses tempreg, an unnecessary nop
5298                  will be inserted.  */
5299             }
5300           else
5301             {
5302               if (breg == treg)
5303                 {
5304                   /* We must add in the base register now, as in the
5305                      external symbol case.  */
5306                   assert (tempreg == AT);
5307                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5308                   p += 4;
5309                   macro_build (p, &icnt, (expressionS *) NULL,
5310                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5311                                "d,v,t", treg, AT, breg);
5312                   p += 4;
5313                   tempreg = treg;
5314                   /* We set breg to 0 because we have arranged to add
5315                      it in in both cases.  */
5316                   breg = 0;
5317                 }
5318
5319               macro_build_lui (p, &icnt, &expr1, AT);
5320               p += 4;
5321               macro_build (p, &icnt, &expr1,
5322                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5323                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5324               p += 4;
5325               macro_build (p, &icnt, (expressionS *) NULL,
5326                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5327                            "d,v,t", tempreg, tempreg, AT);
5328               p += 4;
5329             }
5330         }
5331       else if (mips_pic == EMBEDDED_PIC)
5332         {
5333           /* We use
5334                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5335              */
5336           macro_build ((char *) NULL, &icnt, &offset_expr,
5337                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5338                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5339         }
5340       else
5341         abort ();
5342
5343       if (breg != 0)
5344         {
5345           char *s;
5346
5347           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5348             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5349           else
5350             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5351
5352           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5353                        "d,v,t", treg, tempreg, breg);
5354         }
5355
5356       if (! used_at)
5357         return;
5358
5359       break;
5360
5361     case M_J_A:
5362       /* The j instruction may not be used in PIC code, since it
5363          requires an absolute address.  We convert it to a b
5364          instruction.  */
5365       if (mips_pic == NO_PIC)
5366         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5367       else
5368         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5369       return;
5370
5371       /* The jal instructions must be handled as macros because when
5372          generating PIC code they expand to multi-instruction
5373          sequences.  Normally they are simple instructions.  */
5374     case M_JAL_1:
5375       dreg = RA;
5376       /* Fall through.  */
5377     case M_JAL_2:
5378       if (mips_pic == NO_PIC
5379           || mips_pic == EMBEDDED_PIC)
5380         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5381                      "d,s", dreg, sreg);
5382       else if (mips_pic == SVR4_PIC)
5383         {
5384           if (sreg != PIC_CALL_REG)
5385             as_warn (_("MIPS PIC call to register other than $25"));
5386
5387           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5388                        "d,s", dreg, sreg);
5389           if (! HAVE_NEWABI)
5390             {
5391               if (mips_cprestore_offset < 0)
5392                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5393               else
5394                 {
5395                   if (! mips_frame_reg_valid)
5396                     {
5397                       as_warn (_("No .frame pseudo-op used in PIC code"));
5398                       /* Quiet this warning.  */
5399                       mips_frame_reg_valid = 1;
5400                     }
5401                   if (! mips_cprestore_valid)
5402                     {
5403                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5404                       /* Quiet this warning.  */
5405                       mips_cprestore_valid = 1;
5406                     }
5407                   expr1.X_add_number = mips_cprestore_offset;
5408                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5409                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5410                                                 mips_gp_register, mips_frame_reg);
5411                 }
5412             }
5413         }
5414       else
5415         abort ();
5416
5417       return;
5418
5419     case M_JAL_A:
5420       if (mips_pic == NO_PIC)
5421         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5422       else if (mips_pic == SVR4_PIC)
5423         {
5424           char *p;
5425
5426           /* If this is a reference to an external symbol, and we are
5427              using a small GOT, we want
5428                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5429                nop
5430                jalr     $ra,$25
5431                nop
5432                lw       $gp,cprestore($sp)
5433              The cprestore value is set using the .cprestore
5434              pseudo-op.  If we are using a big GOT, we want
5435                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5436                addu     $25,$25,$gp
5437                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5438                nop
5439                jalr     $ra,$25
5440                nop
5441                lw       $gp,cprestore($sp)
5442              If the symbol is not external, we want
5443                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5444                nop
5445                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5446                jalr     $ra,$25
5447                nop
5448                lw $gp,cprestore($sp)
5449              For NewABI, we want
5450                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5451                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5452            */
5453           if (HAVE_NEWABI)
5454             {
5455               macro_build ((char *) NULL, &icnt, &offset_expr,
5456                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5457                            "t,o(b)", PIC_CALL_REG,
5458                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5459               macro_build_jalr (icnt, &offset_expr);
5460             }
5461           else
5462             {
5463               frag_grow (40);
5464               if (! mips_big_got)
5465                 {
5466                   macro_build ((char *) NULL, &icnt, &offset_expr,
5467                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5468                                "t,o(b)", PIC_CALL_REG,
5469                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5470                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5471                                "nop", "");
5472                   p = frag_var (rs_machine_dependent, 4, 0,
5473                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5474                                 offset_expr.X_add_symbol, 0, NULL);
5475                 }
5476               else
5477                 {
5478                   int gpdel;
5479
5480                   if (reg_needs_delay (mips_gp_register))
5481                     gpdel = 4;
5482                   else
5483                     gpdel = 0;
5484                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5485                                "t,u", PIC_CALL_REG,
5486                                (int) BFD_RELOC_MIPS_CALL_HI16);
5487                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5488                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5489                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5490                                mips_gp_register);
5491                   macro_build ((char *) NULL, &icnt, &offset_expr,
5492                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5493                                "t,o(b)", PIC_CALL_REG,
5494                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5495                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5496                                "nop", "");
5497                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5498                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5499                                               8 + gpdel, 0, 0),
5500                                 offset_expr.X_add_symbol, 0, NULL);
5501                   if (gpdel > 0)
5502                     {
5503                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5504                       p += 4;
5505                     }
5506                   macro_build (p, &icnt, &offset_expr,
5507                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5508                                "t,o(b)", PIC_CALL_REG,
5509                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5510                   p += 4;
5511                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5512                   p += 4;
5513                 }
5514               macro_build (p, &icnt, &offset_expr,
5515                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5516                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5517                            (int) BFD_RELOC_LO16);
5518               macro_build_jalr (icnt, &offset_expr);
5519
5520               if (mips_cprestore_offset < 0)
5521                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5522               else
5523                 {
5524                   if (! mips_frame_reg_valid)
5525                     {
5526                       as_warn (_("No .frame pseudo-op used in PIC code"));
5527                       /* Quiet this warning.  */
5528                       mips_frame_reg_valid = 1;
5529                     }
5530                   if (! mips_cprestore_valid)
5531                     {
5532                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5533                       /* Quiet this warning.  */
5534                       mips_cprestore_valid = 1;
5535                     }
5536                   if (mips_opts.noreorder)
5537                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5538                                  "nop", "");
5539                   expr1.X_add_number = mips_cprestore_offset;
5540                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5541                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5542                                                 mips_gp_register, mips_frame_reg);
5543                 }
5544             }
5545         }
5546       else if (mips_pic == EMBEDDED_PIC)
5547         {
5548           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5549           /* The linker may expand the call to a longer sequence which
5550              uses $at, so we must break rather than return.  */
5551           break;
5552         }
5553       else
5554         abort ();
5555
5556       return;
5557
5558     case M_LB_AB:
5559       s = "lb";
5560       goto ld;
5561     case M_LBU_AB:
5562       s = "lbu";
5563       goto ld;
5564     case M_LH_AB:
5565       s = "lh";
5566       goto ld;
5567     case M_LHU_AB:
5568       s = "lhu";
5569       goto ld;
5570     case M_LW_AB:
5571       s = "lw";
5572       goto ld;
5573     case M_LWC0_AB:
5574       s = "lwc0";
5575       /* Itbl support may require additional care here.  */
5576       coproc = 1;
5577       goto ld;
5578     case M_LWC1_AB:
5579       s = "lwc1";
5580       /* Itbl support may require additional care here.  */
5581       coproc = 1;
5582       goto ld;
5583     case M_LWC2_AB:
5584       s = "lwc2";
5585       /* Itbl support may require additional care here.  */
5586       coproc = 1;
5587       goto ld;
5588     case M_LWC3_AB:
5589       s = "lwc3";
5590       /* Itbl support may require additional care here.  */
5591       coproc = 1;
5592       goto ld;
5593     case M_LWL_AB:
5594       s = "lwl";
5595       lr = 1;
5596       goto ld;
5597     case M_LWR_AB:
5598       s = "lwr";
5599       lr = 1;
5600       goto ld;
5601     case M_LDC1_AB:
5602       if (mips_arch == CPU_R4650)
5603         {
5604           as_bad (_("opcode not supported on this processor"));
5605           return;
5606         }
5607       s = "ldc1";
5608       /* Itbl support may require additional care here.  */
5609       coproc = 1;
5610       goto ld;
5611     case M_LDC2_AB:
5612       s = "ldc2";
5613       /* Itbl support may require additional care here.  */
5614       coproc = 1;
5615       goto ld;
5616     case M_LDC3_AB:
5617       s = "ldc3";
5618       /* Itbl support may require additional care here.  */
5619       coproc = 1;
5620       goto ld;
5621     case M_LDL_AB:
5622       s = "ldl";
5623       lr = 1;
5624       goto ld;
5625     case M_LDR_AB:
5626       s = "ldr";
5627       lr = 1;
5628       goto ld;
5629     case M_LL_AB:
5630       s = "ll";
5631       goto ld;
5632     case M_LLD_AB:
5633       s = "lld";
5634       goto ld;
5635     case M_LWU_AB:
5636       s = "lwu";
5637     ld:
5638       if (breg == treg || coproc || lr)
5639         {
5640           tempreg = AT;
5641           used_at = 1;
5642         }
5643       else
5644         {
5645           tempreg = treg;
5646           used_at = 0;
5647         }
5648       goto ld_st;
5649     case M_SB_AB:
5650       s = "sb";
5651       goto st;
5652     case M_SH_AB:
5653       s = "sh";
5654       goto st;
5655     case M_SW_AB:
5656       s = "sw";
5657       goto st;
5658     case M_SWC0_AB:
5659       s = "swc0";
5660       /* Itbl support may require additional care here.  */
5661       coproc = 1;
5662       goto st;
5663     case M_SWC1_AB:
5664       s = "swc1";
5665       /* Itbl support may require additional care here.  */
5666       coproc = 1;
5667       goto st;
5668     case M_SWC2_AB:
5669       s = "swc2";
5670       /* Itbl support may require additional care here.  */
5671       coproc = 1;
5672       goto st;
5673     case M_SWC3_AB:
5674       s = "swc3";
5675       /* Itbl support may require additional care here.  */
5676       coproc = 1;
5677       goto st;
5678     case M_SWL_AB:
5679       s = "swl";
5680       goto st;
5681     case M_SWR_AB:
5682       s = "swr";
5683       goto st;
5684     case M_SC_AB:
5685       s = "sc";
5686       goto st;
5687     case M_SCD_AB:
5688       s = "scd";
5689       goto st;
5690     case M_SDC1_AB:
5691       if (mips_arch == CPU_R4650)
5692         {
5693           as_bad (_("opcode not supported on this processor"));
5694           return;
5695         }
5696       s = "sdc1";
5697       coproc = 1;
5698       /* Itbl support may require additional care here.  */
5699       goto st;
5700     case M_SDC2_AB:
5701       s = "sdc2";
5702       /* Itbl support may require additional care here.  */
5703       coproc = 1;
5704       goto st;
5705     case M_SDC3_AB:
5706       s = "sdc3";
5707       /* Itbl support may require additional care here.  */
5708       coproc = 1;
5709       goto st;
5710     case M_SDL_AB:
5711       s = "sdl";
5712       goto st;
5713     case M_SDR_AB:
5714       s = "sdr";
5715     st:
5716       tempreg = AT;
5717       used_at = 1;
5718     ld_st:
5719       /* Itbl support may require additional care here.  */
5720       if (mask == M_LWC1_AB
5721           || mask == M_SWC1_AB
5722           || mask == M_LDC1_AB
5723           || mask == M_SDC1_AB
5724           || mask == M_L_DAB
5725           || mask == M_S_DAB)
5726         fmt = "T,o(b)";
5727       else if (coproc)
5728         fmt = "E,o(b)";
5729       else
5730         fmt = "t,o(b)";
5731
5732       /* For embedded PIC, we allow loads where the offset is calculated
5733          by subtracting a symbol in the current segment from an unknown
5734          symbol, relative to a base register, e.g.:
5735                 <op>    $treg, <sym>-<localsym>($breg)
5736          This is used by the compiler for switch statements.  */
5737       if (mips_pic == EMBEDDED_PIC
5738           && offset_expr.X_op == O_subtract
5739           && (symbol_constant_p (offset_expr.X_op_symbol)
5740               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5741               : (symbol_equated_p (offset_expr.X_op_symbol)
5742                  && (S_GET_SEGMENT
5743                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5744                       ->X_add_symbol)
5745                      == now_seg)))
5746           && breg != 0
5747           && (offset_expr.X_add_number == 0
5748               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5749         {
5750           /* For this case, we output the instructions:
5751                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5752                 addiu   $tempreg,$tempreg,$breg
5753                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5754              If the relocation would fit entirely in 16 bits, it would be
5755              nice to emit:
5756                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5757              instead, but that seems quite difficult.  */
5758           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5759                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5760           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5761                        ((bfd_arch_bits_per_address (stdoutput) == 32
5762                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5763                         ? "addu" : "daddu"),
5764                        "d,v,t", tempreg, tempreg, breg);
5765           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5766                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5767           if (! used_at)
5768             return;
5769           break;
5770         }
5771
5772       if (offset_expr.X_op != O_constant
5773           && offset_expr.X_op != O_symbol)
5774         {
5775           as_bad (_("expression too complex"));
5776           offset_expr.X_op = O_constant;
5777         }
5778
5779       /* A constant expression in PIC code can be handled just as it
5780          is in non PIC code.  */
5781       if (mips_pic == NO_PIC
5782           || offset_expr.X_op == O_constant)
5783         {
5784           char *p;
5785
5786           /* If this is a reference to a GP relative symbol, and there
5787              is no base register, we want
5788                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5789              Otherwise, if there is no base register, we want
5790                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5791                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5792              If we have a constant, we need two instructions anyhow,
5793              so we always use the latter form.
5794
5795              If we have a base register, and this is a reference to a
5796              GP relative symbol, we want
5797                addu     $tempreg,$breg,$gp
5798                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5799              Otherwise we want
5800                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5801                addu     $tempreg,$tempreg,$breg
5802                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5803              With a constant we always use the latter case.
5804
5805              With 64bit address space and no base register and $at usable,
5806              we want
5807                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5808                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5809                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5810                dsll32   $tempreg,0
5811                daddu    $tempreg,$at
5812                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5813              If we have a base register, we want
5814                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5815                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5816                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5817                daddu    $at,$breg
5818                dsll32   $tempreg,0
5819                daddu    $tempreg,$at
5820                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5821
5822              Without $at we can't generate the optimal path for superscalar
5823              processors here since this would require two temporary registers.
5824                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5825                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5826                dsll     $tempreg,16
5827                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5828                dsll     $tempreg,16
5829                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5830              If we have a base register, we want
5831                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5832                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5833                dsll     $tempreg,16
5834                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5835                dsll     $tempreg,16
5836                daddu    $tempreg,$tempreg,$breg
5837                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5838
5839              If we have 64-bit addresses, as an optimization, for
5840              addresses which are 32-bit constants (e.g. kseg0/kseg1
5841              addresses) we fall back to the 32-bit address generation
5842              mechanism since it is more efficient.  Note that due to
5843              the signed offset used by memory operations, the 32-bit
5844              range is shifted down by 32768 here.  This code should
5845              probably attempt to generate 64-bit constants more
5846              efficiently in general.
5847            */
5848           if (HAVE_64BIT_ADDRESSES
5849               && !(offset_expr.X_op == O_constant
5850                    && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5851             {
5852               p = NULL;
5853
5854               /* We don't do GP optimization for now because RELAX_ENCODE can't
5855                  hold the data for such large chunks.  */
5856
5857               if (used_at == 0 && ! mips_opts.noat)
5858                 {
5859                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5860                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5861                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5862                                AT, (int) BFD_RELOC_HI16_S);
5863                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5864                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5865                   if (breg != 0)
5866                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5867                                  "d,v,t", AT, AT, breg);
5868                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5869                                "d,w,<", tempreg, tempreg, 0);
5870                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5871                                "d,v,t", tempreg, tempreg, AT);
5872                   macro_build (p, &icnt, &offset_expr, s,
5873                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5874                   used_at = 1;
5875                 }
5876               else
5877                 {
5878                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5879                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5880                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5881                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5882                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5883                                "d,w,<", tempreg, tempreg, 16);
5884                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5885                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5886                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5887                                "d,w,<", tempreg, tempreg, 16);
5888                   if (breg != 0)
5889                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5890                                  "d,v,t", tempreg, tempreg, breg);
5891                   macro_build (p, &icnt, &offset_expr, s,
5892                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5893                 }
5894
5895               return;
5896             }
5897
5898           if (breg == 0)
5899             {
5900               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5901                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5902                 p = NULL;
5903               else
5904                 {
5905                   frag_grow (20);
5906                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5907                                treg, (int) BFD_RELOC_GPREL16,
5908                                mips_gp_register);
5909                   p = frag_var (rs_machine_dependent, 8, 0,
5910                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5911                                               (mips_opts.warn_about_macros
5912                                                || (used_at
5913                                                    && mips_opts.noat))),
5914                                 offset_expr.X_add_symbol, 0, NULL);
5915                   used_at = 0;
5916                 }
5917               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5918               if (p != NULL)
5919                 p += 4;
5920               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5921                            (int) BFD_RELOC_LO16, tempreg);
5922             }
5923           else
5924             {
5925               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5926                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5927                 p = NULL;
5928               else
5929                 {
5930                   frag_grow (28);
5931                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5932                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5933                                "d,v,t", tempreg, breg, mips_gp_register);
5934                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5935                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5936                   p = frag_var (rs_machine_dependent, 12, 0,
5937                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5938                                 offset_expr.X_add_symbol, 0, NULL);
5939                 }
5940               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5941               if (p != NULL)
5942                 p += 4;
5943               macro_build (p, &icnt, (expressionS *) NULL,
5944                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5945                            "d,v,t", tempreg, tempreg, breg);
5946               if (p != NULL)
5947                 p += 4;
5948               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5949                            (int) BFD_RELOC_LO16, tempreg);
5950             }
5951         }
5952       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5953         {
5954           char *p;
5955           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5956
5957           /* If this is a reference to an external symbol, we want
5958                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5959                nop
5960                <op>     $treg,0($tempreg)
5961              Otherwise we want
5962                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5963                nop
5964                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5965                <op>     $treg,0($tempreg)
5966              If we have NewABI, we want
5967                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5968              If there is a base register, we add it to $tempreg before
5969              the <op>.  If there is a constant, we stick it in the
5970              <op> instruction.  We don't handle constants larger than
5971              16 bits, because we have no way to load the upper 16 bits
5972              (actually, we could handle them for the subset of cases
5973              in which we are not using $at).  */
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 (HAVE_NEWABI)
5978             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5979           if (expr1.X_add_number < -0x8000
5980               || expr1.X_add_number >= 0x8000)
5981             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5982           frag_grow (20);
5983           macro_build ((char *) NULL, &icnt, &offset_expr,
5984                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5985                        (int) lw_reloc_type, mips_gp_register);
5986           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5987           p = frag_var (rs_machine_dependent, 4, 0,
5988                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5989                         offset_expr.X_add_symbol, 0, NULL);
5990           macro_build (p, &icnt, &offset_expr,
5991                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5992                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5993           if (breg != 0)
5994             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5995                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5996                          "d,v,t", tempreg, tempreg, breg);
5997           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5998                        (int) BFD_RELOC_LO16, tempreg);
5999         }
6000       else if (mips_pic == SVR4_PIC)
6001         {
6002           int gpdel;
6003           char *p;
6004
6005           /* If this is a reference to an external symbol, we want
6006                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6007                addu     $tempreg,$tempreg,$gp
6008                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6009                <op>     $treg,0($tempreg)
6010              Otherwise we want
6011                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6012                nop
6013                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6014                <op>     $treg,0($tempreg)
6015              If there is a base register, we add it to $tempreg before
6016              the <op>.  If there is a constant, we stick it in the
6017              <op> instruction.  We don't handle constants larger than
6018              16 bits, because we have no way to load the upper 16 bits
6019              (actually, we could handle them for the subset of cases
6020              in which we are not using $at).
6021
6022              For NewABI, we want
6023                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6024                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6025                <op>     $treg,0($tempreg)
6026            */
6027           assert (offset_expr.X_op == O_symbol);
6028           expr1.X_add_number = offset_expr.X_add_number;
6029           offset_expr.X_add_number = 0;
6030           if (expr1.X_add_number < -0x8000
6031               || expr1.X_add_number >= 0x8000)
6032             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6033           if (HAVE_NEWABI)
6034             {
6035               macro_build ((char *) NULL, &icnt, &offset_expr,
6036                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6037                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6038                            mips_gp_register);
6039               macro_build ((char *) NULL, &icnt, &offset_expr,
6040                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6041                            "t,r,j", tempreg, tempreg,
6042                            BFD_RELOC_MIPS_GOT_OFST);
6043               if (breg != 0)
6044                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6045                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6046                              "d,v,t", tempreg, tempreg, breg);
6047               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6048                            (int) BFD_RELOC_LO16, tempreg);
6049
6050               if (! used_at)
6051                 return;
6052
6053               break;
6054             }
6055           if (reg_needs_delay (mips_gp_register))
6056             gpdel = 4;
6057           else
6058             gpdel = 0;
6059           frag_grow (36);
6060           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6061                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6062           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6063                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6064                        "d,v,t", tempreg, tempreg, mips_gp_register);
6065           macro_build ((char *) NULL, &icnt, &offset_expr,
6066                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6067                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6068                        tempreg);
6069           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6070                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6071                         offset_expr.X_add_symbol, 0, NULL);
6072           if (gpdel > 0)
6073             {
6074               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6075               p += 4;
6076             }
6077           macro_build (p, &icnt, &offset_expr,
6078                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6079                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6080                        mips_gp_register);
6081           p += 4;
6082           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6083           p += 4;
6084           macro_build (p, &icnt, &offset_expr,
6085                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6086                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6087           if (breg != 0)
6088             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6089                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6090                          "d,v,t", tempreg, tempreg, breg);
6091           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6092                        (int) BFD_RELOC_LO16, tempreg);
6093         }
6094       else if (mips_pic == EMBEDDED_PIC)
6095         {
6096           /* If there is no base register, we want
6097                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6098              If there is a base register, we want
6099                addu     $tempreg,$breg,$gp
6100                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6101              */
6102           assert (offset_expr.X_op == O_symbol);
6103           if (breg == 0)
6104             {
6105               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6106                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6107               used_at = 0;
6108             }
6109           else
6110             {
6111               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6112                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6113                            "d,v,t", tempreg, breg, mips_gp_register);
6114               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6115                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6116             }
6117         }
6118       else
6119         abort ();
6120
6121       if (! used_at)
6122         return;
6123
6124       break;
6125
6126     case M_LI:
6127     case M_LI_S:
6128       load_register (&icnt, treg, &imm_expr, 0);
6129       return;
6130
6131     case M_DLI:
6132       load_register (&icnt, treg, &imm_expr, 1);
6133       return;
6134
6135     case M_LI_SS:
6136       if (imm_expr.X_op == O_constant)
6137         {
6138           load_register (&icnt, AT, &imm_expr, 0);
6139           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6140                        "mtc1", "t,G", AT, treg);
6141           break;
6142         }
6143       else
6144         {
6145           assert (offset_expr.X_op == O_symbol
6146                   && strcmp (segment_name (S_GET_SEGMENT
6147                                            (offset_expr.X_add_symbol)),
6148                              ".lit4") == 0
6149                   && offset_expr.X_add_number == 0);
6150           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6151                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6152           return;
6153         }
6154
6155     case M_LI_D:
6156       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6157          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6158          order 32 bits of the value and the low order 32 bits are either
6159          zero or in OFFSET_EXPR.  */
6160       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6161         {
6162           if (HAVE_64BIT_GPRS)
6163             load_register (&icnt, treg, &imm_expr, 1);
6164           else
6165             {
6166               int hreg, lreg;
6167
6168               if (target_big_endian)
6169                 {
6170                   hreg = treg;
6171                   lreg = treg + 1;
6172                 }
6173               else
6174                 {
6175                   hreg = treg + 1;
6176                   lreg = treg;
6177                 }
6178
6179               if (hreg <= 31)
6180                 load_register (&icnt, hreg, &imm_expr, 0);
6181               if (lreg <= 31)
6182                 {
6183                   if (offset_expr.X_op == O_absent)
6184                     move_register (&icnt, lreg, 0);
6185                   else
6186                     {
6187                       assert (offset_expr.X_op == O_constant);
6188                       load_register (&icnt, lreg, &offset_expr, 0);
6189                     }
6190                 }
6191             }
6192           return;
6193         }
6194
6195       /* We know that sym is in the .rdata section.  First we get the
6196          upper 16 bits of the address.  */
6197       if (mips_pic == NO_PIC)
6198         {
6199           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6200         }
6201       else if (mips_pic == SVR4_PIC)
6202         {
6203           macro_build ((char *) NULL, &icnt, &offset_expr,
6204                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6205                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6206                        mips_gp_register);
6207         }
6208       else if (mips_pic == EMBEDDED_PIC)
6209         {
6210           /* For embedded PIC we pick up the entire address off $gp in
6211              a single instruction.  */
6212           macro_build ((char *) NULL, &icnt, &offset_expr,
6213                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6214                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6215           offset_expr.X_op = O_constant;
6216           offset_expr.X_add_number = 0;
6217         }
6218       else
6219         abort ();
6220
6221       /* Now we load the register(s).  */
6222       if (HAVE_64BIT_GPRS)
6223         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6224                      treg, (int) BFD_RELOC_LO16, AT);
6225       else
6226         {
6227           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6228                        treg, (int) BFD_RELOC_LO16, AT);
6229           if (treg != RA)
6230             {
6231               /* FIXME: How in the world do we deal with the possible
6232                  overflow here?  */
6233               offset_expr.X_add_number += 4;
6234               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6235                            treg + 1, (int) BFD_RELOC_LO16, AT);
6236             }
6237         }
6238
6239       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6240          does not become a variant frag.  */
6241       frag_wane (frag_now);
6242       frag_new (0);
6243
6244       break;
6245
6246     case M_LI_DD:
6247       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6248          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6249          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6250          the value and the low order 32 bits are either zero or in
6251          OFFSET_EXPR.  */
6252       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6253         {
6254           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6255           if (HAVE_64BIT_FPRS)
6256             {
6257               assert (HAVE_64BIT_GPRS);
6258               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6259                            "dmtc1", "t,S", AT, treg);
6260             }
6261           else
6262             {
6263               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6264                            "mtc1", "t,G", AT, treg + 1);
6265               if (offset_expr.X_op == O_absent)
6266                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6267                              "mtc1", "t,G", 0, treg);
6268               else
6269                 {
6270                   assert (offset_expr.X_op == O_constant);
6271                   load_register (&icnt, AT, &offset_expr, 0);
6272                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6273                                "mtc1", "t,G", AT, treg);
6274                 }
6275             }
6276           break;
6277         }
6278
6279       assert (offset_expr.X_op == O_symbol
6280               && offset_expr.X_add_number == 0);
6281       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6282       if (strcmp (s, ".lit8") == 0)
6283         {
6284           if (mips_opts.isa != ISA_MIPS1)
6285             {
6286               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6287                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6288                            mips_gp_register);
6289               return;
6290             }
6291           breg = mips_gp_register;
6292           r = BFD_RELOC_MIPS_LITERAL;
6293           goto dob;
6294         }
6295       else
6296         {
6297           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6298           if (mips_pic == SVR4_PIC)
6299             macro_build ((char *) NULL, &icnt, &offset_expr,
6300                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6301                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6302                          mips_gp_register);
6303           else
6304             {
6305               /* FIXME: This won't work for a 64 bit address.  */
6306               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6307             }
6308
6309           if (mips_opts.isa != ISA_MIPS1)
6310             {
6311               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6312                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6313
6314               /* To avoid confusion in tc_gen_reloc, we must ensure
6315                  that this does not become a variant frag.  */
6316               frag_wane (frag_now);
6317               frag_new (0);
6318
6319               break;
6320             }
6321           breg = AT;
6322           r = BFD_RELOC_LO16;
6323           goto dob;
6324         }
6325
6326     case M_L_DOB:
6327       if (mips_arch == CPU_R4650)
6328         {
6329           as_bad (_("opcode not supported on this processor"));
6330           return;
6331         }
6332       /* Even on a big endian machine $fn comes before $fn+1.  We have
6333          to adjust when loading from memory.  */
6334       r = BFD_RELOC_LO16;
6335     dob:
6336       assert (mips_opts.isa == ISA_MIPS1);
6337       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6338                    target_big_endian ? treg + 1 : treg,
6339                    (int) r, breg);
6340       /* FIXME: A possible overflow which I don't know how to deal
6341          with.  */
6342       offset_expr.X_add_number += 4;
6343       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6344                    target_big_endian ? treg : treg + 1,
6345                    (int) r, breg);
6346
6347       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6348          does not become a variant frag.  */
6349       frag_wane (frag_now);
6350       frag_new (0);
6351
6352       if (breg != AT)
6353         return;
6354       break;
6355
6356     case M_L_DAB:
6357       /*
6358        * The MIPS assembler seems to check for X_add_number not
6359        * being double aligned and generating:
6360        *        lui     at,%hi(foo+1)
6361        *        addu    at,at,v1
6362        *        addiu   at,at,%lo(foo+1)
6363        *        lwc1    f2,0(at)
6364        *        lwc1    f3,4(at)
6365        * But, the resulting address is the same after relocation so why
6366        * generate the extra instruction?
6367        */
6368       if (mips_arch == CPU_R4650)
6369         {
6370           as_bad (_("opcode not supported on this processor"));
6371           return;
6372         }
6373       /* Itbl support may require additional care here.  */
6374       coproc = 1;
6375       if (mips_opts.isa != ISA_MIPS1)
6376         {
6377           s = "ldc1";
6378           goto ld;
6379         }
6380
6381       s = "lwc1";
6382       fmt = "T,o(b)";
6383       goto ldd_std;
6384
6385     case M_S_DAB:
6386       if (mips_arch == CPU_R4650)
6387         {
6388           as_bad (_("opcode not supported on this processor"));
6389           return;
6390         }
6391
6392       if (mips_opts.isa != ISA_MIPS1)
6393         {
6394           s = "sdc1";
6395           goto st;
6396         }
6397
6398       s = "swc1";
6399       fmt = "T,o(b)";
6400       /* Itbl support may require additional care here.  */
6401       coproc = 1;
6402       goto ldd_std;
6403
6404     case M_LD_AB:
6405       if (HAVE_64BIT_GPRS)
6406         {
6407           s = "ld";
6408           goto ld;
6409         }
6410
6411       s = "lw";
6412       fmt = "t,o(b)";
6413       goto ldd_std;
6414
6415     case M_SD_AB:
6416       if (HAVE_64BIT_GPRS)
6417         {
6418           s = "sd";
6419           goto st;
6420         }
6421
6422       s = "sw";
6423       fmt = "t,o(b)";
6424
6425     ldd_std:
6426       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6427          loads for the case of doing a pair of loads to simulate an 'ld'.
6428          This is not currently done by the compiler, and assembly coders
6429          writing embedded-pic code can cope.  */
6430
6431       if (offset_expr.X_op != O_symbol
6432           && offset_expr.X_op != O_constant)
6433         {
6434           as_bad (_("expression too complex"));
6435           offset_expr.X_op = O_constant;
6436         }
6437
6438       /* Even on a big endian machine $fn comes before $fn+1.  We have
6439          to adjust when loading from memory.  We set coproc if we must
6440          load $fn+1 first.  */
6441       /* Itbl support may require additional care here.  */
6442       if (! target_big_endian)
6443         coproc = 0;
6444
6445       if (mips_pic == NO_PIC
6446           || offset_expr.X_op == O_constant)
6447         {
6448           char *p;
6449
6450           /* If this is a reference to a GP relative symbol, we want
6451                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6452                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6453              If we have a base register, we use this
6454                addu     $at,$breg,$gp
6455                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6456                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6457              If this is not a GP relative symbol, we want
6458                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6459                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6460                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6461              If there is a base register, we add it to $at after the
6462              lui instruction.  If there is a constant, we always use
6463              the last case.  */
6464           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6465               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6466             {
6467               p = NULL;
6468               used_at = 1;
6469             }
6470           else
6471             {
6472               int off;
6473
6474               if (breg == 0)
6475                 {
6476                   frag_grow (28);
6477                   tempreg = mips_gp_register;
6478                   off = 0;
6479                   used_at = 0;
6480                 }
6481               else
6482                 {
6483                   frag_grow (36);
6484                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6485                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6486                                "d,v,t", AT, breg, mips_gp_register);
6487                   tempreg = AT;
6488                   off = 4;
6489                   used_at = 1;
6490                 }
6491
6492               /* Itbl support may require additional care here.  */
6493               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6494                            coproc ? treg + 1 : treg,
6495                            (int) BFD_RELOC_GPREL16, tempreg);
6496               offset_expr.X_add_number += 4;
6497
6498               /* Set mips_optimize to 2 to avoid inserting an
6499                  undesired nop.  */
6500               hold_mips_optimize = mips_optimize;
6501               mips_optimize = 2;
6502               /* Itbl support may require additional care here.  */
6503               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6504                            coproc ? treg : treg + 1,
6505                            (int) BFD_RELOC_GPREL16, tempreg);
6506               mips_optimize = hold_mips_optimize;
6507
6508               p = frag_var (rs_machine_dependent, 12 + off, 0,
6509                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6510                                           used_at && mips_opts.noat),
6511                             offset_expr.X_add_symbol, 0, NULL);
6512
6513               /* We just generated two relocs.  When tc_gen_reloc
6514                  handles this case, it will skip the first reloc and
6515                  handle the second.  The second reloc already has an
6516                  extra addend of 4, which we added above.  We must
6517                  subtract it out, and then subtract another 4 to make
6518                  the first reloc come out right.  The second reloc
6519                  will come out right because we are going to add 4 to
6520                  offset_expr when we build its instruction below.
6521
6522                  If we have a symbol, then we don't want to include
6523                  the offset, because it will wind up being included
6524                  when we generate the reloc.  */
6525
6526               if (offset_expr.X_op == O_constant)
6527                 offset_expr.X_add_number -= 8;
6528               else
6529                 {
6530                   offset_expr.X_add_number = -4;
6531                   offset_expr.X_op = O_constant;
6532                 }
6533             }
6534           macro_build_lui (p, &icnt, &offset_expr, AT);
6535           if (p != NULL)
6536             p += 4;
6537           if (breg != 0)
6538             {
6539               macro_build (p, &icnt, (expressionS *) NULL,
6540                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6541                            "d,v,t", AT, breg, AT);
6542               if (p != NULL)
6543                 p += 4;
6544             }
6545           /* Itbl support may require additional care here.  */
6546           macro_build (p, &icnt, &offset_expr, s, fmt,
6547                        coproc ? treg + 1 : treg,
6548                        (int) BFD_RELOC_LO16, AT);
6549           if (p != NULL)
6550             p += 4;
6551           /* FIXME: How do we handle overflow here?  */
6552           offset_expr.X_add_number += 4;
6553           /* Itbl support may require additional care here.  */
6554           macro_build (p, &icnt, &offset_expr, s, fmt,
6555                        coproc ? treg : treg + 1,
6556                        (int) BFD_RELOC_LO16, AT);
6557         }
6558       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6559         {
6560           int off;
6561
6562           /* If this is a reference to an external symbol, we want
6563                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6564                nop
6565                <op>     $treg,0($at)
6566                <op>     $treg+1,4($at)
6567              Otherwise we want
6568                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6569                nop
6570                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6571                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6572              If there is a base register we add it to $at before the
6573              lwc1 instructions.  If there is a constant we include it
6574              in the lwc1 instructions.  */
6575           used_at = 1;
6576           expr1.X_add_number = offset_expr.X_add_number;
6577           offset_expr.X_add_number = 0;
6578           if (expr1.X_add_number < -0x8000
6579               || expr1.X_add_number >= 0x8000 - 4)
6580             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6581           if (breg == 0)
6582             off = 0;
6583           else
6584             off = 4;
6585           frag_grow (24 + off);
6586           macro_build ((char *) NULL, &icnt, &offset_expr,
6587                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6588                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6589           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6590           if (breg != 0)
6591             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6592                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6593                          "d,v,t", AT, breg, AT);
6594           /* Itbl support may require additional care here.  */
6595           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6596                        coproc ? treg + 1 : treg,
6597                        (int) BFD_RELOC_LO16, AT);
6598           expr1.X_add_number += 4;
6599
6600           /* Set mips_optimize to 2 to avoid inserting an undesired
6601              nop.  */
6602           hold_mips_optimize = mips_optimize;
6603           mips_optimize = 2;
6604           /* Itbl support may require additional care here.  */
6605           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6606                        coproc ? treg : treg + 1,
6607                        (int) BFD_RELOC_LO16, AT);
6608           mips_optimize = hold_mips_optimize;
6609
6610           (void) frag_var (rs_machine_dependent, 0, 0,
6611                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6612                            offset_expr.X_add_symbol, 0, NULL);
6613         }
6614       else if (mips_pic == SVR4_PIC)
6615         {
6616           int gpdel, off;
6617           char *p;
6618
6619           /* If this is a reference to an external symbol, we want
6620                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6621                addu     $at,$at,$gp
6622                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6623                nop
6624                <op>     $treg,0($at)
6625                <op>     $treg+1,4($at)
6626              Otherwise we want
6627                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6628                nop
6629                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6630                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6631              If there is a base register we add it to $at before the
6632              lwc1 instructions.  If there is a constant we include it
6633              in the lwc1 instructions.  */
6634           used_at = 1;
6635           expr1.X_add_number = offset_expr.X_add_number;
6636           offset_expr.X_add_number = 0;
6637           if (expr1.X_add_number < -0x8000
6638               || expr1.X_add_number >= 0x8000 - 4)
6639             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6640           if (reg_needs_delay (mips_gp_register))
6641             gpdel = 4;
6642           else
6643             gpdel = 0;
6644           if (breg == 0)
6645             off = 0;
6646           else
6647             off = 4;
6648           frag_grow (56);
6649           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6650                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6651           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6652                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6653                        "d,v,t", AT, AT, mips_gp_register);
6654           macro_build ((char *) NULL, &icnt, &offset_expr,
6655                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6656                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6657           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6658           if (breg != 0)
6659             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6660                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6661                          "d,v,t", AT, breg, AT);
6662           /* Itbl support may require additional care here.  */
6663           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6664                        coproc ? treg + 1 : treg,
6665                        (int) BFD_RELOC_LO16, AT);
6666           expr1.X_add_number += 4;
6667
6668           /* Set mips_optimize to 2 to avoid inserting an undesired
6669              nop.  */
6670           hold_mips_optimize = mips_optimize;
6671           mips_optimize = 2;
6672           /* Itbl support may require additional care here.  */
6673           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6674                        coproc ? treg : treg + 1,
6675                        (int) BFD_RELOC_LO16, AT);
6676           mips_optimize = hold_mips_optimize;
6677           expr1.X_add_number -= 4;
6678
6679           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6680                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6681                                       8 + gpdel + off, 1, 0),
6682                         offset_expr.X_add_symbol, 0, NULL);
6683           if (gpdel > 0)
6684             {
6685               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6686               p += 4;
6687             }
6688           macro_build (p, &icnt, &offset_expr,
6689                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6690                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6691                        mips_gp_register);
6692           p += 4;
6693           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6694           p += 4;
6695           if (breg != 0)
6696             {
6697               macro_build (p, &icnt, (expressionS *) NULL,
6698                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6699                            "d,v,t", AT, breg, AT);
6700               p += 4;
6701             }
6702           /* Itbl support may require additional care here.  */
6703           macro_build (p, &icnt, &expr1, s, fmt,
6704                        coproc ? treg + 1 : treg,
6705                        (int) BFD_RELOC_LO16, AT);
6706           p += 4;
6707           expr1.X_add_number += 4;
6708
6709           /* Set mips_optimize to 2 to avoid inserting an undesired
6710              nop.  */
6711           hold_mips_optimize = mips_optimize;
6712           mips_optimize = 2;
6713           /* Itbl support may require additional care here.  */
6714           macro_build (p, &icnt, &expr1, s, fmt,
6715                        coproc ? treg : treg + 1,
6716                        (int) BFD_RELOC_LO16, AT);
6717           mips_optimize = hold_mips_optimize;
6718         }
6719       else if (mips_pic == EMBEDDED_PIC)
6720         {
6721           /* If there is no base register, we use
6722                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6723                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6724              If we have a base register, we use
6725                addu     $at,$breg,$gp
6726                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6727                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6728              */
6729           if (breg == 0)
6730             {
6731               tempreg = mips_gp_register;
6732               used_at = 0;
6733             }
6734           else
6735             {
6736               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6737                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6738                            "d,v,t", AT, breg, mips_gp_register);
6739               tempreg = AT;
6740               used_at = 1;
6741             }
6742
6743           /* Itbl support may require additional care here.  */
6744           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6745                        coproc ? treg + 1 : treg,
6746                        (int) BFD_RELOC_GPREL16, tempreg);
6747           offset_expr.X_add_number += 4;
6748           /* Itbl support may require additional care here.  */
6749           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6750                        coproc ? treg : treg + 1,
6751                        (int) BFD_RELOC_GPREL16, tempreg);
6752         }
6753       else
6754         abort ();
6755
6756       if (! used_at)
6757         return;
6758
6759       break;
6760
6761     case M_LD_OB:
6762       s = "lw";
6763       goto sd_ob;
6764     case M_SD_OB:
6765       s = "sw";
6766     sd_ob:
6767       assert (HAVE_32BIT_ADDRESSES);
6768       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6769                    (int) BFD_RELOC_LO16, breg);
6770       offset_expr.X_add_number += 4;
6771       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6772                    (int) BFD_RELOC_LO16, breg);
6773       return;
6774
6775    /* New code added to support COPZ instructions.
6776       This code builds table entries out of the macros in mip_opcodes.
6777       R4000 uses interlocks to handle coproc delays.
6778       Other chips (like the R3000) require nops to be inserted for delays.
6779
6780       FIXME: Currently, we require that the user handle delays.
6781       In order to fill delay slots for non-interlocked chips,
6782       we must have a way to specify delays based on the coprocessor.
6783       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6784       What are the side-effects of the cop instruction?
6785       What cache support might we have and what are its effects?
6786       Both coprocessor & memory require delays. how long???
6787       What registers are read/set/modified?
6788
6789       If an itbl is provided to interpret cop instructions,
6790       this knowledge can be encoded in the itbl spec.  */
6791
6792     case M_COP0:
6793       s = "c0";
6794       goto copz;
6795     case M_COP1:
6796       s = "c1";
6797       goto copz;
6798     case M_COP2:
6799       s = "c2";
6800       goto copz;
6801     case M_COP3:
6802       s = "c3";
6803     copz:
6804       /* For now we just do C (same as Cz).  The parameter will be
6805          stored in insn_opcode by mips_ip.  */
6806       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6807                    ip->insn_opcode);
6808       return;
6809
6810     case M_MOVE:
6811       move_register (&icnt, dreg, sreg);
6812       return;
6813
6814 #ifdef LOSING_COMPILER
6815     default:
6816       /* Try and see if this is a new itbl instruction.
6817          This code builds table entries out of the macros in mip_opcodes.
6818          FIXME: For now we just assemble the expression and pass it's
6819          value along as a 32-bit immediate.
6820          We may want to have the assembler assemble this value,
6821          so that we gain the assembler's knowledge of delay slots,
6822          symbols, etc.
6823          Would it be more efficient to use mask (id) here? */
6824       if (itbl_have_entries
6825           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6826         {
6827           s = ip->insn_mo->name;
6828           s2 = "cop3";
6829           coproc = ITBL_DECODE_PNUM (immed_expr);;
6830           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6831           return;
6832         }
6833       macro2 (ip);
6834       return;
6835     }
6836   if (mips_opts.noat)
6837     as_warn (_("Macro used $at after \".set noat\""));
6838 }
6839
6840 static void
6841 macro2 (ip)
6842      struct mips_cl_insn *ip;
6843 {
6844   register int treg, sreg, dreg, breg;
6845   int tempreg;
6846   int mask;
6847   int icnt = 0;
6848   int used_at;
6849   expressionS expr1;
6850   const char *s;
6851   const char *s2;
6852   const char *fmt;
6853   int likely = 0;
6854   int dbl = 0;
6855   int coproc = 0;
6856   int lr = 0;
6857   int imm = 0;
6858   int off;
6859   offsetT maxnum;
6860   bfd_reloc_code_real_type r;
6861   char *p;
6862
6863   treg = (ip->insn_opcode >> 16) & 0x1f;
6864   dreg = (ip->insn_opcode >> 11) & 0x1f;
6865   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6866   mask = ip->insn_mo->mask;
6867
6868   expr1.X_op = O_constant;
6869   expr1.X_op_symbol = NULL;
6870   expr1.X_add_symbol = NULL;
6871   expr1.X_add_number = 1;
6872
6873   switch (mask)
6874     {
6875 #endif /* LOSING_COMPILER */
6876
6877     case M_DMUL:
6878       dbl = 1;
6879     case M_MUL:
6880       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6881                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6882       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6883                    dreg);
6884       return;
6885
6886     case M_DMUL_I:
6887       dbl = 1;
6888     case M_MUL_I:
6889       /* The MIPS assembler some times generates shifts and adds.  I'm
6890          not trying to be that fancy. GCC should do this for us
6891          anyway.  */
6892       load_register (&icnt, AT, &imm_expr, dbl);
6893       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6894                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6895       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6896                    dreg);
6897       break;
6898
6899     case M_DMULO_I:
6900       dbl = 1;
6901     case M_MULO_I:
6902       imm = 1;
6903       goto do_mulo;
6904
6905     case M_DMULO:
6906       dbl = 1;
6907     case M_MULO:
6908     do_mulo:
6909       mips_emit_delays (TRUE);
6910       ++mips_opts.noreorder;
6911       mips_any_noreorder = 1;
6912       if (imm)
6913         load_register (&icnt, AT, &imm_expr, dbl);
6914       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6915                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6916       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6917                    dreg);
6918       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6919                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6920       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6921                    AT);
6922       if (mips_trap)
6923         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6924                      "s,t,q", dreg, AT, 6);
6925       else
6926         {
6927           expr1.X_add_number = 8;
6928           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6929                        AT);
6930           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6931                        0);
6932           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6933                        "c", 6);
6934         }
6935       --mips_opts.noreorder;
6936       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6937       break;
6938
6939     case M_DMULOU_I:
6940       dbl = 1;
6941     case M_MULOU_I:
6942       imm = 1;
6943       goto do_mulou;
6944
6945     case M_DMULOU:
6946       dbl = 1;
6947     case M_MULOU:
6948     do_mulou:
6949       mips_emit_delays (TRUE);
6950       ++mips_opts.noreorder;
6951       mips_any_noreorder = 1;
6952       if (imm)
6953         load_register (&icnt, AT, &imm_expr, dbl);
6954       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6955                    dbl ? "dmultu" : "multu",
6956                    "s,t", sreg, imm ? AT : treg);
6957       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6958                    AT);
6959       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6960                    dreg);
6961       if (mips_trap)
6962         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6963                      "s,t,q", AT, 0, 6);
6964       else
6965         {
6966           expr1.X_add_number = 8;
6967           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6968           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6969                        0);
6970           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6971                        "c", 6);
6972         }
6973       --mips_opts.noreorder;
6974       break;
6975
6976     case M_DROL:
6977       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
6978         {
6979           if (dreg == sreg)
6980             {
6981               tempreg = AT;
6982               used_at = 1;
6983             }
6984           else
6985             {
6986               tempreg = dreg;
6987               used_at = 0;
6988             }
6989           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
6990                        "d,w", tempreg, treg);
6991           macro_build ((char *) NULL, &icnt, NULL, "drorv",
6992                        "d,t,s", dreg, sreg, tempreg);
6993           if (used_at)
6994             break;
6995           return;
6996         }
6997       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6998                    "d,v,t", AT, 0, treg);
6999       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7000                    "d,t,s", AT, sreg, AT);
7001       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7002                    "d,t,s", dreg, sreg, treg);
7003       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7004                    "d,v,t", dreg, dreg, AT);
7005       break;
7006
7007     case M_ROL:
7008       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7009         {
7010           if (dreg == sreg)
7011             {
7012               tempreg = AT;
7013               used_at = 1;
7014             }
7015           else
7016             {
7017               tempreg = dreg;
7018               used_at = 0;
7019             }
7020           macro_build ((char *) NULL, &icnt, NULL, "negu",
7021                        "d,w", tempreg, treg);
7022           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7023                        "d,t,s", dreg, sreg, tempreg);
7024           if (used_at)
7025             break;
7026           return;
7027         }
7028       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7029                    "d,v,t", AT, 0, treg);
7030       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7031                    "d,t,s", AT, sreg, AT);
7032       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7033                    "d,t,s", dreg, sreg, treg);
7034       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7035                    "d,v,t", dreg, dreg, AT);
7036       break;
7037
7038     case M_DROL_I:
7039       {
7040         unsigned int rot;
7041         char *l, *r;
7042
7043         if (imm_expr.X_op != O_constant)
7044           as_bad (_("Improper rotate count"));
7045         rot = imm_expr.X_add_number & 0x3f;
7046         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7047           {
7048             rot = (64 - rot) & 0x3f;
7049             if (rot >= 32)
7050               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7051                            "d,w,<", dreg, sreg, rot - 32);
7052             else
7053               macro_build ((char *) NULL, &icnt, NULL, "dror",
7054                            "d,w,<", dreg, sreg, rot);
7055             return;
7056           }
7057         if (rot == 0)
7058           {
7059             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7060                          "d,w,<", dreg, sreg, 0);
7061             return;
7062           }
7063         l = (rot < 0x20) ? "dsll" : "dsll32";
7064         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7065         rot &= 0x1f;
7066         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7067                      "d,w,<", AT, sreg, rot);
7068         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7069                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7070         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7071                      "d,v,t", dreg, dreg, AT);
7072       }
7073       break;
7074
7075     case M_ROL_I:
7076       {
7077         unsigned int rot;
7078
7079         if (imm_expr.X_op != O_constant)
7080           as_bad (_("Improper rotate count"));
7081         rot = imm_expr.X_add_number & 0x1f;
7082         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7083           {
7084             macro_build ((char *) NULL, &icnt, NULL, "ror",
7085                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7086             return;
7087           }
7088         if (rot == 0)
7089           {
7090             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7091                          "d,w,<", dreg, sreg, 0);
7092             return;
7093           }
7094         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7095                      "d,w,<", AT, sreg, rot);
7096         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7097                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7098         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7099                      "d,v,t", dreg, dreg, AT);
7100       }
7101       break;
7102
7103     case M_DROR:
7104       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7105         {
7106           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7107                        "d,t,s", dreg, sreg, treg);
7108           return;
7109         }
7110       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7111                    "d,v,t", AT, 0, treg);
7112       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7113                    "d,t,s", AT, sreg, AT);
7114       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7115                    "d,t,s", dreg, sreg, treg);
7116       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7117                    "d,v,t", dreg, dreg, AT);
7118       break;
7119
7120     case M_ROR:
7121       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7122         {
7123           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7124                        "d,t,s", dreg, sreg, treg);
7125           return;
7126         }
7127       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7128                    "d,v,t", AT, 0, treg);
7129       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7130                    "d,t,s", AT, sreg, AT);
7131       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7132                    "d,t,s", dreg, sreg, treg);
7133       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7134                    "d,v,t", dreg, dreg, AT);
7135       break;
7136
7137     case M_DROR_I:
7138       {
7139         unsigned int rot;
7140         char *l, *r;
7141
7142         if (imm_expr.X_op != O_constant)
7143           as_bad (_("Improper rotate count"));
7144         rot = imm_expr.X_add_number & 0x3f;
7145         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7146           {
7147             if (rot >= 32)
7148               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7149                            "d,w,<", dreg, sreg, rot - 32);
7150             else
7151               macro_build ((char *) NULL, &icnt, NULL, "dror",
7152                            "d,w,<", dreg, sreg, rot);
7153             return;
7154           }
7155         if (rot == 0)
7156           {
7157             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7158                          "d,w,<", dreg, sreg, 0);
7159             return;
7160           }
7161         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7162         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7163         rot &= 0x1f;
7164         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7165                      "d,w,<", AT, sreg, rot);
7166         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7167                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7168         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7169                      "d,v,t", dreg, dreg, AT);
7170       }
7171       break;
7172
7173     case M_ROR_I:
7174       {
7175         unsigned int rot;
7176
7177         if (imm_expr.X_op != O_constant)
7178           as_bad (_("Improper rotate count"));
7179         rot = imm_expr.X_add_number & 0x1f;
7180         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7181           {
7182             macro_build ((char *) NULL, &icnt, NULL, "ror",
7183                          "d,w,<", dreg, sreg, rot);
7184             return;
7185           }
7186         if (rot == 0)
7187           {
7188             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7189                          "d,w,<", dreg, sreg, 0);
7190             return;
7191           }
7192         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7193                      "d,w,<", AT, sreg, rot);
7194         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7195                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7196         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7197                      "d,v,t", dreg, dreg, AT);
7198       }
7199       break;
7200
7201     case M_S_DOB:
7202       if (mips_arch == CPU_R4650)
7203         {
7204           as_bad (_("opcode not supported on this processor"));
7205           return;
7206         }
7207       assert (mips_opts.isa == ISA_MIPS1);
7208       /* Even on a big endian machine $fn comes before $fn+1.  We have
7209          to adjust when storing to memory.  */
7210       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7211                    target_big_endian ? treg + 1 : treg,
7212                    (int) BFD_RELOC_LO16, breg);
7213       offset_expr.X_add_number += 4;
7214       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7215                    target_big_endian ? treg : treg + 1,
7216                    (int) BFD_RELOC_LO16, breg);
7217       return;
7218
7219     case M_SEQ:
7220       if (sreg == 0)
7221         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7222                      treg, (int) BFD_RELOC_LO16);
7223       else if (treg == 0)
7224         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7225                      sreg, (int) BFD_RELOC_LO16);
7226       else
7227         {
7228           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7229                        "d,v,t", dreg, sreg, treg);
7230           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7231                        dreg, (int) BFD_RELOC_LO16);
7232         }
7233       return;
7234
7235     case M_SEQ_I:
7236       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7237         {
7238           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7239                        sreg, (int) BFD_RELOC_LO16);
7240           return;
7241         }
7242       if (sreg == 0)
7243         {
7244           as_warn (_("Instruction %s: result is always false"),
7245                    ip->insn_mo->name);
7246           move_register (&icnt, dreg, 0);
7247           return;
7248         }
7249       if (imm_expr.X_op == O_constant
7250           && imm_expr.X_add_number >= 0
7251           && imm_expr.X_add_number < 0x10000)
7252         {
7253           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7254                        sreg, (int) BFD_RELOC_LO16);
7255           used_at = 0;
7256         }
7257       else if (imm_expr.X_op == O_constant
7258                && imm_expr.X_add_number > -0x8000
7259                && imm_expr.X_add_number < 0)
7260         {
7261           imm_expr.X_add_number = -imm_expr.X_add_number;
7262           macro_build ((char *) NULL, &icnt, &imm_expr,
7263                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7264                        "t,r,j", dreg, sreg,
7265                        (int) BFD_RELOC_LO16);
7266           used_at = 0;
7267         }
7268       else
7269         {
7270           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7271           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7272                        "d,v,t", dreg, sreg, AT);
7273           used_at = 1;
7274         }
7275       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7276                    (int) BFD_RELOC_LO16);
7277       if (used_at)
7278         break;
7279       return;
7280
7281     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7282       s = "slt";
7283       goto sge;
7284     case M_SGEU:
7285       s = "sltu";
7286     sge:
7287       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7288                    dreg, sreg, treg);
7289       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7290                    (int) BFD_RELOC_LO16);
7291       return;
7292
7293     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7294     case M_SGEU_I:
7295       if (imm_expr.X_op == O_constant
7296           && imm_expr.X_add_number >= -0x8000
7297           && imm_expr.X_add_number < 0x8000)
7298         {
7299           macro_build ((char *) NULL, &icnt, &imm_expr,
7300                        mask == M_SGE_I ? "slti" : "sltiu",
7301                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7302           used_at = 0;
7303         }
7304       else
7305         {
7306           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7307           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7308                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7309                        AT);
7310           used_at = 1;
7311         }
7312       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7313                    (int) BFD_RELOC_LO16);
7314       if (used_at)
7315         break;
7316       return;
7317
7318     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7319       s = "slt";
7320       goto sgt;
7321     case M_SGTU:
7322       s = "sltu";
7323     sgt:
7324       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7325                    dreg, treg, sreg);
7326       return;
7327
7328     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7329       s = "slt";
7330       goto sgti;
7331     case M_SGTU_I:
7332       s = "sltu";
7333     sgti:
7334       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7335       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7336                    dreg, AT, sreg);
7337       break;
7338
7339     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7340       s = "slt";
7341       goto sle;
7342     case M_SLEU:
7343       s = "sltu";
7344     sle:
7345       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7346                    dreg, treg, sreg);
7347       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7348                    (int) BFD_RELOC_LO16);
7349       return;
7350
7351     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7352       s = "slt";
7353       goto slei;
7354     case M_SLEU_I:
7355       s = "sltu";
7356     slei:
7357       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7358       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7359                    dreg, AT, sreg);
7360       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7361                    (int) BFD_RELOC_LO16);
7362       break;
7363
7364     case M_SLT_I:
7365       if (imm_expr.X_op == O_constant
7366           && imm_expr.X_add_number >= -0x8000
7367           && imm_expr.X_add_number < 0x8000)
7368         {
7369           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7370                        dreg, sreg, (int) BFD_RELOC_LO16);
7371           return;
7372         }
7373       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7374       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7375                    dreg, sreg, AT);
7376       break;
7377
7378     case M_SLTU_I:
7379       if (imm_expr.X_op == O_constant
7380           && imm_expr.X_add_number >= -0x8000
7381           && imm_expr.X_add_number < 0x8000)
7382         {
7383           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7384                        dreg, sreg, (int) BFD_RELOC_LO16);
7385           return;
7386         }
7387       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7388       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7389                    "d,v,t", dreg, sreg, AT);
7390       break;
7391
7392     case M_SNE:
7393       if (sreg == 0)
7394         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7395                      "d,v,t", dreg, 0, treg);
7396       else if (treg == 0)
7397         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7398                      "d,v,t", dreg, 0, sreg);
7399       else
7400         {
7401           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7402                        "d,v,t", dreg, sreg, treg);
7403           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7404                        "d,v,t", dreg, 0, dreg);
7405         }
7406       return;
7407
7408     case M_SNE_I:
7409       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7410         {
7411           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7412                        "d,v,t", dreg, 0, sreg);
7413           return;
7414         }
7415       if (sreg == 0)
7416         {
7417           as_warn (_("Instruction %s: result is always true"),
7418                    ip->insn_mo->name);
7419           macro_build ((char *) NULL, &icnt, &expr1,
7420                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7421                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7422           return;
7423         }
7424       if (imm_expr.X_op == O_constant
7425           && imm_expr.X_add_number >= 0
7426           && imm_expr.X_add_number < 0x10000)
7427         {
7428           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7429                        dreg, sreg, (int) BFD_RELOC_LO16);
7430           used_at = 0;
7431         }
7432       else if (imm_expr.X_op == O_constant
7433                && imm_expr.X_add_number > -0x8000
7434                && imm_expr.X_add_number < 0)
7435         {
7436           imm_expr.X_add_number = -imm_expr.X_add_number;
7437           macro_build ((char *) NULL, &icnt, &imm_expr,
7438                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7439                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7440           used_at = 0;
7441         }
7442       else
7443         {
7444           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7445           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7446                        "d,v,t", dreg, sreg, AT);
7447           used_at = 1;
7448         }
7449       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7450                    "d,v,t", dreg, 0, dreg);
7451       if (used_at)
7452         break;
7453       return;
7454
7455     case M_DSUB_I:
7456       dbl = 1;
7457     case M_SUB_I:
7458       if (imm_expr.X_op == O_constant
7459           && imm_expr.X_add_number > -0x8000
7460           && imm_expr.X_add_number <= 0x8000)
7461         {
7462           imm_expr.X_add_number = -imm_expr.X_add_number;
7463           macro_build ((char *) NULL, &icnt, &imm_expr,
7464                        dbl ? "daddi" : "addi",
7465                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7466           return;
7467         }
7468       load_register (&icnt, AT, &imm_expr, dbl);
7469       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7470                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7471       break;
7472
7473     case M_DSUBU_I:
7474       dbl = 1;
7475     case M_SUBU_I:
7476       if (imm_expr.X_op == O_constant
7477           && imm_expr.X_add_number > -0x8000
7478           && imm_expr.X_add_number <= 0x8000)
7479         {
7480           imm_expr.X_add_number = -imm_expr.X_add_number;
7481           macro_build ((char *) NULL, &icnt, &imm_expr,
7482                        dbl ? "daddiu" : "addiu",
7483                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7484           return;
7485         }
7486       load_register (&icnt, AT, &imm_expr, dbl);
7487       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7488                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7489       break;
7490
7491     case M_TEQ_I:
7492       s = "teq";
7493       goto trap;
7494     case M_TGE_I:
7495       s = "tge";
7496       goto trap;
7497     case M_TGEU_I:
7498       s = "tgeu";
7499       goto trap;
7500     case M_TLT_I:
7501       s = "tlt";
7502       goto trap;
7503     case M_TLTU_I:
7504       s = "tltu";
7505       goto trap;
7506     case M_TNE_I:
7507       s = "tne";
7508     trap:
7509       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7510       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7511                    AT);
7512       break;
7513
7514     case M_TRUNCWS:
7515     case M_TRUNCWD:
7516       assert (mips_opts.isa == ISA_MIPS1);
7517       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7518       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7519
7520       /*
7521        * Is the double cfc1 instruction a bug in the mips assembler;
7522        * or is there a reason for it?
7523        */
7524       mips_emit_delays (TRUE);
7525       ++mips_opts.noreorder;
7526       mips_any_noreorder = 1;
7527       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7528                    treg, RA);
7529       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7530                    treg, RA);
7531       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7532       expr1.X_add_number = 3;
7533       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7534                    (int) BFD_RELOC_LO16);
7535       expr1.X_add_number = 2;
7536       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7537                      (int) BFD_RELOC_LO16);
7538       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7539                    AT, RA);
7540       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7541       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7542               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7543       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7544                    treg, RA);
7545       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7546       --mips_opts.noreorder;
7547       break;
7548
7549     case M_ULH:
7550       s = "lb";
7551       goto ulh;
7552     case M_ULHU:
7553       s = "lbu";
7554     ulh:
7555       if (offset_expr.X_add_number >= 0x7fff)
7556         as_bad (_("operand overflow"));
7557       if (! target_big_endian)
7558         ++offset_expr.X_add_number;
7559       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7560                    (int) BFD_RELOC_LO16, breg);
7561       if (! target_big_endian)
7562         --offset_expr.X_add_number;
7563       else
7564         ++offset_expr.X_add_number;
7565       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7566                    (int) BFD_RELOC_LO16, breg);
7567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7568                    AT, AT, 8);
7569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7570                    treg, treg, AT);
7571       break;
7572
7573     case M_ULD:
7574       s = "ldl";
7575       s2 = "ldr";
7576       off = 7;
7577       goto ulw;
7578     case M_ULW:
7579       s = "lwl";
7580       s2 = "lwr";
7581       off = 3;
7582     ulw:
7583       if (offset_expr.X_add_number >= 0x8000 - off)
7584         as_bad (_("operand overflow"));
7585       if (treg != breg)
7586         tempreg = treg;
7587       else
7588         tempreg = AT;
7589       if (! target_big_endian)
7590         offset_expr.X_add_number += off;
7591       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7592                    (int) BFD_RELOC_LO16, breg);
7593       if (! target_big_endian)
7594         offset_expr.X_add_number -= off;
7595       else
7596         offset_expr.X_add_number += off;
7597       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7598                    (int) BFD_RELOC_LO16, breg);
7599
7600       /* If necessary, move the result in tempreg the final destination.  */
7601       if (treg == tempreg)
7602         return;
7603       /* Protect second load's delay slot.  */
7604       if (!gpr_interlocks)
7605         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7606       move_register (&icnt, treg, tempreg);
7607       break;
7608
7609     case M_ULD_A:
7610       s = "ldl";
7611       s2 = "ldr";
7612       off = 7;
7613       goto ulwa;
7614     case M_ULW_A:
7615       s = "lwl";
7616       s2 = "lwr";
7617       off = 3;
7618     ulwa:
7619       used_at = 1;
7620       load_address (&icnt, AT, &offset_expr, &used_at);
7621       if (breg != 0)
7622         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7623                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7624                      "d,v,t", AT, AT, breg);
7625       if (! target_big_endian)
7626         expr1.X_add_number = off;
7627       else
7628         expr1.X_add_number = 0;
7629       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7630                    (int) BFD_RELOC_LO16, AT);
7631       if (! target_big_endian)
7632         expr1.X_add_number = 0;
7633       else
7634         expr1.X_add_number = off;
7635       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7636                    (int) BFD_RELOC_LO16, AT);
7637       break;
7638
7639     case M_ULH_A:
7640     case M_ULHU_A:
7641       used_at = 1;
7642       load_address (&icnt, AT, &offset_expr, &used_at);
7643       if (breg != 0)
7644         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7645                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7646                      "d,v,t", AT, AT, breg);
7647       if (target_big_endian)
7648         expr1.X_add_number = 0;
7649       macro_build ((char *) NULL, &icnt, &expr1,
7650                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7651                    (int) BFD_RELOC_LO16, AT);
7652       if (target_big_endian)
7653         expr1.X_add_number = 1;
7654       else
7655         expr1.X_add_number = 0;
7656       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7657                    (int) BFD_RELOC_LO16, AT);
7658       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7659                    treg, treg, 8);
7660       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7661                    treg, treg, AT);
7662       break;
7663
7664     case M_USH:
7665       if (offset_expr.X_add_number >= 0x7fff)
7666         as_bad (_("operand overflow"));
7667       if (target_big_endian)
7668         ++offset_expr.X_add_number;
7669       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7670                    (int) BFD_RELOC_LO16, breg);
7671       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7672                    AT, treg, 8);
7673       if (target_big_endian)
7674         --offset_expr.X_add_number;
7675       else
7676         ++offset_expr.X_add_number;
7677       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7678                    (int) BFD_RELOC_LO16, breg);
7679       break;
7680
7681     case M_USD:
7682       s = "sdl";
7683       s2 = "sdr";
7684       off = 7;
7685       goto usw;
7686     case M_USW:
7687       s = "swl";
7688       s2 = "swr";
7689       off = 3;
7690     usw:
7691       if (offset_expr.X_add_number >= 0x8000 - off)
7692         as_bad (_("operand overflow"));
7693       if (! target_big_endian)
7694         offset_expr.X_add_number += off;
7695       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7696                    (int) BFD_RELOC_LO16, breg);
7697       if (! target_big_endian)
7698         offset_expr.X_add_number -= off;
7699       else
7700         offset_expr.X_add_number += off;
7701       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7702                    (int) BFD_RELOC_LO16, breg);
7703       return;
7704
7705     case M_USD_A:
7706       s = "sdl";
7707       s2 = "sdr";
7708       off = 7;
7709       goto uswa;
7710     case M_USW_A:
7711       s = "swl";
7712       s2 = "swr";
7713       off = 3;
7714     uswa:
7715       used_at = 1;
7716       load_address (&icnt, AT, &offset_expr, &used_at);
7717       if (breg != 0)
7718         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7719                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7720                      "d,v,t", AT, AT, breg);
7721       if (! target_big_endian)
7722         expr1.X_add_number = off;
7723       else
7724         expr1.X_add_number = 0;
7725       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7726                    (int) BFD_RELOC_LO16, AT);
7727       if (! target_big_endian)
7728         expr1.X_add_number = 0;
7729       else
7730         expr1.X_add_number = off;
7731       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7732                    (int) BFD_RELOC_LO16, AT);
7733       break;
7734
7735     case M_USH_A:
7736       used_at = 1;
7737       load_address (&icnt, AT, &offset_expr, &used_at);
7738       if (breg != 0)
7739         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7740                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7741                      "d,v,t", AT, AT, breg);
7742       if (! target_big_endian)
7743         expr1.X_add_number = 0;
7744       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7745                    (int) BFD_RELOC_LO16, AT);
7746       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7747                    treg, treg, 8);
7748       if (! target_big_endian)
7749         expr1.X_add_number = 1;
7750       else
7751         expr1.X_add_number = 0;
7752       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7753                    (int) BFD_RELOC_LO16, AT);
7754       if (! target_big_endian)
7755         expr1.X_add_number = 0;
7756       else
7757         expr1.X_add_number = 1;
7758       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7759                    (int) BFD_RELOC_LO16, AT);
7760       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7761                    treg, treg, 8);
7762       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7763                    treg, treg, AT);
7764       break;
7765
7766     default:
7767       /* FIXME: Check if this is one of the itbl macros, since they
7768          are added dynamically.  */
7769       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7770       break;
7771     }
7772   if (mips_opts.noat)
7773     as_warn (_("Macro used $at after \".set noat\""));
7774 }
7775
7776 /* Implement macros in mips16 mode.  */
7777
7778 static void
7779 mips16_macro (ip)
7780      struct mips_cl_insn *ip;
7781 {
7782   int mask;
7783   int xreg, yreg, zreg, tmp;
7784   int icnt;
7785   expressionS expr1;
7786   int dbl;
7787   const char *s, *s2, *s3;
7788
7789   mask = ip->insn_mo->mask;
7790
7791   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7792   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7793   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7794
7795   icnt = 0;
7796
7797   expr1.X_op = O_constant;
7798   expr1.X_op_symbol = NULL;
7799   expr1.X_add_symbol = NULL;
7800   expr1.X_add_number = 1;
7801
7802   dbl = 0;
7803
7804   switch (mask)
7805     {
7806     default:
7807       internalError ();
7808
7809     case M_DDIV_3:
7810       dbl = 1;
7811     case M_DIV_3:
7812       s = "mflo";
7813       goto do_div3;
7814     case M_DREM_3:
7815       dbl = 1;
7816     case M_REM_3:
7817       s = "mfhi";
7818     do_div3:
7819       mips_emit_delays (TRUE);
7820       ++mips_opts.noreorder;
7821       mips_any_noreorder = 1;
7822       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7823                    dbl ? "ddiv" : "div",
7824                    "0,x,y", xreg, yreg);
7825       expr1.X_add_number = 2;
7826       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7827       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7828                    7);
7829
7830       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7831          since that causes an overflow.  We should do that as well,
7832          but I don't see how to do the comparisons without a temporary
7833          register.  */
7834       --mips_opts.noreorder;
7835       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7836       break;
7837
7838     case M_DIVU_3:
7839       s = "divu";
7840       s2 = "mflo";
7841       goto do_divu3;
7842     case M_REMU_3:
7843       s = "divu";
7844       s2 = "mfhi";
7845       goto do_divu3;
7846     case M_DDIVU_3:
7847       s = "ddivu";
7848       s2 = "mflo";
7849       goto do_divu3;
7850     case M_DREMU_3:
7851       s = "ddivu";
7852       s2 = "mfhi";
7853     do_divu3:
7854       mips_emit_delays (TRUE);
7855       ++mips_opts.noreorder;
7856       mips_any_noreorder = 1;
7857       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7858                    xreg, yreg);
7859       expr1.X_add_number = 2;
7860       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7861       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7862                    "6", 7);
7863       --mips_opts.noreorder;
7864       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7865       break;
7866
7867     case M_DMUL:
7868       dbl = 1;
7869     case M_MUL:
7870       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7871                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7872       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7873                    zreg);
7874       return;
7875
7876     case M_DSUBU_I:
7877       dbl = 1;
7878       goto do_subu;
7879     case M_SUBU_I:
7880     do_subu:
7881       if (imm_expr.X_op != O_constant)
7882         as_bad (_("Unsupported large constant"));
7883       imm_expr.X_add_number = -imm_expr.X_add_number;
7884       macro_build ((char *) NULL, &icnt, &imm_expr,
7885                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7886       break;
7887
7888     case M_SUBU_I_2:
7889       if (imm_expr.X_op != O_constant)
7890         as_bad (_("Unsupported large constant"));
7891       imm_expr.X_add_number = -imm_expr.X_add_number;
7892       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7893                    "x,k", xreg);
7894       break;
7895
7896     case M_DSUBU_I_2:
7897       if (imm_expr.X_op != O_constant)
7898         as_bad (_("Unsupported large constant"));
7899       imm_expr.X_add_number = -imm_expr.X_add_number;
7900       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7901                    "y,j", yreg);
7902       break;
7903
7904     case M_BEQ:
7905       s = "cmp";
7906       s2 = "bteqz";
7907       goto do_branch;
7908     case M_BNE:
7909       s = "cmp";
7910       s2 = "btnez";
7911       goto do_branch;
7912     case M_BLT:
7913       s = "slt";
7914       s2 = "btnez";
7915       goto do_branch;
7916     case M_BLTU:
7917       s = "sltu";
7918       s2 = "btnez";
7919       goto do_branch;
7920     case M_BLE:
7921       s = "slt";
7922       s2 = "bteqz";
7923       goto do_reverse_branch;
7924     case M_BLEU:
7925       s = "sltu";
7926       s2 = "bteqz";
7927       goto do_reverse_branch;
7928     case M_BGE:
7929       s = "slt";
7930       s2 = "bteqz";
7931       goto do_branch;
7932     case M_BGEU:
7933       s = "sltu";
7934       s2 = "bteqz";
7935       goto do_branch;
7936     case M_BGT:
7937       s = "slt";
7938       s2 = "btnez";
7939       goto do_reverse_branch;
7940     case M_BGTU:
7941       s = "sltu";
7942       s2 = "btnez";
7943
7944     do_reverse_branch:
7945       tmp = xreg;
7946       xreg = yreg;
7947       yreg = tmp;
7948
7949     do_branch:
7950       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7951                    xreg, yreg);
7952       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7953       break;
7954
7955     case M_BEQ_I:
7956       s = "cmpi";
7957       s2 = "bteqz";
7958       s3 = "x,U";
7959       goto do_branch_i;
7960     case M_BNE_I:
7961       s = "cmpi";
7962       s2 = "btnez";
7963       s3 = "x,U";
7964       goto do_branch_i;
7965     case M_BLT_I:
7966       s = "slti";
7967       s2 = "btnez";
7968       s3 = "x,8";
7969       goto do_branch_i;
7970     case M_BLTU_I:
7971       s = "sltiu";
7972       s2 = "btnez";
7973       s3 = "x,8";
7974       goto do_branch_i;
7975     case M_BLE_I:
7976       s = "slti";
7977       s2 = "btnez";
7978       s3 = "x,8";
7979       goto do_addone_branch_i;
7980     case M_BLEU_I:
7981       s = "sltiu";
7982       s2 = "btnez";
7983       s3 = "x,8";
7984       goto do_addone_branch_i;
7985     case M_BGE_I:
7986       s = "slti";
7987       s2 = "bteqz";
7988       s3 = "x,8";
7989       goto do_branch_i;
7990     case M_BGEU_I:
7991       s = "sltiu";
7992       s2 = "bteqz";
7993       s3 = "x,8";
7994       goto do_branch_i;
7995     case M_BGT_I:
7996       s = "slti";
7997       s2 = "bteqz";
7998       s3 = "x,8";
7999       goto do_addone_branch_i;
8000     case M_BGTU_I:
8001       s = "sltiu";
8002       s2 = "bteqz";
8003       s3 = "x,8";
8004
8005     do_addone_branch_i:
8006       if (imm_expr.X_op != O_constant)
8007         as_bad (_("Unsupported large constant"));
8008       ++imm_expr.X_add_number;
8009
8010     do_branch_i:
8011       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8012       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8013       break;
8014
8015     case M_ABS:
8016       expr1.X_add_number = 0;
8017       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8018       if (xreg != yreg)
8019         move_register (&icnt, xreg, yreg);
8020       expr1.X_add_number = 2;
8021       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8022       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8023                    "neg", "x,w", xreg, xreg);
8024     }
8025 }
8026
8027 /* For consistency checking, verify that all bits are specified either
8028    by the match/mask part of the instruction definition, or by the
8029    operand list.  */
8030 static int
8031 validate_mips_insn (opc)
8032      const struct mips_opcode *opc;
8033 {
8034   const char *p = opc->args;
8035   char c;
8036   unsigned long used_bits = opc->mask;
8037
8038   if ((used_bits & opc->match) != opc->match)
8039     {
8040       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8041               opc->name, opc->args);
8042       return 0;
8043     }
8044 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8045   while (*p)
8046     switch (c = *p++)
8047       {
8048       case ',': break;
8049       case '(': break;
8050       case ')': break;
8051       case '+':
8052         switch (c = *p++)
8053           {
8054           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8055           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8056           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8057           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8058                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8059           default:
8060             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8061                     c, opc->name, opc->args);
8062             return 0;
8063           }
8064         break;
8065       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8066       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8067       case 'A': break;
8068       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8069       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8070       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8071       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8072       case 'F': break;
8073       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8074       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8075       case 'I': break;
8076       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8077       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8078       case 'L': break;
8079       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8080       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8081       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8082       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8083                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8084       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8085       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8086       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8087       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8088       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8089       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8090       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8091       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8092       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8093       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8094       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8095       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8096       case 'f': break;
8097       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8098       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8099       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8100       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8101       case 'l': break;
8102       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8103       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8104       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8105       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8106       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8107       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8108       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8109       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8110       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8111       case 'x': break;
8112       case 'z': break;
8113       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8114       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8115                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8116       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8117       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8118       case '[': break;
8119       case ']': break;
8120       default:
8121         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8122                 c, opc->name, opc->args);
8123         return 0;
8124       }
8125 #undef USE_BITS
8126   if (used_bits != 0xffffffff)
8127     {
8128       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8129               ~used_bits & 0xffffffff, opc->name, opc->args);
8130       return 0;
8131     }
8132   return 1;
8133 }
8134
8135 /* This routine assembles an instruction into its binary format.  As a
8136    side effect, it sets one of the global variables imm_reloc or
8137    offset_reloc to the type of relocation to do if one of the operands
8138    is an address expression.  */
8139
8140 static void
8141 mips_ip (str, ip)
8142      char *str;
8143      struct mips_cl_insn *ip;
8144 {
8145   char *s;
8146   const char *args;
8147   char c = 0;
8148   struct mips_opcode *insn;
8149   char *argsStart;
8150   unsigned int regno;
8151   unsigned int lastregno = 0;
8152   unsigned int lastpos = 0;
8153   unsigned int limlo, limhi;
8154   char *s_reset;
8155   char save_c = 0;
8156
8157   insn_error = NULL;
8158
8159   /* If the instruction contains a '.', we first try to match an instruction
8160      including the '.'.  Then we try again without the '.'.  */
8161   insn = NULL;
8162   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8163     continue;
8164
8165   /* If we stopped on whitespace, then replace the whitespace with null for
8166      the call to hash_find.  Save the character we replaced just in case we
8167      have to re-parse the instruction.  */
8168   if (ISSPACE (*s))
8169     {
8170       save_c = *s;
8171       *s++ = '\0';
8172     }
8173
8174   insn = (struct mips_opcode *) hash_find (op_hash, str);
8175
8176   /* If we didn't find the instruction in the opcode table, try again, but
8177      this time with just the instruction up to, but not including the
8178      first '.'.  */
8179   if (insn == NULL)
8180     {
8181       /* Restore the character we overwrite above (if any).  */
8182       if (save_c)
8183         *(--s) = save_c;
8184
8185       /* Scan up to the first '.' or whitespace.  */
8186       for (s = str;
8187            *s != '\0' && *s != '.' && !ISSPACE (*s);
8188            ++s)
8189         continue;
8190
8191       /* If we did not find a '.', then we can quit now.  */
8192       if (*s != '.')
8193         {
8194           insn_error = "unrecognized opcode";
8195           return;
8196         }
8197
8198       /* Lookup the instruction in the hash table.  */
8199       *s++ = '\0';
8200       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8201         {
8202           insn_error = "unrecognized opcode";
8203           return;
8204         }
8205     }
8206
8207   argsStart = s;
8208   for (;;)
8209     {
8210       bfd_boolean ok;
8211
8212       assert (strcmp (insn->name, str) == 0);
8213
8214       if (OPCODE_IS_MEMBER (insn,
8215                             (mips_opts.isa
8216                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8217                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8218                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8219                             mips_arch))
8220         ok = TRUE;
8221       else
8222         ok = FALSE;
8223
8224       if (insn->pinfo != INSN_MACRO)
8225         {
8226           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8227             ok = FALSE;
8228         }
8229
8230       if (! ok)
8231         {
8232           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8233               && strcmp (insn->name, insn[1].name) == 0)
8234             {
8235               ++insn;
8236               continue;
8237             }
8238           else
8239             {
8240               if (!insn_error)
8241                 {
8242                   static char buf[100];
8243                   if (mips_arch_info->is_isa)
8244                     sprintf (buf,
8245                              _("opcode not supported at this ISA level (%s)"),
8246                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8247                   else
8248                     sprintf (buf,
8249                              _("opcode not supported on this processor: %s (%s)"),
8250                              mips_arch_info->name,
8251                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8252                   insn_error = buf;
8253                 }
8254               if (save_c)
8255                 *(--s) = save_c;
8256               return;
8257             }
8258         }
8259
8260       ip->insn_mo = insn;
8261       ip->insn_opcode = insn->match;
8262       insn_error = NULL;
8263       for (args = insn->args;; ++args)
8264         {
8265           int is_mdmx;
8266
8267           s += strspn (s, " \t");
8268           is_mdmx = 0;
8269           switch (*args)
8270             {
8271             case '\0':          /* end of args */
8272               if (*s == '\0')
8273                 return;
8274               break;
8275
8276             case ',':
8277               if (*s++ == *args)
8278                 continue;
8279               s--;
8280               switch (*++args)
8281                 {
8282                 case 'r':
8283                 case 'v':
8284                   ip->insn_opcode |= lastregno << OP_SH_RS;
8285                   continue;
8286
8287                 case 'w':
8288                   ip->insn_opcode |= lastregno << OP_SH_RT;
8289                   continue;
8290
8291                 case 'W':
8292                   ip->insn_opcode |= lastregno << OP_SH_FT;
8293                   continue;
8294
8295                 case 'V':
8296                   ip->insn_opcode |= lastregno << OP_SH_FS;
8297                   continue;
8298                 }
8299               break;
8300
8301             case '(':
8302               /* Handle optional base register.
8303                  Either the base register is omitted or
8304                  we must have a left paren.  */
8305               /* This is dependent on the next operand specifier
8306                  is a base register specification.  */
8307               assert (args[1] == 'b' || args[1] == '5'
8308                       || args[1] == '-' || args[1] == '4');
8309               if (*s == '\0')
8310                 return;
8311
8312             case ')':           /* these must match exactly */
8313             case '[':
8314             case ']':
8315               if (*s++ == *args)
8316                 continue;
8317               break;
8318
8319             case '+':           /* Opcode extension character.  */
8320               switch (*++args)
8321                 {
8322                 case 'A':               /* ins/ext position, becomes LSB.  */
8323                   limlo = 0;
8324                   limhi = 31;
8325                   my_getExpression (&imm_expr, s);
8326                   check_absolute_expr (ip, &imm_expr);
8327                   if ((unsigned long) imm_expr.X_add_number < limlo
8328                       || (unsigned long) imm_expr.X_add_number > limhi)
8329                     {
8330                       as_bad (_("Improper position (%lu)"),
8331                               (unsigned long) imm_expr.X_add_number);
8332                       imm_expr.X_add_number = limlo;
8333                     }
8334                   lastpos = imm_expr.X_add_number;
8335                   ip->insn_opcode |= (imm_expr.X_add_number
8336                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8337                   imm_expr.X_op = O_absent;
8338                   s = expr_end;
8339                   continue;
8340
8341                 case 'B':               /* ins size, becomes MSB.  */
8342                   limlo = 1;
8343                   limhi = 32;
8344                   my_getExpression (&imm_expr, s);
8345                   check_absolute_expr (ip, &imm_expr);
8346                   /* Check for negative input so that small negative numbers
8347                      will not succeed incorrectly.  The checks against
8348                      (pos+size) transitively check "size" itself,
8349                      assuming that "pos" is reasonable.  */
8350                   if ((long) imm_expr.X_add_number < 0
8351                       || ((unsigned long) imm_expr.X_add_number
8352                           + lastpos) < limlo
8353                       || ((unsigned long) imm_expr.X_add_number
8354                           + lastpos) > limhi)
8355                     {
8356                       as_bad (_("Improper insert size (%lu, position %lu)"),
8357                               (unsigned long) imm_expr.X_add_number,
8358                               (unsigned long) lastpos);
8359                       imm_expr.X_add_number = limlo - lastpos;
8360                     }
8361                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8362                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8363                   imm_expr.X_op = O_absent;
8364                   s = expr_end;
8365                   continue;
8366
8367                 case 'C':               /* ext size, becomes MSBD.  */
8368                   limlo = 1;
8369                   limhi = 32;
8370                   my_getExpression (&imm_expr, s);
8371                   check_absolute_expr (ip, &imm_expr);
8372                   /* Check for negative input so that small negative numbers
8373                      will not succeed incorrectly.  The checks against
8374                      (pos+size) transitively check "size" itself,
8375                      assuming that "pos" is reasonable.  */
8376                   if ((long) imm_expr.X_add_number < 0
8377                       || ((unsigned long) imm_expr.X_add_number
8378                           + lastpos) < limlo
8379                       || ((unsigned long) imm_expr.X_add_number
8380                           + lastpos) > limhi)
8381                     {
8382                       as_bad (_("Improper extract size (%lu, position %lu)"),
8383                               (unsigned long) imm_expr.X_add_number,
8384                               (unsigned long) lastpos);
8385                       imm_expr.X_add_number = limlo - lastpos;
8386                     }
8387                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8388                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8389                   imm_expr.X_op = O_absent;
8390                   s = expr_end;
8391                   continue;
8392
8393                 case 'D':
8394                   /* +D is for disassembly only; never match.  */
8395                   break;
8396
8397                 default:
8398                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8399                     *args, insn->name, insn->args);
8400                   /* Further processing is fruitless.  */
8401                   return;
8402                 }
8403               break;
8404
8405             case '<':           /* must be at least one digit */
8406               /*
8407                * According to the manual, if the shift amount is greater
8408                * than 31 or less than 0, then the shift amount should be
8409                * mod 32.  In reality the mips assembler issues an error.
8410                * We issue a warning and mask out all but the low 5 bits.
8411                */
8412               my_getExpression (&imm_expr, s);
8413               check_absolute_expr (ip, &imm_expr);
8414               if ((unsigned long) imm_expr.X_add_number > 31)
8415                 {
8416                   as_warn (_("Improper shift amount (%lu)"),
8417                            (unsigned long) imm_expr.X_add_number);
8418                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8419                 }
8420               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8421               imm_expr.X_op = O_absent;
8422               s = expr_end;
8423               continue;
8424
8425             case '>':           /* shift amount minus 32 */
8426               my_getExpression (&imm_expr, s);
8427               check_absolute_expr (ip, &imm_expr);
8428               if ((unsigned long) imm_expr.X_add_number < 32
8429                   || (unsigned long) imm_expr.X_add_number > 63)
8430                 break;
8431               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8432               imm_expr.X_op = O_absent;
8433               s = expr_end;
8434               continue;
8435
8436             case 'k':           /* cache code */
8437             case 'h':           /* prefx code */
8438               my_getExpression (&imm_expr, s);
8439               check_absolute_expr (ip, &imm_expr);
8440               if ((unsigned long) imm_expr.X_add_number > 31)
8441                 {
8442                   as_warn (_("Invalid value for `%s' (%lu)"),
8443                            ip->insn_mo->name,
8444                            (unsigned long) imm_expr.X_add_number);
8445                   imm_expr.X_add_number &= 0x1f;
8446                 }
8447               if (*args == 'k')
8448                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8449               else
8450                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8451               imm_expr.X_op = O_absent;
8452               s = expr_end;
8453               continue;
8454
8455             case 'c':           /* break code */
8456               my_getExpression (&imm_expr, s);
8457               check_absolute_expr (ip, &imm_expr);
8458               if ((unsigned long) imm_expr.X_add_number > 1023)
8459                 {
8460                   as_warn (_("Illegal break code (%lu)"),
8461                            (unsigned long) imm_expr.X_add_number);
8462                   imm_expr.X_add_number &= OP_MASK_CODE;
8463                 }
8464               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8465               imm_expr.X_op = O_absent;
8466               s = expr_end;
8467               continue;
8468
8469             case 'q':           /* lower break code */
8470               my_getExpression (&imm_expr, s);
8471               check_absolute_expr (ip, &imm_expr);
8472               if ((unsigned long) imm_expr.X_add_number > 1023)
8473                 {
8474                   as_warn (_("Illegal lower break code (%lu)"),
8475                            (unsigned long) imm_expr.X_add_number);
8476                   imm_expr.X_add_number &= OP_MASK_CODE2;
8477                 }
8478               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8479               imm_expr.X_op = O_absent;
8480               s = expr_end;
8481               continue;
8482
8483             case 'B':           /* 20-bit syscall/break code.  */
8484               my_getExpression (&imm_expr, s);
8485               check_absolute_expr (ip, &imm_expr);
8486               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8487                 as_warn (_("Illegal 20-bit code (%lu)"),
8488                          (unsigned long) imm_expr.X_add_number);
8489               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8490               imm_expr.X_op = O_absent;
8491               s = expr_end;
8492               continue;
8493
8494             case 'C':           /* Coprocessor code */
8495               my_getExpression (&imm_expr, s);
8496               check_absolute_expr (ip, &imm_expr);
8497               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8498                 {
8499                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8500                            (unsigned long) imm_expr.X_add_number);
8501                   imm_expr.X_add_number &= ((1 << 25) - 1);
8502                 }
8503               ip->insn_opcode |= imm_expr.X_add_number;
8504               imm_expr.X_op = O_absent;
8505               s = expr_end;
8506               continue;
8507
8508             case 'J':           /* 19-bit wait code.  */
8509               my_getExpression (&imm_expr, s);
8510               check_absolute_expr (ip, &imm_expr);
8511               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8512                 as_warn (_("Illegal 19-bit code (%lu)"),
8513                          (unsigned long) imm_expr.X_add_number);
8514               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8515               imm_expr.X_op = O_absent;
8516               s = expr_end;
8517               continue;
8518
8519             case 'P':           /* Performance register */
8520               my_getExpression (&imm_expr, s);
8521               check_absolute_expr (ip, &imm_expr);
8522               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8523                 {
8524                   as_warn (_("Invalid performance register (%lu)"),
8525                            (unsigned long) imm_expr.X_add_number);
8526                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8527                 }
8528               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8529               imm_expr.X_op = O_absent;
8530               s = expr_end;
8531               continue;
8532
8533             case 'b':           /* base register */
8534             case 'd':           /* destination register */
8535             case 's':           /* source register */
8536             case 't':           /* target register */
8537             case 'r':           /* both target and source */
8538             case 'v':           /* both dest and source */
8539             case 'w':           /* both dest and target */
8540             case 'E':           /* coprocessor target register */
8541             case 'G':           /* coprocessor destination register */
8542             case 'K':           /* 'rdhwr' destination register */
8543             case 'x':           /* ignore register name */
8544             case 'z':           /* must be zero register */
8545             case 'U':           /* destination register (clo/clz).  */
8546               s_reset = s;
8547               if (s[0] == '$')
8548                 {
8549
8550                   if (ISDIGIT (s[1]))
8551                     {
8552                       ++s;
8553                       regno = 0;
8554                       do
8555                         {
8556                           regno *= 10;
8557                           regno += *s - '0';
8558                           ++s;
8559                         }
8560                       while (ISDIGIT (*s));
8561                       if (regno > 31)
8562                         as_bad (_("Invalid register number (%d)"), regno);
8563                     }
8564                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8565                     goto notreg;
8566                   else
8567                     {
8568                       if (s[1] == 'r' && s[2] == 'a')
8569                         {
8570                           s += 3;
8571                           regno = RA;
8572                         }
8573                       else if (s[1] == 'f' && s[2] == 'p')
8574                         {
8575                           s += 3;
8576                           regno = FP;
8577                         }
8578                       else if (s[1] == 's' && s[2] == 'p')
8579                         {
8580                           s += 3;
8581                           regno = SP;
8582                         }
8583                       else if (s[1] == 'g' && s[2] == 'p')
8584                         {
8585                           s += 3;
8586                           regno = GP;
8587                         }
8588                       else if (s[1] == 'a' && s[2] == 't')
8589                         {
8590                           s += 3;
8591                           regno = AT;
8592                         }
8593                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8594                         {
8595                           s += 4;
8596                           regno = KT0;
8597                         }
8598                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8599                         {
8600                           s += 4;
8601                           regno = KT1;
8602                         }
8603                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8604                         {
8605                           s += 5;
8606                           regno = ZERO;
8607                         }
8608                       else if (itbl_have_entries)
8609                         {
8610                           char *p, *n;
8611                           unsigned long r;
8612
8613                           p = s + 1;    /* advance past '$' */
8614                           n = itbl_get_field (&p);  /* n is name */
8615
8616                           /* See if this is a register defined in an
8617                              itbl entry.  */
8618                           if (itbl_get_reg_val (n, &r))
8619                             {
8620                               /* Get_field advances to the start of
8621                                  the next field, so we need to back
8622                                  rack to the end of the last field.  */
8623                               if (p)
8624                                 s = p - 1;
8625                               else
8626                                 s = strchr (s, '\0');
8627                               regno = r;
8628                             }
8629                           else
8630                             goto notreg;
8631                         }
8632                       else
8633                         goto notreg;
8634                     }
8635                   if (regno == AT
8636                       && ! mips_opts.noat
8637                       && *args != 'E'
8638                       && *args != 'G'
8639                       && *args != 'K')
8640                     as_warn (_("Used $at without \".set noat\""));
8641                   c = *args;
8642                   if (*s == ' ')
8643                     ++s;
8644                   if (args[1] != *s)
8645                     {
8646                       if (c == 'r' || c == 'v' || c == 'w')
8647                         {
8648                           regno = lastregno;
8649                           s = s_reset;
8650                           ++args;
8651                         }
8652                     }
8653                   /* 'z' only matches $0.  */
8654                   if (c == 'z' && regno != 0)
8655                     break;
8656
8657         /* Now that we have assembled one operand, we use the args string
8658          * to figure out where it goes in the instruction.  */
8659                   switch (c)
8660                     {
8661                     case 'r':
8662                     case 's':
8663                     case 'v':
8664                     case 'b':
8665                       ip->insn_opcode |= regno << OP_SH_RS;
8666                       break;
8667                     case 'd':
8668                     case 'G':
8669                     case 'K':
8670                       ip->insn_opcode |= regno << OP_SH_RD;
8671                       break;
8672                     case 'U':
8673                       ip->insn_opcode |= regno << OP_SH_RD;
8674                       ip->insn_opcode |= regno << OP_SH_RT;
8675                       break;
8676                     case 'w':
8677                     case 't':
8678                     case 'E':
8679                       ip->insn_opcode |= regno << OP_SH_RT;
8680                       break;
8681                     case 'x':
8682                       /* This case exists because on the r3000 trunc
8683                          expands into a macro which requires a gp
8684                          register.  On the r6000 or r4000 it is
8685                          assembled into a single instruction which
8686                          ignores the register.  Thus the insn version
8687                          is MIPS_ISA2 and uses 'x', and the macro
8688                          version is MIPS_ISA1 and uses 't'.  */
8689                       break;
8690                     case 'z':
8691                       /* This case is for the div instruction, which
8692                          acts differently if the destination argument
8693                          is $0.  This only matches $0, and is checked
8694                          outside the switch.  */
8695                       break;
8696                     case 'D':
8697                       /* Itbl operand; not yet implemented. FIXME ?? */
8698                       break;
8699                       /* What about all other operands like 'i', which
8700                          can be specified in the opcode table? */
8701                     }
8702                   lastregno = regno;
8703                   continue;
8704                 }
8705             notreg:
8706               switch (*args++)
8707                 {
8708                 case 'r':
8709                 case 'v':
8710                   ip->insn_opcode |= lastregno << OP_SH_RS;
8711                   continue;
8712                 case 'w':
8713                   ip->insn_opcode |= lastregno << OP_SH_RT;
8714                   continue;
8715                 }
8716               break;
8717
8718             case 'O':           /* MDMX alignment immediate constant.  */
8719               my_getExpression (&imm_expr, s);
8720               check_absolute_expr (ip, &imm_expr);
8721               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8722                 {
8723                   as_warn ("Improper align amount (%ld), using low bits",
8724                            (long) imm_expr.X_add_number);
8725                   imm_expr.X_add_number &= OP_MASK_ALN;
8726                 }
8727               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8728               imm_expr.X_op = O_absent;
8729               s = expr_end;
8730               continue;
8731
8732             case 'Q':           /* MDMX vector, element sel, or const.  */
8733               if (s[0] != '$')
8734                 {
8735                   /* MDMX Immediate.  */
8736                   my_getExpression (&imm_expr, s);
8737                   check_absolute_expr (ip, &imm_expr);
8738                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8739                     {
8740                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8741                                (long) imm_expr.X_add_number);
8742                       imm_expr.X_add_number &= OP_MASK_FT;
8743                     }
8744                   imm_expr.X_add_number &= OP_MASK_FT;
8745                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8746                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8747                   else
8748                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8749                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8750                   imm_expr.X_op = O_absent;
8751                   s = expr_end;
8752                   continue;
8753                 }
8754               /* Not MDMX Immediate.  Fall through.  */
8755             case 'X':           /* MDMX destination register.  */
8756             case 'Y':           /* MDMX source register.  */
8757             case 'Z':           /* MDMX target register.  */
8758               is_mdmx = 1;
8759             case 'D':           /* floating point destination register */
8760             case 'S':           /* floating point source register */
8761             case 'T':           /* floating point target register */
8762             case 'R':           /* floating point source register */
8763             case 'V':
8764             case 'W':
8765               s_reset = s;
8766               /* Accept $fN for FP and MDMX register numbers, and in
8767                  addition accept $vN for MDMX register numbers.  */
8768               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8769                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8770                       && ISDIGIT (s[2])))
8771                 {
8772                   s += 2;
8773                   regno = 0;
8774                   do
8775                     {
8776                       regno *= 10;
8777                       regno += *s - '0';
8778                       ++s;
8779                     }
8780                   while (ISDIGIT (*s));
8781
8782                   if (regno > 31)
8783                     as_bad (_("Invalid float register number (%d)"), regno);
8784
8785                   if ((regno & 1) != 0
8786                       && HAVE_32BIT_FPRS
8787                       && ! (strcmp (str, "mtc1") == 0
8788                             || strcmp (str, "mfc1") == 0
8789                             || strcmp (str, "lwc1") == 0
8790                             || strcmp (str, "swc1") == 0
8791                             || strcmp (str, "l.s") == 0
8792                             || strcmp (str, "s.s") == 0))
8793                     as_warn (_("Float register should be even, was %d"),
8794                              regno);
8795
8796                   c = *args;
8797                   if (*s == ' ')
8798                     ++s;
8799                   if (args[1] != *s)
8800                     {
8801                       if (c == 'V' || c == 'W')
8802                         {
8803                           regno = lastregno;
8804                           s = s_reset;
8805                           ++args;
8806                         }
8807                     }
8808                   switch (c)
8809                     {
8810                     case 'D':
8811                     case 'X':
8812                       ip->insn_opcode |= regno << OP_SH_FD;
8813                       break;
8814                     case 'V':
8815                     case 'S':
8816                     case 'Y':
8817                       ip->insn_opcode |= regno << OP_SH_FS;
8818                       break;
8819                     case 'Q':
8820                       /* This is like 'Z', but also needs to fix the MDMX
8821                          vector/scalar select bits.  Note that the
8822                          scalar immediate case is handled above.  */
8823                       if (*s == '[')
8824                         {
8825                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8826                           int max_el = (is_qh ? 3 : 7);
8827                           s++;
8828                           my_getExpression(&imm_expr, s);
8829                           check_absolute_expr (ip, &imm_expr);
8830                           s = expr_end;
8831                           if (imm_expr.X_add_number > max_el)
8832                             as_bad(_("Bad element selector %ld"),
8833                                    (long) imm_expr.X_add_number);
8834                           imm_expr.X_add_number &= max_el;
8835                           ip->insn_opcode |= (imm_expr.X_add_number
8836                                               << (OP_SH_VSEL +
8837                                                   (is_qh ? 2 : 1)));
8838                           if (*s != ']')
8839                             as_warn(_("Expecting ']' found '%s'"), s);
8840                           else
8841                             s++;
8842                         }
8843                       else
8844                         {
8845                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8846                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8847                                                 << OP_SH_VSEL);
8848                           else
8849                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8850                                                 OP_SH_VSEL);
8851                         }
8852                       /* Fall through */
8853                     case 'W':
8854                     case 'T':
8855                     case 'Z':
8856                       ip->insn_opcode |= regno << OP_SH_FT;
8857                       break;
8858                     case 'R':
8859                       ip->insn_opcode |= regno << OP_SH_FR;
8860                       break;
8861                     }
8862                   lastregno = regno;
8863                   continue;
8864                 }
8865
8866               switch (*args++)
8867                 {
8868                 case 'V':
8869                   ip->insn_opcode |= lastregno << OP_SH_FS;
8870                   continue;
8871                 case 'W':
8872                   ip->insn_opcode |= lastregno << OP_SH_FT;
8873                   continue;
8874                 }
8875               break;
8876
8877             case 'I':
8878               my_getExpression (&imm_expr, s);
8879               if (imm_expr.X_op != O_big
8880                   && imm_expr.X_op != O_constant)
8881                 insn_error = _("absolute expression required");
8882               s = expr_end;
8883               continue;
8884
8885             case 'A':
8886               my_getExpression (&offset_expr, s);
8887               *imm_reloc = BFD_RELOC_32;
8888               s = expr_end;
8889               continue;
8890
8891             case 'F':
8892             case 'L':
8893             case 'f':
8894             case 'l':
8895               {
8896                 int f64;
8897                 int using_gprs;
8898                 char *save_in;
8899                 char *err;
8900                 unsigned char temp[8];
8901                 int len;
8902                 unsigned int length;
8903                 segT seg;
8904                 subsegT subseg;
8905                 char *p;
8906
8907                 /* These only appear as the last operand in an
8908                    instruction, and every instruction that accepts
8909                    them in any variant accepts them in all variants.
8910                    This means we don't have to worry about backing out
8911                    any changes if the instruction does not match.
8912
8913                    The difference between them is the size of the
8914                    floating point constant and where it goes.  For 'F'
8915                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8916                    is 32 bits.  Where the constant is placed is based
8917                    on how the MIPS assembler does things:
8918                     F -- .rdata
8919                     L -- .lit8
8920                     f -- immediate value
8921                     l -- .lit4
8922
8923                     The .lit4 and .lit8 sections are only used if
8924                     permitted by the -G argument.
8925
8926                     When generating embedded PIC code, we use the
8927                     .lit8 section but not the .lit4 section (we can do
8928                     .lit4 inline easily; we need to put .lit8
8929                     somewhere in the data segment, and using .lit8
8930                     permits the linker to eventually combine identical
8931                     .lit8 entries).
8932
8933                     The code below needs to know whether the target register
8934                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8935                     'F' are used with GPR-based instructions and 'l' and
8936                     'L' are used with FPR-based instructions.  */
8937
8938                 f64 = *args == 'F' || *args == 'L';
8939                 using_gprs = *args == 'F' || *args == 'f';
8940
8941                 save_in = input_line_pointer;
8942                 input_line_pointer = s;
8943                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8944                 length = len;
8945                 s = input_line_pointer;
8946                 input_line_pointer = save_in;
8947                 if (err != NULL && *err != '\0')
8948                   {
8949                     as_bad (_("Bad floating point constant: %s"), err);
8950                     memset (temp, '\0', sizeof temp);
8951                     length = f64 ? 8 : 4;
8952                   }
8953
8954                 assert (length == (unsigned) (f64 ? 8 : 4));
8955
8956                 if (*args == 'f'
8957                     || (*args == 'l'
8958                         && (! USE_GLOBAL_POINTER_OPT
8959                             || mips_pic == EMBEDDED_PIC
8960                             || g_switch_value < 4
8961                             || (temp[0] == 0 && temp[1] == 0)
8962                             || (temp[2] == 0 && temp[3] == 0))))
8963                   {
8964                     imm_expr.X_op = O_constant;
8965                     if (! target_big_endian)
8966                       imm_expr.X_add_number = bfd_getl32 (temp);
8967                     else
8968                       imm_expr.X_add_number = bfd_getb32 (temp);
8969                   }
8970                 else if (length > 4
8971                          && ! mips_disable_float_construction
8972                          /* Constants can only be constructed in GPRs and
8973                             copied to FPRs if the GPRs are at least as wide
8974                             as the FPRs.  Force the constant into memory if
8975                             we are using 64-bit FPRs but the GPRs are only
8976                             32 bits wide.  */
8977                          && (using_gprs
8978                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8979                          && ((temp[0] == 0 && temp[1] == 0)
8980                              || (temp[2] == 0 && temp[3] == 0))
8981                          && ((temp[4] == 0 && temp[5] == 0)
8982                              || (temp[6] == 0 && temp[7] == 0)))
8983                   {
8984                     /* The value is simple enough to load with a couple of
8985                        instructions.  If using 32-bit registers, set
8986                        imm_expr to the high order 32 bits and offset_expr to
8987                        the low order 32 bits.  Otherwise, set imm_expr to
8988                        the entire 64 bit constant.  */
8989                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8990                       {
8991                         imm_expr.X_op = O_constant;
8992                         offset_expr.X_op = O_constant;
8993                         if (! target_big_endian)
8994                           {
8995                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
8996                             offset_expr.X_add_number = bfd_getl32 (temp);
8997                           }
8998                         else
8999                           {
9000                             imm_expr.X_add_number = bfd_getb32 (temp);
9001                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9002                           }
9003                         if (offset_expr.X_add_number == 0)
9004                           offset_expr.X_op = O_absent;
9005                       }
9006                     else if (sizeof (imm_expr.X_add_number) > 4)
9007                       {
9008                         imm_expr.X_op = O_constant;
9009                         if (! target_big_endian)
9010                           imm_expr.X_add_number = bfd_getl64 (temp);
9011                         else
9012                           imm_expr.X_add_number = bfd_getb64 (temp);
9013                       }
9014                     else
9015                       {
9016                         imm_expr.X_op = O_big;
9017                         imm_expr.X_add_number = 4;
9018                         if (! target_big_endian)
9019                           {
9020                             generic_bignum[0] = bfd_getl16 (temp);
9021                             generic_bignum[1] = bfd_getl16 (temp + 2);
9022                             generic_bignum[2] = bfd_getl16 (temp + 4);
9023                             generic_bignum[3] = bfd_getl16 (temp + 6);
9024                           }
9025                         else
9026                           {
9027                             generic_bignum[0] = bfd_getb16 (temp + 6);
9028                             generic_bignum[1] = bfd_getb16 (temp + 4);
9029                             generic_bignum[2] = bfd_getb16 (temp + 2);
9030                             generic_bignum[3] = bfd_getb16 (temp);
9031                           }
9032                       }
9033                   }
9034                 else
9035                   {
9036                     const char *newname;
9037                     segT new_seg;
9038
9039                     /* Switch to the right section.  */
9040                     seg = now_seg;
9041                     subseg = now_subseg;
9042                     switch (*args)
9043                       {
9044                       default: /* unused default case avoids warnings.  */
9045                       case 'L':
9046                         newname = RDATA_SECTION_NAME;
9047                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9048                             || mips_pic == EMBEDDED_PIC)
9049                           newname = ".lit8";
9050                         break;
9051                       case 'F':
9052                         if (mips_pic == EMBEDDED_PIC)
9053                           newname = ".lit8";
9054                         else
9055                           newname = RDATA_SECTION_NAME;
9056                         break;
9057                       case 'l':
9058                         assert (!USE_GLOBAL_POINTER_OPT
9059                                 || g_switch_value >= 4);
9060                         newname = ".lit4";
9061                         break;
9062                       }
9063                     new_seg = subseg_new (newname, (subsegT) 0);
9064                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9065                       bfd_set_section_flags (stdoutput, new_seg,
9066                                              (SEC_ALLOC
9067                                               | SEC_LOAD
9068                                               | SEC_READONLY
9069                                               | SEC_DATA));
9070                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9071                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9072                         && strcmp (TARGET_OS, "elf") != 0)
9073                       record_alignment (new_seg, 4);
9074                     else
9075                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9076                     if (seg == now_seg)
9077                       as_bad (_("Can't use floating point insn in this section"));
9078
9079                     /* Set the argument to the current address in the
9080                        section.  */
9081                     offset_expr.X_op = O_symbol;
9082                     offset_expr.X_add_symbol =
9083                       symbol_new ("L0\001", now_seg,
9084                                   (valueT) frag_now_fix (), frag_now);
9085                     offset_expr.X_add_number = 0;
9086
9087                     /* Put the floating point number into the section.  */
9088                     p = frag_more ((int) length);
9089                     memcpy (p, temp, length);
9090
9091                     /* Switch back to the original section.  */
9092                     subseg_set (seg, subseg);
9093                   }
9094               }
9095               continue;
9096
9097             case 'i':           /* 16 bit unsigned immediate */
9098             case 'j':           /* 16 bit signed immediate */
9099               *imm_reloc = BFD_RELOC_LO16;
9100               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9101                 {
9102                   int more;
9103                   offsetT minval, maxval;
9104
9105                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9106                           && strcmp (insn->name, insn[1].name) == 0);
9107
9108                   /* If the expression was written as an unsigned number,
9109                      only treat it as signed if there are no more
9110                      alternatives.  */
9111                   if (more
9112                       && *args == 'j'
9113                       && sizeof (imm_expr.X_add_number) <= 4
9114                       && imm_expr.X_op == O_constant
9115                       && imm_expr.X_add_number < 0
9116                       && imm_expr.X_unsigned
9117                       && HAVE_64BIT_GPRS)
9118                     break;
9119
9120                   /* For compatibility with older assemblers, we accept
9121                      0x8000-0xffff as signed 16-bit numbers when only
9122                      signed numbers are allowed.  */
9123                   if (*args == 'i')
9124                     minval = 0, maxval = 0xffff;
9125                   else if (more)
9126                     minval = -0x8000, maxval = 0x7fff;
9127                   else
9128                     minval = -0x8000, maxval = 0xffff;
9129
9130                   if (imm_expr.X_op != O_constant
9131                       || imm_expr.X_add_number < minval
9132                       || imm_expr.X_add_number > maxval)
9133                     {
9134                       if (more)
9135                         break;
9136                       if (imm_expr.X_op == O_constant
9137                           || imm_expr.X_op == O_big)
9138                         as_bad (_("expression out of range"));
9139                     }
9140                 }
9141               s = expr_end;
9142               continue;
9143
9144             case 'o':           /* 16 bit offset */
9145               /* Check whether there is only a single bracketed expression
9146                  left.  If so, it must be the base register and the
9147                  constant must be zero.  */
9148               if (*s == '(' && strchr (s + 1, '(') == 0)
9149                 {
9150                   offset_expr.X_op = O_constant;
9151                   offset_expr.X_add_number = 0;
9152                   continue;
9153                 }
9154
9155               /* If this value won't fit into a 16 bit offset, then go
9156                  find a macro that will generate the 32 bit offset
9157                  code pattern.  */
9158               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9159                   && (offset_expr.X_op != O_constant
9160                       || offset_expr.X_add_number >= 0x8000
9161                       || offset_expr.X_add_number < -0x8000))
9162                 break;
9163
9164               s = expr_end;
9165               continue;
9166
9167             case 'p':           /* pc relative offset */
9168               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9169               my_getExpression (&offset_expr, s);
9170               s = expr_end;
9171               continue;
9172
9173             case 'u':           /* upper 16 bits */
9174               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9175                   && imm_expr.X_op == O_constant
9176                   && (imm_expr.X_add_number < 0
9177                       || imm_expr.X_add_number >= 0x10000))
9178                 as_bad (_("lui expression not in range 0..65535"));
9179               s = expr_end;
9180               continue;
9181
9182             case 'a':           /* 26 bit address */
9183               my_getExpression (&offset_expr, s);
9184               s = expr_end;
9185               *offset_reloc = BFD_RELOC_MIPS_JMP;
9186               continue;
9187
9188             case 'N':           /* 3 bit branch condition code */
9189             case 'M':           /* 3 bit compare condition code */
9190               if (strncmp (s, "$fcc", 4) != 0)
9191                 break;
9192               s += 4;
9193               regno = 0;
9194               do
9195                 {
9196                   regno *= 10;
9197                   regno += *s - '0';
9198                   ++s;
9199                 }
9200               while (ISDIGIT (*s));
9201               if (regno > 7)
9202                 as_bad (_("invalid condition code register $fcc%d"), regno);
9203               if (*args == 'N')
9204                 ip->insn_opcode |= regno << OP_SH_BCC;
9205               else
9206                 ip->insn_opcode |= regno << OP_SH_CCC;
9207               continue;
9208
9209             case 'H':
9210               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9211                 s += 2;
9212               if (ISDIGIT (*s))
9213                 {
9214                   c = 0;
9215                   do
9216                     {
9217                       c *= 10;
9218                       c += *s - '0';
9219                       ++s;
9220                     }
9221                   while (ISDIGIT (*s));
9222                 }
9223               else
9224                 c = 8; /* Invalid sel value.  */
9225
9226               if (c > 7)
9227                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9228               ip->insn_opcode |= c;
9229               continue;
9230
9231             case 'e':
9232               /* Must be at least one digit.  */
9233               my_getExpression (&imm_expr, s);
9234               check_absolute_expr (ip, &imm_expr);
9235
9236               if ((unsigned long) imm_expr.X_add_number
9237                   > (unsigned long) OP_MASK_VECBYTE)
9238                 {
9239                   as_bad (_("bad byte vector index (%ld)"),
9240                            (long) imm_expr.X_add_number);
9241                   imm_expr.X_add_number = 0;
9242                 }
9243
9244               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9245               imm_expr.X_op = O_absent;
9246               s = expr_end;
9247               continue;
9248
9249             case '%':
9250               my_getExpression (&imm_expr, s);
9251               check_absolute_expr (ip, &imm_expr);
9252
9253               if ((unsigned long) imm_expr.X_add_number
9254                   > (unsigned long) OP_MASK_VECALIGN)
9255                 {
9256                   as_bad (_("bad byte vector index (%ld)"),
9257                            (long) imm_expr.X_add_number);
9258                   imm_expr.X_add_number = 0;
9259                 }
9260
9261               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9262               imm_expr.X_op = O_absent;
9263               s = expr_end;
9264               continue;
9265
9266             default:
9267               as_bad (_("bad char = '%c'\n"), *args);
9268               internalError ();
9269             }
9270           break;
9271         }
9272       /* Args don't match.  */
9273       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9274           !strcmp (insn->name, insn[1].name))
9275         {
9276           ++insn;
9277           s = argsStart;
9278           insn_error = _("illegal operands");
9279           continue;
9280         }
9281       if (save_c)
9282         *(--s) = save_c;
9283       insn_error = _("illegal operands");
9284       return;
9285     }
9286 }
9287
9288 /* This routine assembles an instruction into its binary format when
9289    assembling for the mips16.  As a side effect, it sets one of the
9290    global variables imm_reloc or offset_reloc to the type of
9291    relocation to do if one of the operands is an address expression.
9292    It also sets mips16_small and mips16_ext if the user explicitly
9293    requested a small or extended instruction.  */
9294
9295 static void
9296 mips16_ip (str, ip)
9297      char *str;
9298      struct mips_cl_insn *ip;
9299 {
9300   char *s;
9301   const char *args;
9302   struct mips_opcode *insn;
9303   char *argsstart;
9304   unsigned int regno;
9305   unsigned int lastregno = 0;
9306   char *s_reset;
9307
9308   insn_error = NULL;
9309
9310   mips16_small = FALSE;
9311   mips16_ext = FALSE;
9312
9313   for (s = str; ISLOWER (*s); ++s)
9314     ;
9315   switch (*s)
9316     {
9317     case '\0':
9318       break;
9319
9320     case ' ':
9321       *s++ = '\0';
9322       break;
9323
9324     case '.':
9325       if (s[1] == 't' && s[2] == ' ')
9326         {
9327           *s = '\0';
9328           mips16_small = TRUE;
9329           s += 3;
9330           break;
9331         }
9332       else if (s[1] == 'e' && s[2] == ' ')
9333         {
9334           *s = '\0';
9335           mips16_ext = TRUE;
9336           s += 3;
9337           break;
9338         }
9339       /* Fall through.  */
9340     default:
9341       insn_error = _("unknown opcode");
9342       return;
9343     }
9344
9345   if (mips_opts.noautoextend && ! mips16_ext)
9346     mips16_small = TRUE;
9347
9348   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9349     {
9350       insn_error = _("unrecognized opcode");
9351       return;
9352     }
9353
9354   argsstart = s;
9355   for (;;)
9356     {
9357       assert (strcmp (insn->name, str) == 0);
9358
9359       ip->insn_mo = insn;
9360       ip->insn_opcode = insn->match;
9361       ip->use_extend = FALSE;
9362       imm_expr.X_op = O_absent;
9363       imm_reloc[0] = BFD_RELOC_UNUSED;
9364       imm_reloc[1] = BFD_RELOC_UNUSED;
9365       imm_reloc[2] = BFD_RELOC_UNUSED;
9366       offset_expr.X_op = O_absent;
9367       offset_reloc[0] = BFD_RELOC_UNUSED;
9368       offset_reloc[1] = BFD_RELOC_UNUSED;
9369       offset_reloc[2] = BFD_RELOC_UNUSED;
9370       for (args = insn->args; 1; ++args)
9371         {
9372           int c;
9373
9374           if (*s == ' ')
9375             ++s;
9376
9377           /* In this switch statement we call break if we did not find
9378              a match, continue if we did find a match, or return if we
9379              are done.  */
9380
9381           c = *args;
9382           switch (c)
9383             {
9384             case '\0':
9385               if (*s == '\0')
9386                 {
9387                   /* Stuff the immediate value in now, if we can.  */
9388                   if (imm_expr.X_op == O_constant
9389                       && *imm_reloc > BFD_RELOC_UNUSED
9390                       && insn->pinfo != INSN_MACRO)
9391                     {
9392                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9393                                     imm_expr.X_add_number, TRUE, mips16_small,
9394                                     mips16_ext, &ip->insn_opcode,
9395                                     &ip->use_extend, &ip->extend);
9396                       imm_expr.X_op = O_absent;
9397                       *imm_reloc = BFD_RELOC_UNUSED;
9398                     }
9399
9400                   return;
9401                 }
9402               break;
9403
9404             case ',':
9405               if (*s++ == c)
9406                 continue;
9407               s--;
9408               switch (*++args)
9409                 {
9410                 case 'v':
9411                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9412                   continue;
9413                 case 'w':
9414                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9415                   continue;
9416                 }
9417               break;
9418
9419             case '(':
9420             case ')':
9421               if (*s++ == c)
9422                 continue;
9423               break;
9424
9425             case 'v':
9426             case 'w':
9427               if (s[0] != '$')
9428                 {
9429                   if (c == 'v')
9430                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9431                   else
9432                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9433                   ++args;
9434                   continue;
9435                 }
9436               /* Fall through.  */
9437             case 'x':
9438             case 'y':
9439             case 'z':
9440             case 'Z':
9441             case '0':
9442             case 'S':
9443             case 'R':
9444             case 'X':
9445             case 'Y':
9446               if (s[0] != '$')
9447                 break;
9448               s_reset = s;
9449               if (ISDIGIT (s[1]))
9450                 {
9451                   ++s;
9452                   regno = 0;
9453                   do
9454                     {
9455                       regno *= 10;
9456                       regno += *s - '0';
9457                       ++s;
9458                     }
9459                   while (ISDIGIT (*s));
9460                   if (regno > 31)
9461                     {
9462                       as_bad (_("invalid register number (%d)"), regno);
9463                       regno = 2;
9464                     }
9465                 }
9466               else
9467                 {
9468                   if (s[1] == 'r' && s[2] == 'a')
9469                     {
9470                       s += 3;
9471                       regno = RA;
9472                     }
9473                   else if (s[1] == 'f' && s[2] == 'p')
9474                     {
9475                       s += 3;
9476                       regno = FP;
9477                     }
9478                   else if (s[1] == 's' && s[2] == 'p')
9479                     {
9480                       s += 3;
9481                       regno = SP;
9482                     }
9483                   else if (s[1] == 'g' && s[2] == 'p')
9484                     {
9485                       s += 3;
9486                       regno = GP;
9487                     }
9488                   else if (s[1] == 'a' && s[2] == 't')
9489                     {
9490                       s += 3;
9491                       regno = AT;
9492                     }
9493                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9494                     {
9495                       s += 4;
9496                       regno = KT0;
9497                     }
9498                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9499                     {
9500                       s += 4;
9501                       regno = KT1;
9502                     }
9503                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9504                     {
9505                       s += 5;
9506                       regno = ZERO;
9507                     }
9508                   else
9509                     break;
9510                 }
9511
9512               if (*s == ' ')
9513                 ++s;
9514               if (args[1] != *s)
9515                 {
9516                   if (c == 'v' || c == 'w')
9517                     {
9518                       regno = mips16_to_32_reg_map[lastregno];
9519                       s = s_reset;
9520                       ++args;
9521                     }
9522                 }
9523
9524               switch (c)
9525                 {
9526                 case 'x':
9527                 case 'y':
9528                 case 'z':
9529                 case 'v':
9530                 case 'w':
9531                 case 'Z':
9532                   regno = mips32_to_16_reg_map[regno];
9533                   break;
9534
9535                 case '0':
9536                   if (regno != 0)
9537                     regno = ILLEGAL_REG;
9538                   break;
9539
9540                 case 'S':
9541                   if (regno != SP)
9542                     regno = ILLEGAL_REG;
9543                   break;
9544
9545                 case 'R':
9546                   if (regno != RA)
9547                     regno = ILLEGAL_REG;
9548                   break;
9549
9550                 case 'X':
9551                 case 'Y':
9552                   if (regno == AT && ! mips_opts.noat)
9553                     as_warn (_("used $at without \".set noat\""));
9554                   break;
9555
9556                 default:
9557                   internalError ();
9558                 }
9559
9560               if (regno == ILLEGAL_REG)
9561                 break;
9562
9563               switch (c)
9564                 {
9565                 case 'x':
9566                 case 'v':
9567                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9568                   break;
9569                 case 'y':
9570                 case 'w':
9571                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9572                   break;
9573                 case 'z':
9574                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9575                   break;
9576                 case 'Z':
9577                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9578                 case '0':
9579                 case 'S':
9580                 case 'R':
9581                   break;
9582                 case 'X':
9583                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9584                   break;
9585                 case 'Y':
9586                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9587                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9588                   break;
9589                 default:
9590                   internalError ();
9591                 }
9592
9593               lastregno = regno;
9594               continue;
9595
9596             case 'P':
9597               if (strncmp (s, "$pc", 3) == 0)
9598                 {
9599                   s += 3;
9600                   continue;
9601                 }
9602               break;
9603
9604             case '<':
9605             case '>':
9606             case '[':
9607             case ']':
9608             case '4':
9609             case '5':
9610             case 'H':
9611             case 'W':
9612             case 'D':
9613             case 'j':
9614             case '8':
9615             case 'V':
9616             case 'C':
9617             case 'U':
9618             case 'k':
9619             case 'K':
9620               if (s[0] == '%'
9621                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9622                 {
9623                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9624                      and generate the appropriate reloc.  If the text
9625                      inside %gprel is not a symbol name with an
9626                      optional offset, then we generate a normal reloc
9627                      and will probably fail later.  */
9628                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9629                   if (imm_expr.X_op == O_symbol)
9630                     {
9631                       mips16_ext = TRUE;
9632                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9633                       s = expr_end;
9634                       ip->use_extend = TRUE;
9635                       ip->extend = 0;
9636                       continue;
9637                     }
9638                 }
9639               else
9640                 {
9641                   /* Just pick up a normal expression.  */
9642                   my_getExpression (&imm_expr, s);
9643                 }
9644
9645               if (imm_expr.X_op == O_register)
9646                 {
9647                   /* What we thought was an expression turned out to
9648                      be a register.  */
9649
9650                   if (s[0] == '(' && args[1] == '(')
9651                     {
9652                       /* It looks like the expression was omitted
9653                          before a register indirection, which means
9654                          that the expression is implicitly zero.  We
9655                          still set up imm_expr, so that we handle
9656                          explicit extensions correctly.  */
9657                       imm_expr.X_op = O_constant;
9658                       imm_expr.X_add_number = 0;
9659                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9660                       continue;
9661                     }
9662
9663                   break;
9664                 }
9665
9666               /* We need to relax this instruction.  */
9667               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9668               s = expr_end;
9669               continue;
9670
9671             case 'p':
9672             case 'q':
9673             case 'A':
9674             case 'B':
9675             case 'E':
9676               /* We use offset_reloc rather than imm_reloc for the PC
9677                  relative operands.  This lets macros with both
9678                  immediate and address operands work correctly.  */
9679               my_getExpression (&offset_expr, s);
9680
9681               if (offset_expr.X_op == O_register)
9682                 break;
9683
9684               /* We need to relax this instruction.  */
9685               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9686               s = expr_end;
9687               continue;
9688
9689             case '6':           /* break code */
9690               my_getExpression (&imm_expr, s);
9691               check_absolute_expr (ip, &imm_expr);
9692               if ((unsigned long) imm_expr.X_add_number > 63)
9693                 {
9694                   as_warn (_("Invalid value for `%s' (%lu)"),
9695                            ip->insn_mo->name,
9696                            (unsigned long) imm_expr.X_add_number);
9697                   imm_expr.X_add_number &= 0x3f;
9698                 }
9699               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9700               imm_expr.X_op = O_absent;
9701               s = expr_end;
9702               continue;
9703
9704             case 'a':           /* 26 bit address */
9705               my_getExpression (&offset_expr, s);
9706               s = expr_end;
9707               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9708               ip->insn_opcode <<= 16;
9709               continue;
9710
9711             case 'l':           /* register list for entry macro */
9712             case 'L':           /* register list for exit macro */
9713               {
9714                 int mask;
9715
9716                 if (c == 'l')
9717                   mask = 0;
9718                 else
9719                   mask = 7 << 3;
9720                 while (*s != '\0')
9721                   {
9722                     int freg, reg1, reg2;
9723
9724                     while (*s == ' ' || *s == ',')
9725                       ++s;
9726                     if (*s != '$')
9727                       {
9728                         as_bad (_("can't parse register list"));
9729                         break;
9730                       }
9731                     ++s;
9732                     if (*s != 'f')
9733                       freg = 0;
9734                     else
9735                       {
9736                         freg = 1;
9737                         ++s;
9738                       }
9739                     reg1 = 0;
9740                     while (ISDIGIT (*s))
9741                       {
9742                         reg1 *= 10;
9743                         reg1 += *s - '0';
9744                         ++s;
9745                       }
9746                     if (*s == ' ')
9747                       ++s;
9748                     if (*s != '-')
9749                       reg2 = reg1;
9750                     else
9751                       {
9752                         ++s;
9753                         if (*s != '$')
9754                           break;
9755                         ++s;
9756                         if (freg)
9757                           {
9758                             if (*s == 'f')
9759                               ++s;
9760                             else
9761                               {
9762                                 as_bad (_("invalid register list"));
9763                                 break;
9764                               }
9765                           }
9766                         reg2 = 0;
9767                         while (ISDIGIT (*s))
9768                           {
9769                             reg2 *= 10;
9770                             reg2 += *s - '0';
9771                             ++s;
9772                           }
9773                       }
9774                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9775                       {
9776                         mask &= ~ (7 << 3);
9777                         mask |= 5 << 3;
9778                       }
9779                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9780                       {
9781                         mask &= ~ (7 << 3);
9782                         mask |= 6 << 3;
9783                       }
9784                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9785                       mask |= (reg2 - 3) << 3;
9786                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9787                       mask |= (reg2 - 15) << 1;
9788                     else if (reg1 == RA && reg2 == RA)
9789                       mask |= 1;
9790                     else
9791                       {
9792                         as_bad (_("invalid register list"));
9793                         break;
9794                       }
9795                   }
9796                 /* The mask is filled in in the opcode table for the
9797                    benefit of the disassembler.  We remove it before
9798                    applying the actual mask.  */
9799                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9800                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9801               }
9802             continue;
9803
9804             case 'e':           /* extend code */
9805               my_getExpression (&imm_expr, s);
9806               check_absolute_expr (ip, &imm_expr);
9807               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9808                 {
9809                   as_warn (_("Invalid value for `%s' (%lu)"),
9810                            ip->insn_mo->name,
9811                            (unsigned long) imm_expr.X_add_number);
9812                   imm_expr.X_add_number &= 0x7ff;
9813                 }
9814               ip->insn_opcode |= imm_expr.X_add_number;
9815               imm_expr.X_op = O_absent;
9816               s = expr_end;
9817               continue;
9818
9819             default:
9820               internalError ();
9821             }
9822           break;
9823         }
9824
9825       /* Args don't match.  */
9826       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9827           strcmp (insn->name, insn[1].name) == 0)
9828         {
9829           ++insn;
9830           s = argsstart;
9831           continue;
9832         }
9833
9834       insn_error = _("illegal operands");
9835
9836       return;
9837     }
9838 }
9839
9840 /* This structure holds information we know about a mips16 immediate
9841    argument type.  */
9842
9843 struct mips16_immed_operand
9844 {
9845   /* The type code used in the argument string in the opcode table.  */
9846   int type;
9847   /* The number of bits in the short form of the opcode.  */
9848   int nbits;
9849   /* The number of bits in the extended form of the opcode.  */
9850   int extbits;
9851   /* The amount by which the short form is shifted when it is used;
9852      for example, the sw instruction has a shift count of 2.  */
9853   int shift;
9854   /* The amount by which the short form is shifted when it is stored
9855      into the instruction code.  */
9856   int op_shift;
9857   /* Non-zero if the short form is unsigned.  */
9858   int unsp;
9859   /* Non-zero if the extended form is unsigned.  */
9860   int extu;
9861   /* Non-zero if the value is PC relative.  */
9862   int pcrel;
9863 };
9864
9865 /* The mips16 immediate operand types.  */
9866
9867 static const struct mips16_immed_operand mips16_immed_operands[] =
9868 {
9869   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9870   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9871   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9872   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9873   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9874   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9875   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9876   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9877   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9878   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9879   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9880   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9881   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9882   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9883   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9884   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9885   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9886   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9887   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9888   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9889   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9890 };
9891
9892 #define MIPS16_NUM_IMMED \
9893   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9894
9895 /* Handle a mips16 instruction with an immediate value.  This or's the
9896    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9897    whether an extended value is needed; if one is needed, it sets
9898    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9899    If SMALL is true, an unextended opcode was explicitly requested.
9900    If EXT is true, an extended opcode was explicitly requested.  If
9901    WARN is true, warn if EXT does not match reality.  */
9902
9903 static void
9904 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9905               extend)
9906      char *file;
9907      unsigned int line;
9908      int type;
9909      offsetT val;
9910      bfd_boolean warn;
9911      bfd_boolean small;
9912      bfd_boolean ext;
9913      unsigned long *insn;
9914      bfd_boolean *use_extend;
9915      unsigned short *extend;
9916 {
9917   register const struct mips16_immed_operand *op;
9918   int mintiny, maxtiny;
9919   bfd_boolean needext;
9920
9921   op = mips16_immed_operands;
9922   while (op->type != type)
9923     {
9924       ++op;
9925       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9926     }
9927
9928   if (op->unsp)
9929     {
9930       if (type == '<' || type == '>' || type == '[' || type == ']')
9931         {
9932           mintiny = 1;
9933           maxtiny = 1 << op->nbits;
9934         }
9935       else
9936         {
9937           mintiny = 0;
9938           maxtiny = (1 << op->nbits) - 1;
9939         }
9940     }
9941   else
9942     {
9943       mintiny = - (1 << (op->nbits - 1));
9944       maxtiny = (1 << (op->nbits - 1)) - 1;
9945     }
9946
9947   /* Branch offsets have an implicit 0 in the lowest bit.  */
9948   if (type == 'p' || type == 'q')
9949     val /= 2;
9950
9951   if ((val & ((1 << op->shift) - 1)) != 0
9952       || val < (mintiny << op->shift)
9953       || val > (maxtiny << op->shift))
9954     needext = TRUE;
9955   else
9956     needext = FALSE;
9957
9958   if (warn && ext && ! needext)
9959     as_warn_where (file, line,
9960                    _("extended operand requested but not required"));
9961   if (small && needext)
9962     as_bad_where (file, line, _("invalid unextended operand value"));
9963
9964   if (small || (! ext && ! needext))
9965     {
9966       int insnval;
9967
9968       *use_extend = FALSE;
9969       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9970       insnval <<= op->op_shift;
9971       *insn |= insnval;
9972     }
9973   else
9974     {
9975       long minext, maxext;
9976       int extval;
9977
9978       if (op->extu)
9979         {
9980           minext = 0;
9981           maxext = (1 << op->extbits) - 1;
9982         }
9983       else
9984         {
9985           minext = - (1 << (op->extbits - 1));
9986           maxext = (1 << (op->extbits - 1)) - 1;
9987         }
9988       if (val < minext || val > maxext)
9989         as_bad_where (file, line,
9990                       _("operand value out of range for instruction"));
9991
9992       *use_extend = TRUE;
9993       if (op->extbits == 16)
9994         {
9995           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9996           val &= 0x1f;
9997         }
9998       else if (op->extbits == 15)
9999         {
10000           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10001           val &= 0xf;
10002         }
10003       else
10004         {
10005           extval = ((val & 0x1f) << 6) | (val & 0x20);
10006           val = 0;
10007         }
10008
10009       *extend = (unsigned short) extval;
10010       *insn |= val;
10011     }
10012 }
10013 \f
10014 static const struct percent_op_match
10015 {
10016   const char *str;
10017   bfd_reloc_code_real_type reloc;
10018 } percent_op[] =
10019 {
10020   {"%lo", BFD_RELOC_LO16},
10021 #ifdef OBJ_ELF
10022   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10023   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10024   {"%call16", BFD_RELOC_MIPS_CALL16},
10025   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10026   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10027   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10028   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10029   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10030   {"%got", BFD_RELOC_MIPS_GOT16},
10031   {"%gp_rel", BFD_RELOC_GPREL16},
10032   {"%half", BFD_RELOC_16},
10033   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10034   {"%higher", BFD_RELOC_MIPS_HIGHER},
10035   {"%neg", BFD_RELOC_MIPS_SUB},
10036 #endif
10037   {"%hi", BFD_RELOC_HI16_S}
10038 };
10039
10040
10041 /* Return true if *STR points to a relocation operator.  When returning true,
10042    move *STR over the operator and store its relocation code in *RELOC.
10043    Leave both *STR and *RELOC alone when returning false.  */
10044
10045 static bfd_boolean
10046 parse_relocation (str, reloc)
10047      char **str;
10048      bfd_reloc_code_real_type *reloc;
10049 {
10050   size_t i;
10051
10052   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10053     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10054       {
10055         *str += strlen (percent_op[i].str);
10056         *reloc = percent_op[i].reloc;
10057
10058         /* Check whether the output BFD supports this relocation.
10059            If not, issue an error and fall back on something safe.  */
10060         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10061           {
10062             as_bad ("relocation %s isn't supported by the current ABI",
10063                     percent_op[i].str);
10064             *reloc = BFD_RELOC_LO16;
10065           }
10066         return TRUE;
10067       }
10068   return FALSE;
10069 }
10070
10071
10072 /* Parse string STR as a 16-bit relocatable operand.  Store the
10073    expression in *EP and the relocations in the array starting
10074    at RELOC.  Return the number of relocation operators used.
10075
10076    On exit, EXPR_END points to the first character after the expression.
10077    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10078
10079 static size_t
10080 my_getSmallExpression (ep, reloc, str)
10081      expressionS *ep;
10082      bfd_reloc_code_real_type *reloc;
10083      char *str;
10084 {
10085   bfd_reloc_code_real_type reversed_reloc[3];
10086   size_t reloc_index, i;
10087   int crux_depth, str_depth;
10088   char *crux;
10089
10090   /* Search for the start of the main expression, recoding relocations
10091      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10092      of the main expression and with CRUX_DEPTH containing the number
10093      of open brackets at that point.  */
10094   reloc_index = -1;
10095   str_depth = 0;
10096   do
10097     {
10098       reloc_index++;
10099       crux = str;
10100       crux_depth = str_depth;
10101
10102       /* Skip over whitespace and brackets, keeping count of the number
10103          of brackets.  */
10104       while (*str == ' ' || *str == '\t' || *str == '(')
10105         if (*str++ == '(')
10106           str_depth++;
10107     }
10108   while (*str == '%'
10109          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10110          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10111
10112   my_getExpression (ep, crux);
10113   str = expr_end;
10114
10115   /* Match every open bracket.  */
10116   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10117     if (*str++ == ')')
10118       crux_depth--;
10119
10120   if (crux_depth > 0)
10121     as_bad ("unclosed '('");
10122
10123   expr_end = str;
10124
10125   if (reloc_index == 0)
10126     reloc[0] = BFD_RELOC_LO16;
10127   else
10128     {
10129       prev_reloc_op_frag = frag_now;
10130       for (i = 0; i < reloc_index; i++)
10131         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10132     }
10133
10134   return reloc_index;
10135 }
10136
10137 static void
10138 my_getExpression (ep, str)
10139      expressionS *ep;
10140      char *str;
10141 {
10142   char *save_in;
10143   valueT val;
10144
10145   save_in = input_line_pointer;
10146   input_line_pointer = str;
10147   expression (ep);
10148   expr_end = input_line_pointer;
10149   input_line_pointer = save_in;
10150
10151   /* If we are in mips16 mode, and this is an expression based on `.',
10152      then we bump the value of the symbol by 1 since that is how other
10153      text symbols are handled.  We don't bother to handle complex
10154      expressions, just `.' plus or minus a constant.  */
10155   if (mips_opts.mips16
10156       && ep->X_op == O_symbol
10157       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10158       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10159       && symbol_get_frag (ep->X_add_symbol) == frag_now
10160       && symbol_constant_p (ep->X_add_symbol)
10161       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10162     S_SET_VALUE (ep->X_add_symbol, val + 1);
10163 }
10164
10165 /* Turn a string in input_line_pointer into a floating point constant
10166    of type TYPE, and store the appropriate bytes in *LITP.  The number
10167    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10168    returned, or NULL on OK.  */
10169
10170 char *
10171 md_atof (type, litP, sizeP)
10172      int type;
10173      char *litP;
10174      int *sizeP;
10175 {
10176   int prec;
10177   LITTLENUM_TYPE words[4];
10178   char *t;
10179   int i;
10180
10181   switch (type)
10182     {
10183     case 'f':
10184       prec = 2;
10185       break;
10186
10187     case 'd':
10188       prec = 4;
10189       break;
10190
10191     default:
10192       *sizeP = 0;
10193       return _("bad call to md_atof");
10194     }
10195
10196   t = atof_ieee (input_line_pointer, type, words);
10197   if (t)
10198     input_line_pointer = t;
10199
10200   *sizeP = prec * 2;
10201
10202   if (! target_big_endian)
10203     {
10204       for (i = prec - 1; i >= 0; i--)
10205         {
10206           md_number_to_chars (litP, (valueT) words[i], 2);
10207           litP += 2;
10208         }
10209     }
10210   else
10211     {
10212       for (i = 0; i < prec; i++)
10213         {
10214           md_number_to_chars (litP, (valueT) words[i], 2);
10215           litP += 2;
10216         }
10217     }
10218
10219   return NULL;
10220 }
10221
10222 void
10223 md_number_to_chars (buf, val, n)
10224      char *buf;
10225      valueT val;
10226      int n;
10227 {
10228   if (target_big_endian)
10229     number_to_chars_bigendian (buf, val, n);
10230   else
10231     number_to_chars_littleendian (buf, val, n);
10232 }
10233 \f
10234 #ifdef OBJ_ELF
10235 static int support_64bit_objects(void)
10236 {
10237   const char **list, **l;
10238   int yes;
10239
10240   list = bfd_target_list ();
10241   for (l = list; *l != NULL; l++)
10242 #ifdef TE_TMIPS
10243     /* This is traditional mips */
10244     if (strcmp (*l, "elf64-tradbigmips") == 0
10245         || strcmp (*l, "elf64-tradlittlemips") == 0)
10246 #else
10247     if (strcmp (*l, "elf64-bigmips") == 0
10248         || strcmp (*l, "elf64-littlemips") == 0)
10249 #endif
10250       break;
10251   yes = (*l != NULL);
10252   free (list);
10253   return yes;
10254 }
10255 #endif /* OBJ_ELF */
10256
10257 const char *md_shortopts = "nO::g::G:";
10258
10259 struct option md_longopts[] =
10260 {
10261 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10262   {"mips0", no_argument, NULL, OPTION_MIPS1},
10263   {"mips1", no_argument, NULL, OPTION_MIPS1},
10264 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10265   {"mips2", no_argument, NULL, OPTION_MIPS2},
10266 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10267   {"mips3", no_argument, NULL, OPTION_MIPS3},
10268 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10269   {"mips4", no_argument, NULL, OPTION_MIPS4},
10270 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10271   {"mips5", no_argument, NULL, OPTION_MIPS5},
10272 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10273   {"mips32", no_argument, NULL, OPTION_MIPS32},
10274 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10275   {"mips64", no_argument, NULL, OPTION_MIPS64},
10276 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10277   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10278 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10279   {"trap", no_argument, NULL, OPTION_TRAP},
10280   {"no-break", no_argument, NULL, OPTION_TRAP},
10281 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10282   {"break", no_argument, NULL, OPTION_BREAK},
10283   {"no-trap", no_argument, NULL, OPTION_BREAK},
10284 #define OPTION_EB (OPTION_MD_BASE + 11)
10285   {"EB", no_argument, NULL, OPTION_EB},
10286 #define OPTION_EL (OPTION_MD_BASE + 12)
10287   {"EL", no_argument, NULL, OPTION_EL},
10288 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10289   {"mips16", no_argument, NULL, OPTION_MIPS16},
10290 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10291   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10292 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10293   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10294 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10295   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10296   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10297 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10298   {"mfp32", no_argument, NULL, OPTION_FP32},
10299 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10300   {"mgp32", no_argument, NULL, OPTION_GP32},
10301 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10302   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10303 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10304   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10305 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10306   {"march", required_argument, NULL, OPTION_MARCH},
10307 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10308   {"mtune", required_argument, NULL, OPTION_MTUNE},
10309 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10310   {"mfp64", no_argument, NULL, OPTION_FP64},
10311 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10312   {"m4650", no_argument, NULL, OPTION_M4650},
10313 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10314   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10315 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10316   {"m4010", no_argument, NULL, OPTION_M4010},
10317 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10318   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10319 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10320   {"m4100", no_argument, NULL, OPTION_M4100},
10321 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10322   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10323 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10324   {"m3900", no_argument, NULL, OPTION_M3900},
10325 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10326   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10327 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10328   {"mgp64", no_argument, NULL, OPTION_GP64},
10329 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10330   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10331 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10332   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10333 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10334   {"mdmx", no_argument, NULL, OPTION_MDMX},
10335 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10336   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10337 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10338 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10339   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10340   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10341 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10342 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10343   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10344   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10345 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10346   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10347 #ifdef OBJ_ELF
10348 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 42)
10349 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10350   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10351   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10352 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10353   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10354 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10355   {"xgot",        no_argument, NULL, OPTION_XGOT},
10356 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10357   {"mabi", required_argument, NULL, OPTION_MABI},
10358 #define OPTION_32          (OPTION_ELF_BASE + 4)
10359   {"32",          no_argument, NULL, OPTION_32},
10360 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10361   {"n32",         no_argument, NULL, OPTION_N32},
10362 #define OPTION_64          (OPTION_ELF_BASE + 6)
10363   {"64",          no_argument, NULL, OPTION_64},
10364 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10365   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10366 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10367   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10368 #endif /* OBJ_ELF */
10369   {NULL, no_argument, NULL, 0}
10370 };
10371 size_t md_longopts_size = sizeof (md_longopts);
10372
10373 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10374    NEW_VALUE.  Warn if another value was already specified.  Note:
10375    we have to defer parsing the -march and -mtune arguments in order
10376    to handle 'from-abi' correctly, since the ABI might be specified
10377    in a later argument.  */
10378
10379 static void
10380 mips_set_option_string (string_ptr, new_value)
10381      const char **string_ptr, *new_value;
10382 {
10383   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10384     as_warn (_("A different %s was already specified, is now %s"),
10385              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10386              new_value);
10387
10388   *string_ptr = new_value;
10389 }
10390
10391 int
10392 md_parse_option (c, arg)
10393      int c;
10394      char *arg;
10395 {
10396   switch (c)
10397     {
10398     case OPTION_CONSTRUCT_FLOATS:
10399       mips_disable_float_construction = 0;
10400       break;
10401
10402     case OPTION_NO_CONSTRUCT_FLOATS:
10403       mips_disable_float_construction = 1;
10404       break;
10405
10406     case OPTION_TRAP:
10407       mips_trap = 1;
10408       break;
10409
10410     case OPTION_BREAK:
10411       mips_trap = 0;
10412       break;
10413
10414     case OPTION_EB:
10415       target_big_endian = 1;
10416       break;
10417
10418     case OPTION_EL:
10419       target_big_endian = 0;
10420       break;
10421
10422     case 'n':
10423       warn_nops = 1;
10424       break;
10425
10426     case 'O':
10427       if (arg && arg[1] == '0')
10428         mips_optimize = 1;
10429       else
10430         mips_optimize = 2;
10431       break;
10432
10433     case 'g':
10434       if (arg == NULL)
10435         mips_debug = 2;
10436       else
10437         mips_debug = atoi (arg);
10438       /* When the MIPS assembler sees -g or -g2, it does not do
10439          optimizations which limit full symbolic debugging.  We take
10440          that to be equivalent to -O0.  */
10441       if (mips_debug == 2)
10442         mips_optimize = 1;
10443       break;
10444
10445     case OPTION_MIPS1:
10446       file_mips_isa = ISA_MIPS1;
10447       break;
10448
10449     case OPTION_MIPS2:
10450       file_mips_isa = ISA_MIPS2;
10451       break;
10452
10453     case OPTION_MIPS3:
10454       file_mips_isa = ISA_MIPS3;
10455       break;
10456
10457     case OPTION_MIPS4:
10458       file_mips_isa = ISA_MIPS4;
10459       break;
10460
10461     case OPTION_MIPS5:
10462       file_mips_isa = ISA_MIPS5;
10463       break;
10464
10465     case OPTION_MIPS32:
10466       file_mips_isa = ISA_MIPS32;
10467       break;
10468
10469     case OPTION_MIPS32R2:
10470       file_mips_isa = ISA_MIPS32R2;
10471       break;
10472
10473     case OPTION_MIPS64:
10474       file_mips_isa = ISA_MIPS64;
10475       break;
10476
10477     case OPTION_MTUNE:
10478       mips_set_option_string (&mips_tune_string, arg);
10479       break;
10480
10481     case OPTION_MARCH:
10482       mips_set_option_string (&mips_arch_string, arg);
10483       break;
10484
10485     case OPTION_M4650:
10486       mips_set_option_string (&mips_arch_string, "4650");
10487       mips_set_option_string (&mips_tune_string, "4650");
10488       break;
10489
10490     case OPTION_NO_M4650:
10491       break;
10492
10493     case OPTION_M4010:
10494       mips_set_option_string (&mips_arch_string, "4010");
10495       mips_set_option_string (&mips_tune_string, "4010");
10496       break;
10497
10498     case OPTION_NO_M4010:
10499       break;
10500
10501     case OPTION_M4100:
10502       mips_set_option_string (&mips_arch_string, "4100");
10503       mips_set_option_string (&mips_tune_string, "4100");
10504       break;
10505
10506     case OPTION_NO_M4100:
10507       break;
10508
10509     case OPTION_M3900:
10510       mips_set_option_string (&mips_arch_string, "3900");
10511       mips_set_option_string (&mips_tune_string, "3900");
10512       break;
10513
10514     case OPTION_NO_M3900:
10515       break;
10516
10517     case OPTION_MDMX:
10518       mips_opts.ase_mdmx = 1;
10519       break;
10520
10521     case OPTION_NO_MDMX:
10522       mips_opts.ase_mdmx = 0;
10523       break;
10524
10525     case OPTION_MIPS16:
10526       mips_opts.mips16 = 1;
10527       mips_no_prev_insn (FALSE);
10528       break;
10529
10530     case OPTION_NO_MIPS16:
10531       mips_opts.mips16 = 0;
10532       mips_no_prev_insn (FALSE);
10533       break;
10534
10535     case OPTION_MIPS3D:
10536       mips_opts.ase_mips3d = 1;
10537       break;
10538
10539     case OPTION_NO_MIPS3D:
10540       mips_opts.ase_mips3d = 0;
10541       break;
10542
10543     case OPTION_MEMBEDDED_PIC:
10544       mips_pic = EMBEDDED_PIC;
10545       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10546         {
10547           as_bad (_("-G may not be used with embedded PIC code"));
10548           return 0;
10549         }
10550       g_switch_value = 0x7fffffff;
10551       break;
10552
10553     case OPTION_FIX_VR4122:
10554       mips_fix_4122_bugs = 1;
10555       break;
10556
10557     case OPTION_NO_FIX_VR4122:
10558       mips_fix_4122_bugs = 0;
10559       break;
10560
10561     case OPTION_RELAX_BRANCH:
10562       mips_relax_branch = 1;
10563       break;
10564
10565     case OPTION_NO_RELAX_BRANCH:
10566       mips_relax_branch = 0;
10567       break;
10568
10569 #ifdef OBJ_ELF
10570       /* When generating ELF code, we permit -KPIC and -call_shared to
10571          select SVR4_PIC, and -non_shared to select no PIC.  This is
10572          intended to be compatible with Irix 5.  */
10573     case OPTION_CALL_SHARED:
10574       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10575         {
10576           as_bad (_("-call_shared is supported only for ELF format"));
10577           return 0;
10578         }
10579       mips_pic = SVR4_PIC;
10580       if (g_switch_seen && g_switch_value != 0)
10581         {
10582           as_bad (_("-G may not be used with SVR4 PIC code"));
10583           return 0;
10584         }
10585       g_switch_value = 0;
10586       break;
10587
10588     case OPTION_NON_SHARED:
10589       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10590         {
10591           as_bad (_("-non_shared is supported only for ELF format"));
10592           return 0;
10593         }
10594       mips_pic = NO_PIC;
10595       break;
10596
10597       /* The -xgot option tells the assembler to use 32 offsets when
10598          accessing the got in SVR4_PIC mode.  It is for Irix
10599          compatibility.  */
10600     case OPTION_XGOT:
10601       mips_big_got = 1;
10602       break;
10603 #endif /* OBJ_ELF */
10604
10605     case 'G':
10606       if (! USE_GLOBAL_POINTER_OPT)
10607         {
10608           as_bad (_("-G is not supported for this configuration"));
10609           return 0;
10610         }
10611       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10612         {
10613           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10614           return 0;
10615         }
10616       else
10617         g_switch_value = atoi (arg);
10618       g_switch_seen = 1;
10619       break;
10620
10621 #ifdef OBJ_ELF
10622       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10623          and -mabi=64.  */
10624     case OPTION_32:
10625       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10626         {
10627           as_bad (_("-32 is supported for ELF format only"));
10628           return 0;
10629         }
10630       mips_abi = O32_ABI;
10631       break;
10632
10633     case OPTION_N32:
10634       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10635         {
10636           as_bad (_("-n32 is supported for ELF format only"));
10637           return 0;
10638         }
10639       mips_abi = N32_ABI;
10640       break;
10641
10642     case OPTION_64:
10643       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10644         {
10645           as_bad (_("-64 is supported for ELF format only"));
10646           return 0;
10647         }
10648       mips_abi = N64_ABI;
10649       if (! support_64bit_objects())
10650         as_fatal (_("No compiled in support for 64 bit object file format"));
10651       break;
10652 #endif /* OBJ_ELF */
10653
10654     case OPTION_GP32:
10655       file_mips_gp32 = 1;
10656       break;
10657
10658     case OPTION_GP64:
10659       file_mips_gp32 = 0;
10660       break;
10661
10662     case OPTION_FP32:
10663       file_mips_fp32 = 1;
10664       break;
10665
10666     case OPTION_FP64:
10667       file_mips_fp32 = 0;
10668       break;
10669
10670 #ifdef OBJ_ELF
10671     case OPTION_MABI:
10672       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10673         {
10674           as_bad (_("-mabi is supported for ELF format only"));
10675           return 0;
10676         }
10677       if (strcmp (arg, "32") == 0)
10678         mips_abi = O32_ABI;
10679       else if (strcmp (arg, "o64") == 0)
10680         mips_abi = O64_ABI;
10681       else if (strcmp (arg, "n32") == 0)
10682         mips_abi = N32_ABI;
10683       else if (strcmp (arg, "64") == 0)
10684         {
10685           mips_abi = N64_ABI;
10686           if (! support_64bit_objects())
10687             as_fatal (_("No compiled in support for 64 bit object file "
10688                         "format"));
10689         }
10690       else if (strcmp (arg, "eabi") == 0)
10691         mips_abi = EABI_ABI;
10692       else
10693         {
10694           as_fatal (_("invalid abi -mabi=%s"), arg);
10695           return 0;
10696         }
10697       break;
10698 #endif /* OBJ_ELF */
10699
10700     case OPTION_M7000_HILO_FIX:
10701       mips_7000_hilo_fix = TRUE;
10702       break;
10703
10704     case OPTION_MNO_7000_HILO_FIX:
10705       mips_7000_hilo_fix = FALSE;
10706       break;
10707
10708 #ifdef OBJ_ELF
10709     case OPTION_MDEBUG:
10710       mips_flag_mdebug = TRUE;
10711       break;
10712
10713     case OPTION_NO_MDEBUG:
10714       mips_flag_mdebug = FALSE;
10715       break;
10716 #endif /* OBJ_ELF */
10717
10718     default:
10719       return 0;
10720     }
10721
10722   return 1;
10723 }
10724 \f
10725 /* Set up globals to generate code for the ISA or processor
10726    described by INFO.  */
10727
10728 static void
10729 mips_set_architecture (info)
10730      const struct mips_cpu_info *info;
10731 {
10732   if (info != 0)
10733     {
10734       mips_arch_info = info;
10735       mips_arch = info->cpu;
10736       mips_opts.isa = info->isa;
10737     }
10738 }
10739
10740
10741 /* Likewise for tuning.  */
10742
10743 static void
10744 mips_set_tune (info)
10745      const struct mips_cpu_info *info;
10746 {
10747   if (info != 0)
10748     {
10749       mips_tune_info = info;
10750       mips_tune = info->cpu;
10751     }
10752 }
10753
10754
10755 void
10756 mips_after_parse_args ()
10757 {
10758   /* GP relative stuff not working for PE */
10759   if (strncmp (TARGET_OS, "pe", 2) == 0
10760       && g_switch_value != 0)
10761     {
10762       if (g_switch_seen)
10763         as_bad (_("-G not supported in this configuration."));
10764       g_switch_value = 0;
10765     }
10766
10767   /* The following code determines the architecture and register size.
10768      Similar code was added to GCC 3.3 (see override_options() in
10769      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10770      as much as possible.  */
10771
10772   if (mips_arch_string != 0)
10773     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10774
10775   if (mips_tune_string != 0)
10776     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10777
10778   if (file_mips_isa != ISA_UNKNOWN)
10779     {
10780       /* Handle -mipsN.  At this point, file_mips_isa contains the
10781          ISA level specified by -mipsN, while mips_opts.isa contains
10782          the -march selection (if any).  */
10783       if (mips_arch_info != 0)
10784         {
10785           /* -march takes precedence over -mipsN, since it is more descriptive.
10786              There's no harm in specifying both as long as the ISA levels
10787              are the same.  */
10788           if (file_mips_isa != mips_opts.isa)
10789             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10790                     mips_cpu_info_from_isa (file_mips_isa)->name,
10791                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10792         }
10793       else
10794         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10795     }
10796
10797   if (mips_arch_info == 0)
10798     mips_set_architecture (mips_parse_cpu ("default CPU",
10799                                            MIPS_CPU_STRING_DEFAULT));
10800
10801   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10802     as_bad ("-march=%s is not compatible with the selected ABI",
10803             mips_arch_info->name);
10804
10805   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10806   if (mips_tune_info == 0)
10807     mips_set_tune (mips_arch_info);
10808
10809   if (file_mips_gp32 >= 0)
10810     {
10811       /* The user specified the size of the integer registers.  Make sure
10812          it agrees with the ABI and ISA.  */
10813       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10814         as_bad (_("-mgp64 used with a 32-bit processor"));
10815       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10816         as_bad (_("-mgp32 used with a 64-bit ABI"));
10817       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10818         as_bad (_("-mgp64 used with a 32-bit ABI"));
10819     }
10820   else
10821     {
10822       /* Infer the integer register size from the ABI and processor.
10823          Restrict ourselves to 32-bit registers if that's all the
10824          processor has, or if the ABI cannot handle 64-bit registers.  */
10825       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10826                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10827     }
10828
10829   /* ??? GAS treats single-float processors as though they had 64-bit
10830      float registers (although it complains when double-precision
10831      instructions are used).  As things stand, saying they have 32-bit
10832      registers would lead to spurious "register must be even" messages.
10833      So here we assume float registers are always the same size as
10834      integer ones, unless the user says otherwise.  */
10835   if (file_mips_fp32 < 0)
10836     file_mips_fp32 = file_mips_gp32;
10837
10838   /* End of GCC-shared inference code.  */
10839
10840   /* ??? When do we want this flag to be set?   Who uses it?  */
10841   if (file_mips_gp32 == 1
10842       && mips_abi == NO_ABI
10843       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10844     mips_32bitmode = 1;
10845
10846   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10847     as_bad (_("trap exception not supported at ISA 1"));
10848
10849   /* If the selected architecture includes support for ASEs, enable
10850      generation of code for them.  */
10851   if (mips_opts.mips16 == -1)
10852     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10853   if (mips_opts.ase_mips3d == -1)
10854     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10855   if (mips_opts.ase_mdmx == -1)
10856     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10857
10858   file_mips_isa = mips_opts.isa;
10859   file_ase_mips16 = mips_opts.mips16;
10860   file_ase_mips3d = mips_opts.ase_mips3d;
10861   file_ase_mdmx = mips_opts.ase_mdmx;
10862   mips_opts.gp32 = file_mips_gp32;
10863   mips_opts.fp32 = file_mips_fp32;
10864
10865   if (mips_flag_mdebug < 0)
10866     {
10867 #ifdef OBJ_MAYBE_ECOFF
10868       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10869         mips_flag_mdebug = 1;
10870       else
10871 #endif /* OBJ_MAYBE_ECOFF */
10872         mips_flag_mdebug = 0;
10873     }
10874 }
10875 \f
10876 void
10877 mips_init_after_args ()
10878 {
10879   /* initialize opcodes */
10880   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10881   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10882 }
10883
10884 long
10885 md_pcrel_from (fixP)
10886      fixS *fixP;
10887 {
10888   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10889       && fixP->fx_addsy != (symbolS *) NULL
10890       && ! S_IS_DEFINED (fixP->fx_addsy))
10891     return 4;
10892
10893   /* Return the address of the delay slot.  */
10894   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10895 }
10896
10897 /* This is called before the symbol table is processed.  In order to
10898    work with gcc when using mips-tfile, we must keep all local labels.
10899    However, in other cases, we want to discard them.  If we were
10900    called with -g, but we didn't see any debugging information, it may
10901    mean that gcc is smuggling debugging information through to
10902    mips-tfile, in which case we must generate all local labels.  */
10903
10904 void
10905 mips_frob_file_before_adjust ()
10906 {
10907 #ifndef NO_ECOFF_DEBUGGING
10908   if (ECOFF_DEBUGGING
10909       && mips_debug != 0
10910       && ! ecoff_debugging_seen)
10911     flag_keep_locals = 1;
10912 #endif
10913 }
10914
10915 /* Sort any unmatched HI16_S relocs so that they immediately precede
10916    the corresponding LO reloc.  This is called before md_apply_fix3 and
10917    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10918    explicit use of the %hi modifier.  */
10919
10920 void
10921 mips_frob_file ()
10922 {
10923   struct mips_hi_fixup *l;
10924
10925   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10926     {
10927       segment_info_type *seginfo;
10928       int pass;
10929
10930       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10931
10932       /* If a GOT16 relocation turns out to be against a global symbol,
10933          there isn't supposed to be a matching LO.  */
10934       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10935           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10936         continue;
10937
10938       /* Check quickly whether the next fixup happens to be a matching %lo.  */
10939       if (fixup_has_matching_lo_p (l->fixp))
10940         continue;
10941
10942       /* Look through the fixups for this segment for a matching %lo.
10943          When we find one, move the %hi just in front of it.  We do
10944          this in two passes.  In the first pass, we try to find a
10945          unique %lo.  In the second pass, we permit multiple %hi
10946          relocs for a single %lo (this is a GNU extension).  */
10947       seginfo = seg_info (l->seg);
10948       for (pass = 0; pass < 2; pass++)
10949         {
10950           fixS *f, *prev;
10951
10952           prev = NULL;
10953           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10954             {
10955               /* Check whether this is a %lo fixup which matches l->fixp.  */
10956               if (f->fx_r_type == BFD_RELOC_LO16
10957                   && f->fx_addsy == l->fixp->fx_addsy
10958                   && f->fx_offset == l->fixp->fx_offset
10959                   && (pass == 1
10960                       || prev == NULL
10961                       || !reloc_needs_lo_p (prev->fx_r_type)
10962                       || !fixup_has_matching_lo_p (prev)))
10963                 {
10964                   fixS **pf;
10965
10966                   /* Move l->fixp before f.  */
10967                   for (pf = &seginfo->fix_root;
10968                        *pf != l->fixp;
10969                        pf = &(*pf)->fx_next)
10970                     assert (*pf != NULL);
10971
10972                   *pf = l->fixp->fx_next;
10973
10974                   l->fixp->fx_next = f;
10975                   if (prev == NULL)
10976                     seginfo->fix_root = l->fixp;
10977                   else
10978                     prev->fx_next = l->fixp;
10979
10980                   break;
10981                 }
10982
10983               prev = f;
10984             }
10985
10986           if (f != NULL)
10987             break;
10988
10989 #if 0 /* GCC code motion plus incomplete dead code elimination
10990          can leave a %hi without a %lo.  */
10991           if (pass == 1)
10992             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10993                            _("Unmatched %%hi reloc"));
10994 #endif
10995         }
10996     }
10997 }
10998
10999 /* When generating embedded PIC code we need to use a special
11000    relocation to represent the difference of two symbols in the .text
11001    section (switch tables use a difference of this sort).  See
11002    include/coff/mips.h for details.  This macro checks whether this
11003    fixup requires the special reloc.  */
11004 #define SWITCH_TABLE(fixp) \
11005   ((fixp)->fx_r_type == BFD_RELOC_32 \
11006    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11007    && (fixp)->fx_addsy != NULL \
11008    && (fixp)->fx_subsy != NULL \
11009    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11010    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11011
11012 /* When generating embedded PIC code we must keep all PC relative
11013    relocations, in case the linker has to relax a call.  We also need
11014    to keep relocations for switch table entries.
11015
11016    We may have combined relocations without symbols in the N32/N64 ABI.
11017    We have to prevent gas from dropping them.  */
11018
11019 int
11020 mips_force_relocation (fixp)
11021      fixS *fixp;
11022 {
11023   if (generic_force_reloc (fixp))
11024     return 1;
11025
11026   if (HAVE_NEWABI
11027       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11028       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11029           || fixp->fx_r_type == BFD_RELOC_HI16_S
11030           || fixp->fx_r_type == BFD_RELOC_LO16))
11031     return 1;
11032
11033   return (mips_pic == EMBEDDED_PIC
11034           && (fixp->fx_pcrel
11035               || SWITCH_TABLE (fixp)
11036               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11037               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11038 }
11039
11040 /* This hook is called before a fix is simplified.  We don't really
11041    decide whether to skip a fix here.  Rather, we turn global symbols
11042    used as branch targets into local symbols, such that they undergo
11043    simplification.  We can only do this if the symbol is defined and
11044    it is in the same section as the branch.  If this doesn't hold, we
11045    emit a better error message than just saying the relocation is not
11046    valid for the selected object format.
11047
11048    FIXP is the fix-up we're going to try to simplify, SEG is the
11049    segment in which the fix up occurs.  The return value should be
11050    non-zero to indicate the fix-up is valid for further
11051    simplifications.  */
11052
11053 int
11054 mips_validate_fix (fixP, seg)
11055      struct fix *fixP;
11056      asection *seg;
11057 {
11058   /* There's a lot of discussion on whether it should be possible to
11059      use R_MIPS_PC16 to represent branch relocations.  The outcome
11060      seems to be that it can, but gas/bfd are very broken in creating
11061      RELA relocations for this, so for now we only accept branches to
11062      symbols in the same section.  Anything else is of dubious value,
11063      since there's no guarantee that at link time the symbol would be
11064      in range.  Even for branches to local symbols this is arguably
11065      wrong, since it we assume the symbol is not going to be
11066      overridden, which should be possible per ELF library semantics,
11067      but then, there isn't a dynamic relocation that could be used to
11068      this effect, and the target would likely be out of range as well.
11069
11070      Unfortunately, it seems that there is too much code out there
11071      that relies on branches to symbols that are global to be resolved
11072      as if they were local, like the IRIX tools do, so we do it as
11073      well, but with a warning so that people are reminded to fix their
11074      code.  If we ever get back to using R_MIPS_PC16 for branch
11075      targets, this entire block should go away (and probably the
11076      whole function).  */
11077
11078   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11079       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11080             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11081            && mips_pic != EMBEDDED_PIC)
11082           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11083       && fixP->fx_addsy)
11084     {
11085       if (! S_IS_DEFINED (fixP->fx_addsy))
11086         {
11087           as_bad_where (fixP->fx_file, fixP->fx_line,
11088                         _("Cannot branch to undefined symbol."));
11089           /* Avoid any further errors about this fixup.  */
11090           fixP->fx_done = 1;
11091         }
11092       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11093         {
11094           as_bad_where (fixP->fx_file, fixP->fx_line,
11095                         _("Cannot branch to symbol in another section."));
11096           fixP->fx_done = 1;
11097         }
11098       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11099         {
11100           symbolS *sym = fixP->fx_addsy;
11101
11102           as_warn_where (fixP->fx_file, fixP->fx_line,
11103                          _("Pretending global symbol used as branch target is local."));
11104
11105           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11106                                           S_GET_SEGMENT (sym),
11107                                           S_GET_VALUE (sym),
11108                                           symbol_get_frag (sym));
11109           copy_symbol_attributes (fixP->fx_addsy, sym);
11110           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11111           assert (symbol_resolved_p (sym));
11112           symbol_mark_resolved (fixP->fx_addsy);
11113         }
11114     }
11115
11116   return 1;
11117 }
11118
11119 #ifdef OBJ_ELF
11120 static int
11121 mips_need_elf_addend_fixup (fixP)
11122      fixS *fixP;
11123 {
11124   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11125     return 1;
11126   if (mips_pic == EMBEDDED_PIC
11127       && S_IS_WEAK (fixP->fx_addsy))
11128     return 1;
11129   if (mips_pic != EMBEDDED_PIC
11130       && (S_IS_WEAK (fixP->fx_addsy)
11131           || S_IS_EXTERNAL (fixP->fx_addsy))
11132       && !S_IS_COMMON (fixP->fx_addsy))
11133     return 1;
11134   if (symbol_used_in_reloc_p (fixP->fx_addsy)
11135       && (((bfd_get_section_flags (stdoutput,
11136                                    S_GET_SEGMENT (fixP->fx_addsy))
11137             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11138           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11139                        ".gnu.linkonce",
11140                        sizeof (".gnu.linkonce") - 1)))
11141     return 1;
11142   return 0;
11143 }
11144 #endif
11145
11146 /* Apply a fixup to the object file.  */
11147
11148 void
11149 md_apply_fix3 (fixP, valP, seg)
11150      fixS *fixP;
11151      valueT *valP;
11152      segT seg ATTRIBUTE_UNUSED;
11153 {
11154   bfd_byte *buf;
11155   long insn;
11156   valueT value;
11157   static int previous_fx_r_type = 0;
11158
11159   /* FIXME: Maybe just return for all reloc types not listed below?
11160      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11161   if (fixP->fx_r_type == BFD_RELOC_8)
11162       return;
11163
11164   assert (fixP->fx_size == 4
11165           || fixP->fx_r_type == BFD_RELOC_16
11166           || fixP->fx_r_type == BFD_RELOC_32
11167           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11168           || fixP->fx_r_type == BFD_RELOC_HI16_S
11169           || fixP->fx_r_type == BFD_RELOC_LO16
11170           || fixP->fx_r_type == BFD_RELOC_GPREL16
11171           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11172           || fixP->fx_r_type == BFD_RELOC_GPREL32
11173           || fixP->fx_r_type == BFD_RELOC_64
11174           || fixP->fx_r_type == BFD_RELOC_CTOR
11175           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11176           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11177           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11178           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11179           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11180           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11181           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11182           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11183           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11184
11185   value = *valP;
11186
11187   /* If we aren't adjusting this fixup to be against the section
11188      symbol, we need to adjust the value.  */
11189 #ifdef OBJ_ELF
11190   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11191     {
11192       if (mips_need_elf_addend_fixup (fixP))
11193         {
11194           reloc_howto_type *howto;
11195           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11196
11197           value -= symval;
11198
11199           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11200           if (value != 0 && howto && howto->partial_inplace
11201               && (! fixP->fx_pcrel || howto->pcrel_offset))
11202             {
11203               /* In this case, the bfd_install_relocation routine will
11204                  incorrectly add the symbol value back in.  We just want
11205                  the addend to appear in the object file.
11206
11207                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11208                  generated for code like
11209
11210                         globl g1 .text
11211                         .text
11212                         .space 20
11213                  g1:
11214                  x:
11215                         bal g1
11216                */
11217               value -= symval;
11218
11219               /* Make sure the addend is still non-zero.  If it became zero
11220                  after the last operation, set it to a spurious value and
11221                  subtract the same value from the object file's contents.  */
11222               if (value == 0)
11223                 {
11224                   value = 8;
11225
11226                   /* The in-place addends for LO16 relocations are signed;
11227                      leave the matching HI16 in-place addends as zero.  */
11228                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11229                     {
11230                       bfd_vma contents, mask, field;
11231
11232                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11233                                                + fixP->fx_where,
11234                                                fixP->fx_size * 8,
11235                                                target_big_endian);
11236
11237                       /* MASK has bits set where the relocation should go.
11238                          FIELD is -value, shifted into the appropriate place
11239                          for this relocation.  */
11240                       mask = 1 << (howto->bitsize - 1);
11241                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11242                       field = (-value >> howto->rightshift) << howto->bitpos;
11243
11244                       bfd_put_bits ((field & mask) | (contents & ~mask),
11245                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11246                                     fixP->fx_size * 8,
11247                                     target_big_endian);
11248                     }
11249                 }
11250             }
11251         }
11252
11253       /* This code was generated using trial and error and so is
11254          fragile and not trustworthy.  If you change it, you should
11255          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11256          they still pass.  */
11257       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11258         {
11259           value += fixP->fx_frag->fr_address + fixP->fx_where;
11260
11261           /* BFD's REL handling, for MIPS, is _very_ weird.
11262              This gives the right results, but it can't possibly
11263              be the way things are supposed to work.  */
11264           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11265               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11266             value += fixP->fx_frag->fr_address + fixP->fx_where;
11267         }
11268     }
11269 #endif
11270
11271   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11272
11273   /* We are not done if this is a composite relocation to set up gp.  */
11274   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11275       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11276            || (fixP->fx_r_type == BFD_RELOC_64
11277                && (previous_fx_r_type == BFD_RELOC_GPREL32
11278                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11279            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11280                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11281                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11282     fixP->fx_done = 1;
11283   previous_fx_r_type = fixP->fx_r_type;
11284
11285   switch (fixP->fx_r_type)
11286     {
11287     case BFD_RELOC_MIPS_JMP:
11288     case BFD_RELOC_MIPS_SHIFT5:
11289     case BFD_RELOC_MIPS_SHIFT6:
11290     case BFD_RELOC_MIPS_GOT_DISP:
11291     case BFD_RELOC_MIPS_GOT_PAGE:
11292     case BFD_RELOC_MIPS_GOT_OFST:
11293     case BFD_RELOC_MIPS_SUB:
11294     case BFD_RELOC_MIPS_INSERT_A:
11295     case BFD_RELOC_MIPS_INSERT_B:
11296     case BFD_RELOC_MIPS_DELETE:
11297     case BFD_RELOC_MIPS_HIGHEST:
11298     case BFD_RELOC_MIPS_HIGHER:
11299     case BFD_RELOC_MIPS_SCN_DISP:
11300     case BFD_RELOC_MIPS_REL16:
11301     case BFD_RELOC_MIPS_RELGOT:
11302     case BFD_RELOC_MIPS_JALR:
11303     case BFD_RELOC_HI16:
11304     case BFD_RELOC_HI16_S:
11305     case BFD_RELOC_GPREL16:
11306     case BFD_RELOC_MIPS_LITERAL:
11307     case BFD_RELOC_MIPS_CALL16:
11308     case BFD_RELOC_MIPS_GOT16:
11309     case BFD_RELOC_GPREL32:
11310     case BFD_RELOC_MIPS_GOT_HI16:
11311     case BFD_RELOC_MIPS_GOT_LO16:
11312     case BFD_RELOC_MIPS_CALL_HI16:
11313     case BFD_RELOC_MIPS_CALL_LO16:
11314     case BFD_RELOC_MIPS16_GPREL:
11315       if (fixP->fx_pcrel)
11316         as_bad_where (fixP->fx_file, fixP->fx_line,
11317                       _("Invalid PC relative reloc"));
11318       /* Nothing needed to do. The value comes from the reloc entry */
11319       break;
11320
11321     case BFD_RELOC_MIPS16_JMP:
11322       /* We currently always generate a reloc against a symbol, which
11323          means that we don't want an addend even if the symbol is
11324          defined.  */
11325       fixP->fx_addnumber = 0;
11326       break;
11327
11328     case BFD_RELOC_PCREL_HI16_S:
11329       /* The addend for this is tricky if it is internal, so we just
11330          do everything here rather than in bfd_install_relocation.  */
11331       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11332           && !fixP->fx_done
11333           && value != 0)
11334         break;
11335       if (fixP->fx_addsy
11336           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11337         {
11338           /* For an external symbol adjust by the address to make it
11339              pcrel_offset.  We use the address of the RELLO reloc
11340              which follows this one.  */
11341           value += (fixP->fx_next->fx_frag->fr_address
11342                     + fixP->fx_next->fx_where);
11343         }
11344       value = ((value + 0x8000) >> 16) & 0xffff;
11345       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11346       if (target_big_endian)
11347         buf += 2;
11348       md_number_to_chars ((char *) buf, value, 2);
11349       break;
11350
11351     case BFD_RELOC_PCREL_LO16:
11352       /* The addend for this is tricky if it is internal, so we just
11353          do everything here rather than in bfd_install_relocation.  */
11354       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11355           && !fixP->fx_done
11356           && value != 0)
11357         break;
11358       if (fixP->fx_addsy
11359           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11360         value += fixP->fx_frag->fr_address + fixP->fx_where;
11361       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11362       if (target_big_endian)
11363         buf += 2;
11364       md_number_to_chars ((char *) buf, value, 2);
11365       break;
11366
11367     case BFD_RELOC_64:
11368       /* This is handled like BFD_RELOC_32, but we output a sign
11369          extended value if we are only 32 bits.  */
11370       if (fixP->fx_done
11371           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11372         {
11373           if (8 <= sizeof (valueT))
11374             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11375                                 value, 8);
11376           else
11377             {
11378               long w1, w2;
11379               long hiv;
11380
11381               w1 = w2 = fixP->fx_where;
11382               if (target_big_endian)
11383                 w1 += 4;
11384               else
11385                 w2 += 4;
11386               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11387               if ((value & 0x80000000) != 0)
11388                 hiv = 0xffffffff;
11389               else
11390                 hiv = 0;
11391               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11392             }
11393         }
11394       break;
11395
11396     case BFD_RELOC_RVA:
11397     case BFD_RELOC_32:
11398       /* If we are deleting this reloc entry, we must fill in the
11399          value now.  This can happen if we have a .word which is not
11400          resolved when it appears but is later defined.  We also need
11401          to fill in the value if this is an embedded PIC switch table
11402          entry.  */
11403       if (fixP->fx_done
11404           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11405         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11406                             value, 4);
11407       break;
11408
11409     case BFD_RELOC_16:
11410       /* If we are deleting this reloc entry, we must fill in the
11411          value now.  */
11412       assert (fixP->fx_size == 2);
11413       if (fixP->fx_done)
11414         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11415                             value, 2);
11416       break;
11417
11418     case BFD_RELOC_LO16:
11419       /* When handling an embedded PIC switch statement, we can wind
11420          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11421       if (fixP->fx_done)
11422         {
11423           if (value + 0x8000 > 0xffff)
11424             as_bad_where (fixP->fx_file, fixP->fx_line,
11425                           _("relocation overflow"));
11426           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11427           if (target_big_endian)
11428             buf += 2;
11429           md_number_to_chars ((char *) buf, value, 2);
11430         }
11431       break;
11432
11433     case BFD_RELOC_16_PCREL_S2:
11434       if ((value & 0x3) != 0)
11435         as_bad_where (fixP->fx_file, fixP->fx_line,
11436                       _("Branch to odd address (%lx)"), (long) value);
11437
11438       /*
11439        * We need to save the bits in the instruction since fixup_segment()
11440        * might be deleting the relocation entry (i.e., a branch within
11441        * the current segment).
11442        */
11443       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11444         break;
11445       /* If 'value' is zero, the remaining reloc code won't actually
11446          do the store, so it must be done here.  This is probably
11447          a bug somewhere.  */
11448       if (!fixP->fx_done
11449           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11450               || fixP->fx_addsy == NULL                 /* ??? */
11451               || ! S_IS_DEFINED (fixP->fx_addsy)))
11452         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11453
11454       value = (offsetT) value >> 2;
11455
11456       /* update old instruction data */
11457       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11458       if (target_big_endian)
11459         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11460       else
11461         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11462
11463       if (value + 0x8000 <= 0xffff)
11464         insn |= value & 0xffff;
11465       else
11466         {
11467           /* The branch offset is too large.  If this is an
11468              unconditional branch, and we are not generating PIC code,
11469              we can convert it to an absolute jump instruction.  */
11470           if (mips_pic == NO_PIC
11471               && fixP->fx_done
11472               && fixP->fx_frag->fr_address >= text_section->vma
11473               && (fixP->fx_frag->fr_address
11474                   < text_section->vma + text_section->_raw_size)
11475               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11476                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11477                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11478             {
11479               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11480                 insn = 0x0c000000;      /* jal */
11481               else
11482                 insn = 0x08000000;      /* j */
11483               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11484               fixP->fx_done = 0;
11485               fixP->fx_addsy = section_symbol (text_section);
11486               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11487             }
11488           else
11489             {
11490               /* If we got here, we have branch-relaxation disabled,
11491                  and there's nothing we can do to fix this instruction
11492                  without turning it into a longer sequence.  */
11493               as_bad_where (fixP->fx_file, fixP->fx_line,
11494                             _("Branch out of range"));
11495             }
11496         }
11497
11498       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11499       break;
11500
11501     case BFD_RELOC_VTABLE_INHERIT:
11502       fixP->fx_done = 0;
11503       if (fixP->fx_addsy
11504           && !S_IS_DEFINED (fixP->fx_addsy)
11505           && !S_IS_WEAK (fixP->fx_addsy))
11506         S_SET_WEAK (fixP->fx_addsy);
11507       break;
11508
11509     case BFD_RELOC_VTABLE_ENTRY:
11510       fixP->fx_done = 0;
11511       break;
11512
11513     default:
11514       internalError ();
11515     }
11516 }
11517
11518 #if 0
11519 void
11520 printInsn (oc)
11521      unsigned long oc;
11522 {
11523   const struct mips_opcode *p;
11524   int treg, sreg, dreg, shamt;
11525   short imm;
11526   const char *args;
11527   int i;
11528
11529   for (i = 0; i < NUMOPCODES; ++i)
11530     {
11531       p = &mips_opcodes[i];
11532       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11533         {
11534           printf ("%08lx %s\t", oc, p->name);
11535           treg = (oc >> 16) & 0x1f;
11536           sreg = (oc >> 21) & 0x1f;
11537           dreg = (oc >> 11) & 0x1f;
11538           shamt = (oc >> 6) & 0x1f;
11539           imm = oc;
11540           for (args = p->args;; ++args)
11541             {
11542               switch (*args)
11543                 {
11544                 case '\0':
11545                   printf ("\n");
11546                   break;
11547
11548                 case ',':
11549                 case '(':
11550                 case ')':
11551                   printf ("%c", *args);
11552                   continue;
11553
11554                 case 'r':
11555                   assert (treg == sreg);
11556                   printf ("$%d,$%d", treg, sreg);
11557                   continue;
11558
11559                 case 'd':
11560                 case 'G':
11561                   printf ("$%d", dreg);
11562                   continue;
11563
11564                 case 't':
11565                 case 'E':
11566                   printf ("$%d", treg);
11567                   continue;
11568
11569                 case 'k':
11570                   printf ("0x%x", treg);
11571                   continue;
11572
11573                 case 'b':
11574                 case 's':
11575                   printf ("$%d", sreg);
11576                   continue;
11577
11578                 case 'a':
11579                   printf ("0x%08lx", oc & 0x1ffffff);
11580                   continue;
11581
11582                 case 'i':
11583                 case 'j':
11584                 case 'o':
11585                 case 'u':
11586                   printf ("%d", imm);
11587                   continue;
11588
11589                 case '<':
11590                 case '>':
11591                   printf ("$%d", shamt);
11592                   continue;
11593
11594                 default:
11595                   internalError ();
11596                 }
11597               break;
11598             }
11599           return;
11600         }
11601     }
11602   printf (_("%08lx  UNDEFINED\n"), oc);
11603 }
11604 #endif
11605
11606 static symbolS *
11607 get_symbol ()
11608 {
11609   int c;
11610   char *name;
11611   symbolS *p;
11612
11613   name = input_line_pointer;
11614   c = get_symbol_end ();
11615   p = (symbolS *) symbol_find_or_make (name);
11616   *input_line_pointer = c;
11617   return p;
11618 }
11619
11620 /* Align the current frag to a given power of two.  The MIPS assembler
11621    also automatically adjusts any preceding label.  */
11622
11623 static void
11624 mips_align (to, fill, label)
11625      int to;
11626      int fill;
11627      symbolS *label;
11628 {
11629   mips_emit_delays (FALSE);
11630   frag_align (to, fill, 0);
11631   record_alignment (now_seg, to);
11632   if (label != NULL)
11633     {
11634       assert (S_GET_SEGMENT (label) == now_seg);
11635       symbol_set_frag (label, frag_now);
11636       S_SET_VALUE (label, (valueT) frag_now_fix ());
11637     }
11638 }
11639
11640 /* Align to a given power of two.  .align 0 turns off the automatic
11641    alignment used by the data creating pseudo-ops.  */
11642
11643 static void
11644 s_align (x)
11645      int x ATTRIBUTE_UNUSED;
11646 {
11647   register int temp;
11648   register long temp_fill;
11649   long max_alignment = 15;
11650
11651   /*
11652
11653     o  Note that the assembler pulls down any immediately preceeding label
11654        to the aligned address.
11655     o  It's not documented but auto alignment is reinstated by
11656        a .align pseudo instruction.
11657     o  Note also that after auto alignment is turned off the mips assembler
11658        issues an error on attempt to assemble an improperly aligned data item.
11659        We don't.
11660
11661     */
11662
11663   temp = get_absolute_expression ();
11664   if (temp > max_alignment)
11665     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11666   else if (temp < 0)
11667     {
11668       as_warn (_("Alignment negative: 0 assumed."));
11669       temp = 0;
11670     }
11671   if (*input_line_pointer == ',')
11672     {
11673       ++input_line_pointer;
11674       temp_fill = get_absolute_expression ();
11675     }
11676   else
11677     temp_fill = 0;
11678   if (temp)
11679     {
11680       auto_align = 1;
11681       mips_align (temp, (int) temp_fill,
11682                   insn_labels != NULL ? insn_labels->label : NULL);
11683     }
11684   else
11685     {
11686       auto_align = 0;
11687     }
11688
11689   demand_empty_rest_of_line ();
11690 }
11691
11692 void
11693 mips_flush_pending_output ()
11694 {
11695   mips_emit_delays (FALSE);
11696   mips_clear_insn_labels ();
11697 }
11698
11699 static void
11700 s_change_sec (sec)
11701      int sec;
11702 {
11703   segT seg;
11704
11705   /* When generating embedded PIC code, we only use the .text, .lit8,
11706      .sdata and .sbss sections.  We change the .data and .rdata
11707      pseudo-ops to use .sdata.  */
11708   if (mips_pic == EMBEDDED_PIC
11709       && (sec == 'd' || sec == 'r'))
11710     sec = 's';
11711
11712 #ifdef OBJ_ELF
11713   /* The ELF backend needs to know that we are changing sections, so
11714      that .previous works correctly.  We could do something like check
11715      for an obj_section_change_hook macro, but that might be confusing
11716      as it would not be appropriate to use it in the section changing
11717      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11718      This should be cleaner, somehow.  */
11719   obj_elf_section_change_hook ();
11720 #endif
11721
11722   mips_emit_delays (FALSE);
11723   switch (sec)
11724     {
11725     case 't':
11726       s_text (0);
11727       break;
11728     case 'd':
11729       s_data (0);
11730       break;
11731     case 'b':
11732       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11733       demand_empty_rest_of_line ();
11734       break;
11735
11736     case 'r':
11737       if (USE_GLOBAL_POINTER_OPT)
11738         {
11739           seg = subseg_new (RDATA_SECTION_NAME,
11740                             (subsegT) get_absolute_expression ());
11741           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11742             {
11743               bfd_set_section_flags (stdoutput, seg,
11744                                      (SEC_ALLOC
11745                                       | SEC_LOAD
11746                                       | SEC_READONLY
11747                                       | SEC_RELOC
11748                                       | SEC_DATA));
11749               if (strcmp (TARGET_OS, "elf") != 0)
11750                 record_alignment (seg, 4);
11751             }
11752           demand_empty_rest_of_line ();
11753         }
11754       else
11755         {
11756           as_bad (_("No read only data section in this object file format"));
11757           demand_empty_rest_of_line ();
11758           return;
11759         }
11760       break;
11761
11762     case 's':
11763       if (USE_GLOBAL_POINTER_OPT)
11764         {
11765           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11766           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11767             {
11768               bfd_set_section_flags (stdoutput, seg,
11769                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11770                                      | SEC_DATA);
11771               if (strcmp (TARGET_OS, "elf") != 0)
11772                 record_alignment (seg, 4);
11773             }
11774           demand_empty_rest_of_line ();
11775           break;
11776         }
11777       else
11778         {
11779           as_bad (_("Global pointers not supported; recompile -G 0"));
11780           demand_empty_rest_of_line ();
11781           return;
11782         }
11783     }
11784
11785   auto_align = 1;
11786 }
11787
11788 void
11789 s_change_section (ignore)
11790      int ignore ATTRIBUTE_UNUSED;
11791 {
11792 #ifdef OBJ_ELF
11793   char *section_name;
11794   char c;
11795   char next_c;
11796   int section_type;
11797   int section_flag;
11798   int section_entry_size;
11799   int section_alignment;
11800
11801   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11802     return;
11803
11804   section_name = input_line_pointer;
11805   c = get_symbol_end ();
11806   if (c)
11807     next_c = *(input_line_pointer + 1);
11808
11809   /* Do we have .section Name<,"flags">?  */
11810   if (c != ',' || (c == ',' && next_c == '"'))
11811     {
11812       /* just after name is now '\0'.  */
11813       *input_line_pointer = c;
11814       input_line_pointer = section_name;
11815       obj_elf_section (ignore);
11816       return;
11817     }
11818   input_line_pointer++;
11819
11820   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11821   if (c == ',')
11822     section_type = get_absolute_expression ();
11823   else
11824     section_type = 0;
11825   if (*input_line_pointer++ == ',')
11826     section_flag = get_absolute_expression ();
11827   else
11828     section_flag = 0;
11829   if (*input_line_pointer++ == ',')
11830     section_entry_size = get_absolute_expression ();
11831   else
11832     section_entry_size = 0;
11833   if (*input_line_pointer++ == ',')
11834     section_alignment = get_absolute_expression ();
11835   else
11836     section_alignment = 0;
11837
11838   section_name = xstrdup (section_name);
11839
11840   obj_elf_change_section (section_name, section_type, section_flag,
11841                           section_entry_size, 0, 0, 0);
11842
11843   if (now_seg->name != section_name)
11844     free (section_name);
11845 #endif /* OBJ_ELF */
11846 }
11847
11848 void
11849 mips_enable_auto_align ()
11850 {
11851   auto_align = 1;
11852 }
11853
11854 static void
11855 s_cons (log_size)
11856      int log_size;
11857 {
11858   symbolS *label;
11859
11860   label = insn_labels != NULL ? insn_labels->label : NULL;
11861   mips_emit_delays (FALSE);
11862   if (log_size > 0 && auto_align)
11863     mips_align (log_size, 0, label);
11864   mips_clear_insn_labels ();
11865   cons (1 << log_size);
11866 }
11867
11868 static void
11869 s_float_cons (type)
11870      int type;
11871 {
11872   symbolS *label;
11873
11874   label = insn_labels != NULL ? insn_labels->label : NULL;
11875
11876   mips_emit_delays (FALSE);
11877
11878   if (auto_align)
11879     {
11880       if (type == 'd')
11881         mips_align (3, 0, label);
11882       else
11883         mips_align (2, 0, label);
11884     }
11885
11886   mips_clear_insn_labels ();
11887
11888   float_cons (type);
11889 }
11890
11891 /* Handle .globl.  We need to override it because on Irix 5 you are
11892    permitted to say
11893        .globl foo .text
11894    where foo is an undefined symbol, to mean that foo should be
11895    considered to be the address of a function.  */
11896
11897 static void
11898 s_mips_globl (x)
11899      int x ATTRIBUTE_UNUSED;
11900 {
11901   char *name;
11902   int c;
11903   symbolS *symbolP;
11904   flagword flag;
11905
11906   name = input_line_pointer;
11907   c = get_symbol_end ();
11908   symbolP = symbol_find_or_make (name);
11909   *input_line_pointer = c;
11910   SKIP_WHITESPACE ();
11911
11912   /* On Irix 5, every global symbol that is not explicitly labelled as
11913      being a function is apparently labelled as being an object.  */
11914   flag = BSF_OBJECT;
11915
11916   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11917     {
11918       char *secname;
11919       asection *sec;
11920
11921       secname = input_line_pointer;
11922       c = get_symbol_end ();
11923       sec = bfd_get_section_by_name (stdoutput, secname);
11924       if (sec == NULL)
11925         as_bad (_("%s: no such section"), secname);
11926       *input_line_pointer = c;
11927
11928       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11929         flag = BSF_FUNCTION;
11930     }
11931
11932   symbol_get_bfdsym (symbolP)->flags |= flag;
11933
11934   S_SET_EXTERNAL (symbolP);
11935   demand_empty_rest_of_line ();
11936 }
11937
11938 static void
11939 s_option (x)
11940      int x ATTRIBUTE_UNUSED;
11941 {
11942   char *opt;
11943   char c;
11944
11945   opt = input_line_pointer;
11946   c = get_symbol_end ();
11947
11948   if (*opt == 'O')
11949     {
11950       /* FIXME: What does this mean?  */
11951     }
11952   else if (strncmp (opt, "pic", 3) == 0)
11953     {
11954       int i;
11955
11956       i = atoi (opt + 3);
11957       if (i == 0)
11958         mips_pic = NO_PIC;
11959       else if (i == 2)
11960         mips_pic = SVR4_PIC;
11961       else
11962         as_bad (_(".option pic%d not supported"), i);
11963
11964       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11965         {
11966           if (g_switch_seen && g_switch_value != 0)
11967             as_warn (_("-G may not be used with SVR4 PIC code"));
11968           g_switch_value = 0;
11969           bfd_set_gp_size (stdoutput, 0);
11970         }
11971     }
11972   else
11973     as_warn (_("Unrecognized option \"%s\""), opt);
11974
11975   *input_line_pointer = c;
11976   demand_empty_rest_of_line ();
11977 }
11978
11979 /* This structure is used to hold a stack of .set values.  */
11980
11981 struct mips_option_stack
11982 {
11983   struct mips_option_stack *next;
11984   struct mips_set_options options;
11985 };
11986
11987 static struct mips_option_stack *mips_opts_stack;
11988
11989 /* Handle the .set pseudo-op.  */
11990
11991 static void
11992 s_mipsset (x)
11993      int x ATTRIBUTE_UNUSED;
11994 {
11995   char *name = input_line_pointer, ch;
11996
11997   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11998     ++input_line_pointer;
11999   ch = *input_line_pointer;
12000   *input_line_pointer = '\0';
12001
12002   if (strcmp (name, "reorder") == 0)
12003     {
12004       if (mips_opts.noreorder && prev_nop_frag != NULL)
12005         {
12006           /* If we still have pending nops, we can discard them.  The
12007              usual nop handling will insert any that are still
12008              needed.  */
12009           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12010                                     * (mips_opts.mips16 ? 2 : 4));
12011           prev_nop_frag = NULL;
12012         }
12013       mips_opts.noreorder = 0;
12014     }
12015   else if (strcmp (name, "noreorder") == 0)
12016     {
12017       mips_emit_delays (TRUE);
12018       mips_opts.noreorder = 1;
12019       mips_any_noreorder = 1;
12020     }
12021   else if (strcmp (name, "at") == 0)
12022     {
12023       mips_opts.noat = 0;
12024     }
12025   else if (strcmp (name, "noat") == 0)
12026     {
12027       mips_opts.noat = 1;
12028     }
12029   else if (strcmp (name, "macro") == 0)
12030     {
12031       mips_opts.warn_about_macros = 0;
12032     }
12033   else if (strcmp (name, "nomacro") == 0)
12034     {
12035       if (mips_opts.noreorder == 0)
12036         as_bad (_("`noreorder' must be set before `nomacro'"));
12037       mips_opts.warn_about_macros = 1;
12038     }
12039   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12040     {
12041       mips_opts.nomove = 0;
12042     }
12043   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12044     {
12045       mips_opts.nomove = 1;
12046     }
12047   else if (strcmp (name, "bopt") == 0)
12048     {
12049       mips_opts.nobopt = 0;
12050     }
12051   else if (strcmp (name, "nobopt") == 0)
12052     {
12053       mips_opts.nobopt = 1;
12054     }
12055   else if (strcmp (name, "mips16") == 0
12056            || strcmp (name, "MIPS-16") == 0)
12057     mips_opts.mips16 = 1;
12058   else if (strcmp (name, "nomips16") == 0
12059            || strcmp (name, "noMIPS-16") == 0)
12060     mips_opts.mips16 = 0;
12061   else if (strcmp (name, "mips3d") == 0)
12062     mips_opts.ase_mips3d = 1;
12063   else if (strcmp (name, "nomips3d") == 0)
12064     mips_opts.ase_mips3d = 0;
12065   else if (strcmp (name, "mdmx") == 0)
12066     mips_opts.ase_mdmx = 1;
12067   else if (strcmp (name, "nomdmx") == 0)
12068     mips_opts.ase_mdmx = 0;
12069   else if (strncmp (name, "mips", 4) == 0)
12070     {
12071       int reset = 0;
12072
12073       /* Permit the user to change the ISA on the fly.  Needless to
12074          say, misuse can cause serious problems.  */
12075       if (strcmp (name, "mips0") == 0)
12076         {
12077           reset = 1;
12078           mips_opts.isa = file_mips_isa;
12079         }
12080       else if (strcmp (name, "mips1") == 0)
12081         mips_opts.isa = ISA_MIPS1;
12082       else if (strcmp (name, "mips2") == 0)
12083         mips_opts.isa = ISA_MIPS2;
12084       else if (strcmp (name, "mips3") == 0)
12085         mips_opts.isa = ISA_MIPS3;
12086       else if (strcmp (name, "mips4") == 0)
12087         mips_opts.isa = ISA_MIPS4;
12088       else if (strcmp (name, "mips5") == 0)
12089         mips_opts.isa = ISA_MIPS5;
12090       else if (strcmp (name, "mips32") == 0)
12091         mips_opts.isa = ISA_MIPS32;
12092       else if (strcmp (name, "mips32r2") == 0)
12093         mips_opts.isa = ISA_MIPS32R2;
12094       else if (strcmp (name, "mips64") == 0)
12095         mips_opts.isa = ISA_MIPS64;
12096       else
12097         as_bad (_("unknown ISA level %s"), name + 4);
12098
12099       switch (mips_opts.isa)
12100         {
12101         case  0:
12102           break;
12103         case ISA_MIPS1:
12104         case ISA_MIPS2:
12105         case ISA_MIPS32:
12106         case ISA_MIPS32R2:
12107           mips_opts.gp32 = 1;
12108           mips_opts.fp32 = 1;
12109           break;
12110         case ISA_MIPS3:
12111         case ISA_MIPS4:
12112         case ISA_MIPS5:
12113         case ISA_MIPS64:
12114           mips_opts.gp32 = 0;
12115           mips_opts.fp32 = 0;
12116           break;
12117         default:
12118           as_bad (_("unknown ISA level %s"), name + 4);
12119           break;
12120         }
12121       if (reset)
12122         {
12123           mips_opts.gp32 = file_mips_gp32;
12124           mips_opts.fp32 = file_mips_fp32;
12125         }
12126     }
12127   else if (strcmp (name, "autoextend") == 0)
12128     mips_opts.noautoextend = 0;
12129   else if (strcmp (name, "noautoextend") == 0)
12130     mips_opts.noautoextend = 1;
12131   else if (strcmp (name, "push") == 0)
12132     {
12133       struct mips_option_stack *s;
12134
12135       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12136       s->next = mips_opts_stack;
12137       s->options = mips_opts;
12138       mips_opts_stack = s;
12139     }
12140   else if (strcmp (name, "pop") == 0)
12141     {
12142       struct mips_option_stack *s;
12143
12144       s = mips_opts_stack;
12145       if (s == NULL)
12146         as_bad (_(".set pop with no .set push"));
12147       else
12148         {
12149           /* If we're changing the reorder mode we need to handle
12150              delay slots correctly.  */
12151           if (s->options.noreorder && ! mips_opts.noreorder)
12152             mips_emit_delays (TRUE);
12153           else if (! s->options.noreorder && mips_opts.noreorder)
12154             {
12155               if (prev_nop_frag != NULL)
12156                 {
12157                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12158                                             * (mips_opts.mips16 ? 2 : 4));
12159                   prev_nop_frag = NULL;
12160                 }
12161             }
12162
12163           mips_opts = s->options;
12164           mips_opts_stack = s->next;
12165           free (s);
12166         }
12167     }
12168   else
12169     {
12170       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12171     }
12172   *input_line_pointer = ch;
12173   demand_empty_rest_of_line ();
12174 }
12175
12176 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12177    .option pic2.  It means to generate SVR4 PIC calls.  */
12178
12179 static void
12180 s_abicalls (ignore)
12181      int ignore ATTRIBUTE_UNUSED;
12182 {
12183   mips_pic = SVR4_PIC;
12184   if (USE_GLOBAL_POINTER_OPT)
12185     {
12186       if (g_switch_seen && g_switch_value != 0)
12187         as_warn (_("-G may not be used with SVR4 PIC code"));
12188       g_switch_value = 0;
12189     }
12190   bfd_set_gp_size (stdoutput, 0);
12191   demand_empty_rest_of_line ();
12192 }
12193
12194 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12195    PIC code.  It sets the $gp register for the function based on the
12196    function address, which is in the register named in the argument.
12197    This uses a relocation against _gp_disp, which is handled specially
12198    by the linker.  The result is:
12199         lui     $gp,%hi(_gp_disp)
12200         addiu   $gp,$gp,%lo(_gp_disp)
12201         addu    $gp,$gp,.cpload argument
12202    The .cpload argument is normally $25 == $t9.  */
12203
12204 static void
12205 s_cpload (ignore)
12206      int ignore ATTRIBUTE_UNUSED;
12207 {
12208   expressionS ex;
12209   int icnt = 0;
12210
12211   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12212      .cpload is ignored.  */
12213   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12214     {
12215       s_ignore (0);
12216       return;
12217     }
12218
12219   /* .cpload should be in a .set noreorder section.  */
12220   if (mips_opts.noreorder == 0)
12221     as_warn (_(".cpload not in noreorder section"));
12222
12223   ex.X_op = O_symbol;
12224   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12225   ex.X_op_symbol = NULL;
12226   ex.X_add_number = 0;
12227
12228   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12229   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12230
12231   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12232   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12233                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12234
12235   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12236                mips_gp_register, mips_gp_register, tc_get_register (0));
12237
12238   demand_empty_rest_of_line ();
12239 }
12240
12241 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12242      .cpsetup $reg1, offset|$reg2, label
12243
12244    If offset is given, this results in:
12245      sd         $gp, offset($sp)
12246      lui        $gp, %hi(%neg(%gp_rel(label)))
12247      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12248      daddu      $gp, $gp, $reg1
12249
12250    If $reg2 is given, this results in:
12251      daddu      $reg2, $gp, $0
12252      lui        $gp, %hi(%neg(%gp_rel(label)))
12253      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12254      daddu      $gp, $gp, $reg1
12255    $reg1 is normally $25 == $t9.  */
12256 static void
12257 s_cpsetup (ignore)
12258      int ignore ATTRIBUTE_UNUSED;
12259 {
12260   expressionS ex_off;
12261   expressionS ex_sym;
12262   int reg1;
12263   int icnt = 0;
12264   char *f;
12265
12266   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12267      We also need NewABI support.  */
12268   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12269     {
12270       s_ignore (0);
12271       return;
12272     }
12273
12274   reg1 = tc_get_register (0);
12275   SKIP_WHITESPACE ();
12276   if (*input_line_pointer != ',')
12277     {
12278       as_bad (_("missing argument separator ',' for .cpsetup"));
12279       return;
12280     }
12281   else
12282     ++input_line_pointer;
12283   SKIP_WHITESPACE ();
12284   if (*input_line_pointer == '$')
12285     {
12286       mips_cpreturn_register = tc_get_register (0);
12287       mips_cpreturn_offset = -1;
12288     }
12289   else
12290     {
12291       mips_cpreturn_offset = get_absolute_expression ();
12292       mips_cpreturn_register = -1;
12293     }
12294   SKIP_WHITESPACE ();
12295   if (*input_line_pointer != ',')
12296     {
12297       as_bad (_("missing argument separator ',' for .cpsetup"));
12298       return;
12299     }
12300   else
12301     ++input_line_pointer;
12302   SKIP_WHITESPACE ();
12303   expression (&ex_sym);
12304
12305   if (mips_cpreturn_register == -1)
12306     {
12307       ex_off.X_op = O_constant;
12308       ex_off.X_add_symbol = NULL;
12309       ex_off.X_op_symbol = NULL;
12310       ex_off.X_add_number = mips_cpreturn_offset;
12311
12312       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12313                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12314     }
12315   else
12316     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12317                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12318
12319   /* Ensure there's room for the next two instructions, so that `f'
12320      doesn't end up with an address in the wrong frag.  */
12321   frag_grow (8);
12322   f = frag_more (0);
12323   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12324                (int) BFD_RELOC_GPREL16);
12325   fix_new (frag_now, f - frag_now->fr_literal,
12326            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12327   fix_new (frag_now, f - frag_now->fr_literal,
12328            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12329
12330   f = frag_more (0);
12331   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12332                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12333   fix_new (frag_now, f - frag_now->fr_literal,
12334            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12335   fix_new (frag_now, f - frag_now->fr_literal,
12336            0, NULL, 0, 0, BFD_RELOC_LO16);
12337
12338   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12339                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12340                mips_gp_register, mips_gp_register, reg1);
12341
12342   demand_empty_rest_of_line ();
12343 }
12344
12345 static void
12346 s_cplocal (ignore)
12347      int ignore ATTRIBUTE_UNUSED;
12348 {
12349   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12350    .cplocal is ignored.  */
12351   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12352     {
12353       s_ignore (0);
12354       return;
12355     }
12356
12357   mips_gp_register = tc_get_register (0);
12358   demand_empty_rest_of_line ();
12359 }
12360
12361 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12362    offset from $sp.  The offset is remembered, and after making a PIC
12363    call $gp is restored from that location.  */
12364
12365 static void
12366 s_cprestore (ignore)
12367      int ignore ATTRIBUTE_UNUSED;
12368 {
12369   expressionS ex;
12370   int icnt = 0;
12371
12372   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12373      .cprestore is ignored.  */
12374   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12375     {
12376       s_ignore (0);
12377       return;
12378     }
12379
12380   mips_cprestore_offset = get_absolute_expression ();
12381   mips_cprestore_valid = 1;
12382
12383   ex.X_op = O_constant;
12384   ex.X_add_symbol = NULL;
12385   ex.X_op_symbol = NULL;
12386   ex.X_add_number = mips_cprestore_offset;
12387
12388   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12389                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12390                                 mips_gp_register, SP);
12391
12392   demand_empty_rest_of_line ();
12393 }
12394
12395 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12396    was given in the preceeding .gpsetup, it results in:
12397      ld         $gp, offset($sp)
12398
12399    If a register $reg2 was given there, it results in:
12400      daddiu     $gp, $gp, $reg2
12401  */
12402 static void
12403 s_cpreturn (ignore)
12404      int ignore ATTRIBUTE_UNUSED;
12405 {
12406   expressionS ex;
12407   int icnt = 0;
12408
12409   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12410      We also need NewABI support.  */
12411   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12412     {
12413       s_ignore (0);
12414       return;
12415     }
12416
12417   if (mips_cpreturn_register == -1)
12418     {
12419       ex.X_op = O_constant;
12420       ex.X_add_symbol = NULL;
12421       ex.X_op_symbol = NULL;
12422       ex.X_add_number = mips_cpreturn_offset;
12423
12424       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12425                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12426     }
12427   else
12428     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12429                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12430
12431   demand_empty_rest_of_line ();
12432 }
12433
12434 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12435    code.  It sets the offset to use in gp_rel relocations.  */
12436
12437 static void
12438 s_gpvalue (ignore)
12439      int ignore ATTRIBUTE_UNUSED;
12440 {
12441   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12442      We also need NewABI support.  */
12443   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12444     {
12445       s_ignore (0);
12446       return;
12447     }
12448
12449   mips_gprel_offset = get_absolute_expression ();
12450
12451   demand_empty_rest_of_line ();
12452 }
12453
12454 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12455    code.  It generates a 32 bit GP relative reloc.  */
12456
12457 static void
12458 s_gpword (ignore)
12459      int ignore ATTRIBUTE_UNUSED;
12460 {
12461   symbolS *label;
12462   expressionS ex;
12463   char *p;
12464
12465   /* When not generating PIC code, this is treated as .word.  */
12466   if (mips_pic != SVR4_PIC)
12467     {
12468       s_cons (2);
12469       return;
12470     }
12471
12472   label = insn_labels != NULL ? insn_labels->label : NULL;
12473   mips_emit_delays (TRUE);
12474   if (auto_align)
12475     mips_align (2, 0, label);
12476   mips_clear_insn_labels ();
12477
12478   expression (&ex);
12479
12480   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12481     {
12482       as_bad (_("Unsupported use of .gpword"));
12483       ignore_rest_of_line ();
12484     }
12485
12486   p = frag_more (4);
12487   md_number_to_chars (p, (valueT) 0, 4);
12488   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12489                BFD_RELOC_GPREL32);
12490
12491   demand_empty_rest_of_line ();
12492 }
12493
12494 static void
12495 s_gpdword (ignore)
12496      int ignore ATTRIBUTE_UNUSED;
12497 {
12498   symbolS *label;
12499   expressionS ex;
12500   char *p;
12501
12502   /* When not generating PIC code, this is treated as .dword.  */
12503   if (mips_pic != SVR4_PIC)
12504     {
12505       s_cons (3);
12506       return;
12507     }
12508
12509   label = insn_labels != NULL ? insn_labels->label : NULL;
12510   mips_emit_delays (TRUE);
12511   if (auto_align)
12512     mips_align (3, 0, label);
12513   mips_clear_insn_labels ();
12514
12515   expression (&ex);
12516
12517   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12518     {
12519       as_bad (_("Unsupported use of .gpdword"));
12520       ignore_rest_of_line ();
12521     }
12522
12523   p = frag_more (8);
12524   md_number_to_chars (p, (valueT) 0, 8);
12525   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12526                BFD_RELOC_GPREL32);
12527
12528   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12529   ex.X_op = O_absent;
12530   ex.X_add_symbol = 0;
12531   ex.X_add_number = 0;
12532   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12533                BFD_RELOC_64);
12534
12535   demand_empty_rest_of_line ();
12536 }
12537
12538 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12539    tables in SVR4 PIC code.  */
12540
12541 static void
12542 s_cpadd (ignore)
12543      int ignore ATTRIBUTE_UNUSED;
12544 {
12545   int icnt = 0;
12546   int reg;
12547
12548   /* This is ignored when not generating SVR4 PIC code.  */
12549   if (mips_pic != SVR4_PIC)
12550     {
12551       s_ignore (0);
12552       return;
12553     }
12554
12555   /* Add $gp to the register named as an argument.  */
12556   reg = tc_get_register (0);
12557   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12558                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12559                "d,v,t", reg, reg, mips_gp_register);
12560
12561   demand_empty_rest_of_line ();
12562 }
12563
12564 /* Handle the .insn pseudo-op.  This marks instruction labels in
12565    mips16 mode.  This permits the linker to handle them specially,
12566    such as generating jalx instructions when needed.  We also make
12567    them odd for the duration of the assembly, in order to generate the
12568    right sort of code.  We will make them even in the adjust_symtab
12569    routine, while leaving them marked.  This is convenient for the
12570    debugger and the disassembler.  The linker knows to make them odd
12571    again.  */
12572
12573 static void
12574 s_insn (ignore)
12575      int ignore ATTRIBUTE_UNUSED;
12576 {
12577   mips16_mark_labels ();
12578
12579   demand_empty_rest_of_line ();
12580 }
12581
12582 /* Handle a .stabn directive.  We need these in order to mark a label
12583    as being a mips16 text label correctly.  Sometimes the compiler
12584    will emit a label, followed by a .stabn, and then switch sections.
12585    If the label and .stabn are in mips16 mode, then the label is
12586    really a mips16 text label.  */
12587
12588 static void
12589 s_mips_stab (type)
12590      int type;
12591 {
12592   if (type == 'n')
12593     mips16_mark_labels ();
12594
12595   s_stab (type);
12596 }
12597
12598 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12599  */
12600
12601 static void
12602 s_mips_weakext (ignore)
12603      int ignore ATTRIBUTE_UNUSED;
12604 {
12605   char *name;
12606   int c;
12607   symbolS *symbolP;
12608   expressionS exp;
12609
12610   name = input_line_pointer;
12611   c = get_symbol_end ();
12612   symbolP = symbol_find_or_make (name);
12613   S_SET_WEAK (symbolP);
12614   *input_line_pointer = c;
12615
12616   SKIP_WHITESPACE ();
12617
12618   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12619     {
12620       if (S_IS_DEFINED (symbolP))
12621         {
12622           as_bad ("ignoring attempt to redefine symbol %s",
12623                   S_GET_NAME (symbolP));
12624           ignore_rest_of_line ();
12625           return;
12626         }
12627
12628       if (*input_line_pointer == ',')
12629         {
12630           ++input_line_pointer;
12631           SKIP_WHITESPACE ();
12632         }
12633
12634       expression (&exp);
12635       if (exp.X_op != O_symbol)
12636         {
12637           as_bad ("bad .weakext directive");
12638           ignore_rest_of_line ();
12639           return;
12640         }
12641       symbol_set_value_expression (symbolP, &exp);
12642     }
12643
12644   demand_empty_rest_of_line ();
12645 }
12646
12647 /* Parse a register string into a number.  Called from the ECOFF code
12648    to parse .frame.  The argument is non-zero if this is the frame
12649    register, so that we can record it in mips_frame_reg.  */
12650
12651 int
12652 tc_get_register (frame)
12653      int frame;
12654 {
12655   int reg;
12656
12657   SKIP_WHITESPACE ();
12658   if (*input_line_pointer++ != '$')
12659     {
12660       as_warn (_("expected `$'"));
12661       reg = ZERO;
12662     }
12663   else if (ISDIGIT (*input_line_pointer))
12664     {
12665       reg = get_absolute_expression ();
12666       if (reg < 0 || reg >= 32)
12667         {
12668           as_warn (_("Bad register number"));
12669           reg = ZERO;
12670         }
12671     }
12672   else
12673     {
12674       if (strncmp (input_line_pointer, "ra", 2) == 0)
12675         {
12676           reg = RA;
12677           input_line_pointer += 2;
12678         }
12679       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12680         {
12681           reg = FP;
12682           input_line_pointer += 2;
12683         }
12684       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12685         {
12686           reg = SP;
12687           input_line_pointer += 2;
12688         }
12689       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12690         {
12691           reg = GP;
12692           input_line_pointer += 2;
12693         }
12694       else if (strncmp (input_line_pointer, "at", 2) == 0)
12695         {
12696           reg = AT;
12697           input_line_pointer += 2;
12698         }
12699       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12700         {
12701           reg = KT0;
12702           input_line_pointer += 3;
12703         }
12704       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12705         {
12706           reg = KT1;
12707           input_line_pointer += 3;
12708         }
12709       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12710         {
12711           reg = ZERO;
12712           input_line_pointer += 4;
12713         }
12714       else
12715         {
12716           as_warn (_("Unrecognized register name"));
12717           reg = ZERO;
12718           while (ISALNUM(*input_line_pointer))
12719            input_line_pointer++;
12720         }
12721     }
12722   if (frame)
12723     {
12724       mips_frame_reg = reg != 0 ? reg : SP;
12725       mips_frame_reg_valid = 1;
12726       mips_cprestore_valid = 0;
12727     }
12728   return reg;
12729 }
12730
12731 valueT
12732 md_section_align (seg, addr)
12733      asection *seg;
12734      valueT addr;
12735 {
12736   int align = bfd_get_section_alignment (stdoutput, seg);
12737
12738 #ifdef OBJ_ELF
12739   /* We don't need to align ELF sections to the full alignment.
12740      However, Irix 5 may prefer that we align them at least to a 16
12741      byte boundary.  We don't bother to align the sections if we are
12742      targeted for an embedded system.  */
12743   if (strcmp (TARGET_OS, "elf") == 0)
12744     return addr;
12745   if (align > 4)
12746     align = 4;
12747 #endif
12748
12749   return ((addr + (1 << align) - 1) & (-1 << align));
12750 }
12751
12752 /* Utility routine, called from above as well.  If called while the
12753    input file is still being read, it's only an approximation.  (For
12754    example, a symbol may later become defined which appeared to be
12755    undefined earlier.)  */
12756
12757 static int
12758 nopic_need_relax (sym, before_relaxing)
12759      symbolS *sym;
12760      int before_relaxing;
12761 {
12762   if (sym == 0)
12763     return 0;
12764
12765   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12766     {
12767       const char *symname;
12768       int change;
12769
12770       /* Find out whether this symbol can be referenced off the $gp
12771          register.  It can be if it is smaller than the -G size or if
12772          it is in the .sdata or .sbss section.  Certain symbols can
12773          not be referenced off the $gp, although it appears as though
12774          they can.  */
12775       symname = S_GET_NAME (sym);
12776       if (symname != (const char *) NULL
12777           && (strcmp (symname, "eprol") == 0
12778               || strcmp (symname, "etext") == 0
12779               || strcmp (symname, "_gp") == 0
12780               || strcmp (symname, "edata") == 0
12781               || strcmp (symname, "_fbss") == 0
12782               || strcmp (symname, "_fdata") == 0
12783               || strcmp (symname, "_ftext") == 0
12784               || strcmp (symname, "end") == 0
12785               || strcmp (symname, "_gp_disp") == 0))
12786         change = 1;
12787       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12788                && !S_IS_EXTERN (sym)
12789                && (0
12790 #ifndef NO_ECOFF_DEBUGGING
12791                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12792                        && (symbol_get_obj (sym)->ecoff_extern_size
12793                            <= g_switch_value))
12794 #endif
12795                    /* We must defer this decision until after the whole
12796                       file has been read, since there might be a .extern
12797                       after the first use of this symbol.  */
12798                    || (before_relaxing
12799 #ifndef NO_ECOFF_DEBUGGING
12800                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12801 #endif
12802                        && S_GET_VALUE (sym) == 0)
12803                    || (S_GET_VALUE (sym) != 0
12804                        && S_GET_VALUE (sym) <= g_switch_value)))
12805         change = 0;
12806       else
12807         {
12808           const char *segname;
12809
12810           segname = segment_name (S_GET_SEGMENT (sym));
12811           assert (strcmp (segname, ".lit8") != 0
12812                   && strcmp (segname, ".lit4") != 0);
12813           change = (strcmp (segname, ".sdata") != 0
12814                     && strcmp (segname, ".sbss") != 0
12815                     && strncmp (segname, ".sdata.", 7) != 0
12816                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12817         }
12818       return change;
12819     }
12820   else
12821     /* We are not optimizing for the $gp register.  */
12822     return 1;
12823 }
12824
12825
12826 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12827
12828 static bfd_boolean
12829 pic_need_relax (sym, segtype)
12830      symbolS *sym;
12831      asection *segtype;
12832 {
12833   asection *symsec;
12834   bfd_boolean linkonce;
12835
12836   /* Handle the case of a symbol equated to another symbol.  */
12837   while (symbol_equated_reloc_p (sym))
12838     {
12839       symbolS *n;
12840
12841       /* It's possible to get a loop here in a badly written
12842          program.  */
12843       n = symbol_get_value_expression (sym)->X_add_symbol;
12844       if (n == sym)
12845         break;
12846       sym = n;
12847     }
12848
12849   symsec = S_GET_SEGMENT (sym);
12850
12851   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12852   linkonce = FALSE;
12853   if (symsec != segtype && ! S_IS_LOCAL (sym))
12854     {
12855       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12856           != 0)
12857         linkonce = TRUE;
12858
12859       /* The GNU toolchain uses an extension for ELF: a section
12860          beginning with the magic string .gnu.linkonce is a linkonce
12861          section.  */
12862       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12863                    sizeof ".gnu.linkonce" - 1) == 0)
12864         linkonce = TRUE;
12865     }
12866
12867   /* This must duplicate the test in adjust_reloc_syms.  */
12868   return (symsec != &bfd_und_section
12869           && symsec != &bfd_abs_section
12870           && ! bfd_is_com_section (symsec)
12871           && !linkonce
12872 #ifdef OBJ_ELF
12873           /* A global or weak symbol is treated as external.  */
12874           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12875               || (! S_IS_WEAK (sym)
12876                   && (! S_IS_EXTERNAL (sym)
12877                       || mips_pic == EMBEDDED_PIC)))
12878 #endif
12879           );
12880 }
12881
12882
12883 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12884    extended opcode.  SEC is the section the frag is in.  */
12885
12886 static int
12887 mips16_extended_frag (fragp, sec, stretch)
12888      fragS *fragp;
12889      asection *sec;
12890      long stretch;
12891 {
12892   int type;
12893   register const struct mips16_immed_operand *op;
12894   offsetT val;
12895   int mintiny, maxtiny;
12896   segT symsec;
12897   fragS *sym_frag;
12898
12899   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12900     return 0;
12901   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12902     return 1;
12903
12904   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12905   op = mips16_immed_operands;
12906   while (op->type != type)
12907     {
12908       ++op;
12909       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12910     }
12911
12912   if (op->unsp)
12913     {
12914       if (type == '<' || type == '>' || type == '[' || type == ']')
12915         {
12916           mintiny = 1;
12917           maxtiny = 1 << op->nbits;
12918         }
12919       else
12920         {
12921           mintiny = 0;
12922           maxtiny = (1 << op->nbits) - 1;
12923         }
12924     }
12925   else
12926     {
12927       mintiny = - (1 << (op->nbits - 1));
12928       maxtiny = (1 << (op->nbits - 1)) - 1;
12929     }
12930
12931   sym_frag = symbol_get_frag (fragp->fr_symbol);
12932   val = S_GET_VALUE (fragp->fr_symbol);
12933   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12934
12935   if (op->pcrel)
12936     {
12937       addressT addr;
12938
12939       /* We won't have the section when we are called from
12940          mips_relax_frag.  However, we will always have been called
12941          from md_estimate_size_before_relax first.  If this is a
12942          branch to a different section, we mark it as such.  If SEC is
12943          NULL, and the frag is not marked, then it must be a branch to
12944          the same section.  */
12945       if (sec == NULL)
12946         {
12947           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12948             return 1;
12949         }
12950       else
12951         {
12952           /* Must have been called from md_estimate_size_before_relax.  */
12953           if (symsec != sec)
12954             {
12955               fragp->fr_subtype =
12956                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12957
12958               /* FIXME: We should support this, and let the linker
12959                  catch branches and loads that are out of range.  */
12960               as_bad_where (fragp->fr_file, fragp->fr_line,
12961                             _("unsupported PC relative reference to different section"));
12962
12963               return 1;
12964             }
12965           if (fragp != sym_frag && sym_frag->fr_address == 0)
12966             /* Assume non-extended on the first relaxation pass.
12967                The address we have calculated will be bogus if this is
12968                a forward branch to another frag, as the forward frag
12969                will have fr_address == 0.  */
12970             return 0;
12971         }
12972
12973       /* In this case, we know for sure that the symbol fragment is in
12974          the same section.  If the relax_marker of the symbol fragment
12975          differs from the relax_marker of this fragment, we have not
12976          yet adjusted the symbol fragment fr_address.  We want to add
12977          in STRETCH in order to get a better estimate of the address.
12978          This particularly matters because of the shift bits.  */
12979       if (stretch != 0
12980           && sym_frag->relax_marker != fragp->relax_marker)
12981         {
12982           fragS *f;
12983
12984           /* Adjust stretch for any alignment frag.  Note that if have
12985              been expanding the earlier code, the symbol may be
12986              defined in what appears to be an earlier frag.  FIXME:
12987              This doesn't handle the fr_subtype field, which specifies
12988              a maximum number of bytes to skip when doing an
12989              alignment.  */
12990           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12991             {
12992               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12993                 {
12994                   if (stretch < 0)
12995                     stretch = - ((- stretch)
12996                                  & ~ ((1 << (int) f->fr_offset) - 1));
12997                   else
12998                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12999                   if (stretch == 0)
13000                     break;
13001                 }
13002             }
13003           if (f != NULL)
13004             val += stretch;
13005         }
13006
13007       addr = fragp->fr_address + fragp->fr_fix;
13008
13009       /* The base address rules are complicated.  The base address of
13010          a branch is the following instruction.  The base address of a
13011          PC relative load or add is the instruction itself, but if it
13012          is in a delay slot (in which case it can not be extended) use
13013          the address of the instruction whose delay slot it is in.  */
13014       if (type == 'p' || type == 'q')
13015         {
13016           addr += 2;
13017
13018           /* If we are currently assuming that this frag should be
13019              extended, then, the current address is two bytes
13020              higher.  */
13021           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13022             addr += 2;
13023
13024           /* Ignore the low bit in the target, since it will be set
13025              for a text label.  */
13026           if ((val & 1) != 0)
13027             --val;
13028         }
13029       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13030         addr -= 4;
13031       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13032         addr -= 2;
13033
13034       val -= addr & ~ ((1 << op->shift) - 1);
13035
13036       /* Branch offsets have an implicit 0 in the lowest bit.  */
13037       if (type == 'p' || type == 'q')
13038         val /= 2;
13039
13040       /* If any of the shifted bits are set, we must use an extended
13041          opcode.  If the address depends on the size of this
13042          instruction, this can lead to a loop, so we arrange to always
13043          use an extended opcode.  We only check this when we are in
13044          the main relaxation loop, when SEC is NULL.  */
13045       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13046         {
13047           fragp->fr_subtype =
13048             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13049           return 1;
13050         }
13051
13052       /* If we are about to mark a frag as extended because the value
13053          is precisely maxtiny + 1, then there is a chance of an
13054          infinite loop as in the following code:
13055              la $4,foo
13056              .skip      1020
13057              .align     2
13058            foo:
13059          In this case when the la is extended, foo is 0x3fc bytes
13060          away, so the la can be shrunk, but then foo is 0x400 away, so
13061          the la must be extended.  To avoid this loop, we mark the
13062          frag as extended if it was small, and is about to become
13063          extended with a value of maxtiny + 1.  */
13064       if (val == ((maxtiny + 1) << op->shift)
13065           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13066           && sec == NULL)
13067         {
13068           fragp->fr_subtype =
13069             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13070           return 1;
13071         }
13072     }
13073   else if (symsec != absolute_section && sec != NULL)
13074     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13075
13076   if ((val & ((1 << op->shift) - 1)) != 0
13077       || val < (mintiny << op->shift)
13078       || val > (maxtiny << op->shift))
13079     return 1;
13080   else
13081     return 0;
13082 }
13083
13084 /* Compute the length of a branch sequence, and adjust the
13085    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13086    worst-case length is computed, with UPDATE being used to indicate
13087    whether an unconditional (-1), branch-likely (+1) or regular (0)
13088    branch is to be computed.  */
13089 static int
13090 relaxed_branch_length (fragp, sec, update)
13091      fragS *fragp;
13092      asection *sec;
13093      int update;
13094 {
13095   bfd_boolean toofar;
13096   int length;
13097
13098   if (fragp
13099       && S_IS_DEFINED (fragp->fr_symbol)
13100       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13101     {
13102       addressT addr;
13103       offsetT val;
13104
13105       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13106
13107       addr = fragp->fr_address + fragp->fr_fix + 4;
13108
13109       val -= addr;
13110
13111       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13112     }
13113   else if (fragp)
13114     /* If the symbol is not defined or it's in a different segment,
13115        assume the user knows what's going on and emit a short
13116        branch.  */
13117     toofar = FALSE;
13118   else
13119     toofar = TRUE;
13120
13121   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13122     fragp->fr_subtype
13123       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13124                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13125                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13126                              toofar);
13127
13128   length = 4;
13129   if (toofar)
13130     {
13131       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13132         length += 8;
13133
13134       if (mips_pic != NO_PIC)
13135         {
13136           /* Additional space for PIC loading of target address.  */
13137           length += 8;
13138           if (mips_opts.isa == ISA_MIPS1)
13139             /* Additional space for $at-stabilizing nop.  */
13140             length += 4;
13141         }
13142
13143       /* If branch is conditional.  */
13144       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13145         length += 8;
13146     }
13147
13148   return length;
13149 }
13150
13151 /* Estimate the size of a frag before relaxing.  Unless this is the
13152    mips16, we are not really relaxing here, and the final size is
13153    encoded in the subtype information.  For the mips16, we have to
13154    decide whether we are using an extended opcode or not.  */
13155
13156 int
13157 md_estimate_size_before_relax (fragp, segtype)
13158      fragS *fragp;
13159      asection *segtype;
13160 {
13161   int change;
13162
13163   if (RELAX_BRANCH_P (fragp->fr_subtype))
13164     {
13165
13166       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13167
13168       return fragp->fr_var;
13169     }
13170
13171   if (RELAX_MIPS16_P (fragp->fr_subtype))
13172     /* We don't want to modify the EXTENDED bit here; it might get us
13173        into infinite loops.  We change it only in mips_relax_frag().  */
13174     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13175
13176   if (mips_pic == NO_PIC)
13177     change = nopic_need_relax (fragp->fr_symbol, 0);
13178   else if (mips_pic == SVR4_PIC)
13179     change = pic_need_relax (fragp->fr_symbol, segtype);
13180   else
13181     abort ();
13182
13183   if (change)
13184     {
13185       /* Record the offset to the first reloc in the fr_opcode field.
13186          This lets md_convert_frag and tc_gen_reloc know that the code
13187          must be expanded.  */
13188       fragp->fr_opcode = (fragp->fr_literal
13189                           + fragp->fr_fix
13190                           - RELAX_OLD (fragp->fr_subtype)
13191                           + RELAX_RELOC1 (fragp->fr_subtype));
13192       /* FIXME: This really needs as_warn_where.  */
13193       if (RELAX_WARN (fragp->fr_subtype))
13194         as_warn (_("AT used after \".set noat\" or macro used after "
13195                    "\".set nomacro\""));
13196
13197       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13198     }
13199
13200   return 0;
13201 }
13202
13203 /* This is called to see whether a reloc against a defined symbol
13204    should be converted into a reloc against a section.  Don't adjust
13205    MIPS16 jump relocations, so we don't have to worry about the format
13206    of the offset in the .o file.  Don't adjust relocations against
13207    mips16 symbols, so that the linker can find them if it needs to set
13208    up a stub.  */
13209
13210 int
13211 mips_fix_adjustable (fixp)
13212      fixS *fixp;
13213 {
13214   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13215     return 0;
13216
13217   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13218       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13219     return 0;
13220
13221   if (fixp->fx_addsy == NULL)
13222     return 1;
13223
13224 #ifdef OBJ_ELF
13225   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13226       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13227       && fixp->fx_subsy == NULL)
13228     return 0;
13229 #endif
13230
13231   return 1;
13232 }
13233
13234 /* Translate internal representation of relocation info to BFD target
13235    format.  */
13236
13237 arelent **
13238 tc_gen_reloc (section, fixp)
13239      asection *section ATTRIBUTE_UNUSED;
13240      fixS *fixp;
13241 {
13242   static arelent *retval[4];
13243   arelent *reloc;
13244   bfd_reloc_code_real_type code;
13245
13246   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13247   retval[1] = NULL;
13248
13249   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13250   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13251   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13252
13253   if (mips_pic == EMBEDDED_PIC
13254       && SWITCH_TABLE (fixp))
13255     {
13256       /* For a switch table entry we use a special reloc.  The addend
13257          is actually the difference between the reloc address and the
13258          subtrahend.  */
13259       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13260       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13261         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13262       fixp->fx_r_type = BFD_RELOC_GPREL32;
13263     }
13264   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13265     {
13266       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13267         reloc->addend = fixp->fx_addnumber;
13268       else
13269         {
13270           /* We use a special addend for an internal RELLO reloc.  */
13271           if (symbol_section_p (fixp->fx_addsy))
13272             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13273           else
13274             reloc->addend = fixp->fx_addnumber + reloc->address;
13275         }
13276     }
13277   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13278     {
13279       assert (fixp->fx_next != NULL
13280               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13281
13282       /* The reloc is relative to the RELLO; adjust the addend
13283          accordingly.  */
13284       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13285         reloc->addend = fixp->fx_next->fx_addnumber;
13286       else
13287         {
13288           /* We use a special addend for an internal RELHI reloc.  */
13289           if (symbol_section_p (fixp->fx_addsy))
13290             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13291                              + fixp->fx_next->fx_where
13292                              - S_GET_VALUE (fixp->fx_subsy));
13293           else
13294             reloc->addend = (fixp->fx_addnumber
13295                              + fixp->fx_next->fx_frag->fr_address
13296                              + fixp->fx_next->fx_where);
13297         }
13298     }
13299   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13300     reloc->addend = fixp->fx_addnumber;
13301   else
13302     {
13303       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13304         /* A gruesome hack which is a result of the gruesome gas reloc
13305            handling.  */
13306         reloc->addend = reloc->address;
13307       else
13308         reloc->addend = -reloc->address;
13309     }
13310
13311   /* If this is a variant frag, we may need to adjust the existing
13312      reloc and generate a new one.  */
13313   if (fixp->fx_frag->fr_opcode != NULL
13314       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13315            && ! HAVE_NEWABI)
13316           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13317           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13318           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13319           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13320           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13321           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13322     )
13323     {
13324       arelent *reloc2;
13325
13326       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13327
13328       /* If this is not the last reloc in this frag, then we have two
13329          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13330          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13331          the second one handle all of them.  */
13332       if (fixp->fx_next != NULL
13333           && fixp->fx_frag == fixp->fx_next->fx_frag)
13334         {
13335           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13336                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13337                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13338                       && (fixp->fx_next->fx_r_type
13339                           == BFD_RELOC_MIPS_GOT_LO16))
13340                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13341                       && (fixp->fx_next->fx_r_type
13342                           == BFD_RELOC_MIPS_CALL_LO16)));
13343           retval[0] = NULL;
13344           return retval;
13345         }
13346
13347       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13348       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13349       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13350       retval[2] = NULL;
13351       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13352       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13353       reloc2->address = (reloc->address
13354                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13355                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13356       reloc2->addend = fixp->fx_addnumber;
13357       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13358       assert (reloc2->howto != NULL);
13359
13360       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13361         {
13362           arelent *reloc3;
13363
13364           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13365           retval[3] = NULL;
13366           *reloc3 = *reloc2;
13367           reloc3->address += 4;
13368         }
13369
13370       if (mips_pic == NO_PIC)
13371         {
13372           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13373           fixp->fx_r_type = BFD_RELOC_HI16_S;
13374         }
13375       else if (mips_pic == SVR4_PIC)
13376         {
13377           switch (fixp->fx_r_type)
13378             {
13379             default:
13380               abort ();
13381             case BFD_RELOC_MIPS_GOT16:
13382               break;
13383             case BFD_RELOC_MIPS_GOT_LO16:
13384             case BFD_RELOC_MIPS_CALL_LO16:
13385               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13386               break;
13387             case BFD_RELOC_MIPS_CALL16:
13388               if (HAVE_NEWABI)
13389                 {
13390                   /* BFD_RELOC_MIPS_GOT16;*/
13391                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13392                   reloc2->howto = bfd_reloc_type_lookup
13393                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13394                 }
13395               else
13396                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13397               break;
13398             }
13399         }
13400       else
13401         abort ();
13402
13403       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13404       if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13405         {
13406           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13407           retval[1] = NULL;
13408         }
13409     }
13410
13411   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13412      entry to be used in the relocation's section offset.  */
13413   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13414     {
13415       reloc->address = reloc->addend;
13416       reloc->addend = 0;
13417     }
13418
13419   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13420      fixup_segment converted a non-PC relative reloc into a PC
13421      relative reloc.  In such a case, we need to convert the reloc
13422      code.  */
13423   code = fixp->fx_r_type;
13424   if (fixp->fx_pcrel)
13425     {
13426       switch (code)
13427         {
13428         case BFD_RELOC_8:
13429           code = BFD_RELOC_8_PCREL;
13430           break;
13431         case BFD_RELOC_16:
13432           code = BFD_RELOC_16_PCREL;
13433           break;
13434         case BFD_RELOC_32:
13435           code = BFD_RELOC_32_PCREL;
13436           break;
13437         case BFD_RELOC_64:
13438           code = BFD_RELOC_64_PCREL;
13439           break;
13440         case BFD_RELOC_8_PCREL:
13441         case BFD_RELOC_16_PCREL:
13442         case BFD_RELOC_32_PCREL:
13443         case BFD_RELOC_64_PCREL:
13444         case BFD_RELOC_16_PCREL_S2:
13445         case BFD_RELOC_PCREL_HI16_S:
13446         case BFD_RELOC_PCREL_LO16:
13447           break;
13448         default:
13449           as_bad_where (fixp->fx_file, fixp->fx_line,
13450                         _("Cannot make %s relocation PC relative"),
13451                         bfd_get_reloc_code_name (code));
13452         }
13453     }
13454
13455 #ifdef OBJ_ELF
13456   /* md_apply_fix3 has a double-subtraction hack to get
13457      bfd_install_relocation to behave nicely.  GPREL relocations are
13458      handled correctly without this hack, so undo it here.  We can't
13459      stop md_apply_fix3 from subtracting twice in the first place since
13460      the fake addend is required for variant frags above.  */
13461   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13462       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13463       && reloc->addend != 0
13464       && mips_need_elf_addend_fixup (fixp))
13465     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13466 #endif
13467
13468   /* To support a PC relative reloc when generating embedded PIC code
13469      for ECOFF, we use a Cygnus extension.  We check for that here to
13470      make sure that we don't let such a reloc escape normally.  */
13471   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13472        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13473       && code == BFD_RELOC_16_PCREL_S2
13474       && mips_pic != EMBEDDED_PIC)
13475     reloc->howto = NULL;
13476   else
13477     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13478
13479   if (reloc->howto == NULL)
13480     {
13481       as_bad_where (fixp->fx_file, fixp->fx_line,
13482                     _("Can not represent %s relocation in this object file format"),
13483                     bfd_get_reloc_code_name (code));
13484       retval[0] = NULL;
13485     }
13486
13487   return retval;
13488 }
13489
13490 /* Relax a machine dependent frag.  This returns the amount by which
13491    the current size of the frag should change.  */
13492
13493 int
13494 mips_relax_frag (sec, fragp, stretch)
13495      asection *sec;
13496      fragS *fragp;
13497      long stretch;
13498 {
13499   if (RELAX_BRANCH_P (fragp->fr_subtype))
13500     {
13501       offsetT old_var = fragp->fr_var;
13502
13503       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13504
13505       return fragp->fr_var - old_var;
13506     }
13507
13508   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13509     return 0;
13510
13511   if (mips16_extended_frag (fragp, NULL, stretch))
13512     {
13513       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13514         return 0;
13515       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13516       return 2;
13517     }
13518   else
13519     {
13520       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13521         return 0;
13522       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13523       return -2;
13524     }
13525
13526   return 0;
13527 }
13528
13529 /* Convert a machine dependent frag.  */
13530
13531 void
13532 md_convert_frag (abfd, asec, fragp)
13533      bfd *abfd ATTRIBUTE_UNUSED;
13534      segT asec;
13535      fragS *fragp;
13536 {
13537   int old, new;
13538   char *fixptr;
13539
13540   if (RELAX_BRANCH_P (fragp->fr_subtype))
13541     {
13542       bfd_byte *buf;
13543       unsigned long insn;
13544       expressionS exp;
13545       fixS *fixp;
13546
13547       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13548
13549       if (target_big_endian)
13550         insn = bfd_getb32 (buf);
13551       else
13552         insn = bfd_getl32 (buf);
13553
13554       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13555         {
13556           /* We generate a fixup instead of applying it right now
13557              because, if there are linker relaxations, we're going to
13558              need the relocations.  */
13559           exp.X_op = O_symbol;
13560           exp.X_add_symbol = fragp->fr_symbol;
13561           exp.X_add_number = fragp->fr_offset;
13562
13563           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13564                               4, &exp, 1,
13565                               BFD_RELOC_16_PCREL_S2);
13566           fixp->fx_file = fragp->fr_file;
13567           fixp->fx_line = fragp->fr_line;
13568
13569           md_number_to_chars ((char *)buf, insn, 4);
13570           buf += 4;
13571         }
13572       else
13573         {
13574           int i;
13575
13576           as_warn_where (fragp->fr_file, fragp->fr_line,
13577                          _("relaxed out-of-range branch into a jump"));
13578
13579           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13580             goto uncond;
13581
13582           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13583             {
13584               /* Reverse the branch.  */
13585               switch ((insn >> 28) & 0xf)
13586                 {
13587                 case 4:
13588                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13589                      have the condition reversed by tweaking a single
13590                      bit, and their opcodes all have 0x4???????.  */
13591                   assert ((insn & 0xf1000000) == 0x41000000);
13592                   insn ^= 0x00010000;
13593                   break;
13594
13595                 case 0:
13596                   /* bltz       0x04000000      bgez    0x04010000
13597                      bltzal     0x04100000      bgezal  0x04110000 */
13598                   assert ((insn & 0xfc0e0000) == 0x04000000);
13599                   insn ^= 0x00010000;
13600                   break;
13601
13602                 case 1:
13603                   /* beq        0x10000000      bne     0x14000000
13604                      blez       0x18000000      bgtz    0x1c000000 */
13605                   insn ^= 0x04000000;
13606                   break;
13607
13608                 default:
13609                   abort ();
13610                 }
13611             }
13612
13613           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13614             {
13615               /* Clear the and-link bit.  */
13616               assert ((insn & 0xfc1c0000) == 0x04100000);
13617
13618               /* bltzal 0x04100000      bgezal  0x04110000
13619                 bltzall 0x04120000     bgezall  0x04130000 */
13620               insn &= ~0x00100000;
13621             }
13622
13623           /* Branch over the branch (if the branch was likely) or the
13624              full jump (not likely case).  Compute the offset from the
13625              current instruction to branch to.  */
13626           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13627             i = 16;
13628           else
13629             {
13630               /* How many bytes in instructions we've already emitted?  */
13631               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13632               /* How many bytes in instructions from here to the end?  */
13633               i = fragp->fr_var - i;
13634             }
13635           /* Convert to instruction count.  */
13636           i >>= 2;
13637           /* Branch counts from the next instruction.  */
13638           i--;
13639           insn |= i;
13640           /* Branch over the jump.  */
13641           md_number_to_chars ((char *)buf, insn, 4);
13642           buf += 4;
13643
13644           /* Nop */
13645           md_number_to_chars ((char*)buf, 0, 4);
13646           buf += 4;
13647
13648           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13649             {
13650               /* beql $0, $0, 2f */
13651               insn = 0x50000000;
13652               /* Compute the PC offset from the current instruction to
13653                  the end of the variable frag.  */
13654               /* How many bytes in instructions we've already emitted?  */
13655               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13656               /* How many bytes in instructions from here to the end?  */
13657               i = fragp->fr_var - i;
13658               /* Convert to instruction count.  */
13659               i >>= 2;
13660               /* Don't decrement i, because we want to branch over the
13661                  delay slot.  */
13662
13663               insn |= i;
13664               md_number_to_chars ((char *)buf, insn, 4);
13665               buf += 4;
13666
13667               md_number_to_chars ((char *)buf, 0, 4);
13668               buf += 4;
13669             }
13670
13671         uncond:
13672           if (mips_pic == NO_PIC)
13673             {
13674               /* j or jal.  */
13675               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13676                       ? 0x0c000000 : 0x08000000);
13677               exp.X_op = O_symbol;
13678               exp.X_add_symbol = fragp->fr_symbol;
13679               exp.X_add_number = fragp->fr_offset;
13680
13681               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13682                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13683               fixp->fx_file = fragp->fr_file;
13684               fixp->fx_line = fragp->fr_line;
13685
13686               md_number_to_chars ((char*)buf, insn, 4);
13687               buf += 4;
13688             }
13689           else
13690             {
13691               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13692               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13693               exp.X_op = O_symbol;
13694               exp.X_add_symbol = fragp->fr_symbol;
13695               exp.X_add_number = fragp->fr_offset;
13696
13697               if (fragp->fr_offset)
13698                 {
13699                   exp.X_add_symbol = make_expr_symbol (&exp);
13700                   exp.X_add_number = 0;
13701                 }
13702
13703               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13704                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13705               fixp->fx_file = fragp->fr_file;
13706               fixp->fx_line = fragp->fr_line;
13707
13708               md_number_to_chars ((char*)buf, insn, 4);
13709               buf += 4;
13710
13711               if (mips_opts.isa == ISA_MIPS1)
13712                 {
13713                   /* nop */
13714                   md_number_to_chars ((char*)buf, 0, 4);
13715                   buf += 4;
13716                 }
13717
13718               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13719               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13720
13721               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13722                                   4, &exp, 0, BFD_RELOC_LO16);
13723               fixp->fx_file = fragp->fr_file;
13724               fixp->fx_line = fragp->fr_line;
13725
13726               md_number_to_chars ((char*)buf, insn, 4);
13727               buf += 4;
13728
13729               /* j(al)r $at.  */
13730               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13731                 insn = 0x0020f809;
13732               else
13733                 insn = 0x00200008;
13734
13735               md_number_to_chars ((char*)buf, insn, 4);
13736               buf += 4;
13737             }
13738         }
13739
13740       assert (buf == (bfd_byte *)fragp->fr_literal
13741               + fragp->fr_fix + fragp->fr_var);
13742
13743       fragp->fr_fix += fragp->fr_var;
13744
13745       return;
13746     }
13747
13748   if (RELAX_MIPS16_P (fragp->fr_subtype))
13749     {
13750       int type;
13751       register const struct mips16_immed_operand *op;
13752       bfd_boolean small, ext;
13753       offsetT val;
13754       bfd_byte *buf;
13755       unsigned long insn;
13756       bfd_boolean use_extend;
13757       unsigned short extend;
13758
13759       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13760       op = mips16_immed_operands;
13761       while (op->type != type)
13762         ++op;
13763
13764       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13765         {
13766           small = FALSE;
13767           ext = TRUE;
13768         }
13769       else
13770         {
13771           small = TRUE;
13772           ext = FALSE;
13773         }
13774
13775       resolve_symbol_value (fragp->fr_symbol);
13776       val = S_GET_VALUE (fragp->fr_symbol);
13777       if (op->pcrel)
13778         {
13779           addressT addr;
13780
13781           addr = fragp->fr_address + fragp->fr_fix;
13782
13783           /* The rules for the base address of a PC relative reloc are
13784              complicated; see mips16_extended_frag.  */
13785           if (type == 'p' || type == 'q')
13786             {
13787               addr += 2;
13788               if (ext)
13789                 addr += 2;
13790               /* Ignore the low bit in the target, since it will be
13791                  set for a text label.  */
13792               if ((val & 1) != 0)
13793                 --val;
13794             }
13795           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13796             addr -= 4;
13797           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13798             addr -= 2;
13799
13800           addr &= ~ (addressT) ((1 << op->shift) - 1);
13801           val -= addr;
13802
13803           /* Make sure the section winds up with the alignment we have
13804              assumed.  */
13805           if (op->shift > 0)
13806             record_alignment (asec, op->shift);
13807         }
13808
13809       if (ext
13810           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13811               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13812         as_warn_where (fragp->fr_file, fragp->fr_line,
13813                        _("extended instruction in delay slot"));
13814
13815       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13816
13817       if (target_big_endian)
13818         insn = bfd_getb16 (buf);
13819       else
13820         insn = bfd_getl16 (buf);
13821
13822       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13823                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13824                     small, ext, &insn, &use_extend, &extend);
13825
13826       if (use_extend)
13827         {
13828           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13829           fragp->fr_fix += 2;
13830           buf += 2;
13831         }
13832
13833       md_number_to_chars ((char *) buf, insn, 2);
13834       fragp->fr_fix += 2;
13835       buf += 2;
13836     }
13837   else
13838     {
13839       if (fragp->fr_opcode == NULL)
13840         return;
13841
13842       old = RELAX_OLD (fragp->fr_subtype);
13843       new = RELAX_NEW (fragp->fr_subtype);
13844       fixptr = fragp->fr_literal + fragp->fr_fix;
13845
13846       if (new > 0)
13847         memcpy (fixptr - old, fixptr, new);
13848
13849       fragp->fr_fix += new - old;
13850     }
13851 }
13852
13853 #ifdef OBJ_ELF
13854
13855 /* This function is called after the relocs have been generated.
13856    We've been storing mips16 text labels as odd.  Here we convert them
13857    back to even for the convenience of the debugger.  */
13858
13859 void
13860 mips_frob_file_after_relocs ()
13861 {
13862   asymbol **syms;
13863   unsigned int count, i;
13864
13865   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13866     return;
13867
13868   syms = bfd_get_outsymbols (stdoutput);
13869   count = bfd_get_symcount (stdoutput);
13870   for (i = 0; i < count; i++, syms++)
13871     {
13872       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13873           && ((*syms)->value & 1) != 0)
13874         {
13875           (*syms)->value &= ~1;
13876           /* If the symbol has an odd size, it was probably computed
13877              incorrectly, so adjust that as well.  */
13878           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13879             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13880         }
13881     }
13882 }
13883
13884 #endif
13885
13886 /* This function is called whenever a label is defined.  It is used
13887    when handling branch delays; if a branch has a label, we assume we
13888    can not move it.  */
13889
13890 void
13891 mips_define_label (sym)
13892      symbolS *sym;
13893 {
13894   struct insn_label_list *l;
13895
13896   if (free_insn_labels == NULL)
13897     l = (struct insn_label_list *) xmalloc (sizeof *l);
13898   else
13899     {
13900       l = free_insn_labels;
13901       free_insn_labels = l->next;
13902     }
13903
13904   l->label = sym;
13905   l->next = insn_labels;
13906   insn_labels = l;
13907 }
13908 \f
13909 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13910
13911 /* Some special processing for a MIPS ELF file.  */
13912
13913 void
13914 mips_elf_final_processing ()
13915 {
13916   /* Write out the register information.  */
13917   if (mips_abi != N64_ABI)
13918     {
13919       Elf32_RegInfo s;
13920
13921       s.ri_gprmask = mips_gprmask;
13922       s.ri_cprmask[0] = mips_cprmask[0];
13923       s.ri_cprmask[1] = mips_cprmask[1];
13924       s.ri_cprmask[2] = mips_cprmask[2];
13925       s.ri_cprmask[3] = mips_cprmask[3];
13926       /* The gp_value field is set by the MIPS ELF backend.  */
13927
13928       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13929                                        ((Elf32_External_RegInfo *)
13930                                         mips_regmask_frag));
13931     }
13932   else
13933     {
13934       Elf64_Internal_RegInfo s;
13935
13936       s.ri_gprmask = mips_gprmask;
13937       s.ri_pad = 0;
13938       s.ri_cprmask[0] = mips_cprmask[0];
13939       s.ri_cprmask[1] = mips_cprmask[1];
13940       s.ri_cprmask[2] = mips_cprmask[2];
13941       s.ri_cprmask[3] = mips_cprmask[3];
13942       /* The gp_value field is set by the MIPS ELF backend.  */
13943
13944       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13945                                        ((Elf64_External_RegInfo *)
13946                                         mips_regmask_frag));
13947     }
13948
13949   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13950      sort of BFD interface for this.  */
13951   if (mips_any_noreorder)
13952     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13953   if (mips_pic != NO_PIC)
13954     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13955
13956   /* Set MIPS ELF flags for ASEs.  */
13957   if (file_ase_mips16)
13958     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13959 #if 0 /* XXX FIXME */
13960   if (file_ase_mips3d)
13961     elf_elfheader (stdoutput)->e_flags |= ???;
13962 #endif
13963   if (file_ase_mdmx)
13964     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13965
13966   /* Set the MIPS ELF ABI flags.  */
13967   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13968     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13969   else if (mips_abi == O64_ABI)
13970     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13971   else if (mips_abi == EABI_ABI)
13972     {
13973       if (!file_mips_gp32)
13974         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13975       else
13976         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13977     }
13978   else if (mips_abi == N32_ABI)
13979     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13980
13981   /* Nothing to do for N64_ABI.  */
13982
13983   if (mips_32bitmode)
13984     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13985 }
13986
13987 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13988 \f
13989 typedef struct proc {
13990   symbolS *isym;
13991   unsigned long reg_mask;
13992   unsigned long reg_offset;
13993   unsigned long fpreg_mask;
13994   unsigned long fpreg_offset;
13995   unsigned long frame_offset;
13996   unsigned long frame_reg;
13997   unsigned long pc_reg;
13998 } procS;
13999
14000 static procS cur_proc;
14001 static procS *cur_proc_ptr;
14002 static int numprocs;
14003
14004 /* Fill in an rs_align_code fragment.  */
14005
14006 void
14007 mips_handle_align (fragp)
14008      fragS *fragp;
14009 {
14010   if (fragp->fr_type != rs_align_code)
14011     return;
14012
14013   if (mips_opts.mips16)
14014     {
14015       static const unsigned char be_nop[] = { 0x65, 0x00 };
14016       static const unsigned char le_nop[] = { 0x00, 0x65 };
14017
14018       int bytes;
14019       char *p;
14020
14021       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14022       p = fragp->fr_literal + fragp->fr_fix;
14023
14024       if (bytes & 1)
14025         {
14026           *p++ = 0;
14027           fragp->fr_fix++;
14028         }
14029
14030       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14031       fragp->fr_var = 2;
14032     }
14033
14034   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14035 }
14036
14037 static void
14038 md_obj_begin ()
14039 {
14040 }
14041
14042 static void
14043 md_obj_end ()
14044 {
14045   /* check for premature end, nesting errors, etc */
14046   if (cur_proc_ptr)
14047     as_warn (_("missing .end at end of assembly"));
14048 }
14049
14050 static long
14051 get_number ()
14052 {
14053   int negative = 0;
14054   long val = 0;
14055
14056   if (*input_line_pointer == '-')
14057     {
14058       ++input_line_pointer;
14059       negative = 1;
14060     }
14061   if (!ISDIGIT (*input_line_pointer))
14062     as_bad (_("expected simple number"));
14063   if (input_line_pointer[0] == '0')
14064     {
14065       if (input_line_pointer[1] == 'x')
14066         {
14067           input_line_pointer += 2;
14068           while (ISXDIGIT (*input_line_pointer))
14069             {
14070               val <<= 4;
14071               val |= hex_value (*input_line_pointer++);
14072             }
14073           return negative ? -val : val;
14074         }
14075       else
14076         {
14077           ++input_line_pointer;
14078           while (ISDIGIT (*input_line_pointer))
14079             {
14080               val <<= 3;
14081               val |= *input_line_pointer++ - '0';
14082             }
14083           return negative ? -val : val;
14084         }
14085     }
14086   if (!ISDIGIT (*input_line_pointer))
14087     {
14088       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14089               *input_line_pointer, *input_line_pointer);
14090       as_warn (_("invalid number"));
14091       return -1;
14092     }
14093   while (ISDIGIT (*input_line_pointer))
14094     {
14095       val *= 10;
14096       val += *input_line_pointer++ - '0';
14097     }
14098   return negative ? -val : val;
14099 }
14100
14101 /* The .file directive; just like the usual .file directive, but there
14102    is an initial number which is the ECOFF file index.  In the non-ECOFF
14103    case .file implies DWARF-2.  */
14104
14105 static void
14106 s_mips_file (x)
14107      int x ATTRIBUTE_UNUSED;
14108 {
14109   static int first_file_directive = 0;
14110
14111   if (ECOFF_DEBUGGING)
14112     {
14113       get_number ();
14114       s_app_file (0);
14115     }
14116   else
14117     {
14118       char *filename;
14119
14120       filename = dwarf2_directive_file (0);
14121
14122       /* Versions of GCC up to 3.1 start files with a ".file"
14123          directive even for stabs output.  Make sure that this
14124          ".file" is handled.  Note that you need a version of GCC
14125          after 3.1 in order to support DWARF-2 on MIPS.  */
14126       if (filename != NULL && ! first_file_directive)
14127         {
14128           (void) new_logical_line (filename, -1);
14129           s_app_file_string (filename);
14130         }
14131       first_file_directive = 1;
14132     }
14133 }
14134
14135 /* The .loc directive, implying DWARF-2.  */
14136
14137 static void
14138 s_mips_loc (x)
14139      int x ATTRIBUTE_UNUSED;
14140 {
14141   if (!ECOFF_DEBUGGING)
14142     dwarf2_directive_loc (0);
14143 }
14144
14145 /* The .end directive.  */
14146
14147 static void
14148 s_mips_end (x)
14149      int x ATTRIBUTE_UNUSED;
14150 {
14151   symbolS *p;
14152
14153   /* Following functions need their own .frame and .cprestore directives.  */
14154   mips_frame_reg_valid = 0;
14155   mips_cprestore_valid = 0;
14156
14157   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14158     {
14159       p = get_symbol ();
14160       demand_empty_rest_of_line ();
14161     }
14162   else
14163     p = NULL;
14164
14165   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14166     as_warn (_(".end not in text section"));
14167
14168   if (!cur_proc_ptr)
14169     {
14170       as_warn (_(".end directive without a preceding .ent directive."));
14171       demand_empty_rest_of_line ();
14172       return;
14173     }
14174
14175   if (p != NULL)
14176     {
14177       assert (S_GET_NAME (p));
14178       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14179         as_warn (_(".end symbol does not match .ent symbol."));
14180
14181       if (debug_type == DEBUG_STABS)
14182         stabs_generate_asm_endfunc (S_GET_NAME (p),
14183                                     S_GET_NAME (p));
14184     }
14185   else
14186     as_warn (_(".end directive missing or unknown symbol"));
14187
14188 #ifdef OBJ_ELF
14189   /* Generate a .pdr section.  */
14190   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14191     {
14192       segT saved_seg = now_seg;
14193       subsegT saved_subseg = now_subseg;
14194       valueT dot;
14195       expressionS exp;
14196       char *fragp;
14197
14198       dot = frag_now_fix ();
14199
14200 #ifdef md_flush_pending_output
14201       md_flush_pending_output ();
14202 #endif
14203
14204       assert (pdr_seg);
14205       subseg_set (pdr_seg, 0);
14206
14207       /* Write the symbol.  */
14208       exp.X_op = O_symbol;
14209       exp.X_add_symbol = p;
14210       exp.X_add_number = 0;
14211       emit_expr (&exp, 4);
14212
14213       fragp = frag_more (7 * 4);
14214
14215       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14216       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14217       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14218       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14219       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14220       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14221       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14222
14223       subseg_set (saved_seg, saved_subseg);
14224     }
14225 #endif /* OBJ_ELF */
14226
14227   cur_proc_ptr = NULL;
14228 }
14229
14230 /* The .aent and .ent directives.  */
14231
14232 static void
14233 s_mips_ent (aent)
14234      int aent;
14235 {
14236   symbolS *symbolP;
14237
14238   symbolP = get_symbol ();
14239   if (*input_line_pointer == ',')
14240     ++input_line_pointer;
14241   SKIP_WHITESPACE ();
14242   if (ISDIGIT (*input_line_pointer)
14243       || *input_line_pointer == '-')
14244     get_number ();
14245
14246   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14247     as_warn (_(".ent or .aent not in text section."));
14248
14249   if (!aent && cur_proc_ptr)
14250     as_warn (_("missing .end"));
14251
14252   if (!aent)
14253     {
14254       /* This function needs its own .frame and .cprestore directives.  */
14255       mips_frame_reg_valid = 0;
14256       mips_cprestore_valid = 0;
14257
14258       cur_proc_ptr = &cur_proc;
14259       memset (cur_proc_ptr, '\0', sizeof (procS));
14260
14261       cur_proc_ptr->isym = symbolP;
14262
14263       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14264
14265       ++numprocs;
14266
14267       if (debug_type == DEBUG_STABS)
14268         stabs_generate_asm_func (S_GET_NAME (symbolP),
14269                                  S_GET_NAME (symbolP));
14270     }
14271
14272   demand_empty_rest_of_line ();
14273 }
14274
14275 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14276    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14277    s_mips_frame is used so that we can set the PDR information correctly.
14278    We can't use the ecoff routines because they make reference to the ecoff
14279    symbol table (in the mdebug section).  */
14280
14281 static void
14282 s_mips_frame (ignore)
14283      int ignore ATTRIBUTE_UNUSED;
14284 {
14285 #ifdef OBJ_ELF
14286   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14287     {
14288       long val;
14289
14290       if (cur_proc_ptr == (procS *) NULL)
14291         {
14292           as_warn (_(".frame outside of .ent"));
14293           demand_empty_rest_of_line ();
14294           return;
14295         }
14296
14297       cur_proc_ptr->frame_reg = tc_get_register (1);
14298
14299       SKIP_WHITESPACE ();
14300       if (*input_line_pointer++ != ','
14301           || get_absolute_expression_and_terminator (&val) != ',')
14302         {
14303           as_warn (_("Bad .frame directive"));
14304           --input_line_pointer;
14305           demand_empty_rest_of_line ();
14306           return;
14307         }
14308
14309       cur_proc_ptr->frame_offset = val;
14310       cur_proc_ptr->pc_reg = tc_get_register (0);
14311
14312       demand_empty_rest_of_line ();
14313     }
14314   else
14315 #endif /* OBJ_ELF */
14316     s_ignore (ignore);
14317 }
14318
14319 /* The .fmask and .mask directives. If the mdebug section is present
14320    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14321    embedded targets, s_mips_mask is used so that we can set the PDR
14322    information correctly. We can't use the ecoff routines because they
14323    make reference to the ecoff symbol table (in the mdebug section).  */
14324
14325 static void
14326 s_mips_mask (reg_type)
14327      char reg_type;
14328 {
14329 #ifdef OBJ_ELF
14330   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14331     {
14332       long mask, off;
14333
14334       if (cur_proc_ptr == (procS *) NULL)
14335         {
14336           as_warn (_(".mask/.fmask outside of .ent"));
14337           demand_empty_rest_of_line ();
14338           return;
14339         }
14340
14341       if (get_absolute_expression_and_terminator (&mask) != ',')
14342         {
14343           as_warn (_("Bad .mask/.fmask directive"));
14344           --input_line_pointer;
14345           demand_empty_rest_of_line ();
14346           return;
14347         }
14348
14349       off = get_absolute_expression ();
14350
14351       if (reg_type == 'F')
14352         {
14353           cur_proc_ptr->fpreg_mask = mask;
14354           cur_proc_ptr->fpreg_offset = off;
14355         }
14356       else
14357         {
14358           cur_proc_ptr->reg_mask = mask;
14359           cur_proc_ptr->reg_offset = off;
14360         }
14361
14362       demand_empty_rest_of_line ();
14363     }
14364   else
14365 #endif /* OBJ_ELF */
14366     s_ignore (reg_type);
14367 }
14368
14369 /* The .loc directive.  */
14370
14371 #if 0
14372 static void
14373 s_loc (x)
14374      int x;
14375 {
14376   symbolS *symbolP;
14377   int lineno;
14378   int addroff;
14379
14380   assert (now_seg == text_section);
14381
14382   lineno = get_number ();
14383   addroff = frag_now_fix ();
14384
14385   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14386   S_SET_TYPE (symbolP, N_SLINE);
14387   S_SET_OTHER (symbolP, 0);
14388   S_SET_DESC (symbolP, lineno);
14389   symbolP->sy_segment = now_seg;
14390 }
14391 #endif
14392
14393 /* A table describing all the processors gas knows about.  Names are
14394    matched in the order listed.
14395
14396    To ease comparison, please keep this table in the same order as
14397    gcc's mips_cpu_info_table[].  */
14398 static const struct mips_cpu_info mips_cpu_info_table[] =
14399 {
14400   /* Entries for generic ISAs */
14401   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14402   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14403   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14404   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14405   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14406   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14407   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14408   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14409
14410   /* MIPS I */
14411   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14412   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14413   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14414
14415   /* MIPS II */
14416   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14417
14418   /* MIPS III */
14419   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14420   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14421   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14422   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14423   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14424   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14425   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14426   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14427   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14428   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14429   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14430   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14431
14432   /* MIPS IV */
14433   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14434   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14435   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14436   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14437   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14438   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14439   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14440   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14441   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14442   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14443   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14444   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14445
14446   /* MIPS 32 */
14447   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14448   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14449   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14450
14451   /* MIPS 64 */
14452   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14453   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14454
14455   /* Broadcom SB-1 CPU core */
14456   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14457
14458   /* End marker */
14459   { NULL, 0, 0, 0 }
14460 };
14461
14462
14463 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14464    with a final "000" replaced by "k".  Ignore case.
14465
14466    Note: this function is shared between GCC and GAS.  */
14467
14468 static bfd_boolean
14469 mips_strict_matching_cpu_name_p (canonical, given)
14470      const char *canonical, *given;
14471 {
14472   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14473     given++, canonical++;
14474
14475   return ((*given == 0 && *canonical == 0)
14476           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14477 }
14478
14479
14480 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14481    CPU name.  We've traditionally allowed a lot of variation here.
14482
14483    Note: this function is shared between GCC and GAS.  */
14484
14485 static bfd_boolean
14486 mips_matching_cpu_name_p (canonical, given)
14487      const char *canonical, *given;
14488 {
14489   /* First see if the name matches exactly, or with a final "000"
14490      turned into "k".  */
14491   if (mips_strict_matching_cpu_name_p (canonical, given))
14492     return TRUE;
14493
14494   /* If not, try comparing based on numerical designation alone.
14495      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14496   if (TOLOWER (*given) == 'r')
14497     given++;
14498   if (!ISDIGIT (*given))
14499     return FALSE;
14500
14501   /* Skip over some well-known prefixes in the canonical name,
14502      hoping to find a number there too.  */
14503   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14504     canonical += 2;
14505   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14506     canonical += 2;
14507   else if (TOLOWER (canonical[0]) == 'r')
14508     canonical += 1;
14509
14510   return mips_strict_matching_cpu_name_p (canonical, given);
14511 }
14512
14513
14514 /* Parse an option that takes the name of a processor as its argument.
14515    OPTION is the name of the option and CPU_STRING is the argument.
14516    Return the corresponding processor enumeration if the CPU_STRING is
14517    recognized, otherwise report an error and return null.
14518
14519    A similar function exists in GCC.  */
14520
14521 static const struct mips_cpu_info *
14522 mips_parse_cpu (option, cpu_string)
14523      const char *option, *cpu_string;
14524 {
14525   const struct mips_cpu_info *p;
14526
14527   /* 'from-abi' selects the most compatible architecture for the given
14528      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14529      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14530      version.  Look first at the -mgp options, if given, otherwise base
14531      the choice on MIPS_DEFAULT_64BIT.
14532
14533      Treat NO_ABI like the EABIs.  One reason to do this is that the
14534      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14535      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14536      'mips64', just as we did in the days before 'from-abi'.  */
14537   if (strcasecmp (cpu_string, "from-abi") == 0)
14538     {
14539       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14540         return mips_cpu_info_from_isa (ISA_MIPS1);
14541
14542       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14543         return mips_cpu_info_from_isa (ISA_MIPS3);
14544
14545       if (file_mips_gp32 >= 0)
14546         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14547
14548       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14549                                      ? ISA_MIPS3
14550                                      : ISA_MIPS1);
14551     }
14552
14553   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14554   if (strcasecmp (cpu_string, "default") == 0)
14555     return 0;
14556
14557   for (p = mips_cpu_info_table; p->name != 0; p++)
14558     if (mips_matching_cpu_name_p (p->name, cpu_string))
14559       return p;
14560
14561   as_bad ("Bad value (%s) for %s", cpu_string, option);
14562   return 0;
14563 }
14564
14565 /* Return the canonical processor information for ISA (a member of the
14566    ISA_MIPS* enumeration).  */
14567
14568 static const struct mips_cpu_info *
14569 mips_cpu_info_from_isa (isa)
14570      int isa;
14571 {
14572   int i;
14573
14574   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14575     if (mips_cpu_info_table[i].is_isa
14576         && isa == mips_cpu_info_table[i].isa)
14577       return (&mips_cpu_info_table[i]);
14578
14579   return NULL;
14580 }
14581 \f
14582 static void
14583 show (stream, string, col_p, first_p)
14584      FILE *stream;
14585      const char *string;
14586      int *col_p;
14587      int *first_p;
14588 {
14589   if (*first_p)
14590     {
14591       fprintf (stream, "%24s", "");
14592       *col_p = 24;
14593     }
14594   else
14595     {
14596       fprintf (stream, ", ");
14597       *col_p += 2;
14598     }
14599
14600   if (*col_p + strlen (string) > 72)
14601     {
14602       fprintf (stream, "\n%24s", "");
14603       *col_p = 24;
14604     }
14605
14606   fprintf (stream, "%s", string);
14607   *col_p += strlen (string);
14608
14609   *first_p = 0;
14610 }
14611
14612 void
14613 md_show_usage (stream)
14614      FILE *stream;
14615 {
14616   int column, first;
14617   size_t i;
14618
14619   fprintf (stream, _("\
14620 MIPS options:\n\
14621 -membedded-pic          generate embedded position independent code\n\
14622 -EB                     generate big endian output\n\
14623 -EL                     generate little endian output\n\
14624 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14625 -G NUM                  allow referencing objects up to NUM bytes\n\
14626                         implicitly with the gp register [default 8]\n"));
14627   fprintf (stream, _("\
14628 -mips1                  generate MIPS ISA I instructions\n\
14629 -mips2                  generate MIPS ISA II instructions\n\
14630 -mips3                  generate MIPS ISA III instructions\n\
14631 -mips4                  generate MIPS ISA IV instructions\n\
14632 -mips5                  generate MIPS ISA V instructions\n\
14633 -mips32                 generate MIPS32 ISA instructions\n\
14634 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14635 -mips64                 generate MIPS64 ISA instructions\n\
14636 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14637
14638   first = 1;
14639
14640   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14641     show (stream, mips_cpu_info_table[i].name, &column, &first);
14642   show (stream, "from-abi", &column, &first);
14643   fputc ('\n', stream);
14644
14645   fprintf (stream, _("\
14646 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14647 -no-mCPU                don't generate code specific to CPU.\n\
14648                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14649
14650   first = 1;
14651
14652   show (stream, "3900", &column, &first);
14653   show (stream, "4010", &column, &first);
14654   show (stream, "4100", &column, &first);
14655   show (stream, "4650", &column, &first);
14656   fputc ('\n', stream);
14657
14658   fprintf (stream, _("\
14659 -mips16                 generate mips16 instructions\n\
14660 -no-mips16              do not generate mips16 instructions\n"));
14661   fprintf (stream, _("\
14662 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14663 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14664 -O0                     remove unneeded NOPs, do not swap branches\n\
14665 -O                      remove unneeded NOPs and swap branches\n\
14666 -n                      warn about NOPs generated from macros\n\
14667 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14668 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14669 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14670 #ifdef OBJ_ELF
14671   fprintf (stream, _("\
14672 -KPIC, -call_shared     generate SVR4 position independent code\n\
14673 -non_shared             do not generate position independent code\n\
14674 -xgot                   assume a 32 bit GOT\n\
14675 -mabi=ABI               create ABI conformant object file for:\n"));
14676
14677   first = 1;
14678
14679   show (stream, "32", &column, &first);
14680   show (stream, "o64", &column, &first);
14681   show (stream, "n32", &column, &first);
14682   show (stream, "64", &column, &first);
14683   show (stream, "eabi", &column, &first);
14684
14685   fputc ('\n', stream);
14686
14687   fprintf (stream, _("\
14688 -32                     create o32 ABI object file (default)\n\
14689 -n32                    create n32 ABI object file\n\
14690 -64                     create 64 ABI object file\n"));
14691 #endif
14692 }
14693
14694 enum dwarf2_format
14695 mips_dwarf2_format ()
14696 {
14697   if (mips_abi == N64_ABI)
14698     {
14699 #ifdef TE_IRIX
14700       return dwarf2_format_64bit_irix;
14701 #else
14702       return dwarf2_format_64bit;
14703 #endif
14704     }
14705   else
14706     return dwarf2_format_32bit;
14707 }