* configure.in (MIPS_DEFAULT_ABI): AC_DEFINE.
[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   bfd_boolean force_new_frag = FALSE;
1635
1636   /* Mark instruction labels in mips16 mode.  */
1637   mips16_mark_labels ();
1638
1639   prev_pinfo = prev_insn.insn_mo->pinfo;
1640   pinfo = ip->insn_mo->pinfo;
1641
1642   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1643     {
1644       int prev_prev_nop;
1645
1646       /* If the previous insn required any delay slots, see if we need
1647          to insert a NOP or two.  There are eight kinds of possible
1648          hazards, of which an instruction can have at most one type.
1649          (1) a load from memory delay
1650          (2) a load from a coprocessor delay
1651          (3) an unconditional branch delay
1652          (4) a conditional branch delay
1653          (5) a move to coprocessor register delay
1654          (6) a load coprocessor register from memory delay
1655          (7) a coprocessor condition code delay
1656          (8) a HI/LO special register delay
1657
1658          There are a lot of optimizations we could do that we don't.
1659          In particular, we do not, in general, reorder instructions.
1660          If you use gcc with optimization, it will reorder
1661          instructions and generally do much more optimization then we
1662          do here; repeating all that work in the assembler would only
1663          benefit hand written assembly code, and does not seem worth
1664          it.  */
1665
1666       /* This is how a NOP is emitted.  */
1667 #define emit_nop()                                      \
1668   (mips_opts.mips16                                     \
1669    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1670    : md_number_to_chars (frag_more (4), 0, 4))
1671
1672       /* The previous insn might require a delay slot, depending upon
1673          the contents of the current insn.  */
1674       if (! mips_opts.mips16
1675           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1676           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1677                && ! cop_interlocks)
1678               || (! gpr_interlocks
1679                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1680         {
1681           /* A load from a coprocessor or from memory.  All load
1682              delays delay the use of general register rt for one
1683              instruction on the r3000.  The r6000 and r4000 use
1684              interlocks.  */
1685           /* Itbl support may require additional care here.  */
1686           know (prev_pinfo & INSN_WRITE_GPR_T);
1687           if (mips_optimize == 0
1688               || insn_uses_reg (ip,
1689                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1690                                  & OP_MASK_RT),
1691                                 MIPS_GR_REG))
1692             ++nops;
1693         }
1694       else if (! mips_opts.mips16
1695                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1696                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1697                     && ! cop_interlocks)
1698                    || (mips_opts.isa == ISA_MIPS1
1699                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1700         {
1701           /* A generic coprocessor delay.  The previous instruction
1702              modified a coprocessor general or control register.  If
1703              it modified a control register, we need to avoid any
1704              coprocessor instruction (this is probably not always
1705              required, but it sometimes is).  If it modified a general
1706              register, we avoid using that register.
1707
1708              On the r6000 and r4000 loading a coprocessor register
1709              from memory is interlocked, and does not require a delay.
1710
1711              This case is not handled very well.  There is no special
1712              knowledge of CP0 handling, and the coprocessors other
1713              than the floating point unit are not distinguished at
1714              all.  */
1715           /* Itbl support may require additional care here. FIXME!
1716              Need to modify this to include knowledge about
1717              user specified delays!  */
1718           if (prev_pinfo & INSN_WRITE_FPR_T)
1719             {
1720               if (mips_optimize == 0
1721                   || insn_uses_reg (ip,
1722                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1723                                      & OP_MASK_FT),
1724                                     MIPS_FP_REG))
1725                 ++nops;
1726             }
1727           else if (prev_pinfo & INSN_WRITE_FPR_S)
1728             {
1729               if (mips_optimize == 0
1730                   || insn_uses_reg (ip,
1731                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1732                                      & OP_MASK_FS),
1733                                     MIPS_FP_REG))
1734                 ++nops;
1735             }
1736           else
1737             {
1738               /* We don't know exactly what the previous instruction
1739                  does.  If the current instruction uses a coprocessor
1740                  register, we must insert a NOP.  If previous
1741                  instruction may set the condition codes, and the
1742                  current instruction uses them, we must insert two
1743                  NOPS.  */
1744               /* Itbl support may require additional care here.  */
1745               if (mips_optimize == 0
1746                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1747                       && (pinfo & INSN_READ_COND_CODE)))
1748                 nops += 2;
1749               else if (pinfo & INSN_COP)
1750                 ++nops;
1751             }
1752         }
1753       else if (! mips_opts.mips16
1754                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1755                && (prev_pinfo & INSN_WRITE_COND_CODE)
1756                && ! cop_interlocks)
1757         {
1758           /* The previous instruction sets the coprocessor condition
1759              codes, but does not require a general coprocessor delay
1760              (this means it is a floating point comparison
1761              instruction).  If this instruction uses the condition
1762              codes, we need to insert a single NOP.  */
1763           /* Itbl support may require additional care here.  */
1764           if (mips_optimize == 0
1765               || (pinfo & INSN_READ_COND_CODE))
1766             ++nops;
1767         }
1768
1769       /* If we're fixing up mfhi/mflo for the r7000 and the
1770          previous insn was an mfhi/mflo and the current insn
1771          reads the register that the mfhi/mflo wrote to, then
1772          insert two nops.  */
1773
1774       else if (mips_7000_hilo_fix
1775                && MF_HILO_INSN (prev_pinfo)
1776                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1777                                       & OP_MASK_RD),
1778                                  MIPS_GR_REG))
1779         {
1780           nops += 2;
1781         }
1782
1783       /* If we're fixing up mfhi/mflo for the r7000 and the
1784          2nd previous insn was an mfhi/mflo and the current insn
1785          reads the register that the mfhi/mflo wrote to, then
1786          insert one nop.  */
1787
1788       else if (mips_7000_hilo_fix
1789                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1790                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1791                                        & OP_MASK_RD),
1792                                     MIPS_GR_REG))
1793
1794         {
1795           ++nops;
1796         }
1797
1798       else if (prev_pinfo & INSN_READ_LO)
1799         {
1800           /* The previous instruction reads the LO register; if the
1801              current instruction writes to the LO register, we must
1802              insert two NOPS.  Some newer processors have interlocks.
1803              Also the tx39's multiply instructions can be exectuted
1804              immediatly after a read from HI/LO (without the delay),
1805              though the tx39's divide insns still do require the
1806              delay.  */
1807           if (! (hilo_interlocks
1808                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1809               && (mips_optimize == 0
1810                   || (pinfo & INSN_WRITE_LO)))
1811             nops += 2;
1812           /* Most mips16 branch insns don't have a delay slot.
1813              If a read from LO is immediately followed by a branch
1814              to a write to LO we have a read followed by a write
1815              less than 2 insns away.  We assume the target of
1816              a branch might be a write to LO, and insert a nop
1817              between a read and an immediately following branch.  */
1818           else if (mips_opts.mips16
1819                    && (mips_optimize == 0
1820                        || (pinfo & MIPS16_INSN_BRANCH)))
1821             ++nops;
1822         }
1823       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1824         {
1825           /* The previous instruction reads the HI register; if the
1826              current instruction writes to the HI register, we must
1827              insert a NOP.  Some newer processors have interlocks.
1828              Also the note tx39's multiply above.  */
1829           if (! (hilo_interlocks
1830                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1831               && (mips_optimize == 0
1832                   || (pinfo & INSN_WRITE_HI)))
1833             nops += 2;
1834           /* Most mips16 branch insns don't have a delay slot.
1835              If a read from HI is immediately followed by a branch
1836              to a write to HI we have a read followed by a write
1837              less than 2 insns away.  We assume the target of
1838              a branch might be a write to HI, and insert a nop
1839              between a read and an immediately following branch.  */
1840           else if (mips_opts.mips16
1841                    && (mips_optimize == 0
1842                        || (pinfo & MIPS16_INSN_BRANCH)))
1843             ++nops;
1844         }
1845
1846       /* If the previous instruction was in a noreorder section, then
1847          we don't want to insert the nop after all.  */
1848       /* Itbl support may require additional care here.  */
1849       if (prev_insn_unreordered)
1850         nops = 0;
1851
1852       /* There are two cases which require two intervening
1853          instructions: 1) setting the condition codes using a move to
1854          coprocessor instruction which requires a general coprocessor
1855          delay and then reading the condition codes 2) reading the HI
1856          or LO register and then writing to it (except on processors
1857          which have interlocks).  If we are not already emitting a NOP
1858          instruction, we must check for these cases compared to the
1859          instruction previous to the previous instruction.  */
1860       if ((! mips_opts.mips16
1861            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1862            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1863            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1864            && (pinfo & INSN_READ_COND_CODE)
1865            && ! cop_interlocks)
1866           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1867               && (pinfo & INSN_WRITE_LO)
1868               && ! (hilo_interlocks
1869                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1870           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1871               && (pinfo & INSN_WRITE_HI)
1872               && ! (hilo_interlocks
1873                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1874         prev_prev_nop = 1;
1875       else
1876         prev_prev_nop = 0;
1877
1878       if (prev_prev_insn_unreordered)
1879         prev_prev_nop = 0;
1880
1881       if (prev_prev_nop && nops == 0)
1882         ++nops;
1883
1884       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1885         {
1886           /* We're out of bits in pinfo, so we must resort to string
1887              ops here.  Shortcuts are selected based on opcodes being
1888              limited to the VR4122 instruction set.  */
1889           int min_nops = 0;
1890           const char *pn = prev_insn.insn_mo->name;
1891           const char *tn = ip->insn_mo->name;
1892           if (strncmp(pn, "macc", 4) == 0
1893               || strncmp(pn, "dmacc", 5) == 0)
1894             {
1895               /* Errata 21 - [D]DIV[U] after [D]MACC */
1896               if (strstr (tn, "div"))
1897                 {
1898                   min_nops = 1;
1899                 }
1900
1901               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1902               if (pn[0] == 'd' /* dmacc */
1903                   && (strncmp(tn, "dmult", 5) == 0
1904                       || strncmp(tn, "dmacc", 5) == 0))
1905                 {
1906                   min_nops = 1;
1907                 }
1908
1909               /* Errata 24 - MT{LO,HI} after [D]MACC */
1910               if (strcmp (tn, "mtlo") == 0
1911                   || strcmp (tn, "mthi") == 0)
1912                 {
1913                   min_nops = 1;
1914                 }
1915
1916             }
1917           else if (strncmp(pn, "dmult", 5) == 0
1918                    && (strncmp(tn, "dmult", 5) == 0
1919                        || strncmp(tn, "dmacc", 5) == 0))
1920             {
1921               /* Here is the rest of errata 23.  */
1922               min_nops = 1;
1923             }
1924           if (nops < min_nops)
1925             nops = min_nops;
1926         }
1927
1928       /* If we are being given a nop instruction, don't bother with
1929          one of the nops we would otherwise output.  This will only
1930          happen when a nop instruction is used with mips_optimize set
1931          to 0.  */
1932       if (nops > 0
1933           && ! mips_opts.noreorder
1934           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1935         --nops;
1936
1937       /* Now emit the right number of NOP instructions.  */
1938       if (nops > 0 && ! mips_opts.noreorder)
1939         {
1940           fragS *old_frag;
1941           unsigned long old_frag_offset;
1942           int i;
1943           struct insn_label_list *l;
1944
1945           old_frag = frag_now;
1946           old_frag_offset = frag_now_fix ();
1947
1948           for (i = 0; i < nops; i++)
1949             emit_nop ();
1950
1951           if (listing)
1952             {
1953               listing_prev_line ();
1954               /* We may be at the start of a variant frag.  In case we
1955                  are, make sure there is enough space for the frag
1956                  after the frags created by listing_prev_line.  The
1957                  argument to frag_grow here must be at least as large
1958                  as the argument to all other calls to frag_grow in
1959                  this file.  We don't have to worry about being in the
1960                  middle of a variant frag, because the variants insert
1961                  all needed nop instructions themselves.  */
1962               frag_grow (40);
1963             }
1964
1965           for (l = insn_labels; l != NULL; l = l->next)
1966             {
1967               valueT val;
1968
1969               assert (S_GET_SEGMENT (l->label) == now_seg);
1970               symbol_set_frag (l->label, frag_now);
1971               val = (valueT) frag_now_fix ();
1972               /* mips16 text labels are stored as odd.  */
1973               if (mips_opts.mips16)
1974                 ++val;
1975               S_SET_VALUE (l->label, val);
1976             }
1977
1978 #ifndef NO_ECOFF_DEBUGGING
1979           if (ECOFF_DEBUGGING)
1980             ecoff_fix_loc (old_frag, old_frag_offset);
1981 #endif
1982         }
1983       else if (prev_nop_frag != NULL)
1984         {
1985           /* We have a frag holding nops we may be able to remove.  If
1986              we don't need any nops, we can decrease the size of
1987              prev_nop_frag by the size of one instruction.  If we do
1988              need some nops, we count them in prev_nops_required.  */
1989           if (prev_nop_frag_since == 0)
1990             {
1991               if (nops == 0)
1992                 {
1993                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1994                   --prev_nop_frag_holds;
1995                 }
1996               else
1997                 prev_nop_frag_required += nops;
1998             }
1999           else
2000             {
2001               if (prev_prev_nop == 0)
2002                 {
2003                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2004                   --prev_nop_frag_holds;
2005                 }
2006               else
2007                 ++prev_nop_frag_required;
2008             }
2009
2010           if (prev_nop_frag_holds <= prev_nop_frag_required)
2011             prev_nop_frag = NULL;
2012
2013           ++prev_nop_frag_since;
2014
2015           /* Sanity check: by the time we reach the second instruction
2016              after prev_nop_frag, we should have used up all the nops
2017              one way or another.  */
2018           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2019         }
2020     }
2021
2022   if (place == NULL
2023       && address_expr
2024       && *reloc_type == BFD_RELOC_16_PCREL_S2
2025       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2026           || pinfo & INSN_COND_BRANCH_LIKELY)
2027       && mips_relax_branch
2028       /* Don't try branch relaxation within .set nomacro, or within
2029          .set noat if we use $at for PIC computations.  If it turns
2030          out that the branch was out-of-range, we'll get an error.  */
2031       && !mips_opts.warn_about_macros
2032       && !(mips_opts.noat && mips_pic != NO_PIC)
2033       && !mips_opts.mips16)
2034     {
2035       f = frag_var (rs_machine_dependent,
2036                     relaxed_branch_length
2037                     (NULL, NULL,
2038                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2039                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2040                     RELAX_BRANCH_ENCODE
2041                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
2042                      pinfo & INSN_COND_BRANCH_LIKELY,
2043                      pinfo & INSN_WRITE_GPR_31,
2044                      0),
2045                     address_expr->X_add_symbol,
2046                     address_expr->X_add_number,
2047                     0);
2048       *reloc_type = BFD_RELOC_UNUSED;
2049     }
2050   else if (*reloc_type > BFD_RELOC_UNUSED)
2051     {
2052       /* We need to set up a variant frag.  */
2053       assert (mips_opts.mips16 && address_expr != NULL);
2054       f = frag_var (rs_machine_dependent, 4, 0,
2055                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2056                                          mips16_small, mips16_ext,
2057                                          (prev_pinfo
2058                                           & INSN_UNCOND_BRANCH_DELAY),
2059                                          (*prev_insn_reloc_type
2060                                           == BFD_RELOC_MIPS16_JMP)),
2061                     make_expr_symbol (address_expr), 0, NULL);
2062     }
2063   else if (place != NULL)
2064     f = place;
2065   else if (mips_opts.mips16
2066            && ! ip->use_extend
2067            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2068     {
2069       /* Make sure there is enough room to swap this instruction with
2070          a following jump instruction.  */
2071       frag_grow (6);
2072       f = frag_more (2);
2073     }
2074   else
2075     {
2076       if (mips_opts.mips16
2077           && mips_opts.noreorder
2078           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2079         as_warn (_("extended instruction in delay slot"));
2080
2081       f = frag_more (4);
2082     }
2083
2084   fixp[0] = fixp[1] = fixp[2] = NULL;
2085   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2086     {
2087       if (address_expr->X_op == O_constant)
2088         {
2089           valueT tmp;
2090
2091           switch (*reloc_type)
2092             {
2093             case BFD_RELOC_32:
2094               ip->insn_opcode |= address_expr->X_add_number;
2095               break;
2096
2097             case BFD_RELOC_MIPS_HIGHEST:
2098               tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2099               tmp >>= 16;
2100               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2101               break;
2102
2103             case BFD_RELOC_MIPS_HIGHER:
2104               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2105               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2106               break;
2107
2108             case BFD_RELOC_HI16_S:
2109               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2110                                   >> 16) & 0xffff;
2111               break;
2112
2113             case BFD_RELOC_HI16:
2114               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2115               break;
2116
2117             case BFD_RELOC_LO16:
2118             case BFD_RELOC_MIPS_GOT_DISP:
2119               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2120               break;
2121
2122             case BFD_RELOC_MIPS_JMP:
2123               if ((address_expr->X_add_number & 3) != 0)
2124                 as_bad (_("jump to misaligned address (0x%lx)"),
2125                         (unsigned long) address_expr->X_add_number);
2126               if (address_expr->X_add_number & ~0xfffffff)
2127                 as_bad (_("jump address range overflow (0x%lx)"),
2128                         (unsigned long) address_expr->X_add_number);
2129               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2130               break;
2131
2132             case BFD_RELOC_MIPS16_JMP:
2133               if ((address_expr->X_add_number & 3) != 0)
2134                 as_bad (_("jump to misaligned address (0x%lx)"),
2135                         (unsigned long) address_expr->X_add_number);
2136               if (address_expr->X_add_number & ~0xfffffff)
2137                 as_bad (_("jump address range overflow (0x%lx)"),
2138                         (unsigned long) address_expr->X_add_number);
2139               ip->insn_opcode |=
2140                 (((address_expr->X_add_number & 0x7c0000) << 3)
2141                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2142                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2143               break;
2144
2145             case BFD_RELOC_16_PCREL_S2:
2146               goto need_reloc;
2147
2148             default:
2149               internalError ();
2150             }
2151         }
2152       else
2153         {
2154         need_reloc:
2155           /* Don't generate a reloc if we are writing into a variant frag.  */
2156           if (place == NULL)
2157             {
2158               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2159                                      address_expr,
2160                                      *reloc_type == BFD_RELOC_16_PCREL_S2,
2161                                      reloc_type[0]);
2162
2163               /* These relocations can have an addend that won't fit in
2164                  4 octets for 64bit assembly.  */
2165               if (HAVE_64BIT_GPRS &&
2166                   (*reloc_type == BFD_RELOC_16
2167                    || *reloc_type == BFD_RELOC_32
2168                    || *reloc_type == BFD_RELOC_MIPS_JMP
2169                    || *reloc_type == BFD_RELOC_HI16_S
2170                    || *reloc_type == BFD_RELOC_LO16
2171                    || *reloc_type == BFD_RELOC_GPREL16
2172                    || *reloc_type == BFD_RELOC_MIPS_LITERAL
2173                    || *reloc_type == BFD_RELOC_GPREL32
2174                    || *reloc_type == BFD_RELOC_64
2175                    || *reloc_type == BFD_RELOC_CTOR
2176                    || *reloc_type == BFD_RELOC_MIPS_SUB
2177                    || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2178                    || *reloc_type == BFD_RELOC_MIPS_HIGHER
2179                    || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2180                    || *reloc_type == BFD_RELOC_MIPS_REL16
2181                    || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2182                 fixp[0]->fx_no_overflow = 1;
2183
2184               if (reloc_needs_lo_p (*reloc_type))
2185                 {
2186                   struct mips_hi_fixup *hi_fixup;
2187
2188                   /* Reuse the last entry if it already has a matching %lo.  */
2189                   hi_fixup = mips_hi_fixup_list;
2190                   if (hi_fixup == 0
2191                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2192                     {
2193                       hi_fixup = ((struct mips_hi_fixup *)
2194                                   xmalloc (sizeof (struct mips_hi_fixup)));
2195                       hi_fixup->next = mips_hi_fixup_list;
2196                       mips_hi_fixup_list = hi_fixup;
2197                     }
2198                   hi_fixup->fixp = fixp[0];
2199                   hi_fixup->seg = now_seg;
2200                 }
2201
2202               if (reloc_type[1] != BFD_RELOC_UNUSED)
2203                 {
2204                   /* FIXME: This symbol can be one of
2205                      RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC.  */
2206                   address_expr->X_op = O_absent;
2207                   address_expr->X_add_symbol = 0;
2208                   address_expr->X_add_number = 0;
2209
2210                   fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2211                                          4, address_expr, FALSE,
2212                                          reloc_type[1]);
2213
2214                   /* These relocations can have an addend that won't fit in
2215                      4 octets for 64bit assembly.  */
2216                   if (HAVE_64BIT_GPRS &&
2217                       (*reloc_type == BFD_RELOC_16
2218                        || *reloc_type == BFD_RELOC_32
2219                        || *reloc_type == BFD_RELOC_MIPS_JMP
2220                        || *reloc_type == BFD_RELOC_HI16_S
2221                        || *reloc_type == BFD_RELOC_LO16
2222                        || *reloc_type == BFD_RELOC_GPREL16
2223                        || *reloc_type == BFD_RELOC_MIPS_LITERAL
2224                        || *reloc_type == BFD_RELOC_GPREL32
2225                        || *reloc_type == BFD_RELOC_64
2226                        || *reloc_type == BFD_RELOC_CTOR
2227                        || *reloc_type == BFD_RELOC_MIPS_SUB
2228                        || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2229                        || *reloc_type == BFD_RELOC_MIPS_HIGHER
2230                        || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2231                        || *reloc_type == BFD_RELOC_MIPS_REL16
2232                        || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2233                     fixp[1]->fx_no_overflow = 1;
2234
2235                   if (reloc_type[2] != BFD_RELOC_UNUSED)
2236                     {
2237                       address_expr->X_op = O_absent;
2238                       address_expr->X_add_symbol = 0;
2239                       address_expr->X_add_number = 0;
2240
2241                       fixp[2] = fix_new_exp (frag_now,
2242                                              f - frag_now->fr_literal, 4,
2243                                              address_expr, FALSE,
2244                                              reloc_type[2]);
2245
2246                       /* These relocations can have an addend that won't fit in
2247                          4 octets for 64bit assembly.  */
2248                       if (HAVE_64BIT_GPRS &&
2249                           (*reloc_type == BFD_RELOC_16
2250                            || *reloc_type == BFD_RELOC_32
2251                            || *reloc_type == BFD_RELOC_MIPS_JMP
2252                            || *reloc_type == BFD_RELOC_HI16_S
2253                            || *reloc_type == BFD_RELOC_LO16
2254                            || *reloc_type == BFD_RELOC_GPREL16
2255                            || *reloc_type == BFD_RELOC_MIPS_LITERAL
2256                            || *reloc_type == BFD_RELOC_GPREL32
2257                            || *reloc_type == BFD_RELOC_64
2258                            || *reloc_type == BFD_RELOC_CTOR
2259                            || *reloc_type == BFD_RELOC_MIPS_SUB
2260                            || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2261                            || *reloc_type == BFD_RELOC_MIPS_HIGHER
2262                            || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2263                            || *reloc_type == BFD_RELOC_MIPS_REL16
2264                            || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2265                         fixp[2]->fx_no_overflow = 1;
2266                     }
2267                 }
2268             }
2269         }
2270     }
2271
2272   if (! mips_opts.mips16)
2273     {
2274       md_number_to_chars (f, ip->insn_opcode, 4);
2275 #ifdef OBJ_ELF
2276       dwarf2_emit_insn (4);
2277 #endif
2278     }
2279   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2280     {
2281       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2282       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2283 #ifdef OBJ_ELF
2284       dwarf2_emit_insn (4);
2285 #endif
2286     }
2287   else
2288     {
2289       if (ip->use_extend)
2290         {
2291           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2292           f += 2;
2293         }
2294       md_number_to_chars (f, ip->insn_opcode, 2);
2295 #ifdef OBJ_ELF
2296       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2297 #endif
2298     }
2299
2300   /* Update the register mask information.  */
2301   if (! mips_opts.mips16)
2302     {
2303       if (pinfo & INSN_WRITE_GPR_D)
2304         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2305       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2306         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2307       if (pinfo & INSN_READ_GPR_S)
2308         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2309       if (pinfo & INSN_WRITE_GPR_31)
2310         mips_gprmask |= 1 << RA;
2311       if (pinfo & INSN_WRITE_FPR_D)
2312         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2313       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2314         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2315       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2316         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2317       if ((pinfo & INSN_READ_FPR_R) != 0)
2318         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2319       if (pinfo & INSN_COP)
2320         {
2321           /* We don't keep enough information to sort these cases out.
2322              The itbl support does keep this information however, although
2323              we currently don't support itbl fprmats as part of the cop
2324              instruction.  May want to add this support in the future.  */
2325         }
2326       /* Never set the bit for $0, which is always zero.  */
2327       mips_gprmask &= ~1 << 0;
2328     }
2329   else
2330     {
2331       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2332         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2333                               & MIPS16OP_MASK_RX);
2334       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2335         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2336                               & MIPS16OP_MASK_RY);
2337       if (pinfo & MIPS16_INSN_WRITE_Z)
2338         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2339                               & MIPS16OP_MASK_RZ);
2340       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2341         mips_gprmask |= 1 << TREG;
2342       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2343         mips_gprmask |= 1 << SP;
2344       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2345         mips_gprmask |= 1 << RA;
2346       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2347         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2348       if (pinfo & MIPS16_INSN_READ_Z)
2349         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2350                               & MIPS16OP_MASK_MOVE32Z);
2351       if (pinfo & MIPS16_INSN_READ_GPR_X)
2352         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2353                               & MIPS16OP_MASK_REGR32);
2354     }
2355
2356   if (place == NULL && ! mips_opts.noreorder)
2357     {
2358       /* Filling the branch delay slot is more complex.  We try to
2359          switch the branch with the previous instruction, which we can
2360          do if the previous instruction does not set up a condition
2361          that the branch tests and if the branch is not itself the
2362          target of any branch.  */
2363       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2364           || (pinfo & INSN_COND_BRANCH_DELAY))
2365         {
2366           if (mips_optimize < 2
2367               /* If we have seen .set volatile or .set nomove, don't
2368                  optimize.  */
2369               || mips_opts.nomove != 0
2370               /* If we had to emit any NOP instructions, then we
2371                  already know we can not swap.  */
2372               || nops != 0
2373               /* If we don't even know the previous insn, we can not
2374                  swap.  */
2375               || ! prev_insn_valid
2376               /* If the previous insn is already in a branch delay
2377                  slot, then we can not swap.  */
2378               || prev_insn_is_delay_slot
2379               /* If the previous previous insn was in a .set
2380                  noreorder, we can't swap.  Actually, the MIPS
2381                  assembler will swap in this situation.  However, gcc
2382                  configured -with-gnu-as will generate code like
2383                    .set noreorder
2384                    lw   $4,XXX
2385                    .set reorder
2386                    INSN
2387                    bne  $4,$0,foo
2388                  in which we can not swap the bne and INSN.  If gcc is
2389                  not configured -with-gnu-as, it does not output the
2390                  .set pseudo-ops.  We don't have to check
2391                  prev_insn_unreordered, because prev_insn_valid will
2392                  be 0 in that case.  We don't want to use
2393                  prev_prev_insn_valid, because we do want to be able
2394                  to swap at the start of a function.  */
2395               || prev_prev_insn_unreordered
2396               /* If the branch is itself the target of a branch, we
2397                  can not swap.  We cheat on this; all we check for is
2398                  whether there is a label on this instruction.  If
2399                  there are any branches to anything other than a
2400                  label, users must use .set noreorder.  */
2401               || insn_labels != NULL
2402               /* If the previous instruction is in a variant frag, we
2403                  can not do the swap.  This does not apply to the
2404                  mips16, which uses variant frags for different
2405                  purposes.  */
2406               || (! mips_opts.mips16
2407                   && prev_insn_frag->fr_type == rs_machine_dependent)
2408               /* If the branch reads the condition codes, we don't
2409                  even try to swap, because in the sequence
2410                    ctc1 $X,$31
2411                    INSN
2412                    INSN
2413                    bc1t LABEL
2414                  we can not swap, and I don't feel like handling that
2415                  case.  */
2416               || (! mips_opts.mips16
2417                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2418                   && (pinfo & INSN_READ_COND_CODE))
2419               /* We can not swap with an instruction that requires a
2420                  delay slot, becase the target of the branch might
2421                  interfere with that instruction.  */
2422               || (! mips_opts.mips16
2423                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2424                   && (prev_pinfo
2425               /* Itbl support may require additional care here.  */
2426                       & (INSN_LOAD_COPROC_DELAY
2427                          | INSN_COPROC_MOVE_DELAY
2428                          | INSN_WRITE_COND_CODE)))
2429               || (! (hilo_interlocks
2430                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2431                   && (prev_pinfo
2432                       & (INSN_READ_LO
2433                          | INSN_READ_HI)))
2434               || (! mips_opts.mips16
2435                   && ! gpr_interlocks
2436                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2437               || (! mips_opts.mips16
2438                   && mips_opts.isa == ISA_MIPS1
2439                   /* Itbl support may require additional care here.  */
2440                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2441               /* We can not swap with a branch instruction.  */
2442               || (prev_pinfo
2443                   & (INSN_UNCOND_BRANCH_DELAY
2444                      | INSN_COND_BRANCH_DELAY
2445                      | INSN_COND_BRANCH_LIKELY))
2446               /* We do not swap with a trap instruction, since it
2447                  complicates trap handlers to have the trap
2448                  instruction be in a delay slot.  */
2449               || (prev_pinfo & INSN_TRAP)
2450               /* If the branch reads a register that the previous
2451                  instruction sets, we can not swap.  */
2452               || (! mips_opts.mips16
2453                   && (prev_pinfo & INSN_WRITE_GPR_T)
2454                   && insn_uses_reg (ip,
2455                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2456                                      & OP_MASK_RT),
2457                                     MIPS_GR_REG))
2458               || (! mips_opts.mips16
2459                   && (prev_pinfo & INSN_WRITE_GPR_D)
2460                   && insn_uses_reg (ip,
2461                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2462                                      & OP_MASK_RD),
2463                                     MIPS_GR_REG))
2464               || (mips_opts.mips16
2465                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2466                        && insn_uses_reg (ip,
2467                                          ((prev_insn.insn_opcode
2468                                            >> MIPS16OP_SH_RX)
2469                                           & MIPS16OP_MASK_RX),
2470                                          MIPS16_REG))
2471                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2472                           && insn_uses_reg (ip,
2473                                             ((prev_insn.insn_opcode
2474                                               >> MIPS16OP_SH_RY)
2475                                              & MIPS16OP_MASK_RY),
2476                                             MIPS16_REG))
2477                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2478                           && insn_uses_reg (ip,
2479                                             ((prev_insn.insn_opcode
2480                                               >> MIPS16OP_SH_RZ)
2481                                              & MIPS16OP_MASK_RZ),
2482                                             MIPS16_REG))
2483                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2484                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2485                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2486                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2487                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2488                           && insn_uses_reg (ip,
2489                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2490                                                                      insn_opcode),
2491                                             MIPS_GR_REG))))
2492               /* If the branch writes a register that the previous
2493                  instruction sets, we can not swap (we know that
2494                  branches write only to RD or to $31).  */
2495               || (! mips_opts.mips16
2496                   && (prev_pinfo & INSN_WRITE_GPR_T)
2497                   && (((pinfo & INSN_WRITE_GPR_D)
2498                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2499                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2500                       || ((pinfo & INSN_WRITE_GPR_31)
2501                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2502                                & OP_MASK_RT)
2503                               == RA))))
2504               || (! mips_opts.mips16
2505                   && (prev_pinfo & INSN_WRITE_GPR_D)
2506                   && (((pinfo & INSN_WRITE_GPR_D)
2507                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2508                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2509                       || ((pinfo & INSN_WRITE_GPR_31)
2510                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2511                                & OP_MASK_RD)
2512                               == RA))))
2513               || (mips_opts.mips16
2514                   && (pinfo & MIPS16_INSN_WRITE_31)
2515                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2516                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2517                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2518                               == RA))))
2519               /* If the branch writes a register that the previous
2520                  instruction reads, we can not swap (we know that
2521                  branches only write to RD or to $31).  */
2522               || (! mips_opts.mips16
2523                   && (pinfo & INSN_WRITE_GPR_D)
2524                   && insn_uses_reg (&prev_insn,
2525                                     ((ip->insn_opcode >> OP_SH_RD)
2526                                      & OP_MASK_RD),
2527                                     MIPS_GR_REG))
2528               || (! mips_opts.mips16
2529                   && (pinfo & INSN_WRITE_GPR_31)
2530                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2531               || (mips_opts.mips16
2532                   && (pinfo & MIPS16_INSN_WRITE_31)
2533                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2534               /* If we are generating embedded PIC code, the branch
2535                  might be expanded into a sequence which uses $at, so
2536                  we can't swap with an instruction which reads it.  */
2537               || (mips_pic == EMBEDDED_PIC
2538                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2539               /* If the previous previous instruction has a load
2540                  delay, and sets a register that the branch reads, we
2541                  can not swap.  */
2542               || (! mips_opts.mips16
2543                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2544               /* Itbl support may require additional care here.  */
2545                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2546                       || (! gpr_interlocks
2547                           && (prev_prev_insn.insn_mo->pinfo
2548                               & INSN_LOAD_MEMORY_DELAY)))
2549                   && insn_uses_reg (ip,
2550                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2551                                      & OP_MASK_RT),
2552                                     MIPS_GR_REG))
2553               /* If one instruction sets a condition code and the
2554                  other one uses a condition code, we can not swap.  */
2555               || ((pinfo & INSN_READ_COND_CODE)
2556                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2557               || ((pinfo & INSN_WRITE_COND_CODE)
2558                   && (prev_pinfo & INSN_READ_COND_CODE))
2559               /* If the previous instruction uses the PC, we can not
2560                  swap.  */
2561               || (mips_opts.mips16
2562                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2563               /* If the previous instruction was extended, we can not
2564                  swap.  */
2565               || (mips_opts.mips16 && prev_insn_extended)
2566               /* If the previous instruction had a fixup in mips16
2567                  mode, we can not swap.  This normally means that the
2568                  previous instruction was a 4 byte branch anyhow.  */
2569               || (mips_opts.mips16 && prev_insn_fixp[0])
2570               /* If the previous instruction is a sync, sync.l, or
2571                  sync.p, we can not swap.  */
2572               || (prev_pinfo & INSN_SYNC))
2573             {
2574               /* We could do even better for unconditional branches to
2575                  portions of this object file; we could pick up the
2576                  instruction at the destination, put it in the delay
2577                  slot, and bump the destination address.  */
2578               emit_nop ();
2579               /* Update the previous insn information.  */
2580               prev_prev_insn = *ip;
2581               prev_insn.insn_mo = &dummy_opcode;
2582             }
2583           else
2584             {
2585               /* It looks like we can actually do the swap.  */
2586               if (! mips_opts.mips16)
2587                 {
2588                   char *prev_f;
2589                   char temp[4];
2590
2591                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2592                   memcpy (temp, prev_f, 4);
2593                   memcpy (prev_f, f, 4);
2594                   memcpy (f, temp, 4);
2595                   if (prev_insn_fixp[0])
2596                     {
2597                       prev_insn_fixp[0]->fx_frag = frag_now;
2598                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2599                     }
2600                   if (prev_insn_fixp[1])
2601                     {
2602                       prev_insn_fixp[1]->fx_frag = frag_now;
2603                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2604                     }
2605                   if (prev_insn_fixp[2])
2606                     {
2607                       prev_insn_fixp[2]->fx_frag = frag_now;
2608                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2609                     }
2610                   if (prev_insn_fixp[0] && HAVE_NEWABI
2611                       && prev_insn_frag != frag_now
2612                       && (prev_insn_fixp[0]->fx_r_type
2613                           == BFD_RELOC_MIPS_GOT_DISP
2614                           || (prev_insn_fixp[0]->fx_r_type
2615                               == BFD_RELOC_MIPS_CALL16)))
2616                     {
2617                       /* To avoid confusion in tc_gen_reloc, we must
2618                          ensure that this does not become a variant
2619                          frag.  */
2620                       force_new_frag = TRUE;
2621                     }
2622                   if (fixp[0])
2623                     {
2624                       fixp[0]->fx_frag = prev_insn_frag;
2625                       fixp[0]->fx_where = prev_insn_where;
2626                     }
2627                   if (fixp[1])
2628                     {
2629                       fixp[1]->fx_frag = prev_insn_frag;
2630                       fixp[1]->fx_where = prev_insn_where;
2631                     }
2632                   if (fixp[2])
2633                     {
2634                       fixp[2]->fx_frag = prev_insn_frag;
2635                       fixp[2]->fx_where = prev_insn_where;
2636                     }
2637                 }
2638               else
2639                 {
2640                   char *prev_f;
2641                   char temp[2];
2642
2643                   assert (prev_insn_fixp[0] == NULL);
2644                   assert (prev_insn_fixp[1] == NULL);
2645                   assert (prev_insn_fixp[2] == NULL);
2646                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2647                   memcpy (temp, prev_f, 2);
2648                   memcpy (prev_f, f, 2);
2649                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2650                     {
2651                       assert (*reloc_type == BFD_RELOC_UNUSED);
2652                       memcpy (f, temp, 2);
2653                     }
2654                   else
2655                     {
2656                       memcpy (f, f + 2, 2);
2657                       memcpy (f + 2, temp, 2);
2658                     }
2659                   if (fixp[0])
2660                     {
2661                       fixp[0]->fx_frag = prev_insn_frag;
2662                       fixp[0]->fx_where = prev_insn_where;
2663                     }
2664                   if (fixp[1])
2665                     {
2666                       fixp[1]->fx_frag = prev_insn_frag;
2667                       fixp[1]->fx_where = prev_insn_where;
2668                     }
2669                   if (fixp[2])
2670                     {
2671                       fixp[2]->fx_frag = prev_insn_frag;
2672                       fixp[2]->fx_where = prev_insn_where;
2673                     }
2674                 }
2675
2676               /* Update the previous insn information; leave prev_insn
2677                  unchanged.  */
2678               prev_prev_insn = *ip;
2679             }
2680           prev_insn_is_delay_slot = 1;
2681
2682           /* If that was an unconditional branch, forget the previous
2683              insn information.  */
2684           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2685             {
2686               prev_prev_insn.insn_mo = &dummy_opcode;
2687               prev_insn.insn_mo = &dummy_opcode;
2688             }
2689
2690           prev_insn_fixp[0] = NULL;
2691           prev_insn_fixp[1] = NULL;
2692           prev_insn_fixp[2] = NULL;
2693           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2694           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2695           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2696           prev_insn_extended = 0;
2697         }
2698       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2699         {
2700           /* We don't yet optimize a branch likely.  What we should do
2701              is look at the target, copy the instruction found there
2702              into the delay slot, and increment the branch to jump to
2703              the next instruction.  */
2704           emit_nop ();
2705           /* Update the previous insn information.  */
2706           prev_prev_insn = *ip;
2707           prev_insn.insn_mo = &dummy_opcode;
2708           prev_insn_fixp[0] = NULL;
2709           prev_insn_fixp[1] = NULL;
2710           prev_insn_fixp[2] = NULL;
2711           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2712           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2713           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2714           prev_insn_extended = 0;
2715         }
2716       else
2717         {
2718           /* Update the previous insn information.  */
2719           if (nops > 0)
2720             prev_prev_insn.insn_mo = &dummy_opcode;
2721           else
2722             prev_prev_insn = prev_insn;
2723           prev_insn = *ip;
2724
2725           /* Any time we see a branch, we always fill the delay slot
2726              immediately; since this insn is not a branch, we know it
2727              is not in a delay slot.  */
2728           prev_insn_is_delay_slot = 0;
2729
2730           prev_insn_fixp[0] = fixp[0];
2731           prev_insn_fixp[1] = fixp[1];
2732           prev_insn_fixp[2] = fixp[2];
2733           prev_insn_reloc_type[0] = reloc_type[0];
2734           prev_insn_reloc_type[1] = reloc_type[1];
2735           prev_insn_reloc_type[2] = reloc_type[2];
2736           if (mips_opts.mips16)
2737             prev_insn_extended = (ip->use_extend
2738                                   || *reloc_type > BFD_RELOC_UNUSED);
2739         }
2740
2741       prev_prev_insn_unreordered = prev_insn_unreordered;
2742       prev_insn_unreordered = 0;
2743       prev_insn_frag = frag_now;
2744       prev_insn_where = f - frag_now->fr_literal;
2745       prev_insn_valid = 1;
2746     }
2747   else if (place == NULL)
2748     {
2749       /* We need to record a bit of information even when we are not
2750          reordering, in order to determine the base address for mips16
2751          PC relative relocs.  */
2752       prev_prev_insn = prev_insn;
2753       prev_insn = *ip;
2754       prev_insn_reloc_type[0] = reloc_type[0];
2755       prev_insn_reloc_type[1] = reloc_type[1];
2756       prev_insn_reloc_type[2] = reloc_type[2];
2757       prev_prev_insn_unreordered = prev_insn_unreordered;
2758       prev_insn_unreordered = 1;
2759     }
2760
2761   /* We just output an insn, so the next one doesn't have a label.  */
2762   mips_clear_insn_labels ();
2763
2764   /* We must ensure that the frag to which an instruction that was
2765      moved from a non-variant frag doesn't become a variant frag,
2766      otherwise tc_gen_reloc may get confused.  */
2767   if (force_new_frag)
2768     {
2769       frag_wane (frag_now);
2770       frag_new (0);
2771     }
2772 }
2773
2774 /* This function forgets that there was any previous instruction or
2775    label.  If PRESERVE is non-zero, it remembers enough information to
2776    know whether nops are needed before a noreorder section.  */
2777
2778 static void
2779 mips_no_prev_insn (preserve)
2780      int preserve;
2781 {
2782   if (! preserve)
2783     {
2784       prev_insn.insn_mo = &dummy_opcode;
2785       prev_prev_insn.insn_mo = &dummy_opcode;
2786       prev_nop_frag = NULL;
2787       prev_nop_frag_holds = 0;
2788       prev_nop_frag_required = 0;
2789       prev_nop_frag_since = 0;
2790     }
2791   prev_insn_valid = 0;
2792   prev_insn_is_delay_slot = 0;
2793   prev_insn_unreordered = 0;
2794   prev_insn_extended = 0;
2795   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2796   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2797   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2798   prev_prev_insn_unreordered = 0;
2799   mips_clear_insn_labels ();
2800 }
2801
2802 /* This function must be called whenever we turn on noreorder or emit
2803    something other than instructions.  It inserts any NOPS which might
2804    be needed by the previous instruction, and clears the information
2805    kept for the previous instructions.  The INSNS parameter is true if
2806    instructions are to follow.  */
2807
2808 static void
2809 mips_emit_delays (insns)
2810      bfd_boolean insns;
2811 {
2812   if (! mips_opts.noreorder)
2813     {
2814       int nops;
2815
2816       nops = 0;
2817       if ((! mips_opts.mips16
2818            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2819            && (! cop_interlocks
2820                && (prev_insn.insn_mo->pinfo
2821                    & (INSN_LOAD_COPROC_DELAY
2822                       | INSN_COPROC_MOVE_DELAY
2823                       | INSN_WRITE_COND_CODE))))
2824           || (! hilo_interlocks
2825               && (prev_insn.insn_mo->pinfo
2826                   & (INSN_READ_LO
2827                      | INSN_READ_HI)))
2828           || (! mips_opts.mips16
2829               && ! gpr_interlocks
2830               && (prev_insn.insn_mo->pinfo
2831                   & INSN_LOAD_MEMORY_DELAY))
2832           || (! mips_opts.mips16
2833               && mips_opts.isa == ISA_MIPS1
2834               && (prev_insn.insn_mo->pinfo
2835                   & INSN_COPROC_MEMORY_DELAY)))
2836         {
2837           /* Itbl support may require additional care here.  */
2838           ++nops;
2839           if ((! mips_opts.mips16
2840                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2841                && (! cop_interlocks
2842                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2843               || (! hilo_interlocks
2844                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2845                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2846             ++nops;
2847
2848           if (prev_insn_unreordered)
2849             nops = 0;
2850         }
2851       else if ((! mips_opts.mips16
2852                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2853                 && (! cop_interlocks
2854                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2855                || (! hilo_interlocks
2856                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2857                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2858         {
2859           /* Itbl support may require additional care here.  */
2860           if (! prev_prev_insn_unreordered)
2861             ++nops;
2862         }
2863
2864       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2865         {
2866           int min_nops = 0;
2867           const char *pn = prev_insn.insn_mo->name;
2868           if (strncmp(pn, "macc", 4) == 0
2869               || strncmp(pn, "dmacc", 5) == 0
2870               || strncmp(pn, "dmult", 5) == 0)
2871             {
2872               min_nops = 1;
2873             }
2874           if (nops < min_nops)
2875             nops = min_nops;
2876         }
2877
2878       if (nops > 0)
2879         {
2880           struct insn_label_list *l;
2881
2882           if (insns)
2883             {
2884               /* Record the frag which holds the nop instructions, so
2885                  that we can remove them if we don't need them.  */
2886               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2887               prev_nop_frag = frag_now;
2888               prev_nop_frag_holds = nops;
2889               prev_nop_frag_required = 0;
2890               prev_nop_frag_since = 0;
2891             }
2892
2893           for (; nops > 0; --nops)
2894             emit_nop ();
2895
2896           if (insns)
2897             {
2898               /* Move on to a new frag, so that it is safe to simply
2899                  decrease the size of prev_nop_frag.  */
2900               frag_wane (frag_now);
2901               frag_new (0);
2902             }
2903
2904           for (l = insn_labels; l != NULL; l = l->next)
2905             {
2906               valueT val;
2907
2908               assert (S_GET_SEGMENT (l->label) == now_seg);
2909               symbol_set_frag (l->label, frag_now);
2910               val = (valueT) frag_now_fix ();
2911               /* mips16 text labels are stored as odd.  */
2912               if (mips_opts.mips16)
2913                 ++val;
2914               S_SET_VALUE (l->label, val);
2915             }
2916         }
2917     }
2918
2919   /* Mark instruction labels in mips16 mode.  */
2920   if (insns)
2921     mips16_mark_labels ();
2922
2923   mips_no_prev_insn (insns);
2924 }
2925
2926 /* Build an instruction created by a macro expansion.  This is passed
2927    a pointer to the count of instructions created so far, an
2928    expression, the name of the instruction to build, an operand format
2929    string, and corresponding arguments.  */
2930
2931 #ifdef USE_STDARG
2932 static void
2933 macro_build (char *place,
2934              int *counter,
2935              expressionS * ep,
2936              const char *name,
2937              const char *fmt,
2938              ...)
2939 #else
2940 static void
2941 macro_build (place, counter, ep, name, fmt, va_alist)
2942      char *place;
2943      int *counter;
2944      expressionS *ep;
2945      const char *name;
2946      const char *fmt;
2947      va_dcl
2948 #endif
2949 {
2950   struct mips_cl_insn insn;
2951   bfd_reloc_code_real_type r[3];
2952   va_list args;
2953
2954 #ifdef USE_STDARG
2955   va_start (args, fmt);
2956 #else
2957   va_start (args);
2958 #endif
2959
2960   /*
2961    * If the macro is about to expand into a second instruction,
2962    * print a warning if needed. We need to pass ip as a parameter
2963    * to generate a better warning message here...
2964    */
2965   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2966     as_warn (_("Macro instruction expanded into multiple instructions"));
2967
2968   /*
2969    * If the macro is about to expand into a second instruction,
2970    * and it is in a delay slot, print a warning.
2971    */
2972   if (place == NULL
2973       && *counter == 1
2974       && mips_opts.noreorder
2975       && (prev_prev_insn.insn_mo->pinfo
2976           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2977              | INSN_COND_BRANCH_LIKELY)) != 0)
2978     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2979
2980   if (place == NULL)
2981     ++*counter;         /* bump instruction counter */
2982
2983   if (mips_opts.mips16)
2984     {
2985       mips16_macro_build (place, counter, ep, name, fmt, args);
2986       va_end (args);
2987       return;
2988     }
2989
2990   r[0] = BFD_RELOC_UNUSED;
2991   r[1] = BFD_RELOC_UNUSED;
2992   r[2] = BFD_RELOC_UNUSED;
2993   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2994   assert (insn.insn_mo);
2995   assert (strcmp (name, insn.insn_mo->name) == 0);
2996
2997   /* Search until we get a match for NAME.  */
2998   while (1)
2999     {
3000       /* It is assumed here that macros will never generate
3001          MDMX or MIPS-3D instructions.  */
3002       if (strcmp (fmt, insn.insn_mo->args) == 0
3003           && insn.insn_mo->pinfo != INSN_MACRO
3004           && OPCODE_IS_MEMBER (insn.insn_mo,
3005                                (mips_opts.isa
3006                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3007                                mips_arch)
3008           && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3009         break;
3010
3011       ++insn.insn_mo;
3012       assert (insn.insn_mo->name);
3013       assert (strcmp (name, insn.insn_mo->name) == 0);
3014     }
3015
3016   insn.insn_opcode = insn.insn_mo->match;
3017   for (;;)
3018     {
3019       switch (*fmt++)
3020         {
3021         case '\0':
3022           break;
3023
3024         case ',':
3025         case '(':
3026         case ')':
3027           continue;
3028
3029         case 't':
3030         case 'w':
3031         case 'E':
3032           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3033           continue;
3034
3035         case 'c':
3036           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3037           continue;
3038
3039         case 'T':
3040         case 'W':
3041           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3042           continue;
3043
3044         case 'd':
3045         case 'G':
3046         case 'K':
3047           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3048           continue;
3049
3050         case 'U':
3051           {
3052             int tmp = va_arg (args, int);
3053
3054             insn.insn_opcode |= tmp << OP_SH_RT;
3055             insn.insn_opcode |= tmp << OP_SH_RD;
3056             continue;
3057           }
3058
3059         case 'V':
3060         case 'S':
3061           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3062           continue;
3063
3064         case 'z':
3065           continue;
3066
3067         case '<':
3068           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3069           continue;
3070
3071         case 'D':
3072           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3073           continue;
3074
3075         case 'B':
3076           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3077           continue;
3078
3079         case 'J':
3080           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3081           continue;
3082
3083         case 'q':
3084           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3085           continue;
3086
3087         case 'b':
3088         case 's':
3089         case 'r':
3090         case 'v':
3091           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3092           continue;
3093
3094         case 'i':
3095         case 'j':
3096         case 'o':
3097           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3098           assert (*r == BFD_RELOC_GPREL16
3099                   || *r == BFD_RELOC_MIPS_LITERAL
3100                   || *r == BFD_RELOC_MIPS_HIGHER
3101                   || *r == BFD_RELOC_HI16_S
3102                   || *r == BFD_RELOC_LO16
3103                   || *r == BFD_RELOC_MIPS_GOT16
3104                   || *r == BFD_RELOC_MIPS_CALL16
3105                   || *r == BFD_RELOC_MIPS_GOT_DISP
3106                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3107                   || *r == BFD_RELOC_MIPS_GOT_OFST
3108                   || *r == BFD_RELOC_MIPS_GOT_LO16
3109                   || *r == BFD_RELOC_MIPS_CALL_LO16
3110                   || (ep->X_op == O_subtract
3111                       && *r == BFD_RELOC_PCREL_LO16));
3112           continue;
3113
3114         case 'u':
3115           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3116           assert (ep != NULL
3117                   && (ep->X_op == O_constant
3118                       || (ep->X_op == O_symbol
3119                           && (*r == BFD_RELOC_MIPS_HIGHEST
3120                               || *r == BFD_RELOC_HI16_S
3121                               || *r == BFD_RELOC_HI16
3122                               || *r == BFD_RELOC_GPREL16
3123                               || *r == BFD_RELOC_MIPS_GOT_HI16
3124                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3125                       || (ep->X_op == O_subtract
3126                           && *r == BFD_RELOC_PCREL_HI16_S)));
3127           continue;
3128
3129         case 'p':
3130           assert (ep != NULL);
3131           /*
3132            * This allows macro() to pass an immediate expression for
3133            * creating short branches without creating a symbol.
3134            * Note that the expression still might come from the assembly
3135            * input, in which case the value is not checked for range nor
3136            * is a relocation entry generated (yuck).
3137            */
3138           if (ep->X_op == O_constant)
3139             {
3140               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3141               ep = NULL;
3142             }
3143           else
3144             *r = BFD_RELOC_16_PCREL_S2;
3145           continue;
3146
3147         case 'a':
3148           assert (ep != NULL);
3149           *r = BFD_RELOC_MIPS_JMP;
3150           continue;
3151
3152         case 'C':
3153           insn.insn_opcode |= va_arg (args, unsigned long);
3154           continue;
3155
3156         default:
3157           internalError ();
3158         }
3159       break;
3160     }
3161   va_end (args);
3162   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3163
3164   append_insn (place, &insn, ep, r);
3165 }
3166
3167 static void
3168 mips16_macro_build (place, counter, ep, name, fmt, args)
3169      char *place;
3170      int *counter ATTRIBUTE_UNUSED;
3171      expressionS *ep;
3172      const char *name;
3173      const char *fmt;
3174      va_list args;
3175 {
3176   struct mips_cl_insn insn;
3177   bfd_reloc_code_real_type r[3]
3178     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3179
3180   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3181   assert (insn.insn_mo);
3182   assert (strcmp (name, insn.insn_mo->name) == 0);
3183
3184   while (strcmp (fmt, insn.insn_mo->args) != 0
3185          || insn.insn_mo->pinfo == INSN_MACRO)
3186     {
3187       ++insn.insn_mo;
3188       assert (insn.insn_mo->name);
3189       assert (strcmp (name, insn.insn_mo->name) == 0);
3190     }
3191
3192   insn.insn_opcode = insn.insn_mo->match;
3193   insn.use_extend = FALSE;
3194
3195   for (;;)
3196     {
3197       int c;
3198
3199       c = *fmt++;
3200       switch (c)
3201         {
3202         case '\0':
3203           break;
3204
3205         case ',':
3206         case '(':
3207         case ')':
3208           continue;
3209
3210         case 'y':
3211         case 'w':
3212           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3213           continue;
3214
3215         case 'x':
3216         case 'v':
3217           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3218           continue;
3219
3220         case 'z':
3221           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3222           continue;
3223
3224         case 'Z':
3225           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3226           continue;
3227
3228         case '0':
3229         case 'S':
3230         case 'P':
3231         case 'R':
3232           continue;
3233
3234         case 'X':
3235           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3236           continue;
3237
3238         case 'Y':
3239           {
3240             int regno;
3241
3242             regno = va_arg (args, int);
3243             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3244             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3245           }
3246           continue;
3247
3248         case '<':
3249         case '>':
3250         case '4':
3251         case '5':
3252         case 'H':
3253         case 'W':
3254         case 'D':
3255         case 'j':
3256         case '8':
3257         case 'V':
3258         case 'C':
3259         case 'U':
3260         case 'k':
3261         case 'K':
3262         case 'p':
3263         case 'q':
3264           {
3265             assert (ep != NULL);
3266
3267             if (ep->X_op != O_constant)
3268               *r = (int) BFD_RELOC_UNUSED + c;
3269             else
3270               {
3271                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3272                               FALSE, &insn.insn_opcode, &insn.use_extend,
3273                               &insn.extend);
3274                 ep = NULL;
3275                 *r = BFD_RELOC_UNUSED;
3276               }
3277           }
3278           continue;
3279
3280         case '6':
3281           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3282           continue;
3283         }
3284
3285       break;
3286     }
3287
3288   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3289
3290   append_insn (place, &insn, ep, r);
3291 }
3292
3293 /*
3294  * Generate a "jalr" instruction with a relocation hint to the called
3295  * function.  This occurs in NewABI PIC code.
3296  */
3297 static void
3298 macro_build_jalr (icnt, ep)
3299      int icnt;
3300      expressionS *ep;
3301 {
3302   char *f;
3303
3304   if (HAVE_NEWABI)
3305     {
3306       frag_grow (4);
3307       f = frag_more (0);
3308     }
3309   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3310                RA, PIC_CALL_REG);
3311   if (HAVE_NEWABI)
3312     fix_new_exp (frag_now, f - frag_now->fr_literal,
3313                  0, ep, FALSE, BFD_RELOC_MIPS_JALR);
3314 }
3315
3316 /*
3317  * Generate a "lui" instruction.
3318  */
3319 static void
3320 macro_build_lui (place, counter, ep, regnum)
3321      char *place;
3322      int *counter;
3323      expressionS *ep;
3324      int regnum;
3325 {
3326   expressionS high_expr;
3327   struct mips_cl_insn insn;
3328   bfd_reloc_code_real_type r[3]
3329     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3330   const char *name = "lui";
3331   const char *fmt = "t,u";
3332
3333   assert (! mips_opts.mips16);
3334
3335   if (place == NULL)
3336     high_expr = *ep;
3337   else
3338     {
3339       high_expr.X_op = O_constant;
3340       high_expr.X_add_number = ep->X_add_number;
3341     }
3342
3343   if (high_expr.X_op == O_constant)
3344     {
3345       /* we can compute the instruction now without a relocation entry */
3346       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3347                                 >> 16) & 0xffff;
3348       *r = BFD_RELOC_UNUSED;
3349     }
3350   else
3351     {
3352       assert (ep->X_op == O_symbol);
3353       /* _gp_disp is a special case, used from s_cpload.  */
3354       assert (mips_pic == NO_PIC
3355               || (! HAVE_NEWABI
3356                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3357       *r = BFD_RELOC_HI16_S;
3358     }
3359
3360   /*
3361    * If the macro is about to expand into a second instruction,
3362    * print a warning if needed. We need to pass ip as a parameter
3363    * to generate a better warning message here...
3364    */
3365   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3366     as_warn (_("Macro instruction expanded into multiple instructions"));
3367
3368   if (place == NULL)
3369     ++*counter;         /* bump instruction counter */
3370
3371   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3372   assert (insn.insn_mo);
3373   assert (strcmp (name, insn.insn_mo->name) == 0);
3374   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3375
3376   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3377   if (*r == BFD_RELOC_UNUSED)
3378     {
3379       insn.insn_opcode |= high_expr.X_add_number;
3380       append_insn (place, &insn, NULL, r);
3381     }
3382   else
3383     append_insn (place, &insn, &high_expr, r);
3384 }
3385
3386 /* Generate a sequence of instructions to do a load or store from a constant
3387    offset off of a base register (breg) into/from a target register (treg),
3388    using AT if necessary.  */
3389 static void
3390 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3391      char *place;
3392      int *counter;
3393      expressionS *ep;
3394      const char *op;
3395      int treg, breg;
3396 {
3397   assert (ep->X_op == O_constant);
3398
3399   /* Right now, this routine can only handle signed 32-bit contants.  */
3400   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3401     as_warn (_("operand overflow"));
3402
3403   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3404     {
3405       /* Signed 16-bit offset will fit in the op.  Easy!  */
3406       macro_build (place, counter, ep, op, "t,o(b)", treg,
3407                    (int) BFD_RELOC_LO16, breg);
3408     }
3409   else
3410     {
3411       /* 32-bit offset, need multiple instructions and AT, like:
3412            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3413            addu     $tempreg,$tempreg,$breg
3414            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3415          to handle the complete offset.  */
3416       macro_build_lui (place, counter, ep, AT);
3417       if (place != NULL)
3418         place += 4;
3419       macro_build (place, counter, (expressionS *) NULL,
3420                    HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3421                    ? "add" : "addu" : "daddu",
3422                    "d,v,t", AT, AT, breg);
3423       if (place != NULL)
3424         place += 4;
3425       macro_build (place, counter, ep, op, "t,o(b)", treg,
3426                    (int) BFD_RELOC_LO16, AT);
3427
3428       if (mips_opts.noat)
3429         as_warn (_("Macro used $at after \".set noat\""));
3430     }
3431 }
3432
3433 /*                      set_at()
3434  * Generates code to set the $at register to true (one)
3435  * if reg is less than the immediate expression.
3436  */
3437 static void
3438 set_at (counter, reg, unsignedp)
3439      int *counter;
3440      int reg;
3441      int unsignedp;
3442 {
3443   if (imm_expr.X_op == O_constant
3444       && imm_expr.X_add_number >= -0x8000
3445       && imm_expr.X_add_number < 0x8000)
3446     macro_build ((char *) NULL, counter, &imm_expr,
3447                  unsignedp ? "sltiu" : "slti",
3448                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3449   else
3450     {
3451       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3452       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3453                    unsignedp ? "sltu" : "slt",
3454                    "d,v,t", AT, reg, AT);
3455     }
3456 }
3457
3458 /* Warn if an expression is not a constant.  */
3459
3460 static void
3461 check_absolute_expr (ip, ex)
3462      struct mips_cl_insn *ip;
3463      expressionS *ex;
3464 {
3465   if (ex->X_op == O_big)
3466     as_bad (_("unsupported large constant"));
3467   else if (ex->X_op != O_constant)
3468     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3469 }
3470
3471 /* Count the leading zeroes by performing a binary chop. This is a
3472    bulky bit of source, but performance is a LOT better for the
3473    majority of values than a simple loop to count the bits:
3474        for (lcnt = 0; (lcnt < 32); lcnt++)
3475          if ((v) & (1 << (31 - lcnt)))
3476            break;
3477   However it is not code size friendly, and the gain will drop a bit
3478   on certain cached systems.
3479 */
3480 #define COUNT_TOP_ZEROES(v)             \
3481   (((v) & ~0xffff) == 0                 \
3482    ? ((v) & ~0xff) == 0                 \
3483      ? ((v) & ~0xf) == 0                \
3484        ? ((v) & ~0x3) == 0              \
3485          ? ((v) & ~0x1) == 0            \
3486            ? !(v)                       \
3487              ? 32                       \
3488              : 31                       \
3489            : 30                         \
3490          : ((v) & ~0x7) == 0            \
3491            ? 29                         \
3492            : 28                         \
3493        : ((v) & ~0x3f) == 0             \
3494          ? ((v) & ~0x1f) == 0           \
3495            ? 27                         \
3496            : 26                         \
3497          : ((v) & ~0x7f) == 0           \
3498            ? 25                         \
3499            : 24                         \
3500      : ((v) & ~0xfff) == 0              \
3501        ? ((v) & ~0x3ff) == 0            \
3502          ? ((v) & ~0x1ff) == 0          \
3503            ? 23                         \
3504            : 22                         \
3505          : ((v) & ~0x7ff) == 0          \
3506            ? 21                         \
3507            : 20                         \
3508        : ((v) & ~0x3fff) == 0           \
3509          ? ((v) & ~0x1fff) == 0         \
3510            ? 19                         \
3511            : 18                         \
3512          : ((v) & ~0x7fff) == 0         \
3513            ? 17                         \
3514            : 16                         \
3515    : ((v) & ~0xffffff) == 0             \
3516      ? ((v) & ~0xfffff) == 0            \
3517        ? ((v) & ~0x3ffff) == 0          \
3518          ? ((v) & ~0x1ffff) == 0        \
3519            ? 15                         \
3520            : 14                         \
3521          : ((v) & ~0x7ffff) == 0        \
3522            ? 13                         \
3523            : 12                         \
3524        : ((v) & ~0x3fffff) == 0         \
3525          ? ((v) & ~0x1fffff) == 0       \
3526            ? 11                         \
3527            : 10                         \
3528          : ((v) & ~0x7fffff) == 0       \
3529            ? 9                          \
3530            : 8                          \
3531      : ((v) & ~0xfffffff) == 0          \
3532        ? ((v) & ~0x3ffffff) == 0        \
3533          ? ((v) & ~0x1ffffff) == 0      \
3534            ? 7                          \
3535            : 6                          \
3536          : ((v) & ~0x7ffffff) == 0      \
3537            ? 5                          \
3538            : 4                          \
3539        : ((v) & ~0x3fffffff) == 0       \
3540          ? ((v) & ~0x1fffffff) == 0     \
3541            ? 3                          \
3542            : 2                          \
3543          : ((v) & ~0x7fffffff) == 0     \
3544            ? 1                          \
3545            : 0)
3546
3547 /*                      load_register()
3548  *  This routine generates the least number of instructions neccessary to load
3549  *  an absolute expression value into a register.
3550  */
3551 static void
3552 load_register (counter, reg, ep, dbl)
3553      int *counter;
3554      int reg;
3555      expressionS *ep;
3556      int dbl;
3557 {
3558   int freg;
3559   expressionS hi32, lo32;
3560
3561   if (ep->X_op != O_big)
3562     {
3563       assert (ep->X_op == O_constant);
3564       if (ep->X_add_number < 0x8000
3565           && (ep->X_add_number >= 0
3566               || (ep->X_add_number >= -0x8000
3567                   && (! dbl
3568                       || ! ep->X_unsigned
3569                       || sizeof (ep->X_add_number) > 4))))
3570         {
3571           /* We can handle 16 bit signed values with an addiu to
3572              $zero.  No need to ever use daddiu here, since $zero and
3573              the result are always correct in 32 bit mode.  */
3574           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3575                        (int) BFD_RELOC_LO16);
3576           return;
3577         }
3578       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3579         {
3580           /* We can handle 16 bit unsigned values with an ori to
3581              $zero.  */
3582           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3583                        (int) BFD_RELOC_LO16);
3584           return;
3585         }
3586       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3587                 && (! dbl
3588                     || ! ep->X_unsigned
3589                     || sizeof (ep->X_add_number) > 4
3590                     || (ep->X_add_number & 0x80000000) == 0))
3591                || ((HAVE_32BIT_GPRS || ! dbl)
3592                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3593                || (HAVE_32BIT_GPRS
3594                    && ! dbl
3595                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3596                        == ~ (offsetT) 0xffffffff)))
3597         {
3598           /* 32 bit values require an lui.  */
3599           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3600                        (int) BFD_RELOC_HI16);
3601           if ((ep->X_add_number & 0xffff) != 0)
3602             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3603                          (int) BFD_RELOC_LO16);
3604           return;
3605         }
3606     }
3607
3608   /* The value is larger than 32 bits.  */
3609
3610   if (HAVE_32BIT_GPRS)
3611     {
3612       as_bad (_("Number (0x%lx) larger than 32 bits"),
3613               (unsigned long) ep->X_add_number);
3614       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3615                    (int) BFD_RELOC_LO16);
3616       return;
3617     }
3618
3619   if (ep->X_op != O_big)
3620     {
3621       hi32 = *ep;
3622       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3623       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3624       hi32.X_add_number &= 0xffffffff;
3625       lo32 = *ep;
3626       lo32.X_add_number &= 0xffffffff;
3627     }
3628   else
3629     {
3630       assert (ep->X_add_number > 2);
3631       if (ep->X_add_number == 3)
3632         generic_bignum[3] = 0;
3633       else if (ep->X_add_number > 4)
3634         as_bad (_("Number larger than 64 bits"));
3635       lo32.X_op = O_constant;
3636       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3637       hi32.X_op = O_constant;
3638       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3639     }
3640
3641   if (hi32.X_add_number == 0)
3642     freg = 0;
3643   else
3644     {
3645       int shift, bit;
3646       unsigned long hi, lo;
3647
3648       if (hi32.X_add_number == (offsetT) 0xffffffff)
3649         {
3650           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3651             {
3652               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3653                            reg, 0, (int) BFD_RELOC_LO16);
3654               return;
3655             }
3656           if (lo32.X_add_number & 0x80000000)
3657             {
3658               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3659                            (int) BFD_RELOC_HI16);
3660               if (lo32.X_add_number & 0xffff)
3661                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3662                              reg, reg, (int) BFD_RELOC_LO16);
3663               return;
3664             }
3665         }
3666
3667       /* Check for 16bit shifted constant.  We know that hi32 is
3668          non-zero, so start the mask on the first bit of the hi32
3669          value.  */
3670       shift = 17;
3671       do
3672         {
3673           unsigned long himask, lomask;
3674
3675           if (shift < 32)
3676             {
3677               himask = 0xffff >> (32 - shift);
3678               lomask = (0xffff << shift) & 0xffffffff;
3679             }
3680           else
3681             {
3682               himask = 0xffff << (shift - 32);
3683               lomask = 0;
3684             }
3685           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3686               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3687             {
3688               expressionS tmp;
3689
3690               tmp.X_op = O_constant;
3691               if (shift < 32)
3692                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3693                                     | (lo32.X_add_number >> shift));
3694               else
3695                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3696               macro_build ((char *) NULL, counter, &tmp,
3697                            "ori", "t,r,i", reg, 0,
3698                            (int) BFD_RELOC_LO16);
3699               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3700                            (shift >= 32) ? "dsll32" : "dsll",
3701                            "d,w,<", reg, reg,
3702                            (shift >= 32) ? shift - 32 : shift);
3703               return;
3704             }
3705           ++shift;
3706         }
3707       while (shift <= (64 - 16));
3708
3709       /* Find the bit number of the lowest one bit, and store the
3710          shifted value in hi/lo.  */
3711       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3712       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3713       if (lo != 0)
3714         {
3715           bit = 0;
3716           while ((lo & 1) == 0)
3717             {
3718               lo >>= 1;
3719               ++bit;
3720             }
3721           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3722           hi >>= bit;
3723         }
3724       else
3725         {
3726           bit = 32;
3727           while ((hi & 1) == 0)
3728             {
3729               hi >>= 1;
3730               ++bit;
3731             }
3732           lo = hi;
3733           hi = 0;
3734         }
3735
3736       /* Optimize if the shifted value is a (power of 2) - 1.  */
3737       if ((hi == 0 && ((lo + 1) & lo) == 0)
3738           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3739         {
3740           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3741           if (shift != 0)
3742             {
3743               expressionS tmp;
3744
3745               /* This instruction will set the register to be all
3746                  ones.  */
3747               tmp.X_op = O_constant;
3748               tmp.X_add_number = (offsetT) -1;
3749               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3750                            reg, 0, (int) BFD_RELOC_LO16);
3751               if (bit != 0)
3752                 {
3753                   bit += shift;
3754                   macro_build ((char *) NULL, counter, (expressionS *) NULL,
3755                                (bit >= 32) ? "dsll32" : "dsll",
3756                                "d,w,<", reg, reg,
3757                                (bit >= 32) ? bit - 32 : bit);
3758                 }
3759               macro_build ((char *) NULL, counter, (expressionS *) NULL,
3760                            (shift >= 32) ? "dsrl32" : "dsrl",
3761                            "d,w,<", reg, reg,
3762                            (shift >= 32) ? shift - 32 : shift);
3763               return;
3764             }
3765         }
3766
3767       /* Sign extend hi32 before calling load_register, because we can
3768          generally get better code when we load a sign extended value.  */
3769       if ((hi32.X_add_number & 0x80000000) != 0)
3770         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3771       load_register (counter, reg, &hi32, 0);
3772       freg = reg;
3773     }
3774   if ((lo32.X_add_number & 0xffff0000) == 0)
3775     {
3776       if (freg != 0)
3777         {
3778           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3779                        "dsll32", "d,w,<", reg, freg, 0);
3780           freg = reg;
3781         }
3782     }
3783   else
3784     {
3785       expressionS mid16;
3786
3787       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3788         {
3789           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3790                        (int) BFD_RELOC_HI16);
3791           macro_build ((char *) NULL, counter, (expressionS *) NULL,
3792                        "dsrl32", "d,w,<", reg, reg, 0);
3793           return;
3794         }
3795
3796       if (freg != 0)
3797         {
3798           macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3799                        "d,w,<", reg, freg, 16);
3800           freg = reg;
3801         }
3802       mid16 = lo32;
3803       mid16.X_add_number >>= 16;
3804       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3805                    freg, (int) BFD_RELOC_LO16);
3806       macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3807                    "d,w,<", reg, reg, 16);
3808       freg = reg;
3809     }
3810   if ((lo32.X_add_number & 0xffff) != 0)
3811     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3812                  (int) BFD_RELOC_LO16);
3813 }
3814
3815 /* Load an address into a register.  */
3816
3817 static void
3818 load_address (counter, reg, ep, used_at)
3819      int *counter;
3820      int reg;
3821      expressionS *ep;
3822      int *used_at;
3823 {
3824   char *p = NULL;
3825
3826   if (ep->X_op != O_constant
3827       && ep->X_op != O_symbol)
3828     {
3829       as_bad (_("expression too complex"));
3830       ep->X_op = O_constant;
3831     }
3832
3833   if (ep->X_op == O_constant)
3834     {
3835       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3836       return;
3837     }
3838
3839   if (mips_pic == NO_PIC)
3840     {
3841       /* If this is a reference to a GP relative symbol, we want
3842            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3843          Otherwise we want
3844            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3845            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3846          If we have an addend, we always use the latter form.
3847
3848          With 64bit address space and a usable $at we want
3849            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3850            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3851            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3852            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3853            dsll32       $reg,0
3854            daddu        $reg,$reg,$at
3855
3856          If $at is already in use, we use a path which is suboptimal
3857          on superscalar processors.
3858            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3859            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3860            dsll         $reg,16
3861            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3862            dsll         $reg,16
3863            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3864        */
3865       if (HAVE_64BIT_ADDRESSES)
3866         {
3867           /* We don't do GP optimization for now because RELAX_ENCODE can't
3868              hold the data for such large chunks.  */
3869
3870           if (*used_at == 0 && ! mips_opts.noat)
3871             {
3872               macro_build (p, counter, ep, "lui", "t,u",
3873                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3874               macro_build (p, counter, ep, "lui", "t,u",
3875                            AT, (int) BFD_RELOC_HI16_S);
3876               macro_build (p, counter, ep, "daddiu", "t,r,j",
3877                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3878               macro_build (p, counter, ep, "daddiu", "t,r,j",
3879                            AT, AT, (int) BFD_RELOC_LO16);
3880               macro_build (p, counter, (expressionS *) NULL, "dsll32",
3881                            "d,w,<", reg, reg, 0);
3882               macro_build (p, counter, (expressionS *) NULL, "daddu",
3883                            "d,v,t", reg, reg, AT);
3884               *used_at = 1;
3885             }
3886           else
3887             {
3888               macro_build (p, counter, ep, "lui", "t,u",
3889                            reg, (int) BFD_RELOC_MIPS_HIGHEST);
3890               macro_build (p, counter, ep, "daddiu", "t,r,j",
3891                            reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3892               macro_build (p, counter, (expressionS *) NULL, "dsll",
3893                            "d,w,<", reg, reg, 16);
3894               macro_build (p, counter, ep, "daddiu", "t,r,j",
3895                            reg, reg, (int) BFD_RELOC_HI16_S);
3896               macro_build (p, counter, (expressionS *) NULL, "dsll",
3897                            "d,w,<", reg, reg, 16);
3898               macro_build (p, counter, ep, "daddiu", "t,r,j",
3899                            reg, reg, (int) BFD_RELOC_LO16);
3900             }
3901         }
3902       else
3903         {
3904           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3905               && ! nopic_need_relax (ep->X_add_symbol, 1))
3906             {
3907               frag_grow (20);
3908               macro_build ((char *) NULL, counter, ep,
3909                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3910                            ? "addi" : "addiu" : "daddiu", "t,r,j",
3911                            reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3912               p = frag_var (rs_machine_dependent, 8, 0,
3913                             RELAX_ENCODE (4, 8, 0, 4, 0,
3914                                           mips_opts.warn_about_macros),
3915                             ep->X_add_symbol, 0, NULL);
3916             }
3917           macro_build_lui (p, counter, ep, reg);
3918           if (p != NULL)
3919             p += 4;
3920           macro_build (p, counter, ep,
3921                        HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3922                        ? "addi" : "addiu" : "daddiu",
3923                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3924         }
3925     }
3926   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3927     {
3928       expressionS ex;
3929
3930       /* If this is a reference to an external symbol, we want
3931            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3932          Otherwise we want
3933            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3934            nop
3935            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3936          If there is a constant, it must be added in after.
3937
3938          If we have NewABI, we want
3939            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3940          unless we're referencing a global symbol with a non-zero
3941          offset, in which case cst must be added separately.  */
3942       if (HAVE_NEWABI)
3943         {
3944           frag_grow (12);
3945
3946           if (ep->X_add_number)
3947             {
3948               frag_now->tc_frag_data.tc_fr_offset =
3949                 ex.X_add_number = ep->X_add_number;
3950               ep->X_add_number = 0;
3951               macro_build ((char *) NULL, counter, ep,
3952                            HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3953                            (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3954               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3955                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3956               ex.X_op = O_constant;
3957               macro_build ((char *) NULL, counter, &ex,
3958                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
3959                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3960               p = frag_var (rs_machine_dependent, 8, 0,
3961                             RELAX_ENCODE (8, 4, 0, 0, 0,
3962                                           mips_opts.warn_about_macros),
3963                             ep->X_add_symbol, 0, (char *) NULL);
3964               ep->X_add_number = ex.X_add_number;
3965             }
3966
3967           macro_build (p, counter, ep,
3968                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3969                        (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3970
3971           if (! p)
3972             {
3973               /* To avoid confusion in tc_gen_reloc, we must ensure
3974                  that this does not become a variant frag.  */
3975               frag_wane (frag_now);
3976               frag_new (0);
3977             }
3978         }
3979       else
3980         {
3981           ex.X_add_number = ep->X_add_number;
3982           ep->X_add_number = 0;
3983           frag_grow (20);
3984           macro_build ((char *) NULL, counter, ep,
3985                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3986                        reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3987           macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3988           p = frag_var (rs_machine_dependent, 4, 0,
3989                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3990                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3991           macro_build (p, counter, ep,
3992                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3993                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3994
3995           if (ex.X_add_number != 0)
3996             {
3997               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3998                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3999               ex.X_op = O_constant;
4000               macro_build ((char *) NULL, counter, &ex,
4001                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4002                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4003             }
4004         }
4005     }
4006   else if (mips_pic == SVR4_PIC)
4007     {
4008       expressionS ex;
4009       int off;
4010
4011       /* This is the large GOT case.  If this is a reference to an
4012          external symbol, we want
4013            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
4014            addu         $reg,$reg,$gp
4015            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
4016
4017          Otherwise, for a reference to a local symbol in old ABI, we want
4018            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
4019            nop
4020            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
4021          If there is a constant, it must be added in after.
4022
4023          In the NewABI, for local symbols, with or without offsets, we want:
4024            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
4025            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
4026       */
4027       if (HAVE_NEWABI)
4028         {
4029           frag_grow (24);
4030
4031           frag_now->tc_frag_data.tc_fr_offset =
4032             ex.X_add_number = ep->X_add_number;
4033           ep->X_add_number = 0;
4034           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4035                        (int) BFD_RELOC_MIPS_GOT_HI16);
4036           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4037                        HAVE_32BIT_ADDRESSES ? "add" : "daddu", "d,v,t", reg,
4038                        reg, mips_gp_register);
4039           macro_build ((char *) NULL, counter, ep,
4040                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4041                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4042           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4043             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4044           else if (ex.X_add_number)
4045             {
4046               ex.X_op = O_constant;
4047               macro_build ((char *) NULL, counter, &ex,
4048                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
4049                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4050             }
4051
4052           ep->X_add_number = ex.X_add_number;
4053           p = frag_var (rs_machine_dependent, 8, 0,
4054                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
4055                                       mips_opts.warn_about_macros),
4056                         ep->X_add_symbol, 0, (char *) NULL);
4057           macro_build (p, counter, ep,
4058                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4059                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4060           macro_build (p + 4, counter, ep,
4061                        HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
4062                        reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
4063         }
4064       else
4065         {
4066           ex.X_add_number = ep->X_add_number;
4067           ep->X_add_number = 0;
4068           if (reg_needs_delay (mips_gp_register))
4069             off = 4;
4070           else
4071             off = 0;
4072           frag_grow (32);
4073           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4074                        (int) BFD_RELOC_MIPS_GOT_HI16);
4075           macro_build ((char *) NULL, counter, (expressionS *) NULL,
4076                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
4077                        reg, mips_gp_register);
4078           macro_build ((char *) NULL, counter, ep,
4079                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4080                        "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4081           p = frag_var (rs_machine_dependent, 12 + off, 0,
4082                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4083                                       mips_opts.warn_about_macros),
4084                         ep->X_add_symbol, 0, NULL);
4085           if (off > 0)
4086             {
4087               /* We need a nop before loading from $gp.  This special
4088                  check is required because the lui which starts the main
4089                  instruction stream does not refer to $gp, and so will not
4090                  insert the nop which may be required.  */
4091               macro_build (p, counter, (expressionS *) NULL, "nop", "");
4092                 p += 4;
4093             }
4094           macro_build (p, counter, ep,
4095                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4096                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4097           p += 4;
4098           macro_build (p, counter, (expressionS *) NULL, "nop", "");
4099           p += 4;
4100           macro_build (p, counter, ep,
4101                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4102                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4103
4104           if (ex.X_add_number != 0)
4105             {
4106               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4107                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4108               ex.X_op = O_constant;
4109               macro_build ((char *) NULL, counter, &ex,
4110                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4111                            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4112             }
4113         }
4114     }
4115   else if (mips_pic == EMBEDDED_PIC)
4116     {
4117       /* We always do
4118            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4119        */
4120       macro_build ((char *) NULL, counter, ep,
4121                    HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4122                    "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4123     }
4124   else
4125     abort ();
4126 }
4127
4128 /* Move the contents of register SOURCE into register DEST.  */
4129
4130 static void
4131 move_register (counter, dest, source)
4132      int *counter;
4133      int dest;
4134      int source;
4135 {
4136   macro_build ((char *) NULL, counter, (expressionS *) NULL,
4137                HAVE_32BIT_GPRS ? "addu" : "daddu",
4138                "d,v,t", dest, source, 0);
4139 }
4140
4141 /*
4142  *                      Build macros
4143  *   This routine implements the seemingly endless macro or synthesized
4144  * instructions and addressing modes in the mips assembly language. Many
4145  * of these macros are simple and are similar to each other. These could
4146  * probably be handled by some kind of table or grammer aproach instead of
4147  * this verbose method. Others are not simple macros but are more like
4148  * optimizing code generation.
4149  *   One interesting optimization is when several store macros appear
4150  * consecutivly that would load AT with the upper half of the same address.
4151  * The ensuing load upper instructions are ommited. This implies some kind
4152  * of global optimization. We currently only optimize within a single macro.
4153  *   For many of the load and store macros if the address is specified as a
4154  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4155  * first load register 'at' with zero and use it as the base register. The
4156  * mips assembler simply uses register $zero. Just one tiny optimization
4157  * we're missing.
4158  */
4159 static void
4160 macro (ip)
4161      struct mips_cl_insn *ip;
4162 {
4163   register int treg, sreg, dreg, breg;
4164   int tempreg;
4165   int mask;
4166   int icnt = 0;
4167   int used_at = 0;
4168   expressionS expr1;
4169   const char *s;
4170   const char *s2;
4171   const char *fmt;
4172   int likely = 0;
4173   int dbl = 0;
4174   int coproc = 0;
4175   int lr = 0;
4176   int imm = 0;
4177   offsetT maxnum;
4178   int off;
4179   bfd_reloc_code_real_type r;
4180   int hold_mips_optimize;
4181
4182   assert (! mips_opts.mips16);
4183
4184   treg = (ip->insn_opcode >> 16) & 0x1f;
4185   dreg = (ip->insn_opcode >> 11) & 0x1f;
4186   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4187   mask = ip->insn_mo->mask;
4188
4189   expr1.X_op = O_constant;
4190   expr1.X_op_symbol = NULL;
4191   expr1.X_add_symbol = NULL;
4192   expr1.X_add_number = 1;
4193
4194   /* Umatched fixups should not be put in the same frag as a relaxable
4195      macro.  For example, suppose we have:
4196
4197         lui $4,%hi(l1)          # 1
4198         la $5,l2                # 2
4199         addiu $4,$4,%lo(l1)     # 3
4200
4201      If instructions 1 and 2 were put in the same frag, md_frob_file would
4202      move the fixup for #1 after the fixups for the "unrelaxed" version of
4203      #2.  This would confuse tc_gen_reloc, which expects the relocations
4204      for #2 to be the last for that frag.
4205
4206      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4207      it assumes that they belong to a relaxable macro.  We mustn't put
4208      other uses of such relocations into a variant frag.
4209
4210      To avoid both problems, finish the current frag it contains a
4211      %reloc() operator.  The macro then goes into a new frag.  */
4212   if (prev_reloc_op_frag == frag_now)
4213     {
4214       frag_wane (frag_now);
4215       frag_new (0);
4216     }
4217
4218   switch (mask)
4219     {
4220     case M_DABS:
4221       dbl = 1;
4222     case M_ABS:
4223       /* bgez $a0,.+12
4224          move v0,$a0
4225          sub v0,$zero,$a0
4226          */
4227
4228       mips_emit_delays (TRUE);
4229       ++mips_opts.noreorder;
4230       mips_any_noreorder = 1;
4231
4232       expr1.X_add_number = 8;
4233       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4234       if (dreg == sreg)
4235         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4236                      0);
4237       else
4238         move_register (&icnt, dreg, sreg);
4239       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4240                    dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4241
4242       --mips_opts.noreorder;
4243       return;
4244
4245     case M_ADD_I:
4246       s = "addi";
4247       s2 = "add";
4248       goto do_addi;
4249     case M_ADDU_I:
4250       s = "addiu";
4251       s2 = "addu";
4252       goto do_addi;
4253     case M_DADD_I:
4254       dbl = 1;
4255       s = "daddi";
4256       s2 = "dadd";
4257       goto do_addi;
4258     case M_DADDU_I:
4259       dbl = 1;
4260       s = "daddiu";
4261       s2 = "daddu";
4262     do_addi:
4263       if (imm_expr.X_op == O_constant
4264           && imm_expr.X_add_number >= -0x8000
4265           && imm_expr.X_add_number < 0x8000)
4266         {
4267           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4268                        (int) BFD_RELOC_LO16);
4269           return;
4270         }
4271       load_register (&icnt, AT, &imm_expr, dbl);
4272       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4273                    treg, sreg, AT);
4274       break;
4275
4276     case M_AND_I:
4277       s = "andi";
4278       s2 = "and";
4279       goto do_bit;
4280     case M_OR_I:
4281       s = "ori";
4282       s2 = "or";
4283       goto do_bit;
4284     case M_NOR_I:
4285       s = "";
4286       s2 = "nor";
4287       goto do_bit;
4288     case M_XOR_I:
4289       s = "xori";
4290       s2 = "xor";
4291     do_bit:
4292       if (imm_expr.X_op == O_constant
4293           && imm_expr.X_add_number >= 0
4294           && imm_expr.X_add_number < 0x10000)
4295         {
4296           if (mask != M_NOR_I)
4297             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4298                          sreg, (int) BFD_RELOC_LO16);
4299           else
4300             {
4301               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4302                            treg, sreg, (int) BFD_RELOC_LO16);
4303               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4304                            "d,v,t", treg, treg, 0);
4305             }
4306           return;
4307         }
4308
4309       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4310       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4311                    treg, sreg, AT);
4312       break;
4313
4314     case M_BEQ_I:
4315       s = "beq";
4316       goto beq_i;
4317     case M_BEQL_I:
4318       s = "beql";
4319       likely = 1;
4320       goto beq_i;
4321     case M_BNE_I:
4322       s = "bne";
4323       goto beq_i;
4324     case M_BNEL_I:
4325       s = "bnel";
4326       likely = 1;
4327     beq_i:
4328       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4329         {
4330           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4331                        0);
4332           return;
4333         }
4334       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4335       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4336       break;
4337
4338     case M_BGEL:
4339       likely = 1;
4340     case M_BGE:
4341       if (treg == 0)
4342         {
4343           macro_build ((char *) NULL, &icnt, &offset_expr,
4344                        likely ? "bgezl" : "bgez", "s,p", sreg);
4345           return;
4346         }
4347       if (sreg == 0)
4348         {
4349           macro_build ((char *) NULL, &icnt, &offset_expr,
4350                        likely ? "blezl" : "blez", "s,p", treg);
4351           return;
4352         }
4353       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4354                    AT, sreg, treg);
4355       macro_build ((char *) NULL, &icnt, &offset_expr,
4356                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4357       break;
4358
4359     case M_BGTL_I:
4360       likely = 1;
4361     case M_BGT_I:
4362       /* check for > max integer */
4363       maxnum = 0x7fffffff;
4364       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4365         {
4366           maxnum <<= 16;
4367           maxnum |= 0xffff;
4368           maxnum <<= 16;
4369           maxnum |= 0xffff;
4370         }
4371       if (imm_expr.X_op == O_constant
4372           && imm_expr.X_add_number >= maxnum
4373           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4374         {
4375         do_false:
4376           /* result is always false */
4377           if (! likely)
4378             {
4379               if (warn_nops)
4380                 as_warn (_("Branch %s is always false (nop)"),
4381                          ip->insn_mo->name);
4382               macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4383                            "", 0);
4384             }
4385           else
4386             {
4387               if (warn_nops)
4388                 as_warn (_("Branch likely %s is always false"),
4389                          ip->insn_mo->name);
4390               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4391                            "s,t,p", 0, 0);
4392             }
4393           return;
4394         }
4395       if (imm_expr.X_op != O_constant)
4396         as_bad (_("Unsupported large constant"));
4397       ++imm_expr.X_add_number;
4398       /* FALLTHROUGH */
4399     case M_BGE_I:
4400     case M_BGEL_I:
4401       if (mask == M_BGEL_I)
4402         likely = 1;
4403       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4404         {
4405           macro_build ((char *) NULL, &icnt, &offset_expr,
4406                        likely ? "bgezl" : "bgez", "s,p", sreg);
4407           return;
4408         }
4409       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4410         {
4411           macro_build ((char *) NULL, &icnt, &offset_expr,
4412                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4413           return;
4414         }
4415       maxnum = 0x7fffffff;
4416       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4417         {
4418           maxnum <<= 16;
4419           maxnum |= 0xffff;
4420           maxnum <<= 16;
4421           maxnum |= 0xffff;
4422         }
4423       maxnum = - maxnum - 1;
4424       if (imm_expr.X_op == O_constant
4425           && imm_expr.X_add_number <= maxnum
4426           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4427         {
4428         do_true:
4429           /* result is always true */
4430           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4431           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4432           return;
4433         }
4434       set_at (&icnt, sreg, 0);
4435       macro_build ((char *) NULL, &icnt, &offset_expr,
4436                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4437       break;
4438
4439     case M_BGEUL:
4440       likely = 1;
4441     case M_BGEU:
4442       if (treg == 0)
4443         goto do_true;
4444       if (sreg == 0)
4445         {
4446           macro_build ((char *) NULL, &icnt, &offset_expr,
4447                        likely ? "beql" : "beq", "s,t,p", 0, treg);
4448           return;
4449         }
4450       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4451                    "d,v,t", AT, sreg, treg);
4452       macro_build ((char *) NULL, &icnt, &offset_expr,
4453                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4454       break;
4455
4456     case M_BGTUL_I:
4457       likely = 1;
4458     case M_BGTU_I:
4459       if (sreg == 0
4460           || (HAVE_32BIT_GPRS
4461               && imm_expr.X_op == O_constant
4462               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4463         goto do_false;
4464       if (imm_expr.X_op != O_constant)
4465         as_bad (_("Unsupported large constant"));
4466       ++imm_expr.X_add_number;
4467       /* FALLTHROUGH */
4468     case M_BGEU_I:
4469     case M_BGEUL_I:
4470       if (mask == M_BGEUL_I)
4471         likely = 1;
4472       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4473         goto do_true;
4474       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4475         {
4476           macro_build ((char *) NULL, &icnt, &offset_expr,
4477                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4478           return;
4479         }
4480       set_at (&icnt, sreg, 1);
4481       macro_build ((char *) NULL, &icnt, &offset_expr,
4482                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4483       break;
4484
4485     case M_BGTL:
4486       likely = 1;
4487     case M_BGT:
4488       if (treg == 0)
4489         {
4490           macro_build ((char *) NULL, &icnt, &offset_expr,
4491                        likely ? "bgtzl" : "bgtz", "s,p", sreg);
4492           return;
4493         }
4494       if (sreg == 0)
4495         {
4496           macro_build ((char *) NULL, &icnt, &offset_expr,
4497                        likely ? "bltzl" : "bltz", "s,p", treg);
4498           return;
4499         }
4500       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4501                    AT, treg, sreg);
4502       macro_build ((char *) NULL, &icnt, &offset_expr,
4503                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4504       break;
4505
4506     case M_BGTUL:
4507       likely = 1;
4508     case M_BGTU:
4509       if (treg == 0)
4510         {
4511           macro_build ((char *) NULL, &icnt, &offset_expr,
4512                        likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4513           return;
4514         }
4515       if (sreg == 0)
4516         goto do_false;
4517       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4518                    "d,v,t", AT, treg, sreg);
4519       macro_build ((char *) NULL, &icnt, &offset_expr,
4520                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4521       break;
4522
4523     case M_BLEL:
4524       likely = 1;
4525     case M_BLE:
4526       if (treg == 0)
4527         {
4528           macro_build ((char *) NULL, &icnt, &offset_expr,
4529                        likely ? "blezl" : "blez", "s,p", sreg);
4530           return;
4531         }
4532       if (sreg == 0)
4533         {
4534           macro_build ((char *) NULL, &icnt, &offset_expr,
4535                        likely ? "bgezl" : "bgez", "s,p", treg);
4536           return;
4537         }
4538       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4539                    AT, treg, sreg);
4540       macro_build ((char *) NULL, &icnt, &offset_expr,
4541                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4542       break;
4543
4544     case M_BLEL_I:
4545       likely = 1;
4546     case M_BLE_I:
4547       maxnum = 0x7fffffff;
4548       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4549         {
4550           maxnum <<= 16;
4551           maxnum |= 0xffff;
4552           maxnum <<= 16;
4553           maxnum |= 0xffff;
4554         }
4555       if (imm_expr.X_op == O_constant
4556           && imm_expr.X_add_number >= maxnum
4557           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4558         goto do_true;
4559       if (imm_expr.X_op != O_constant)
4560         as_bad (_("Unsupported large constant"));
4561       ++imm_expr.X_add_number;
4562       /* FALLTHROUGH */
4563     case M_BLT_I:
4564     case M_BLTL_I:
4565       if (mask == M_BLTL_I)
4566         likely = 1;
4567       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4568         {
4569           macro_build ((char *) NULL, &icnt, &offset_expr,
4570                        likely ? "bltzl" : "bltz", "s,p", sreg);
4571           return;
4572         }
4573       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4574         {
4575           macro_build ((char *) NULL, &icnt, &offset_expr,
4576                        likely ? "blezl" : "blez", "s,p", sreg);
4577           return;
4578         }
4579       set_at (&icnt, sreg, 0);
4580       macro_build ((char *) NULL, &icnt, &offset_expr,
4581                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4582       break;
4583
4584     case M_BLEUL:
4585       likely = 1;
4586     case M_BLEU:
4587       if (treg == 0)
4588         {
4589           macro_build ((char *) NULL, &icnt, &offset_expr,
4590                        likely ? "beql" : "beq", "s,t,p", sreg, 0);
4591           return;
4592         }
4593       if (sreg == 0)
4594         goto do_true;
4595       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4596                    "d,v,t", AT, treg, sreg);
4597       macro_build ((char *) NULL, &icnt, &offset_expr,
4598                    likely ? "beql" : "beq", "s,t,p", AT, 0);
4599       break;
4600
4601     case M_BLEUL_I:
4602       likely = 1;
4603     case M_BLEU_I:
4604       if (sreg == 0
4605           || (HAVE_32BIT_GPRS
4606               && imm_expr.X_op == O_constant
4607               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4608         goto do_true;
4609       if (imm_expr.X_op != O_constant)
4610         as_bad (_("Unsupported large constant"));
4611       ++imm_expr.X_add_number;
4612       /* FALLTHROUGH */
4613     case M_BLTU_I:
4614     case M_BLTUL_I:
4615       if (mask == M_BLTUL_I)
4616         likely = 1;
4617       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4618         goto do_false;
4619       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4620         {
4621           macro_build ((char *) NULL, &icnt, &offset_expr,
4622                        likely ? "beql" : "beq",
4623                        "s,t,p", sreg, 0);
4624           return;
4625         }
4626       set_at (&icnt, sreg, 1);
4627       macro_build ((char *) NULL, &icnt, &offset_expr,
4628                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4629       break;
4630
4631     case M_BLTL:
4632       likely = 1;
4633     case M_BLT:
4634       if (treg == 0)
4635         {
4636           macro_build ((char *) NULL, &icnt, &offset_expr,
4637                        likely ? "bltzl" : "bltz", "s,p", sreg);
4638           return;
4639         }
4640       if (sreg == 0)
4641         {
4642           macro_build ((char *) NULL, &icnt, &offset_expr,
4643                        likely ? "bgtzl" : "bgtz", "s,p", treg);
4644           return;
4645         }
4646       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4647                    AT, sreg, treg);
4648       macro_build ((char *) NULL, &icnt, &offset_expr,
4649                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4650       break;
4651
4652     case M_BLTUL:
4653       likely = 1;
4654     case M_BLTU:
4655       if (treg == 0)
4656         goto do_false;
4657       if (sreg == 0)
4658         {
4659           macro_build ((char *) NULL, &icnt, &offset_expr,
4660                        likely ? "bnel" : "bne", "s,t,p", 0, treg);
4661           return;
4662         }
4663       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4664                    "d,v,t", AT, sreg,
4665                    treg);
4666       macro_build ((char *) NULL, &icnt, &offset_expr,
4667                    likely ? "bnel" : "bne", "s,t,p", AT, 0);
4668       break;
4669
4670     case M_DDIV_3:
4671       dbl = 1;
4672     case M_DIV_3:
4673       s = "mflo";
4674       goto do_div3;
4675     case M_DREM_3:
4676       dbl = 1;
4677     case M_REM_3:
4678       s = "mfhi";
4679     do_div3:
4680       if (treg == 0)
4681         {
4682           as_warn (_("Divide by zero."));
4683           if (mips_trap)
4684             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4685                          "s,t,q", 0, 0, 7);
4686           else
4687             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4688                          "c", 7);
4689           return;
4690         }
4691
4692       mips_emit_delays (TRUE);
4693       ++mips_opts.noreorder;
4694       mips_any_noreorder = 1;
4695       if (mips_trap)
4696         {
4697           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4698                        "s,t,q", treg, 0, 7);
4699           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4700                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4701         }
4702       else
4703         {
4704           expr1.X_add_number = 8;
4705           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4706           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4707                        dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4708           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4709                        "c", 7);
4710         }
4711       expr1.X_add_number = -1;
4712       macro_build ((char *) NULL, &icnt, &expr1,
4713                    dbl ? "daddiu" : "addiu",
4714                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4715       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4716       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4717       if (dbl)
4718         {
4719           expr1.X_add_number = 1;
4720           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4721                        (int) BFD_RELOC_LO16);
4722           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4723                        "d,w,<", AT, AT, 31);
4724         }
4725       else
4726         {
4727           expr1.X_add_number = 0x80000000;
4728           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4729                        (int) BFD_RELOC_HI16);
4730         }
4731       if (mips_trap)
4732         {
4733           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4734                        "s,t,q", sreg, AT, 6);
4735           /* We want to close the noreorder block as soon as possible, so
4736              that later insns are available for delay slot filling.  */
4737           --mips_opts.noreorder;
4738         }
4739       else
4740         {
4741           expr1.X_add_number = 8;
4742           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4743           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4744                        0);
4745
4746           /* We want to close the noreorder block as soon as possible, so
4747              that later insns are available for delay slot filling.  */
4748           --mips_opts.noreorder;
4749
4750           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4751                        "c", 6);
4752         }
4753       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4754       break;
4755
4756     case M_DIV_3I:
4757       s = "div";
4758       s2 = "mflo";
4759       goto do_divi;
4760     case M_DIVU_3I:
4761       s = "divu";
4762       s2 = "mflo";
4763       goto do_divi;
4764     case M_REM_3I:
4765       s = "div";
4766       s2 = "mfhi";
4767       goto do_divi;
4768     case M_REMU_3I:
4769       s = "divu";
4770       s2 = "mfhi";
4771       goto do_divi;
4772     case M_DDIV_3I:
4773       dbl = 1;
4774       s = "ddiv";
4775       s2 = "mflo";
4776       goto do_divi;
4777     case M_DDIVU_3I:
4778       dbl = 1;
4779       s = "ddivu";
4780       s2 = "mflo";
4781       goto do_divi;
4782     case M_DREM_3I:
4783       dbl = 1;
4784       s = "ddiv";
4785       s2 = "mfhi";
4786       goto do_divi;
4787     case M_DREMU_3I:
4788       dbl = 1;
4789       s = "ddivu";
4790       s2 = "mfhi";
4791     do_divi:
4792       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4793         {
4794           as_warn (_("Divide by zero."));
4795           if (mips_trap)
4796             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4797                          "s,t,q", 0, 0, 7);
4798           else
4799             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4800                          "c", 7);
4801           return;
4802         }
4803       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4804         {
4805           if (strcmp (s2, "mflo") == 0)
4806             move_register (&icnt, dreg, sreg);
4807           else
4808             move_register (&icnt, dreg, 0);
4809           return;
4810         }
4811       if (imm_expr.X_op == O_constant
4812           && imm_expr.X_add_number == -1
4813           && s[strlen (s) - 1] != 'u')
4814         {
4815           if (strcmp (s2, "mflo") == 0)
4816             {
4817               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4818                            dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4819             }
4820           else
4821             move_register (&icnt, dreg, 0);
4822           return;
4823         }
4824
4825       load_register (&icnt, AT, &imm_expr, dbl);
4826       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4827                    sreg, AT);
4828       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4829       break;
4830
4831     case M_DIVU_3:
4832       s = "divu";
4833       s2 = "mflo";
4834       goto do_divu3;
4835     case M_REMU_3:
4836       s = "divu";
4837       s2 = "mfhi";
4838       goto do_divu3;
4839     case M_DDIVU_3:
4840       s = "ddivu";
4841       s2 = "mflo";
4842       goto do_divu3;
4843     case M_DREMU_3:
4844       s = "ddivu";
4845       s2 = "mfhi";
4846     do_divu3:
4847       mips_emit_delays (TRUE);
4848       ++mips_opts.noreorder;
4849       mips_any_noreorder = 1;
4850       if (mips_trap)
4851         {
4852           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4853                        "s,t,q", treg, 0, 7);
4854           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4855                        sreg, treg);
4856           /* We want to close the noreorder block as soon as possible, so
4857              that later insns are available for delay slot filling.  */
4858           --mips_opts.noreorder;
4859         }
4860       else
4861         {
4862           expr1.X_add_number = 8;
4863           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4864           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4865                        sreg, treg);
4866
4867           /* We want to close the noreorder block as soon as possible, so
4868              that later insns are available for delay slot filling.  */
4869           --mips_opts.noreorder;
4870           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4871                        "c", 7);
4872         }
4873       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4874       return;
4875
4876     case M_DLA_AB:
4877       dbl = 1;
4878     case M_LA_AB:
4879       /* Load the address of a symbol into a register.  If breg is not
4880          zero, we then add a base register to it.  */
4881
4882       if (dbl && HAVE_32BIT_GPRS)
4883         as_warn (_("dla used to load 32-bit register"));
4884
4885       if (! dbl && HAVE_64BIT_OBJECTS)
4886         as_warn (_("la used to load 64-bit address"));
4887
4888       if (offset_expr.X_op == O_constant
4889           && offset_expr.X_add_number >= -0x8000
4890           && offset_expr.X_add_number < 0x8000)
4891         {
4892           macro_build ((char *) NULL, &icnt, &offset_expr,
4893                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" :
4894                        HAVE_NEWABI ? "addi" : "addiu",
4895                        "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4896           return;
4897         }
4898
4899       if (treg == breg)
4900         {
4901           tempreg = AT;
4902           used_at = 1;
4903         }
4904       else
4905         {
4906           tempreg = treg;
4907           used_at = 0;
4908         }
4909
4910       /* When generating embedded PIC code, we permit expressions of
4911          the form
4912            la   $treg,foo-bar
4913            la   $treg,foo-bar($breg)
4914          where bar is an address in the current section.  These are used
4915          when getting the addresses of functions.  We don't permit
4916          X_add_number to be non-zero, because if the symbol is
4917          external the relaxing code needs to know that any addend is
4918          purely the offset to X_op_symbol.  */
4919       if (mips_pic == EMBEDDED_PIC
4920           && offset_expr.X_op == O_subtract
4921           && (symbol_constant_p (offset_expr.X_op_symbol)
4922               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4923               : (symbol_equated_p (offset_expr.X_op_symbol)
4924                  && (S_GET_SEGMENT
4925                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4926                       ->X_add_symbol)
4927                      == now_seg)))
4928           && (offset_expr.X_add_number == 0
4929               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4930         {
4931           if (breg == 0)
4932             {
4933               tempreg = treg;
4934               used_at = 0;
4935               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4936                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4937             }
4938           else
4939             {
4940               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4941                            tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4942               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4943                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4944                            "d,v,t", tempreg, tempreg, breg);
4945             }
4946           macro_build ((char *) NULL, &icnt, &offset_expr,
4947                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4948                        "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4949           if (! used_at)
4950             return;
4951           break;
4952         }
4953
4954       if (offset_expr.X_op != O_symbol
4955           && offset_expr.X_op != O_constant)
4956         {
4957           as_bad (_("expression too complex"));
4958           offset_expr.X_op = O_constant;
4959         }
4960
4961       if (offset_expr.X_op == O_constant)
4962         load_register (&icnt, tempreg, &offset_expr,
4963                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4964                         ? (dbl || HAVE_64BIT_ADDRESSES)
4965                         : HAVE_64BIT_ADDRESSES));
4966       else if (mips_pic == NO_PIC)
4967         {
4968           /* If this is a reference to a GP relative symbol, we want
4969                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4970              Otherwise we want
4971                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4972                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4973              If we have a constant, we need two instructions anyhow,
4974              so we may as well always use the latter form.
4975
4976             With 64bit address space and a usable $at we want
4977               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4978               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4979               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4980               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4981               dsll32    $tempreg,0
4982               daddu     $tempreg,$tempreg,$at
4983
4984             If $at is already in use, we use a path which is suboptimal
4985             on superscalar processors.
4986               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4987               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4988               dsll      $tempreg,16
4989               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4990               dsll      $tempreg,16
4991               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4992           */
4993           char *p = NULL;
4994           if (HAVE_64BIT_ADDRESSES)
4995             {
4996               /* We don't do GP optimization for now because RELAX_ENCODE can't
4997                  hold the data for such large chunks.  */
4998
4999               if (used_at == 0 && ! mips_opts.noat)
5000                 {
5001                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5002                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5003                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5004                                AT, (int) BFD_RELOC_HI16_S);
5005                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5006                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5007                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5008                                AT, AT, (int) BFD_RELOC_LO16);
5009                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5010                                "d,w,<", tempreg, tempreg, 0);
5011                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5012                                "d,v,t", tempreg, tempreg, AT);
5013                   used_at = 1;
5014                 }
5015               else
5016                 {
5017                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5018                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5019                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5020                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5021                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5022                                tempreg, tempreg, 16);
5023                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5024                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5025                   macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5026                                tempreg, tempreg, 16);
5027                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5028                                tempreg, tempreg, (int) BFD_RELOC_LO16);
5029                 }
5030             }
5031           else
5032             {
5033               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5034                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5035                 {
5036                   frag_grow (20);
5037                   macro_build ((char *) NULL, &icnt, &offset_expr,
5038                                HAVE_NEWABI ? "addi" : "addiu",
5039                                "t,r,j", tempreg, mips_gp_register,
5040                                (int) BFD_RELOC_GPREL16);
5041                   p = frag_var (rs_machine_dependent, 8, 0,
5042                                 RELAX_ENCODE (4, 8, 0, 4, 0,
5043                                               mips_opts.warn_about_macros),
5044                                 offset_expr.X_add_symbol, 0, NULL);
5045                 }
5046               macro_build_lui (p, &icnt, &offset_expr, tempreg);
5047               if (p != NULL)
5048                 p += 4;
5049               macro_build (p, &icnt, &offset_expr,
5050                            HAVE_NEWABI ? "addi" : "addiu",
5051                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5052             }
5053         }
5054       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5055         {
5056           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5057
5058           /* If this is a reference to an external symbol, and there
5059              is no constant, we want
5060                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5061              or if tempreg is PIC_CALL_REG
5062                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5063              For a local symbol, we want
5064                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5065                nop
5066                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5067
5068              If we have a small constant, and this is a reference to
5069              an external symbol, we want
5070                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5071                nop
5072                addiu    $tempreg,$tempreg,<constant>
5073              For a local symbol, we want the same instruction
5074              sequence, but we output a BFD_RELOC_LO16 reloc on the
5075              addiu instruction.
5076
5077              If we have a large constant, and this is a reference to
5078              an external symbol, we want
5079                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5080                lui      $at,<hiconstant>
5081                addiu    $at,$at,<loconstant>
5082                addu     $tempreg,$tempreg,$at
5083              For a local symbol, we want the same instruction
5084              sequence, but we output a BFD_RELOC_LO16 reloc on the
5085              addiu instruction.
5086            */
5087
5088           expr1.X_add_number = offset_expr.X_add_number;
5089           offset_expr.X_add_number = 0;
5090           frag_grow (32);
5091           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5092             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5093           macro_build ((char *) NULL, &icnt, &offset_expr,
5094                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5095                        "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5096           if (expr1.X_add_number == 0)
5097             {
5098               int off;
5099               char *p;
5100
5101               if (breg == 0)
5102                 off = 0;
5103               else
5104                 {
5105                   /* We're going to put in an addu instruction using
5106                      tempreg, so we may as well insert the nop right
5107                      now.  */
5108                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5109                                "nop", "");
5110                   off = 4;
5111                 }
5112               p = frag_var (rs_machine_dependent, 8 - off, 0,
5113                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5114                                           (breg == 0
5115                                            ? mips_opts.warn_about_macros
5116                                            : 0)),
5117                             offset_expr.X_add_symbol, 0, NULL);
5118               if (breg == 0)
5119                 {
5120                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5121                   p += 4;
5122                 }
5123               macro_build (p, &icnt, &expr1,
5124                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5125                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5126               /* FIXME: If breg == 0, and the next instruction uses
5127                  $tempreg, then if this variant case is used an extra
5128                  nop will be generated.  */
5129             }
5130           else if (expr1.X_add_number >= -0x8000
5131                    && expr1.X_add_number < 0x8000)
5132             {
5133               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5134                            "nop", "");
5135               macro_build ((char *) NULL, &icnt, &expr1,
5136                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5137                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5138               frag_var (rs_machine_dependent, 0, 0,
5139                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5140                         offset_expr.X_add_symbol, 0, NULL);
5141             }
5142           else
5143             {
5144               int off1;
5145
5146               /* If we are going to add in a base register, and the
5147                  target register and the base register are the same,
5148                  then we are using AT as a temporary register.  Since
5149                  we want to load the constant into AT, we add our
5150                  current AT (from the global offset table) and the
5151                  register into the register now, and pretend we were
5152                  not using a base register.  */
5153               if (breg != treg)
5154                 off1 = 0;
5155               else
5156                 {
5157                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5158                                "nop", "");
5159                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5160                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5161                                "d,v,t", treg, AT, breg);
5162                   breg = 0;
5163                   tempreg = treg;
5164                   off1 = -8;
5165                 }
5166
5167               /* Set mips_optimize around the lui instruction to avoid
5168                  inserting an unnecessary nop after the lw.  */
5169               hold_mips_optimize = mips_optimize;
5170               mips_optimize = 2;
5171               macro_build_lui (NULL, &icnt, &expr1, AT);
5172               mips_optimize = hold_mips_optimize;
5173
5174               macro_build ((char *) NULL, &icnt, &expr1,
5175                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5176                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5177               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5178                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5179                            "d,v,t", tempreg, tempreg, AT);
5180               frag_var (rs_machine_dependent, 0, 0,
5181                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5182                         offset_expr.X_add_symbol, 0, NULL);
5183               used_at = 1;
5184             }
5185         }
5186       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5187         {
5188           char *p = NULL;
5189           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5190           int adj = 0;
5191
5192           /* If this is a reference to an external, and there is no
5193              constant, or local symbol (*), with or without a
5194              constant, we want
5195                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5196              or if tempreg is PIC_CALL_REG
5197                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5198
5199              If we have a small constant, and this is a reference to
5200              an external symbol, we want
5201                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5202                addiu    $tempreg,$tempreg,<constant>
5203
5204              If we have a large constant, and this is a reference to
5205              an external symbol, we want
5206                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5207                lui      $at,<hiconstant>
5208                addiu    $at,$at,<loconstant>
5209                addu     $tempreg,$tempreg,$at
5210
5211              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5212              local symbols, even though it introduces an additional
5213              instruction.  */
5214
5215           frag_grow (28);
5216           if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5217             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5218           if (offset_expr.X_add_number)
5219             {
5220               frag_now->tc_frag_data.tc_fr_offset =
5221                 expr1.X_add_number = offset_expr.X_add_number;
5222               offset_expr.X_add_number = 0;
5223
5224               macro_build ((char *) NULL, &icnt, &offset_expr,
5225                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5226                            "t,o(b)", tempreg, lw_reloc_type,
5227                            mips_gp_register);
5228
5229               if (expr1.X_add_number >= -0x8000
5230                   && expr1.X_add_number < 0x8000)
5231                 {
5232                   macro_build ((char *) NULL, &icnt, &expr1,
5233                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5234                                "t,r,j", tempreg, tempreg,
5235                                (int) BFD_RELOC_LO16);
5236                   p = frag_var (rs_machine_dependent, 4, 0,
5237                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5238                                 offset_expr.X_add_symbol, 0, NULL);
5239                 }
5240               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5241                 {
5242                   int dreg;
5243
5244                   /* If we are going to add in a base register, and the
5245                      target register and the base register are the same,
5246                      then we are using AT as a temporary register.  Since
5247                      we want to load the constant into AT, we add our
5248                      current AT (from the global offset table) and the
5249                      register into the register now, and pretend we were
5250                      not using a base register.  */
5251                   if (breg != treg)
5252                     dreg = tempreg;
5253                   else
5254                     {
5255                       assert (tempreg == AT);
5256                       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5257                                    HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5258                                    "d,v,t", treg, AT, breg);
5259                       dreg = treg;
5260                       adj = 4;
5261                     }
5262
5263                   macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5264                   macro_build ((char *) NULL, &icnt, &expr1,
5265                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5266                                "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5267                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5268                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5269                                "d,v,t", dreg, dreg, AT);
5270
5271                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
5272                                 RELAX_ENCODE (16 + adj, 4 + adj,
5273                                               0, 0, 0, 0),
5274                                 offset_expr.X_add_symbol, 0, NULL);
5275
5276                   used_at = 1;
5277                 }
5278               else
5279                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5280
5281               offset_expr.X_add_number = expr1.X_add_number;
5282
5283               macro_build (p, &icnt, &offset_expr,
5284                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5285                            "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_DISP,
5286                            mips_gp_register);
5287               if (adj)
5288                 {
5289                   macro_build (p + 4, &icnt, (expressionS *) NULL,
5290                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5291                                "d,v,t", treg, tempreg, breg);
5292                   breg = 0;
5293                   tempreg = treg;
5294                 }
5295             }
5296           else
5297             {
5298               macro_build ((char *) NULL, &icnt, &offset_expr,
5299                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5300                            "t,o(b)", tempreg, lw_reloc_type,
5301                            mips_gp_register);
5302               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5303                 p = frag_var (rs_machine_dependent, 0, 0,
5304                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5305                               offset_expr.X_add_symbol, 0, NULL);
5306             }
5307
5308           if (! p)
5309             {
5310               /* To avoid confusion in tc_gen_reloc, we must ensure
5311                  that this does not become a variant frag.  */
5312               frag_wane (frag_now);
5313               frag_new (0);
5314             }
5315         }
5316       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5317         {
5318           int gpdel;
5319           char *p;
5320           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5321           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5322           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5323
5324           /* This is the large GOT case.  If this is a reference to an
5325              external symbol, and there is no constant, we want
5326                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5327                addu     $tempreg,$tempreg,$gp
5328                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5329              or if tempreg is PIC_CALL_REG
5330                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5331                addu     $tempreg,$tempreg,$gp
5332                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5333              For a local symbol, we want
5334                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5335                nop
5336                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5337
5338              If we have a small constant, and this is a reference to
5339              an external symbol, we want
5340                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5341                addu     $tempreg,$tempreg,$gp
5342                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5343                nop
5344                addiu    $tempreg,$tempreg,<constant>
5345              For a local symbol, we want
5346                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5347                nop
5348                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5349
5350              If we have a large constant, and this is a reference to
5351              an external symbol, we want
5352                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5353                addu     $tempreg,$tempreg,$gp
5354                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5355                lui      $at,<hiconstant>
5356                addiu    $at,$at,<loconstant>
5357                addu     $tempreg,$tempreg,$at
5358              For a local symbol, we want
5359                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5360                lui      $at,<hiconstant>
5361                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5362                addu     $tempreg,$tempreg,$at
5363           */
5364
5365           expr1.X_add_number = offset_expr.X_add_number;
5366           offset_expr.X_add_number = 0;
5367           frag_grow (52);
5368           if (reg_needs_delay (mips_gp_register))
5369             gpdel = 4;
5370           else
5371             gpdel = 0;
5372           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5373             {
5374               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5375               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5376             }
5377           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5378                        tempreg, lui_reloc_type);
5379           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5380                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5381                        "d,v,t", tempreg, tempreg, mips_gp_register);
5382           macro_build ((char *) NULL, &icnt, &offset_expr,
5383                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5384                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5385           if (expr1.X_add_number == 0)
5386             {
5387               int off;
5388
5389               if (breg == 0)
5390                 off = 0;
5391               else
5392                 {
5393                   /* We're going to put in an addu instruction using
5394                      tempreg, so we may as well insert the nop right
5395                      now.  */
5396                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5397                                "nop", "");
5398                   off = 4;
5399                 }
5400
5401               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5402                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5403                                           8 + gpdel, 0,
5404                                           (breg == 0
5405                                            ? mips_opts.warn_about_macros
5406                                            : 0)),
5407                             offset_expr.X_add_symbol, 0, NULL);
5408             }
5409           else if (expr1.X_add_number >= -0x8000
5410                    && expr1.X_add_number < 0x8000)
5411             {
5412               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5413                            "nop", "");
5414               macro_build ((char *) NULL, &icnt, &expr1,
5415                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5416                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5417
5418               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5419                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5420                                           (breg == 0
5421                                            ? mips_opts.warn_about_macros
5422                                            : 0)),
5423                             offset_expr.X_add_symbol, 0, NULL);
5424             }
5425           else
5426             {
5427               int adj, dreg;
5428
5429               /* If we are going to add in a base register, and the
5430                  target register and the base register are the same,
5431                  then we are using AT as a temporary register.  Since
5432                  we want to load the constant into AT, we add our
5433                  current AT (from the global offset table) and the
5434                  register into the register now, and pretend we were
5435                  not using a base register.  */
5436               if (breg != treg)
5437                 {
5438                   adj = 0;
5439                   dreg = tempreg;
5440                 }
5441               else
5442                 {
5443                   assert (tempreg == AT);
5444                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5445                                "nop", "");
5446                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5447                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5448                                "d,v,t", treg, AT, breg);
5449                   dreg = treg;
5450                   adj = 8;
5451                 }
5452
5453               /* Set mips_optimize around the lui instruction to avoid
5454                  inserting an unnecessary nop after the lw.  */
5455               hold_mips_optimize = mips_optimize;
5456               mips_optimize = 2;
5457               macro_build_lui (NULL, &icnt, &expr1, AT);
5458               mips_optimize = hold_mips_optimize;
5459
5460               macro_build ((char *) NULL, &icnt, &expr1,
5461                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5462                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5463               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5464                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5465                            "d,v,t", dreg, dreg, AT);
5466
5467               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5468                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5469                                           8 + gpdel, 0,
5470                                           (breg == 0
5471                                            ? mips_opts.warn_about_macros
5472                                            : 0)),
5473                             offset_expr.X_add_symbol, 0, NULL);
5474
5475               used_at = 1;
5476             }
5477
5478           if (gpdel > 0)
5479             {
5480               /* This is needed because this instruction uses $gp, but
5481                  the first instruction on the main stream does not.  */
5482               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5483               p += 4;
5484             }
5485
5486           macro_build (p, &icnt, &offset_expr,
5487                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5488                        "t,o(b)", tempreg,
5489                        local_reloc_type,
5490                        mips_gp_register);
5491           p += 4;
5492           if (expr1.X_add_number >= -0x8000
5493               && expr1.X_add_number < 0x8000)
5494             {
5495               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5496               p += 4;
5497               macro_build (p, &icnt, &expr1,
5498                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5499                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5500               /* FIXME: If add_number is 0, and there was no base
5501                  register, the external symbol case ended with a load,
5502                  so if the symbol turns out to not be external, and
5503                  the next instruction uses tempreg, an unnecessary nop
5504                  will be inserted.  */
5505             }
5506           else
5507             {
5508               if (breg == treg)
5509                 {
5510                   /* We must add in the base register now, as in the
5511                      external symbol case.  */
5512                   assert (tempreg == AT);
5513                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5514                   p += 4;
5515                   macro_build (p, &icnt, (expressionS *) NULL,
5516                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5517                                "d,v,t", treg, AT, breg);
5518                   p += 4;
5519                   tempreg = treg;
5520                   /* We set breg to 0 because we have arranged to add
5521                      it in in both cases.  */
5522                   breg = 0;
5523                 }
5524
5525               macro_build_lui (p, &icnt, &expr1, AT);
5526               p += 4;
5527               macro_build (p, &icnt, &expr1,
5528                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5529                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5530               p += 4;
5531               macro_build (p, &icnt, (expressionS *) NULL,
5532                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5533                            "d,v,t", tempreg, tempreg, AT);
5534               p += 4;
5535             }
5536         }
5537       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5538         {
5539           char *p = NULL;
5540           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5541           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5542           int adj = 0;
5543
5544           /* This is the large GOT case.  If this is a reference to an
5545              external symbol, and there is no constant, we want
5546                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5547                add      $tempreg,$tempreg,$gp
5548                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5549              or if tempreg is PIC_CALL_REG
5550                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5551                add      $tempreg,$tempreg,$gp
5552                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5553
5554              If we have a small constant, and this is a reference to
5555              an external symbol, we want
5556                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5557                add      $tempreg,$tempreg,$gp
5558                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5559                addi     $tempreg,$tempreg,<constant>
5560
5561              If we have a large constant, and this is a reference to
5562              an external symbol, we want
5563                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5564                addu     $tempreg,$tempreg,$gp
5565                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5566                lui      $at,<hiconstant>
5567                addi     $at,$at,<loconstant>
5568                add      $tempreg,$tempreg,$at
5569
5570              If we have NewABI, and we know it's a local symbol, we want
5571                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5572                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5573              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5574
5575           frag_grow (40);
5576
5577           frag_now->tc_frag_data.tc_fr_offset =
5578             expr1.X_add_number = offset_expr.X_add_number;
5579           offset_expr.X_add_number = 0;
5580
5581           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5582             {
5583               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5584               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5585             }
5586           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5587                        tempreg, lui_reloc_type);
5588           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5589                        HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5590                        "d,v,t", tempreg, tempreg, mips_gp_register);
5591           macro_build ((char *) NULL, &icnt, &offset_expr,
5592                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5593                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5594
5595           if (expr1.X_add_number == 0)
5596             {
5597               p = frag_var (rs_machine_dependent, 8, 0,
5598                             RELAX_ENCODE (12, 8, 0, 4, 0,
5599                                           mips_opts.warn_about_macros),
5600                             offset_expr.X_add_symbol, 0, NULL);
5601             }
5602           else if (expr1.X_add_number >= -0x8000
5603                    && expr1.X_add_number < 0x8000)
5604             {
5605               macro_build ((char *) NULL, &icnt, &expr1,
5606                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5607                            "t,r,j", tempreg, tempreg,
5608                            (int) BFD_RELOC_LO16);
5609               p = frag_var (rs_machine_dependent, 8, 0,
5610                             RELAX_ENCODE (16, 8, 0, 4, 0,
5611                                           mips_opts.warn_about_macros),
5612                             offset_expr.X_add_symbol, 0, NULL);
5613             }
5614           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5615             {
5616               int dreg;
5617
5618               /* If we are going to add in a base register, and the
5619                  target register and the base register are the same,
5620                  then we are using AT as a temporary register.  Since
5621                  we want to load the constant into AT, we add our
5622                  current AT (from the global offset table) and the
5623                  register into the register now, and pretend we were
5624                  not using a base register.  */
5625               if (breg != treg)
5626                 dreg = tempreg;
5627               else
5628                 {
5629                   assert (tempreg == AT);
5630                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5631                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5632                                "d,v,t", treg, AT, breg);
5633                   dreg = treg;
5634                   adj = 4;
5635                 }
5636
5637               /* Set mips_optimize around the lui instruction to avoid
5638                  inserting an unnecessary nop after the lw.  */
5639               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5640               macro_build ((char *) NULL, &icnt, &expr1,
5641                            HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5642                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5643               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5644                            HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5645                            "d,v,t", dreg, dreg, AT);
5646
5647               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5648                             RELAX_ENCODE (24 + adj, 8 + adj,
5649                                           0, 4, 0,
5650                                           (breg == 0
5651                                            ? mips_opts.warn_about_macros
5652                                            : 0)),
5653                             offset_expr.X_add_symbol, 0, NULL);
5654
5655               used_at = 1;
5656             }
5657           else
5658             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5659
5660           offset_expr.X_add_number = expr1.X_add_number;
5661           macro_build (p, &icnt, &offset_expr,
5662                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5663                        tempreg,
5664                        (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5665           macro_build (p + 4, &icnt, &offset_expr,
5666                        HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
5667                        tempreg, tempreg, (int) BFD_RELOC_MIPS_GOT_OFST);
5668           if (adj)
5669             {
5670               macro_build (p + 8, &icnt, (expressionS *) NULL,
5671                            HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5672                            "d,v,t", treg, tempreg, breg);
5673               breg = 0;
5674               tempreg = treg;
5675             }
5676         }
5677       else if (mips_pic == EMBEDDED_PIC)
5678         {
5679           /* We use
5680                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5681              */
5682           macro_build ((char *) NULL, &icnt, &offset_expr,
5683                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5684                        tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5685         }
5686       else
5687         abort ();
5688
5689       if (breg != 0)
5690         {
5691           char *s;
5692
5693           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5694             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" :
5695               HAVE_NEWABI ? "add" : "addu";
5696           else
5697             s = HAVE_64BIT_ADDRESSES ? "daddu" : HAVE_NEWABI ? "add" : "addu";
5698
5699           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5700                        "d,v,t", treg, tempreg, breg);
5701         }
5702
5703       if (! used_at)
5704         return;
5705
5706       break;
5707
5708     case M_J_A:
5709       /* The j instruction may not be used in PIC code, since it
5710          requires an absolute address.  We convert it to a b
5711          instruction.  */
5712       if (mips_pic == NO_PIC)
5713         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5714       else
5715         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5716       return;
5717
5718       /* The jal instructions must be handled as macros because when
5719          generating PIC code they expand to multi-instruction
5720          sequences.  Normally they are simple instructions.  */
5721     case M_JAL_1:
5722       dreg = RA;
5723       /* Fall through.  */
5724     case M_JAL_2:
5725       if (mips_pic == NO_PIC
5726           || mips_pic == EMBEDDED_PIC)
5727         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5728                      "d,s", dreg, sreg);
5729       else if (mips_pic == SVR4_PIC)
5730         {
5731           if (sreg != PIC_CALL_REG)
5732             as_warn (_("MIPS PIC call to register other than $25"));
5733
5734           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5735                        "d,s", dreg, sreg);
5736           if (! HAVE_NEWABI)
5737             {
5738               if (mips_cprestore_offset < 0)
5739                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5740               else
5741                 {
5742                   if (! mips_frame_reg_valid)
5743                     {
5744                       as_warn (_("No .frame pseudo-op used in PIC code"));
5745                       /* Quiet this warning.  */
5746                       mips_frame_reg_valid = 1;
5747                     }
5748                   if (! mips_cprestore_valid)
5749                     {
5750                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5751                       /* Quiet this warning.  */
5752                       mips_cprestore_valid = 1;
5753                     }
5754                   expr1.X_add_number = mips_cprestore_offset;
5755                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5756                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5757                                                 mips_gp_register, mips_frame_reg);
5758                 }
5759             }
5760         }
5761       else
5762         abort ();
5763
5764       return;
5765
5766     case M_JAL_A:
5767       if (mips_pic == NO_PIC)
5768         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5769       else if (mips_pic == SVR4_PIC)
5770         {
5771           char *p;
5772
5773           /* If this is a reference to an external symbol, and we are
5774              using a small GOT, we want
5775                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5776                nop
5777                jalr     $ra,$25
5778                nop
5779                lw       $gp,cprestore($sp)
5780              The cprestore value is set using the .cprestore
5781              pseudo-op.  If we are using a big GOT, we want
5782                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5783                addu     $25,$25,$gp
5784                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5785                nop
5786                jalr     $ra,$25
5787                nop
5788                lw       $gp,cprestore($sp)
5789              If the symbol is not external, we want
5790                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5791                nop
5792                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5793                jalr     $ra,$25
5794                nop
5795                lw $gp,cprestore($sp)
5796
5797              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5798              sequences above, minus nops, unless the symbol is local,
5799              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5800              GOT_DISP.  */
5801           if (HAVE_NEWABI)
5802             {
5803               if (! mips_big_got)
5804                 {
5805                   frag_grow (4);
5806                   macro_build ((char *) NULL, &icnt, &offset_expr,
5807                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5808                                "t,o(b)", PIC_CALL_REG,
5809                                (int) BFD_RELOC_MIPS_CALL16,
5810                                mips_gp_register);
5811                   frag_var (rs_machine_dependent, 0, 0,
5812                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5813                             offset_expr.X_add_symbol, 0, NULL);
5814                 }
5815               else
5816                 {
5817                   frag_grow (20);
5818                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5819                                "t,u", PIC_CALL_REG,
5820                                (int) BFD_RELOC_MIPS_CALL_HI16);
5821                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5822                                HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5823                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5824                                mips_gp_register);
5825                   macro_build ((char *) NULL, &icnt, &offset_expr,
5826                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5827                                "t,o(b)", PIC_CALL_REG,
5828                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5829                   p = frag_var (rs_machine_dependent, 8, 0,
5830                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5831                                 offset_expr.X_add_symbol, 0, NULL);
5832                   macro_build (p, &icnt, &offset_expr,
5833                                HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5834                                tempreg, (int) BFD_RELOC_MIPS_GOT_PAGE,
5835                                mips_gp_register);
5836                   macro_build (p + 4, &icnt, &offset_expr,
5837                                HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5838                                "t,r,j", tempreg, tempreg,
5839                                (int) BFD_RELOC_MIPS_GOT_OFST);
5840                 }
5841
5842               macro_build_jalr (icnt, &offset_expr);
5843             }
5844           else
5845             {
5846               frag_grow (40);
5847               if (! mips_big_got)
5848                 {
5849                   macro_build ((char *) NULL, &icnt, &offset_expr,
5850                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5851                                "t,o(b)", PIC_CALL_REG,
5852                                (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5853                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5854                                "nop", "");
5855                   p = frag_var (rs_machine_dependent, 4, 0,
5856                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5857                                 offset_expr.X_add_symbol, 0, NULL);
5858                 }
5859               else
5860                 {
5861                   int gpdel;
5862
5863                   if (reg_needs_delay (mips_gp_register))
5864                     gpdel = 4;
5865                   else
5866                     gpdel = 0;
5867                   macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5868                                "t,u", PIC_CALL_REG,
5869                                (int) BFD_RELOC_MIPS_CALL_HI16);
5870                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5871                                HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5872                                "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5873                                mips_gp_register);
5874                   macro_build ((char *) NULL, &icnt, &offset_expr,
5875                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5876                                "t,o(b)", PIC_CALL_REG,
5877                                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5878                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5879                                "nop", "");
5880                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5881                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5882                                               8 + gpdel, 0, 0),
5883                                 offset_expr.X_add_symbol, 0, NULL);
5884                   if (gpdel > 0)
5885                     {
5886                       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5887                       p += 4;
5888                     }
5889                   macro_build (p, &icnt, &offset_expr,
5890                                HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5891                                "t,o(b)", PIC_CALL_REG,
5892                                (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5893                   p += 4;
5894                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5895                   p += 4;
5896                 }
5897               macro_build (p, &icnt, &offset_expr,
5898                            HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5899                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5900                            (int) BFD_RELOC_LO16);
5901               macro_build_jalr (icnt, &offset_expr);
5902
5903               if (mips_cprestore_offset < 0)
5904                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5905               else
5906                 {
5907                   if (! mips_frame_reg_valid)
5908                     {
5909                       as_warn (_("No .frame pseudo-op used in PIC code"));
5910                       /* Quiet this warning.  */
5911                       mips_frame_reg_valid = 1;
5912                     }
5913                   if (! mips_cprestore_valid)
5914                     {
5915                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5916                       /* Quiet this warning.  */
5917                       mips_cprestore_valid = 1;
5918                     }
5919                   if (mips_opts.noreorder)
5920                     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5921                                  "nop", "");
5922                   expr1.X_add_number = mips_cprestore_offset;
5923                   macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5924                                                 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5925                                                 mips_gp_register, mips_frame_reg);
5926                 }
5927             }
5928         }
5929       else if (mips_pic == EMBEDDED_PIC)
5930         {
5931           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5932           /* The linker may expand the call to a longer sequence which
5933              uses $at, so we must break rather than return.  */
5934           break;
5935         }
5936       else
5937         abort ();
5938
5939       return;
5940
5941     case M_LB_AB:
5942       s = "lb";
5943       goto ld;
5944     case M_LBU_AB:
5945       s = "lbu";
5946       goto ld;
5947     case M_LH_AB:
5948       s = "lh";
5949       goto ld;
5950     case M_LHU_AB:
5951       s = "lhu";
5952       goto ld;
5953     case M_LW_AB:
5954       s = "lw";
5955       goto ld;
5956     case M_LWC0_AB:
5957       s = "lwc0";
5958       /* Itbl support may require additional care here.  */
5959       coproc = 1;
5960       goto ld;
5961     case M_LWC1_AB:
5962       s = "lwc1";
5963       /* Itbl support may require additional care here.  */
5964       coproc = 1;
5965       goto ld;
5966     case M_LWC2_AB:
5967       s = "lwc2";
5968       /* Itbl support may require additional care here.  */
5969       coproc = 1;
5970       goto ld;
5971     case M_LWC3_AB:
5972       s = "lwc3";
5973       /* Itbl support may require additional care here.  */
5974       coproc = 1;
5975       goto ld;
5976     case M_LWL_AB:
5977       s = "lwl";
5978       lr = 1;
5979       goto ld;
5980     case M_LWR_AB:
5981       s = "lwr";
5982       lr = 1;
5983       goto ld;
5984     case M_LDC1_AB:
5985       if (mips_arch == CPU_R4650)
5986         {
5987           as_bad (_("opcode not supported on this processor"));
5988           return;
5989         }
5990       s = "ldc1";
5991       /* Itbl support may require additional care here.  */
5992       coproc = 1;
5993       goto ld;
5994     case M_LDC2_AB:
5995       s = "ldc2";
5996       /* Itbl support may require additional care here.  */
5997       coproc = 1;
5998       goto ld;
5999     case M_LDC3_AB:
6000       s = "ldc3";
6001       /* Itbl support may require additional care here.  */
6002       coproc = 1;
6003       goto ld;
6004     case M_LDL_AB:
6005       s = "ldl";
6006       lr = 1;
6007       goto ld;
6008     case M_LDR_AB:
6009       s = "ldr";
6010       lr = 1;
6011       goto ld;
6012     case M_LL_AB:
6013       s = "ll";
6014       goto ld;
6015     case M_LLD_AB:
6016       s = "lld";
6017       goto ld;
6018     case M_LWU_AB:
6019       s = "lwu";
6020     ld:
6021       if (breg == treg || coproc || lr)
6022         {
6023           tempreg = AT;
6024           used_at = 1;
6025         }
6026       else
6027         {
6028           tempreg = treg;
6029           used_at = 0;
6030         }
6031       goto ld_st;
6032     case M_SB_AB:
6033       s = "sb";
6034       goto st;
6035     case M_SH_AB:
6036       s = "sh";
6037       goto st;
6038     case M_SW_AB:
6039       s = "sw";
6040       goto st;
6041     case M_SWC0_AB:
6042       s = "swc0";
6043       /* Itbl support may require additional care here.  */
6044       coproc = 1;
6045       goto st;
6046     case M_SWC1_AB:
6047       s = "swc1";
6048       /* Itbl support may require additional care here.  */
6049       coproc = 1;
6050       goto st;
6051     case M_SWC2_AB:
6052       s = "swc2";
6053       /* Itbl support may require additional care here.  */
6054       coproc = 1;
6055       goto st;
6056     case M_SWC3_AB:
6057       s = "swc3";
6058       /* Itbl support may require additional care here.  */
6059       coproc = 1;
6060       goto st;
6061     case M_SWL_AB:
6062       s = "swl";
6063       goto st;
6064     case M_SWR_AB:
6065       s = "swr";
6066       goto st;
6067     case M_SC_AB:
6068       s = "sc";
6069       goto st;
6070     case M_SCD_AB:
6071       s = "scd";
6072       goto st;
6073     case M_SDC1_AB:
6074       if (mips_arch == CPU_R4650)
6075         {
6076           as_bad (_("opcode not supported on this processor"));
6077           return;
6078         }
6079       s = "sdc1";
6080       coproc = 1;
6081       /* Itbl support may require additional care here.  */
6082       goto st;
6083     case M_SDC2_AB:
6084       s = "sdc2";
6085       /* Itbl support may require additional care here.  */
6086       coproc = 1;
6087       goto st;
6088     case M_SDC3_AB:
6089       s = "sdc3";
6090       /* Itbl support may require additional care here.  */
6091       coproc = 1;
6092       goto st;
6093     case M_SDL_AB:
6094       s = "sdl";
6095       goto st;
6096     case M_SDR_AB:
6097       s = "sdr";
6098     st:
6099       tempreg = AT;
6100       used_at = 1;
6101     ld_st:
6102       /* Itbl support may require additional care here.  */
6103       if (mask == M_LWC1_AB
6104           || mask == M_SWC1_AB
6105           || mask == M_LDC1_AB
6106           || mask == M_SDC1_AB
6107           || mask == M_L_DAB
6108           || mask == M_S_DAB)
6109         fmt = "T,o(b)";
6110       else if (coproc)
6111         fmt = "E,o(b)";
6112       else
6113         fmt = "t,o(b)";
6114
6115       /* For embedded PIC, we allow loads where the offset is calculated
6116          by subtracting a symbol in the current segment from an unknown
6117          symbol, relative to a base register, e.g.:
6118                 <op>    $treg, <sym>-<localsym>($breg)
6119          This is used by the compiler for switch statements.  */
6120       if (mips_pic == EMBEDDED_PIC
6121           && offset_expr.X_op == O_subtract
6122           && (symbol_constant_p (offset_expr.X_op_symbol)
6123               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
6124               : (symbol_equated_p (offset_expr.X_op_symbol)
6125                  && (S_GET_SEGMENT
6126                      (symbol_get_value_expression (offset_expr.X_op_symbol)
6127                       ->X_add_symbol)
6128                      == now_seg)))
6129           && breg != 0
6130           && (offset_expr.X_add_number == 0
6131               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
6132         {
6133           /* For this case, we output the instructions:
6134                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
6135                 addiu   $tempreg,$tempreg,$breg
6136                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
6137              If the relocation would fit entirely in 16 bits, it would be
6138              nice to emit:
6139                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
6140              instead, but that seems quite difficult.  */
6141           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6142                        tempreg, (int) BFD_RELOC_PCREL_HI16_S);
6143           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6144                        ((bfd_arch_bits_per_address (stdoutput) == 32
6145                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
6146                         ? HAVE_NEWABI ? "add" : "addu" : "daddu"),
6147                        "d,v,t", tempreg, tempreg, breg);
6148           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6149                        (int) BFD_RELOC_PCREL_LO16, tempreg);
6150           if (! used_at)
6151             return;
6152           break;
6153         }
6154
6155       if (offset_expr.X_op != O_constant
6156           && offset_expr.X_op != O_symbol)
6157         {
6158           as_bad (_("expression too complex"));
6159           offset_expr.X_op = O_constant;
6160         }
6161
6162       /* A constant expression in PIC code can be handled just as it
6163          is in non PIC code.  */
6164       if (mips_pic == NO_PIC
6165           || offset_expr.X_op == O_constant)
6166         {
6167           char *p;
6168
6169           /* If this is a reference to a GP relative symbol, and there
6170              is no base register, we want
6171                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6172              Otherwise, if there is no base register, we want
6173                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6174                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6175              If we have a constant, we need two instructions anyhow,
6176              so we always use the latter form.
6177
6178              If we have a base register, and this is a reference to a
6179              GP relative symbol, we want
6180                addu     $tempreg,$breg,$gp
6181                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6182              Otherwise we want
6183                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6184                addu     $tempreg,$tempreg,$breg
6185                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6186              With a constant we always use the latter case.
6187
6188              With 64bit address space and no base register and $at usable,
6189              we want
6190                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6191                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6192                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6193                dsll32   $tempreg,0
6194                daddu    $tempreg,$at
6195                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6196              If we have a base register, we want
6197                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6198                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6199                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6200                daddu    $at,$breg
6201                dsll32   $tempreg,0
6202                daddu    $tempreg,$at
6203                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6204
6205              Without $at we can't generate the optimal path for superscalar
6206              processors here since this would require two temporary registers.
6207                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6208                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6209                dsll     $tempreg,16
6210                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6211                dsll     $tempreg,16
6212                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6213              If we have a base register, we want
6214                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6215                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6216                dsll     $tempreg,16
6217                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6218                dsll     $tempreg,16
6219                daddu    $tempreg,$tempreg,$breg
6220                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6221
6222              If we have 64-bit addresses, as an optimization, for
6223              addresses which are 32-bit constants (e.g. kseg0/kseg1
6224              addresses) we fall back to the 32-bit address generation
6225              mechanism since it is more efficient.  Note that due to
6226              the signed offset used by memory operations, the 32-bit
6227              range is shifted down by 32768 here.  This code should
6228              probably attempt to generate 64-bit constants more
6229              efficiently in general.
6230
6231              As an extension for architectures with 64-bit registers,
6232              we don't truncate 64-bit addresses given as literal
6233              constants down to 32 bits, to support existing practice
6234              in the mips64 Linux (the kernel), that compiles source
6235              files with -mabi=64, assembling them as o32 or n32 (with
6236              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
6237              the whole kernel is loaded into a memory region that is
6238              addressible with sign-extended 32-bit addresses, it is
6239              wasteful to compute the upper 32 bits of every
6240              non-literal address, that takes more space and time.
6241              Some day this should probably be implemented as an
6242              assembler option, such that the kernel doesn't have to
6243              use such ugly hacks, even though it will still have to
6244              end up converting the binary to ELF32 for a number of
6245              platforms whose boot loaders don't support ELF64
6246              binaries.  */
6247           if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
6248               || (offset_expr.X_op == O_constant
6249                   && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
6250                   && HAVE_64BIT_ADDRESS_CONSTANTS))
6251             {
6252               p = NULL;
6253
6254               /* We don't do GP optimization for now because RELAX_ENCODE can't
6255                  hold the data for such large chunks.  */
6256
6257               if (used_at == 0 && ! mips_opts.noat)
6258                 {
6259                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6260                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6261                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6262                                AT, (int) BFD_RELOC_HI16_S);
6263                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6264                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6265                   if (breg != 0)
6266                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6267                                  "d,v,t", AT, AT, breg);
6268                   macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
6269                                "d,w,<", tempreg, tempreg, 0);
6270                   macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6271                                "d,v,t", tempreg, tempreg, AT);
6272                   macro_build (p, &icnt, &offset_expr, s,
6273                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6274                   used_at = 1;
6275                 }
6276               else
6277                 {
6278                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6279                                tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6280                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6281                                tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6282                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6283                                "d,w,<", tempreg, tempreg, 16);
6284                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6285                                tempreg, tempreg, (int) BFD_RELOC_HI16_S);
6286                   macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6287                                "d,w,<", tempreg, tempreg, 16);
6288                   if (breg != 0)
6289                     macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6290                                  "d,v,t", tempreg, tempreg, breg);
6291                   macro_build (p, &icnt, &offset_expr, s,
6292                                fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6293                 }
6294
6295               return;
6296             }
6297           else if (offset_expr.X_op == O_constant
6298                    && !HAVE_64BIT_ADDRESS_CONSTANTS
6299                    && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6300             as_bad (_("load/store address overflow (max 32 bits)"));
6301
6302           if (breg == 0)
6303             {
6304               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6305                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6306                 p = NULL;
6307               else
6308                 {
6309                   frag_grow (20);
6310                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6311                                treg, (int) BFD_RELOC_GPREL16,
6312                                mips_gp_register);
6313                   p = frag_var (rs_machine_dependent, 8, 0,
6314                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6315                                               (mips_opts.warn_about_macros
6316                                                || (used_at
6317                                                    && mips_opts.noat))),
6318                                 offset_expr.X_add_symbol, 0, NULL);
6319                   used_at = 0;
6320                 }
6321               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6322               if (p != NULL)
6323                 p += 4;
6324               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6325                            (int) BFD_RELOC_LO16, tempreg);
6326             }
6327           else
6328             {
6329               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6330                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6331                 p = NULL;
6332               else
6333                 {
6334                   frag_grow (28);
6335                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6336                                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6337                                ? "add" : "addu" : "daddu",
6338                                "d,v,t", tempreg, breg, mips_gp_register);
6339                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6340                                treg, (int) BFD_RELOC_GPREL16, tempreg);
6341                   p = frag_var (rs_machine_dependent, 12, 0,
6342                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6343                                 offset_expr.X_add_symbol, 0, NULL);
6344                 }
6345               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6346               if (p != NULL)
6347                 p += 4;
6348               macro_build (p, &icnt, (expressionS *) NULL,
6349                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6350                            ? "add" : "addu" : "daddu",
6351                            "d,v,t", tempreg, tempreg, breg);
6352               if (p != NULL)
6353                 p += 4;
6354               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6355                            (int) BFD_RELOC_LO16, tempreg);
6356             }
6357         }
6358       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6359         {
6360           char *p;
6361           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6362
6363           /* If this is a reference to an external symbol, we want
6364                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6365                nop
6366                <op>     $treg,0($tempreg)
6367              Otherwise we want
6368                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6369                nop
6370                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6371                <op>     $treg,0($tempreg)
6372
6373              For NewABI, we want
6374                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6375                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6376
6377              If there is a base register, we add it to $tempreg before
6378              the <op>.  If there is a constant, we stick it in the
6379              <op> instruction.  We don't handle constants larger than
6380              16 bits, because we have no way to load the upper 16 bits
6381              (actually, we could handle them for the subset of cases
6382              in which we are not using $at).  */
6383           assert (offset_expr.X_op == O_symbol);
6384           if (HAVE_NEWABI)
6385             {
6386               macro_build ((char *) NULL, &icnt, &offset_expr,
6387                            HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6388                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6389                            mips_gp_register);
6390               if (breg != 0)
6391                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6392                              HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6393                              "d,v,t", tempreg, tempreg, breg);
6394               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6395                            (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6396
6397               if (! used_at)
6398                 return;
6399
6400               break;
6401             }
6402           expr1.X_add_number = offset_expr.X_add_number;
6403           offset_expr.X_add_number = 0;
6404           if (expr1.X_add_number < -0x8000
6405               || expr1.X_add_number >= 0x8000)
6406             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6407           frag_grow (20);
6408           macro_build ((char *) NULL, &icnt, &offset_expr,
6409                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
6410                        (int) lw_reloc_type, mips_gp_register);
6411           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6412           p = frag_var (rs_machine_dependent, 4, 0,
6413                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6414                         offset_expr.X_add_symbol, 0, NULL);
6415           macro_build (p, &icnt, &offset_expr,
6416                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6417                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6418           if (breg != 0)
6419             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6420                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6421                          "d,v,t", tempreg, tempreg, breg);
6422           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6423                        (int) BFD_RELOC_LO16, tempreg);
6424         }
6425       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6426         {
6427           int gpdel;
6428           char *p;
6429
6430           /* If this is a reference to an external symbol, we want
6431                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6432                addu     $tempreg,$tempreg,$gp
6433                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6434                <op>     $treg,0($tempreg)
6435              Otherwise we want
6436                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6437                nop
6438                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6439                <op>     $treg,0($tempreg)
6440              If there is a base register, we add it to $tempreg before
6441              the <op>.  If there is a constant, we stick it in the
6442              <op> instruction.  We don't handle constants larger than
6443              16 bits, because we have no way to load the upper 16 bits
6444              (actually, we could handle them for the subset of cases
6445              in which we are not using $at).  */
6446           assert (offset_expr.X_op == O_symbol);
6447           expr1.X_add_number = offset_expr.X_add_number;
6448           offset_expr.X_add_number = 0;
6449           if (expr1.X_add_number < -0x8000
6450               || expr1.X_add_number >= 0x8000)
6451             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6452           if (reg_needs_delay (mips_gp_register))
6453             gpdel = 4;
6454           else
6455             gpdel = 0;
6456           frag_grow (36);
6457           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6458                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6459           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6460                        HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6461                        "d,v,t", tempreg, tempreg, mips_gp_register);
6462           macro_build ((char *) NULL, &icnt, &offset_expr,
6463                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6464                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6465                        tempreg);
6466           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6467                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6468                         offset_expr.X_add_symbol, 0, NULL);
6469           if (gpdel > 0)
6470             {
6471               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6472               p += 4;
6473             }
6474           macro_build (p, &icnt, &offset_expr,
6475                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6476                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6477                        mips_gp_register);
6478           p += 4;
6479           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6480           p += 4;
6481           macro_build (p, &icnt, &offset_expr,
6482                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6483                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6484           if (breg != 0)
6485             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6486                          HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6487                          "d,v,t", tempreg, tempreg, breg);
6488           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6489                        (int) BFD_RELOC_LO16, tempreg);
6490         }
6491       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6492         {
6493           char *p;
6494           int bregsz = breg != 0 ? 4 : 0;
6495
6496           /* If this is a reference to an external symbol, we want
6497                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6498                add      $tempreg,$tempreg,$gp
6499                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6500                <op>     $treg,<ofst>($tempreg)
6501              Otherwise, for local symbols, we want:
6502                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6503                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6504           assert (offset_expr.X_op == O_symbol);
6505           frag_now->tc_frag_data.tc_fr_offset =
6506             expr1.X_add_number = offset_expr.X_add_number;
6507           offset_expr.X_add_number = 0;
6508           if (expr1.X_add_number < -0x8000
6509               || expr1.X_add_number >= 0x8000)
6510             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6511           frag_grow (36);
6512           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6513                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6514           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6515                        HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6516                        "d,v,t", tempreg, tempreg, mips_gp_register);
6517           macro_build ((char *) NULL, &icnt, &offset_expr,
6518                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6519                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6520                        tempreg);
6521           if (breg != 0)
6522             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6523                          HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6524                          "d,v,t", tempreg, tempreg, breg);
6525           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6526                        (int) BFD_RELOC_LO16, tempreg);
6527
6528           offset_expr.X_add_number = expr1.X_add_number;
6529           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6530                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6531                                       0, 4 + bregsz, 0, 0),
6532                         offset_expr.X_add_symbol, 0, NULL);
6533           macro_build (p, &icnt, &offset_expr,
6534                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6535                        "t,o(b)", tempreg,
6536                        (int) BFD_RELOC_MIPS_GOT_PAGE,
6537                        mips_gp_register);
6538           if (breg != 0)
6539             macro_build (p + 4, &icnt, (expressionS *) NULL,
6540                          HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6541                          "d,v,t", tempreg, tempreg, breg);
6542           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6543                        (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6544         }
6545       else if (mips_pic == EMBEDDED_PIC)
6546         {
6547           /* If there is no base register, we want
6548                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6549              If there is a base register, we want
6550                addu     $tempreg,$breg,$gp
6551                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6552              */
6553           assert (offset_expr.X_op == O_symbol);
6554           if (breg == 0)
6555             {
6556               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6557                            treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6558               used_at = 0;
6559             }
6560           else
6561             {
6562               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6563                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6564                            "d,v,t", tempreg, breg, mips_gp_register);
6565               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6566                            treg, (int) BFD_RELOC_GPREL16, tempreg);
6567             }
6568         }
6569       else
6570         abort ();
6571
6572       if (! used_at)
6573         return;
6574
6575       break;
6576
6577     case M_LI:
6578     case M_LI_S:
6579       load_register (&icnt, treg, &imm_expr, 0);
6580       return;
6581
6582     case M_DLI:
6583       load_register (&icnt, treg, &imm_expr, 1);
6584       return;
6585
6586     case M_LI_SS:
6587       if (imm_expr.X_op == O_constant)
6588         {
6589           load_register (&icnt, AT, &imm_expr, 0);
6590           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6591                        "mtc1", "t,G", AT, treg);
6592           break;
6593         }
6594       else
6595         {
6596           assert (offset_expr.X_op == O_symbol
6597                   && strcmp (segment_name (S_GET_SEGMENT
6598                                            (offset_expr.X_add_symbol)),
6599                              ".lit4") == 0
6600                   && offset_expr.X_add_number == 0);
6601           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6602                        treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6603           return;
6604         }
6605
6606     case M_LI_D:
6607       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6608          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6609          order 32 bits of the value and the low order 32 bits are either
6610          zero or in OFFSET_EXPR.  */
6611       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6612         {
6613           if (HAVE_64BIT_GPRS)
6614             load_register (&icnt, treg, &imm_expr, 1);
6615           else
6616             {
6617               int hreg, lreg;
6618
6619               if (target_big_endian)
6620                 {
6621                   hreg = treg;
6622                   lreg = treg + 1;
6623                 }
6624               else
6625                 {
6626                   hreg = treg + 1;
6627                   lreg = treg;
6628                 }
6629
6630               if (hreg <= 31)
6631                 load_register (&icnt, hreg, &imm_expr, 0);
6632               if (lreg <= 31)
6633                 {
6634                   if (offset_expr.X_op == O_absent)
6635                     move_register (&icnt, lreg, 0);
6636                   else
6637                     {
6638                       assert (offset_expr.X_op == O_constant);
6639                       load_register (&icnt, lreg, &offset_expr, 0);
6640                     }
6641                 }
6642             }
6643           return;
6644         }
6645
6646       /* We know that sym is in the .rdata section.  First we get the
6647          upper 16 bits of the address.  */
6648       if (mips_pic == NO_PIC)
6649         {
6650           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6651         }
6652       else if (mips_pic == SVR4_PIC)
6653         {
6654           macro_build ((char *) NULL, &icnt, &offset_expr,
6655                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6656                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6657                        mips_gp_register);
6658         }
6659       else if (mips_pic == EMBEDDED_PIC)
6660         {
6661           /* For embedded PIC we pick up the entire address off $gp in
6662              a single instruction.  */
6663           macro_build ((char *) NULL, &icnt, &offset_expr,
6664                        HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6665                        mips_gp_register, (int) BFD_RELOC_GPREL16);
6666           offset_expr.X_op = O_constant;
6667           offset_expr.X_add_number = 0;
6668         }
6669       else
6670         abort ();
6671
6672       /* Now we load the register(s).  */
6673       if (HAVE_64BIT_GPRS)
6674         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6675                      treg, (int) BFD_RELOC_LO16, AT);
6676       else
6677         {
6678           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6679                        treg, (int) BFD_RELOC_LO16, AT);
6680           if (treg != RA)
6681             {
6682               /* FIXME: How in the world do we deal with the possible
6683                  overflow here?  */
6684               offset_expr.X_add_number += 4;
6685               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6686                            treg + 1, (int) BFD_RELOC_LO16, AT);
6687             }
6688         }
6689
6690       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6691          does not become a variant frag.  */
6692       frag_wane (frag_now);
6693       frag_new (0);
6694
6695       break;
6696
6697     case M_LI_DD:
6698       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6699          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6700          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6701          the value and the low order 32 bits are either zero or in
6702          OFFSET_EXPR.  */
6703       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6704         {
6705           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6706           if (HAVE_64BIT_FPRS)
6707             {
6708               assert (HAVE_64BIT_GPRS);
6709               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6710                            "dmtc1", "t,S", AT, treg);
6711             }
6712           else
6713             {
6714               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6715                            "mtc1", "t,G", AT, treg + 1);
6716               if (offset_expr.X_op == O_absent)
6717                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6718                              "mtc1", "t,G", 0, treg);
6719               else
6720                 {
6721                   assert (offset_expr.X_op == O_constant);
6722                   load_register (&icnt, AT, &offset_expr, 0);
6723                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6724                                "mtc1", "t,G", AT, treg);
6725                 }
6726             }
6727           break;
6728         }
6729
6730       assert (offset_expr.X_op == O_symbol
6731               && offset_expr.X_add_number == 0);
6732       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6733       if (strcmp (s, ".lit8") == 0)
6734         {
6735           if (mips_opts.isa != ISA_MIPS1)
6736             {
6737               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6738                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6739                            mips_gp_register);
6740               return;
6741             }
6742           breg = mips_gp_register;
6743           r = BFD_RELOC_MIPS_LITERAL;
6744           goto dob;
6745         }
6746       else
6747         {
6748           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6749           if (mips_pic == SVR4_PIC)
6750             macro_build ((char *) NULL, &icnt, &offset_expr,
6751                          HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6752                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6753                          mips_gp_register);
6754           else
6755             {
6756               /* FIXME: This won't work for a 64 bit address.  */
6757               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6758             }
6759
6760           if (mips_opts.isa != ISA_MIPS1)
6761             {
6762               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6763                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6764
6765               /* To avoid confusion in tc_gen_reloc, we must ensure
6766                  that this does not become a variant frag.  */
6767               frag_wane (frag_now);
6768               frag_new (0);
6769
6770               break;
6771             }
6772           breg = AT;
6773           r = BFD_RELOC_LO16;
6774           goto dob;
6775         }
6776
6777     case M_L_DOB:
6778       if (mips_arch == CPU_R4650)
6779         {
6780           as_bad (_("opcode not supported on this processor"));
6781           return;
6782         }
6783       /* Even on a big endian machine $fn comes before $fn+1.  We have
6784          to adjust when loading from memory.  */
6785       r = BFD_RELOC_LO16;
6786     dob:
6787       assert (mips_opts.isa == ISA_MIPS1);
6788       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6789                    target_big_endian ? treg + 1 : treg,
6790                    (int) r, breg);
6791       /* FIXME: A possible overflow which I don't know how to deal
6792          with.  */
6793       offset_expr.X_add_number += 4;
6794       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6795                    target_big_endian ? treg : treg + 1,
6796                    (int) r, breg);
6797
6798       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6799          does not become a variant frag.  */
6800       frag_wane (frag_now);
6801       frag_new (0);
6802
6803       if (breg != AT)
6804         return;
6805       break;
6806
6807     case M_L_DAB:
6808       /*
6809        * The MIPS assembler seems to check for X_add_number not
6810        * being double aligned and generating:
6811        *        lui     at,%hi(foo+1)
6812        *        addu    at,at,v1
6813        *        addiu   at,at,%lo(foo+1)
6814        *        lwc1    f2,0(at)
6815        *        lwc1    f3,4(at)
6816        * But, the resulting address is the same after relocation so why
6817        * generate the extra instruction?
6818        */
6819       if (mips_arch == CPU_R4650)
6820         {
6821           as_bad (_("opcode not supported on this processor"));
6822           return;
6823         }
6824       /* Itbl support may require additional care here.  */
6825       coproc = 1;
6826       if (mips_opts.isa != ISA_MIPS1)
6827         {
6828           s = "ldc1";
6829           goto ld;
6830         }
6831
6832       s = "lwc1";
6833       fmt = "T,o(b)";
6834       goto ldd_std;
6835
6836     case M_S_DAB:
6837       if (mips_arch == CPU_R4650)
6838         {
6839           as_bad (_("opcode not supported on this processor"));
6840           return;
6841         }
6842
6843       if (mips_opts.isa != ISA_MIPS1)
6844         {
6845           s = "sdc1";
6846           goto st;
6847         }
6848
6849       s = "swc1";
6850       fmt = "T,o(b)";
6851       /* Itbl support may require additional care here.  */
6852       coproc = 1;
6853       goto ldd_std;
6854
6855     case M_LD_AB:
6856       if (HAVE_64BIT_GPRS)
6857         {
6858           s = "ld";
6859           goto ld;
6860         }
6861
6862       s = "lw";
6863       fmt = "t,o(b)";
6864       goto ldd_std;
6865
6866     case M_SD_AB:
6867       if (HAVE_64BIT_GPRS)
6868         {
6869           s = "sd";
6870           goto st;
6871         }
6872
6873       s = "sw";
6874       fmt = "t,o(b)";
6875
6876     ldd_std:
6877       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6878          loads for the case of doing a pair of loads to simulate an 'ld'.
6879          This is not currently done by the compiler, and assembly coders
6880          writing embedded-pic code can cope.  */
6881
6882       if (offset_expr.X_op != O_symbol
6883           && offset_expr.X_op != O_constant)
6884         {
6885           as_bad (_("expression too complex"));
6886           offset_expr.X_op = O_constant;
6887         }
6888
6889       /* Even on a big endian machine $fn comes before $fn+1.  We have
6890          to adjust when loading from memory.  We set coproc if we must
6891          load $fn+1 first.  */
6892       /* Itbl support may require additional care here.  */
6893       if (! target_big_endian)
6894         coproc = 0;
6895
6896       if (mips_pic == NO_PIC
6897           || offset_expr.X_op == O_constant)
6898         {
6899           char *p;
6900
6901           /* If this is a reference to a GP relative symbol, we want
6902                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6903                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6904              If we have a base register, we use this
6905                addu     $at,$breg,$gp
6906                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6907                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6908              If this is not a GP relative symbol, we want
6909                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6910                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6911                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6912              If there is a base register, we add it to $at after the
6913              lui instruction.  If there is a constant, we always use
6914              the last case.  */
6915           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6916               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6917             {
6918               p = NULL;
6919               used_at = 1;
6920             }
6921           else
6922             {
6923               int off;
6924
6925               if (breg == 0)
6926                 {
6927                   frag_grow (28);
6928                   tempreg = mips_gp_register;
6929                   off = 0;
6930                   used_at = 0;
6931                 }
6932               else
6933                 {
6934                   frag_grow (36);
6935                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6936                                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6937                                ? "add" : "addu" : "daddu",
6938                                "d,v,t", AT, breg, mips_gp_register);
6939                   tempreg = AT;
6940                   off = 4;
6941                   used_at = 1;
6942                 }
6943
6944               /* Itbl support may require additional care here.  */
6945               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6946                            coproc ? treg + 1 : treg,
6947                            (int) BFD_RELOC_GPREL16, tempreg);
6948               offset_expr.X_add_number += 4;
6949
6950               /* Set mips_optimize to 2 to avoid inserting an
6951                  undesired nop.  */
6952               hold_mips_optimize = mips_optimize;
6953               mips_optimize = 2;
6954               /* Itbl support may require additional care here.  */
6955               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6956                            coproc ? treg : treg + 1,
6957                            (int) BFD_RELOC_GPREL16, tempreg);
6958               mips_optimize = hold_mips_optimize;
6959
6960               p = frag_var (rs_machine_dependent, 12 + off, 0,
6961                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6962                                           used_at && mips_opts.noat),
6963                             offset_expr.X_add_symbol, 0, NULL);
6964
6965               /* We just generated two relocs.  When tc_gen_reloc
6966                  handles this case, it will skip the first reloc and
6967                  handle the second.  The second reloc already has an
6968                  extra addend of 4, which we added above.  We must
6969                  subtract it out, and then subtract another 4 to make
6970                  the first reloc come out right.  The second reloc
6971                  will come out right because we are going to add 4 to
6972                  offset_expr when we build its instruction below.
6973
6974                  If we have a symbol, then we don't want to include
6975                  the offset, because it will wind up being included
6976                  when we generate the reloc.  */
6977
6978               if (offset_expr.X_op == O_constant)
6979                 offset_expr.X_add_number -= 8;
6980               else
6981                 {
6982                   offset_expr.X_add_number = -4;
6983                   offset_expr.X_op = O_constant;
6984                 }
6985             }
6986           macro_build_lui (p, &icnt, &offset_expr, AT);
6987           if (p != NULL)
6988             p += 4;
6989           if (breg != 0)
6990             {
6991               macro_build (p, &icnt, (expressionS *) NULL,
6992                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6993                            ? "add" : "addu" : "daddu",
6994                            "d,v,t", AT, breg, AT);
6995               if (p != NULL)
6996                 p += 4;
6997             }
6998           /* Itbl support may require additional care here.  */
6999           macro_build (p, &icnt, &offset_expr, s, fmt,
7000                        coproc ? treg + 1 : treg,
7001                        (int) BFD_RELOC_LO16, AT);
7002           if (p != NULL)
7003             p += 4;
7004           /* FIXME: How do we handle overflow here?  */
7005           offset_expr.X_add_number += 4;
7006           /* Itbl support may require additional care here.  */
7007           macro_build (p, &icnt, &offset_expr, s, fmt,
7008                        coproc ? treg : treg + 1,
7009                        (int) BFD_RELOC_LO16, AT);
7010         }
7011       else if (mips_pic == SVR4_PIC && ! mips_big_got)
7012         {
7013           int off;
7014
7015           /* If this is a reference to an external symbol, we want
7016                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7017                nop
7018                <op>     $treg,0($at)
7019                <op>     $treg+1,4($at)
7020              Otherwise we want
7021                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7022                nop
7023                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
7024                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
7025              If there is a base register we add it to $at before the
7026              lwc1 instructions.  If there is a constant we include it
7027              in the lwc1 instructions.  */
7028           used_at = 1;
7029           expr1.X_add_number = offset_expr.X_add_number;
7030           offset_expr.X_add_number = 0;
7031           if (expr1.X_add_number < -0x8000
7032               || expr1.X_add_number >= 0x8000 - 4)
7033             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7034           if (breg == 0)
7035             off = 0;
7036           else
7037             off = 4;
7038           frag_grow (24 + off);
7039           macro_build ((char *) NULL, &icnt, &offset_expr,
7040                        HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
7041                        (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
7042           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7043           if (breg != 0)
7044             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7045                          HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7046                          ? "add" : "addu" : "daddu",
7047                          "d,v,t", AT, breg, AT);
7048           /* Itbl support may require additional care here.  */
7049           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7050                        coproc ? treg + 1 : treg,
7051                        (int) BFD_RELOC_LO16, AT);
7052           expr1.X_add_number += 4;
7053
7054           /* Set mips_optimize to 2 to avoid inserting an undesired
7055              nop.  */
7056           hold_mips_optimize = mips_optimize;
7057           mips_optimize = 2;
7058           /* Itbl support may require additional care here.  */
7059           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7060                        coproc ? treg : treg + 1,
7061                        (int) BFD_RELOC_LO16, AT);
7062           mips_optimize = hold_mips_optimize;
7063
7064           (void) frag_var (rs_machine_dependent, 0, 0,
7065                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
7066                            offset_expr.X_add_symbol, 0, NULL);
7067         }
7068       else if (mips_pic == SVR4_PIC)
7069         {
7070           int gpdel, off;
7071           char *p;
7072
7073           /* If this is a reference to an external symbol, we want
7074                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
7075                addu     $at,$at,$gp
7076                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
7077                nop
7078                <op>     $treg,0($at)
7079                <op>     $treg+1,4($at)
7080              Otherwise we want
7081                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
7082                nop
7083                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
7084                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
7085              If there is a base register we add it to $at before the
7086              lwc1 instructions.  If there is a constant we include it
7087              in the lwc1 instructions.  */
7088           used_at = 1;
7089           expr1.X_add_number = offset_expr.X_add_number;
7090           offset_expr.X_add_number = 0;
7091           if (expr1.X_add_number < -0x8000
7092               || expr1.X_add_number >= 0x8000 - 4)
7093             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7094           if (reg_needs_delay (mips_gp_register))
7095             gpdel = 4;
7096           else
7097             gpdel = 0;
7098           if (breg == 0)
7099             off = 0;
7100           else
7101             off = 4;
7102           frag_grow (56);
7103           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
7104                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
7105           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7106                        HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7107                        ? "add" : "addu" : "daddu",
7108                        "d,v,t", AT, AT, mips_gp_register);
7109           macro_build ((char *) NULL, &icnt, &offset_expr,
7110                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7111                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
7112           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7113           if (breg != 0)
7114             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7115                          HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7116                          ? "add" : "addu" : "daddu",
7117                          "d,v,t", AT, breg, AT);
7118           /* Itbl support may require additional care here.  */
7119           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7120                        coproc ? treg + 1 : treg,
7121                        (int) BFD_RELOC_LO16, AT);
7122           expr1.X_add_number += 4;
7123
7124           /* Set mips_optimize to 2 to avoid inserting an undesired
7125              nop.  */
7126           hold_mips_optimize = mips_optimize;
7127           mips_optimize = 2;
7128           /* Itbl support may require additional care here.  */
7129           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7130                        coproc ? treg : treg + 1,
7131                        (int) BFD_RELOC_LO16, AT);
7132           mips_optimize = hold_mips_optimize;
7133           expr1.X_add_number -= 4;
7134
7135           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
7136                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
7137                                       8 + gpdel + off, 1, 0),
7138                         offset_expr.X_add_symbol, 0, NULL);
7139           if (gpdel > 0)
7140             {
7141               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7142               p += 4;
7143             }
7144           macro_build (p, &icnt, &offset_expr,
7145                        HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7146                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
7147                        mips_gp_register);
7148           p += 4;
7149           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7150           p += 4;
7151           if (breg != 0)
7152             {
7153               macro_build (p, &icnt, (expressionS *) NULL,
7154                            HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7155                            ? "add" : "addu" : "daddu",
7156                            "d,v,t", AT, breg, AT);
7157               p += 4;
7158             }
7159           /* Itbl support may require additional care here.  */
7160           macro_build (p, &icnt, &expr1, s, fmt,
7161                        coproc ? treg + 1 : treg,
7162                        (int) BFD_RELOC_LO16, AT);
7163           p += 4;
7164           expr1.X_add_number += 4;
7165
7166           /* Set mips_optimize to 2 to avoid inserting an undesired
7167              nop.  */
7168           hold_mips_optimize = mips_optimize;
7169           mips_optimize = 2;
7170           /* Itbl support may require additional care here.  */
7171           macro_build (p, &icnt, &expr1, s, fmt,
7172                        coproc ? treg : treg + 1,
7173                        (int) BFD_RELOC_LO16, AT);
7174           mips_optimize = hold_mips_optimize;
7175         }
7176       else if (mips_pic == EMBEDDED_PIC)
7177         {
7178           /* If there is no base register, we use
7179                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
7180                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
7181              If we have a base register, we use
7182                addu     $at,$breg,$gp
7183                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
7184                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
7185              */
7186           if (breg == 0)
7187             {
7188               tempreg = mips_gp_register;
7189               used_at = 0;
7190             }
7191           else
7192             {
7193               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7194                            HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7195                            "d,v,t", AT, breg, mips_gp_register);
7196               tempreg = AT;
7197               used_at = 1;
7198             }
7199
7200           /* Itbl support may require additional care here.  */
7201           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7202                        coproc ? treg + 1 : treg,
7203                        (int) BFD_RELOC_GPREL16, tempreg);
7204           offset_expr.X_add_number += 4;
7205           /* Itbl support may require additional care here.  */
7206           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7207                        coproc ? treg : treg + 1,
7208                        (int) BFD_RELOC_GPREL16, tempreg);
7209         }
7210       else
7211         abort ();
7212
7213       if (! used_at)
7214         return;
7215
7216       break;
7217
7218     case M_LD_OB:
7219       s = "lw";
7220       goto sd_ob;
7221     case M_SD_OB:
7222       s = "sw";
7223     sd_ob:
7224       assert (HAVE_32BIT_ADDRESSES);
7225       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7226                    (int) BFD_RELOC_LO16, breg);
7227       offset_expr.X_add_number += 4;
7228       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7229                    (int) BFD_RELOC_LO16, breg);
7230       return;
7231
7232    /* New code added to support COPZ instructions.
7233       This code builds table entries out of the macros in mip_opcodes.
7234       R4000 uses interlocks to handle coproc delays.
7235       Other chips (like the R3000) require nops to be inserted for delays.
7236
7237       FIXME: Currently, we require that the user handle delays.
7238       In order to fill delay slots for non-interlocked chips,
7239       we must have a way to specify delays based on the coprocessor.
7240       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7241       What are the side-effects of the cop instruction?
7242       What cache support might we have and what are its effects?
7243       Both coprocessor & memory require delays. how long???
7244       What registers are read/set/modified?
7245
7246       If an itbl is provided to interpret cop instructions,
7247       this knowledge can be encoded in the itbl spec.  */
7248
7249     case M_COP0:
7250       s = "c0";
7251       goto copz;
7252     case M_COP1:
7253       s = "c1";
7254       goto copz;
7255     case M_COP2:
7256       s = "c2";
7257       goto copz;
7258     case M_COP3:
7259       s = "c3";
7260     copz:
7261       /* For now we just do C (same as Cz).  The parameter will be
7262          stored in insn_opcode by mips_ip.  */
7263       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
7264                    ip->insn_opcode);
7265       return;
7266
7267     case M_MOVE:
7268       move_register (&icnt, dreg, sreg);
7269       return;
7270
7271 #ifdef LOSING_COMPILER
7272     default:
7273       /* Try and see if this is a new itbl instruction.
7274          This code builds table entries out of the macros in mip_opcodes.
7275          FIXME: For now we just assemble the expression and pass it's
7276          value along as a 32-bit immediate.
7277          We may want to have the assembler assemble this value,
7278          so that we gain the assembler's knowledge of delay slots,
7279          symbols, etc.
7280          Would it be more efficient to use mask (id) here? */
7281       if (itbl_have_entries
7282           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7283         {
7284           s = ip->insn_mo->name;
7285           s2 = "cop3";
7286           coproc = ITBL_DECODE_PNUM (immed_expr);;
7287           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
7288           return;
7289         }
7290       macro2 (ip);
7291       return;
7292     }
7293   if (mips_opts.noat)
7294     as_warn (_("Macro used $at after \".set noat\""));
7295 }
7296
7297 static void
7298 macro2 (ip)
7299      struct mips_cl_insn *ip;
7300 {
7301   register int treg, sreg, dreg, breg;
7302   int tempreg;
7303   int mask;
7304   int icnt = 0;
7305   int used_at;
7306   expressionS expr1;
7307   const char *s;
7308   const char *s2;
7309   const char *fmt;
7310   int likely = 0;
7311   int dbl = 0;
7312   int coproc = 0;
7313   int lr = 0;
7314   int imm = 0;
7315   int off;
7316   offsetT maxnum;
7317   bfd_reloc_code_real_type r;
7318   char *p;
7319
7320   treg = (ip->insn_opcode >> 16) & 0x1f;
7321   dreg = (ip->insn_opcode >> 11) & 0x1f;
7322   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7323   mask = ip->insn_mo->mask;
7324
7325   expr1.X_op = O_constant;
7326   expr1.X_op_symbol = NULL;
7327   expr1.X_add_symbol = NULL;
7328   expr1.X_add_number = 1;
7329
7330   switch (mask)
7331     {
7332 #endif /* LOSING_COMPILER */
7333
7334     case M_DMUL:
7335       dbl = 1;
7336     case M_MUL:
7337       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7338                    dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7339       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7340                    dreg);
7341       return;
7342
7343     case M_DMUL_I:
7344       dbl = 1;
7345     case M_MUL_I:
7346       /* The MIPS assembler some times generates shifts and adds.  I'm
7347          not trying to be that fancy. GCC should do this for us
7348          anyway.  */
7349       load_register (&icnt, AT, &imm_expr, dbl);
7350       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7351                    dbl ? "dmult" : "mult", "s,t", sreg, AT);
7352       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7353                    dreg);
7354       break;
7355
7356     case M_DMULO_I:
7357       dbl = 1;
7358     case M_MULO_I:
7359       imm = 1;
7360       goto do_mulo;
7361
7362     case M_DMULO:
7363       dbl = 1;
7364     case M_MULO:
7365     do_mulo:
7366       mips_emit_delays (TRUE);
7367       ++mips_opts.noreorder;
7368       mips_any_noreorder = 1;
7369       if (imm)
7370         load_register (&icnt, AT, &imm_expr, dbl);
7371       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7372                    dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7373       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7374                    dreg);
7375       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7376                    dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7377       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7378                    AT);
7379       if (mips_trap)
7380         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7381                      "s,t,q", dreg, AT, 6);
7382       else
7383         {
7384           expr1.X_add_number = 8;
7385           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
7386                        AT);
7387           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7388                        0);
7389           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7390                        "c", 6);
7391         }
7392       --mips_opts.noreorder;
7393       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
7394       break;
7395
7396     case M_DMULOU_I:
7397       dbl = 1;
7398     case M_MULOU_I:
7399       imm = 1;
7400       goto do_mulou;
7401
7402     case M_DMULOU:
7403       dbl = 1;
7404     case M_MULOU:
7405     do_mulou:
7406       mips_emit_delays (TRUE);
7407       ++mips_opts.noreorder;
7408       mips_any_noreorder = 1;
7409       if (imm)
7410         load_register (&icnt, AT, &imm_expr, dbl);
7411       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7412                    dbl ? "dmultu" : "multu",
7413                    "s,t", sreg, imm ? AT : treg);
7414       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7415                    AT);
7416       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7417                    dreg);
7418       if (mips_trap)
7419         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7420                      "s,t,q", AT, 0, 6);
7421       else
7422         {
7423           expr1.X_add_number = 8;
7424           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7425           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7426                        0);
7427           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7428                        "c", 6);
7429         }
7430       --mips_opts.noreorder;
7431       break;
7432
7433     case M_DROL:
7434       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7435         {
7436           if (dreg == sreg)
7437             {
7438               tempreg = AT;
7439               used_at = 1;
7440             }
7441           else
7442             {
7443               tempreg = dreg;
7444               used_at = 0;
7445             }
7446           macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7447                        "d,w", tempreg, treg);
7448           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7449                        "d,t,s", dreg, sreg, tempreg);
7450           if (used_at)
7451             break;
7452           return;
7453         }
7454       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7455                    "d,v,t", AT, 0, treg);
7456       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7457                    "d,t,s", AT, sreg, AT);
7458       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7459                    "d,t,s", dreg, sreg, treg);
7460       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7461                    "d,v,t", dreg, dreg, AT);
7462       break;
7463
7464     case M_ROL:
7465       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7466         {
7467           if (dreg == sreg)
7468             {
7469               tempreg = AT;
7470               used_at = 1;
7471             }
7472           else
7473             {
7474               tempreg = dreg;
7475               used_at = 0;
7476             }
7477           macro_build ((char *) NULL, &icnt, NULL, "negu",
7478                        "d,w", tempreg, treg);
7479           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7480                        "d,t,s", dreg, sreg, tempreg);
7481           if (used_at)
7482             break;
7483           return;
7484         }
7485       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7486                    "d,v,t", AT, 0, treg);
7487       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7488                    "d,t,s", AT, sreg, AT);
7489       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7490                    "d,t,s", dreg, sreg, treg);
7491       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7492                    "d,v,t", dreg, dreg, AT);
7493       break;
7494
7495     case M_DROL_I:
7496       {
7497         unsigned int rot;
7498         char *l, *r;
7499
7500         if (imm_expr.X_op != O_constant)
7501           as_bad (_("Improper rotate count"));
7502         rot = imm_expr.X_add_number & 0x3f;
7503         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7504           {
7505             rot = (64 - rot) & 0x3f;
7506             if (rot >= 32)
7507               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7508                            "d,w,<", dreg, sreg, rot - 32);
7509             else
7510               macro_build ((char *) NULL, &icnt, NULL, "dror",
7511                            "d,w,<", dreg, sreg, rot);
7512             return;
7513           }
7514         if (rot == 0)
7515           {
7516             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7517                          "d,w,<", dreg, sreg, 0);
7518             return;
7519           }
7520         l = (rot < 0x20) ? "dsll" : "dsll32";
7521         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7522         rot &= 0x1f;
7523         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7524                      "d,w,<", AT, sreg, rot);
7525         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7526                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7527         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7528                      "d,v,t", dreg, dreg, AT);
7529       }
7530       break;
7531
7532     case M_ROL_I:
7533       {
7534         unsigned int rot;
7535
7536         if (imm_expr.X_op != O_constant)
7537           as_bad (_("Improper rotate count"));
7538         rot = imm_expr.X_add_number & 0x1f;
7539         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7540           {
7541             macro_build ((char *) NULL, &icnt, NULL, "ror",
7542                          "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7543             return;
7544           }
7545         if (rot == 0)
7546           {
7547             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7548                          "d,w,<", dreg, sreg, 0);
7549             return;
7550           }
7551         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7552                      "d,w,<", AT, sreg, rot);
7553         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7554                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7555         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7556                      "d,v,t", dreg, dreg, AT);
7557       }
7558       break;
7559
7560     case M_DROR:
7561       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7562         {
7563           macro_build ((char *) NULL, &icnt, NULL, "drorv",
7564                        "d,t,s", dreg, sreg, treg);
7565           return;
7566         }
7567       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7568                    "d,v,t", AT, 0, treg);
7569       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7570                    "d,t,s", AT, sreg, AT);
7571       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7572                    "d,t,s", dreg, sreg, treg);
7573       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7574                    "d,v,t", dreg, dreg, AT);
7575       break;
7576
7577     case M_ROR:
7578       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7579         {
7580           macro_build ((char *) NULL, &icnt, NULL, "rorv",
7581                        "d,t,s", dreg, sreg, treg);
7582           return;
7583         }
7584       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7585                    "d,v,t", AT, 0, treg);
7586       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7587                    "d,t,s", AT, sreg, AT);
7588       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7589                    "d,t,s", dreg, sreg, treg);
7590       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7591                    "d,v,t", dreg, dreg, AT);
7592       break;
7593
7594     case M_DROR_I:
7595       {
7596         unsigned int rot;
7597         char *l, *r;
7598
7599         if (imm_expr.X_op != O_constant)
7600           as_bad (_("Improper rotate count"));
7601         rot = imm_expr.X_add_number & 0x3f;
7602         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7603           {
7604             if (rot >= 32)
7605               macro_build ((char *) NULL, &icnt, NULL, "dror32",
7606                            "d,w,<", dreg, sreg, rot - 32);
7607             else
7608               macro_build ((char *) NULL, &icnt, NULL, "dror",
7609                            "d,w,<", dreg, sreg, rot);
7610             return;
7611           }
7612         if (rot == 0)
7613           {
7614             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7615                          "d,w,<", dreg, sreg, 0);
7616             return;
7617           }
7618         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7619         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7620         rot &= 0x1f;
7621         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7622                      "d,w,<", AT, sreg, rot);
7623         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7624                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7625         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7626                      "d,v,t", dreg, dreg, AT);
7627       }
7628       break;
7629
7630     case M_ROR_I:
7631       {
7632         unsigned int rot;
7633
7634         if (imm_expr.X_op != O_constant)
7635           as_bad (_("Improper rotate count"));
7636         rot = imm_expr.X_add_number & 0x1f;
7637         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7638           {
7639             macro_build ((char *) NULL, &icnt, NULL, "ror",
7640                          "d,w,<", dreg, sreg, rot);
7641             return;
7642           }
7643         if (rot == 0)
7644           {
7645             macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7646                          "d,w,<", dreg, sreg, 0);
7647             return;
7648           }
7649         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7650                      "d,w,<", AT, sreg, rot);
7651         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7652                      "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7653         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7654                      "d,v,t", dreg, dreg, AT);
7655       }
7656       break;
7657
7658     case M_S_DOB:
7659       if (mips_arch == CPU_R4650)
7660         {
7661           as_bad (_("opcode not supported on this processor"));
7662           return;
7663         }
7664       assert (mips_opts.isa == ISA_MIPS1);
7665       /* Even on a big endian machine $fn comes before $fn+1.  We have
7666          to adjust when storing to memory.  */
7667       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7668                    target_big_endian ? treg + 1 : treg,
7669                    (int) BFD_RELOC_LO16, breg);
7670       offset_expr.X_add_number += 4;
7671       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7672                    target_big_endian ? treg : treg + 1,
7673                    (int) BFD_RELOC_LO16, breg);
7674       return;
7675
7676     case M_SEQ:
7677       if (sreg == 0)
7678         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7679                      treg, (int) BFD_RELOC_LO16);
7680       else if (treg == 0)
7681         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7682                      sreg, (int) BFD_RELOC_LO16);
7683       else
7684         {
7685           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7686                        "d,v,t", dreg, sreg, treg);
7687           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7688                        dreg, (int) BFD_RELOC_LO16);
7689         }
7690       return;
7691
7692     case M_SEQ_I:
7693       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7694         {
7695           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7696                        sreg, (int) BFD_RELOC_LO16);
7697           return;
7698         }
7699       if (sreg == 0)
7700         {
7701           as_warn (_("Instruction %s: result is always false"),
7702                    ip->insn_mo->name);
7703           move_register (&icnt, dreg, 0);
7704           return;
7705         }
7706       if (imm_expr.X_op == O_constant
7707           && imm_expr.X_add_number >= 0
7708           && imm_expr.X_add_number < 0x10000)
7709         {
7710           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7711                        sreg, (int) BFD_RELOC_LO16);
7712           used_at = 0;
7713         }
7714       else if (imm_expr.X_op == O_constant
7715                && imm_expr.X_add_number > -0x8000
7716                && imm_expr.X_add_number < 0)
7717         {
7718           imm_expr.X_add_number = -imm_expr.X_add_number;
7719           macro_build ((char *) NULL, &icnt, &imm_expr,
7720                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7721                        "t,r,j", dreg, sreg,
7722                        (int) BFD_RELOC_LO16);
7723           used_at = 0;
7724         }
7725       else
7726         {
7727           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7728           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7729                        "d,v,t", dreg, sreg, AT);
7730           used_at = 1;
7731         }
7732       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7733                    (int) BFD_RELOC_LO16);
7734       if (used_at)
7735         break;
7736       return;
7737
7738     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7739       s = "slt";
7740       goto sge;
7741     case M_SGEU:
7742       s = "sltu";
7743     sge:
7744       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7745                    dreg, sreg, treg);
7746       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7747                    (int) BFD_RELOC_LO16);
7748       return;
7749
7750     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7751     case M_SGEU_I:
7752       if (imm_expr.X_op == O_constant
7753           && imm_expr.X_add_number >= -0x8000
7754           && imm_expr.X_add_number < 0x8000)
7755         {
7756           macro_build ((char *) NULL, &icnt, &imm_expr,
7757                        mask == M_SGE_I ? "slti" : "sltiu",
7758                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7759           used_at = 0;
7760         }
7761       else
7762         {
7763           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7764           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7765                        mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7766                        AT);
7767           used_at = 1;
7768         }
7769       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7770                    (int) BFD_RELOC_LO16);
7771       if (used_at)
7772         break;
7773       return;
7774
7775     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7776       s = "slt";
7777       goto sgt;
7778     case M_SGTU:
7779       s = "sltu";
7780     sgt:
7781       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7782                    dreg, treg, sreg);
7783       return;
7784
7785     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7786       s = "slt";
7787       goto sgti;
7788     case M_SGTU_I:
7789       s = "sltu";
7790     sgti:
7791       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7792       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7793                    dreg, AT, sreg);
7794       break;
7795
7796     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7797       s = "slt";
7798       goto sle;
7799     case M_SLEU:
7800       s = "sltu";
7801     sle:
7802       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7803                    dreg, treg, sreg);
7804       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7805                    (int) BFD_RELOC_LO16);
7806       return;
7807
7808     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7809       s = "slt";
7810       goto slei;
7811     case M_SLEU_I:
7812       s = "sltu";
7813     slei:
7814       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7815       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7816                    dreg, AT, sreg);
7817       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7818                    (int) BFD_RELOC_LO16);
7819       break;
7820
7821     case M_SLT_I:
7822       if (imm_expr.X_op == O_constant
7823           && imm_expr.X_add_number >= -0x8000
7824           && imm_expr.X_add_number < 0x8000)
7825         {
7826           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7827                        dreg, sreg, (int) BFD_RELOC_LO16);
7828           return;
7829         }
7830       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7831       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7832                    dreg, sreg, AT);
7833       break;
7834
7835     case M_SLTU_I:
7836       if (imm_expr.X_op == O_constant
7837           && imm_expr.X_add_number >= -0x8000
7838           && imm_expr.X_add_number < 0x8000)
7839         {
7840           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7841                        dreg, sreg, (int) BFD_RELOC_LO16);
7842           return;
7843         }
7844       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7845       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7846                    "d,v,t", dreg, sreg, AT);
7847       break;
7848
7849     case M_SNE:
7850       if (sreg == 0)
7851         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7852                      "d,v,t", dreg, 0, treg);
7853       else if (treg == 0)
7854         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7855                      "d,v,t", dreg, 0, sreg);
7856       else
7857         {
7858           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7859                        "d,v,t", dreg, sreg, treg);
7860           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7861                        "d,v,t", dreg, 0, dreg);
7862         }
7863       return;
7864
7865     case M_SNE_I:
7866       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7867         {
7868           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7869                        "d,v,t", dreg, 0, sreg);
7870           return;
7871         }
7872       if (sreg == 0)
7873         {
7874           as_warn (_("Instruction %s: result is always true"),
7875                    ip->insn_mo->name);
7876           macro_build ((char *) NULL, &icnt, &expr1,
7877                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7878                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7879           return;
7880         }
7881       if (imm_expr.X_op == O_constant
7882           && imm_expr.X_add_number >= 0
7883           && imm_expr.X_add_number < 0x10000)
7884         {
7885           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7886                        dreg, sreg, (int) BFD_RELOC_LO16);
7887           used_at = 0;
7888         }
7889       else if (imm_expr.X_op == O_constant
7890                && imm_expr.X_add_number > -0x8000
7891                && imm_expr.X_add_number < 0)
7892         {
7893           imm_expr.X_add_number = -imm_expr.X_add_number;
7894           macro_build ((char *) NULL, &icnt, &imm_expr,
7895                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7896                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7897           used_at = 0;
7898         }
7899       else
7900         {
7901           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7902           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7903                        "d,v,t", dreg, sreg, AT);
7904           used_at = 1;
7905         }
7906       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7907                    "d,v,t", dreg, 0, dreg);
7908       if (used_at)
7909         break;
7910       return;
7911
7912     case M_DSUB_I:
7913       dbl = 1;
7914     case M_SUB_I:
7915       if (imm_expr.X_op == O_constant
7916           && imm_expr.X_add_number > -0x8000
7917           && imm_expr.X_add_number <= 0x8000)
7918         {
7919           imm_expr.X_add_number = -imm_expr.X_add_number;
7920           macro_build ((char *) NULL, &icnt, &imm_expr,
7921                        dbl ? "daddi" : "addi",
7922                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7923           return;
7924         }
7925       load_register (&icnt, AT, &imm_expr, dbl);
7926       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7927                    dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7928       break;
7929
7930     case M_DSUBU_I:
7931       dbl = 1;
7932     case M_SUBU_I:
7933       if (imm_expr.X_op == O_constant
7934           && imm_expr.X_add_number > -0x8000
7935           && imm_expr.X_add_number <= 0x8000)
7936         {
7937           imm_expr.X_add_number = -imm_expr.X_add_number;
7938           macro_build ((char *) NULL, &icnt, &imm_expr,
7939                        dbl ? "daddiu" : "addiu",
7940                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7941           return;
7942         }
7943       load_register (&icnt, AT, &imm_expr, dbl);
7944       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7945                    dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7946       break;
7947
7948     case M_TEQ_I:
7949       s = "teq";
7950       goto trap;
7951     case M_TGE_I:
7952       s = "tge";
7953       goto trap;
7954     case M_TGEU_I:
7955       s = "tgeu";
7956       goto trap;
7957     case M_TLT_I:
7958       s = "tlt";
7959       goto trap;
7960     case M_TLTU_I:
7961       s = "tltu";
7962       goto trap;
7963     case M_TNE_I:
7964       s = "tne";
7965     trap:
7966       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7967       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7968                    AT);
7969       break;
7970
7971     case M_TRUNCWS:
7972     case M_TRUNCWD:
7973       assert (mips_opts.isa == ISA_MIPS1);
7974       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7975       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7976
7977       /*
7978        * Is the double cfc1 instruction a bug in the mips assembler;
7979        * or is there a reason for it?
7980        */
7981       mips_emit_delays (TRUE);
7982       ++mips_opts.noreorder;
7983       mips_any_noreorder = 1;
7984       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7985                    treg, RA);
7986       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7987                    treg, RA);
7988       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7989       expr1.X_add_number = 3;
7990       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7991                    (int) BFD_RELOC_LO16);
7992       expr1.X_add_number = 2;
7993       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7994                      (int) BFD_RELOC_LO16);
7995       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7996                    AT, RA);
7997       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7998       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7999               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
8000       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
8001                    treg, RA);
8002       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8003       --mips_opts.noreorder;
8004       break;
8005
8006     case M_ULH:
8007       s = "lb";
8008       goto ulh;
8009     case M_ULHU:
8010       s = "lbu";
8011     ulh:
8012       if (offset_expr.X_add_number >= 0x7fff)
8013         as_bad (_("operand overflow"));
8014       if (! target_big_endian)
8015         ++offset_expr.X_add_number;
8016       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
8017                    (int) BFD_RELOC_LO16, breg);
8018       if (! target_big_endian)
8019         --offset_expr.X_add_number;
8020       else
8021         ++offset_expr.X_add_number;
8022       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
8023                    (int) BFD_RELOC_LO16, breg);
8024       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8025                    AT, AT, 8);
8026       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8027                    treg, treg, AT);
8028       break;
8029
8030     case M_ULD:
8031       s = "ldl";
8032       s2 = "ldr";
8033       off = 7;
8034       goto ulw;
8035     case M_ULW:
8036       s = "lwl";
8037       s2 = "lwr";
8038       off = 3;
8039     ulw:
8040       if (offset_expr.X_add_number >= 0x8000 - off)
8041         as_bad (_("operand overflow"));
8042       if (treg != breg)
8043         tempreg = treg;
8044       else
8045         tempreg = AT;
8046       if (! target_big_endian)
8047         offset_expr.X_add_number += off;
8048       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
8049                    (int) BFD_RELOC_LO16, breg);
8050       if (! target_big_endian)
8051         offset_expr.X_add_number -= off;
8052       else
8053         offset_expr.X_add_number += off;
8054       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
8055                    (int) BFD_RELOC_LO16, breg);
8056
8057       /* If necessary, move the result in tempreg the final destination.  */
8058       if (treg == tempreg)
8059         return;
8060       /* Protect second load's delay slot.  */
8061       if (!gpr_interlocks)
8062         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8063       move_register (&icnt, treg, tempreg);
8064       break;
8065
8066     case M_ULD_A:
8067       s = "ldl";
8068       s2 = "ldr";
8069       off = 7;
8070       goto ulwa;
8071     case M_ULW_A:
8072       s = "lwl";
8073       s2 = "lwr";
8074       off = 3;
8075     ulwa:
8076       used_at = 1;
8077       load_address (&icnt, AT, &offset_expr, &used_at);
8078       if (breg != 0)
8079         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8080                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8081                      ? "add" : "addu" : "daddu",
8082                      "d,v,t", AT, AT, breg);
8083       if (! target_big_endian)
8084         expr1.X_add_number = off;
8085       else
8086         expr1.X_add_number = 0;
8087       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8088                    (int) BFD_RELOC_LO16, AT);
8089       if (! target_big_endian)
8090         expr1.X_add_number = 0;
8091       else
8092         expr1.X_add_number = off;
8093       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8094                    (int) BFD_RELOC_LO16, AT);
8095       break;
8096
8097     case M_ULH_A:
8098     case M_ULHU_A:
8099       used_at = 1;
8100       load_address (&icnt, AT, &offset_expr, &used_at);
8101       if (breg != 0)
8102         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8103                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8104                      ? "add" : "addu" : "daddu",
8105                      "d,v,t", AT, AT, breg);
8106       if (target_big_endian)
8107         expr1.X_add_number = 0;
8108       macro_build ((char *) NULL, &icnt, &expr1,
8109                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
8110                    (int) BFD_RELOC_LO16, AT);
8111       if (target_big_endian)
8112         expr1.X_add_number = 1;
8113       else
8114         expr1.X_add_number = 0;
8115       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8116                    (int) BFD_RELOC_LO16, AT);
8117       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8118                    treg, treg, 8);
8119       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8120                    treg, treg, AT);
8121       break;
8122
8123     case M_USH:
8124       if (offset_expr.X_add_number >= 0x7fff)
8125         as_bad (_("operand overflow"));
8126       if (target_big_endian)
8127         ++offset_expr.X_add_number;
8128       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
8129                    (int) BFD_RELOC_LO16, breg);
8130       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8131                    AT, treg, 8);
8132       if (target_big_endian)
8133         --offset_expr.X_add_number;
8134       else
8135         ++offset_expr.X_add_number;
8136       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
8137                    (int) BFD_RELOC_LO16, breg);
8138       break;
8139
8140     case M_USD:
8141       s = "sdl";
8142       s2 = "sdr";
8143       off = 7;
8144       goto usw;
8145     case M_USW:
8146       s = "swl";
8147       s2 = "swr";
8148       off = 3;
8149     usw:
8150       if (offset_expr.X_add_number >= 0x8000 - off)
8151         as_bad (_("operand overflow"));
8152       if (! target_big_endian)
8153         offset_expr.X_add_number += off;
8154       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
8155                    (int) BFD_RELOC_LO16, breg);
8156       if (! target_big_endian)
8157         offset_expr.X_add_number -= off;
8158       else
8159         offset_expr.X_add_number += off;
8160       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
8161                    (int) BFD_RELOC_LO16, breg);
8162       return;
8163
8164     case M_USD_A:
8165       s = "sdl";
8166       s2 = "sdr";
8167       off = 7;
8168       goto uswa;
8169     case M_USW_A:
8170       s = "swl";
8171       s2 = "swr";
8172       off = 3;
8173     uswa:
8174       used_at = 1;
8175       load_address (&icnt, AT, &offset_expr, &used_at);
8176       if (breg != 0)
8177         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8178                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8179                      ? "add" : "addu" : "daddu",
8180                      "d,v,t", AT, AT, breg);
8181       if (! target_big_endian)
8182         expr1.X_add_number = off;
8183       else
8184         expr1.X_add_number = 0;
8185       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8186                    (int) BFD_RELOC_LO16, AT);
8187       if (! target_big_endian)
8188         expr1.X_add_number = 0;
8189       else
8190         expr1.X_add_number = off;
8191       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8192                    (int) BFD_RELOC_LO16, AT);
8193       break;
8194
8195     case M_USH_A:
8196       used_at = 1;
8197       load_address (&icnt, AT, &offset_expr, &used_at);
8198       if (breg != 0)
8199         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8200                      HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8201                      ? "add" : "addu" : "daddu",
8202                      "d,v,t", AT, AT, breg);
8203       if (! target_big_endian)
8204         expr1.X_add_number = 0;
8205       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8206                    (int) BFD_RELOC_LO16, AT);
8207       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8208                    treg, treg, 8);
8209       if (! target_big_endian)
8210         expr1.X_add_number = 1;
8211       else
8212         expr1.X_add_number = 0;
8213       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8214                    (int) BFD_RELOC_LO16, AT);
8215       if (! target_big_endian)
8216         expr1.X_add_number = 0;
8217       else
8218         expr1.X_add_number = 1;
8219       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8220                    (int) BFD_RELOC_LO16, AT);
8221       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8222                    treg, treg, 8);
8223       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8224                    treg, treg, AT);
8225       break;
8226
8227     default:
8228       /* FIXME: Check if this is one of the itbl macros, since they
8229          are added dynamically.  */
8230       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8231       break;
8232     }
8233   if (mips_opts.noat)
8234     as_warn (_("Macro used $at after \".set noat\""));
8235 }
8236
8237 /* Implement macros in mips16 mode.  */
8238
8239 static void
8240 mips16_macro (ip)
8241      struct mips_cl_insn *ip;
8242 {
8243   int mask;
8244   int xreg, yreg, zreg, tmp;
8245   int icnt;
8246   expressionS expr1;
8247   int dbl;
8248   const char *s, *s2, *s3;
8249
8250   mask = ip->insn_mo->mask;
8251
8252   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
8253   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
8254   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
8255
8256   icnt = 0;
8257
8258   expr1.X_op = O_constant;
8259   expr1.X_op_symbol = NULL;
8260   expr1.X_add_symbol = NULL;
8261   expr1.X_add_number = 1;
8262
8263   dbl = 0;
8264
8265   switch (mask)
8266     {
8267     default:
8268       internalError ();
8269
8270     case M_DDIV_3:
8271       dbl = 1;
8272     case M_DIV_3:
8273       s = "mflo";
8274       goto do_div3;
8275     case M_DREM_3:
8276       dbl = 1;
8277     case M_REM_3:
8278       s = "mfhi";
8279     do_div3:
8280       mips_emit_delays (TRUE);
8281       ++mips_opts.noreorder;
8282       mips_any_noreorder = 1;
8283       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8284                    dbl ? "ddiv" : "div",
8285                    "0,x,y", xreg, yreg);
8286       expr1.X_add_number = 2;
8287       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8288       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
8289                    7);
8290
8291       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8292          since that causes an overflow.  We should do that as well,
8293          but I don't see how to do the comparisons without a temporary
8294          register.  */
8295       --mips_opts.noreorder;
8296       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
8297       break;
8298
8299     case M_DIVU_3:
8300       s = "divu";
8301       s2 = "mflo";
8302       goto do_divu3;
8303     case M_REMU_3:
8304       s = "divu";
8305       s2 = "mfhi";
8306       goto do_divu3;
8307     case M_DDIVU_3:
8308       s = "ddivu";
8309       s2 = "mflo";
8310       goto do_divu3;
8311     case M_DREMU_3:
8312       s = "ddivu";
8313       s2 = "mfhi";
8314     do_divu3:
8315       mips_emit_delays (TRUE);
8316       ++mips_opts.noreorder;
8317       mips_any_noreorder = 1;
8318       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
8319                    xreg, yreg);
8320       expr1.X_add_number = 2;
8321       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8322       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
8323                    "6", 7);
8324       --mips_opts.noreorder;
8325       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
8326       break;
8327
8328     case M_DMUL:
8329       dbl = 1;
8330     case M_MUL:
8331       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8332                    dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8333       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
8334                    zreg);
8335       return;
8336
8337     case M_DSUBU_I:
8338       dbl = 1;
8339       goto do_subu;
8340     case M_SUBU_I:
8341     do_subu:
8342       if (imm_expr.X_op != O_constant)
8343         as_bad (_("Unsupported large constant"));
8344       imm_expr.X_add_number = -imm_expr.X_add_number;
8345       macro_build ((char *) NULL, &icnt, &imm_expr,
8346                    dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
8347       break;
8348
8349     case M_SUBU_I_2:
8350       if (imm_expr.X_op != O_constant)
8351         as_bad (_("Unsupported large constant"));
8352       imm_expr.X_add_number = -imm_expr.X_add_number;
8353       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
8354                    "x,k", xreg);
8355       break;
8356
8357     case M_DSUBU_I_2:
8358       if (imm_expr.X_op != O_constant)
8359         as_bad (_("Unsupported large constant"));
8360       imm_expr.X_add_number = -imm_expr.X_add_number;
8361       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
8362                    "y,j", yreg);
8363       break;
8364
8365     case M_BEQ:
8366       s = "cmp";
8367       s2 = "bteqz";
8368       goto do_branch;
8369     case M_BNE:
8370       s = "cmp";
8371       s2 = "btnez";
8372       goto do_branch;
8373     case M_BLT:
8374       s = "slt";
8375       s2 = "btnez";
8376       goto do_branch;
8377     case M_BLTU:
8378       s = "sltu";
8379       s2 = "btnez";
8380       goto do_branch;
8381     case M_BLE:
8382       s = "slt";
8383       s2 = "bteqz";
8384       goto do_reverse_branch;
8385     case M_BLEU:
8386       s = "sltu";
8387       s2 = "bteqz";
8388       goto do_reverse_branch;
8389     case M_BGE:
8390       s = "slt";
8391       s2 = "bteqz";
8392       goto do_branch;
8393     case M_BGEU:
8394       s = "sltu";
8395       s2 = "bteqz";
8396       goto do_branch;
8397     case M_BGT:
8398       s = "slt";
8399       s2 = "btnez";
8400       goto do_reverse_branch;
8401     case M_BGTU:
8402       s = "sltu";
8403       s2 = "btnez";
8404
8405     do_reverse_branch:
8406       tmp = xreg;
8407       xreg = yreg;
8408       yreg = tmp;
8409
8410     do_branch:
8411       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
8412                    xreg, yreg);
8413       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8414       break;
8415
8416     case M_BEQ_I:
8417       s = "cmpi";
8418       s2 = "bteqz";
8419       s3 = "x,U";
8420       goto do_branch_i;
8421     case M_BNE_I:
8422       s = "cmpi";
8423       s2 = "btnez";
8424       s3 = "x,U";
8425       goto do_branch_i;
8426     case M_BLT_I:
8427       s = "slti";
8428       s2 = "btnez";
8429       s3 = "x,8";
8430       goto do_branch_i;
8431     case M_BLTU_I:
8432       s = "sltiu";
8433       s2 = "btnez";
8434       s3 = "x,8";
8435       goto do_branch_i;
8436     case M_BLE_I:
8437       s = "slti";
8438       s2 = "btnez";
8439       s3 = "x,8";
8440       goto do_addone_branch_i;
8441     case M_BLEU_I:
8442       s = "sltiu";
8443       s2 = "btnez";
8444       s3 = "x,8";
8445       goto do_addone_branch_i;
8446     case M_BGE_I:
8447       s = "slti";
8448       s2 = "bteqz";
8449       s3 = "x,8";
8450       goto do_branch_i;
8451     case M_BGEU_I:
8452       s = "sltiu";
8453       s2 = "bteqz";
8454       s3 = "x,8";
8455       goto do_branch_i;
8456     case M_BGT_I:
8457       s = "slti";
8458       s2 = "bteqz";
8459       s3 = "x,8";
8460       goto do_addone_branch_i;
8461     case M_BGTU_I:
8462       s = "sltiu";
8463       s2 = "bteqz";
8464       s3 = "x,8";
8465
8466     do_addone_branch_i:
8467       if (imm_expr.X_op != O_constant)
8468         as_bad (_("Unsupported large constant"));
8469       ++imm_expr.X_add_number;
8470
8471     do_branch_i:
8472       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8473       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8474       break;
8475
8476     case M_ABS:
8477       expr1.X_add_number = 0;
8478       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8479       if (xreg != yreg)
8480         move_register (&icnt, xreg, yreg);
8481       expr1.X_add_number = 2;
8482       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8483       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8484                    "neg", "x,w", xreg, xreg);
8485     }
8486 }
8487
8488 /* For consistency checking, verify that all bits are specified either
8489    by the match/mask part of the instruction definition, or by the
8490    operand list.  */
8491 static int
8492 validate_mips_insn (opc)
8493      const struct mips_opcode *opc;
8494 {
8495   const char *p = opc->args;
8496   char c;
8497   unsigned long used_bits = opc->mask;
8498
8499   if ((used_bits & opc->match) != opc->match)
8500     {
8501       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8502               opc->name, opc->args);
8503       return 0;
8504     }
8505 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8506   while (*p)
8507     switch (c = *p++)
8508       {
8509       case ',': break;
8510       case '(': break;
8511       case ')': break;
8512       case '+':
8513         switch (c = *p++)
8514           {
8515           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8516           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8517           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8518           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8519                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8520           default:
8521             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8522                     c, opc->name, opc->args);
8523             return 0;
8524           }
8525         break;
8526       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8527       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8528       case 'A': break;
8529       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8530       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8531       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8532       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8533       case 'F': break;
8534       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8535       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8536       case 'I': break;
8537       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8538       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8539       case 'L': break;
8540       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8541       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8542       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8543       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8544                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8545       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8546       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8547       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8548       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8549       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8550       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8551       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8552       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8553       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8554       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8555       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8556       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8557       case 'f': break;
8558       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8559       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8560       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8561       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8562       case 'l': break;
8563       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8564       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8565       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8566       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8567       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8568       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8569       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8570       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8571       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8572       case 'x': break;
8573       case 'z': break;
8574       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8575       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8576                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8577       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8578       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8579       case '[': break;
8580       case ']': break;
8581       default:
8582         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8583                 c, opc->name, opc->args);
8584         return 0;
8585       }
8586 #undef USE_BITS
8587   if (used_bits != 0xffffffff)
8588     {
8589       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8590               ~used_bits & 0xffffffff, opc->name, opc->args);
8591       return 0;
8592     }
8593   return 1;
8594 }
8595
8596 /* This routine assembles an instruction into its binary format.  As a
8597    side effect, it sets one of the global variables imm_reloc or
8598    offset_reloc to the type of relocation to do if one of the operands
8599    is an address expression.  */
8600
8601 static void
8602 mips_ip (str, ip)
8603      char *str;
8604      struct mips_cl_insn *ip;
8605 {
8606   char *s;
8607   const char *args;
8608   char c = 0;
8609   struct mips_opcode *insn;
8610   char *argsStart;
8611   unsigned int regno;
8612   unsigned int lastregno = 0;
8613   unsigned int lastpos = 0;
8614   unsigned int limlo, limhi;
8615   char *s_reset;
8616   char save_c = 0;
8617
8618   insn_error = NULL;
8619
8620   /* If the instruction contains a '.', we first try to match an instruction
8621      including the '.'.  Then we try again without the '.'.  */
8622   insn = NULL;
8623   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8624     continue;
8625
8626   /* If we stopped on whitespace, then replace the whitespace with null for
8627      the call to hash_find.  Save the character we replaced just in case we
8628      have to re-parse the instruction.  */
8629   if (ISSPACE (*s))
8630     {
8631       save_c = *s;
8632       *s++ = '\0';
8633     }
8634
8635   insn = (struct mips_opcode *) hash_find (op_hash, str);
8636
8637   /* If we didn't find the instruction in the opcode table, try again, but
8638      this time with just the instruction up to, but not including the
8639      first '.'.  */
8640   if (insn == NULL)
8641     {
8642       /* Restore the character we overwrite above (if any).  */
8643       if (save_c)
8644         *(--s) = save_c;
8645
8646       /* Scan up to the first '.' or whitespace.  */
8647       for (s = str;
8648            *s != '\0' && *s != '.' && !ISSPACE (*s);
8649            ++s)
8650         continue;
8651
8652       /* If we did not find a '.', then we can quit now.  */
8653       if (*s != '.')
8654         {
8655           insn_error = "unrecognized opcode";
8656           return;
8657         }
8658
8659       /* Lookup the instruction in the hash table.  */
8660       *s++ = '\0';
8661       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8662         {
8663           insn_error = "unrecognized opcode";
8664           return;
8665         }
8666     }
8667
8668   argsStart = s;
8669   for (;;)
8670     {
8671       bfd_boolean ok;
8672
8673       assert (strcmp (insn->name, str) == 0);
8674
8675       if (OPCODE_IS_MEMBER (insn,
8676                             (mips_opts.isa
8677                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8678                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8679                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8680                             mips_arch))
8681         ok = TRUE;
8682       else
8683         ok = FALSE;
8684
8685       if (insn->pinfo != INSN_MACRO)
8686         {
8687           if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8688             ok = FALSE;
8689         }
8690
8691       if (! ok)
8692         {
8693           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8694               && strcmp (insn->name, insn[1].name) == 0)
8695             {
8696               ++insn;
8697               continue;
8698             }
8699           else
8700             {
8701               if (!insn_error)
8702                 {
8703                   static char buf[100];
8704                   if (mips_arch_info->is_isa)
8705                     sprintf (buf,
8706                              _("opcode not supported at this ISA level (%s)"),
8707                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8708                   else
8709                     sprintf (buf,
8710                              _("opcode not supported on this processor: %s (%s)"),
8711                              mips_arch_info->name,
8712                              mips_cpu_info_from_isa (mips_opts.isa)->name);
8713                   insn_error = buf;
8714                 }
8715               if (save_c)
8716                 *(--s) = save_c;
8717               return;
8718             }
8719         }
8720
8721       ip->insn_mo = insn;
8722       ip->insn_opcode = insn->match;
8723       insn_error = NULL;
8724       for (args = insn->args;; ++args)
8725         {
8726           int is_mdmx;
8727
8728           s += strspn (s, " \t");
8729           is_mdmx = 0;
8730           switch (*args)
8731             {
8732             case '\0':          /* end of args */
8733               if (*s == '\0')
8734                 return;
8735               break;
8736
8737             case ',':
8738               if (*s++ == *args)
8739                 continue;
8740               s--;
8741               switch (*++args)
8742                 {
8743                 case 'r':
8744                 case 'v':
8745                   ip->insn_opcode |= lastregno << OP_SH_RS;
8746                   continue;
8747
8748                 case 'w':
8749                   ip->insn_opcode |= lastregno << OP_SH_RT;
8750                   continue;
8751
8752                 case 'W':
8753                   ip->insn_opcode |= lastregno << OP_SH_FT;
8754                   continue;
8755
8756                 case 'V':
8757                   ip->insn_opcode |= lastregno << OP_SH_FS;
8758                   continue;
8759                 }
8760               break;
8761
8762             case '(':
8763               /* Handle optional base register.
8764                  Either the base register is omitted or
8765                  we must have a left paren.  */
8766               /* This is dependent on the next operand specifier
8767                  is a base register specification.  */
8768               assert (args[1] == 'b' || args[1] == '5'
8769                       || args[1] == '-' || args[1] == '4');
8770               if (*s == '\0')
8771                 return;
8772
8773             case ')':           /* these must match exactly */
8774             case '[':
8775             case ']':
8776               if (*s++ == *args)
8777                 continue;
8778               break;
8779
8780             case '+':           /* Opcode extension character.  */
8781               switch (*++args)
8782                 {
8783                 case 'A':               /* ins/ext position, becomes LSB.  */
8784                   limlo = 0;
8785                   limhi = 31;
8786                   my_getExpression (&imm_expr, s);
8787                   check_absolute_expr (ip, &imm_expr);
8788                   if ((unsigned long) imm_expr.X_add_number < limlo
8789                       || (unsigned long) imm_expr.X_add_number > limhi)
8790                     {
8791                       as_bad (_("Improper position (%lu)"),
8792                               (unsigned long) imm_expr.X_add_number);
8793                       imm_expr.X_add_number = limlo;
8794                     }
8795                   lastpos = imm_expr.X_add_number;
8796                   ip->insn_opcode |= (imm_expr.X_add_number
8797                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8798                   imm_expr.X_op = O_absent;
8799                   s = expr_end;
8800                   continue;
8801
8802                 case 'B':               /* ins size, becomes MSB.  */
8803                   limlo = 1;
8804                   limhi = 32;
8805                   my_getExpression (&imm_expr, s);
8806                   check_absolute_expr (ip, &imm_expr);
8807                   /* Check for negative input so that small negative numbers
8808                      will not succeed incorrectly.  The checks against
8809                      (pos+size) transitively check "size" itself,
8810                      assuming that "pos" is reasonable.  */
8811                   if ((long) imm_expr.X_add_number < 0
8812                       || ((unsigned long) imm_expr.X_add_number
8813                           + lastpos) < limlo
8814                       || ((unsigned long) imm_expr.X_add_number
8815                           + lastpos) > limhi)
8816                     {
8817                       as_bad (_("Improper insert size (%lu, position %lu)"),
8818                               (unsigned long) imm_expr.X_add_number,
8819                               (unsigned long) lastpos);
8820                       imm_expr.X_add_number = limlo - lastpos;
8821                     }
8822                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8823                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8824                   imm_expr.X_op = O_absent;
8825                   s = expr_end;
8826                   continue;
8827
8828                 case 'C':               /* ext size, becomes MSBD.  */
8829                   limlo = 1;
8830                   limhi = 32;
8831                   my_getExpression (&imm_expr, s);
8832                   check_absolute_expr (ip, &imm_expr);
8833                   /* Check for negative input so that small negative numbers
8834                      will not succeed incorrectly.  The checks against
8835                      (pos+size) transitively check "size" itself,
8836                      assuming that "pos" is reasonable.  */
8837                   if ((long) imm_expr.X_add_number < 0
8838                       || ((unsigned long) imm_expr.X_add_number
8839                           + lastpos) < limlo
8840                       || ((unsigned long) imm_expr.X_add_number
8841                           + lastpos) > limhi)
8842                     {
8843                       as_bad (_("Improper extract size (%lu, position %lu)"),
8844                               (unsigned long) imm_expr.X_add_number,
8845                               (unsigned long) lastpos);
8846                       imm_expr.X_add_number = limlo - lastpos;
8847                     }
8848                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8849                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8850                   imm_expr.X_op = O_absent;
8851                   s = expr_end;
8852                   continue;
8853
8854                 case 'D':
8855                   /* +D is for disassembly only; never match.  */
8856                   break;
8857
8858                 default:
8859                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8860                     *args, insn->name, insn->args);
8861                   /* Further processing is fruitless.  */
8862                   return;
8863                 }
8864               break;
8865
8866             case '<':           /* must be at least one digit */
8867               /*
8868                * According to the manual, if the shift amount is greater
8869                * than 31 or less than 0, then the shift amount should be
8870                * mod 32.  In reality the mips assembler issues an error.
8871                * We issue a warning and mask out all but the low 5 bits.
8872                */
8873               my_getExpression (&imm_expr, s);
8874               check_absolute_expr (ip, &imm_expr);
8875               if ((unsigned long) imm_expr.X_add_number > 31)
8876                 {
8877                   as_warn (_("Improper shift amount (%lu)"),
8878                            (unsigned long) imm_expr.X_add_number);
8879                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8880                 }
8881               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8882               imm_expr.X_op = O_absent;
8883               s = expr_end;
8884               continue;
8885
8886             case '>':           /* shift amount minus 32 */
8887               my_getExpression (&imm_expr, s);
8888               check_absolute_expr (ip, &imm_expr);
8889               if ((unsigned long) imm_expr.X_add_number < 32
8890                   || (unsigned long) imm_expr.X_add_number > 63)
8891                 break;
8892               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8893               imm_expr.X_op = O_absent;
8894               s = expr_end;
8895               continue;
8896
8897             case 'k':           /* cache code */
8898             case 'h':           /* prefx code */
8899               my_getExpression (&imm_expr, s);
8900               check_absolute_expr (ip, &imm_expr);
8901               if ((unsigned long) imm_expr.X_add_number > 31)
8902                 {
8903                   as_warn (_("Invalid value for `%s' (%lu)"),
8904                            ip->insn_mo->name,
8905                            (unsigned long) imm_expr.X_add_number);
8906                   imm_expr.X_add_number &= 0x1f;
8907                 }
8908               if (*args == 'k')
8909                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8910               else
8911                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8912               imm_expr.X_op = O_absent;
8913               s = expr_end;
8914               continue;
8915
8916             case 'c':           /* break code */
8917               my_getExpression (&imm_expr, s);
8918               check_absolute_expr (ip, &imm_expr);
8919               if ((unsigned long) imm_expr.X_add_number > 1023)
8920                 {
8921                   as_warn (_("Illegal break code (%lu)"),
8922                            (unsigned long) imm_expr.X_add_number);
8923                   imm_expr.X_add_number &= OP_MASK_CODE;
8924                 }
8925               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8926               imm_expr.X_op = O_absent;
8927               s = expr_end;
8928               continue;
8929
8930             case 'q':           /* lower break code */
8931               my_getExpression (&imm_expr, s);
8932               check_absolute_expr (ip, &imm_expr);
8933               if ((unsigned long) imm_expr.X_add_number > 1023)
8934                 {
8935                   as_warn (_("Illegal lower break code (%lu)"),
8936                            (unsigned long) imm_expr.X_add_number);
8937                   imm_expr.X_add_number &= OP_MASK_CODE2;
8938                 }
8939               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8940               imm_expr.X_op = O_absent;
8941               s = expr_end;
8942               continue;
8943
8944             case 'B':           /* 20-bit syscall/break code.  */
8945               my_getExpression (&imm_expr, s);
8946               check_absolute_expr (ip, &imm_expr);
8947               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8948                 as_warn (_("Illegal 20-bit code (%lu)"),
8949                          (unsigned long) imm_expr.X_add_number);
8950               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8951               imm_expr.X_op = O_absent;
8952               s = expr_end;
8953               continue;
8954
8955             case 'C':           /* Coprocessor code */
8956               my_getExpression (&imm_expr, s);
8957               check_absolute_expr (ip, &imm_expr);
8958               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8959                 {
8960                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8961                            (unsigned long) imm_expr.X_add_number);
8962                   imm_expr.X_add_number &= ((1 << 25) - 1);
8963                 }
8964               ip->insn_opcode |= imm_expr.X_add_number;
8965               imm_expr.X_op = O_absent;
8966               s = expr_end;
8967               continue;
8968
8969             case 'J':           /* 19-bit wait code.  */
8970               my_getExpression (&imm_expr, s);
8971               check_absolute_expr (ip, &imm_expr);
8972               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8973                 as_warn (_("Illegal 19-bit code (%lu)"),
8974                          (unsigned long) imm_expr.X_add_number);
8975               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8976               imm_expr.X_op = O_absent;
8977               s = expr_end;
8978               continue;
8979
8980             case 'P':           /* Performance register */
8981               my_getExpression (&imm_expr, s);
8982               check_absolute_expr (ip, &imm_expr);
8983               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8984                 {
8985                   as_warn (_("Invalid performance register (%lu)"),
8986                            (unsigned long) imm_expr.X_add_number);
8987                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8988                 }
8989               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8990               imm_expr.X_op = O_absent;
8991               s = expr_end;
8992               continue;
8993
8994             case 'b':           /* base register */
8995             case 'd':           /* destination register */
8996             case 's':           /* source register */
8997             case 't':           /* target register */
8998             case 'r':           /* both target and source */
8999             case 'v':           /* both dest and source */
9000             case 'w':           /* both dest and target */
9001             case 'E':           /* coprocessor target register */
9002             case 'G':           /* coprocessor destination register */
9003             case 'K':           /* 'rdhwr' destination register */
9004             case 'x':           /* ignore register name */
9005             case 'z':           /* must be zero register */
9006             case 'U':           /* destination register (clo/clz).  */
9007               s_reset = s;
9008               if (s[0] == '$')
9009                 {
9010
9011                   if (ISDIGIT (s[1]))
9012                     {
9013                       ++s;
9014                       regno = 0;
9015                       do
9016                         {
9017                           regno *= 10;
9018                           regno += *s - '0';
9019                           ++s;
9020                         }
9021                       while (ISDIGIT (*s));
9022                       if (regno > 31)
9023                         as_bad (_("Invalid register number (%d)"), regno);
9024                     }
9025                   else if (*args == 'E' || *args == 'G' || *args == 'K')
9026                     goto notreg;
9027                   else
9028                     {
9029                       if (s[1] == 'r' && s[2] == 'a')
9030                         {
9031                           s += 3;
9032                           regno = RA;
9033                         }
9034                       else if (s[1] == 'f' && s[2] == 'p')
9035                         {
9036                           s += 3;
9037                           regno = FP;
9038                         }
9039                       else if (s[1] == 's' && s[2] == 'p')
9040                         {
9041                           s += 3;
9042                           regno = SP;
9043                         }
9044                       else if (s[1] == 'g' && s[2] == 'p')
9045                         {
9046                           s += 3;
9047                           regno = GP;
9048                         }
9049                       else if (s[1] == 'a' && s[2] == 't')
9050                         {
9051                           s += 3;
9052                           regno = AT;
9053                         }
9054                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9055                         {
9056                           s += 4;
9057                           regno = KT0;
9058                         }
9059                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9060                         {
9061                           s += 4;
9062                           regno = KT1;
9063                         }
9064                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9065                         {
9066                           s += 5;
9067                           regno = ZERO;
9068                         }
9069                       else if (itbl_have_entries)
9070                         {
9071                           char *p, *n;
9072                           unsigned long r;
9073
9074                           p = s + 1;    /* advance past '$' */
9075                           n = itbl_get_field (&p);  /* n is name */
9076
9077                           /* See if this is a register defined in an
9078                              itbl entry.  */
9079                           if (itbl_get_reg_val (n, &r))
9080                             {
9081                               /* Get_field advances to the start of
9082                                  the next field, so we need to back
9083                                  rack to the end of the last field.  */
9084                               if (p)
9085                                 s = p - 1;
9086                               else
9087                                 s = strchr (s, '\0');
9088                               regno = r;
9089                             }
9090                           else
9091                             goto notreg;
9092                         }
9093                       else
9094                         goto notreg;
9095                     }
9096                   if (regno == AT
9097                       && ! mips_opts.noat
9098                       && *args != 'E'
9099                       && *args != 'G'
9100                       && *args != 'K')
9101                     as_warn (_("Used $at without \".set noat\""));
9102                   c = *args;
9103                   if (*s == ' ')
9104                     ++s;
9105                   if (args[1] != *s)
9106                     {
9107                       if (c == 'r' || c == 'v' || c == 'w')
9108                         {
9109                           regno = lastregno;
9110                           s = s_reset;
9111                           ++args;
9112                         }
9113                     }
9114                   /* 'z' only matches $0.  */
9115                   if (c == 'z' && regno != 0)
9116                     break;
9117
9118         /* Now that we have assembled one operand, we use the args string
9119          * to figure out where it goes in the instruction.  */
9120                   switch (c)
9121                     {
9122                     case 'r':
9123                     case 's':
9124                     case 'v':
9125                     case 'b':
9126                       ip->insn_opcode |= regno << OP_SH_RS;
9127                       break;
9128                     case 'd':
9129                     case 'G':
9130                     case 'K':
9131                       ip->insn_opcode |= regno << OP_SH_RD;
9132                       break;
9133                     case 'U':
9134                       ip->insn_opcode |= regno << OP_SH_RD;
9135                       ip->insn_opcode |= regno << OP_SH_RT;
9136                       break;
9137                     case 'w':
9138                     case 't':
9139                     case 'E':
9140                       ip->insn_opcode |= regno << OP_SH_RT;
9141                       break;
9142                     case 'x':
9143                       /* This case exists because on the r3000 trunc
9144                          expands into a macro which requires a gp
9145                          register.  On the r6000 or r4000 it is
9146                          assembled into a single instruction which
9147                          ignores the register.  Thus the insn version
9148                          is MIPS_ISA2 and uses 'x', and the macro
9149                          version is MIPS_ISA1 and uses 't'.  */
9150                       break;
9151                     case 'z':
9152                       /* This case is for the div instruction, which
9153                          acts differently if the destination argument
9154                          is $0.  This only matches $0, and is checked
9155                          outside the switch.  */
9156                       break;
9157                     case 'D':
9158                       /* Itbl operand; not yet implemented. FIXME ?? */
9159                       break;
9160                       /* What about all other operands like 'i', which
9161                          can be specified in the opcode table? */
9162                     }
9163                   lastregno = regno;
9164                   continue;
9165                 }
9166             notreg:
9167               switch (*args++)
9168                 {
9169                 case 'r':
9170                 case 'v':
9171                   ip->insn_opcode |= lastregno << OP_SH_RS;
9172                   continue;
9173                 case 'w':
9174                   ip->insn_opcode |= lastregno << OP_SH_RT;
9175                   continue;
9176                 }
9177               break;
9178
9179             case 'O':           /* MDMX alignment immediate constant.  */
9180               my_getExpression (&imm_expr, s);
9181               check_absolute_expr (ip, &imm_expr);
9182               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9183                 {
9184                   as_warn ("Improper align amount (%ld), using low bits",
9185                            (long) imm_expr.X_add_number);
9186                   imm_expr.X_add_number &= OP_MASK_ALN;
9187                 }
9188               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
9189               imm_expr.X_op = O_absent;
9190               s = expr_end;
9191               continue;
9192
9193             case 'Q':           /* MDMX vector, element sel, or const.  */
9194               if (s[0] != '$')
9195                 {
9196                   /* MDMX Immediate.  */
9197                   my_getExpression (&imm_expr, s);
9198                   check_absolute_expr (ip, &imm_expr);
9199                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9200                     {
9201                       as_warn (_("Invalid MDMX Immediate (%ld)"),
9202                                (long) imm_expr.X_add_number);
9203                       imm_expr.X_add_number &= OP_MASK_FT;
9204                     }
9205                   imm_expr.X_add_number &= OP_MASK_FT;
9206                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9207                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9208                   else
9209                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9210                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
9211                   imm_expr.X_op = O_absent;
9212                   s = expr_end;
9213                   continue;
9214                 }
9215               /* Not MDMX Immediate.  Fall through.  */
9216             case 'X':           /* MDMX destination register.  */
9217             case 'Y':           /* MDMX source register.  */
9218             case 'Z':           /* MDMX target register.  */
9219               is_mdmx = 1;
9220             case 'D':           /* floating point destination register */
9221             case 'S':           /* floating point source register */
9222             case 'T':           /* floating point target register */
9223             case 'R':           /* floating point source register */
9224             case 'V':
9225             case 'W':
9226               s_reset = s;
9227               /* Accept $fN for FP and MDMX register numbers, and in
9228                  addition accept $vN for MDMX register numbers.  */
9229               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
9230                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
9231                       && ISDIGIT (s[2])))
9232                 {
9233                   s += 2;
9234                   regno = 0;
9235                   do
9236                     {
9237                       regno *= 10;
9238                       regno += *s - '0';
9239                       ++s;
9240                     }
9241                   while (ISDIGIT (*s));
9242
9243                   if (regno > 31)
9244                     as_bad (_("Invalid float register number (%d)"), regno);
9245
9246                   if ((regno & 1) != 0
9247                       && HAVE_32BIT_FPRS
9248                       && ! (strcmp (str, "mtc1") == 0
9249                             || strcmp (str, "mfc1") == 0
9250                             || strcmp (str, "lwc1") == 0
9251                             || strcmp (str, "swc1") == 0
9252                             || strcmp (str, "l.s") == 0
9253                             || strcmp (str, "s.s") == 0))
9254                     as_warn (_("Float register should be even, was %d"),
9255                              regno);
9256
9257                   c = *args;
9258                   if (*s == ' ')
9259                     ++s;
9260                   if (args[1] != *s)
9261                     {
9262                       if (c == 'V' || c == 'W')
9263                         {
9264                           regno = lastregno;
9265                           s = s_reset;
9266                           ++args;
9267                         }
9268                     }
9269                   switch (c)
9270                     {
9271                     case 'D':
9272                     case 'X':
9273                       ip->insn_opcode |= regno << OP_SH_FD;
9274                       break;
9275                     case 'V':
9276                     case 'S':
9277                     case 'Y':
9278                       ip->insn_opcode |= regno << OP_SH_FS;
9279                       break;
9280                     case 'Q':
9281                       /* This is like 'Z', but also needs to fix the MDMX
9282                          vector/scalar select bits.  Note that the
9283                          scalar immediate case is handled above.  */
9284                       if (*s == '[')
9285                         {
9286                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9287                           int max_el = (is_qh ? 3 : 7);
9288                           s++;
9289                           my_getExpression(&imm_expr, s);
9290                           check_absolute_expr (ip, &imm_expr);
9291                           s = expr_end;
9292                           if (imm_expr.X_add_number > max_el)
9293                             as_bad(_("Bad element selector %ld"),
9294                                    (long) imm_expr.X_add_number);
9295                           imm_expr.X_add_number &= max_el;
9296                           ip->insn_opcode |= (imm_expr.X_add_number
9297                                               << (OP_SH_VSEL +
9298                                                   (is_qh ? 2 : 1)));
9299                           if (*s != ']')
9300                             as_warn(_("Expecting ']' found '%s'"), s);
9301                           else
9302                             s++;
9303                         }
9304                       else
9305                         {
9306                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9307                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9308                                                 << OP_SH_VSEL);
9309                           else
9310                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9311                                                 OP_SH_VSEL);
9312                         }
9313                       /* Fall through */
9314                     case 'W':
9315                     case 'T':
9316                     case 'Z':
9317                       ip->insn_opcode |= regno << OP_SH_FT;
9318                       break;
9319                     case 'R':
9320                       ip->insn_opcode |= regno << OP_SH_FR;
9321                       break;
9322                     }
9323                   lastregno = regno;
9324                   continue;
9325                 }
9326
9327               switch (*args++)
9328                 {
9329                 case 'V':
9330                   ip->insn_opcode |= lastregno << OP_SH_FS;
9331                   continue;
9332                 case 'W':
9333                   ip->insn_opcode |= lastregno << OP_SH_FT;
9334                   continue;
9335                 }
9336               break;
9337
9338             case 'I':
9339               my_getExpression (&imm_expr, s);
9340               if (imm_expr.X_op != O_big
9341                   && imm_expr.X_op != O_constant)
9342                 insn_error = _("absolute expression required");
9343               s = expr_end;
9344               continue;
9345
9346             case 'A':
9347               my_getExpression (&offset_expr, s);
9348               *imm_reloc = BFD_RELOC_32;
9349               s = expr_end;
9350               continue;
9351
9352             case 'F':
9353             case 'L':
9354             case 'f':
9355             case 'l':
9356               {
9357                 int f64;
9358                 int using_gprs;
9359                 char *save_in;
9360                 char *err;
9361                 unsigned char temp[8];
9362                 int len;
9363                 unsigned int length;
9364                 segT seg;
9365                 subsegT subseg;
9366                 char *p;
9367
9368                 /* These only appear as the last operand in an
9369                    instruction, and every instruction that accepts
9370                    them in any variant accepts them in all variants.
9371                    This means we don't have to worry about backing out
9372                    any changes if the instruction does not match.
9373
9374                    The difference between them is the size of the
9375                    floating point constant and where it goes.  For 'F'
9376                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9377                    is 32 bits.  Where the constant is placed is based
9378                    on how the MIPS assembler does things:
9379                     F -- .rdata
9380                     L -- .lit8
9381                     f -- immediate value
9382                     l -- .lit4
9383
9384                     The .lit4 and .lit8 sections are only used if
9385                     permitted by the -G argument.
9386
9387                     When generating embedded PIC code, we use the
9388                     .lit8 section but not the .lit4 section (we can do
9389                     .lit4 inline easily; we need to put .lit8
9390                     somewhere in the data segment, and using .lit8
9391                     permits the linker to eventually combine identical
9392                     .lit8 entries).
9393
9394                     The code below needs to know whether the target register
9395                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9396                     'F' are used with GPR-based instructions and 'l' and
9397                     'L' are used with FPR-based instructions.  */
9398
9399                 f64 = *args == 'F' || *args == 'L';
9400                 using_gprs = *args == 'F' || *args == 'f';
9401
9402                 save_in = input_line_pointer;
9403                 input_line_pointer = s;
9404                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9405                 length = len;
9406                 s = input_line_pointer;
9407                 input_line_pointer = save_in;
9408                 if (err != NULL && *err != '\0')
9409                   {
9410                     as_bad (_("Bad floating point constant: %s"), err);
9411                     memset (temp, '\0', sizeof temp);
9412                     length = f64 ? 8 : 4;
9413                   }
9414
9415                 assert (length == (unsigned) (f64 ? 8 : 4));
9416
9417                 if (*args == 'f'
9418                     || (*args == 'l'
9419                         && (! USE_GLOBAL_POINTER_OPT
9420                             || mips_pic == EMBEDDED_PIC
9421                             || g_switch_value < 4
9422                             || (temp[0] == 0 && temp[1] == 0)
9423                             || (temp[2] == 0 && temp[3] == 0))))
9424                   {
9425                     imm_expr.X_op = O_constant;
9426                     if (! target_big_endian)
9427                       imm_expr.X_add_number = bfd_getl32 (temp);
9428                     else
9429                       imm_expr.X_add_number = bfd_getb32 (temp);
9430                   }
9431                 else if (length > 4
9432                          && ! mips_disable_float_construction
9433                          /* Constants can only be constructed in GPRs and
9434                             copied to FPRs if the GPRs are at least as wide
9435                             as the FPRs.  Force the constant into memory if
9436                             we are using 64-bit FPRs but the GPRs are only
9437                             32 bits wide.  */
9438                          && (using_gprs
9439                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9440                          && ((temp[0] == 0 && temp[1] == 0)
9441                              || (temp[2] == 0 && temp[3] == 0))
9442                          && ((temp[4] == 0 && temp[5] == 0)
9443                              || (temp[6] == 0 && temp[7] == 0)))
9444                   {
9445                     /* The value is simple enough to load with a couple of
9446                        instructions.  If using 32-bit registers, set
9447                        imm_expr to the high order 32 bits and offset_expr to
9448                        the low order 32 bits.  Otherwise, set imm_expr to
9449                        the entire 64 bit constant.  */
9450                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9451                       {
9452                         imm_expr.X_op = O_constant;
9453                         offset_expr.X_op = O_constant;
9454                         if (! target_big_endian)
9455                           {
9456                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9457                             offset_expr.X_add_number = bfd_getl32 (temp);
9458                           }
9459                         else
9460                           {
9461                             imm_expr.X_add_number = bfd_getb32 (temp);
9462                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9463                           }
9464                         if (offset_expr.X_add_number == 0)
9465                           offset_expr.X_op = O_absent;
9466                       }
9467                     else if (sizeof (imm_expr.X_add_number) > 4)
9468                       {
9469                         imm_expr.X_op = O_constant;
9470                         if (! target_big_endian)
9471                           imm_expr.X_add_number = bfd_getl64 (temp);
9472                         else
9473                           imm_expr.X_add_number = bfd_getb64 (temp);
9474                       }
9475                     else
9476                       {
9477                         imm_expr.X_op = O_big;
9478                         imm_expr.X_add_number = 4;
9479                         if (! target_big_endian)
9480                           {
9481                             generic_bignum[0] = bfd_getl16 (temp);
9482                             generic_bignum[1] = bfd_getl16 (temp + 2);
9483                             generic_bignum[2] = bfd_getl16 (temp + 4);
9484                             generic_bignum[3] = bfd_getl16 (temp + 6);
9485                           }
9486                         else
9487                           {
9488                             generic_bignum[0] = bfd_getb16 (temp + 6);
9489                             generic_bignum[1] = bfd_getb16 (temp + 4);
9490                             generic_bignum[2] = bfd_getb16 (temp + 2);
9491                             generic_bignum[3] = bfd_getb16 (temp);
9492                           }
9493                       }
9494                   }
9495                 else
9496                   {
9497                     const char *newname;
9498                     segT new_seg;
9499
9500                     /* Switch to the right section.  */
9501                     seg = now_seg;
9502                     subseg = now_subseg;
9503                     switch (*args)
9504                       {
9505                       default: /* unused default case avoids warnings.  */
9506                       case 'L':
9507                         newname = RDATA_SECTION_NAME;
9508                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9509                             || mips_pic == EMBEDDED_PIC)
9510                           newname = ".lit8";
9511                         break;
9512                       case 'F':
9513                         if (mips_pic == EMBEDDED_PIC)
9514                           newname = ".lit8";
9515                         else
9516                           newname = RDATA_SECTION_NAME;
9517                         break;
9518                       case 'l':
9519                         assert (!USE_GLOBAL_POINTER_OPT
9520                                 || g_switch_value >= 4);
9521                         newname = ".lit4";
9522                         break;
9523                       }
9524                     new_seg = subseg_new (newname, (subsegT) 0);
9525                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9526                       bfd_set_section_flags (stdoutput, new_seg,
9527                                              (SEC_ALLOC
9528                                               | SEC_LOAD
9529                                               | SEC_READONLY
9530                                               | SEC_DATA));
9531                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9532                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9533                         && strcmp (TARGET_OS, "elf") != 0)
9534                       record_alignment (new_seg, 4);
9535                     else
9536                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9537                     if (seg == now_seg)
9538                       as_bad (_("Can't use floating point insn in this section"));
9539
9540                     /* Set the argument to the current address in the
9541                        section.  */
9542                     offset_expr.X_op = O_symbol;
9543                     offset_expr.X_add_symbol =
9544                       symbol_new ("L0\001", now_seg,
9545                                   (valueT) frag_now_fix (), frag_now);
9546                     offset_expr.X_add_number = 0;
9547
9548                     /* Put the floating point number into the section.  */
9549                     p = frag_more ((int) length);
9550                     memcpy (p, temp, length);
9551
9552                     /* Switch back to the original section.  */
9553                     subseg_set (seg, subseg);
9554                   }
9555               }
9556               continue;
9557
9558             case 'i':           /* 16 bit unsigned immediate */
9559             case 'j':           /* 16 bit signed immediate */
9560               *imm_reloc = BFD_RELOC_LO16;
9561               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9562                 {
9563                   int more;
9564                   offsetT minval, maxval;
9565
9566                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9567                           && strcmp (insn->name, insn[1].name) == 0);
9568
9569                   /* If the expression was written as an unsigned number,
9570                      only treat it as signed if there are no more
9571                      alternatives.  */
9572                   if (more
9573                       && *args == 'j'
9574                       && sizeof (imm_expr.X_add_number) <= 4
9575                       && imm_expr.X_op == O_constant
9576                       && imm_expr.X_add_number < 0
9577                       && imm_expr.X_unsigned
9578                       && HAVE_64BIT_GPRS)
9579                     break;
9580
9581                   /* For compatibility with older assemblers, we accept
9582                      0x8000-0xffff as signed 16-bit numbers when only
9583                      signed numbers are allowed.  */
9584                   if (*args == 'i')
9585                     minval = 0, maxval = 0xffff;
9586                   else if (more)
9587                     minval = -0x8000, maxval = 0x7fff;
9588                   else
9589                     minval = -0x8000, maxval = 0xffff;
9590
9591                   if (imm_expr.X_op != O_constant
9592                       || imm_expr.X_add_number < minval
9593                       || imm_expr.X_add_number > maxval)
9594                     {
9595                       if (more)
9596                         break;
9597                       if (imm_expr.X_op == O_constant
9598                           || imm_expr.X_op == O_big)
9599                         as_bad (_("expression out of range"));
9600                     }
9601                 }
9602               s = expr_end;
9603               continue;
9604
9605             case 'o':           /* 16 bit offset */
9606               /* Check whether there is only a single bracketed expression
9607                  left.  If so, it must be the base register and the
9608                  constant must be zero.  */
9609               if (*s == '(' && strchr (s + 1, '(') == 0)
9610                 {
9611                   offset_expr.X_op = O_constant;
9612                   offset_expr.X_add_number = 0;
9613                   continue;
9614                 }
9615
9616               /* If this value won't fit into a 16 bit offset, then go
9617                  find a macro that will generate the 32 bit offset
9618                  code pattern.  */
9619               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9620                   && (offset_expr.X_op != O_constant
9621                       || offset_expr.X_add_number >= 0x8000
9622                       || offset_expr.X_add_number < -0x8000))
9623                 break;
9624
9625               s = expr_end;
9626               continue;
9627
9628             case 'p':           /* pc relative offset */
9629               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9630               my_getExpression (&offset_expr, s);
9631               s = expr_end;
9632               continue;
9633
9634             case 'u':           /* upper 16 bits */
9635               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9636                   && imm_expr.X_op == O_constant
9637                   && (imm_expr.X_add_number < 0
9638                       || imm_expr.X_add_number >= 0x10000))
9639                 as_bad (_("lui expression not in range 0..65535"));
9640               s = expr_end;
9641               continue;
9642
9643             case 'a':           /* 26 bit address */
9644               my_getExpression (&offset_expr, s);
9645               s = expr_end;
9646               *offset_reloc = BFD_RELOC_MIPS_JMP;
9647               continue;
9648
9649             case 'N':           /* 3 bit branch condition code */
9650             case 'M':           /* 3 bit compare condition code */
9651               if (strncmp (s, "$fcc", 4) != 0)
9652                 break;
9653               s += 4;
9654               regno = 0;
9655               do
9656                 {
9657                   regno *= 10;
9658                   regno += *s - '0';
9659                   ++s;
9660                 }
9661               while (ISDIGIT (*s));
9662               if (regno > 7)
9663                 as_bad (_("invalid condition code register $fcc%d"), regno);
9664               if (*args == 'N')
9665                 ip->insn_opcode |= regno << OP_SH_BCC;
9666               else
9667                 ip->insn_opcode |= regno << OP_SH_CCC;
9668               continue;
9669
9670             case 'H':
9671               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9672                 s += 2;
9673               if (ISDIGIT (*s))
9674                 {
9675                   c = 0;
9676                   do
9677                     {
9678                       c *= 10;
9679                       c += *s - '0';
9680                       ++s;
9681                     }
9682                   while (ISDIGIT (*s));
9683                 }
9684               else
9685                 c = 8; /* Invalid sel value.  */
9686
9687               if (c > 7)
9688                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9689               ip->insn_opcode |= c;
9690               continue;
9691
9692             case 'e':
9693               /* Must be at least one digit.  */
9694               my_getExpression (&imm_expr, s);
9695               check_absolute_expr (ip, &imm_expr);
9696
9697               if ((unsigned long) imm_expr.X_add_number
9698                   > (unsigned long) OP_MASK_VECBYTE)
9699                 {
9700                   as_bad (_("bad byte vector index (%ld)"),
9701                            (long) imm_expr.X_add_number);
9702                   imm_expr.X_add_number = 0;
9703                 }
9704
9705               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9706               imm_expr.X_op = O_absent;
9707               s = expr_end;
9708               continue;
9709
9710             case '%':
9711               my_getExpression (&imm_expr, s);
9712               check_absolute_expr (ip, &imm_expr);
9713
9714               if ((unsigned long) imm_expr.X_add_number
9715                   > (unsigned long) OP_MASK_VECALIGN)
9716                 {
9717                   as_bad (_("bad byte vector index (%ld)"),
9718                            (long) imm_expr.X_add_number);
9719                   imm_expr.X_add_number = 0;
9720                 }
9721
9722               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9723               imm_expr.X_op = O_absent;
9724               s = expr_end;
9725               continue;
9726
9727             default:
9728               as_bad (_("bad char = '%c'\n"), *args);
9729               internalError ();
9730             }
9731           break;
9732         }
9733       /* Args don't match.  */
9734       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9735           !strcmp (insn->name, insn[1].name))
9736         {
9737           ++insn;
9738           s = argsStart;
9739           insn_error = _("illegal operands");
9740           continue;
9741         }
9742       if (save_c)
9743         *(--s) = save_c;
9744       insn_error = _("illegal operands");
9745       return;
9746     }
9747 }
9748
9749 /* This routine assembles an instruction into its binary format when
9750    assembling for the mips16.  As a side effect, it sets one of the
9751    global variables imm_reloc or offset_reloc to the type of
9752    relocation to do if one of the operands is an address expression.
9753    It also sets mips16_small and mips16_ext if the user explicitly
9754    requested a small or extended instruction.  */
9755
9756 static void
9757 mips16_ip (str, ip)
9758      char *str;
9759      struct mips_cl_insn *ip;
9760 {
9761   char *s;
9762   const char *args;
9763   struct mips_opcode *insn;
9764   char *argsstart;
9765   unsigned int regno;
9766   unsigned int lastregno = 0;
9767   char *s_reset;
9768
9769   insn_error = NULL;
9770
9771   mips16_small = FALSE;
9772   mips16_ext = FALSE;
9773
9774   for (s = str; ISLOWER (*s); ++s)
9775     ;
9776   switch (*s)
9777     {
9778     case '\0':
9779       break;
9780
9781     case ' ':
9782       *s++ = '\0';
9783       break;
9784
9785     case '.':
9786       if (s[1] == 't' && s[2] == ' ')
9787         {
9788           *s = '\0';
9789           mips16_small = TRUE;
9790           s += 3;
9791           break;
9792         }
9793       else if (s[1] == 'e' && s[2] == ' ')
9794         {
9795           *s = '\0';
9796           mips16_ext = TRUE;
9797           s += 3;
9798           break;
9799         }
9800       /* Fall through.  */
9801     default:
9802       insn_error = _("unknown opcode");
9803       return;
9804     }
9805
9806   if (mips_opts.noautoextend && ! mips16_ext)
9807     mips16_small = TRUE;
9808
9809   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9810     {
9811       insn_error = _("unrecognized opcode");
9812       return;
9813     }
9814
9815   argsstart = s;
9816   for (;;)
9817     {
9818       assert (strcmp (insn->name, str) == 0);
9819
9820       ip->insn_mo = insn;
9821       ip->insn_opcode = insn->match;
9822       ip->use_extend = FALSE;
9823       imm_expr.X_op = O_absent;
9824       imm_reloc[0] = BFD_RELOC_UNUSED;
9825       imm_reloc[1] = BFD_RELOC_UNUSED;
9826       imm_reloc[2] = BFD_RELOC_UNUSED;
9827       offset_expr.X_op = O_absent;
9828       offset_reloc[0] = BFD_RELOC_UNUSED;
9829       offset_reloc[1] = BFD_RELOC_UNUSED;
9830       offset_reloc[2] = BFD_RELOC_UNUSED;
9831       for (args = insn->args; 1; ++args)
9832         {
9833           int c;
9834
9835           if (*s == ' ')
9836             ++s;
9837
9838           /* In this switch statement we call break if we did not find
9839              a match, continue if we did find a match, or return if we
9840              are done.  */
9841
9842           c = *args;
9843           switch (c)
9844             {
9845             case '\0':
9846               if (*s == '\0')
9847                 {
9848                   /* Stuff the immediate value in now, if we can.  */
9849                   if (imm_expr.X_op == O_constant
9850                       && *imm_reloc > BFD_RELOC_UNUSED
9851                       && insn->pinfo != INSN_MACRO)
9852                     {
9853                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9854                                     imm_expr.X_add_number, TRUE, mips16_small,
9855                                     mips16_ext, &ip->insn_opcode,
9856                                     &ip->use_extend, &ip->extend);
9857                       imm_expr.X_op = O_absent;
9858                       *imm_reloc = BFD_RELOC_UNUSED;
9859                     }
9860
9861                   return;
9862                 }
9863               break;
9864
9865             case ',':
9866               if (*s++ == c)
9867                 continue;
9868               s--;
9869               switch (*++args)
9870                 {
9871                 case 'v':
9872                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9873                   continue;
9874                 case 'w':
9875                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9876                   continue;
9877                 }
9878               break;
9879
9880             case '(':
9881             case ')':
9882               if (*s++ == c)
9883                 continue;
9884               break;
9885
9886             case 'v':
9887             case 'w':
9888               if (s[0] != '$')
9889                 {
9890                   if (c == 'v')
9891                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9892                   else
9893                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9894                   ++args;
9895                   continue;
9896                 }
9897               /* Fall through.  */
9898             case 'x':
9899             case 'y':
9900             case 'z':
9901             case 'Z':
9902             case '0':
9903             case 'S':
9904             case 'R':
9905             case 'X':
9906             case 'Y':
9907               if (s[0] != '$')
9908                 break;
9909               s_reset = s;
9910               if (ISDIGIT (s[1]))
9911                 {
9912                   ++s;
9913                   regno = 0;
9914                   do
9915                     {
9916                       regno *= 10;
9917                       regno += *s - '0';
9918                       ++s;
9919                     }
9920                   while (ISDIGIT (*s));
9921                   if (regno > 31)
9922                     {
9923                       as_bad (_("invalid register number (%d)"), regno);
9924                       regno = 2;
9925                     }
9926                 }
9927               else
9928                 {
9929                   if (s[1] == 'r' && s[2] == 'a')
9930                     {
9931                       s += 3;
9932                       regno = RA;
9933                     }
9934                   else if (s[1] == 'f' && s[2] == 'p')
9935                     {
9936                       s += 3;
9937                       regno = FP;
9938                     }
9939                   else if (s[1] == 's' && s[2] == 'p')
9940                     {
9941                       s += 3;
9942                       regno = SP;
9943                     }
9944                   else if (s[1] == 'g' && s[2] == 'p')
9945                     {
9946                       s += 3;
9947                       regno = GP;
9948                     }
9949                   else if (s[1] == 'a' && s[2] == 't')
9950                     {
9951                       s += 3;
9952                       regno = AT;
9953                     }
9954                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9955                     {
9956                       s += 4;
9957                       regno = KT0;
9958                     }
9959                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9960                     {
9961                       s += 4;
9962                       regno = KT1;
9963                     }
9964                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9965                     {
9966                       s += 5;
9967                       regno = ZERO;
9968                     }
9969                   else
9970                     break;
9971                 }
9972
9973               if (*s == ' ')
9974                 ++s;
9975               if (args[1] != *s)
9976                 {
9977                   if (c == 'v' || c == 'w')
9978                     {
9979                       regno = mips16_to_32_reg_map[lastregno];
9980                       s = s_reset;
9981                       ++args;
9982                     }
9983                 }
9984
9985               switch (c)
9986                 {
9987                 case 'x':
9988                 case 'y':
9989                 case 'z':
9990                 case 'v':
9991                 case 'w':
9992                 case 'Z':
9993                   regno = mips32_to_16_reg_map[regno];
9994                   break;
9995
9996                 case '0':
9997                   if (regno != 0)
9998                     regno = ILLEGAL_REG;
9999                   break;
10000
10001                 case 'S':
10002                   if (regno != SP)
10003                     regno = ILLEGAL_REG;
10004                   break;
10005
10006                 case 'R':
10007                   if (regno != RA)
10008                     regno = ILLEGAL_REG;
10009                   break;
10010
10011                 case 'X':
10012                 case 'Y':
10013                   if (regno == AT && ! mips_opts.noat)
10014                     as_warn (_("used $at without \".set noat\""));
10015                   break;
10016
10017                 default:
10018                   internalError ();
10019                 }
10020
10021               if (regno == ILLEGAL_REG)
10022                 break;
10023
10024               switch (c)
10025                 {
10026                 case 'x':
10027                 case 'v':
10028                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
10029                   break;
10030                 case 'y':
10031                 case 'w':
10032                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
10033                   break;
10034                 case 'z':
10035                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
10036                   break;
10037                 case 'Z':
10038                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
10039                 case '0':
10040                 case 'S':
10041                 case 'R':
10042                   break;
10043                 case 'X':
10044                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
10045                   break;
10046                 case 'Y':
10047                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
10048                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
10049                   break;
10050                 default:
10051                   internalError ();
10052                 }
10053
10054               lastregno = regno;
10055               continue;
10056
10057             case 'P':
10058               if (strncmp (s, "$pc", 3) == 0)
10059                 {
10060                   s += 3;
10061                   continue;
10062                 }
10063               break;
10064
10065             case '<':
10066             case '>':
10067             case '[':
10068             case ']':
10069             case '4':
10070             case '5':
10071             case 'H':
10072             case 'W':
10073             case 'D':
10074             case 'j':
10075             case '8':
10076             case 'V':
10077             case 'C':
10078             case 'U':
10079             case 'k':
10080             case 'K':
10081               if (s[0] == '%'
10082                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
10083                 {
10084                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
10085                      and generate the appropriate reloc.  If the text
10086                      inside %gprel is not a symbol name with an
10087                      optional offset, then we generate a normal reloc
10088                      and will probably fail later.  */
10089                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
10090                   if (imm_expr.X_op == O_symbol)
10091                     {
10092                       mips16_ext = TRUE;
10093                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
10094                       s = expr_end;
10095                       ip->use_extend = TRUE;
10096                       ip->extend = 0;
10097                       continue;
10098                     }
10099                 }
10100               else
10101                 {
10102                   /* Just pick up a normal expression.  */
10103                   my_getExpression (&imm_expr, s);
10104                 }
10105
10106               if (imm_expr.X_op == O_register)
10107                 {
10108                   /* What we thought was an expression turned out to
10109                      be a register.  */
10110
10111                   if (s[0] == '(' && args[1] == '(')
10112                     {
10113                       /* It looks like the expression was omitted
10114                          before a register indirection, which means
10115                          that the expression is implicitly zero.  We
10116                          still set up imm_expr, so that we handle
10117                          explicit extensions correctly.  */
10118                       imm_expr.X_op = O_constant;
10119                       imm_expr.X_add_number = 0;
10120                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10121                       continue;
10122                     }
10123
10124                   break;
10125                 }
10126
10127               /* We need to relax this instruction.  */
10128               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10129               s = expr_end;
10130               continue;
10131
10132             case 'p':
10133             case 'q':
10134             case 'A':
10135             case 'B':
10136             case 'E':
10137               /* We use offset_reloc rather than imm_reloc for the PC
10138                  relative operands.  This lets macros with both
10139                  immediate and address operands work correctly.  */
10140               my_getExpression (&offset_expr, s);
10141
10142               if (offset_expr.X_op == O_register)
10143                 break;
10144
10145               /* We need to relax this instruction.  */
10146               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10147               s = expr_end;
10148               continue;
10149
10150             case '6':           /* break code */
10151               my_getExpression (&imm_expr, s);
10152               check_absolute_expr (ip, &imm_expr);
10153               if ((unsigned long) imm_expr.X_add_number > 63)
10154                 {
10155                   as_warn (_("Invalid value for `%s' (%lu)"),
10156                            ip->insn_mo->name,
10157                            (unsigned long) imm_expr.X_add_number);
10158                   imm_expr.X_add_number &= 0x3f;
10159                 }
10160               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
10161               imm_expr.X_op = O_absent;
10162               s = expr_end;
10163               continue;
10164
10165             case 'a':           /* 26 bit address */
10166               my_getExpression (&offset_expr, s);
10167               s = expr_end;
10168               *offset_reloc = BFD_RELOC_MIPS16_JMP;
10169               ip->insn_opcode <<= 16;
10170               continue;
10171
10172             case 'l':           /* register list for entry macro */
10173             case 'L':           /* register list for exit macro */
10174               {
10175                 int mask;
10176
10177                 if (c == 'l')
10178                   mask = 0;
10179                 else
10180                   mask = 7 << 3;
10181                 while (*s != '\0')
10182                   {
10183                     int freg, reg1, reg2;
10184
10185                     while (*s == ' ' || *s == ',')
10186                       ++s;
10187                     if (*s != '$')
10188                       {
10189                         as_bad (_("can't parse register list"));
10190                         break;
10191                       }
10192                     ++s;
10193                     if (*s != 'f')
10194                       freg = 0;
10195                     else
10196                       {
10197                         freg = 1;
10198                         ++s;
10199                       }
10200                     reg1 = 0;
10201                     while (ISDIGIT (*s))
10202                       {
10203                         reg1 *= 10;
10204                         reg1 += *s - '0';
10205                         ++s;
10206                       }
10207                     if (*s == ' ')
10208                       ++s;
10209                     if (*s != '-')
10210                       reg2 = reg1;
10211                     else
10212                       {
10213                         ++s;
10214                         if (*s != '$')
10215                           break;
10216                         ++s;
10217                         if (freg)
10218                           {
10219                             if (*s == 'f')
10220                               ++s;
10221                             else
10222                               {
10223                                 as_bad (_("invalid register list"));
10224                                 break;
10225                               }
10226                           }
10227                         reg2 = 0;
10228                         while (ISDIGIT (*s))
10229                           {
10230                             reg2 *= 10;
10231                             reg2 += *s - '0';
10232                             ++s;
10233                           }
10234                       }
10235                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10236                       {
10237                         mask &= ~ (7 << 3);
10238                         mask |= 5 << 3;
10239                       }
10240                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10241                       {
10242                         mask &= ~ (7 << 3);
10243                         mask |= 6 << 3;
10244                       }
10245                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10246                       mask |= (reg2 - 3) << 3;
10247                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10248                       mask |= (reg2 - 15) << 1;
10249                     else if (reg1 == RA && reg2 == RA)
10250                       mask |= 1;
10251                     else
10252                       {
10253                         as_bad (_("invalid register list"));
10254                         break;
10255                       }
10256                   }
10257                 /* The mask is filled in in the opcode table for the
10258                    benefit of the disassembler.  We remove it before
10259                    applying the actual mask.  */
10260                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10261                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10262               }
10263             continue;
10264
10265             case 'e':           /* extend code */
10266               my_getExpression (&imm_expr, s);
10267               check_absolute_expr (ip, &imm_expr);
10268               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10269                 {
10270                   as_warn (_("Invalid value for `%s' (%lu)"),
10271                            ip->insn_mo->name,
10272                            (unsigned long) imm_expr.X_add_number);
10273                   imm_expr.X_add_number &= 0x7ff;
10274                 }
10275               ip->insn_opcode |= imm_expr.X_add_number;
10276               imm_expr.X_op = O_absent;
10277               s = expr_end;
10278               continue;
10279
10280             default:
10281               internalError ();
10282             }
10283           break;
10284         }
10285
10286       /* Args don't match.  */
10287       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10288           strcmp (insn->name, insn[1].name) == 0)
10289         {
10290           ++insn;
10291           s = argsstart;
10292           continue;
10293         }
10294
10295       insn_error = _("illegal operands");
10296
10297       return;
10298     }
10299 }
10300
10301 /* This structure holds information we know about a mips16 immediate
10302    argument type.  */
10303
10304 struct mips16_immed_operand
10305 {
10306   /* The type code used in the argument string in the opcode table.  */
10307   int type;
10308   /* The number of bits in the short form of the opcode.  */
10309   int nbits;
10310   /* The number of bits in the extended form of the opcode.  */
10311   int extbits;
10312   /* The amount by which the short form is shifted when it is used;
10313      for example, the sw instruction has a shift count of 2.  */
10314   int shift;
10315   /* The amount by which the short form is shifted when it is stored
10316      into the instruction code.  */
10317   int op_shift;
10318   /* Non-zero if the short form is unsigned.  */
10319   int unsp;
10320   /* Non-zero if the extended form is unsigned.  */
10321   int extu;
10322   /* Non-zero if the value is PC relative.  */
10323   int pcrel;
10324 };
10325
10326 /* The mips16 immediate operand types.  */
10327
10328 static const struct mips16_immed_operand mips16_immed_operands[] =
10329 {
10330   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10331   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10332   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10333   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10334   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10335   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10336   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10337   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10338   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10339   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10340   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10341   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10342   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10343   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10344   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10345   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10346   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10347   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10348   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10349   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10350   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10351 };
10352
10353 #define MIPS16_NUM_IMMED \
10354   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10355
10356 /* Handle a mips16 instruction with an immediate value.  This or's the
10357    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10358    whether an extended value is needed; if one is needed, it sets
10359    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10360    If SMALL is true, an unextended opcode was explicitly requested.
10361    If EXT is true, an extended opcode was explicitly requested.  If
10362    WARN is true, warn if EXT does not match reality.  */
10363
10364 static void
10365 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
10366               extend)
10367      char *file;
10368      unsigned int line;
10369      int type;
10370      offsetT val;
10371      bfd_boolean warn;
10372      bfd_boolean small;
10373      bfd_boolean ext;
10374      unsigned long *insn;
10375      bfd_boolean *use_extend;
10376      unsigned short *extend;
10377 {
10378   register const struct mips16_immed_operand *op;
10379   int mintiny, maxtiny;
10380   bfd_boolean needext;
10381
10382   op = mips16_immed_operands;
10383   while (op->type != type)
10384     {
10385       ++op;
10386       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10387     }
10388
10389   if (op->unsp)
10390     {
10391       if (type == '<' || type == '>' || type == '[' || type == ']')
10392         {
10393           mintiny = 1;
10394           maxtiny = 1 << op->nbits;
10395         }
10396       else
10397         {
10398           mintiny = 0;
10399           maxtiny = (1 << op->nbits) - 1;
10400         }
10401     }
10402   else
10403     {
10404       mintiny = - (1 << (op->nbits - 1));
10405       maxtiny = (1 << (op->nbits - 1)) - 1;
10406     }
10407
10408   /* Branch offsets have an implicit 0 in the lowest bit.  */
10409   if (type == 'p' || type == 'q')
10410     val /= 2;
10411
10412   if ((val & ((1 << op->shift) - 1)) != 0
10413       || val < (mintiny << op->shift)
10414       || val > (maxtiny << op->shift))
10415     needext = TRUE;
10416   else
10417     needext = FALSE;
10418
10419   if (warn && ext && ! needext)
10420     as_warn_where (file, line,
10421                    _("extended operand requested but not required"));
10422   if (small && needext)
10423     as_bad_where (file, line, _("invalid unextended operand value"));
10424
10425   if (small || (! ext && ! needext))
10426     {
10427       int insnval;
10428
10429       *use_extend = FALSE;
10430       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10431       insnval <<= op->op_shift;
10432       *insn |= insnval;
10433     }
10434   else
10435     {
10436       long minext, maxext;
10437       int extval;
10438
10439       if (op->extu)
10440         {
10441           minext = 0;
10442           maxext = (1 << op->extbits) - 1;
10443         }
10444       else
10445         {
10446           minext = - (1 << (op->extbits - 1));
10447           maxext = (1 << (op->extbits - 1)) - 1;
10448         }
10449       if (val < minext || val > maxext)
10450         as_bad_where (file, line,
10451                       _("operand value out of range for instruction"));
10452
10453       *use_extend = TRUE;
10454       if (op->extbits == 16)
10455         {
10456           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10457           val &= 0x1f;
10458         }
10459       else if (op->extbits == 15)
10460         {
10461           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10462           val &= 0xf;
10463         }
10464       else
10465         {
10466           extval = ((val & 0x1f) << 6) | (val & 0x20);
10467           val = 0;
10468         }
10469
10470       *extend = (unsigned short) extval;
10471       *insn |= val;
10472     }
10473 }
10474 \f
10475 static const struct percent_op_match
10476 {
10477   const char *str;
10478   bfd_reloc_code_real_type reloc;
10479 } percent_op[] =
10480 {
10481   {"%lo", BFD_RELOC_LO16},
10482 #ifdef OBJ_ELF
10483   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10484   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10485   {"%call16", BFD_RELOC_MIPS_CALL16},
10486   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10487   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10488   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10489   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10490   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10491   {"%got", BFD_RELOC_MIPS_GOT16},
10492   {"%gp_rel", BFD_RELOC_GPREL16},
10493   {"%half", BFD_RELOC_16},
10494   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10495   {"%higher", BFD_RELOC_MIPS_HIGHER},
10496   {"%neg", BFD_RELOC_MIPS_SUB},
10497 #endif
10498   {"%hi", BFD_RELOC_HI16_S}
10499 };
10500
10501
10502 /* Return true if *STR points to a relocation operator.  When returning true,
10503    move *STR over the operator and store its relocation code in *RELOC.
10504    Leave both *STR and *RELOC alone when returning false.  */
10505
10506 static bfd_boolean
10507 parse_relocation (str, reloc)
10508      char **str;
10509      bfd_reloc_code_real_type *reloc;
10510 {
10511   size_t i;
10512
10513   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10514     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10515       {
10516         *str += strlen (percent_op[i].str);
10517         *reloc = percent_op[i].reloc;
10518
10519         /* Check whether the output BFD supports this relocation.
10520            If not, issue an error and fall back on something safe.  */
10521         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10522           {
10523             as_bad ("relocation %s isn't supported by the current ABI",
10524                     percent_op[i].str);
10525             *reloc = BFD_RELOC_LO16;
10526           }
10527         return TRUE;
10528       }
10529   return FALSE;
10530 }
10531
10532
10533 /* Parse string STR as a 16-bit relocatable operand.  Store the
10534    expression in *EP and the relocations in the array starting
10535    at RELOC.  Return the number of relocation operators used.
10536
10537    On exit, EXPR_END points to the first character after the expression.
10538    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10539
10540 static size_t
10541 my_getSmallExpression (ep, reloc, str)
10542      expressionS *ep;
10543      bfd_reloc_code_real_type *reloc;
10544      char *str;
10545 {
10546   bfd_reloc_code_real_type reversed_reloc[3];
10547   size_t reloc_index, i;
10548   int crux_depth, str_depth;
10549   char *crux;
10550
10551   /* Search for the start of the main expression, recoding relocations
10552      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10553      of the main expression and with CRUX_DEPTH containing the number
10554      of open brackets at that point.  */
10555   reloc_index = -1;
10556   str_depth = 0;
10557   do
10558     {
10559       reloc_index++;
10560       crux = str;
10561       crux_depth = str_depth;
10562
10563       /* Skip over whitespace and brackets, keeping count of the number
10564          of brackets.  */
10565       while (*str == ' ' || *str == '\t' || *str == '(')
10566         if (*str++ == '(')
10567           str_depth++;
10568     }
10569   while (*str == '%'
10570          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10571          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10572
10573   my_getExpression (ep, crux);
10574   str = expr_end;
10575
10576   /* Match every open bracket.  */
10577   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10578     if (*str++ == ')')
10579       crux_depth--;
10580
10581   if (crux_depth > 0)
10582     as_bad ("unclosed '('");
10583
10584   expr_end = str;
10585
10586   if (reloc_index == 0)
10587     reloc[0] = BFD_RELOC_LO16;
10588   else
10589     {
10590       prev_reloc_op_frag = frag_now;
10591       for (i = 0; i < reloc_index; i++)
10592         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10593     }
10594
10595   return reloc_index;
10596 }
10597
10598 static void
10599 my_getExpression (ep, str)
10600      expressionS *ep;
10601      char *str;
10602 {
10603   char *save_in;
10604   valueT val;
10605
10606   save_in = input_line_pointer;
10607   input_line_pointer = str;
10608   expression (ep);
10609   expr_end = input_line_pointer;
10610   input_line_pointer = save_in;
10611
10612   /* If we are in mips16 mode, and this is an expression based on `.',
10613      then we bump the value of the symbol by 1 since that is how other
10614      text symbols are handled.  We don't bother to handle complex
10615      expressions, just `.' plus or minus a constant.  */
10616   if (mips_opts.mips16
10617       && ep->X_op == O_symbol
10618       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10619       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10620       && symbol_get_frag (ep->X_add_symbol) == frag_now
10621       && symbol_constant_p (ep->X_add_symbol)
10622       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10623     S_SET_VALUE (ep->X_add_symbol, val + 1);
10624 }
10625
10626 /* Turn a string in input_line_pointer into a floating point constant
10627    of type TYPE, and store the appropriate bytes in *LITP.  The number
10628    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10629    returned, or NULL on OK.  */
10630
10631 char *
10632 md_atof (type, litP, sizeP)
10633      int type;
10634      char *litP;
10635      int *sizeP;
10636 {
10637   int prec;
10638   LITTLENUM_TYPE words[4];
10639   char *t;
10640   int i;
10641
10642   switch (type)
10643     {
10644     case 'f':
10645       prec = 2;
10646       break;
10647
10648     case 'd':
10649       prec = 4;
10650       break;
10651
10652     default:
10653       *sizeP = 0;
10654       return _("bad call to md_atof");
10655     }
10656
10657   t = atof_ieee (input_line_pointer, type, words);
10658   if (t)
10659     input_line_pointer = t;
10660
10661   *sizeP = prec * 2;
10662
10663   if (! target_big_endian)
10664     {
10665       for (i = prec - 1; i >= 0; i--)
10666         {
10667           md_number_to_chars (litP, (valueT) words[i], 2);
10668           litP += 2;
10669         }
10670     }
10671   else
10672     {
10673       for (i = 0; i < prec; i++)
10674         {
10675           md_number_to_chars (litP, (valueT) words[i], 2);
10676           litP += 2;
10677         }
10678     }
10679
10680   return NULL;
10681 }
10682
10683 void
10684 md_number_to_chars (buf, val, n)
10685      char *buf;
10686      valueT val;
10687      int n;
10688 {
10689   if (target_big_endian)
10690     number_to_chars_bigendian (buf, val, n);
10691   else
10692     number_to_chars_littleendian (buf, val, n);
10693 }
10694 \f
10695 #ifdef OBJ_ELF
10696 static int support_64bit_objects(void)
10697 {
10698   const char **list, **l;
10699   int yes;
10700
10701   list = bfd_target_list ();
10702   for (l = list; *l != NULL; l++)
10703 #ifdef TE_TMIPS
10704     /* This is traditional mips */
10705     if (strcmp (*l, "elf64-tradbigmips") == 0
10706         || strcmp (*l, "elf64-tradlittlemips") == 0)
10707 #else
10708     if (strcmp (*l, "elf64-bigmips") == 0
10709         || strcmp (*l, "elf64-littlemips") == 0)
10710 #endif
10711       break;
10712   yes = (*l != NULL);
10713   free (list);
10714   return yes;
10715 }
10716 #endif /* OBJ_ELF */
10717
10718 const char *md_shortopts = "nO::g::G:";
10719
10720 struct option md_longopts[] =
10721 {
10722 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10723   {"mips0", no_argument, NULL, OPTION_MIPS1},
10724   {"mips1", no_argument, NULL, OPTION_MIPS1},
10725 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10726   {"mips2", no_argument, NULL, OPTION_MIPS2},
10727 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10728   {"mips3", no_argument, NULL, OPTION_MIPS3},
10729 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10730   {"mips4", no_argument, NULL, OPTION_MIPS4},
10731 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10732   {"mips5", no_argument, NULL, OPTION_MIPS5},
10733 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10734   {"mips32", no_argument, NULL, OPTION_MIPS32},
10735 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10736   {"mips64", no_argument, NULL, OPTION_MIPS64},
10737 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
10738   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10739 #define OPTION_TRAP (OPTION_MD_BASE + 9)
10740   {"trap", no_argument, NULL, OPTION_TRAP},
10741   {"no-break", no_argument, NULL, OPTION_TRAP},
10742 #define OPTION_BREAK (OPTION_MD_BASE + 10)
10743   {"break", no_argument, NULL, OPTION_BREAK},
10744   {"no-trap", no_argument, NULL, OPTION_BREAK},
10745 #define OPTION_EB (OPTION_MD_BASE + 11)
10746   {"EB", no_argument, NULL, OPTION_EB},
10747 #define OPTION_EL (OPTION_MD_BASE + 12)
10748   {"EL", no_argument, NULL, OPTION_EL},
10749 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
10750   {"mips16", no_argument, NULL, OPTION_MIPS16},
10751 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
10752   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10753 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
10754   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10755 #define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10756   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10757   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10758 #define OPTION_FP32 (OPTION_MD_BASE + 17)
10759   {"mfp32", no_argument, NULL, OPTION_FP32},
10760 #define OPTION_GP32 (OPTION_MD_BASE + 18)
10761   {"mgp32", no_argument, NULL, OPTION_GP32},
10762 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
10763   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10764 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
10765   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10766 #define OPTION_MARCH (OPTION_MD_BASE + 21)
10767   {"march", required_argument, NULL, OPTION_MARCH},
10768 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
10769   {"mtune", required_argument, NULL, OPTION_MTUNE},
10770 #define OPTION_FP64 (OPTION_MD_BASE + 23)
10771   {"mfp64", no_argument, NULL, OPTION_FP64},
10772 #define OPTION_M4650 (OPTION_MD_BASE + 24)
10773   {"m4650", no_argument, NULL, OPTION_M4650},
10774 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10775   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10776 #define OPTION_M4010 (OPTION_MD_BASE + 26)
10777   {"m4010", no_argument, NULL, OPTION_M4010},
10778 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10779   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10780 #define OPTION_M4100 (OPTION_MD_BASE + 28)
10781   {"m4100", no_argument, NULL, OPTION_M4100},
10782 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10783   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10784 #define OPTION_M3900 (OPTION_MD_BASE + 30)
10785   {"m3900", no_argument, NULL, OPTION_M3900},
10786 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10787   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10788 #define OPTION_GP64 (OPTION_MD_BASE + 32)
10789   {"mgp64", no_argument, NULL, OPTION_GP64},
10790 #define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10791   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10792 #define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10793   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10794 #define OPTION_MDMX (OPTION_MD_BASE + 35)
10795   {"mdmx", no_argument, NULL, OPTION_MDMX},
10796 #define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10797   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10798 #define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10799 #define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10800   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10801   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10802 #define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10803 #define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10804   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10805   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10806 #define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10807   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10808 #ifdef OBJ_ELF
10809 #define OPTION_ELF_BASE    (OPTION_MD_BASE + 42)
10810 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10811   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10812   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10813 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10814   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10815 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10816   {"xgot",        no_argument, NULL, OPTION_XGOT},
10817 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10818   {"mabi", required_argument, NULL, OPTION_MABI},
10819 #define OPTION_32          (OPTION_ELF_BASE + 4)
10820   {"32",          no_argument, NULL, OPTION_32},
10821 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10822   {"n32",         no_argument, NULL, OPTION_N32},
10823 #define OPTION_64          (OPTION_ELF_BASE + 6)
10824   {"64",          no_argument, NULL, OPTION_64},
10825 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10826   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10827 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10828   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10829 #endif /* OBJ_ELF */
10830   {NULL, no_argument, NULL, 0}
10831 };
10832 size_t md_longopts_size = sizeof (md_longopts);
10833
10834 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10835    NEW_VALUE.  Warn if another value was already specified.  Note:
10836    we have to defer parsing the -march and -mtune arguments in order
10837    to handle 'from-abi' correctly, since the ABI might be specified
10838    in a later argument.  */
10839
10840 static void
10841 mips_set_option_string (string_ptr, new_value)
10842      const char **string_ptr, *new_value;
10843 {
10844   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10845     as_warn (_("A different %s was already specified, is now %s"),
10846              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10847              new_value);
10848
10849   *string_ptr = new_value;
10850 }
10851
10852 int
10853 md_parse_option (c, arg)
10854      int c;
10855      char *arg;
10856 {
10857   switch (c)
10858     {
10859     case OPTION_CONSTRUCT_FLOATS:
10860       mips_disable_float_construction = 0;
10861       break;
10862
10863     case OPTION_NO_CONSTRUCT_FLOATS:
10864       mips_disable_float_construction = 1;
10865       break;
10866
10867     case OPTION_TRAP:
10868       mips_trap = 1;
10869       break;
10870
10871     case OPTION_BREAK:
10872       mips_trap = 0;
10873       break;
10874
10875     case OPTION_EB:
10876       target_big_endian = 1;
10877       break;
10878
10879     case OPTION_EL:
10880       target_big_endian = 0;
10881       break;
10882
10883     case 'n':
10884       warn_nops = 1;
10885       break;
10886
10887     case 'O':
10888       if (arg && arg[1] == '0')
10889         mips_optimize = 1;
10890       else
10891         mips_optimize = 2;
10892       break;
10893
10894     case 'g':
10895       if (arg == NULL)
10896         mips_debug = 2;
10897       else
10898         mips_debug = atoi (arg);
10899       /* When the MIPS assembler sees -g or -g2, it does not do
10900          optimizations which limit full symbolic debugging.  We take
10901          that to be equivalent to -O0.  */
10902       if (mips_debug == 2)
10903         mips_optimize = 1;
10904       break;
10905
10906     case OPTION_MIPS1:
10907       file_mips_isa = ISA_MIPS1;
10908       break;
10909
10910     case OPTION_MIPS2:
10911       file_mips_isa = ISA_MIPS2;
10912       break;
10913
10914     case OPTION_MIPS3:
10915       file_mips_isa = ISA_MIPS3;
10916       break;
10917
10918     case OPTION_MIPS4:
10919       file_mips_isa = ISA_MIPS4;
10920       break;
10921
10922     case OPTION_MIPS5:
10923       file_mips_isa = ISA_MIPS5;
10924       break;
10925
10926     case OPTION_MIPS32:
10927       file_mips_isa = ISA_MIPS32;
10928       break;
10929
10930     case OPTION_MIPS32R2:
10931       file_mips_isa = ISA_MIPS32R2;
10932       break;
10933
10934     case OPTION_MIPS64:
10935       file_mips_isa = ISA_MIPS64;
10936       break;
10937
10938     case OPTION_MTUNE:
10939       mips_set_option_string (&mips_tune_string, arg);
10940       break;
10941
10942     case OPTION_MARCH:
10943       mips_set_option_string (&mips_arch_string, arg);
10944       break;
10945
10946     case OPTION_M4650:
10947       mips_set_option_string (&mips_arch_string, "4650");
10948       mips_set_option_string (&mips_tune_string, "4650");
10949       break;
10950
10951     case OPTION_NO_M4650:
10952       break;
10953
10954     case OPTION_M4010:
10955       mips_set_option_string (&mips_arch_string, "4010");
10956       mips_set_option_string (&mips_tune_string, "4010");
10957       break;
10958
10959     case OPTION_NO_M4010:
10960       break;
10961
10962     case OPTION_M4100:
10963       mips_set_option_string (&mips_arch_string, "4100");
10964       mips_set_option_string (&mips_tune_string, "4100");
10965       break;
10966
10967     case OPTION_NO_M4100:
10968       break;
10969
10970     case OPTION_M3900:
10971       mips_set_option_string (&mips_arch_string, "3900");
10972       mips_set_option_string (&mips_tune_string, "3900");
10973       break;
10974
10975     case OPTION_NO_M3900:
10976       break;
10977
10978     case OPTION_MDMX:
10979       mips_opts.ase_mdmx = 1;
10980       break;
10981
10982     case OPTION_NO_MDMX:
10983       mips_opts.ase_mdmx = 0;
10984       break;
10985
10986     case OPTION_MIPS16:
10987       mips_opts.mips16 = 1;
10988       mips_no_prev_insn (FALSE);
10989       break;
10990
10991     case OPTION_NO_MIPS16:
10992       mips_opts.mips16 = 0;
10993       mips_no_prev_insn (FALSE);
10994       break;
10995
10996     case OPTION_MIPS3D:
10997       mips_opts.ase_mips3d = 1;
10998       break;
10999
11000     case OPTION_NO_MIPS3D:
11001       mips_opts.ase_mips3d = 0;
11002       break;
11003
11004     case OPTION_MEMBEDDED_PIC:
11005       mips_pic = EMBEDDED_PIC;
11006       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
11007         {
11008           as_bad (_("-G may not be used with embedded PIC code"));
11009           return 0;
11010         }
11011       g_switch_value = 0x7fffffff;
11012       break;
11013
11014     case OPTION_FIX_VR4122:
11015       mips_fix_4122_bugs = 1;
11016       break;
11017
11018     case OPTION_NO_FIX_VR4122:
11019       mips_fix_4122_bugs = 0;
11020       break;
11021
11022     case OPTION_RELAX_BRANCH:
11023       mips_relax_branch = 1;
11024       break;
11025
11026     case OPTION_NO_RELAX_BRANCH:
11027       mips_relax_branch = 0;
11028       break;
11029
11030 #ifdef OBJ_ELF
11031       /* When generating ELF code, we permit -KPIC and -call_shared to
11032          select SVR4_PIC, and -non_shared to select no PIC.  This is
11033          intended to be compatible with Irix 5.  */
11034     case OPTION_CALL_SHARED:
11035       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11036         {
11037           as_bad (_("-call_shared is supported only for ELF format"));
11038           return 0;
11039         }
11040       mips_pic = SVR4_PIC;
11041       if (g_switch_seen && g_switch_value != 0)
11042         {
11043           as_bad (_("-G may not be used with SVR4 PIC code"));
11044           return 0;
11045         }
11046       g_switch_value = 0;
11047       break;
11048
11049     case OPTION_NON_SHARED:
11050       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11051         {
11052           as_bad (_("-non_shared is supported only for ELF format"));
11053           return 0;
11054         }
11055       mips_pic = NO_PIC;
11056       break;
11057
11058       /* The -xgot option tells the assembler to use 32 offsets when
11059          accessing the got in SVR4_PIC mode.  It is for Irix
11060          compatibility.  */
11061     case OPTION_XGOT:
11062       mips_big_got = 1;
11063       break;
11064 #endif /* OBJ_ELF */
11065
11066     case 'G':
11067       if (! USE_GLOBAL_POINTER_OPT)
11068         {
11069           as_bad (_("-G is not supported for this configuration"));
11070           return 0;
11071         }
11072       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
11073         {
11074           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
11075           return 0;
11076         }
11077       else
11078         g_switch_value = atoi (arg);
11079       g_switch_seen = 1;
11080       break;
11081
11082 #ifdef OBJ_ELF
11083       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11084          and -mabi=64.  */
11085     case OPTION_32:
11086       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11087         {
11088           as_bad (_("-32 is supported for ELF format only"));
11089           return 0;
11090         }
11091       mips_abi = O32_ABI;
11092       break;
11093
11094     case OPTION_N32:
11095       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11096         {
11097           as_bad (_("-n32 is supported for ELF format only"));
11098           return 0;
11099         }
11100       mips_abi = N32_ABI;
11101       break;
11102
11103     case OPTION_64:
11104       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11105         {
11106           as_bad (_("-64 is supported for ELF format only"));
11107           return 0;
11108         }
11109       mips_abi = N64_ABI;
11110       if (! support_64bit_objects())
11111         as_fatal (_("No compiled in support for 64 bit object file format"));
11112       break;
11113 #endif /* OBJ_ELF */
11114
11115     case OPTION_GP32:
11116       file_mips_gp32 = 1;
11117       break;
11118
11119     case OPTION_GP64:
11120       file_mips_gp32 = 0;
11121       break;
11122
11123     case OPTION_FP32:
11124       file_mips_fp32 = 1;
11125       break;
11126
11127     case OPTION_FP64:
11128       file_mips_fp32 = 0;
11129       break;
11130
11131 #ifdef OBJ_ELF
11132     case OPTION_MABI:
11133       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11134         {
11135           as_bad (_("-mabi is supported for ELF format only"));
11136           return 0;
11137         }
11138       if (strcmp (arg, "32") == 0)
11139         mips_abi = O32_ABI;
11140       else if (strcmp (arg, "o64") == 0)
11141         mips_abi = O64_ABI;
11142       else if (strcmp (arg, "n32") == 0)
11143         mips_abi = N32_ABI;
11144       else if (strcmp (arg, "64") == 0)
11145         {
11146           mips_abi = N64_ABI;
11147           if (! support_64bit_objects())
11148             as_fatal (_("No compiled in support for 64 bit object file "
11149                         "format"));
11150         }
11151       else if (strcmp (arg, "eabi") == 0)
11152         mips_abi = EABI_ABI;
11153       else
11154         {
11155           as_fatal (_("invalid abi -mabi=%s"), arg);
11156           return 0;
11157         }
11158       break;
11159 #endif /* OBJ_ELF */
11160
11161     case OPTION_M7000_HILO_FIX:
11162       mips_7000_hilo_fix = TRUE;
11163       break;
11164
11165     case OPTION_MNO_7000_HILO_FIX:
11166       mips_7000_hilo_fix = FALSE;
11167       break;
11168
11169 #ifdef OBJ_ELF
11170     case OPTION_MDEBUG:
11171       mips_flag_mdebug = TRUE;
11172       break;
11173
11174     case OPTION_NO_MDEBUG:
11175       mips_flag_mdebug = FALSE;
11176       break;
11177 #endif /* OBJ_ELF */
11178
11179     default:
11180       return 0;
11181     }
11182
11183   return 1;
11184 }
11185 \f
11186 /* Set up globals to generate code for the ISA or processor
11187    described by INFO.  */
11188
11189 static void
11190 mips_set_architecture (info)
11191      const struct mips_cpu_info *info;
11192 {
11193   if (info != 0)
11194     {
11195       mips_arch_info = info;
11196       mips_arch = info->cpu;
11197       mips_opts.isa = info->isa;
11198     }
11199 }
11200
11201
11202 /* Likewise for tuning.  */
11203
11204 static void
11205 mips_set_tune (info)
11206      const struct mips_cpu_info *info;
11207 {
11208   if (info != 0)
11209     {
11210       mips_tune_info = info;
11211       mips_tune = info->cpu;
11212     }
11213 }
11214
11215
11216 void
11217 mips_after_parse_args ()
11218 {
11219   /* GP relative stuff not working for PE */
11220   if (strncmp (TARGET_OS, "pe", 2) == 0
11221       && g_switch_value != 0)
11222     {
11223       if (g_switch_seen)
11224         as_bad (_("-G not supported in this configuration."));
11225       g_switch_value = 0;
11226     }
11227
11228   if (mips_abi == NO_ABI)
11229     mips_abi = MIPS_DEFAULT_ABI;
11230
11231   /* The following code determines the architecture and register size.
11232      Similar code was added to GCC 3.3 (see override_options() in
11233      config/mips/mips.c).  The GAS and GCC code should be kept in sync
11234      as much as possible.  */
11235
11236   if (mips_arch_string != 0)
11237     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
11238
11239   if (mips_tune_string != 0)
11240     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
11241
11242   if (file_mips_isa != ISA_UNKNOWN)
11243     {
11244       /* Handle -mipsN.  At this point, file_mips_isa contains the
11245          ISA level specified by -mipsN, while mips_opts.isa contains
11246          the -march selection (if any).  */
11247       if (mips_arch_info != 0)
11248         {
11249           /* -march takes precedence over -mipsN, since it is more descriptive.
11250              There's no harm in specifying both as long as the ISA levels
11251              are the same.  */
11252           if (file_mips_isa != mips_opts.isa)
11253             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11254                     mips_cpu_info_from_isa (file_mips_isa)->name,
11255                     mips_cpu_info_from_isa (mips_opts.isa)->name);
11256         }
11257       else
11258         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
11259     }
11260
11261   if (mips_arch_info == 0)
11262     mips_set_architecture (mips_parse_cpu ("default CPU",
11263                                            MIPS_CPU_STRING_DEFAULT));
11264
11265   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11266     as_bad ("-march=%s is not compatible with the selected ABI",
11267             mips_arch_info->name);
11268
11269   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
11270   if (mips_tune_info == 0)
11271     mips_set_tune (mips_arch_info);
11272
11273   if (file_mips_gp32 >= 0)
11274     {
11275       /* The user specified the size of the integer registers.  Make sure
11276          it agrees with the ABI and ISA.  */
11277       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11278         as_bad (_("-mgp64 used with a 32-bit processor"));
11279       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11280         as_bad (_("-mgp32 used with a 64-bit ABI"));
11281       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11282         as_bad (_("-mgp64 used with a 32-bit ABI"));
11283     }
11284   else
11285     {
11286       /* Infer the integer register size from the ABI and processor.
11287          Restrict ourselves to 32-bit registers if that's all the
11288          processor has, or if the ABI cannot handle 64-bit registers.  */
11289       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11290                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11291     }
11292
11293   /* ??? GAS treats single-float processors as though they had 64-bit
11294      float registers (although it complains when double-precision
11295      instructions are used).  As things stand, saying they have 32-bit
11296      registers would lead to spurious "register must be even" messages.
11297      So here we assume float registers are always the same size as
11298      integer ones, unless the user says otherwise.  */
11299   if (file_mips_fp32 < 0)
11300     file_mips_fp32 = file_mips_gp32;
11301
11302   /* End of GCC-shared inference code.  */
11303
11304   /* ??? When do we want this flag to be set?   Who uses it?  */
11305   if (file_mips_gp32 == 1
11306       && mips_abi == NO_ABI
11307       && ISA_HAS_64BIT_REGS (mips_opts.isa))
11308     mips_32bitmode = 1;
11309
11310   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11311     as_bad (_("trap exception not supported at ISA 1"));
11312
11313   /* If the selected architecture includes support for ASEs, enable
11314      generation of code for them.  */
11315   if (mips_opts.mips16 == -1)
11316     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
11317   if (mips_opts.ase_mips3d == -1)
11318     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
11319   if (mips_opts.ase_mdmx == -1)
11320     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
11321
11322   file_mips_isa = mips_opts.isa;
11323   file_ase_mips16 = mips_opts.mips16;
11324   file_ase_mips3d = mips_opts.ase_mips3d;
11325   file_ase_mdmx = mips_opts.ase_mdmx;
11326   mips_opts.gp32 = file_mips_gp32;
11327   mips_opts.fp32 = file_mips_fp32;
11328
11329   if (mips_flag_mdebug < 0)
11330     {
11331 #ifdef OBJ_MAYBE_ECOFF
11332       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11333         mips_flag_mdebug = 1;
11334       else
11335 #endif /* OBJ_MAYBE_ECOFF */
11336         mips_flag_mdebug = 0;
11337     }
11338 }
11339 \f
11340 void
11341 mips_init_after_args ()
11342 {
11343   /* initialize opcodes */
11344   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11345   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11346 }
11347
11348 long
11349 md_pcrel_from (fixP)
11350      fixS *fixP;
11351 {
11352   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
11353       && fixP->fx_addsy != (symbolS *) NULL
11354       && ! S_IS_DEFINED (fixP->fx_addsy))
11355     return 4;
11356
11357   /* Return the address of the delay slot.  */
11358   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
11359 }
11360
11361 /* This is called before the symbol table is processed.  In order to
11362    work with gcc when using mips-tfile, we must keep all local labels.
11363    However, in other cases, we want to discard them.  If we were
11364    called with -g, but we didn't see any debugging information, it may
11365    mean that gcc is smuggling debugging information through to
11366    mips-tfile, in which case we must generate all local labels.  */
11367
11368 void
11369 mips_frob_file_before_adjust ()
11370 {
11371 #ifndef NO_ECOFF_DEBUGGING
11372   if (ECOFF_DEBUGGING
11373       && mips_debug != 0
11374       && ! ecoff_debugging_seen)
11375     flag_keep_locals = 1;
11376 #endif
11377 }
11378
11379 /* Sort any unmatched HI16_S relocs so that they immediately precede
11380    the corresponding LO reloc.  This is called before md_apply_fix3 and
11381    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11382    explicit use of the %hi modifier.  */
11383
11384 void
11385 mips_frob_file ()
11386 {
11387   struct mips_hi_fixup *l;
11388
11389   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11390     {
11391       segment_info_type *seginfo;
11392       int pass;
11393
11394       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11395
11396       /* If a GOT16 relocation turns out to be against a global symbol,
11397          there isn't supposed to be a matching LO.  */
11398       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11399           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11400         continue;
11401
11402       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11403       if (fixup_has_matching_lo_p (l->fixp))
11404         continue;
11405
11406       /* Look through the fixups for this segment for a matching %lo.
11407          When we find one, move the %hi just in front of it.  We do
11408          this in two passes.  In the first pass, we try to find a
11409          unique %lo.  In the second pass, we permit multiple %hi
11410          relocs for a single %lo (this is a GNU extension).  */
11411       seginfo = seg_info (l->seg);
11412       for (pass = 0; pass < 2; pass++)
11413         {
11414           fixS *f, *prev;
11415
11416           prev = NULL;
11417           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11418             {
11419               /* Check whether this is a %lo fixup which matches l->fixp.  */
11420               if (f->fx_r_type == BFD_RELOC_LO16
11421                   && f->fx_addsy == l->fixp->fx_addsy
11422                   && f->fx_offset == l->fixp->fx_offset
11423                   && (pass == 1
11424                       || prev == NULL
11425                       || !reloc_needs_lo_p (prev->fx_r_type)
11426                       || !fixup_has_matching_lo_p (prev)))
11427                 {
11428                   fixS **pf;
11429
11430                   /* Move l->fixp before f.  */
11431                   for (pf = &seginfo->fix_root;
11432                        *pf != l->fixp;
11433                        pf = &(*pf)->fx_next)
11434                     assert (*pf != NULL);
11435
11436                   *pf = l->fixp->fx_next;
11437
11438                   l->fixp->fx_next = f;
11439                   if (prev == NULL)
11440                     seginfo->fix_root = l->fixp;
11441                   else
11442                     prev->fx_next = l->fixp;
11443
11444                   break;
11445                 }
11446
11447               prev = f;
11448             }
11449
11450           if (f != NULL)
11451             break;
11452
11453 #if 0 /* GCC code motion plus incomplete dead code elimination
11454          can leave a %hi without a %lo.  */
11455           if (pass == 1)
11456             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11457                            _("Unmatched %%hi reloc"));
11458 #endif
11459         }
11460     }
11461 }
11462
11463 /* When generating embedded PIC code we need to use a special
11464    relocation to represent the difference of two symbols in the .text
11465    section (switch tables use a difference of this sort).  See
11466    include/coff/mips.h for details.  This macro checks whether this
11467    fixup requires the special reloc.  */
11468 #define SWITCH_TABLE(fixp) \
11469   ((fixp)->fx_r_type == BFD_RELOC_32 \
11470    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11471    && (fixp)->fx_addsy != NULL \
11472    && (fixp)->fx_subsy != NULL \
11473    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11474    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11475
11476 /* When generating embedded PIC code we must keep all PC relative
11477    relocations, in case the linker has to relax a call.  We also need
11478    to keep relocations for switch table entries.
11479
11480    We may have combined relocations without symbols in the N32/N64 ABI.
11481    We have to prevent gas from dropping them.  */
11482
11483 int
11484 mips_force_relocation (fixp)
11485      fixS *fixp;
11486 {
11487   if (generic_force_reloc (fixp))
11488     return 1;
11489
11490   if (HAVE_NEWABI
11491       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11492       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11493           || fixp->fx_r_type == BFD_RELOC_HI16_S
11494           || fixp->fx_r_type == BFD_RELOC_LO16))
11495     return 1;
11496
11497   return (mips_pic == EMBEDDED_PIC
11498           && (fixp->fx_pcrel
11499               || SWITCH_TABLE (fixp)
11500               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11501               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11502 }
11503
11504 /* This hook is called before a fix is simplified.  We don't really
11505    decide whether to skip a fix here.  Rather, we turn global symbols
11506    used as branch targets into local symbols, such that they undergo
11507    simplification.  We can only do this if the symbol is defined and
11508    it is in the same section as the branch.  If this doesn't hold, we
11509    emit a better error message than just saying the relocation is not
11510    valid for the selected object format.
11511
11512    FIXP is the fix-up we're going to try to simplify, SEG is the
11513    segment in which the fix up occurs.  The return value should be
11514    non-zero to indicate the fix-up is valid for further
11515    simplifications.  */
11516
11517 int
11518 mips_validate_fix (fixP, seg)
11519      struct fix *fixP;
11520      asection *seg;
11521 {
11522   /* There's a lot of discussion on whether it should be possible to
11523      use R_MIPS_PC16 to represent branch relocations.  The outcome
11524      seems to be that it can, but gas/bfd are very broken in creating
11525      RELA relocations for this, so for now we only accept branches to
11526      symbols in the same section.  Anything else is of dubious value,
11527      since there's no guarantee that at link time the symbol would be
11528      in range.  Even for branches to local symbols this is arguably
11529      wrong, since it we assume the symbol is not going to be
11530      overridden, which should be possible per ELF library semantics,
11531      but then, there isn't a dynamic relocation that could be used to
11532      this effect, and the target would likely be out of range as well.
11533
11534      Unfortunately, it seems that there is too much code out there
11535      that relies on branches to symbols that are global to be resolved
11536      as if they were local, like the IRIX tools do, so we do it as
11537      well, but with a warning so that people are reminded to fix their
11538      code.  If we ever get back to using R_MIPS_PC16 for branch
11539      targets, this entire block should go away (and probably the
11540      whole function).  */
11541
11542   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11543       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11544             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11545            && mips_pic != EMBEDDED_PIC)
11546           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11547       && fixP->fx_addsy)
11548     {
11549       if (! S_IS_DEFINED (fixP->fx_addsy))
11550         {
11551           as_bad_where (fixP->fx_file, fixP->fx_line,
11552                         _("Cannot branch to undefined symbol."));
11553           /* Avoid any further errors about this fixup.  */
11554           fixP->fx_done = 1;
11555         }
11556       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11557         {
11558           as_bad_where (fixP->fx_file, fixP->fx_line,
11559                         _("Cannot branch to symbol in another section."));
11560           fixP->fx_done = 1;
11561         }
11562       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11563         {
11564           symbolS *sym = fixP->fx_addsy;
11565
11566           as_warn_where (fixP->fx_file, fixP->fx_line,
11567                          _("Pretending global symbol used as branch target is local."));
11568
11569           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11570                                           S_GET_SEGMENT (sym),
11571                                           S_GET_VALUE (sym),
11572                                           symbol_get_frag (sym));
11573           copy_symbol_attributes (fixP->fx_addsy, sym);
11574           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11575           assert (symbol_resolved_p (sym));
11576           symbol_mark_resolved (fixP->fx_addsy);
11577         }
11578     }
11579
11580   return 1;
11581 }
11582
11583 #ifdef OBJ_ELF
11584 static int
11585 mips_need_elf_addend_fixup (fixP)
11586      fixS *fixP;
11587 {
11588   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11589     return 1;
11590   if (mips_pic == EMBEDDED_PIC
11591       && S_IS_WEAK (fixP->fx_addsy))
11592     return 1;
11593   if (mips_pic != EMBEDDED_PIC
11594       && (S_IS_WEAK (fixP->fx_addsy)
11595           || S_IS_EXTERNAL (fixP->fx_addsy))
11596       && !S_IS_COMMON (fixP->fx_addsy))
11597     return 1;
11598   if (((bfd_get_section_flags (stdoutput,
11599                                S_GET_SEGMENT (fixP->fx_addsy))
11600         & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11601       || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11602                    ".gnu.linkonce",
11603                    sizeof (".gnu.linkonce") - 1))
11604     return 1;
11605   return 0;
11606 }
11607 #endif
11608
11609 /* Apply a fixup to the object file.  */
11610
11611 void
11612 md_apply_fix3 (fixP, valP, seg)
11613      fixS *fixP;
11614      valueT *valP;
11615      segT seg ATTRIBUTE_UNUSED;
11616 {
11617   bfd_byte *buf;
11618   long insn;
11619   valueT value;
11620   static int previous_fx_r_type = 0;
11621
11622   /* FIXME: Maybe just return for all reloc types not listed below?
11623      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11624   if (fixP->fx_r_type == BFD_RELOC_8)
11625       return;
11626
11627   assert (fixP->fx_size == 4
11628           || fixP->fx_r_type == BFD_RELOC_16
11629           || fixP->fx_r_type == BFD_RELOC_32
11630           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11631           || fixP->fx_r_type == BFD_RELOC_HI16_S
11632           || fixP->fx_r_type == BFD_RELOC_LO16
11633           || fixP->fx_r_type == BFD_RELOC_GPREL16
11634           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11635           || fixP->fx_r_type == BFD_RELOC_GPREL32
11636           || fixP->fx_r_type == BFD_RELOC_64
11637           || fixP->fx_r_type == BFD_RELOC_CTOR
11638           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11639           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11640           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11641           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11642           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11643           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11644           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11645           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11646           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11647
11648   value = *valP;
11649
11650   /* If we aren't adjusting this fixup to be against the section
11651      symbol, we need to adjust the value.  */
11652 #ifdef OBJ_ELF
11653   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11654     {
11655       if (mips_need_elf_addend_fixup (fixP))
11656         {
11657           reloc_howto_type *howto;
11658           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11659
11660           value -= symval;
11661
11662           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11663           if (value != 0 && howto && howto->partial_inplace)
11664             {
11665               /* In this case, the bfd_install_relocation routine will
11666                  incorrectly add the symbol value back in.  We just want
11667                  the addend to appear in the object file.
11668
11669                  The condition above used to include
11670                  "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11671
11672                  However, howto can't be trusted here, because we
11673                  might change the reloc type in tc_gen_reloc.  We can
11674                  check howto->partial_inplace because that conversion
11675                  happens to preserve howto->partial_inplace; but it
11676                  does not preserve howto->pcrel_offset.  I've just
11677                  eliminated the check, because all MIPS PC-relative
11678                  relocations are marked howto->pcrel_offset.
11679
11680                  howto->pcrel_offset was originally added for
11681                  R_MIPS_PC16, which is generated for code like
11682
11683                         globl g1 .text
11684                         .text
11685                         .space 20
11686                  g1:
11687                  x:
11688                         bal g1
11689                */
11690               value -= symval;
11691
11692               /* Make sure the addend is still non-zero.  If it became zero
11693                  after the last operation, set it to a spurious value and
11694                  subtract the same value from the object file's contents.  */
11695               if (value == 0)
11696                 {
11697                   value = 8;
11698
11699                   /* The in-place addends for LO16 relocations are signed;
11700                      leave the matching HI16 in-place addends as zero.  */
11701                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11702                     {
11703                       bfd_vma contents, mask, field;
11704
11705                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11706                                                + fixP->fx_where,
11707                                                fixP->fx_size * 8,
11708                                                target_big_endian);
11709
11710                       /* MASK has bits set where the relocation should go.
11711                          FIELD is -value, shifted into the appropriate place
11712                          for this relocation.  */
11713                       mask = 1 << (howto->bitsize - 1);
11714                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11715                       field = (-value >> howto->rightshift) << howto->bitpos;
11716
11717                       bfd_put_bits ((field & mask) | (contents & ~mask),
11718                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11719                                     fixP->fx_size * 8,
11720                                     target_big_endian);
11721                     }
11722                 }
11723             }
11724         }
11725
11726       /* This code was generated using trial and error and so is
11727          fragile and not trustworthy.  If you change it, you should
11728          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11729          they still pass.  */
11730       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11731         {
11732           value += fixP->fx_frag->fr_address + fixP->fx_where;
11733
11734           /* BFD's REL handling, for MIPS, is _very_ weird.
11735              This gives the right results, but it can't possibly
11736              be the way things are supposed to work.  */
11737           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11738               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11739             value += fixP->fx_frag->fr_address + fixP->fx_where;
11740         }
11741     }
11742 #endif
11743
11744   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11745
11746   /* We are not done if this is a composite relocation to set up gp.  */
11747   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11748       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11749            || (fixP->fx_r_type == BFD_RELOC_64
11750                && (previous_fx_r_type == BFD_RELOC_GPREL32
11751                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11752            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11753                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11754                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11755     fixP->fx_done = 1;
11756   previous_fx_r_type = fixP->fx_r_type;
11757
11758   switch (fixP->fx_r_type)
11759     {
11760     case BFD_RELOC_MIPS_JMP:
11761     case BFD_RELOC_MIPS_SHIFT5:
11762     case BFD_RELOC_MIPS_SHIFT6:
11763     case BFD_RELOC_MIPS_GOT_DISP:
11764     case BFD_RELOC_MIPS_GOT_PAGE:
11765     case BFD_RELOC_MIPS_GOT_OFST:
11766     case BFD_RELOC_MIPS_SUB:
11767     case BFD_RELOC_MIPS_INSERT_A:
11768     case BFD_RELOC_MIPS_INSERT_B:
11769     case BFD_RELOC_MIPS_DELETE:
11770     case BFD_RELOC_MIPS_HIGHEST:
11771     case BFD_RELOC_MIPS_HIGHER:
11772     case BFD_RELOC_MIPS_SCN_DISP:
11773     case BFD_RELOC_MIPS_REL16:
11774     case BFD_RELOC_MIPS_RELGOT:
11775     case BFD_RELOC_MIPS_JALR:
11776     case BFD_RELOC_HI16:
11777     case BFD_RELOC_HI16_S:
11778     case BFD_RELOC_GPREL16:
11779     case BFD_RELOC_MIPS_LITERAL:
11780     case BFD_RELOC_MIPS_CALL16:
11781     case BFD_RELOC_MIPS_GOT16:
11782     case BFD_RELOC_GPREL32:
11783     case BFD_RELOC_MIPS_GOT_HI16:
11784     case BFD_RELOC_MIPS_GOT_LO16:
11785     case BFD_RELOC_MIPS_CALL_HI16:
11786     case BFD_RELOC_MIPS_CALL_LO16:
11787     case BFD_RELOC_MIPS16_GPREL:
11788       if (fixP->fx_pcrel)
11789         as_bad_where (fixP->fx_file, fixP->fx_line,
11790                       _("Invalid PC relative reloc"));
11791       /* Nothing needed to do. The value comes from the reloc entry */
11792       break;
11793
11794     case BFD_RELOC_MIPS16_JMP:
11795       /* We currently always generate a reloc against a symbol, which
11796          means that we don't want an addend even if the symbol is
11797          defined.  */
11798       fixP->fx_addnumber = 0;
11799       break;
11800
11801     case BFD_RELOC_PCREL_HI16_S:
11802       /* The addend for this is tricky if it is internal, so we just
11803          do everything here rather than in bfd_install_relocation.  */
11804       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11805           && !fixP->fx_done
11806           && value != 0)
11807         break;
11808       if (fixP->fx_addsy
11809           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11810         {
11811           /* For an external symbol adjust by the address to make it
11812              pcrel_offset.  We use the address of the RELLO reloc
11813              which follows this one.  */
11814           value += (fixP->fx_next->fx_frag->fr_address
11815                     + fixP->fx_next->fx_where);
11816         }
11817       value = ((value + 0x8000) >> 16) & 0xffff;
11818       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11819       if (target_big_endian)
11820         buf += 2;
11821       md_number_to_chars ((char *) buf, value, 2);
11822       break;
11823
11824     case BFD_RELOC_PCREL_LO16:
11825       /* The addend for this is tricky if it is internal, so we just
11826          do everything here rather than in bfd_install_relocation.  */
11827       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11828           && !fixP->fx_done
11829           && value != 0)
11830         break;
11831       if (fixP->fx_addsy
11832           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11833         value += fixP->fx_frag->fr_address + fixP->fx_where;
11834       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11835       if (target_big_endian)
11836         buf += 2;
11837       md_number_to_chars ((char *) buf, value, 2);
11838       break;
11839
11840     case BFD_RELOC_64:
11841       /* This is handled like BFD_RELOC_32, but we output a sign
11842          extended value if we are only 32 bits.  */
11843       if (fixP->fx_done
11844           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11845         {
11846           if (8 <= sizeof (valueT))
11847             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11848                                 value, 8);
11849           else
11850             {
11851               long w1, w2;
11852               long hiv;
11853
11854               w1 = w2 = fixP->fx_where;
11855               if (target_big_endian)
11856                 w1 += 4;
11857               else
11858                 w2 += 4;
11859               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11860               if ((value & 0x80000000) != 0)
11861                 hiv = 0xffffffff;
11862               else
11863                 hiv = 0;
11864               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11865             }
11866         }
11867       break;
11868
11869     case BFD_RELOC_RVA:
11870     case BFD_RELOC_32:
11871       /* If we are deleting this reloc entry, we must fill in the
11872          value now.  This can happen if we have a .word which is not
11873          resolved when it appears but is later defined.  We also need
11874          to fill in the value if this is an embedded PIC switch table
11875          entry.  */
11876       if (fixP->fx_done
11877           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11878         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11879                             value, 4);
11880       break;
11881
11882     case BFD_RELOC_16:
11883       /* If we are deleting this reloc entry, we must fill in the
11884          value now.  */
11885       assert (fixP->fx_size == 2);
11886       if (fixP->fx_done)
11887         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11888                             value, 2);
11889       break;
11890
11891     case BFD_RELOC_LO16:
11892       /* When handling an embedded PIC switch statement, we can wind
11893          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11894       if (fixP->fx_done)
11895         {
11896           if (value + 0x8000 > 0xffff)
11897             as_bad_where (fixP->fx_file, fixP->fx_line,
11898                           _("relocation overflow"));
11899           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11900           if (target_big_endian)
11901             buf += 2;
11902           md_number_to_chars ((char *) buf, value, 2);
11903         }
11904       break;
11905
11906     case BFD_RELOC_16_PCREL_S2:
11907       if ((value & 0x3) != 0)
11908         as_bad_where (fixP->fx_file, fixP->fx_line,
11909                       _("Branch to odd address (%lx)"), (long) value);
11910
11911       /*
11912        * We need to save the bits in the instruction since fixup_segment()
11913        * might be deleting the relocation entry (i.e., a branch within
11914        * the current segment).
11915        */
11916       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11917         break;
11918       /* If 'value' is zero, the remaining reloc code won't actually
11919          do the store, so it must be done here.  This is probably
11920          a bug somewhere.  */
11921       if (!fixP->fx_done
11922           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11923               || fixP->fx_addsy == NULL                 /* ??? */
11924               || ! S_IS_DEFINED (fixP->fx_addsy)))
11925         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11926
11927       value = (offsetT) value >> 2;
11928
11929       /* update old instruction data */
11930       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11931       if (target_big_endian)
11932         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11933       else
11934         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11935
11936       if (value + 0x8000 <= 0xffff)
11937         insn |= value & 0xffff;
11938       else
11939         {
11940           /* The branch offset is too large.  If this is an
11941              unconditional branch, and we are not generating PIC code,
11942              we can convert it to an absolute jump instruction.  */
11943           if (mips_pic == NO_PIC
11944               && fixP->fx_done
11945               && fixP->fx_frag->fr_address >= text_section->vma
11946               && (fixP->fx_frag->fr_address
11947                   < text_section->vma + text_section->_raw_size)
11948               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11949                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11950                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11951             {
11952               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11953                 insn = 0x0c000000;      /* jal */
11954               else
11955                 insn = 0x08000000;      /* j */
11956               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11957               fixP->fx_done = 0;
11958               fixP->fx_addsy = section_symbol (text_section);
11959               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11960             }
11961           else
11962             {
11963               /* If we got here, we have branch-relaxation disabled,
11964                  and there's nothing we can do to fix this instruction
11965                  without turning it into a longer sequence.  */
11966               as_bad_where (fixP->fx_file, fixP->fx_line,
11967                             _("Branch out of range"));
11968             }
11969         }
11970
11971       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11972       break;
11973
11974     case BFD_RELOC_VTABLE_INHERIT:
11975       fixP->fx_done = 0;
11976       if (fixP->fx_addsy
11977           && !S_IS_DEFINED (fixP->fx_addsy)
11978           && !S_IS_WEAK (fixP->fx_addsy))
11979         S_SET_WEAK (fixP->fx_addsy);
11980       break;
11981
11982     case BFD_RELOC_VTABLE_ENTRY:
11983       fixP->fx_done = 0;
11984       break;
11985
11986     default:
11987       internalError ();
11988     }
11989 }
11990
11991 #if 0
11992 void
11993 printInsn (oc)
11994      unsigned long oc;
11995 {
11996   const struct mips_opcode *p;
11997   int treg, sreg, dreg, shamt;
11998   short imm;
11999   const char *args;
12000   int i;
12001
12002   for (i = 0; i < NUMOPCODES; ++i)
12003     {
12004       p = &mips_opcodes[i];
12005       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
12006         {
12007           printf ("%08lx %s\t", oc, p->name);
12008           treg = (oc >> 16) & 0x1f;
12009           sreg = (oc >> 21) & 0x1f;
12010           dreg = (oc >> 11) & 0x1f;
12011           shamt = (oc >> 6) & 0x1f;
12012           imm = oc;
12013           for (args = p->args;; ++args)
12014             {
12015               switch (*args)
12016                 {
12017                 case '\0':
12018                   printf ("\n");
12019                   break;
12020
12021                 case ',':
12022                 case '(':
12023                 case ')':
12024                   printf ("%c", *args);
12025                   continue;
12026
12027                 case 'r':
12028                   assert (treg == sreg);
12029                   printf ("$%d,$%d", treg, sreg);
12030                   continue;
12031
12032                 case 'd':
12033                 case 'G':
12034                   printf ("$%d", dreg);
12035                   continue;
12036
12037                 case 't':
12038                 case 'E':
12039                   printf ("$%d", treg);
12040                   continue;
12041
12042                 case 'k':
12043                   printf ("0x%x", treg);
12044                   continue;
12045
12046                 case 'b':
12047                 case 's':
12048                   printf ("$%d", sreg);
12049                   continue;
12050
12051                 case 'a':
12052                   printf ("0x%08lx", oc & 0x1ffffff);
12053                   continue;
12054
12055                 case 'i':
12056                 case 'j':
12057                 case 'o':
12058                 case 'u':
12059                   printf ("%d", imm);
12060                   continue;
12061
12062                 case '<':
12063                 case '>':
12064                   printf ("$%d", shamt);
12065                   continue;
12066
12067                 default:
12068                   internalError ();
12069                 }
12070               break;
12071             }
12072           return;
12073         }
12074     }
12075   printf (_("%08lx  UNDEFINED\n"), oc);
12076 }
12077 #endif
12078
12079 static symbolS *
12080 get_symbol ()
12081 {
12082   int c;
12083   char *name;
12084   symbolS *p;
12085
12086   name = input_line_pointer;
12087   c = get_symbol_end ();
12088   p = (symbolS *) symbol_find_or_make (name);
12089   *input_line_pointer = c;
12090   return p;
12091 }
12092
12093 /* Align the current frag to a given power of two.  The MIPS assembler
12094    also automatically adjusts any preceding label.  */
12095
12096 static void
12097 mips_align (to, fill, label)
12098      int to;
12099      int fill;
12100      symbolS *label;
12101 {
12102   mips_emit_delays (FALSE);
12103   frag_align (to, fill, 0);
12104   record_alignment (now_seg, to);
12105   if (label != NULL)
12106     {
12107       assert (S_GET_SEGMENT (label) == now_seg);
12108       symbol_set_frag (label, frag_now);
12109       S_SET_VALUE (label, (valueT) frag_now_fix ());
12110     }
12111 }
12112
12113 /* Align to a given power of two.  .align 0 turns off the automatic
12114    alignment used by the data creating pseudo-ops.  */
12115
12116 static void
12117 s_align (x)
12118      int x ATTRIBUTE_UNUSED;
12119 {
12120   register int temp;
12121   register long temp_fill;
12122   long max_alignment = 15;
12123
12124   /*
12125
12126     o  Note that the assembler pulls down any immediately preceeding label
12127        to the aligned address.
12128     o  It's not documented but auto alignment is reinstated by
12129        a .align pseudo instruction.
12130     o  Note also that after auto alignment is turned off the mips assembler
12131        issues an error on attempt to assemble an improperly aligned data item.
12132        We don't.
12133
12134     */
12135
12136   temp = get_absolute_expression ();
12137   if (temp > max_alignment)
12138     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12139   else if (temp < 0)
12140     {
12141       as_warn (_("Alignment negative: 0 assumed."));
12142       temp = 0;
12143     }
12144   if (*input_line_pointer == ',')
12145     {
12146       ++input_line_pointer;
12147       temp_fill = get_absolute_expression ();
12148     }
12149   else
12150     temp_fill = 0;
12151   if (temp)
12152     {
12153       auto_align = 1;
12154       mips_align (temp, (int) temp_fill,
12155                   insn_labels != NULL ? insn_labels->label : NULL);
12156     }
12157   else
12158     {
12159       auto_align = 0;
12160     }
12161
12162   demand_empty_rest_of_line ();
12163 }
12164
12165 void
12166 mips_flush_pending_output ()
12167 {
12168   mips_emit_delays (FALSE);
12169   mips_clear_insn_labels ();
12170 }
12171
12172 static void
12173 s_change_sec (sec)
12174      int sec;
12175 {
12176   segT seg;
12177
12178   /* When generating embedded PIC code, we only use the .text, .lit8,
12179      .sdata and .sbss sections.  We change the .data and .rdata
12180      pseudo-ops to use .sdata.  */
12181   if (mips_pic == EMBEDDED_PIC
12182       && (sec == 'd' || sec == 'r'))
12183     sec = 's';
12184
12185 #ifdef OBJ_ELF
12186   /* The ELF backend needs to know that we are changing sections, so
12187      that .previous works correctly.  We could do something like check
12188      for an obj_section_change_hook macro, but that might be confusing
12189      as it would not be appropriate to use it in the section changing
12190      functions in read.c, since obj-elf.c intercepts those.  FIXME:
12191      This should be cleaner, somehow.  */
12192   obj_elf_section_change_hook ();
12193 #endif
12194
12195   mips_emit_delays (FALSE);
12196   switch (sec)
12197     {
12198     case 't':
12199       s_text (0);
12200       break;
12201     case 'd':
12202       s_data (0);
12203       break;
12204     case 'b':
12205       subseg_set (bss_section, (subsegT) get_absolute_expression ());
12206       demand_empty_rest_of_line ();
12207       break;
12208
12209     case 'r':
12210       if (USE_GLOBAL_POINTER_OPT)
12211         {
12212           seg = subseg_new (RDATA_SECTION_NAME,
12213                             (subsegT) get_absolute_expression ());
12214           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12215             {
12216               bfd_set_section_flags (stdoutput, seg,
12217                                      (SEC_ALLOC
12218                                       | SEC_LOAD
12219                                       | SEC_READONLY
12220                                       | SEC_RELOC
12221                                       | SEC_DATA));
12222               if (strcmp (TARGET_OS, "elf") != 0)
12223                 record_alignment (seg, 4);
12224             }
12225           demand_empty_rest_of_line ();
12226         }
12227       else
12228         {
12229           as_bad (_("No read only data section in this object file format"));
12230           demand_empty_rest_of_line ();
12231           return;
12232         }
12233       break;
12234
12235     case 's':
12236       if (USE_GLOBAL_POINTER_OPT)
12237         {
12238           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12239           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12240             {
12241               bfd_set_section_flags (stdoutput, seg,
12242                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
12243                                      | SEC_DATA);
12244               if (strcmp (TARGET_OS, "elf") != 0)
12245                 record_alignment (seg, 4);
12246             }
12247           demand_empty_rest_of_line ();
12248           break;
12249         }
12250       else
12251         {
12252           as_bad (_("Global pointers not supported; recompile -G 0"));
12253           demand_empty_rest_of_line ();
12254           return;
12255         }
12256     }
12257
12258   auto_align = 1;
12259 }
12260
12261 void
12262 s_change_section (ignore)
12263      int ignore ATTRIBUTE_UNUSED;
12264 {
12265 #ifdef OBJ_ELF
12266   char *section_name;
12267   char c;
12268   char next_c = 0;
12269   int section_type;
12270   int section_flag;
12271   int section_entry_size;
12272   int section_alignment;
12273
12274   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12275     return;
12276
12277   section_name = input_line_pointer;
12278   c = get_symbol_end ();
12279   if (c)
12280     next_c = *(input_line_pointer + 1);
12281
12282   /* Do we have .section Name<,"flags">?  */
12283   if (c != ',' || (c == ',' && next_c == '"'))
12284     {
12285       /* just after name is now '\0'.  */
12286       *input_line_pointer = c;
12287       input_line_pointer = section_name;
12288       obj_elf_section (ignore);
12289       return;
12290     }
12291   input_line_pointer++;
12292
12293   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
12294   if (c == ',')
12295     section_type = get_absolute_expression ();
12296   else
12297     section_type = 0;
12298   if (*input_line_pointer++ == ',')
12299     section_flag = get_absolute_expression ();
12300   else
12301     section_flag = 0;
12302   if (*input_line_pointer++ == ',')
12303     section_entry_size = get_absolute_expression ();
12304   else
12305     section_entry_size = 0;
12306   if (*input_line_pointer++ == ',')
12307     section_alignment = get_absolute_expression ();
12308   else
12309     section_alignment = 0;
12310
12311   section_name = xstrdup (section_name);
12312
12313   obj_elf_change_section (section_name, section_type, section_flag,
12314                           section_entry_size, 0, 0, 0);
12315
12316   if (now_seg->name != section_name)
12317     free (section_name);
12318 #endif /* OBJ_ELF */
12319 }
12320
12321 void
12322 mips_enable_auto_align ()
12323 {
12324   auto_align = 1;
12325 }
12326
12327 static void
12328 s_cons (log_size)
12329      int log_size;
12330 {
12331   symbolS *label;
12332
12333   label = insn_labels != NULL ? insn_labels->label : NULL;
12334   mips_emit_delays (FALSE);
12335   if (log_size > 0 && auto_align)
12336     mips_align (log_size, 0, label);
12337   mips_clear_insn_labels ();
12338   cons (1 << log_size);
12339 }
12340
12341 static void
12342 s_float_cons (type)
12343      int type;
12344 {
12345   symbolS *label;
12346
12347   label = insn_labels != NULL ? insn_labels->label : NULL;
12348
12349   mips_emit_delays (FALSE);
12350
12351   if (auto_align)
12352     {
12353       if (type == 'd')
12354         mips_align (3, 0, label);
12355       else
12356         mips_align (2, 0, label);
12357     }
12358
12359   mips_clear_insn_labels ();
12360
12361   float_cons (type);
12362 }
12363
12364 /* Handle .globl.  We need to override it because on Irix 5 you are
12365    permitted to say
12366        .globl foo .text
12367    where foo is an undefined symbol, to mean that foo should be
12368    considered to be the address of a function.  */
12369
12370 static void
12371 s_mips_globl (x)
12372      int x ATTRIBUTE_UNUSED;
12373 {
12374   char *name;
12375   int c;
12376   symbolS *symbolP;
12377   flagword flag;
12378
12379   name = input_line_pointer;
12380   c = get_symbol_end ();
12381   symbolP = symbol_find_or_make (name);
12382   *input_line_pointer = c;
12383   SKIP_WHITESPACE ();
12384
12385   /* On Irix 5, every global symbol that is not explicitly labelled as
12386      being a function is apparently labelled as being an object.  */
12387   flag = BSF_OBJECT;
12388
12389   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12390     {
12391       char *secname;
12392       asection *sec;
12393
12394       secname = input_line_pointer;
12395       c = get_symbol_end ();
12396       sec = bfd_get_section_by_name (stdoutput, secname);
12397       if (sec == NULL)
12398         as_bad (_("%s: no such section"), secname);
12399       *input_line_pointer = c;
12400
12401       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12402         flag = BSF_FUNCTION;
12403     }
12404
12405   symbol_get_bfdsym (symbolP)->flags |= flag;
12406
12407   S_SET_EXTERNAL (symbolP);
12408   demand_empty_rest_of_line ();
12409 }
12410
12411 static void
12412 s_option (x)
12413      int x ATTRIBUTE_UNUSED;
12414 {
12415   char *opt;
12416   char c;
12417
12418   opt = input_line_pointer;
12419   c = get_symbol_end ();
12420
12421   if (*opt == 'O')
12422     {
12423       /* FIXME: What does this mean?  */
12424     }
12425   else if (strncmp (opt, "pic", 3) == 0)
12426     {
12427       int i;
12428
12429       i = atoi (opt + 3);
12430       if (i == 0)
12431         mips_pic = NO_PIC;
12432       else if (i == 2)
12433         mips_pic = SVR4_PIC;
12434       else
12435         as_bad (_(".option pic%d not supported"), i);
12436
12437       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12438         {
12439           if (g_switch_seen && g_switch_value != 0)
12440             as_warn (_("-G may not be used with SVR4 PIC code"));
12441           g_switch_value = 0;
12442           bfd_set_gp_size (stdoutput, 0);
12443         }
12444     }
12445   else
12446     as_warn (_("Unrecognized option \"%s\""), opt);
12447
12448   *input_line_pointer = c;
12449   demand_empty_rest_of_line ();
12450 }
12451
12452 /* This structure is used to hold a stack of .set values.  */
12453
12454 struct mips_option_stack
12455 {
12456   struct mips_option_stack *next;
12457   struct mips_set_options options;
12458 };
12459
12460 static struct mips_option_stack *mips_opts_stack;
12461
12462 /* Handle the .set pseudo-op.  */
12463
12464 static void
12465 s_mipsset (x)
12466      int x ATTRIBUTE_UNUSED;
12467 {
12468   char *name = input_line_pointer, ch;
12469
12470   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12471     ++input_line_pointer;
12472   ch = *input_line_pointer;
12473   *input_line_pointer = '\0';
12474
12475   if (strcmp (name, "reorder") == 0)
12476     {
12477       if (mips_opts.noreorder && prev_nop_frag != NULL)
12478         {
12479           /* If we still have pending nops, we can discard them.  The
12480              usual nop handling will insert any that are still
12481              needed.  */
12482           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12483                                     * (mips_opts.mips16 ? 2 : 4));
12484           prev_nop_frag = NULL;
12485         }
12486       mips_opts.noreorder = 0;
12487     }
12488   else if (strcmp (name, "noreorder") == 0)
12489     {
12490       mips_emit_delays (TRUE);
12491       mips_opts.noreorder = 1;
12492       mips_any_noreorder = 1;
12493     }
12494   else if (strcmp (name, "at") == 0)
12495     {
12496       mips_opts.noat = 0;
12497     }
12498   else if (strcmp (name, "noat") == 0)
12499     {
12500       mips_opts.noat = 1;
12501     }
12502   else if (strcmp (name, "macro") == 0)
12503     {
12504       mips_opts.warn_about_macros = 0;
12505     }
12506   else if (strcmp (name, "nomacro") == 0)
12507     {
12508       if (mips_opts.noreorder == 0)
12509         as_bad (_("`noreorder' must be set before `nomacro'"));
12510       mips_opts.warn_about_macros = 1;
12511     }
12512   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12513     {
12514       mips_opts.nomove = 0;
12515     }
12516   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12517     {
12518       mips_opts.nomove = 1;
12519     }
12520   else if (strcmp (name, "bopt") == 0)
12521     {
12522       mips_opts.nobopt = 0;
12523     }
12524   else if (strcmp (name, "nobopt") == 0)
12525     {
12526       mips_opts.nobopt = 1;
12527     }
12528   else if (strcmp (name, "mips16") == 0
12529            || strcmp (name, "MIPS-16") == 0)
12530     mips_opts.mips16 = 1;
12531   else if (strcmp (name, "nomips16") == 0
12532            || strcmp (name, "noMIPS-16") == 0)
12533     mips_opts.mips16 = 0;
12534   else if (strcmp (name, "mips3d") == 0)
12535     mips_opts.ase_mips3d = 1;
12536   else if (strcmp (name, "nomips3d") == 0)
12537     mips_opts.ase_mips3d = 0;
12538   else if (strcmp (name, "mdmx") == 0)
12539     mips_opts.ase_mdmx = 1;
12540   else if (strcmp (name, "nomdmx") == 0)
12541     mips_opts.ase_mdmx = 0;
12542   else if (strncmp (name, "mips", 4) == 0)
12543     {
12544       int reset = 0;
12545
12546       /* Permit the user to change the ISA on the fly.  Needless to
12547          say, misuse can cause serious problems.  */
12548       if (strcmp (name, "mips0") == 0)
12549         {
12550           reset = 1;
12551           mips_opts.isa = file_mips_isa;
12552         }
12553       else if (strcmp (name, "mips1") == 0)
12554         mips_opts.isa = ISA_MIPS1;
12555       else if (strcmp (name, "mips2") == 0)
12556         mips_opts.isa = ISA_MIPS2;
12557       else if (strcmp (name, "mips3") == 0)
12558         mips_opts.isa = ISA_MIPS3;
12559       else if (strcmp (name, "mips4") == 0)
12560         mips_opts.isa = ISA_MIPS4;
12561       else if (strcmp (name, "mips5") == 0)
12562         mips_opts.isa = ISA_MIPS5;
12563       else if (strcmp (name, "mips32") == 0)
12564         mips_opts.isa = ISA_MIPS32;
12565       else if (strcmp (name, "mips32r2") == 0)
12566         mips_opts.isa = ISA_MIPS32R2;
12567       else if (strcmp (name, "mips64") == 0)
12568         mips_opts.isa = ISA_MIPS64;
12569       else
12570         as_bad (_("unknown ISA level %s"), name + 4);
12571
12572       switch (mips_opts.isa)
12573         {
12574         case  0:
12575           break;
12576         case ISA_MIPS1:
12577         case ISA_MIPS2:
12578         case ISA_MIPS32:
12579         case ISA_MIPS32R2:
12580           mips_opts.gp32 = 1;
12581           mips_opts.fp32 = 1;
12582           break;
12583         case ISA_MIPS3:
12584         case ISA_MIPS4:
12585         case ISA_MIPS5:
12586         case ISA_MIPS64:
12587           mips_opts.gp32 = 0;
12588           mips_opts.fp32 = 0;
12589           break;
12590         default:
12591           as_bad (_("unknown ISA level %s"), name + 4);
12592           break;
12593         }
12594       if (reset)
12595         {
12596           mips_opts.gp32 = file_mips_gp32;
12597           mips_opts.fp32 = file_mips_fp32;
12598         }
12599     }
12600   else if (strcmp (name, "autoextend") == 0)
12601     mips_opts.noautoextend = 0;
12602   else if (strcmp (name, "noautoextend") == 0)
12603     mips_opts.noautoextend = 1;
12604   else if (strcmp (name, "push") == 0)
12605     {
12606       struct mips_option_stack *s;
12607
12608       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12609       s->next = mips_opts_stack;
12610       s->options = mips_opts;
12611       mips_opts_stack = s;
12612     }
12613   else if (strcmp (name, "pop") == 0)
12614     {
12615       struct mips_option_stack *s;
12616
12617       s = mips_opts_stack;
12618       if (s == NULL)
12619         as_bad (_(".set pop with no .set push"));
12620       else
12621         {
12622           /* If we're changing the reorder mode we need to handle
12623              delay slots correctly.  */
12624           if (s->options.noreorder && ! mips_opts.noreorder)
12625             mips_emit_delays (TRUE);
12626           else if (! s->options.noreorder && mips_opts.noreorder)
12627             {
12628               if (prev_nop_frag != NULL)
12629                 {
12630                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12631                                             * (mips_opts.mips16 ? 2 : 4));
12632                   prev_nop_frag = NULL;
12633                 }
12634             }
12635
12636           mips_opts = s->options;
12637           mips_opts_stack = s->next;
12638           free (s);
12639         }
12640     }
12641   else
12642     {
12643       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12644     }
12645   *input_line_pointer = ch;
12646   demand_empty_rest_of_line ();
12647 }
12648
12649 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12650    .option pic2.  It means to generate SVR4 PIC calls.  */
12651
12652 static void
12653 s_abicalls (ignore)
12654      int ignore ATTRIBUTE_UNUSED;
12655 {
12656   mips_pic = SVR4_PIC;
12657   if (USE_GLOBAL_POINTER_OPT)
12658     {
12659       if (g_switch_seen && g_switch_value != 0)
12660         as_warn (_("-G may not be used with SVR4 PIC code"));
12661       g_switch_value = 0;
12662     }
12663   bfd_set_gp_size (stdoutput, 0);
12664   demand_empty_rest_of_line ();
12665 }
12666
12667 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12668    PIC code.  It sets the $gp register for the function based on the
12669    function address, which is in the register named in the argument.
12670    This uses a relocation against _gp_disp, which is handled specially
12671    by the linker.  The result is:
12672         lui     $gp,%hi(_gp_disp)
12673         addiu   $gp,$gp,%lo(_gp_disp)
12674         addu    $gp,$gp,.cpload argument
12675    The .cpload argument is normally $25 == $t9.  */
12676
12677 static void
12678 s_cpload (ignore)
12679      int ignore ATTRIBUTE_UNUSED;
12680 {
12681   expressionS ex;
12682   int icnt = 0;
12683
12684   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12685      .cpload is ignored.  */
12686   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12687     {
12688       s_ignore (0);
12689       return;
12690     }
12691
12692   /* .cpload should be in a .set noreorder section.  */
12693   if (mips_opts.noreorder == 0)
12694     as_warn (_(".cpload not in noreorder section"));
12695
12696   ex.X_op = O_symbol;
12697   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12698   ex.X_op_symbol = NULL;
12699   ex.X_add_number = 0;
12700
12701   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12702   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12703
12704   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12705   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12706                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12707
12708   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12709                mips_gp_register, mips_gp_register, tc_get_register (0));
12710
12711   demand_empty_rest_of_line ();
12712 }
12713
12714 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12715      .cpsetup $reg1, offset|$reg2, label
12716
12717    If offset is given, this results in:
12718      sd         $gp, offset($sp)
12719      lui        $gp, %hi(%neg(%gp_rel(label)))
12720      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12721      daddu      $gp, $gp, $reg1
12722
12723    If $reg2 is given, this results in:
12724      daddu      $reg2, $gp, $0
12725      lui        $gp, %hi(%neg(%gp_rel(label)))
12726      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12727      daddu      $gp, $gp, $reg1
12728    $reg1 is normally $25 == $t9.  */
12729 static void
12730 s_cpsetup (ignore)
12731      int ignore ATTRIBUTE_UNUSED;
12732 {
12733   expressionS ex_off;
12734   expressionS ex_sym;
12735   int reg1;
12736   int icnt = 0;
12737   char *f;
12738
12739   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12740      We also need NewABI support.  */
12741   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12742     {
12743       s_ignore (0);
12744       return;
12745     }
12746
12747   reg1 = tc_get_register (0);
12748   SKIP_WHITESPACE ();
12749   if (*input_line_pointer != ',')
12750     {
12751       as_bad (_("missing argument separator ',' for .cpsetup"));
12752       return;
12753     }
12754   else
12755     ++input_line_pointer;
12756   SKIP_WHITESPACE ();
12757   if (*input_line_pointer == '$')
12758     {
12759       mips_cpreturn_register = tc_get_register (0);
12760       mips_cpreturn_offset = -1;
12761     }
12762   else
12763     {
12764       mips_cpreturn_offset = get_absolute_expression ();
12765       mips_cpreturn_register = -1;
12766     }
12767   SKIP_WHITESPACE ();
12768   if (*input_line_pointer != ',')
12769     {
12770       as_bad (_("missing argument separator ',' for .cpsetup"));
12771       return;
12772     }
12773   else
12774     ++input_line_pointer;
12775   SKIP_WHITESPACE ();
12776   expression (&ex_sym);
12777
12778   if (mips_cpreturn_register == -1)
12779     {
12780       ex_off.X_op = O_constant;
12781       ex_off.X_add_symbol = NULL;
12782       ex_off.X_op_symbol = NULL;
12783       ex_off.X_add_number = mips_cpreturn_offset;
12784
12785       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12786                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12787     }
12788   else
12789     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12790                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12791
12792   /* Ensure there's room for the next two instructions, so that `f'
12793      doesn't end up with an address in the wrong frag.  */
12794   frag_grow (8);
12795   f = frag_more (0);
12796   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12797                (int) BFD_RELOC_GPREL16);
12798   fix_new (frag_now, f - frag_now->fr_literal,
12799            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12800   fix_new (frag_now, f - frag_now->fr_literal,
12801            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12802
12803   f = frag_more (0);
12804   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12805                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12806   fix_new (frag_now, f - frag_now->fr_literal,
12807            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12808   fix_new (frag_now, f - frag_now->fr_literal,
12809            0, NULL, 0, 0, BFD_RELOC_LO16);
12810
12811   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12812                HAVE_64BIT_ADDRESSES ? "daddu" : "add", "d,v,t",
12813                mips_gp_register, mips_gp_register, reg1);
12814
12815   demand_empty_rest_of_line ();
12816 }
12817
12818 static void
12819 s_cplocal (ignore)
12820      int ignore ATTRIBUTE_UNUSED;
12821 {
12822   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12823    .cplocal is ignored.  */
12824   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12825     {
12826       s_ignore (0);
12827       return;
12828     }
12829
12830   mips_gp_register = tc_get_register (0);
12831   demand_empty_rest_of_line ();
12832 }
12833
12834 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12835    offset from $sp.  The offset is remembered, and after making a PIC
12836    call $gp is restored from that location.  */
12837
12838 static void
12839 s_cprestore (ignore)
12840      int ignore ATTRIBUTE_UNUSED;
12841 {
12842   expressionS ex;
12843   int icnt = 0;
12844
12845   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12846      .cprestore is ignored.  */
12847   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12848     {
12849       s_ignore (0);
12850       return;
12851     }
12852
12853   mips_cprestore_offset = get_absolute_expression ();
12854   mips_cprestore_valid = 1;
12855
12856   ex.X_op = O_constant;
12857   ex.X_add_symbol = NULL;
12858   ex.X_op_symbol = NULL;
12859   ex.X_add_number = mips_cprestore_offset;
12860
12861   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12862                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12863                                 mips_gp_register, SP);
12864
12865   demand_empty_rest_of_line ();
12866 }
12867
12868 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12869    was given in the preceeding .gpsetup, it results in:
12870      ld         $gp, offset($sp)
12871
12872    If a register $reg2 was given there, it results in:
12873      daddiu     $gp, $gp, $reg2
12874  */
12875 static void
12876 s_cpreturn (ignore)
12877      int ignore ATTRIBUTE_UNUSED;
12878 {
12879   expressionS ex;
12880   int icnt = 0;
12881
12882   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12883      We also need NewABI support.  */
12884   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12885     {
12886       s_ignore (0);
12887       return;
12888     }
12889
12890   if (mips_cpreturn_register == -1)
12891     {
12892       ex.X_op = O_constant;
12893       ex.X_add_symbol = NULL;
12894       ex.X_op_symbol = NULL;
12895       ex.X_add_number = mips_cpreturn_offset;
12896
12897       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12898                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12899     }
12900   else
12901     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12902                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12903
12904   demand_empty_rest_of_line ();
12905 }
12906
12907 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12908    code.  It sets the offset to use in gp_rel relocations.  */
12909
12910 static void
12911 s_gpvalue (ignore)
12912      int ignore ATTRIBUTE_UNUSED;
12913 {
12914   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12915      We also need NewABI support.  */
12916   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12917     {
12918       s_ignore (0);
12919       return;
12920     }
12921
12922   mips_gprel_offset = get_absolute_expression ();
12923
12924   demand_empty_rest_of_line ();
12925 }
12926
12927 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12928    code.  It generates a 32 bit GP relative reloc.  */
12929
12930 static void
12931 s_gpword (ignore)
12932      int ignore ATTRIBUTE_UNUSED;
12933 {
12934   symbolS *label;
12935   expressionS ex;
12936   char *p;
12937
12938   /* When not generating PIC code, this is treated as .word.  */
12939   if (mips_pic != SVR4_PIC)
12940     {
12941       s_cons (2);
12942       return;
12943     }
12944
12945   label = insn_labels != NULL ? insn_labels->label : NULL;
12946   mips_emit_delays (TRUE);
12947   if (auto_align)
12948     mips_align (2, 0, label);
12949   mips_clear_insn_labels ();
12950
12951   expression (&ex);
12952
12953   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12954     {
12955       as_bad (_("Unsupported use of .gpword"));
12956       ignore_rest_of_line ();
12957     }
12958
12959   p = frag_more (4);
12960   md_number_to_chars (p, (valueT) 0, 4);
12961   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12962                BFD_RELOC_GPREL32);
12963
12964   demand_empty_rest_of_line ();
12965 }
12966
12967 static void
12968 s_gpdword (ignore)
12969      int ignore ATTRIBUTE_UNUSED;
12970 {
12971   symbolS *label;
12972   expressionS ex;
12973   char *p;
12974
12975   /* When not generating PIC code, this is treated as .dword.  */
12976   if (mips_pic != SVR4_PIC)
12977     {
12978       s_cons (3);
12979       return;
12980     }
12981
12982   label = insn_labels != NULL ? insn_labels->label : NULL;
12983   mips_emit_delays (TRUE);
12984   if (auto_align)
12985     mips_align (3, 0, label);
12986   mips_clear_insn_labels ();
12987
12988   expression (&ex);
12989
12990   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12991     {
12992       as_bad (_("Unsupported use of .gpdword"));
12993       ignore_rest_of_line ();
12994     }
12995
12996   p = frag_more (8);
12997   md_number_to_chars (p, (valueT) 0, 8);
12998   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12999                BFD_RELOC_GPREL32);
13000
13001   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
13002   ex.X_op = O_absent;
13003   ex.X_add_symbol = 0;
13004   ex.X_add_number = 0;
13005   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
13006                BFD_RELOC_64);
13007
13008   demand_empty_rest_of_line ();
13009 }
13010
13011 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
13012    tables in SVR4 PIC code.  */
13013
13014 static void
13015 s_cpadd (ignore)
13016      int ignore ATTRIBUTE_UNUSED;
13017 {
13018   int icnt = 0;
13019   int reg;
13020
13021   /* This is ignored when not generating SVR4 PIC code.  */
13022   if (mips_pic != SVR4_PIC)
13023     {
13024       s_ignore (0);
13025       return;
13026     }
13027
13028   /* Add $gp to the register named as an argument.  */
13029   reg = tc_get_register (0);
13030   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
13031                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI ? "add" : "addu" : "daddu",
13032                "d,v,t", reg, reg, mips_gp_register);
13033
13034   demand_empty_rest_of_line ();
13035 }
13036
13037 /* Handle the .insn pseudo-op.  This marks instruction labels in
13038    mips16 mode.  This permits the linker to handle them specially,
13039    such as generating jalx instructions when needed.  We also make
13040    them odd for the duration of the assembly, in order to generate the
13041    right sort of code.  We will make them even in the adjust_symtab
13042    routine, while leaving them marked.  This is convenient for the
13043    debugger and the disassembler.  The linker knows to make them odd
13044    again.  */
13045
13046 static void
13047 s_insn (ignore)
13048      int ignore ATTRIBUTE_UNUSED;
13049 {
13050   mips16_mark_labels ();
13051
13052   demand_empty_rest_of_line ();
13053 }
13054
13055 /* Handle a .stabn directive.  We need these in order to mark a label
13056    as being a mips16 text label correctly.  Sometimes the compiler
13057    will emit a label, followed by a .stabn, and then switch sections.
13058    If the label and .stabn are in mips16 mode, then the label is
13059    really a mips16 text label.  */
13060
13061 static void
13062 s_mips_stab (type)
13063      int type;
13064 {
13065   if (type == 'n')
13066     mips16_mark_labels ();
13067
13068   s_stab (type);
13069 }
13070
13071 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
13072  */
13073
13074 static void
13075 s_mips_weakext (ignore)
13076      int ignore ATTRIBUTE_UNUSED;
13077 {
13078   char *name;
13079   int c;
13080   symbolS *symbolP;
13081   expressionS exp;
13082
13083   name = input_line_pointer;
13084   c = get_symbol_end ();
13085   symbolP = symbol_find_or_make (name);
13086   S_SET_WEAK (symbolP);
13087   *input_line_pointer = c;
13088
13089   SKIP_WHITESPACE ();
13090
13091   if (! is_end_of_line[(unsigned char) *input_line_pointer])
13092     {
13093       if (S_IS_DEFINED (symbolP))
13094         {
13095           as_bad ("ignoring attempt to redefine symbol %s",
13096                   S_GET_NAME (symbolP));
13097           ignore_rest_of_line ();
13098           return;
13099         }
13100
13101       if (*input_line_pointer == ',')
13102         {
13103           ++input_line_pointer;
13104           SKIP_WHITESPACE ();
13105         }
13106
13107       expression (&exp);
13108       if (exp.X_op != O_symbol)
13109         {
13110           as_bad ("bad .weakext directive");
13111           ignore_rest_of_line ();
13112           return;
13113         }
13114       symbol_set_value_expression (symbolP, &exp);
13115     }
13116
13117   demand_empty_rest_of_line ();
13118 }
13119
13120 /* Parse a register string into a number.  Called from the ECOFF code
13121    to parse .frame.  The argument is non-zero if this is the frame
13122    register, so that we can record it in mips_frame_reg.  */
13123
13124 int
13125 tc_get_register (frame)
13126      int frame;
13127 {
13128   int reg;
13129
13130   SKIP_WHITESPACE ();
13131   if (*input_line_pointer++ != '$')
13132     {
13133       as_warn (_("expected `$'"));
13134       reg = ZERO;
13135     }
13136   else if (ISDIGIT (*input_line_pointer))
13137     {
13138       reg = get_absolute_expression ();
13139       if (reg < 0 || reg >= 32)
13140         {
13141           as_warn (_("Bad register number"));
13142           reg = ZERO;
13143         }
13144     }
13145   else
13146     {
13147       if (strncmp (input_line_pointer, "ra", 2) == 0)
13148         {
13149           reg = RA;
13150           input_line_pointer += 2;
13151         }
13152       else if (strncmp (input_line_pointer, "fp", 2) == 0)
13153         {
13154           reg = FP;
13155           input_line_pointer += 2;
13156         }
13157       else if (strncmp (input_line_pointer, "sp", 2) == 0)
13158         {
13159           reg = SP;
13160           input_line_pointer += 2;
13161         }
13162       else if (strncmp (input_line_pointer, "gp", 2) == 0)
13163         {
13164           reg = GP;
13165           input_line_pointer += 2;
13166         }
13167       else if (strncmp (input_line_pointer, "at", 2) == 0)
13168         {
13169           reg = AT;
13170           input_line_pointer += 2;
13171         }
13172       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13173         {
13174           reg = KT0;
13175           input_line_pointer += 3;
13176         }
13177       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13178         {
13179           reg = KT1;
13180           input_line_pointer += 3;
13181         }
13182       else if (strncmp (input_line_pointer, "zero", 4) == 0)
13183         {
13184           reg = ZERO;
13185           input_line_pointer += 4;
13186         }
13187       else
13188         {
13189           as_warn (_("Unrecognized register name"));
13190           reg = ZERO;
13191           while (ISALNUM(*input_line_pointer))
13192            input_line_pointer++;
13193         }
13194     }
13195   if (frame)
13196     {
13197       mips_frame_reg = reg != 0 ? reg : SP;
13198       mips_frame_reg_valid = 1;
13199       mips_cprestore_valid = 0;
13200     }
13201   return reg;
13202 }
13203
13204 valueT
13205 md_section_align (seg, addr)
13206      asection *seg;
13207      valueT addr;
13208 {
13209   int align = bfd_get_section_alignment (stdoutput, seg);
13210
13211 #ifdef OBJ_ELF
13212   /* We don't need to align ELF sections to the full alignment.
13213      However, Irix 5 may prefer that we align them at least to a 16
13214      byte boundary.  We don't bother to align the sections if we are
13215      targeted for an embedded system.  */
13216   if (strcmp (TARGET_OS, "elf") == 0)
13217     return addr;
13218   if (align > 4)
13219     align = 4;
13220 #endif
13221
13222   return ((addr + (1 << align) - 1) & (-1 << align));
13223 }
13224
13225 /* Utility routine, called from above as well.  If called while the
13226    input file is still being read, it's only an approximation.  (For
13227    example, a symbol may later become defined which appeared to be
13228    undefined earlier.)  */
13229
13230 static int
13231 nopic_need_relax (sym, before_relaxing)
13232      symbolS *sym;
13233      int before_relaxing;
13234 {
13235   if (sym == 0)
13236     return 0;
13237
13238   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
13239     {
13240       const char *symname;
13241       int change;
13242
13243       /* Find out whether this symbol can be referenced off the $gp
13244          register.  It can be if it is smaller than the -G size or if
13245          it is in the .sdata or .sbss section.  Certain symbols can
13246          not be referenced off the $gp, although it appears as though
13247          they can.  */
13248       symname = S_GET_NAME (sym);
13249       if (symname != (const char *) NULL
13250           && (strcmp (symname, "eprol") == 0
13251               || strcmp (symname, "etext") == 0
13252               || strcmp (symname, "_gp") == 0
13253               || strcmp (symname, "edata") == 0
13254               || strcmp (symname, "_fbss") == 0
13255               || strcmp (symname, "_fdata") == 0
13256               || strcmp (symname, "_ftext") == 0
13257               || strcmp (symname, "end") == 0
13258               || strcmp (symname, "_gp_disp") == 0))
13259         change = 1;
13260       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13261                && (0
13262 #ifndef NO_ECOFF_DEBUGGING
13263                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
13264                        && (symbol_get_obj (sym)->ecoff_extern_size
13265                            <= g_switch_value))
13266 #endif
13267                    /* We must defer this decision until after the whole
13268                       file has been read, since there might be a .extern
13269                       after the first use of this symbol.  */
13270                    || (before_relaxing
13271 #ifndef NO_ECOFF_DEBUGGING
13272                        && symbol_get_obj (sym)->ecoff_extern_size == 0
13273 #endif
13274                        && S_GET_VALUE (sym) == 0)
13275                    || (S_GET_VALUE (sym) != 0
13276                        && S_GET_VALUE (sym) <= g_switch_value)))
13277         change = 0;
13278       else
13279         {
13280           const char *segname;
13281
13282           segname = segment_name (S_GET_SEGMENT (sym));
13283           assert (strcmp (segname, ".lit8") != 0
13284                   && strcmp (segname, ".lit4") != 0);
13285           change = (strcmp (segname, ".sdata") != 0
13286                     && strcmp (segname, ".sbss") != 0
13287                     && strncmp (segname, ".sdata.", 7) != 0
13288                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13289         }
13290       return change;
13291     }
13292   else
13293     /* We are not optimizing for the $gp register.  */
13294     return 1;
13295 }
13296
13297
13298 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
13299
13300 static bfd_boolean
13301 pic_need_relax (sym, segtype)
13302      symbolS *sym;
13303      asection *segtype;
13304 {
13305   asection *symsec;
13306   bfd_boolean linkonce;
13307
13308   /* Handle the case of a symbol equated to another symbol.  */
13309   while (symbol_equated_reloc_p (sym))
13310     {
13311       symbolS *n;
13312
13313       /* It's possible to get a loop here in a badly written
13314          program.  */
13315       n = symbol_get_value_expression (sym)->X_add_symbol;
13316       if (n == sym)
13317         break;
13318       sym = n;
13319     }
13320
13321   symsec = S_GET_SEGMENT (sym);
13322
13323   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13324   linkonce = FALSE;
13325   if (symsec != segtype && ! S_IS_LOCAL (sym))
13326     {
13327       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13328           != 0)
13329         linkonce = TRUE;
13330
13331       /* The GNU toolchain uses an extension for ELF: a section
13332          beginning with the magic string .gnu.linkonce is a linkonce
13333          section.  */
13334       if (strncmp (segment_name (symsec), ".gnu.linkonce",
13335                    sizeof ".gnu.linkonce" - 1) == 0)
13336         linkonce = TRUE;
13337     }
13338
13339   /* This must duplicate the test in adjust_reloc_syms.  */
13340   return (symsec != &bfd_und_section
13341           && symsec != &bfd_abs_section
13342           && ! bfd_is_com_section (symsec)
13343           && !linkonce
13344 #ifdef OBJ_ELF
13345           /* A global or weak symbol is treated as external.  */
13346           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13347               || (! S_IS_WEAK (sym)
13348                   && (! S_IS_EXTERNAL (sym)
13349                       || mips_pic == EMBEDDED_PIC)))
13350 #endif
13351           );
13352 }
13353
13354
13355 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13356    extended opcode.  SEC is the section the frag is in.  */
13357
13358 static int
13359 mips16_extended_frag (fragp, sec, stretch)
13360      fragS *fragp;
13361      asection *sec;
13362      long stretch;
13363 {
13364   int type;
13365   register const struct mips16_immed_operand *op;
13366   offsetT val;
13367   int mintiny, maxtiny;
13368   segT symsec;
13369   fragS *sym_frag;
13370
13371   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13372     return 0;
13373   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13374     return 1;
13375
13376   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13377   op = mips16_immed_operands;
13378   while (op->type != type)
13379     {
13380       ++op;
13381       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13382     }
13383
13384   if (op->unsp)
13385     {
13386       if (type == '<' || type == '>' || type == '[' || type == ']')
13387         {
13388           mintiny = 1;
13389           maxtiny = 1 << op->nbits;
13390         }
13391       else
13392         {
13393           mintiny = 0;
13394           maxtiny = (1 << op->nbits) - 1;
13395         }
13396     }
13397   else
13398     {
13399       mintiny = - (1 << (op->nbits - 1));
13400       maxtiny = (1 << (op->nbits - 1)) - 1;
13401     }
13402
13403   sym_frag = symbol_get_frag (fragp->fr_symbol);
13404   val = S_GET_VALUE (fragp->fr_symbol);
13405   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13406
13407   if (op->pcrel)
13408     {
13409       addressT addr;
13410
13411       /* We won't have the section when we are called from
13412          mips_relax_frag.  However, we will always have been called
13413          from md_estimate_size_before_relax first.  If this is a
13414          branch to a different section, we mark it as such.  If SEC is
13415          NULL, and the frag is not marked, then it must be a branch to
13416          the same section.  */
13417       if (sec == NULL)
13418         {
13419           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13420             return 1;
13421         }
13422       else
13423         {
13424           /* Must have been called from md_estimate_size_before_relax.  */
13425           if (symsec != sec)
13426             {
13427               fragp->fr_subtype =
13428                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13429
13430               /* FIXME: We should support this, and let the linker
13431                  catch branches and loads that are out of range.  */
13432               as_bad_where (fragp->fr_file, fragp->fr_line,
13433                             _("unsupported PC relative reference to different section"));
13434
13435               return 1;
13436             }
13437           if (fragp != sym_frag && sym_frag->fr_address == 0)
13438             /* Assume non-extended on the first relaxation pass.
13439                The address we have calculated will be bogus if this is
13440                a forward branch to another frag, as the forward frag
13441                will have fr_address == 0.  */
13442             return 0;
13443         }
13444
13445       /* In this case, we know for sure that the symbol fragment is in
13446          the same section.  If the relax_marker of the symbol fragment
13447          differs from the relax_marker of this fragment, we have not
13448          yet adjusted the symbol fragment fr_address.  We want to add
13449          in STRETCH in order to get a better estimate of the address.
13450          This particularly matters because of the shift bits.  */
13451       if (stretch != 0
13452           && sym_frag->relax_marker != fragp->relax_marker)
13453         {
13454           fragS *f;
13455
13456           /* Adjust stretch for any alignment frag.  Note that if have
13457              been expanding the earlier code, the symbol may be
13458              defined in what appears to be an earlier frag.  FIXME:
13459              This doesn't handle the fr_subtype field, which specifies
13460              a maximum number of bytes to skip when doing an
13461              alignment.  */
13462           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13463             {
13464               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13465                 {
13466                   if (stretch < 0)
13467                     stretch = - ((- stretch)
13468                                  & ~ ((1 << (int) f->fr_offset) - 1));
13469                   else
13470                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13471                   if (stretch == 0)
13472                     break;
13473                 }
13474             }
13475           if (f != NULL)
13476             val += stretch;
13477         }
13478
13479       addr = fragp->fr_address + fragp->fr_fix;
13480
13481       /* The base address rules are complicated.  The base address of
13482          a branch is the following instruction.  The base address of a
13483          PC relative load or add is the instruction itself, but if it
13484          is in a delay slot (in which case it can not be extended) use
13485          the address of the instruction whose delay slot it is in.  */
13486       if (type == 'p' || type == 'q')
13487         {
13488           addr += 2;
13489
13490           /* If we are currently assuming that this frag should be
13491              extended, then, the current address is two bytes
13492              higher.  */
13493           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13494             addr += 2;
13495
13496           /* Ignore the low bit in the target, since it will be set
13497              for a text label.  */
13498           if ((val & 1) != 0)
13499             --val;
13500         }
13501       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13502         addr -= 4;
13503       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13504         addr -= 2;
13505
13506       val -= addr & ~ ((1 << op->shift) - 1);
13507
13508       /* Branch offsets have an implicit 0 in the lowest bit.  */
13509       if (type == 'p' || type == 'q')
13510         val /= 2;
13511
13512       /* If any of the shifted bits are set, we must use an extended
13513          opcode.  If the address depends on the size of this
13514          instruction, this can lead to a loop, so we arrange to always
13515          use an extended opcode.  We only check this when we are in
13516          the main relaxation loop, when SEC is NULL.  */
13517       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13518         {
13519           fragp->fr_subtype =
13520             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13521           return 1;
13522         }
13523
13524       /* If we are about to mark a frag as extended because the value
13525          is precisely maxtiny + 1, then there is a chance of an
13526          infinite loop as in the following code:
13527              la $4,foo
13528              .skip      1020
13529              .align     2
13530            foo:
13531          In this case when the la is extended, foo is 0x3fc bytes
13532          away, so the la can be shrunk, but then foo is 0x400 away, so
13533          the la must be extended.  To avoid this loop, we mark the
13534          frag as extended if it was small, and is about to become
13535          extended with a value of maxtiny + 1.  */
13536       if (val == ((maxtiny + 1) << op->shift)
13537           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13538           && sec == NULL)
13539         {
13540           fragp->fr_subtype =
13541             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13542           return 1;
13543         }
13544     }
13545   else if (symsec != absolute_section && sec != NULL)
13546     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13547
13548   if ((val & ((1 << op->shift) - 1)) != 0
13549       || val < (mintiny << op->shift)
13550       || val > (maxtiny << op->shift))
13551     return 1;
13552   else
13553     return 0;
13554 }
13555
13556 /* Compute the length of a branch sequence, and adjust the
13557    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13558    worst-case length is computed, with UPDATE being used to indicate
13559    whether an unconditional (-1), branch-likely (+1) or regular (0)
13560    branch is to be computed.  */
13561 static int
13562 relaxed_branch_length (fragp, sec, update)
13563      fragS *fragp;
13564      asection *sec;
13565      int update;
13566 {
13567   bfd_boolean toofar;
13568   int length;
13569
13570   if (fragp
13571       && S_IS_DEFINED (fragp->fr_symbol)
13572       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13573     {
13574       addressT addr;
13575       offsetT val;
13576
13577       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13578
13579       addr = fragp->fr_address + fragp->fr_fix + 4;
13580
13581       val -= addr;
13582
13583       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13584     }
13585   else if (fragp)
13586     /* If the symbol is not defined or it's in a different segment,
13587        assume the user knows what's going on and emit a short
13588        branch.  */
13589     toofar = FALSE;
13590   else
13591     toofar = TRUE;
13592
13593   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13594     fragp->fr_subtype
13595       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13596                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13597                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13598                              toofar);
13599
13600   length = 4;
13601   if (toofar)
13602     {
13603       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13604         length += 8;
13605
13606       if (mips_pic != NO_PIC)
13607         {
13608           /* Additional space for PIC loading of target address.  */
13609           length += 8;
13610           if (mips_opts.isa == ISA_MIPS1)
13611             /* Additional space for $at-stabilizing nop.  */
13612             length += 4;
13613         }
13614
13615       /* If branch is conditional.  */
13616       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13617         length += 8;
13618     }
13619
13620   return length;
13621 }
13622
13623 /* Estimate the size of a frag before relaxing.  Unless this is the
13624    mips16, we are not really relaxing here, and the final size is
13625    encoded in the subtype information.  For the mips16, we have to
13626    decide whether we are using an extended opcode or not.  */
13627
13628 int
13629 md_estimate_size_before_relax (fragp, segtype)
13630      fragS *fragp;
13631      asection *segtype;
13632 {
13633   int change;
13634
13635   if (RELAX_BRANCH_P (fragp->fr_subtype))
13636     {
13637
13638       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13639
13640       return fragp->fr_var;
13641     }
13642
13643   if (RELAX_MIPS16_P (fragp->fr_subtype))
13644     /* We don't want to modify the EXTENDED bit here; it might get us
13645        into infinite loops.  We change it only in mips_relax_frag().  */
13646     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13647
13648   if (mips_pic == NO_PIC)
13649     change = nopic_need_relax (fragp->fr_symbol, 0);
13650   else if (mips_pic == SVR4_PIC)
13651     change = pic_need_relax (fragp->fr_symbol, segtype);
13652   else
13653     abort ();
13654
13655   if (change)
13656     {
13657       /* Record the offset to the first reloc in the fr_opcode field.
13658          This lets md_convert_frag and tc_gen_reloc know that the code
13659          must be expanded.  */
13660       fragp->fr_opcode = (fragp->fr_literal
13661                           + fragp->fr_fix
13662                           - RELAX_OLD (fragp->fr_subtype)
13663                           + RELAX_RELOC1 (fragp->fr_subtype));
13664       /* FIXME: This really needs as_warn_where.  */
13665       if (RELAX_WARN (fragp->fr_subtype))
13666         as_warn (_("AT used after \".set noat\" or macro used after "
13667                    "\".set nomacro\""));
13668
13669       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13670     }
13671
13672   return 0;
13673 }
13674
13675 /* This is called to see whether a reloc against a defined symbol
13676    should be converted into a reloc against a section.  Don't adjust
13677    MIPS16 jump relocations, so we don't have to worry about the format
13678    of the offset in the .o file.  Don't adjust relocations against
13679    mips16 symbols, so that the linker can find them if it needs to set
13680    up a stub.  */
13681
13682 int
13683 mips_fix_adjustable (fixp)
13684      fixS *fixp;
13685 {
13686   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13687     return 0;
13688
13689   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13690       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13691     return 0;
13692
13693   if (fixp->fx_addsy == NULL)
13694     return 1;
13695
13696 #ifdef OBJ_ELF
13697   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13698       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13699       && fixp->fx_subsy == NULL)
13700     return 0;
13701 #endif
13702
13703   return 1;
13704 }
13705
13706 /* Translate internal representation of relocation info to BFD target
13707    format.  */
13708
13709 arelent **
13710 tc_gen_reloc (section, fixp)
13711      asection *section ATTRIBUTE_UNUSED;
13712      fixS *fixp;
13713 {
13714   static arelent *retval[4];
13715   arelent *reloc;
13716   bfd_reloc_code_real_type code;
13717
13718   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13719   retval[1] = NULL;
13720
13721   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13722   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13723   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13724
13725   if (mips_pic == EMBEDDED_PIC
13726       && SWITCH_TABLE (fixp))
13727     {
13728       /* For a switch table entry we use a special reloc.  The addend
13729          is actually the difference between the reloc address and the
13730          subtrahend.  */
13731       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13732       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13733         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13734       fixp->fx_r_type = BFD_RELOC_GPREL32;
13735     }
13736   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13737     {
13738       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13739         reloc->addend = fixp->fx_addnumber;
13740       else
13741         {
13742           /* We use a special addend for an internal RELLO reloc.  */
13743           if (symbol_section_p (fixp->fx_addsy))
13744             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13745           else
13746             reloc->addend = fixp->fx_addnumber + reloc->address;
13747         }
13748     }
13749   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13750     {
13751       assert (fixp->fx_next != NULL
13752               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13753
13754       /* The reloc is relative to the RELLO; adjust the addend
13755          accordingly.  */
13756       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13757         reloc->addend = fixp->fx_next->fx_addnumber;
13758       else
13759         {
13760           /* We use a special addend for an internal RELHI reloc.  */
13761           if (symbol_section_p (fixp->fx_addsy))
13762             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13763                              + fixp->fx_next->fx_where
13764                              - S_GET_VALUE (fixp->fx_subsy));
13765           else
13766             reloc->addend = (fixp->fx_addnumber
13767                              + fixp->fx_next->fx_frag->fr_address
13768                              + fixp->fx_next->fx_where);
13769         }
13770     }
13771   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13772     reloc->addend = fixp->fx_addnumber;
13773   else
13774     {
13775       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13776         /* A gruesome hack which is a result of the gruesome gas reloc
13777            handling.  */
13778         reloc->addend = reloc->address;
13779       else
13780         reloc->addend = -reloc->address;
13781     }
13782
13783   /* If this is a variant frag, we may need to adjust the existing
13784      reloc and generate a new one.  */
13785   if (fixp->fx_frag->fr_opcode != NULL
13786       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13787            && ! HAVE_NEWABI)
13788           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13789               && HAVE_NEWABI)
13790           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13791           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13792           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13793           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13794           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13795           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13796     )
13797     {
13798       arelent *reloc2;
13799
13800       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13801
13802       /* If this is not the last reloc in this frag, then we have two
13803          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13804          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13805          the second one handle all of them.  */
13806       if (fixp->fx_next != NULL
13807           && fixp->fx_frag == fixp->fx_next->fx_frag)
13808         {
13809           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13810                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13811                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13812                       && (fixp->fx_next->fx_r_type
13813                           == BFD_RELOC_MIPS_GOT_LO16))
13814                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13815                       && (fixp->fx_next->fx_r_type
13816                           == BFD_RELOC_MIPS_CALL_LO16)));
13817           retval[0] = NULL;
13818           return retval;
13819         }
13820
13821       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13822       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13823       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13824       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13825       retval[2] = NULL;
13826       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13827       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13828       reloc2->address = (reloc->address
13829                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13830                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13831       reloc2->addend = fixp->fx_addnumber
13832         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13833       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13834       assert (reloc2->howto != NULL);
13835
13836       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13837         {
13838           arelent *reloc3;
13839
13840           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13841           retval[3] = NULL;
13842           *reloc3 = *reloc2;
13843           reloc3->address += 4;
13844         }
13845
13846       if (mips_pic == NO_PIC)
13847         {
13848           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13849           fixp->fx_r_type = BFD_RELOC_HI16_S;
13850         }
13851       else if (mips_pic == SVR4_PIC)
13852         {
13853           switch (fixp->fx_r_type)
13854             {
13855             default:
13856               abort ();
13857             case BFD_RELOC_MIPS_GOT16:
13858               break;
13859             case BFD_RELOC_MIPS_GOT_LO16:
13860             case BFD_RELOC_MIPS_CALL_LO16:
13861               if (HAVE_NEWABI)
13862                 {
13863                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13864                   reloc2->howto = bfd_reloc_type_lookup
13865                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13866                 }
13867               else
13868                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13869               break;
13870             case BFD_RELOC_MIPS_CALL16:
13871             case BFD_RELOC_MIPS_GOT_OFST:
13872             case BFD_RELOC_MIPS_GOT_DISP:
13873               if (HAVE_NEWABI)
13874                 {
13875                   /* It may seem nonsensical to relax GOT_DISP to
13876                      GOT_DISP, but we're actually turning a GOT_DISP
13877                      without offset into a GOT_DISP with an offset,
13878                      getting rid of the separate addition, which we can
13879                      do when the symbol is found to be local.  */
13880                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13881                   retval[1] = NULL;
13882                 }
13883               else
13884                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13885               break;
13886             }
13887         }
13888       else
13889         abort ();
13890     }
13891
13892   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13893      entry to be used in the relocation's section offset.  */
13894   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13895     {
13896       reloc->address = reloc->addend;
13897       reloc->addend = 0;
13898     }
13899
13900   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13901      fixup_segment converted a non-PC relative reloc into a PC
13902      relative reloc.  In such a case, we need to convert the reloc
13903      code.  */
13904   code = fixp->fx_r_type;
13905   if (fixp->fx_pcrel)
13906     {
13907       switch (code)
13908         {
13909         case BFD_RELOC_8:
13910           code = BFD_RELOC_8_PCREL;
13911           break;
13912         case BFD_RELOC_16:
13913           code = BFD_RELOC_16_PCREL;
13914           break;
13915         case BFD_RELOC_32:
13916           code = BFD_RELOC_32_PCREL;
13917           break;
13918         case BFD_RELOC_64:
13919           code = BFD_RELOC_64_PCREL;
13920           break;
13921         case BFD_RELOC_8_PCREL:
13922         case BFD_RELOC_16_PCREL:
13923         case BFD_RELOC_32_PCREL:
13924         case BFD_RELOC_64_PCREL:
13925         case BFD_RELOC_16_PCREL_S2:
13926         case BFD_RELOC_PCREL_HI16_S:
13927         case BFD_RELOC_PCREL_LO16:
13928           break;
13929         default:
13930           as_bad_where (fixp->fx_file, fixp->fx_line,
13931                         _("Cannot make %s relocation PC relative"),
13932                         bfd_get_reloc_code_name (code));
13933         }
13934     }
13935
13936 #ifdef OBJ_ELF
13937   /* md_apply_fix3 has a double-subtraction hack to get
13938      bfd_install_relocation to behave nicely.  GPREL relocations are
13939      handled correctly without this hack, so undo it here.  We can't
13940      stop md_apply_fix3 from subtracting twice in the first place since
13941      the fake addend is required for variant frags above.  */
13942   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13943       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13944       && reloc->addend != 0
13945       && mips_need_elf_addend_fixup (fixp))
13946     {
13947       /* If howto->partial_inplace is false, md_apply_fix3 will only
13948          subtract it once.  */
13949       reloc_howto_type *howto;
13950
13951       howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
13952       if (howto->partial_inplace)
13953         reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13954     }
13955 #endif
13956
13957   /* To support a PC relative reloc when generating embedded PIC code
13958      for ECOFF, we use a Cygnus extension.  We check for that here to
13959      make sure that we don't let such a reloc escape normally.  */
13960   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13961        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13962       && code == BFD_RELOC_16_PCREL_S2
13963       && mips_pic != EMBEDDED_PIC)
13964     reloc->howto = NULL;
13965   else
13966     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13967
13968   if (reloc->howto == NULL)
13969     {
13970       as_bad_where (fixp->fx_file, fixp->fx_line,
13971                     _("Can not represent %s relocation in this object file format"),
13972                     bfd_get_reloc_code_name (code));
13973       retval[0] = NULL;
13974     }
13975
13976   return retval;
13977 }
13978
13979 /* Relax a machine dependent frag.  This returns the amount by which
13980    the current size of the frag should change.  */
13981
13982 int
13983 mips_relax_frag (sec, fragp, stretch)
13984      asection *sec;
13985      fragS *fragp;
13986      long stretch;
13987 {
13988   if (RELAX_BRANCH_P (fragp->fr_subtype))
13989     {
13990       offsetT old_var = fragp->fr_var;
13991
13992       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13993
13994       return fragp->fr_var - old_var;
13995     }
13996
13997   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13998     return 0;
13999
14000   if (mips16_extended_frag (fragp, NULL, stretch))
14001     {
14002       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14003         return 0;
14004       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
14005       return 2;
14006     }
14007   else
14008     {
14009       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14010         return 0;
14011       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
14012       return -2;
14013     }
14014
14015   return 0;
14016 }
14017
14018 /* Convert a machine dependent frag.  */
14019
14020 void
14021 md_convert_frag (abfd, asec, fragp)
14022      bfd *abfd ATTRIBUTE_UNUSED;
14023      segT asec;
14024      fragS *fragp;
14025 {
14026   int old, new;
14027   char *fixptr;
14028
14029   if (RELAX_BRANCH_P (fragp->fr_subtype))
14030     {
14031       bfd_byte *buf;
14032       unsigned long insn;
14033       expressionS exp;
14034       fixS *fixp;
14035
14036       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
14037
14038       if (target_big_endian)
14039         insn = bfd_getb32 (buf);
14040       else
14041         insn = bfd_getl32 (buf);
14042
14043       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
14044         {
14045           /* We generate a fixup instead of applying it right now
14046              because, if there are linker relaxations, we're going to
14047              need the relocations.  */
14048           exp.X_op = O_symbol;
14049           exp.X_add_symbol = fragp->fr_symbol;
14050           exp.X_add_number = fragp->fr_offset;
14051
14052           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14053                               4, &exp, 1,
14054                               BFD_RELOC_16_PCREL_S2);
14055           fixp->fx_file = fragp->fr_file;
14056           fixp->fx_line = fragp->fr_line;
14057
14058           md_number_to_chars ((char *)buf, insn, 4);
14059           buf += 4;
14060         }
14061       else
14062         {
14063           int i;
14064
14065           as_warn_where (fragp->fr_file, fragp->fr_line,
14066                          _("relaxed out-of-range branch into a jump"));
14067
14068           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14069             goto uncond;
14070
14071           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14072             {
14073               /* Reverse the branch.  */
14074               switch ((insn >> 28) & 0xf)
14075                 {
14076                 case 4:
14077                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14078                      have the condition reversed by tweaking a single
14079                      bit, and their opcodes all have 0x4???????.  */
14080                   assert ((insn & 0xf1000000) == 0x41000000);
14081                   insn ^= 0x00010000;
14082                   break;
14083
14084                 case 0:
14085                   /* bltz       0x04000000      bgez    0x04010000
14086                      bltzal     0x04100000      bgezal  0x04110000 */
14087                   assert ((insn & 0xfc0e0000) == 0x04000000);
14088                   insn ^= 0x00010000;
14089                   break;
14090
14091                 case 1:
14092                   /* beq        0x10000000      bne     0x14000000
14093                      blez       0x18000000      bgtz    0x1c000000 */
14094                   insn ^= 0x04000000;
14095                   break;
14096
14097                 default:
14098                   abort ();
14099                 }
14100             }
14101
14102           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14103             {
14104               /* Clear the and-link bit.  */
14105               assert ((insn & 0xfc1c0000) == 0x04100000);
14106
14107               /* bltzal 0x04100000      bgezal  0x04110000
14108                 bltzall 0x04120000     bgezall  0x04130000 */
14109               insn &= ~0x00100000;
14110             }
14111
14112           /* Branch over the branch (if the branch was likely) or the
14113              full jump (not likely case).  Compute the offset from the
14114              current instruction to branch to.  */
14115           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14116             i = 16;
14117           else
14118             {
14119               /* How many bytes in instructions we've already emitted?  */
14120               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14121               /* How many bytes in instructions from here to the end?  */
14122               i = fragp->fr_var - i;
14123             }
14124           /* Convert to instruction count.  */
14125           i >>= 2;
14126           /* Branch counts from the next instruction.  */
14127           i--;
14128           insn |= i;
14129           /* Branch over the jump.  */
14130           md_number_to_chars ((char *)buf, insn, 4);
14131           buf += 4;
14132
14133           /* Nop */
14134           md_number_to_chars ((char*)buf, 0, 4);
14135           buf += 4;
14136
14137           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14138             {
14139               /* beql $0, $0, 2f */
14140               insn = 0x50000000;
14141               /* Compute the PC offset from the current instruction to
14142                  the end of the variable frag.  */
14143               /* How many bytes in instructions we've already emitted?  */
14144               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14145               /* How many bytes in instructions from here to the end?  */
14146               i = fragp->fr_var - i;
14147               /* Convert to instruction count.  */
14148               i >>= 2;
14149               /* Don't decrement i, because we want to branch over the
14150                  delay slot.  */
14151
14152               insn |= i;
14153               md_number_to_chars ((char *)buf, insn, 4);
14154               buf += 4;
14155
14156               md_number_to_chars ((char *)buf, 0, 4);
14157               buf += 4;
14158             }
14159
14160         uncond:
14161           if (mips_pic == NO_PIC)
14162             {
14163               /* j or jal.  */
14164               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14165                       ? 0x0c000000 : 0x08000000);
14166               exp.X_op = O_symbol;
14167               exp.X_add_symbol = fragp->fr_symbol;
14168               exp.X_add_number = fragp->fr_offset;
14169
14170               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14171                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
14172               fixp->fx_file = fragp->fr_file;
14173               fixp->fx_line = fragp->fr_line;
14174
14175               md_number_to_chars ((char*)buf, insn, 4);
14176               buf += 4;
14177             }
14178           else
14179             {
14180               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
14181               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14182               exp.X_op = O_symbol;
14183               exp.X_add_symbol = fragp->fr_symbol;
14184               exp.X_add_number = fragp->fr_offset;
14185
14186               if (fragp->fr_offset)
14187                 {
14188                   exp.X_add_symbol = make_expr_symbol (&exp);
14189                   exp.X_add_number = 0;
14190                 }
14191
14192               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14193                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14194               fixp->fx_file = fragp->fr_file;
14195               fixp->fx_line = fragp->fr_line;
14196
14197               md_number_to_chars ((char*)buf, insn, 4);
14198               buf += 4;
14199
14200               if (mips_opts.isa == ISA_MIPS1)
14201                 {
14202                   /* nop */
14203                   md_number_to_chars ((char*)buf, 0, 4);
14204                   buf += 4;
14205                 }
14206
14207               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
14208               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14209
14210               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14211                                   4, &exp, 0, BFD_RELOC_LO16);
14212               fixp->fx_file = fragp->fr_file;
14213               fixp->fx_line = fragp->fr_line;
14214
14215               md_number_to_chars ((char*)buf, insn, 4);
14216               buf += 4;
14217
14218               /* j(al)r $at.  */
14219               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14220                 insn = 0x0020f809;
14221               else
14222                 insn = 0x00200008;
14223
14224               md_number_to_chars ((char*)buf, insn, 4);
14225               buf += 4;
14226             }
14227         }
14228
14229       assert (buf == (bfd_byte *)fragp->fr_literal
14230               + fragp->fr_fix + fragp->fr_var);
14231
14232       fragp->fr_fix += fragp->fr_var;
14233
14234       return;
14235     }
14236
14237   if (RELAX_MIPS16_P (fragp->fr_subtype))
14238     {
14239       int type;
14240       register const struct mips16_immed_operand *op;
14241       bfd_boolean small, ext;
14242       offsetT val;
14243       bfd_byte *buf;
14244       unsigned long insn;
14245       bfd_boolean use_extend;
14246       unsigned short extend;
14247
14248       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14249       op = mips16_immed_operands;
14250       while (op->type != type)
14251         ++op;
14252
14253       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14254         {
14255           small = FALSE;
14256           ext = TRUE;
14257         }
14258       else
14259         {
14260           small = TRUE;
14261           ext = FALSE;
14262         }
14263
14264       resolve_symbol_value (fragp->fr_symbol);
14265       val = S_GET_VALUE (fragp->fr_symbol);
14266       if (op->pcrel)
14267         {
14268           addressT addr;
14269
14270           addr = fragp->fr_address + fragp->fr_fix;
14271
14272           /* The rules for the base address of a PC relative reloc are
14273              complicated; see mips16_extended_frag.  */
14274           if (type == 'p' || type == 'q')
14275             {
14276               addr += 2;
14277               if (ext)
14278                 addr += 2;
14279               /* Ignore the low bit in the target, since it will be
14280                  set for a text label.  */
14281               if ((val & 1) != 0)
14282                 --val;
14283             }
14284           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14285             addr -= 4;
14286           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14287             addr -= 2;
14288
14289           addr &= ~ (addressT) ((1 << op->shift) - 1);
14290           val -= addr;
14291
14292           /* Make sure the section winds up with the alignment we have
14293              assumed.  */
14294           if (op->shift > 0)
14295             record_alignment (asec, op->shift);
14296         }
14297
14298       if (ext
14299           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14300               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14301         as_warn_where (fragp->fr_file, fragp->fr_line,
14302                        _("extended instruction in delay slot"));
14303
14304       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14305
14306       if (target_big_endian)
14307         insn = bfd_getb16 (buf);
14308       else
14309         insn = bfd_getl16 (buf);
14310
14311       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14312                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14313                     small, ext, &insn, &use_extend, &extend);
14314
14315       if (use_extend)
14316         {
14317           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
14318           fragp->fr_fix += 2;
14319           buf += 2;
14320         }
14321
14322       md_number_to_chars ((char *) buf, insn, 2);
14323       fragp->fr_fix += 2;
14324       buf += 2;
14325     }
14326   else
14327     {
14328       if (fragp->fr_opcode == NULL)
14329         return;
14330
14331       old = RELAX_OLD (fragp->fr_subtype);
14332       new = RELAX_NEW (fragp->fr_subtype);
14333       fixptr = fragp->fr_literal + fragp->fr_fix;
14334
14335       if (new > 0)
14336         memmove (fixptr - old, fixptr, new);
14337
14338       fragp->fr_fix += new - old;
14339     }
14340 }
14341
14342 #ifdef OBJ_ELF
14343
14344 /* This function is called after the relocs have been generated.
14345    We've been storing mips16 text labels as odd.  Here we convert them
14346    back to even for the convenience of the debugger.  */
14347
14348 void
14349 mips_frob_file_after_relocs ()
14350 {
14351   asymbol **syms;
14352   unsigned int count, i;
14353
14354   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14355     return;
14356
14357   syms = bfd_get_outsymbols (stdoutput);
14358   count = bfd_get_symcount (stdoutput);
14359   for (i = 0; i < count; i++, syms++)
14360     {
14361       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14362           && ((*syms)->value & 1) != 0)
14363         {
14364           (*syms)->value &= ~1;
14365           /* If the symbol has an odd size, it was probably computed
14366              incorrectly, so adjust that as well.  */
14367           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14368             ++elf_symbol (*syms)->internal_elf_sym.st_size;
14369         }
14370     }
14371 }
14372
14373 #endif
14374
14375 /* This function is called whenever a label is defined.  It is used
14376    when handling branch delays; if a branch has a label, we assume we
14377    can not move it.  */
14378
14379 void
14380 mips_define_label (sym)
14381      symbolS *sym;
14382 {
14383   struct insn_label_list *l;
14384
14385   if (free_insn_labels == NULL)
14386     l = (struct insn_label_list *) xmalloc (sizeof *l);
14387   else
14388     {
14389       l = free_insn_labels;
14390       free_insn_labels = l->next;
14391     }
14392
14393   l->label = sym;
14394   l->next = insn_labels;
14395   insn_labels = l;
14396 }
14397 \f
14398 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14399
14400 /* Some special processing for a MIPS ELF file.  */
14401
14402 void
14403 mips_elf_final_processing ()
14404 {
14405   /* Write out the register information.  */
14406   if (mips_abi != N64_ABI)
14407     {
14408       Elf32_RegInfo s;
14409
14410       s.ri_gprmask = mips_gprmask;
14411       s.ri_cprmask[0] = mips_cprmask[0];
14412       s.ri_cprmask[1] = mips_cprmask[1];
14413       s.ri_cprmask[2] = mips_cprmask[2];
14414       s.ri_cprmask[3] = mips_cprmask[3];
14415       /* The gp_value field is set by the MIPS ELF backend.  */
14416
14417       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14418                                        ((Elf32_External_RegInfo *)
14419                                         mips_regmask_frag));
14420     }
14421   else
14422     {
14423       Elf64_Internal_RegInfo s;
14424
14425       s.ri_gprmask = mips_gprmask;
14426       s.ri_pad = 0;
14427       s.ri_cprmask[0] = mips_cprmask[0];
14428       s.ri_cprmask[1] = mips_cprmask[1];
14429       s.ri_cprmask[2] = mips_cprmask[2];
14430       s.ri_cprmask[3] = mips_cprmask[3];
14431       /* The gp_value field is set by the MIPS ELF backend.  */
14432
14433       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14434                                        ((Elf64_External_RegInfo *)
14435                                         mips_regmask_frag));
14436     }
14437
14438   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14439      sort of BFD interface for this.  */
14440   if (mips_any_noreorder)
14441     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14442   if (mips_pic != NO_PIC)
14443     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14444
14445   /* Set MIPS ELF flags for ASEs.  */
14446   if (file_ase_mips16)
14447     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14448 #if 0 /* XXX FIXME */
14449   if (file_ase_mips3d)
14450     elf_elfheader (stdoutput)->e_flags |= ???;
14451 #endif
14452   if (file_ase_mdmx)
14453     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14454
14455   /* Set the MIPS ELF ABI flags.  */
14456   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14457     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14458   else if (mips_abi == O64_ABI)
14459     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14460   else if (mips_abi == EABI_ABI)
14461     {
14462       if (!file_mips_gp32)
14463         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14464       else
14465         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14466     }
14467   else if (mips_abi == N32_ABI)
14468     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14469
14470   /* Nothing to do for N64_ABI.  */
14471
14472   if (mips_32bitmode)
14473     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14474 }
14475
14476 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14477 \f
14478 typedef struct proc {
14479   symbolS *isym;
14480   unsigned long reg_mask;
14481   unsigned long reg_offset;
14482   unsigned long fpreg_mask;
14483   unsigned long fpreg_offset;
14484   unsigned long frame_offset;
14485   unsigned long frame_reg;
14486   unsigned long pc_reg;
14487 } procS;
14488
14489 static procS cur_proc;
14490 static procS *cur_proc_ptr;
14491 static int numprocs;
14492
14493 /* Fill in an rs_align_code fragment.  */
14494
14495 void
14496 mips_handle_align (fragp)
14497      fragS *fragp;
14498 {
14499   if (fragp->fr_type != rs_align_code)
14500     return;
14501
14502   if (mips_opts.mips16)
14503     {
14504       static const unsigned char be_nop[] = { 0x65, 0x00 };
14505       static const unsigned char le_nop[] = { 0x00, 0x65 };
14506
14507       int bytes;
14508       char *p;
14509
14510       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14511       p = fragp->fr_literal + fragp->fr_fix;
14512
14513       if (bytes & 1)
14514         {
14515           *p++ = 0;
14516           fragp->fr_fix++;
14517         }
14518
14519       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14520       fragp->fr_var = 2;
14521     }
14522
14523   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14524 }
14525
14526 static void
14527 md_obj_begin ()
14528 {
14529 }
14530
14531 static void
14532 md_obj_end ()
14533 {
14534   /* check for premature end, nesting errors, etc */
14535   if (cur_proc_ptr)
14536     as_warn (_("missing .end at end of assembly"));
14537 }
14538
14539 static long
14540 get_number ()
14541 {
14542   int negative = 0;
14543   long val = 0;
14544
14545   if (*input_line_pointer == '-')
14546     {
14547       ++input_line_pointer;
14548       negative = 1;
14549     }
14550   if (!ISDIGIT (*input_line_pointer))
14551     as_bad (_("expected simple number"));
14552   if (input_line_pointer[0] == '0')
14553     {
14554       if (input_line_pointer[1] == 'x')
14555         {
14556           input_line_pointer += 2;
14557           while (ISXDIGIT (*input_line_pointer))
14558             {
14559               val <<= 4;
14560               val |= hex_value (*input_line_pointer++);
14561             }
14562           return negative ? -val : val;
14563         }
14564       else
14565         {
14566           ++input_line_pointer;
14567           while (ISDIGIT (*input_line_pointer))
14568             {
14569               val <<= 3;
14570               val |= *input_line_pointer++ - '0';
14571             }
14572           return negative ? -val : val;
14573         }
14574     }
14575   if (!ISDIGIT (*input_line_pointer))
14576     {
14577       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14578               *input_line_pointer, *input_line_pointer);
14579       as_warn (_("invalid number"));
14580       return -1;
14581     }
14582   while (ISDIGIT (*input_line_pointer))
14583     {
14584       val *= 10;
14585       val += *input_line_pointer++ - '0';
14586     }
14587   return negative ? -val : val;
14588 }
14589
14590 /* The .file directive; just like the usual .file directive, but there
14591    is an initial number which is the ECOFF file index.  In the non-ECOFF
14592    case .file implies DWARF-2.  */
14593
14594 static void
14595 s_mips_file (x)
14596      int x ATTRIBUTE_UNUSED;
14597 {
14598   static int first_file_directive = 0;
14599
14600   if (ECOFF_DEBUGGING)
14601     {
14602       get_number ();
14603       s_app_file (0);
14604     }
14605   else
14606     {
14607       char *filename;
14608
14609       filename = dwarf2_directive_file (0);
14610
14611       /* Versions of GCC up to 3.1 start files with a ".file"
14612          directive even for stabs output.  Make sure that this
14613          ".file" is handled.  Note that you need a version of GCC
14614          after 3.1 in order to support DWARF-2 on MIPS.  */
14615       if (filename != NULL && ! first_file_directive)
14616         {
14617           (void) new_logical_line (filename, -1);
14618           s_app_file_string (filename);
14619         }
14620       first_file_directive = 1;
14621     }
14622 }
14623
14624 /* The .loc directive, implying DWARF-2.  */
14625
14626 static void
14627 s_mips_loc (x)
14628      int x ATTRIBUTE_UNUSED;
14629 {
14630   if (!ECOFF_DEBUGGING)
14631     dwarf2_directive_loc (0);
14632 }
14633
14634 /* The .end directive.  */
14635
14636 static void
14637 s_mips_end (x)
14638      int x ATTRIBUTE_UNUSED;
14639 {
14640   symbolS *p;
14641
14642   /* Following functions need their own .frame and .cprestore directives.  */
14643   mips_frame_reg_valid = 0;
14644   mips_cprestore_valid = 0;
14645
14646   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14647     {
14648       p = get_symbol ();
14649       demand_empty_rest_of_line ();
14650     }
14651   else
14652     p = NULL;
14653
14654   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14655     as_warn (_(".end not in text section"));
14656
14657   if (!cur_proc_ptr)
14658     {
14659       as_warn (_(".end directive without a preceding .ent directive."));
14660       demand_empty_rest_of_line ();
14661       return;
14662     }
14663
14664   if (p != NULL)
14665     {
14666       assert (S_GET_NAME (p));
14667       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14668         as_warn (_(".end symbol does not match .ent symbol."));
14669
14670       if (debug_type == DEBUG_STABS)
14671         stabs_generate_asm_endfunc (S_GET_NAME (p),
14672                                     S_GET_NAME (p));
14673     }
14674   else
14675     as_warn (_(".end directive missing or unknown symbol"));
14676
14677 #ifdef OBJ_ELF
14678   /* Generate a .pdr section.  */
14679   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14680     {
14681       segT saved_seg = now_seg;
14682       subsegT saved_subseg = now_subseg;
14683       valueT dot;
14684       expressionS exp;
14685       char *fragp;
14686
14687       dot = frag_now_fix ();
14688
14689 #ifdef md_flush_pending_output
14690       md_flush_pending_output ();
14691 #endif
14692
14693       assert (pdr_seg);
14694       subseg_set (pdr_seg, 0);
14695
14696       /* Write the symbol.  */
14697       exp.X_op = O_symbol;
14698       exp.X_add_symbol = p;
14699       exp.X_add_number = 0;
14700       emit_expr (&exp, 4);
14701
14702       fragp = frag_more (7 * 4);
14703
14704       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14705       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14706       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14707       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14708       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14709       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14710       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14711
14712       subseg_set (saved_seg, saved_subseg);
14713     }
14714 #endif /* OBJ_ELF */
14715
14716   cur_proc_ptr = NULL;
14717 }
14718
14719 /* The .aent and .ent directives.  */
14720
14721 static void
14722 s_mips_ent (aent)
14723      int aent;
14724 {
14725   symbolS *symbolP;
14726
14727   symbolP = get_symbol ();
14728   if (*input_line_pointer == ',')
14729     ++input_line_pointer;
14730   SKIP_WHITESPACE ();
14731   if (ISDIGIT (*input_line_pointer)
14732       || *input_line_pointer == '-')
14733     get_number ();
14734
14735   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14736     as_warn (_(".ent or .aent not in text section."));
14737
14738   if (!aent && cur_proc_ptr)
14739     as_warn (_("missing .end"));
14740
14741   if (!aent)
14742     {
14743       /* This function needs its own .frame and .cprestore directives.  */
14744       mips_frame_reg_valid = 0;
14745       mips_cprestore_valid = 0;
14746
14747       cur_proc_ptr = &cur_proc;
14748       memset (cur_proc_ptr, '\0', sizeof (procS));
14749
14750       cur_proc_ptr->isym = symbolP;
14751
14752       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14753
14754       ++numprocs;
14755
14756       if (debug_type == DEBUG_STABS)
14757         stabs_generate_asm_func (S_GET_NAME (symbolP),
14758                                  S_GET_NAME (symbolP));
14759     }
14760
14761   demand_empty_rest_of_line ();
14762 }
14763
14764 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14765    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14766    s_mips_frame is used so that we can set the PDR information correctly.
14767    We can't use the ecoff routines because they make reference to the ecoff
14768    symbol table (in the mdebug section).  */
14769
14770 static void
14771 s_mips_frame (ignore)
14772      int ignore ATTRIBUTE_UNUSED;
14773 {
14774 #ifdef OBJ_ELF
14775   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14776     {
14777       long val;
14778
14779       if (cur_proc_ptr == (procS *) NULL)
14780         {
14781           as_warn (_(".frame outside of .ent"));
14782           demand_empty_rest_of_line ();
14783           return;
14784         }
14785
14786       cur_proc_ptr->frame_reg = tc_get_register (1);
14787
14788       SKIP_WHITESPACE ();
14789       if (*input_line_pointer++ != ','
14790           || get_absolute_expression_and_terminator (&val) != ',')
14791         {
14792           as_warn (_("Bad .frame directive"));
14793           --input_line_pointer;
14794           demand_empty_rest_of_line ();
14795           return;
14796         }
14797
14798       cur_proc_ptr->frame_offset = val;
14799       cur_proc_ptr->pc_reg = tc_get_register (0);
14800
14801       demand_empty_rest_of_line ();
14802     }
14803   else
14804 #endif /* OBJ_ELF */
14805     s_ignore (ignore);
14806 }
14807
14808 /* The .fmask and .mask directives. If the mdebug section is present
14809    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14810    embedded targets, s_mips_mask is used so that we can set the PDR
14811    information correctly. We can't use the ecoff routines because they
14812    make reference to the ecoff symbol table (in the mdebug section).  */
14813
14814 static void
14815 s_mips_mask (reg_type)
14816      char reg_type;
14817 {
14818 #ifdef OBJ_ELF
14819   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14820     {
14821       long mask, off;
14822
14823       if (cur_proc_ptr == (procS *) NULL)
14824         {
14825           as_warn (_(".mask/.fmask outside of .ent"));
14826           demand_empty_rest_of_line ();
14827           return;
14828         }
14829
14830       if (get_absolute_expression_and_terminator (&mask) != ',')
14831         {
14832           as_warn (_("Bad .mask/.fmask directive"));
14833           --input_line_pointer;
14834           demand_empty_rest_of_line ();
14835           return;
14836         }
14837
14838       off = get_absolute_expression ();
14839
14840       if (reg_type == 'F')
14841         {
14842           cur_proc_ptr->fpreg_mask = mask;
14843           cur_proc_ptr->fpreg_offset = off;
14844         }
14845       else
14846         {
14847           cur_proc_ptr->reg_mask = mask;
14848           cur_proc_ptr->reg_offset = off;
14849         }
14850
14851       demand_empty_rest_of_line ();
14852     }
14853   else
14854 #endif /* OBJ_ELF */
14855     s_ignore (reg_type);
14856 }
14857
14858 /* The .loc directive.  */
14859
14860 #if 0
14861 static void
14862 s_loc (x)
14863      int x;
14864 {
14865   symbolS *symbolP;
14866   int lineno;
14867   int addroff;
14868
14869   assert (now_seg == text_section);
14870
14871   lineno = get_number ();
14872   addroff = frag_now_fix ();
14873
14874   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14875   S_SET_TYPE (symbolP, N_SLINE);
14876   S_SET_OTHER (symbolP, 0);
14877   S_SET_DESC (symbolP, lineno);
14878   symbolP->sy_segment = now_seg;
14879 }
14880 #endif
14881
14882 /* A table describing all the processors gas knows about.  Names are
14883    matched in the order listed.
14884
14885    To ease comparison, please keep this table in the same order as
14886    gcc's mips_cpu_info_table[].  */
14887 static const struct mips_cpu_info mips_cpu_info_table[] =
14888 {
14889   /* Entries for generic ISAs */
14890   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14891   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14892   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14893   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14894   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14895   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14896   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14897   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14898
14899   /* MIPS I */
14900   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14901   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14902   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14903
14904   /* MIPS II */
14905   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14906
14907   /* MIPS III */
14908   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14909   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14910   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14911   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14912   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14913   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14914   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14915   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14916   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14917   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14918   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14919   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14920
14921   /* MIPS IV */
14922   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14923   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14924   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14925   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14926   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14927   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14928   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14929   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14930   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14931   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14932   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14933   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14934
14935   /* MIPS 32 */
14936   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14937   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14938   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14939
14940   /* MIPS 64 */
14941   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14942   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14943
14944   /* Broadcom SB-1 CPU core */
14945   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14946
14947   /* End marker */
14948   { NULL, 0, 0, 0 }
14949 };
14950
14951
14952 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14953    with a final "000" replaced by "k".  Ignore case.
14954
14955    Note: this function is shared between GCC and GAS.  */
14956
14957 static bfd_boolean
14958 mips_strict_matching_cpu_name_p (canonical, given)
14959      const char *canonical, *given;
14960 {
14961   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14962     given++, canonical++;
14963
14964   return ((*given == 0 && *canonical == 0)
14965           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14966 }
14967
14968
14969 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14970    CPU name.  We've traditionally allowed a lot of variation here.
14971
14972    Note: this function is shared between GCC and GAS.  */
14973
14974 static bfd_boolean
14975 mips_matching_cpu_name_p (canonical, given)
14976      const char *canonical, *given;
14977 {
14978   /* First see if the name matches exactly, or with a final "000"
14979      turned into "k".  */
14980   if (mips_strict_matching_cpu_name_p (canonical, given))
14981     return TRUE;
14982
14983   /* If not, try comparing based on numerical designation alone.
14984      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14985   if (TOLOWER (*given) == 'r')
14986     given++;
14987   if (!ISDIGIT (*given))
14988     return FALSE;
14989
14990   /* Skip over some well-known prefixes in the canonical name,
14991      hoping to find a number there too.  */
14992   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14993     canonical += 2;
14994   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14995     canonical += 2;
14996   else if (TOLOWER (canonical[0]) == 'r')
14997     canonical += 1;
14998
14999   return mips_strict_matching_cpu_name_p (canonical, given);
15000 }
15001
15002
15003 /* Parse an option that takes the name of a processor as its argument.
15004    OPTION is the name of the option and CPU_STRING is the argument.
15005    Return the corresponding processor enumeration if the CPU_STRING is
15006    recognized, otherwise report an error and return null.
15007
15008    A similar function exists in GCC.  */
15009
15010 static const struct mips_cpu_info *
15011 mips_parse_cpu (option, cpu_string)
15012      const char *option, *cpu_string;
15013 {
15014   const struct mips_cpu_info *p;
15015
15016   /* 'from-abi' selects the most compatible architecture for the given
15017      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
15018      EABIs, we have to decide whether we're using the 32-bit or 64-bit
15019      version.  Look first at the -mgp options, if given, otherwise base
15020      the choice on MIPS_DEFAULT_64BIT.
15021
15022      Treat NO_ABI like the EABIs.  One reason to do this is that the
15023      plain 'mips' and 'mips64' configs have 'from-abi' as their default
15024      architecture.  This code picks MIPS I for 'mips' and MIPS III for
15025      'mips64', just as we did in the days before 'from-abi'.  */
15026   if (strcasecmp (cpu_string, "from-abi") == 0)
15027     {
15028       if (ABI_NEEDS_32BIT_REGS (mips_abi))
15029         return mips_cpu_info_from_isa (ISA_MIPS1);
15030
15031       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15032         return mips_cpu_info_from_isa (ISA_MIPS3);
15033
15034       if (file_mips_gp32 >= 0)
15035         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
15036
15037       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
15038                                      ? ISA_MIPS3
15039                                      : ISA_MIPS1);
15040     }
15041
15042   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
15043   if (strcasecmp (cpu_string, "default") == 0)
15044     return 0;
15045
15046   for (p = mips_cpu_info_table; p->name != 0; p++)
15047     if (mips_matching_cpu_name_p (p->name, cpu_string))
15048       return p;
15049
15050   as_bad ("Bad value (%s) for %s", cpu_string, option);
15051   return 0;
15052 }
15053
15054 /* Return the canonical processor information for ISA (a member of the
15055    ISA_MIPS* enumeration).  */
15056
15057 static const struct mips_cpu_info *
15058 mips_cpu_info_from_isa (isa)
15059      int isa;
15060 {
15061   int i;
15062
15063   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15064     if (mips_cpu_info_table[i].is_isa
15065         && isa == mips_cpu_info_table[i].isa)
15066       return (&mips_cpu_info_table[i]);
15067
15068   return NULL;
15069 }
15070 \f
15071 static void
15072 show (stream, string, col_p, first_p)
15073      FILE *stream;
15074      const char *string;
15075      int *col_p;
15076      int *first_p;
15077 {
15078   if (*first_p)
15079     {
15080       fprintf (stream, "%24s", "");
15081       *col_p = 24;
15082     }
15083   else
15084     {
15085       fprintf (stream, ", ");
15086       *col_p += 2;
15087     }
15088
15089   if (*col_p + strlen (string) > 72)
15090     {
15091       fprintf (stream, "\n%24s", "");
15092       *col_p = 24;
15093     }
15094
15095   fprintf (stream, "%s", string);
15096   *col_p += strlen (string);
15097
15098   *first_p = 0;
15099 }
15100
15101 void
15102 md_show_usage (stream)
15103      FILE *stream;
15104 {
15105   int column, first;
15106   size_t i;
15107
15108   fprintf (stream, _("\
15109 MIPS options:\n\
15110 -membedded-pic          generate embedded position independent code\n\
15111 -EB                     generate big endian output\n\
15112 -EL                     generate little endian output\n\
15113 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
15114 -G NUM                  allow referencing objects up to NUM bytes\n\
15115                         implicitly with the gp register [default 8]\n"));
15116   fprintf (stream, _("\
15117 -mips1                  generate MIPS ISA I instructions\n\
15118 -mips2                  generate MIPS ISA II instructions\n\
15119 -mips3                  generate MIPS ISA III instructions\n\
15120 -mips4                  generate MIPS ISA IV instructions\n\
15121 -mips5                  generate MIPS ISA V instructions\n\
15122 -mips32                 generate MIPS32 ISA instructions\n\
15123 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
15124 -mips64                 generate MIPS64 ISA instructions\n\
15125 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
15126
15127   first = 1;
15128
15129   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15130     show (stream, mips_cpu_info_table[i].name, &column, &first);
15131   show (stream, "from-abi", &column, &first);
15132   fputc ('\n', stream);
15133
15134   fprintf (stream, _("\
15135 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15136 -no-mCPU                don't generate code specific to CPU.\n\
15137                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
15138
15139   first = 1;
15140
15141   show (stream, "3900", &column, &first);
15142   show (stream, "4010", &column, &first);
15143   show (stream, "4100", &column, &first);
15144   show (stream, "4650", &column, &first);
15145   fputc ('\n', stream);
15146
15147   fprintf (stream, _("\
15148 -mips16                 generate mips16 instructions\n\
15149 -no-mips16              do not generate mips16 instructions\n"));
15150   fprintf (stream, _("\
15151 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
15152 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
15153 -O0                     remove unneeded NOPs, do not swap branches\n\
15154 -O                      remove unneeded NOPs and swap branches\n\
15155 -n                      warn about NOPs generated from macros\n\
15156 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
15157 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
15158 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
15159 #ifdef OBJ_ELF
15160   fprintf (stream, _("\
15161 -KPIC, -call_shared     generate SVR4 position independent code\n\
15162 -non_shared             do not generate position independent code\n\
15163 -xgot                   assume a 32 bit GOT\n\
15164 -mabi=ABI               create ABI conformant object file for:\n"));
15165
15166   first = 1;
15167
15168   show (stream, "32", &column, &first);
15169   show (stream, "o64", &column, &first);
15170   show (stream, "n32", &column, &first);
15171   show (stream, "64", &column, &first);
15172   show (stream, "eabi", &column, &first);
15173
15174   fputc ('\n', stream);
15175
15176   fprintf (stream, _("\
15177 -32                     create o32 ABI object file (default)\n\
15178 -n32                    create n32 ABI object file\n\
15179 -64                     create 64 ABI object file\n"));
15180 #endif
15181 }
15182
15183 enum dwarf2_format
15184 mips_dwarf2_format ()
15185 {
15186   if (mips_abi == N64_ABI)
15187     {
15188 #ifdef TE_IRIX
15189       return dwarf2_format_64bit_irix;
15190 #else
15191       return dwarf2_format_64bit;
15192 #endif
15193     }
15194   else
15195     return dwarf2_format_32bit;
15196 }