2003-04-24 Eric Christopher <echristo@redhat.com>
[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   /* The following code determines the architecture and register size.
11229      Similar code was added to GCC 3.3 (see override_options() in
11230      config/mips/mips.c).  The GAS and GCC code should be kept in sync
11231      as much as possible.  */
11232
11233   if (mips_arch_string != 0)
11234     mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
11235
11236   if (mips_tune_string != 0)
11237     mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
11238
11239   if (file_mips_isa != ISA_UNKNOWN)
11240     {
11241       /* Handle -mipsN.  At this point, file_mips_isa contains the
11242          ISA level specified by -mipsN, while mips_opts.isa contains
11243          the -march selection (if any).  */
11244       if (mips_arch_info != 0)
11245         {
11246           /* -march takes precedence over -mipsN, since it is more descriptive.
11247              There's no harm in specifying both as long as the ISA levels
11248              are the same.  */
11249           if (file_mips_isa != mips_opts.isa)
11250             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11251                     mips_cpu_info_from_isa (file_mips_isa)->name,
11252                     mips_cpu_info_from_isa (mips_opts.isa)->name);
11253         }
11254       else
11255         mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
11256     }
11257
11258   if (mips_arch_info == 0)
11259     mips_set_architecture (mips_parse_cpu ("default CPU",
11260                                            MIPS_CPU_STRING_DEFAULT));
11261
11262   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11263     as_bad ("-march=%s is not compatible with the selected ABI",
11264             mips_arch_info->name);
11265
11266   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
11267   if (mips_tune_info == 0)
11268     mips_set_tune (mips_arch_info);
11269
11270   if (file_mips_gp32 >= 0)
11271     {
11272       /* The user specified the size of the integer registers.  Make sure
11273          it agrees with the ABI and ISA.  */
11274       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11275         as_bad (_("-mgp64 used with a 32-bit processor"));
11276       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11277         as_bad (_("-mgp32 used with a 64-bit ABI"));
11278       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11279         as_bad (_("-mgp64 used with a 32-bit ABI"));
11280     }
11281   else
11282     {
11283       /* Infer the integer register size from the ABI and processor.
11284          Restrict ourselves to 32-bit registers if that's all the
11285          processor has, or if the ABI cannot handle 64-bit registers.  */
11286       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11287                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11288     }
11289
11290   /* ??? GAS treats single-float processors as though they had 64-bit
11291      float registers (although it complains when double-precision
11292      instructions are used).  As things stand, saying they have 32-bit
11293      registers would lead to spurious "register must be even" messages.
11294      So here we assume float registers are always the same size as
11295      integer ones, unless the user says otherwise.  */
11296   if (file_mips_fp32 < 0)
11297     file_mips_fp32 = file_mips_gp32;
11298
11299   /* End of GCC-shared inference code.  */
11300
11301   /* ??? When do we want this flag to be set?   Who uses it?  */
11302   if (file_mips_gp32 == 1
11303       && mips_abi == NO_ABI
11304       && ISA_HAS_64BIT_REGS (mips_opts.isa))
11305     mips_32bitmode = 1;
11306
11307   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11308     as_bad (_("trap exception not supported at ISA 1"));
11309
11310   /* If the selected architecture includes support for ASEs, enable
11311      generation of code for them.  */
11312   if (mips_opts.mips16 == -1)
11313     mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
11314   if (mips_opts.ase_mips3d == -1)
11315     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
11316   if (mips_opts.ase_mdmx == -1)
11317     mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
11318
11319   file_mips_isa = mips_opts.isa;
11320   file_ase_mips16 = mips_opts.mips16;
11321   file_ase_mips3d = mips_opts.ase_mips3d;
11322   file_ase_mdmx = mips_opts.ase_mdmx;
11323   mips_opts.gp32 = file_mips_gp32;
11324   mips_opts.fp32 = file_mips_fp32;
11325
11326   if (mips_flag_mdebug < 0)
11327     {
11328 #ifdef OBJ_MAYBE_ECOFF
11329       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11330         mips_flag_mdebug = 1;
11331       else
11332 #endif /* OBJ_MAYBE_ECOFF */
11333         mips_flag_mdebug = 0;
11334     }
11335 }
11336 \f
11337 void
11338 mips_init_after_args ()
11339 {
11340   /* initialize opcodes */
11341   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11342   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11343 }
11344
11345 long
11346 md_pcrel_from (fixP)
11347      fixS *fixP;
11348 {
11349   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
11350       && fixP->fx_addsy != (symbolS *) NULL
11351       && ! S_IS_DEFINED (fixP->fx_addsy))
11352     return 4;
11353
11354   /* Return the address of the delay slot.  */
11355   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
11356 }
11357
11358 /* This is called before the symbol table is processed.  In order to
11359    work with gcc when using mips-tfile, we must keep all local labels.
11360    However, in other cases, we want to discard them.  If we were
11361    called with -g, but we didn't see any debugging information, it may
11362    mean that gcc is smuggling debugging information through to
11363    mips-tfile, in which case we must generate all local labels.  */
11364
11365 void
11366 mips_frob_file_before_adjust ()
11367 {
11368 #ifndef NO_ECOFF_DEBUGGING
11369   if (ECOFF_DEBUGGING
11370       && mips_debug != 0
11371       && ! ecoff_debugging_seen)
11372     flag_keep_locals = 1;
11373 #endif
11374 }
11375
11376 /* Sort any unmatched HI16_S relocs so that they immediately precede
11377    the corresponding LO reloc.  This is called before md_apply_fix3 and
11378    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11379    explicit use of the %hi modifier.  */
11380
11381 void
11382 mips_frob_file ()
11383 {
11384   struct mips_hi_fixup *l;
11385
11386   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11387     {
11388       segment_info_type *seginfo;
11389       int pass;
11390
11391       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11392
11393       /* If a GOT16 relocation turns out to be against a global symbol,
11394          there isn't supposed to be a matching LO.  */
11395       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11396           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11397         continue;
11398
11399       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11400       if (fixup_has_matching_lo_p (l->fixp))
11401         continue;
11402
11403       /* Look through the fixups for this segment for a matching %lo.
11404          When we find one, move the %hi just in front of it.  We do
11405          this in two passes.  In the first pass, we try to find a
11406          unique %lo.  In the second pass, we permit multiple %hi
11407          relocs for a single %lo (this is a GNU extension).  */
11408       seginfo = seg_info (l->seg);
11409       for (pass = 0; pass < 2; pass++)
11410         {
11411           fixS *f, *prev;
11412
11413           prev = NULL;
11414           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11415             {
11416               /* Check whether this is a %lo fixup which matches l->fixp.  */
11417               if (f->fx_r_type == BFD_RELOC_LO16
11418                   && f->fx_addsy == l->fixp->fx_addsy
11419                   && f->fx_offset == l->fixp->fx_offset
11420                   && (pass == 1
11421                       || prev == NULL
11422                       || !reloc_needs_lo_p (prev->fx_r_type)
11423                       || !fixup_has_matching_lo_p (prev)))
11424                 {
11425                   fixS **pf;
11426
11427                   /* Move l->fixp before f.  */
11428                   for (pf = &seginfo->fix_root;
11429                        *pf != l->fixp;
11430                        pf = &(*pf)->fx_next)
11431                     assert (*pf != NULL);
11432
11433                   *pf = l->fixp->fx_next;
11434
11435                   l->fixp->fx_next = f;
11436                   if (prev == NULL)
11437                     seginfo->fix_root = l->fixp;
11438                   else
11439                     prev->fx_next = l->fixp;
11440
11441                   break;
11442                 }
11443
11444               prev = f;
11445             }
11446
11447           if (f != NULL)
11448             break;
11449
11450 #if 0 /* GCC code motion plus incomplete dead code elimination
11451          can leave a %hi without a %lo.  */
11452           if (pass == 1)
11453             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11454                            _("Unmatched %%hi reloc"));
11455 #endif
11456         }
11457     }
11458 }
11459
11460 /* When generating embedded PIC code we need to use a special
11461    relocation to represent the difference of two symbols in the .text
11462    section (switch tables use a difference of this sort).  See
11463    include/coff/mips.h for details.  This macro checks whether this
11464    fixup requires the special reloc.  */
11465 #define SWITCH_TABLE(fixp) \
11466   ((fixp)->fx_r_type == BFD_RELOC_32 \
11467    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11468    && (fixp)->fx_addsy != NULL \
11469    && (fixp)->fx_subsy != NULL \
11470    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11471    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11472
11473 /* When generating embedded PIC code we must keep all PC relative
11474    relocations, in case the linker has to relax a call.  We also need
11475    to keep relocations for switch table entries.
11476
11477    We may have combined relocations without symbols in the N32/N64 ABI.
11478    We have to prevent gas from dropping them.  */
11479
11480 int
11481 mips_force_relocation (fixp)
11482      fixS *fixp;
11483 {
11484   if (generic_force_reloc (fixp))
11485     return 1;
11486
11487   if (HAVE_NEWABI
11488       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11489       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11490           || fixp->fx_r_type == BFD_RELOC_HI16_S
11491           || fixp->fx_r_type == BFD_RELOC_LO16))
11492     return 1;
11493
11494   return (mips_pic == EMBEDDED_PIC
11495           && (fixp->fx_pcrel
11496               || SWITCH_TABLE (fixp)
11497               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11498               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11499 }
11500
11501 /* This hook is called before a fix is simplified.  We don't really
11502    decide whether to skip a fix here.  Rather, we turn global symbols
11503    used as branch targets into local symbols, such that they undergo
11504    simplification.  We can only do this if the symbol is defined and
11505    it is in the same section as the branch.  If this doesn't hold, we
11506    emit a better error message than just saying the relocation is not
11507    valid for the selected object format.
11508
11509    FIXP is the fix-up we're going to try to simplify, SEG is the
11510    segment in which the fix up occurs.  The return value should be
11511    non-zero to indicate the fix-up is valid for further
11512    simplifications.  */
11513
11514 int
11515 mips_validate_fix (fixP, seg)
11516      struct fix *fixP;
11517      asection *seg;
11518 {
11519   /* There's a lot of discussion on whether it should be possible to
11520      use R_MIPS_PC16 to represent branch relocations.  The outcome
11521      seems to be that it can, but gas/bfd are very broken in creating
11522      RELA relocations for this, so for now we only accept branches to
11523      symbols in the same section.  Anything else is of dubious value,
11524      since there's no guarantee that at link time the symbol would be
11525      in range.  Even for branches to local symbols this is arguably
11526      wrong, since it we assume the symbol is not going to be
11527      overridden, which should be possible per ELF library semantics,
11528      but then, there isn't a dynamic relocation that could be used to
11529      this effect, and the target would likely be out of range as well.
11530
11531      Unfortunately, it seems that there is too much code out there
11532      that relies on branches to symbols that are global to be resolved
11533      as if they were local, like the IRIX tools do, so we do it as
11534      well, but with a warning so that people are reminded to fix their
11535      code.  If we ever get back to using R_MIPS_PC16 for branch
11536      targets, this entire block should go away (and probably the
11537      whole function).  */
11538
11539   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11540       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11541             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11542            && mips_pic != EMBEDDED_PIC)
11543           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11544       && fixP->fx_addsy)
11545     {
11546       if (! S_IS_DEFINED (fixP->fx_addsy))
11547         {
11548           as_bad_where (fixP->fx_file, fixP->fx_line,
11549                         _("Cannot branch to undefined symbol."));
11550           /* Avoid any further errors about this fixup.  */
11551           fixP->fx_done = 1;
11552         }
11553       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11554         {
11555           as_bad_where (fixP->fx_file, fixP->fx_line,
11556                         _("Cannot branch to symbol in another section."));
11557           fixP->fx_done = 1;
11558         }
11559       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11560         {
11561           symbolS *sym = fixP->fx_addsy;
11562
11563           as_warn_where (fixP->fx_file, fixP->fx_line,
11564                          _("Pretending global symbol used as branch target is local."));
11565
11566           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11567                                           S_GET_SEGMENT (sym),
11568                                           S_GET_VALUE (sym),
11569                                           symbol_get_frag (sym));
11570           copy_symbol_attributes (fixP->fx_addsy, sym);
11571           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11572           assert (symbol_resolved_p (sym));
11573           symbol_mark_resolved (fixP->fx_addsy);
11574         }
11575     }
11576
11577   return 1;
11578 }
11579
11580 #ifdef OBJ_ELF
11581 static int
11582 mips_need_elf_addend_fixup (fixP)
11583      fixS *fixP;
11584 {
11585   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11586     return 1;
11587   if (mips_pic == EMBEDDED_PIC
11588       && S_IS_WEAK (fixP->fx_addsy))
11589     return 1;
11590   if (mips_pic != EMBEDDED_PIC
11591       && (S_IS_WEAK (fixP->fx_addsy)
11592           || S_IS_EXTERNAL (fixP->fx_addsy))
11593       && !S_IS_COMMON (fixP->fx_addsy))
11594     return 1;
11595   if (symbol_used_in_reloc_p (fixP->fx_addsy)
11596       && (((bfd_get_section_flags (stdoutput,
11597                                    S_GET_SEGMENT (fixP->fx_addsy))
11598             & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11599           || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11600                        ".gnu.linkonce",
11601                        sizeof (".gnu.linkonce") - 1)))
11602     return 1;
11603   return 0;
11604 }
11605 #endif
11606
11607 /* Apply a fixup to the object file.  */
11608
11609 void
11610 md_apply_fix3 (fixP, valP, seg)
11611      fixS *fixP;
11612      valueT *valP;
11613      segT seg ATTRIBUTE_UNUSED;
11614 {
11615   bfd_byte *buf;
11616   long insn;
11617   valueT value;
11618   static int previous_fx_r_type = 0;
11619
11620   /* FIXME: Maybe just return for all reloc types not listed below?
11621      Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11622   if (fixP->fx_r_type == BFD_RELOC_8)
11623       return;
11624
11625   assert (fixP->fx_size == 4
11626           || fixP->fx_r_type == BFD_RELOC_16
11627           || fixP->fx_r_type == BFD_RELOC_32
11628           || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11629           || fixP->fx_r_type == BFD_RELOC_HI16_S
11630           || fixP->fx_r_type == BFD_RELOC_LO16
11631           || fixP->fx_r_type == BFD_RELOC_GPREL16
11632           || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
11633           || fixP->fx_r_type == BFD_RELOC_GPREL32
11634           || fixP->fx_r_type == BFD_RELOC_64
11635           || fixP->fx_r_type == BFD_RELOC_CTOR
11636           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11637           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11638           || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11639           || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11640           || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11641           || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
11642           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11643           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11644           || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
11645
11646   value = *valP;
11647
11648   /* If we aren't adjusting this fixup to be against the section
11649      symbol, we need to adjust the value.  */
11650 #ifdef OBJ_ELF
11651   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11652     {
11653       if (mips_need_elf_addend_fixup (fixP))
11654         {
11655           reloc_howto_type *howto;
11656           valueT symval = S_GET_VALUE (fixP->fx_addsy);
11657
11658           value -= symval;
11659
11660           howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11661           if (value != 0 && howto && howto->partial_inplace
11662               && (! fixP->fx_pcrel || howto->pcrel_offset))
11663             {
11664               /* In this case, the bfd_install_relocation routine will
11665                  incorrectly add the symbol value back in.  We just want
11666                  the addend to appear in the object file.
11667
11668                  howto->pcrel_offset is added for R_MIPS_PC16, which is
11669                  generated for code like
11670
11671                         globl g1 .text
11672                         .text
11673                         .space 20
11674                  g1:
11675                  x:
11676                         bal g1
11677                */
11678               value -= symval;
11679
11680               /* Make sure the addend is still non-zero.  If it became zero
11681                  after the last operation, set it to a spurious value and
11682                  subtract the same value from the object file's contents.  */
11683               if (value == 0)
11684                 {
11685                   value = 8;
11686
11687                   /* The in-place addends for LO16 relocations are signed;
11688                      leave the matching HI16 in-place addends as zero.  */
11689                   if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11690                     {
11691                       bfd_vma contents, mask, field;
11692
11693                       contents = bfd_get_bits (fixP->fx_frag->fr_literal
11694                                                + fixP->fx_where,
11695                                                fixP->fx_size * 8,
11696                                                target_big_endian);
11697
11698                       /* MASK has bits set where the relocation should go.
11699                          FIELD is -value, shifted into the appropriate place
11700                          for this relocation.  */
11701                       mask = 1 << (howto->bitsize - 1);
11702                       mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11703                       field = (-value >> howto->rightshift) << howto->bitpos;
11704
11705                       bfd_put_bits ((field & mask) | (contents & ~mask),
11706                                     fixP->fx_frag->fr_literal + fixP->fx_where,
11707                                     fixP->fx_size * 8,
11708                                     target_big_endian);
11709                     }
11710                 }
11711             }
11712         }
11713
11714       /* This code was generated using trial and error and so is
11715          fragile and not trustworthy.  If you change it, you should
11716          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11717          they still pass.  */
11718       if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11719         {
11720           value += fixP->fx_frag->fr_address + fixP->fx_where;
11721
11722           /* BFD's REL handling, for MIPS, is _very_ weird.
11723              This gives the right results, but it can't possibly
11724              be the way things are supposed to work.  */
11725           if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11726               || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11727             value += fixP->fx_frag->fr_address + fixP->fx_where;
11728         }
11729     }
11730 #endif
11731
11732   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc.  */
11733
11734   /* We are not done if this is a composite relocation to set up gp.  */
11735   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11736       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11737            || (fixP->fx_r_type == BFD_RELOC_64
11738                && (previous_fx_r_type == BFD_RELOC_GPREL32
11739                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11740            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11741                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11742                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11743     fixP->fx_done = 1;
11744   previous_fx_r_type = fixP->fx_r_type;
11745
11746   switch (fixP->fx_r_type)
11747     {
11748     case BFD_RELOC_MIPS_JMP:
11749     case BFD_RELOC_MIPS_SHIFT5:
11750     case BFD_RELOC_MIPS_SHIFT6:
11751     case BFD_RELOC_MIPS_GOT_DISP:
11752     case BFD_RELOC_MIPS_GOT_PAGE:
11753     case BFD_RELOC_MIPS_GOT_OFST:
11754     case BFD_RELOC_MIPS_SUB:
11755     case BFD_RELOC_MIPS_INSERT_A:
11756     case BFD_RELOC_MIPS_INSERT_B:
11757     case BFD_RELOC_MIPS_DELETE:
11758     case BFD_RELOC_MIPS_HIGHEST:
11759     case BFD_RELOC_MIPS_HIGHER:
11760     case BFD_RELOC_MIPS_SCN_DISP:
11761     case BFD_RELOC_MIPS_REL16:
11762     case BFD_RELOC_MIPS_RELGOT:
11763     case BFD_RELOC_MIPS_JALR:
11764     case BFD_RELOC_HI16:
11765     case BFD_RELOC_HI16_S:
11766     case BFD_RELOC_GPREL16:
11767     case BFD_RELOC_MIPS_LITERAL:
11768     case BFD_RELOC_MIPS_CALL16:
11769     case BFD_RELOC_MIPS_GOT16:
11770     case BFD_RELOC_GPREL32:
11771     case BFD_RELOC_MIPS_GOT_HI16:
11772     case BFD_RELOC_MIPS_GOT_LO16:
11773     case BFD_RELOC_MIPS_CALL_HI16:
11774     case BFD_RELOC_MIPS_CALL_LO16:
11775     case BFD_RELOC_MIPS16_GPREL:
11776       if (fixP->fx_pcrel)
11777         as_bad_where (fixP->fx_file, fixP->fx_line,
11778                       _("Invalid PC relative reloc"));
11779       /* Nothing needed to do. The value comes from the reloc entry */
11780       break;
11781
11782     case BFD_RELOC_MIPS16_JMP:
11783       /* We currently always generate a reloc against a symbol, which
11784          means that we don't want an addend even if the symbol is
11785          defined.  */
11786       fixP->fx_addnumber = 0;
11787       break;
11788
11789     case BFD_RELOC_PCREL_HI16_S:
11790       /* The addend for this is tricky if it is internal, so we just
11791          do everything here rather than in bfd_install_relocation.  */
11792       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11793           && !fixP->fx_done
11794           && value != 0)
11795         break;
11796       if (fixP->fx_addsy
11797           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11798         {
11799           /* For an external symbol adjust by the address to make it
11800              pcrel_offset.  We use the address of the RELLO reloc
11801              which follows this one.  */
11802           value += (fixP->fx_next->fx_frag->fr_address
11803                     + fixP->fx_next->fx_where);
11804         }
11805       value = ((value + 0x8000) >> 16) & 0xffff;
11806       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11807       if (target_big_endian)
11808         buf += 2;
11809       md_number_to_chars ((char *) buf, value, 2);
11810       break;
11811
11812     case BFD_RELOC_PCREL_LO16:
11813       /* The addend for this is tricky if it is internal, so we just
11814          do everything here rather than in bfd_install_relocation.  */
11815       if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11816           && !fixP->fx_done
11817           && value != 0)
11818         break;
11819       if (fixP->fx_addsy
11820           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11821         value += fixP->fx_frag->fr_address + fixP->fx_where;
11822       buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11823       if (target_big_endian)
11824         buf += 2;
11825       md_number_to_chars ((char *) buf, value, 2);
11826       break;
11827
11828     case BFD_RELOC_64:
11829       /* This is handled like BFD_RELOC_32, but we output a sign
11830          extended value if we are only 32 bits.  */
11831       if (fixP->fx_done
11832           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11833         {
11834           if (8 <= sizeof (valueT))
11835             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11836                                 value, 8);
11837           else
11838             {
11839               long w1, w2;
11840               long hiv;
11841
11842               w1 = w2 = fixP->fx_where;
11843               if (target_big_endian)
11844                 w1 += 4;
11845               else
11846                 w2 += 4;
11847               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11848               if ((value & 0x80000000) != 0)
11849                 hiv = 0xffffffff;
11850               else
11851                 hiv = 0;
11852               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11853             }
11854         }
11855       break;
11856
11857     case BFD_RELOC_RVA:
11858     case BFD_RELOC_32:
11859       /* If we are deleting this reloc entry, we must fill in the
11860          value now.  This can happen if we have a .word which is not
11861          resolved when it appears but is later defined.  We also need
11862          to fill in the value if this is an embedded PIC switch table
11863          entry.  */
11864       if (fixP->fx_done
11865           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11866         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11867                             value, 4);
11868       break;
11869
11870     case BFD_RELOC_16:
11871       /* If we are deleting this reloc entry, we must fill in the
11872          value now.  */
11873       assert (fixP->fx_size == 2);
11874       if (fixP->fx_done)
11875         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11876                             value, 2);
11877       break;
11878
11879     case BFD_RELOC_LO16:
11880       /* When handling an embedded PIC switch statement, we can wind
11881          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11882       if (fixP->fx_done)
11883         {
11884           if (value + 0x8000 > 0xffff)
11885             as_bad_where (fixP->fx_file, fixP->fx_line,
11886                           _("relocation overflow"));
11887           buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
11888           if (target_big_endian)
11889             buf += 2;
11890           md_number_to_chars ((char *) buf, value, 2);
11891         }
11892       break;
11893
11894     case BFD_RELOC_16_PCREL_S2:
11895       if ((value & 0x3) != 0)
11896         as_bad_where (fixP->fx_file, fixP->fx_line,
11897                       _("Branch to odd address (%lx)"), (long) value);
11898
11899       /*
11900        * We need to save the bits in the instruction since fixup_segment()
11901        * might be deleting the relocation entry (i.e., a branch within
11902        * the current segment).
11903        */
11904       if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
11905         break;
11906       /* If 'value' is zero, the remaining reloc code won't actually
11907          do the store, so it must be done here.  This is probably
11908          a bug somewhere.  */
11909       if (!fixP->fx_done
11910           && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11911               || fixP->fx_addsy == NULL                 /* ??? */
11912               || ! S_IS_DEFINED (fixP->fx_addsy)))
11913         value -= fixP->fx_frag->fr_address + fixP->fx_where;
11914
11915       value = (offsetT) value >> 2;
11916
11917       /* update old instruction data */
11918       buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
11919       if (target_big_endian)
11920         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11921       else
11922         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11923
11924       if (value + 0x8000 <= 0xffff)
11925         insn |= value & 0xffff;
11926       else
11927         {
11928           /* The branch offset is too large.  If this is an
11929              unconditional branch, and we are not generating PIC code,
11930              we can convert it to an absolute jump instruction.  */
11931           if (mips_pic == NO_PIC
11932               && fixP->fx_done
11933               && fixP->fx_frag->fr_address >= text_section->vma
11934               && (fixP->fx_frag->fr_address
11935                   < text_section->vma + text_section->_raw_size)
11936               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
11937                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
11938                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11939             {
11940               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
11941                 insn = 0x0c000000;      /* jal */
11942               else
11943                 insn = 0x08000000;      /* j */
11944               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11945               fixP->fx_done = 0;
11946               fixP->fx_addsy = section_symbol (text_section);
11947               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11948             }
11949           else
11950             {
11951               /* If we got here, we have branch-relaxation disabled,
11952                  and there's nothing we can do to fix this instruction
11953                  without turning it into a longer sequence.  */
11954               as_bad_where (fixP->fx_file, fixP->fx_line,
11955                             _("Branch out of range"));
11956             }
11957         }
11958
11959       md_number_to_chars ((char *) buf, (valueT) insn, 4);
11960       break;
11961
11962     case BFD_RELOC_VTABLE_INHERIT:
11963       fixP->fx_done = 0;
11964       if (fixP->fx_addsy
11965           && !S_IS_DEFINED (fixP->fx_addsy)
11966           && !S_IS_WEAK (fixP->fx_addsy))
11967         S_SET_WEAK (fixP->fx_addsy);
11968       break;
11969
11970     case BFD_RELOC_VTABLE_ENTRY:
11971       fixP->fx_done = 0;
11972       break;
11973
11974     default:
11975       internalError ();
11976     }
11977 }
11978
11979 #if 0
11980 void
11981 printInsn (oc)
11982      unsigned long oc;
11983 {
11984   const struct mips_opcode *p;
11985   int treg, sreg, dreg, shamt;
11986   short imm;
11987   const char *args;
11988   int i;
11989
11990   for (i = 0; i < NUMOPCODES; ++i)
11991     {
11992       p = &mips_opcodes[i];
11993       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11994         {
11995           printf ("%08lx %s\t", oc, p->name);
11996           treg = (oc >> 16) & 0x1f;
11997           sreg = (oc >> 21) & 0x1f;
11998           dreg = (oc >> 11) & 0x1f;
11999           shamt = (oc >> 6) & 0x1f;
12000           imm = oc;
12001           for (args = p->args;; ++args)
12002             {
12003               switch (*args)
12004                 {
12005                 case '\0':
12006                   printf ("\n");
12007                   break;
12008
12009                 case ',':
12010                 case '(':
12011                 case ')':
12012                   printf ("%c", *args);
12013                   continue;
12014
12015                 case 'r':
12016                   assert (treg == sreg);
12017                   printf ("$%d,$%d", treg, sreg);
12018                   continue;
12019
12020                 case 'd':
12021                 case 'G':
12022                   printf ("$%d", dreg);
12023                   continue;
12024
12025                 case 't':
12026                 case 'E':
12027                   printf ("$%d", treg);
12028                   continue;
12029
12030                 case 'k':
12031                   printf ("0x%x", treg);
12032                   continue;
12033
12034                 case 'b':
12035                 case 's':
12036                   printf ("$%d", sreg);
12037                   continue;
12038
12039                 case 'a':
12040                   printf ("0x%08lx", oc & 0x1ffffff);
12041                   continue;
12042
12043                 case 'i':
12044                 case 'j':
12045                 case 'o':
12046                 case 'u':
12047                   printf ("%d", imm);
12048                   continue;
12049
12050                 case '<':
12051                 case '>':
12052                   printf ("$%d", shamt);
12053                   continue;
12054
12055                 default:
12056                   internalError ();
12057                 }
12058               break;
12059             }
12060           return;
12061         }
12062     }
12063   printf (_("%08lx  UNDEFINED\n"), oc);
12064 }
12065 #endif
12066
12067 static symbolS *
12068 get_symbol ()
12069 {
12070   int c;
12071   char *name;
12072   symbolS *p;
12073
12074   name = input_line_pointer;
12075   c = get_symbol_end ();
12076   p = (symbolS *) symbol_find_or_make (name);
12077   *input_line_pointer = c;
12078   return p;
12079 }
12080
12081 /* Align the current frag to a given power of two.  The MIPS assembler
12082    also automatically adjusts any preceding label.  */
12083
12084 static void
12085 mips_align (to, fill, label)
12086      int to;
12087      int fill;
12088      symbolS *label;
12089 {
12090   mips_emit_delays (FALSE);
12091   frag_align (to, fill, 0);
12092   record_alignment (now_seg, to);
12093   if (label != NULL)
12094     {
12095       assert (S_GET_SEGMENT (label) == now_seg);
12096       symbol_set_frag (label, frag_now);
12097       S_SET_VALUE (label, (valueT) frag_now_fix ());
12098     }
12099 }
12100
12101 /* Align to a given power of two.  .align 0 turns off the automatic
12102    alignment used by the data creating pseudo-ops.  */
12103
12104 static void
12105 s_align (x)
12106      int x ATTRIBUTE_UNUSED;
12107 {
12108   register int temp;
12109   register long temp_fill;
12110   long max_alignment = 15;
12111
12112   /*
12113
12114     o  Note that the assembler pulls down any immediately preceeding label
12115        to the aligned address.
12116     o  It's not documented but auto alignment is reinstated by
12117        a .align pseudo instruction.
12118     o  Note also that after auto alignment is turned off the mips assembler
12119        issues an error on attempt to assemble an improperly aligned data item.
12120        We don't.
12121
12122     */
12123
12124   temp = get_absolute_expression ();
12125   if (temp > max_alignment)
12126     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12127   else if (temp < 0)
12128     {
12129       as_warn (_("Alignment negative: 0 assumed."));
12130       temp = 0;
12131     }
12132   if (*input_line_pointer == ',')
12133     {
12134       ++input_line_pointer;
12135       temp_fill = get_absolute_expression ();
12136     }
12137   else
12138     temp_fill = 0;
12139   if (temp)
12140     {
12141       auto_align = 1;
12142       mips_align (temp, (int) temp_fill,
12143                   insn_labels != NULL ? insn_labels->label : NULL);
12144     }
12145   else
12146     {
12147       auto_align = 0;
12148     }
12149
12150   demand_empty_rest_of_line ();
12151 }
12152
12153 void
12154 mips_flush_pending_output ()
12155 {
12156   mips_emit_delays (FALSE);
12157   mips_clear_insn_labels ();
12158 }
12159
12160 static void
12161 s_change_sec (sec)
12162      int sec;
12163 {
12164   segT seg;
12165
12166   /* When generating embedded PIC code, we only use the .text, .lit8,
12167      .sdata and .sbss sections.  We change the .data and .rdata
12168      pseudo-ops to use .sdata.  */
12169   if (mips_pic == EMBEDDED_PIC
12170       && (sec == 'd' || sec == 'r'))
12171     sec = 's';
12172
12173 #ifdef OBJ_ELF
12174   /* The ELF backend needs to know that we are changing sections, so
12175      that .previous works correctly.  We could do something like check
12176      for an obj_section_change_hook macro, but that might be confusing
12177      as it would not be appropriate to use it in the section changing
12178      functions in read.c, since obj-elf.c intercepts those.  FIXME:
12179      This should be cleaner, somehow.  */
12180   obj_elf_section_change_hook ();
12181 #endif
12182
12183   mips_emit_delays (FALSE);
12184   switch (sec)
12185     {
12186     case 't':
12187       s_text (0);
12188       break;
12189     case 'd':
12190       s_data (0);
12191       break;
12192     case 'b':
12193       subseg_set (bss_section, (subsegT) get_absolute_expression ());
12194       demand_empty_rest_of_line ();
12195       break;
12196
12197     case 'r':
12198       if (USE_GLOBAL_POINTER_OPT)
12199         {
12200           seg = subseg_new (RDATA_SECTION_NAME,
12201                             (subsegT) get_absolute_expression ());
12202           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12203             {
12204               bfd_set_section_flags (stdoutput, seg,
12205                                      (SEC_ALLOC
12206                                       | SEC_LOAD
12207                                       | SEC_READONLY
12208                                       | SEC_RELOC
12209                                       | SEC_DATA));
12210               if (strcmp (TARGET_OS, "elf") != 0)
12211                 record_alignment (seg, 4);
12212             }
12213           demand_empty_rest_of_line ();
12214         }
12215       else
12216         {
12217           as_bad (_("No read only data section in this object file format"));
12218           demand_empty_rest_of_line ();
12219           return;
12220         }
12221       break;
12222
12223     case 's':
12224       if (USE_GLOBAL_POINTER_OPT)
12225         {
12226           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12227           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12228             {
12229               bfd_set_section_flags (stdoutput, seg,
12230                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
12231                                      | SEC_DATA);
12232               if (strcmp (TARGET_OS, "elf") != 0)
12233                 record_alignment (seg, 4);
12234             }
12235           demand_empty_rest_of_line ();
12236           break;
12237         }
12238       else
12239         {
12240           as_bad (_("Global pointers not supported; recompile -G 0"));
12241           demand_empty_rest_of_line ();
12242           return;
12243         }
12244     }
12245
12246   auto_align = 1;
12247 }
12248
12249 void
12250 s_change_section (ignore)
12251      int ignore ATTRIBUTE_UNUSED;
12252 {
12253 #ifdef OBJ_ELF
12254   char *section_name;
12255   char c;
12256   char next_c = 0;
12257   int section_type;
12258   int section_flag;
12259   int section_entry_size;
12260   int section_alignment;
12261
12262   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12263     return;
12264
12265   section_name = input_line_pointer;
12266   c = get_symbol_end ();
12267   if (c)
12268     next_c = *(input_line_pointer + 1);
12269
12270   /* Do we have .section Name<,"flags">?  */
12271   if (c != ',' || (c == ',' && next_c == '"'))
12272     {
12273       /* just after name is now '\0'.  */
12274       *input_line_pointer = c;
12275       input_line_pointer = section_name;
12276       obj_elf_section (ignore);
12277       return;
12278     }
12279   input_line_pointer++;
12280
12281   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
12282   if (c == ',')
12283     section_type = get_absolute_expression ();
12284   else
12285     section_type = 0;
12286   if (*input_line_pointer++ == ',')
12287     section_flag = get_absolute_expression ();
12288   else
12289     section_flag = 0;
12290   if (*input_line_pointer++ == ',')
12291     section_entry_size = get_absolute_expression ();
12292   else
12293     section_entry_size = 0;
12294   if (*input_line_pointer++ == ',')
12295     section_alignment = get_absolute_expression ();
12296   else
12297     section_alignment = 0;
12298
12299   section_name = xstrdup (section_name);
12300
12301   obj_elf_change_section (section_name, section_type, section_flag,
12302                           section_entry_size, 0, 0, 0);
12303
12304   if (now_seg->name != section_name)
12305     free (section_name);
12306 #endif /* OBJ_ELF */
12307 }
12308
12309 void
12310 mips_enable_auto_align ()
12311 {
12312   auto_align = 1;
12313 }
12314
12315 static void
12316 s_cons (log_size)
12317      int log_size;
12318 {
12319   symbolS *label;
12320
12321   label = insn_labels != NULL ? insn_labels->label : NULL;
12322   mips_emit_delays (FALSE);
12323   if (log_size > 0 && auto_align)
12324     mips_align (log_size, 0, label);
12325   mips_clear_insn_labels ();
12326   cons (1 << log_size);
12327 }
12328
12329 static void
12330 s_float_cons (type)
12331      int type;
12332 {
12333   symbolS *label;
12334
12335   label = insn_labels != NULL ? insn_labels->label : NULL;
12336
12337   mips_emit_delays (FALSE);
12338
12339   if (auto_align)
12340     {
12341       if (type == 'd')
12342         mips_align (3, 0, label);
12343       else
12344         mips_align (2, 0, label);
12345     }
12346
12347   mips_clear_insn_labels ();
12348
12349   float_cons (type);
12350 }
12351
12352 /* Handle .globl.  We need to override it because on Irix 5 you are
12353    permitted to say
12354        .globl foo .text
12355    where foo is an undefined symbol, to mean that foo should be
12356    considered to be the address of a function.  */
12357
12358 static void
12359 s_mips_globl (x)
12360      int x ATTRIBUTE_UNUSED;
12361 {
12362   char *name;
12363   int c;
12364   symbolS *symbolP;
12365   flagword flag;
12366
12367   name = input_line_pointer;
12368   c = get_symbol_end ();
12369   symbolP = symbol_find_or_make (name);
12370   *input_line_pointer = c;
12371   SKIP_WHITESPACE ();
12372
12373   /* On Irix 5, every global symbol that is not explicitly labelled as
12374      being a function is apparently labelled as being an object.  */
12375   flag = BSF_OBJECT;
12376
12377   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12378     {
12379       char *secname;
12380       asection *sec;
12381
12382       secname = input_line_pointer;
12383       c = get_symbol_end ();
12384       sec = bfd_get_section_by_name (stdoutput, secname);
12385       if (sec == NULL)
12386         as_bad (_("%s: no such section"), secname);
12387       *input_line_pointer = c;
12388
12389       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12390         flag = BSF_FUNCTION;
12391     }
12392
12393   symbol_get_bfdsym (symbolP)->flags |= flag;
12394
12395   S_SET_EXTERNAL (symbolP);
12396   demand_empty_rest_of_line ();
12397 }
12398
12399 static void
12400 s_option (x)
12401      int x ATTRIBUTE_UNUSED;
12402 {
12403   char *opt;
12404   char c;
12405
12406   opt = input_line_pointer;
12407   c = get_symbol_end ();
12408
12409   if (*opt == 'O')
12410     {
12411       /* FIXME: What does this mean?  */
12412     }
12413   else if (strncmp (opt, "pic", 3) == 0)
12414     {
12415       int i;
12416
12417       i = atoi (opt + 3);
12418       if (i == 0)
12419         mips_pic = NO_PIC;
12420       else if (i == 2)
12421         mips_pic = SVR4_PIC;
12422       else
12423         as_bad (_(".option pic%d not supported"), i);
12424
12425       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12426         {
12427           if (g_switch_seen && g_switch_value != 0)
12428             as_warn (_("-G may not be used with SVR4 PIC code"));
12429           g_switch_value = 0;
12430           bfd_set_gp_size (stdoutput, 0);
12431         }
12432     }
12433   else
12434     as_warn (_("Unrecognized option \"%s\""), opt);
12435
12436   *input_line_pointer = c;
12437   demand_empty_rest_of_line ();
12438 }
12439
12440 /* This structure is used to hold a stack of .set values.  */
12441
12442 struct mips_option_stack
12443 {
12444   struct mips_option_stack *next;
12445   struct mips_set_options options;
12446 };
12447
12448 static struct mips_option_stack *mips_opts_stack;
12449
12450 /* Handle the .set pseudo-op.  */
12451
12452 static void
12453 s_mipsset (x)
12454      int x ATTRIBUTE_UNUSED;
12455 {
12456   char *name = input_line_pointer, ch;
12457
12458   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12459     ++input_line_pointer;
12460   ch = *input_line_pointer;
12461   *input_line_pointer = '\0';
12462
12463   if (strcmp (name, "reorder") == 0)
12464     {
12465       if (mips_opts.noreorder && prev_nop_frag != NULL)
12466         {
12467           /* If we still have pending nops, we can discard them.  The
12468              usual nop handling will insert any that are still
12469              needed.  */
12470           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12471                                     * (mips_opts.mips16 ? 2 : 4));
12472           prev_nop_frag = NULL;
12473         }
12474       mips_opts.noreorder = 0;
12475     }
12476   else if (strcmp (name, "noreorder") == 0)
12477     {
12478       mips_emit_delays (TRUE);
12479       mips_opts.noreorder = 1;
12480       mips_any_noreorder = 1;
12481     }
12482   else if (strcmp (name, "at") == 0)
12483     {
12484       mips_opts.noat = 0;
12485     }
12486   else if (strcmp (name, "noat") == 0)
12487     {
12488       mips_opts.noat = 1;
12489     }
12490   else if (strcmp (name, "macro") == 0)
12491     {
12492       mips_opts.warn_about_macros = 0;
12493     }
12494   else if (strcmp (name, "nomacro") == 0)
12495     {
12496       if (mips_opts.noreorder == 0)
12497         as_bad (_("`noreorder' must be set before `nomacro'"));
12498       mips_opts.warn_about_macros = 1;
12499     }
12500   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12501     {
12502       mips_opts.nomove = 0;
12503     }
12504   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12505     {
12506       mips_opts.nomove = 1;
12507     }
12508   else if (strcmp (name, "bopt") == 0)
12509     {
12510       mips_opts.nobopt = 0;
12511     }
12512   else if (strcmp (name, "nobopt") == 0)
12513     {
12514       mips_opts.nobopt = 1;
12515     }
12516   else if (strcmp (name, "mips16") == 0
12517            || strcmp (name, "MIPS-16") == 0)
12518     mips_opts.mips16 = 1;
12519   else if (strcmp (name, "nomips16") == 0
12520            || strcmp (name, "noMIPS-16") == 0)
12521     mips_opts.mips16 = 0;
12522   else if (strcmp (name, "mips3d") == 0)
12523     mips_opts.ase_mips3d = 1;
12524   else if (strcmp (name, "nomips3d") == 0)
12525     mips_opts.ase_mips3d = 0;
12526   else if (strcmp (name, "mdmx") == 0)
12527     mips_opts.ase_mdmx = 1;
12528   else if (strcmp (name, "nomdmx") == 0)
12529     mips_opts.ase_mdmx = 0;
12530   else if (strncmp (name, "mips", 4) == 0)
12531     {
12532       int reset = 0;
12533
12534       /* Permit the user to change the ISA on the fly.  Needless to
12535          say, misuse can cause serious problems.  */
12536       if (strcmp (name, "mips0") == 0)
12537         {
12538           reset = 1;
12539           mips_opts.isa = file_mips_isa;
12540         }
12541       else if (strcmp (name, "mips1") == 0)
12542         mips_opts.isa = ISA_MIPS1;
12543       else if (strcmp (name, "mips2") == 0)
12544         mips_opts.isa = ISA_MIPS2;
12545       else if (strcmp (name, "mips3") == 0)
12546         mips_opts.isa = ISA_MIPS3;
12547       else if (strcmp (name, "mips4") == 0)
12548         mips_opts.isa = ISA_MIPS4;
12549       else if (strcmp (name, "mips5") == 0)
12550         mips_opts.isa = ISA_MIPS5;
12551       else if (strcmp (name, "mips32") == 0)
12552         mips_opts.isa = ISA_MIPS32;
12553       else if (strcmp (name, "mips32r2") == 0)
12554         mips_opts.isa = ISA_MIPS32R2;
12555       else if (strcmp (name, "mips64") == 0)
12556         mips_opts.isa = ISA_MIPS64;
12557       else
12558         as_bad (_("unknown ISA level %s"), name + 4);
12559
12560       switch (mips_opts.isa)
12561         {
12562         case  0:
12563           break;
12564         case ISA_MIPS1:
12565         case ISA_MIPS2:
12566         case ISA_MIPS32:
12567         case ISA_MIPS32R2:
12568           mips_opts.gp32 = 1;
12569           mips_opts.fp32 = 1;
12570           break;
12571         case ISA_MIPS3:
12572         case ISA_MIPS4:
12573         case ISA_MIPS5:
12574         case ISA_MIPS64:
12575           mips_opts.gp32 = 0;
12576           mips_opts.fp32 = 0;
12577           break;
12578         default:
12579           as_bad (_("unknown ISA level %s"), name + 4);
12580           break;
12581         }
12582       if (reset)
12583         {
12584           mips_opts.gp32 = file_mips_gp32;
12585           mips_opts.fp32 = file_mips_fp32;
12586         }
12587     }
12588   else if (strcmp (name, "autoextend") == 0)
12589     mips_opts.noautoextend = 0;
12590   else if (strcmp (name, "noautoextend") == 0)
12591     mips_opts.noautoextend = 1;
12592   else if (strcmp (name, "push") == 0)
12593     {
12594       struct mips_option_stack *s;
12595
12596       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12597       s->next = mips_opts_stack;
12598       s->options = mips_opts;
12599       mips_opts_stack = s;
12600     }
12601   else if (strcmp (name, "pop") == 0)
12602     {
12603       struct mips_option_stack *s;
12604
12605       s = mips_opts_stack;
12606       if (s == NULL)
12607         as_bad (_(".set pop with no .set push"));
12608       else
12609         {
12610           /* If we're changing the reorder mode we need to handle
12611              delay slots correctly.  */
12612           if (s->options.noreorder && ! mips_opts.noreorder)
12613             mips_emit_delays (TRUE);
12614           else if (! s->options.noreorder && mips_opts.noreorder)
12615             {
12616               if (prev_nop_frag != NULL)
12617                 {
12618                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12619                                             * (mips_opts.mips16 ? 2 : 4));
12620                   prev_nop_frag = NULL;
12621                 }
12622             }
12623
12624           mips_opts = s->options;
12625           mips_opts_stack = s->next;
12626           free (s);
12627         }
12628     }
12629   else
12630     {
12631       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12632     }
12633   *input_line_pointer = ch;
12634   demand_empty_rest_of_line ();
12635 }
12636
12637 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12638    .option pic2.  It means to generate SVR4 PIC calls.  */
12639
12640 static void
12641 s_abicalls (ignore)
12642      int ignore ATTRIBUTE_UNUSED;
12643 {
12644   mips_pic = SVR4_PIC;
12645   if (USE_GLOBAL_POINTER_OPT)
12646     {
12647       if (g_switch_seen && g_switch_value != 0)
12648         as_warn (_("-G may not be used with SVR4 PIC code"));
12649       g_switch_value = 0;
12650     }
12651   bfd_set_gp_size (stdoutput, 0);
12652   demand_empty_rest_of_line ();
12653 }
12654
12655 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12656    PIC code.  It sets the $gp register for the function based on the
12657    function address, which is in the register named in the argument.
12658    This uses a relocation against _gp_disp, which is handled specially
12659    by the linker.  The result is:
12660         lui     $gp,%hi(_gp_disp)
12661         addiu   $gp,$gp,%lo(_gp_disp)
12662         addu    $gp,$gp,.cpload argument
12663    The .cpload argument is normally $25 == $t9.  */
12664
12665 static void
12666 s_cpload (ignore)
12667      int ignore ATTRIBUTE_UNUSED;
12668 {
12669   expressionS ex;
12670   int icnt = 0;
12671
12672   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12673      .cpload is ignored.  */
12674   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12675     {
12676       s_ignore (0);
12677       return;
12678     }
12679
12680   /* .cpload should be in a .set noreorder section.  */
12681   if (mips_opts.noreorder == 0)
12682     as_warn (_(".cpload not in noreorder section"));
12683
12684   ex.X_op = O_symbol;
12685   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12686   ex.X_op_symbol = NULL;
12687   ex.X_add_number = 0;
12688
12689   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12690   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12691
12692   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12693   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12694                mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12695
12696   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12697                mips_gp_register, mips_gp_register, tc_get_register (0));
12698
12699   demand_empty_rest_of_line ();
12700 }
12701
12702 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12703      .cpsetup $reg1, offset|$reg2, label
12704
12705    If offset is given, this results in:
12706      sd         $gp, offset($sp)
12707      lui        $gp, %hi(%neg(%gp_rel(label)))
12708      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12709      daddu      $gp, $gp, $reg1
12710
12711    If $reg2 is given, this results in:
12712      daddu      $reg2, $gp, $0
12713      lui        $gp, %hi(%neg(%gp_rel(label)))
12714      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12715      daddu      $gp, $gp, $reg1
12716    $reg1 is normally $25 == $t9.  */
12717 static void
12718 s_cpsetup (ignore)
12719      int ignore ATTRIBUTE_UNUSED;
12720 {
12721   expressionS ex_off;
12722   expressionS ex_sym;
12723   int reg1;
12724   int icnt = 0;
12725   char *f;
12726
12727   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12728      We also need NewABI support.  */
12729   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12730     {
12731       s_ignore (0);
12732       return;
12733     }
12734
12735   reg1 = tc_get_register (0);
12736   SKIP_WHITESPACE ();
12737   if (*input_line_pointer != ',')
12738     {
12739       as_bad (_("missing argument separator ',' for .cpsetup"));
12740       return;
12741     }
12742   else
12743     ++input_line_pointer;
12744   SKIP_WHITESPACE ();
12745   if (*input_line_pointer == '$')
12746     {
12747       mips_cpreturn_register = tc_get_register (0);
12748       mips_cpreturn_offset = -1;
12749     }
12750   else
12751     {
12752       mips_cpreturn_offset = get_absolute_expression ();
12753       mips_cpreturn_register = -1;
12754     }
12755   SKIP_WHITESPACE ();
12756   if (*input_line_pointer != ',')
12757     {
12758       as_bad (_("missing argument separator ',' for .cpsetup"));
12759       return;
12760     }
12761   else
12762     ++input_line_pointer;
12763   SKIP_WHITESPACE ();
12764   expression (&ex_sym);
12765
12766   if (mips_cpreturn_register == -1)
12767     {
12768       ex_off.X_op = O_constant;
12769       ex_off.X_add_symbol = NULL;
12770       ex_off.X_op_symbol = NULL;
12771       ex_off.X_add_number = mips_cpreturn_offset;
12772
12773       macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12774                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12775     }
12776   else
12777     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12778                  "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12779
12780   /* Ensure there's room for the next two instructions, so that `f'
12781      doesn't end up with an address in the wrong frag.  */
12782   frag_grow (8);
12783   f = frag_more (0);
12784   macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12785                (int) BFD_RELOC_GPREL16);
12786   fix_new (frag_now, f - frag_now->fr_literal,
12787            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12788   fix_new (frag_now, f - frag_now->fr_literal,
12789            0, NULL, 0, 0, BFD_RELOC_HI16_S);
12790
12791   f = frag_more (0);
12792   macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12793                mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12794   fix_new (frag_now, f - frag_now->fr_literal,
12795            0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12796   fix_new (frag_now, f - frag_now->fr_literal,
12797            0, NULL, 0, 0, BFD_RELOC_LO16);
12798
12799   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12800                HAVE_64BIT_ADDRESSES ? "daddu" : "add", "d,v,t",
12801                mips_gp_register, mips_gp_register, reg1);
12802
12803   demand_empty_rest_of_line ();
12804 }
12805
12806 static void
12807 s_cplocal (ignore)
12808      int ignore ATTRIBUTE_UNUSED;
12809 {
12810   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12811    .cplocal is ignored.  */
12812   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12813     {
12814       s_ignore (0);
12815       return;
12816     }
12817
12818   mips_gp_register = tc_get_register (0);
12819   demand_empty_rest_of_line ();
12820 }
12821
12822 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12823    offset from $sp.  The offset is remembered, and after making a PIC
12824    call $gp is restored from that location.  */
12825
12826 static void
12827 s_cprestore (ignore)
12828      int ignore ATTRIBUTE_UNUSED;
12829 {
12830   expressionS ex;
12831   int icnt = 0;
12832
12833   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12834      .cprestore is ignored.  */
12835   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12836     {
12837       s_ignore (0);
12838       return;
12839     }
12840
12841   mips_cprestore_offset = get_absolute_expression ();
12842   mips_cprestore_valid = 1;
12843
12844   ex.X_op = O_constant;
12845   ex.X_add_symbol = NULL;
12846   ex.X_op_symbol = NULL;
12847   ex.X_add_number = mips_cprestore_offset;
12848
12849   macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12850                                 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12851                                 mips_gp_register, SP);
12852
12853   demand_empty_rest_of_line ();
12854 }
12855
12856 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12857    was given in the preceeding .gpsetup, it results in:
12858      ld         $gp, offset($sp)
12859
12860    If a register $reg2 was given there, it results in:
12861      daddiu     $gp, $gp, $reg2
12862  */
12863 static void
12864 s_cpreturn (ignore)
12865      int ignore ATTRIBUTE_UNUSED;
12866 {
12867   expressionS ex;
12868   int icnt = 0;
12869
12870   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12871      We also need NewABI support.  */
12872   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12873     {
12874       s_ignore (0);
12875       return;
12876     }
12877
12878   if (mips_cpreturn_register == -1)
12879     {
12880       ex.X_op = O_constant;
12881       ex.X_add_symbol = NULL;
12882       ex.X_op_symbol = NULL;
12883       ex.X_add_number = mips_cpreturn_offset;
12884
12885       macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12886                    mips_gp_register, (int) BFD_RELOC_LO16, SP);
12887     }
12888   else
12889     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12890                  "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12891
12892   demand_empty_rest_of_line ();
12893 }
12894
12895 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12896    code.  It sets the offset to use in gp_rel relocations.  */
12897
12898 static void
12899 s_gpvalue (ignore)
12900      int ignore ATTRIBUTE_UNUSED;
12901 {
12902   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12903      We also need NewABI support.  */
12904   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12905     {
12906       s_ignore (0);
12907       return;
12908     }
12909
12910   mips_gprel_offset = get_absolute_expression ();
12911
12912   demand_empty_rest_of_line ();
12913 }
12914
12915 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12916    code.  It generates a 32 bit GP relative reloc.  */
12917
12918 static void
12919 s_gpword (ignore)
12920      int ignore ATTRIBUTE_UNUSED;
12921 {
12922   symbolS *label;
12923   expressionS ex;
12924   char *p;
12925
12926   /* When not generating PIC code, this is treated as .word.  */
12927   if (mips_pic != SVR4_PIC)
12928     {
12929       s_cons (2);
12930       return;
12931     }
12932
12933   label = insn_labels != NULL ? insn_labels->label : NULL;
12934   mips_emit_delays (TRUE);
12935   if (auto_align)
12936     mips_align (2, 0, label);
12937   mips_clear_insn_labels ();
12938
12939   expression (&ex);
12940
12941   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12942     {
12943       as_bad (_("Unsupported use of .gpword"));
12944       ignore_rest_of_line ();
12945     }
12946
12947   p = frag_more (4);
12948   md_number_to_chars (p, (valueT) 0, 4);
12949   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12950                BFD_RELOC_GPREL32);
12951
12952   demand_empty_rest_of_line ();
12953 }
12954
12955 static void
12956 s_gpdword (ignore)
12957      int ignore ATTRIBUTE_UNUSED;
12958 {
12959   symbolS *label;
12960   expressionS ex;
12961   char *p;
12962
12963   /* When not generating PIC code, this is treated as .dword.  */
12964   if (mips_pic != SVR4_PIC)
12965     {
12966       s_cons (3);
12967       return;
12968     }
12969
12970   label = insn_labels != NULL ? insn_labels->label : NULL;
12971   mips_emit_delays (TRUE);
12972   if (auto_align)
12973     mips_align (3, 0, label);
12974   mips_clear_insn_labels ();
12975
12976   expression (&ex);
12977
12978   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12979     {
12980       as_bad (_("Unsupported use of .gpdword"));
12981       ignore_rest_of_line ();
12982     }
12983
12984   p = frag_more (8);
12985   md_number_to_chars (p, (valueT) 0, 8);
12986   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12987                BFD_RELOC_GPREL32);
12988
12989   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12990   ex.X_op = O_absent;
12991   ex.X_add_symbol = 0;
12992   ex.X_add_number = 0;
12993   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12994                BFD_RELOC_64);
12995
12996   demand_empty_rest_of_line ();
12997 }
12998
12999 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
13000    tables in SVR4 PIC code.  */
13001
13002 static void
13003 s_cpadd (ignore)
13004      int ignore ATTRIBUTE_UNUSED;
13005 {
13006   int icnt = 0;
13007   int reg;
13008
13009   /* This is ignored when not generating SVR4 PIC code.  */
13010   if (mips_pic != SVR4_PIC)
13011     {
13012       s_ignore (0);
13013       return;
13014     }
13015
13016   /* Add $gp to the register named as an argument.  */
13017   reg = tc_get_register (0);
13018   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
13019                HAVE_32BIT_ADDRESSES ? HAVE_NEWABI ? "add" : "addu" : "daddu",
13020                "d,v,t", reg, reg, mips_gp_register);
13021
13022   demand_empty_rest_of_line ();
13023 }
13024
13025 /* Handle the .insn pseudo-op.  This marks instruction labels in
13026    mips16 mode.  This permits the linker to handle them specially,
13027    such as generating jalx instructions when needed.  We also make
13028    them odd for the duration of the assembly, in order to generate the
13029    right sort of code.  We will make them even in the adjust_symtab
13030    routine, while leaving them marked.  This is convenient for the
13031    debugger and the disassembler.  The linker knows to make them odd
13032    again.  */
13033
13034 static void
13035 s_insn (ignore)
13036      int ignore ATTRIBUTE_UNUSED;
13037 {
13038   mips16_mark_labels ();
13039
13040   demand_empty_rest_of_line ();
13041 }
13042
13043 /* Handle a .stabn directive.  We need these in order to mark a label
13044    as being a mips16 text label correctly.  Sometimes the compiler
13045    will emit a label, followed by a .stabn, and then switch sections.
13046    If the label and .stabn are in mips16 mode, then the label is
13047    really a mips16 text label.  */
13048
13049 static void
13050 s_mips_stab (type)
13051      int type;
13052 {
13053   if (type == 'n')
13054     mips16_mark_labels ();
13055
13056   s_stab (type);
13057 }
13058
13059 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
13060  */
13061
13062 static void
13063 s_mips_weakext (ignore)
13064      int ignore ATTRIBUTE_UNUSED;
13065 {
13066   char *name;
13067   int c;
13068   symbolS *symbolP;
13069   expressionS exp;
13070
13071   name = input_line_pointer;
13072   c = get_symbol_end ();
13073   symbolP = symbol_find_or_make (name);
13074   S_SET_WEAK (symbolP);
13075   *input_line_pointer = c;
13076
13077   SKIP_WHITESPACE ();
13078
13079   if (! is_end_of_line[(unsigned char) *input_line_pointer])
13080     {
13081       if (S_IS_DEFINED (symbolP))
13082         {
13083           as_bad ("ignoring attempt to redefine symbol %s",
13084                   S_GET_NAME (symbolP));
13085           ignore_rest_of_line ();
13086           return;
13087         }
13088
13089       if (*input_line_pointer == ',')
13090         {
13091           ++input_line_pointer;
13092           SKIP_WHITESPACE ();
13093         }
13094
13095       expression (&exp);
13096       if (exp.X_op != O_symbol)
13097         {
13098           as_bad ("bad .weakext directive");
13099           ignore_rest_of_line ();
13100           return;
13101         }
13102       symbol_set_value_expression (symbolP, &exp);
13103     }
13104
13105   demand_empty_rest_of_line ();
13106 }
13107
13108 /* Parse a register string into a number.  Called from the ECOFF code
13109    to parse .frame.  The argument is non-zero if this is the frame
13110    register, so that we can record it in mips_frame_reg.  */
13111
13112 int
13113 tc_get_register (frame)
13114      int frame;
13115 {
13116   int reg;
13117
13118   SKIP_WHITESPACE ();
13119   if (*input_line_pointer++ != '$')
13120     {
13121       as_warn (_("expected `$'"));
13122       reg = ZERO;
13123     }
13124   else if (ISDIGIT (*input_line_pointer))
13125     {
13126       reg = get_absolute_expression ();
13127       if (reg < 0 || reg >= 32)
13128         {
13129           as_warn (_("Bad register number"));
13130           reg = ZERO;
13131         }
13132     }
13133   else
13134     {
13135       if (strncmp (input_line_pointer, "ra", 2) == 0)
13136         {
13137           reg = RA;
13138           input_line_pointer += 2;
13139         }
13140       else if (strncmp (input_line_pointer, "fp", 2) == 0)
13141         {
13142           reg = FP;
13143           input_line_pointer += 2;
13144         }
13145       else if (strncmp (input_line_pointer, "sp", 2) == 0)
13146         {
13147           reg = SP;
13148           input_line_pointer += 2;
13149         }
13150       else if (strncmp (input_line_pointer, "gp", 2) == 0)
13151         {
13152           reg = GP;
13153           input_line_pointer += 2;
13154         }
13155       else if (strncmp (input_line_pointer, "at", 2) == 0)
13156         {
13157           reg = AT;
13158           input_line_pointer += 2;
13159         }
13160       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13161         {
13162           reg = KT0;
13163           input_line_pointer += 3;
13164         }
13165       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13166         {
13167           reg = KT1;
13168           input_line_pointer += 3;
13169         }
13170       else if (strncmp (input_line_pointer, "zero", 4) == 0)
13171         {
13172           reg = ZERO;
13173           input_line_pointer += 4;
13174         }
13175       else
13176         {
13177           as_warn (_("Unrecognized register name"));
13178           reg = ZERO;
13179           while (ISALNUM(*input_line_pointer))
13180            input_line_pointer++;
13181         }
13182     }
13183   if (frame)
13184     {
13185       mips_frame_reg = reg != 0 ? reg : SP;
13186       mips_frame_reg_valid = 1;
13187       mips_cprestore_valid = 0;
13188     }
13189   return reg;
13190 }
13191
13192 valueT
13193 md_section_align (seg, addr)
13194      asection *seg;
13195      valueT addr;
13196 {
13197   int align = bfd_get_section_alignment (stdoutput, seg);
13198
13199 #ifdef OBJ_ELF
13200   /* We don't need to align ELF sections to the full alignment.
13201      However, Irix 5 may prefer that we align them at least to a 16
13202      byte boundary.  We don't bother to align the sections if we are
13203      targeted for an embedded system.  */
13204   if (strcmp (TARGET_OS, "elf") == 0)
13205     return addr;
13206   if (align > 4)
13207     align = 4;
13208 #endif
13209
13210   return ((addr + (1 << align) - 1) & (-1 << align));
13211 }
13212
13213 /* Utility routine, called from above as well.  If called while the
13214    input file is still being read, it's only an approximation.  (For
13215    example, a symbol may later become defined which appeared to be
13216    undefined earlier.)  */
13217
13218 static int
13219 nopic_need_relax (sym, before_relaxing)
13220      symbolS *sym;
13221      int before_relaxing;
13222 {
13223   if (sym == 0)
13224     return 0;
13225
13226   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
13227     {
13228       const char *symname;
13229       int change;
13230
13231       /* Find out whether this symbol can be referenced off the $gp
13232          register.  It can be if it is smaller than the -G size or if
13233          it is in the .sdata or .sbss section.  Certain symbols can
13234          not be referenced off the $gp, although it appears as though
13235          they can.  */
13236       symname = S_GET_NAME (sym);
13237       if (symname != (const char *) NULL
13238           && (strcmp (symname, "eprol") == 0
13239               || strcmp (symname, "etext") == 0
13240               || strcmp (symname, "_gp") == 0
13241               || strcmp (symname, "edata") == 0
13242               || strcmp (symname, "_fbss") == 0
13243               || strcmp (symname, "_fdata") == 0
13244               || strcmp (symname, "_ftext") == 0
13245               || strcmp (symname, "end") == 0
13246               || strcmp (symname, "_gp_disp") == 0))
13247         change = 1;
13248       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13249                && (0
13250 #ifndef NO_ECOFF_DEBUGGING
13251                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
13252                        && (symbol_get_obj (sym)->ecoff_extern_size
13253                            <= g_switch_value))
13254 #endif
13255                    /* We must defer this decision until after the whole
13256                       file has been read, since there might be a .extern
13257                       after the first use of this symbol.  */
13258                    || (before_relaxing
13259 #ifndef NO_ECOFF_DEBUGGING
13260                        && symbol_get_obj (sym)->ecoff_extern_size == 0
13261 #endif
13262                        && S_GET_VALUE (sym) == 0)
13263                    || (S_GET_VALUE (sym) != 0
13264                        && S_GET_VALUE (sym) <= g_switch_value)))
13265         change = 0;
13266       else
13267         {
13268           const char *segname;
13269
13270           segname = segment_name (S_GET_SEGMENT (sym));
13271           assert (strcmp (segname, ".lit8") != 0
13272                   && strcmp (segname, ".lit4") != 0);
13273           change = (strcmp (segname, ".sdata") != 0
13274                     && strcmp (segname, ".sbss") != 0
13275                     && strncmp (segname, ".sdata.", 7) != 0
13276                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13277         }
13278       return change;
13279     }
13280   else
13281     /* We are not optimizing for the $gp register.  */
13282     return 1;
13283 }
13284
13285
13286 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
13287
13288 static bfd_boolean
13289 pic_need_relax (sym, segtype)
13290      symbolS *sym;
13291      asection *segtype;
13292 {
13293   asection *symsec;
13294   bfd_boolean linkonce;
13295
13296   /* Handle the case of a symbol equated to another symbol.  */
13297   while (symbol_equated_reloc_p (sym))
13298     {
13299       symbolS *n;
13300
13301       /* It's possible to get a loop here in a badly written
13302          program.  */
13303       n = symbol_get_value_expression (sym)->X_add_symbol;
13304       if (n == sym)
13305         break;
13306       sym = n;
13307     }
13308
13309   symsec = S_GET_SEGMENT (sym);
13310
13311   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13312   linkonce = FALSE;
13313   if (symsec != segtype && ! S_IS_LOCAL (sym))
13314     {
13315       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13316           != 0)
13317         linkonce = TRUE;
13318
13319       /* The GNU toolchain uses an extension for ELF: a section
13320          beginning with the magic string .gnu.linkonce is a linkonce
13321          section.  */
13322       if (strncmp (segment_name (symsec), ".gnu.linkonce",
13323                    sizeof ".gnu.linkonce" - 1) == 0)
13324         linkonce = TRUE;
13325     }
13326
13327   /* This must duplicate the test in adjust_reloc_syms.  */
13328   return (symsec != &bfd_und_section
13329           && symsec != &bfd_abs_section
13330           && ! bfd_is_com_section (symsec)
13331           && !linkonce
13332 #ifdef OBJ_ELF
13333           /* A global or weak symbol is treated as external.  */
13334           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13335               || (! S_IS_WEAK (sym)
13336                   && (! S_IS_EXTERNAL (sym)
13337                       || mips_pic == EMBEDDED_PIC)))
13338 #endif
13339           );
13340 }
13341
13342
13343 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13344    extended opcode.  SEC is the section the frag is in.  */
13345
13346 static int
13347 mips16_extended_frag (fragp, sec, stretch)
13348      fragS *fragp;
13349      asection *sec;
13350      long stretch;
13351 {
13352   int type;
13353   register const struct mips16_immed_operand *op;
13354   offsetT val;
13355   int mintiny, maxtiny;
13356   segT symsec;
13357   fragS *sym_frag;
13358
13359   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13360     return 0;
13361   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13362     return 1;
13363
13364   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13365   op = mips16_immed_operands;
13366   while (op->type != type)
13367     {
13368       ++op;
13369       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13370     }
13371
13372   if (op->unsp)
13373     {
13374       if (type == '<' || type == '>' || type == '[' || type == ']')
13375         {
13376           mintiny = 1;
13377           maxtiny = 1 << op->nbits;
13378         }
13379       else
13380         {
13381           mintiny = 0;
13382           maxtiny = (1 << op->nbits) - 1;
13383         }
13384     }
13385   else
13386     {
13387       mintiny = - (1 << (op->nbits - 1));
13388       maxtiny = (1 << (op->nbits - 1)) - 1;
13389     }
13390
13391   sym_frag = symbol_get_frag (fragp->fr_symbol);
13392   val = S_GET_VALUE (fragp->fr_symbol);
13393   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13394
13395   if (op->pcrel)
13396     {
13397       addressT addr;
13398
13399       /* We won't have the section when we are called from
13400          mips_relax_frag.  However, we will always have been called
13401          from md_estimate_size_before_relax first.  If this is a
13402          branch to a different section, we mark it as such.  If SEC is
13403          NULL, and the frag is not marked, then it must be a branch to
13404          the same section.  */
13405       if (sec == NULL)
13406         {
13407           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13408             return 1;
13409         }
13410       else
13411         {
13412           /* Must have been called from md_estimate_size_before_relax.  */
13413           if (symsec != sec)
13414             {
13415               fragp->fr_subtype =
13416                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13417
13418               /* FIXME: We should support this, and let the linker
13419                  catch branches and loads that are out of range.  */
13420               as_bad_where (fragp->fr_file, fragp->fr_line,
13421                             _("unsupported PC relative reference to different section"));
13422
13423               return 1;
13424             }
13425           if (fragp != sym_frag && sym_frag->fr_address == 0)
13426             /* Assume non-extended on the first relaxation pass.
13427                The address we have calculated will be bogus if this is
13428                a forward branch to another frag, as the forward frag
13429                will have fr_address == 0.  */
13430             return 0;
13431         }
13432
13433       /* In this case, we know for sure that the symbol fragment is in
13434          the same section.  If the relax_marker of the symbol fragment
13435          differs from the relax_marker of this fragment, we have not
13436          yet adjusted the symbol fragment fr_address.  We want to add
13437          in STRETCH in order to get a better estimate of the address.
13438          This particularly matters because of the shift bits.  */
13439       if (stretch != 0
13440           && sym_frag->relax_marker != fragp->relax_marker)
13441         {
13442           fragS *f;
13443
13444           /* Adjust stretch for any alignment frag.  Note that if have
13445              been expanding the earlier code, the symbol may be
13446              defined in what appears to be an earlier frag.  FIXME:
13447              This doesn't handle the fr_subtype field, which specifies
13448              a maximum number of bytes to skip when doing an
13449              alignment.  */
13450           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13451             {
13452               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13453                 {
13454                   if (stretch < 0)
13455                     stretch = - ((- stretch)
13456                                  & ~ ((1 << (int) f->fr_offset) - 1));
13457                   else
13458                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13459                   if (stretch == 0)
13460                     break;
13461                 }
13462             }
13463           if (f != NULL)
13464             val += stretch;
13465         }
13466
13467       addr = fragp->fr_address + fragp->fr_fix;
13468
13469       /* The base address rules are complicated.  The base address of
13470          a branch is the following instruction.  The base address of a
13471          PC relative load or add is the instruction itself, but if it
13472          is in a delay slot (in which case it can not be extended) use
13473          the address of the instruction whose delay slot it is in.  */
13474       if (type == 'p' || type == 'q')
13475         {
13476           addr += 2;
13477
13478           /* If we are currently assuming that this frag should be
13479              extended, then, the current address is two bytes
13480              higher.  */
13481           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13482             addr += 2;
13483
13484           /* Ignore the low bit in the target, since it will be set
13485              for a text label.  */
13486           if ((val & 1) != 0)
13487             --val;
13488         }
13489       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13490         addr -= 4;
13491       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13492         addr -= 2;
13493
13494       val -= addr & ~ ((1 << op->shift) - 1);
13495
13496       /* Branch offsets have an implicit 0 in the lowest bit.  */
13497       if (type == 'p' || type == 'q')
13498         val /= 2;
13499
13500       /* If any of the shifted bits are set, we must use an extended
13501          opcode.  If the address depends on the size of this
13502          instruction, this can lead to a loop, so we arrange to always
13503          use an extended opcode.  We only check this when we are in
13504          the main relaxation loop, when SEC is NULL.  */
13505       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13506         {
13507           fragp->fr_subtype =
13508             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13509           return 1;
13510         }
13511
13512       /* If we are about to mark a frag as extended because the value
13513          is precisely maxtiny + 1, then there is a chance of an
13514          infinite loop as in the following code:
13515              la $4,foo
13516              .skip      1020
13517              .align     2
13518            foo:
13519          In this case when the la is extended, foo is 0x3fc bytes
13520          away, so the la can be shrunk, but then foo is 0x400 away, so
13521          the la must be extended.  To avoid this loop, we mark the
13522          frag as extended if it was small, and is about to become
13523          extended with a value of maxtiny + 1.  */
13524       if (val == ((maxtiny + 1) << op->shift)
13525           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13526           && sec == NULL)
13527         {
13528           fragp->fr_subtype =
13529             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13530           return 1;
13531         }
13532     }
13533   else if (symsec != absolute_section && sec != NULL)
13534     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13535
13536   if ((val & ((1 << op->shift) - 1)) != 0
13537       || val < (mintiny << op->shift)
13538       || val > (maxtiny << op->shift))
13539     return 1;
13540   else
13541     return 0;
13542 }
13543
13544 /* Compute the length of a branch sequence, and adjust the
13545    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13546    worst-case length is computed, with UPDATE being used to indicate
13547    whether an unconditional (-1), branch-likely (+1) or regular (0)
13548    branch is to be computed.  */
13549 static int
13550 relaxed_branch_length (fragp, sec, update)
13551      fragS *fragp;
13552      asection *sec;
13553      int update;
13554 {
13555   bfd_boolean toofar;
13556   int length;
13557
13558   if (fragp
13559       && S_IS_DEFINED (fragp->fr_symbol)
13560       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13561     {
13562       addressT addr;
13563       offsetT val;
13564
13565       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13566
13567       addr = fragp->fr_address + fragp->fr_fix + 4;
13568
13569       val -= addr;
13570
13571       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13572     }
13573   else if (fragp)
13574     /* If the symbol is not defined or it's in a different segment,
13575        assume the user knows what's going on and emit a short
13576        branch.  */
13577     toofar = FALSE;
13578   else
13579     toofar = TRUE;
13580
13581   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13582     fragp->fr_subtype
13583       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13584                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13585                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13586                              toofar);
13587
13588   length = 4;
13589   if (toofar)
13590     {
13591       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13592         length += 8;
13593
13594       if (mips_pic != NO_PIC)
13595         {
13596           /* Additional space for PIC loading of target address.  */
13597           length += 8;
13598           if (mips_opts.isa == ISA_MIPS1)
13599             /* Additional space for $at-stabilizing nop.  */
13600             length += 4;
13601         }
13602
13603       /* If branch is conditional.  */
13604       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13605         length += 8;
13606     }
13607
13608   return length;
13609 }
13610
13611 /* Estimate the size of a frag before relaxing.  Unless this is the
13612    mips16, we are not really relaxing here, and the final size is
13613    encoded in the subtype information.  For the mips16, we have to
13614    decide whether we are using an extended opcode or not.  */
13615
13616 int
13617 md_estimate_size_before_relax (fragp, segtype)
13618      fragS *fragp;
13619      asection *segtype;
13620 {
13621   int change;
13622
13623   if (RELAX_BRANCH_P (fragp->fr_subtype))
13624     {
13625
13626       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13627
13628       return fragp->fr_var;
13629     }
13630
13631   if (RELAX_MIPS16_P (fragp->fr_subtype))
13632     /* We don't want to modify the EXTENDED bit here; it might get us
13633        into infinite loops.  We change it only in mips_relax_frag().  */
13634     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13635
13636   if (mips_pic == NO_PIC)
13637     change = nopic_need_relax (fragp->fr_symbol, 0);
13638   else if (mips_pic == SVR4_PIC)
13639     change = pic_need_relax (fragp->fr_symbol, segtype);
13640   else
13641     abort ();
13642
13643   if (change)
13644     {
13645       /* Record the offset to the first reloc in the fr_opcode field.
13646          This lets md_convert_frag and tc_gen_reloc know that the code
13647          must be expanded.  */
13648       fragp->fr_opcode = (fragp->fr_literal
13649                           + fragp->fr_fix
13650                           - RELAX_OLD (fragp->fr_subtype)
13651                           + RELAX_RELOC1 (fragp->fr_subtype));
13652       /* FIXME: This really needs as_warn_where.  */
13653       if (RELAX_WARN (fragp->fr_subtype))
13654         as_warn (_("AT used after \".set noat\" or macro used after "
13655                    "\".set nomacro\""));
13656
13657       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13658     }
13659
13660   return 0;
13661 }
13662
13663 /* This is called to see whether a reloc against a defined symbol
13664    should be converted into a reloc against a section.  Don't adjust
13665    MIPS16 jump relocations, so we don't have to worry about the format
13666    of the offset in the .o file.  Don't adjust relocations against
13667    mips16 symbols, so that the linker can find them if it needs to set
13668    up a stub.  */
13669
13670 int
13671 mips_fix_adjustable (fixp)
13672      fixS *fixp;
13673 {
13674   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13675     return 0;
13676
13677   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13678       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13679     return 0;
13680
13681   if (fixp->fx_addsy == NULL)
13682     return 1;
13683
13684 #ifdef OBJ_ELF
13685   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13686       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13687       && fixp->fx_subsy == NULL)
13688     return 0;
13689 #endif
13690
13691   return 1;
13692 }
13693
13694 /* Translate internal representation of relocation info to BFD target
13695    format.  */
13696
13697 arelent **
13698 tc_gen_reloc (section, fixp)
13699      asection *section ATTRIBUTE_UNUSED;
13700      fixS *fixp;
13701 {
13702   static arelent *retval[4];
13703   arelent *reloc;
13704   bfd_reloc_code_real_type code;
13705
13706   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13707   retval[1] = NULL;
13708
13709   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13710   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13711   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13712
13713   if (mips_pic == EMBEDDED_PIC
13714       && SWITCH_TABLE (fixp))
13715     {
13716       /* For a switch table entry we use a special reloc.  The addend
13717          is actually the difference between the reloc address and the
13718          subtrahend.  */
13719       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13720       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13721         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13722       fixp->fx_r_type = BFD_RELOC_GPREL32;
13723     }
13724   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13725     {
13726       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13727         reloc->addend = fixp->fx_addnumber;
13728       else
13729         {
13730           /* We use a special addend for an internal RELLO reloc.  */
13731           if (symbol_section_p (fixp->fx_addsy))
13732             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13733           else
13734             reloc->addend = fixp->fx_addnumber + reloc->address;
13735         }
13736     }
13737   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13738     {
13739       assert (fixp->fx_next != NULL
13740               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13741
13742       /* The reloc is relative to the RELLO; adjust the addend
13743          accordingly.  */
13744       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13745         reloc->addend = fixp->fx_next->fx_addnumber;
13746       else
13747         {
13748           /* We use a special addend for an internal RELHI reloc.  */
13749           if (symbol_section_p (fixp->fx_addsy))
13750             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13751                              + fixp->fx_next->fx_where
13752                              - S_GET_VALUE (fixp->fx_subsy));
13753           else
13754             reloc->addend = (fixp->fx_addnumber
13755                              + fixp->fx_next->fx_frag->fr_address
13756                              + fixp->fx_next->fx_where);
13757         }
13758     }
13759   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13760     reloc->addend = fixp->fx_addnumber;
13761   else
13762     {
13763       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13764         /* A gruesome hack which is a result of the gruesome gas reloc
13765            handling.  */
13766         reloc->addend = reloc->address;
13767       else
13768         reloc->addend = -reloc->address;
13769     }
13770
13771   /* If this is a variant frag, we may need to adjust the existing
13772      reloc and generate a new one.  */
13773   if (fixp->fx_frag->fr_opcode != NULL
13774       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13775            && ! HAVE_NEWABI)
13776           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13777               && HAVE_NEWABI)
13778           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13779           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13780           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13781           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13782           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13783           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13784     )
13785     {
13786       arelent *reloc2;
13787
13788       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13789
13790       /* If this is not the last reloc in this frag, then we have two
13791          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13792          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13793          the second one handle all of them.  */
13794       if (fixp->fx_next != NULL
13795           && fixp->fx_frag == fixp->fx_next->fx_frag)
13796         {
13797           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13798                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13799                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13800                       && (fixp->fx_next->fx_r_type
13801                           == BFD_RELOC_MIPS_GOT_LO16))
13802                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13803                       && (fixp->fx_next->fx_r_type
13804                           == BFD_RELOC_MIPS_CALL_LO16)));
13805           retval[0] = NULL;
13806           return retval;
13807         }
13808
13809       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13810       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13811       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13812       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13813       retval[2] = NULL;
13814       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13815       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13816       reloc2->address = (reloc->address
13817                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13818                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13819       reloc2->addend = fixp->fx_addnumber
13820         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13821       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13822       assert (reloc2->howto != NULL);
13823
13824       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13825         {
13826           arelent *reloc3;
13827
13828           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13829           retval[3] = NULL;
13830           *reloc3 = *reloc2;
13831           reloc3->address += 4;
13832         }
13833
13834       if (mips_pic == NO_PIC)
13835         {
13836           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13837           fixp->fx_r_type = BFD_RELOC_HI16_S;
13838         }
13839       else if (mips_pic == SVR4_PIC)
13840         {
13841           switch (fixp->fx_r_type)
13842             {
13843             default:
13844               abort ();
13845             case BFD_RELOC_MIPS_GOT16:
13846               break;
13847             case BFD_RELOC_MIPS_GOT_LO16:
13848             case BFD_RELOC_MIPS_CALL_LO16:
13849               if (HAVE_NEWABI)
13850                 {
13851                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13852                   reloc2->howto = bfd_reloc_type_lookup
13853                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13854                 }
13855               else
13856                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13857               break;
13858             case BFD_RELOC_MIPS_CALL16:
13859             case BFD_RELOC_MIPS_GOT_OFST:
13860             case BFD_RELOC_MIPS_GOT_DISP:
13861               if (HAVE_NEWABI)
13862                 {
13863                   /* It may seem nonsensical to relax GOT_DISP to
13864                      GOT_DISP, but we're actually turning a GOT_DISP
13865                      without offset into a GOT_DISP with an offset,
13866                      getting rid of the separate addition, which we can
13867                      do when the symbol is found to be local.  */
13868                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13869                   retval[1] = NULL;
13870                 }
13871               else
13872                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13873               break;
13874             }
13875         }
13876       else
13877         abort ();
13878     }
13879
13880   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13881      entry to be used in the relocation's section offset.  */
13882   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13883     {
13884       reloc->address = reloc->addend;
13885       reloc->addend = 0;
13886     }
13887
13888   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13889      fixup_segment converted a non-PC relative reloc into a PC
13890      relative reloc.  In such a case, we need to convert the reloc
13891      code.  */
13892   code = fixp->fx_r_type;
13893   if (fixp->fx_pcrel)
13894     {
13895       switch (code)
13896         {
13897         case BFD_RELOC_8:
13898           code = BFD_RELOC_8_PCREL;
13899           break;
13900         case BFD_RELOC_16:
13901           code = BFD_RELOC_16_PCREL;
13902           break;
13903         case BFD_RELOC_32:
13904           code = BFD_RELOC_32_PCREL;
13905           break;
13906         case BFD_RELOC_64:
13907           code = BFD_RELOC_64_PCREL;
13908           break;
13909         case BFD_RELOC_8_PCREL:
13910         case BFD_RELOC_16_PCREL:
13911         case BFD_RELOC_32_PCREL:
13912         case BFD_RELOC_64_PCREL:
13913         case BFD_RELOC_16_PCREL_S2:
13914         case BFD_RELOC_PCREL_HI16_S:
13915         case BFD_RELOC_PCREL_LO16:
13916           break;
13917         default:
13918           as_bad_where (fixp->fx_file, fixp->fx_line,
13919                         _("Cannot make %s relocation PC relative"),
13920                         bfd_get_reloc_code_name (code));
13921         }
13922     }
13923
13924 #ifdef OBJ_ELF
13925   /* md_apply_fix3 has a double-subtraction hack to get
13926      bfd_install_relocation to behave nicely.  GPREL relocations are
13927      handled correctly without this hack, so undo it here.  We can't
13928      stop md_apply_fix3 from subtracting twice in the first place since
13929      the fake addend is required for variant frags above.  */
13930   if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
13931       && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
13932       && reloc->addend != 0
13933       && mips_need_elf_addend_fixup (fixp))
13934     reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13935 #endif
13936
13937   /* To support a PC relative reloc when generating embedded PIC code
13938      for ECOFF, we use a Cygnus extension.  We check for that here to
13939      make sure that we don't let such a reloc escape normally.  */
13940   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13941        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13942       && code == BFD_RELOC_16_PCREL_S2
13943       && mips_pic != EMBEDDED_PIC)
13944     reloc->howto = NULL;
13945   else
13946     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13947
13948   if (reloc->howto == NULL)
13949     {
13950       as_bad_where (fixp->fx_file, fixp->fx_line,
13951                     _("Can not represent %s relocation in this object file format"),
13952                     bfd_get_reloc_code_name (code));
13953       retval[0] = NULL;
13954     }
13955
13956   return retval;
13957 }
13958
13959 /* Relax a machine dependent frag.  This returns the amount by which
13960    the current size of the frag should change.  */
13961
13962 int
13963 mips_relax_frag (sec, fragp, stretch)
13964      asection *sec;
13965      fragS *fragp;
13966      long stretch;
13967 {
13968   if (RELAX_BRANCH_P (fragp->fr_subtype))
13969     {
13970       offsetT old_var = fragp->fr_var;
13971
13972       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13973
13974       return fragp->fr_var - old_var;
13975     }
13976
13977   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13978     return 0;
13979
13980   if (mips16_extended_frag (fragp, NULL, stretch))
13981     {
13982       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13983         return 0;
13984       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13985       return 2;
13986     }
13987   else
13988     {
13989       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13990         return 0;
13991       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13992       return -2;
13993     }
13994
13995   return 0;
13996 }
13997
13998 /* Convert a machine dependent frag.  */
13999
14000 void
14001 md_convert_frag (abfd, asec, fragp)
14002      bfd *abfd ATTRIBUTE_UNUSED;
14003      segT asec;
14004      fragS *fragp;
14005 {
14006   int old, new;
14007   char *fixptr;
14008
14009   if (RELAX_BRANCH_P (fragp->fr_subtype))
14010     {
14011       bfd_byte *buf;
14012       unsigned long insn;
14013       expressionS exp;
14014       fixS *fixp;
14015
14016       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
14017
14018       if (target_big_endian)
14019         insn = bfd_getb32 (buf);
14020       else
14021         insn = bfd_getl32 (buf);
14022
14023       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
14024         {
14025           /* We generate a fixup instead of applying it right now
14026              because, if there are linker relaxations, we're going to
14027              need the relocations.  */
14028           exp.X_op = O_symbol;
14029           exp.X_add_symbol = fragp->fr_symbol;
14030           exp.X_add_number = fragp->fr_offset;
14031
14032           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14033                               4, &exp, 1,
14034                               BFD_RELOC_16_PCREL_S2);
14035           fixp->fx_file = fragp->fr_file;
14036           fixp->fx_line = fragp->fr_line;
14037
14038           md_number_to_chars ((char *)buf, insn, 4);
14039           buf += 4;
14040         }
14041       else
14042         {
14043           int i;
14044
14045           as_warn_where (fragp->fr_file, fragp->fr_line,
14046                          _("relaxed out-of-range branch into a jump"));
14047
14048           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14049             goto uncond;
14050
14051           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14052             {
14053               /* Reverse the branch.  */
14054               switch ((insn >> 28) & 0xf)
14055                 {
14056                 case 4:
14057                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14058                      have the condition reversed by tweaking a single
14059                      bit, and their opcodes all have 0x4???????.  */
14060                   assert ((insn & 0xf1000000) == 0x41000000);
14061                   insn ^= 0x00010000;
14062                   break;
14063
14064                 case 0:
14065                   /* bltz       0x04000000      bgez    0x04010000
14066                      bltzal     0x04100000      bgezal  0x04110000 */
14067                   assert ((insn & 0xfc0e0000) == 0x04000000);
14068                   insn ^= 0x00010000;
14069                   break;
14070
14071                 case 1:
14072                   /* beq        0x10000000      bne     0x14000000
14073                      blez       0x18000000      bgtz    0x1c000000 */
14074                   insn ^= 0x04000000;
14075                   break;
14076
14077                 default:
14078                   abort ();
14079                 }
14080             }
14081
14082           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14083             {
14084               /* Clear the and-link bit.  */
14085               assert ((insn & 0xfc1c0000) == 0x04100000);
14086
14087               /* bltzal 0x04100000      bgezal  0x04110000
14088                 bltzall 0x04120000     bgezall  0x04130000 */
14089               insn &= ~0x00100000;
14090             }
14091
14092           /* Branch over the branch (if the branch was likely) or the
14093              full jump (not likely case).  Compute the offset from the
14094              current instruction to branch to.  */
14095           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14096             i = 16;
14097           else
14098             {
14099               /* How many bytes in instructions we've already emitted?  */
14100               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14101               /* How many bytes in instructions from here to the end?  */
14102               i = fragp->fr_var - i;
14103             }
14104           /* Convert to instruction count.  */
14105           i >>= 2;
14106           /* Branch counts from the next instruction.  */
14107           i--;
14108           insn |= i;
14109           /* Branch over the jump.  */
14110           md_number_to_chars ((char *)buf, insn, 4);
14111           buf += 4;
14112
14113           /* Nop */
14114           md_number_to_chars ((char*)buf, 0, 4);
14115           buf += 4;
14116
14117           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14118             {
14119               /* beql $0, $0, 2f */
14120               insn = 0x50000000;
14121               /* Compute the PC offset from the current instruction to
14122                  the end of the variable frag.  */
14123               /* How many bytes in instructions we've already emitted?  */
14124               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14125               /* How many bytes in instructions from here to the end?  */
14126               i = fragp->fr_var - i;
14127               /* Convert to instruction count.  */
14128               i >>= 2;
14129               /* Don't decrement i, because we want to branch over the
14130                  delay slot.  */
14131
14132               insn |= i;
14133               md_number_to_chars ((char *)buf, insn, 4);
14134               buf += 4;
14135
14136               md_number_to_chars ((char *)buf, 0, 4);
14137               buf += 4;
14138             }
14139
14140         uncond:
14141           if (mips_pic == NO_PIC)
14142             {
14143               /* j or jal.  */
14144               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14145                       ? 0x0c000000 : 0x08000000);
14146               exp.X_op = O_symbol;
14147               exp.X_add_symbol = fragp->fr_symbol;
14148               exp.X_add_number = fragp->fr_offset;
14149
14150               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14151                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
14152               fixp->fx_file = fragp->fr_file;
14153               fixp->fx_line = fragp->fr_line;
14154
14155               md_number_to_chars ((char*)buf, insn, 4);
14156               buf += 4;
14157             }
14158           else
14159             {
14160               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
14161               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14162               exp.X_op = O_symbol;
14163               exp.X_add_symbol = fragp->fr_symbol;
14164               exp.X_add_number = fragp->fr_offset;
14165
14166               if (fragp->fr_offset)
14167                 {
14168                   exp.X_add_symbol = make_expr_symbol (&exp);
14169                   exp.X_add_number = 0;
14170                 }
14171
14172               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14173                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14174               fixp->fx_file = fragp->fr_file;
14175               fixp->fx_line = fragp->fr_line;
14176
14177               md_number_to_chars ((char*)buf, insn, 4);
14178               buf += 4;
14179
14180               if (mips_opts.isa == ISA_MIPS1)
14181                 {
14182                   /* nop */
14183                   md_number_to_chars ((char*)buf, 0, 4);
14184                   buf += 4;
14185                 }
14186
14187               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
14188               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14189
14190               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14191                                   4, &exp, 0, BFD_RELOC_LO16);
14192               fixp->fx_file = fragp->fr_file;
14193               fixp->fx_line = fragp->fr_line;
14194
14195               md_number_to_chars ((char*)buf, insn, 4);
14196               buf += 4;
14197
14198               /* j(al)r $at.  */
14199               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14200                 insn = 0x0020f809;
14201               else
14202                 insn = 0x00200008;
14203
14204               md_number_to_chars ((char*)buf, insn, 4);
14205               buf += 4;
14206             }
14207         }
14208
14209       assert (buf == (bfd_byte *)fragp->fr_literal
14210               + fragp->fr_fix + fragp->fr_var);
14211
14212       fragp->fr_fix += fragp->fr_var;
14213
14214       return;
14215     }
14216
14217   if (RELAX_MIPS16_P (fragp->fr_subtype))
14218     {
14219       int type;
14220       register const struct mips16_immed_operand *op;
14221       bfd_boolean small, ext;
14222       offsetT val;
14223       bfd_byte *buf;
14224       unsigned long insn;
14225       bfd_boolean use_extend;
14226       unsigned short extend;
14227
14228       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14229       op = mips16_immed_operands;
14230       while (op->type != type)
14231         ++op;
14232
14233       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14234         {
14235           small = FALSE;
14236           ext = TRUE;
14237         }
14238       else
14239         {
14240           small = TRUE;
14241           ext = FALSE;
14242         }
14243
14244       resolve_symbol_value (fragp->fr_symbol);
14245       val = S_GET_VALUE (fragp->fr_symbol);
14246       if (op->pcrel)
14247         {
14248           addressT addr;
14249
14250           addr = fragp->fr_address + fragp->fr_fix;
14251
14252           /* The rules for the base address of a PC relative reloc are
14253              complicated; see mips16_extended_frag.  */
14254           if (type == 'p' || type == 'q')
14255             {
14256               addr += 2;
14257               if (ext)
14258                 addr += 2;
14259               /* Ignore the low bit in the target, since it will be
14260                  set for a text label.  */
14261               if ((val & 1) != 0)
14262                 --val;
14263             }
14264           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14265             addr -= 4;
14266           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14267             addr -= 2;
14268
14269           addr &= ~ (addressT) ((1 << op->shift) - 1);
14270           val -= addr;
14271
14272           /* Make sure the section winds up with the alignment we have
14273              assumed.  */
14274           if (op->shift > 0)
14275             record_alignment (asec, op->shift);
14276         }
14277
14278       if (ext
14279           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14280               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14281         as_warn_where (fragp->fr_file, fragp->fr_line,
14282                        _("extended instruction in delay slot"));
14283
14284       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14285
14286       if (target_big_endian)
14287         insn = bfd_getb16 (buf);
14288       else
14289         insn = bfd_getl16 (buf);
14290
14291       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14292                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14293                     small, ext, &insn, &use_extend, &extend);
14294
14295       if (use_extend)
14296         {
14297           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
14298           fragp->fr_fix += 2;
14299           buf += 2;
14300         }
14301
14302       md_number_to_chars ((char *) buf, insn, 2);
14303       fragp->fr_fix += 2;
14304       buf += 2;
14305     }
14306   else
14307     {
14308       if (fragp->fr_opcode == NULL)
14309         return;
14310
14311       old = RELAX_OLD (fragp->fr_subtype);
14312       new = RELAX_NEW (fragp->fr_subtype);
14313       fixptr = fragp->fr_literal + fragp->fr_fix;
14314
14315       if (new > 0)
14316         memmove (fixptr - old, fixptr, new);
14317
14318       fragp->fr_fix += new - old;
14319     }
14320 }
14321
14322 #ifdef OBJ_ELF
14323
14324 /* This function is called after the relocs have been generated.
14325    We've been storing mips16 text labels as odd.  Here we convert them
14326    back to even for the convenience of the debugger.  */
14327
14328 void
14329 mips_frob_file_after_relocs ()
14330 {
14331   asymbol **syms;
14332   unsigned int count, i;
14333
14334   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14335     return;
14336
14337   syms = bfd_get_outsymbols (stdoutput);
14338   count = bfd_get_symcount (stdoutput);
14339   for (i = 0; i < count; i++, syms++)
14340     {
14341       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14342           && ((*syms)->value & 1) != 0)
14343         {
14344           (*syms)->value &= ~1;
14345           /* If the symbol has an odd size, it was probably computed
14346              incorrectly, so adjust that as well.  */
14347           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14348             ++elf_symbol (*syms)->internal_elf_sym.st_size;
14349         }
14350     }
14351 }
14352
14353 #endif
14354
14355 /* This function is called whenever a label is defined.  It is used
14356    when handling branch delays; if a branch has a label, we assume we
14357    can not move it.  */
14358
14359 void
14360 mips_define_label (sym)
14361      symbolS *sym;
14362 {
14363   struct insn_label_list *l;
14364
14365   if (free_insn_labels == NULL)
14366     l = (struct insn_label_list *) xmalloc (sizeof *l);
14367   else
14368     {
14369       l = free_insn_labels;
14370       free_insn_labels = l->next;
14371     }
14372
14373   l->label = sym;
14374   l->next = insn_labels;
14375   insn_labels = l;
14376 }
14377 \f
14378 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14379
14380 /* Some special processing for a MIPS ELF file.  */
14381
14382 void
14383 mips_elf_final_processing ()
14384 {
14385   /* Write out the register information.  */
14386   if (mips_abi != N64_ABI)
14387     {
14388       Elf32_RegInfo s;
14389
14390       s.ri_gprmask = mips_gprmask;
14391       s.ri_cprmask[0] = mips_cprmask[0];
14392       s.ri_cprmask[1] = mips_cprmask[1];
14393       s.ri_cprmask[2] = mips_cprmask[2];
14394       s.ri_cprmask[3] = mips_cprmask[3];
14395       /* The gp_value field is set by the MIPS ELF backend.  */
14396
14397       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14398                                        ((Elf32_External_RegInfo *)
14399                                         mips_regmask_frag));
14400     }
14401   else
14402     {
14403       Elf64_Internal_RegInfo s;
14404
14405       s.ri_gprmask = mips_gprmask;
14406       s.ri_pad = 0;
14407       s.ri_cprmask[0] = mips_cprmask[0];
14408       s.ri_cprmask[1] = mips_cprmask[1];
14409       s.ri_cprmask[2] = mips_cprmask[2];
14410       s.ri_cprmask[3] = mips_cprmask[3];
14411       /* The gp_value field is set by the MIPS ELF backend.  */
14412
14413       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14414                                        ((Elf64_External_RegInfo *)
14415                                         mips_regmask_frag));
14416     }
14417
14418   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14419      sort of BFD interface for this.  */
14420   if (mips_any_noreorder)
14421     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14422   if (mips_pic != NO_PIC)
14423     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14424
14425   /* Set MIPS ELF flags for ASEs.  */
14426   if (file_ase_mips16)
14427     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14428 #if 0 /* XXX FIXME */
14429   if (file_ase_mips3d)
14430     elf_elfheader (stdoutput)->e_flags |= ???;
14431 #endif
14432   if (file_ase_mdmx)
14433     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14434
14435   /* Set the MIPS ELF ABI flags.  */
14436   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14437     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14438   else if (mips_abi == O64_ABI)
14439     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14440   else if (mips_abi == EABI_ABI)
14441     {
14442       if (!file_mips_gp32)
14443         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14444       else
14445         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14446     }
14447   else if (mips_abi == N32_ABI)
14448     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14449
14450   /* Nothing to do for N64_ABI.  */
14451
14452   if (mips_32bitmode)
14453     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14454 }
14455
14456 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14457 \f
14458 typedef struct proc {
14459   symbolS *isym;
14460   unsigned long reg_mask;
14461   unsigned long reg_offset;
14462   unsigned long fpreg_mask;
14463   unsigned long fpreg_offset;
14464   unsigned long frame_offset;
14465   unsigned long frame_reg;
14466   unsigned long pc_reg;
14467 } procS;
14468
14469 static procS cur_proc;
14470 static procS *cur_proc_ptr;
14471 static int numprocs;
14472
14473 /* Fill in an rs_align_code fragment.  */
14474
14475 void
14476 mips_handle_align (fragp)
14477      fragS *fragp;
14478 {
14479   if (fragp->fr_type != rs_align_code)
14480     return;
14481
14482   if (mips_opts.mips16)
14483     {
14484       static const unsigned char be_nop[] = { 0x65, 0x00 };
14485       static const unsigned char le_nop[] = { 0x00, 0x65 };
14486
14487       int bytes;
14488       char *p;
14489
14490       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14491       p = fragp->fr_literal + fragp->fr_fix;
14492
14493       if (bytes & 1)
14494         {
14495           *p++ = 0;
14496           fragp->fr_fix++;
14497         }
14498
14499       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14500       fragp->fr_var = 2;
14501     }
14502
14503   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14504 }
14505
14506 static void
14507 md_obj_begin ()
14508 {
14509 }
14510
14511 static void
14512 md_obj_end ()
14513 {
14514   /* check for premature end, nesting errors, etc */
14515   if (cur_proc_ptr)
14516     as_warn (_("missing .end at end of assembly"));
14517 }
14518
14519 static long
14520 get_number ()
14521 {
14522   int negative = 0;
14523   long val = 0;
14524
14525   if (*input_line_pointer == '-')
14526     {
14527       ++input_line_pointer;
14528       negative = 1;
14529     }
14530   if (!ISDIGIT (*input_line_pointer))
14531     as_bad (_("expected simple number"));
14532   if (input_line_pointer[0] == '0')
14533     {
14534       if (input_line_pointer[1] == 'x')
14535         {
14536           input_line_pointer += 2;
14537           while (ISXDIGIT (*input_line_pointer))
14538             {
14539               val <<= 4;
14540               val |= hex_value (*input_line_pointer++);
14541             }
14542           return negative ? -val : val;
14543         }
14544       else
14545         {
14546           ++input_line_pointer;
14547           while (ISDIGIT (*input_line_pointer))
14548             {
14549               val <<= 3;
14550               val |= *input_line_pointer++ - '0';
14551             }
14552           return negative ? -val : val;
14553         }
14554     }
14555   if (!ISDIGIT (*input_line_pointer))
14556     {
14557       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14558               *input_line_pointer, *input_line_pointer);
14559       as_warn (_("invalid number"));
14560       return -1;
14561     }
14562   while (ISDIGIT (*input_line_pointer))
14563     {
14564       val *= 10;
14565       val += *input_line_pointer++ - '0';
14566     }
14567   return negative ? -val : val;
14568 }
14569
14570 /* The .file directive; just like the usual .file directive, but there
14571    is an initial number which is the ECOFF file index.  In the non-ECOFF
14572    case .file implies DWARF-2.  */
14573
14574 static void
14575 s_mips_file (x)
14576      int x ATTRIBUTE_UNUSED;
14577 {
14578   static int first_file_directive = 0;
14579
14580   if (ECOFF_DEBUGGING)
14581     {
14582       get_number ();
14583       s_app_file (0);
14584     }
14585   else
14586     {
14587       char *filename;
14588
14589       filename = dwarf2_directive_file (0);
14590
14591       /* Versions of GCC up to 3.1 start files with a ".file"
14592          directive even for stabs output.  Make sure that this
14593          ".file" is handled.  Note that you need a version of GCC
14594          after 3.1 in order to support DWARF-2 on MIPS.  */
14595       if (filename != NULL && ! first_file_directive)
14596         {
14597           (void) new_logical_line (filename, -1);
14598           s_app_file_string (filename);
14599         }
14600       first_file_directive = 1;
14601     }
14602 }
14603
14604 /* The .loc directive, implying DWARF-2.  */
14605
14606 static void
14607 s_mips_loc (x)
14608      int x ATTRIBUTE_UNUSED;
14609 {
14610   if (!ECOFF_DEBUGGING)
14611     dwarf2_directive_loc (0);
14612 }
14613
14614 /* The .end directive.  */
14615
14616 static void
14617 s_mips_end (x)
14618      int x ATTRIBUTE_UNUSED;
14619 {
14620   symbolS *p;
14621
14622   /* Following functions need their own .frame and .cprestore directives.  */
14623   mips_frame_reg_valid = 0;
14624   mips_cprestore_valid = 0;
14625
14626   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14627     {
14628       p = get_symbol ();
14629       demand_empty_rest_of_line ();
14630     }
14631   else
14632     p = NULL;
14633
14634   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14635     as_warn (_(".end not in text section"));
14636
14637   if (!cur_proc_ptr)
14638     {
14639       as_warn (_(".end directive without a preceding .ent directive."));
14640       demand_empty_rest_of_line ();
14641       return;
14642     }
14643
14644   if (p != NULL)
14645     {
14646       assert (S_GET_NAME (p));
14647       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14648         as_warn (_(".end symbol does not match .ent symbol."));
14649
14650       if (debug_type == DEBUG_STABS)
14651         stabs_generate_asm_endfunc (S_GET_NAME (p),
14652                                     S_GET_NAME (p));
14653     }
14654   else
14655     as_warn (_(".end directive missing or unknown symbol"));
14656
14657 #ifdef OBJ_ELF
14658   /* Generate a .pdr section.  */
14659   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14660     {
14661       segT saved_seg = now_seg;
14662       subsegT saved_subseg = now_subseg;
14663       valueT dot;
14664       expressionS exp;
14665       char *fragp;
14666
14667       dot = frag_now_fix ();
14668
14669 #ifdef md_flush_pending_output
14670       md_flush_pending_output ();
14671 #endif
14672
14673       assert (pdr_seg);
14674       subseg_set (pdr_seg, 0);
14675
14676       /* Write the symbol.  */
14677       exp.X_op = O_symbol;
14678       exp.X_add_symbol = p;
14679       exp.X_add_number = 0;
14680       emit_expr (&exp, 4);
14681
14682       fragp = frag_more (7 * 4);
14683
14684       md_number_to_chars (fragp,      (valueT) cur_proc_ptr->reg_mask, 4);
14685       md_number_to_chars (fragp +  4, (valueT) cur_proc_ptr->reg_offset, 4);
14686       md_number_to_chars (fragp +  8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14687       md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14688       md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14689       md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14690       md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14691
14692       subseg_set (saved_seg, saved_subseg);
14693     }
14694 #endif /* OBJ_ELF */
14695
14696   cur_proc_ptr = NULL;
14697 }
14698
14699 /* The .aent and .ent directives.  */
14700
14701 static void
14702 s_mips_ent (aent)
14703      int aent;
14704 {
14705   symbolS *symbolP;
14706
14707   symbolP = get_symbol ();
14708   if (*input_line_pointer == ',')
14709     ++input_line_pointer;
14710   SKIP_WHITESPACE ();
14711   if (ISDIGIT (*input_line_pointer)
14712       || *input_line_pointer == '-')
14713     get_number ();
14714
14715   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14716     as_warn (_(".ent or .aent not in text section."));
14717
14718   if (!aent && cur_proc_ptr)
14719     as_warn (_("missing .end"));
14720
14721   if (!aent)
14722     {
14723       /* This function needs its own .frame and .cprestore directives.  */
14724       mips_frame_reg_valid = 0;
14725       mips_cprestore_valid = 0;
14726
14727       cur_proc_ptr = &cur_proc;
14728       memset (cur_proc_ptr, '\0', sizeof (procS));
14729
14730       cur_proc_ptr->isym = symbolP;
14731
14732       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14733
14734       ++numprocs;
14735
14736       if (debug_type == DEBUG_STABS)
14737         stabs_generate_asm_func (S_GET_NAME (symbolP),
14738                                  S_GET_NAME (symbolP));
14739     }
14740
14741   demand_empty_rest_of_line ();
14742 }
14743
14744 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14745    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14746    s_mips_frame is used so that we can set the PDR information correctly.
14747    We can't use the ecoff routines because they make reference to the ecoff
14748    symbol table (in the mdebug section).  */
14749
14750 static void
14751 s_mips_frame (ignore)
14752      int ignore ATTRIBUTE_UNUSED;
14753 {
14754 #ifdef OBJ_ELF
14755   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14756     {
14757       long val;
14758
14759       if (cur_proc_ptr == (procS *) NULL)
14760         {
14761           as_warn (_(".frame outside of .ent"));
14762           demand_empty_rest_of_line ();
14763           return;
14764         }
14765
14766       cur_proc_ptr->frame_reg = tc_get_register (1);
14767
14768       SKIP_WHITESPACE ();
14769       if (*input_line_pointer++ != ','
14770           || get_absolute_expression_and_terminator (&val) != ',')
14771         {
14772           as_warn (_("Bad .frame directive"));
14773           --input_line_pointer;
14774           demand_empty_rest_of_line ();
14775           return;
14776         }
14777
14778       cur_proc_ptr->frame_offset = val;
14779       cur_proc_ptr->pc_reg = tc_get_register (0);
14780
14781       demand_empty_rest_of_line ();
14782     }
14783   else
14784 #endif /* OBJ_ELF */
14785     s_ignore (ignore);
14786 }
14787
14788 /* The .fmask and .mask directives. If the mdebug section is present
14789    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14790    embedded targets, s_mips_mask is used so that we can set the PDR
14791    information correctly. We can't use the ecoff routines because they
14792    make reference to the ecoff symbol table (in the mdebug section).  */
14793
14794 static void
14795 s_mips_mask (reg_type)
14796      char reg_type;
14797 {
14798 #ifdef OBJ_ELF
14799   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14800     {
14801       long mask, off;
14802
14803       if (cur_proc_ptr == (procS *) NULL)
14804         {
14805           as_warn (_(".mask/.fmask outside of .ent"));
14806           demand_empty_rest_of_line ();
14807           return;
14808         }
14809
14810       if (get_absolute_expression_and_terminator (&mask) != ',')
14811         {
14812           as_warn (_("Bad .mask/.fmask directive"));
14813           --input_line_pointer;
14814           demand_empty_rest_of_line ();
14815           return;
14816         }
14817
14818       off = get_absolute_expression ();
14819
14820       if (reg_type == 'F')
14821         {
14822           cur_proc_ptr->fpreg_mask = mask;
14823           cur_proc_ptr->fpreg_offset = off;
14824         }
14825       else
14826         {
14827           cur_proc_ptr->reg_mask = mask;
14828           cur_proc_ptr->reg_offset = off;
14829         }
14830
14831       demand_empty_rest_of_line ();
14832     }
14833   else
14834 #endif /* OBJ_ELF */
14835     s_ignore (reg_type);
14836 }
14837
14838 /* The .loc directive.  */
14839
14840 #if 0
14841 static void
14842 s_loc (x)
14843      int x;
14844 {
14845   symbolS *symbolP;
14846   int lineno;
14847   int addroff;
14848
14849   assert (now_seg == text_section);
14850
14851   lineno = get_number ();
14852   addroff = frag_now_fix ();
14853
14854   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14855   S_SET_TYPE (symbolP, N_SLINE);
14856   S_SET_OTHER (symbolP, 0);
14857   S_SET_DESC (symbolP, lineno);
14858   symbolP->sy_segment = now_seg;
14859 }
14860 #endif
14861
14862 /* A table describing all the processors gas knows about.  Names are
14863    matched in the order listed.
14864
14865    To ease comparison, please keep this table in the same order as
14866    gcc's mips_cpu_info_table[].  */
14867 static const struct mips_cpu_info mips_cpu_info_table[] =
14868 {
14869   /* Entries for generic ISAs */
14870   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14871   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14872   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14873   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14874   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14875   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14876   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14877   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14878
14879   /* MIPS I */
14880   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14881   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14882   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14883
14884   /* MIPS II */
14885   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14886
14887   /* MIPS III */
14888   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14889   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14890   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14891   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14892   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14893   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14894   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14895   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14896   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14897   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14898   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14899   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14900
14901   /* MIPS IV */
14902   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14903   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14904   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14905   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14906   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14907   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14908   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14909   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14910   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14911   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14912   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14913   { "r7000",          0,      ISA_MIPS4,      CPU_R5000 },
14914
14915   /* MIPS 32 */
14916   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32, },
14917   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14918   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14919
14920   /* MIPS 64 */
14921   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14922   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14923
14924   /* Broadcom SB-1 CPU core */
14925   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14926
14927   /* End marker */
14928   { NULL, 0, 0, 0 }
14929 };
14930
14931
14932 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14933    with a final "000" replaced by "k".  Ignore case.
14934
14935    Note: this function is shared between GCC and GAS.  */
14936
14937 static bfd_boolean
14938 mips_strict_matching_cpu_name_p (canonical, given)
14939      const char *canonical, *given;
14940 {
14941   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14942     given++, canonical++;
14943
14944   return ((*given == 0 && *canonical == 0)
14945           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14946 }
14947
14948
14949 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14950    CPU name.  We've traditionally allowed a lot of variation here.
14951
14952    Note: this function is shared between GCC and GAS.  */
14953
14954 static bfd_boolean
14955 mips_matching_cpu_name_p (canonical, given)
14956      const char *canonical, *given;
14957 {
14958   /* First see if the name matches exactly, or with a final "000"
14959      turned into "k".  */
14960   if (mips_strict_matching_cpu_name_p (canonical, given))
14961     return TRUE;
14962
14963   /* If not, try comparing based on numerical designation alone.
14964      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14965   if (TOLOWER (*given) == 'r')
14966     given++;
14967   if (!ISDIGIT (*given))
14968     return FALSE;
14969
14970   /* Skip over some well-known prefixes in the canonical name,
14971      hoping to find a number there too.  */
14972   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14973     canonical += 2;
14974   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14975     canonical += 2;
14976   else if (TOLOWER (canonical[0]) == 'r')
14977     canonical += 1;
14978
14979   return mips_strict_matching_cpu_name_p (canonical, given);
14980 }
14981
14982
14983 /* Parse an option that takes the name of a processor as its argument.
14984    OPTION is the name of the option and CPU_STRING is the argument.
14985    Return the corresponding processor enumeration if the CPU_STRING is
14986    recognized, otherwise report an error and return null.
14987
14988    A similar function exists in GCC.  */
14989
14990 static const struct mips_cpu_info *
14991 mips_parse_cpu (option, cpu_string)
14992      const char *option, *cpu_string;
14993 {
14994   const struct mips_cpu_info *p;
14995
14996   /* 'from-abi' selects the most compatible architecture for the given
14997      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14998      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14999      version.  Look first at the -mgp options, if given, otherwise base
15000      the choice on MIPS_DEFAULT_64BIT.
15001
15002      Treat NO_ABI like the EABIs.  One reason to do this is that the
15003      plain 'mips' and 'mips64' configs have 'from-abi' as their default
15004      architecture.  This code picks MIPS I for 'mips' and MIPS III for
15005      'mips64', just as we did in the days before 'from-abi'.  */
15006   if (strcasecmp (cpu_string, "from-abi") == 0)
15007     {
15008       if (ABI_NEEDS_32BIT_REGS (mips_abi))
15009         return mips_cpu_info_from_isa (ISA_MIPS1);
15010
15011       if (ABI_NEEDS_64BIT_REGS (mips_abi))
15012         return mips_cpu_info_from_isa (ISA_MIPS3);
15013
15014       if (file_mips_gp32 >= 0)
15015         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
15016
15017       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
15018                                      ? ISA_MIPS3
15019                                      : ISA_MIPS1);
15020     }
15021
15022   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
15023   if (strcasecmp (cpu_string, "default") == 0)
15024     return 0;
15025
15026   for (p = mips_cpu_info_table; p->name != 0; p++)
15027     if (mips_matching_cpu_name_p (p->name, cpu_string))
15028       return p;
15029
15030   as_bad ("Bad value (%s) for %s", cpu_string, option);
15031   return 0;
15032 }
15033
15034 /* Return the canonical processor information for ISA (a member of the
15035    ISA_MIPS* enumeration).  */
15036
15037 static const struct mips_cpu_info *
15038 mips_cpu_info_from_isa (isa)
15039      int isa;
15040 {
15041   int i;
15042
15043   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15044     if (mips_cpu_info_table[i].is_isa
15045         && isa == mips_cpu_info_table[i].isa)
15046       return (&mips_cpu_info_table[i]);
15047
15048   return NULL;
15049 }
15050 \f
15051 static void
15052 show (stream, string, col_p, first_p)
15053      FILE *stream;
15054      const char *string;
15055      int *col_p;
15056      int *first_p;
15057 {
15058   if (*first_p)
15059     {
15060       fprintf (stream, "%24s", "");
15061       *col_p = 24;
15062     }
15063   else
15064     {
15065       fprintf (stream, ", ");
15066       *col_p += 2;
15067     }
15068
15069   if (*col_p + strlen (string) > 72)
15070     {
15071       fprintf (stream, "\n%24s", "");
15072       *col_p = 24;
15073     }
15074
15075   fprintf (stream, "%s", string);
15076   *col_p += strlen (string);
15077
15078   *first_p = 0;
15079 }
15080
15081 void
15082 md_show_usage (stream)
15083      FILE *stream;
15084 {
15085   int column, first;
15086   size_t i;
15087
15088   fprintf (stream, _("\
15089 MIPS options:\n\
15090 -membedded-pic          generate embedded position independent code\n\
15091 -EB                     generate big endian output\n\
15092 -EL                     generate little endian output\n\
15093 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
15094 -G NUM                  allow referencing objects up to NUM bytes\n\
15095                         implicitly with the gp register [default 8]\n"));
15096   fprintf (stream, _("\
15097 -mips1                  generate MIPS ISA I instructions\n\
15098 -mips2                  generate MIPS ISA II instructions\n\
15099 -mips3                  generate MIPS ISA III instructions\n\
15100 -mips4                  generate MIPS ISA IV instructions\n\
15101 -mips5                  generate MIPS ISA V instructions\n\
15102 -mips32                 generate MIPS32 ISA instructions\n\
15103 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
15104 -mips64                 generate MIPS64 ISA instructions\n\
15105 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
15106
15107   first = 1;
15108
15109   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15110     show (stream, mips_cpu_info_table[i].name, &column, &first);
15111   show (stream, "from-abi", &column, &first);
15112   fputc ('\n', stream);
15113
15114   fprintf (stream, _("\
15115 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15116 -no-mCPU                don't generate code specific to CPU.\n\
15117                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
15118
15119   first = 1;
15120
15121   show (stream, "3900", &column, &first);
15122   show (stream, "4010", &column, &first);
15123   show (stream, "4100", &column, &first);
15124   show (stream, "4650", &column, &first);
15125   fputc ('\n', stream);
15126
15127   fprintf (stream, _("\
15128 -mips16                 generate mips16 instructions\n\
15129 -no-mips16              do not generate mips16 instructions\n"));
15130   fprintf (stream, _("\
15131 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
15132 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
15133 -O0                     remove unneeded NOPs, do not swap branches\n\
15134 -O                      remove unneeded NOPs and swap branches\n\
15135 -n                      warn about NOPs generated from macros\n\
15136 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
15137 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
15138 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
15139 #ifdef OBJ_ELF
15140   fprintf (stream, _("\
15141 -KPIC, -call_shared     generate SVR4 position independent code\n\
15142 -non_shared             do not generate position independent code\n\
15143 -xgot                   assume a 32 bit GOT\n\
15144 -mabi=ABI               create ABI conformant object file for:\n"));
15145
15146   first = 1;
15147
15148   show (stream, "32", &column, &first);
15149   show (stream, "o64", &column, &first);
15150   show (stream, "n32", &column, &first);
15151   show (stream, "64", &column, &first);
15152   show (stream, "eabi", &column, &first);
15153
15154   fputc ('\n', stream);
15155
15156   fprintf (stream, _("\
15157 -32                     create o32 ABI object file (default)\n\
15158 -n32                    create n32 ABI object file\n\
15159 -64                     create 64 ABI object file\n"));
15160 #endif
15161 }
15162
15163 enum dwarf2_format
15164 mips_dwarf2_format ()
15165 {
15166   if (mips_abi == N64_ABI)
15167     {
15168 #ifdef TE_IRIX
15169       return dwarf2_format_64bit_irix;
15170 #else
15171       return dwarf2_format_64bit;
15172 #endif
15173     }
15174   else
15175     return dwarf2_format_32bit;
15176 }