* config/tc-mips.c (macro): Add comments explaining the rationale
[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 #define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
298                                       || HAVE_64BIT_GPRS)
299
300 /* Return true if the given CPU supports the MIPS16 ASE.  */
301 #define CPU_HAS_MIPS16(cpu)                                             \
302    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
303     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
304
305 /* Return true if the given CPU supports the MIPS3D ASE.  */
306 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
307                                  )
308
309 /* Return true if the given CPU supports the MDMX ASE.  */
310 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
311                                  )
312
313 /* True if CPU has a dror instruction.  */
314 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
315
316 /* True if CPU has a ror instruction.  */
317 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
318
319 /* Whether the processor uses hardware interlocks to protect
320    reads from the HI and LO registers, and thus does not
321    require nops to be inserted.  */
322
323 #define hilo_interlocks (mips_arch == CPU_R4010                       \
324                          || mips_arch == CPU_VR5500                   \
325                          || mips_arch == CPU_SB1                      \
326                          )
327
328 /* Whether the processor uses hardware interlocks to protect reads
329    from the GPRs, and thus does not require nops to be inserted.  */
330 #define gpr_interlocks \
331   (mips_opts.isa != ISA_MIPS1  \
332    || mips_arch == CPU_VR5400  \
333    || mips_arch == CPU_VR5500  \
334    || mips_arch == CPU_R3900)
335
336 /* As with other "interlocks" this is used by hardware that has FP
337    (co-processor) interlocks.  */
338 /* Itbl support may require additional care here.  */
339 #define cop_interlocks (mips_arch == CPU_R4300                        \
340                         || mips_arch == CPU_VR5400                    \
341                         || mips_arch == CPU_VR5500                    \
342                         || mips_arch == CPU_SB1                       \
343                         )
344
345 /* Is this a mfhi or mflo instruction?  */
346 #define MF_HILO_INSN(PINFO) \
347           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
348
349 /* MIPS PIC level.  */
350
351 enum mips_pic_level mips_pic;
352
353 /* Warn about all NOPS that the assembler generates.  */
354 static int warn_nops = 0;
355
356 /* 1 if we should generate 32 bit offsets from the $gp register in
357    SVR4_PIC mode.  Currently has no meaning in other modes.  */
358 static int mips_big_got = 0;
359
360 /* 1 if trap instructions should used for overflow rather than break
361    instructions.  */
362 static int mips_trap = 0;
363
364 /* 1 if double width floating point constants should not be constructed
365    by assembling two single width halves into two single width floating
366    point registers which just happen to alias the double width destination
367    register.  On some architectures this aliasing can be disabled by a bit
368    in the status register, and the setting of this bit cannot be determined
369    automatically at assemble time.  */
370 static int mips_disable_float_construction;
371
372 /* Non-zero if any .set noreorder directives were used.  */
373
374 static int mips_any_noreorder;
375
376 /* Non-zero if nops should be inserted when the register referenced in
377    an mfhi/mflo instruction is read in the next two instructions.  */
378 static int mips_7000_hilo_fix;
379
380 /* The size of the small data section.  */
381 static unsigned int g_switch_value = 8;
382 /* Whether the -G option was used.  */
383 static int g_switch_seen = 0;
384
385 #define N_RMASK 0xc4
386 #define N_VFP   0xd4
387
388 /* If we can determine in advance that GP optimization won't be
389    possible, we can skip the relaxation stuff that tries to produce
390    GP-relative references.  This makes delay slot optimization work
391    better.
392
393    This function can only provide a guess, but it seems to work for
394    gcc output.  It needs to guess right for gcc, otherwise gcc
395    will put what it thinks is a GP-relative instruction in a branch
396    delay slot.
397
398    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
399    fixed it for the non-PIC mode.  KR 95/04/07  */
400 static int nopic_need_relax PARAMS ((symbolS *, int));
401
402 /* handle of the OPCODE hash table */
403 static struct hash_control *op_hash = NULL;
404
405 /* The opcode hash table we use for the mips16.  */
406 static struct hash_control *mips16_op_hash = NULL;
407
408 /* This array holds the chars that always start a comment.  If the
409     pre-processor is disabled, these aren't very useful */
410 const char comment_chars[] = "#";
411
412 /* This array holds the chars that only start a comment at the beginning of
413    a line.  If the line seems to have the form '# 123 filename'
414    .line and .file directives will appear in the pre-processed output */
415 /* Note that input_file.c hand checks for '#' at the beginning of the
416    first line of the input file.  This is because the compiler outputs
417    #NO_APP at the beginning of its output.  */
418 /* Also note that C style comments are always supported.  */
419 const char line_comment_chars[] = "#";
420
421 /* This array holds machine specific line separator characters.  */
422 const char line_separator_chars[] = ";";
423
424 /* Chars that can be used to separate mant from exp in floating point nums */
425 const char EXP_CHARS[] = "eE";
426
427 /* Chars that mean this number is a floating point constant */
428 /* As in 0f12.456 */
429 /* or    0d1.2345e12 */
430 const char FLT_CHARS[] = "rRsSfFdDxXpP";
431
432 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
433    changed in read.c .  Ideally it shouldn't have to know about it at all,
434    but nothing is ideal around here.
435  */
436
437 static char *insn_error;
438
439 static int auto_align = 1;
440
441 /* When outputting SVR4 PIC code, the assembler needs to know the
442    offset in the stack frame from which to restore the $gp register.
443    This is set by the .cprestore pseudo-op, and saved in this
444    variable.  */
445 static offsetT mips_cprestore_offset = -1;
446
447 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
448    more optimizations, it can use a register value instead of a memory-saved
449    offset and even an other register than $gp as global pointer.  */
450 static offsetT mips_cpreturn_offset = -1;
451 static int mips_cpreturn_register = -1;
452 static int mips_gp_register = GP;
453 static int mips_gprel_offset = 0;
454
455 /* Whether mips_cprestore_offset has been set in the current function
456    (or whether it has already been warned about, if not).  */
457 static int mips_cprestore_valid = 0;
458
459 /* This is the register which holds the stack frame, as set by the
460    .frame pseudo-op.  This is needed to implement .cprestore.  */
461 static int mips_frame_reg = SP;
462
463 /* Whether mips_frame_reg has been set in the current function
464    (or whether it has already been warned about, if not).  */
465 static int mips_frame_reg_valid = 0;
466
467 /* To output NOP instructions correctly, we need to keep information
468    about the previous two instructions.  */
469
470 /* Whether we are optimizing.  The default value of 2 means to remove
471    unneeded NOPs and swap branch instructions when possible.  A value
472    of 1 means to not swap branches.  A value of 0 means to always
473    insert NOPs.  */
474 static int mips_optimize = 2;
475
476 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
477    equivalent to seeing no -g option at all.  */
478 static int mips_debug = 0;
479
480 /* The previous instruction.  */
481 static struct mips_cl_insn prev_insn;
482
483 /* The instruction before prev_insn.  */
484 static struct mips_cl_insn prev_prev_insn;
485
486 /* If we don't want information for prev_insn or prev_prev_insn, we
487    point the insn_mo field at this dummy integer.  */
488 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
489
490 /* Non-zero if prev_insn is valid.  */
491 static int prev_insn_valid;
492
493 /* The frag for the previous instruction.  */
494 static struct frag *prev_insn_frag;
495
496 /* The offset into prev_insn_frag for the previous instruction.  */
497 static long prev_insn_where;
498
499 /* The reloc type for the previous instruction, if any.  */
500 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
501
502 /* The reloc for the previous instruction, if any.  */
503 static fixS *prev_insn_fixp[3];
504
505 /* Non-zero if the previous instruction was in a delay slot.  */
506 static int prev_insn_is_delay_slot;
507
508 /* Non-zero if the previous instruction was in a .set noreorder.  */
509 static int prev_insn_unreordered;
510
511 /* Non-zero if the previous instruction uses an extend opcode (if
512    mips16).  */
513 static int prev_insn_extended;
514
515 /* Non-zero if the previous previous instruction was in a .set
516    noreorder.  */
517 static int prev_prev_insn_unreordered;
518
519 /* If this is set, it points to a frag holding nop instructions which
520    were inserted before the start of a noreorder section.  If those
521    nops turn out to be unnecessary, the size of the frag can be
522    decreased.  */
523 static fragS *prev_nop_frag;
524
525 /* The number of nop instructions we created in prev_nop_frag.  */
526 static int prev_nop_frag_holds;
527
528 /* The number of nop instructions that we know we need in
529    prev_nop_frag.  */
530 static int prev_nop_frag_required;
531
532 /* The number of instructions we've seen since prev_nop_frag.  */
533 static int prev_nop_frag_since;
534
535 /* For ECOFF and ELF, relocations against symbols are done in two
536    parts, with a HI relocation and a LO relocation.  Each relocation
537    has only 16 bits of space to store an addend.  This means that in
538    order for the linker to handle carries correctly, it must be able
539    to locate both the HI and the LO relocation.  This means that the
540    relocations must appear in order in the relocation table.
541
542    In order to implement this, we keep track of each unmatched HI
543    relocation.  We then sort them so that they immediately precede the
544    corresponding LO relocation.  */
545
546 struct mips_hi_fixup
547 {
548   /* Next HI fixup.  */
549   struct mips_hi_fixup *next;
550   /* This fixup.  */
551   fixS *fixp;
552   /* The section this fixup is in.  */
553   segT seg;
554 };
555
556 /* The list of unmatched HI relocs.  */
557
558 static struct mips_hi_fixup *mips_hi_fixup_list;
559
560 /* The frag containing the last explicit relocation operator.
561    Null if explicit relocations have not been used.  */
562
563 static fragS *prev_reloc_op_frag;
564
565 /* Map normal MIPS register numbers to mips16 register numbers.  */
566
567 #define X ILLEGAL_REG
568 static const int mips32_to_16_reg_map[] =
569 {
570   X, X, 2, 3, 4, 5, 6, 7,
571   X, X, X, X, X, X, X, X,
572   0, 1, X, X, X, X, X, X,
573   X, X, X, X, X, X, X, X
574 };
575 #undef X
576
577 /* Map mips16 register numbers to normal MIPS register numbers.  */
578
579 static const unsigned int mips16_to_32_reg_map[] =
580 {
581   16, 17, 2, 3, 4, 5, 6, 7
582 };
583
584 static int mips_fix_4122_bugs;
585
586 /* We don't relax branches by default, since this causes us to expand
587    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
588    fail to compute the offset before expanding the macro to the most
589    efficient expansion.  */
590
591 static int mips_relax_branch;
592 \f
593 /* Since the MIPS does not have multiple forms of PC relative
594    instructions, we do not have to do relaxing as is done on other
595    platforms.  However, we do have to handle GP relative addressing
596    correctly, which turns out to be a similar problem.
597
598    Every macro that refers to a symbol can occur in (at least) two
599    forms, one with GP relative addressing and one without.  For
600    example, loading a global variable into a register generally uses
601    a macro instruction like this:
602      lw $4,i
603    If i can be addressed off the GP register (this is true if it is in
604    the .sbss or .sdata section, or if it is known to be smaller than
605    the -G argument) this will generate the following instruction:
606      lw $4,i($gp)
607    This instruction will use a GPREL reloc.  If i can not be addressed
608    off the GP register, the following instruction sequence will be used:
609      lui $at,i
610      lw $4,i($at)
611    In this case the first instruction will have a HI16 reloc, and the
612    second reloc will have a LO16 reloc.  Both relocs will be against
613    the symbol i.
614
615    The issue here is that we may not know whether i is GP addressable
616    until after we see the instruction that uses it.  Therefore, we
617    want to be able to choose the final instruction sequence only at
618    the end of the assembly.  This is similar to the way other
619    platforms choose the size of a PC relative instruction only at the
620    end of assembly.
621
622    When generating position independent code we do not use GP
623    addressing in quite the same way, but the issue still arises as
624    external symbols and local symbols must be handled differently.
625
626    We handle these issues by actually generating both possible
627    instruction sequences.  The longer one is put in a frag_var with
628    type rs_machine_dependent.  We encode what to do with the frag in
629    the subtype field.  We encode (1) the number of existing bytes to
630    replace, (2) the number of new bytes to use, (3) the offset from
631    the start of the existing bytes to the first reloc we must generate
632    (that is, the offset is applied from the start of the existing
633    bytes after they are replaced by the new bytes, if any), (4) the
634    offset from the start of the existing bytes to the second reloc,
635    (5) whether a third reloc is needed (the third reloc is always four
636    bytes after the second reloc), and (6) whether to warn if this
637    variant is used (this is sometimes needed if .set nomacro or .set
638    noat is in effect).  All these numbers are reasonably small.
639
640    Generating two instruction sequences must be handled carefully to
641    ensure that delay slots are handled correctly.  Fortunately, there
642    are a limited number of cases.  When the second instruction
643    sequence is generated, append_insn is directed to maintain the
644    existing delay slot information, so it continues to apply to any
645    code after the second instruction sequence.  This means that the
646    second instruction sequence must not impose any requirements not
647    required by the first instruction sequence.
648
649    These variant frags are then handled in functions called by the
650    machine independent code.  md_estimate_size_before_relax returns
651    the final size of the frag.  md_convert_frag sets up the final form
652    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
653    one if needed.  */
654 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
655   ((relax_substateT) \
656    (((old) << 23) \
657     | ((new) << 16) \
658     | (((reloc1) + 64) << 9) \
659     | (((reloc2) + 64) << 2) \
660     | ((reloc3) ? (1 << 1) : 0) \
661     | ((warn) ? 1 : 0)))
662 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
663 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
664 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
665 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
666 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
667 #define RELAX_WARN(i) ((i) & 1)
668
669 /* Branch without likely bit.  If label is out of range, we turn:
670
671         beq reg1, reg2, label
672         delay slot
673
674    into
675
676         bne reg1, reg2, 0f
677         nop
678         j label
679      0: delay slot
680
681    with the following opcode replacements:
682
683         beq <-> bne
684         blez <-> bgtz
685         bltz <-> bgez
686         bc1f <-> bc1t
687
688         bltzal <-> bgezal  (with jal label instead of j label)
689
690    Even though keeping the delay slot instruction in the delay slot of
691    the branch would be more efficient, it would be very tricky to do
692    correctly, because we'd have to introduce a variable frag *after*
693    the delay slot instruction, and expand that instead.  Let's do it
694    the easy way for now, even if the branch-not-taken case now costs
695    one additional instruction.  Out-of-range branches are not supposed
696    to be common, anyway.
697
698    Branch likely.  If label is out of range, we turn:
699
700         beql reg1, reg2, label
701         delay slot (annulled if branch not taken)
702
703    into
704
705         beql reg1, reg2, 1f
706         nop
707         beql $0, $0, 2f
708         nop
709      1: j[al] label
710         delay slot (executed only if branch taken)
711      2:
712
713    It would be possible to generate a shorter sequence by losing the
714    likely bit, generating something like:
715
716         bne reg1, reg2, 0f
717         nop
718         j[al] label
719         delay slot (executed only if branch taken)
720      0:
721
722         beql -> bne
723         bnel -> beq
724         blezl -> bgtz
725         bgtzl -> blez
726         bltzl -> bgez
727         bgezl -> bltz
728         bc1fl -> bc1t
729         bc1tl -> bc1f
730
731         bltzall -> bgezal  (with jal label instead of j label)
732         bgezall -> bltzal  (ditto)
733
734
735    but it's not clear that it would actually improve performance.  */
736 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
737   ((relax_substateT) \
738    (0xc0000000 \
739     | ((toofar) ? 1 : 0) \
740     | ((link) ? 2 : 0) \
741     | ((likely) ? 4 : 0) \
742     | ((uncond) ? 8 : 0)))
743 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
744 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
745 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
746 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
747 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
748
749 /* For mips16 code, we use an entirely different form of relaxation.
750    mips16 supports two versions of most instructions which take
751    immediate values: a small one which takes some small value, and a
752    larger one which takes a 16 bit value.  Since branches also follow
753    this pattern, relaxing these values is required.
754
755    We can assemble both mips16 and normal MIPS code in a single
756    object.  Therefore, we need to support this type of relaxation at
757    the same time that we support the relaxation described above.  We
758    use the high bit of the subtype field to distinguish these cases.
759
760    The information we store for this type of relaxation is the
761    argument code found in the opcode file for this relocation, whether
762    the user explicitly requested a small or extended form, and whether
763    the relocation is in a jump or jal delay slot.  That tells us the
764    size of the value, and how it should be stored.  We also store
765    whether the fragment is considered to be extended or not.  We also
766    store whether this is known to be a branch to a different section,
767    whether we have tried to relax this frag yet, and whether we have
768    ever extended a PC relative fragment because of a shift count.  */
769 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
770   (0x80000000                                                   \
771    | ((type) & 0xff)                                            \
772    | ((small) ? 0x100 : 0)                                      \
773    | ((ext) ? 0x200 : 0)                                        \
774    | ((dslot) ? 0x400 : 0)                                      \
775    | ((jal_dslot) ? 0x800 : 0))
776 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
777 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
778 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
779 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
780 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
781 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
782 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
783 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
784 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
785 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
786 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
787 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
788
789 /* Is the given value a sign-extended 32-bit value?  */
790 #define IS_SEXT_32BIT_NUM(x)                                            \
791   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
792    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
793
794 /* Is the given value a sign-extended 16-bit value?  */
795 #define IS_SEXT_16BIT_NUM(x)                                            \
796   (((x) &~ (offsetT) 0x7fff) == 0                                       \
797    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
798
799 \f
800 /* Prototypes for static functions.  */
801
802 #ifdef __STDC__
803 #define internalError() \
804     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
805 #else
806 #define internalError() as_fatal (_("MIPS internal Error"));
807 #endif
808
809 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
810
811 static inline bfd_boolean reloc_needs_lo_p
812   PARAMS ((bfd_reloc_code_real_type));
813 static inline bfd_boolean fixup_has_matching_lo_p
814   PARAMS ((fixS *));
815 static int insn_uses_reg
816   PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
817            enum mips_regclass class));
818 static int reg_needs_delay
819   PARAMS ((unsigned int));
820 static void mips16_mark_labels
821   PARAMS ((void));
822 static void append_insn
823   PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
824            bfd_reloc_code_real_type *r));
825 static void mips_no_prev_insn
826   PARAMS ((int));
827 static void mips_emit_delays
828   PARAMS ((bfd_boolean));
829 #ifdef USE_STDARG
830 static void macro_build
831   PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
832            const char *fmt, ...));
833 #else
834 static void macro_build ();
835 #endif
836 static void mips16_macro_build
837   PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
838 static void macro_build_jalr
839   PARAMS ((int, expressionS *));
840 static void macro_build_lui
841   PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
842 static void macro_build_ldst_constoffset
843   PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
844            int valreg, int breg));
845 static void set_at
846   PARAMS ((int *counter, int reg, int unsignedp));
847 static void check_absolute_expr
848   PARAMS ((struct mips_cl_insn * ip, expressionS *));
849 static void load_register
850   PARAMS ((int *, int, expressionS *, int));
851 static void load_address
852   PARAMS ((int *, int, expressionS *, int *));
853 static void move_register
854   PARAMS ((int *, int, int));
855 static void macro
856   PARAMS ((struct mips_cl_insn * ip));
857 static void mips16_macro
858   PARAMS ((struct mips_cl_insn * ip));
859 #ifdef LOSING_COMPILER
860 static void macro2
861   PARAMS ((struct mips_cl_insn * ip));
862 #endif
863 static void mips_ip
864   PARAMS ((char *str, struct mips_cl_insn * ip));
865 static void mips16_ip
866   PARAMS ((char *str, struct mips_cl_insn * ip));
867 static void mips16_immed
868   PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
869            bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
870 static bfd_boolean parse_relocation
871   PARAMS ((char **, bfd_reloc_code_real_type *));
872 static size_t my_getSmallExpression
873   PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
874 static void my_getExpression
875   PARAMS ((expressionS *, char *));
876 #ifdef OBJ_ELF
877 static int support_64bit_objects
878   PARAMS((void));
879 #endif
880 static void mips_set_option_string
881   PARAMS ((const char **, const char *));
882 static symbolS *get_symbol
883   PARAMS ((void));
884 static void mips_align
885   PARAMS ((int to, int fill, symbolS *label));
886 static void s_align
887   PARAMS ((int));
888 static void s_change_sec
889   PARAMS ((int));
890 static void s_change_section
891   PARAMS ((int));
892 static void s_cons
893   PARAMS ((int));
894 static void s_float_cons
895   PARAMS ((int));
896 static void s_mips_globl
897   PARAMS ((int));
898 static void s_option
899   PARAMS ((int));
900 static void s_mipsset
901   PARAMS ((int));
902 static void s_abicalls
903   PARAMS ((int));
904 static void s_cpload
905   PARAMS ((int));
906 static void s_cpsetup
907   PARAMS ((int));
908 static void s_cplocal
909   PARAMS ((int));
910 static void s_cprestore
911   PARAMS ((int));
912 static void s_cpreturn
913   PARAMS ((int));
914 static void s_gpvalue
915   PARAMS ((int));
916 static void s_gpword
917   PARAMS ((int));
918 static void s_gpdword
919   PARAMS ((int));
920 static void s_cpadd
921   PARAMS ((int));
922 static void s_insn
923   PARAMS ((int));
924 static void md_obj_begin
925   PARAMS ((void));
926 static void md_obj_end
927   PARAMS ((void));
928 static long get_number
929   PARAMS ((void));
930 static void s_mips_ent
931   PARAMS ((int));
932 static void s_mips_end
933   PARAMS ((int));
934 static void s_mips_frame
935   PARAMS ((int));
936 static void s_mips_mask
937   PARAMS ((int));
938 static void s_mips_stab
939   PARAMS ((int));
940 static void s_mips_weakext
941   PARAMS ((int));
942 static void s_mips_file
943   PARAMS ((int));
944 static void s_mips_loc
945   PARAMS ((int));
946 static bfd_boolean pic_need_relax
947   PARAMS ((symbolS *, asection *));
948 static int mips16_extended_frag
949   PARAMS ((fragS *, asection *, long));
950 static int relaxed_branch_length (fragS *, asection *, int);
951 static int validate_mips_insn
952   PARAMS ((const struct mips_opcode *));
953 static void show
954   PARAMS ((FILE *, const char *, int *, int *));
955 #ifdef OBJ_ELF
956 static int mips_need_elf_addend_fixup
957   PARAMS ((fixS *));
958 #endif
959
960 /* Table and functions used to map between CPU/ISA names, and
961    ISA levels, and CPU numbers.  */
962
963 struct mips_cpu_info
964 {
965   const char *name;           /* CPU or ISA name.  */
966   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
967   int isa;                    /* ISA level.  */
968   int cpu;                    /* CPU number (default CPU if ISA).  */
969 };
970
971 static void mips_set_architecture
972   PARAMS ((const struct mips_cpu_info *));
973 static void mips_set_tune
974   PARAMS ((const struct mips_cpu_info *));
975 static bfd_boolean mips_strict_matching_cpu_name_p
976   PARAMS ((const char *, const char *));
977 static bfd_boolean mips_matching_cpu_name_p
978   PARAMS ((const char *, const char *));
979 static const struct mips_cpu_info *mips_parse_cpu
980   PARAMS ((const char *, const char *));
981 static const struct mips_cpu_info *mips_cpu_info_from_isa
982   PARAMS ((int));
983 \f
984 /* Pseudo-op table.
985
986    The following pseudo-ops from the Kane and Heinrich MIPS book
987    should be defined here, but are currently unsupported: .alias,
988    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
989
990    The following pseudo-ops from the Kane and Heinrich MIPS book are
991    specific to the type of debugging information being generated, and
992    should be defined by the object format: .aent, .begin, .bend,
993    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
994    .vreg.
995
996    The following pseudo-ops from the Kane and Heinrich MIPS book are
997    not MIPS CPU specific, but are also not specific to the object file
998    format.  This file is probably the best place to define them, but
999    they are not currently supported: .asm0, .endr, .lab, .repeat,
1000    .struct.  */
1001
1002 static const pseudo_typeS mips_pseudo_table[] =
1003 {
1004   /* MIPS specific pseudo-ops.  */
1005   {"option", s_option, 0},
1006   {"set", s_mipsset, 0},
1007   {"rdata", s_change_sec, 'r'},
1008   {"sdata", s_change_sec, 's'},
1009   {"livereg", s_ignore, 0},
1010   {"abicalls", s_abicalls, 0},
1011   {"cpload", s_cpload, 0},
1012   {"cpsetup", s_cpsetup, 0},
1013   {"cplocal", s_cplocal, 0},
1014   {"cprestore", s_cprestore, 0},
1015   {"cpreturn", s_cpreturn, 0},
1016   {"gpvalue", s_gpvalue, 0},
1017   {"gpword", s_gpword, 0},
1018   {"gpdword", s_gpdword, 0},
1019   {"cpadd", s_cpadd, 0},
1020   {"insn", s_insn, 0},
1021
1022   /* Relatively generic pseudo-ops that happen to be used on MIPS
1023      chips.  */
1024   {"asciiz", stringer, 1},
1025   {"bss", s_change_sec, 'b'},
1026   {"err", s_err, 0},
1027   {"half", s_cons, 1},
1028   {"dword", s_cons, 3},
1029   {"weakext", s_mips_weakext, 0},
1030
1031   /* These pseudo-ops are defined in read.c, but must be overridden
1032      here for one reason or another.  */
1033   {"align", s_align, 0},
1034   {"byte", s_cons, 0},
1035   {"data", s_change_sec, 'd'},
1036   {"double", s_float_cons, 'd'},
1037   {"float", s_float_cons, 'f'},
1038   {"globl", s_mips_globl, 0},
1039   {"global", s_mips_globl, 0},
1040   {"hword", s_cons, 1},
1041   {"int", s_cons, 2},
1042   {"long", s_cons, 2},
1043   {"octa", s_cons, 4},
1044   {"quad", s_cons, 3},
1045   {"section", s_change_section, 0},
1046   {"short", s_cons, 1},
1047   {"single", s_float_cons, 'f'},
1048   {"stabn", s_mips_stab, 'n'},
1049   {"text", s_change_sec, 't'},
1050   {"word", s_cons, 2},
1051
1052   { "extern", ecoff_directive_extern, 0},
1053
1054   { NULL, NULL, 0 },
1055 };
1056
1057 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1058 {
1059   /* These pseudo-ops should be defined by the object file format.
1060      However, a.out doesn't support them, so we have versions here.  */
1061   {"aent", s_mips_ent, 1},
1062   {"bgnb", s_ignore, 0},
1063   {"end", s_mips_end, 0},
1064   {"endb", s_ignore, 0},
1065   {"ent", s_mips_ent, 0},
1066   {"file", s_mips_file, 0},
1067   {"fmask", s_mips_mask, 'F'},
1068   {"frame", s_mips_frame, 0},
1069   {"loc", s_mips_loc, 0},
1070   {"mask", s_mips_mask, 'R'},
1071   {"verstamp", s_ignore, 0},
1072   { NULL, NULL, 0 },
1073 };
1074
1075 extern void pop_insert PARAMS ((const pseudo_typeS *));
1076
1077 void
1078 mips_pop_insert ()
1079 {
1080   pop_insert (mips_pseudo_table);
1081   if (! ECOFF_DEBUGGING)
1082     pop_insert (mips_nonecoff_pseudo_table);
1083 }
1084 \f
1085 /* Symbols labelling the current insn.  */
1086
1087 struct insn_label_list
1088 {
1089   struct insn_label_list *next;
1090   symbolS *label;
1091 };
1092
1093 static struct insn_label_list *insn_labels;
1094 static struct insn_label_list *free_insn_labels;
1095
1096 static void mips_clear_insn_labels PARAMS ((void));
1097
1098 static inline void
1099 mips_clear_insn_labels ()
1100 {
1101   register struct insn_label_list **pl;
1102
1103   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1104     ;
1105   *pl = insn_labels;
1106   insn_labels = NULL;
1107 }
1108 \f
1109 static char *expr_end;
1110
1111 /* Expressions which appear in instructions.  These are set by
1112    mips_ip.  */
1113
1114 static expressionS imm_expr;
1115 static expressionS offset_expr;
1116
1117 /* Relocs associated with imm_expr and offset_expr.  */
1118
1119 static bfd_reloc_code_real_type imm_reloc[3]
1120   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1121 static bfd_reloc_code_real_type offset_reloc[3]
1122   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1123
1124 /* These are set by mips16_ip if an explicit extension is used.  */
1125
1126 static bfd_boolean mips16_small, mips16_ext;
1127
1128 #ifdef OBJ_ELF
1129 /* The pdr segment for per procedure frame/regmask info.  Not used for
1130    ECOFF debugging.  */
1131
1132 static segT pdr_seg;
1133 #endif
1134
1135 /* The default target format to use.  */
1136
1137 const char *
1138 mips_target_format ()
1139 {
1140   switch (OUTPUT_FLAVOR)
1141     {
1142     case bfd_target_aout_flavour:
1143       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1144     case bfd_target_ecoff_flavour:
1145       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1146     case bfd_target_coff_flavour:
1147       return "pe-mips";
1148     case bfd_target_elf_flavour:
1149 #ifdef TE_TMIPS
1150       /* This is traditional mips.  */
1151       return (target_big_endian
1152               ? (HAVE_64BIT_OBJECTS
1153                  ? "elf64-tradbigmips"
1154                  : (HAVE_NEWABI
1155                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1156               : (HAVE_64BIT_OBJECTS
1157                  ? "elf64-tradlittlemips"
1158                  : (HAVE_NEWABI
1159                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1160 #else
1161       return (target_big_endian
1162               ? (HAVE_64BIT_OBJECTS
1163                  ? "elf64-bigmips"
1164                  : (HAVE_NEWABI
1165                     ? "elf32-nbigmips" : "elf32-bigmips"))
1166               : (HAVE_64BIT_OBJECTS
1167                  ? "elf64-littlemips"
1168                  : (HAVE_NEWABI
1169                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1170 #endif
1171     default:
1172       abort ();
1173       return NULL;
1174     }
1175 }
1176
1177 /* This function is called once, at assembler startup time.  It should
1178    set up all the tables, etc. that the MD part of the assembler will need.  */
1179
1180 void
1181 md_begin ()
1182 {
1183   register const char *retval = NULL;
1184   int i = 0;
1185   int broken = 0;
1186
1187   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1188     as_warn (_("Could not set architecture and machine"));
1189
1190   op_hash = hash_new ();
1191
1192   for (i = 0; i < NUMOPCODES;)
1193     {
1194       const char *name = mips_opcodes[i].name;
1195
1196       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1197       if (retval != NULL)
1198         {
1199           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1200                    mips_opcodes[i].name, retval);
1201           /* Probably a memory allocation problem?  Give up now.  */
1202           as_fatal (_("Broken assembler.  No assembly attempted."));
1203         }
1204       do
1205         {
1206           if (mips_opcodes[i].pinfo != INSN_MACRO)
1207             {
1208               if (!validate_mips_insn (&mips_opcodes[i]))
1209                 broken = 1;
1210             }
1211           ++i;
1212         }
1213       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1214     }
1215
1216   mips16_op_hash = hash_new ();
1217
1218   i = 0;
1219   while (i < bfd_mips16_num_opcodes)
1220     {
1221       const char *name = mips16_opcodes[i].name;
1222
1223       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1224       if (retval != NULL)
1225         as_fatal (_("internal: can't hash `%s': %s"),
1226                   mips16_opcodes[i].name, retval);
1227       do
1228         {
1229           if (mips16_opcodes[i].pinfo != INSN_MACRO
1230               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1231                   != mips16_opcodes[i].match))
1232             {
1233               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1234                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1235               broken = 1;
1236             }
1237           ++i;
1238         }
1239       while (i < bfd_mips16_num_opcodes
1240              && strcmp (mips16_opcodes[i].name, name) == 0);
1241     }
1242
1243   if (broken)
1244     as_fatal (_("Broken assembler.  No assembly attempted."));
1245
1246   /* We add all the general register names to the symbol table.  This
1247      helps us detect invalid uses of them.  */
1248   for (i = 0; i < 32; i++)
1249     {
1250       char buf[5];
1251
1252       sprintf (buf, "$%d", i);
1253       symbol_table_insert (symbol_new (buf, reg_section, i,
1254                                        &zero_address_frag));
1255     }
1256   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1257                                    &zero_address_frag));
1258   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1259                                    &zero_address_frag));
1260   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1261                                    &zero_address_frag));
1262   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1263                                    &zero_address_frag));
1264   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1265                                    &zero_address_frag));
1266   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1267                                    &zero_address_frag));
1268   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1269                                    &zero_address_frag));
1270   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1271                                    &zero_address_frag));
1272   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1273                                    &zero_address_frag));
1274
1275   /* If we don't add these register names to the symbol table, they
1276      may end up being added as regular symbols by operand(), and then
1277      make it to the object file as undefined in case they're not
1278      regarded as local symbols.  They're local in o32, since `$' is a
1279      local symbol prefix, but not in n32 or n64.  */
1280   for (i = 0; i < 8; i++)
1281     {
1282       char buf[6];
1283
1284       sprintf (buf, "$fcc%i", i);
1285       symbol_table_insert (symbol_new (buf, reg_section, -1,
1286                                        &zero_address_frag));
1287     }
1288
1289   mips_no_prev_insn (FALSE);
1290
1291   mips_gprmask = 0;
1292   mips_cprmask[0] = 0;
1293   mips_cprmask[1] = 0;
1294   mips_cprmask[2] = 0;
1295   mips_cprmask[3] = 0;
1296
1297   /* set the default alignment for the text section (2**2) */
1298   record_alignment (text_section, 2);
1299
1300   if (USE_GLOBAL_POINTER_OPT)
1301     bfd_set_gp_size (stdoutput, g_switch_value);
1302
1303   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1304     {
1305       /* On a native system, sections must be aligned to 16 byte
1306          boundaries.  When configured for an embedded ELF target, we
1307          don't bother.  */
1308       if (strcmp (TARGET_OS, "elf") != 0)
1309         {
1310           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1311           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1312           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1313         }
1314
1315       /* Create a .reginfo section for register masks and a .mdebug
1316          section for debugging information.  */
1317       {
1318         segT seg;
1319         subsegT subseg;
1320         flagword flags;
1321         segT sec;
1322
1323         seg = now_seg;
1324         subseg = now_subseg;
1325
1326         /* The ABI says this section should be loaded so that the
1327            running program can access it.  However, we don't load it
1328            if we are configured for an embedded target */
1329         flags = SEC_READONLY | SEC_DATA;
1330         if (strcmp (TARGET_OS, "elf") != 0)
1331           flags |= SEC_ALLOC | SEC_LOAD;
1332
1333         if (mips_abi != N64_ABI)
1334           {
1335             sec = subseg_new (".reginfo", (subsegT) 0);
1336
1337             bfd_set_section_flags (stdoutput, sec, flags);
1338             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1339
1340 #ifdef OBJ_ELF
1341             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1342 #endif
1343           }
1344         else
1345           {
1346             /* The 64-bit ABI uses a .MIPS.options section rather than
1347                .reginfo section.  */
1348             sec = subseg_new (".MIPS.options", (subsegT) 0);
1349             bfd_set_section_flags (stdoutput, sec, flags);
1350             bfd_set_section_alignment (stdoutput, sec, 3);
1351
1352 #ifdef OBJ_ELF
1353             /* Set up the option header.  */
1354             {
1355               Elf_Internal_Options opthdr;
1356               char *f;
1357
1358               opthdr.kind = ODK_REGINFO;
1359               opthdr.size = (sizeof (Elf_External_Options)
1360                              + sizeof (Elf64_External_RegInfo));
1361               opthdr.section = 0;
1362               opthdr.info = 0;
1363               f = frag_more (sizeof (Elf_External_Options));
1364               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1365                                              (Elf_External_Options *) f);
1366
1367               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1368             }
1369 #endif
1370           }
1371
1372         if (ECOFF_DEBUGGING)
1373           {
1374             sec = subseg_new (".mdebug", (subsegT) 0);
1375             (void) bfd_set_section_flags (stdoutput, sec,
1376                                           SEC_HAS_CONTENTS | SEC_READONLY);
1377             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1378           }
1379 #ifdef OBJ_ELF
1380         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1381           {
1382             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1383             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1384                                           SEC_READONLY | SEC_RELOC
1385                                           | SEC_DEBUGGING);
1386             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1387           }
1388 #endif
1389
1390         subseg_set (seg, subseg);
1391       }
1392     }
1393
1394   if (! ECOFF_DEBUGGING)
1395     md_obj_begin ();
1396 }
1397
1398 void
1399 md_mips_end ()
1400 {
1401   if (! ECOFF_DEBUGGING)
1402     md_obj_end ();
1403 }
1404
1405 void
1406 md_assemble (str)
1407      char *str;
1408 {
1409   struct mips_cl_insn insn;
1410   bfd_reloc_code_real_type unused_reloc[3]
1411     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1412
1413   imm_expr.X_op = O_absent;
1414   offset_expr.X_op = O_absent;
1415   imm_reloc[0] = BFD_RELOC_UNUSED;
1416   imm_reloc[1] = BFD_RELOC_UNUSED;
1417   imm_reloc[2] = BFD_RELOC_UNUSED;
1418   offset_reloc[0] = BFD_RELOC_UNUSED;
1419   offset_reloc[1] = BFD_RELOC_UNUSED;
1420   offset_reloc[2] = BFD_RELOC_UNUSED;
1421
1422   if (mips_opts.mips16)
1423     mips16_ip (str, &insn);
1424   else
1425     {
1426       mips_ip (str, &insn);
1427       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1428             str, insn.insn_opcode));
1429     }
1430
1431   if (insn_error)
1432     {
1433       as_bad ("%s `%s'", insn_error, str);
1434       return;
1435     }
1436
1437   if (insn.insn_mo->pinfo == INSN_MACRO)
1438     {
1439       if (mips_opts.mips16)
1440         mips16_macro (&insn);
1441       else
1442         macro (&insn);
1443     }
1444   else
1445     {
1446       if (imm_expr.X_op != O_absent)
1447         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1448       else if (offset_expr.X_op != O_absent)
1449         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1450       else
1451         append_insn (NULL, &insn, NULL, unused_reloc);
1452     }
1453 }
1454
1455 /* Return true if the given relocation might need a matching %lo().
1456    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1457    applied to local symbols.  */
1458
1459 static inline bfd_boolean
1460 reloc_needs_lo_p (reloc)
1461      bfd_reloc_code_real_type reloc;
1462 {
1463   return (reloc == BFD_RELOC_HI16_S
1464           || reloc == BFD_RELOC_MIPS_GOT16);
1465 }
1466
1467 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1468    relocation.  */
1469
1470 static inline bfd_boolean
1471 fixup_has_matching_lo_p (fixp)
1472      fixS *fixp;
1473 {
1474   return (fixp->fx_next != NULL
1475           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1476           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1477           && fixp->fx_offset == fixp->fx_next->fx_offset);
1478 }
1479
1480 /* See whether instruction IP reads register REG.  CLASS is the type
1481    of register.  */
1482
1483 static int
1484 insn_uses_reg (ip, reg, class)
1485      struct mips_cl_insn *ip;
1486      unsigned int reg;
1487      enum mips_regclass class;
1488 {
1489   if (class == MIPS16_REG)
1490     {
1491       assert (mips_opts.mips16);
1492       reg = mips16_to_32_reg_map[reg];
1493       class = MIPS_GR_REG;
1494     }
1495
1496   /* Don't report on general register ZERO, since it never changes.  */
1497   if (class == MIPS_GR_REG && reg == ZERO)
1498     return 0;
1499
1500   if (class == MIPS_FP_REG)
1501     {
1502       assert (! mips_opts.mips16);
1503       /* If we are called with either $f0 or $f1, we must check $f0.
1504          This is not optimal, because it will introduce an unnecessary
1505          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1506          need to distinguish reading both $f0 and $f1 or just one of
1507          them.  Note that we don't have to check the other way,
1508          because there is no instruction that sets both $f0 and $f1
1509          and requires a delay.  */
1510       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1511           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1512               == (reg &~ (unsigned) 1)))
1513         return 1;
1514       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1515           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1516               == (reg &~ (unsigned) 1)))
1517         return 1;
1518     }
1519   else if (! mips_opts.mips16)
1520     {
1521       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1522           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1523         return 1;
1524       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1525           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1526         return 1;
1527     }
1528   else
1529     {
1530       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1531           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1532                                     & MIPS16OP_MASK_RX)]
1533               == reg))
1534         return 1;
1535       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1536           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1537                                     & MIPS16OP_MASK_RY)]
1538               == reg))
1539         return 1;
1540       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1541           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1542                                     & MIPS16OP_MASK_MOVE32Z)]
1543               == reg))
1544         return 1;
1545       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1546         return 1;
1547       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1548         return 1;
1549       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1550         return 1;
1551       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1552           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1553               & MIPS16OP_MASK_REGR32) == reg)
1554         return 1;
1555     }
1556
1557   return 0;
1558 }
1559
1560 /* This function returns true if modifying a register requires a
1561    delay.  */
1562
1563 static int
1564 reg_needs_delay (reg)
1565      unsigned int reg;
1566 {
1567   unsigned long prev_pinfo;
1568
1569   prev_pinfo = prev_insn.insn_mo->pinfo;
1570   if (! mips_opts.noreorder
1571       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1572       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1573           || (! gpr_interlocks
1574               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1575     {
1576       /* A load from a coprocessor or from memory.  All load
1577          delays delay the use of general register rt for one
1578          instruction on the r3000.  The r6000 and r4000 use
1579          interlocks.  */
1580       /* Itbl support may require additional care here.  */
1581       know (prev_pinfo & INSN_WRITE_GPR_T);
1582       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1583         return 1;
1584     }
1585
1586   return 0;
1587 }
1588
1589 /* Mark instruction labels in mips16 mode.  This permits the linker to
1590    handle them specially, such as generating jalx instructions when
1591    needed.  We also make them odd for the duration of the assembly, in
1592    order to generate the right sort of code.  We will make them even
1593    in the adjust_symtab routine, while leaving them marked.  This is
1594    convenient for the debugger and the disassembler.  The linker knows
1595    to make them odd again.  */
1596
1597 static void
1598 mips16_mark_labels ()
1599 {
1600   if (mips_opts.mips16)
1601     {
1602       struct insn_label_list *l;
1603       valueT val;
1604
1605       for (l = insn_labels; l != NULL; l = l->next)
1606         {
1607 #ifdef OBJ_ELF
1608           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1609             S_SET_OTHER (l->label, STO_MIPS16);
1610 #endif
1611           val = S_GET_VALUE (l->label);
1612           if ((val & 1) == 0)
1613             S_SET_VALUE (l->label, val + 1);
1614         }
1615     }
1616 }
1617
1618 /* Output an instruction.  PLACE is where to put the instruction; if
1619    it is NULL, this uses frag_more to get room.  IP is the instruction
1620    information.  ADDRESS_EXPR is an operand of the instruction to be
1621    used with RELOC_TYPE.  */
1622
1623 static void
1624 append_insn (place, ip, address_expr, reloc_type)
1625      char *place;
1626      struct mips_cl_insn *ip;
1627      expressionS *address_expr;
1628      bfd_reloc_code_real_type *reloc_type;
1629 {
1630   register unsigned long prev_pinfo, pinfo;
1631   char *f;
1632   fixS *fixp[3];
1633   int nops = 0;
1634
1635   /* Mark instruction labels in mips16 mode.  */
1636   mips16_mark_labels ();
1637
1638   prev_pinfo = prev_insn.insn_mo->pinfo;
1639   pinfo = ip->insn_mo->pinfo;
1640
1641   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1642     {
1643       int prev_prev_nop;
1644
1645       /* If the previous insn required any delay slots, see if we need
1646          to insert a NOP or two.  There are eight kinds of possible
1647          hazards, of which an instruction can have at most one type.
1648          (1) a load from memory delay
1649          (2) a load from a coprocessor delay
1650          (3) an unconditional branch delay
1651          (4) a conditional branch delay
1652          (5) a move to coprocessor register delay
1653          (6) a load coprocessor register from memory delay
1654          (7) a coprocessor condition code delay
1655          (8) a HI/LO special register delay
1656
1657          There are a lot of optimizations we could do that we don't.
1658          In particular, we do not, in general, reorder instructions.
1659          If you use gcc with optimization, it will reorder
1660          instructions and generally do much more optimization then we
1661          do here; repeating all that work in the assembler would only
1662          benefit hand written assembly code, and does not seem worth
1663          it.  */
1664
1665       /* This is how a NOP is emitted.  */
1666 #define emit_nop()                                      \
1667   (mips_opts.mips16                                     \
1668    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1669    : md_number_to_chars (frag_more (4), 0, 4))
1670
1671       /* The previous insn might require a delay slot, depending upon
1672          the contents of the current insn.  */
1673       if (! mips_opts.mips16
1674           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1675           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1676                && ! cop_interlocks)
1677               || (! gpr_interlocks
1678                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1679         {
1680           /* A load from a coprocessor or from memory.  All load
1681              delays delay the use of general register rt for one
1682              instruction on the r3000.  The r6000 and r4000 use
1683              interlocks.  */
1684           /* Itbl support may require additional care here.  */
1685           know (prev_pinfo & INSN_WRITE_GPR_T);
1686           if (mips_optimize == 0
1687               || insn_uses_reg (ip,
1688                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1689                                  & OP_MASK_RT),
1690                                 MIPS_GR_REG))
1691             ++nops;
1692         }
1693       else if (! mips_opts.mips16
1694                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1695                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1696                     && ! cop_interlocks)
1697                    || (mips_opts.isa == ISA_MIPS1
1698                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1699         {
1700           /* A generic coprocessor delay.  The previous instruction
1701              modified a coprocessor general or control register.  If
1702              it modified a control register, we need to avoid any
1703              coprocessor instruction (this is probably not always
1704              required, but it sometimes is).  If it modified a general
1705              register, we avoid using that register.
1706
1707              On the r6000 and r4000 loading a coprocessor register
1708              from memory is interlocked, and does not require a delay.
1709
1710              This case is not handled very well.  There is no special
1711              knowledge of CP0 handling, and the coprocessors other
1712              than the floating point unit are not distinguished at
1713              all.  */
1714           /* Itbl support may require additional care here. FIXME!
1715              Need to modify this to include knowledge about
1716              user specified delays!  */
1717           if (prev_pinfo & INSN_WRITE_FPR_T)
1718             {
1719               if (mips_optimize == 0
1720                   || insn_uses_reg (ip,
1721                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1722                                      & OP_MASK_FT),
1723                                     MIPS_FP_REG))
1724                 ++nops;
1725             }
1726           else if (prev_pinfo & INSN_WRITE_FPR_S)
1727             {
1728               if (mips_optimize == 0
1729                   || insn_uses_reg (ip,
1730                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1731                                      & OP_MASK_FS),
1732                                     MIPS_FP_REG))
1733                 ++nops;
1734             }
1735           else
1736             {
1737               /* We don't know exactly what the previous instruction
1738                  does.  If the current instruction uses a coprocessor
1739                  register, we must insert a NOP.  If previous
1740                  instruction may set the condition codes, and the
1741                  current instruction uses them, we must insert two
1742                  NOPS.  */
1743               /* Itbl support may require additional care here.  */
1744               if (mips_optimize == 0
1745                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1746                       && (pinfo & INSN_READ_COND_CODE)))
1747                 nops += 2;
1748               else if (pinfo & INSN_COP)
1749                 ++nops;
1750             }
1751         }
1752       else if (! mips_opts.mips16
1753                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1754                && (prev_pinfo & INSN_WRITE_COND_CODE)
1755                && ! cop_interlocks)
1756         {
1757           /* The previous instruction sets the coprocessor condition
1758              codes, but does not require a general coprocessor delay
1759              (this means it is a floating point comparison
1760              instruction).  If this instruction uses the condition
1761              codes, we need to insert a single NOP.  */
1762           /* Itbl support may require additional care here.  */
1763           if (mips_optimize == 0
1764               || (pinfo & INSN_READ_COND_CODE))
1765             ++nops;
1766         }
1767
1768       /* If we're fixing up mfhi/mflo for the r7000 and the
1769          previous insn was an mfhi/mflo and the current insn
1770          reads the register that the mfhi/mflo wrote to, then
1771          insert two nops.  */
1772
1773       else if (mips_7000_hilo_fix
1774                && MF_HILO_INSN (prev_pinfo)
1775                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1776                                       & OP_MASK_RD),
1777                                  MIPS_GR_REG))
1778         {
1779           nops += 2;
1780         }
1781
1782       /* If we're fixing up mfhi/mflo for the r7000 and the
1783          2nd previous insn was an mfhi/mflo and the current insn
1784          reads the register that the mfhi/mflo wrote to, then
1785          insert one nop.  */
1786
1787       else if (mips_7000_hilo_fix
1788                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1789                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1790                                        & OP_MASK_RD),
1791                                     MIPS_GR_REG))
1792
1793         {
1794           ++nops;
1795         }
1796
1797       else if (prev_pinfo & INSN_READ_LO)
1798         {
1799           /* The previous instruction reads the LO register; if the
1800              current instruction writes to the LO register, we must
1801              insert two NOPS.  Some newer processors have interlocks.
1802              Also the tx39's multiply instructions can be exectuted
1803              immediatly after a read from HI/LO (without the delay),
1804              though the tx39's divide insns still do require the
1805              delay.  */
1806           if (! (hilo_interlocks
1807                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1808               && (mips_optimize == 0
1809                   || (pinfo & INSN_WRITE_LO)))
1810             nops += 2;
1811           /* Most mips16 branch insns don't have a delay slot.
1812              If a read from LO is immediately followed by a branch
1813              to a write to LO we have a read followed by a write
1814              less than 2 insns away.  We assume the target of
1815              a branch might be a write to LO, and insert a nop
1816              between a read and an immediately following branch.  */
1817           else if (mips_opts.mips16
1818                    && (mips_optimize == 0
1819                        || (pinfo & MIPS16_INSN_BRANCH)))
1820             ++nops;
1821         }
1822       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1823         {
1824           /* The previous instruction reads the HI register; if the
1825              current instruction writes to the HI register, we must
1826              insert a NOP.  Some newer processors have interlocks.
1827              Also the note tx39's multiply above.  */
1828           if (! (hilo_interlocks
1829                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1830               && (mips_optimize == 0
1831                   || (pinfo & INSN_WRITE_HI)))
1832             nops += 2;
1833           /* Most mips16 branch insns don't have a delay slot.
1834              If a read from HI is immediately followed by a branch
1835              to a write to HI we have a read followed by a write
1836              less than 2 insns away.  We assume the target of
1837              a branch might be a write to HI, and insert a nop
1838              between a read and an immediately following branch.  */
1839           else if (mips_opts.mips16
1840                    && (mips_optimize == 0
1841                        || (pinfo & MIPS16_INSN_BRANCH)))
1842             ++nops;
1843         }
1844
1845       /* If the previous instruction was in a noreorder section, then
1846          we don't want to insert the nop after all.  */
1847       /* Itbl support may require additional care here.  */
1848       if (prev_insn_unreordered)
1849         nops = 0;
1850
1851       /* There are two cases which require two intervening
1852          instructions: 1) setting the condition codes using a move to
1853          coprocessor instruction which requires a general coprocessor
1854          delay and then reading the condition codes 2) reading the HI
1855          or LO register and then writing to it (except on processors
1856          which have interlocks).  If we are not already emitting a NOP
1857          instruction, we must check for these cases compared to the
1858          instruction previous to the previous instruction.  */
1859       if ((! mips_opts.mips16
1860            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1861            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1862            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1863            && (pinfo & INSN_READ_COND_CODE)
1864            && ! cop_interlocks)
1865           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1866               && (pinfo & INSN_WRITE_LO)
1867               && ! (hilo_interlocks
1868                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1869           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1870               && (pinfo & INSN_WRITE_HI)
1871               && ! (hilo_interlocks
1872                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1873         prev_prev_nop = 1;
1874       else
1875         prev_prev_nop = 0;
1876
1877       if (prev_prev_insn_unreordered)
1878         prev_prev_nop = 0;
1879
1880       if (prev_prev_nop && nops == 0)
1881         ++nops;
1882
1883       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1884         {
1885           /* We're out of bits in pinfo, so we must resort to string
1886              ops here.  Shortcuts are selected based on opcodes being
1887              limited to the VR4122 instruction set.  */
1888           int min_nops = 0;
1889           const char *pn = prev_insn.insn_mo->name;
1890           const char *tn = ip->insn_mo->name;
1891           if (strncmp(pn, "macc", 4) == 0
1892               || strncmp(pn, "dmacc", 5) == 0)
1893             {
1894               /* Errata 21 - [D]DIV[U] after [D]MACC */
1895               if (strstr (tn, "div"))
1896                 {
1897                   min_nops = 1;
1898                 }
1899
1900               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1901               if (pn[0] == 'd' /* dmacc */
1902                   && (strncmp(tn, "dmult", 5) == 0
1903                       || strncmp(tn, "dmacc", 5) == 0))
1904                 {
1905                   min_nops = 1;
1906                 }
1907
1908               /* Errata 24 - MT{LO,HI} after [D]MACC */
1909               if (strcmp (tn, "mtlo") == 0
1910                   || strcmp (tn, "mthi") == 0)
1911                 {
1912                   min_nops = 1;
1913                 }
1914
1915             }
1916           else if (strncmp(pn, "dmult", 5) == 0
1917                    && (strncmp(tn, "dmult", 5) == 0
1918                        || strncmp(tn, "dmacc", 5) == 0))
1919             {
1920               /* Here is the rest of errata 23.  */
1921               min_nops = 1;
1922             }
1923           if (nops < min_nops)
1924             nops = min_nops;
1925         }
1926
1927       /* If we are being given a nop instruction, don't bother with
1928          one of the nops we would otherwise output.  This will only
1929          happen when a nop instruction is used with mips_optimize set
1930          to 0.  */
1931       if (nops > 0
1932           && ! mips_opts.noreorder
1933           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1934         --nops;
1935
1936       /* Now emit the right number of NOP instructions.  */
1937       if (nops > 0 && ! mips_opts.noreorder)
1938         {
1939           fragS *old_frag;
1940           unsigned long old_frag_offset;
1941           int i;
1942           struct insn_label_list *l;
1943
1944           old_frag = frag_now;
1945           old_frag_offset = frag_now_fix ();
1946
1947           for (i = 0; i < nops; i++)
1948             emit_nop ();
1949
1950           if (listing)
1951             {
1952               listing_prev_line ();
1953               /* We may be at the start of a variant frag.  In case we
1954                  are, make sure there is enough space for the frag
1955                  after the frags created by listing_prev_line.  The
1956                  argument to frag_grow here must be at least as large
1957                  as the argument to all other calls to frag_grow in
1958                  this file.  We don't have to worry about being in the
1959                  middle of a variant frag, because the variants insert
1960                  all needed nop instructions themselves.  */
1961               frag_grow (40);
1962             }
1963
1964           for (l = insn_labels; l != NULL; l = l->next)
1965             {
1966               valueT val;
1967
1968               assert (S_GET_SEGMENT (l->label) == now_seg);
1969               symbol_set_frag (l->label, frag_now);
1970               val = (valueT) frag_now_fix ();
1971               /* mips16 text labels are stored as odd.  */
1972               if (mips_opts.mips16)
1973                 ++val;
1974               S_SET_VALUE (l->label, val);
1975             }
1976
1977 #ifndef NO_ECOFF_DEBUGGING
1978           if (ECOFF_DEBUGGING)
1979             ecoff_fix_loc (old_frag, old_frag_offset);
1980 #endif
1981         }
1982       else if (prev_nop_frag != NULL)
1983         {
1984           /* We have a frag holding nops we may be able to remove.  If
1985              we don't need any nops, we can decrease the size of
1986              prev_nop_frag by the size of one instruction.  If we do
1987              need some nops, we count them in prev_nops_required.  */
1988           if (prev_nop_frag_since == 0)
1989             {
1990               if (nops == 0)
1991                 {
1992                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1993                   --prev_nop_frag_holds;
1994                 }
1995               else
1996                 prev_nop_frag_required += nops;
1997             }
1998           else
1999             {
2000               if (prev_prev_nop == 0)
2001                 {
2002                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2003                   --prev_nop_frag_holds;
2004                 }
2005               else
2006                 ++prev_nop_frag_required;
2007             }
2008
2009           if (prev_nop_frag_holds <= prev_nop_frag_required)
2010             prev_nop_frag = NULL;
2011
2012           ++prev_nop_frag_since;
2013
2014           /* Sanity check: by the time we reach the second instruction
2015              after prev_nop_frag, we should have used up all the nops
2016              one way or another.  */
2017           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2018         }
2019     }
2020
2021   if (place == NULL
2022       && address_expr
2023       && *reloc_type == BFD_RELOC_16_PCREL_S2
2024       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2025           || pinfo & INSN_COND_BRANCH_LIKELY)
2026       && mips_relax_branch
2027       /* Don't try branch relaxation within .set nomacro, or within
2028          .set noat if we use $at for PIC computations.  If it turns
2029          out that the branch was out-of-range, we'll get an error.  */
2030       && !mips_opts.warn_about_macros
2031       && !(mips_opts.noat && mips_pic != NO_PIC)
2032       && !mips_opts.mips16)
2033     {
2034       f = frag_var (rs_machine_dependent,
2035                     relaxed_branch_length
2036                     (NULL, NULL,
2037                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2038                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2039                     RELAX_BRANCH_ENCODE
2040                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2041                      pinfo & INSN_COND_BRANCH_LIKELY,
2042                      pinfo & INSN_WRITE_GPR_31,
2043                      0),
2044                     address_expr->X_add_symbol,
2045                     address_expr->X_add_number,
2046                     0);
2047       *reloc_type = BFD_RELOC_UNUSED;
2048     }
2049   else if (*reloc_type > BFD_RELOC_UNUSED)
2050     {
2051       /* We need to set up a variant frag.  */
2052       assert (mips_opts.mips16 && address_expr != NULL);
2053       f = frag_var (rs_machine_dependent, 4, 0,
2054                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2055                                          mips16_small, mips16_ext,
2056                                          (prev_pinfo
2057                                           & INSN_UNCOND_BRANCH_DELAY),
2058                                          (*prev_insn_reloc_type
2059                                           == BFD_RELOC_MIPS16_JMP)),
2060                     make_expr_symbol (address_expr), 0, NULL);
2061     }
2062   else if (place != NULL)
2063     f = place;
2064   else if (mips_opts.mips16
2065            && ! ip->use_extend
2066            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2067     {
2068       /* Make sure there is enough room to swap this instruction with
2069          a following jump instruction.  */
2070       frag_grow (6);
2071       f = frag_more (2);
2072     }
2073   else
2074     {
2075       if (mips_opts.mips16
2076           && mips_opts.noreorder
2077           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2078         as_warn (_("extended instruction in delay slot"));
2079
2080       f = frag_more (4);
2081     }
2082
2083   fixp[0] = fixp[1] = fixp[2] = NULL;
2084   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2085     {
2086       if (address_expr->X_op == O_constant)
2087         {
2088           valueT tmp;
2089
2090           switch (*reloc_type)
2091             {
2092             case BFD_RELOC_32:
2093               ip->insn_opcode |= address_expr->X_add_number;
2094               break;
2095
2096             case BFD_RELOC_MIPS_HIGHEST:
2097               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2098               tmp >>= 16;
2099               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2100               break;
2101
2102             case BFD_RELOC_MIPS_HIGHER:
2103               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2104               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2105               break;
2106
2107             case BFD_RELOC_HI16_S:
2108               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2109                                   >> 16) & 0xffff;
2110               break;
2111
2112             case BFD_RELOC_HI16:
2113               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2114               break;
2115
2116             case BFD_RELOC_LO16:
2117             case BFD_RELOC_MIPS_GOT_DISP:
2118               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2119               break;
2120
2121             case BFD_RELOC_MIPS_JMP:
2122               if ((address_expr->X_add_number & 3) != 0)
2123                 as_bad (_("jump to misaligned address (0x%lx)"),
2124                         (unsigned long) address_expr->X_add_number);
2125               if (address_expr->X_add_number & ~0xfffffff)
2126                 as_bad (_("jump address range overflow (0x%lx)"),
2127                         (unsigned long) address_expr->X_add_number);
2128               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2129               break;
2130
2131             case BFD_RELOC_MIPS16_JMP:
2132               if ((address_expr->X_add_number & 3) != 0)
2133                 as_bad (_("jump to misaligned address (0x%lx)"),
2134                         (unsigned long) address_expr->X_add_number);
2135               if (address_expr->X_add_number & ~0xfffffff)
2136                 as_bad (_("jump address range overflow (0x%lx)"),
2137                         (unsigned long) address_expr->X_add_number);
2138               ip->insn_opcode |=
2139                 (((address_expr->X_add_number & 0x7c0000) << 3)
2140                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2141                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2142               break;
2143
2144             case BFD_RELOC_16_PCREL_S2:
2145               goto need_reloc;
2146
2147             default:
2148               internalError ();
2149             }
2150         }
2151       else
2152         {
2153         need_reloc:
2154           /* Don't generate a reloc if we are writing into a variant frag.  */
2155           if (place == NULL)
2156             {
2157               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2158                                      address_expr,
2159                                      *reloc_type == BFD_RELOC_16_PCREL_S2,
2160                                      reloc_type[0]);
2161
2162               /* These relocations can have an addend that won't fit in
2163                  4 octets for 64bit assembly.  */
2164               if (HAVE_64BIT_GPRS &&
2165                   (*reloc_type == BFD_RELOC_16
2166                    || *reloc_type == BFD_RELOC_32
2167                    || *reloc_type == BFD_RELOC_MIPS_JMP
2168                    || *reloc_type == BFD_RELOC_HI16_S
2169                    || *reloc_type == BFD_RELOC_LO16
2170                    || *reloc_type == BFD_RELOC_GPREL16
2171                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2172                    || *reloc_type == BFD_RELOC_GPREL32
2173                    || *reloc_type == BFD_RELOC_64
2174                    || *reloc_type == BFD_RELOC_CTOR
2175                    || *reloc_type == BFD_RELOC_MIPS_SUB
2176                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2177                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2178                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2179                    || *reloc_type == BFD_RELOC_MIPS_REL16
2180                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2181                 fixp[0]->fx_no_overflow = 1;
2182
2183               if (reloc_needs_lo_p (*reloc_type))
2184                 {
2185                   struct mips_hi_fixup *hi_fixup;
2186
2187                   /* Reuse the last entry if it already has a matching %lo.  */
2188                   hi_fixup = mips_hi_fixup_list;
2189                   if (hi_fixup == 0
2190                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2191                     {
2192                       hi_fixup = ((struct mips_hi_fixup *)
2193                                   xmalloc (sizeof (struct mips_hi_fixup)));
2194                       hi_fixup->next = mips_hi_fixup_list;
2195                       mips_hi_fixup_list = hi_fixup;
2196                     }
2197                   hi_fixup->fixp = fixp[0];
2198                   hi_fixup->seg = now_seg;
2199                 }
2200
2201               if (reloc_type[1] != BFD_RELOC_UNUSED)
2202                 {
2203                   /* FIXME: This symbol can be one of
2204                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2205                   address_expr->X_op = O_absent;
2206                   address_expr->X_add_symbol = 0;
2207                   address_expr->X_add_number = 0;
2208
2209                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2210                                          4, address_expr, FALSE,
2211                                          reloc_type[1]);
2212
2213                   /* These relocations can have an addend that won't fit in
2214                      4 octets for 64bit assembly.  */
2215                   if (HAVE_64BIT_GPRS &&
2216                       (*reloc_type == BFD_RELOC_16
2217                        || *reloc_type == BFD_RELOC_32
2218                        || *reloc_type == BFD_RELOC_MIPS_JMP
2219                        || *reloc_type == BFD_RELOC_HI16_S
2220                        || *reloc_type == BFD_RELOC_LO16
2221                        || *reloc_type == BFD_RELOC_GPREL16
2222                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2223                        || *reloc_type == BFD_RELOC_GPREL32
2224                        || *reloc_type == BFD_RELOC_64
2225                        || *reloc_type == BFD_RELOC_CTOR
2226                        || *reloc_type == BFD_RELOC_MIPS_SUB
2227                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2228                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2229                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2230                        || *reloc_type == BFD_RELOC_MIPS_REL16
2231                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2232                     fixp[1]->fx_no_overflow = 1;
2233
2234                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2235                     {
2236                       address_expr->X_op = O_absent;
2237                       address_expr->X_add_symbol = 0;
2238                       address_expr->X_add_number = 0;
2239
2240                       fixp[2] = fix_new_exp (frag_now,
2241                                              f - frag_now->fr_literal, 4,
2242                                              address_expr, FALSE,
2243                                              reloc_type[2]);
2244
2245                       /* These relocations can have an addend that won't fit in
2246                          4 octets for 64bit assembly.  */
2247                       if (HAVE_64BIT_GPRS &&
2248                           (*reloc_type == BFD_RELOC_16
2249                            || *reloc_type == BFD_RELOC_32
2250                            || *reloc_type == BFD_RELOC_MIPS_JMP
2251                            || *reloc_type == BFD_RELOC_HI16_S
2252                            || *reloc_type == BFD_RELOC_LO16
2253                            || *reloc_type == BFD_RELOC_GPREL16
2254                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2255                            || *reloc_type == BFD_RELOC_GPREL32
2256                            || *reloc_type == BFD_RELOC_64
2257                            || *reloc_type == BFD_RELOC_CTOR
2258                            || *reloc_type == BFD_RELOC_MIPS_SUB
2259                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2260                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2261                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2262                            || *reloc_type == BFD_RELOC_MIPS_REL16
2263                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2264                         fixp[2]->fx_no_overflow = 1;
2265                     }
2266                 }
2267             }
2268         }
2269     }
2270
2271   if (! mips_opts.mips16)
2272     {
2273       md_number_to_chars (f, ip->insn_opcode, 4);
2274 #ifdef OBJ_ELF
2275       dwarf2_emit_insn (4);
2276 #endif
2277     }
2278   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2279     {
2280       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2281       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2282 #ifdef OBJ_ELF
2283       dwarf2_emit_insn (4);
2284 #endif
2285     }
2286   else
2287     {
2288       if (ip->use_extend)
2289         {
2290           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2291           f += 2;
2292         }
2293       md_number_to_chars (f, ip->insn_opcode, 2);
2294 #ifdef OBJ_ELF
2295       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2296 #endif
2297     }
2298
2299   /* Update the register mask information.  */
2300   if (! mips_opts.mips16)
2301     {
2302       if (pinfo & INSN_WRITE_GPR_D)
2303         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2304       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2305         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2306       if (pinfo & INSN_READ_GPR_S)
2307         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2308       if (pinfo & INSN_WRITE_GPR_31)
2309         mips_gprmask |= 1 << RA;
2310       if (pinfo & INSN_WRITE_FPR_D)
2311         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2312       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2313         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2314       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2315         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2316       if ((pinfo & INSN_READ_FPR_R) != 0)
2317         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2318       if (pinfo & INSN_COP)
2319         {
2320           /* We don't keep enough information to sort these cases out.
2321              The itbl support does keep this information however, although
2322              we currently don't support itbl fprmats as part of the cop
2323              instruction.  May want to add this support in the future.  */
2324         }
2325       /* Never set the bit for $0, which is always zero.  */
2326       mips_gprmask &= ~1 << 0;
2327     }
2328   else
2329     {
2330       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2331         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2332                               & MIPS16OP_MASK_RX);
2333       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2334         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2335                               & MIPS16OP_MASK_RY);
2336       if (pinfo & MIPS16_INSN_WRITE_Z)
2337         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2338                               & MIPS16OP_MASK_RZ);
2339       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2340         mips_gprmask |= 1 << TREG;
2341       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2342         mips_gprmask |= 1 << SP;
2343       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2344         mips_gprmask |= 1 << RA;
2345       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2346         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2347       if (pinfo & MIPS16_INSN_READ_Z)
2348         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2349                               & MIPS16OP_MASK_MOVE32Z);
2350       if (pinfo & MIPS16_INSN_READ_GPR_X)
2351         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2352                               & MIPS16OP_MASK_REGR32);
2353     }
2354
2355   if (place == NULL && ! mips_opts.noreorder)
2356     {
2357       /* Filling the branch delay slot is more complex.  We try to
2358          switch the branch with the previous instruction, which we can
2359          do if the previous instruction does not set up a condition
2360          that the branch tests and if the branch is not itself the
2361          target of any branch.  */
2362       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2363           || (pinfo & INSN_COND_BRANCH_DELAY))
2364         {
2365           if (mips_optimize < 2
2366               /* If we have seen .set volatile or .set nomove, don't
2367                  optimize.  */
2368               || mips_opts.nomove != 0
2369               /* If we had to emit any NOP instructions, then we
2370                  already know we can not swap.  */
2371               || nops != 0
2372               /* If we don't even know the previous insn, we can not
2373                  swap.  */
2374               || ! prev_insn_valid
2375               /* If the previous insn is already in a branch delay
2376                  slot, then we can not swap.  */
2377               || prev_insn_is_delay_slot
2378               /* If the previous previous insn was in a .set
2379                  noreorder, we can't swap.  Actually, the MIPS
2380                  assembler will swap in this situation.  However, gcc
2381                  configured -with-gnu-as will generate code like
2382                    .set noreorder
2383                    lw   $4,XXX
2384                    .set reorder
2385                    INSN
2386                    bne  $4,$0,foo
2387                  in which we can not swap the bne and INSN.  If gcc is
2388                  not configured -with-gnu-as, it does not output the
2389                  .set pseudo-ops.  We don't have to check
2390                  prev_insn_unreordered, because prev_insn_valid will
2391                  be 0 in that case.  We don't want to use
2392                  prev_prev_insn_valid, because we do want to be able
2393                  to swap at the start of a function.  */
2394               || prev_prev_insn_unreordered
2395               /* If the branch is itself the target of a branch, we
2396                  can not swap.  We cheat on this; all we check for is
2397                  whether there is a label on this instruction.  If
2398                  there are any branches to anything other than a
2399                  label, users must use .set noreorder.  */
2400               || insn_labels != NULL
2401               /* If the previous instruction is in a variant frag, we
2402                  can not do the swap.  This does not apply to the
2403                  mips16, which uses variant frags for different
2404                  purposes.  */
2405               || (! mips_opts.mips16
2406                   && prev_insn_frag->fr_type == rs_machine_dependent)
2407               /* If the branch reads the condition codes, we don't
2408                  even try to swap, because in the sequence
2409                    ctc1 $X,$31
2410                    INSN
2411                    INSN
2412                    bc1t LABEL
2413                  we can not swap, and I don't feel like handling that
2414                  case.  */
2415               || (! mips_opts.mips16
2416                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2417                   && (pinfo & INSN_READ_COND_CODE))
2418               /* We can not swap with an instruction that requires a
2419                  delay slot, becase the target of the branch might
2420                  interfere with that instruction.  */
2421               || (! mips_opts.mips16
2422                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2423                   && (prev_pinfo
2424               /* Itbl support may require additional care here.  */
2425                       & (INSN_LOAD_COPROC_DELAY
2426                          | INSN_COPROC_MOVE_DELAY
2427                          | INSN_WRITE_COND_CODE)))
2428               || (! (hilo_interlocks
2429                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2430                   && (prev_pinfo
2431                       & (INSN_READ_LO
2432                          | INSN_READ_HI)))
2433               || (! mips_opts.mips16
2434                   && ! gpr_interlocks
2435                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2436               || (! mips_opts.mips16
2437                   && mips_opts.isa == ISA_MIPS1
2438                   /* Itbl support may require additional care here.  */
2439                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2440               /* We can not swap with a branch instruction.  */
2441               || (prev_pinfo
2442                   & (INSN_UNCOND_BRANCH_DELAY
2443                      | INSN_COND_BRANCH_DELAY
2444                      | INSN_COND_BRANCH_LIKELY))
2445               /* We do not swap with a trap instruction, since it
2446                  complicates trap handlers to have the trap
2447                  instruction be in a delay slot.  */
2448               || (prev_pinfo & INSN_TRAP)
2449               /* If the branch reads a register that the previous
2450                  instruction sets, we can not swap.  */
2451               || (! mips_opts.mips16
2452                   && (prev_pinfo & INSN_WRITE_GPR_T)
2453                   && insn_uses_reg (ip,
2454                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2455                                      & OP_MASK_RT),
2456                                     MIPS_GR_REG))
2457               || (! mips_opts.mips16
2458                   && (prev_pinfo & INSN_WRITE_GPR_D)
2459                   && insn_uses_reg (ip,
2460                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2461                                      & OP_MASK_RD),
2462                                     MIPS_GR_REG))
2463               || (mips_opts.mips16
2464                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2465                        && insn_uses_reg (ip,
2466                                          ((prev_insn.insn_opcode
2467                                            >> MIPS16OP_SH_RX)
2468                                           & MIPS16OP_MASK_RX),
2469                                          MIPS16_REG))
2470                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2471                           && insn_uses_reg (ip,
2472                                             ((prev_insn.insn_opcode
2473                                               >> MIPS16OP_SH_RY)
2474                                              & MIPS16OP_MASK_RY),
2475                                             MIPS16_REG))
2476                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2477                           && insn_uses_reg (ip,
2478                                             ((prev_insn.insn_opcode
2479                                               >> MIPS16OP_SH_RZ)
2480                                              & MIPS16OP_MASK_RZ),
2481                                             MIPS16_REG))
2482                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2483                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2484                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2485                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2486                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2487                           && insn_uses_reg (ip,
2488                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2489                                                                      insn_opcode),
2490                                             MIPS_GR_REG))))
2491               /* If the branch writes a register that the previous
2492                  instruction sets, we can not swap (we know that
2493                  branches write only to RD or to $31).  */
2494               || (! mips_opts.mips16
2495                   && (prev_pinfo & INSN_WRITE_GPR_T)
2496                   && (((pinfo & INSN_WRITE_GPR_D)
2497                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2498                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2499                       || ((pinfo & INSN_WRITE_GPR_31)
2500                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2501                                & OP_MASK_RT)
2502                               == RA))))
2503               || (! mips_opts.mips16
2504                   && (prev_pinfo & INSN_WRITE_GPR_D)
2505                   && (((pinfo & INSN_WRITE_GPR_D)
2506                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2507                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2508                       || ((pinfo & INSN_WRITE_GPR_31)
2509                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2510                                & OP_MASK_RD)
2511                               == RA))))
2512               || (mips_opts.mips16
2513                   && (pinfo & MIPS16_INSN_WRITE_31)
2514                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2515                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2516                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2517                               == RA))))
2518               /* If the branch writes a register that the previous
2519                  instruction reads, we can not swap (we know that
2520                  branches only write to RD or to $31).  */
2521               || (! mips_opts.mips16
2522                   && (pinfo & INSN_WRITE_GPR_D)
2523                   && insn_uses_reg (&prev_insn,
2524                                     ((ip->insn_opcode >> OP_SH_RD)
2525                                      & OP_MASK_RD),
2526                                     MIPS_GR_REG))
2527               || (! mips_opts.mips16
2528                   && (pinfo & INSN_WRITE_GPR_31)
2529                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2530               || (mips_opts.mips16
2531                   && (pinfo & MIPS16_INSN_WRITE_31)
2532                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2533               /* If we are generating embedded PIC code, the branch
2534                  might be expanded into a sequence which uses $at, so
2535                  we can't swap with an instruction which reads it.  */
2536               || (mips_pic == EMBEDDED_PIC
2537                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2538               /* If the previous previous instruction has a load
2539                  delay, and sets a register that the branch reads, we
2540                  can not swap.  */
2541               || (! mips_opts.mips16
2542                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2543               /* Itbl support may require additional care here.  */
2544                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2545                       || (! gpr_interlocks
2546                           && (prev_prev_insn.insn_mo->pinfo
2547                               & INSN_LOAD_MEMORY_DELAY)))
2548                   && insn_uses_reg (ip,
2549                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2550                                      & OP_MASK_RT),
2551                                     MIPS_GR_REG))
2552               /* If one instruction sets a condition code and the
2553                  other one uses a condition code, we can not swap.  */
2554               || ((pinfo & INSN_READ_COND_CODE)
2555                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2556               || ((pinfo & INSN_WRITE_COND_CODE)
2557                   && (prev_pinfo & INSN_READ_COND_CODE))
2558               /* If the previous instruction uses the PC, we can not
2559                  swap.  */
2560               || (mips_opts.mips16
2561                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2562               /* If the previous instruction was extended, we can not
2563                  swap.  */
2564               || (mips_opts.mips16 && prev_insn_extended)
2565               /* If the previous instruction had a fixup in mips16
2566                  mode, we can not swap.  This normally means that the
2567                  previous instruction was a 4 byte branch anyhow.  */
2568               || (mips_opts.mips16 && prev_insn_fixp[0])
2569               /* If the previous instruction is a sync, sync.l, or
2570                  sync.p, we can not swap.  */
2571               || (prev_pinfo & INSN_SYNC))
2572             {
2573               /* We could do even better for unconditional branches to
2574                  portions of this object file; we could pick up the
2575                  instruction at the destination, put it in the delay
2576                  slot, and bump the destination address.  */
2577               emit_nop ();
2578               /* Update the previous insn information.  */
2579               prev_prev_insn = *ip;
2580               prev_insn.insn_mo = &dummy_opcode;
2581             }
2582           else
2583             {
2584               /* It looks like we can actually do the swap.  */
2585               if (! mips_opts.mips16)
2586                 {
2587                   char *prev_f;
2588                   char temp[4];
2589
2590                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2591                   memcpy (temp, prev_f, 4);
2592                   memcpy (prev_f, f, 4);
2593                   memcpy (f, temp, 4);
2594                   if (prev_insn_fixp[0])
2595                     {
2596                       prev_insn_fixp[0]->fx_frag = frag_now;
2597                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2598                     }
2599                   if (prev_insn_fixp[1])
2600                     {
2601                       prev_insn_fixp[1]->fx_frag = frag_now;
2602                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2603                     }
2604                   if (prev_insn_fixp[2])
2605                     {
2606                       prev_insn_fixp[2]->fx_frag = frag_now;
2607                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2608                     }
2609                   if (fixp[0])
2610                     {
2611                       fixp[0]->fx_frag = prev_insn_frag;
2612                       fixp[0]->fx_where = prev_insn_where;
2613                     }
2614                   if (fixp[1])
2615                     {
2616                       fixp[1]->fx_frag = prev_insn_frag;
2617                       fixp[1]->fx_where = prev_insn_where;
2618                     }
2619                   if (fixp[2])
2620                     {
2621                       fixp[2]->fx_frag = prev_insn_frag;
2622                       fixp[2]->fx_where = prev_insn_where;
2623                     }
2624                 }
2625               else
2626                 {
2627                   char *prev_f;
2628                   char temp[2];
2629
2630                   assert (prev_insn_fixp[0] == NULL);
2631                   assert (prev_insn_fixp[1] == NULL);
2632                   assert (prev_insn_fixp[2] == NULL);
2633                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2634                   memcpy (temp, prev_f, 2);
2635                   memcpy (prev_f, f, 2);
2636                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2637                     {
2638                       assert (*reloc_type == BFD_RELOC_UNUSED);
2639                       memcpy (f, temp, 2);
2640                     }
2641                   else
2642                     {
2643                       memcpy (f, f + 2, 2);
2644                       memcpy (f + 2, temp, 2);
2645                     }
2646                   if (fixp[0])
2647                     {
2648                       fixp[0]->fx_frag = prev_insn_frag;
2649                       fixp[0]->fx_where = prev_insn_where;
2650                     }
2651                   if (fixp[1])
2652                     {
2653                       fixp[1]->fx_frag = prev_insn_frag;
2654                       fixp[1]->fx_where = prev_insn_where;
2655                     }
2656                   if (fixp[2])
2657                     {
2658                       fixp[2]->fx_frag = prev_insn_frag;
2659                       fixp[2]->fx_where = prev_insn_where;
2660                     }
2661                 }
2662
2663               /* Update the previous insn information; leave prev_insn
2664                  unchanged.  */
2665               prev_prev_insn = *ip;
2666             }
2667           prev_insn_is_delay_slot = 1;
2668
2669           /* If that was an unconditional branch, forget the previous
2670              insn information.  */
2671           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2672             {
2673               prev_prev_insn.insn_mo = &dummy_opcode;
2674               prev_insn.insn_mo = &dummy_opcode;
2675             }
2676
2677           prev_insn_fixp[0] = NULL;
2678           prev_insn_fixp[1] = NULL;
2679           prev_insn_fixp[2] = NULL;
2680           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2681           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2682           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2683           prev_insn_extended = 0;
2684         }
2685       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2686         {
2687           /* We don't yet optimize a branch likely.  What we should do
2688              is look at the target, copy the instruction found there
2689              into the delay slot, and increment the branch to jump to
2690              the next instruction.  */
2691           emit_nop ();
2692           /* Update the previous insn information.  */
2693           prev_prev_insn = *ip;
2694           prev_insn.insn_mo = &dummy_opcode;
2695           prev_insn_fixp[0] = NULL;
2696           prev_insn_fixp[1] = NULL;
2697           prev_insn_fixp[2] = NULL;
2698           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2699           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2700           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2701           prev_insn_extended = 0;
2702         }
2703       else
2704         {
2705           /* Update the previous insn information.  */
2706           if (nops > 0)
2707             prev_prev_insn.insn_mo = &dummy_opcode;
2708           else
2709             prev_prev_insn = prev_insn;
2710           prev_insn = *ip;
2711
2712           /* Any time we see a branch, we always fill the delay slot
2713              immediately; since this insn is not a branch, we know it
2714              is not in a delay slot.  */
2715           prev_insn_is_delay_slot = 0;
2716
2717           prev_insn_fixp[0] = fixp[0];
2718           prev_insn_fixp[1] = fixp[1];
2719           prev_insn_fixp[2] = fixp[2];
2720           prev_insn_reloc_type[0] = reloc_type[0];
2721           prev_insn_reloc_type[1] = reloc_type[1];
2722           prev_insn_reloc_type[2] = reloc_type[2];
2723           if (mips_opts.mips16)
2724             prev_insn_extended = (ip->use_extend
2725                                   || *reloc_type > BFD_RELOC_UNUSED);
2726         }
2727
2728       prev_prev_insn_unreordered = prev_insn_unreordered;
2729       prev_insn_unreordered = 0;
2730       prev_insn_frag = frag_now;
2731       prev_insn_where = f - frag_now->fr_literal;
2732       prev_insn_valid = 1;
2733     }
2734   else if (place == NULL)
2735     {
2736       /* We need to record a bit of information even when we are not
2737          reordering, in order to determine the base address for mips16
2738          PC relative relocs.  */
2739       prev_prev_insn = prev_insn;
2740       prev_insn = *ip;
2741       prev_insn_reloc_type[0] = reloc_type[0];
2742       prev_insn_reloc_type[1] = reloc_type[1];
2743       prev_insn_reloc_type[2] = reloc_type[2];
2744       prev_prev_insn_unreordered = prev_insn_unreordered;
2745       prev_insn_unreordered = 1;
2746     }
2747
2748   /* We just output an insn, so the next one doesn't have a label.  */
2749   mips_clear_insn_labels ();
2750 }
2751
2752 /* This function forgets that there was any previous instruction or
2753    label.  If PRESERVE is non-zero, it remembers enough information to
2754    know whether nops are needed before a noreorder section.  */
2755
2756 static void
2757 mips_no_prev_insn (preserve)
2758      int preserve;
2759 {
2760   if (! preserve)
2761     {
2762       prev_insn.insn_mo = &dummy_opcode;
2763       prev_prev_insn.insn_mo = &dummy_opcode;
2764       prev_nop_frag = NULL;
2765       prev_nop_frag_holds = 0;
2766       prev_nop_frag_required = 0;
2767       prev_nop_frag_since = 0;
2768     }
2769   prev_insn_valid = 0;
2770   prev_insn_is_delay_slot = 0;
2771   prev_insn_unreordered = 0;
2772   prev_insn_extended = 0;
2773   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2774   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2775   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2776   prev_prev_insn_unreordered = 0;
2777   mips_clear_insn_labels ();
2778 }
2779
2780 /* This function must be called whenever we turn on noreorder or emit
2781    something other than instructions.  It inserts any NOPS which might
2782    be needed by the previous instruction, and clears the information
2783    kept for the previous instructions.  The INSNS parameter is true if
2784    instructions are to follow.  */
2785
2786 static void
2787 mips_emit_delays (insns)
2788      bfd_boolean insns;
2789 {
2790   if (! mips_opts.noreorder)
2791     {
2792       int nops;
2793
2794       nops = 0;
2795       if ((! mips_opts.mips16
2796            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2797            && (! cop_interlocks
2798                && (prev_insn.insn_mo->pinfo
2799                    & (INSN_LOAD_COPROC_DELAY
2800                       | INSN_COPROC_MOVE_DELAY
2801                       | INSN_WRITE_COND_CODE))))
2802           || (! hilo_interlocks
2803               && (prev_insn.insn_mo->pinfo
2804                   & (INSN_READ_LO
2805                      | INSN_READ_HI)))
2806           || (! mips_opts.mips16
2807               && ! gpr_interlocks
2808               && (prev_insn.insn_mo->pinfo
2809                   & INSN_LOAD_MEMORY_DELAY))
2810           || (! mips_opts.mips16
2811               && mips_opts.isa == ISA_MIPS1
2812               && (prev_insn.insn_mo->pinfo
2813                   & INSN_COPROC_MEMORY_DELAY)))
2814         {
2815           /* Itbl support may require additional care here.  */
2816           ++nops;
2817           if ((! mips_opts.mips16
2818                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2819                && (! cop_interlocks
2820                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2821               || (! hilo_interlocks
2822                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2823                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2824             ++nops;
2825
2826           if (prev_insn_unreordered)
2827             nops = 0;
2828         }
2829       else if ((! mips_opts.mips16
2830                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2831                 && (! cop_interlocks
2832                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2833                || (! hilo_interlocks
2834                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2835                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2836         {
2837           /* Itbl support may require additional care here.  */
2838           if (! prev_prev_insn_unreordered)
2839             ++nops;
2840         }
2841
2842       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2843         {
2844           int min_nops = 0;
2845           const char *pn = prev_insn.insn_mo->name;
2846           if (strncmp(pn, "macc", 4) == 0
2847               || strncmp(pn, "dmacc", 5) == 0
2848               || strncmp(pn, "dmult", 5) == 0)
2849             {
2850               min_nops = 1;
2851             }
2852           if (nops < min_nops)
2853             nops = min_nops;
2854         }
2855
2856       if (nops > 0)
2857         {
2858           struct insn_label_list *l;
2859
2860           if (insns)
2861             {
2862               /* Record the frag which holds the nop instructions, so
2863                  that we can remove them if we don't need them.  */
2864               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2865               prev_nop_frag = frag_now;
2866               prev_nop_frag_holds = nops;
2867               prev_nop_frag_required = 0;
2868               prev_nop_frag_since = 0;
2869             }
2870
2871           for (; nops > 0; --nops)
2872             emit_nop ();
2873
2874           if (insns)
2875             {
2876               /* Move on to a new frag, so that it is safe to simply
2877                  decrease the size of prev_nop_frag.  */
2878               frag_wane (frag_now);
2879               frag_new (0);
2880             }
2881
2882           for (l = insn_labels; l != NULL; l = l->next)
2883             {
2884               valueT val;
2885
2886               assert (S_GET_SEGMENT (l->label) == now_seg);
2887               symbol_set_frag (l->label, frag_now);
2888               val = (valueT) frag_now_fix ();
2889               /* mips16 text labels are stored as odd.  */
2890               if (mips_opts.mips16)
2891                 ++val;
2892               S_SET_VALUE (l->label, val);
2893             }
2894         }
2895     }
2896
2897   /* Mark instruction labels in mips16 mode.  */
2898   if (insns)
2899     mips16_mark_labels ();
2900
2901   mips_no_prev_insn (insns);
2902 }
2903
2904 /* Build an instruction created by a macro expansion.  This is passed
2905    a pointer to the count of instructions created so far, an
2906    expression, the name of the instruction to build, an operand format
2907    string, and corresponding arguments.  */
2908
2909 #ifdef USE_STDARG
2910 static void
2911 macro_build (char *place,
2912              int *counter,
2913              expressionS * ep,
2914              const char *name,
2915              const char *fmt,
2916              ...)
2917 #else
2918 static void
2919 macro_build (place, counter, ep, name, fmt, va_alist)
2920      char *place;
2921      int *counter;
2922      expressionS *ep;
2923      const char *name;
2924      const char *fmt;
2925      va_dcl
2926 #endif
2927 {
2928   struct mips_cl_insn insn;
2929   bfd_reloc_code_real_type r[3];
2930   va_list args;
2931
2932 #ifdef USE_STDARG
2933   va_start (args, fmt);
2934 #else
2935   va_start (args);
2936 #endif
2937
2938   /*
2939    * If the macro is about to expand into a second instruction,
2940    * print a warning if needed. We need to pass ip as a parameter
2941    * to generate a better warning message here...
2942    */
2943   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2944     as_warn (_("Macro instruction expanded into multiple instructions"));
2945
2946   /*
2947    * If the macro is about to expand into a second instruction,
2948    * and it is in a delay slot, print a warning.
2949    */
2950   if (place == NULL
2951       && *counter == 1
2952       && mips_opts.noreorder
2953       && (prev_prev_insn.insn_mo->pinfo
2954           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2955              | INSN_COND_BRANCH_LIKELY)) != 0)
2956     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2957
2958   if (place == NULL)
2959     ++*counter;         /* bump instruction counter */
2960
2961   if (mips_opts.mips16)
2962     {
2963       mips16_macro_build (place, counter, ep, name, fmt, args);
2964       va_end (args);
2965       return;
2966     }
2967
2968   r[0] = BFD_RELOC_UNUSED;
2969   r[1] = BFD_RELOC_UNUSED;
2970   r[2] = BFD_RELOC_UNUSED;
2971   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2972   assert (insn.insn_mo);
2973   assert (strcmp (name, insn.insn_mo->name) == 0);
2974
2975   /* Search until we get a match for NAME.  */
2976   while (1)
2977     {
2978       /* It is assumed here that macros will never generate
2979          MDMX or MIPS-3D instructions.  */
2980       if (strcmp (fmt, insn.insn_mo->args) == 0
2981           && insn.insn_mo->pinfo != INSN_MACRO
2982           && OPCODE_IS_MEMBER (insn.insn_mo,
2983                                (mips_opts.isa
2984                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2985                                mips_arch)
2986           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2987         break;
2988
2989       ++insn.insn_mo;
2990       assert (insn.insn_mo->name);
2991       assert (strcmp (name, insn.insn_mo->name) == 0);
2992     }
2993
2994   insn.insn_opcode = insn.insn_mo->match;
2995   for (;;)
2996     {
2997       switch (*fmt++)
2998         {
2999         case '\0':
3000           break;
3001
3002         case ',':
3003         case '(':
3004         case ')':
3005           continue;
3006
3007         case 't':
3008         case 'w':
3009         case 'E':
3010           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3011           continue;
3012
3013         case 'c':
3014           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3015           continue;
3016
3017         case 'T':
3018         case 'W':
3019           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3020           continue;
3021
3022         case 'd':
3023         case 'G':
3024         case 'K':
3025           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3026           continue;
3027
3028         case 'U':
3029           {
3030             int tmp = va_arg (args, int);
3031
3032             insn.insn_opcode |= tmp << OP_SH_RT;
3033             insn.insn_opcode |= tmp << OP_SH_RD;
3034             continue;
3035           }
3036
3037         case 'V':
3038         case 'S':
3039           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3040           continue;
3041
3042         case 'z':
3043           continue;
3044
3045         case '<':
3046           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3047           continue;
3048
3049         case 'D':
3050           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3051           continue;
3052
3053         case 'B':
3054           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3055           continue;
3056
3057         case 'J':
3058           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3059           continue;
3060
3061         case 'q':
3062           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3063           continue;
3064
3065         case 'b':
3066         case 's':
3067         case 'r':
3068         case 'v':
3069           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3070           continue;
3071
3072         case 'i':
3073         case 'j':
3074         case 'o':
3075           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3076           assert (*r == BFD_RELOC_GPREL16
3077                   || *r == BFD_RELOC_MIPS_LITERAL
3078                   || *r == BFD_RELOC_MIPS_HIGHER
3079                   || *r == BFD_RELOC_HI16_S
3080                   || *r == BFD_RELOC_LO16
3081                   || *r == BFD_RELOC_MIPS_GOT16
3082                   || *r == BFD_RELOC_MIPS_CALL16
3083                   || *r == BFD_RELOC_MIPS_GOT_DISP
3084                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3085                   || *r == BFD_RELOC_MIPS_GOT_OFST
3086                   || *r == BFD_RELOC_MIPS_GOT_LO16
3087                   || *r == BFD_RELOC_MIPS_CALL_LO16
3088                   || (ep->X_op == O_subtract
3089                       && *r == BFD_RELOC_PCREL_LO16));
3090           continue;
3091
3092         case 'u':
3093           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3094           assert (ep != NULL
3095                   && (ep->X_op == O_constant
3096                       || (ep->X_op == O_symbol
3097                           && (*r == BFD_RELOC_MIPS_HIGHEST
3098                               || *r == BFD_RELOC_HI16_S
3099                               || *r == BFD_RELOC_HI16
3100                               || *r == BFD_RELOC_GPREL16
3101                               || *r == BFD_RELOC_MIPS_GOT_HI16
3102                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3103                       || (ep->X_op == O_subtract
3104                           && *r == BFD_RELOC_PCREL_HI16_S)));
3105           continue;
3106
3107         case 'p':
3108           assert (ep != NULL);
3109           /*
3110            * This allows macro() to pass an immediate expression for
3111            * creating short branches without creating a symbol.
3112            * Note that the expression still might come from the assembly
3113            * input, in which case the value is not checked for range nor
3114            * is a relocation entry generated (yuck).
3115            */
3116           if (ep->X_op == O_constant)
3117             {
3118               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3119               ep = NULL;
3120             }
3121           else
3122             *r = BFD_RELOC_16_PCREL_S2;
3123           continue;
3124
3125         case 'a':
3126           assert (ep != NULL);
3127           *r = BFD_RELOC_MIPS_JMP;
3128           continue;
3129
3130         case 'C':
3131           insn.insn_opcode |= va_arg (args, unsigned long);
3132           continue;
3133
3134         default:
3135           internalError ();
3136         }
3137       break;
3138     }
3139   va_end (args);
3140   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3141
3142   append_insn (place, &insn, ep, r);
3143 }
3144
3145 static void
3146 mips16_macro_build (place, counter, ep, name, fmt, args)
3147      char *place;
3148      int *counter ATTRIBUTE_UNUSED;
3149      expressionS *ep;
3150      const char *name;
3151      const char *fmt;
3152      va_list args;
3153 {
3154   struct mips_cl_insn insn;
3155   bfd_reloc_code_real_type r[3]
3156     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3157
3158   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3159   assert (insn.insn_mo);
3160   assert (strcmp (name, insn.insn_mo->name) == 0);
3161
3162   while (strcmp (fmt, insn.insn_mo->args) != 0
3163          || insn.insn_mo->pinfo == INSN_MACRO)
3164     {
3165       ++insn.insn_mo;
3166       assert (insn.insn_mo->name);
3167       assert (strcmp (name, insn.insn_mo->name) == 0);
3168     }
3169
3170   insn.insn_opcode = insn.insn_mo->match;
3171   insn.use_extend = FALSE;
3172
3173   for (;;)
3174     {
3175       int c;
3176
3177       c = *fmt++;
3178       switch (c)
3179         {
3180         case '\0':
3181           break;
3182
3183         case ',':
3184         case '(':
3185         case ')':
3186           continue;
3187
3188         case 'y':
3189         case 'w':
3190           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3191           continue;
3192
3193         case 'x':
3194         case 'v':
3195           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3196           continue;
3197
3198         case 'z':
3199           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3200           continue;
3201
3202         case 'Z':
3203           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3204           continue;
3205
3206         case '0':
3207         case 'S':
3208         case 'P':
3209         case 'R':
3210           continue;
3211
3212         case 'X':
3213           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3214           continue;
3215
3216         case 'Y':
3217           {
3218             int regno;
3219
3220             regno = va_arg (args, int);
3221             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3222             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3223           }
3224           continue;
3225
3226         case '<':
3227         case '>':
3228         case '4':
3229         case '5':
3230         case 'H':
3231         case 'W':
3232         case 'D':
3233         case 'j':
3234         case '8':
3235         case 'V':
3236         case 'C':
3237         case 'U':
3238         case 'k':
3239         case 'K':
3240         case 'p':
3241         case 'q':
3242           {
3243             assert (ep != NULL);
3244
3245             if (ep->X_op != O_constant)
3246               *r = (int) BFD_RELOC_UNUSED + c;
3247             else
3248               {
3249                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3250                               FALSE, &insn.insn_opcode, &insn.use_extend,
3251                               &insn.extend);
3252                 ep = NULL;
3253                 *r = BFD_RELOC_UNUSED;
3254               }
3255           }
3256           continue;
3257
3258         case '6':
3259           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3260           continue;
3261         }
3262
3263       break;
3264     }
3265
3266   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3267
3268   append_insn (place, &insn, ep, r);
3269 }
3270
3271 /*
3272  * Generate a "jalr" instruction with a relocation hint to the called
3273  * function.  This occurs in NewABI PIC code.
3274  */
3275 static void
3276 macro_build_jalr (icnt, ep)
3277      int icnt;
3278      expressionS *ep;
3279 {
3280   char *f;
3281
3282   if (HAVE_NEWABI)
3283     {
3284       frag_grow (4);
3285       f = frag_more (0);
3286     }
3287   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3288                RA, PIC_CALL_REG);
3289   if (HAVE_NEWABI)
3290     fix_new_exp (frag_now, f - frag_now->fr_literal,
3291                  0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3292 }
3293
3294 /*
3295  * Generate a "lui" instruction.
3296  */
3297 static void
3298 macro_build_lui (place, counter, ep, regnum)
3299      char *place;
3300      int *counter;
3301      expressionS *ep;
3302      int regnum;
3303 {
3304   expressionS high_expr;
3305   struct mips_cl_insn insn;
3306   bfd_reloc_code_real_type r[3]
3307     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3308   const char *name = "lui";
3309   const char *fmt = "t,u";
3310
3311   assert (! mips_opts.mips16);
3312
3313   if (place == NULL)
3314     high_expr = *ep;
3315   else
3316     {
3317       high_expr.X_op = O_constant;
3318       high_expr.X_add_number = ep->X_add_number;
3319     }
3320
3321   if (high_expr.X_op == O_constant)
3322     {
3323       /* we can compute the instruction now without a relocation entry */
3324       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3325                                 >> 16) & 0xffff;
3326       *r = BFD_RELOC_UNUSED;
3327     }
3328   else
3329     {
3330       assert (ep->X_op == O_symbol);
3331       /* _gp_disp is a special case, used from s_cpload.  */
3332       assert (mips_pic == NO_PIC
3333               || (! HAVE_NEWABI
3334                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3335       *r = BFD_RELOC_HI16_S;
3336     }
3337
3338   /*
3339    * If the macro is about to expand into a second instruction,
3340    * print a warning if needed. We need to pass ip as a parameter
3341    * to generate a better warning message here...
3342    */
3343   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3344     as_warn (_("Macro instruction expanded into multiple instructions"));
3345
3346   if (place == NULL)
3347     ++*counter;         /* bump instruction counter */
3348
3349   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3350   assert (insn.insn_mo);
3351   assert (strcmp (name, insn.insn_mo->name) == 0);
3352   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3353
3354   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3355   if (*r == BFD_RELOC_UNUSED)
3356     {
3357       insn.insn_opcode |= high_expr.X_add_number;
3358       append_insn (place, &insn, NULL, r);
3359     }
3360   else
3361     append_insn (place, &insn, &high_expr, r);
3362 }
3363
3364 /* Generate a sequence of instructions to do a load or store from a constant
3365    offset off of a base register (breg) into/from a target register (treg),
3366    using AT if necessary.  */
3367 static void
3368 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3369      char *place;
3370      int *counter;
3371      expressionS *ep;
3372      const char *op;
3373      int treg, breg;
3374 {
3375   assert (ep->X_op == O_constant);
3376
3377   /* Right now, this routine can only handle signed 32-bit contants.  */
3378   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3379     as_warn (_("operand overflow"));
3380
3381   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3382     {
3383       /* Signed 16-bit offset will fit in the op.  Easy!  */
3384       macro_build (place, counter, ep, op, "t,o(b)", treg,
3385                    (int) BFD_RELOC_LO16, breg);
3386     }
3387   else
3388     {
3389       /* 32-bit offset, need multiple instructions and AT, like:
3390            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3391            addu     $tempreg,$tempreg,$breg
3392            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3393          to handle the complete offset.  */
3394       macro_build_lui (place, counter, ep, AT);
3395       if (place != NULL)
3396         place += 4;
3397       macro_build (place, counter, (expressionS *) NULL,
3398                    HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3399                    "d,v,t", AT, AT, breg);
3400       if (place != NULL)
3401         place += 4;
3402       macro_build (place, counter, ep, op, "t,o(b)", treg,
3403                    (int) BFD_RELOC_LO16, AT);
3404
3405       if (mips_opts.noat)
3406         as_warn (_("Macro used $at after \".set noat\""));
3407     }
3408 }
3409
3410 /*                      set_at()
3411  * Generates code to set the $at register to true (one)
3412  * if reg is less than the immediate expression.
3413  */
3414 static void
3415 set_at (counter, reg, unsignedp)
3416      int *counter;
3417      int reg;
3418      int unsignedp;
3419 {
3420   if (imm_expr.X_op == O_constant
3421       && imm_expr.X_add_number >= -0x8000
3422       && imm_expr.X_add_number < 0x8000)
3423     macro_build ((char *) NULL, counter, &imm_expr,
3424                  unsignedp ? "sltiu" : "slti",
3425                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3426   else
3427     {
3428       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3429       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3430                    unsignedp ? "sltu" : "slt",
3431                    "d,v,t", AT, reg, AT);
3432     }
3433 }
3434
3435 /* Warn if an expression is not a constant.  */
3436
3437 static void
3438 check_absolute_expr (ip, ex)
3439      struct mips_cl_insn *ip;
3440      expressionS *ex;
3441 {
3442   if (ex->X_op == O_big)
3443     as_bad (_("unsupported large constant"));
3444   else if (ex->X_op != O_constant)
3445     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3446 }
3447
3448 /* Count the leading zeroes by performing a binary chop. This is a
3449    bulky bit of source, but performance is a LOT better for the
3450    majority of values than a simple loop to count the bits:
3451        for (lcnt = 0; (lcnt < 32); lcnt++)
3452          if ((v) & (1 << (31 - lcnt)))
3453            break;
3454   However it is not code size friendly, and the gain will drop a bit
3455   on certain cached systems.
3456 */
3457 #define COUNT_TOP_ZEROES(v)             \
3458   (((v) & ~0xffff) == 0                 \
3459    ? ((v) & ~0xff) == 0                 \
3460      ? ((v) & ~0xf) == 0                \
3461        ? ((v) & ~0x3) == 0              \
3462          ? ((v) & ~0x1) == 0            \
3463            ? !(v)                       \
3464              ? 32                       \
3465              : 31                       \
3466            : 30                         \
3467          : ((v) & ~0x7) == 0            \
3468            ? 29                         \
3469            : 28                         \
3470        : ((v) & ~0x3f) == 0             \
3471          ? ((v) & ~0x1f) == 0           \
3472            ? 27                         \
3473            : 26                         \
3474          : ((v) & ~0x7f) == 0           \
3475            ? 25                         \
3476            : 24                         \
3477      : ((v) & ~0xfff) == 0              \
3478        ? ((v) & ~0x3ff) == 0            \
3479          ? ((v) & ~0x1ff) == 0          \
3480            ? 23                         \
3481            : 22                         \
3482          : ((v) & ~0x7ff) == 0          \
3483            ? 21                         \
3484            : 20                         \
3485        : ((v) & ~0x3fff) == 0           \
3486          ? ((v) & ~0x1fff) == 0         \
3487            ? 19                         \
3488            : 18                         \
3489          : ((v) & ~0x7fff) == 0         \
3490            ? 17                         \
3491            : 16                         \
3492    : ((v) & ~0xffffff) == 0             \
3493      ? ((v) & ~0xfffff) == 0            \
3494        ? ((v) & ~0x3ffff) == 0          \
3495          ? ((v) & ~0x1ffff) == 0        \
3496            ? 15                         \
3497            : 14                         \
3498          : ((v) & ~0x7ffff) == 0        \
3499            ? 13                         \
3500            : 12                         \
3501        : ((v) & ~0x3fffff) == 0         \
3502          ? ((v) & ~0x1fffff) == 0       \
3503            ? 11                         \
3504            : 10                         \
3505          : ((v) & ~0x7fffff) == 0       \
3506            ? 9                          \
3507            : 8                          \
3508      : ((v) & ~0xfffffff) == 0          \
3509        ? ((v) & ~0x3ffffff) == 0        \
3510          ? ((v) & ~0x1ffffff) == 0      \
3511            ? 7                          \
3512            : 6                          \
3513          : ((v) & ~0x7ffffff) == 0      \
3514            ? 5                          \
3515            : 4                          \
3516        : ((v) & ~0x3fffffff) == 0       \
3517          ? ((v) & ~0x1fffffff) == 0     \
3518            ? 3                          \
3519            : 2                          \
3520          : ((v) & ~0x7fffffff) == 0     \
3521            ? 1                          \
3522            : 0)
3523
3524 /*                      load_register()
3525  *  This routine generates the least number of instructions neccessary to load
3526  *  an absolute expression value into a register.
3527  */
3528 static void
3529 load_register (counter, reg, ep, dbl)
3530      int *counter;
3531      int reg;
3532      expressionS *ep;
3533      int dbl;
3534 {
3535   int freg;
3536   expressionS hi32, lo32;
3537
3538   if (ep->X_op != O_big)
3539     {
3540       assert (ep->X_op == O_constant);
3541       if (ep->X_add_number < 0x8000
3542           && (ep->X_add_number >= 0
3543               || (ep->X_add_number >= -0x8000
3544                   && (! dbl
3545                       || ! ep->X_unsigned
3546                       || sizeof (ep->X_add_number) > 4))))
3547         {
3548           /* We can handle 16 bit signed values with an addiu to
3549              $zero.  No need to ever use daddiu here, since $zero and
3550              the result are always correct in 32 bit mode.  */
3551           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3552                        (int) BFD_RELOC_LO16);
3553           return;
3554         }
3555       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3556         {
3557           /* We can handle 16 bit unsigned values with an ori to
3558              $zero.  */
3559           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3560                        (int) BFD_RELOC_LO16);
3561           return;
3562         }
3563       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3564                 && (! dbl
3565                     || ! ep->X_unsigned
3566                     || sizeof (ep->X_add_number) > 4
3567                     || (ep->X_add_number & 0x80000000) == 0))
3568                || ((HAVE_32BIT_GPRS || ! dbl)
3569                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3570                || (HAVE_32BIT_GPRS
3571                    && ! dbl
3572                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3573                        == ~ (offsetT) 0xffffffff)))
3574         {
3575           /* 32 bit values require an lui.  */
3576           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3577                        (int) BFD_RELOC_HI16);
3578           if ((ep->X_add_number & 0xffff) != 0)
3579             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3580                          (int) BFD_RELOC_LO16);
3581           return;
3582         }
3583     }
3584
3585   /* The value is larger than 32 bits.  */
3586
3587   if (HAVE_32BIT_GPRS)
3588     {
3589       as_bad (_("Number (0x%lx) larger than 32 bits"),
3590               (unsigned long) ep->X_add_number);
3591       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3592                    (int) BFD_RELOC_LO16);
3593       return;
3594     }
3595
3596   if (ep->X_op != O_big)
3597     {
3598       hi32 = *ep;
3599       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3600       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3601       hi32.X_add_number &= 0xffffffff;
3602       lo32 = *ep;
3603       lo32.X_add_number &= 0xffffffff;
3604     }
3605   else
3606     {
3607       assert (ep->X_add_number > 2);
3608       if (ep->X_add_number == 3)
3609         generic_bignum[3] = 0;
3610       else if (ep->X_add_number > 4)
3611         as_bad (_("Number larger than 64 bits"));
3612       lo32.X_op = O_constant;
3613       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3614       hi32.X_op = O_constant;
3615       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3616     }
3617
3618   if (hi32.X_add_number == 0)
3619     freg = 0;
3620   else
3621     {
3622       int shift, bit;
3623       unsigned long hi, lo;
3624
3625       if (hi32.X_add_number == (offsetT) 0xffffffff)
3626         {
3627           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3628             {
3629               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3630                            reg, 0, (int) BFD_RELOC_LO16);
3631               return;
3632             }
3633           if (lo32.X_add_number & 0x80000000)
3634             {
3635               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3636                            (int) BFD_RELOC_HI16);
3637               if (lo32.X_add_number & 0xffff)
3638                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3639                              reg, reg, (int) BFD_RELOC_LO16);
3640               return;
3641             }
3642         }
3643
3644       /* Check for 16bit shifted constant.  We know that hi32 is
3645          non-zero, so start the mask on the first bit of the hi32
3646          value.  */
3647       shift = 17;
3648       do
3649         {
3650           unsigned long himask, lomask;
3651
3652           if (shift < 32)
3653             {
3654               himask = 0xffff >> (32 - shift);
3655               lomask = (0xffff << shift) & 0xffffffff;
3656             }
3657           else
3658             {
3659               himask = 0xffff << (shift - 32);
3660               lomask = 0;
3661             }
3662           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3663               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3664             {
3665               expressionS tmp;
3666
3667               tmp.X_op = O_constant;
3668               if (shift < 32)
3669                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3670                                     | (lo32.X_add_number >> shift));
3671               else
3672                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3673               macro_build ((char *) NULL, counter, &tmp,
3674                            "ori", "t,r,i", reg, 0,
3675                            (int) BFD_RELOC_LO16);
3676               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3677                            (shift >= 32) ? "dsll32" : "dsll",
3678                            "d,w,<", reg, reg,
3679                            (shift >= 32) ? shift - 32 : shift);
3680               return;
3681             }
3682           ++shift;
3683         }
3684       while (shift <= (64 - 16));
3685
3686       /* Find the bit number of the lowest one bit, and store the
3687          shifted value in hi/lo.  */
3688       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3689       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3690       if (lo != 0)
3691         {
3692           bit = 0;
3693           while ((lo & 1) == 0)
3694             {
3695               lo >>= 1;
3696               ++bit;
3697             }
3698           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3699           hi >>= bit;
3700         }
3701       else
3702         {
3703           bit = 32;
3704           while ((hi & 1) == 0)
3705             {
3706               hi >>= 1;
3707               ++bit;
3708             }
3709           lo = hi;
3710           hi = 0;
3711         }
3712
3713       /* Optimize if the shifted value is a (power of 2) - 1.  */
3714       if ((hi == 0 && ((lo + 1) & lo) == 0)
3715           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3716         {
3717           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3718           if (shift != 0)
3719             {
3720               expressionS tmp;
3721
3722               /* This instruction will set the register to be all
3723                  ones.  */
3724               tmp.X_op = O_constant;
3725               tmp.X_add_number = (offsetT) -1;
3726               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3727                            reg, 0, (int) BFD_RELOC_LO16);
3728               if (bit != 0)
3729                 {
3730                   bit += shift;
3731                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3732                                (bit >= 32) ? "dsll32" : "dsll",
3733                                "d,w,<", reg, reg,
3734                                (bit >= 32) ? bit - 32 : bit);
3735                 }
3736               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3737                            (shift >= 32) ? "dsrl32" : "dsrl",
3738                            "d,w,<", reg, reg,
3739                            (shift >= 32) ? shift - 32 : shift);
3740               return;
3741             }
3742         }
3743
3744       /* Sign extend hi32 before calling load_register, because we can
3745          generally get better code when we load a sign extended value.  */
3746       if ((hi32.X_add_number & 0x80000000) != 0)
3747         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3748       load_register (counter, reg, &hi32, 0);
3749       freg = reg;
3750     }
3751   if ((lo32.X_add_number & 0xffff0000) == 0)
3752     {
3753       if (freg != 0)
3754         {
3755           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3756                        "dsll32", "d,w,<", reg, freg, 0);
3757           freg = reg;
3758         }
3759     }
3760   else
3761     {
3762       expressionS mid16;
3763
3764       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3765         {
3766           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3767                        (int) BFD_RELOC_HI16);
3768           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3769                        "dsrl32", "d,w,<", reg, reg, 0);
3770           return;
3771         }
3772
3773       if (freg != 0)
3774         {
3775           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3776                        "d,w,<", reg, freg, 16);
3777           freg = reg;
3778         }
3779       mid16 = lo32;
3780       mid16.X_add_number >>= 16;
3781       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3782                    freg, (int) BFD_RELOC_LO16);
3783       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3784                    "d,w,<", reg, reg, 16);
3785       freg = reg;
3786     }
3787   if ((lo32.X_add_number & 0xffff) != 0)
3788     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3789                  (int) BFD_RELOC_LO16);
3790 }
3791
3792 /* Load an address into a register.  */
3793
3794 static void
3795 load_address (counter, reg, ep, used_at)
3796      int *counter;
3797      int reg;
3798      expressionS *ep;
3799      int *used_at;
3800 {
3801   char *p = NULL;
3802
3803   if (ep->X_op != O_constant
3804       && ep->X_op != O_symbol)
3805     {
3806       as_bad (_("expression too complex"));
3807       ep->X_op = O_constant;
3808     }
3809
3810   if (ep->X_op == O_constant)
3811     {
3812       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3813       return;
3814     }
3815
3816   if (mips_pic == NO_PIC)
3817     {
3818       /* If this is a reference to a GP relative symbol, we want
3819            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3820          Otherwise we want
3821            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3822            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3823          If we have an addend, we always use the latter form.
3824
3825          With 64bit address space and a usable $at we want
3826            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3827            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3828            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3829            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3830            dsll32       $reg,0
3831            daddu        $reg,$reg,$at
3832
3833          If $at is already in use, we use a path which is suboptimal
3834          on superscalar processors.
3835            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3836            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3837            dsll         $reg,16
3838            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3839            dsll         $reg,16
3840            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3841        */
3842       if (HAVE_64BIT_ADDRESSES)
3843         {
3844           /* We don't do GP optimization for now because RELAX_ENCODE can't
3845              hold the data for such large chunks.  */
3846
3847           if (*used_at == 0 && ! mips_opts.noat)
3848             {
3849               macro_build (p, counter, ep, "lui", "t,u",
3850                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3851               macro_build (p, counter, ep, "lui", "t,u",
3852                            AT, (int) BFD_RELOC_HI16_S);
3853               macro_build (p, counter, ep, "daddiu", "t,r,j",
3854                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3855               macro_build (p, counter, ep, "daddiu", "t,r,j",
3856                            AT, AT, (int) BFD_RELOC_LO16);
3857               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3858                            "d,w,<", reg, reg, 0);
3859               macro_build (p, counter, (expressionS *) NULL, "daddu",
3860                            "d,v,t", reg, reg, AT);
3861               *used_at = 1;
3862             }
3863           else
3864             {
3865               macro_build (p, counter, ep, "lui", "t,u",
3866                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3867               macro_build (p, counter, ep, "daddiu", "t,r,j",
3868                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3869               macro_build (p, counter, (expressionS *) NULL, "dsll",
3870                            "d,w,<", reg, reg, 16);
3871               macro_build (p, counter, ep, "daddiu", "t,r,j",
3872                            reg, reg, (int) BFD_RELOC_HI16_S);
3873               macro_build (p, counter, (expressionS *) NULL, "dsll",
3874                            "d,w,<", reg, reg, 16);
3875               macro_build (p, counter, ep, "daddiu", "t,r,j",
3876                            reg, reg, (int) BFD_RELOC_LO16);
3877             }
3878         }
3879       else
3880         {
3881           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3882               && ! nopic_need_relax (ep->X_add_symbol, 1))
3883             {
3884               frag_grow (20);
3885               macro_build ((char *) NULL, counter, ep,
3886                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3887                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3888               p = frag_var (rs_machine_dependent, 8, 0,
3889                             RELAX_ENCODE (4, 8, 0, 4, 0,
3890                                           mips_opts.warn_about_macros),
3891                             ep->X_add_symbol, 0, NULL);
3892             }
3893           macro_build_lui (p, counter, ep, reg);
3894           if (p != NULL)
3895             p += 4;
3896           macro_build (p, counter, ep,
3897                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3898                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3899         }
3900     }
3901   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3902     {
3903       expressionS ex;
3904
3905       /* If this is a reference to an external symbol, we want
3906            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3907          Otherwise we want
3908            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3909            nop
3910            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3911          If we have NewABI, we want
3912            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
3913          If there is a constant, it must be added in after.  */
3914       ex.X_add_number = ep->X_add_number;
3915       ep->X_add_number = 0;
3916       frag_grow (20);
3917       if (HAVE_NEWABI)
3918         {
3919           macro_build ((char *) NULL, counter, ep,
3920                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3921                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3922         }
3923       else
3924         {
3925           macro_build ((char *) NULL, counter, ep,
3926                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3927                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3928           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3929           p = frag_var (rs_machine_dependent, 4, 0,
3930                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3931                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3932           macro_build (p, counter, ep,
3933                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3934                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3935         }
3936
3937       if (ex.X_add_number != 0)
3938         {
3939           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941           ex.X_op = O_constant;
3942           macro_build ((char *) NULL, counter, &ex,
3943                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3944                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3945         }
3946     }
3947   else if (mips_pic == SVR4_PIC)
3948     {
3949       expressionS ex;
3950       int off;
3951
3952       /* This is the large GOT case.  If this is a reference to an
3953          external symbol, we want
3954            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3955            addu         $reg,$reg,$gp
3956            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3957          Otherwise, for a reference to a local symbol, we want
3958            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3959            nop
3960            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3961          If we have NewABI, we want
3962            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3963            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3964          If there is a constant, it must be added in after.  */
3965       ex.X_add_number = ep->X_add_number;
3966       ep->X_add_number = 0;
3967       if (HAVE_NEWABI)
3968         {
3969           macro_build ((char *) NULL, counter, ep,
3970                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3971                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3972           macro_build (p, counter, ep,
3973                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3974                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3975         }
3976       else
3977         {
3978           if (reg_needs_delay (mips_gp_register))
3979             off = 4;
3980           else
3981             off = 0;
3982           frag_grow (32);
3983           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3984                        (int) BFD_RELOC_MIPS_GOT_HI16);
3985           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3986                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3987                        reg, mips_gp_register);
3988           macro_build ((char *) NULL, counter, ep,
3989                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3990                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3991           p = frag_var (rs_machine_dependent, 12 + off, 0,
3992                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3993                                       mips_opts.warn_about_macros),
3994                         ep->X_add_symbol, 0, NULL);
3995           if (off > 0)
3996             {
3997               /* We need a nop before loading from $gp.  This special
3998                  check is required because the lui which starts the main
3999                  instruction stream does not refer to $gp, and so will not
4000                  insert the nop which may be required.  */
4001               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4002                 p += 4;
4003             }
4004           macro_build (p, counter, ep,
4005                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4006                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4007           p += 4;
4008           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4009           p += 4;
4010           macro_build (p, counter, ep,
4011                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4012                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4013         }
4014
4015       if (ex.X_add_number != 0)
4016         {
4017           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4018             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4019           ex.X_op = O_constant;
4020           macro_build ((char *) NULL, counter, &ex,
4021                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4022                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4023         }
4024     }
4025   else if (mips_pic == EMBEDDED_PIC)
4026     {
4027       /* We always do
4028            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4029        */
4030       macro_build ((char *) NULL, counter, ep,
4031                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4032                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4033     }
4034   else
4035     abort ();
4036 }
4037
4038 /* Move the contents of register SOURCE into register DEST.  */
4039
4040 static void
4041 move_register (counter, dest, source)
4042      int *counter;
4043      int dest;
4044      int source;
4045 {
4046   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4047                HAVE_32BIT_GPRS ? "addu" : "daddu",
4048                "d,v,t", dest, source, 0);
4049 }
4050
4051 /*
4052  *                      Build macros
4053  *   This routine implements the seemingly endless macro or synthesized
4054  * instructions and addressing modes in the mips assembly language. Many
4055  * of these macros are simple and are similar to each other. These could
4056  * probably be handled by some kind of table or grammer aproach instead of
4057  * this verbose method. Others are not simple macros but are more like
4058  * optimizing code generation.
4059  *   One interesting optimization is when several store macros appear
4060  * consecutivly that would load AT with the upper half of the same address.
4061  * The ensuing load upper instructions are ommited. This implies some kind
4062  * of global optimization. We currently only optimize within a single macro.
4063  *   For many of the load and store macros if the address is specified as a
4064  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4065  * first load register 'at' with zero and use it as the base register. The
4066  * mips assembler simply uses register $zero. Just one tiny optimization
4067  * we're missing.
4068  */
4069 static void
4070 macro (ip)
4071      struct mips_cl_insn *ip;
4072 {
4073   register int treg, sreg, dreg, breg;
4074   int tempreg;
4075   int mask;
4076   int icnt = 0;
4077   int used_at = 0;
4078   expressionS expr1;
4079   const char *s;
4080   const char *s2;
4081   const char *fmt;
4082   int likely = 0;
4083   int dbl = 0;
4084   int coproc = 0;
4085   int lr = 0;
4086   int imm = 0;
4087   offsetT maxnum;
4088   int off;
4089   bfd_reloc_code_real_type r;
4090   int hold_mips_optimize;
4091
4092   assert (! mips_opts.mips16);
4093
4094   treg = (ip->insn_opcode >> 16) & 0x1f;
4095   dreg = (ip->insn_opcode >> 11) & 0x1f;
4096   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4097   mask = ip->insn_mo->mask;
4098
4099   expr1.X_op = O_constant;
4100   expr1.X_op_symbol = NULL;
4101   expr1.X_add_symbol = NULL;
4102   expr1.X_add_number = 1;
4103
4104   /* Umatched fixups should not be put in the same frag as a relaxable
4105      macro.  For example, suppose we have:
4106
4107         lui $4,%hi(l1)          # 1
4108         la $5,l2                # 2
4109         addiu $4,$4,%lo(l1)     # 3
4110
4111      If instructions 1 and 2 were put in the same frag, md_frob_file would
4112      move the fixup for #1 after the fixups for the "unrelaxed" version of
4113      #2.  This would confuse tc_gen_reloc, which expects the relocations
4114      for #2 to be the last for that frag.
4115
4116      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4117      it assumes that they belong to a relaxable macro.  We mustn't put
4118      other uses of such relocations into a variant frag.
4119
4120      To avoid both problems, finish the current frag it contains a
4121      %reloc() operator.  The macro then goes into a new frag.  */
4122   if (prev_reloc_op_frag == frag_now)
4123     {
4124       frag_wane (frag_now);
4125       frag_new (0);
4126     }
4127
4128   switch (mask)
4129     {
4130     case M_DABS:
4131       dbl = 1;
4132     case M_ABS:
4133       /* bgez $a0,.+12
4134          move v0,$a0
4135          sub v0,$zero,$a0
4136          */
4137
4138       mips_emit_delays (TRUE);
4139       ++mips_opts.noreorder;
4140       mips_any_noreorder = 1;
4141
4142       expr1.X_add_number = 8;
4143       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4144       if (dreg == sreg)
4145         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4146                      0);
4147       else
4148         move_register (&icnt, dreg, sreg);
4149       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4150                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4151
4152       --mips_opts.noreorder;
4153       return;
4154
4155     case M_ADD_I:
4156       s = "addi";
4157       s2 = "add";
4158       goto do_addi;
4159     case M_ADDU_I:
4160       s = "addiu";
4161       s2 = "addu";
4162       goto do_addi;
4163     case M_DADD_I:
4164       dbl = 1;
4165       s = "daddi";
4166       s2 = "dadd";
4167       goto do_addi;
4168     case M_DADDU_I:
4169       dbl = 1;
4170       s = "daddiu";
4171       s2 = "daddu";
4172     do_addi:
4173       if (imm_expr.X_op == O_constant
4174           && imm_expr.X_add_number >= -0x8000
4175           && imm_expr.X_add_number < 0x8000)
4176         {
4177           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4178                        (int) BFD_RELOC_LO16);
4179           return;
4180         }
4181       load_register (&icnt, AT, &imm_expr, dbl);
4182       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4183                    treg, sreg, AT);
4184       break;
4185
4186     case M_AND_I:
4187       s = "andi";
4188       s2 = "and";
4189       goto do_bit;
4190     case M_OR_I:
4191       s = "ori";
4192       s2 = "or";
4193       goto do_bit;
4194     case M_NOR_I:
4195       s = "";
4196       s2 = "nor";
4197       goto do_bit;
4198     case M_XOR_I:
4199       s = "xori";
4200       s2 = "xor";
4201     do_bit:
4202       if (imm_expr.X_op == O_constant
4203           && imm_expr.X_add_number >= 0
4204           && imm_expr.X_add_number < 0x10000)
4205         {
4206           if (mask != M_NOR_I)
4207             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4208                          sreg, (int) BFD_RELOC_LO16);
4209           else
4210             {
4211               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4212                            treg, sreg, (int) BFD_RELOC_LO16);
4213               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4214                            "d,v,t", treg, treg, 0);
4215             }
4216           return;
4217         }
4218
4219       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4220       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4221                    treg, sreg, AT);
4222       break;
4223
4224     case M_BEQ_I:
4225       s = "beq";
4226       goto beq_i;
4227     case M_BEQL_I:
4228       s = "beql";
4229       likely = 1;
4230       goto beq_i;
4231     case M_BNE_I:
4232       s = "bne";
4233       goto beq_i;
4234     case M_BNEL_I:
4235       s = "bnel";
4236       likely = 1;
4237     beq_i:
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4239         {
4240           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4241                        0);
4242           return;
4243         }
4244       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4245       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4246       break;
4247
4248     case M_BGEL:
4249       likely = 1;
4250     case M_BGE:
4251       if (treg == 0)
4252         {
4253           macro_build ((char *) NULL, &icnt, &offset_expr,
4254                        likely ? "bgezl" : "bgez", "s,p", sreg);
4255           return;
4256         }
4257       if (sreg == 0)
4258         {
4259           macro_build ((char *) NULL, &icnt, &offset_expr,
4260                        likely ? "blezl" : "blez", "s,p", treg);
4261           return;
4262         }
4263       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4264                    AT, sreg, treg);
4265       macro_build ((char *) NULL, &icnt, &offset_expr,
4266                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4267       break;
4268
4269     case M_BGTL_I:
4270       likely = 1;
4271     case M_BGT_I:
4272       /* check for > max integer */
4273       maxnum = 0x7fffffff;
4274       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4275         {
4276           maxnum <<= 16;
4277           maxnum |= 0xffff;
4278           maxnum <<= 16;
4279           maxnum |= 0xffff;
4280         }
4281       if (imm_expr.X_op == O_constant
4282           && imm_expr.X_add_number >= maxnum
4283           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4284         {
4285         do_false:
4286           /* result is always false */
4287           if (! likely)
4288             {
4289               if (warn_nops)
4290                 as_warn (_("Branch %s is always false (nop)"),
4291                          ip->insn_mo->name);
4292               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4293                            "", 0);
4294             }
4295           else
4296             {
4297               if (warn_nops)
4298                 as_warn (_("Branch likely %s is always false"),
4299                          ip->insn_mo->name);
4300               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4301                            "s,t,p", 0, 0);
4302             }
4303           return;
4304         }
4305       if (imm_expr.X_op != O_constant)
4306         as_bad (_("Unsupported large constant"));
4307       ++imm_expr.X_add_number;
4308       /* FALLTHROUGH */
4309     case M_BGE_I:
4310     case M_BGEL_I:
4311       if (mask == M_BGEL_I)
4312         likely = 1;
4313       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4314         {
4315           macro_build ((char *) NULL, &icnt, &offset_expr,
4316                        likely ? "bgezl" : "bgez", "s,p", sreg);
4317           return;
4318         }
4319       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4320         {
4321           macro_build ((char *) NULL, &icnt, &offset_expr,
4322                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4323           return;
4324         }
4325       maxnum = 0x7fffffff;
4326       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4327         {
4328           maxnum <<= 16;
4329           maxnum |= 0xffff;
4330           maxnum <<= 16;
4331           maxnum |= 0xffff;
4332         }
4333       maxnum = - maxnum - 1;
4334       if (imm_expr.X_op == O_constant
4335           && imm_expr.X_add_number <= maxnum
4336           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4337         {
4338         do_true:
4339           /* result is always true */
4340           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4341           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4342           return;
4343         }
4344       set_at (&icnt, sreg, 0);
4345       macro_build ((char *) NULL, &icnt, &offset_expr,
4346                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4347       break;
4348
4349     case M_BGEUL:
4350       likely = 1;
4351     case M_BGEU:
4352       if (treg == 0)
4353         goto do_true;
4354       if (sreg == 0)
4355         {
4356           macro_build ((char *) NULL, &icnt, &offset_expr,
4357                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4358           return;
4359         }
4360       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4361                    "d,v,t", AT, sreg, treg);
4362       macro_build ((char *) NULL, &icnt, &offset_expr,
4363                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4364       break;
4365
4366     case M_BGTUL_I:
4367       likely = 1;
4368     case M_BGTU_I:
4369       if (sreg == 0
4370           || (HAVE_32BIT_GPRS
4371               && imm_expr.X_op == O_constant
4372               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4373         goto do_false;
4374       if (imm_expr.X_op != O_constant)
4375         as_bad (_("Unsupported large constant"));
4376       ++imm_expr.X_add_number;
4377       /* FALLTHROUGH */
4378     case M_BGEU_I:
4379     case M_BGEUL_I:
4380       if (mask == M_BGEUL_I)
4381         likely = 1;
4382       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4383         goto do_true;
4384       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4385         {
4386           macro_build ((char *) NULL, &icnt, &offset_expr,
4387                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4388           return;
4389         }
4390       set_at (&icnt, sreg, 1);
4391       macro_build ((char *) NULL, &icnt, &offset_expr,
4392                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4393       break;
4394
4395     case M_BGTL:
4396       likely = 1;
4397     case M_BGT:
4398       if (treg == 0)
4399         {
4400           macro_build ((char *) NULL, &icnt, &offset_expr,
4401                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4402           return;
4403         }
4404       if (sreg == 0)
4405         {
4406           macro_build ((char *) NULL, &icnt, &offset_expr,
4407                        likely ? "bltzl" : "bltz", "s,p", treg);
4408           return;
4409         }
4410       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4411                    AT, treg, sreg);
4412       macro_build ((char *) NULL, &icnt, &offset_expr,
4413                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4414       break;
4415
4416     case M_BGTUL:
4417       likely = 1;
4418     case M_BGTU:
4419       if (treg == 0)
4420         {
4421           macro_build ((char *) NULL, &icnt, &offset_expr,
4422                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4423           return;
4424         }
4425       if (sreg == 0)
4426         goto do_false;
4427       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4428                    "d,v,t", AT, treg, sreg);
4429       macro_build ((char *) NULL, &icnt, &offset_expr,
4430                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4431       break;
4432
4433     case M_BLEL:
4434       likely = 1;
4435     case M_BLE:
4436       if (treg == 0)
4437         {
4438           macro_build ((char *) NULL, &icnt, &offset_expr,
4439                        likely ? "blezl" : "blez", "s,p", sreg);
4440           return;
4441         }
4442       if (sreg == 0)
4443         {
4444           macro_build ((char *) NULL, &icnt, &offset_expr,
4445                        likely ? "bgezl" : "bgez", "s,p", treg);
4446           return;
4447         }
4448       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4449                    AT, treg, sreg);
4450       macro_build ((char *) NULL, &icnt, &offset_expr,
4451                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4452       break;
4453
4454     case M_BLEL_I:
4455       likely = 1;
4456     case M_BLE_I:
4457       maxnum = 0x7fffffff;
4458       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4459         {
4460           maxnum <<= 16;
4461           maxnum |= 0xffff;
4462           maxnum <<= 16;
4463           maxnum |= 0xffff;
4464         }
4465       if (imm_expr.X_op == O_constant
4466           && imm_expr.X_add_number >= maxnum
4467           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4468         goto do_true;
4469       if (imm_expr.X_op != O_constant)
4470         as_bad (_("Unsupported large constant"));
4471       ++imm_expr.X_add_number;
4472       /* FALLTHROUGH */
4473     case M_BLT_I:
4474     case M_BLTL_I:
4475       if (mask == M_BLTL_I)
4476         likely = 1;
4477       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4478         {
4479           macro_build ((char *) NULL, &icnt, &offset_expr,
4480                        likely ? "bltzl" : "bltz", "s,p", sreg);
4481           return;
4482         }
4483       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4484         {
4485           macro_build ((char *) NULL, &icnt, &offset_expr,
4486                        likely ? "blezl" : "blez", "s,p", sreg);
4487           return;
4488         }
4489       set_at (&icnt, sreg, 0);
4490       macro_build ((char *) NULL, &icnt, &offset_expr,
4491                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4492       break;
4493
4494     case M_BLEUL:
4495       likely = 1;
4496     case M_BLEU:
4497       if (treg == 0)
4498         {
4499           macro_build ((char *) NULL, &icnt, &offset_expr,
4500                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4501           return;
4502         }
4503       if (sreg == 0)
4504         goto do_true;
4505       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4506                    "d,v,t", AT, treg, sreg);
4507       macro_build ((char *) NULL, &icnt, &offset_expr,
4508                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4509       break;
4510
4511     case M_BLEUL_I:
4512       likely = 1;
4513     case M_BLEU_I:
4514       if (sreg == 0
4515           || (HAVE_32BIT_GPRS
4516               && imm_expr.X_op == O_constant
4517               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4518         goto do_true;
4519       if (imm_expr.X_op != O_constant)
4520         as_bad (_("Unsupported large constant"));
4521       ++imm_expr.X_add_number;
4522       /* FALLTHROUGH */
4523     case M_BLTU_I:
4524     case M_BLTUL_I:
4525       if (mask == M_BLTUL_I)
4526         likely = 1;
4527       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4528         goto do_false;
4529       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4530         {
4531           macro_build ((char *) NULL, &icnt, &offset_expr,
4532                        likely ? "beql" : "beq",
4533                        "s,t,p", sreg, 0);
4534           return;
4535         }
4536       set_at (&icnt, sreg, 1);
4537       macro_build ((char *) NULL, &icnt, &offset_expr,
4538                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4539       break;
4540
4541     case M_BLTL:
4542       likely = 1;
4543     case M_BLT:
4544       if (treg == 0)
4545         {
4546           macro_build ((char *) NULL, &icnt, &offset_expr,
4547                        likely ? "bltzl" : "bltz", "s,p", sreg);
4548           return;
4549         }
4550       if (sreg == 0)
4551         {
4552           macro_build ((char *) NULL, &icnt, &offset_expr,
4553                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4554           return;
4555         }
4556       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4557                    AT, sreg, treg);
4558       macro_build ((char *) NULL, &icnt, &offset_expr,
4559                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4560       break;
4561
4562     case M_BLTUL:
4563       likely = 1;
4564     case M_BLTU:
4565       if (treg == 0)
4566         goto do_false;
4567       if (sreg == 0)
4568         {
4569           macro_build ((char *) NULL, &icnt, &offset_expr,
4570                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4571           return;
4572         }
4573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4574                    "d,v,t", AT, sreg,
4575                    treg);
4576       macro_build ((char *) NULL, &icnt, &offset_expr,
4577                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4578       break;
4579
4580     case M_DDIV_3:
4581       dbl = 1;
4582     case M_DIV_3:
4583       s = "mflo";
4584       goto do_div3;
4585     case M_DREM_3:
4586       dbl = 1;
4587     case M_REM_3:
4588       s = "mfhi";
4589     do_div3:
4590       if (treg == 0)
4591         {
4592           as_warn (_("Divide by zero."));
4593           if (mips_trap)
4594             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4595                          "s,t,q", 0, 0, 7);
4596           else
4597             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4598                          "c", 7);
4599           return;
4600         }
4601
4602       mips_emit_delays (TRUE);
4603       ++mips_opts.noreorder;
4604       mips_any_noreorder = 1;
4605       if (mips_trap)
4606         {
4607           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4608                        "s,t,q", treg, 0, 7);
4609           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4610                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4611         }
4612       else
4613         {
4614           expr1.X_add_number = 8;
4615           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4616           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4617                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4618           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4619                        "c", 7);
4620         }
4621       expr1.X_add_number = -1;
4622       macro_build ((char *) NULL, &icnt, &expr1,
4623                    dbl ? "daddiu" : "addiu",
4624                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4625       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4626       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4627       if (dbl)
4628         {
4629           expr1.X_add_number = 1;
4630           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4631                        (int) BFD_RELOC_LO16);
4632           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4633                        "d,w,<", AT, AT, 31);
4634         }
4635       else
4636         {
4637           expr1.X_add_number = 0x80000000;
4638           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4639                        (int) BFD_RELOC_HI16);
4640         }
4641       if (mips_trap)
4642         {
4643           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4644                        "s,t,q", sreg, AT, 6);
4645           /* We want to close the noreorder block as soon as possible, so
4646              that later insns are available for delay slot filling.  */
4647           --mips_opts.noreorder;
4648         }
4649       else
4650         {
4651           expr1.X_add_number = 8;
4652           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4653           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4654                        0);
4655
4656           /* We want to close the noreorder block as soon as possible, so
4657              that later insns are available for delay slot filling.  */
4658           --mips_opts.noreorder;
4659
4660           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4661                        "c", 6);
4662         }
4663       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4664       break;
4665
4666     case M_DIV_3I:
4667       s = "div";
4668       s2 = "mflo";
4669       goto do_divi;
4670     case M_DIVU_3I:
4671       s = "divu";
4672       s2 = "mflo";
4673       goto do_divi;
4674     case M_REM_3I:
4675       s = "div";
4676       s2 = "mfhi";
4677       goto do_divi;
4678     case M_REMU_3I:
4679       s = "divu";
4680       s2 = "mfhi";
4681       goto do_divi;
4682     case M_DDIV_3I:
4683       dbl = 1;
4684       s = "ddiv";
4685       s2 = "mflo";
4686       goto do_divi;
4687     case M_DDIVU_3I:
4688       dbl = 1;
4689       s = "ddivu";
4690       s2 = "mflo";
4691       goto do_divi;
4692     case M_DREM_3I:
4693       dbl = 1;
4694       s = "ddiv";
4695       s2 = "mfhi";
4696       goto do_divi;
4697     case M_DREMU_3I:
4698       dbl = 1;
4699       s = "ddivu";
4700       s2 = "mfhi";
4701     do_divi:
4702       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4703         {
4704           as_warn (_("Divide by zero."));
4705           if (mips_trap)
4706             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4707                          "s,t,q", 0, 0, 7);
4708           else
4709             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4710                          "c", 7);
4711           return;
4712         }
4713       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4714         {
4715           if (strcmp (s2, "mflo") == 0)
4716             move_register (&icnt, dreg, sreg);
4717           else
4718             move_register (&icnt, dreg, 0);
4719           return;
4720         }
4721       if (imm_expr.X_op == O_constant
4722           && imm_expr.X_add_number == -1
4723           && s[strlen (s) - 1] != 'u')
4724         {
4725           if (strcmp (s2, "mflo") == 0)
4726             {
4727               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4728                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4729             }
4730           else
4731             move_register (&icnt, dreg, 0);
4732           return;
4733         }
4734
4735       load_register (&icnt, AT, &imm_expr, dbl);
4736       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4737                    sreg, AT);
4738       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4739       break;
4740
4741     case M_DIVU_3:
4742       s = "divu";
4743       s2 = "mflo";
4744       goto do_divu3;
4745     case M_REMU_3:
4746       s = "divu";
4747       s2 = "mfhi";
4748       goto do_divu3;
4749     case M_DDIVU_3:
4750       s = "ddivu";
4751       s2 = "mflo";
4752       goto do_divu3;
4753     case M_DREMU_3:
4754       s = "ddivu";
4755       s2 = "mfhi";
4756     do_divu3:
4757       mips_emit_delays (TRUE);
4758       ++mips_opts.noreorder;
4759       mips_any_noreorder = 1;
4760       if (mips_trap)
4761         {
4762           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4763                        "s,t,q", treg, 0, 7);
4764           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4765                        sreg, treg);
4766           /* We want to close the noreorder block as soon as possible, so
4767              that later insns are available for delay slot filling.  */
4768           --mips_opts.noreorder;
4769         }
4770       else
4771         {
4772           expr1.X_add_number = 8;
4773           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4774           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4775                        sreg, treg);
4776
4777           /* We want to close the noreorder block as soon as possible, so
4778              that later insns are available for delay slot filling.  */
4779           --mips_opts.noreorder;
4780           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4781                        "c", 7);
4782         }
4783       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4784       return;
4785
4786     case M_DLA_AB:
4787       dbl = 1;
4788     case M_LA_AB:
4789       /* Load the address of a symbol into a register.  If breg is not
4790          zero, we then add a base register to it.  */
4791
4792       if (dbl && HAVE_32BIT_GPRS)
4793         as_warn (_("dla used to load 32-bit register"));
4794
4795       if (! dbl && HAVE_64BIT_OBJECTS)
4796         as_warn (_("la used to load 64-bit address"));
4797
4798       if (offset_expr.X_op == O_constant
4799           && offset_expr.X_add_number >= -0x8000
4800           && offset_expr.X_add_number < 0x8000)
4801         {
4802           macro_build ((char *) NULL, &icnt, &offset_expr,
4803                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4804                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4805           return;
4806         }
4807
4808       if (treg == breg)
4809         {
4810           tempreg = AT;
4811           used_at = 1;
4812         }
4813       else
4814         {
4815           tempreg = treg;
4816           used_at = 0;
4817         }
4818
4819       /* When generating embedded PIC code, we permit expressions of
4820          the form
4821            la   $treg,foo-bar
4822            la   $treg,foo-bar($breg)
4823          where bar is an address in the current section.  These are used
4824          when getting the addresses of functions.  We don't permit
4825          X_add_number to be non-zero, because if the symbol is
4826          external the relaxing code needs to know that any addend is
4827          purely the offset to X_op_symbol.  */
4828       if (mips_pic == EMBEDDED_PIC
4829           && offset_expr.X_op == O_subtract
4830           && (symbol_constant_p (offset_expr.X_op_symbol)
4831               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4832               : (symbol_equated_p (offset_expr.X_op_symbol)
4833                  && (S_GET_SEGMENT
4834                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4835                       ->X_add_symbol)
4836                      == now_seg)))
4837           && (offset_expr.X_add_number == 0
4838               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4839         {
4840           if (breg == 0)
4841             {
4842               tempreg = treg;
4843               used_at = 0;
4844               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4845                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4846             }
4847           else
4848             {
4849               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4850                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4851               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4852                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4853                            "d,v,t", tempreg, tempreg, breg);
4854             }
4855           macro_build ((char *) NULL, &icnt, &offset_expr,
4856                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4857                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4858           if (! used_at)
4859             return;
4860           break;
4861         }
4862
4863       if (offset_expr.X_op != O_symbol
4864           && offset_expr.X_op != O_constant)
4865         {
4866           as_bad (_("expression too complex"));
4867           offset_expr.X_op = O_constant;
4868         }
4869
4870       if (offset_expr.X_op == O_constant)
4871         load_register (&icnt, tempreg, &offset_expr,
4872                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4873                         ? (dbl || HAVE_64BIT_ADDRESSES)
4874                         : HAVE_64BIT_ADDRESSES));
4875       else if (mips_pic == NO_PIC)
4876         {
4877           /* If this is a reference to a GP relative symbol, we want
4878                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4879              Otherwise we want
4880                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4881                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4882              If we have a constant, we need two instructions anyhow,
4883              so we may as well always use the latter form.
4884
4885             With 64bit address space and a usable $at we want
4886               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4887               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4888               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4889               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4890               dsll32    $tempreg,0
4891               daddu     $tempreg,$tempreg,$at
4892
4893             If $at is already in use, we use a path which is suboptimal
4894             on superscalar processors.
4895               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4896               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4897               dsll      $tempreg,16
4898               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4899               dsll      $tempreg,16
4900               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4901           */
4902           char *p = NULL;
4903           if (HAVE_64BIT_ADDRESSES)
4904             {
4905               /* We don't do GP optimization for now because RELAX_ENCODE can't
4906                  hold the data for such large chunks.  */
4907
4908               if (used_at == 0 && ! mips_opts.noat)
4909                 {
4910                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4911                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4912                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4913                                AT, (int) BFD_RELOC_HI16_S);
4914                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4915                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4916                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4917                                AT, AT, (int) BFD_RELOC_LO16);
4918                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4919                                "d,w,<", tempreg, tempreg, 0);
4920                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4921                                "d,v,t", tempreg, tempreg, AT);
4922                   used_at = 1;
4923                 }
4924               else
4925                 {
4926                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4927                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4928                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4929                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4930                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4931                                tempreg, tempreg, 16);
4932                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4933                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4934                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4935                                tempreg, tempreg, 16);
4936                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4937                                tempreg, tempreg, (int) BFD_RELOC_LO16);
4938                 }
4939             }
4940           else
4941             {
4942               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4943                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4944                 {
4945                   frag_grow (20);
4946                   macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4947                                "t,r,j", tempreg, mips_gp_register,
4948                                (int) BFD_RELOC_GPREL16);
4949                   p = frag_var (rs_machine_dependent, 8, 0,
4950                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4951                                               mips_opts.warn_about_macros),
4952                                 offset_expr.X_add_symbol, 0, NULL);
4953                 }
4954               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4955               if (p != NULL)
4956                 p += 4;
4957               macro_build (p, &icnt, &offset_expr, "addiu",
4958                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4959             }
4960         }
4961       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4962         {
4963           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4964
4965           /* If this is a reference to an external symbol, and there
4966              is no constant, we want
4967                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4968              or if tempreg is PIC_CALL_REG
4969                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4970              For a local symbol, we want
4971                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4972                nop
4973                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4974
4975              If we have a small constant, and this is a reference to
4976              an external symbol, we want
4977                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4978                nop
4979                addiu    $tempreg,$tempreg,<constant>
4980              For a local symbol, we want the same instruction
4981              sequence, but we output a BFD_RELOC_LO16 reloc on the
4982              addiu instruction.
4983
4984              If we have a large constant, and this is a reference to
4985              an external symbol, we want
4986                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4987                lui      $at,<hiconstant>
4988                addiu    $at,$at,<loconstant>
4989                addu     $tempreg,$tempreg,$at
4990              For a local symbol, we want the same instruction
4991              sequence, but we output a BFD_RELOC_LO16 reloc on the
4992              addiu instruction.
4993
4994              For NewABI, we want for local or external data addresses
4995                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4996              For a local function symbol, we want
4997                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
4998                nop
4999                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5000            */
5001
5002           expr1.X_add_number = offset_expr.X_add_number;
5003           offset_expr.X_add_number = 0;
5004           frag_grow (32);
5005           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5006             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5007           else if (HAVE_NEWABI)
5008             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5009           macro_build ((char *) NULL, &icnt, &offset_expr,
5010                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5011                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5012           if (expr1.X_add_number == 0)
5013             {
5014               int off;
5015               char *p;
5016
5017               if (breg == 0)
5018                 off = 0;
5019               else
5020                 {
5021                   /* We're going to put in an addu instruction using
5022                      tempreg, so we may as well insert the nop right
5023                      now.  */
5024                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5025                                "nop", "");
5026                   off = 4;
5027                 }
5028               p = frag_var (rs_machine_dependent, 8 - off, 0,
5029                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5030                                           (breg == 0
5031                                            ? mips_opts.warn_about_macros
5032                                            : 0)),
5033                             offset_expr.X_add_symbol, 0, NULL);
5034               if (breg == 0)
5035                 {
5036                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5037                   p += 4;
5038                 }
5039               macro_build (p, &icnt, &expr1,
5040                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5041                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5042               /* FIXME: If breg == 0, and the next instruction uses
5043                  $tempreg, then if this variant case is used an extra
5044                  nop will be generated.  */
5045             }
5046           else if (expr1.X_add_number >= -0x8000
5047                    && expr1.X_add_number < 0x8000)
5048             {
5049               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5050                            "nop", "");
5051               macro_build ((char *) NULL, &icnt, &expr1,
5052                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5053                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5054               frag_var (rs_machine_dependent, 0, 0,
5055                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5056                         offset_expr.X_add_symbol, 0, NULL);
5057             }
5058           else
5059             {
5060               int off1;
5061
5062               /* If we are going to add in a base register, and the
5063                  target register and the base register are the same,
5064                  then we are using AT as a temporary register.  Since
5065                  we want to load the constant into AT, we add our
5066                  current AT (from the global offset table) and the
5067                  register into the register now, and pretend we were
5068                  not using a base register.  */
5069               if (breg != treg)
5070                 off1 = 0;
5071               else
5072                 {
5073                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5074                                "nop", "");
5075                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5076                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5077                                "d,v,t", treg, AT, breg);
5078                   breg = 0;
5079                   tempreg = treg;
5080                   off1 = -8;
5081                 }
5082
5083               /* Set mips_optimize around the lui instruction to avoid
5084                  inserting an unnecessary nop after the lw.  */
5085               hold_mips_optimize = mips_optimize;
5086               mips_optimize = 2;
5087               macro_build_lui (NULL, &icnt, &expr1, AT);
5088               mips_optimize = hold_mips_optimize;
5089
5090               macro_build ((char *) NULL, &icnt, &expr1,
5091                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5092                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5093               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5094                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5095                            "d,v,t", tempreg, tempreg, AT);
5096               frag_var (rs_machine_dependent, 0, 0,
5097                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5098                         offset_expr.X_add_symbol, 0, NULL);
5099               used_at = 1;
5100             }
5101         }
5102       else if (mips_pic == SVR4_PIC)
5103         {
5104           int gpdel;
5105           char *p;
5106           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5107           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5108           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5109
5110           /* This is the large GOT case.  If this is a reference to an
5111              external symbol, and there is no constant, we want
5112                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5113                addu     $tempreg,$tempreg,$gp
5114                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5115              or if tempreg is PIC_CALL_REG
5116                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5117                addu     $tempreg,$tempreg,$gp
5118                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5119              For a local symbol, we want
5120                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5121                nop
5122                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5123
5124              If we have a small constant, and this is a reference to
5125              an external symbol, we want
5126                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5127                addu     $tempreg,$tempreg,$gp
5128                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5129                nop
5130                addiu    $tempreg,$tempreg,<constant>
5131              For a local symbol, we want
5132                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5133                nop
5134                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5135
5136              If we have a large constant, and this is a reference to
5137              an external symbol, we want
5138                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5139                addu     $tempreg,$tempreg,$gp
5140                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5141                lui      $at,<hiconstant>
5142                addiu    $at,$at,<loconstant>
5143                addu     $tempreg,$tempreg,$at
5144              For a local symbol, we want
5145                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5146                lui      $at,<hiconstant>
5147                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5148                addu     $tempreg,$tempreg,$at
5149
5150              For NewABI, we want for local data addresses
5151               lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5152            */
5153
5154           expr1.X_add_number = offset_expr.X_add_number;
5155           offset_expr.X_add_number = 0;
5156           frag_grow (52);
5157           if (reg_needs_delay (mips_gp_register))
5158             gpdel = 4;
5159           else
5160             gpdel = 0;
5161           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5162             {
5163               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5164               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5165             }
5166           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5167                        tempreg, lui_reloc_type);
5168           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5169                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5170                        "d,v,t", tempreg, tempreg, mips_gp_register);
5171           macro_build ((char *) NULL, &icnt, &offset_expr,
5172                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5173                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5174           if (expr1.X_add_number == 0)
5175             {
5176               int off;
5177
5178               if (breg == 0)
5179                 off = 0;
5180               else
5181                 {
5182                   /* We're going to put in an addu instruction using
5183                      tempreg, so we may as well insert the nop right
5184                      now.  */
5185                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5186                                "nop", "");
5187                   off = 4;
5188                 }
5189
5190               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5191                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5192                                           8 + gpdel, 0,
5193                                           (breg == 0
5194                                            ? mips_opts.warn_about_macros
5195                                            : 0)),
5196                             offset_expr.X_add_symbol, 0, NULL);
5197             }
5198           else if (expr1.X_add_number >= -0x8000
5199                    && expr1.X_add_number < 0x8000)
5200             {
5201               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5202                            "nop", "");
5203               macro_build ((char *) NULL, &icnt, &expr1,
5204                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5205                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5206
5207               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5208                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5209                                           (breg == 0
5210                                            ? mips_opts.warn_about_macros
5211                                            : 0)),
5212                             offset_expr.X_add_symbol, 0, NULL);
5213             }
5214           else
5215             {
5216               int adj, dreg;
5217
5218               /* If we are going to add in a base register, and the
5219                  target register and the base register are the same,
5220                  then we are using AT as a temporary register.  Since
5221                  we want to load the constant into AT, we add our
5222                  current AT (from the global offset table) and the
5223                  register into the register now, and pretend we were
5224                  not using a base register.  */
5225               if (breg != treg)
5226                 {
5227                   adj = 0;
5228                   dreg = tempreg;
5229                 }
5230               else
5231                 {
5232                   assert (tempreg == AT);
5233                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5234                                "nop", "");
5235                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5236                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5237                                "d,v,t", treg, AT, breg);
5238                   dreg = treg;
5239                   adj = 8;
5240                 }
5241
5242               /* Set mips_optimize around the lui instruction to avoid
5243                  inserting an unnecessary nop after the lw.  */
5244               hold_mips_optimize = mips_optimize;
5245               mips_optimize = 2;
5246               macro_build_lui (NULL, &icnt, &expr1, AT);
5247               mips_optimize = hold_mips_optimize;
5248
5249               macro_build ((char *) NULL, &icnt, &expr1,
5250                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5251                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5252               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5253                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5254                            "d,v,t", dreg, dreg, AT);
5255
5256               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5257                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5258                                           8 + gpdel, 0,
5259                                           (breg == 0
5260                                            ? mips_opts.warn_about_macros
5261                                            : 0)),
5262                             offset_expr.X_add_symbol, 0, NULL);
5263
5264               used_at = 1;
5265             }
5266
5267           if (gpdel > 0)
5268             {
5269               /* This is needed because this instruction uses $gp, but
5270                  the first instruction on the main stream does not.  */
5271               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5272               p += 4;
5273             }
5274
5275           if (HAVE_NEWABI)
5276             local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5277           macro_build (p, &icnt, &offset_expr,
5278                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5279                        "t,o(b)", tempreg,
5280                        local_reloc_type,
5281                        mips_gp_register);
5282           p += 4;
5283           if (expr1.X_add_number == 0 && HAVE_NEWABI)
5284             {
5285               /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5286             }
5287          else
5288            if (expr1.X_add_number >= -0x8000
5289               && expr1.X_add_number < 0x8000)
5290             {
5291               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5292               p += 4;
5293               macro_build (p, &icnt, &expr1,
5294                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5295                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5296               /* FIXME: If add_number is 0, and there was no base
5297                  register, the external symbol case ended with a load,
5298                  so if the symbol turns out to not be external, and
5299                  the next instruction uses tempreg, an unnecessary nop
5300                  will be inserted.  */
5301             }
5302           else
5303             {
5304               if (breg == treg)
5305                 {
5306                   /* We must add in the base register now, as in the
5307                      external symbol case.  */
5308                   assert (tempreg == AT);
5309                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5310                   p += 4;
5311                   macro_build (p, &icnt, (expressionS *) NULL,
5312                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5313                                "d,v,t", treg, AT, breg);
5314                   p += 4;
5315                   tempreg = treg;
5316                   /* We set breg to 0 because we have arranged to add
5317                      it in in both cases.  */
5318                   breg = 0;
5319                 }
5320
5321               macro_build_lui (p, &icnt, &expr1, AT);
5322               p += 4;
5323               macro_build (p, &icnt, &expr1,
5324                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5325                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5326               p += 4;
5327               macro_build (p, &icnt, (expressionS *) NULL,
5328                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5329                            "d,v,t", tempreg, tempreg, AT);
5330               p += 4;
5331             }
5332         }
5333       else if (mips_pic == EMBEDDED_PIC)
5334         {
5335           /* We use
5336                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5337              */
5338           macro_build ((char *) NULL, &icnt, &offset_expr,
5339                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5340                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5341         }
5342       else
5343         abort ();
5344
5345       if (breg != 0)
5346         {
5347           char *s;
5348
5349           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5350             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5351           else
5352             s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5353
5354           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5355                        "d,v,t", treg, tempreg, breg);
5356         }
5357
5358       if (! used_at)
5359         return;
5360
5361       break;
5362
5363     case M_J_A:
5364       /* The j instruction may not be used in PIC code, since it
5365          requires an absolute address.  We convert it to a b
5366          instruction.  */
5367       if (mips_pic == NO_PIC)
5368         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5369       else
5370         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5371       return;
5372
5373       /* The jal instructions must be handled as macros because when
5374          generating PIC code they expand to multi-instruction
5375          sequences.  Normally they are simple instructions.  */
5376     case M_JAL_1:
5377       dreg = RA;
5378       /* Fall through.  */
5379     case M_JAL_2:
5380       if (mips_pic == NO_PIC
5381           || mips_pic == EMBEDDED_PIC)
5382         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5383                      "d,s", dreg, sreg);
5384       else if (mips_pic == SVR4_PIC)
5385         {
5386           if (sreg != PIC_CALL_REG)
5387             as_warn (_("MIPS PIC call to register other than $25"));
5388
5389           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5390                        "d,s", dreg, sreg);
5391           if (! HAVE_NEWABI)
5392             {
5393               if (mips_cprestore_offset < 0)
5394                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5395               else
5396                 {
5397                   if (! mips_frame_reg_valid)
5398                     {
5399                       as_warn (_("No .frame pseudo-op used in PIC code"));
5400                       /* Quiet this warning.  */
5401                       mips_frame_reg_valid = 1;
5402                     }
5403                   if (! mips_cprestore_valid)
5404                     {
5405                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5406                       /* Quiet this warning.  */
5407                       mips_cprestore_valid = 1;
5408                     }
5409                   expr1.X_add_number = mips_cprestore_offset;
5410                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5411                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5412                                                 mips_gp_register, mips_frame_reg);
5413                 }
5414             }
5415         }
5416       else
5417         abort ();
5418
5419       return;
5420
5421     case M_JAL_A:
5422       if (mips_pic == NO_PIC)
5423         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5424       else if (mips_pic == SVR4_PIC)
5425         {
5426           char *p;
5427
5428           /* If this is a reference to an external symbol, and we are
5429              using a small GOT, we want
5430                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5431                nop
5432                jalr     $ra,$25
5433                nop
5434                lw       $gp,cprestore($sp)
5435              The cprestore value is set using the .cprestore
5436              pseudo-op.  If we are using a big GOT, we want
5437                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5438                addu     $25,$25,$gp
5439                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5440                nop
5441                jalr     $ra,$25
5442                nop
5443                lw       $gp,cprestore($sp)
5444              If the symbol is not external, we want
5445                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5446                nop
5447                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5448                jalr     $ra,$25
5449                nop
5450                lw $gp,cprestore($sp)
5451              For NewABI, we want
5452                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT_DISP)
5453                jalr     $ra,$25                 (BFD_RELOC_MIPS_JALR)
5454            */
5455           if (HAVE_NEWABI)
5456             {
5457               macro_build ((char *) NULL, &icnt, &offset_expr,
5458                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5459                            "t,o(b)", PIC_CALL_REG,
5460                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5461               macro_build_jalr (icnt, &offset_expr);
5462             }
5463           else
5464             {
5465               frag_grow (40);
5466               if (! mips_big_got)
5467                 {
5468                   macro_build ((char *) NULL, &icnt, &offset_expr,
5469                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5470                                "t,o(b)", PIC_CALL_REG,
5471                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5472                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5473                                "nop", "");
5474                   p = frag_var (rs_machine_dependent, 4, 0,
5475                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5476                                 offset_expr.X_add_symbol, 0, NULL);
5477                 }
5478               else
5479                 {
5480                   int gpdel;
5481
5482                   if (reg_needs_delay (mips_gp_register))
5483                     gpdel = 4;
5484                   else
5485                     gpdel = 0;
5486                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5487                                "t,u", PIC_CALL_REG,
5488                                (int) BFD_RELOC_MIPS_CALL_HI16);
5489                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5490                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5491                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5492                                mips_gp_register);
5493                   macro_build ((char *) NULL, &icnt, &offset_expr,
5494                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5495                                "t,o(b)", PIC_CALL_REG,
5496                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5497                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5498                                "nop", "");
5499                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5500                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5501                                               8 + gpdel, 0, 0),
5502                                 offset_expr.X_add_symbol, 0, NULL);
5503                   if (gpdel > 0)
5504                     {
5505                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5506                       p += 4;
5507                     }
5508                   macro_build (p, &icnt, &offset_expr,
5509                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5510                                "t,o(b)", PIC_CALL_REG,
5511                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5512                   p += 4;
5513                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5514                   p += 4;
5515                 }
5516               macro_build (p, &icnt, &offset_expr,
5517                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5518                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5519                            (int) BFD_RELOC_LO16);
5520               macro_build_jalr (icnt, &offset_expr);
5521
5522               if (mips_cprestore_offset < 0)
5523                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5524               else
5525                 {
5526                   if (! mips_frame_reg_valid)
5527                     {
5528                       as_warn (_("No .frame pseudo-op used in PIC code"));
5529                       /* Quiet this warning.  */
5530                       mips_frame_reg_valid = 1;
5531                     }
5532                   if (! mips_cprestore_valid)
5533                     {
5534                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5535                       /* Quiet this warning.  */
5536                       mips_cprestore_valid = 1;
5537                     }
5538                   if (mips_opts.noreorder)
5539                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5540                                  "nop", "");
5541                   expr1.X_add_number = mips_cprestore_offset;
5542                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5543                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5544                                                 mips_gp_register, mips_frame_reg);
5545                 }
5546             }
5547         }
5548       else if (mips_pic == EMBEDDED_PIC)
5549         {
5550           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5551           /* The linker may expand the call to a longer sequence which
5552              uses $at, so we must break rather than return.  */
5553           break;
5554         }
5555       else
5556         abort ();
5557
5558       return;
5559
5560     case M_LB_AB:
5561       s = "lb";
5562       goto ld;
5563     case M_LBU_AB:
5564       s = "lbu";
5565       goto ld;
5566     case M_LH_AB:
5567       s = "lh";
5568       goto ld;
5569     case M_LHU_AB:
5570       s = "lhu";
5571       goto ld;
5572     case M_LW_AB:
5573       s = "lw";
5574       goto ld;
5575     case M_LWC0_AB:
5576       s = "lwc0";
5577       /* Itbl support may require additional care here.  */
5578       coproc = 1;
5579       goto ld;
5580     case M_LWC1_AB:
5581       s = "lwc1";
5582       /* Itbl support may require additional care here.  */
5583       coproc = 1;
5584       goto ld;
5585     case M_LWC2_AB:
5586       s = "lwc2";
5587       /* Itbl support may require additional care here.  */
5588       coproc = 1;
5589       goto ld;
5590     case M_LWC3_AB:
5591       s = "lwc3";
5592       /* Itbl support may require additional care here.  */
5593       coproc = 1;
5594       goto ld;
5595     case M_LWL_AB:
5596       s = "lwl";
5597       lr = 1;
5598       goto ld;
5599     case M_LWR_AB:
5600       s = "lwr";
5601       lr = 1;
5602       goto ld;
5603     case M_LDC1_AB:
5604       if (mips_arch == CPU_R4650)
5605         {
5606           as_bad (_("opcode not supported on this processor"));
5607           return;
5608         }
5609       s = "ldc1";
5610       /* Itbl support may require additional care here.  */
5611       coproc = 1;
5612       goto ld;
5613     case M_LDC2_AB:
5614       s = "ldc2";
5615       /* Itbl support may require additional care here.  */
5616       coproc = 1;
5617       goto ld;
5618     case M_LDC3_AB:
5619       s = "ldc3";
5620       /* Itbl support may require additional care here.  */
5621       coproc = 1;
5622       goto ld;
5623     case M_LDL_AB:
5624       s = "ldl";
5625       lr = 1;
5626       goto ld;
5627     case M_LDR_AB:
5628       s = "ldr";
5629       lr = 1;
5630       goto ld;
5631     case M_LL_AB:
5632       s = "ll";
5633       goto ld;
5634     case M_LLD_AB:
5635       s = "lld";
5636       goto ld;
5637     case M_LWU_AB:
5638       s = "lwu";
5639     ld:
5640       if (breg == treg || coproc || lr)
5641         {
5642           tempreg = AT;
5643           used_at = 1;
5644         }
5645       else
5646         {
5647           tempreg = treg;
5648           used_at = 0;
5649         }
5650       goto ld_st;
5651     case M_SB_AB:
5652       s = "sb";
5653       goto st;
5654     case M_SH_AB:
5655       s = "sh";
5656       goto st;
5657     case M_SW_AB:
5658       s = "sw";
5659       goto st;
5660     case M_SWC0_AB:
5661       s = "swc0";
5662       /* Itbl support may require additional care here.  */
5663       coproc = 1;
5664       goto st;
5665     case M_SWC1_AB:
5666       s = "swc1";
5667       /* Itbl support may require additional care here.  */
5668       coproc = 1;
5669       goto st;
5670     case M_SWC2_AB:
5671       s = "swc2";
5672       /* Itbl support may require additional care here.  */
5673       coproc = 1;
5674       goto st;
5675     case M_SWC3_AB:
5676       s = "swc3";
5677       /* Itbl support may require additional care here.  */
5678       coproc = 1;
5679       goto st;
5680     case M_SWL_AB:
5681       s = "swl";
5682       goto st;
5683     case M_SWR_AB:
5684       s = "swr";
5685       goto st;
5686     case M_SC_AB:
5687       s = "sc";
5688       goto st;
5689     case M_SCD_AB:
5690       s = "scd";
5691       goto st;
5692     case M_SDC1_AB:
5693       if (mips_arch == CPU_R4650)
5694         {
5695           as_bad (_("opcode not supported on this processor"));
5696           return;
5697         }
5698       s = "sdc1";
5699       coproc = 1;
5700       /* Itbl support may require additional care here.  */
5701       goto st;
5702     case M_SDC2_AB:
5703       s = "sdc2";
5704       /* Itbl support may require additional care here.  */
5705       coproc = 1;
5706       goto st;
5707     case M_SDC3_AB:
5708       s = "sdc3";
5709       /* Itbl support may require additional care here.  */
5710       coproc = 1;
5711       goto st;
5712     case M_SDL_AB:
5713       s = "sdl";
5714       goto st;
5715     case M_SDR_AB:
5716       s = "sdr";
5717     st:
5718       tempreg = AT;
5719       used_at = 1;
5720     ld_st:
5721       /* Itbl support may require additional care here.  */
5722       if (mask == M_LWC1_AB
5723           || mask == M_SWC1_AB
5724           || mask == M_LDC1_AB
5725           || mask == M_SDC1_AB
5726           || mask == M_L_DAB
5727           || mask == M_S_DAB)
5728         fmt = "T,o(b)";
5729       else if (coproc)
5730         fmt = "E,o(b)";
5731       else
5732         fmt = "t,o(b)";
5733
5734       /* For embedded PIC, we allow loads where the offset is calculated
5735          by subtracting a symbol in the current segment from an unknown
5736          symbol, relative to a base register, e.g.:
5737                 <op>    $treg, <sym>-<localsym>($breg)
5738          This is used by the compiler for switch statements.  */
5739       if (mips_pic == EMBEDDED_PIC
5740           && offset_expr.X_op == O_subtract
5741           && (symbol_constant_p (offset_expr.X_op_symbol)
5742               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5743               : (symbol_equated_p (offset_expr.X_op_symbol)
5744                  && (S_GET_SEGMENT
5745                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5746                       ->X_add_symbol)
5747                      == now_seg)))
5748           && breg != 0
5749           && (offset_expr.X_add_number == 0
5750               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5751         {
5752           /* For this case, we output the instructions:
5753                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5754                 addiu   $tempreg,$tempreg,$breg
5755                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5756              If the relocation would fit entirely in 16 bits, it would be
5757              nice to emit:
5758                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5759              instead, but that seems quite difficult.  */
5760           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5761                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5762           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5763                        ((bfd_arch_bits_per_address (stdoutput) == 32
5764                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5765                         ? "addu" : "daddu"),
5766                        "d,v,t", tempreg, tempreg, breg);
5767           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5768                        (int) BFD_RELOC_PCREL_LO16, tempreg);
5769           if (! used_at)
5770             return;
5771           break;
5772         }
5773
5774       if (offset_expr.X_op != O_constant
5775           && offset_expr.X_op != O_symbol)
5776         {
5777           as_bad (_("expression too complex"));
5778           offset_expr.X_op = O_constant;
5779         }
5780
5781       /* A constant expression in PIC code can be handled just as it
5782          is in non PIC code.  */
5783       if (mips_pic == NO_PIC
5784           || offset_expr.X_op == O_constant)
5785         {
5786           char *p;
5787
5788           /* If this is a reference to a GP relative symbol, and there
5789              is no base register, we want
5790                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
5791              Otherwise, if there is no base register, we want
5792                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5793                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5794              If we have a constant, we need two instructions anyhow,
5795              so we always use the latter form.
5796
5797              If we have a base register, and this is a reference to a
5798              GP relative symbol, we want
5799                addu     $tempreg,$breg,$gp
5800                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
5801              Otherwise we want
5802                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5803                addu     $tempreg,$tempreg,$breg
5804                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5805              With a constant we always use the latter case.
5806
5807              With 64bit address space and no base register and $at usable,
5808              we want
5809                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5810                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5811                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5812                dsll32   $tempreg,0
5813                daddu    $tempreg,$at
5814                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5815              If we have a base register, we want
5816                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5817                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5818                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5819                daddu    $at,$breg
5820                dsll32   $tempreg,0
5821                daddu    $tempreg,$at
5822                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5823
5824              Without $at we can't generate the optimal path for superscalar
5825              processors here since this would require two temporary registers.
5826                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5827                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5828                dsll     $tempreg,16
5829                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5830                dsll     $tempreg,16
5831                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5832              If we have a base register, we want
5833                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5834                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5835                dsll     $tempreg,16
5836                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5837                dsll     $tempreg,16
5838                daddu    $tempreg,$tempreg,$breg
5839                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
5840
5841              If we have 64-bit addresses, as an optimization, for
5842              addresses which are 32-bit constants (e.g. kseg0/kseg1
5843              addresses) we fall back to the 32-bit address generation
5844              mechanism since it is more efficient.  Note that due to
5845              the signed offset used by memory operations, the 32-bit
5846              range is shifted down by 32768 here.  This code should
5847              probably attempt to generate 64-bit constants more
5848              efficiently in general.
5849
5850              As an extension for architectures with 64-bit registers,
5851              we don't truncate 64-bit addresses given as literal
5852              constants down to 32 bits, to support existing practice
5853              in the mips64 Linux (the kernel), that compiles source
5854              files with -mabi=64, assembling them as o32 or n32 (with
5855              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
5856              the whole kernel is loaded into a memory region that is
5857              addressible with sign-extended 32-bit addresses, it is
5858              wasteful to compute the upper 32 bits of every
5859              non-literal address, that takes more space and time.
5860              Some day this should probably be implemented as an
5861              assembler option, such that the kernel doesn't have to
5862              use such ugly hacks, even though it will still have to
5863              end up converting the binary to ELF32 for a number of
5864              platforms whose boot loaders don't support ELF64
5865              binaries.  */
5866           if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
5867               || (offset_expr.X_op == O_constant
5868                   && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
5869                   && HAVE_64BIT_ADDRESS_CONSTANTS))
5870             {
5871               p = NULL;
5872
5873               /* We don't do GP optimization for now because RELAX_ENCODE can't
5874                  hold the data for such large chunks.  */
5875
5876               if (used_at == 0 && ! mips_opts.noat)
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, "lui", "t,u",
5881                                AT, (int) BFD_RELOC_HI16_S);
5882                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5883                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5884                   if (breg != 0)
5885                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5886                                  "d,v,t", AT, AT, breg);
5887                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5888                                "d,w,<", tempreg, tempreg, 0);
5889                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5890                                "d,v,t", tempreg, tempreg, AT);
5891                   macro_build (p, &icnt, &offset_expr, s,
5892                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5893                   used_at = 1;
5894                 }
5895               else
5896                 {
5897                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5898                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5899                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5900                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5901                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5902                                "d,w,<", tempreg, tempreg, 16);
5903                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5904                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5905                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5906                                "d,w,<", tempreg, tempreg, 16);
5907                   if (breg != 0)
5908                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5909                                  "d,v,t", tempreg, tempreg, breg);
5910                   macro_build (p, &icnt, &offset_expr, s,
5911                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5912                 }
5913
5914               return;
5915             }
5916           else if (offset_expr.X_op == O_constant
5917                    && !HAVE_64BIT_ADDRESS_CONSTANTS
5918                    && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5919             as_bad (_("load/store address overflow (max 32 bits)"));
5920
5921           if (breg == 0)
5922             {
5923               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5924                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5925                 p = NULL;
5926               else
5927                 {
5928                   frag_grow (20);
5929                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5930                                treg, (int) BFD_RELOC_GPREL16,
5931                                mips_gp_register);
5932                   p = frag_var (rs_machine_dependent, 8, 0,
5933                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5934                                               (mips_opts.warn_about_macros
5935                                                || (used_at
5936                                                    && mips_opts.noat))),
5937                                 offset_expr.X_add_symbol, 0, NULL);
5938                   used_at = 0;
5939                 }
5940               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5941               if (p != NULL)
5942                 p += 4;
5943               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5944                            (int) BFD_RELOC_LO16, tempreg);
5945             }
5946           else
5947             {
5948               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5949                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
5950                 p = NULL;
5951               else
5952                 {
5953                   frag_grow (28);
5954                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5955                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5956                                "d,v,t", tempreg, breg, mips_gp_register);
5957                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5958                                treg, (int) BFD_RELOC_GPREL16, tempreg);
5959                   p = frag_var (rs_machine_dependent, 12, 0,
5960                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5961                                 offset_expr.X_add_symbol, 0, NULL);
5962                 }
5963               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5964               if (p != NULL)
5965                 p += 4;
5966               macro_build (p, &icnt, (expressionS *) NULL,
5967                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5968                            "d,v,t", tempreg, tempreg, breg);
5969               if (p != NULL)
5970                 p += 4;
5971               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5972                            (int) BFD_RELOC_LO16, tempreg);
5973             }
5974         }
5975       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5976         {
5977           char *p;
5978           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5979
5980           /* If this is a reference to an external symbol, we want
5981                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5982                nop
5983                <op>     $treg,0($tempreg)
5984              Otherwise we want
5985                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5986                nop
5987                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5988                <op>     $treg,0($tempreg)
5989              If we have NewABI, we want
5990                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_DISP)
5991              If there is a base register, we add it to $tempreg before
5992              the <op>.  If there is a constant, we stick it in the
5993              <op> instruction.  We don't handle constants larger than
5994              16 bits, because we have no way to load the upper 16 bits
5995              (actually, we could handle them for the subset of cases
5996              in which we are not using $at).  */
5997           assert (offset_expr.X_op == O_symbol);
5998           expr1.X_add_number = offset_expr.X_add_number;
5999           offset_expr.X_add_number = 0;
6000           if (HAVE_NEWABI)
6001             lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
6002           if (expr1.X_add_number < -0x8000
6003               || expr1.X_add_number >= 0x8000)
6004             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6005           frag_grow (20);
6006           macro_build ((char *) NULL, &icnt, &offset_expr,
6007                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
6008                        (int) lw_reloc_type, mips_gp_register);
6009           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6010           p = frag_var (rs_machine_dependent, 4, 0,
6011                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6012                         offset_expr.X_add_symbol, 0, NULL);
6013           macro_build (p, &icnt, &offset_expr,
6014                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6015                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6016           if (breg != 0)
6017             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6018                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6019                          "d,v,t", tempreg, tempreg, breg);
6020           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6021                        (int) BFD_RELOC_LO16, tempreg);
6022         }
6023       else if (mips_pic == SVR4_PIC)
6024         {
6025           int gpdel;
6026           char *p;
6027
6028           /* If this is a reference to an external symbol, we want
6029                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6030                addu     $tempreg,$tempreg,$gp
6031                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6032                <op>     $treg,0($tempreg)
6033              Otherwise we want
6034                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6035                nop
6036                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6037                <op>     $treg,0($tempreg)
6038              If there is a base register, we add it to $tempreg before
6039              the <op>.  If there is a constant, we stick it in the
6040              <op> instruction.  We don't handle constants larger than
6041              16 bits, because we have no way to load the upper 16 bits
6042              (actually, we could handle them for the subset of cases
6043              in which we are not using $at).
6044
6045              For NewABI, we want
6046                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6047                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
6048                <op>     $treg,0($tempreg)
6049            */
6050           assert (offset_expr.X_op == O_symbol);
6051           expr1.X_add_number = offset_expr.X_add_number;
6052           offset_expr.X_add_number = 0;
6053           if (expr1.X_add_number < -0x8000
6054               || expr1.X_add_number >= 0x8000)
6055             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6056           if (HAVE_NEWABI)
6057             {
6058               macro_build ((char *) NULL, &icnt, &offset_expr,
6059                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6060                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6061                            mips_gp_register);
6062               macro_build ((char *) NULL, &icnt, &offset_expr,
6063                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6064                            "t,r,j", tempreg, tempreg,
6065                            BFD_RELOC_MIPS_GOT_OFST);
6066               if (breg != 0)
6067                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6068                              HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6069                              "d,v,t", tempreg, tempreg, breg);
6070               macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6071                            (int) BFD_RELOC_LO16, tempreg);
6072
6073               if (! used_at)
6074                 return;
6075
6076               break;
6077             }
6078           if (reg_needs_delay (mips_gp_register))
6079             gpdel = 4;
6080           else
6081             gpdel = 0;
6082           frag_grow (36);
6083           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6084                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6085           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6086                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6087                        "d,v,t", tempreg, tempreg, mips_gp_register);
6088           macro_build ((char *) NULL, &icnt, &offset_expr,
6089                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6090                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6091                        tempreg);
6092           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6093                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6094                         offset_expr.X_add_symbol, 0, NULL);
6095           if (gpdel > 0)
6096             {
6097               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6098               p += 4;
6099             }
6100           macro_build (p, &icnt, &offset_expr,
6101                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6102                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6103                        mips_gp_register);
6104           p += 4;
6105           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6106           p += 4;
6107           macro_build (p, &icnt, &offset_expr,
6108                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6109                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6110           if (breg != 0)
6111             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6112                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6113                          "d,v,t", tempreg, tempreg, breg);
6114           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6115                        (int) BFD_RELOC_LO16, tempreg);
6116         }
6117       else if (mips_pic == EMBEDDED_PIC)
6118         {
6119           /* If there is no base register, we want
6120                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6121              If there is a base register, we want
6122                addu     $tempreg,$breg,$gp
6123                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6124              */
6125           assert (offset_expr.X_op == O_symbol);
6126           if (breg == 0)
6127             {
6128               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6129                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6130               used_at = 0;
6131             }
6132           else
6133             {
6134               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6135                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6136                            "d,v,t", tempreg, breg, mips_gp_register);
6137               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6138                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6139             }
6140         }
6141       else
6142         abort ();
6143
6144       if (! used_at)
6145         return;
6146
6147       break;
6148
6149     case M_LI:
6150     case M_LI_S:
6151       load_register (&icnt, treg, &imm_expr, 0);
6152       return;
6153
6154     case M_DLI:
6155       load_register (&icnt, treg, &imm_expr, 1);
6156       return;
6157
6158     case M_LI_SS:
6159       if (imm_expr.X_op == O_constant)
6160         {
6161           load_register (&icnt, AT, &imm_expr, 0);
6162           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6163                        "mtc1", "t,G", AT, treg);
6164           break;
6165         }
6166       else
6167         {
6168           assert (offset_expr.X_op == O_symbol
6169                   && strcmp (segment_name (S_GET_SEGMENT
6170                                            (offset_expr.X_add_symbol)),
6171                              ".lit4") == 0
6172                   && offset_expr.X_add_number == 0);
6173           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6174                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6175           return;
6176         }
6177
6178     case M_LI_D:
6179       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6180          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6181          order 32 bits of the value and the low order 32 bits are either
6182          zero or in OFFSET_EXPR.  */
6183       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6184         {
6185           if (HAVE_64BIT_GPRS)
6186             load_register (&icnt, treg, &imm_expr, 1);
6187           else
6188             {
6189               int hreg, lreg;
6190
6191               if (target_big_endian)
6192                 {
6193                   hreg = treg;
6194                   lreg = treg + 1;
6195                 }
6196               else
6197                 {
6198                   hreg = treg + 1;
6199                   lreg = treg;
6200                 }
6201
6202               if (hreg <= 31)
6203                 load_register (&icnt, hreg, &imm_expr, 0);
6204               if (lreg <= 31)
6205                 {
6206                   if (offset_expr.X_op == O_absent)
6207                     move_register (&icnt, lreg, 0);
6208                   else
6209                     {
6210                       assert (offset_expr.X_op == O_constant);
6211                       load_register (&icnt, lreg, &offset_expr, 0);
6212                     }
6213                 }
6214             }
6215           return;
6216         }
6217
6218       /* We know that sym is in the .rdata section.  First we get the
6219          upper 16 bits of the address.  */
6220       if (mips_pic == NO_PIC)
6221         {
6222           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6223         }
6224       else if (mips_pic == SVR4_PIC)
6225         {
6226           macro_build ((char *) NULL, &icnt, &offset_expr,
6227                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6228                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6229                        mips_gp_register);
6230         }
6231       else if (mips_pic == EMBEDDED_PIC)
6232         {
6233           /* For embedded PIC we pick up the entire address off $gp in
6234              a single instruction.  */
6235           macro_build ((char *) NULL, &icnt, &offset_expr,
6236                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6237                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6238           offset_expr.X_op = O_constant;
6239           offset_expr.X_add_number = 0;
6240         }
6241       else
6242         abort ();
6243
6244       /* Now we load the register(s).  */
6245       if (HAVE_64BIT_GPRS)
6246         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6247                      treg, (int) BFD_RELOC_LO16, AT);
6248       else
6249         {
6250           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6251                        treg, (int) BFD_RELOC_LO16, AT);
6252           if (treg != RA)
6253             {
6254               /* FIXME: How in the world do we deal with the possible
6255                  overflow here?  */
6256               offset_expr.X_add_number += 4;
6257               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6258                            treg + 1, (int) BFD_RELOC_LO16, AT);
6259             }
6260         }
6261
6262       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6263          does not become a variant frag.  */
6264       frag_wane (frag_now);
6265       frag_new (0);
6266
6267       break;
6268
6269     case M_LI_DD:
6270       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6271          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6272          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6273          the value and the low order 32 bits are either zero or in
6274          OFFSET_EXPR.  */
6275       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6276         {
6277           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6278           if (HAVE_64BIT_FPRS)
6279             {
6280               assert (HAVE_64BIT_GPRS);
6281               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6282                            "dmtc1", "t,S", AT, treg);
6283             }
6284           else
6285             {
6286               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6287                            "mtc1", "t,G", AT, treg + 1);
6288               if (offset_expr.X_op == O_absent)
6289                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6290                              "mtc1", "t,G", 0, treg);
6291               else
6292                 {
6293                   assert (offset_expr.X_op == O_constant);
6294                   load_register (&icnt, AT, &offset_expr, 0);
6295                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6296                                "mtc1", "t,G", AT, treg);
6297                 }
6298             }
6299           break;
6300         }
6301
6302       assert (offset_expr.X_op == O_symbol
6303               && offset_expr.X_add_number == 0);
6304       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6305       if (strcmp (s, ".lit8") == 0)
6306         {
6307           if (mips_opts.isa != ISA_MIPS1)
6308             {
6309               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6310                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6311                            mips_gp_register);
6312               return;
6313             }
6314           breg = mips_gp_register;
6315           r = BFD_RELOC_MIPS_LITERAL;
6316           goto dob;
6317         }
6318       else
6319         {
6320           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6321           if (mips_pic == SVR4_PIC)
6322             macro_build ((char *) NULL, &icnt, &offset_expr,
6323                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6324                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6325                          mips_gp_register);
6326           else
6327             {
6328               /* FIXME: This won't work for a 64 bit address.  */
6329               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6330             }
6331
6332           if (mips_opts.isa != ISA_MIPS1)
6333             {
6334               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6335                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6336
6337               /* To avoid confusion in tc_gen_reloc, we must ensure
6338                  that this does not become a variant frag.  */
6339               frag_wane (frag_now);
6340               frag_new (0);
6341
6342               break;
6343             }
6344           breg = AT;
6345           r = BFD_RELOC_LO16;
6346           goto dob;
6347         }
6348
6349     case M_L_DOB:
6350       if (mips_arch == CPU_R4650)
6351         {
6352           as_bad (_("opcode not supported on this processor"));
6353           return;
6354         }
6355       /* Even on a big endian machine $fn comes before $fn+1.  We have
6356          to adjust when loading from memory.  */
6357       r = BFD_RELOC_LO16;
6358     dob:
6359       assert (mips_opts.isa == ISA_MIPS1);
6360       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6361                    target_big_endian ? treg + 1 : treg,
6362                    (int) r, breg);
6363       /* FIXME: A possible overflow which I don't know how to deal
6364          with.  */
6365       offset_expr.X_add_number += 4;
6366       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6367                    target_big_endian ? treg : treg + 1,
6368                    (int) r, breg);
6369
6370       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6371          does not become a variant frag.  */
6372       frag_wane (frag_now);
6373       frag_new (0);
6374
6375       if (breg != AT)
6376         return;
6377       break;
6378
6379     case M_L_DAB:
6380       /*
6381        * The MIPS assembler seems to check for X_add_number not
6382        * being double aligned and generating:
6383        *        lui     at,%hi(foo+1)
6384        *        addu    at,at,v1
6385        *        addiu   at,at,%lo(foo+1)
6386        *        lwc1    f2,0(at)
6387        *        lwc1    f3,4(at)
6388        * But, the resulting address is the same after relocation so why
6389        * generate the extra instruction?
6390        */
6391       if (mips_arch == CPU_R4650)
6392         {
6393           as_bad (_("opcode not supported on this processor"));
6394           return;
6395         }
6396       /* Itbl support may require additional care here.  */
6397       coproc = 1;
6398       if (mips_opts.isa != ISA_MIPS1)
6399         {
6400           s = "ldc1";
6401           goto ld;
6402         }
6403
6404       s = "lwc1";
6405       fmt = "T,o(b)";
6406       goto ldd_std;
6407
6408     case M_S_DAB:
6409       if (mips_arch == CPU_R4650)
6410         {
6411           as_bad (_("opcode not supported on this processor"));
6412           return;
6413         }
6414
6415       if (mips_opts.isa != ISA_MIPS1)
6416         {
6417           s = "sdc1";
6418           goto st;
6419         }
6420
6421       s = "swc1";
6422       fmt = "T,o(b)";
6423       /* Itbl support may require additional care here.  */
6424       coproc = 1;
6425       goto ldd_std;
6426
6427     case M_LD_AB:
6428       if (HAVE_64BIT_GPRS)
6429         {
6430           s = "ld";
6431           goto ld;
6432         }
6433
6434       s = "lw";
6435       fmt = "t,o(b)";
6436       goto ldd_std;
6437
6438     case M_SD_AB:
6439       if (HAVE_64BIT_GPRS)
6440         {
6441           s = "sd";
6442           goto st;
6443         }
6444
6445       s = "sw";
6446       fmt = "t,o(b)";
6447
6448     ldd_std:
6449       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6450          loads for the case of doing a pair of loads to simulate an 'ld'.
6451          This is not currently done by the compiler, and assembly coders
6452          writing embedded-pic code can cope.  */
6453
6454       if (offset_expr.X_op != O_symbol
6455           && offset_expr.X_op != O_constant)
6456         {
6457           as_bad (_("expression too complex"));
6458           offset_expr.X_op = O_constant;
6459         }
6460
6461       /* Even on a big endian machine $fn comes before $fn+1.  We have
6462          to adjust when loading from memory.  We set coproc if we must
6463          load $fn+1 first.  */
6464       /* Itbl support may require additional care here.  */
6465       if (! target_big_endian)
6466         coproc = 0;
6467
6468       if (mips_pic == NO_PIC
6469           || offset_expr.X_op == O_constant)
6470         {
6471           char *p;
6472
6473           /* If this is a reference to a GP relative symbol, we want
6474                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6475                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6476              If we have a base register, we use this
6477                addu     $at,$breg,$gp
6478                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6479                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6480              If this is not a GP relative symbol, we want
6481                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6482                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6483                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6484              If there is a base register, we add it to $at after the
6485              lui instruction.  If there is a constant, we always use
6486              the last case.  */
6487           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6488               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6489             {
6490               p = NULL;
6491               used_at = 1;
6492             }
6493           else
6494             {
6495               int off;
6496
6497               if (breg == 0)
6498                 {
6499                   frag_grow (28);
6500                   tempreg = mips_gp_register;
6501                   off = 0;
6502                   used_at = 0;
6503                 }
6504               else
6505                 {
6506                   frag_grow (36);
6507                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6508                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6509                                "d,v,t", AT, breg, mips_gp_register);
6510                   tempreg = AT;
6511                   off = 4;
6512                   used_at = 1;
6513                 }
6514
6515               /* Itbl support may require additional care here.  */
6516               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6517                            coproc ? treg + 1 : treg,
6518                            (int) BFD_RELOC_GPREL16, tempreg);
6519               offset_expr.X_add_number += 4;
6520
6521               /* Set mips_optimize to 2 to avoid inserting an
6522                  undesired nop.  */
6523               hold_mips_optimize = mips_optimize;
6524               mips_optimize = 2;
6525               /* Itbl support may require additional care here.  */
6526               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6527                            coproc ? treg : treg + 1,
6528                            (int) BFD_RELOC_GPREL16, tempreg);
6529               mips_optimize = hold_mips_optimize;
6530
6531               p = frag_var (rs_machine_dependent, 12 + off, 0,
6532                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6533                                           used_at && mips_opts.noat),
6534                             offset_expr.X_add_symbol, 0, NULL);
6535
6536               /* We just generated two relocs.  When tc_gen_reloc
6537                  handles this case, it will skip the first reloc and
6538                  handle the second.  The second reloc already has an
6539                  extra addend of 4, which we added above.  We must
6540                  subtract it out, and then subtract another 4 to make
6541                  the first reloc come out right.  The second reloc
6542                  will come out right because we are going to add 4 to
6543                  offset_expr when we build its instruction below.
6544
6545                  If we have a symbol, then we don't want to include
6546                  the offset, because it will wind up being included
6547                  when we generate the reloc.  */
6548
6549               if (offset_expr.X_op == O_constant)
6550                 offset_expr.X_add_number -= 8;
6551               else
6552                 {
6553                   offset_expr.X_add_number = -4;
6554                   offset_expr.X_op = O_constant;
6555                 }
6556             }
6557           macro_build_lui (p, &icnt, &offset_expr, AT);
6558           if (p != NULL)
6559             p += 4;
6560           if (breg != 0)
6561             {
6562               macro_build (p, &icnt, (expressionS *) NULL,
6563                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6564                            "d,v,t", AT, breg, AT);
6565               if (p != NULL)
6566                 p += 4;
6567             }
6568           /* Itbl support may require additional care here.  */
6569           macro_build (p, &icnt, &offset_expr, s, fmt,
6570                        coproc ? treg + 1 : treg,
6571                        (int) BFD_RELOC_LO16, AT);
6572           if (p != NULL)
6573             p += 4;
6574           /* FIXME: How do we handle overflow here?  */
6575           offset_expr.X_add_number += 4;
6576           /* Itbl support may require additional care here.  */
6577           macro_build (p, &icnt, &offset_expr, s, fmt,
6578                        coproc ? treg : treg + 1,
6579                        (int) BFD_RELOC_LO16, AT);
6580         }
6581       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6582         {
6583           int off;
6584
6585           /* If this is a reference to an external symbol, we want
6586                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6587                nop
6588                <op>     $treg,0($at)
6589                <op>     $treg+1,4($at)
6590              Otherwise we want
6591                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6592                nop
6593                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6594                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6595              If there is a base register we add it to $at before the
6596              lwc1 instructions.  If there is a constant we include it
6597              in the lwc1 instructions.  */
6598           used_at = 1;
6599           expr1.X_add_number = offset_expr.X_add_number;
6600           offset_expr.X_add_number = 0;
6601           if (expr1.X_add_number < -0x8000
6602               || expr1.X_add_number >= 0x8000 - 4)
6603             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6604           if (breg == 0)
6605             off = 0;
6606           else
6607             off = 4;
6608           frag_grow (24 + off);
6609           macro_build ((char *) NULL, &icnt, &offset_expr,
6610                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6611                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
6612           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6613           if (breg != 0)
6614             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6615                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6616                          "d,v,t", AT, breg, AT);
6617           /* Itbl support may require additional care here.  */
6618           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6619                        coproc ? treg + 1 : treg,
6620                        (int) BFD_RELOC_LO16, AT);
6621           expr1.X_add_number += 4;
6622
6623           /* Set mips_optimize to 2 to avoid inserting an undesired
6624              nop.  */
6625           hold_mips_optimize = mips_optimize;
6626           mips_optimize = 2;
6627           /* Itbl support may require additional care here.  */
6628           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6629                        coproc ? treg : treg + 1,
6630                        (int) BFD_RELOC_LO16, AT);
6631           mips_optimize = hold_mips_optimize;
6632
6633           (void) frag_var (rs_machine_dependent, 0, 0,
6634                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6635                            offset_expr.X_add_symbol, 0, NULL);
6636         }
6637       else if (mips_pic == SVR4_PIC)
6638         {
6639           int gpdel, off;
6640           char *p;
6641
6642           /* If this is a reference to an external symbol, we want
6643                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6644                addu     $at,$at,$gp
6645                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6646                nop
6647                <op>     $treg,0($at)
6648                <op>     $treg+1,4($at)
6649              Otherwise we want
6650                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6651                nop
6652                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6653                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6654              If there is a base register we add it to $at before the
6655              lwc1 instructions.  If there is a constant we include it
6656              in the lwc1 instructions.  */
6657           used_at = 1;
6658           expr1.X_add_number = offset_expr.X_add_number;
6659           offset_expr.X_add_number = 0;
6660           if (expr1.X_add_number < -0x8000
6661               || expr1.X_add_number >= 0x8000 - 4)
6662             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6663           if (reg_needs_delay (mips_gp_register))
6664             gpdel = 4;
6665           else
6666             gpdel = 0;
6667           if (breg == 0)
6668             off = 0;
6669           else
6670             off = 4;
6671           frag_grow (56);
6672           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6673                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6674           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6675                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6676                        "d,v,t", AT, AT, mips_gp_register);
6677           macro_build ((char *) NULL, &icnt, &offset_expr,
6678                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6679                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6680           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6681           if (breg != 0)
6682             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6683                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6684                          "d,v,t", AT, breg, AT);
6685           /* Itbl support may require additional care here.  */
6686           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6687                        coproc ? treg + 1 : treg,
6688                        (int) BFD_RELOC_LO16, AT);
6689           expr1.X_add_number += 4;
6690
6691           /* Set mips_optimize to 2 to avoid inserting an undesired
6692              nop.  */
6693           hold_mips_optimize = mips_optimize;
6694           mips_optimize = 2;
6695           /* Itbl support may require additional care here.  */
6696           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6697                        coproc ? treg : treg + 1,
6698                        (int) BFD_RELOC_LO16, AT);
6699           mips_optimize = hold_mips_optimize;
6700           expr1.X_add_number -= 4;
6701
6702           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6703                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6704                                       8 + gpdel + off, 1, 0),
6705                         offset_expr.X_add_symbol, 0, NULL);
6706           if (gpdel > 0)
6707             {
6708               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6709               p += 4;
6710             }
6711           macro_build (p, &icnt, &offset_expr,
6712                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6713                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6714                        mips_gp_register);
6715           p += 4;
6716           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6717           p += 4;
6718           if (breg != 0)
6719             {
6720               macro_build (p, &icnt, (expressionS *) NULL,
6721                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6722                            "d,v,t", AT, breg, AT);
6723               p += 4;
6724             }
6725           /* Itbl support may require additional care here.  */
6726           macro_build (p, &icnt, &expr1, s, fmt,
6727                        coproc ? treg + 1 : treg,
6728                        (int) BFD_RELOC_LO16, AT);
6729           p += 4;
6730           expr1.X_add_number += 4;
6731
6732           /* Set mips_optimize to 2 to avoid inserting an undesired
6733              nop.  */
6734           hold_mips_optimize = mips_optimize;
6735           mips_optimize = 2;
6736           /* Itbl support may require additional care here.  */
6737           macro_build (p, &icnt, &expr1, s, fmt,
6738                        coproc ? treg : treg + 1,
6739                        (int) BFD_RELOC_LO16, AT);
6740           mips_optimize = hold_mips_optimize;
6741         }
6742       else if (mips_pic == EMBEDDED_PIC)
6743         {
6744           /* If there is no base register, we use
6745                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6746                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6747              If we have a base register, we use
6748                addu     $at,$breg,$gp
6749                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6750                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6751              */
6752           if (breg == 0)
6753             {
6754               tempreg = mips_gp_register;
6755               used_at = 0;
6756             }
6757           else
6758             {
6759               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6760                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6761                            "d,v,t", AT, breg, mips_gp_register);
6762               tempreg = AT;
6763               used_at = 1;
6764             }
6765
6766           /* Itbl support may require additional care here.  */
6767           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6768                        coproc ? treg + 1 : treg,
6769                        (int) BFD_RELOC_GPREL16, tempreg);
6770           offset_expr.X_add_number += 4;
6771           /* Itbl support may require additional care here.  */
6772           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6773                        coproc ? treg : treg + 1,
6774                        (int) BFD_RELOC_GPREL16, tempreg);
6775         }
6776       else
6777         abort ();
6778
6779       if (! used_at)
6780         return;
6781
6782       break;
6783
6784     case M_LD_OB:
6785       s = "lw";
6786       goto sd_ob;
6787     case M_SD_OB:
6788       s = "sw";
6789     sd_ob:
6790       assert (HAVE_32BIT_ADDRESSES);
6791       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6792                    (int) BFD_RELOC_LO16, breg);
6793       offset_expr.X_add_number += 4;
6794       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6795                    (int) BFD_RELOC_LO16, breg);
6796       return;
6797
6798    /* New code added to support COPZ instructions.
6799       This code builds table entries out of the macros in mip_opcodes.
6800       R4000 uses interlocks to handle coproc delays.
6801       Other chips (like the R3000) require nops to be inserted for delays.
6802
6803       FIXME: Currently, we require that the user handle delays.
6804       In order to fill delay slots for non-interlocked chips,
6805       we must have a way to specify delays based on the coprocessor.
6806       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6807       What are the side-effects of the cop instruction?
6808       What cache support might we have and what are its effects?
6809       Both coprocessor & memory require delays. how long???
6810       What registers are read/set/modified?
6811
6812       If an itbl is provided to interpret cop instructions,
6813       this knowledge can be encoded in the itbl spec.  */
6814
6815     case M_COP0:
6816       s = "c0";
6817       goto copz;
6818     case M_COP1:
6819       s = "c1";
6820       goto copz;
6821     case M_COP2:
6822       s = "c2";
6823       goto copz;
6824     case M_COP3:
6825       s = "c3";
6826     copz:
6827       /* For now we just do C (same as Cz).  The parameter will be
6828          stored in insn_opcode by mips_ip.  */
6829       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6830                    ip->insn_opcode);
6831       return;
6832
6833     case M_MOVE:
6834       move_register (&icnt, dreg, sreg);
6835       return;
6836
6837 #ifdef LOSING_COMPILER
6838     default:
6839       /* Try and see if this is a new itbl instruction.
6840          This code builds table entries out of the macros in mip_opcodes.
6841          FIXME: For now we just assemble the expression and pass it's
6842          value along as a 32-bit immediate.
6843          We may want to have the assembler assemble this value,
6844          so that we gain the assembler's knowledge of delay slots,
6845          symbols, etc.
6846          Would it be more efficient to use mask (id) here? */
6847       if (itbl_have_entries
6848           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6849         {
6850           s = ip->insn_mo->name;
6851           s2 = "cop3";
6852           coproc = ITBL_DECODE_PNUM (immed_expr);;
6853           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6854           return;
6855         }
6856       macro2 (ip);
6857       return;
6858     }
6859   if (mips_opts.noat)
6860     as_warn (_("Macro used $at after \".set noat\""));
6861 }
6862
6863 static void
6864 macro2 (ip)
6865      struct mips_cl_insn *ip;
6866 {
6867   register int treg, sreg, dreg, breg;
6868   int tempreg;
6869   int mask;
6870   int icnt = 0;
6871   int used_at;
6872   expressionS expr1;
6873   const char *s;
6874   const char *s2;
6875   const char *fmt;
6876   int likely = 0;
6877   int dbl = 0;
6878   int coproc = 0;
6879   int lr = 0;
6880   int imm = 0;
6881   int off;
6882   offsetT maxnum;
6883   bfd_reloc_code_real_type r;
6884   char *p;
6885
6886   treg = (ip->insn_opcode >> 16) & 0x1f;
6887   dreg = (ip->insn_opcode >> 11) & 0x1f;
6888   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6889   mask = ip->insn_mo->mask;
6890
6891   expr1.X_op = O_constant;
6892   expr1.X_op_symbol = NULL;
6893   expr1.X_add_symbol = NULL;
6894   expr1.X_add_number = 1;
6895
6896   switch (mask)
6897     {
6898 #endif /* LOSING_COMPILER */
6899
6900     case M_DMUL:
6901       dbl = 1;
6902     case M_MUL:
6903       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6904                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6905       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6906                    dreg);
6907       return;
6908
6909     case M_DMUL_I:
6910       dbl = 1;
6911     case M_MUL_I:
6912       /* The MIPS assembler some times generates shifts and adds.  I'm
6913          not trying to be that fancy. GCC should do this for us
6914          anyway.  */
6915       load_register (&icnt, AT, &imm_expr, dbl);
6916       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6917                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
6918       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6919                    dreg);
6920       break;
6921
6922     case M_DMULO_I:
6923       dbl = 1;
6924     case M_MULO_I:
6925       imm = 1;
6926       goto do_mulo;
6927
6928     case M_DMULO:
6929       dbl = 1;
6930     case M_MULO:
6931     do_mulo:
6932       mips_emit_delays (TRUE);
6933       ++mips_opts.noreorder;
6934       mips_any_noreorder = 1;
6935       if (imm)
6936         load_register (&icnt, AT, &imm_expr, dbl);
6937       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6938                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6939       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6940                    dreg);
6941       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6942                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6943       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6944                    AT);
6945       if (mips_trap)
6946         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6947                      "s,t,q", dreg, AT, 6);
6948       else
6949         {
6950           expr1.X_add_number = 8;
6951           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6952                        AT);
6953           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6954                        0);
6955           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6956                        "c", 6);
6957         }
6958       --mips_opts.noreorder;
6959       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6960       break;
6961
6962     case M_DMULOU_I:
6963       dbl = 1;
6964     case M_MULOU_I:
6965       imm = 1;
6966       goto do_mulou;
6967
6968     case M_DMULOU:
6969       dbl = 1;
6970     case M_MULOU:
6971     do_mulou:
6972       mips_emit_delays (TRUE);
6973       ++mips_opts.noreorder;
6974       mips_any_noreorder = 1;
6975       if (imm)
6976         load_register (&icnt, AT, &imm_expr, dbl);
6977       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6978                    dbl ? "dmultu" : "multu",
6979                    "s,t", sreg, imm ? AT : treg);
6980       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6981                    AT);
6982       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6983                    dreg);
6984       if (mips_trap)
6985         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6986                      "s,t,q", AT, 0, 6);
6987       else
6988         {
6989           expr1.X_add_number = 8;
6990           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6991           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6992                        0);
6993           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6994                        "c", 6);
6995         }
6996       --mips_opts.noreorder;
6997       break;
6998
6999     case M_DROL:
7000       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7001         {
7002           if (dreg == sreg)
7003             {
7004               tempreg = AT;
7005               used_at = 1;
7006             }
7007           else
7008             {
7009               tempreg = dreg;
7010               used_at = 0;
7011             }
7012           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7013                        "d,w", tempreg, treg);
7014           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7015                        "d,t,s", dreg, sreg, tempreg);
7016           if (used_at)
7017             break;
7018           return;
7019         }
7020       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7021                    "d,v,t", AT, 0, treg);
7022       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7023                    "d,t,s", AT, sreg, AT);
7024       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7025                    "d,t,s", dreg, sreg, treg);
7026       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7027                    "d,v,t", dreg, dreg, AT);
7028       break;
7029
7030     case M_ROL:
7031       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7032         {
7033           if (dreg == sreg)
7034             {
7035               tempreg = AT;
7036               used_at = 1;
7037             }
7038           else
7039             {
7040               tempreg = dreg;
7041               used_at = 0;
7042             }
7043           macro_build ((char *) NULL, &icnt, NULL, "negu",
7044                        "d,w", tempreg, treg);
7045           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7046                        "d,t,s", dreg, sreg, tempreg);
7047           if (used_at)
7048             break;
7049           return;
7050         }
7051       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7052                    "d,v,t", AT, 0, treg);
7053       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7054                    "d,t,s", AT, sreg, AT);
7055       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7056                    "d,t,s", dreg, sreg, treg);
7057       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7058                    "d,v,t", dreg, dreg, AT);
7059       break;
7060
7061     case M_DROL_I:
7062       {
7063         unsigned int rot;
7064         char *l, *r;
7065
7066         if (imm_expr.X_op != O_constant)
7067           as_bad (_("Improper rotate count"));
7068         rot = imm_expr.X_add_number & 0x3f;
7069         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7070           {
7071             rot = (64 - rot) & 0x3f;
7072             if (rot >= 32)
7073               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7074                            "d,w,<", dreg, sreg, rot - 32);
7075             else
7076               macro_build ((char *) NULL, &icnt, NULL, "dror",
7077                            "d,w,<", dreg, sreg, rot);
7078             return;
7079           }
7080         if (rot == 0)
7081           {
7082             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7083                          "d,w,<", dreg, sreg, 0);
7084             return;
7085           }
7086         l = (rot < 0x20) ? "dsll" : "dsll32";
7087         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7088         rot &= 0x1f;
7089         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7090                      "d,w,<", AT, sreg, rot);
7091         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7092                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7093         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7094                      "d,v,t", dreg, dreg, AT);
7095       }
7096       break;
7097
7098     case M_ROL_I:
7099       {
7100         unsigned int rot;
7101
7102         if (imm_expr.X_op != O_constant)
7103           as_bad (_("Improper rotate count"));
7104         rot = imm_expr.X_add_number & 0x1f;
7105         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7106           {
7107             macro_build ((char *) NULL, &icnt, NULL, "ror",
7108                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7109             return;
7110           }
7111         if (rot == 0)
7112           {
7113             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7114                          "d,w,<", dreg, sreg, 0);
7115             return;
7116           }
7117         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7118                      "d,w,<", AT, sreg, rot);
7119         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7120                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7121         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7122                      "d,v,t", dreg, dreg, AT);
7123       }
7124       break;
7125
7126     case M_DROR:
7127       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7128         {
7129           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7130                        "d,t,s", dreg, sreg, treg);
7131           return;
7132         }
7133       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7134                    "d,v,t", AT, 0, treg);
7135       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7136                    "d,t,s", AT, sreg, AT);
7137       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7138                    "d,t,s", dreg, sreg, treg);
7139       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7140                    "d,v,t", dreg, dreg, AT);
7141       break;
7142
7143     case M_ROR:
7144       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7145         {
7146           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7147                        "d,t,s", dreg, sreg, treg);
7148           return;
7149         }
7150       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7151                    "d,v,t", AT, 0, treg);
7152       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7153                    "d,t,s", AT, sreg, AT);
7154       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7155                    "d,t,s", dreg, sreg, treg);
7156       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7157                    "d,v,t", dreg, dreg, AT);
7158       break;
7159
7160     case M_DROR_I:
7161       {
7162         unsigned int rot;
7163         char *l, *r;
7164
7165         if (imm_expr.X_op != O_constant)
7166           as_bad (_("Improper rotate count"));
7167         rot = imm_expr.X_add_number & 0x3f;
7168         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7169           {
7170             if (rot >= 32)
7171               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7172                            "d,w,<", dreg, sreg, rot - 32);
7173             else
7174               macro_build ((char *) NULL, &icnt, NULL, "dror",
7175                            "d,w,<", dreg, sreg, rot);
7176             return;
7177           }
7178         if (rot == 0)
7179           {
7180             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7181                          "d,w,<", dreg, sreg, 0);
7182             return;
7183           }
7184         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7185         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7186         rot &= 0x1f;
7187         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7188                      "d,w,<", AT, sreg, rot);
7189         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7190                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7191         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7192                      "d,v,t", dreg, dreg, AT);
7193       }
7194       break;
7195
7196     case M_ROR_I:
7197       {
7198         unsigned int rot;
7199
7200         if (imm_expr.X_op != O_constant)
7201           as_bad (_("Improper rotate count"));
7202         rot = imm_expr.X_add_number & 0x1f;
7203         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7204           {
7205             macro_build ((char *) NULL, &icnt, NULL, "ror",
7206                          "d,w,<", dreg, sreg, rot);
7207             return;
7208           }
7209         if (rot == 0)
7210           {
7211             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7212                          "d,w,<", dreg, sreg, 0);
7213             return;
7214           }
7215         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7216                      "d,w,<", AT, sreg, rot);
7217         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7218                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7219         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7220                      "d,v,t", dreg, dreg, AT);
7221       }
7222       break;
7223
7224     case M_S_DOB:
7225       if (mips_arch == CPU_R4650)
7226         {
7227           as_bad (_("opcode not supported on this processor"));
7228           return;
7229         }
7230       assert (mips_opts.isa == ISA_MIPS1);
7231       /* Even on a big endian machine $fn comes before $fn+1.  We have
7232          to adjust when storing to memory.  */
7233       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7234                    target_big_endian ? treg + 1 : treg,
7235                    (int) BFD_RELOC_LO16, breg);
7236       offset_expr.X_add_number += 4;
7237       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7238                    target_big_endian ? treg : treg + 1,
7239                    (int) BFD_RELOC_LO16, breg);
7240       return;
7241
7242     case M_SEQ:
7243       if (sreg == 0)
7244         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7245                      treg, (int) BFD_RELOC_LO16);
7246       else if (treg == 0)
7247         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7248                      sreg, (int) BFD_RELOC_LO16);
7249       else
7250         {
7251           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7252                        "d,v,t", dreg, sreg, treg);
7253           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7254                        dreg, (int) BFD_RELOC_LO16);
7255         }
7256       return;
7257
7258     case M_SEQ_I:
7259       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7260         {
7261           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7262                        sreg, (int) BFD_RELOC_LO16);
7263           return;
7264         }
7265       if (sreg == 0)
7266         {
7267           as_warn (_("Instruction %s: result is always false"),
7268                    ip->insn_mo->name);
7269           move_register (&icnt, dreg, 0);
7270           return;
7271         }
7272       if (imm_expr.X_op == O_constant
7273           && imm_expr.X_add_number >= 0
7274           && imm_expr.X_add_number < 0x10000)
7275         {
7276           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7277                        sreg, (int) BFD_RELOC_LO16);
7278           used_at = 0;
7279         }
7280       else if (imm_expr.X_op == O_constant
7281                && imm_expr.X_add_number > -0x8000
7282                && imm_expr.X_add_number < 0)
7283         {
7284           imm_expr.X_add_number = -imm_expr.X_add_number;
7285           macro_build ((char *) NULL, &icnt, &imm_expr,
7286                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7287                        "t,r,j", dreg, sreg,
7288                        (int) BFD_RELOC_LO16);
7289           used_at = 0;
7290         }
7291       else
7292         {
7293           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7294           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7295                        "d,v,t", dreg, sreg, AT);
7296           used_at = 1;
7297         }
7298       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7299                    (int) BFD_RELOC_LO16);
7300       if (used_at)
7301         break;
7302       return;
7303
7304     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7305       s = "slt";
7306       goto sge;
7307     case M_SGEU:
7308       s = "sltu";
7309     sge:
7310       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7311                    dreg, sreg, treg);
7312       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7313                    (int) BFD_RELOC_LO16);
7314       return;
7315
7316     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7317     case M_SGEU_I:
7318       if (imm_expr.X_op == O_constant
7319           && imm_expr.X_add_number >= -0x8000
7320           && imm_expr.X_add_number < 0x8000)
7321         {
7322           macro_build ((char *) NULL, &icnt, &imm_expr,
7323                        mask == M_SGE_I ? "slti" : "sltiu",
7324                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7325           used_at = 0;
7326         }
7327       else
7328         {
7329           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7330           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7331                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7332                        AT);
7333           used_at = 1;
7334         }
7335       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7336                    (int) BFD_RELOC_LO16);
7337       if (used_at)
7338         break;
7339       return;
7340
7341     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7342       s = "slt";
7343       goto sgt;
7344     case M_SGTU:
7345       s = "sltu";
7346     sgt:
7347       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7348                    dreg, treg, sreg);
7349       return;
7350
7351     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7352       s = "slt";
7353       goto sgti;
7354     case M_SGTU_I:
7355       s = "sltu";
7356     sgti:
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       break;
7361
7362     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7363       s = "slt";
7364       goto sle;
7365     case M_SLEU:
7366       s = "sltu";
7367     sle:
7368       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7369                    dreg, treg, sreg);
7370       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7371                    (int) BFD_RELOC_LO16);
7372       return;
7373
7374     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7375       s = "slt";
7376       goto slei;
7377     case M_SLEU_I:
7378       s = "sltu";
7379     slei:
7380       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7381       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7382                    dreg, AT, sreg);
7383       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7384                    (int) BFD_RELOC_LO16);
7385       break;
7386
7387     case M_SLT_I:
7388       if (imm_expr.X_op == O_constant
7389           && imm_expr.X_add_number >= -0x8000
7390           && imm_expr.X_add_number < 0x8000)
7391         {
7392           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7393                        dreg, sreg, (int) BFD_RELOC_LO16);
7394           return;
7395         }
7396       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7397       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7398                    dreg, sreg, AT);
7399       break;
7400
7401     case M_SLTU_I:
7402       if (imm_expr.X_op == O_constant
7403           && imm_expr.X_add_number >= -0x8000
7404           && imm_expr.X_add_number < 0x8000)
7405         {
7406           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7407                        dreg, sreg, (int) BFD_RELOC_LO16);
7408           return;
7409         }
7410       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7411       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7412                    "d,v,t", dreg, sreg, AT);
7413       break;
7414
7415     case M_SNE:
7416       if (sreg == 0)
7417         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7418                      "d,v,t", dreg, 0, treg);
7419       else if (treg == 0)
7420         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7421                      "d,v,t", dreg, 0, sreg);
7422       else
7423         {
7424           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7425                        "d,v,t", dreg, sreg, treg);
7426           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7427                        "d,v,t", dreg, 0, dreg);
7428         }
7429       return;
7430
7431     case M_SNE_I:
7432       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7433         {
7434           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7435                        "d,v,t", dreg, 0, sreg);
7436           return;
7437         }
7438       if (sreg == 0)
7439         {
7440           as_warn (_("Instruction %s: result is always true"),
7441                    ip->insn_mo->name);
7442           macro_build ((char *) NULL, &icnt, &expr1,
7443                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7444                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7445           return;
7446         }
7447       if (imm_expr.X_op == O_constant
7448           && imm_expr.X_add_number >= 0
7449           && imm_expr.X_add_number < 0x10000)
7450         {
7451           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7452                        dreg, sreg, (int) BFD_RELOC_LO16);
7453           used_at = 0;
7454         }
7455       else if (imm_expr.X_op == O_constant
7456                && imm_expr.X_add_number > -0x8000
7457                && imm_expr.X_add_number < 0)
7458         {
7459           imm_expr.X_add_number = -imm_expr.X_add_number;
7460           macro_build ((char *) NULL, &icnt, &imm_expr,
7461                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7462                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7463           used_at = 0;
7464         }
7465       else
7466         {
7467           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7468           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7469                        "d,v,t", dreg, sreg, AT);
7470           used_at = 1;
7471         }
7472       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7473                    "d,v,t", dreg, 0, dreg);
7474       if (used_at)
7475         break;
7476       return;
7477
7478     case M_DSUB_I:
7479       dbl = 1;
7480     case M_SUB_I:
7481       if (imm_expr.X_op == O_constant
7482           && imm_expr.X_add_number > -0x8000
7483           && imm_expr.X_add_number <= 0x8000)
7484         {
7485           imm_expr.X_add_number = -imm_expr.X_add_number;
7486           macro_build ((char *) NULL, &icnt, &imm_expr,
7487                        dbl ? "daddi" : "addi",
7488                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7489           return;
7490         }
7491       load_register (&icnt, AT, &imm_expr, dbl);
7492       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7493                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7494       break;
7495
7496     case M_DSUBU_I:
7497       dbl = 1;
7498     case M_SUBU_I:
7499       if (imm_expr.X_op == O_constant
7500           && imm_expr.X_add_number > -0x8000
7501           && imm_expr.X_add_number <= 0x8000)
7502         {
7503           imm_expr.X_add_number = -imm_expr.X_add_number;
7504           macro_build ((char *) NULL, &icnt, &imm_expr,
7505                        dbl ? "daddiu" : "addiu",
7506                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7507           return;
7508         }
7509       load_register (&icnt, AT, &imm_expr, dbl);
7510       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7511                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7512       break;
7513
7514     case M_TEQ_I:
7515       s = "teq";
7516       goto trap;
7517     case M_TGE_I:
7518       s = "tge";
7519       goto trap;
7520     case M_TGEU_I:
7521       s = "tgeu";
7522       goto trap;
7523     case M_TLT_I:
7524       s = "tlt";
7525       goto trap;
7526     case M_TLTU_I:
7527       s = "tltu";
7528       goto trap;
7529     case M_TNE_I:
7530       s = "tne";
7531     trap:
7532       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7533       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7534                    AT);
7535       break;
7536
7537     case M_TRUNCWS:
7538     case M_TRUNCWD:
7539       assert (mips_opts.isa == ISA_MIPS1);
7540       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7541       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7542
7543       /*
7544        * Is the double cfc1 instruction a bug in the mips assembler;
7545        * or is there a reason for it?
7546        */
7547       mips_emit_delays (TRUE);
7548       ++mips_opts.noreorder;
7549       mips_any_noreorder = 1;
7550       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7551                    treg, RA);
7552       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7553                    treg, RA);
7554       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7555       expr1.X_add_number = 3;
7556       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7557                    (int) BFD_RELOC_LO16);
7558       expr1.X_add_number = 2;
7559       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7560                      (int) BFD_RELOC_LO16);
7561       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7562                    AT, RA);
7563       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7564       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7565               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7566       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7567                    treg, RA);
7568       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7569       --mips_opts.noreorder;
7570       break;
7571
7572     case M_ULH:
7573       s = "lb";
7574       goto ulh;
7575     case M_ULHU:
7576       s = "lbu";
7577     ulh:
7578       if (offset_expr.X_add_number >= 0x7fff)
7579         as_bad (_("operand overflow"));
7580       if (! target_big_endian)
7581         ++offset_expr.X_add_number;
7582       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7583                    (int) BFD_RELOC_LO16, breg);
7584       if (! target_big_endian)
7585         --offset_expr.X_add_number;
7586       else
7587         ++offset_expr.X_add_number;
7588       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7589                    (int) BFD_RELOC_LO16, breg);
7590       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7591                    AT, AT, 8);
7592       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7593                    treg, treg, AT);
7594       break;
7595
7596     case M_ULD:
7597       s = "ldl";
7598       s2 = "ldr";
7599       off = 7;
7600       goto ulw;
7601     case M_ULW:
7602       s = "lwl";
7603       s2 = "lwr";
7604       off = 3;
7605     ulw:
7606       if (offset_expr.X_add_number >= 0x8000 - off)
7607         as_bad (_("operand overflow"));
7608       if (treg != breg)
7609         tempreg = treg;
7610       else
7611         tempreg = AT;
7612       if (! target_big_endian)
7613         offset_expr.X_add_number += off;
7614       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7615                    (int) BFD_RELOC_LO16, breg);
7616       if (! target_big_endian)
7617         offset_expr.X_add_number -= off;
7618       else
7619         offset_expr.X_add_number += off;
7620       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7621                    (int) BFD_RELOC_LO16, breg);
7622
7623       /* If necessary, move the result in tempreg the final destination.  */
7624       if (treg == tempreg)
7625         return;
7626       /* Protect second load's delay slot.  */
7627       if (!gpr_interlocks)
7628         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7629       move_register (&icnt, treg, tempreg);
7630       break;
7631
7632     case M_ULD_A:
7633       s = "ldl";
7634       s2 = "ldr";
7635       off = 7;
7636       goto ulwa;
7637     case M_ULW_A:
7638       s = "lwl";
7639       s2 = "lwr";
7640       off = 3;
7641     ulwa:
7642       used_at = 1;
7643       load_address (&icnt, AT, &offset_expr, &used_at);
7644       if (breg != 0)
7645         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7646                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7647                      "d,v,t", AT, AT, breg);
7648       if (! target_big_endian)
7649         expr1.X_add_number = off;
7650       else
7651         expr1.X_add_number = 0;
7652       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7653                    (int) BFD_RELOC_LO16, AT);
7654       if (! target_big_endian)
7655         expr1.X_add_number = 0;
7656       else
7657         expr1.X_add_number = off;
7658       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7659                    (int) BFD_RELOC_LO16, AT);
7660       break;
7661
7662     case M_ULH_A:
7663     case M_ULHU_A:
7664       used_at = 1;
7665       load_address (&icnt, AT, &offset_expr, &used_at);
7666       if (breg != 0)
7667         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7668                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7669                      "d,v,t", AT, AT, breg);
7670       if (target_big_endian)
7671         expr1.X_add_number = 0;
7672       macro_build ((char *) NULL, &icnt, &expr1,
7673                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7674                    (int) BFD_RELOC_LO16, AT);
7675       if (target_big_endian)
7676         expr1.X_add_number = 1;
7677       else
7678         expr1.X_add_number = 0;
7679       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7680                    (int) BFD_RELOC_LO16, AT);
7681       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7682                    treg, treg, 8);
7683       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7684                    treg, treg, AT);
7685       break;
7686
7687     case M_USH:
7688       if (offset_expr.X_add_number >= 0x7fff)
7689         as_bad (_("operand overflow"));
7690       if (target_big_endian)
7691         ++offset_expr.X_add_number;
7692       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7693                    (int) BFD_RELOC_LO16, breg);
7694       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7695                    AT, treg, 8);
7696       if (target_big_endian)
7697         --offset_expr.X_add_number;
7698       else
7699         ++offset_expr.X_add_number;
7700       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7701                    (int) BFD_RELOC_LO16, breg);
7702       break;
7703
7704     case M_USD:
7705       s = "sdl";
7706       s2 = "sdr";
7707       off = 7;
7708       goto usw;
7709     case M_USW:
7710       s = "swl";
7711       s2 = "swr";
7712       off = 3;
7713     usw:
7714       if (offset_expr.X_add_number >= 0x8000 - off)
7715         as_bad (_("operand overflow"));
7716       if (! target_big_endian)
7717         offset_expr.X_add_number += off;
7718       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7719                    (int) BFD_RELOC_LO16, breg);
7720       if (! target_big_endian)
7721         offset_expr.X_add_number -= off;
7722       else
7723         offset_expr.X_add_number += off;
7724       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7725                    (int) BFD_RELOC_LO16, breg);
7726       return;
7727
7728     case M_USD_A:
7729       s = "sdl";
7730       s2 = "sdr";
7731       off = 7;
7732       goto uswa;
7733     case M_USW_A:
7734       s = "swl";
7735       s2 = "swr";
7736       off = 3;
7737     uswa:
7738       used_at = 1;
7739       load_address (&icnt, AT, &offset_expr, &used_at);
7740       if (breg != 0)
7741         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7742                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7743                      "d,v,t", AT, AT, breg);
7744       if (! target_big_endian)
7745         expr1.X_add_number = off;
7746       else
7747         expr1.X_add_number = 0;
7748       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7749                    (int) BFD_RELOC_LO16, AT);
7750       if (! target_big_endian)
7751         expr1.X_add_number = 0;
7752       else
7753         expr1.X_add_number = off;
7754       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7755                    (int) BFD_RELOC_LO16, AT);
7756       break;
7757
7758     case M_USH_A:
7759       used_at = 1;
7760       load_address (&icnt, AT, &offset_expr, &used_at);
7761       if (breg != 0)
7762         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7763                      HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7764                      "d,v,t", AT, AT, breg);
7765       if (! target_big_endian)
7766         expr1.X_add_number = 0;
7767       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7768                    (int) BFD_RELOC_LO16, AT);
7769       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7770                    treg, treg, 8);
7771       if (! target_big_endian)
7772         expr1.X_add_number = 1;
7773       else
7774         expr1.X_add_number = 0;
7775       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7776                    (int) BFD_RELOC_LO16, AT);
7777       if (! target_big_endian)
7778         expr1.X_add_number = 0;
7779       else
7780         expr1.X_add_number = 1;
7781       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7782                    (int) BFD_RELOC_LO16, AT);
7783       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7784                    treg, treg, 8);
7785       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7786                    treg, treg, AT);
7787       break;
7788
7789     default:
7790       /* FIXME: Check if this is one of the itbl macros, since they
7791          are added dynamically.  */
7792       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7793       break;
7794     }
7795   if (mips_opts.noat)
7796     as_warn (_("Macro used $at after \".set noat\""));
7797 }
7798
7799 /* Implement macros in mips16 mode.  */
7800
7801 static void
7802 mips16_macro (ip)
7803      struct mips_cl_insn *ip;
7804 {
7805   int mask;
7806   int xreg, yreg, zreg, tmp;
7807   int icnt;
7808   expressionS expr1;
7809   int dbl;
7810   const char *s, *s2, *s3;
7811
7812   mask = ip->insn_mo->mask;
7813
7814   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7815   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7816   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7817
7818   icnt = 0;
7819
7820   expr1.X_op = O_constant;
7821   expr1.X_op_symbol = NULL;
7822   expr1.X_add_symbol = NULL;
7823   expr1.X_add_number = 1;
7824
7825   dbl = 0;
7826
7827   switch (mask)
7828     {
7829     default:
7830       internalError ();
7831
7832     case M_DDIV_3:
7833       dbl = 1;
7834     case M_DIV_3:
7835       s = "mflo";
7836       goto do_div3;
7837     case M_DREM_3:
7838       dbl = 1;
7839     case M_REM_3:
7840       s = "mfhi";
7841     do_div3:
7842       mips_emit_delays (TRUE);
7843       ++mips_opts.noreorder;
7844       mips_any_noreorder = 1;
7845       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7846                    dbl ? "ddiv" : "div",
7847                    "0,x,y", xreg, yreg);
7848       expr1.X_add_number = 2;
7849       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7850       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7851                    7);
7852
7853       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7854          since that causes an overflow.  We should do that as well,
7855          but I don't see how to do the comparisons without a temporary
7856          register.  */
7857       --mips_opts.noreorder;
7858       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7859       break;
7860
7861     case M_DIVU_3:
7862       s = "divu";
7863       s2 = "mflo";
7864       goto do_divu3;
7865     case M_REMU_3:
7866       s = "divu";
7867       s2 = "mfhi";
7868       goto do_divu3;
7869     case M_DDIVU_3:
7870       s = "ddivu";
7871       s2 = "mflo";
7872       goto do_divu3;
7873     case M_DREMU_3:
7874       s = "ddivu";
7875       s2 = "mfhi";
7876     do_divu3:
7877       mips_emit_delays (TRUE);
7878       ++mips_opts.noreorder;
7879       mips_any_noreorder = 1;
7880       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7881                    xreg, yreg);
7882       expr1.X_add_number = 2;
7883       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7884       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7885                    "6", 7);
7886       --mips_opts.noreorder;
7887       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7888       break;
7889
7890     case M_DMUL:
7891       dbl = 1;
7892     case M_MUL:
7893       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7894                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7895       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7896                    zreg);
7897       return;
7898
7899     case M_DSUBU_I:
7900       dbl = 1;
7901       goto do_subu;
7902     case M_SUBU_I:
7903     do_subu:
7904       if (imm_expr.X_op != O_constant)
7905         as_bad (_("Unsupported large constant"));
7906       imm_expr.X_add_number = -imm_expr.X_add_number;
7907       macro_build ((char *) NULL, &icnt, &imm_expr,
7908                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7909       break;
7910
7911     case M_SUBU_I_2:
7912       if (imm_expr.X_op != O_constant)
7913         as_bad (_("Unsupported large constant"));
7914       imm_expr.X_add_number = -imm_expr.X_add_number;
7915       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7916                    "x,k", xreg);
7917       break;
7918
7919     case M_DSUBU_I_2:
7920       if (imm_expr.X_op != O_constant)
7921         as_bad (_("Unsupported large constant"));
7922       imm_expr.X_add_number = -imm_expr.X_add_number;
7923       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7924                    "y,j", yreg);
7925       break;
7926
7927     case M_BEQ:
7928       s = "cmp";
7929       s2 = "bteqz";
7930       goto do_branch;
7931     case M_BNE:
7932       s = "cmp";
7933       s2 = "btnez";
7934       goto do_branch;
7935     case M_BLT:
7936       s = "slt";
7937       s2 = "btnez";
7938       goto do_branch;
7939     case M_BLTU:
7940       s = "sltu";
7941       s2 = "btnez";
7942       goto do_branch;
7943     case M_BLE:
7944       s = "slt";
7945       s2 = "bteqz";
7946       goto do_reverse_branch;
7947     case M_BLEU:
7948       s = "sltu";
7949       s2 = "bteqz";
7950       goto do_reverse_branch;
7951     case M_BGE:
7952       s = "slt";
7953       s2 = "bteqz";
7954       goto do_branch;
7955     case M_BGEU:
7956       s = "sltu";
7957       s2 = "bteqz";
7958       goto do_branch;
7959     case M_BGT:
7960       s = "slt";
7961       s2 = "btnez";
7962       goto do_reverse_branch;
7963     case M_BGTU:
7964       s = "sltu";
7965       s2 = "btnez";
7966
7967     do_reverse_branch:
7968       tmp = xreg;
7969       xreg = yreg;
7970       yreg = tmp;
7971
7972     do_branch:
7973       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7974                    xreg, yreg);
7975       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7976       break;
7977
7978     case M_BEQ_I:
7979       s = "cmpi";
7980       s2 = "bteqz";
7981       s3 = "x,U";
7982       goto do_branch_i;
7983     case M_BNE_I:
7984       s = "cmpi";
7985       s2 = "btnez";
7986       s3 = "x,U";
7987       goto do_branch_i;
7988     case M_BLT_I:
7989       s = "slti";
7990       s2 = "btnez";
7991       s3 = "x,8";
7992       goto do_branch_i;
7993     case M_BLTU_I:
7994       s = "sltiu";
7995       s2 = "btnez";
7996       s3 = "x,8";
7997       goto do_branch_i;
7998     case M_BLE_I:
7999       s = "slti";
8000       s2 = "btnez";
8001       s3 = "x,8";
8002       goto do_addone_branch_i;
8003     case M_BLEU_I:
8004       s = "sltiu";
8005       s2 = "btnez";
8006       s3 = "x,8";
8007       goto do_addone_branch_i;
8008     case M_BGE_I:
8009       s = "slti";
8010       s2 = "bteqz";
8011       s3 = "x,8";
8012       goto do_branch_i;
8013     case M_BGEU_I:
8014       s = "sltiu";
8015       s2 = "bteqz";
8016       s3 = "x,8";
8017       goto do_branch_i;
8018     case M_BGT_I:
8019       s = "slti";
8020       s2 = "bteqz";
8021       s3 = "x,8";
8022       goto do_addone_branch_i;
8023     case M_BGTU_I:
8024       s = "sltiu";
8025       s2 = "bteqz";
8026       s3 = "x,8";
8027
8028     do_addone_branch_i:
8029       if (imm_expr.X_op != O_constant)
8030         as_bad (_("Unsupported large constant"));
8031       ++imm_expr.X_add_number;
8032
8033     do_branch_i:
8034       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8035       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8036       break;
8037
8038     case M_ABS:
8039       expr1.X_add_number = 0;
8040       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8041       if (xreg != yreg)
8042         move_register (&icnt, xreg, yreg);
8043       expr1.X_add_number = 2;
8044       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8045       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8046                    "neg", "x,w", xreg, xreg);
8047     }
8048 }
8049
8050 /* For consistency checking, verify that all bits are specified either
8051    by the match/mask part of the instruction definition, or by the
8052    operand list.  */
8053 static int
8054 validate_mips_insn (opc)
8055      const struct mips_opcode *opc;
8056 {
8057   const char *p = opc->args;
8058   char c;
8059   unsigned long used_bits = opc->mask;
8060
8061   if ((used_bits & opc->match) != opc->match)
8062     {
8063       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8064               opc->name, opc->args);
8065       return 0;
8066     }
8067 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8068   while (*p)
8069     switch (c = *p++)
8070       {
8071       case ',': break;
8072       case '(': break;
8073       case ')': break;
8074       case '+':
8075         switch (c = *p++)
8076           {
8077           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8078           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8079           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8080           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8081                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8082           default:
8083             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8084                     c, opc->name, opc->args);
8085             return 0;
8086           }
8087         break;
8088       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8089       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8090       case 'A': break;
8091       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8092       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8093       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8094       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8095       case 'F': break;
8096       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8097       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8098       case 'I': break;
8099       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8100       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8101       case 'L': break;
8102       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8103       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8104       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8105       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8106                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8107       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8108       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8109       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8110       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8111       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8112       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8113       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8114       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8115       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8116       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8117       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8118       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8119       case 'f': break;
8120       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8121       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8122       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8123       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8124       case 'l': break;
8125       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8126       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8127       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8128       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8129       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8130       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8131       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8132       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8133       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8134       case 'x': break;
8135       case 'z': break;
8136       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8137       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8138                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8139       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8140       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8141       case '[': break;
8142       case ']': break;
8143       default:
8144         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8145                 c, opc->name, opc->args);
8146         return 0;
8147       }
8148 #undef USE_BITS
8149   if (used_bits != 0xffffffff)
8150     {
8151       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8152               ~used_bits & 0xffffffff, opc->name, opc->args);
8153       return 0;
8154     }
8155   return 1;
8156 }
8157
8158 /* This routine assembles an instruction into its binary format.  As a
8159    side effect, it sets one of the global variables imm_reloc or
8160    offset_reloc to the type of relocation to do if one of the operands
8161    is an address expression.  */
8162
8163 static void
8164 mips_ip (str, ip)
8165      char *str;
8166      struct mips_cl_insn *ip;
8167 {
8168   char *s;
8169   const char *args;
8170   char c = 0;
8171   struct mips_opcode *insn;
8172   char *argsStart;
8173   unsigned int regno;
8174   unsigned int lastregno = 0;
8175   unsigned int lastpos = 0;
8176   unsigned int limlo, limhi;
8177   char *s_reset;
8178   char save_c = 0;
8179
8180   insn_error = NULL;
8181
8182   /* If the instruction contains a '.', we first try to match an instruction
8183      including the '.'.  Then we try again without the '.'.  */
8184   insn = NULL;
8185   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8186     continue;
8187
8188   /* If we stopped on whitespace, then replace the whitespace with null for
8189      the call to hash_find.  Save the character we replaced just in case we
8190      have to re-parse the instruction.  */
8191   if (ISSPACE (*s))
8192     {
8193       save_c = *s;
8194       *s++ = '\0';
8195     }
8196
8197   insn = (struct mips_opcode *) hash_find (op_hash, str);
8198
8199   /* If we didn't find the instruction in the opcode table, try again, but
8200      this time with just the instruction up to, but not including the
8201      first '.'.  */
8202   if (insn == NULL)
8203     {
8204       /* Restore the character we overwrite above (if any).  */
8205       if (save_c)
8206         *(--s) = save_c;
8207
8208       /* Scan up to the first '.' or whitespace.  */
8209       for (s = str;
8210            *s != '\0' && *s != '.' && !ISSPACE (*s);
8211            ++s)
8212         continue;
8213
8214       /* If we did not find a '.', then we can quit now.  */
8215       if (*s != '.')
8216         {
8217           insn_error = "unrecognized opcode";
8218           return;
8219         }
8220
8221       /* Lookup the instruction in the hash table.  */
8222       *s++ = '\0';
8223       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8224         {
8225           insn_error = "unrecognized opcode";
8226           return;
8227         }
8228     }
8229
8230   argsStart = s;
8231   for (;;)
8232     {
8233       bfd_boolean ok;
8234
8235       assert (strcmp (insn->name, str) == 0);
8236
8237       if (OPCODE_IS_MEMBER (insn,
8238                             (mips_opts.isa
8239                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8240                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8241                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8242                             mips_arch))
8243         ok = TRUE;
8244       else
8245         ok = FALSE;
8246
8247       if (insn->pinfo != INSN_MACRO)
8248         {
8249           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8250             ok = FALSE;
8251         }
8252
8253       if (! ok)
8254         {
8255           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8256               && strcmp (insn->name, insn[1].name) == 0)
8257             {
8258               ++insn;
8259               continue;
8260             }
8261           else
8262             {
8263               if (!insn_error)
8264                 {
8265                   static char buf[100];
8266                   if (mips_arch_info->is_isa)
8267                     sprintf (buf,
8268                              _("opcode not supported at this ISA level (%s)"),
8269                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8270                   else
8271                     sprintf (buf,
8272                              _("opcode not supported on this processor: %s (%s)"),
8273                              mips_arch_info->name,
8274                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8275                   insn_error = buf;
8276                 }
8277               if (save_c)
8278                 *(--s) = save_c;
8279               return;
8280             }
8281         }
8282
8283       ip->insn_mo = insn;
8284       ip->insn_opcode = insn->match;
8285       insn_error = NULL;
8286       for (args = insn->args;; ++args)
8287         {
8288           int is_mdmx;
8289
8290           s += strspn (s, " \t");
8291           is_mdmx = 0;
8292           switch (*args)
8293             {
8294             case '\0':          /* end of args */
8295               if (*s == '\0')
8296                 return;
8297               break;
8298
8299             case ',':
8300               if (*s++ == *args)
8301                 continue;
8302               s--;
8303               switch (*++args)
8304                 {
8305                 case 'r':
8306                 case 'v':
8307                   ip->insn_opcode |= lastregno << OP_SH_RS;
8308                   continue;
8309
8310                 case 'w':
8311                   ip->insn_opcode |= lastregno << OP_SH_RT;
8312                   continue;
8313
8314                 case 'W':
8315                   ip->insn_opcode |= lastregno << OP_SH_FT;
8316                   continue;
8317
8318                 case 'V':
8319                   ip->insn_opcode |= lastregno << OP_SH_FS;
8320                   continue;
8321                 }
8322               break;
8323
8324             case '(':
8325               /* Handle optional base register.
8326                  Either the base register is omitted or
8327                  we must have a left paren.  */
8328               /* This is dependent on the next operand specifier
8329                  is a base register specification.  */
8330               assert (args[1] == 'b' || args[1] == '5'
8331                       || args[1] == '-' || args[1] == '4');
8332               if (*s == '\0')
8333                 return;
8334
8335             case ')':           /* these must match exactly */
8336             case '[':
8337             case ']':
8338               if (*s++ == *args)
8339                 continue;
8340               break;
8341
8342             case '+':           /* Opcode extension character.  */
8343               switch (*++args)
8344                 {
8345                 case 'A':               /* ins/ext position, becomes LSB.  */
8346                   limlo = 0;
8347                   limhi = 31;
8348                   my_getExpression (&imm_expr, s);
8349                   check_absolute_expr (ip, &imm_expr);
8350                   if ((unsigned long) imm_expr.X_add_number < limlo
8351                       || (unsigned long) imm_expr.X_add_number > limhi)
8352                     {
8353                       as_bad (_("Improper position (%lu)"),
8354                               (unsigned long) imm_expr.X_add_number);
8355                       imm_expr.X_add_number = limlo;
8356                     }
8357                   lastpos = imm_expr.X_add_number;
8358                   ip->insn_opcode |= (imm_expr.X_add_number
8359                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8360                   imm_expr.X_op = O_absent;
8361                   s = expr_end;
8362                   continue;
8363
8364                 case 'B':               /* ins size, becomes MSB.  */
8365                   limlo = 1;
8366                   limhi = 32;
8367                   my_getExpression (&imm_expr, s);
8368                   check_absolute_expr (ip, &imm_expr);
8369                   /* Check for negative input so that small negative numbers
8370                      will not succeed incorrectly.  The checks against
8371                      (pos+size) transitively check "size" itself,
8372                      assuming that "pos" is reasonable.  */
8373                   if ((long) imm_expr.X_add_number < 0
8374                       || ((unsigned long) imm_expr.X_add_number
8375                           + lastpos) < limlo
8376                       || ((unsigned long) imm_expr.X_add_number
8377                           + lastpos) > limhi)
8378                     {
8379                       as_bad (_("Improper insert size (%lu, position %lu)"),
8380                               (unsigned long) imm_expr.X_add_number,
8381                               (unsigned long) lastpos);
8382                       imm_expr.X_add_number = limlo - lastpos;
8383                     }
8384                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8385                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8386                   imm_expr.X_op = O_absent;
8387                   s = expr_end;
8388                   continue;
8389
8390                 case 'C':               /* ext size, becomes MSBD.  */
8391                   limlo = 1;
8392                   limhi = 32;
8393                   my_getExpression (&imm_expr, s);
8394                   check_absolute_expr (ip, &imm_expr);
8395                   /* Check for negative input so that small negative numbers
8396                      will not succeed incorrectly.  The checks against
8397                      (pos+size) transitively check "size" itself,
8398                      assuming that "pos" is reasonable.  */
8399                   if ((long) imm_expr.X_add_number < 0
8400                       || ((unsigned long) imm_expr.X_add_number
8401                           + lastpos) < limlo
8402                       || ((unsigned long) imm_expr.X_add_number
8403                           + lastpos) > limhi)
8404                     {
8405                       as_bad (_("Improper extract size (%lu, position %lu)"),
8406                               (unsigned long) imm_expr.X_add_number,
8407                               (unsigned long) lastpos);
8408                       imm_expr.X_add_number = limlo - lastpos;
8409                     }
8410                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8411                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8412                   imm_expr.X_op = O_absent;
8413                   s = expr_end;
8414                   continue;
8415
8416                 case 'D':
8417                   /* +D is for disassembly only; never match.  */
8418                   break;
8419
8420                 default:
8421                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8422                     *args, insn->name, insn->args);
8423                   /* Further processing is fruitless.  */
8424                   return;
8425                 }
8426               break;
8427
8428             case '<':           /* must be at least one digit */
8429               /*
8430                * According to the manual, if the shift amount is greater
8431                * than 31 or less than 0, then the shift amount should be
8432                * mod 32.  In reality the mips assembler issues an error.
8433                * We issue a warning and mask out all but the low 5 bits.
8434                */
8435               my_getExpression (&imm_expr, s);
8436               check_absolute_expr (ip, &imm_expr);
8437               if ((unsigned long) imm_expr.X_add_number > 31)
8438                 {
8439                   as_warn (_("Improper shift amount (%lu)"),
8440                            (unsigned long) imm_expr.X_add_number);
8441                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8442                 }
8443               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8444               imm_expr.X_op = O_absent;
8445               s = expr_end;
8446               continue;
8447
8448             case '>':           /* shift amount minus 32 */
8449               my_getExpression (&imm_expr, s);
8450               check_absolute_expr (ip, &imm_expr);
8451               if ((unsigned long) imm_expr.X_add_number < 32
8452                   || (unsigned long) imm_expr.X_add_number > 63)
8453                 break;
8454               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8455               imm_expr.X_op = O_absent;
8456               s = expr_end;
8457               continue;
8458
8459             case 'k':           /* cache code */
8460             case 'h':           /* prefx code */
8461               my_getExpression (&imm_expr, s);
8462               check_absolute_expr (ip, &imm_expr);
8463               if ((unsigned long) imm_expr.X_add_number > 31)
8464                 {
8465                   as_warn (_("Invalid value for `%s' (%lu)"),
8466                            ip->insn_mo->name,
8467                            (unsigned long) imm_expr.X_add_number);
8468                   imm_expr.X_add_number &= 0x1f;
8469                 }
8470               if (*args == 'k')
8471                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8472               else
8473                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8474               imm_expr.X_op = O_absent;
8475               s = expr_end;
8476               continue;
8477
8478             case 'c':           /* break code */
8479               my_getExpression (&imm_expr, s);
8480               check_absolute_expr (ip, &imm_expr);
8481               if ((unsigned long) imm_expr.X_add_number > 1023)
8482                 {
8483                   as_warn (_("Illegal break code (%lu)"),
8484                            (unsigned long) imm_expr.X_add_number);
8485                   imm_expr.X_add_number &= OP_MASK_CODE;
8486                 }
8487               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8488               imm_expr.X_op = O_absent;
8489               s = expr_end;
8490               continue;
8491
8492             case 'q':           /* lower break code */
8493               my_getExpression (&imm_expr, s);
8494               check_absolute_expr (ip, &imm_expr);
8495               if ((unsigned long) imm_expr.X_add_number > 1023)
8496                 {
8497                   as_warn (_("Illegal lower break code (%lu)"),
8498                            (unsigned long) imm_expr.X_add_number);
8499                   imm_expr.X_add_number &= OP_MASK_CODE2;
8500                 }
8501               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8502               imm_expr.X_op = O_absent;
8503               s = expr_end;
8504               continue;
8505
8506             case 'B':           /* 20-bit syscall/break code.  */
8507               my_getExpression (&imm_expr, s);
8508               check_absolute_expr (ip, &imm_expr);
8509               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8510                 as_warn (_("Illegal 20-bit code (%lu)"),
8511                          (unsigned long) imm_expr.X_add_number);
8512               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8513               imm_expr.X_op = O_absent;
8514               s = expr_end;
8515               continue;
8516
8517             case 'C':           /* Coprocessor code */
8518               my_getExpression (&imm_expr, s);
8519               check_absolute_expr (ip, &imm_expr);
8520               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8521                 {
8522                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8523                            (unsigned long) imm_expr.X_add_number);
8524                   imm_expr.X_add_number &= ((1 << 25) - 1);
8525                 }
8526               ip->insn_opcode |= imm_expr.X_add_number;
8527               imm_expr.X_op = O_absent;
8528               s = expr_end;
8529               continue;
8530
8531             case 'J':           /* 19-bit wait code.  */
8532               my_getExpression (&imm_expr, s);
8533               check_absolute_expr (ip, &imm_expr);
8534               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8535                 as_warn (_("Illegal 19-bit code (%lu)"),
8536                          (unsigned long) imm_expr.X_add_number);
8537               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8538               imm_expr.X_op = O_absent;
8539               s = expr_end;
8540               continue;
8541
8542             case 'P':           /* Performance register */
8543               my_getExpression (&imm_expr, s);
8544               check_absolute_expr (ip, &imm_expr);
8545               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8546                 {
8547                   as_warn (_("Invalid performance register (%lu)"),
8548                            (unsigned long) imm_expr.X_add_number);
8549                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8550                 }
8551               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8552               imm_expr.X_op = O_absent;
8553               s = expr_end;
8554               continue;
8555
8556             case 'b':           /* base register */
8557             case 'd':           /* destination register */
8558             case 's':           /* source register */
8559             case 't':           /* target register */
8560             case 'r':           /* both target and source */
8561             case 'v':           /* both dest and source */
8562             case 'w':           /* both dest and target */
8563             case 'E':           /* coprocessor target register */
8564             case 'G':           /* coprocessor destination register */
8565             case 'K':           /* 'rdhwr' destination register */
8566             case 'x':           /* ignore register name */
8567             case 'z':           /* must be zero register */
8568             case 'U':           /* destination register (clo/clz).  */
8569               s_reset = s;
8570               if (s[0] == '$')
8571                 {
8572
8573                   if (ISDIGIT (s[1]))
8574                     {
8575                       ++s;
8576                       regno = 0;
8577                       do
8578                         {
8579                           regno *= 10;
8580                           regno += *s - '0';
8581                           ++s;
8582                         }
8583                       while (ISDIGIT (*s));
8584                       if (regno > 31)
8585                         as_bad (_("Invalid register number (%d)"), regno);
8586                     }
8587                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8588                     goto notreg;
8589                   else
8590                     {
8591                       if (s[1] == 'r' && s[2] == 'a')
8592                         {
8593                           s += 3;
8594                           regno = RA;
8595                         }
8596                       else if (s[1] == 'f' && s[2] == 'p')
8597                         {
8598                           s += 3;
8599                           regno = FP;
8600                         }
8601                       else if (s[1] == 's' && s[2] == 'p')
8602                         {
8603                           s += 3;
8604                           regno = SP;
8605                         }
8606                       else if (s[1] == 'g' && s[2] == 'p')
8607                         {
8608                           s += 3;
8609                           regno = GP;
8610                         }
8611                       else if (s[1] == 'a' && s[2] == 't')
8612                         {
8613                           s += 3;
8614                           regno = AT;
8615                         }
8616                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8617                         {
8618                           s += 4;
8619                           regno = KT0;
8620                         }
8621                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8622                         {
8623                           s += 4;
8624                           regno = KT1;
8625                         }
8626                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8627                         {
8628                           s += 5;
8629                           regno = ZERO;
8630                         }
8631                       else if (itbl_have_entries)
8632                         {
8633                           char *p, *n;
8634                           unsigned long r;
8635
8636                           p = s + 1;    /* advance past '$' */
8637                           n = itbl_get_field (&p);  /* n is name */
8638
8639                           /* See if this is a register defined in an
8640                              itbl entry.  */
8641                           if (itbl_get_reg_val (n, &r))
8642                             {
8643                               /* Get_field advances to the start of
8644                                  the next field, so we need to back
8645                                  rack to the end of the last field.  */
8646                               if (p)
8647                                 s = p - 1;
8648                               else
8649                                 s = strchr (s, '\0');
8650                               regno = r;
8651                             }
8652                           else
8653                             goto notreg;
8654                         }
8655                       else
8656                         goto notreg;
8657                     }
8658                   if (regno == AT
8659                       && ! mips_opts.noat
8660                       && *args != 'E'
8661                       && *args != 'G'
8662                       && *args != 'K')
8663                     as_warn (_("Used $at without \".set noat\""));
8664                   c = *args;
8665                   if (*s == ' ')
8666                     ++s;
8667                   if (args[1] != *s)
8668                     {
8669                       if (c == 'r' || c == 'v' || c == 'w')
8670                         {
8671                           regno = lastregno;
8672                           s = s_reset;
8673                           ++args;
8674                         }
8675                     }
8676                   /* 'z' only matches $0.  */
8677                   if (c == 'z' && regno != 0)
8678                     break;
8679
8680         /* Now that we have assembled one operand, we use the args string
8681          * to figure out where it goes in the instruction.  */
8682                   switch (c)
8683                     {
8684                     case 'r':
8685                     case 's':
8686                     case 'v':
8687                     case 'b':
8688                       ip->insn_opcode |= regno << OP_SH_RS;
8689                       break;
8690                     case 'd':
8691                     case 'G':
8692                     case 'K':
8693                       ip->insn_opcode |= regno << OP_SH_RD;
8694                       break;
8695                     case 'U':
8696                       ip->insn_opcode |= regno << OP_SH_RD;
8697                       ip->insn_opcode |= regno << OP_SH_RT;
8698                       break;
8699                     case 'w':
8700                     case 't':
8701                     case 'E':
8702                       ip->insn_opcode |= regno << OP_SH_RT;
8703                       break;
8704                     case 'x':
8705                       /* This case exists because on the r3000 trunc
8706                          expands into a macro which requires a gp
8707                          register.  On the r6000 or r4000 it is
8708                          assembled into a single instruction which
8709                          ignores the register.  Thus the insn version
8710                          is MIPS_ISA2 and uses 'x', and the macro
8711                          version is MIPS_ISA1 and uses 't'.  */
8712                       break;
8713                     case 'z':
8714                       /* This case is for the div instruction, which
8715                          acts differently if the destination argument
8716                          is $0.  This only matches $0, and is checked
8717                          outside the switch.  */
8718                       break;
8719                     case 'D':
8720                       /* Itbl operand; not yet implemented. FIXME ?? */
8721                       break;
8722                       /* What about all other operands like 'i', which
8723                          can be specified in the opcode table? */
8724                     }
8725                   lastregno = regno;
8726                   continue;
8727                 }
8728             notreg:
8729               switch (*args++)
8730                 {
8731                 case 'r':
8732                 case 'v':
8733                   ip->insn_opcode |= lastregno << OP_SH_RS;
8734                   continue;
8735                 case 'w':
8736                   ip->insn_opcode |= lastregno << OP_SH_RT;
8737                   continue;
8738                 }
8739               break;
8740
8741             case 'O':           /* MDMX alignment immediate constant.  */
8742               my_getExpression (&imm_expr, s);
8743               check_absolute_expr (ip, &imm_expr);
8744               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8745                 {
8746                   as_warn ("Improper align amount (%ld), using low bits",
8747                            (long) imm_expr.X_add_number);
8748                   imm_expr.X_add_number &= OP_MASK_ALN;
8749                 }
8750               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8751               imm_expr.X_op = O_absent;
8752               s = expr_end;
8753               continue;
8754
8755             case 'Q':           /* MDMX vector, element sel, or const.  */
8756               if (s[0] != '$')
8757                 {
8758                   /* MDMX Immediate.  */
8759                   my_getExpression (&imm_expr, s);
8760                   check_absolute_expr (ip, &imm_expr);
8761                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8762                     {
8763                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8764                                (long) imm_expr.X_add_number);
8765                       imm_expr.X_add_number &= OP_MASK_FT;
8766                     }
8767                   imm_expr.X_add_number &= OP_MASK_FT;
8768                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8769                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8770                   else
8771                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8772                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8773                   imm_expr.X_op = O_absent;
8774                   s = expr_end;
8775                   continue;
8776                 }
8777               /* Not MDMX Immediate.  Fall through.  */
8778             case 'X':           /* MDMX destination register.  */
8779             case 'Y':           /* MDMX source register.  */
8780             case 'Z':           /* MDMX target register.  */
8781               is_mdmx = 1;
8782             case 'D':           /* floating point destination register */
8783             case 'S':           /* floating point source register */
8784             case 'T':           /* floating point target register */
8785             case 'R':           /* floating point source register */
8786             case 'V':
8787             case 'W':
8788               s_reset = s;
8789               /* Accept $fN for FP and MDMX register numbers, and in
8790                  addition accept $vN for MDMX register numbers.  */
8791               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8792                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8793                       && ISDIGIT (s[2])))
8794                 {
8795                   s += 2;
8796                   regno = 0;
8797                   do
8798                     {
8799                       regno *= 10;
8800                       regno += *s - '0';
8801                       ++s;
8802                     }
8803                   while (ISDIGIT (*s));
8804
8805                   if (regno > 31)
8806                     as_bad (_("Invalid float register number (%d)"), regno);
8807
8808                   if ((regno & 1) != 0
8809                       && HAVE_32BIT_FPRS
8810                       && ! (strcmp (str, "mtc1") == 0
8811                             || strcmp (str, "mfc1") == 0
8812                             || strcmp (str, "lwc1") == 0
8813                             || strcmp (str, "swc1") == 0
8814                             || strcmp (str, "l.s") == 0
8815                             || strcmp (str, "s.s") == 0))
8816                     as_warn (_("Float register should be even, was %d"),
8817                              regno);
8818
8819                   c = *args;
8820                   if (*s == ' ')
8821                     ++s;
8822                   if (args[1] != *s)
8823                     {
8824                       if (c == 'V' || c == 'W')
8825                         {
8826                           regno = lastregno;
8827                           s = s_reset;
8828                           ++args;
8829                         }
8830                     }
8831                   switch (c)
8832                     {
8833                     case 'D':
8834                     case 'X':
8835                       ip->insn_opcode |= regno << OP_SH_FD;
8836                       break;
8837                     case 'V':
8838                     case 'S':
8839                     case 'Y':
8840                       ip->insn_opcode |= regno << OP_SH_FS;
8841                       break;
8842                     case 'Q':
8843                       /* This is like 'Z', but also needs to fix the MDMX
8844                          vector/scalar select bits.  Note that the
8845                          scalar immediate case is handled above.  */
8846                       if (*s == '[')
8847                         {
8848                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8849                           int max_el = (is_qh ? 3 : 7);
8850                           s++;
8851                           my_getExpression(&imm_expr, s);
8852                           check_absolute_expr (ip, &imm_expr);
8853                           s = expr_end;
8854                           if (imm_expr.X_add_number > max_el)
8855                             as_bad(_("Bad element selector %ld"),
8856                                    (long) imm_expr.X_add_number);
8857                           imm_expr.X_add_number &= max_el;
8858                           ip->insn_opcode |= (imm_expr.X_add_number
8859                                               << (OP_SH_VSEL +
8860                                                   (is_qh ? 2 : 1)));
8861                           if (*s != ']')
8862                             as_warn(_("Expecting ']' found '%s'"), s);
8863                           else
8864                             s++;
8865                         }
8866                       else
8867                         {
8868                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8869                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8870                                                 << OP_SH_VSEL);
8871                           else
8872                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8873                                                 OP_SH_VSEL);
8874                         }
8875                       /* Fall through */
8876                     case 'W':
8877                     case 'T':
8878                     case 'Z':
8879                       ip->insn_opcode |= regno << OP_SH_FT;
8880                       break;
8881                     case 'R':
8882                       ip->insn_opcode |= regno << OP_SH_FR;
8883                       break;
8884                     }
8885                   lastregno = regno;
8886                   continue;
8887                 }
8888
8889               switch (*args++)
8890                 {
8891                 case 'V':
8892                   ip->insn_opcode |= lastregno << OP_SH_FS;
8893                   continue;
8894                 case 'W':
8895                   ip->insn_opcode |= lastregno << OP_SH_FT;
8896                   continue;
8897                 }
8898               break;
8899
8900             case 'I':
8901               my_getExpression (&imm_expr, s);
8902               if (imm_expr.X_op != O_big
8903                   && imm_expr.X_op != O_constant)
8904                 insn_error = _("absolute expression required");
8905               s = expr_end;
8906               continue;
8907
8908             case 'A':
8909               my_getExpression (&offset_expr, s);
8910               *imm_reloc = BFD_RELOC_32;
8911               s = expr_end;
8912               continue;
8913
8914             case 'F':
8915             case 'L':
8916             case 'f':
8917             case 'l':
8918               {
8919                 int f64;
8920                 int using_gprs;
8921                 char *save_in;
8922                 char *err;
8923                 unsigned char temp[8];
8924                 int len;
8925                 unsigned int length;
8926                 segT seg;
8927                 subsegT subseg;
8928                 char *p;
8929
8930                 /* These only appear as the last operand in an
8931                    instruction, and every instruction that accepts
8932                    them in any variant accepts them in all variants.
8933                    This means we don't have to worry about backing out
8934                    any changes if the instruction does not match.
8935
8936                    The difference between them is the size of the
8937                    floating point constant and where it goes.  For 'F'
8938                    and 'L' the constant is 64 bits; for 'f' and 'l' it
8939                    is 32 bits.  Where the constant is placed is based
8940                    on how the MIPS assembler does things:
8941                     F -- .rdata
8942                     L -- .lit8
8943                     f -- immediate value
8944                     l -- .lit4
8945
8946                     The .lit4 and .lit8 sections are only used if
8947                     permitted by the -G argument.
8948
8949                     When generating embedded PIC code, we use the
8950                     .lit8 section but not the .lit4 section (we can do
8951                     .lit4 inline easily; we need to put .lit8
8952                     somewhere in the data segment, and using .lit8
8953                     permits the linker to eventually combine identical
8954                     .lit8 entries).
8955
8956                     The code below needs to know whether the target register
8957                     is 32 or 64 bits wide.  It relies on the fact 'f' and
8958                     'F' are used with GPR-based instructions and 'l' and
8959                     'L' are used with FPR-based instructions.  */
8960
8961                 f64 = *args == 'F' || *args == 'L';
8962                 using_gprs = *args == 'F' || *args == 'f';
8963
8964                 save_in = input_line_pointer;
8965                 input_line_pointer = s;
8966                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8967                 length = len;
8968                 s = input_line_pointer;
8969                 input_line_pointer = save_in;
8970                 if (err != NULL && *err != '\0')
8971                   {
8972                     as_bad (_("Bad floating point constant: %s"), err);
8973                     memset (temp, '\0', sizeof temp);
8974                     length = f64 ? 8 : 4;
8975                   }
8976
8977                 assert (length == (unsigned) (f64 ? 8 : 4));
8978
8979                 if (*args == 'f'
8980                     || (*args == 'l'
8981                         && (! USE_GLOBAL_POINTER_OPT
8982                             || mips_pic == EMBEDDED_PIC
8983                             || g_switch_value < 4
8984                             || (temp[0] == 0 && temp[1] == 0)
8985                             || (temp[2] == 0 && temp[3] == 0))))
8986                   {
8987                     imm_expr.X_op = O_constant;
8988                     if (! target_big_endian)
8989                       imm_expr.X_add_number = bfd_getl32 (temp);
8990                     else
8991                       imm_expr.X_add_number = bfd_getb32 (temp);
8992                   }
8993                 else if (length > 4
8994                          && ! mips_disable_float_construction
8995                          /* Constants can only be constructed in GPRs and
8996                             copied to FPRs if the GPRs are at least as wide
8997                             as the FPRs.  Force the constant into memory if
8998                             we are using 64-bit FPRs but the GPRs are only
8999                             32 bits wide.  */
9000                          && (using_gprs
9001                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9002                          && ((temp[0] == 0 && temp[1] == 0)
9003                              || (temp[2] == 0 && temp[3] == 0))
9004                          && ((temp[4] == 0 && temp[5] == 0)
9005                              || (temp[6] == 0 && temp[7] == 0)))
9006                   {
9007                     /* The value is simple enough to load with a couple of
9008                        instructions.  If using 32-bit registers, set
9009                        imm_expr to the high order 32 bits and offset_expr to
9010                        the low order 32 bits.  Otherwise, set imm_expr to
9011                        the entire 64 bit constant.  */
9012                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9013                       {
9014                         imm_expr.X_op = O_constant;
9015                         offset_expr.X_op = O_constant;
9016                         if (! target_big_endian)
9017                           {
9018                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9019                             offset_expr.X_add_number = bfd_getl32 (temp);
9020                           }
9021                         else
9022                           {
9023                             imm_expr.X_add_number = bfd_getb32 (temp);
9024                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9025                           }
9026                         if (offset_expr.X_add_number == 0)
9027                           offset_expr.X_op = O_absent;
9028                       }
9029                     else if (sizeof (imm_expr.X_add_number) > 4)
9030                       {
9031                         imm_expr.X_op = O_constant;
9032                         if (! target_big_endian)
9033                           imm_expr.X_add_number = bfd_getl64 (temp);
9034                         else
9035                           imm_expr.X_add_number = bfd_getb64 (temp);
9036                       }
9037                     else
9038                       {
9039                         imm_expr.X_op = O_big;
9040                         imm_expr.X_add_number = 4;
9041                         if (! target_big_endian)
9042                           {
9043                             generic_bignum[0] = bfd_getl16 (temp);
9044                             generic_bignum[1] = bfd_getl16 (temp + 2);
9045                             generic_bignum[2] = bfd_getl16 (temp + 4);
9046                             generic_bignum[3] = bfd_getl16 (temp + 6);
9047                           }
9048                         else
9049                           {
9050                             generic_bignum[0] = bfd_getb16 (temp + 6);
9051                             generic_bignum[1] = bfd_getb16 (temp + 4);
9052                             generic_bignum[2] = bfd_getb16 (temp + 2);
9053                             generic_bignum[3] = bfd_getb16 (temp);
9054                           }
9055                       }
9056                   }
9057                 else
9058                   {
9059                     const char *newname;
9060                     segT new_seg;
9061
9062                     /* Switch to the right section.  */
9063                     seg = now_seg;
9064                     subseg = now_subseg;
9065                     switch (*args)
9066                       {
9067                       default: /* unused default case avoids warnings.  */
9068                       case 'L':
9069                         newname = RDATA_SECTION_NAME;
9070                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9071                             || mips_pic == EMBEDDED_PIC)
9072                           newname = ".lit8";
9073                         break;
9074                       case 'F':
9075                         if (mips_pic == EMBEDDED_PIC)
9076                           newname = ".lit8";
9077                         else
9078                           newname = RDATA_SECTION_NAME;
9079                         break;
9080                       case 'l':
9081                         assert (!USE_GLOBAL_POINTER_OPT
9082                                 || g_switch_value >= 4);
9083                         newname = ".lit4";
9084                         break;
9085                       }
9086                     new_seg = subseg_new (newname, (subsegT) 0);
9087                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9088                       bfd_set_section_flags (stdoutput, new_seg,
9089                                              (SEC_ALLOC
9090                                               | SEC_LOAD
9091                                               | SEC_READONLY
9092                                               | SEC_DATA));
9093                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9094                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9095                         && strcmp (TARGET_OS, "elf") != 0)
9096                       record_alignment (new_seg, 4);
9097                     else
9098                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9099                     if (seg == now_seg)
9100                       as_bad (_("Can't use floating point insn in this section"));
9101
9102                     /* Set the argument to the current address in the
9103                        section.  */
9104                     offset_expr.X_op = O_symbol;
9105                     offset_expr.X_add_symbol =
9106                       symbol_new ("L0\001", now_seg,
9107                                   (valueT) frag_now_fix (), frag_now);
9108                     offset_expr.X_add_number = 0;
9109
9110                     /* Put the floating point number into the section.  */
9111                     p = frag_more ((int) length);
9112                     memcpy (p, temp, length);
9113
9114                     /* Switch back to the original section.  */
9115                     subseg_set (seg, subseg);
9116                   }
9117               }
9118               continue;
9119
9120             case 'i':           /* 16 bit unsigned immediate */
9121             case 'j':           /* 16 bit signed immediate */
9122               *imm_reloc = BFD_RELOC_LO16;
9123               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9124                 {
9125                   int more;
9126                   offsetT minval, maxval;
9127
9128                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9129                           && strcmp (insn->name, insn[1].name) == 0);
9130
9131                   /* If the expression was written as an unsigned number,
9132                      only treat it as signed if there are no more
9133                      alternatives.  */
9134                   if (more
9135                       && *args == 'j'
9136                       && sizeof (imm_expr.X_add_number) <= 4
9137                       && imm_expr.X_op == O_constant
9138                       && imm_expr.X_add_number < 0
9139                       && imm_expr.X_unsigned
9140                       && HAVE_64BIT_GPRS)
9141                     break;
9142
9143                   /* For compatibility with older assemblers, we accept
9144                      0x8000-0xffff as signed 16-bit numbers when only
9145                      signed numbers are allowed.  */
9146                   if (*args == 'i')
9147                     minval = 0, maxval = 0xffff;
9148                   else if (more)
9149                     minval = -0x8000, maxval = 0x7fff;
9150                   else
9151                     minval = -0x8000, maxval = 0xffff;
9152
9153                   if (imm_expr.X_op != O_constant
9154                       || imm_expr.X_add_number < minval
9155                       || imm_expr.X_add_number > maxval)
9156                     {
9157                       if (more)
9158                         break;
9159                       if (imm_expr.X_op == O_constant
9160                           || imm_expr.X_op == O_big)
9161                         as_bad (_("expression out of range"));
9162                     }
9163                 }
9164               s = expr_end;
9165               continue;
9166
9167             case 'o':           /* 16 bit offset */
9168               /* Check whether there is only a single bracketed expression
9169                  left.  If so, it must be the base register and the
9170                  constant must be zero.  */
9171               if (*s == '(' && strchr (s + 1, '(') == 0)
9172                 {
9173                   offset_expr.X_op = O_constant;
9174                   offset_expr.X_add_number = 0;
9175                   continue;
9176                 }
9177
9178               /* If this value won't fit into a 16 bit offset, then go
9179                  find a macro that will generate the 32 bit offset
9180                  code pattern.  */
9181               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9182                   && (offset_expr.X_op != O_constant
9183                       || offset_expr.X_add_number >= 0x8000
9184                       || offset_expr.X_add_number < -0x8000))
9185                 break;
9186
9187               s = expr_end;
9188               continue;
9189
9190             case 'p':           /* pc relative offset */
9191               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9192               my_getExpression (&offset_expr, s);
9193               s = expr_end;
9194               continue;
9195
9196             case 'u':           /* upper 16 bits */
9197               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9198                   && imm_expr.X_op == O_constant
9199                   && (imm_expr.X_add_number < 0
9200                       || imm_expr.X_add_number >= 0x10000))
9201                 as_bad (_("lui expression not in range 0..65535"));
9202               s = expr_end;
9203               continue;
9204
9205             case 'a':           /* 26 bit address */
9206               my_getExpression (&offset_expr, s);
9207               s = expr_end;
9208               *offset_reloc = BFD_RELOC_MIPS_JMP;
9209               continue;
9210
9211             case 'N':           /* 3 bit branch condition code */
9212             case 'M':           /* 3 bit compare condition code */
9213               if (strncmp (s, "$fcc", 4) != 0)
9214                 break;
9215               s += 4;
9216               regno = 0;
9217               do
9218                 {
9219                   regno *= 10;
9220                   regno += *s - '0';
9221                   ++s;
9222                 }
9223               while (ISDIGIT (*s));
9224               if (regno > 7)
9225                 as_bad (_("invalid condition code register $fcc%d"), regno);
9226               if (*args == 'N')
9227                 ip->insn_opcode |= regno << OP_SH_BCC;
9228               else
9229                 ip->insn_opcode |= regno << OP_SH_CCC;
9230               continue;
9231
9232             case 'H':
9233               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9234                 s += 2;
9235               if (ISDIGIT (*s))
9236                 {
9237                   c = 0;
9238                   do
9239                     {
9240                       c *= 10;
9241                       c += *s - '0';
9242                       ++s;
9243                     }
9244                   while (ISDIGIT (*s));
9245                 }
9246               else
9247                 c = 8; /* Invalid sel value.  */
9248
9249               if (c > 7)
9250                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9251               ip->insn_opcode |= c;
9252               continue;
9253
9254             case 'e':
9255               /* Must be at least one digit.  */
9256               my_getExpression (&imm_expr, s);
9257               check_absolute_expr (ip, &imm_expr);
9258
9259               if ((unsigned long) imm_expr.X_add_number
9260                   > (unsigned long) OP_MASK_VECBYTE)
9261                 {
9262                   as_bad (_("bad byte vector index (%ld)"),
9263                            (long) imm_expr.X_add_number);
9264                   imm_expr.X_add_number = 0;
9265                 }
9266
9267               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9268               imm_expr.X_op = O_absent;
9269               s = expr_end;
9270               continue;
9271
9272             case '%':
9273               my_getExpression (&imm_expr, s);
9274               check_absolute_expr (ip, &imm_expr);
9275
9276               if ((unsigned long) imm_expr.X_add_number
9277                   > (unsigned long) OP_MASK_VECALIGN)
9278                 {
9279                   as_bad (_("bad byte vector index (%ld)"),
9280                            (long) imm_expr.X_add_number);
9281                   imm_expr.X_add_number = 0;
9282                 }
9283
9284               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9285               imm_expr.X_op = O_absent;
9286               s = expr_end;
9287               continue;
9288
9289             default:
9290               as_bad (_("bad char = '%c'\n"), *args);
9291               internalError ();
9292             }
9293           break;
9294         }
9295       /* Args don't match.  */
9296       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9297           !strcmp (insn->name, insn[1].name))
9298         {
9299           ++insn;
9300           s = argsStart;
9301           insn_error = _("illegal operands");
9302           continue;
9303         }
9304       if (save_c)
9305         *(--s) = save_c;
9306       insn_error = _("illegal operands");
9307       return;
9308     }
9309 }
9310
9311 /* This routine assembles an instruction into its binary format when
9312    assembling for the mips16.  As a side effect, it sets one of the
9313    global variables imm_reloc or offset_reloc to the type of
9314    relocation to do if one of the operands is an address expression.
9315    It also sets mips16_small and mips16_ext if the user explicitly
9316    requested a small or extended instruction.  */
9317
9318 static void
9319 mips16_ip (str, ip)
9320      char *str;
9321      struct mips_cl_insn *ip;
9322 {
9323   char *s;
9324   const char *args;
9325   struct mips_opcode *insn;
9326   char *argsstart;
9327   unsigned int regno;
9328   unsigned int lastregno = 0;
9329   char *s_reset;
9330
9331   insn_error = NULL;
9332
9333   mips16_small = FALSE;
9334   mips16_ext = FALSE;
9335
9336   for (s = str; ISLOWER (*s); ++s)
9337     ;
9338   switch (*s)
9339     {
9340     case '\0':
9341       break;
9342
9343     case ' ':
9344       *s++ = '\0';
9345       break;
9346
9347     case '.':
9348       if (s[1] == 't' && s[2] == ' ')
9349         {
9350           *s = '\0';
9351           mips16_small = TRUE;
9352           s += 3;
9353           break;
9354         }
9355       else if (s[1] == 'e' && s[2] == ' ')
9356         {
9357           *s = '\0';
9358           mips16_ext = TRUE;
9359           s += 3;
9360           break;
9361         }
9362       /* Fall through.  */
9363     default:
9364       insn_error = _("unknown opcode");
9365       return;
9366     }
9367
9368   if (mips_opts.noautoextend && ! mips16_ext)
9369     mips16_small = TRUE;
9370
9371   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9372     {
9373       insn_error = _("unrecognized opcode");
9374       return;
9375     }
9376
9377   argsstart = s;
9378   for (;;)
9379     {
9380       assert (strcmp (insn->name, str) == 0);
9381
9382       ip->insn_mo = insn;
9383       ip->insn_opcode = insn->match;
9384       ip->use_extend = FALSE;
9385       imm_expr.X_op = O_absent;
9386       imm_reloc[0] = BFD_RELOC_UNUSED;
9387       imm_reloc[1] = BFD_RELOC_UNUSED;
9388       imm_reloc[2] = BFD_RELOC_UNUSED;
9389       offset_expr.X_op = O_absent;
9390       offset_reloc[0] = BFD_RELOC_UNUSED;
9391       offset_reloc[1] = BFD_RELOC_UNUSED;
9392       offset_reloc[2] = BFD_RELOC_UNUSED;
9393       for (args = insn->args; 1; ++args)
9394         {
9395           int c;
9396
9397           if (*s == ' ')
9398             ++s;
9399
9400           /* In this switch statement we call break if we did not find
9401              a match, continue if we did find a match, or return if we
9402              are done.  */
9403
9404           c = *args;
9405           switch (c)
9406             {
9407             case '\0':
9408               if (*s == '\0')
9409                 {
9410                   /* Stuff the immediate value in now, if we can.  */
9411                   if (imm_expr.X_op == O_constant
9412                       && *imm_reloc > BFD_RELOC_UNUSED
9413                       && insn->pinfo != INSN_MACRO)
9414                     {
9415                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9416                                     imm_expr.X_add_number, TRUE, mips16_small,
9417                                     mips16_ext, &ip->insn_opcode,
9418                                     &ip->use_extend, &ip->extend);
9419                       imm_expr.X_op = O_absent;
9420                       *imm_reloc = BFD_RELOC_UNUSED;
9421                     }
9422
9423                   return;
9424                 }
9425               break;
9426
9427             case ',':
9428               if (*s++ == c)
9429                 continue;
9430               s--;
9431               switch (*++args)
9432                 {
9433                 case 'v':
9434                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9435                   continue;
9436                 case 'w':
9437                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9438                   continue;
9439                 }
9440               break;
9441
9442             case '(':
9443             case ')':
9444               if (*s++ == c)
9445                 continue;
9446               break;
9447
9448             case 'v':
9449             case 'w':
9450               if (s[0] != '$')
9451                 {
9452                   if (c == 'v')
9453                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9454                   else
9455                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9456                   ++args;
9457                   continue;
9458                 }
9459               /* Fall through.  */
9460             case 'x':
9461             case 'y':
9462             case 'z':
9463             case 'Z':
9464             case '0':
9465             case 'S':
9466             case 'R':
9467             case 'X':
9468             case 'Y':
9469               if (s[0] != '$')
9470                 break;
9471               s_reset = s;
9472               if (ISDIGIT (s[1]))
9473                 {
9474                   ++s;
9475                   regno = 0;
9476                   do
9477                     {
9478                       regno *= 10;
9479                       regno += *s - '0';
9480                       ++s;
9481                     }
9482                   while (ISDIGIT (*s));
9483                   if (regno > 31)
9484                     {
9485                       as_bad (_("invalid register number (%d)"), regno);
9486                       regno = 2;
9487                     }
9488                 }
9489               else
9490                 {
9491                   if (s[1] == 'r' && s[2] == 'a')
9492                     {
9493                       s += 3;
9494                       regno = RA;
9495                     }
9496                   else if (s[1] == 'f' && s[2] == 'p')
9497                     {
9498                       s += 3;
9499                       regno = FP;
9500                     }
9501                   else if (s[1] == 's' && s[2] == 'p')
9502                     {
9503                       s += 3;
9504                       regno = SP;
9505                     }
9506                   else if (s[1] == 'g' && s[2] == 'p')
9507                     {
9508                       s += 3;
9509                       regno = GP;
9510                     }
9511                   else if (s[1] == 'a' && s[2] == 't')
9512                     {
9513                       s += 3;
9514                       regno = AT;
9515                     }
9516                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9517                     {
9518                       s += 4;
9519                       regno = KT0;
9520                     }
9521                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9522                     {
9523                       s += 4;
9524                       regno = KT1;
9525                     }
9526                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9527                     {
9528                       s += 5;
9529                       regno = ZERO;
9530                     }
9531                   else
9532                     break;
9533                 }
9534
9535               if (*s == ' ')
9536                 ++s;
9537               if (args[1] != *s)
9538                 {
9539                   if (c == 'v' || c == 'w')
9540                     {
9541                       regno = mips16_to_32_reg_map[lastregno];
9542                       s = s_reset;
9543                       ++args;
9544                     }
9545                 }
9546
9547               switch (c)
9548                 {
9549                 case 'x':
9550                 case 'y':
9551                 case 'z':
9552                 case 'v':
9553                 case 'w':
9554                 case 'Z':
9555                   regno = mips32_to_16_reg_map[regno];
9556                   break;
9557
9558                 case '0':
9559                   if (regno != 0)
9560                     regno = ILLEGAL_REG;
9561                   break;
9562
9563                 case 'S':
9564                   if (regno != SP)
9565                     regno = ILLEGAL_REG;
9566                   break;
9567
9568                 case 'R':
9569                   if (regno != RA)
9570                     regno = ILLEGAL_REG;
9571                   break;
9572
9573                 case 'X':
9574                 case 'Y':
9575                   if (regno == AT && ! mips_opts.noat)
9576                     as_warn (_("used $at without \".set noat\""));
9577                   break;
9578
9579                 default:
9580                   internalError ();
9581                 }
9582
9583               if (regno == ILLEGAL_REG)
9584                 break;
9585
9586               switch (c)
9587                 {
9588                 case 'x':
9589                 case 'v':
9590                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9591                   break;
9592                 case 'y':
9593                 case 'w':
9594                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9595                   break;
9596                 case 'z':
9597                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9598                   break;
9599                 case 'Z':
9600                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9601                 case '0':
9602                 case 'S':
9603                 case 'R':
9604                   break;
9605                 case 'X':
9606                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9607                   break;
9608                 case 'Y':
9609                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9610                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9611                   break;
9612                 default:
9613                   internalError ();
9614                 }
9615
9616               lastregno = regno;
9617               continue;
9618
9619             case 'P':
9620               if (strncmp (s, "$pc", 3) == 0)
9621                 {
9622                   s += 3;
9623                   continue;
9624                 }
9625               break;
9626
9627             case '<':
9628             case '>':
9629             case '[':
9630             case ']':
9631             case '4':
9632             case '5':
9633             case 'H':
9634             case 'W':
9635             case 'D':
9636             case 'j':
9637             case '8':
9638             case 'V':
9639             case 'C':
9640             case 'U':
9641             case 'k':
9642             case 'K':
9643               if (s[0] == '%'
9644                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9645                 {
9646                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9647                      and generate the appropriate reloc.  If the text
9648                      inside %gprel is not a symbol name with an
9649                      optional offset, then we generate a normal reloc
9650                      and will probably fail later.  */
9651                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9652                   if (imm_expr.X_op == O_symbol)
9653                     {
9654                       mips16_ext = TRUE;
9655                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9656                       s = expr_end;
9657                       ip->use_extend = TRUE;
9658                       ip->extend = 0;
9659                       continue;
9660                     }
9661                 }
9662               else
9663                 {
9664                   /* Just pick up a normal expression.  */
9665                   my_getExpression (&imm_expr, s);
9666                 }
9667
9668               if (imm_expr.X_op == O_register)
9669                 {
9670                   /* What we thought was an expression turned out to
9671                      be a register.  */
9672
9673                   if (s[0] == '(' && args[1] == '(')
9674                     {
9675                       /* It looks like the expression was omitted
9676                          before a register indirection, which means
9677                          that the expression is implicitly zero.  We
9678                          still set up imm_expr, so that we handle
9679                          explicit extensions correctly.  */
9680                       imm_expr.X_op = O_constant;
9681                       imm_expr.X_add_number = 0;
9682                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9683                       continue;
9684                     }
9685
9686                   break;
9687                 }
9688
9689               /* We need to relax this instruction.  */
9690               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9691               s = expr_end;
9692               continue;
9693
9694             case 'p':
9695             case 'q':
9696             case 'A':
9697             case 'B':
9698             case 'E':
9699               /* We use offset_reloc rather than imm_reloc for the PC
9700                  relative operands.  This lets macros with both
9701                  immediate and address operands work correctly.  */
9702               my_getExpression (&offset_expr, s);
9703
9704               if (offset_expr.X_op == O_register)
9705                 break;
9706
9707               /* We need to relax this instruction.  */
9708               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9709               s = expr_end;
9710               continue;
9711
9712             case '6':           /* break code */
9713               my_getExpression (&imm_expr, s);
9714               check_absolute_expr (ip, &imm_expr);
9715               if ((unsigned long) imm_expr.X_add_number > 63)
9716                 {
9717                   as_warn (_("Invalid value for `%s' (%lu)"),
9718                            ip->insn_mo->name,
9719                            (unsigned long) imm_expr.X_add_number);
9720                   imm_expr.X_add_number &= 0x3f;
9721                 }
9722               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9723               imm_expr.X_op = O_absent;
9724               s = expr_end;
9725               continue;
9726
9727             case 'a':           /* 26 bit address */
9728               my_getExpression (&offset_expr, s);
9729               s = expr_end;
9730               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9731               ip->insn_opcode <<= 16;
9732               continue;
9733
9734             case 'l':           /* register list for entry macro */
9735             case 'L':           /* register list for exit macro */
9736               {
9737                 int mask;
9738
9739                 if (c == 'l')
9740                   mask = 0;
9741                 else
9742                   mask = 7 << 3;
9743                 while (*s != '\0')
9744                   {
9745                     int freg, reg1, reg2;
9746
9747                     while (*s == ' ' || *s == ',')
9748                       ++s;
9749                     if (*s != '$')
9750                       {
9751                         as_bad (_("can't parse register list"));
9752                         break;
9753                       }
9754                     ++s;
9755                     if (*s != 'f')
9756                       freg = 0;
9757                     else
9758                       {
9759                         freg = 1;
9760                         ++s;
9761                       }
9762                     reg1 = 0;
9763                     while (ISDIGIT (*s))
9764                       {
9765                         reg1 *= 10;
9766                         reg1 += *s - '0';
9767                         ++s;
9768                       }
9769                     if (*s == ' ')
9770                       ++s;
9771                     if (*s != '-')
9772                       reg2 = reg1;
9773                     else
9774                       {
9775                         ++s;
9776                         if (*s != '$')
9777                           break;
9778                         ++s;
9779                         if (freg)
9780                           {
9781                             if (*s == 'f')
9782                               ++s;
9783                             else
9784                               {
9785                                 as_bad (_("invalid register list"));
9786                                 break;
9787                               }
9788                           }
9789                         reg2 = 0;
9790                         while (ISDIGIT (*s))
9791                           {
9792                             reg2 *= 10;
9793                             reg2 += *s - '0';
9794                             ++s;
9795                           }
9796                       }
9797                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9798                       {
9799                         mask &= ~ (7 << 3);
9800                         mask |= 5 << 3;
9801                       }
9802                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9803                       {
9804                         mask &= ~ (7 << 3);
9805                         mask |= 6 << 3;
9806                       }
9807                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9808                       mask |= (reg2 - 3) << 3;
9809                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9810                       mask |= (reg2 - 15) << 1;
9811                     else if (reg1 == RA && reg2 == RA)
9812                       mask |= 1;
9813                     else
9814                       {
9815                         as_bad (_("invalid register list"));
9816                         break;
9817                       }
9818                   }
9819                 /* The mask is filled in in the opcode table for the
9820                    benefit of the disassembler.  We remove it before
9821                    applying the actual mask.  */
9822                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9823                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9824               }
9825             continue;
9826
9827             case 'e':           /* extend code */
9828               my_getExpression (&imm_expr, s);
9829               check_absolute_expr (ip, &imm_expr);
9830               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9831                 {
9832                   as_warn (_("Invalid value for `%s' (%lu)"),
9833                            ip->insn_mo->name,
9834                            (unsigned long) imm_expr.X_add_number);
9835                   imm_expr.X_add_number &= 0x7ff;
9836                 }
9837               ip->insn_opcode |= imm_expr.X_add_number;
9838               imm_expr.X_op = O_absent;
9839               s = expr_end;
9840               continue;
9841
9842             default:
9843               internalError ();
9844             }
9845           break;
9846         }
9847
9848       /* Args don't match.  */
9849       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9850           strcmp (insn->name, insn[1].name) == 0)
9851         {
9852           ++insn;
9853           s = argsstart;
9854           continue;
9855         }
9856
9857       insn_error = _("illegal operands");
9858
9859       return;
9860     }
9861 }
9862
9863 /* This structure holds information we know about a mips16 immediate
9864    argument type.  */
9865
9866 struct mips16_immed_operand
9867 {
9868   /* The type code used in the argument string in the opcode table.  */
9869   int type;
9870   /* The number of bits in the short form of the opcode.  */
9871   int nbits;
9872   /* The number of bits in the extended form of the opcode.  */
9873   int extbits;
9874   /* The amount by which the short form is shifted when it is used;
9875      for example, the sw instruction has a shift count of 2.  */
9876   int shift;
9877   /* The amount by which the short form is shifted when it is stored
9878      into the instruction code.  */
9879   int op_shift;
9880   /* Non-zero if the short form is unsigned.  */
9881   int unsp;
9882   /* Non-zero if the extended form is unsigned.  */
9883   int extu;
9884   /* Non-zero if the value is PC relative.  */
9885   int pcrel;
9886 };
9887
9888 /* The mips16 immediate operand types.  */
9889
9890 static const struct mips16_immed_operand mips16_immed_operands[] =
9891 {
9892   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9893   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9894   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9895   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9896   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9897   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9898   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9899   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9900   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9901   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9902   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9903   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9904   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9905   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9906   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9907   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9908   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9909   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9910   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9911   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9912   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9913 };
9914
9915 #define MIPS16_NUM_IMMED \
9916   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9917
9918 /* Handle a mips16 instruction with an immediate value.  This or's the
9919    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9920    whether an extended value is needed; if one is needed, it sets
9921    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9922    If SMALL is true, an unextended opcode was explicitly requested.
9923    If EXT is true, an extended opcode was explicitly requested.  If
9924    WARN is true, warn if EXT does not match reality.  */
9925
9926 static void
9927 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9928               extend)
9929      char *file;
9930      unsigned int line;
9931      int type;
9932      offsetT val;
9933      bfd_boolean warn;
9934      bfd_boolean small;
9935      bfd_boolean ext;
9936      unsigned long *insn;
9937      bfd_boolean *use_extend;
9938      unsigned short *extend;
9939 {
9940   register const struct mips16_immed_operand *op;
9941   int mintiny, maxtiny;
9942   bfd_boolean needext;
9943
9944   op = mips16_immed_operands;
9945   while (op->type != type)
9946     {
9947       ++op;
9948       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9949     }
9950
9951   if (op->unsp)
9952     {
9953       if (type == '<' || type == '>' || type == '[' || type == ']')
9954         {
9955           mintiny = 1;
9956           maxtiny = 1 << op->nbits;
9957         }
9958       else
9959         {
9960           mintiny = 0;
9961           maxtiny = (1 << op->nbits) - 1;
9962         }
9963     }
9964   else
9965     {
9966       mintiny = - (1 << (op->nbits - 1));
9967       maxtiny = (1 << (op->nbits - 1)) - 1;
9968     }
9969
9970   /* Branch offsets have an implicit 0 in the lowest bit.  */
9971   if (type == 'p' || type == 'q')
9972     val /= 2;
9973
9974   if ((val & ((1 << op->shift) - 1)) != 0
9975       || val < (mintiny << op->shift)
9976       || val > (maxtiny << op->shift))
9977     needext = TRUE;
9978   else
9979     needext = FALSE;
9980
9981   if (warn && ext && ! needext)
9982     as_warn_where (file, line,
9983                    _("extended operand requested but not required"));
9984   if (small && needext)
9985     as_bad_where (file, line, _("invalid unextended operand value"));
9986
9987   if (small || (! ext && ! needext))
9988     {
9989       int insnval;
9990
9991       *use_extend = FALSE;
9992       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9993       insnval <<= op->op_shift;
9994       *insn |= insnval;
9995     }
9996   else
9997     {
9998       long minext, maxext;
9999       int extval;
10000
10001       if (op->extu)
10002         {
10003           minext = 0;
10004           maxext = (1 << op->extbits) - 1;
10005         }
10006       else
10007         {
10008           minext = - (1 << (op->extbits - 1));
10009           maxext = (1 << (op->extbits - 1)) - 1;
10010         }
10011       if (val < minext || val > maxext)
10012         as_bad_where (file, line,
10013                       _("operand value out of range for instruction"));
10014
10015       *use_extend = TRUE;
10016       if (op->extbits == 16)
10017         {
10018           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10019           val &= 0x1f;
10020         }
10021       else if (op->extbits == 15)
10022         {
10023           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10024           val &= 0xf;
10025         }
10026       else
10027         {
10028           extval = ((val & 0x1f) << 6) | (val & 0x20);
10029           val = 0;
10030         }
10031
10032       *extend = (unsigned short) extval;
10033       *insn |= val;
10034     }
10035 }
10036 \f
10037 static const struct percent_op_match
10038 {
10039   const char *str;
10040   bfd_reloc_code_real_type reloc;
10041 } percent_op[] =
10042 {
10043   {"%lo", BFD_RELOC_LO16},
10044 #ifdef OBJ_ELF
10045   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10046   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10047   {"%call16", BFD_RELOC_MIPS_CALL16},
10048   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10049   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10050   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10051   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10052   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10053   {"%got", BFD_RELOC_MIPS_GOT16},
10054   {"%gp_rel", BFD_RELOC_GPREL16},
10055   {"%half", BFD_RELOC_16},
10056   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10057   {"%higher", BFD_RELOC_MIPS_HIGHER},
10058   {"%neg", BFD_RELOC_MIPS_SUB},
10059 #endif
10060   {"%hi", BFD_RELOC_HI16_S}
10061 };
10062
10063
10064 /* Return true if *STR points to a relocation operator.  When returning true,
10065    move *STR over the operator and store its relocation code in *RELOC.
10066    Leave both *STR and *RELOC alone when returning false.  */
10067
10068 static bfd_boolean
10069 parse_relocation (str, reloc)
10070      char **str;
10071      bfd_reloc_code_real_type *reloc;
10072 {
10073   size_t i;
10074
10075   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10076     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10077       {
10078         *str += strlen (percent_op[i].str);
10079         *reloc = percent_op[i].reloc;
10080
10081         /* Check whether the output BFD supports this relocation.
10082            If not, issue an error and fall back on something safe.  */
10083         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10084           {
10085             as_bad ("relocation %s isn't supported by the current ABI",
10086                     percent_op[i].str);
10087             *reloc = BFD_RELOC_LO16;
10088           }
10089         return TRUE;
10090       }
10091   return FALSE;
10092 }
10093
10094
10095 /* Parse string STR as a 16-bit relocatable operand.  Store the
10096    expression in *EP and the relocations in the array starting
10097    at RELOC.  Return the number of relocation operators used.
10098
10099    On exit, EXPR_END points to the first character after the expression.
10100    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10101
10102 static size_t
10103 my_getSmallExpression (ep, reloc, str)
10104      expressionS *ep;
10105      bfd_reloc_code_real_type *reloc;
10106      char *str;
10107 {
10108   bfd_reloc_code_real_type reversed_reloc[3];
10109   size_t reloc_index, i;
10110   int crux_depth, str_depth;
10111   char *crux;
10112
10113   /* Search for the start of the main expression, recoding relocations
10114      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10115      of the main expression and with CRUX_DEPTH containing the number
10116      of open brackets at that point.  */
10117   reloc_index = -1;
10118   str_depth = 0;
10119   do
10120     {
10121       reloc_index++;
10122       crux = str;
10123       crux_depth = str_depth;
10124
10125       /* Skip over whitespace and brackets, keeping count of the number
10126          of brackets.  */
10127       while (*str == ' ' || *str == '\t' || *str == '(')
10128         if (*str++ == '(')
10129           str_depth++;
10130     }
10131   while (*str == '%'
10132          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10133          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10134
10135   my_getExpression (ep, crux);
10136   str = expr_end;
10137
10138   /* Match every open bracket.  */
10139   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10140     if (*str++ == ')')
10141       crux_depth--;
10142
10143   if (crux_depth > 0)
10144     as_bad ("unclosed '('");
10145
10146   expr_end = str;
10147
10148   if (reloc_index == 0)
10149     reloc[0] = BFD_RELOC_LO16;
10150   else
10151     {
10152       prev_reloc_op_frag = frag_now;
10153       for (i = 0; i < reloc_index; i++)
10154         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10155     }
10156
10157   return reloc_index;
10158 }
10159
10160 static void
10161 my_getExpression (ep, str)
10162      expressionS *ep;
10163      char *str;
10164 {
10165   char *save_in;
10166   valueT val;
10167
10168   save_in = input_line_pointer;
10169   input_line_pointer = str;
10170   expression (ep);
10171   expr_end = input_line_pointer;
10172   input_line_pointer = save_in;
10173
10174   /* If we are in mips16 mode, and this is an expression based on `.',
10175      then we bump the value of the symbol by 1 since that is how other
10176      text symbols are handled.  We don't bother to handle complex
10177      expressions, just `.' plus or minus a constant.  */
10178   if (mips_opts.mips16
10179       && ep->X_op == O_symbol
10180       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10181       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10182       && symbol_get_frag (ep->X_add_symbol) == frag_now
10183       && symbol_constant_p (ep->X_add_symbol)
10184       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10185     S_SET_VALUE (ep->X_add_symbol, val + 1);
10186 }
10187
10188 /* Turn a string in input_line_pointer into a floating point constant
10189    of type TYPE, and store the appropriate bytes in *LITP.  The number
10190    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10191    returned, or NULL on OK.  */
10192
10193 char *
10194 md_atof (type, litP, sizeP)
10195      int type;
10196      char *litP;
10197      int *sizeP;
10198 {
10199   int prec;
10200   LITTLENUM_TYPE words[4];
10201   char *t;
10202   int i;
10203
10204   switch (type)
10205     {
10206     case 'f':
10207       prec = 2;
10208       break;
10209
10210     case 'd':
10211       prec = 4;
10212       break;
10213
10214     default:
10215       *sizeP = 0;
10216       return _("bad call to md_atof");
10217     }
10218
10219   t = atof_ieee (input_line_pointer, type, words);
10220   if (t)
10221     input_line_pointer = t;
10222
10223   *sizeP = prec * 2;
10224
10225   if (! target_big_endian)
10226     {
10227       for (i = prec - 1; i >= 0; i--)
10228         {
10229           md_number_to_chars (litP, (valueT) words[i], 2);
10230           litP += 2;
10231         }
10232     }
10233   else
10234     {
10235       for (i = 0; i < prec; i++)
10236         {
10237           md_number_to_chars (litP, (valueT) words[i], 2);
10238           litP += 2;
10239         }
10240     }
10241
10242   return NULL;
10243 }
10244
10245 void
10246 md_number_to_chars (buf, val, n)
10247      char *buf;
10248      valueT val;
10249      int n;
10250 {
10251   if (target_big_endian)
10252     number_to_chars_bigendian (buf, val, n);
10253   else
10254     number_to_chars_littleendian (buf, val, n);
10255 }
10256 \f
10257 #ifdef OBJ_ELF
10258 static int support_64bit_objects(void)
10259 {
10260   const char **list, **l;
10261   int yes;
10262
10263   list = bfd_target_list ();
10264   for (l = list; *l != NULL; l++)
10265 #ifdef TE_TMIPS
10266     /* This is traditional mips */
10267     if (strcmp (*l, "elf64-tradbigmips") == 0
10268         || strcmp (*l, "elf64-tradlittlemips") == 0)
10269 #else
10270     if (strcmp (*l, "elf64-bigmips") == 0
10271         || strcmp (*l, "elf64-littlemips") == 0)
10272 #endif
10273       break;
10274   yes = (*l != NULL);
10275   free (list);
10276   return yes;
10277 }
10278 #endif /* OBJ_ELF */
10279
10280 const char *md_shortopts = "nO::g::G:";
10281
10282 struct option md_longopts[] =
10283 {
10284 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10285   {"mips0", no_argument, NULL, OPTION_MIPS1},
10286   {"mips1", no_argument, NULL, OPTION_MIPS1},
10287 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10288   {"mips2", no_argument, NULL, OPTION_MIPS2},
10289 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10290   {"mips3", no_argument, NULL, OPTION_MIPS3},
10291 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10292   {"mips4", no_argument, NULL, OPTION_MIPS4},
10293 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10294   {"mips5", no_argument, NULL, OPTION_MIPS5},
10295 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10296   {"mips32", no_argument, NULL, OPTION_MIPS32},
10297 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10298   {"mips64", no_argument, NULL, OPTION_MIPS64},
10299 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10300   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10301 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10302   {"trap", no_argument, NULL, OPTION_TRAP},
10303   {"no-break", no_argument, NULL, OPTION_TRAP},
10304 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10305   {"break", no_argument, NULL, OPTION_BREAK},
10306   {"no-trap", no_argument, NULL, OPTION_BREAK},
10307 #define OPTION_EB (OPTION_MD_BASE + 11)
10308   {"EB", no_argument, NULL, OPTION_EB},
10309 #define OPTION_EL (OPTION_MD_BASE + 12)
10310   {"EL", no_argument, NULL, OPTION_EL},
10311 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10312   {"mips16", no_argument, NULL, OPTION_MIPS16},
10313 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10314   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10315 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10316   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10317 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10318   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10319   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10320 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10321   {"mfp32", no_argument, NULL, OPTION_FP32},
10322 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10323   {"mgp32", no_argument, NULL, OPTION_GP32},
10324 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10325   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10326 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10327   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10328 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10329   {"march", required_argument, NULL, OPTION_MARCH},
10330 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10331   {"mtune", required_argument, NULL, OPTION_MTUNE},
10332 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10333   {"mfp64", no_argument, NULL, OPTION_FP64},
10334 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10335   {"m4650", no_argument, NULL, OPTION_M4650},
10336 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10337   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10338 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10339   {"m4010", no_argument, NULL, OPTION_M4010},
10340 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10341   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10342 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10343   {"m4100", no_argument, NULL, OPTION_M4100},
10344 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10345   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10346 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10347   {"m3900", no_argument, NULL, OPTION_M3900},
10348 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10349   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10350 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10351   {"mgp64", no_argument, NULL, OPTION_GP64},
10352 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10353   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10354 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10355   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10356 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10357   {"mdmx", no_argument, NULL, OPTION_MDMX},
10358 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10359   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10360 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10361 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10362   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10363   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10364 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10365 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10366   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10367   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10368 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10369   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10370 #ifdef OBJ_ELF
10371 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 42)
10372 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10373   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10374   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10375 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10376   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10377 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10378   {"xgot",        no_argument, NULL, OPTION_XGOT},
10379 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10380   {"mabi", required_argument, NULL, OPTION_MABI},
10381 #define OPTION_32          (OPTION_ELF_BASE + 4)
10382   {"32",          no_argument, NULL, OPTION_32},
10383 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10384   {"n32",         no_argument, NULL, OPTION_N32},
10385 #define OPTION_64          (OPTION_ELF_BASE + 6)
10386   {"64",          no_argument, NULL, OPTION_64},
10387 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10388   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10389 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10390   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10391 #endif /* OBJ_ELF */
10392   {NULL, no_argument, NULL, 0}
10393 };
10394 size_t md_longopts_size = sizeof (md_longopts);
10395
10396 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10397    NEW_VALUE.  Warn if another value was already specified.  Note:
10398    we have to defer parsing the -march and -mtune arguments in order
10399    to handle 'from-abi' correctly, since the ABI might be specified
10400    in a later argument.  */
10401
10402 static void
10403 mips_set_option_string (string_ptr, new_value)
10404      const char **string_ptr, *new_value;
10405 {
10406   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10407     as_warn (_("A different %s was already specified, is now %s"),
10408              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10409              new_value);
10410
10411   *string_ptr = new_value;
10412 }
10413
10414 int
10415 md_parse_option (c, arg)
10416      int c;
10417      char *arg;
10418 {
10419   switch (c)
10420     {
10421     case OPTION_CONSTRUCT_FLOATS:
10422       mips_disable_float_construction = 0;
10423       break;
10424
10425     case OPTION_NO_CONSTRUCT_FLOATS:
10426       mips_disable_float_construction = 1;
10427       break;
10428
10429     case OPTION_TRAP:
10430       mips_trap = 1;
10431       break;
10432
10433     case OPTION_BREAK:
10434       mips_trap = 0;
10435       break;
10436
10437     case OPTION_EB:
10438       target_big_endian = 1;
10439       break;
10440
10441     case OPTION_EL:
10442       target_big_endian = 0;
10443       break;
10444
10445     case 'n':
10446       warn_nops = 1;
10447       break;
10448
10449     case 'O':
10450       if (arg && arg[1] == '0')
10451         mips_optimize = 1;
10452       else
10453         mips_optimize = 2;
10454       break;
10455
10456     case 'g':
10457       if (arg == NULL)
10458         mips_debug = 2;
10459       else
10460         mips_debug = atoi (arg);
10461       /* When the MIPS assembler sees -g or -g2, it does not do
10462          optimizations which limit full symbolic debugging.  We take
10463          that to be equivalent to -O0.  */
10464       if (mips_debug == 2)
10465         mips_optimize = 1;
10466       break;
10467
10468     case OPTION_MIPS1:
10469       file_mips_isa = ISA_MIPS1;
10470       break;
10471
10472     case OPTION_MIPS2:
10473       file_mips_isa = ISA_MIPS2;
10474       break;
10475
10476     case OPTION_MIPS3:
10477       file_mips_isa = ISA_MIPS3;
10478       break;
10479
10480     case OPTION_MIPS4:
10481       file_mips_isa = ISA_MIPS4;
10482       break;
10483
10484     case OPTION_MIPS5:
10485       file_mips_isa = ISA_MIPS5;
10486       break;
10487
10488     case OPTION_MIPS32:
10489       file_mips_isa = ISA_MIPS32;
10490       break;
10491
10492     case OPTION_MIPS32R2:
10493       file_mips_isa = ISA_MIPS32R2;
10494       break;
10495
10496     case OPTION_MIPS64:
10497       file_mips_isa = ISA_MIPS64;
10498       break;
10499
10500     case OPTION_MTUNE:
10501       mips_set_option_string (&mips_tune_string, arg);
10502       break;
10503
10504     case OPTION_MARCH:
10505       mips_set_option_string (&mips_arch_string, arg);
10506       break;
10507
10508     case OPTION_M4650:
10509       mips_set_option_string (&mips_arch_string, "4650");
10510       mips_set_option_string (&mips_tune_string, "4650");
10511       break;
10512
10513     case OPTION_NO_M4650:
10514       break;
10515
10516     case OPTION_M4010:
10517       mips_set_option_string (&mips_arch_string, "4010");
10518       mips_set_option_string (&mips_tune_string, "4010");
10519       break;
10520
10521     case OPTION_NO_M4010:
10522       break;
10523
10524     case OPTION_M4100:
10525       mips_set_option_string (&mips_arch_string, "4100");
10526       mips_set_option_string (&mips_tune_string, "4100");
10527       break;
10528
10529     case OPTION_NO_M4100:
10530       break;
10531
10532     case OPTION_M3900:
10533       mips_set_option_string (&mips_arch_string, "3900");
10534       mips_set_option_string (&mips_tune_string, "3900");
10535       break;
10536
10537     case OPTION_NO_M3900:
10538       break;
10539
10540     case OPTION_MDMX:
10541       mips_opts.ase_mdmx = 1;
10542       break;
10543
10544     case OPTION_NO_MDMX:
10545       mips_opts.ase_mdmx = 0;
10546       break;
10547
10548     case OPTION_MIPS16:
10549       mips_opts.mips16 = 1;
10550       mips_no_prev_insn (FALSE);
10551       break;
10552
10553     case OPTION_NO_MIPS16:
10554       mips_opts.mips16 = 0;
10555       mips_no_prev_insn (FALSE);
10556       break;
10557
10558     case OPTION_MIPS3D:
10559       mips_opts.ase_mips3d = 1;
10560       break;
10561
10562     case OPTION_NO_MIPS3D:
10563       mips_opts.ase_mips3d = 0;
10564       break;
10565
10566     case OPTION_MEMBEDDED_PIC:
10567       mips_pic = EMBEDDED_PIC;
10568       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10569         {
10570           as_bad (_("-G may not be used with embedded PIC code"));
10571           return 0;
10572         }
10573       g_switch_value = 0x7fffffff;
10574       break;
10575
10576     case OPTION_FIX_VR4122:
10577       mips_fix_4122_bugs = 1;
10578       break;
10579
10580     case OPTION_NO_FIX_VR4122:
10581       mips_fix_4122_bugs = 0;
10582       break;
10583
10584     case OPTION_RELAX_BRANCH:
10585       mips_relax_branch = 1;
10586       break;
10587
10588     case OPTION_NO_RELAX_BRANCH:
10589       mips_relax_branch = 0;
10590       break;
10591
10592 #ifdef OBJ_ELF
10593       /* When generating ELF code, we permit -KPIC and -call_shared to
10594          select SVR4_PIC, and -non_shared to select no PIC.  This is
10595          intended to be compatible with Irix 5.  */
10596     case OPTION_CALL_SHARED:
10597       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10598         {
10599           as_bad (_("-call_shared is supported only for ELF format"));
10600           return 0;
10601         }
10602       mips_pic = SVR4_PIC;
10603       if (g_switch_seen && g_switch_value != 0)
10604         {
10605           as_bad (_("-G may not be used with SVR4 PIC code"));
10606           return 0;
10607         }
10608       g_switch_value = 0;
10609       break;
10610
10611     case OPTION_NON_SHARED:
10612       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10613         {
10614           as_bad (_("-non_shared is supported only for ELF format"));
10615           return 0;
10616         }
10617       mips_pic = NO_PIC;
10618       break;
10619
10620       /* The -xgot option tells the assembler to use 32 offsets when
10621          accessing the got in SVR4_PIC mode.  It is for Irix
10622          compatibility.  */
10623     case OPTION_XGOT:
10624       mips_big_got = 1;
10625       break;
10626 #endif /* OBJ_ELF */
10627
10628     case 'G':
10629       if (! USE_GLOBAL_POINTER_OPT)
10630         {
10631           as_bad (_("-G is not supported for this configuration"));
10632           return 0;
10633         }
10634       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10635         {
10636           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10637           return 0;
10638         }
10639       else
10640         g_switch_value = atoi (arg);
10641       g_switch_seen = 1;
10642       break;
10643
10644 #ifdef OBJ_ELF
10645       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10646          and -mabi=64.  */
10647     case OPTION_32:
10648       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10649         {
10650           as_bad (_("-32 is supported for ELF format only"));
10651           return 0;
10652         }
10653       mips_abi = O32_ABI;
10654       break;
10655
10656     case OPTION_N32:
10657       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10658         {
10659           as_bad (_("-n32 is supported for ELF format only"));
10660           return 0;
10661         }
10662       mips_abi = N32_ABI;
10663       break;
10664
10665     case OPTION_64:
10666       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10667         {
10668           as_bad (_("-64 is supported for ELF format only"));
10669           return 0;
10670         }
10671       mips_abi = N64_ABI;
10672       if (! support_64bit_objects())
10673         as_fatal (_("No compiled in support for 64 bit object file format"));
10674       break;
10675 #endif /* OBJ_ELF */
10676
10677     case OPTION_GP32:
10678       file_mips_gp32 = 1;
10679       break;
10680
10681     case OPTION_GP64:
10682       file_mips_gp32 = 0;
10683       break;
10684
10685     case OPTION_FP32:
10686       file_mips_fp32 = 1;
10687       break;
10688
10689     case OPTION_FP64:
10690       file_mips_fp32 = 0;
10691       break;
10692
10693 #ifdef OBJ_ELF
10694     case OPTION_MABI:
10695       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10696         {
10697           as_bad (_("-mabi is supported for ELF format only"));
10698           return 0;
10699         }
10700       if (strcmp (arg, "32") == 0)
10701         mips_abi = O32_ABI;
10702       else if (strcmp (arg, "o64") == 0)
10703         mips_abi = O64_ABI;
10704       else if (strcmp (arg, "n32") == 0)
10705         mips_abi = N32_ABI;
10706       else if (strcmp (arg, "64") == 0)
10707         {
10708           mips_abi = N64_ABI;
10709           if (! support_64bit_objects())
10710             as_fatal (_("No compiled in support for 64 bit object file "
10711                         "format"));
10712         }
10713       else if (strcmp (arg, "eabi") == 0)
10714         mips_abi = EABI_ABI;
10715       else
10716         {
10717           as_fatal (_("invalid abi -mabi=%s"), arg);
10718           return 0;
10719         }
10720       break;
10721 #endif /* OBJ_ELF */
10722
10723     case OPTION_M7000_HILO_FIX:
10724       mips_7000_hilo_fix = TRUE;
10725       break;
10726
10727     case OPTION_MNO_7000_HILO_FIX:
10728       mips_7000_hilo_fix = FALSE;
10729       break;
10730
10731 #ifdef OBJ_ELF
10732     case OPTION_MDEBUG:
10733       mips_flag_mdebug = TRUE;
10734       break;
10735
10736     case OPTION_NO_MDEBUG:
10737       mips_flag_mdebug = FALSE;
10738       break;
10739 #endif /* OBJ_ELF */
10740
10741     default:
10742       return 0;
10743     }
10744
10745   return 1;
10746 }
10747 \f
10748 /* Set up globals to generate code for the ISA or processor
10749    described by INFO.  */
10750
10751 static void
10752 mips_set_architecture (info)
10753      const struct mips_cpu_info *info;
10754 {
10755   if (info != 0)
10756     {
10757       mips_arch_info = info;
10758       mips_arch = info->cpu;
10759       mips_opts.isa = info->isa;
10760     }
10761 }
10762
10763
10764 /* Likewise for tuning.  */
10765
10766 static void
10767 mips_set_tune (info)
10768      const struct mips_cpu_info *info;
10769 {
10770   if (info != 0)
10771     {
10772       mips_tune_info = info;
10773       mips_tune = info->cpu;
10774     }
10775 }
10776
10777
10778 void
10779 mips_after_parse_args ()
10780 {
10781   /* GP relative stuff not working for PE */
10782   if (strncmp (TARGET_OS, "pe", 2) == 0
10783       && g_switch_value != 0)
10784     {
10785       if (g_switch_seen)
10786         as_bad (_("-G not supported in this configuration."));
10787       g_switch_value = 0;
10788     }
10789
10790   /* The following code determines the architecture and register size.
10791      Similar code was added to GCC 3.3 (see override_options() in
10792      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10793      as much as possible.  */
10794
10795   if (mips_arch_string != 0)
10796     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
10797
10798   if (mips_tune_string != 0)
10799     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
10800
10801   if (file_mips_isa != ISA_UNKNOWN)
10802     {
10803       /* Handle -mipsN.  At this point, file_mips_isa contains the
10804          ISA level specified by -mipsN, while mips_opts.isa contains
10805          the -march selection (if any).  */
10806       if (mips_arch_info != 0)
10807         {
10808           /* -march takes precedence over -mipsN, since it is more descriptive.
10809              There's no harm in specifying both as long as the ISA levels
10810              are the same.  */
10811           if (file_mips_isa != mips_opts.isa)
10812             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10813                     mips_cpu_info_from_isa (file_mips_isa)->name,
10814                     mips_cpu_info_from_isa (mips_opts.isa)->name);
10815         }
10816       else
10817         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
10818     }
10819
10820   if (mips_arch_info == 0)
10821     mips_set_architecture (mips_parse_cpu ("default CPU",
10822                                            MIPS_CPU_STRING_DEFAULT));
10823
10824   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10825     as_bad ("-march=%s is not compatible with the selected ABI",
10826             mips_arch_info->name);
10827
10828   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
10829   if (mips_tune_info == 0)
10830     mips_set_tune (mips_arch_info);
10831
10832   if (file_mips_gp32 >= 0)
10833     {
10834       /* The user specified the size of the integer registers.  Make sure
10835          it agrees with the ABI and ISA.  */
10836       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10837         as_bad (_("-mgp64 used with a 32-bit processor"));
10838       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10839         as_bad (_("-mgp32 used with a 64-bit ABI"));
10840       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10841         as_bad (_("-mgp64 used with a 32-bit ABI"));
10842     }
10843   else
10844     {
10845       /* Infer the integer register size from the ABI and processor.
10846          Restrict ourselves to 32-bit registers if that's all the
10847          processor has, or if the ABI cannot handle 64-bit registers.  */
10848       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10849                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10850     }
10851
10852   /* ??? GAS treats single-float processors as though they had 64-bit
10853      float registers (although it complains when double-precision
10854      instructions are used).  As things stand, saying they have 32-bit
10855      registers would lead to spurious "register must be even" messages.
10856      So here we assume float registers are always the same size as
10857      integer ones, unless the user says otherwise.  */
10858   if (file_mips_fp32 < 0)
10859     file_mips_fp32 = file_mips_gp32;
10860
10861   /* End of GCC-shared inference code.  */
10862
10863   /* ??? When do we want this flag to be set?   Who uses it?  */
10864   if (file_mips_gp32 == 1
10865       && mips_abi == NO_ABI
10866       && ISA_HAS_64BIT_REGS (mips_opts.isa))
10867     mips_32bitmode = 1;
10868
10869   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10870     as_bad (_("trap exception not supported at ISA 1"));
10871
10872   /* If the selected architecture includes support for ASEs, enable
10873      generation of code for them.  */
10874   if (mips_opts.mips16 == -1)
10875     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
10876   if (mips_opts.ase_mips3d == -1)
10877     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
10878   if (mips_opts.ase_mdmx == -1)
10879     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
10880
10881   file_mips_isa = mips_opts.isa;
10882   file_ase_mips16 = mips_opts.mips16;
10883   file_ase_mips3d = mips_opts.ase_mips3d;
10884   file_ase_mdmx = mips_opts.ase_mdmx;
10885   mips_opts.gp32 = file_mips_gp32;
10886   mips_opts.fp32 = file_mips_fp32;
10887
10888   if (mips_flag_mdebug < 0)
10889     {
10890 #ifdef OBJ_MAYBE_ECOFF
10891       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10892         mips_flag_mdebug = 1;
10893       else
10894 #endif /* OBJ_MAYBE_ECOFF */
10895         mips_flag_mdebug = 0;
10896     }
10897 }
10898 \f
10899 void
10900 mips_init_after_args ()
10901 {
10902   /* initialize opcodes */
10903   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10904   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10905 }
10906
10907 long
10908 md_pcrel_from (fixP)
10909      fixS *fixP;
10910 {
10911   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10912       && fixP->fx_addsy != (symbolS *) NULL
10913       && ! S_IS_DEFINED (fixP->fx_addsy))
10914     return 4;
10915
10916   /* Return the address of the delay slot.  */
10917   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10918 }
10919
10920 /* This is called before the symbol table is processed.  In order to
10921    work with gcc when using mips-tfile, we must keep all local labels.
10922    However, in other cases, we want to discard them.  If we were
10923    called with -g, but we didn't see any debugging information, it may
10924    mean that gcc is smuggling debugging information through to
10925    mips-tfile, in which case we must generate all local labels.  */
10926
10927 void
10928 mips_frob_file_before_adjust ()
10929 {
10930 #ifndef NO_ECOFF_DEBUGGING
10931   if (ECOFF_DEBUGGING
10932       && mips_debug != 0
10933       && ! ecoff_debugging_seen)
10934     flag_keep_locals = 1;
10935 #endif
10936 }
10937
10938 /* Sort any unmatched HI16_S relocs so that they immediately precede
10939    the corresponding LO reloc.  This is called before md_apply_fix3 and
10940    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
10941    explicit use of the %hi modifier.  */
10942
10943 void
10944 mips_frob_file ()
10945 {
10946   struct mips_hi_fixup *l;
10947
10948   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10949     {
10950       segment_info_type *seginfo;
10951       int pass;
10952
10953       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10954
10955       /* If a GOT16 relocation turns out to be against a global symbol,
10956          there isn't supposed to be a matching LO.  */
10957       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10958           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10959         continue;
10960
10961       /* Check quickly whether the next fixup happens to be a matching %lo.  */
10962       if (fixup_has_matching_lo_p (l->fixp))
10963         continue;
10964
10965       /* Look through the fixups for this segment for a matching %lo.
10966          When we find one, move the %hi just in front of it.  We do
10967          this in two passes.  In the first pass, we try to find a
10968          unique %lo.  In the second pass, we permit multiple %hi
10969          relocs for a single %lo (this is a GNU extension).  */
10970       seginfo = seg_info (l->seg);
10971       for (pass = 0; pass < 2; pass++)
10972         {
10973           fixS *f, *prev;
10974
10975           prev = NULL;
10976           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10977             {
10978               /* Check whether this is a %lo fixup which matches l->fixp.  */
10979               if (f->fx_r_type == BFD_RELOC_LO16
10980                   && f->fx_addsy == l->fixp->fx_addsy
10981                   && f->fx_offset == l->fixp->fx_offset
10982                   && (pass == 1
10983                       || prev == NULL
10984                       || !reloc_needs_lo_p (prev->fx_r_type)
10985                       || !fixup_has_matching_lo_p (prev)))
10986                 {
10987                   fixS **pf;
10988
10989                   /* Move l->fixp before f.  */
10990                   for (pf = &seginfo->fix_root;
10991                        *pf != l->fixp;
10992                        pf = &(*pf)->fx_next)
10993                     assert (*pf != NULL);
10994
10995                   *pf = l->fixp->fx_next;
10996
10997                   l->fixp->fx_next = f;
10998                   if (prev == NULL)
10999                     seginfo->fix_root = l->fixp;
11000                   else
11001                     prev->fx_next = l->fixp;
11002
11003                   break;
11004                 }
11005
11006               prev = f;
11007             }
11008
11009           if (f != NULL)
11010             break;
11011
11012 #if 0 /* GCC code motion plus incomplete dead code elimination
11013          can leave a %hi without a %lo.  */
11014           if (pass == 1)
11015             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11016                            _("Unmatched %%hi reloc"));
11017 #endif
11018         }
11019     }
11020 }
11021
11022 /* When generating embedded PIC code we need to use a special
11023    relocation to represent the difference of two symbols in the .text
11024    section (switch tables use a difference of this sort).  See
11025    include/coff/mips.h for details.  This macro checks whether this
11026    fixup requires the special reloc.  */
11027 #define SWITCH_TABLE(fixp) \
11028   ((fixp)->fx_r_type == BFD_RELOC_32 \
11029    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11030    && (fixp)->fx_addsy != NULL \
11031    && (fixp)->fx_subsy != NULL \
11032    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11033    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11034
11035 /* When generating embedded PIC code we must keep all PC relative
11036    relocations, in case the linker has to relax a call.  We also need
11037    to keep relocations for switch table entries.
11038
11039    We may have combined relocations without symbols in the N32/N64 ABI.
11040    We have to prevent gas from dropping them.  */
11041
11042 int
11043 mips_force_relocation (fixp)
11044      fixS *fixp;
11045 {
11046   if (generic_force_reloc (fixp))
11047     return 1;
11048
11049   if (HAVE_NEWABI
11050       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11051       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11052           || fixp->fx_r_type == BFD_RELOC_HI16_S
11053           || fixp->fx_r_type == BFD_RELOC_LO16))
11054     return 1;
11055
11056   return (mips_pic == EMBEDDED_PIC
11057           && (fixp->fx_pcrel
11058               || SWITCH_TABLE (fixp)
11059               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11060               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11061 }
11062
11063 /* This hook is called before a fix is simplified.  We don't really
11064    decide whether to skip a fix here.  Rather, we turn global symbols
11065    used as branch targets into local symbols, such that they undergo
11066    simplification.  We can only do this if the symbol is defined and
11067    it is in the same section as the branch.  If this doesn't hold, we
11068    emit a better error message than just saying the relocation is not
11069    valid for the selected object format.
11070
11071    FIXP is the fix-up we're going to try to simplify, SEG is the
11072    segment in which the fix up occurs.  The return value should be
11073    non-zero to indicate the fix-up is valid for further
11074    simplifications.  */
11075
11076 int
11077 mips_validate_fix (fixP, seg)
11078      struct fix *fixP;
11079      asection *seg;
11080 {
11081   /* There's a lot of discussion on whether it should be possible to
11082      use R_MIPS_PC16 to represent branch relocations.  The outcome
11083      seems to be that it can, but gas/bfd are very broken in creating
11084      RELA relocations for this, so for now we only accept branches to
11085      symbols in the same section.  Anything else is of dubious value,
11086      since there's no guarantee that at link time the symbol would be
11087      in range.  Even for branches to local symbols this is arguably
11088      wrong, since it we assume the symbol is not going to be
11089      overridden, which should be possible per ELF library semantics,
11090      but then, there isn't a dynamic relocation that could be used to
11091      this effect, and the target would likely be out of range as well.
11092
11093      Unfortunately, it seems that there is too much code out there
11094      that relies on branches to symbols that are global to be resolved
11095      as if they were local, like the IRIX tools do, so we do it as
11096      well, but with a warning so that people are reminded to fix their
11097      code.  If we ever get back to using R_MIPS_PC16 for branch
11098      targets, this entire block should go away (and probably the
11099      whole function).  */
11100
11101   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11102       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11103             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11104            && mips_pic != EMBEDDED_PIC)
11105           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11106       && fixP->fx_addsy)
11107     {
11108       if (! S_IS_DEFINED (fixP->fx_addsy))
11109         {
11110           as_bad_where (fixP->fx_file, fixP->fx_line,
11111                         _("Cannot branch to undefined symbol."));
11112           /* Avoid any further errors about this fixup.  */
11113           fixP->fx_done = 1;
11114         }
11115       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11116         {
11117           as_bad_where (fixP->fx_file, fixP->fx_line,
11118                         _("Cannot branch to symbol in another section."));
11119           fixP->fx_done = 1;
11120         }
11121       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11122         {
11123           symbolS *sym = fixP->fx_addsy;
11124
11125           as_warn_where (fixP->fx_file, fixP->fx_line,
11126                          _("Pretending global symbol used as branch target is local."));
11127
11128           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11129                                           S_GET_SEGMENT (sym),
11130                                           S_GET_VALUE (sym),
11131                                           symbol_get_frag (sym));
11132           copy_symbol_attributes (fixP->fx_addsy, sym);
11133           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11134           assert (symbol_resolved_p (sym));
11135           symbol_mark_resolved (fixP->fx_addsy);
11136         }
11137     }
11138
11139   return 1;
11140 }
11141
11142 #ifdef OBJ_ELF
11143 static int
11144 mips_need_elf_addend_fixup (fixP)
11145      fixS *fixP;
11146 {
11147   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11148     return 1;
11149   if (mips_pic == EMBEDDED_PIC
11150       && S_IS_WEAK (fixP->fx_addsy))
11151     return 1;
11152   if (mips_pic != EMBEDDED_PIC
11153       && (S_IS_WEAK (fixP->fx_addsy)
11154           || S_IS_EXTERNAL (fixP->fx_addsy))
11155       && !S_IS_COMMON (fixP->fx_addsy))
11156     return 1;
11157   if (symbol_used_in_reloc_p (fixP->fx_addsy)
11158       && (((bfd_get_section_flags (stdoutput,
11159                                    S_GET_SEGMENT (fixP->fx_addsy))
11160             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11161           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11162                        ".gnu.linkonce",
11163                        sizeof (".gnu.linkonce") - 1)))
11164     return 1;
11165   return 0;
11166 }
11167 #endif
11168
11169 /* Apply a fixup to the object file.  */
11170
11171 void
11172 md_apply_fix3 (fixP, valP, seg)
11173      fixS *fixP;
11174      valueT *valP;
11175      segT seg ATTRIBUTE_UNUSED;
11176 {
11177   bfd_byte *buf;
11178   long insn;
11179   valueT value;
11180   static int previous_fx_r_type = 0;
11181
11182   /* FIXME: Maybe just return for all reloc types not listed below?
11183      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11184   if (fixP->fx_r_type == BFD_RELOC_8)
11185       return;
11186
11187   assert (fixP->fx_size == 4
11188           || fixP->fx_r_type == BFD_RELOC_16
11189           || fixP->fx_r_type == BFD_RELOC_32
11190           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11191           || fixP->fx_r_type == BFD_RELOC_HI16_S
11192           || fixP->fx_r_type == BFD_RELOC_LO16
11193           || fixP->fx_r_type == BFD_RELOC_GPREL16
11194           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11195           || fixP->fx_r_type == BFD_RELOC_GPREL32
11196           || fixP->fx_r_type == BFD_RELOC_64
11197           || fixP->fx_r_type == BFD_RELOC_CTOR
11198           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11199           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11200           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11201           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11202           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11203           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11204           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11205           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11206           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11207
11208   value = *valP;
11209
11210   /* If we aren't adjusting this fixup to be against the section
11211      symbol, we need to adjust the value.  */
11212 #ifdef OBJ_ELF
11213   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11214     {
11215       if (mips_need_elf_addend_fixup (fixP))
11216         {
11217           reloc_howto_type *howto;
11218           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11219
11220           value -= symval;
11221
11222           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11223           if (value != 0 && howto && howto->partial_inplace
11224               && (! fixP->fx_pcrel || howto->pcrel_offset))
11225             {
11226               /* In this case, the bfd_install_relocation routine will
11227                  incorrectly add the symbol value back in.  We just want
11228                  the addend to appear in the object file.
11229
11230                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11231                  generated for code like
11232
11233                         globl g1 .text
11234                         .text
11235                         .space 20
11236                  g1:
11237                  x:
11238                         bal g1
11239                */
11240               value -= symval;
11241
11242               /* Make sure the addend is still non-zero.  If it became zero
11243                  after the last operation, set it to a spurious value and
11244                  subtract the same value from the object file's contents.  */
11245               if (value == 0)
11246                 {
11247                   value = 8;
11248
11249                   /* The in-place addends for LO16 relocations are signed;
11250                      leave the matching HI16 in-place addends as zero.  */
11251                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11252                     {
11253                       bfd_vma contents, mask, field;
11254
11255                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11256                                                + fixP->fx_where,
11257                                                fixP->fx_size * 8,
11258                                                target_big_endian);
11259
11260                       /* MASK has bits set where the relocation should go.
11261                          FIELD is -value, shifted into the appropriate place
11262                          for this relocation.  */
11263                       mask = 1 << (howto->bitsize - 1);
11264                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11265                       field = (-value >> howto->rightshift) << howto->bitpos;
11266
11267                       bfd_put_bits ((field & mask) | (contents & ~mask),
11268                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11269                                     fixP->fx_size * 8,
11270                                     target_big_endian);
11271                     }
11272                 }
11273             }
11274         }
11275
11276       /* This code was generated using trial and error and so is
11277          fragile and not trustworthy.  If you change it, you should
11278          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11279          they still pass.  */
11280       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11281         {
11282           value += fixP->fx_frag->fr_address + fixP->fx_where;
11283
11284           /* BFD's REL handling, for MIPS, is _very_ weird.
11285              This gives the right results, but it can't possibly
11286              be the way things are supposed to work.  */
11287           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11288               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11289             value += fixP->fx_frag->fr_address + fixP->fx_where;
11290         }
11291     }
11292 #endif
11293
11294   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11295
11296   /* We are not done if this is a composite relocation to set up gp.  */
11297   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11298       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11299            || (fixP->fx_r_type == BFD_RELOC_64
11300                && (previous_fx_r_type == BFD_RELOC_GPREL32
11301                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11302            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11303                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11304                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11305     fixP->fx_done = 1;
11306   previous_fx_r_type = fixP->fx_r_type;
11307
11308   switch (fixP->fx_r_type)
11309     {
11310     case BFD_RELOC_MIPS_JMP:
11311     case BFD_RELOC_MIPS_SHIFT5:
11312     case BFD_RELOC_MIPS_SHIFT6:
11313     case BFD_RELOC_MIPS_GOT_DISP:
11314     case BFD_RELOC_MIPS_GOT_PAGE:
11315     case BFD_RELOC_MIPS_GOT_OFST:
11316     case BFD_RELOC_MIPS_SUB:
11317     case BFD_RELOC_MIPS_INSERT_A:
11318     case BFD_RELOC_MIPS_INSERT_B:
11319     case BFD_RELOC_MIPS_DELETE:
11320     case BFD_RELOC_MIPS_HIGHEST:
11321     case BFD_RELOC_MIPS_HIGHER:
11322     case BFD_RELOC_MIPS_SCN_DISP:
11323     case BFD_RELOC_MIPS_REL16:
11324     case BFD_RELOC_MIPS_RELGOT:
11325     case BFD_RELOC_MIPS_JALR:
11326     case BFD_RELOC_HI16:
11327     case BFD_RELOC_HI16_S:
11328     case BFD_RELOC_GPREL16:
11329     case BFD_RELOC_MIPS_LITERAL:
11330     case BFD_RELOC_MIPS_CALL16:
11331     case BFD_RELOC_MIPS_GOT16:
11332     case BFD_RELOC_GPREL32:
11333     case BFD_RELOC_MIPS_GOT_HI16:
11334     case BFD_RELOC_MIPS_GOT_LO16:
11335     case BFD_RELOC_MIPS_CALL_HI16:
11336     case BFD_RELOC_MIPS_CALL_LO16:
11337     case BFD_RELOC_MIPS16_GPREL:
11338       if (fixP->fx_pcrel)
11339         as_bad_where (fixP->fx_file, fixP->fx_line,
11340                       _("Invalid PC relative reloc"));
11341       /* Nothing needed to do. The value comes from the reloc entry */
11342       break;
11343
11344     case BFD_RELOC_MIPS16_JMP:
11345       /* We currently always generate a reloc against a symbol, which
11346          means that we don't want an addend even if the symbol is
11347          defined.  */
11348       fixP->fx_addnumber = 0;
11349       break;
11350
11351     case BFD_RELOC_PCREL_HI16_S:
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         {
11361           /* For an external symbol adjust by the address to make it
11362              pcrel_offset.  We use the address of the RELLO reloc
11363              which follows this one.  */
11364           value += (fixP->fx_next->fx_frag->fr_address
11365                     + fixP->fx_next->fx_where);
11366         }
11367       value = ((value + 0x8000) >> 16) & 0xffff;
11368       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11369       if (target_big_endian)
11370         buf += 2;
11371       md_number_to_chars ((char *) buf, value, 2);
11372       break;
11373
11374     case BFD_RELOC_PCREL_LO16:
11375       /* The addend for this is tricky if it is internal, so we just
11376          do everything here rather than in bfd_install_relocation.  */
11377       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11378           && !fixP->fx_done
11379           && value != 0)
11380         break;
11381       if (fixP->fx_addsy
11382           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11383         value += fixP->fx_frag->fr_address + fixP->fx_where;
11384       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11385       if (target_big_endian)
11386         buf += 2;
11387       md_number_to_chars ((char *) buf, value, 2);
11388       break;
11389
11390     case BFD_RELOC_64:
11391       /* This is handled like BFD_RELOC_32, but we output a sign
11392          extended value if we are only 32 bits.  */
11393       if (fixP->fx_done
11394           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11395         {
11396           if (8 <= sizeof (valueT))
11397             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11398                                 value, 8);
11399           else
11400             {
11401               long w1, w2;
11402               long hiv;
11403
11404               w1 = w2 = fixP->fx_where;
11405               if (target_big_endian)
11406                 w1 += 4;
11407               else
11408                 w2 += 4;
11409               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11410               if ((value & 0x80000000) != 0)
11411                 hiv = 0xffffffff;
11412               else
11413                 hiv = 0;
11414               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11415             }
11416         }
11417       break;
11418
11419     case BFD_RELOC_RVA:
11420     case BFD_RELOC_32:
11421       /* If we are deleting this reloc entry, we must fill in the
11422          value now.  This can happen if we have a .word which is not
11423          resolved when it appears but is later defined.  We also need
11424          to fill in the value if this is an embedded PIC switch table
11425          entry.  */
11426       if (fixP->fx_done
11427           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11428         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11429                             value, 4);
11430       break;
11431
11432     case BFD_RELOC_16:
11433       /* If we are deleting this reloc entry, we must fill in the
11434          value now.  */
11435       assert (fixP->fx_size == 2);
11436       if (fixP->fx_done)
11437         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11438                             value, 2);
11439       break;
11440
11441     case BFD_RELOC_LO16:
11442       /* When handling an embedded PIC switch statement, we can wind
11443          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11444       if (fixP->fx_done)
11445         {
11446           if (value + 0x8000 > 0xffff)
11447             as_bad_where (fixP->fx_file, fixP->fx_line,
11448                           _("relocation overflow"));
11449           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11450           if (target_big_endian)
11451             buf += 2;
11452           md_number_to_chars ((char *) buf, value, 2);
11453         }
11454       break;
11455
11456     case BFD_RELOC_16_PCREL_S2:
11457       if ((value & 0x3) != 0)
11458         as_bad_where (fixP->fx_file, fixP->fx_line,
11459                       _("Branch to odd address (%lx)"), (long) value);
11460
11461       /*
11462        * We need to save the bits in the instruction since fixup_segment()
11463        * might be deleting the relocation entry (i.e., a branch within
11464        * the current segment).
11465        */
11466       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11467         break;
11468       /* If 'value' is zero, the remaining reloc code won't actually
11469          do the store, so it must be done here.  This is probably
11470          a bug somewhere.  */
11471       if (!fixP->fx_done
11472           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11473               || fixP->fx_addsy == NULL                 /* ??? */
11474               || ! S_IS_DEFINED (fixP->fx_addsy)))
11475         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11476
11477       value = (offsetT) value >> 2;
11478
11479       /* update old instruction data */
11480       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11481       if (target_big_endian)
11482         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11483       else
11484         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11485
11486       if (value + 0x8000 <= 0xffff)
11487         insn |= value & 0xffff;
11488       else
11489         {
11490           /* The branch offset is too large.  If this is an
11491              unconditional branch, and we are not generating PIC code,
11492              we can convert it to an absolute jump instruction.  */
11493           if (mips_pic == NO_PIC
11494               && fixP->fx_done
11495               && fixP->fx_frag->fr_address >= text_section->vma
11496               && (fixP->fx_frag->fr_address
11497                   < text_section->vma + text_section->_raw_size)
11498               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11499                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11500                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11501             {
11502               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11503                 insn = 0x0c000000;      /* jal */
11504               else
11505                 insn = 0x08000000;      /* j */
11506               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11507               fixP->fx_done = 0;
11508               fixP->fx_addsy = section_symbol (text_section);
11509               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11510             }
11511           else
11512             {
11513               /* If we got here, we have branch-relaxation disabled,
11514                  and there's nothing we can do to fix this instruction
11515                  without turning it into a longer sequence.  */
11516               as_bad_where (fixP->fx_file, fixP->fx_line,
11517                             _("Branch out of range"));
11518             }
11519         }
11520
11521       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11522       break;
11523
11524     case BFD_RELOC_VTABLE_INHERIT:
11525       fixP->fx_done = 0;
11526       if (fixP->fx_addsy
11527           && !S_IS_DEFINED (fixP->fx_addsy)
11528           && !S_IS_WEAK (fixP->fx_addsy))
11529         S_SET_WEAK (fixP->fx_addsy);
11530       break;
11531
11532     case BFD_RELOC_VTABLE_ENTRY:
11533       fixP->fx_done = 0;
11534       break;
11535
11536     default:
11537       internalError ();
11538     }
11539 }
11540
11541 #if 0
11542 void
11543 printInsn (oc)
11544      unsigned long oc;
11545 {
11546   const struct mips_opcode *p;
11547   int treg, sreg, dreg, shamt;
11548   short imm;
11549   const char *args;
11550   int i;
11551
11552   for (i = 0; i < NUMOPCODES; ++i)
11553     {
11554       p = &mips_opcodes[i];
11555       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11556         {
11557           printf ("%08lx %s\t", oc, p->name);
11558           treg = (oc >> 16) & 0x1f;
11559           sreg = (oc >> 21) & 0x1f;
11560           dreg = (oc >> 11) & 0x1f;
11561           shamt = (oc >> 6) & 0x1f;
11562           imm = oc;
11563           for (args = p->args;; ++args)
11564             {
11565               switch (*args)
11566                 {
11567                 case '\0':
11568                   printf ("\n");
11569                   break;
11570
11571                 case ',':
11572                 case '(':
11573                 case ')':
11574                   printf ("%c", *args);
11575                   continue;
11576
11577                 case 'r':
11578                   assert (treg == sreg);
11579                   printf ("$%d,$%d", treg, sreg);
11580                   continue;
11581
11582                 case 'd':
11583                 case 'G':
11584                   printf ("$%d", dreg);
11585                   continue;
11586
11587                 case 't':
11588                 case 'E':
11589                   printf ("$%d", treg);
11590                   continue;
11591
11592                 case 'k':
11593                   printf ("0x%x", treg);
11594                   continue;
11595
11596                 case 'b':
11597                 case 's':
11598                   printf ("$%d", sreg);
11599                   continue;
11600
11601                 case 'a':
11602                   printf ("0x%08lx", oc & 0x1ffffff);
11603                   continue;
11604
11605                 case 'i':
11606                 case 'j':
11607                 case 'o':
11608                 case 'u':
11609                   printf ("%d", imm);
11610                   continue;
11611
11612                 case '<':
11613                 case '>':
11614                   printf ("$%d", shamt);
11615                   continue;
11616
11617                 default:
11618                   internalError ();
11619                 }
11620               break;
11621             }
11622           return;
11623         }
11624     }
11625   printf (_("%08lx  UNDEFINED\n"), oc);
11626 }
11627 #endif
11628
11629 static symbolS *
11630 get_symbol ()
11631 {
11632   int c;
11633   char *name;
11634   symbolS *p;
11635
11636   name = input_line_pointer;
11637   c = get_symbol_end ();
11638   p = (symbolS *) symbol_find_or_make (name);
11639   *input_line_pointer = c;
11640   return p;
11641 }
11642
11643 /* Align the current frag to a given power of two.  The MIPS assembler
11644    also automatically adjusts any preceding label.  */
11645
11646 static void
11647 mips_align (to, fill, label)
11648      int to;
11649      int fill;
11650      symbolS *label;
11651 {
11652   mips_emit_delays (FALSE);
11653   frag_align (to, fill, 0);
11654   record_alignment (now_seg, to);
11655   if (label != NULL)
11656     {
11657       assert (S_GET_SEGMENT (label) == now_seg);
11658       symbol_set_frag (label, frag_now);
11659       S_SET_VALUE (label, (valueT) frag_now_fix ());
11660     }
11661 }
11662
11663 /* Align to a given power of two.  .align 0 turns off the automatic
11664    alignment used by the data creating pseudo-ops.  */
11665
11666 static void
11667 s_align (x)
11668      int x ATTRIBUTE_UNUSED;
11669 {
11670   register int temp;
11671   register long temp_fill;
11672   long max_alignment = 15;
11673
11674   /*
11675
11676     o  Note that the assembler pulls down any immediately preceeding label
11677        to the aligned address.
11678     o  It's not documented but auto alignment is reinstated by
11679        a .align pseudo instruction.
11680     o  Note also that after auto alignment is turned off the mips assembler
11681        issues an error on attempt to assemble an improperly aligned data item.
11682        We don't.
11683
11684     */
11685
11686   temp = get_absolute_expression ();
11687   if (temp > max_alignment)
11688     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11689   else if (temp < 0)
11690     {
11691       as_warn (_("Alignment negative: 0 assumed."));
11692       temp = 0;
11693     }
11694   if (*input_line_pointer == ',')
11695     {
11696       ++input_line_pointer;
11697       temp_fill = get_absolute_expression ();
11698     }
11699   else
11700     temp_fill = 0;
11701   if (temp)
11702     {
11703       auto_align = 1;
11704       mips_align (temp, (int) temp_fill,
11705                   insn_labels != NULL ? insn_labels->label : NULL);
11706     }
11707   else
11708     {
11709       auto_align = 0;
11710     }
11711
11712   demand_empty_rest_of_line ();
11713 }
11714
11715 void
11716 mips_flush_pending_output ()
11717 {
11718   mips_emit_delays (FALSE);
11719   mips_clear_insn_labels ();
11720 }
11721
11722 static void
11723 s_change_sec (sec)
11724      int sec;
11725 {
11726   segT seg;
11727
11728   /* When generating embedded PIC code, we only use the .text, .lit8,
11729      .sdata and .sbss sections.  We change the .data and .rdata
11730      pseudo-ops to use .sdata.  */
11731   if (mips_pic == EMBEDDED_PIC
11732       && (sec == 'd' || sec == 'r'))
11733     sec = 's';
11734
11735 #ifdef OBJ_ELF
11736   /* The ELF backend needs to know that we are changing sections, so
11737      that .previous works correctly.  We could do something like check
11738      for an obj_section_change_hook macro, but that might be confusing
11739      as it would not be appropriate to use it in the section changing
11740      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11741      This should be cleaner, somehow.  */
11742   obj_elf_section_change_hook ();
11743 #endif
11744
11745   mips_emit_delays (FALSE);
11746   switch (sec)
11747     {
11748     case 't':
11749       s_text (0);
11750       break;
11751     case 'd':
11752       s_data (0);
11753       break;
11754     case 'b':
11755       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11756       demand_empty_rest_of_line ();
11757       break;
11758
11759     case 'r':
11760       if (USE_GLOBAL_POINTER_OPT)
11761         {
11762           seg = subseg_new (RDATA_SECTION_NAME,
11763                             (subsegT) get_absolute_expression ());
11764           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11765             {
11766               bfd_set_section_flags (stdoutput, seg,
11767                                      (SEC_ALLOC
11768                                       | SEC_LOAD
11769                                       | SEC_READONLY
11770                                       | SEC_RELOC
11771                                       | SEC_DATA));
11772               if (strcmp (TARGET_OS, "elf") != 0)
11773                 record_alignment (seg, 4);
11774             }
11775           demand_empty_rest_of_line ();
11776         }
11777       else
11778         {
11779           as_bad (_("No read only data section in this object file format"));
11780           demand_empty_rest_of_line ();
11781           return;
11782         }
11783       break;
11784
11785     case 's':
11786       if (USE_GLOBAL_POINTER_OPT)
11787         {
11788           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11789           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11790             {
11791               bfd_set_section_flags (stdoutput, seg,
11792                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11793                                      | SEC_DATA);
11794               if (strcmp (TARGET_OS, "elf") != 0)
11795                 record_alignment (seg, 4);
11796             }
11797           demand_empty_rest_of_line ();
11798           break;
11799         }
11800       else
11801         {
11802           as_bad (_("Global pointers not supported; recompile -G 0"));
11803           demand_empty_rest_of_line ();
11804           return;
11805         }
11806     }
11807
11808   auto_align = 1;
11809 }
11810
11811 void
11812 s_change_section (ignore)
11813      int ignore ATTRIBUTE_UNUSED;
11814 {
11815 #ifdef OBJ_ELF
11816   char *section_name;
11817   char c;
11818   char next_c;
11819   int section_type;
11820   int section_flag;
11821   int section_entry_size;
11822   int section_alignment;
11823
11824   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11825     return;
11826
11827   section_name = input_line_pointer;
11828   c = get_symbol_end ();
11829   if (c)
11830     next_c = *(input_line_pointer + 1);
11831
11832   /* Do we have .section Name<,"flags">?  */
11833   if (c != ',' || (c == ',' && next_c == '"'))
11834     {
11835       /* just after name is now '\0'.  */
11836       *input_line_pointer = c;
11837       input_line_pointer = section_name;
11838       obj_elf_section (ignore);
11839       return;
11840     }
11841   input_line_pointer++;
11842
11843   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11844   if (c == ',')
11845     section_type = get_absolute_expression ();
11846   else
11847     section_type = 0;
11848   if (*input_line_pointer++ == ',')
11849     section_flag = get_absolute_expression ();
11850   else
11851     section_flag = 0;
11852   if (*input_line_pointer++ == ',')
11853     section_entry_size = get_absolute_expression ();
11854   else
11855     section_entry_size = 0;
11856   if (*input_line_pointer++ == ',')
11857     section_alignment = get_absolute_expression ();
11858   else
11859     section_alignment = 0;
11860
11861   section_name = xstrdup (section_name);
11862
11863   obj_elf_change_section (section_name, section_type, section_flag,
11864                           section_entry_size, 0, 0, 0);
11865
11866   if (now_seg->name != section_name)
11867     free (section_name);
11868 #endif /* OBJ_ELF */
11869 }
11870
11871 void
11872 mips_enable_auto_align ()
11873 {
11874   auto_align = 1;
11875 }
11876
11877 static void
11878 s_cons (log_size)
11879      int log_size;
11880 {
11881   symbolS *label;
11882
11883   label = insn_labels != NULL ? insn_labels->label : NULL;
11884   mips_emit_delays (FALSE);
11885   if (log_size > 0 && auto_align)
11886     mips_align (log_size, 0, label);
11887   mips_clear_insn_labels ();
11888   cons (1 << log_size);
11889 }
11890
11891 static void
11892 s_float_cons (type)
11893      int type;
11894 {
11895   symbolS *label;
11896
11897   label = insn_labels != NULL ? insn_labels->label : NULL;
11898
11899   mips_emit_delays (FALSE);
11900
11901   if (auto_align)
11902     {
11903       if (type == 'd')
11904         mips_align (3, 0, label);
11905       else
11906         mips_align (2, 0, label);
11907     }
11908
11909   mips_clear_insn_labels ();
11910
11911   float_cons (type);
11912 }
11913
11914 /* Handle .globl.  We need to override it because on Irix 5 you are
11915    permitted to say
11916        .globl foo .text
11917    where foo is an undefined symbol, to mean that foo should be
11918    considered to be the address of a function.  */
11919
11920 static void
11921 s_mips_globl (x)
11922      int x ATTRIBUTE_UNUSED;
11923 {
11924   char *name;
11925   int c;
11926   symbolS *symbolP;
11927   flagword flag;
11928
11929   name = input_line_pointer;
11930   c = get_symbol_end ();
11931   symbolP = symbol_find_or_make (name);
11932   *input_line_pointer = c;
11933   SKIP_WHITESPACE ();
11934
11935   /* On Irix 5, every global symbol that is not explicitly labelled as
11936      being a function is apparently labelled as being an object.  */
11937   flag = BSF_OBJECT;
11938
11939   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11940     {
11941       char *secname;
11942       asection *sec;
11943
11944       secname = input_line_pointer;
11945       c = get_symbol_end ();
11946       sec = bfd_get_section_by_name (stdoutput, secname);
11947       if (sec == NULL)
11948         as_bad (_("%s: no such section"), secname);
11949       *input_line_pointer = c;
11950
11951       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11952         flag = BSF_FUNCTION;
11953     }
11954
11955   symbol_get_bfdsym (symbolP)->flags |= flag;
11956
11957   S_SET_EXTERNAL (symbolP);
11958   demand_empty_rest_of_line ();
11959 }
11960
11961 static void
11962 s_option (x)
11963      int x ATTRIBUTE_UNUSED;
11964 {
11965   char *opt;
11966   char c;
11967
11968   opt = input_line_pointer;
11969   c = get_symbol_end ();
11970
11971   if (*opt == 'O')
11972     {
11973       /* FIXME: What does this mean?  */
11974     }
11975   else if (strncmp (opt, "pic", 3) == 0)
11976     {
11977       int i;
11978
11979       i = atoi (opt + 3);
11980       if (i == 0)
11981         mips_pic = NO_PIC;
11982       else if (i == 2)
11983         mips_pic = SVR4_PIC;
11984       else
11985         as_bad (_(".option pic%d not supported"), i);
11986
11987       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11988         {
11989           if (g_switch_seen && g_switch_value != 0)
11990             as_warn (_("-G may not be used with SVR4 PIC code"));
11991           g_switch_value = 0;
11992           bfd_set_gp_size (stdoutput, 0);
11993         }
11994     }
11995   else
11996     as_warn (_("Unrecognized option \"%s\""), opt);
11997
11998   *input_line_pointer = c;
11999   demand_empty_rest_of_line ();
12000 }
12001
12002 /* This structure is used to hold a stack of .set values.  */
12003
12004 struct mips_option_stack
12005 {
12006   struct mips_option_stack *next;
12007   struct mips_set_options options;
12008 };
12009
12010 static struct mips_option_stack *mips_opts_stack;
12011
12012 /* Handle the .set pseudo-op.  */
12013
12014 static void
12015 s_mipsset (x)
12016      int x ATTRIBUTE_UNUSED;
12017 {
12018   char *name = input_line_pointer, ch;
12019
12020   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12021     ++input_line_pointer;
12022   ch = *input_line_pointer;
12023   *input_line_pointer = '\0';
12024
12025   if (strcmp (name, "reorder") == 0)
12026     {
12027       if (mips_opts.noreorder && prev_nop_frag != NULL)
12028         {
12029           /* If we still have pending nops, we can discard them.  The
12030              usual nop handling will insert any that are still
12031              needed.  */
12032           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12033                                     * (mips_opts.mips16 ? 2 : 4));
12034           prev_nop_frag = NULL;
12035         }
12036       mips_opts.noreorder = 0;
12037     }
12038   else if (strcmp (name, "noreorder") == 0)
12039     {
12040       mips_emit_delays (TRUE);
12041       mips_opts.noreorder = 1;
12042       mips_any_noreorder = 1;
12043     }
12044   else if (strcmp (name, "at") == 0)
12045     {
12046       mips_opts.noat = 0;
12047     }
12048   else if (strcmp (name, "noat") == 0)
12049     {
12050       mips_opts.noat = 1;
12051     }
12052   else if (strcmp (name, "macro") == 0)
12053     {
12054       mips_opts.warn_about_macros = 0;
12055     }
12056   else if (strcmp (name, "nomacro") == 0)
12057     {
12058       if (mips_opts.noreorder == 0)
12059         as_bad (_("`noreorder' must be set before `nomacro'"));
12060       mips_opts.warn_about_macros = 1;
12061     }
12062   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12063     {
12064       mips_opts.nomove = 0;
12065     }
12066   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12067     {
12068       mips_opts.nomove = 1;
12069     }
12070   else if (strcmp (name, "bopt") == 0)
12071     {
12072       mips_opts.nobopt = 0;
12073     }
12074   else if (strcmp (name, "nobopt") == 0)
12075     {
12076       mips_opts.nobopt = 1;
12077     }
12078   else if (strcmp (name, "mips16") == 0
12079            || strcmp (name, "MIPS-16") == 0)
12080     mips_opts.mips16 = 1;
12081   else if (strcmp (name, "nomips16") == 0
12082            || strcmp (name, "noMIPS-16") == 0)
12083     mips_opts.mips16 = 0;
12084   else if (strcmp (name, "mips3d") == 0)
12085     mips_opts.ase_mips3d = 1;
12086   else if (strcmp (name, "nomips3d") == 0)
12087     mips_opts.ase_mips3d = 0;
12088   else if (strcmp (name, "mdmx") == 0)
12089     mips_opts.ase_mdmx = 1;
12090   else if (strcmp (name, "nomdmx") == 0)
12091     mips_opts.ase_mdmx = 0;
12092   else if (strncmp (name, "mips", 4) == 0)
12093     {
12094       int reset = 0;
12095
12096       /* Permit the user to change the ISA on the fly.  Needless to
12097          say, misuse can cause serious problems.  */
12098       if (strcmp (name, "mips0") == 0)
12099         {
12100           reset = 1;
12101           mips_opts.isa = file_mips_isa;
12102         }
12103       else if (strcmp (name, "mips1") == 0)
12104         mips_opts.isa = ISA_MIPS1;
12105       else if (strcmp (name, "mips2") == 0)
12106         mips_opts.isa = ISA_MIPS2;
12107       else if (strcmp (name, "mips3") == 0)
12108         mips_opts.isa = ISA_MIPS3;
12109       else if (strcmp (name, "mips4") == 0)
12110         mips_opts.isa = ISA_MIPS4;
12111       else if (strcmp (name, "mips5") == 0)
12112         mips_opts.isa = ISA_MIPS5;
12113       else if (strcmp (name, "mips32") == 0)
12114         mips_opts.isa = ISA_MIPS32;
12115       else if (strcmp (name, "mips32r2") == 0)
12116         mips_opts.isa = ISA_MIPS32R2;
12117       else if (strcmp (name, "mips64") == 0)
12118         mips_opts.isa = ISA_MIPS64;
12119       else
12120         as_bad (_("unknown ISA level %s"), name + 4);
12121
12122       switch (mips_opts.isa)
12123         {
12124         case  0:
12125           break;
12126         case ISA_MIPS1:
12127         case ISA_MIPS2:
12128         case ISA_MIPS32:
12129         case ISA_MIPS32R2:
12130           mips_opts.gp32 = 1;
12131           mips_opts.fp32 = 1;
12132           break;
12133         case ISA_MIPS3:
12134         case ISA_MIPS4:
12135         case ISA_MIPS5:
12136         case ISA_MIPS64:
12137           mips_opts.gp32 = 0;
12138           mips_opts.fp32 = 0;
12139           break;
12140         default:
12141           as_bad (_("unknown ISA level %s"), name + 4);
12142           break;
12143         }
12144       if (reset)
12145         {
12146           mips_opts.gp32 = file_mips_gp32;
12147           mips_opts.fp32 = file_mips_fp32;
12148         }
12149     }
12150   else if (strcmp (name, "autoextend") == 0)
12151     mips_opts.noautoextend = 0;
12152   else if (strcmp (name, "noautoextend") == 0)
12153     mips_opts.noautoextend = 1;
12154   else if (strcmp (name, "push") == 0)
12155     {
12156       struct mips_option_stack *s;
12157
12158       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12159       s->next = mips_opts_stack;
12160       s->options = mips_opts;
12161       mips_opts_stack = s;
12162     }
12163   else if (strcmp (name, "pop") == 0)
12164     {
12165       struct mips_option_stack *s;
12166
12167       s = mips_opts_stack;
12168       if (s == NULL)
12169         as_bad (_(".set pop with no .set push"));
12170       else
12171         {
12172           /* If we're changing the reorder mode we need to handle
12173              delay slots correctly.  */
12174           if (s->options.noreorder && ! mips_opts.noreorder)
12175             mips_emit_delays (TRUE);
12176           else if (! s->options.noreorder && mips_opts.noreorder)
12177             {
12178               if (prev_nop_frag != NULL)
12179                 {
12180                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12181                                             * (mips_opts.mips16 ? 2 : 4));
12182                   prev_nop_frag = NULL;
12183                 }
12184             }
12185
12186           mips_opts = s->options;
12187           mips_opts_stack = s->next;
12188           free (s);
12189         }
12190     }
12191   else
12192     {
12193       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12194     }
12195   *input_line_pointer = ch;
12196   demand_empty_rest_of_line ();
12197 }
12198
12199 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12200    .option pic2.  It means to generate SVR4 PIC calls.  */
12201
12202 static void
12203 s_abicalls (ignore)
12204      int ignore ATTRIBUTE_UNUSED;
12205 {
12206   mips_pic = SVR4_PIC;
12207   if (USE_GLOBAL_POINTER_OPT)
12208     {
12209       if (g_switch_seen && g_switch_value != 0)
12210         as_warn (_("-G may not be used with SVR4 PIC code"));
12211       g_switch_value = 0;
12212     }
12213   bfd_set_gp_size (stdoutput, 0);
12214   demand_empty_rest_of_line ();
12215 }
12216
12217 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12218    PIC code.  It sets the $gp register for the function based on the
12219    function address, which is in the register named in the argument.
12220    This uses a relocation against _gp_disp, which is handled specially
12221    by the linker.  The result is:
12222         lui     $gp,%hi(_gp_disp)
12223         addiu   $gp,$gp,%lo(_gp_disp)
12224         addu    $gp,$gp,.cpload argument
12225    The .cpload argument is normally $25 == $t9.  */
12226
12227 static void
12228 s_cpload (ignore)
12229      int ignore ATTRIBUTE_UNUSED;
12230 {
12231   expressionS ex;
12232   int icnt = 0;
12233
12234   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12235      .cpload is ignored.  */
12236   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12237     {
12238       s_ignore (0);
12239       return;
12240     }
12241
12242   /* .cpload should be in a .set noreorder section.  */
12243   if (mips_opts.noreorder == 0)
12244     as_warn (_(".cpload not in noreorder section"));
12245
12246   ex.X_op = O_symbol;
12247   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12248   ex.X_op_symbol = NULL;
12249   ex.X_add_number = 0;
12250
12251   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12252   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12253
12254   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12255   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12256                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12257
12258   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12259                mips_gp_register, mips_gp_register, tc_get_register (0));
12260
12261   demand_empty_rest_of_line ();
12262 }
12263
12264 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12265      .cpsetup $reg1, offset|$reg2, label
12266
12267    If offset is given, this results in:
12268      sd         $gp, offset($sp)
12269      lui        $gp, %hi(%neg(%gp_rel(label)))
12270      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12271      daddu      $gp, $gp, $reg1
12272
12273    If $reg2 is given, this results in:
12274      daddu      $reg2, $gp, $0
12275      lui        $gp, %hi(%neg(%gp_rel(label)))
12276      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12277      daddu      $gp, $gp, $reg1
12278    $reg1 is normally $25 == $t9.  */
12279 static void
12280 s_cpsetup (ignore)
12281      int ignore ATTRIBUTE_UNUSED;
12282 {
12283   expressionS ex_off;
12284   expressionS ex_sym;
12285   int reg1;
12286   int icnt = 0;
12287   char *f;
12288
12289   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12290      We also need NewABI support.  */
12291   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12292     {
12293       s_ignore (0);
12294       return;
12295     }
12296
12297   reg1 = tc_get_register (0);
12298   SKIP_WHITESPACE ();
12299   if (*input_line_pointer != ',')
12300     {
12301       as_bad (_("missing argument separator ',' for .cpsetup"));
12302       return;
12303     }
12304   else
12305     ++input_line_pointer;
12306   SKIP_WHITESPACE ();
12307   if (*input_line_pointer == '$')
12308     {
12309       mips_cpreturn_register = tc_get_register (0);
12310       mips_cpreturn_offset = -1;
12311     }
12312   else
12313     {
12314       mips_cpreturn_offset = get_absolute_expression ();
12315       mips_cpreturn_register = -1;
12316     }
12317   SKIP_WHITESPACE ();
12318   if (*input_line_pointer != ',')
12319     {
12320       as_bad (_("missing argument separator ',' for .cpsetup"));
12321       return;
12322     }
12323   else
12324     ++input_line_pointer;
12325   SKIP_WHITESPACE ();
12326   expression (&ex_sym);
12327
12328   if (mips_cpreturn_register == -1)
12329     {
12330       ex_off.X_op = O_constant;
12331       ex_off.X_add_symbol = NULL;
12332       ex_off.X_op_symbol = NULL;
12333       ex_off.X_add_number = mips_cpreturn_offset;
12334
12335       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12336                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12337     }
12338   else
12339     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12340                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12341
12342   /* Ensure there's room for the next two instructions, so that `f'
12343      doesn't end up with an address in the wrong frag.  */
12344   frag_grow (8);
12345   f = frag_more (0);
12346   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12347                (int) BFD_RELOC_GPREL16);
12348   fix_new (frag_now, f - frag_now->fr_literal,
12349            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12350   fix_new (frag_now, f - frag_now->fr_literal,
12351            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12352
12353   f = frag_more (0);
12354   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12355                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12356   fix_new (frag_now, f - frag_now->fr_literal,
12357            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12358   fix_new (frag_now, f - frag_now->fr_literal,
12359            0, NULL, 0, 0, BFD_RELOC_LO16);
12360
12361   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12362                HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12363                mips_gp_register, mips_gp_register, reg1);
12364
12365   demand_empty_rest_of_line ();
12366 }
12367
12368 static void
12369 s_cplocal (ignore)
12370      int ignore ATTRIBUTE_UNUSED;
12371 {
12372   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12373    .cplocal is ignored.  */
12374   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12375     {
12376       s_ignore (0);
12377       return;
12378     }
12379
12380   mips_gp_register = tc_get_register (0);
12381   demand_empty_rest_of_line ();
12382 }
12383
12384 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12385    offset from $sp.  The offset is remembered, and after making a PIC
12386    call $gp is restored from that location.  */
12387
12388 static void
12389 s_cprestore (ignore)
12390      int ignore ATTRIBUTE_UNUSED;
12391 {
12392   expressionS ex;
12393   int icnt = 0;
12394
12395   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12396      .cprestore is ignored.  */
12397   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12398     {
12399       s_ignore (0);
12400       return;
12401     }
12402
12403   mips_cprestore_offset = get_absolute_expression ();
12404   mips_cprestore_valid = 1;
12405
12406   ex.X_op = O_constant;
12407   ex.X_add_symbol = NULL;
12408   ex.X_op_symbol = NULL;
12409   ex.X_add_number = mips_cprestore_offset;
12410
12411   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12412                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12413                                 mips_gp_register, SP);
12414
12415   demand_empty_rest_of_line ();
12416 }
12417
12418 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12419    was given in the preceeding .gpsetup, it results in:
12420      ld         $gp, offset($sp)
12421
12422    If a register $reg2 was given there, it results in:
12423      daddiu     $gp, $gp, $reg2
12424  */
12425 static void
12426 s_cpreturn (ignore)
12427      int ignore ATTRIBUTE_UNUSED;
12428 {
12429   expressionS ex;
12430   int icnt = 0;
12431
12432   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12433      We also need NewABI support.  */
12434   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12435     {
12436       s_ignore (0);
12437       return;
12438     }
12439
12440   if (mips_cpreturn_register == -1)
12441     {
12442       ex.X_op = O_constant;
12443       ex.X_add_symbol = NULL;
12444       ex.X_op_symbol = NULL;
12445       ex.X_add_number = mips_cpreturn_offset;
12446
12447       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12448                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12449     }
12450   else
12451     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12452                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12453
12454   demand_empty_rest_of_line ();
12455 }
12456
12457 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12458    code.  It sets the offset to use in gp_rel relocations.  */
12459
12460 static void
12461 s_gpvalue (ignore)
12462      int ignore ATTRIBUTE_UNUSED;
12463 {
12464   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12465      We also need NewABI support.  */
12466   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12467     {
12468       s_ignore (0);
12469       return;
12470     }
12471
12472   mips_gprel_offset = get_absolute_expression ();
12473
12474   demand_empty_rest_of_line ();
12475 }
12476
12477 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12478    code.  It generates a 32 bit GP relative reloc.  */
12479
12480 static void
12481 s_gpword (ignore)
12482      int ignore ATTRIBUTE_UNUSED;
12483 {
12484   symbolS *label;
12485   expressionS ex;
12486   char *p;
12487
12488   /* When not generating PIC code, this is treated as .word.  */
12489   if (mips_pic != SVR4_PIC)
12490     {
12491       s_cons (2);
12492       return;
12493     }
12494
12495   label = insn_labels != NULL ? insn_labels->label : NULL;
12496   mips_emit_delays (TRUE);
12497   if (auto_align)
12498     mips_align (2, 0, label);
12499   mips_clear_insn_labels ();
12500
12501   expression (&ex);
12502
12503   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12504     {
12505       as_bad (_("Unsupported use of .gpword"));
12506       ignore_rest_of_line ();
12507     }
12508
12509   p = frag_more (4);
12510   md_number_to_chars (p, (valueT) 0, 4);
12511   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12512                BFD_RELOC_GPREL32);
12513
12514   demand_empty_rest_of_line ();
12515 }
12516
12517 static void
12518 s_gpdword (ignore)
12519      int ignore ATTRIBUTE_UNUSED;
12520 {
12521   symbolS *label;
12522   expressionS ex;
12523   char *p;
12524
12525   /* When not generating PIC code, this is treated as .dword.  */
12526   if (mips_pic != SVR4_PIC)
12527     {
12528       s_cons (3);
12529       return;
12530     }
12531
12532   label = insn_labels != NULL ? insn_labels->label : NULL;
12533   mips_emit_delays (TRUE);
12534   if (auto_align)
12535     mips_align (3, 0, label);
12536   mips_clear_insn_labels ();
12537
12538   expression (&ex);
12539
12540   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12541     {
12542       as_bad (_("Unsupported use of .gpdword"));
12543       ignore_rest_of_line ();
12544     }
12545
12546   p = frag_more (8);
12547   md_number_to_chars (p, (valueT) 0, 8);
12548   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12549                BFD_RELOC_GPREL32);
12550
12551   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12552   ex.X_op = O_absent;
12553   ex.X_add_symbol = 0;
12554   ex.X_add_number = 0;
12555   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12556                BFD_RELOC_64);
12557
12558   demand_empty_rest_of_line ();
12559 }
12560
12561 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12562    tables in SVR4 PIC code.  */
12563
12564 static void
12565 s_cpadd (ignore)
12566      int ignore ATTRIBUTE_UNUSED;
12567 {
12568   int icnt = 0;
12569   int reg;
12570
12571   /* This is ignored when not generating SVR4 PIC code.  */
12572   if (mips_pic != SVR4_PIC)
12573     {
12574       s_ignore (0);
12575       return;
12576     }
12577
12578   /* Add $gp to the register named as an argument.  */
12579   reg = tc_get_register (0);
12580   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12581                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
12582                "d,v,t", reg, reg, mips_gp_register);
12583
12584   demand_empty_rest_of_line ();
12585 }
12586
12587 /* Handle the .insn pseudo-op.  This marks instruction labels in
12588    mips16 mode.  This permits the linker to handle them specially,
12589    such as generating jalx instructions when needed.  We also make
12590    them odd for the duration of the assembly, in order to generate the
12591    right sort of code.  We will make them even in the adjust_symtab
12592    routine, while leaving them marked.  This is convenient for the
12593    debugger and the disassembler.  The linker knows to make them odd
12594    again.  */
12595
12596 static void
12597 s_insn (ignore)
12598      int ignore ATTRIBUTE_UNUSED;
12599 {
12600   mips16_mark_labels ();
12601
12602   demand_empty_rest_of_line ();
12603 }
12604
12605 /* Handle a .stabn directive.  We need these in order to mark a label
12606    as being a mips16 text label correctly.  Sometimes the compiler
12607    will emit a label, followed by a .stabn, and then switch sections.
12608    If the label and .stabn are in mips16 mode, then the label is
12609    really a mips16 text label.  */
12610
12611 static void
12612 s_mips_stab (type)
12613      int type;
12614 {
12615   if (type == 'n')
12616     mips16_mark_labels ();
12617
12618   s_stab (type);
12619 }
12620
12621 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12622  */
12623
12624 static void
12625 s_mips_weakext (ignore)
12626      int ignore ATTRIBUTE_UNUSED;
12627 {
12628   char *name;
12629   int c;
12630   symbolS *symbolP;
12631   expressionS exp;
12632
12633   name = input_line_pointer;
12634   c = get_symbol_end ();
12635   symbolP = symbol_find_or_make (name);
12636   S_SET_WEAK (symbolP);
12637   *input_line_pointer = c;
12638
12639   SKIP_WHITESPACE ();
12640
12641   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12642     {
12643       if (S_IS_DEFINED (symbolP))
12644         {
12645           as_bad ("ignoring attempt to redefine symbol %s",
12646                   S_GET_NAME (symbolP));
12647           ignore_rest_of_line ();
12648           return;
12649         }
12650
12651       if (*input_line_pointer == ',')
12652         {
12653           ++input_line_pointer;
12654           SKIP_WHITESPACE ();
12655         }
12656
12657       expression (&exp);
12658       if (exp.X_op != O_symbol)
12659         {
12660           as_bad ("bad .weakext directive");
12661           ignore_rest_of_line ();
12662           return;
12663         }
12664       symbol_set_value_expression (symbolP, &exp);
12665     }
12666
12667   demand_empty_rest_of_line ();
12668 }
12669
12670 /* Parse a register string into a number.  Called from the ECOFF code
12671    to parse .frame.  The argument is non-zero if this is the frame
12672    register, so that we can record it in mips_frame_reg.  */
12673
12674 int
12675 tc_get_register (frame)
12676      int frame;
12677 {
12678   int reg;
12679
12680   SKIP_WHITESPACE ();
12681   if (*input_line_pointer++ != '$')
12682     {
12683       as_warn (_("expected `$'"));
12684       reg = ZERO;
12685     }
12686   else if (ISDIGIT (*input_line_pointer))
12687     {
12688       reg = get_absolute_expression ();
12689       if (reg < 0 || reg >= 32)
12690         {
12691           as_warn (_("Bad register number"));
12692           reg = ZERO;
12693         }
12694     }
12695   else
12696     {
12697       if (strncmp (input_line_pointer, "ra", 2) == 0)
12698         {
12699           reg = RA;
12700           input_line_pointer += 2;
12701         }
12702       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12703         {
12704           reg = FP;
12705           input_line_pointer += 2;
12706         }
12707       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12708         {
12709           reg = SP;
12710           input_line_pointer += 2;
12711         }
12712       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12713         {
12714           reg = GP;
12715           input_line_pointer += 2;
12716         }
12717       else if (strncmp (input_line_pointer, "at", 2) == 0)
12718         {
12719           reg = AT;
12720           input_line_pointer += 2;
12721         }
12722       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12723         {
12724           reg = KT0;
12725           input_line_pointer += 3;
12726         }
12727       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12728         {
12729           reg = KT1;
12730           input_line_pointer += 3;
12731         }
12732       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12733         {
12734           reg = ZERO;
12735           input_line_pointer += 4;
12736         }
12737       else
12738         {
12739           as_warn (_("Unrecognized register name"));
12740           reg = ZERO;
12741           while (ISALNUM(*input_line_pointer))
12742            input_line_pointer++;
12743         }
12744     }
12745   if (frame)
12746     {
12747       mips_frame_reg = reg != 0 ? reg : SP;
12748       mips_frame_reg_valid = 1;
12749       mips_cprestore_valid = 0;
12750     }
12751   return reg;
12752 }
12753
12754 valueT
12755 md_section_align (seg, addr)
12756      asection *seg;
12757      valueT addr;
12758 {
12759   int align = bfd_get_section_alignment (stdoutput, seg);
12760
12761 #ifdef OBJ_ELF
12762   /* We don't need to align ELF sections to the full alignment.
12763      However, Irix 5 may prefer that we align them at least to a 16
12764      byte boundary.  We don't bother to align the sections if we are
12765      targeted for an embedded system.  */
12766   if (strcmp (TARGET_OS, "elf") == 0)
12767     return addr;
12768   if (align > 4)
12769     align = 4;
12770 #endif
12771
12772   return ((addr + (1 << align) - 1) & (-1 << align));
12773 }
12774
12775 /* Utility routine, called from above as well.  If called while the
12776    input file is still being read, it's only an approximation.  (For
12777    example, a symbol may later become defined which appeared to be
12778    undefined earlier.)  */
12779
12780 static int
12781 nopic_need_relax (sym, before_relaxing)
12782      symbolS *sym;
12783      int before_relaxing;
12784 {
12785   if (sym == 0)
12786     return 0;
12787
12788   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12789     {
12790       const char *symname;
12791       int change;
12792
12793       /* Find out whether this symbol can be referenced off the $gp
12794          register.  It can be if it is smaller than the -G size or if
12795          it is in the .sdata or .sbss section.  Certain symbols can
12796          not be referenced off the $gp, although it appears as though
12797          they can.  */
12798       symname = S_GET_NAME (sym);
12799       if (symname != (const char *) NULL
12800           && (strcmp (symname, "eprol") == 0
12801               || strcmp (symname, "etext") == 0
12802               || strcmp (symname, "_gp") == 0
12803               || strcmp (symname, "edata") == 0
12804               || strcmp (symname, "_fbss") == 0
12805               || strcmp (symname, "_fdata") == 0
12806               || strcmp (symname, "_ftext") == 0
12807               || strcmp (symname, "end") == 0
12808               || strcmp (symname, "_gp_disp") == 0))
12809         change = 1;
12810       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12811                && !S_IS_EXTERN (sym)
12812                && (0
12813 #ifndef NO_ECOFF_DEBUGGING
12814                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12815                        && (symbol_get_obj (sym)->ecoff_extern_size
12816                            <= g_switch_value))
12817 #endif
12818                    /* We must defer this decision until after the whole
12819                       file has been read, since there might be a .extern
12820                       after the first use of this symbol.  */
12821                    || (before_relaxing
12822 #ifndef NO_ECOFF_DEBUGGING
12823                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12824 #endif
12825                        && S_GET_VALUE (sym) == 0)
12826                    || (S_GET_VALUE (sym) != 0
12827                        && S_GET_VALUE (sym) <= g_switch_value)))
12828         change = 0;
12829       else
12830         {
12831           const char *segname;
12832
12833           segname = segment_name (S_GET_SEGMENT (sym));
12834           assert (strcmp (segname, ".lit8") != 0
12835                   && strcmp (segname, ".lit4") != 0);
12836           change = (strcmp (segname, ".sdata") != 0
12837                     && strcmp (segname, ".sbss") != 0
12838                     && strncmp (segname, ".sdata.", 7) != 0
12839                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12840         }
12841       return change;
12842     }
12843   else
12844     /* We are not optimizing for the $gp register.  */
12845     return 1;
12846 }
12847
12848
12849 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12850
12851 static bfd_boolean
12852 pic_need_relax (sym, segtype)
12853      symbolS *sym;
12854      asection *segtype;
12855 {
12856   asection *symsec;
12857   bfd_boolean linkonce;
12858
12859   /* Handle the case of a symbol equated to another symbol.  */
12860   while (symbol_equated_reloc_p (sym))
12861     {
12862       symbolS *n;
12863
12864       /* It's possible to get a loop here in a badly written
12865          program.  */
12866       n = symbol_get_value_expression (sym)->X_add_symbol;
12867       if (n == sym)
12868         break;
12869       sym = n;
12870     }
12871
12872   symsec = S_GET_SEGMENT (sym);
12873
12874   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12875   linkonce = FALSE;
12876   if (symsec != segtype && ! S_IS_LOCAL (sym))
12877     {
12878       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12879           != 0)
12880         linkonce = TRUE;
12881
12882       /* The GNU toolchain uses an extension for ELF: a section
12883          beginning with the magic string .gnu.linkonce is a linkonce
12884          section.  */
12885       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12886                    sizeof ".gnu.linkonce" - 1) == 0)
12887         linkonce = TRUE;
12888     }
12889
12890   /* This must duplicate the test in adjust_reloc_syms.  */
12891   return (symsec != &bfd_und_section
12892           && symsec != &bfd_abs_section
12893           && ! bfd_is_com_section (symsec)
12894           && !linkonce
12895 #ifdef OBJ_ELF
12896           /* A global or weak symbol is treated as external.  */
12897           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12898               || (! S_IS_WEAK (sym)
12899                   && (! S_IS_EXTERNAL (sym)
12900                       || mips_pic == EMBEDDED_PIC)))
12901 #endif
12902           );
12903 }
12904
12905
12906 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12907    extended opcode.  SEC is the section the frag is in.  */
12908
12909 static int
12910 mips16_extended_frag (fragp, sec, stretch)
12911      fragS *fragp;
12912      asection *sec;
12913      long stretch;
12914 {
12915   int type;
12916   register const struct mips16_immed_operand *op;
12917   offsetT val;
12918   int mintiny, maxtiny;
12919   segT symsec;
12920   fragS *sym_frag;
12921
12922   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12923     return 0;
12924   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12925     return 1;
12926
12927   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12928   op = mips16_immed_operands;
12929   while (op->type != type)
12930     {
12931       ++op;
12932       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12933     }
12934
12935   if (op->unsp)
12936     {
12937       if (type == '<' || type == '>' || type == '[' || type == ']')
12938         {
12939           mintiny = 1;
12940           maxtiny = 1 << op->nbits;
12941         }
12942       else
12943         {
12944           mintiny = 0;
12945           maxtiny = (1 << op->nbits) - 1;
12946         }
12947     }
12948   else
12949     {
12950       mintiny = - (1 << (op->nbits - 1));
12951       maxtiny = (1 << (op->nbits - 1)) - 1;
12952     }
12953
12954   sym_frag = symbol_get_frag (fragp->fr_symbol);
12955   val = S_GET_VALUE (fragp->fr_symbol);
12956   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12957
12958   if (op->pcrel)
12959     {
12960       addressT addr;
12961
12962       /* We won't have the section when we are called from
12963          mips_relax_frag.  However, we will always have been called
12964          from md_estimate_size_before_relax first.  If this is a
12965          branch to a different section, we mark it as such.  If SEC is
12966          NULL, and the frag is not marked, then it must be a branch to
12967          the same section.  */
12968       if (sec == NULL)
12969         {
12970           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12971             return 1;
12972         }
12973       else
12974         {
12975           /* Must have been called from md_estimate_size_before_relax.  */
12976           if (symsec != sec)
12977             {
12978               fragp->fr_subtype =
12979                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12980
12981               /* FIXME: We should support this, and let the linker
12982                  catch branches and loads that are out of range.  */
12983               as_bad_where (fragp->fr_file, fragp->fr_line,
12984                             _("unsupported PC relative reference to different section"));
12985
12986               return 1;
12987             }
12988           if (fragp != sym_frag && sym_frag->fr_address == 0)
12989             /* Assume non-extended on the first relaxation pass.
12990                The address we have calculated will be bogus if this is
12991                a forward branch to another frag, as the forward frag
12992                will have fr_address == 0.  */
12993             return 0;
12994         }
12995
12996       /* In this case, we know for sure that the symbol fragment is in
12997          the same section.  If the relax_marker of the symbol fragment
12998          differs from the relax_marker of this fragment, we have not
12999          yet adjusted the symbol fragment fr_address.  We want to add
13000          in STRETCH in order to get a better estimate of the address.
13001          This particularly matters because of the shift bits.  */
13002       if (stretch != 0
13003           && sym_frag->relax_marker != fragp->relax_marker)
13004         {
13005           fragS *f;
13006
13007           /* Adjust stretch for any alignment frag.  Note that if have
13008              been expanding the earlier code, the symbol may be
13009              defined in what appears to be an earlier frag.  FIXME:
13010              This doesn't handle the fr_subtype field, which specifies
13011              a maximum number of bytes to skip when doing an
13012              alignment.  */
13013           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13014             {
13015               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13016                 {
13017                   if (stretch < 0)
13018                     stretch = - ((- stretch)
13019                                  & ~ ((1 << (int) f->fr_offset) - 1));
13020                   else
13021                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13022                   if (stretch == 0)
13023                     break;
13024                 }
13025             }
13026           if (f != NULL)
13027             val += stretch;
13028         }
13029
13030       addr = fragp->fr_address + fragp->fr_fix;
13031
13032       /* The base address rules are complicated.  The base address of
13033          a branch is the following instruction.  The base address of a
13034          PC relative load or add is the instruction itself, but if it
13035          is in a delay slot (in which case it can not be extended) use
13036          the address of the instruction whose delay slot it is in.  */
13037       if (type == 'p' || type == 'q')
13038         {
13039           addr += 2;
13040
13041           /* If we are currently assuming that this frag should be
13042              extended, then, the current address is two bytes
13043              higher.  */
13044           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13045             addr += 2;
13046
13047           /* Ignore the low bit in the target, since it will be set
13048              for a text label.  */
13049           if ((val & 1) != 0)
13050             --val;
13051         }
13052       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13053         addr -= 4;
13054       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13055         addr -= 2;
13056
13057       val -= addr & ~ ((1 << op->shift) - 1);
13058
13059       /* Branch offsets have an implicit 0 in the lowest bit.  */
13060       if (type == 'p' || type == 'q')
13061         val /= 2;
13062
13063       /* If any of the shifted bits are set, we must use an extended
13064          opcode.  If the address depends on the size of this
13065          instruction, this can lead to a loop, so we arrange to always
13066          use an extended opcode.  We only check this when we are in
13067          the main relaxation loop, when SEC is NULL.  */
13068       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13069         {
13070           fragp->fr_subtype =
13071             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13072           return 1;
13073         }
13074
13075       /* If we are about to mark a frag as extended because the value
13076          is precisely maxtiny + 1, then there is a chance of an
13077          infinite loop as in the following code:
13078              la $4,foo
13079              .skip      1020
13080              .align     2
13081            foo:
13082          In this case when the la is extended, foo is 0x3fc bytes
13083          away, so the la can be shrunk, but then foo is 0x400 away, so
13084          the la must be extended.  To avoid this loop, we mark the
13085          frag as extended if it was small, and is about to become
13086          extended with a value of maxtiny + 1.  */
13087       if (val == ((maxtiny + 1) << op->shift)
13088           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13089           && sec == NULL)
13090         {
13091           fragp->fr_subtype =
13092             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13093           return 1;
13094         }
13095     }
13096   else if (symsec != absolute_section && sec != NULL)
13097     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13098
13099   if ((val & ((1 << op->shift) - 1)) != 0
13100       || val < (mintiny << op->shift)
13101       || val > (maxtiny << op->shift))
13102     return 1;
13103   else
13104     return 0;
13105 }
13106
13107 /* Compute the length of a branch sequence, and adjust the
13108    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13109    worst-case length is computed, with UPDATE being used to indicate
13110    whether an unconditional (-1), branch-likely (+1) or regular (0)
13111    branch is to be computed.  */
13112 static int
13113 relaxed_branch_length (fragp, sec, update)
13114      fragS *fragp;
13115      asection *sec;
13116      int update;
13117 {
13118   bfd_boolean toofar;
13119   int length;
13120
13121   if (fragp
13122       && S_IS_DEFINED (fragp->fr_symbol)
13123       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13124     {
13125       addressT addr;
13126       offsetT val;
13127
13128       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13129
13130       addr = fragp->fr_address + fragp->fr_fix + 4;
13131
13132       val -= addr;
13133
13134       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13135     }
13136   else if (fragp)
13137     /* If the symbol is not defined or it's in a different segment,
13138        assume the user knows what's going on and emit a short
13139        branch.  */
13140     toofar = FALSE;
13141   else
13142     toofar = TRUE;
13143
13144   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13145     fragp->fr_subtype
13146       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13147                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13148                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13149                              toofar);
13150
13151   length = 4;
13152   if (toofar)
13153     {
13154       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13155         length += 8;
13156
13157       if (mips_pic != NO_PIC)
13158         {
13159           /* Additional space for PIC loading of target address.  */
13160           length += 8;
13161           if (mips_opts.isa == ISA_MIPS1)
13162             /* Additional space for $at-stabilizing nop.  */
13163             length += 4;
13164         }
13165
13166       /* If branch is conditional.  */
13167       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13168         length += 8;
13169     }
13170
13171   return length;
13172 }
13173
13174 /* Estimate the size of a frag before relaxing.  Unless this is the
13175    mips16, we are not really relaxing here, and the final size is
13176    encoded in the subtype information.  For the mips16, we have to
13177    decide whether we are using an extended opcode or not.  */
13178
13179 int
13180 md_estimate_size_before_relax (fragp, segtype)
13181      fragS *fragp;
13182      asection *segtype;
13183 {
13184   int change;
13185
13186   if (RELAX_BRANCH_P (fragp->fr_subtype))
13187     {
13188
13189       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13190
13191       return fragp->fr_var;
13192     }
13193
13194   if (RELAX_MIPS16_P (fragp->fr_subtype))
13195     /* We don't want to modify the EXTENDED bit here; it might get us
13196        into infinite loops.  We change it only in mips_relax_frag().  */
13197     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13198
13199   if (mips_pic == NO_PIC)
13200     change = nopic_need_relax (fragp->fr_symbol, 0);
13201   else if (mips_pic == SVR4_PIC)
13202     change = pic_need_relax (fragp->fr_symbol, segtype);
13203   else
13204     abort ();
13205
13206   if (change)
13207     {
13208       /* Record the offset to the first reloc in the fr_opcode field.
13209          This lets md_convert_frag and tc_gen_reloc know that the code
13210          must be expanded.  */
13211       fragp->fr_opcode = (fragp->fr_literal
13212                           + fragp->fr_fix
13213                           - RELAX_OLD (fragp->fr_subtype)
13214                           + RELAX_RELOC1 (fragp->fr_subtype));
13215       /* FIXME: This really needs as_warn_where.  */
13216       if (RELAX_WARN (fragp->fr_subtype))
13217         as_warn (_("AT used after \".set noat\" or macro used after "
13218                    "\".set nomacro\""));
13219
13220       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13221     }
13222
13223   return 0;
13224 }
13225
13226 /* This is called to see whether a reloc against a defined symbol
13227    should be converted into a reloc against a section.  Don't adjust
13228    MIPS16 jump relocations, so we don't have to worry about the format
13229    of the offset in the .o file.  Don't adjust relocations against
13230    mips16 symbols, so that the linker can find them if it needs to set
13231    up a stub.  */
13232
13233 int
13234 mips_fix_adjustable (fixp)
13235      fixS *fixp;
13236 {
13237   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13238     return 0;
13239
13240   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13241       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13242     return 0;
13243
13244   if (fixp->fx_addsy == NULL)
13245     return 1;
13246
13247 #ifdef OBJ_ELF
13248   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13249       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13250       && fixp->fx_subsy == NULL)
13251     return 0;
13252 #endif
13253
13254   return 1;
13255 }
13256
13257 /* Translate internal representation of relocation info to BFD target
13258    format.  */
13259
13260 arelent **
13261 tc_gen_reloc (section, fixp)
13262      asection *section ATTRIBUTE_UNUSED;
13263      fixS *fixp;
13264 {
13265   static arelent *retval[4];
13266   arelent *reloc;
13267   bfd_reloc_code_real_type code;
13268
13269   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13270   retval[1] = NULL;
13271
13272   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13273   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13274   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13275
13276   if (mips_pic == EMBEDDED_PIC
13277       && SWITCH_TABLE (fixp))
13278     {
13279       /* For a switch table entry we use a special reloc.  The addend
13280          is actually the difference between the reloc address and the
13281          subtrahend.  */
13282       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13283       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13284         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13285       fixp->fx_r_type = BFD_RELOC_GPREL32;
13286     }
13287   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13288     {
13289       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13290         reloc->addend = fixp->fx_addnumber;
13291       else
13292         {
13293           /* We use a special addend for an internal RELLO reloc.  */
13294           if (symbol_section_p (fixp->fx_addsy))
13295             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13296           else
13297             reloc->addend = fixp->fx_addnumber + reloc->address;
13298         }
13299     }
13300   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13301     {
13302       assert (fixp->fx_next != NULL
13303               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13304
13305       /* The reloc is relative to the RELLO; adjust the addend
13306          accordingly.  */
13307       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13308         reloc->addend = fixp->fx_next->fx_addnumber;
13309       else
13310         {
13311           /* We use a special addend for an internal RELHI reloc.  */
13312           if (symbol_section_p (fixp->fx_addsy))
13313             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13314                              + fixp->fx_next->fx_where
13315                              - S_GET_VALUE (fixp->fx_subsy));
13316           else
13317             reloc->addend = (fixp->fx_addnumber
13318                              + fixp->fx_next->fx_frag->fr_address
13319                              + fixp->fx_next->fx_where);
13320         }
13321     }
13322   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13323     reloc->addend = fixp->fx_addnumber;
13324   else
13325     {
13326       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13327         /* A gruesome hack which is a result of the gruesome gas reloc
13328            handling.  */
13329         reloc->addend = reloc->address;
13330       else
13331         reloc->addend = -reloc->address;
13332     }
13333
13334   /* If this is a variant frag, we may need to adjust the existing
13335      reloc and generate a new one.  */
13336   if (fixp->fx_frag->fr_opcode != NULL
13337       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13338            && ! HAVE_NEWABI)
13339           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13340           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13341           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13342           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13343           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13344           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13345     )
13346     {
13347       arelent *reloc2;
13348
13349       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13350
13351       /* If this is not the last reloc in this frag, then we have two
13352          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13353          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13354          the second one handle all of them.  */
13355       if (fixp->fx_next != NULL
13356           && fixp->fx_frag == fixp->fx_next->fx_frag)
13357         {
13358           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13359                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13360                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13361                       && (fixp->fx_next->fx_r_type
13362                           == BFD_RELOC_MIPS_GOT_LO16))
13363                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13364                       && (fixp->fx_next->fx_r_type
13365                           == BFD_RELOC_MIPS_CALL_LO16)));
13366           retval[0] = NULL;
13367           return retval;
13368         }
13369
13370       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13371       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13372       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13373       retval[2] = NULL;
13374       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13375       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13376       reloc2->address = (reloc->address
13377                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13378                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13379       reloc2->addend = fixp->fx_addnumber;
13380       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13381       assert (reloc2->howto != NULL);
13382
13383       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13384         {
13385           arelent *reloc3;
13386
13387           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13388           retval[3] = NULL;
13389           *reloc3 = *reloc2;
13390           reloc3->address += 4;
13391         }
13392
13393       if (mips_pic == NO_PIC)
13394         {
13395           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13396           fixp->fx_r_type = BFD_RELOC_HI16_S;
13397         }
13398       else if (mips_pic == SVR4_PIC)
13399         {
13400           switch (fixp->fx_r_type)
13401             {
13402             default:
13403               abort ();
13404             case BFD_RELOC_MIPS_GOT16:
13405               break;
13406             case BFD_RELOC_MIPS_GOT_LO16:
13407             case BFD_RELOC_MIPS_CALL_LO16:
13408               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13409               break;
13410             case BFD_RELOC_MIPS_CALL16:
13411               if (HAVE_NEWABI)
13412                 {
13413                   /* BFD_RELOC_MIPS_GOT16;*/
13414                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13415                   reloc2->howto = bfd_reloc_type_lookup
13416                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13417                 }
13418               else
13419                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13420               break;
13421             }
13422         }
13423       else
13424         abort ();
13425
13426       /* newabi uses R_MIPS_GOT_DISP for local symbols */
13427       if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
13428         {
13429           fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13430           retval[1] = NULL;
13431         }
13432     }
13433
13434   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13435      entry to be used in the relocation's section offset.  */
13436   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13437     {
13438       reloc->address = reloc->addend;
13439       reloc->addend = 0;
13440     }
13441
13442   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13443      fixup_segment converted a non-PC relative reloc into a PC
13444      relative reloc.  In such a case, we need to convert the reloc
13445      code.  */
13446   code = fixp->fx_r_type;
13447   if (fixp->fx_pcrel)
13448     {
13449       switch (code)
13450         {
13451         case BFD_RELOC_8:
13452           code = BFD_RELOC_8_PCREL;
13453           break;
13454         case BFD_RELOC_16:
13455           code = BFD_RELOC_16_PCREL;
13456           break;
13457         case BFD_RELOC_32:
13458           code = BFD_RELOC_32_PCREL;
13459           break;
13460         case BFD_RELOC_64:
13461           code = BFD_RELOC_64_PCREL;
13462           break;
13463         case BFD_RELOC_8_PCREL:
13464         case BFD_RELOC_16_PCREL:
13465         case BFD_RELOC_32_PCREL:
13466         case BFD_RELOC_64_PCREL:
13467         case BFD_RELOC_16_PCREL_S2:
13468         case BFD_RELOC_PCREL_HI16_S:
13469         case BFD_RELOC_PCREL_LO16:
13470           break;
13471         default:
13472           as_bad_where (fixp->fx_file, fixp->fx_line,
13473                         _("Cannot make %s relocation PC relative"),
13474                         bfd_get_reloc_code_name (code));
13475         }
13476     }
13477
13478 #ifdef OBJ_ELF
13479   /* md_apply_fix3 has a double-subtraction hack to get
13480      bfd_install_relocation to behave nicely.  GPREL relocations are
13481      handled correctly without this hack, so undo it here.  We can't
13482      stop md_apply_fix3 from subtracting twice in the first place since
13483      the fake addend is required for variant frags above.  */
13484   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13485       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13486       && reloc->addend != 0
13487       && mips_need_elf_addend_fixup (fixp))
13488     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13489 #endif
13490
13491   /* To support a PC relative reloc when generating embedded PIC code
13492      for ECOFF, we use a Cygnus extension.  We check for that here to
13493      make sure that we don't let such a reloc escape normally.  */
13494   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13495        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13496       && code == BFD_RELOC_16_PCREL_S2
13497       && mips_pic != EMBEDDED_PIC)
13498     reloc->howto = NULL;
13499   else
13500     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13501
13502   if (reloc->howto == NULL)
13503     {
13504       as_bad_where (fixp->fx_file, fixp->fx_line,
13505                     _("Can not represent %s relocation in this object file format"),
13506                     bfd_get_reloc_code_name (code));
13507       retval[0] = NULL;
13508     }
13509
13510   return retval;
13511 }
13512
13513 /* Relax a machine dependent frag.  This returns the amount by which
13514    the current size of the frag should change.  */
13515
13516 int
13517 mips_relax_frag (sec, fragp, stretch)
13518      asection *sec;
13519      fragS *fragp;
13520      long stretch;
13521 {
13522   if (RELAX_BRANCH_P (fragp->fr_subtype))
13523     {
13524       offsetT old_var = fragp->fr_var;
13525
13526       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13527
13528       return fragp->fr_var - old_var;
13529     }
13530
13531   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13532     return 0;
13533
13534   if (mips16_extended_frag (fragp, NULL, stretch))
13535     {
13536       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13537         return 0;
13538       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13539       return 2;
13540     }
13541   else
13542     {
13543       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13544         return 0;
13545       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13546       return -2;
13547     }
13548
13549   return 0;
13550 }
13551
13552 /* Convert a machine dependent frag.  */
13553
13554 void
13555 md_convert_frag (abfd, asec, fragp)
13556      bfd *abfd ATTRIBUTE_UNUSED;
13557      segT asec;
13558      fragS *fragp;
13559 {
13560   int old, new;
13561   char *fixptr;
13562
13563   if (RELAX_BRANCH_P (fragp->fr_subtype))
13564     {
13565       bfd_byte *buf;
13566       unsigned long insn;
13567       expressionS exp;
13568       fixS *fixp;
13569
13570       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13571
13572       if (target_big_endian)
13573         insn = bfd_getb32 (buf);
13574       else
13575         insn = bfd_getl32 (buf);
13576
13577       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13578         {
13579           /* We generate a fixup instead of applying it right now
13580              because, if there are linker relaxations, we're going to
13581              need the relocations.  */
13582           exp.X_op = O_symbol;
13583           exp.X_add_symbol = fragp->fr_symbol;
13584           exp.X_add_number = fragp->fr_offset;
13585
13586           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13587                               4, &exp, 1,
13588                               BFD_RELOC_16_PCREL_S2);
13589           fixp->fx_file = fragp->fr_file;
13590           fixp->fx_line = fragp->fr_line;
13591
13592           md_number_to_chars ((char *)buf, insn, 4);
13593           buf += 4;
13594         }
13595       else
13596         {
13597           int i;
13598
13599           as_warn_where (fragp->fr_file, fragp->fr_line,
13600                          _("relaxed out-of-range branch into a jump"));
13601
13602           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13603             goto uncond;
13604
13605           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13606             {
13607               /* Reverse the branch.  */
13608               switch ((insn >> 28) & 0xf)
13609                 {
13610                 case 4:
13611                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13612                      have the condition reversed by tweaking a single
13613                      bit, and their opcodes all have 0x4???????.  */
13614                   assert ((insn & 0xf1000000) == 0x41000000);
13615                   insn ^= 0x00010000;
13616                   break;
13617
13618                 case 0:
13619                   /* bltz       0x04000000      bgez    0x04010000
13620                      bltzal     0x04100000      bgezal  0x04110000 */
13621                   assert ((insn & 0xfc0e0000) == 0x04000000);
13622                   insn ^= 0x00010000;
13623                   break;
13624
13625                 case 1:
13626                   /* beq        0x10000000      bne     0x14000000
13627                      blez       0x18000000      bgtz    0x1c000000 */
13628                   insn ^= 0x04000000;
13629                   break;
13630
13631                 default:
13632                   abort ();
13633                 }
13634             }
13635
13636           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13637             {
13638               /* Clear the and-link bit.  */
13639               assert ((insn & 0xfc1c0000) == 0x04100000);
13640
13641               /* bltzal 0x04100000      bgezal  0x04110000
13642                 bltzall 0x04120000     bgezall  0x04130000 */
13643               insn &= ~0x00100000;
13644             }
13645
13646           /* Branch over the branch (if the branch was likely) or the
13647              full jump (not likely case).  Compute the offset from the
13648              current instruction to branch to.  */
13649           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13650             i = 16;
13651           else
13652             {
13653               /* How many bytes in instructions we've already emitted?  */
13654               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13655               /* How many bytes in instructions from here to the end?  */
13656               i = fragp->fr_var - i;
13657             }
13658           /* Convert to instruction count.  */
13659           i >>= 2;
13660           /* Branch counts from the next instruction.  */
13661           i--;
13662           insn |= i;
13663           /* Branch over the jump.  */
13664           md_number_to_chars ((char *)buf, insn, 4);
13665           buf += 4;
13666
13667           /* Nop */
13668           md_number_to_chars ((char*)buf, 0, 4);
13669           buf += 4;
13670
13671           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13672             {
13673               /* beql $0, $0, 2f */
13674               insn = 0x50000000;
13675               /* Compute the PC offset from the current instruction to
13676                  the end of the variable frag.  */
13677               /* How many bytes in instructions we've already emitted?  */
13678               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13679               /* How many bytes in instructions from here to the end?  */
13680               i = fragp->fr_var - i;
13681               /* Convert to instruction count.  */
13682               i >>= 2;
13683               /* Don't decrement i, because we want to branch over the
13684                  delay slot.  */
13685
13686               insn |= i;
13687               md_number_to_chars ((char *)buf, insn, 4);
13688               buf += 4;
13689
13690               md_number_to_chars ((char *)buf, 0, 4);
13691               buf += 4;
13692             }
13693
13694         uncond:
13695           if (mips_pic == NO_PIC)
13696             {
13697               /* j or jal.  */
13698               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13699                       ? 0x0c000000 : 0x08000000);
13700               exp.X_op = O_symbol;
13701               exp.X_add_symbol = fragp->fr_symbol;
13702               exp.X_add_number = fragp->fr_offset;
13703
13704               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13705                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13706               fixp->fx_file = fragp->fr_file;
13707               fixp->fx_line = fragp->fr_line;
13708
13709               md_number_to_chars ((char*)buf, insn, 4);
13710               buf += 4;
13711             }
13712           else
13713             {
13714               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13715               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13716               exp.X_op = O_symbol;
13717               exp.X_add_symbol = fragp->fr_symbol;
13718               exp.X_add_number = fragp->fr_offset;
13719
13720               if (fragp->fr_offset)
13721                 {
13722                   exp.X_add_symbol = make_expr_symbol (&exp);
13723                   exp.X_add_number = 0;
13724                 }
13725
13726               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13727                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13728               fixp->fx_file = fragp->fr_file;
13729               fixp->fx_line = fragp->fr_line;
13730
13731               md_number_to_chars ((char*)buf, insn, 4);
13732               buf += 4;
13733
13734               if (mips_opts.isa == ISA_MIPS1)
13735                 {
13736                   /* nop */
13737                   md_number_to_chars ((char*)buf, 0, 4);
13738                   buf += 4;
13739                 }
13740
13741               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13742               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13743
13744               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13745                                   4, &exp, 0, BFD_RELOC_LO16);
13746               fixp->fx_file = fragp->fr_file;
13747               fixp->fx_line = fragp->fr_line;
13748
13749               md_number_to_chars ((char*)buf, insn, 4);
13750               buf += 4;
13751
13752               /* j(al)r $at.  */
13753               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13754                 insn = 0x0020f809;
13755               else
13756                 insn = 0x00200008;
13757
13758               md_number_to_chars ((char*)buf, insn, 4);
13759               buf += 4;
13760             }
13761         }
13762
13763       assert (buf == (bfd_byte *)fragp->fr_literal
13764               + fragp->fr_fix + fragp->fr_var);
13765
13766       fragp->fr_fix += fragp->fr_var;
13767
13768       return;
13769     }
13770
13771   if (RELAX_MIPS16_P (fragp->fr_subtype))
13772     {
13773       int type;
13774       register const struct mips16_immed_operand *op;
13775       bfd_boolean small, ext;
13776       offsetT val;
13777       bfd_byte *buf;
13778       unsigned long insn;
13779       bfd_boolean use_extend;
13780       unsigned short extend;
13781
13782       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13783       op = mips16_immed_operands;
13784       while (op->type != type)
13785         ++op;
13786
13787       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13788         {
13789           small = FALSE;
13790           ext = TRUE;
13791         }
13792       else
13793         {
13794           small = TRUE;
13795           ext = FALSE;
13796         }
13797
13798       resolve_symbol_value (fragp->fr_symbol);
13799       val = S_GET_VALUE (fragp->fr_symbol);
13800       if (op->pcrel)
13801         {
13802           addressT addr;
13803
13804           addr = fragp->fr_address + fragp->fr_fix;
13805
13806           /* The rules for the base address of a PC relative reloc are
13807              complicated; see mips16_extended_frag.  */
13808           if (type == 'p' || type == 'q')
13809             {
13810               addr += 2;
13811               if (ext)
13812                 addr += 2;
13813               /* Ignore the low bit in the target, since it will be
13814                  set for a text label.  */
13815               if ((val & 1) != 0)
13816                 --val;
13817             }
13818           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13819             addr -= 4;
13820           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13821             addr -= 2;
13822
13823           addr &= ~ (addressT) ((1 << op->shift) - 1);
13824           val -= addr;
13825
13826           /* Make sure the section winds up with the alignment we have
13827              assumed.  */
13828           if (op->shift > 0)
13829             record_alignment (asec, op->shift);
13830         }
13831
13832       if (ext
13833           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13834               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13835         as_warn_where (fragp->fr_file, fragp->fr_line,
13836                        _("extended instruction in delay slot"));
13837
13838       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13839
13840       if (target_big_endian)
13841         insn = bfd_getb16 (buf);
13842       else
13843         insn = bfd_getl16 (buf);
13844
13845       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13846                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13847                     small, ext, &insn, &use_extend, &extend);
13848
13849       if (use_extend)
13850         {
13851           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13852           fragp->fr_fix += 2;
13853           buf += 2;
13854         }
13855
13856       md_number_to_chars ((char *) buf, insn, 2);
13857       fragp->fr_fix += 2;
13858       buf += 2;
13859     }
13860   else
13861     {
13862       if (fragp->fr_opcode == NULL)
13863         return;
13864
13865       old = RELAX_OLD (fragp->fr_subtype);
13866       new = RELAX_NEW (fragp->fr_subtype);
13867       fixptr = fragp->fr_literal + fragp->fr_fix;
13868
13869       if (new > 0)
13870         memcpy (fixptr - old, fixptr, new);
13871
13872       fragp->fr_fix += new - old;
13873     }
13874 }
13875
13876 #ifdef OBJ_ELF
13877
13878 /* This function is called after the relocs have been generated.
13879    We've been storing mips16 text labels as odd.  Here we convert them
13880    back to even for the convenience of the debugger.  */
13881
13882 void
13883 mips_frob_file_after_relocs ()
13884 {
13885   asymbol **syms;
13886   unsigned int count, i;
13887
13888   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13889     return;
13890
13891   syms = bfd_get_outsymbols (stdoutput);
13892   count = bfd_get_symcount (stdoutput);
13893   for (i = 0; i < count; i++, syms++)
13894     {
13895       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13896           && ((*syms)->value & 1) != 0)
13897         {
13898           (*syms)->value &= ~1;
13899           /* If the symbol has an odd size, it was probably computed
13900              incorrectly, so adjust that as well.  */
13901           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13902             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13903         }
13904     }
13905 }
13906
13907 #endif
13908
13909 /* This function is called whenever a label is defined.  It is used
13910    when handling branch delays; if a branch has a label, we assume we
13911    can not move it.  */
13912
13913 void
13914 mips_define_label (sym)
13915      symbolS *sym;
13916 {
13917   struct insn_label_list *l;
13918
13919   if (free_insn_labels == NULL)
13920     l = (struct insn_label_list *) xmalloc (sizeof *l);
13921   else
13922     {
13923       l = free_insn_labels;
13924       free_insn_labels = l->next;
13925     }
13926
13927   l->label = sym;
13928   l->next = insn_labels;
13929   insn_labels = l;
13930 }
13931 \f
13932 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13933
13934 /* Some special processing for a MIPS ELF file.  */
13935
13936 void
13937 mips_elf_final_processing ()
13938 {
13939   /* Write out the register information.  */
13940   if (mips_abi != N64_ABI)
13941     {
13942       Elf32_RegInfo s;
13943
13944       s.ri_gprmask = mips_gprmask;
13945       s.ri_cprmask[0] = mips_cprmask[0];
13946       s.ri_cprmask[1] = mips_cprmask[1];
13947       s.ri_cprmask[2] = mips_cprmask[2];
13948       s.ri_cprmask[3] = mips_cprmask[3];
13949       /* The gp_value field is set by the MIPS ELF backend.  */
13950
13951       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13952                                        ((Elf32_External_RegInfo *)
13953                                         mips_regmask_frag));
13954     }
13955   else
13956     {
13957       Elf64_Internal_RegInfo s;
13958
13959       s.ri_gprmask = mips_gprmask;
13960       s.ri_pad = 0;
13961       s.ri_cprmask[0] = mips_cprmask[0];
13962       s.ri_cprmask[1] = mips_cprmask[1];
13963       s.ri_cprmask[2] = mips_cprmask[2];
13964       s.ri_cprmask[3] = mips_cprmask[3];
13965       /* The gp_value field is set by the MIPS ELF backend.  */
13966
13967       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13968                                        ((Elf64_External_RegInfo *)
13969                                         mips_regmask_frag));
13970     }
13971
13972   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13973      sort of BFD interface for this.  */
13974   if (mips_any_noreorder)
13975     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13976   if (mips_pic != NO_PIC)
13977     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13978
13979   /* Set MIPS ELF flags for ASEs.  */
13980   if (file_ase_mips16)
13981     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13982 #if 0 /* XXX FIXME */
13983   if (file_ase_mips3d)
13984     elf_elfheader (stdoutput)->e_flags |= ???;
13985 #endif
13986   if (file_ase_mdmx)
13987     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13988
13989   /* Set the MIPS ELF ABI flags.  */
13990   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13991     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13992   else if (mips_abi == O64_ABI)
13993     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13994   else if (mips_abi == EABI_ABI)
13995     {
13996       if (!file_mips_gp32)
13997         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13998       else
13999         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14000     }
14001   else if (mips_abi == N32_ABI)
14002     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14003
14004   /* Nothing to do for N64_ABI.  */
14005
14006   if (mips_32bitmode)
14007     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14008 }
14009
14010 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14011 \f
14012 typedef struct proc {
14013   symbolS *isym;
14014   unsigned long reg_mask;
14015   unsigned long reg_offset;
14016   unsigned long fpreg_mask;
14017   unsigned long fpreg_offset;
14018   unsigned long frame_offset;
14019   unsigned long frame_reg;
14020   unsigned long pc_reg;
14021 } procS;
14022
14023 static procS cur_proc;
14024 static procS *cur_proc_ptr;
14025 static int numprocs;
14026
14027 /* Fill in an rs_align_code fragment.  */
14028
14029 void
14030 mips_handle_align (fragp)
14031      fragS *fragp;
14032 {
14033   if (fragp->fr_type != rs_align_code)
14034     return;
14035
14036   if (mips_opts.mips16)
14037     {
14038       static const unsigned char be_nop[] = { 0x65, 0x00 };
14039       static const unsigned char le_nop[] = { 0x00, 0x65 };
14040
14041       int bytes;
14042       char *p;
14043
14044       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14045       p = fragp->fr_literal + fragp->fr_fix;
14046
14047       if (bytes & 1)
14048         {
14049           *p++ = 0;
14050           fragp->fr_fix++;
14051         }
14052
14053       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14054       fragp->fr_var = 2;
14055     }
14056
14057   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14058 }
14059
14060 static void
14061 md_obj_begin ()
14062 {
14063 }
14064
14065 static void
14066 md_obj_end ()
14067 {
14068   /* check for premature end, nesting errors, etc */
14069   if (cur_proc_ptr)
14070     as_warn (_("missing .end at end of assembly"));
14071 }
14072
14073 static long
14074 get_number ()
14075 {
14076   int negative = 0;
14077   long val = 0;
14078
14079   if (*input_line_pointer == '-')
14080     {
14081       ++input_line_pointer;
14082       negative = 1;
14083     }
14084   if (!ISDIGIT (*input_line_pointer))
14085     as_bad (_("expected simple number"));
14086   if (input_line_pointer[0] == '0')
14087     {
14088       if (input_line_pointer[1] == 'x')
14089         {
14090           input_line_pointer += 2;
14091           while (ISXDIGIT (*input_line_pointer))
14092             {
14093               val <<= 4;
14094               val |= hex_value (*input_line_pointer++);
14095             }
14096           return negative ? -val : val;
14097         }
14098       else
14099         {
14100           ++input_line_pointer;
14101           while (ISDIGIT (*input_line_pointer))
14102             {
14103               val <<= 3;
14104               val |= *input_line_pointer++ - '0';
14105             }
14106           return negative ? -val : val;
14107         }
14108     }
14109   if (!ISDIGIT (*input_line_pointer))
14110     {
14111       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14112               *input_line_pointer, *input_line_pointer);
14113       as_warn (_("invalid number"));
14114       return -1;
14115     }
14116   while (ISDIGIT (*input_line_pointer))
14117     {
14118       val *= 10;
14119       val += *input_line_pointer++ - '0';
14120     }
14121   return negative ? -val : val;
14122 }
14123
14124 /* The .file directive; just like the usual .file directive, but there
14125    is an initial number which is the ECOFF file index.  In the non-ECOFF
14126    case .file implies DWARF-2.  */
14127
14128 static void
14129 s_mips_file (x)
14130      int x ATTRIBUTE_UNUSED;
14131 {
14132   static int first_file_directive = 0;
14133
14134   if (ECOFF_DEBUGGING)
14135     {
14136       get_number ();
14137       s_app_file (0);
14138     }
14139   else
14140     {
14141       char *filename;
14142
14143       filename = dwarf2_directive_file (0);
14144
14145       /* Versions of GCC up to 3.1 start files with a ".file"
14146          directive even for stabs output.  Make sure that this
14147          ".file" is handled.  Note that you need a version of GCC
14148          after 3.1 in order to support DWARF-2 on MIPS.  */
14149       if (filename != NULL && ! first_file_directive)
14150         {
14151           (void) new_logical_line (filename, -1);
14152           s_app_file_string (filename);
14153         }
14154       first_file_directive = 1;
14155     }
14156 }
14157
14158 /* The .loc directive, implying DWARF-2.  */
14159
14160 static void
14161 s_mips_loc (x)
14162      int x ATTRIBUTE_UNUSED;
14163 {
14164   if (!ECOFF_DEBUGGING)
14165     dwarf2_directive_loc (0);
14166 }
14167
14168 /* The .end directive.  */
14169
14170 static void
14171 s_mips_end (x)
14172      int x ATTRIBUTE_UNUSED;
14173 {
14174   symbolS *p;
14175
14176   /* Following functions need their own .frame and .cprestore directives.  */
14177   mips_frame_reg_valid = 0;
14178   mips_cprestore_valid = 0;
14179
14180   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14181     {
14182       p = get_symbol ();
14183       demand_empty_rest_of_line ();
14184     }
14185   else
14186     p = NULL;
14187
14188   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14189     as_warn (_(".end not in text section"));
14190
14191   if (!cur_proc_ptr)
14192     {
14193       as_warn (_(".end directive without a preceding .ent directive."));
14194       demand_empty_rest_of_line ();
14195       return;
14196     }
14197
14198   if (p != NULL)
14199     {
14200       assert (S_GET_NAME (p));
14201       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14202         as_warn (_(".end symbol does not match .ent symbol."));
14203
14204       if (debug_type == DEBUG_STABS)
14205         stabs_generate_asm_endfunc (S_GET_NAME (p),
14206                                     S_GET_NAME (p));
14207     }
14208   else
14209     as_warn (_(".end directive missing or unknown symbol"));
14210
14211 #ifdef OBJ_ELF
14212   /* Generate a .pdr section.  */
14213   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14214     {
14215       segT saved_seg = now_seg;
14216       subsegT saved_subseg = now_subseg;
14217       valueT dot;
14218       expressionS exp;
14219       char *fragp;
14220
14221       dot = frag_now_fix ();
14222
14223 #ifdef md_flush_pending_output
14224       md_flush_pending_output ();
14225 #endif
14226
14227       assert (pdr_seg);
14228       subseg_set (pdr_seg, 0);
14229
14230       /* Write the symbol.  */
14231       exp.X_op = O_symbol;
14232       exp.X_add_symbol = p;
14233       exp.X_add_number = 0;
14234       emit_expr (&exp, 4);
14235
14236       fragp = frag_more (7 * 4);
14237
14238       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14239       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14240       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14241       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14242       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14243       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14244       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14245
14246       subseg_set (saved_seg, saved_subseg);
14247     }
14248 #endif /* OBJ_ELF */
14249
14250   cur_proc_ptr = NULL;
14251 }
14252
14253 /* The .aent and .ent directives.  */
14254
14255 static void
14256 s_mips_ent (aent)
14257      int aent;
14258 {
14259   symbolS *symbolP;
14260
14261   symbolP = get_symbol ();
14262   if (*input_line_pointer == ',')
14263     ++input_line_pointer;
14264   SKIP_WHITESPACE ();
14265   if (ISDIGIT (*input_line_pointer)
14266       || *input_line_pointer == '-')
14267     get_number ();
14268
14269   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14270     as_warn (_(".ent or .aent not in text section."));
14271
14272   if (!aent && cur_proc_ptr)
14273     as_warn (_("missing .end"));
14274
14275   if (!aent)
14276     {
14277       /* This function needs its own .frame and .cprestore directives.  */
14278       mips_frame_reg_valid = 0;
14279       mips_cprestore_valid = 0;
14280
14281       cur_proc_ptr = &cur_proc;
14282       memset (cur_proc_ptr, '\0', sizeof (procS));
14283
14284       cur_proc_ptr->isym = symbolP;
14285
14286       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14287
14288       ++numprocs;
14289
14290       if (debug_type == DEBUG_STABS)
14291         stabs_generate_asm_func (S_GET_NAME (symbolP),
14292                                  S_GET_NAME (symbolP));
14293     }
14294
14295   demand_empty_rest_of_line ();
14296 }
14297
14298 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14299    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14300    s_mips_frame is used so that we can set the PDR information correctly.
14301    We can't use the ecoff routines because they make reference to the ecoff
14302    symbol table (in the mdebug section).  */
14303
14304 static void
14305 s_mips_frame (ignore)
14306      int ignore ATTRIBUTE_UNUSED;
14307 {
14308 #ifdef OBJ_ELF
14309   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14310     {
14311       long val;
14312
14313       if (cur_proc_ptr == (procS *) NULL)
14314         {
14315           as_warn (_(".frame outside of .ent"));
14316           demand_empty_rest_of_line ();
14317           return;
14318         }
14319
14320       cur_proc_ptr->frame_reg = tc_get_register (1);
14321
14322       SKIP_WHITESPACE ();
14323       if (*input_line_pointer++ != ','
14324           || get_absolute_expression_and_terminator (&val) != ',')
14325         {
14326           as_warn (_("Bad .frame directive"));
14327           --input_line_pointer;
14328           demand_empty_rest_of_line ();
14329           return;
14330         }
14331
14332       cur_proc_ptr->frame_offset = val;
14333       cur_proc_ptr->pc_reg = tc_get_register (0);
14334
14335       demand_empty_rest_of_line ();
14336     }
14337   else
14338 #endif /* OBJ_ELF */
14339     s_ignore (ignore);
14340 }
14341
14342 /* The .fmask and .mask directives. If the mdebug section is present
14343    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14344    embedded targets, s_mips_mask is used so that we can set the PDR
14345    information correctly. We can't use the ecoff routines because they
14346    make reference to the ecoff symbol table (in the mdebug section).  */
14347
14348 static void
14349 s_mips_mask (reg_type)
14350      char reg_type;
14351 {
14352 #ifdef OBJ_ELF
14353   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14354     {
14355       long mask, off;
14356
14357       if (cur_proc_ptr == (procS *) NULL)
14358         {
14359           as_warn (_(".mask/.fmask outside of .ent"));
14360           demand_empty_rest_of_line ();
14361           return;
14362         }
14363
14364       if (get_absolute_expression_and_terminator (&mask) != ',')
14365         {
14366           as_warn (_("Bad .mask/.fmask directive"));
14367           --input_line_pointer;
14368           demand_empty_rest_of_line ();
14369           return;
14370         }
14371
14372       off = get_absolute_expression ();
14373
14374       if (reg_type == 'F')
14375         {
14376           cur_proc_ptr->fpreg_mask = mask;
14377           cur_proc_ptr->fpreg_offset = off;
14378         }
14379       else
14380         {
14381           cur_proc_ptr->reg_mask = mask;
14382           cur_proc_ptr->reg_offset = off;
14383         }
14384
14385       demand_empty_rest_of_line ();
14386     }
14387   else
14388 #endif /* OBJ_ELF */
14389     s_ignore (reg_type);
14390 }
14391
14392 /* The .loc directive.  */
14393
14394 #if 0
14395 static void
14396 s_loc (x)
14397      int x;
14398 {
14399   symbolS *symbolP;
14400   int lineno;
14401   int addroff;
14402
14403   assert (now_seg == text_section);
14404
14405   lineno = get_number ();
14406   addroff = frag_now_fix ();
14407
14408   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14409   S_SET_TYPE (symbolP, N_SLINE);
14410   S_SET_OTHER (symbolP, 0);
14411   S_SET_DESC (symbolP, lineno);
14412   symbolP->sy_segment = now_seg;
14413 }
14414 #endif
14415
14416 /* A table describing all the processors gas knows about.  Names are
14417    matched in the order listed.
14418
14419    To ease comparison, please keep this table in the same order as
14420    gcc's mips_cpu_info_table[].  */
14421 static const struct mips_cpu_info mips_cpu_info_table[] =
14422 {
14423   /* Entries for generic ISAs */
14424   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14425   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14426   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14427   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14428   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14429   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14430   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14431   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14432
14433   /* MIPS I */
14434   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14435   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14436   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14437
14438   /* MIPS II */
14439   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14440
14441   /* MIPS III */
14442   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14443   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14444   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14445   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14446   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14447   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14448   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14449   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14450   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14451   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14452   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14453   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14454
14455   /* MIPS IV */
14456   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14457   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14458   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14459   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14460   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14461   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14462   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14463   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14464   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14465   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14466   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14467   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14468
14469   /* MIPS 32 */
14470   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14471   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14472   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14473
14474   /* MIPS 64 */
14475   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14476   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14477
14478   /* Broadcom SB-1 CPU core */
14479   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14480
14481   /* End marker */
14482   { NULL, 0, 0, 0 }
14483 };
14484
14485
14486 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14487    with a final "000" replaced by "k".  Ignore case.
14488
14489    Note: this function is shared between GCC and GAS.  */
14490
14491 static bfd_boolean
14492 mips_strict_matching_cpu_name_p (canonical, given)
14493      const char *canonical, *given;
14494 {
14495   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14496     given++, canonical++;
14497
14498   return ((*given == 0 && *canonical == 0)
14499           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14500 }
14501
14502
14503 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14504    CPU name.  We've traditionally allowed a lot of variation here.
14505
14506    Note: this function is shared between GCC and GAS.  */
14507
14508 static bfd_boolean
14509 mips_matching_cpu_name_p (canonical, given)
14510      const char *canonical, *given;
14511 {
14512   /* First see if the name matches exactly, or with a final "000"
14513      turned into "k".  */
14514   if (mips_strict_matching_cpu_name_p (canonical, given))
14515     return TRUE;
14516
14517   /* If not, try comparing based on numerical designation alone.
14518      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14519   if (TOLOWER (*given) == 'r')
14520     given++;
14521   if (!ISDIGIT (*given))
14522     return FALSE;
14523
14524   /* Skip over some well-known prefixes in the canonical name,
14525      hoping to find a number there too.  */
14526   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14527     canonical += 2;
14528   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14529     canonical += 2;
14530   else if (TOLOWER (canonical[0]) == 'r')
14531     canonical += 1;
14532
14533   return mips_strict_matching_cpu_name_p (canonical, given);
14534 }
14535
14536
14537 /* Parse an option that takes the name of a processor as its argument.
14538    OPTION is the name of the option and CPU_STRING is the argument.
14539    Return the corresponding processor enumeration if the CPU_STRING is
14540    recognized, otherwise report an error and return null.
14541
14542    A similar function exists in GCC.  */
14543
14544 static const struct mips_cpu_info *
14545 mips_parse_cpu (option, cpu_string)
14546      const char *option, *cpu_string;
14547 {
14548   const struct mips_cpu_info *p;
14549
14550   /* 'from-abi' selects the most compatible architecture for the given
14551      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14552      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14553      version.  Look first at the -mgp options, if given, otherwise base
14554      the choice on MIPS_DEFAULT_64BIT.
14555
14556      Treat NO_ABI like the EABIs.  One reason to do this is that the
14557      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14558      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14559      'mips64', just as we did in the days before 'from-abi'.  */
14560   if (strcasecmp (cpu_string, "from-abi") == 0)
14561     {
14562       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14563         return mips_cpu_info_from_isa (ISA_MIPS1);
14564
14565       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14566         return mips_cpu_info_from_isa (ISA_MIPS3);
14567
14568       if (file_mips_gp32 >= 0)
14569         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14570
14571       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14572                                      ? ISA_MIPS3
14573                                      : ISA_MIPS1);
14574     }
14575
14576   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14577   if (strcasecmp (cpu_string, "default") == 0)
14578     return 0;
14579
14580   for (p = mips_cpu_info_table; p->name != 0; p++)
14581     if (mips_matching_cpu_name_p (p->name, cpu_string))
14582       return p;
14583
14584   as_bad ("Bad value (%s) for %s", cpu_string, option);
14585   return 0;
14586 }
14587
14588 /* Return the canonical processor information for ISA (a member of the
14589    ISA_MIPS* enumeration).  */
14590
14591 static const struct mips_cpu_info *
14592 mips_cpu_info_from_isa (isa)
14593      int isa;
14594 {
14595   int i;
14596
14597   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14598     if (mips_cpu_info_table[i].is_isa
14599         && isa == mips_cpu_info_table[i].isa)
14600       return (&mips_cpu_info_table[i]);
14601
14602   return NULL;
14603 }
14604 \f
14605 static void
14606 show (stream, string, col_p, first_p)
14607      FILE *stream;
14608      const char *string;
14609      int *col_p;
14610      int *first_p;
14611 {
14612   if (*first_p)
14613     {
14614       fprintf (stream, "%24s", "");
14615       *col_p = 24;
14616     }
14617   else
14618     {
14619       fprintf (stream, ", ");
14620       *col_p += 2;
14621     }
14622
14623   if (*col_p + strlen (string) > 72)
14624     {
14625       fprintf (stream, "\n%24s", "");
14626       *col_p = 24;
14627     }
14628
14629   fprintf (stream, "%s", string);
14630   *col_p += strlen (string);
14631
14632   *first_p = 0;
14633 }
14634
14635 void
14636 md_show_usage (stream)
14637      FILE *stream;
14638 {
14639   int column, first;
14640   size_t i;
14641
14642   fprintf (stream, _("\
14643 MIPS options:\n\
14644 -membedded-pic          generate embedded position independent code\n\
14645 -EB                     generate big endian output\n\
14646 -EL                     generate little endian output\n\
14647 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14648 -G NUM                  allow referencing objects up to NUM bytes\n\
14649                         implicitly with the gp register [default 8]\n"));
14650   fprintf (stream, _("\
14651 -mips1                  generate MIPS ISA I instructions\n\
14652 -mips2                  generate MIPS ISA II instructions\n\
14653 -mips3                  generate MIPS ISA III instructions\n\
14654 -mips4                  generate MIPS ISA IV instructions\n\
14655 -mips5                  generate MIPS ISA V instructions\n\
14656 -mips32                 generate MIPS32 ISA instructions\n\
14657 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14658 -mips64                 generate MIPS64 ISA instructions\n\
14659 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14660
14661   first = 1;
14662
14663   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14664     show (stream, mips_cpu_info_table[i].name, &column, &first);
14665   show (stream, "from-abi", &column, &first);
14666   fputc ('\n', stream);
14667
14668   fprintf (stream, _("\
14669 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14670 -no-mCPU                don't generate code specific to CPU.\n\
14671                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14672
14673   first = 1;
14674
14675   show (stream, "3900", &column, &first);
14676   show (stream, "4010", &column, &first);
14677   show (stream, "4100", &column, &first);
14678   show (stream, "4650", &column, &first);
14679   fputc ('\n', stream);
14680
14681   fprintf (stream, _("\
14682 -mips16                 generate mips16 instructions\n\
14683 -no-mips16              do not generate mips16 instructions\n"));
14684   fprintf (stream, _("\
14685 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14686 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14687 -O0                     remove unneeded NOPs, do not swap branches\n\
14688 -O                      remove unneeded NOPs and swap branches\n\
14689 -n                      warn about NOPs generated from macros\n\
14690 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14691 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14692 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14693 #ifdef OBJ_ELF
14694   fprintf (stream, _("\
14695 -KPIC, -call_shared     generate SVR4 position independent code\n\
14696 -non_shared             do not generate position independent code\n\
14697 -xgot                   assume a 32 bit GOT\n\
14698 -mabi=ABI               create ABI conformant object file for:\n"));
14699
14700   first = 1;
14701
14702   show (stream, "32", &column, &first);
14703   show (stream, "o64", &column, &first);
14704   show (stream, "n32", &column, &first);
14705   show (stream, "64", &column, &first);
14706   show (stream, "eabi", &column, &first);
14707
14708   fputc ('\n', stream);
14709
14710   fprintf (stream, _("\
14711 -32                     create o32 ABI object file (default)\n\
14712 -n32                    create n32 ABI object file\n\
14713 -64                     create 64 ABI object file\n"));
14714 #endif
14715 }
14716
14717 enum dwarf2_format
14718 mips_dwarf2_format ()
14719 {
14720   if (mips_abi == N64_ABI)
14721     {
14722 #ifdef TE_IRIX
14723       return dwarf2_format_64bit_irix;
14724 #else
14725       return dwarf2_format_64bit;
14726 #endif
14727     }
14728   else
14729     return dwarf2_format_32bit;
14730 }