bfd/
[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 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too.  */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about.  */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections.  Off by default on IRIX: the native
78    linker doesn't know about and discards them, but relocations against them
79    remain, leading to rld crashes.  */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT  1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP  28
99 #define SP  29
100 #define FP  30
101 #define RA  31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format.  */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section.  */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
115                             ? ".data" \
116                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
117                             ? ".rdata" \
118                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
119                             ? ".rdata" \
120                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
121                             ? ".rodata" \
122                             : (abort (), ""))
123
124 /* The ABI to use.  */
125 enum mips_abi_level
126 {
127   NO_ABI = 0,
128   O32_ABI,
129   O64_ABI,
130   N32_ABI,
131   N64_ABI,
132   EABI_ABI
133 };
134
135 /* MIPS ABI we are using for this output file.  */
136 static enum mips_abi_level mips_abi = NO_ABI;
137
138 /* Whether or not we have code that can call pic code.  */
139 int mips_abicalls = FALSE;
140
141 /* This is the set of options which may be modified by the .set
142    pseudo-op.  We use a struct so that .set push and .set pop are more
143    reliable.  */
144
145 struct mips_set_options
146 {
147   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
148      if it has not been initialized.  Changed by `.set mipsN', and the
149      -mipsN command line option, and the default CPU.  */
150   int isa;
151   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
152      if they have not been initialized.  Changed by `.set <asename>', by
153      command line options, and based on the default architecture.  */
154   int ase_mips3d;
155   int ase_mdmx;
156   /* Whether we are assembling for the mips16 processor.  0 if we are
157      not, 1 if we are, and -1 if the value has not been initialized.
158      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
159      -nomips16 command line options, and the default CPU.  */
160   int mips16;
161   /* Non-zero if we should not reorder instructions.  Changed by `.set
162      reorder' and `.set noreorder'.  */
163   int noreorder;
164   /* Non-zero if we should not permit the $at ($1) register to be used
165      in instructions.  Changed by `.set at' and `.set noat'.  */
166   int noat;
167   /* Non-zero if we should warn when a macro instruction expands into
168      more than one machine instruction.  Changed by `.set nomacro' and
169      `.set macro'.  */
170   int warn_about_macros;
171   /* Non-zero if we should not move instructions.  Changed by `.set
172      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
173   int nomove;
174   /* Non-zero if we should not optimize branches by moving the target
175      of the branch into the delay slot.  Actually, we don't perform
176      this optimization anyhow.  Changed by `.set bopt' and `.set
177      nobopt'.  */
178   int nobopt;
179   /* Non-zero if we should not autoextend mips16 instructions.
180      Changed by `.set autoextend' and `.set noautoextend'.  */
181   int noautoextend;
182   /* Restrict general purpose registers and floating point registers
183      to 32 bit.  This is initially determined when -mgp32 or -mfp32
184      is passed but can changed if the assembler code uses .set mipsN.  */
185   int gp32;
186   int fp32;
187   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
188      command line option, and the default CPU.  */
189   int arch;
190 };
191
192 /* True if -mgp32 was passed.  */
193 static int file_mips_gp32 = -1;
194
195 /* True if -mfp32 was passed.  */
196 static int file_mips_fp32 = -1;
197
198 /* This is the struct we use to hold the current set of options.  Note
199    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
200    -1 to indicate that they have not been initialized.  */
201
202 static struct mips_set_options mips_opts =
203 {
204   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
205 };
206
207 /* These variables are filled in with the masks of registers used.
208    The object format code reads them and puts them in the appropriate
209    place.  */
210 unsigned long mips_gprmask;
211 unsigned long mips_cprmask[4];
212
213 /* MIPS ISA we are using for this output file.  */
214 static int file_mips_isa = ISA_UNKNOWN;
215
216 /* True if -mips16 was passed or implied by arguments passed on the
217    command line (e.g., by -march).  */
218 static int file_ase_mips16;
219
220 /* True if -mips3d was passed or implied by arguments passed on the
221    command line (e.g., by -march).  */
222 static int file_ase_mips3d;
223
224 /* True if -mdmx was passed or implied by arguments passed on the
225    command line (e.g., by -march).  */
226 static int file_ase_mdmx;
227
228 /* The argument of the -march= flag.  The architecture we are assembling.  */
229 static int file_mips_arch = CPU_UNKNOWN;
230 static const char *mips_arch_string;
231
232 /* The argument of the -mtune= flag.  The architecture for which we
233    are optimizing.  */
234 static int mips_tune = CPU_UNKNOWN;
235 static const char *mips_tune_string;
236
237 /* True when generating 32-bit code for a 64-bit processor.  */
238 static int mips_32bitmode = 0;
239
240 /* Some ISA's have delay slots for instructions which read or write
241    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
242    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
243    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
244    delay slot in this ISA.  The uses of this macro assume that any
245    ISA that has delay slots for one of these, has them for all.  They
246    also assume that ISAs which don't have delays for these insns, don't
247    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
248 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
249    (ISA) == ISA_MIPS1                       \
250    || (ISA) == ISA_MIPS2                    \
251    || (ISA) == ISA_MIPS3                    \
252    )
253
254 /* True if the given ABI requires 32-bit registers.  */
255 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
256
257 /* Likewise 64-bit registers.  */
258 #define ABI_NEEDS_64BIT_REGS(ABI) \
259   ((ABI) == N32_ABI               \
260    || (ABI) == N64_ABI            \
261    || (ABI) == O64_ABI)
262
263 /*  Return true if ISA supports 64 bit gp register instructions.  */
264 #define ISA_HAS_64BIT_REGS(ISA) (    \
265    (ISA) == ISA_MIPS3                \
266    || (ISA) == ISA_MIPS4             \
267    || (ISA) == ISA_MIPS5             \
268    || (ISA) == ISA_MIPS64            \
269    || (ISA) == ISA_MIPS64R2          \
270    )
271
272 /* Return true if ISA supports 64-bit right rotate (dror et al.)
273    instructions.  */
274 #define ISA_HAS_DROR(ISA) (     \
275    (ISA) == ISA_MIPS64R2        \
276    )
277
278 /* Return true if ISA supports 32-bit right rotate (ror et al.)
279    instructions.  */
280 #define ISA_HAS_ROR(ISA) (      \
281    (ISA) == ISA_MIPS32R2        \
282    || (ISA) == ISA_MIPS64R2     \
283    )
284
285 #define HAVE_32BIT_GPRS                            \
286     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
287
288 #define HAVE_32BIT_FPRS                            \
289     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
290
291 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
292 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
293
294 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
295
296 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
297
298 /* We can only have 64bit addresses if the object file format
299    supports it.  */
300 #define HAVE_32BIT_ADDRESSES                           \
301    (HAVE_32BIT_GPRS                                    \
302     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
303          || ! HAVE_64BIT_OBJECTS)                      \
304         && mips_pic != EMBEDDED_PIC))
305
306 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
307
308 /* Addresses are loaded in different ways, depending on the address size
309    in use.  The n32 ABI Documentation also mandates the use of additions
310    with overflow checking, but existing implementations don't follow it.  */
311 #define ADDRESS_ADD_INSN                                                \
312    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
313
314 #define ADDRESS_ADDI_INSN                                               \
315    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
316
317 #define ADDRESS_LOAD_INSN                                               \
318    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
319
320 #define ADDRESS_STORE_INSN                                              \
321    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
322
323 /* Return true if the given CPU supports the MIPS16 ASE.  */
324 #define CPU_HAS_MIPS16(cpu)                                             \
325    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
326     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
327
328 /* Return true if the given CPU supports the MIPS3D ASE.  */
329 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
330                                  )
331
332 /* Return true if the given CPU supports the MDMX ASE.  */
333 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
334                                  )
335
336 /* True if CPU has a dror instruction.  */
337 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
338
339 /* True if CPU has a ror instruction.  */
340 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
341
342 /* Whether the processor uses hardware interlocks to protect
343    reads from the HI and LO registers, and thus does not
344    require nops to be inserted.  */
345
346 #define hilo_interlocks (mips_opts.arch == CPU_R4010                       \
347                          || mips_opts.arch == CPU_VR5500                   \
348                          || mips_opts.arch == CPU_RM7000                   \
349                          || mips_opts.arch == CPU_SB1                      \
350                          )
351
352 /* Whether the processor uses hardware interlocks to protect reads
353    from the GPRs, and thus does not require nops to be inserted.  */
354 #define gpr_interlocks \
355   (mips_opts.isa != ISA_MIPS1  \
356    || mips_opts.arch == CPU_VR5400  \
357    || mips_opts.arch == CPU_VR5500  \
358    || mips_opts.arch == CPU_R3900)
359
360 /* As with other "interlocks" this is used by hardware that has FP
361    (co-processor) interlocks.  */
362 /* Itbl support may require additional care here.  */
363 #define cop_interlocks (mips_opts.arch == CPU_R4300                        \
364                         || mips_opts.arch == CPU_VR5400                    \
365                         || mips_opts.arch == CPU_VR5500                    \
366                         || mips_opts.arch == CPU_SB1                       \
367                         )
368
369 /* Is this a mfhi or mflo instruction?  */
370 #define MF_HILO_INSN(PINFO) \
371           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
372
373 /* MIPS PIC level.  */
374
375 enum mips_pic_level mips_pic;
376
377 /* Warn about all NOPS that the assembler generates.  */
378 static int warn_nops = 0;
379
380 /* 1 if we should generate 32 bit offsets from the $gp register in
381    SVR4_PIC mode.  Currently has no meaning in other modes.  */
382 static int mips_big_got = 0;
383
384 /* 1 if trap instructions should used for overflow rather than break
385    instructions.  */
386 static int mips_trap = 0;
387
388 /* 1 if double width floating point constants should not be constructed
389    by assembling two single width halves into two single width floating
390    point registers which just happen to alias the double width destination
391    register.  On some architectures this aliasing can be disabled by a bit
392    in the status register, and the setting of this bit cannot be determined
393    automatically at assemble time.  */
394 static int mips_disable_float_construction;
395
396 /* Non-zero if any .set noreorder directives were used.  */
397
398 static int mips_any_noreorder;
399
400 /* Non-zero if nops should be inserted when the register referenced in
401    an mfhi/mflo instruction is read in the next two instructions.  */
402 static int mips_7000_hilo_fix;
403
404 /* The size of the small data section.  */
405 static unsigned int g_switch_value = 8;
406 /* Whether the -G option was used.  */
407 static int g_switch_seen = 0;
408
409 #define N_RMASK 0xc4
410 #define N_VFP   0xd4
411
412 /* If we can determine in advance that GP optimization won't be
413    possible, we can skip the relaxation stuff that tries to produce
414    GP-relative references.  This makes delay slot optimization work
415    better.
416
417    This function can only provide a guess, but it seems to work for
418    gcc output.  It needs to guess right for gcc, otherwise gcc
419    will put what it thinks is a GP-relative instruction in a branch
420    delay slot.
421
422    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
423    fixed it for the non-PIC mode.  KR 95/04/07  */
424 static int nopic_need_relax (symbolS *, int);
425
426 /* handle of the OPCODE hash table */
427 static struct hash_control *op_hash = NULL;
428
429 /* The opcode hash table we use for the mips16.  */
430 static struct hash_control *mips16_op_hash = NULL;
431
432 /* This array holds the chars that always start a comment.  If the
433     pre-processor is disabled, these aren't very useful */
434 const char comment_chars[] = "#";
435
436 /* This array holds the chars that only start a comment at the beginning of
437    a line.  If the line seems to have the form '# 123 filename'
438    .line and .file directives will appear in the pre-processed output */
439 /* Note that input_file.c hand checks for '#' at the beginning of the
440    first line of the input file.  This is because the compiler outputs
441    #NO_APP at the beginning of its output.  */
442 /* Also note that C style comments are always supported.  */
443 const char line_comment_chars[] = "#";
444
445 /* This array holds machine specific line separator characters.  */
446 const char line_separator_chars[] = ";";
447
448 /* Chars that can be used to separate mant from exp in floating point nums */
449 const char EXP_CHARS[] = "eE";
450
451 /* Chars that mean this number is a floating point constant */
452 /* As in 0f12.456 */
453 /* or    0d1.2345e12 */
454 const char FLT_CHARS[] = "rRsSfFdDxXpP";
455
456 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
457    changed in read.c .  Ideally it shouldn't have to know about it at all,
458    but nothing is ideal around here.
459  */
460
461 static char *insn_error;
462
463 static int auto_align = 1;
464
465 /* When outputting SVR4 PIC code, the assembler needs to know the
466    offset in the stack frame from which to restore the $gp register.
467    This is set by the .cprestore pseudo-op, and saved in this
468    variable.  */
469 static offsetT mips_cprestore_offset = -1;
470
471 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
472    more optimizations, it can use a register value instead of a memory-saved
473    offset and even an other register than $gp as global pointer.  */
474 static offsetT mips_cpreturn_offset = -1;
475 static int mips_cpreturn_register = -1;
476 static int mips_gp_register = GP;
477 static int mips_gprel_offset = 0;
478
479 /* Whether mips_cprestore_offset has been set in the current function
480    (or whether it has already been warned about, if not).  */
481 static int mips_cprestore_valid = 0;
482
483 /* This is the register which holds the stack frame, as set by the
484    .frame pseudo-op.  This is needed to implement .cprestore.  */
485 static int mips_frame_reg = SP;
486
487 /* Whether mips_frame_reg has been set in the current function
488    (or whether it has already been warned about, if not).  */
489 static int mips_frame_reg_valid = 0;
490
491 /* To output NOP instructions correctly, we need to keep information
492    about the previous two instructions.  */
493
494 /* Whether we are optimizing.  The default value of 2 means to remove
495    unneeded NOPs and swap branch instructions when possible.  A value
496    of 1 means to not swap branches.  A value of 0 means to always
497    insert NOPs.  */
498 static int mips_optimize = 2;
499
500 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
501    equivalent to seeing no -g option at all.  */
502 static int mips_debug = 0;
503
504 /* The previous instruction.  */
505 static struct mips_cl_insn prev_insn;
506
507 /* The instruction before prev_insn.  */
508 static struct mips_cl_insn prev_prev_insn;
509
510 /* If we don't want information for prev_insn or prev_prev_insn, we
511    point the insn_mo field at this dummy integer.  */
512 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
513
514 /* Non-zero if prev_insn is valid.  */
515 static int prev_insn_valid;
516
517 /* The frag for the previous instruction.  */
518 static struct frag *prev_insn_frag;
519
520 /* The offset into prev_insn_frag for the previous instruction.  */
521 static long prev_insn_where;
522
523 /* The reloc type for the previous instruction, if any.  */
524 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
525
526 /* The reloc for the previous instruction, if any.  */
527 static fixS *prev_insn_fixp[3];
528
529 /* Non-zero if the previous instruction was in a delay slot.  */
530 static int prev_insn_is_delay_slot;
531
532 /* Non-zero if the previous instruction was in a .set noreorder.  */
533 static int prev_insn_unreordered;
534
535 /* Non-zero if the previous instruction uses an extend opcode (if
536    mips16).  */
537 static int prev_insn_extended;
538
539 /* Non-zero if the previous previous instruction was in a .set
540    noreorder.  */
541 static int prev_prev_insn_unreordered;
542
543 /* If this is set, it points to a frag holding nop instructions which
544    were inserted before the start of a noreorder section.  If those
545    nops turn out to be unnecessary, the size of the frag can be
546    decreased.  */
547 static fragS *prev_nop_frag;
548
549 /* The number of nop instructions we created in prev_nop_frag.  */
550 static int prev_nop_frag_holds;
551
552 /* The number of nop instructions that we know we need in
553    prev_nop_frag.  */
554 static int prev_nop_frag_required;
555
556 /* The number of instructions we've seen since prev_nop_frag.  */
557 static int prev_nop_frag_since;
558
559 /* For ECOFF and ELF, relocations against symbols are done in two
560    parts, with a HI relocation and a LO relocation.  Each relocation
561    has only 16 bits of space to store an addend.  This means that in
562    order for the linker to handle carries correctly, it must be able
563    to locate both the HI and the LO relocation.  This means that the
564    relocations must appear in order in the relocation table.
565
566    In order to implement this, we keep track of each unmatched HI
567    relocation.  We then sort them so that they immediately precede the
568    corresponding LO relocation.  */
569
570 struct mips_hi_fixup
571 {
572   /* Next HI fixup.  */
573   struct mips_hi_fixup *next;
574   /* This fixup.  */
575   fixS *fixp;
576   /* The section this fixup is in.  */
577   segT seg;
578 };
579
580 /* The list of unmatched HI relocs.  */
581
582 static struct mips_hi_fixup *mips_hi_fixup_list;
583
584 /* The frag containing the last explicit relocation operator.
585    Null if explicit relocations have not been used.  */
586
587 static fragS *prev_reloc_op_frag;
588
589 /* Map normal MIPS register numbers to mips16 register numbers.  */
590
591 #define X ILLEGAL_REG
592 static const int mips32_to_16_reg_map[] =
593 {
594   X, X, 2, 3, 4, 5, 6, 7,
595   X, X, X, X, X, X, X, X,
596   0, 1, X, X, X, X, X, X,
597   X, X, X, X, X, X, X, X
598 };
599 #undef X
600
601 /* Map mips16 register numbers to normal MIPS register numbers.  */
602
603 static const unsigned int mips16_to_32_reg_map[] =
604 {
605   16, 17, 2, 3, 4, 5, 6, 7
606 };
607
608 static int mips_fix_4122_bugs;
609
610 /* We don't relax branches by default, since this causes us to expand
611    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
612    fail to compute the offset before expanding the macro to the most
613    efficient expansion.  */
614
615 static int mips_relax_branch;
616 \f
617 /* Since the MIPS does not have multiple forms of PC relative
618    instructions, we do not have to do relaxing as is done on other
619    platforms.  However, we do have to handle GP relative addressing
620    correctly, which turns out to be a similar problem.
621
622    Every macro that refers to a symbol can occur in (at least) two
623    forms, one with GP relative addressing and one without.  For
624    example, loading a global variable into a register generally uses
625    a macro instruction like this:
626      lw $4,i
627    If i can be addressed off the GP register (this is true if it is in
628    the .sbss or .sdata section, or if it is known to be smaller than
629    the -G argument) this will generate the following instruction:
630      lw $4,i($gp)
631    This instruction will use a GPREL reloc.  If i can not be addressed
632    off the GP register, the following instruction sequence will be used:
633      lui $at,i
634      lw $4,i($at)
635    In this case the first instruction will have a HI16 reloc, and the
636    second reloc will have a LO16 reloc.  Both relocs will be against
637    the symbol i.
638
639    The issue here is that we may not know whether i is GP addressable
640    until after we see the instruction that uses it.  Therefore, we
641    want to be able to choose the final instruction sequence only at
642    the end of the assembly.  This is similar to the way other
643    platforms choose the size of a PC relative instruction only at the
644    end of assembly.
645
646    When generating position independent code we do not use GP
647    addressing in quite the same way, but the issue still arises as
648    external symbols and local symbols must be handled differently.
649
650    We handle these issues by actually generating both possible
651    instruction sequences.  The longer one is put in a frag_var with
652    type rs_machine_dependent.  We encode what to do with the frag in
653    the subtype field.  We encode (1) the number of existing bytes to
654    replace, (2) the number of new bytes to use, (3) the offset from
655    the start of the existing bytes to the first reloc we must generate
656    (that is, the offset is applied from the start of the existing
657    bytes after they are replaced by the new bytes, if any), (4) the
658    offset from the start of the existing bytes to the second reloc,
659    (5) whether a third reloc is needed (the third reloc is always four
660    bytes after the second reloc), and (6) whether to warn if this
661    variant is used (this is sometimes needed if .set nomacro or .set
662    noat is in effect).  All these numbers are reasonably small.
663
664    Generating two instruction sequences must be handled carefully to
665    ensure that delay slots are handled correctly.  Fortunately, there
666    are a limited number of cases.  When the second instruction
667    sequence is generated, append_insn is directed to maintain the
668    existing delay slot information, so it continues to apply to any
669    code after the second instruction sequence.  This means that the
670    second instruction sequence must not impose any requirements not
671    required by the first instruction sequence.
672
673    These variant frags are then handled in functions called by the
674    machine independent code.  md_estimate_size_before_relax returns
675    the final size of the frag.  md_convert_frag sets up the final form
676    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
677    one if needed.  */
678 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
679   ((relax_substateT) \
680    (((old) << 23) \
681     | ((new) << 16) \
682     | (((reloc1) + 64) << 9) \
683     | (((reloc2) + 64) << 2) \
684     | ((reloc3) ? (1 << 1) : 0) \
685     | ((warn) ? 1 : 0)))
686 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
687 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
688 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
689 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
690 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
691 #define RELAX_WARN(i) ((i) & 1)
692
693 /* Branch without likely bit.  If label is out of range, we turn:
694
695         beq reg1, reg2, label
696         delay slot
697
698    into
699
700         bne reg1, reg2, 0f
701         nop
702         j label
703      0: delay slot
704
705    with the following opcode replacements:
706
707         beq <-> bne
708         blez <-> bgtz
709         bltz <-> bgez
710         bc1f <-> bc1t
711
712         bltzal <-> bgezal  (with jal label instead of j label)
713
714    Even though keeping the delay slot instruction in the delay slot of
715    the branch would be more efficient, it would be very tricky to do
716    correctly, because we'd have to introduce a variable frag *after*
717    the delay slot instruction, and expand that instead.  Let's do it
718    the easy way for now, even if the branch-not-taken case now costs
719    one additional instruction.  Out-of-range branches are not supposed
720    to be common, anyway.
721
722    Branch likely.  If label is out of range, we turn:
723
724         beql reg1, reg2, label
725         delay slot (annulled if branch not taken)
726
727    into
728
729         beql reg1, reg2, 1f
730         nop
731         beql $0, $0, 2f
732         nop
733      1: j[al] label
734         delay slot (executed only if branch taken)
735      2:
736
737    It would be possible to generate a shorter sequence by losing the
738    likely bit, generating something like:
739
740         bne reg1, reg2, 0f
741         nop
742         j[al] label
743         delay slot (executed only if branch taken)
744      0:
745
746         beql -> bne
747         bnel -> beq
748         blezl -> bgtz
749         bgtzl -> blez
750         bltzl -> bgez
751         bgezl -> bltz
752         bc1fl -> bc1t
753         bc1tl -> bc1f
754
755         bltzall -> bgezal  (with jal label instead of j label)
756         bgezall -> bltzal  (ditto)
757
758
759    but it's not clear that it would actually improve performance.  */
760 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
761   ((relax_substateT) \
762    (0xc0000000 \
763     | ((toofar) ? 1 : 0) \
764     | ((link) ? 2 : 0) \
765     | ((likely) ? 4 : 0) \
766     | ((uncond) ? 8 : 0)))
767 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
768 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
769 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
770 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
771 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
772
773 /* For mips16 code, we use an entirely different form of relaxation.
774    mips16 supports two versions of most instructions which take
775    immediate values: a small one which takes some small value, and a
776    larger one which takes a 16 bit value.  Since branches also follow
777    this pattern, relaxing these values is required.
778
779    We can assemble both mips16 and normal MIPS code in a single
780    object.  Therefore, we need to support this type of relaxation at
781    the same time that we support the relaxation described above.  We
782    use the high bit of the subtype field to distinguish these cases.
783
784    The information we store for this type of relaxation is the
785    argument code found in the opcode file for this relocation, whether
786    the user explicitly requested a small or extended form, and whether
787    the relocation is in a jump or jal delay slot.  That tells us the
788    size of the value, and how it should be stored.  We also store
789    whether the fragment is considered to be extended or not.  We also
790    store whether this is known to be a branch to a different section,
791    whether we have tried to relax this frag yet, and whether we have
792    ever extended a PC relative fragment because of a shift count.  */
793 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
794   (0x80000000                                                   \
795    | ((type) & 0xff)                                            \
796    | ((small) ? 0x100 : 0)                                      \
797    | ((ext) ? 0x200 : 0)                                        \
798    | ((dslot) ? 0x400 : 0)                                      \
799    | ((jal_dslot) ? 0x800 : 0))
800 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
801 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
802 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
803 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
804 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
805 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
806 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
807 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
808 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
809 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
810 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
811 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
812
813 /* Is the given value a sign-extended 32-bit value?  */
814 #define IS_SEXT_32BIT_NUM(x)                                            \
815   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
816    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
817
818 /* Is the given value a sign-extended 16-bit value?  */
819 #define IS_SEXT_16BIT_NUM(x)                                            \
820   (((x) &~ (offsetT) 0x7fff) == 0                                       \
821    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
822
823 \f
824 /* Prototypes for static functions.  */
825
826 #define internalError()                                                 \
827     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
828
829 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
830
831 static void append_insn
832   (char *place, struct mips_cl_insn *ip, expressionS *p,
833    bfd_reloc_code_real_type *r);
834 static void mips_no_prev_insn (int);
835 static void mips16_macro_build
836   (char *, int *, expressionS *, const char *, const char *, va_list);
837 static void load_register (int *, int, expressionS *, int);
838 static void macro (struct mips_cl_insn * ip);
839 static void mips16_macro (struct mips_cl_insn * ip);
840 #ifdef LOSING_COMPILER
841 static void macro2 (struct mips_cl_insn * ip);
842 #endif
843 static void mips_ip (char *str, struct mips_cl_insn * ip);
844 static void mips16_ip (char *str, struct mips_cl_insn * ip);
845 static void mips16_immed
846   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
847    unsigned long *, bfd_boolean *, unsigned short *);
848 static size_t my_getSmallExpression
849   (expressionS *, bfd_reloc_code_real_type *, char *);
850 static void my_getExpression (expressionS *, char *);
851 static void s_align (int);
852 static void s_change_sec (int);
853 static void s_change_section (int);
854 static void s_cons (int);
855 static void s_float_cons (int);
856 static void s_mips_globl (int);
857 static void s_option (int);
858 static void s_mipsset (int);
859 static void s_abicalls (int);
860 static void s_cpload (int);
861 static void s_cpsetup (int);
862 static void s_cplocal (int);
863 static void s_cprestore (int);
864 static void s_cpreturn (int);
865 static void s_gpvalue (int);
866 static void s_gpword (int);
867 static void s_gpdword (int);
868 static void s_cpadd (int);
869 static void s_insn (int);
870 static void md_obj_begin (void);
871 static void md_obj_end (void);
872 static void s_mips_ent (int);
873 static void s_mips_end (int);
874 static void s_mips_frame (int);
875 static void s_mips_mask (int reg_type);
876 static void s_mips_stab (int);
877 static void s_mips_weakext (int);
878 static void s_mips_file (int);
879 static void s_mips_loc (int);
880 static bfd_boolean pic_need_relax (symbolS *, asection *);
881 static int relaxed_branch_length (fragS *, asection *, int);
882 static int validate_mips_insn (const struct mips_opcode *);
883
884 /* Table and functions used to map between CPU/ISA names, and
885    ISA levels, and CPU numbers.  */
886
887 struct mips_cpu_info
888 {
889   const char *name;           /* CPU or ISA name.  */
890   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
891   int isa;                    /* ISA level.  */
892   int cpu;                    /* CPU number (default CPU if ISA).  */
893 };
894
895 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
896 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
897 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
898 \f
899 /* Pseudo-op table.
900
901    The following pseudo-ops from the Kane and Heinrich MIPS book
902    should be defined here, but are currently unsupported: .alias,
903    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
904
905    The following pseudo-ops from the Kane and Heinrich MIPS book are
906    specific to the type of debugging information being generated, and
907    should be defined by the object format: .aent, .begin, .bend,
908    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
909    .vreg.
910
911    The following pseudo-ops from the Kane and Heinrich MIPS book are
912    not MIPS CPU specific, but are also not specific to the object file
913    format.  This file is probably the best place to define them, but
914    they are not currently supported: .asm0, .endr, .lab, .repeat,
915    .struct.  */
916
917 static const pseudo_typeS mips_pseudo_table[] =
918 {
919   /* MIPS specific pseudo-ops.  */
920   {"option", s_option, 0},
921   {"set", s_mipsset, 0},
922   {"rdata", s_change_sec, 'r'},
923   {"sdata", s_change_sec, 's'},
924   {"livereg", s_ignore, 0},
925   {"abicalls", s_abicalls, 0},
926   {"cpload", s_cpload, 0},
927   {"cpsetup", s_cpsetup, 0},
928   {"cplocal", s_cplocal, 0},
929   {"cprestore", s_cprestore, 0},
930   {"cpreturn", s_cpreturn, 0},
931   {"gpvalue", s_gpvalue, 0},
932   {"gpword", s_gpword, 0},
933   {"gpdword", s_gpdword, 0},
934   {"cpadd", s_cpadd, 0},
935   {"insn", s_insn, 0},
936
937   /* Relatively generic pseudo-ops that happen to be used on MIPS
938      chips.  */
939   {"asciiz", stringer, 1},
940   {"bss", s_change_sec, 'b'},
941   {"err", s_err, 0},
942   {"half", s_cons, 1},
943   {"dword", s_cons, 3},
944   {"weakext", s_mips_weakext, 0},
945
946   /* These pseudo-ops are defined in read.c, but must be overridden
947      here for one reason or another.  */
948   {"align", s_align, 0},
949   {"byte", s_cons, 0},
950   {"data", s_change_sec, 'd'},
951   {"double", s_float_cons, 'd'},
952   {"float", s_float_cons, 'f'},
953   {"globl", s_mips_globl, 0},
954   {"global", s_mips_globl, 0},
955   {"hword", s_cons, 1},
956   {"int", s_cons, 2},
957   {"long", s_cons, 2},
958   {"octa", s_cons, 4},
959   {"quad", s_cons, 3},
960   {"section", s_change_section, 0},
961   {"short", s_cons, 1},
962   {"single", s_float_cons, 'f'},
963   {"stabn", s_mips_stab, 'n'},
964   {"text", s_change_sec, 't'},
965   {"word", s_cons, 2},
966
967   { "extern", ecoff_directive_extern, 0},
968
969   { NULL, NULL, 0 },
970 };
971
972 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
973 {
974   /* These pseudo-ops should be defined by the object file format.
975      However, a.out doesn't support them, so we have versions here.  */
976   {"aent", s_mips_ent, 1},
977   {"bgnb", s_ignore, 0},
978   {"end", s_mips_end, 0},
979   {"endb", s_ignore, 0},
980   {"ent", s_mips_ent, 0},
981   {"file", s_mips_file, 0},
982   {"fmask", s_mips_mask, 'F'},
983   {"frame", s_mips_frame, 0},
984   {"loc", s_mips_loc, 0},
985   {"mask", s_mips_mask, 'R'},
986   {"verstamp", s_ignore, 0},
987   { NULL, NULL, 0 },
988 };
989
990 extern void pop_insert (const pseudo_typeS *);
991
992 void
993 mips_pop_insert (void)
994 {
995   pop_insert (mips_pseudo_table);
996   if (! ECOFF_DEBUGGING)
997     pop_insert (mips_nonecoff_pseudo_table);
998 }
999 \f
1000 /* Symbols labelling the current insn.  */
1001
1002 struct insn_label_list
1003 {
1004   struct insn_label_list *next;
1005   symbolS *label;
1006 };
1007
1008 static struct insn_label_list *insn_labels;
1009 static struct insn_label_list *free_insn_labels;
1010
1011 static void mips_clear_insn_labels (void);
1012
1013 static inline void
1014 mips_clear_insn_labels (void)
1015 {
1016   register struct insn_label_list **pl;
1017
1018   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1019     ;
1020   *pl = insn_labels;
1021   insn_labels = NULL;
1022 }
1023 \f
1024 static char *expr_end;
1025
1026 /* Expressions which appear in instructions.  These are set by
1027    mips_ip.  */
1028
1029 static expressionS imm_expr;
1030 static expressionS imm2_expr;
1031 static expressionS offset_expr;
1032
1033 /* Relocs associated with imm_expr and offset_expr.  */
1034
1035 static bfd_reloc_code_real_type imm_reloc[3]
1036   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1037 static bfd_reloc_code_real_type offset_reloc[3]
1038   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1039
1040 /* These are set by mips16_ip if an explicit extension is used.  */
1041
1042 static bfd_boolean mips16_small, mips16_ext;
1043
1044 #ifdef OBJ_ELF
1045 /* The pdr segment for per procedure frame/regmask info.  Not used for
1046    ECOFF debugging.  */
1047
1048 static segT pdr_seg;
1049 #endif
1050
1051 /* The default target format to use.  */
1052
1053 const char *
1054 mips_target_format (void)
1055 {
1056   switch (OUTPUT_FLAVOR)
1057     {
1058     case bfd_target_aout_flavour:
1059       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1060     case bfd_target_ecoff_flavour:
1061       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1062     case bfd_target_coff_flavour:
1063       return "pe-mips";
1064     case bfd_target_elf_flavour:
1065 #ifdef TE_TMIPS
1066       /* This is traditional mips.  */
1067       return (target_big_endian
1068               ? (HAVE_64BIT_OBJECTS
1069                  ? "elf64-tradbigmips"
1070                  : (HAVE_NEWABI
1071                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1072               : (HAVE_64BIT_OBJECTS
1073                  ? "elf64-tradlittlemips"
1074                  : (HAVE_NEWABI
1075                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1076 #else
1077       return (target_big_endian
1078               ? (HAVE_64BIT_OBJECTS
1079                  ? "elf64-bigmips"
1080                  : (HAVE_NEWABI
1081                     ? "elf32-nbigmips" : "elf32-bigmips"))
1082               : (HAVE_64BIT_OBJECTS
1083                  ? "elf64-littlemips"
1084                  : (HAVE_NEWABI
1085                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1086 #endif
1087     default:
1088       abort ();
1089       return NULL;
1090     }
1091 }
1092
1093 /* This function is called once, at assembler startup time.  It should
1094    set up all the tables, etc. that the MD part of the assembler will need.  */
1095
1096 void
1097 md_begin (void)
1098 {
1099   register const char *retval = NULL;
1100   int i = 0;
1101   int broken = 0;
1102
1103   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1104     as_warn (_("Could not set architecture and machine"));
1105
1106   op_hash = hash_new ();
1107
1108   for (i = 0; i < NUMOPCODES;)
1109     {
1110       const char *name = mips_opcodes[i].name;
1111
1112       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1113       if (retval != NULL)
1114         {
1115           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1116                    mips_opcodes[i].name, retval);
1117           /* Probably a memory allocation problem?  Give up now.  */
1118           as_fatal (_("Broken assembler.  No assembly attempted."));
1119         }
1120       do
1121         {
1122           if (mips_opcodes[i].pinfo != INSN_MACRO)
1123             {
1124               if (!validate_mips_insn (&mips_opcodes[i]))
1125                 broken = 1;
1126             }
1127           ++i;
1128         }
1129       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1130     }
1131
1132   mips16_op_hash = hash_new ();
1133
1134   i = 0;
1135   while (i < bfd_mips16_num_opcodes)
1136     {
1137       const char *name = mips16_opcodes[i].name;
1138
1139       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1140       if (retval != NULL)
1141         as_fatal (_("internal: can't hash `%s': %s"),
1142                   mips16_opcodes[i].name, retval);
1143       do
1144         {
1145           if (mips16_opcodes[i].pinfo != INSN_MACRO
1146               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1147                   != mips16_opcodes[i].match))
1148             {
1149               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1150                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1151               broken = 1;
1152             }
1153           ++i;
1154         }
1155       while (i < bfd_mips16_num_opcodes
1156              && strcmp (mips16_opcodes[i].name, name) == 0);
1157     }
1158
1159   if (broken)
1160     as_fatal (_("Broken assembler.  No assembly attempted."));
1161
1162   /* We add all the general register names to the symbol table.  This
1163      helps us detect invalid uses of them.  */
1164   for (i = 0; i < 32; i++)
1165     {
1166       char buf[5];
1167
1168       sprintf (buf, "$%d", i);
1169       symbol_table_insert (symbol_new (buf, reg_section, i,
1170                                        &zero_address_frag));
1171     }
1172   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1173                                    &zero_address_frag));
1174   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1175                                    &zero_address_frag));
1176   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1177                                    &zero_address_frag));
1178   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1179                                    &zero_address_frag));
1180   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1181                                    &zero_address_frag));
1182   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1183                                    &zero_address_frag));
1184   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1185                                    &zero_address_frag));
1186   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1187                                    &zero_address_frag));
1188   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1189                                    &zero_address_frag));
1190
1191   /* If we don't add these register names to the symbol table, they
1192      may end up being added as regular symbols by operand(), and then
1193      make it to the object file as undefined in case they're not
1194      regarded as local symbols.  They're local in o32, since `$' is a
1195      local symbol prefix, but not in n32 or n64.  */
1196   for (i = 0; i < 8; i++)
1197     {
1198       char buf[6];
1199
1200       sprintf (buf, "$fcc%i", i);
1201       symbol_table_insert (symbol_new (buf, reg_section, -1,
1202                                        &zero_address_frag));
1203     }
1204
1205   mips_no_prev_insn (FALSE);
1206
1207   mips_gprmask = 0;
1208   mips_cprmask[0] = 0;
1209   mips_cprmask[1] = 0;
1210   mips_cprmask[2] = 0;
1211   mips_cprmask[3] = 0;
1212
1213   /* set the default alignment for the text section (2**2) */
1214   record_alignment (text_section, 2);
1215
1216   if (USE_GLOBAL_POINTER_OPT)
1217     bfd_set_gp_size (stdoutput, g_switch_value);
1218
1219   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1220     {
1221       /* On a native system, sections must be aligned to 16 byte
1222          boundaries.  When configured for an embedded ELF target, we
1223          don't bother.  */
1224       if (strcmp (TARGET_OS, "elf") != 0)
1225         {
1226           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1227           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1228           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1229         }
1230
1231       /* Create a .reginfo section for register masks and a .mdebug
1232          section for debugging information.  */
1233       {
1234         segT seg;
1235         subsegT subseg;
1236         flagword flags;
1237         segT sec;
1238
1239         seg = now_seg;
1240         subseg = now_subseg;
1241
1242         /* The ABI says this section should be loaded so that the
1243            running program can access it.  However, we don't load it
1244            if we are configured for an embedded target */
1245         flags = SEC_READONLY | SEC_DATA;
1246         if (strcmp (TARGET_OS, "elf") != 0)
1247           flags |= SEC_ALLOC | SEC_LOAD;
1248
1249         if (mips_abi != N64_ABI)
1250           {
1251             sec = subseg_new (".reginfo", (subsegT) 0);
1252
1253             bfd_set_section_flags (stdoutput, sec, flags);
1254             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1255
1256 #ifdef OBJ_ELF
1257             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1258 #endif
1259           }
1260         else
1261           {
1262             /* The 64-bit ABI uses a .MIPS.options section rather than
1263                .reginfo section.  */
1264             sec = subseg_new (".MIPS.options", (subsegT) 0);
1265             bfd_set_section_flags (stdoutput, sec, flags);
1266             bfd_set_section_alignment (stdoutput, sec, 3);
1267
1268 #ifdef OBJ_ELF
1269             /* Set up the option header.  */
1270             {
1271               Elf_Internal_Options opthdr;
1272               char *f;
1273
1274               opthdr.kind = ODK_REGINFO;
1275               opthdr.size = (sizeof (Elf_External_Options)
1276                              + sizeof (Elf64_External_RegInfo));
1277               opthdr.section = 0;
1278               opthdr.info = 0;
1279               f = frag_more (sizeof (Elf_External_Options));
1280               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1281                                              (Elf_External_Options *) f);
1282
1283               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1284             }
1285 #endif
1286           }
1287
1288         if (ECOFF_DEBUGGING)
1289           {
1290             sec = subseg_new (".mdebug", (subsegT) 0);
1291             (void) bfd_set_section_flags (stdoutput, sec,
1292                                           SEC_HAS_CONTENTS | SEC_READONLY);
1293             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1294           }
1295 #ifdef OBJ_ELF
1296         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1297           {
1298             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1299             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1300                                           SEC_READONLY | SEC_RELOC
1301                                           | SEC_DEBUGGING);
1302             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1303           }
1304 #endif
1305
1306         subseg_set (seg, subseg);
1307       }
1308     }
1309
1310   if (! ECOFF_DEBUGGING)
1311     md_obj_begin ();
1312 }
1313
1314 void
1315 md_mips_end (void)
1316 {
1317   if (! ECOFF_DEBUGGING)
1318     md_obj_end ();
1319 }
1320
1321 void
1322 md_assemble (char *str)
1323 {
1324   struct mips_cl_insn insn;
1325   bfd_reloc_code_real_type unused_reloc[3]
1326     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1327
1328   imm_expr.X_op = O_absent;
1329   imm2_expr.X_op = O_absent;
1330   offset_expr.X_op = O_absent;
1331   imm_reloc[0] = BFD_RELOC_UNUSED;
1332   imm_reloc[1] = BFD_RELOC_UNUSED;
1333   imm_reloc[2] = BFD_RELOC_UNUSED;
1334   offset_reloc[0] = BFD_RELOC_UNUSED;
1335   offset_reloc[1] = BFD_RELOC_UNUSED;
1336   offset_reloc[2] = BFD_RELOC_UNUSED;
1337
1338   if (mips_opts.mips16)
1339     mips16_ip (str, &insn);
1340   else
1341     {
1342       mips_ip (str, &insn);
1343       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1344             str, insn.insn_opcode));
1345     }
1346
1347   if (insn_error)
1348     {
1349       as_bad ("%s `%s'", insn_error, str);
1350       return;
1351     }
1352
1353   if (insn.insn_mo->pinfo == INSN_MACRO)
1354     {
1355       if (mips_opts.mips16)
1356         mips16_macro (&insn);
1357       else
1358         macro (&insn);
1359     }
1360   else
1361     {
1362       if (imm_expr.X_op != O_absent)
1363         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1364       else if (offset_expr.X_op != O_absent)
1365         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1366       else
1367         append_insn (NULL, &insn, NULL, unused_reloc);
1368     }
1369 }
1370
1371 /* Return true if the given relocation might need a matching %lo().
1372    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1373    applied to local symbols.  */
1374
1375 static inline bfd_boolean
1376 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1377 {
1378   return (reloc == BFD_RELOC_HI16_S
1379           || reloc == BFD_RELOC_MIPS_GOT16);
1380 }
1381
1382 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1383    relocation.  */
1384
1385 static inline bfd_boolean
1386 fixup_has_matching_lo_p (fixS *fixp)
1387 {
1388   return (fixp->fx_next != NULL
1389           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1390           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1391           && fixp->fx_offset == fixp->fx_next->fx_offset);
1392 }
1393
1394 /* See whether instruction IP reads register REG.  CLASS is the type
1395    of register.  */
1396
1397 static int
1398 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1399                enum mips_regclass class)
1400 {
1401   if (class == MIPS16_REG)
1402     {
1403       assert (mips_opts.mips16);
1404       reg = mips16_to_32_reg_map[reg];
1405       class = MIPS_GR_REG;
1406     }
1407
1408   /* Don't report on general register ZERO, since it never changes.  */
1409   if (class == MIPS_GR_REG && reg == ZERO)
1410     return 0;
1411
1412   if (class == MIPS_FP_REG)
1413     {
1414       assert (! mips_opts.mips16);
1415       /* If we are called with either $f0 or $f1, we must check $f0.
1416          This is not optimal, because it will introduce an unnecessary
1417          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1418          need to distinguish reading both $f0 and $f1 or just one of
1419          them.  Note that we don't have to check the other way,
1420          because there is no instruction that sets both $f0 and $f1
1421          and requires a delay.  */
1422       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1423           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1424               == (reg &~ (unsigned) 1)))
1425         return 1;
1426       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1427           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1428               == (reg &~ (unsigned) 1)))
1429         return 1;
1430     }
1431   else if (! mips_opts.mips16)
1432     {
1433       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1434           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1435         return 1;
1436       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1437           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1438         return 1;
1439     }
1440   else
1441     {
1442       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1443           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1444                                     & MIPS16OP_MASK_RX)]
1445               == reg))
1446         return 1;
1447       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1448           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1449                                     & MIPS16OP_MASK_RY)]
1450               == reg))
1451         return 1;
1452       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1453           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1454                                     & MIPS16OP_MASK_MOVE32Z)]
1455               == reg))
1456         return 1;
1457       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1458         return 1;
1459       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1460         return 1;
1461       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1462         return 1;
1463       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1464           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1465               & MIPS16OP_MASK_REGR32) == reg)
1466         return 1;
1467     }
1468
1469   return 0;
1470 }
1471
1472 /* This function returns true if modifying a register requires a
1473    delay.  */
1474
1475 static int
1476 reg_needs_delay (unsigned int reg)
1477 {
1478   unsigned long prev_pinfo;
1479
1480   prev_pinfo = prev_insn.insn_mo->pinfo;
1481   if (! mips_opts.noreorder
1482       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1483       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484           || (! gpr_interlocks
1485               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1486     {
1487       /* A load from a coprocessor or from memory.  All load
1488          delays delay the use of general register rt for one
1489          instruction on the r3000.  The r6000 and r4000 use
1490          interlocks.  */
1491       /* Itbl support may require additional care here.  */
1492       know (prev_pinfo & INSN_WRITE_GPR_T);
1493       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1494         return 1;
1495     }
1496
1497   return 0;
1498 }
1499
1500 /* Mark instruction labels in mips16 mode.  This permits the linker to
1501    handle them specially, such as generating jalx instructions when
1502    needed.  We also make them odd for the duration of the assembly, in
1503    order to generate the right sort of code.  We will make them even
1504    in the adjust_symtab routine, while leaving them marked.  This is
1505    convenient for the debugger and the disassembler.  The linker knows
1506    to make them odd again.  */
1507
1508 static void
1509 mips16_mark_labels (void)
1510 {
1511   if (mips_opts.mips16)
1512     {
1513       struct insn_label_list *l;
1514       valueT val;
1515
1516       for (l = insn_labels; l != NULL; l = l->next)
1517         {
1518 #ifdef OBJ_ELF
1519           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1520             S_SET_OTHER (l->label, STO_MIPS16);
1521 #endif
1522           val = S_GET_VALUE (l->label);
1523           if ((val & 1) == 0)
1524             S_SET_VALUE (l->label, val + 1);
1525         }
1526     }
1527 }
1528
1529 /* Output an instruction.  PLACE is where to put the instruction; if
1530    it is NULL, this uses frag_more to get room.  IP is the instruction
1531    information.  ADDRESS_EXPR is an operand of the instruction to be
1532    used with RELOC_TYPE.  */
1533
1534 static void
1535 append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1536              bfd_reloc_code_real_type *reloc_type)
1537 {
1538   register unsigned long prev_pinfo, pinfo;
1539   char *f;
1540   fixS *fixp[3];
1541   int nops = 0;
1542   bfd_boolean force_new_frag = FALSE;
1543
1544   /* Mark instruction labels in mips16 mode.  */
1545   mips16_mark_labels ();
1546
1547   prev_pinfo = prev_insn.insn_mo->pinfo;
1548   pinfo = ip->insn_mo->pinfo;
1549
1550   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1551     {
1552       int prev_prev_nop;
1553
1554       /* If the previous insn required any delay slots, see if we need
1555          to insert a NOP or two.  There are eight kinds of possible
1556          hazards, of which an instruction can have at most one type.
1557          (1) a load from memory delay
1558          (2) a load from a coprocessor delay
1559          (3) an unconditional branch delay
1560          (4) a conditional branch delay
1561          (5) a move to coprocessor register delay
1562          (6) a load coprocessor register from memory delay
1563          (7) a coprocessor condition code delay
1564          (8) a HI/LO special register delay
1565
1566          There are a lot of optimizations we could do that we don't.
1567          In particular, we do not, in general, reorder instructions.
1568          If you use gcc with optimization, it will reorder
1569          instructions and generally do much more optimization then we
1570          do here; repeating all that work in the assembler would only
1571          benefit hand written assembly code, and does not seem worth
1572          it.  */
1573
1574       /* This is how a NOP is emitted.  */
1575 #define emit_nop()                                      \
1576   (mips_opts.mips16                                     \
1577    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1578    : md_number_to_chars (frag_more (4), 0, 4))
1579
1580       /* The previous insn might require a delay slot, depending upon
1581          the contents of the current insn.  */
1582       if (! mips_opts.mips16
1583           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1584           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1585                && ! cop_interlocks)
1586               || (! gpr_interlocks
1587                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1588         {
1589           /* A load from a coprocessor or from memory.  All load
1590              delays delay the use of general register rt for one
1591              instruction on the r3000.  The r6000 and r4000 use
1592              interlocks.  */
1593           /* Itbl support may require additional care here.  */
1594           know (prev_pinfo & INSN_WRITE_GPR_T);
1595           if (mips_optimize == 0
1596               || insn_uses_reg (ip,
1597                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1598                                  & OP_MASK_RT),
1599                                 MIPS_GR_REG))
1600             ++nops;
1601         }
1602       else if (! mips_opts.mips16
1603                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1604                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1605                     && ! cop_interlocks)
1606                    || (mips_opts.isa == ISA_MIPS1
1607                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1608         {
1609           /* A generic coprocessor delay.  The previous instruction
1610              modified a coprocessor general or control register.  If
1611              it modified a control register, we need to avoid any
1612              coprocessor instruction (this is probably not always
1613              required, but it sometimes is).  If it modified a general
1614              register, we avoid using that register.
1615
1616              On the r6000 and r4000 loading a coprocessor register
1617              from memory is interlocked, and does not require a delay.
1618
1619              This case is not handled very well.  There is no special
1620              knowledge of CP0 handling, and the coprocessors other
1621              than the floating point unit are not distinguished at
1622              all.  */
1623           /* Itbl support may require additional care here. FIXME!
1624              Need to modify this to include knowledge about
1625              user specified delays!  */
1626           if (prev_pinfo & INSN_WRITE_FPR_T)
1627             {
1628               if (mips_optimize == 0
1629                   || insn_uses_reg (ip,
1630                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1631                                      & OP_MASK_FT),
1632                                     MIPS_FP_REG))
1633                 ++nops;
1634             }
1635           else if (prev_pinfo & INSN_WRITE_FPR_S)
1636             {
1637               if (mips_optimize == 0
1638                   || insn_uses_reg (ip,
1639                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1640                                      & OP_MASK_FS),
1641                                     MIPS_FP_REG))
1642                 ++nops;
1643             }
1644           else
1645             {
1646               /* We don't know exactly what the previous instruction
1647                  does.  If the current instruction uses a coprocessor
1648                  register, we must insert a NOP.  If previous
1649                  instruction may set the condition codes, and the
1650                  current instruction uses them, we must insert two
1651                  NOPS.  */
1652               /* Itbl support may require additional care here.  */
1653               if (mips_optimize == 0
1654                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1655                       && (pinfo & INSN_READ_COND_CODE)))
1656                 nops += 2;
1657               else if (pinfo & INSN_COP)
1658                 ++nops;
1659             }
1660         }
1661       else if (! mips_opts.mips16
1662                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1663                && (prev_pinfo & INSN_WRITE_COND_CODE)
1664                && ! cop_interlocks)
1665         {
1666           /* The previous instruction sets the coprocessor condition
1667              codes, but does not require a general coprocessor delay
1668              (this means it is a floating point comparison
1669              instruction).  If this instruction uses the condition
1670              codes, we need to insert a single NOP.  */
1671           /* Itbl support may require additional care here.  */
1672           if (mips_optimize == 0
1673               || (pinfo & INSN_READ_COND_CODE))
1674             ++nops;
1675         }
1676
1677       /* If we're fixing up mfhi/mflo for the r7000 and the
1678          previous insn was an mfhi/mflo and the current insn
1679          reads the register that the mfhi/mflo wrote to, then
1680          insert two nops.  */
1681
1682       else if (mips_7000_hilo_fix
1683                && MF_HILO_INSN (prev_pinfo)
1684                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1685                                       & OP_MASK_RD),
1686                                  MIPS_GR_REG))
1687         {
1688           nops += 2;
1689         }
1690
1691       /* If we're fixing up mfhi/mflo for the r7000 and the
1692          2nd previous insn was an mfhi/mflo and the current insn
1693          reads the register that the mfhi/mflo wrote to, then
1694          insert one nop.  */
1695
1696       else if (mips_7000_hilo_fix
1697                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1698                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1699                                        & OP_MASK_RD),
1700                                     MIPS_GR_REG))
1701
1702         {
1703           ++nops;
1704         }
1705
1706       else if (prev_pinfo & INSN_READ_LO)
1707         {
1708           /* The previous instruction reads the LO register; if the
1709              current instruction writes to the LO register, we must
1710              insert two NOPS.  Some newer processors have interlocks.
1711              Also the tx39's multiply instructions can be executed
1712              immediately after a read from HI/LO (without the delay),
1713              though the tx39's divide insns still do require the
1714              delay.  */
1715           if (! (hilo_interlocks
1716                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1717               && (mips_optimize == 0
1718                   || (pinfo & INSN_WRITE_LO)))
1719             nops += 2;
1720           /* Most mips16 branch insns don't have a delay slot.
1721              If a read from LO is immediately followed by a branch
1722              to a write to LO we have a read followed by a write
1723              less than 2 insns away.  We assume the target of
1724              a branch might be a write to LO, and insert a nop
1725              between a read and an immediately following branch.  */
1726           else if (mips_opts.mips16
1727                    && (mips_optimize == 0
1728                        || (pinfo & MIPS16_INSN_BRANCH)))
1729             ++nops;
1730         }
1731       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1732         {
1733           /* The previous instruction reads the HI register; if the
1734              current instruction writes to the HI register, we must
1735              insert a NOP.  Some newer processors have interlocks.
1736              Also the note tx39's multiply above.  */
1737           if (! (hilo_interlocks
1738                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1739               && (mips_optimize == 0
1740                   || (pinfo & INSN_WRITE_HI)))
1741             nops += 2;
1742           /* Most mips16 branch insns don't have a delay slot.
1743              If a read from HI is immediately followed by a branch
1744              to a write to HI we have a read followed by a write
1745              less than 2 insns away.  We assume the target of
1746              a branch might be a write to HI, and insert a nop
1747              between a read and an immediately following branch.  */
1748           else if (mips_opts.mips16
1749                    && (mips_optimize == 0
1750                        || (pinfo & MIPS16_INSN_BRANCH)))
1751             ++nops;
1752         }
1753
1754       /* If the previous instruction was in a noreorder section, then
1755          we don't want to insert the nop after all.  */
1756       /* Itbl support may require additional care here.  */
1757       if (prev_insn_unreordered)
1758         nops = 0;
1759
1760       /* There are two cases which require two intervening
1761          instructions: 1) setting the condition codes using a move to
1762          coprocessor instruction which requires a general coprocessor
1763          delay and then reading the condition codes 2) reading the HI
1764          or LO register and then writing to it (except on processors
1765          which have interlocks).  If we are not already emitting a NOP
1766          instruction, we must check for these cases compared to the
1767          instruction previous to the previous instruction.  */
1768       if ((! mips_opts.mips16
1769            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1770            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1771            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1772            && (pinfo & INSN_READ_COND_CODE)
1773            && ! cop_interlocks)
1774           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1775               && (pinfo & INSN_WRITE_LO)
1776               && ! (hilo_interlocks
1777                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1778           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1779               && (pinfo & INSN_WRITE_HI)
1780               && ! (hilo_interlocks
1781                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1782         prev_prev_nop = 1;
1783       else
1784         prev_prev_nop = 0;
1785
1786       if (prev_prev_insn_unreordered)
1787         prev_prev_nop = 0;
1788
1789       if (prev_prev_nop && nops == 0)
1790         ++nops;
1791
1792       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1793         {
1794           /* We're out of bits in pinfo, so we must resort to string
1795              ops here.  Shortcuts are selected based on opcodes being
1796              limited to the VR4122 instruction set.  */
1797           int min_nops = 0;
1798           const char *pn = prev_insn.insn_mo->name;
1799           const char *tn = ip->insn_mo->name;
1800           if (strncmp(pn, "macc", 4) == 0
1801               || strncmp(pn, "dmacc", 5) == 0)
1802             {
1803               /* Errata 21 - [D]DIV[U] after [D]MACC */
1804               if (strstr (tn, "div"))
1805                 {
1806                   min_nops = 1;
1807                 }
1808
1809               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1810               if (pn[0] == 'd' /* dmacc */
1811                   && (strncmp(tn, "dmult", 5) == 0
1812                       || strncmp(tn, "dmacc", 5) == 0))
1813                 {
1814                   min_nops = 1;
1815                 }
1816
1817               /* Errata 24 - MT{LO,HI} after [D]MACC */
1818               if (strcmp (tn, "mtlo") == 0
1819                   || strcmp (tn, "mthi") == 0)
1820                 {
1821                   min_nops = 1;
1822                 }
1823
1824             }
1825           else if (strncmp(pn, "dmult", 5) == 0
1826                    && (strncmp(tn, "dmult", 5) == 0
1827                        || strncmp(tn, "dmacc", 5) == 0))
1828             {
1829               /* Here is the rest of errata 23.  */
1830               min_nops = 1;
1831             }
1832           if (nops < min_nops)
1833             nops = min_nops;
1834         }
1835
1836       /* If we are being given a nop instruction, don't bother with
1837          one of the nops we would otherwise output.  This will only
1838          happen when a nop instruction is used with mips_optimize set
1839          to 0.  */
1840       if (nops > 0
1841           && ! mips_opts.noreorder
1842           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1843         --nops;
1844
1845       /* Now emit the right number of NOP instructions.  */
1846       if (nops > 0 && ! mips_opts.noreorder)
1847         {
1848           fragS *old_frag;
1849           unsigned long old_frag_offset;
1850           int i;
1851           struct insn_label_list *l;
1852
1853           old_frag = frag_now;
1854           old_frag_offset = frag_now_fix ();
1855
1856           for (i = 0; i < nops; i++)
1857             emit_nop ();
1858
1859           if (listing)
1860             {
1861               listing_prev_line ();
1862               /* We may be at the start of a variant frag.  In case we
1863                  are, make sure there is enough space for the frag
1864                  after the frags created by listing_prev_line.  The
1865                  argument to frag_grow here must be at least as large
1866                  as the argument to all other calls to frag_grow in
1867                  this file.  We don't have to worry about being in the
1868                  middle of a variant frag, because the variants insert
1869                  all needed nop instructions themselves.  */
1870               frag_grow (40);
1871             }
1872
1873           for (l = insn_labels; l != NULL; l = l->next)
1874             {
1875               valueT val;
1876
1877               assert (S_GET_SEGMENT (l->label) == now_seg);
1878               symbol_set_frag (l->label, frag_now);
1879               val = (valueT) frag_now_fix ();
1880               /* mips16 text labels are stored as odd.  */
1881               if (mips_opts.mips16)
1882                 ++val;
1883               S_SET_VALUE (l->label, val);
1884             }
1885
1886 #ifndef NO_ECOFF_DEBUGGING
1887           if (ECOFF_DEBUGGING)
1888             ecoff_fix_loc (old_frag, old_frag_offset);
1889 #endif
1890         }
1891       else if (prev_nop_frag != NULL)
1892         {
1893           /* We have a frag holding nops we may be able to remove.  If
1894              we don't need any nops, we can decrease the size of
1895              prev_nop_frag by the size of one instruction.  If we do
1896              need some nops, we count them in prev_nops_required.  */
1897           if (prev_nop_frag_since == 0)
1898             {
1899               if (nops == 0)
1900                 {
1901                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1902                   --prev_nop_frag_holds;
1903                 }
1904               else
1905                 prev_nop_frag_required += nops;
1906             }
1907           else
1908             {
1909               if (prev_prev_nop == 0)
1910                 {
1911                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1912                   --prev_nop_frag_holds;
1913                 }
1914               else
1915                 ++prev_nop_frag_required;
1916             }
1917
1918           if (prev_nop_frag_holds <= prev_nop_frag_required)
1919             prev_nop_frag = NULL;
1920
1921           ++prev_nop_frag_since;
1922
1923           /* Sanity check: by the time we reach the second instruction
1924              after prev_nop_frag, we should have used up all the nops
1925              one way or another.  */
1926           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1927         }
1928     }
1929
1930   if (place == NULL
1931       && address_expr
1932       && *reloc_type == BFD_RELOC_16_PCREL_S2
1933       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1934           || pinfo & INSN_COND_BRANCH_LIKELY)
1935       && mips_relax_branch
1936       /* Don't try branch relaxation within .set nomacro, or within
1937          .set noat if we use $at for PIC computations.  If it turns
1938          out that the branch was out-of-range, we'll get an error.  */
1939       && !mips_opts.warn_about_macros
1940       && !(mips_opts.noat && mips_pic != NO_PIC)
1941       && !mips_opts.mips16)
1942     {
1943       f = frag_var (rs_machine_dependent,
1944                     relaxed_branch_length
1945                     (NULL, NULL,
1946                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1947                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1948                     RELAX_BRANCH_ENCODE
1949                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
1950                      pinfo & INSN_COND_BRANCH_LIKELY,
1951                      pinfo & INSN_WRITE_GPR_31,
1952                      0),
1953                     address_expr->X_add_symbol,
1954                     address_expr->X_add_number,
1955                     0);
1956       *reloc_type = BFD_RELOC_UNUSED;
1957     }
1958   else if (*reloc_type > BFD_RELOC_UNUSED)
1959     {
1960       /* We need to set up a variant frag.  */
1961       assert (mips_opts.mips16 && address_expr != NULL);
1962       f = frag_var (rs_machine_dependent, 4, 0,
1963                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1964                                          mips16_small, mips16_ext,
1965                                          (prev_pinfo
1966                                           & INSN_UNCOND_BRANCH_DELAY),
1967                                          (*prev_insn_reloc_type
1968                                           == BFD_RELOC_MIPS16_JMP)),
1969                     make_expr_symbol (address_expr), 0, NULL);
1970     }
1971   else if (place != NULL)
1972     f = place;
1973   else if (mips_opts.mips16
1974            && ! ip->use_extend
1975            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1976     {
1977       /* Make sure there is enough room to swap this instruction with
1978          a following jump instruction.  */
1979       frag_grow (6);
1980       f = frag_more (2);
1981     }
1982   else
1983     {
1984       if (mips_opts.mips16
1985           && mips_opts.noreorder
1986           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1987         as_warn (_("extended instruction in delay slot"));
1988
1989       f = frag_more (4);
1990     }
1991
1992   fixp[0] = fixp[1] = fixp[2] = NULL;
1993   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1994     {
1995       if (address_expr->X_op == O_constant)
1996         {
1997           valueT tmp;
1998
1999           switch (*reloc_type)
2000             {
2001             case BFD_RELOC_32:
2002               ip->insn_opcode |= address_expr->X_add_number;
2003               break;
2004
2005             case BFD_RELOC_MIPS_HIGHEST:
2006               tmp = (address_expr->X_add_number
2007                      + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2008               tmp >>= 16;
2009               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2010               break;
2011
2012             case BFD_RELOC_MIPS_HIGHER:
2013               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2014               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2015               break;
2016
2017             case BFD_RELOC_HI16_S:
2018               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2019                                   >> 16) & 0xffff;
2020               break;
2021
2022             case BFD_RELOC_HI16:
2023               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2024               break;
2025
2026             case BFD_RELOC_LO16:
2027             case BFD_RELOC_MIPS_GOT_DISP:
2028               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2029               break;
2030
2031             case BFD_RELOC_MIPS_JMP:
2032               if ((address_expr->X_add_number & 3) != 0)
2033                 as_bad (_("jump to misaligned address (0x%lx)"),
2034                         (unsigned long) address_expr->X_add_number);
2035               if (address_expr->X_add_number & ~0xfffffff)
2036                 as_bad (_("jump address range overflow (0x%lx)"),
2037                         (unsigned long) address_expr->X_add_number);
2038               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2039               break;
2040
2041             case BFD_RELOC_MIPS16_JMP:
2042               if ((address_expr->X_add_number & 3) != 0)
2043                 as_bad (_("jump to misaligned address (0x%lx)"),
2044                         (unsigned long) address_expr->X_add_number);
2045               if (address_expr->X_add_number & ~0xfffffff)
2046                 as_bad (_("jump address range overflow (0x%lx)"),
2047                         (unsigned long) address_expr->X_add_number);
2048               ip->insn_opcode |=
2049                 (((address_expr->X_add_number & 0x7c0000) << 3)
2050                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2051                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2052               break;
2053
2054             case BFD_RELOC_16_PCREL_S2:
2055               goto need_reloc;
2056
2057             default:
2058               internalError ();
2059             }
2060         }
2061       else
2062         {
2063         need_reloc:
2064           /* Don't generate a reloc if we are writing into a variant frag.  */
2065           if (place == NULL)
2066             {
2067               reloc_howto_type *howto;
2068               int i;
2069
2070               /* In a compound relocation, it is the final (outermost)
2071                  operator that determines the relocated field.  */
2072               for (i = 1; i < 3; i++)
2073                 if (reloc_type[i] == BFD_RELOC_UNUSED)
2074                   break;
2075
2076               howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2077               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2078                                      bfd_get_reloc_size(howto),
2079                                      address_expr,
2080                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2081                                      reloc_type[0]);
2082
2083               /* These relocations can have an addend that won't fit in
2084                  4 octets for 64bit assembly.  */
2085               if (HAVE_64BIT_GPRS
2086                   && ! howto->partial_inplace
2087                   && (reloc_type[0] == BFD_RELOC_16
2088                       || reloc_type[0] == BFD_RELOC_32
2089                       || reloc_type[0] == BFD_RELOC_MIPS_JMP
2090                       || reloc_type[0] == BFD_RELOC_HI16_S
2091                       || reloc_type[0] == BFD_RELOC_LO16
2092                       || reloc_type[0] == BFD_RELOC_GPREL16
2093                       || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2094                       || reloc_type[0] == BFD_RELOC_GPREL32
2095                       || reloc_type[0] == BFD_RELOC_64
2096                       || reloc_type[0] == BFD_RELOC_CTOR
2097                       || reloc_type[0] == BFD_RELOC_MIPS_SUB
2098                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2099                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2100                       || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2101                       || reloc_type[0] == BFD_RELOC_MIPS_REL16
2102                       || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2103                 fixp[0]->fx_no_overflow = 1;
2104
2105               if (reloc_needs_lo_p (*reloc_type))
2106                 {
2107                   struct mips_hi_fixup *hi_fixup;
2108
2109                   /* Reuse the last entry if it already has a matching %lo.  */
2110                   hi_fixup = mips_hi_fixup_list;
2111                   if (hi_fixup == 0
2112                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2113                     {
2114                       hi_fixup = ((struct mips_hi_fixup *)
2115                                   xmalloc (sizeof (struct mips_hi_fixup)));
2116                       hi_fixup->next = mips_hi_fixup_list;
2117                       mips_hi_fixup_list = hi_fixup;
2118                     }
2119                   hi_fixup->fixp = fixp[0];
2120                   hi_fixup->seg = now_seg;
2121                 }
2122
2123               /* Add fixups for the second and third relocations, if given.
2124                  Note that the ABI allows the second relocation to be
2125                  against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2126                  moment we only use RSS_UNDEF, but we could add support
2127                  for the others if it ever becomes necessary.  */
2128               for (i = 1; i < 3; i++)
2129                 if (reloc_type[i] != BFD_RELOC_UNUSED)
2130                   {
2131                     address_expr->X_op = O_absent;
2132                     address_expr->X_add_symbol = 0;
2133                     address_expr->X_add_number = 0;
2134
2135                     fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2136                                            fixp[0]->fx_size, address_expr,
2137                                            FALSE, reloc_type[i]);
2138                   }
2139             }
2140         }
2141     }
2142
2143   if (! mips_opts.mips16)
2144     {
2145       md_number_to_chars (f, ip->insn_opcode, 4);
2146 #ifdef OBJ_ELF
2147       dwarf2_emit_insn (4);
2148 #endif
2149     }
2150   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2151     {
2152       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2153       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2154 #ifdef OBJ_ELF
2155       dwarf2_emit_insn (4);
2156 #endif
2157     }
2158   else
2159     {
2160       if (ip->use_extend)
2161         {
2162           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2163           f += 2;
2164         }
2165       md_number_to_chars (f, ip->insn_opcode, 2);
2166 #ifdef OBJ_ELF
2167       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2168 #endif
2169     }
2170
2171   /* Update the register mask information.  */
2172   if (! mips_opts.mips16)
2173     {
2174       if (pinfo & INSN_WRITE_GPR_D)
2175         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2176       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2177         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2178       if (pinfo & INSN_READ_GPR_S)
2179         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2180       if (pinfo & INSN_WRITE_GPR_31)
2181         mips_gprmask |= 1 << RA;
2182       if (pinfo & INSN_WRITE_FPR_D)
2183         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2184       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2185         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2186       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2187         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2188       if ((pinfo & INSN_READ_FPR_R) != 0)
2189         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2190       if (pinfo & INSN_COP)
2191         {
2192           /* We don't keep enough information to sort these cases out.
2193              The itbl support does keep this information however, although
2194              we currently don't support itbl fprmats as part of the cop
2195              instruction.  May want to add this support in the future.  */
2196         }
2197       /* Never set the bit for $0, which is always zero.  */
2198       mips_gprmask &= ~1 << 0;
2199     }
2200   else
2201     {
2202       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2203         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2204                               & MIPS16OP_MASK_RX);
2205       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2206         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2207                               & MIPS16OP_MASK_RY);
2208       if (pinfo & MIPS16_INSN_WRITE_Z)
2209         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2210                               & MIPS16OP_MASK_RZ);
2211       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2212         mips_gprmask |= 1 << TREG;
2213       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2214         mips_gprmask |= 1 << SP;
2215       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2216         mips_gprmask |= 1 << RA;
2217       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2218         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2219       if (pinfo & MIPS16_INSN_READ_Z)
2220         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2221                               & MIPS16OP_MASK_MOVE32Z);
2222       if (pinfo & MIPS16_INSN_READ_GPR_X)
2223         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2224                               & MIPS16OP_MASK_REGR32);
2225     }
2226
2227   if (place == NULL && ! mips_opts.noreorder)
2228     {
2229       /* Filling the branch delay slot is more complex.  We try to
2230          switch the branch with the previous instruction, which we can
2231          do if the previous instruction does not set up a condition
2232          that the branch tests and if the branch is not itself the
2233          target of any branch.  */
2234       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2235           || (pinfo & INSN_COND_BRANCH_DELAY))
2236         {
2237           if (mips_optimize < 2
2238               /* If we have seen .set volatile or .set nomove, don't
2239                  optimize.  */
2240               || mips_opts.nomove != 0
2241               /* If we had to emit any NOP instructions, then we
2242                  already know we can not swap.  */
2243               || nops != 0
2244               /* If we don't even know the previous insn, we can not
2245                  swap.  */
2246               || ! prev_insn_valid
2247               /* If the previous insn is already in a branch delay
2248                  slot, then we can not swap.  */
2249               || prev_insn_is_delay_slot
2250               /* If the previous previous insn was in a .set
2251                  noreorder, we can't swap.  Actually, the MIPS
2252                  assembler will swap in this situation.  However, gcc
2253                  configured -with-gnu-as will generate code like
2254                    .set noreorder
2255                    lw   $4,XXX
2256                    .set reorder
2257                    INSN
2258                    bne  $4,$0,foo
2259                  in which we can not swap the bne and INSN.  If gcc is
2260                  not configured -with-gnu-as, it does not output the
2261                  .set pseudo-ops.  We don't have to check
2262                  prev_insn_unreordered, because prev_insn_valid will
2263                  be 0 in that case.  We don't want to use
2264                  prev_prev_insn_valid, because we do want to be able
2265                  to swap at the start of a function.  */
2266               || prev_prev_insn_unreordered
2267               /* If the branch is itself the target of a branch, we
2268                  can not swap.  We cheat on this; all we check for is
2269                  whether there is a label on this instruction.  If
2270                  there are any branches to anything other than a
2271                  label, users must use .set noreorder.  */
2272               || insn_labels != NULL
2273               /* If the previous instruction is in a variant frag, we
2274                  can not do the swap.  This does not apply to the
2275                  mips16, which uses variant frags for different
2276                  purposes.  */
2277               || (! mips_opts.mips16
2278                   && prev_insn_frag->fr_type == rs_machine_dependent)
2279               /* If the branch reads the condition codes, we don't
2280                  even try to swap, because in the sequence
2281                    ctc1 $X,$31
2282                    INSN
2283                    INSN
2284                    bc1t LABEL
2285                  we can not swap, and I don't feel like handling that
2286                  case.  */
2287               || (! mips_opts.mips16
2288                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2289                   && (pinfo & INSN_READ_COND_CODE))
2290               /* We can not swap with an instruction that requires a
2291                  delay slot, because the target of the branch might
2292                  interfere with that instruction.  */
2293               || (! mips_opts.mips16
2294                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2295                   && (prev_pinfo
2296               /* Itbl support may require additional care here.  */
2297                       & (INSN_LOAD_COPROC_DELAY
2298                          | INSN_COPROC_MOVE_DELAY
2299                          | INSN_WRITE_COND_CODE)))
2300               || (! (hilo_interlocks
2301                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2302                   && (prev_pinfo
2303                       & (INSN_READ_LO
2304                          | INSN_READ_HI)))
2305               || (! mips_opts.mips16
2306                   && ! gpr_interlocks
2307                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2308               || (! mips_opts.mips16
2309                   && mips_opts.isa == ISA_MIPS1
2310                   /* Itbl support may require additional care here.  */
2311                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2312               /* We can not swap with a branch instruction.  */
2313               || (prev_pinfo
2314                   & (INSN_UNCOND_BRANCH_DELAY
2315                      | INSN_COND_BRANCH_DELAY
2316                      | INSN_COND_BRANCH_LIKELY))
2317               /* We do not swap with a trap instruction, since it
2318                  complicates trap handlers to have the trap
2319                  instruction be in a delay slot.  */
2320               || (prev_pinfo & INSN_TRAP)
2321               /* If the branch reads a register that the previous
2322                  instruction sets, we can not swap.  */
2323               || (! mips_opts.mips16
2324                   && (prev_pinfo & INSN_WRITE_GPR_T)
2325                   && insn_uses_reg (ip,
2326                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2327                                      & OP_MASK_RT),
2328                                     MIPS_GR_REG))
2329               || (! mips_opts.mips16
2330                   && (prev_pinfo & INSN_WRITE_GPR_D)
2331                   && insn_uses_reg (ip,
2332                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2333                                      & OP_MASK_RD),
2334                                     MIPS_GR_REG))
2335               || (mips_opts.mips16
2336                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2337                        && insn_uses_reg (ip,
2338                                          ((prev_insn.insn_opcode
2339                                            >> MIPS16OP_SH_RX)
2340                                           & MIPS16OP_MASK_RX),
2341                                          MIPS16_REG))
2342                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2343                           && insn_uses_reg (ip,
2344                                             ((prev_insn.insn_opcode
2345                                               >> MIPS16OP_SH_RY)
2346                                              & MIPS16OP_MASK_RY),
2347                                             MIPS16_REG))
2348                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2349                           && insn_uses_reg (ip,
2350                                             ((prev_insn.insn_opcode
2351                                               >> MIPS16OP_SH_RZ)
2352                                              & MIPS16OP_MASK_RZ),
2353                                             MIPS16_REG))
2354                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2355                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2356                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2357                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2358                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2359                           && insn_uses_reg (ip,
2360                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2361                                                                      insn_opcode),
2362                                             MIPS_GR_REG))))
2363               /* If the branch writes a register that the previous
2364                  instruction sets, we can not swap (we know that
2365                  branches write only to RD or to $31).  */
2366               || (! mips_opts.mips16
2367                   && (prev_pinfo & INSN_WRITE_GPR_T)
2368                   && (((pinfo & INSN_WRITE_GPR_D)
2369                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2370                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2371                       || ((pinfo & INSN_WRITE_GPR_31)
2372                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2373                                & OP_MASK_RT)
2374                               == RA))))
2375               || (! mips_opts.mips16
2376                   && (prev_pinfo & INSN_WRITE_GPR_D)
2377                   && (((pinfo & INSN_WRITE_GPR_D)
2378                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2379                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2380                       || ((pinfo & INSN_WRITE_GPR_31)
2381                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2382                                & OP_MASK_RD)
2383                               == RA))))
2384               || (mips_opts.mips16
2385                   && (pinfo & MIPS16_INSN_WRITE_31)
2386                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2387                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2388                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2389                               == RA))))
2390               /* If the branch writes a register that the previous
2391                  instruction reads, we can not swap (we know that
2392                  branches only write to RD or to $31).  */
2393               || (! mips_opts.mips16
2394                   && (pinfo & INSN_WRITE_GPR_D)
2395                   && insn_uses_reg (&prev_insn,
2396                                     ((ip->insn_opcode >> OP_SH_RD)
2397                                      & OP_MASK_RD),
2398                                     MIPS_GR_REG))
2399               || (! mips_opts.mips16
2400                   && (pinfo & INSN_WRITE_GPR_31)
2401                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2402               || (mips_opts.mips16
2403                   && (pinfo & MIPS16_INSN_WRITE_31)
2404                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2405               /* If we are generating embedded PIC code, the branch
2406                  might be expanded into a sequence which uses $at, so
2407                  we can't swap with an instruction which reads it.  */
2408               || (mips_pic == EMBEDDED_PIC
2409                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2410               /* If the previous previous instruction has a load
2411                  delay, and sets a register that the branch reads, we
2412                  can not swap.  */
2413               || (! mips_opts.mips16
2414                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2415               /* Itbl support may require additional care here.  */
2416                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2417                       || (! gpr_interlocks
2418                           && (prev_prev_insn.insn_mo->pinfo
2419                               & INSN_LOAD_MEMORY_DELAY)))
2420                   && insn_uses_reg (ip,
2421                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2422                                      & OP_MASK_RT),
2423                                     MIPS_GR_REG))
2424               /* If one instruction sets a condition code and the
2425                  other one uses a condition code, we can not swap.  */
2426               || ((pinfo & INSN_READ_COND_CODE)
2427                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2428               || ((pinfo & INSN_WRITE_COND_CODE)
2429                   && (prev_pinfo & INSN_READ_COND_CODE))
2430               /* If the previous instruction uses the PC, we can not
2431                  swap.  */
2432               || (mips_opts.mips16
2433                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2434               /* If the previous instruction was extended, we can not
2435                  swap.  */
2436               || (mips_opts.mips16 && prev_insn_extended)
2437               /* If the previous instruction had a fixup in mips16
2438                  mode, we can not swap.  This normally means that the
2439                  previous instruction was a 4 byte branch anyhow.  */
2440               || (mips_opts.mips16 && prev_insn_fixp[0])
2441               /* If the previous instruction is a sync, sync.l, or
2442                  sync.p, we can not swap.  */
2443               || (prev_pinfo & INSN_SYNC))
2444             {
2445               /* We could do even better for unconditional branches to
2446                  portions of this object file; we could pick up the
2447                  instruction at the destination, put it in the delay
2448                  slot, and bump the destination address.  */
2449               emit_nop ();
2450               /* Update the previous insn information.  */
2451               prev_prev_insn = *ip;
2452               prev_insn.insn_mo = &dummy_opcode;
2453             }
2454           else
2455             {
2456               /* It looks like we can actually do the swap.  */
2457               if (! mips_opts.mips16)
2458                 {
2459                   char *prev_f;
2460                   char temp[4];
2461
2462                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2463                   memcpy (temp, prev_f, 4);
2464                   memcpy (prev_f, f, 4);
2465                   memcpy (f, temp, 4);
2466                   if (prev_insn_fixp[0])
2467                     {
2468                       prev_insn_fixp[0]->fx_frag = frag_now;
2469                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2470                     }
2471                   if (prev_insn_fixp[1])
2472                     {
2473                       prev_insn_fixp[1]->fx_frag = frag_now;
2474                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2475                     }
2476                   if (prev_insn_fixp[2])
2477                     {
2478                       prev_insn_fixp[2]->fx_frag = frag_now;
2479                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2480                     }
2481                   if (prev_insn_fixp[0] && HAVE_NEWABI
2482                       && prev_insn_frag != frag_now
2483                       && (prev_insn_fixp[0]->fx_r_type
2484                           == BFD_RELOC_MIPS_GOT_DISP
2485                           || (prev_insn_fixp[0]->fx_r_type
2486                               == BFD_RELOC_MIPS_CALL16)))
2487                     {
2488                       /* To avoid confusion in tc_gen_reloc, we must
2489                          ensure that this does not become a variant
2490                          frag.  */
2491                       force_new_frag = TRUE;
2492                     }
2493                   if (fixp[0])
2494                     {
2495                       fixp[0]->fx_frag = prev_insn_frag;
2496                       fixp[0]->fx_where = prev_insn_where;
2497                     }
2498                   if (fixp[1])
2499                     {
2500                       fixp[1]->fx_frag = prev_insn_frag;
2501                       fixp[1]->fx_where = prev_insn_where;
2502                     }
2503                   if (fixp[2])
2504                     {
2505                       fixp[2]->fx_frag = prev_insn_frag;
2506                       fixp[2]->fx_where = prev_insn_where;
2507                     }
2508                 }
2509               else
2510                 {
2511                   char *prev_f;
2512                   char temp[2];
2513
2514                   assert (prev_insn_fixp[0] == NULL);
2515                   assert (prev_insn_fixp[1] == NULL);
2516                   assert (prev_insn_fixp[2] == NULL);
2517                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2518                   memcpy (temp, prev_f, 2);
2519                   memcpy (prev_f, f, 2);
2520                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2521                     {
2522                       assert (*reloc_type == BFD_RELOC_UNUSED);
2523                       memcpy (f, temp, 2);
2524                     }
2525                   else
2526                     {
2527                       memcpy (f, f + 2, 2);
2528                       memcpy (f + 2, temp, 2);
2529                     }
2530                   if (fixp[0])
2531                     {
2532                       fixp[0]->fx_frag = prev_insn_frag;
2533                       fixp[0]->fx_where = prev_insn_where;
2534                     }
2535                   if (fixp[1])
2536                     {
2537                       fixp[1]->fx_frag = prev_insn_frag;
2538                       fixp[1]->fx_where = prev_insn_where;
2539                     }
2540                   if (fixp[2])
2541                     {
2542                       fixp[2]->fx_frag = prev_insn_frag;
2543                       fixp[2]->fx_where = prev_insn_where;
2544                     }
2545                 }
2546
2547               /* Update the previous insn information; leave prev_insn
2548                  unchanged.  */
2549               prev_prev_insn = *ip;
2550             }
2551           prev_insn_is_delay_slot = 1;
2552
2553           /* If that was an unconditional branch, forget the previous
2554              insn information.  */
2555           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2556             {
2557               prev_prev_insn.insn_mo = &dummy_opcode;
2558               prev_insn.insn_mo = &dummy_opcode;
2559             }
2560
2561           prev_insn_fixp[0] = NULL;
2562           prev_insn_fixp[1] = NULL;
2563           prev_insn_fixp[2] = NULL;
2564           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2565           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2566           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2567           prev_insn_extended = 0;
2568         }
2569       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2570         {
2571           /* We don't yet optimize a branch likely.  What we should do
2572              is look at the target, copy the instruction found there
2573              into the delay slot, and increment the branch to jump to
2574              the next instruction.  */
2575           emit_nop ();
2576           /* Update the previous insn information.  */
2577           prev_prev_insn = *ip;
2578           prev_insn.insn_mo = &dummy_opcode;
2579           prev_insn_fixp[0] = NULL;
2580           prev_insn_fixp[1] = NULL;
2581           prev_insn_fixp[2] = NULL;
2582           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2583           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2584           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2585           prev_insn_extended = 0;
2586         }
2587       else
2588         {
2589           /* Update the previous insn information.  */
2590           if (nops > 0)
2591             prev_prev_insn.insn_mo = &dummy_opcode;
2592           else
2593             prev_prev_insn = prev_insn;
2594           prev_insn = *ip;
2595
2596           /* Any time we see a branch, we always fill the delay slot
2597              immediately; since this insn is not a branch, we know it
2598              is not in a delay slot.  */
2599           prev_insn_is_delay_slot = 0;
2600
2601           prev_insn_fixp[0] = fixp[0];
2602           prev_insn_fixp[1] = fixp[1];
2603           prev_insn_fixp[2] = fixp[2];
2604           prev_insn_reloc_type[0] = reloc_type[0];
2605           prev_insn_reloc_type[1] = reloc_type[1];
2606           prev_insn_reloc_type[2] = reloc_type[2];
2607           if (mips_opts.mips16)
2608             prev_insn_extended = (ip->use_extend
2609                                   || *reloc_type > BFD_RELOC_UNUSED);
2610         }
2611
2612       prev_prev_insn_unreordered = prev_insn_unreordered;
2613       prev_insn_unreordered = 0;
2614       prev_insn_frag = frag_now;
2615       prev_insn_where = f - frag_now->fr_literal;
2616       prev_insn_valid = 1;
2617     }
2618   else if (place == NULL)
2619     {
2620       /* We need to record a bit of information even when we are not
2621          reordering, in order to determine the base address for mips16
2622          PC relative relocs.  */
2623       prev_prev_insn = prev_insn;
2624       prev_insn = *ip;
2625       prev_insn_reloc_type[0] = reloc_type[0];
2626       prev_insn_reloc_type[1] = reloc_type[1];
2627       prev_insn_reloc_type[2] = reloc_type[2];
2628       prev_prev_insn_unreordered = prev_insn_unreordered;
2629       prev_insn_unreordered = 1;
2630     }
2631
2632   /* We just output an insn, so the next one doesn't have a label.  */
2633   mips_clear_insn_labels ();
2634
2635   /* We must ensure that the frag to which an instruction that was
2636      moved from a non-variant frag doesn't become a variant frag,
2637      otherwise tc_gen_reloc may get confused.  */
2638   if (force_new_frag)
2639     {
2640       frag_wane (frag_now);
2641       frag_new (0);
2642     }
2643 }
2644
2645 /* This function forgets that there was any previous instruction or
2646    label.  If PRESERVE is non-zero, it remembers enough information to
2647    know whether nops are needed before a noreorder section.  */
2648
2649 static void
2650 mips_no_prev_insn (int preserve)
2651 {
2652   if (! preserve)
2653     {
2654       prev_insn.insn_mo = &dummy_opcode;
2655       prev_prev_insn.insn_mo = &dummy_opcode;
2656       prev_nop_frag = NULL;
2657       prev_nop_frag_holds = 0;
2658       prev_nop_frag_required = 0;
2659       prev_nop_frag_since = 0;
2660     }
2661   prev_insn_valid = 0;
2662   prev_insn_is_delay_slot = 0;
2663   prev_insn_unreordered = 0;
2664   prev_insn_extended = 0;
2665   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2666   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2667   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2668   prev_prev_insn_unreordered = 0;
2669   mips_clear_insn_labels ();
2670 }
2671
2672 /* This function must be called whenever we turn on noreorder or emit
2673    something other than instructions.  It inserts any NOPS which might
2674    be needed by the previous instruction, and clears the information
2675    kept for the previous instructions.  The INSNS parameter is true if
2676    instructions are to follow.  */
2677
2678 static void
2679 mips_emit_delays (bfd_boolean insns)
2680 {
2681   if (! mips_opts.noreorder)
2682     {
2683       int nops;
2684
2685       nops = 0;
2686       if ((! mips_opts.mips16
2687            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2688            && (! cop_interlocks
2689                && (prev_insn.insn_mo->pinfo
2690                    & (INSN_LOAD_COPROC_DELAY
2691                       | INSN_COPROC_MOVE_DELAY
2692                       | INSN_WRITE_COND_CODE))))
2693           || (! hilo_interlocks
2694               && (prev_insn.insn_mo->pinfo
2695                   & (INSN_READ_LO
2696                      | INSN_READ_HI)))
2697           || (! mips_opts.mips16
2698               && ! gpr_interlocks
2699               && (prev_insn.insn_mo->pinfo
2700                   & INSN_LOAD_MEMORY_DELAY))
2701           || (! mips_opts.mips16
2702               && mips_opts.isa == ISA_MIPS1
2703               && (prev_insn.insn_mo->pinfo
2704                   & INSN_COPROC_MEMORY_DELAY)))
2705         {
2706           /* Itbl support may require additional care here.  */
2707           ++nops;
2708           if ((! mips_opts.mips16
2709                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2710                && (! cop_interlocks
2711                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2712               || (! hilo_interlocks
2713                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2714                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2715             ++nops;
2716
2717           if (prev_insn_unreordered)
2718             nops = 0;
2719         }
2720       else if ((! mips_opts.mips16
2721                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2722                 && (! cop_interlocks
2723                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2724                || (! hilo_interlocks
2725                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2726                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2727         {
2728           /* Itbl support may require additional care here.  */
2729           if (! prev_prev_insn_unreordered)
2730             ++nops;
2731         }
2732
2733       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2734         {
2735           int min_nops = 0;
2736           const char *pn = prev_insn.insn_mo->name;
2737           if (strncmp(pn, "macc", 4) == 0
2738               || strncmp(pn, "dmacc", 5) == 0
2739               || strncmp(pn, "dmult", 5) == 0)
2740             {
2741               min_nops = 1;
2742             }
2743           if (nops < min_nops)
2744             nops = min_nops;
2745         }
2746
2747       if (nops > 0)
2748         {
2749           struct insn_label_list *l;
2750
2751           if (insns)
2752             {
2753               /* Record the frag which holds the nop instructions, so
2754                  that we can remove them if we don't need them.  */
2755               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2756               prev_nop_frag = frag_now;
2757               prev_nop_frag_holds = nops;
2758               prev_nop_frag_required = 0;
2759               prev_nop_frag_since = 0;
2760             }
2761
2762           for (; nops > 0; --nops)
2763             emit_nop ();
2764
2765           if (insns)
2766             {
2767               /* Move on to a new frag, so that it is safe to simply
2768                  decrease the size of prev_nop_frag.  */
2769               frag_wane (frag_now);
2770               frag_new (0);
2771             }
2772
2773           for (l = insn_labels; l != NULL; l = l->next)
2774             {
2775               valueT val;
2776
2777               assert (S_GET_SEGMENT (l->label) == now_seg);
2778               symbol_set_frag (l->label, frag_now);
2779               val = (valueT) frag_now_fix ();
2780               /* mips16 text labels are stored as odd.  */
2781               if (mips_opts.mips16)
2782                 ++val;
2783               S_SET_VALUE (l->label, val);
2784             }
2785         }
2786     }
2787
2788   /* Mark instruction labels in mips16 mode.  */
2789   if (insns)
2790     mips16_mark_labels ();
2791
2792   mips_no_prev_insn (insns);
2793 }
2794
2795 /* Build an instruction created by a macro expansion.  This is passed
2796    a pointer to the count of instructions created so far, an
2797    expression, the name of the instruction to build, an operand format
2798    string, and corresponding arguments.  */
2799
2800 static void
2801 macro_build (char *place, int *counter, expressionS *ep, const char *name,
2802              const char *fmt, ...)
2803 {
2804   struct mips_cl_insn insn;
2805   bfd_reloc_code_real_type r[3];
2806   va_list args;
2807
2808   va_start (args, fmt);
2809
2810   /*
2811    * If the macro is about to expand into a second instruction,
2812    * print a warning if needed. We need to pass ip as a parameter
2813    * to generate a better warning message here...
2814    */
2815   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2816     as_warn (_("Macro instruction expanded into multiple instructions"));
2817
2818   /*
2819    * If the macro is about to expand into a second instruction,
2820    * and it is in a delay slot, print a warning.
2821    */
2822   if (place == NULL
2823       && *counter == 1
2824       && mips_opts.noreorder
2825       && (prev_prev_insn.insn_mo->pinfo
2826           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2827              | INSN_COND_BRANCH_LIKELY)) != 0)
2828     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2829
2830   if (place == NULL)
2831     ++*counter;         /* bump instruction counter */
2832
2833   if (mips_opts.mips16)
2834     {
2835       mips16_macro_build (place, counter, ep, name, fmt, args);
2836       va_end (args);
2837       return;
2838     }
2839
2840   r[0] = BFD_RELOC_UNUSED;
2841   r[1] = BFD_RELOC_UNUSED;
2842   r[2] = BFD_RELOC_UNUSED;
2843   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2844   assert (insn.insn_mo);
2845   assert (strcmp (name, insn.insn_mo->name) == 0);
2846
2847   /* Search until we get a match for NAME.  */
2848   while (1)
2849     {
2850       /* It is assumed here that macros will never generate
2851          MDMX or MIPS-3D instructions.  */
2852       if (strcmp (fmt, insn.insn_mo->args) == 0
2853           && insn.insn_mo->pinfo != INSN_MACRO
2854           && OPCODE_IS_MEMBER (insn.insn_mo,
2855                                (mips_opts.isa
2856                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2857                                mips_opts.arch)
2858           && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2859         break;
2860
2861       ++insn.insn_mo;
2862       assert (insn.insn_mo->name);
2863       assert (strcmp (name, insn.insn_mo->name) == 0);
2864     }
2865
2866   insn.insn_opcode = insn.insn_mo->match;
2867   for (;;)
2868     {
2869       switch (*fmt++)
2870         {
2871         case '\0':
2872           break;
2873
2874         case ',':
2875         case '(':
2876         case ')':
2877           continue;
2878
2879         case '+':
2880           switch (*fmt++)
2881             {
2882             case 'A':
2883             case 'E':
2884               insn.insn_opcode |= (va_arg (args, int)
2885                                    & OP_MASK_SHAMT) << OP_SH_SHAMT;
2886               continue;
2887
2888             case 'B':
2889             case 'F':
2890               /* Note that in the macro case, these arguments are already
2891                  in MSB form.  (When handling the instruction in the
2892                  non-macro case, these arguments are sizes from which
2893                  MSB values must be calculated.)  */
2894               insn.insn_opcode |= (va_arg (args, int)
2895                                    & OP_MASK_INSMSB) << OP_SH_INSMSB;
2896               continue;
2897
2898             case 'C':
2899             case 'G':
2900             case 'H':
2901               /* Note that in the macro case, these arguments are already
2902                  in MSBD form.  (When handling the instruction in the
2903                  non-macro case, these arguments are sizes from which
2904                  MSBD values must be calculated.)  */
2905               insn.insn_opcode |= (va_arg (args, int)
2906                                    & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
2907               continue;
2908
2909             default:
2910               internalError ();
2911             }
2912           continue;
2913
2914         case 't':
2915         case 'w':
2916         case 'E':
2917           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2918           continue;
2919
2920         case 'c':
2921           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2922           continue;
2923
2924         case 'T':
2925         case 'W':
2926           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2927           continue;
2928
2929         case 'd':
2930         case 'G':
2931         case 'K':
2932           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2933           continue;
2934
2935         case 'U':
2936           {
2937             int tmp = va_arg (args, int);
2938
2939             insn.insn_opcode |= tmp << OP_SH_RT;
2940             insn.insn_opcode |= tmp << OP_SH_RD;
2941             continue;
2942           }
2943
2944         case 'V':
2945         case 'S':
2946           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2947           continue;
2948
2949         case 'z':
2950           continue;
2951
2952         case '<':
2953           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2954           continue;
2955
2956         case 'D':
2957           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2958           continue;
2959
2960         case 'B':
2961           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2962           continue;
2963
2964         case 'J':
2965           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2966           continue;
2967
2968         case 'q':
2969           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2970           continue;
2971
2972         case 'b':
2973         case 's':
2974         case 'r':
2975         case 'v':
2976           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2977           continue;
2978
2979         case 'i':
2980         case 'j':
2981         case 'o':
2982           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2983           assert (*r == BFD_RELOC_GPREL16
2984                   || *r == BFD_RELOC_MIPS_LITERAL
2985                   || *r == BFD_RELOC_MIPS_HIGHER
2986                   || *r == BFD_RELOC_HI16_S
2987                   || *r == BFD_RELOC_LO16
2988                   || *r == BFD_RELOC_MIPS_GOT16
2989                   || *r == BFD_RELOC_MIPS_CALL16
2990                   || *r == BFD_RELOC_MIPS_GOT_DISP
2991                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2992                   || *r == BFD_RELOC_MIPS_GOT_OFST
2993                   || *r == BFD_RELOC_MIPS_GOT_LO16
2994                   || *r == BFD_RELOC_MIPS_CALL_LO16
2995                   || (ep->X_op == O_subtract
2996                       && *r == BFD_RELOC_PCREL_LO16));
2997           continue;
2998
2999         case 'u':
3000           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3001           assert (ep != NULL
3002                   && (ep->X_op == O_constant
3003                       || (ep->X_op == O_symbol
3004                           && (*r == BFD_RELOC_MIPS_HIGHEST
3005                               || *r == BFD_RELOC_HI16_S
3006                               || *r == BFD_RELOC_HI16
3007                               || *r == BFD_RELOC_GPREL16
3008                               || *r == BFD_RELOC_MIPS_GOT_HI16
3009                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3010                       || (ep->X_op == O_subtract
3011                           && *r == BFD_RELOC_PCREL_HI16_S)));
3012           continue;
3013
3014         case 'p':
3015           assert (ep != NULL);
3016           /*
3017            * This allows macro() to pass an immediate expression for
3018            * creating short branches without creating a symbol.
3019            * Note that the expression still might come from the assembly
3020            * input, in which case the value is not checked for range nor
3021            * is a relocation entry generated (yuck).
3022            */
3023           if (ep->X_op == O_constant)
3024             {
3025               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3026               ep = NULL;
3027             }
3028           else
3029             *r = BFD_RELOC_16_PCREL_S2;
3030           continue;
3031
3032         case 'a':
3033           assert (ep != NULL);
3034           *r = BFD_RELOC_MIPS_JMP;
3035           continue;
3036
3037         case 'C':
3038           insn.insn_opcode |= va_arg (args, unsigned long);
3039           continue;
3040
3041         default:
3042           internalError ();
3043         }
3044       break;
3045     }
3046   va_end (args);
3047   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3048
3049   append_insn (place, &insn, ep, r);
3050 }
3051
3052 static void
3053 mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3054                     expressionS *ep, const char *name, const char *fmt,
3055                     va_list args)
3056 {
3057   struct mips_cl_insn insn;
3058   bfd_reloc_code_real_type r[3]
3059     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3060
3061   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3062   assert (insn.insn_mo);
3063   assert (strcmp (name, insn.insn_mo->name) == 0);
3064
3065   while (strcmp (fmt, insn.insn_mo->args) != 0
3066          || insn.insn_mo->pinfo == INSN_MACRO)
3067     {
3068       ++insn.insn_mo;
3069       assert (insn.insn_mo->name);
3070       assert (strcmp (name, insn.insn_mo->name) == 0);
3071     }
3072
3073   insn.insn_opcode = insn.insn_mo->match;
3074   insn.use_extend = FALSE;
3075
3076   for (;;)
3077     {
3078       int c;
3079
3080       c = *fmt++;
3081       switch (c)
3082         {
3083         case '\0':
3084           break;
3085
3086         case ',':
3087         case '(':
3088         case ')':
3089           continue;
3090
3091         case 'y':
3092         case 'w':
3093           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3094           continue;
3095
3096         case 'x':
3097         case 'v':
3098           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3099           continue;
3100
3101         case 'z':
3102           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3103           continue;
3104
3105         case 'Z':
3106           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3107           continue;
3108
3109         case '0':
3110         case 'S':
3111         case 'P':
3112         case 'R':
3113           continue;
3114
3115         case 'X':
3116           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3117           continue;
3118
3119         case 'Y':
3120           {
3121             int regno;
3122
3123             regno = va_arg (args, int);
3124             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3125             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3126           }
3127           continue;
3128
3129         case '<':
3130         case '>':
3131         case '4':
3132         case '5':
3133         case 'H':
3134         case 'W':
3135         case 'D':
3136         case 'j':
3137         case '8':
3138         case 'V':
3139         case 'C':
3140         case 'U':
3141         case 'k':
3142         case 'K':
3143         case 'p':
3144         case 'q':
3145           {
3146             assert (ep != NULL);
3147
3148             if (ep->X_op != O_constant)
3149               *r = (int) BFD_RELOC_UNUSED + c;
3150             else
3151               {
3152                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3153                               FALSE, &insn.insn_opcode, &insn.use_extend,
3154                               &insn.extend);
3155                 ep = NULL;
3156                 *r = BFD_RELOC_UNUSED;
3157               }
3158           }
3159           continue;
3160
3161         case '6':
3162           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3163           continue;
3164         }
3165
3166       break;
3167     }
3168
3169   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3170
3171   append_insn (place, &insn, ep, r);
3172 }
3173
3174 /*
3175  * Generate a "jalr" instruction with a relocation hint to the called
3176  * function.  This occurs in NewABI PIC code.
3177  */
3178 static void
3179 macro_build_jalr (int icnt, expressionS *ep)
3180 {
3181   char *f = NULL;
3182
3183   if (HAVE_NEWABI)
3184     {
3185       frag_grow (4);
3186       f = frag_more (0);
3187     }
3188   macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3189   if (HAVE_NEWABI)
3190     fix_new_exp (frag_now, f - frag_now->fr_literal,
3191                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3192 }
3193
3194 /*
3195  * Generate a "lui" instruction.
3196  */
3197 static void
3198 macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
3199 {
3200   expressionS high_expr;
3201   struct mips_cl_insn insn;
3202   bfd_reloc_code_real_type r[3]
3203     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3204   const char *name = "lui";
3205   const char *fmt = "t,u";
3206
3207   assert (! mips_opts.mips16);
3208
3209   if (place == NULL)
3210     high_expr = *ep;
3211   else
3212     {
3213       high_expr.X_op = O_constant;
3214       high_expr.X_add_number = ep->X_add_number;
3215     }
3216
3217   if (high_expr.X_op == O_constant)
3218     {
3219       /* we can compute the instruction now without a relocation entry */
3220       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3221                                 >> 16) & 0xffff;
3222       *r = BFD_RELOC_UNUSED;
3223     }
3224   else
3225     {
3226       assert (ep->X_op == O_symbol);
3227       /* _gp_disp is a special case, used from s_cpload.  */
3228       assert (mips_pic == NO_PIC
3229               || (! HAVE_NEWABI
3230                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3231       *r = BFD_RELOC_HI16_S;
3232     }
3233
3234   /*
3235    * If the macro is about to expand into a second instruction,
3236    * print a warning if needed. We need to pass ip as a parameter
3237    * to generate a better warning message here...
3238    */
3239   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3240     as_warn (_("Macro instruction expanded into multiple instructions"));
3241
3242   if (place == NULL)
3243     ++*counter;         /* bump instruction counter */
3244
3245   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3246   assert (insn.insn_mo);
3247   assert (strcmp (name, insn.insn_mo->name) == 0);
3248   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3249
3250   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3251   if (*r == BFD_RELOC_UNUSED)
3252     {
3253       insn.insn_opcode |= high_expr.X_add_number;
3254       append_insn (place, &insn, NULL, r);
3255     }
3256   else
3257     append_insn (place, &insn, &high_expr, r);
3258 }
3259
3260 /* Generate a sequence of instructions to do a load or store from a constant
3261    offset off of a base register (breg) into/from a target register (treg),
3262    using AT if necessary.  */
3263 static void
3264 macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
3265                               const char *op, int treg, int breg, int dbl)
3266 {
3267   assert (ep->X_op == O_constant);
3268
3269   /* Sign-extending 32-bit constants makes their handling easier.  */
3270   if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3271                   == ~((bfd_vma) 0x7fffffff)))
3272     {
3273       if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3274         as_bad (_("constant too large"));
3275
3276       ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3277                           - 0x80000000);
3278     }
3279
3280   /* Right now, this routine can only handle signed 32-bit constants.  */
3281   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3282     as_warn (_("operand overflow"));
3283
3284   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3285     {
3286       /* Signed 16-bit offset will fit in the op.  Easy!  */
3287       macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3288                    breg);
3289     }
3290   else
3291     {
3292       /* 32-bit offset, need multiple instructions and AT, like:
3293            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3294            addu     $tempreg,$tempreg,$breg
3295            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3296          to handle the complete offset.  */
3297       macro_build_lui (place, counter, ep, AT);
3298       if (place != NULL)
3299         place += 4;
3300       macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3301                    breg);
3302       if (place != NULL)
3303         place += 4;
3304       macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3305                    AT);
3306
3307       if (mips_opts.noat)
3308         as_warn (_("Macro used $at after \".set noat\""));
3309     }
3310 }
3311
3312 /*                      set_at()
3313  * Generates code to set the $at register to true (one)
3314  * if reg is less than the immediate expression.
3315  */
3316 static void
3317 set_at (int *counter, int reg, int unsignedp)
3318 {
3319   if (imm_expr.X_op == O_constant
3320       && imm_expr.X_add_number >= -0x8000
3321       && imm_expr.X_add_number < 0x8000)
3322     macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3323                  "t,r,j", AT, reg, BFD_RELOC_LO16);
3324   else
3325     {
3326       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3327       macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
3328                    "d,v,t", AT, reg, AT);
3329     }
3330 }
3331
3332 static void
3333 normalize_constant_expr (expressionS *ex)
3334 {
3335   if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3336     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3337                         - 0x80000000);
3338 }
3339
3340 /* Warn if an expression is not a constant.  */
3341
3342 static void
3343 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3344 {
3345   if (ex->X_op == O_big)
3346     as_bad (_("unsupported large constant"));
3347   else if (ex->X_op != O_constant)
3348     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3349
3350   normalize_constant_expr (ex);
3351 }
3352
3353 /* Count the leading zeroes by performing a binary chop. This is a
3354    bulky bit of source, but performance is a LOT better for the
3355    majority of values than a simple loop to count the bits:
3356        for (lcnt = 0; (lcnt < 32); lcnt++)
3357          if ((v) & (1 << (31 - lcnt)))
3358            break;
3359   However it is not code size friendly, and the gain will drop a bit
3360   on certain cached systems.
3361 */
3362 #define COUNT_TOP_ZEROES(v)             \
3363   (((v) & ~0xffff) == 0                 \
3364    ? ((v) & ~0xff) == 0                 \
3365      ? ((v) & ~0xf) == 0                \
3366        ? ((v) & ~0x3) == 0              \
3367          ? ((v) & ~0x1) == 0            \
3368            ? !(v)                       \
3369              ? 32                       \
3370              : 31                       \
3371            : 30                         \
3372          : ((v) & ~0x7) == 0            \
3373            ? 29                         \
3374            : 28                         \
3375        : ((v) & ~0x3f) == 0             \
3376          ? ((v) & ~0x1f) == 0           \
3377            ? 27                         \
3378            : 26                         \
3379          : ((v) & ~0x7f) == 0           \
3380            ? 25                         \
3381            : 24                         \
3382      : ((v) & ~0xfff) == 0              \
3383        ? ((v) & ~0x3ff) == 0            \
3384          ? ((v) & ~0x1ff) == 0          \
3385            ? 23                         \
3386            : 22                         \
3387          : ((v) & ~0x7ff) == 0          \
3388            ? 21                         \
3389            : 20                         \
3390        : ((v) & ~0x3fff) == 0           \
3391          ? ((v) & ~0x1fff) == 0         \
3392            ? 19                         \
3393            : 18                         \
3394          : ((v) & ~0x7fff) == 0         \
3395            ? 17                         \
3396            : 16                         \
3397    : ((v) & ~0xffffff) == 0             \
3398      ? ((v) & ~0xfffff) == 0            \
3399        ? ((v) & ~0x3ffff) == 0          \
3400          ? ((v) & ~0x1ffff) == 0        \
3401            ? 15                         \
3402            : 14                         \
3403          : ((v) & ~0x7ffff) == 0        \
3404            ? 13                         \
3405            : 12                         \
3406        : ((v) & ~0x3fffff) == 0         \
3407          ? ((v) & ~0x1fffff) == 0       \
3408            ? 11                         \
3409            : 10                         \
3410          : ((v) & ~0x7fffff) == 0       \
3411            ? 9                          \
3412            : 8                          \
3413      : ((v) & ~0xfffffff) == 0          \
3414        ? ((v) & ~0x3ffffff) == 0        \
3415          ? ((v) & ~0x1ffffff) == 0      \
3416            ? 7                          \
3417            : 6                          \
3418          : ((v) & ~0x7ffffff) == 0      \
3419            ? 5                          \
3420            : 4                          \
3421        : ((v) & ~0x3fffffff) == 0       \
3422          ? ((v) & ~0x1fffffff) == 0     \
3423            ? 3                          \
3424            : 2                          \
3425          : ((v) & ~0x7fffffff) == 0     \
3426            ? 1                          \
3427            : 0)
3428
3429 /*                      load_register()
3430  *  This routine generates the least number of instructions necessary to load
3431  *  an absolute expression value into a register.
3432  */
3433 static void
3434 load_register (int *counter, int reg, expressionS *ep, int dbl)
3435 {
3436   int freg;
3437   expressionS hi32, lo32;
3438
3439   if (ep->X_op != O_big)
3440     {
3441       assert (ep->X_op == O_constant);
3442
3443       /* Sign-extending 32-bit constants makes their handling easier.  */
3444       if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3445                       == ~((bfd_vma) 0x7fffffff)))
3446         {
3447           if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3448             as_bad (_("constant too large"));
3449
3450           ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3451                               - 0x80000000);
3452         }
3453
3454       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3455         {
3456           /* We can handle 16 bit signed values with an addiu to
3457              $zero.  No need to ever use daddiu here, since $zero and
3458              the result are always correct in 32 bit mode.  */
3459           macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3460                        BFD_RELOC_LO16);
3461           return;
3462         }
3463       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3464         {
3465           /* We can handle 16 bit unsigned values with an ori to
3466              $zero.  */
3467           macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3468                        BFD_RELOC_LO16);
3469           return;
3470         }
3471       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3472         {
3473           /* 32 bit values require an lui.  */
3474           macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3475           if ((ep->X_add_number & 0xffff) != 0)
3476             macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3477                          BFD_RELOC_LO16);
3478           return;
3479         }
3480     }
3481
3482   /* The value is larger than 32 bits.  */
3483
3484   if (HAVE_32BIT_GPRS)
3485     {
3486       as_bad (_("Number (0x%lx) larger than 32 bits"),
3487               (unsigned long) ep->X_add_number);
3488       macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3489                    BFD_RELOC_LO16);
3490       return;
3491     }
3492
3493   if (ep->X_op != O_big)
3494     {
3495       hi32 = *ep;
3496       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3497       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3498       hi32.X_add_number &= 0xffffffff;
3499       lo32 = *ep;
3500       lo32.X_add_number &= 0xffffffff;
3501     }
3502   else
3503     {
3504       assert (ep->X_add_number > 2);
3505       if (ep->X_add_number == 3)
3506         generic_bignum[3] = 0;
3507       else if (ep->X_add_number > 4)
3508         as_bad (_("Number larger than 64 bits"));
3509       lo32.X_op = O_constant;
3510       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3511       hi32.X_op = O_constant;
3512       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3513     }
3514
3515   if (hi32.X_add_number == 0)
3516     freg = 0;
3517   else
3518     {
3519       int shift, bit;
3520       unsigned long hi, lo;
3521
3522       if (hi32.X_add_number == (offsetT) 0xffffffff)
3523         {
3524           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3525             {
3526               macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3527                            BFD_RELOC_LO16);
3528               return;
3529             }
3530           if (lo32.X_add_number & 0x80000000)
3531             {
3532               macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3533                            BFD_RELOC_HI16);
3534               if (lo32.X_add_number & 0xffff)
3535                 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3536                              BFD_RELOC_LO16);
3537               return;
3538             }
3539         }
3540
3541       /* Check for 16bit shifted constant.  We know that hi32 is
3542          non-zero, so start the mask on the first bit of the hi32
3543          value.  */
3544       shift = 17;
3545       do
3546         {
3547           unsigned long himask, lomask;
3548
3549           if (shift < 32)
3550             {
3551               himask = 0xffff >> (32 - shift);
3552               lomask = (0xffff << shift) & 0xffffffff;
3553             }
3554           else
3555             {
3556               himask = 0xffff << (shift - 32);
3557               lomask = 0;
3558             }
3559           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3560               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3561             {
3562               expressionS tmp;
3563
3564               tmp.X_op = O_constant;
3565               if (shift < 32)
3566                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3567                                     | (lo32.X_add_number >> shift));
3568               else
3569                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3570               macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3571                            BFD_RELOC_LO16);
3572               macro_build (NULL, counter, NULL,
3573                            (shift >= 32) ? "dsll32" : "dsll",
3574                            "d,w,<", reg, reg,
3575                            (shift >= 32) ? shift - 32 : shift);
3576               return;
3577             }
3578           ++shift;
3579         }
3580       while (shift <= (64 - 16));
3581
3582       /* Find the bit number of the lowest one bit, and store the
3583          shifted value in hi/lo.  */
3584       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3585       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3586       if (lo != 0)
3587         {
3588           bit = 0;
3589           while ((lo & 1) == 0)
3590             {
3591               lo >>= 1;
3592               ++bit;
3593             }
3594           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3595           hi >>= bit;
3596         }
3597       else
3598         {
3599           bit = 32;
3600           while ((hi & 1) == 0)
3601             {
3602               hi >>= 1;
3603               ++bit;
3604             }
3605           lo = hi;
3606           hi = 0;
3607         }
3608
3609       /* Optimize if the shifted value is a (power of 2) - 1.  */
3610       if ((hi == 0 && ((lo + 1) & lo) == 0)
3611           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3612         {
3613           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3614           if (shift != 0)
3615             {
3616               expressionS tmp;
3617
3618               /* This instruction will set the register to be all
3619                  ones.  */
3620               tmp.X_op = O_constant;
3621               tmp.X_add_number = (offsetT) -1;
3622               macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3623                            BFD_RELOC_LO16);
3624               if (bit != 0)
3625                 {
3626                   bit += shift;
3627                   macro_build (NULL, counter, NULL,
3628                                (bit >= 32) ? "dsll32" : "dsll",
3629                                "d,w,<", reg, reg,
3630                                (bit >= 32) ? bit - 32 : bit);
3631                 }
3632               macro_build (NULL, counter, NULL,
3633                            (shift >= 32) ? "dsrl32" : "dsrl",
3634                            "d,w,<", reg, reg,
3635                            (shift >= 32) ? shift - 32 : shift);
3636               return;
3637             }
3638         }
3639
3640       /* Sign extend hi32 before calling load_register, because we can
3641          generally get better code when we load a sign extended value.  */
3642       if ((hi32.X_add_number & 0x80000000) != 0)
3643         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3644       load_register (counter, reg, &hi32, 0);
3645       freg = reg;
3646     }
3647   if ((lo32.X_add_number & 0xffff0000) == 0)
3648     {
3649       if (freg != 0)
3650         {
3651           macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
3652           freg = reg;
3653         }
3654     }
3655   else
3656     {
3657       expressionS mid16;
3658
3659       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3660         {
3661           macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3662                        BFD_RELOC_HI16);
3663           macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
3664           return;
3665         }
3666
3667       if (freg != 0)
3668         {
3669           macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
3670           freg = reg;
3671         }
3672       mid16 = lo32;
3673       mid16.X_add_number >>= 16;
3674       macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3675                    BFD_RELOC_LO16);
3676       macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3677       freg = reg;
3678     }
3679   if ((lo32.X_add_number & 0xffff) != 0)
3680     macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3681                  BFD_RELOC_LO16);
3682 }
3683
3684 /* Load an address into a register.  */
3685
3686 static void
3687 load_address (int *counter, int reg, expressionS *ep, int *used_at)
3688 {
3689   char *p = NULL;
3690
3691   if (ep->X_op != O_constant
3692       && ep->X_op != O_symbol)
3693     {
3694       as_bad (_("expression too complex"));
3695       ep->X_op = O_constant;
3696     }
3697
3698   if (ep->X_op == O_constant)
3699     {
3700       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3701       return;
3702     }
3703
3704   if (mips_pic == NO_PIC)
3705     {
3706       /* If this is a reference to a GP relative symbol, we want
3707            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3708          Otherwise we want
3709            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3710            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3711          If we have an addend, we always use the latter form.
3712
3713          With 64bit address space and a usable $at we want
3714            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3715            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3716            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3717            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3718            dsll32       $reg,0
3719            daddu        $reg,$reg,$at
3720
3721          If $at is already in use, we use a path which is suboptimal
3722          on superscalar processors.
3723            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3724            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3725            dsll         $reg,16
3726            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3727            dsll         $reg,16
3728            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3729        */
3730       if (HAVE_64BIT_ADDRESSES)
3731         {
3732           /* We don't do GP optimization for now because RELAX_ENCODE can't
3733              hold the data for such large chunks.  */
3734
3735           if (*used_at == 0 && ! mips_opts.noat)
3736             {
3737               macro_build (p, counter, ep, "lui", "t,u",
3738                            reg, BFD_RELOC_MIPS_HIGHEST);
3739               macro_build (p, counter, ep, "lui", "t,u",
3740                            AT, BFD_RELOC_HI16_S);
3741               macro_build (p, counter, ep, "daddiu", "t,r,j",
3742                            reg, reg, BFD_RELOC_MIPS_HIGHER);
3743               macro_build (p, counter, ep, "daddiu", "t,r,j",
3744                            AT, AT, BFD_RELOC_LO16);
3745               macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3746               macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
3747               *used_at = 1;
3748             }
3749           else
3750             {
3751               macro_build (p, counter, ep, "lui", "t,u",
3752                            reg, BFD_RELOC_MIPS_HIGHEST);
3753               macro_build (p, counter, ep, "daddiu", "t,r,j",
3754                            reg, reg, BFD_RELOC_MIPS_HIGHER);
3755               macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3756               macro_build (p, counter, ep, "daddiu", "t,r,j",
3757                            reg, reg, BFD_RELOC_HI16_S);
3758               macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3759               macro_build (p, counter, ep, "daddiu", "t,r,j",
3760                            reg, reg, BFD_RELOC_LO16);
3761             }
3762         }
3763       else
3764         {
3765           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3766               && ! nopic_need_relax (ep->X_add_symbol, 1))
3767             {
3768               frag_grow (20);
3769               macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3770                            mips_gp_register, BFD_RELOC_GPREL16);
3771               p = frag_var (rs_machine_dependent, 8, 0,
3772                             RELAX_ENCODE (4, 8, 0, 4, 0,
3773                                           mips_opts.warn_about_macros),
3774                             ep->X_add_symbol, 0, NULL);
3775             }
3776           macro_build_lui (p, counter, ep, reg);
3777           if (p != NULL)
3778             p += 4;
3779           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3780                        BFD_RELOC_LO16);
3781         }
3782     }
3783   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3784     {
3785       expressionS ex;
3786
3787       /* If this is a reference to an external symbol, we want
3788            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3789          Otherwise we want
3790            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3791            nop
3792            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3793          If there is a constant, it must be added in after.
3794
3795          If we have NewABI, we want
3796            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3797          unless we're referencing a global symbol with a non-zero
3798          offset, in which case cst must be added separately.  */
3799       if (HAVE_NEWABI)
3800         {
3801           frag_grow (12);
3802
3803           if (ep->X_add_number)
3804             {
3805               frag_now->tc_frag_data.tc_fr_offset =
3806                 ex.X_add_number = ep->X_add_number;
3807               ep->X_add_number = 0;
3808               macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3809                            reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3810               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3811                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3812               ex.X_op = O_constant;
3813               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3814                            reg, reg, BFD_RELOC_LO16);
3815               p = frag_var (rs_machine_dependent, 8, 0,
3816                             RELAX_ENCODE (8, 4, 0, 0, 0,
3817                                           mips_opts.warn_about_macros),
3818                             ep->X_add_symbol, 0, NULL);
3819               ep->X_add_number = ex.X_add_number;
3820             }
3821
3822           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3823                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3824
3825           if (! p)
3826             {
3827               /* To avoid confusion in tc_gen_reloc, we must ensure
3828                  that this does not become a variant frag.  */
3829               frag_wane (frag_now);
3830               frag_new (0);
3831             }
3832         }
3833       else
3834         {
3835           ex.X_add_number = ep->X_add_number;
3836           ep->X_add_number = 0;
3837           frag_grow (20);
3838           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3839                        BFD_RELOC_MIPS_GOT16,
3840                        mips_gp_register);
3841           macro_build (NULL, counter, NULL, "nop", "");
3842           p = frag_var (rs_machine_dependent, 4, 0,
3843                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3844                         ep->X_add_symbol, 0, NULL);
3845           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3846                        BFD_RELOC_LO16);
3847
3848           if (ex.X_add_number != 0)
3849             {
3850               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3851                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3852               ex.X_op = O_constant;
3853               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3854                            reg, reg, BFD_RELOC_LO16);
3855             }
3856         }
3857     }
3858   else if (mips_pic == SVR4_PIC)
3859     {
3860       expressionS ex;
3861       int off;
3862
3863       /* This is the large GOT case.  If this is a reference to an
3864          external symbol, we want
3865            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3866            addu         $reg,$reg,$gp
3867            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3868
3869          Otherwise, for a reference to a local symbol in old ABI, we want
3870            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3871            nop
3872            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3873          If there is a constant, it must be added in after.
3874
3875          In the NewABI, for local symbols, with or without offsets, we want:
3876            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3877            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3878       */
3879       if (HAVE_NEWABI)
3880         {
3881           frag_grow (24);
3882
3883           frag_now->tc_frag_data.tc_fr_offset =
3884             ex.X_add_number = ep->X_add_number;
3885           ep->X_add_number = 0;
3886           macro_build (NULL, counter, ep, "lui", "t,u", reg,
3887                        BFD_RELOC_MIPS_GOT_HI16);
3888           macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3889                        reg, mips_gp_register);
3890           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3891                        BFD_RELOC_MIPS_GOT_LO16, reg);
3892           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3893             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3894           else if (ex.X_add_number)
3895             {
3896               ex.X_op = O_constant;
3897               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3898                            reg, reg, BFD_RELOC_LO16);
3899             }
3900
3901           ep->X_add_number = ex.X_add_number;
3902           p = frag_var (rs_machine_dependent, 8, 0,
3903                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3904                                       mips_opts.warn_about_macros),
3905                         ep->X_add_symbol, 0, NULL);
3906           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3907                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3908           macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3909                        reg, BFD_RELOC_MIPS_GOT_OFST);
3910         }
3911       else
3912         {
3913           ex.X_add_number = ep->X_add_number;
3914           ep->X_add_number = 0;
3915           if (reg_needs_delay (mips_gp_register))
3916             off = 4;
3917           else
3918             off = 0;
3919           frag_grow (32);
3920           macro_build (NULL, counter, ep, "lui", "t,u", reg,
3921                        BFD_RELOC_MIPS_GOT_HI16);
3922           macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3923                        reg, mips_gp_register);
3924           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3925                        BFD_RELOC_MIPS_GOT_LO16, reg);
3926           p = frag_var (rs_machine_dependent, 12 + off, 0,
3927                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3928                                       mips_opts.warn_about_macros),
3929                         ep->X_add_symbol, 0, NULL);
3930           if (off > 0)
3931             {
3932               /* We need a nop before loading from $gp.  This special
3933                  check is required because the lui which starts the main
3934                  instruction stream does not refer to $gp, and so will not
3935                  insert the nop which may be required.  */
3936               macro_build (p, counter, NULL, "nop", "");
3937                 p += 4;
3938             }
3939           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3940                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
3941           p += 4;
3942           macro_build (p, counter, NULL, "nop", "");
3943           p += 4;
3944           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3945                        BFD_RELOC_LO16);
3946
3947           if (ex.X_add_number != 0)
3948             {
3949               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3950                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3951               ex.X_op = O_constant;
3952               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3953                            reg, reg, BFD_RELOC_LO16);
3954             }
3955         }
3956     }
3957   else if (mips_pic == EMBEDDED_PIC)
3958     {
3959       /* We always do
3960            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3961        */
3962       macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3963                    mips_gp_register, BFD_RELOC_GPREL16);
3964     }
3965   else
3966     abort ();
3967 }
3968
3969 /* Move the contents of register SOURCE into register DEST.  */
3970
3971 static void
3972 move_register (int *counter, int dest, int source)
3973 {
3974   macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
3975                "d,v,t", dest, source, 0);
3976 }
3977
3978 /*
3979  *                      Build macros
3980  *   This routine implements the seemingly endless macro or synthesized
3981  * instructions and addressing modes in the mips assembly language. Many
3982  * of these macros are simple and are similar to each other. These could
3983  * probably be handled by some kind of table or grammar approach instead of
3984  * this verbose method. Others are not simple macros but are more like
3985  * optimizing code generation.
3986  *   One interesting optimization is when several store macros appear
3987  * consecutively that would load AT with the upper half of the same address.
3988  * The ensuing load upper instructions are ommited. This implies some kind
3989  * of global optimization. We currently only optimize within a single macro.
3990  *   For many of the load and store macros if the address is specified as a
3991  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3992  * first load register 'at' with zero and use it as the base register. The
3993  * mips assembler simply uses register $zero. Just one tiny optimization
3994  * we're missing.
3995  */
3996 static void
3997 macro (struct mips_cl_insn *ip)
3998 {
3999   register int treg, sreg, dreg, breg;
4000   int tempreg;
4001   int mask;
4002   int icnt = 0;
4003   int used_at = 0;
4004   expressionS expr1;
4005   const char *s;
4006   const char *s2;
4007   const char *fmt;
4008   int likely = 0;
4009   int dbl = 0;
4010   int coproc = 0;
4011   int lr = 0;
4012   int imm = 0;
4013   int call = 0;
4014   offsetT maxnum;
4015   int off;
4016   bfd_reloc_code_real_type r;
4017   int hold_mips_optimize;
4018
4019   assert (! mips_opts.mips16);
4020
4021   treg = (ip->insn_opcode >> 16) & 0x1f;
4022   dreg = (ip->insn_opcode >> 11) & 0x1f;
4023   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4024   mask = ip->insn_mo->mask;
4025
4026   expr1.X_op = O_constant;
4027   expr1.X_op_symbol = NULL;
4028   expr1.X_add_symbol = NULL;
4029   expr1.X_add_number = 1;
4030
4031   /* Unmatched fixups should not be put in the same frag as a relaxable
4032      macro.  For example, suppose we have:
4033
4034         lui $4,%hi(l1)          # 1
4035         la $5,l2                # 2
4036         addiu $4,$4,%lo(l1)     # 3
4037
4038      If instructions 1 and 2 were put in the same frag, md_frob_file would
4039      move the fixup for #1 after the fixups for the "unrelaxed" version of
4040      #2.  This would confuse tc_gen_reloc, which expects the relocations
4041      for #2 to be the last for that frag.
4042
4043      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4044      it assumes that they belong to a relaxable macro.  We mustn't put
4045      other uses of such relocations into a variant frag.
4046
4047      To avoid both problems, finish the current frag it contains a
4048      %reloc() operator.  The macro then goes into a new frag.  */
4049   if (prev_reloc_op_frag == frag_now)
4050     {
4051       frag_wane (frag_now);
4052       frag_new (0);
4053     }
4054
4055   switch (mask)
4056     {
4057     case M_DABS:
4058       dbl = 1;
4059     case M_ABS:
4060       /* bgez $a0,.+12
4061          move v0,$a0
4062          sub v0,$zero,$a0
4063          */
4064
4065       mips_emit_delays (TRUE);
4066       ++mips_opts.noreorder;
4067       mips_any_noreorder = 1;
4068
4069       expr1.X_add_number = 8;
4070       macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4071       if (dreg == sreg)
4072         macro_build (NULL, &icnt, NULL, "nop", "", 0);
4073       else
4074         move_register (&icnt, dreg, sreg);
4075       macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4076                    sreg);
4077
4078       --mips_opts.noreorder;
4079       return;
4080
4081     case M_ADD_I:
4082       s = "addi";
4083       s2 = "add";
4084       goto do_addi;
4085     case M_ADDU_I:
4086       s = "addiu";
4087       s2 = "addu";
4088       goto do_addi;
4089     case M_DADD_I:
4090       dbl = 1;
4091       s = "daddi";
4092       s2 = "dadd";
4093       goto do_addi;
4094     case M_DADDU_I:
4095       dbl = 1;
4096       s = "daddiu";
4097       s2 = "daddu";
4098     do_addi:
4099       if (imm_expr.X_op == O_constant
4100           && imm_expr.X_add_number >= -0x8000
4101           && imm_expr.X_add_number < 0x8000)
4102         {
4103           macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4104                        BFD_RELOC_LO16);
4105           return;
4106         }
4107       load_register (&icnt, AT, &imm_expr, dbl);
4108       macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4109       break;
4110
4111     case M_AND_I:
4112       s = "andi";
4113       s2 = "and";
4114       goto do_bit;
4115     case M_OR_I:
4116       s = "ori";
4117       s2 = "or";
4118       goto do_bit;
4119     case M_NOR_I:
4120       s = "";
4121       s2 = "nor";
4122       goto do_bit;
4123     case M_XOR_I:
4124       s = "xori";
4125       s2 = "xor";
4126     do_bit:
4127       if (imm_expr.X_op == O_constant
4128           && imm_expr.X_add_number >= 0
4129           && imm_expr.X_add_number < 0x10000)
4130         {
4131           if (mask != M_NOR_I)
4132             macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4133                          BFD_RELOC_LO16);
4134           else
4135             {
4136               macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4137                            BFD_RELOC_LO16);
4138               macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
4139             }
4140           return;
4141         }
4142
4143       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4144       macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4145       break;
4146
4147     case M_BEQ_I:
4148       s = "beq";
4149       goto beq_i;
4150     case M_BEQL_I:
4151       s = "beql";
4152       likely = 1;
4153       goto beq_i;
4154     case M_BNE_I:
4155       s = "bne";
4156       goto beq_i;
4157     case M_BNEL_I:
4158       s = "bnel";
4159       likely = 1;
4160     beq_i:
4161       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4162         {
4163           macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
4164           return;
4165         }
4166       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4167       macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4168       break;
4169
4170     case M_BGEL:
4171       likely = 1;
4172     case M_BGE:
4173       if (treg == 0)
4174         {
4175           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4176                        "s,p", sreg);
4177           return;
4178         }
4179       if (sreg == 0)
4180         {
4181           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4182                        "s,p", treg);
4183           return;
4184         }
4185       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4186       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4187                    "s,t,p", AT, 0);
4188       break;
4189
4190     case M_BGTL_I:
4191       likely = 1;
4192     case M_BGT_I:
4193       /* check for > max integer */
4194       maxnum = 0x7fffffff;
4195       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4196         {
4197           maxnum <<= 16;
4198           maxnum |= 0xffff;
4199           maxnum <<= 16;
4200           maxnum |= 0xffff;
4201         }
4202       if (imm_expr.X_op == O_constant
4203           && imm_expr.X_add_number >= maxnum
4204           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4205         {
4206         do_false:
4207           /* result is always false */
4208           if (! likely)
4209             {
4210               if (warn_nops)
4211                 as_warn (_("Branch %s is always false (nop)"),
4212                          ip->insn_mo->name);
4213               macro_build (NULL, &icnt, NULL, "nop", "", 0);
4214             }
4215           else
4216             {
4217               if (warn_nops)
4218                 as_warn (_("Branch likely %s is always false"),
4219                          ip->insn_mo->name);
4220               macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
4221             }
4222           return;
4223         }
4224       if (imm_expr.X_op != O_constant)
4225         as_bad (_("Unsupported large constant"));
4226       ++imm_expr.X_add_number;
4227       /* FALLTHROUGH */
4228     case M_BGE_I:
4229     case M_BGEL_I:
4230       if (mask == M_BGEL_I)
4231         likely = 1;
4232       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4233         {
4234           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4235                        "s,p", sreg);
4236           return;
4237         }
4238       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4239         {
4240           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4241                        "s,p", sreg);
4242           return;
4243         }
4244       maxnum = 0x7fffffff;
4245       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4246         {
4247           maxnum <<= 16;
4248           maxnum |= 0xffff;
4249           maxnum <<= 16;
4250           maxnum |= 0xffff;
4251         }
4252       maxnum = - maxnum - 1;
4253       if (imm_expr.X_op == O_constant
4254           && imm_expr.X_add_number <= maxnum
4255           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4256         {
4257         do_true:
4258           /* result is always true */
4259           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4260           macro_build (NULL, &icnt, &offset_expr, "b", "p");
4261           return;
4262         }
4263       set_at (&icnt, sreg, 0);
4264       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4265                    "s,t,p", AT, 0);
4266       break;
4267
4268     case M_BGEUL:
4269       likely = 1;
4270     case M_BGEU:
4271       if (treg == 0)
4272         goto do_true;
4273       if (sreg == 0)
4274         {
4275           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4276                        "s,t,p", 0, treg);
4277           return;
4278         }
4279       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4280       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4281                    "s,t,p", AT, 0);
4282       break;
4283
4284     case M_BGTUL_I:
4285       likely = 1;
4286     case M_BGTU_I:
4287       if (sreg == 0
4288           || (HAVE_32BIT_GPRS
4289               && imm_expr.X_op == O_constant
4290               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4291         goto do_false;
4292       if (imm_expr.X_op != O_constant)
4293         as_bad (_("Unsupported large constant"));
4294       ++imm_expr.X_add_number;
4295       /* FALLTHROUGH */
4296     case M_BGEU_I:
4297     case M_BGEUL_I:
4298       if (mask == M_BGEUL_I)
4299         likely = 1;
4300       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4301         goto do_true;
4302       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4303         {
4304           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4305                        "s,t,p", sreg, 0);
4306           return;
4307         }
4308       set_at (&icnt, sreg, 1);
4309       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4310                    "s,t,p", AT, 0);
4311       break;
4312
4313     case M_BGTL:
4314       likely = 1;
4315     case M_BGT:
4316       if (treg == 0)
4317         {
4318           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4319                        "s,p", sreg);
4320           return;
4321         }
4322       if (sreg == 0)
4323         {
4324           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4325                        "s,p", treg);
4326           return;
4327         }
4328       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4329       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4330                    "s,t,p", AT, 0);
4331       break;
4332
4333     case M_BGTUL:
4334       likely = 1;
4335     case M_BGTU:
4336       if (treg == 0)
4337         {
4338           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4339                        "s,t,p", sreg, 0);
4340           return;
4341         }
4342       if (sreg == 0)
4343         goto do_false;
4344       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4345       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4346                    "s,t,p", AT, 0);
4347       break;
4348
4349     case M_BLEL:
4350       likely = 1;
4351     case M_BLE:
4352       if (treg == 0)
4353         {
4354           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4355                        "s,p", sreg);
4356           return;
4357         }
4358       if (sreg == 0)
4359         {
4360           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4361                        "s,p", treg);
4362           return;
4363         }
4364       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4365       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4366                    "s,t,p", AT, 0);
4367       break;
4368
4369     case M_BLEL_I:
4370       likely = 1;
4371     case M_BLE_I:
4372       maxnum = 0x7fffffff;
4373       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4374         {
4375           maxnum <<= 16;
4376           maxnum |= 0xffff;
4377           maxnum <<= 16;
4378           maxnum |= 0xffff;
4379         }
4380       if (imm_expr.X_op == O_constant
4381           && imm_expr.X_add_number >= maxnum
4382           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4383         goto do_true;
4384       if (imm_expr.X_op != O_constant)
4385         as_bad (_("Unsupported large constant"));
4386       ++imm_expr.X_add_number;
4387       /* FALLTHROUGH */
4388     case M_BLT_I:
4389     case M_BLTL_I:
4390       if (mask == M_BLTL_I)
4391         likely = 1;
4392       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4393         {
4394           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4395                        "s,p", sreg);
4396           return;
4397         }
4398       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4399         {
4400           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4401                        "s,p", sreg);
4402           return;
4403         }
4404       set_at (&icnt, sreg, 0);
4405       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4406                    "s,t,p", AT, 0);
4407       break;
4408
4409     case M_BLEUL:
4410       likely = 1;
4411     case M_BLEU:
4412       if (treg == 0)
4413         {
4414           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4415                        "s,t,p", sreg, 0);
4416           return;
4417         }
4418       if (sreg == 0)
4419         goto do_true;
4420       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4421       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4422                    "s,t,p", AT, 0);
4423       break;
4424
4425     case M_BLEUL_I:
4426       likely = 1;
4427     case M_BLEU_I:
4428       if (sreg == 0
4429           || (HAVE_32BIT_GPRS
4430               && imm_expr.X_op == O_constant
4431               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4432         goto do_true;
4433       if (imm_expr.X_op != O_constant)
4434         as_bad (_("Unsupported large constant"));
4435       ++imm_expr.X_add_number;
4436       /* FALLTHROUGH */
4437     case M_BLTU_I:
4438     case M_BLTUL_I:
4439       if (mask == M_BLTUL_I)
4440         likely = 1;
4441       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4442         goto do_false;
4443       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4444         {
4445           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4446                        "s,t,p", sreg, 0);
4447           return;
4448         }
4449       set_at (&icnt, sreg, 1);
4450       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4451                    "s,t,p", AT, 0);
4452       break;
4453
4454     case M_BLTL:
4455       likely = 1;
4456     case M_BLT:
4457       if (treg == 0)
4458         {
4459           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4460                        "s,p", sreg);
4461           return;
4462         }
4463       if (sreg == 0)
4464         {
4465           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4466                        "s,p", treg);
4467           return;
4468         }
4469       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4470       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4471                    "s,t,p", AT, 0);
4472       break;
4473
4474     case M_BLTUL:
4475       likely = 1;
4476     case M_BLTU:
4477       if (treg == 0)
4478         goto do_false;
4479       if (sreg == 0)
4480         {
4481           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4482                        "s,t,p", 0, treg);
4483           return;
4484         }
4485       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4486       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4487                    "s,t,p", AT, 0);
4488       break;
4489
4490     case M_DEXT:
4491       {
4492         unsigned long pos;
4493         unsigned long size;
4494
4495         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4496           {
4497             as_bad (_("Unsupported large constant"));
4498             pos = size = 1;
4499           }
4500         else
4501           {
4502             pos = (unsigned long) imm_expr.X_add_number;
4503             size = (unsigned long) imm2_expr.X_add_number;
4504           }
4505
4506         if (pos > 63)
4507           {
4508             as_bad (_("Improper position (%lu)"), pos);
4509             pos = 1;
4510           }
4511         if (size == 0 || size > 64
4512             || (pos + size - 1) > 63)
4513           {
4514             as_bad (_("Improper extract size (%lu, position %lu)"),
4515                     size, pos);
4516             size = 1;
4517           }
4518
4519         if (size <= 32 && pos < 32)
4520           {
4521             s = "dext";
4522             fmt = "t,r,+A,+C";
4523           }
4524         else if (size <= 32)
4525           {
4526             s = "dextu";
4527             fmt = "t,r,+E,+H";
4528           }
4529         else
4530           {
4531             s = "dextm";
4532             fmt = "t,r,+A,+G";
4533           }
4534         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4535                      fmt, treg, sreg, pos, size - 1);
4536       }
4537       return;
4538
4539     case M_DINS:
4540       {
4541         unsigned long pos;
4542         unsigned long size;
4543
4544         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4545           {
4546             as_bad (_("Unsupported large constant"));
4547             pos = size = 1;
4548           }
4549         else
4550           {
4551             pos = (unsigned long) imm_expr.X_add_number;
4552             size = (unsigned long) imm2_expr.X_add_number;
4553           }
4554
4555         if (pos > 63)
4556           {
4557             as_bad (_("Improper position (%lu)"), pos);
4558             pos = 1;
4559           }
4560         if (size == 0 || size > 64
4561             || (pos + size - 1) > 63)
4562           {
4563             as_bad (_("Improper insert size (%lu, position %lu)"),
4564                     size, pos);
4565             size = 1;
4566           }
4567
4568         if (pos < 32 && (pos + size - 1) < 32)
4569           {
4570             s = "dins";
4571             fmt = "t,r,+A,+B";
4572           }
4573         else if (pos >= 32)
4574           {
4575             s = "dinsu";
4576             fmt = "t,r,+E,+F";
4577           }
4578         else
4579           {
4580             s = "dinsm";
4581             fmt = "t,r,+A,+F";
4582           }
4583         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4584                      fmt, treg, sreg, pos, pos + size - 1);
4585       }
4586       return;
4587
4588     case M_DDIV_3:
4589       dbl = 1;
4590     case M_DIV_3:
4591       s = "mflo";
4592       goto do_div3;
4593     case M_DREM_3:
4594       dbl = 1;
4595     case M_REM_3:
4596       s = "mfhi";
4597     do_div3:
4598       if (treg == 0)
4599         {
4600           as_warn (_("Divide by zero."));
4601           if (mips_trap)
4602             macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4603           else
4604             macro_build (NULL, &icnt, NULL, "break", "c", 7);
4605           return;
4606         }
4607
4608       mips_emit_delays (TRUE);
4609       ++mips_opts.noreorder;
4610       mips_any_noreorder = 1;
4611       if (mips_trap)
4612         {
4613           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4614           macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4615                        sreg, treg);
4616         }
4617       else
4618         {
4619           expr1.X_add_number = 8;
4620           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4621           macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4622                        sreg, treg);
4623           macro_build (NULL, &icnt, NULL, "break", "c", 7);
4624         }
4625       expr1.X_add_number = -1;
4626       macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4627                    AT, 0, BFD_RELOC_LO16);
4628       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4629       macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4630       if (dbl)
4631         {
4632           expr1.X_add_number = 1;
4633           macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4634                        BFD_RELOC_LO16);
4635           macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
4636         }
4637       else
4638         {
4639           expr1.X_add_number = 0x80000000;
4640           macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4641                        BFD_RELOC_HI16);
4642         }
4643       if (mips_trap)
4644         {
4645           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
4646           /* We want to close the noreorder block as soon as possible, so
4647              that later insns are available for delay slot filling.  */
4648           --mips_opts.noreorder;
4649         }
4650       else
4651         {
4652           expr1.X_add_number = 8;
4653           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4654           macro_build (NULL, &icnt, NULL, "nop", "", 0);
4655
4656           /* We want to close the noreorder block as soon as possible, so
4657              that later insns are available for delay slot filling.  */
4658           --mips_opts.noreorder;
4659
4660           macro_build (NULL, &icnt, NULL, "break", "c", 6);
4661         }
4662       macro_build (NULL, &icnt, NULL, s, "d", dreg);
4663       break;
4664
4665     case M_DIV_3I:
4666       s = "div";
4667       s2 = "mflo";
4668       goto do_divi;
4669     case M_DIVU_3I:
4670       s = "divu";
4671       s2 = "mflo";
4672       goto do_divi;
4673     case M_REM_3I:
4674       s = "div";
4675       s2 = "mfhi";
4676       goto do_divi;
4677     case M_REMU_3I:
4678       s = "divu";
4679       s2 = "mfhi";
4680       goto do_divi;
4681     case M_DDIV_3I:
4682       dbl = 1;
4683       s = "ddiv";
4684       s2 = "mflo";
4685       goto do_divi;
4686     case M_DDIVU_3I:
4687       dbl = 1;
4688       s = "ddivu";
4689       s2 = "mflo";
4690       goto do_divi;
4691     case M_DREM_3I:
4692       dbl = 1;
4693       s = "ddiv";
4694       s2 = "mfhi";
4695       goto do_divi;
4696     case M_DREMU_3I:
4697       dbl = 1;
4698       s = "ddivu";
4699       s2 = "mfhi";
4700     do_divi:
4701       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4702         {
4703           as_warn (_("Divide by zero."));
4704           if (mips_trap)
4705             macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4706           else
4707             macro_build (NULL, &icnt, NULL, "break", "c", 7);
4708           return;
4709         }
4710       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4711         {
4712           if (strcmp (s2, "mflo") == 0)
4713             move_register (&icnt, dreg, sreg);
4714           else
4715             move_register (&icnt, dreg, 0);
4716           return;
4717         }
4718       if (imm_expr.X_op == O_constant
4719           && imm_expr.X_add_number == -1
4720           && s[strlen (s) - 1] != 'u')
4721         {
4722           if (strcmp (s2, "mflo") == 0)
4723             {
4724               macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4725                            dreg, sreg);
4726             }
4727           else
4728             move_register (&icnt, dreg, 0);
4729           return;
4730         }
4731
4732       load_register (&icnt, AT, &imm_expr, dbl);
4733       macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4734       macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4735       break;
4736
4737     case M_DIVU_3:
4738       s = "divu";
4739       s2 = "mflo";
4740       goto do_divu3;
4741     case M_REMU_3:
4742       s = "divu";
4743       s2 = "mfhi";
4744       goto do_divu3;
4745     case M_DDIVU_3:
4746       s = "ddivu";
4747       s2 = "mflo";
4748       goto do_divu3;
4749     case M_DREMU_3:
4750       s = "ddivu";
4751       s2 = "mfhi";
4752     do_divu3:
4753       mips_emit_delays (TRUE);
4754       ++mips_opts.noreorder;
4755       mips_any_noreorder = 1;
4756       if (mips_trap)
4757         {
4758           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4759           macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4760           /* We want to close the noreorder block as soon as possible, so
4761              that later insns are available for delay slot filling.  */
4762           --mips_opts.noreorder;
4763         }
4764       else
4765         {
4766           expr1.X_add_number = 8;
4767           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4768           macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4769
4770           /* We want to close the noreorder block as soon as possible, so
4771              that later insns are available for delay slot filling.  */
4772           --mips_opts.noreorder;
4773           macro_build (NULL, &icnt, NULL, "break", "c", 7);
4774         }
4775       macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4776       return;
4777
4778     case M_DLCA_AB:
4779       dbl = 1;
4780     case M_LCA_AB:
4781       call = 1;
4782       goto do_la;
4783     case M_DLA_AB:
4784       dbl = 1;
4785     case M_LA_AB:
4786     do_la:
4787       /* Load the address of a symbol into a register.  If breg is not
4788          zero, we then add a base register to it.  */
4789
4790       if (dbl && HAVE_32BIT_GPRS)
4791         as_warn (_("dla used to load 32-bit register"));
4792
4793       if (! dbl && HAVE_64BIT_OBJECTS)
4794         as_warn (_("la used to load 64-bit address"));
4795
4796       if (offset_expr.X_op == O_constant
4797           && offset_expr.X_add_number >= -0x8000
4798           && offset_expr.X_add_number < 0x8000)
4799         {
4800           macro_build (NULL, &icnt, &offset_expr,
4801                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4802                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
4803           return;
4804         }
4805
4806       if (treg == breg)
4807         {
4808           tempreg = AT;
4809           used_at = 1;
4810         }
4811       else
4812         {
4813           tempreg = treg;
4814           used_at = 0;
4815         }
4816
4817       /* When generating embedded PIC code, we permit expressions of
4818          the form
4819            la   $treg,foo-bar
4820            la   $treg,foo-bar($breg)
4821          where bar is an address in the current section.  These are used
4822          when getting the addresses of functions.  We don't permit
4823          X_add_number to be non-zero, because if the symbol is
4824          external the relaxing code needs to know that any addend is
4825          purely the offset to X_op_symbol.  */
4826       if (mips_pic == EMBEDDED_PIC
4827           && offset_expr.X_op == O_subtract
4828           && (symbol_constant_p (offset_expr.X_op_symbol)
4829               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4830               : (symbol_equated_p (offset_expr.X_op_symbol)
4831                  && (S_GET_SEGMENT
4832                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4833                       ->X_add_symbol)
4834                      == now_seg)))
4835           && (offset_expr.X_add_number == 0
4836               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4837         {
4838           if (breg == 0)
4839             {
4840               tempreg = treg;
4841               used_at = 0;
4842               macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4843                            BFD_RELOC_PCREL_HI16_S);
4844             }
4845           else
4846             {
4847               macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4848                            BFD_RELOC_PCREL_HI16_S);
4849               macro_build (NULL, &icnt, NULL,
4850                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4851                            "d,v,t", tempreg, tempreg, breg);
4852             }
4853           macro_build (NULL, &icnt, &offset_expr,
4854                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4855                        "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
4856           if (! used_at)
4857             return;
4858           break;
4859         }
4860
4861       if (offset_expr.X_op != O_symbol
4862           && offset_expr.X_op != O_constant)
4863         {
4864           as_bad (_("expression too complex"));
4865           offset_expr.X_op = O_constant;
4866         }
4867
4868       if (offset_expr.X_op == O_constant)
4869         load_register (&icnt, tempreg, &offset_expr,
4870                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4871                         ? (dbl || HAVE_64BIT_ADDRESSES)
4872                         : HAVE_64BIT_ADDRESSES));
4873       else if (mips_pic == NO_PIC)
4874         {
4875           /* If this is a reference to a GP relative symbol, we want
4876                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4877              Otherwise we want
4878                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4879                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4880              If we have a constant, we need two instructions anyhow,
4881              so we may as well always use the latter form.
4882
4883             With 64bit address space and a usable $at we want
4884               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4885               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4886               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4887               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4888               dsll32    $tempreg,0
4889               daddu     $tempreg,$tempreg,$at
4890
4891             If $at is already in use, we use a path which is suboptimal
4892             on superscalar processors.
4893               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4894               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4895               dsll      $tempreg,16
4896               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4897               dsll      $tempreg,16
4898               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4899           */
4900           char *p = NULL;
4901           if (HAVE_64BIT_ADDRESSES)
4902             {
4903               /* We don't do GP optimization for now because RELAX_ENCODE can't
4904                  hold the data for such large chunks.  */
4905
4906               if (used_at == 0 && ! mips_opts.noat)
4907                 {
4908                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4909                                tempreg, BFD_RELOC_MIPS_HIGHEST);
4910                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4911                                AT, BFD_RELOC_HI16_S);
4912                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4913                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4914                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4915                                AT, AT, BFD_RELOC_LO16);
4916                   macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4917                                tempreg, tempreg, 0);
4918                   macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4919                                tempreg, tempreg, AT);
4920                   used_at = 1;
4921                 }
4922               else
4923                 {
4924                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4925                                tempreg, BFD_RELOC_MIPS_HIGHEST);
4926                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4927                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4928                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4929                                tempreg, tempreg, 16);
4930                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4931                                tempreg, tempreg, BFD_RELOC_HI16_S);
4932                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4933                                tempreg, tempreg, 16);
4934                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4935                                tempreg, tempreg, BFD_RELOC_LO16);
4936                 }
4937             }
4938           else
4939             {
4940               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4941                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4942                 {
4943                   frag_grow (20);
4944                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4945                                "t,r,j", tempreg, mips_gp_register,
4946                                BFD_RELOC_GPREL16);
4947                   p = frag_var (rs_machine_dependent, 8, 0,
4948                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4949                                               mips_opts.warn_about_macros),
4950                                 offset_expr.X_add_symbol, 0, NULL);
4951                 }
4952               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4953               if (p != NULL)
4954                 p += 4;
4955               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4956                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4957             }
4958         }
4959       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4960         {
4961           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4962
4963           /* If this is a reference to an external symbol, and there
4964              is no constant, we want
4965                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4966              or for lca or if tempreg is PIC_CALL_REG
4967                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4968              For a local symbol, we want
4969                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4970                nop
4971                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4972
4973              If we have a small constant, and this is a reference to
4974              an external symbol, we want
4975                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4976                nop
4977                addiu    $tempreg,$tempreg,<constant>
4978              For a local symbol, we want the same instruction
4979              sequence, but we output a BFD_RELOC_LO16 reloc on the
4980              addiu instruction.
4981
4982              If we have a large constant, and this is a reference to
4983              an external symbol, we want
4984                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4985                lui      $at,<hiconstant>
4986                addiu    $at,$at,<loconstant>
4987                addu     $tempreg,$tempreg,$at
4988              For a local symbol, we want the same instruction
4989              sequence, but we output a BFD_RELOC_LO16 reloc on the
4990              addiu instruction.
4991            */
4992
4993           expr1.X_add_number = offset_expr.X_add_number;
4994           offset_expr.X_add_number = 0;
4995           frag_grow (32);
4996           if (expr1.X_add_number == 0 && breg == 0
4997               && (call || tempreg == PIC_CALL_REG))
4998             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4999           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5000                        tempreg, lw_reloc_type, mips_gp_register);
5001           if (expr1.X_add_number == 0)
5002             {
5003               int off;
5004               char *p;
5005
5006               if (breg == 0)
5007                 off = 0;
5008               else
5009                 {
5010                   /* We're going to put in an addu instruction using
5011                      tempreg, so we may as well insert the nop right
5012                      now.  */
5013                   macro_build (NULL, &icnt, NULL, "nop", "");
5014                   off = 4;
5015                 }
5016               p = frag_var (rs_machine_dependent, 8 - off, 0,
5017                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5018                                           (breg == 0
5019                                            ? mips_opts.warn_about_macros
5020                                            : 0)),
5021                             offset_expr.X_add_symbol, 0, NULL);
5022               if (breg == 0)
5023                 {
5024                   macro_build (p, &icnt, NULL, "nop", "");
5025                   p += 4;
5026                 }
5027               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5028                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5029               /* FIXME: If breg == 0, and the next instruction uses
5030                  $tempreg, then if this variant case is used an extra
5031                  nop will be generated.  */
5032             }
5033           else if (expr1.X_add_number >= -0x8000
5034                    && expr1.X_add_number < 0x8000)
5035             {
5036               macro_build (NULL, &icnt, NULL, "nop", "");
5037               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5038                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5039               frag_var (rs_machine_dependent, 0, 0,
5040                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5041                         offset_expr.X_add_symbol, 0, NULL);
5042             }
5043           else
5044             {
5045               int off1;
5046
5047               /* If we are going to add in a base register, and the
5048                  target register and the base register are the same,
5049                  then we are using AT as a temporary register.  Since
5050                  we want to load the constant into AT, we add our
5051                  current AT (from the global offset table) and the
5052                  register into the register now, and pretend we were
5053                  not using a base register.  */
5054               if (breg != treg)
5055                 off1 = 0;
5056               else
5057                 {
5058                   macro_build (NULL, &icnt, NULL, "nop", "");
5059                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5060                                treg, AT, breg);
5061                   breg = 0;
5062                   tempreg = treg;
5063                   off1 = -8;
5064                 }
5065
5066               /* Set mips_optimize around the lui instruction to avoid
5067                  inserting an unnecessary nop after the lw.  */
5068               hold_mips_optimize = mips_optimize;
5069               mips_optimize = 2;
5070               macro_build_lui (NULL, &icnt, &expr1, AT);
5071               mips_optimize = hold_mips_optimize;
5072
5073               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5074                            AT, AT, BFD_RELOC_LO16);
5075               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5076                            tempreg, tempreg, AT);
5077               frag_var (rs_machine_dependent, 0, 0,
5078                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5079                         offset_expr.X_add_symbol, 0, NULL);
5080               used_at = 1;
5081             }
5082         }
5083       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5084         {
5085           char *p = NULL;
5086           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5087           int adj = 0;
5088
5089           /* If this is a reference to an external, and there is no
5090              constant, or local symbol (*), with or without a
5091              constant, we want
5092                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5093              or for lca or if tempreg is PIC_CALL_REG
5094                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5095
5096              If we have a small constant, and this is a reference to
5097              an external symbol, we want
5098                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5099                addiu    $tempreg,$tempreg,<constant>
5100
5101              If we have a large constant, and this is a reference to
5102              an external symbol, we want
5103                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5104                lui      $at,<hiconstant>
5105                addiu    $at,$at,<loconstant>
5106                addu     $tempreg,$tempreg,$at
5107
5108              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5109              local symbols, even though it introduces an additional
5110              instruction.  */
5111
5112           frag_grow (28);
5113           if (offset_expr.X_add_number == 0 && breg == 0
5114               && (call || tempreg == PIC_CALL_REG))
5115             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5116           if (offset_expr.X_add_number)
5117             {
5118               frag_now->tc_frag_data.tc_fr_offset =
5119                 expr1.X_add_number = offset_expr.X_add_number;
5120               offset_expr.X_add_number = 0;
5121
5122               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5123                            "t,o(b)", tempreg, lw_reloc_type,
5124                            mips_gp_register);
5125
5126               if (expr1.X_add_number >= -0x8000
5127                   && expr1.X_add_number < 0x8000)
5128                 {
5129                   macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5130                                "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5131                   p = frag_var (rs_machine_dependent, 4, 0,
5132                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5133                                 offset_expr.X_add_symbol, 0, NULL);
5134                 }
5135               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5136                 {
5137                   int dreg;
5138
5139                   /* If we are going to add in a base register, and the
5140                      target register and the base register are the same,
5141                      then we are using AT as a temporary register.  Since
5142                      we want to load the constant into AT, we add our
5143                      current AT (from the global offset table) and the
5144                      register into the register now, and pretend we were
5145                      not using a base register.  */
5146                   if (breg != treg)
5147                     dreg = tempreg;
5148                   else
5149                     {
5150                       assert (tempreg == AT);
5151                       macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN,
5152                                    "d,v,t", treg, AT, breg);
5153                       dreg = treg;
5154                       adj = 4;
5155                     }
5156
5157                   macro_build_lui (NULL, &icnt, &expr1, AT);
5158                   macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5159                                "t,r,j", AT, AT, BFD_RELOC_LO16);
5160                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5161                                dreg, dreg, AT);
5162
5163                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
5164                                 RELAX_ENCODE (16 + adj, 4 + adj,
5165                                               0, 0, 0, 0),
5166                                 offset_expr.X_add_symbol, 0, NULL);
5167
5168                   used_at = 1;
5169                 }
5170               else
5171                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5172
5173               offset_expr.X_add_number = expr1.X_add_number;
5174
5175               macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5176                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
5177                            mips_gp_register);
5178               if (adj)
5179                 {
5180                   macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5181                                treg, tempreg, breg);
5182                   breg = 0;
5183                   tempreg = treg;
5184                 }
5185             }
5186           else
5187             {
5188               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5189                            "t,o(b)", tempreg, lw_reloc_type,
5190                            mips_gp_register);
5191               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5192                 p = frag_var (rs_machine_dependent, 0, 0,
5193                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5194                               offset_expr.X_add_symbol, 0, NULL);
5195             }
5196
5197           if (! p)
5198             {
5199               /* To avoid confusion in tc_gen_reloc, we must ensure
5200                  that this does not become a variant frag.  */
5201               frag_wane (frag_now);
5202               frag_new (0);
5203             }
5204         }
5205       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5206         {
5207           int gpdel;
5208           char *p;
5209           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5210           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5211           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5212
5213           /* This is the large GOT case.  If this is a reference to an
5214              external symbol, and there is no constant, we want
5215                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5216                addu     $tempreg,$tempreg,$gp
5217                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5218              or for lca or if tempreg is PIC_CALL_REG
5219                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5220                addu     $tempreg,$tempreg,$gp
5221                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5222              For a local symbol, we want
5223                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5224                nop
5225                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5226
5227              If we have a small constant, and this is a reference to
5228              an external symbol, we want
5229                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5230                addu     $tempreg,$tempreg,$gp
5231                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5232                nop
5233                addiu    $tempreg,$tempreg,<constant>
5234              For a local symbol, we want
5235                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5236                nop
5237                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5238
5239              If we have a large constant, and this is a reference to
5240              an external symbol, we want
5241                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5242                addu     $tempreg,$tempreg,$gp
5243                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5244                lui      $at,<hiconstant>
5245                addiu    $at,$at,<loconstant>
5246                addu     $tempreg,$tempreg,$at
5247              For a local symbol, we want
5248                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5249                lui      $at,<hiconstant>
5250                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5251                addu     $tempreg,$tempreg,$at
5252           */
5253
5254           expr1.X_add_number = offset_expr.X_add_number;
5255           offset_expr.X_add_number = 0;
5256           frag_grow (52);
5257           if (reg_needs_delay (mips_gp_register))
5258             gpdel = 4;
5259           else
5260             gpdel = 0;
5261           if (expr1.X_add_number == 0 && breg == 0
5262               && (call || tempreg == PIC_CALL_REG))
5263             {
5264               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5265               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5266             }
5267           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5268                        tempreg, lui_reloc_type);
5269           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5270                        tempreg, tempreg, mips_gp_register);
5271           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5272                        tempreg, lw_reloc_type, tempreg);
5273           if (expr1.X_add_number == 0)
5274             {
5275               int off;
5276
5277               if (breg == 0)
5278                 off = 0;
5279               else
5280                 {
5281                   /* We're going to put in an addu instruction using
5282                      tempreg, so we may as well insert the nop right
5283                      now.  */
5284                   macro_build (NULL, &icnt, NULL, "nop", "");
5285                   off = 4;
5286                 }
5287
5288               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5289                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5290                                           8 + gpdel, 0,
5291                                           (breg == 0
5292                                            ? mips_opts.warn_about_macros
5293                                            : 0)),
5294                             offset_expr.X_add_symbol, 0, NULL);
5295             }
5296           else if (expr1.X_add_number >= -0x8000
5297                    && expr1.X_add_number < 0x8000)
5298             {
5299               macro_build (NULL, &icnt, NULL, "nop", "");
5300               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5301                            tempreg, tempreg, BFD_RELOC_LO16);
5302
5303               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5304                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5305                                           (breg == 0
5306                                            ? mips_opts.warn_about_macros
5307                                            : 0)),
5308                             offset_expr.X_add_symbol, 0, NULL);
5309             }
5310           else
5311             {
5312               int adj, dreg;
5313
5314               /* If we are going to add in a base register, and the
5315                  target register and the base register are the same,
5316                  then we are using AT as a temporary register.  Since
5317                  we want to load the constant into AT, we add our
5318                  current AT (from the global offset table) and the
5319                  register into the register now, and pretend we were
5320                  not using a base register.  */
5321               if (breg != treg)
5322                 {
5323                   adj = 0;
5324                   dreg = tempreg;
5325                 }
5326               else
5327                 {
5328                   assert (tempreg == AT);
5329                   macro_build (NULL, &icnt, NULL, "nop", "");
5330                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5331                                treg, AT, breg);
5332                   dreg = treg;
5333                   adj = 8;
5334                 }
5335
5336               /* Set mips_optimize around the lui instruction to avoid
5337                  inserting an unnecessary nop after the lw.  */
5338               hold_mips_optimize = mips_optimize;
5339               mips_optimize = 2;
5340               macro_build_lui (NULL, &icnt, &expr1, AT);
5341               mips_optimize = hold_mips_optimize;
5342
5343               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5344                            AT, AT, BFD_RELOC_LO16);
5345               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5346                            dreg, dreg, AT);
5347
5348               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5349                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5350                                           8 + gpdel, 0,
5351                                           (breg == 0
5352                                            ? mips_opts.warn_about_macros
5353                                            : 0)),
5354                             offset_expr.X_add_symbol, 0, NULL);
5355
5356               used_at = 1;
5357             }
5358
5359           if (gpdel > 0)
5360             {
5361               /* This is needed because this instruction uses $gp, but
5362                  the first instruction on the main stream does not.  */
5363               macro_build (p, &icnt, NULL, "nop", "");
5364               p += 4;
5365             }
5366
5367           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5368                        tempreg, local_reloc_type, mips_gp_register);
5369           p += 4;
5370           if (expr1.X_add_number >= -0x8000
5371               && expr1.X_add_number < 0x8000)
5372             {
5373               macro_build (p, &icnt, NULL, "nop", "");
5374               p += 4;
5375               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5376                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5377               /* FIXME: If add_number is 0, and there was no base
5378                  register, the external symbol case ended with a load,
5379                  so if the symbol turns out to not be external, and
5380                  the next instruction uses tempreg, an unnecessary nop
5381                  will be inserted.  */
5382             }
5383           else
5384             {
5385               if (breg == treg)
5386                 {
5387                   /* We must add in the base register now, as in the
5388                      external symbol case.  */
5389                   assert (tempreg == AT);
5390                   macro_build (p, &icnt, NULL, "nop", "");
5391                   p += 4;
5392                   macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5393                                treg, AT, breg);
5394                   p += 4;
5395                   tempreg = treg;
5396                   /* We set breg to 0 because we have arranged to add
5397                      it in in both cases.  */
5398                   breg = 0;
5399                 }
5400
5401               macro_build_lui (p, &icnt, &expr1, AT);
5402               p += 4;
5403               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5404                            AT, AT, BFD_RELOC_LO16);
5405               p += 4;
5406               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5407                            tempreg, tempreg, AT);
5408               p += 4;
5409             }
5410         }
5411       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5412         {
5413           char *p = NULL;
5414           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5415           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5416           int adj = 0;
5417
5418           /* This is the large GOT case.  If this is a reference to an
5419              external symbol, and there is no constant, we want
5420                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5421                add      $tempreg,$tempreg,$gp
5422                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5423              or for lca or if tempreg is PIC_CALL_REG
5424                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5425                add      $tempreg,$tempreg,$gp
5426                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5427
5428              If we have a small constant, and this is a reference to
5429              an external symbol, we want
5430                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5431                add      $tempreg,$tempreg,$gp
5432                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5433                addi     $tempreg,$tempreg,<constant>
5434
5435              If we have a large constant, and this is a reference to
5436              an external symbol, we want
5437                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5438                addu     $tempreg,$tempreg,$gp
5439                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5440                lui      $at,<hiconstant>
5441                addi     $at,$at,<loconstant>
5442                add      $tempreg,$tempreg,$at
5443
5444              If we have NewABI, and we know it's a local symbol, we want
5445                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5446                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5447              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5448
5449           frag_grow (40);
5450
5451           frag_now->tc_frag_data.tc_fr_offset =
5452             expr1.X_add_number = offset_expr.X_add_number;
5453           offset_expr.X_add_number = 0;
5454
5455           if (expr1.X_add_number == 0 && breg == 0
5456               && (call || tempreg == PIC_CALL_REG))
5457             {
5458               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5459               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5460             }
5461           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5462                        tempreg, lui_reloc_type);
5463           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5464                        tempreg, tempreg, mips_gp_register);
5465           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5466                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5467
5468           if (expr1.X_add_number == 0)
5469             {
5470               p = frag_var (rs_machine_dependent, 8, 0,
5471                             RELAX_ENCODE (12, 8, 0, 4, 0,
5472                                           mips_opts.warn_about_macros),
5473                             offset_expr.X_add_symbol, 0, NULL);
5474             }
5475           else if (expr1.X_add_number >= -0x8000
5476                    && expr1.X_add_number < 0x8000)
5477             {
5478               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5479                            tempreg, tempreg, BFD_RELOC_LO16);
5480               p = frag_var (rs_machine_dependent, 8, 0,
5481                             RELAX_ENCODE (16, 8, 0, 4, 0,
5482                                           mips_opts.warn_about_macros),
5483                             offset_expr.X_add_symbol, 0, NULL);
5484             }
5485           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5486             {
5487               int dreg;
5488
5489               /* If we are going to add in a base register, and the
5490                  target register and the base register are the same,
5491                  then we are using AT as a temporary register.  Since
5492                  we want to load the constant into AT, we add our
5493                  current AT (from the global offset table) and the
5494                  register into the register now, and pretend we were
5495                  not using a base register.  */
5496               if (breg != treg)
5497                 dreg = tempreg;
5498               else
5499                 {
5500                   assert (tempreg == AT);
5501                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5502                                treg, AT, breg);
5503                   dreg = treg;
5504                   adj = 4;
5505                 }
5506
5507               /* Set mips_optimize around the lui instruction to avoid
5508                  inserting an unnecessary nop after the lw.  */
5509               macro_build_lui (NULL, &icnt, &expr1, AT);
5510               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5511                            "t,r,j", AT, AT, BFD_RELOC_LO16);
5512               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5513                            dreg, dreg, AT);
5514
5515               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5516                             RELAX_ENCODE (24 + adj, 8 + adj,
5517                                           0, 4, 0,
5518                                           (breg == 0
5519                                            ? mips_opts.warn_about_macros
5520                                            : 0)),
5521                             offset_expr.X_add_symbol, 0, NULL);
5522
5523               used_at = 1;
5524             }
5525           else
5526             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5527
5528           offset_expr.X_add_number = expr1.X_add_number;
5529           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5530                        tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5531           macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5532                        tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
5533           if (adj)
5534             {
5535               macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5536                            treg, tempreg, breg);
5537               breg = 0;
5538               tempreg = treg;
5539             }
5540         }
5541       else if (mips_pic == EMBEDDED_PIC)
5542         {
5543           /* We use
5544                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5545              */
5546           macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5547                        tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5548         }
5549       else
5550         abort ();
5551
5552       if (breg != 0)
5553         {
5554           char *s;
5555
5556           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5557             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5558           else
5559             s = ADDRESS_ADD_INSN;
5560
5561           macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
5562         }
5563
5564       if (! used_at)
5565         return;
5566
5567       break;
5568
5569     case M_J_A:
5570       /* The j instruction may not be used in PIC code, since it
5571          requires an absolute address.  We convert it to a b
5572          instruction.  */
5573       if (mips_pic == NO_PIC)
5574         macro_build (NULL, &icnt, &offset_expr, "j", "a");
5575       else
5576         macro_build (NULL, &icnt, &offset_expr, "b", "p");
5577       return;
5578
5579       /* The jal instructions must be handled as macros because when
5580          generating PIC code they expand to multi-instruction
5581          sequences.  Normally they are simple instructions.  */
5582     case M_JAL_1:
5583       dreg = RA;
5584       /* Fall through.  */
5585     case M_JAL_2:
5586       if (mips_pic == NO_PIC
5587           || mips_pic == EMBEDDED_PIC)
5588         macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5589       else if (mips_pic == SVR4_PIC)
5590         {
5591           if (sreg != PIC_CALL_REG)
5592             as_warn (_("MIPS PIC call to register other than $25"));
5593
5594           macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5595           if (! HAVE_NEWABI)
5596             {
5597               if (mips_cprestore_offset < 0)
5598                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5599               else
5600                 {
5601                   if (! mips_frame_reg_valid)
5602                     {
5603                       as_warn (_("No .frame pseudo-op used in PIC code"));
5604                       /* Quiet this warning.  */
5605                       mips_frame_reg_valid = 1;
5606                     }
5607                   if (! mips_cprestore_valid)
5608                     {
5609                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5610                       /* Quiet this warning.  */
5611                       mips_cprestore_valid = 1;
5612                     }
5613                   expr1.X_add_number = mips_cprestore_offset;
5614                   macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5615                                                 ADDRESS_LOAD_INSN,
5616                                                 mips_gp_register,
5617                                                 mips_frame_reg,
5618                                                 HAVE_64BIT_ADDRESSES);
5619                 }
5620             }
5621         }
5622       else
5623         abort ();
5624
5625       return;
5626
5627     case M_JAL_A:
5628       if (mips_pic == NO_PIC)
5629         macro_build (NULL, &icnt, &offset_expr, "jal", "a");
5630       else if (mips_pic == SVR4_PIC)
5631         {
5632           char *p;
5633
5634           /* If this is a reference to an external symbol, and we are
5635              using a small GOT, we want
5636                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5637                nop
5638                jalr     $ra,$25
5639                nop
5640                lw       $gp,cprestore($sp)
5641              The cprestore value is set using the .cprestore
5642              pseudo-op.  If we are using a big GOT, we want
5643                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5644                addu     $25,$25,$gp
5645                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5646                nop
5647                jalr     $ra,$25
5648                nop
5649                lw       $gp,cprestore($sp)
5650              If the symbol is not external, we want
5651                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5652                nop
5653                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5654                jalr     $ra,$25
5655                nop
5656                lw $gp,cprestore($sp)
5657
5658              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5659              sequences above, minus nops, unless the symbol is local,
5660              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5661              GOT_DISP.  */
5662           if (HAVE_NEWABI)
5663             {
5664               if (! mips_big_got)
5665                 {
5666                   frag_grow (4);
5667                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5668                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5669                                mips_gp_register);
5670                   frag_var (rs_machine_dependent, 0, 0,
5671                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5672                             offset_expr.X_add_symbol, 0, NULL);
5673                 }
5674               else
5675                 {
5676                   frag_grow (20);
5677                   macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5678                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5679                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5680                                PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5681                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5682                                "t,o(b)", PIC_CALL_REG,
5683                                BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5684                   p = frag_var (rs_machine_dependent, 8, 0,
5685                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5686                                 offset_expr.X_add_symbol, 0, NULL);
5687                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5688                                "t,o(b)", PIC_CALL_REG,
5689                                BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5690                   macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5691                                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5692                                BFD_RELOC_MIPS_GOT_OFST);
5693                 }
5694
5695               macro_build_jalr (icnt, &offset_expr);
5696             }
5697           else
5698             {
5699               frag_grow (40);
5700               if (! mips_big_got)
5701                 {
5702                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5703                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5704                                mips_gp_register);
5705                   macro_build (NULL, &icnt, NULL, "nop", "");
5706                   p = frag_var (rs_machine_dependent, 4, 0,
5707                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5708                                 offset_expr.X_add_symbol, 0, NULL);
5709                 }
5710               else
5711                 {
5712                   int gpdel;
5713
5714                   if (reg_needs_delay (mips_gp_register))
5715                     gpdel = 4;
5716                   else
5717                     gpdel = 0;
5718                   macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5719                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5720                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5721                                PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5722                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5723                                "t,o(b)", PIC_CALL_REG,
5724                                BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5725                   macro_build (NULL, &icnt, NULL, "nop", "");
5726                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5727                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5728                                               8 + gpdel, 0, 0),
5729                                 offset_expr.X_add_symbol, 0, NULL);
5730                   if (gpdel > 0)
5731                     {
5732                       macro_build (p, &icnt, NULL, "nop", "");
5733                       p += 4;
5734                     }
5735                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5736                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5737                                mips_gp_register);
5738                   p += 4;
5739                   macro_build (p, &icnt, NULL, "nop", "");
5740                   p += 4;
5741                 }
5742               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5743                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5744                            BFD_RELOC_LO16);
5745               macro_build_jalr (icnt, &offset_expr);
5746
5747               if (mips_cprestore_offset < 0)
5748                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5749               else
5750                 {
5751                   if (! mips_frame_reg_valid)
5752                     {
5753                       as_warn (_("No .frame pseudo-op used in PIC code"));
5754                       /* Quiet this warning.  */
5755                       mips_frame_reg_valid = 1;
5756                     }
5757                   if (! mips_cprestore_valid)
5758                     {
5759                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5760                       /* Quiet this warning.  */
5761                       mips_cprestore_valid = 1;
5762                     }
5763                   if (mips_opts.noreorder)
5764                     macro_build (NULL, &icnt, NULL, "nop", "");
5765                   expr1.X_add_number = mips_cprestore_offset;
5766                   macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5767                                                 ADDRESS_LOAD_INSN,
5768                                                 mips_gp_register,
5769                                                 mips_frame_reg,
5770                                                 HAVE_64BIT_ADDRESSES);
5771                 }
5772             }
5773         }
5774       else if (mips_pic == EMBEDDED_PIC)
5775         {
5776           macro_build (NULL, &icnt, &offset_expr, "bal", "p");
5777           /* The linker may expand the call to a longer sequence which
5778              uses $at, so we must break rather than return.  */
5779           break;
5780         }
5781       else
5782         abort ();
5783
5784       return;
5785
5786     case M_LB_AB:
5787       s = "lb";
5788       goto ld;
5789     case M_LBU_AB:
5790       s = "lbu";
5791       goto ld;
5792     case M_LH_AB:
5793       s = "lh";
5794       goto ld;
5795     case M_LHU_AB:
5796       s = "lhu";
5797       goto ld;
5798     case M_LW_AB:
5799       s = "lw";
5800       goto ld;
5801     case M_LWC0_AB:
5802       s = "lwc0";
5803       /* Itbl support may require additional care here.  */
5804       coproc = 1;
5805       goto ld;
5806     case M_LWC1_AB:
5807       s = "lwc1";
5808       /* Itbl support may require additional care here.  */
5809       coproc = 1;
5810       goto ld;
5811     case M_LWC2_AB:
5812       s = "lwc2";
5813       /* Itbl support may require additional care here.  */
5814       coproc = 1;
5815       goto ld;
5816     case M_LWC3_AB:
5817       s = "lwc3";
5818       /* Itbl support may require additional care here.  */
5819       coproc = 1;
5820       goto ld;
5821     case M_LWL_AB:
5822       s = "lwl";
5823       lr = 1;
5824       goto ld;
5825     case M_LWR_AB:
5826       s = "lwr";
5827       lr = 1;
5828       goto ld;
5829     case M_LDC1_AB:
5830       if (mips_opts.arch == CPU_R4650)
5831         {
5832           as_bad (_("opcode not supported on this processor"));
5833           return;
5834         }
5835       s = "ldc1";
5836       /* Itbl support may require additional care here.  */
5837       coproc = 1;
5838       goto ld;
5839     case M_LDC2_AB:
5840       s = "ldc2";
5841       /* Itbl support may require additional care here.  */
5842       coproc = 1;
5843       goto ld;
5844     case M_LDC3_AB:
5845       s = "ldc3";
5846       /* Itbl support may require additional care here.  */
5847       coproc = 1;
5848       goto ld;
5849     case M_LDL_AB:
5850       s = "ldl";
5851       lr = 1;
5852       goto ld;
5853     case M_LDR_AB:
5854       s = "ldr";
5855       lr = 1;
5856       goto ld;
5857     case M_LL_AB:
5858       s = "ll";
5859       goto ld;
5860     case M_LLD_AB:
5861       s = "lld";
5862       goto ld;
5863     case M_LWU_AB:
5864       s = "lwu";
5865     ld:
5866       if (breg == treg || coproc || lr)
5867         {
5868           tempreg = AT;
5869           used_at = 1;
5870         }
5871       else
5872         {
5873           tempreg = treg;
5874           used_at = 0;
5875         }
5876       goto ld_st;
5877     case M_SB_AB:
5878       s = "sb";
5879       goto st;
5880     case M_SH_AB:
5881       s = "sh";
5882       goto st;
5883     case M_SW_AB:
5884       s = "sw";
5885       goto st;
5886     case M_SWC0_AB:
5887       s = "swc0";
5888       /* Itbl support may require additional care here.  */
5889       coproc = 1;
5890       goto st;
5891     case M_SWC1_AB:
5892       s = "swc1";
5893       /* Itbl support may require additional care here.  */
5894       coproc = 1;
5895       goto st;
5896     case M_SWC2_AB:
5897       s = "swc2";
5898       /* Itbl support may require additional care here.  */
5899       coproc = 1;
5900       goto st;
5901     case M_SWC3_AB:
5902       s = "swc3";
5903       /* Itbl support may require additional care here.  */
5904       coproc = 1;
5905       goto st;
5906     case M_SWL_AB:
5907       s = "swl";
5908       goto st;
5909     case M_SWR_AB:
5910       s = "swr";
5911       goto st;
5912     case M_SC_AB:
5913       s = "sc";
5914       goto st;
5915     case M_SCD_AB:
5916       s = "scd";
5917       goto st;
5918     case M_SDC1_AB:
5919       if (mips_opts.arch == CPU_R4650)
5920         {
5921           as_bad (_("opcode not supported on this processor"));
5922           return;
5923         }
5924       s = "sdc1";
5925       coproc = 1;
5926       /* Itbl support may require additional care here.  */
5927       goto st;
5928     case M_SDC2_AB:
5929       s = "sdc2";
5930       /* Itbl support may require additional care here.  */
5931       coproc = 1;
5932       goto st;
5933     case M_SDC3_AB:
5934       s = "sdc3";
5935       /* Itbl support may require additional care here.  */
5936       coproc = 1;
5937       goto st;
5938     case M_SDL_AB:
5939       s = "sdl";
5940       goto st;
5941     case M_SDR_AB:
5942       s = "sdr";
5943     st:
5944       tempreg = AT;
5945       used_at = 1;
5946     ld_st:
5947       /* Itbl support may require additional care here.  */
5948       if (mask == M_LWC1_AB
5949           || mask == M_SWC1_AB
5950           || mask == M_LDC1_AB
5951           || mask == M_SDC1_AB
5952           || mask == M_L_DAB
5953           || mask == M_S_DAB)
5954         fmt = "T,o(b)";
5955       else if (coproc)
5956         fmt = "E,o(b)";
5957       else
5958         fmt = "t,o(b)";
5959
5960       /* Sign-extending 32-bit constants makes their handling easier.
5961          The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5962          described below.  */
5963       if ((! HAVE_64BIT_ADDRESSES
5964            && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
5965           && (offset_expr.X_op == O_constant)
5966           && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5967                 == ~((bfd_vma) 0x7fffffff)))
5968         {
5969           if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
5970             as_bad (_("constant too large"));
5971
5972           offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5973                                        ^ 0x80000000) - 0x80000000);
5974         }
5975
5976       /* For embedded PIC, we allow loads where the offset is calculated
5977          by subtracting a symbol in the current segment from an unknown
5978          symbol, relative to a base register, e.g.:
5979                 <op>    $treg, <sym>-<localsym>($breg)
5980          This is used by the compiler for switch statements.  */
5981       if (mips_pic == EMBEDDED_PIC
5982           && offset_expr.X_op == O_subtract
5983           && (symbol_constant_p (offset_expr.X_op_symbol)
5984               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5985               : (symbol_equated_p (offset_expr.X_op_symbol)
5986                  && (S_GET_SEGMENT
5987                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5988                       ->X_add_symbol)
5989                      == now_seg)))
5990           && breg != 0
5991           && (offset_expr.X_add_number == 0
5992               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5993         {
5994           /* For this case, we output the instructions:
5995                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5996                 addiu   $tempreg,$tempreg,$breg
5997                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5998              If the relocation would fit entirely in 16 bits, it would be
5999              nice to emit:
6000                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
6001              instead, but that seems quite difficult.  */
6002           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6003                        BFD_RELOC_PCREL_HI16_S);
6004           macro_build (NULL, &icnt, NULL,
6005                        ((bfd_arch_bits_per_address (stdoutput) == 32
6006                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
6007                         ? "addu" : "daddu"),
6008                        "d,v,t", tempreg, tempreg, breg);
6009           macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6010                        BFD_RELOC_PCREL_LO16, tempreg);
6011           if (! used_at)
6012             return;
6013           break;
6014         }
6015
6016       if (offset_expr.X_op != O_constant
6017           && offset_expr.X_op != O_symbol)
6018         {
6019           as_bad (_("expression too complex"));
6020           offset_expr.X_op = O_constant;
6021         }
6022
6023       /* A constant expression in PIC code can be handled just as it
6024          is in non PIC code.  */
6025       if (mips_pic == NO_PIC
6026           || offset_expr.X_op == O_constant)
6027         {
6028           char *p;
6029
6030           /* If this is a reference to a GP relative symbol, and there
6031              is no base register, we want
6032                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6033              Otherwise, if there is no base register, we want
6034                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6035                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6036              If we have a constant, we need two instructions anyhow,
6037              so we always use the latter form.
6038
6039              If we have a base register, and this is a reference to a
6040              GP relative symbol, we want
6041                addu     $tempreg,$breg,$gp
6042                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6043              Otherwise we want
6044                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6045                addu     $tempreg,$tempreg,$breg
6046                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6047              With a constant we always use the latter case.
6048
6049              With 64bit address space and no base register and $at usable,
6050              we want
6051                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6052                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6053                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6054                dsll32   $tempreg,0
6055                daddu    $tempreg,$at
6056                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6057              If we have a base register, we want
6058                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6059                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6060                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6061                daddu    $at,$breg
6062                dsll32   $tempreg,0
6063                daddu    $tempreg,$at
6064                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6065
6066              Without $at we can't generate the optimal path for superscalar
6067              processors here since this would require two temporary registers.
6068                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6069                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6070                dsll     $tempreg,16
6071                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6072                dsll     $tempreg,16
6073                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6074              If we have a base register, we want
6075                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6076                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6077                dsll     $tempreg,16
6078                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6079                dsll     $tempreg,16
6080                daddu    $tempreg,$tempreg,$breg
6081                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6082
6083              If we have 64-bit addresses, as an optimization, for
6084              addresses which are 32-bit constants (e.g. kseg0/kseg1
6085              addresses) we fall back to the 32-bit address generation
6086              mechanism since it is more efficient.  Note that due to
6087              the signed offset used by memory operations, the 32-bit
6088              range is shifted down by 32768 here.  This code should
6089              probably attempt to generate 64-bit constants more
6090              efficiently in general.
6091
6092              As an extension for architectures with 64-bit registers,
6093              we don't truncate 64-bit addresses given as literal
6094              constants down to 32 bits, to support existing practice
6095              in the mips64 Linux (the kernel), that compiles source
6096              files with -mabi=64, assembling them as o32 or n32 (with
6097              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
6098              the whole kernel is loaded into a memory region that is
6099              addressable with sign-extended 32-bit addresses, it is
6100              wasteful to compute the upper 32 bits of every
6101              non-literal address, that takes more space and time.
6102              Some day this should probably be implemented as an
6103              assembler option, such that the kernel doesn't have to
6104              use such ugly hacks, even though it will still have to
6105              end up converting the binary to ELF32 for a number of
6106              platforms whose boot loaders don't support ELF64
6107              binaries.  */
6108           if ((HAVE_64BIT_ADDRESSES
6109                && ! (offset_expr.X_op == O_constant
6110                      && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6111               || (HAVE_64BIT_GPRS
6112                   && offset_expr.X_op == O_constant
6113                   && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6114             {
6115               p = NULL;
6116
6117               /* We don't do GP optimization for now because RELAX_ENCODE can't
6118                  hold the data for such large chunks.  */
6119
6120               if (used_at == 0 && ! mips_opts.noat)
6121                 {
6122                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6123                                tempreg, BFD_RELOC_MIPS_HIGHEST);
6124                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6125                                AT, BFD_RELOC_HI16_S);
6126                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6127                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6128                   if (breg != 0)
6129                     macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6130                                  AT, AT, breg);
6131                   macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
6132                                tempreg, tempreg, 0);
6133                   macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6134                                tempreg, tempreg, AT);
6135                   macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6136                                BFD_RELOC_LO16, tempreg);
6137                   used_at = 1;
6138                 }
6139               else
6140                 {
6141                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6142                                tempreg, BFD_RELOC_MIPS_HIGHEST);
6143                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6144                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6145                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6146                                tempreg, tempreg, 16);
6147                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6148                                tempreg, tempreg, BFD_RELOC_HI16_S);
6149                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6150                                tempreg, tempreg, 16);
6151                   if (breg != 0)
6152                     macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6153                                  tempreg, tempreg, breg);
6154                   macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6155                                BFD_RELOC_LO16, tempreg);
6156                 }
6157
6158               return;
6159             }
6160
6161           if (offset_expr.X_op == O_constant
6162               && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
6163             as_bad (_("load/store address overflow (max 32 bits)"));
6164
6165           if (breg == 0)
6166             {
6167               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6168                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6169                 p = NULL;
6170               else
6171                 {
6172                   frag_grow (20);
6173                   macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6174                                BFD_RELOC_GPREL16, mips_gp_register);
6175                   p = frag_var (rs_machine_dependent, 8, 0,
6176                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6177                                               (mips_opts.warn_about_macros
6178                                                || (used_at
6179                                                    && mips_opts.noat))),
6180                                 offset_expr.X_add_symbol, 0, NULL);
6181                   used_at = 0;
6182                 }
6183               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6184               if (p != NULL)
6185                 p += 4;
6186               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6187                            BFD_RELOC_LO16, tempreg);
6188             }
6189           else
6190             {
6191               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6192                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6193                 p = NULL;
6194               else
6195                 {
6196                   frag_grow (28);
6197                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6198                                tempreg, breg, mips_gp_register);
6199                   macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6200                                BFD_RELOC_GPREL16, tempreg);
6201                   p = frag_var (rs_machine_dependent, 12, 0,
6202                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6203                                 offset_expr.X_add_symbol, 0, NULL);
6204                 }
6205               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6206               if (p != NULL)
6207                 p += 4;
6208               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6209                            tempreg, tempreg, breg);
6210               if (p != NULL)
6211                 p += 4;
6212               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6213                            BFD_RELOC_LO16, tempreg);
6214             }
6215         }
6216       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6217         {
6218           char *p;
6219           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6220
6221           /* If this is a reference to an external symbol, we want
6222                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6223                nop
6224                <op>     $treg,0($tempreg)
6225              Otherwise we want
6226                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6227                nop
6228                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6229                <op>     $treg,0($tempreg)
6230
6231              For NewABI, we want
6232                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6233                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6234
6235              If there is a base register, we add it to $tempreg before
6236              the <op>.  If there is a constant, we stick it in the
6237              <op> instruction.  We don't handle constants larger than
6238              16 bits, because we have no way to load the upper 16 bits
6239              (actually, we could handle them for the subset of cases
6240              in which we are not using $at).  */
6241           assert (offset_expr.X_op == O_symbol);
6242           if (HAVE_NEWABI)
6243             {
6244               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6245                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6246                            mips_gp_register);
6247               if (breg != 0)
6248                 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6249                              tempreg, tempreg, breg);
6250               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6251                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
6252
6253               if (! used_at)
6254                 return;
6255
6256               break;
6257             }
6258           expr1.X_add_number = offset_expr.X_add_number;
6259           offset_expr.X_add_number = 0;
6260           if (expr1.X_add_number < -0x8000
6261               || expr1.X_add_number >= 0x8000)
6262             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6263           frag_grow (20);
6264           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6265                        tempreg, lw_reloc_type, mips_gp_register);
6266           macro_build (NULL, &icnt, NULL, "nop", "");
6267           p = frag_var (rs_machine_dependent, 4, 0,
6268                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6269                         offset_expr.X_add_symbol, 0, NULL);
6270           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6271                        "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
6272           if (breg != 0)
6273             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6274                          tempreg, tempreg, breg);
6275           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6276                        tempreg);
6277         }
6278       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6279         {
6280           int gpdel;
6281           char *p;
6282
6283           /* If this is a reference to an external symbol, we want
6284                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6285                addu     $tempreg,$tempreg,$gp
6286                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6287                <op>     $treg,0($tempreg)
6288              Otherwise we want
6289                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6290                nop
6291                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6292                <op>     $treg,0($tempreg)
6293              If there is a base register, we add it to $tempreg before
6294              the <op>.  If there is a constant, we stick it in the
6295              <op> instruction.  We don't handle constants larger than
6296              16 bits, because we have no way to load the upper 16 bits
6297              (actually, we could handle them for the subset of cases
6298              in which we are not using $at).  */
6299           assert (offset_expr.X_op == O_symbol);
6300           expr1.X_add_number = offset_expr.X_add_number;
6301           offset_expr.X_add_number = 0;
6302           if (expr1.X_add_number < -0x8000
6303               || expr1.X_add_number >= 0x8000)
6304             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6305           if (reg_needs_delay (mips_gp_register))
6306             gpdel = 4;
6307           else
6308             gpdel = 0;
6309           frag_grow (36);
6310           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6311                        BFD_RELOC_MIPS_GOT_HI16);
6312           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6313                        tempreg, tempreg, mips_gp_register);
6314           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6315                        tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6316           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6317                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6318                         offset_expr.X_add_symbol, 0, NULL);
6319           if (gpdel > 0)
6320             {
6321               macro_build (p, &icnt, NULL, "nop", "");
6322               p += 4;
6323             }
6324           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6325                        tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6326           p += 4;
6327           macro_build (p, &icnt, NULL, "nop", "");
6328           p += 4;
6329           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6330                        tempreg, tempreg, BFD_RELOC_LO16);
6331           if (breg != 0)
6332             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6333                          tempreg, tempreg, breg);
6334           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6335                        tempreg);
6336         }
6337       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6338         {
6339           char *p;
6340           int bregsz = breg != 0 ? 4 : 0;
6341
6342           /* If this is a reference to an external symbol, we want
6343                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6344                add      $tempreg,$tempreg,$gp
6345                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6346                <op>     $treg,<ofst>($tempreg)
6347              Otherwise, for local symbols, we want:
6348                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6349                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6350           assert (offset_expr.X_op == O_symbol);
6351           frag_grow (36);
6352           frag_now->tc_frag_data.tc_fr_offset =
6353             expr1.X_add_number = offset_expr.X_add_number;
6354           offset_expr.X_add_number = 0;
6355           if (expr1.X_add_number < -0x8000
6356               || expr1.X_add_number >= 0x8000)
6357             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6358           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6359                        BFD_RELOC_MIPS_GOT_HI16);
6360           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6361                        tempreg, tempreg, mips_gp_register);
6362           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6363                        tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6364           if (breg != 0)
6365             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6366                          tempreg, tempreg, breg);
6367           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6368                        tempreg);
6369
6370           offset_expr.X_add_number = expr1.X_add_number;
6371           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6372                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6373                                       0, 4 + bregsz, 0, 0),
6374                         offset_expr.X_add_symbol, 0, NULL);
6375           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6376                        tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6377           if (breg != 0)
6378             macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6379                          tempreg, tempreg, breg);
6380           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6381                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
6382         }
6383       else if (mips_pic == EMBEDDED_PIC)
6384         {
6385           /* If there is no base register, we want
6386                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6387              If there is a base register, we want
6388                addu     $tempreg,$breg,$gp
6389                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6390              */
6391           assert (offset_expr.X_op == O_symbol);
6392           if (breg == 0)
6393             {
6394               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6395                            BFD_RELOC_GPREL16, mips_gp_register);
6396               used_at = 0;
6397             }
6398           else
6399             {
6400               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6401                            tempreg, breg, mips_gp_register);
6402               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6403                            BFD_RELOC_GPREL16, tempreg);
6404             }
6405         }
6406       else
6407         abort ();
6408
6409       if (! used_at)
6410         return;
6411
6412       break;
6413
6414     case M_LI:
6415     case M_LI_S:
6416       load_register (&icnt, treg, &imm_expr, 0);
6417       return;
6418
6419     case M_DLI:
6420       load_register (&icnt, treg, &imm_expr, 1);
6421       return;
6422
6423     case M_LI_SS:
6424       if (imm_expr.X_op == O_constant)
6425         {
6426           load_register (&icnt, AT, &imm_expr, 0);
6427           macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6428           break;
6429         }
6430       else
6431         {
6432           assert (offset_expr.X_op == O_symbol
6433                   && strcmp (segment_name (S_GET_SEGMENT
6434                                            (offset_expr.X_add_symbol)),
6435                              ".lit4") == 0
6436                   && offset_expr.X_add_number == 0);
6437           macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6438                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6439           return;
6440         }
6441
6442     case M_LI_D:
6443       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6444          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6445          order 32 bits of the value and the low order 32 bits are either
6446          zero or in OFFSET_EXPR.  */
6447       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6448         {
6449           if (HAVE_64BIT_GPRS)
6450             load_register (&icnt, treg, &imm_expr, 1);
6451           else
6452             {
6453               int hreg, lreg;
6454
6455               if (target_big_endian)
6456                 {
6457                   hreg = treg;
6458                   lreg = treg + 1;
6459                 }
6460               else
6461                 {
6462                   hreg = treg + 1;
6463                   lreg = treg;
6464                 }
6465
6466               if (hreg <= 31)
6467                 load_register (&icnt, hreg, &imm_expr, 0);
6468               if (lreg <= 31)
6469                 {
6470                   if (offset_expr.X_op == O_absent)
6471                     move_register (&icnt, lreg, 0);
6472                   else
6473                     {
6474                       assert (offset_expr.X_op == O_constant);
6475                       load_register (&icnt, lreg, &offset_expr, 0);
6476                     }
6477                 }
6478             }
6479           return;
6480         }
6481
6482       /* We know that sym is in the .rdata section.  First we get the
6483          upper 16 bits of the address.  */
6484       if (mips_pic == NO_PIC)
6485         {
6486           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6487         }
6488       else if (mips_pic == SVR4_PIC)
6489         {
6490           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6491                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6492         }
6493       else if (mips_pic == EMBEDDED_PIC)
6494         {
6495           /* For embedded PIC we pick up the entire address off $gp in
6496              a single instruction.  */
6497           macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6498                        AT, mips_gp_register, BFD_RELOC_GPREL16);
6499           offset_expr.X_op = O_constant;
6500           offset_expr.X_add_number = 0;
6501         }
6502       else
6503         abort ();
6504
6505       /* Now we load the register(s).  */
6506       if (HAVE_64BIT_GPRS)
6507         macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6508                      BFD_RELOC_LO16, AT);
6509       else
6510         {
6511           macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6512                        BFD_RELOC_LO16, AT);
6513           if (treg != RA)
6514             {
6515               /* FIXME: How in the world do we deal with the possible
6516                  overflow here?  */
6517               offset_expr.X_add_number += 4;
6518               macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6519                            treg + 1, BFD_RELOC_LO16, AT);
6520             }
6521         }
6522
6523       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6524          does not become a variant frag.  */
6525       frag_wane (frag_now);
6526       frag_new (0);
6527
6528       break;
6529
6530     case M_LI_DD:
6531       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6532          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6533          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6534          the value and the low order 32 bits are either zero or in
6535          OFFSET_EXPR.  */
6536       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6537         {
6538           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6539           if (HAVE_64BIT_FPRS)
6540             {
6541               assert (HAVE_64BIT_GPRS);
6542               macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
6543             }
6544           else
6545             {
6546               macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
6547               if (offset_expr.X_op == O_absent)
6548                 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
6549               else
6550                 {
6551                   assert (offset_expr.X_op == O_constant);
6552                   load_register (&icnt, AT, &offset_expr, 0);
6553                   macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6554                 }
6555             }
6556           break;
6557         }
6558
6559       assert (offset_expr.X_op == O_symbol
6560               && offset_expr.X_add_number == 0);
6561       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6562       if (strcmp (s, ".lit8") == 0)
6563         {
6564           if (mips_opts.isa != ISA_MIPS1)
6565             {
6566               macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6567                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6568               return;
6569             }
6570           breg = mips_gp_register;
6571           r = BFD_RELOC_MIPS_LITERAL;
6572           goto dob;
6573         }
6574       else
6575         {
6576           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6577           if (mips_pic == SVR4_PIC)
6578             macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6579                          "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6580                          mips_gp_register);
6581           else
6582             {
6583               /* FIXME: This won't work for a 64 bit address.  */
6584               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6585             }
6586
6587           if (mips_opts.isa != ISA_MIPS1)
6588             {
6589               macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6590                            BFD_RELOC_LO16, AT);
6591
6592               /* To avoid confusion in tc_gen_reloc, we must ensure
6593                  that this does not become a variant frag.  */
6594               frag_wane (frag_now);
6595               frag_new (0);
6596
6597               break;
6598             }
6599           breg = AT;
6600           r = BFD_RELOC_LO16;
6601           goto dob;
6602         }
6603
6604     case M_L_DOB:
6605       if (mips_opts.arch == CPU_R4650)
6606         {
6607           as_bad (_("opcode not supported on this processor"));
6608           return;
6609         }
6610       /* Even on a big endian machine $fn comes before $fn+1.  We have
6611          to adjust when loading from memory.  */
6612       r = BFD_RELOC_LO16;
6613     dob:
6614       assert (mips_opts.isa == ISA_MIPS1);
6615       macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6616                    target_big_endian ? treg + 1 : treg, r, breg);
6617       /* FIXME: A possible overflow which I don't know how to deal
6618          with.  */
6619       offset_expr.X_add_number += 4;
6620       macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6621                    target_big_endian ? treg : treg + 1, r, breg);
6622
6623       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6624          does not become a variant frag.  */
6625       frag_wane (frag_now);
6626       frag_new (0);
6627
6628       if (breg != AT)
6629         return;
6630       break;
6631
6632     case M_L_DAB:
6633       /*
6634        * The MIPS assembler seems to check for X_add_number not
6635        * being double aligned and generating:
6636        *        lui     at,%hi(foo+1)
6637        *        addu    at,at,v1
6638        *        addiu   at,at,%lo(foo+1)
6639        *        lwc1    f2,0(at)
6640        *        lwc1    f3,4(at)
6641        * But, the resulting address is the same after relocation so why
6642        * generate the extra instruction?
6643        */
6644       if (mips_opts.arch == CPU_R4650)
6645         {
6646           as_bad (_("opcode not supported on this processor"));
6647           return;
6648         }
6649       /* Itbl support may require additional care here.  */
6650       coproc = 1;
6651       if (mips_opts.isa != ISA_MIPS1)
6652         {
6653           s = "ldc1";
6654           goto ld;
6655         }
6656
6657       s = "lwc1";
6658       fmt = "T,o(b)";
6659       goto ldd_std;
6660
6661     case M_S_DAB:
6662       if (mips_opts.arch == CPU_R4650)
6663         {
6664           as_bad (_("opcode not supported on this processor"));
6665           return;
6666         }
6667
6668       if (mips_opts.isa != ISA_MIPS1)
6669         {
6670           s = "sdc1";
6671           goto st;
6672         }
6673
6674       s = "swc1";
6675       fmt = "T,o(b)";
6676       /* Itbl support may require additional care here.  */
6677       coproc = 1;
6678       goto ldd_std;
6679
6680     case M_LD_AB:
6681       if (HAVE_64BIT_GPRS)
6682         {
6683           s = "ld";
6684           goto ld;
6685         }
6686
6687       s = "lw";
6688       fmt = "t,o(b)";
6689       goto ldd_std;
6690
6691     case M_SD_AB:
6692       if (HAVE_64BIT_GPRS)
6693         {
6694           s = "sd";
6695           goto st;
6696         }
6697
6698       s = "sw";
6699       fmt = "t,o(b)";
6700
6701     ldd_std:
6702       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6703          loads for the case of doing a pair of loads to simulate an 'ld'.
6704          This is not currently done by the compiler, and assembly coders
6705          writing embedded-pic code can cope.  */
6706
6707       if (offset_expr.X_op != O_symbol
6708           && offset_expr.X_op != O_constant)
6709         {
6710           as_bad (_("expression too complex"));
6711           offset_expr.X_op = O_constant;
6712         }
6713
6714       /* Even on a big endian machine $fn comes before $fn+1.  We have
6715          to adjust when loading from memory.  We set coproc if we must
6716          load $fn+1 first.  */
6717       /* Itbl support may require additional care here.  */
6718       if (! target_big_endian)
6719         coproc = 0;
6720
6721       if (mips_pic == NO_PIC
6722           || offset_expr.X_op == O_constant)
6723         {
6724           char *p;
6725
6726           /* If this is a reference to a GP relative symbol, we want
6727                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6728                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6729              If we have a base register, we use this
6730                addu     $at,$breg,$gp
6731                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6732                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6733              If this is not a GP relative symbol, we want
6734                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6735                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6736                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6737              If there is a base register, we add it to $at after the
6738              lui instruction.  If there is a constant, we always use
6739              the last case.  */
6740           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6741               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6742             {
6743               p = NULL;
6744               used_at = 1;
6745             }
6746           else
6747             {
6748               int off;
6749
6750               if (breg == 0)
6751                 {
6752                   frag_grow (28);
6753                   tempreg = mips_gp_register;
6754                   off = 0;
6755                   used_at = 0;
6756                 }
6757               else
6758                 {
6759                   frag_grow (36);
6760                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6761                                AT, breg, mips_gp_register);
6762                   tempreg = AT;
6763                   off = 4;
6764                   used_at = 1;
6765                 }
6766
6767               /* Itbl support may require additional care here.  */
6768               macro_build (NULL, &icnt, &offset_expr, s, fmt,
6769                            coproc ? treg + 1 : treg,
6770                            BFD_RELOC_GPREL16, tempreg);
6771               offset_expr.X_add_number += 4;
6772
6773               /* Set mips_optimize to 2 to avoid inserting an
6774                  undesired nop.  */
6775               hold_mips_optimize = mips_optimize;
6776               mips_optimize = 2;
6777               /* Itbl support may require additional care here.  */
6778               macro_build (NULL, &icnt, &offset_expr, s, fmt,
6779                            coproc ? treg : treg + 1,
6780                            BFD_RELOC_GPREL16, tempreg);
6781               mips_optimize = hold_mips_optimize;
6782
6783               p = frag_var (rs_machine_dependent, 12 + off, 0,
6784                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6785                                           used_at && mips_opts.noat),
6786                             offset_expr.X_add_symbol, 0, NULL);
6787
6788               /* We just generated two relocs.  When tc_gen_reloc
6789                  handles this case, it will skip the first reloc and
6790                  handle the second.  The second reloc already has an
6791                  extra addend of 4, which we added above.  We must
6792                  subtract it out, and then subtract another 4 to make
6793                  the first reloc come out right.  The second reloc
6794                  will come out right because we are going to add 4 to
6795                  offset_expr when we build its instruction below.
6796
6797                  If we have a symbol, then we don't want to include
6798                  the offset, because it will wind up being included
6799                  when we generate the reloc.  */
6800
6801               if (offset_expr.X_op == O_constant)
6802                 offset_expr.X_add_number -= 8;
6803               else
6804                 {
6805                   offset_expr.X_add_number = -4;
6806                   offset_expr.X_op = O_constant;
6807                 }
6808             }
6809           macro_build_lui (p, &icnt, &offset_expr, AT);
6810           if (p != NULL)
6811             p += 4;
6812           if (breg != 0)
6813             {
6814               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6815                            AT, breg, AT);
6816               if (p != NULL)
6817                 p += 4;
6818             }
6819           /* Itbl support may require additional care here.  */
6820           macro_build (p, &icnt, &offset_expr, s, fmt,
6821                        coproc ? treg + 1 : treg,
6822                        BFD_RELOC_LO16, AT);
6823           if (p != NULL)
6824             p += 4;
6825           /* FIXME: How do we handle overflow here?  */
6826           offset_expr.X_add_number += 4;
6827           /* Itbl support may require additional care here.  */
6828           macro_build (p, &icnt, &offset_expr, s, fmt,
6829                        coproc ? treg : treg + 1,
6830                        BFD_RELOC_LO16, AT);
6831         }
6832       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6833         {
6834           int off;
6835
6836           /* If this is a reference to an external symbol, we want
6837                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6838                nop
6839                <op>     $treg,0($at)
6840                <op>     $treg+1,4($at)
6841              Otherwise we want
6842                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6843                nop
6844                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6845                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6846              If there is a base register we add it to $at before the
6847              lwc1 instructions.  If there is a constant we include it
6848              in the lwc1 instructions.  */
6849           used_at = 1;
6850           expr1.X_add_number = offset_expr.X_add_number;
6851           offset_expr.X_add_number = 0;
6852           if (expr1.X_add_number < -0x8000
6853               || expr1.X_add_number >= 0x8000 - 4)
6854             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6855           if (breg == 0)
6856             off = 0;
6857           else
6858             off = 4;
6859           frag_grow (24 + off);
6860           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6861                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6862           macro_build (NULL, &icnt, NULL, "nop", "");
6863           if (breg != 0)
6864             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6865                          AT, breg, AT);
6866           /* Itbl support may require additional care here.  */
6867           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6868                        BFD_RELOC_LO16, AT);
6869           expr1.X_add_number += 4;
6870
6871           /* Set mips_optimize to 2 to avoid inserting an undesired
6872              nop.  */
6873           hold_mips_optimize = mips_optimize;
6874           mips_optimize = 2;
6875           /* Itbl support may require additional care here.  */
6876           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6877                        BFD_RELOC_LO16, AT);
6878           mips_optimize = hold_mips_optimize;
6879
6880           (void) frag_var (rs_machine_dependent, 0, 0,
6881                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6882                            offset_expr.X_add_symbol, 0, NULL);
6883         }
6884       else if (mips_pic == SVR4_PIC)
6885         {
6886           int gpdel, off;
6887           char *p;
6888
6889           /* If this is a reference to an external symbol, we want
6890                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6891                addu     $at,$at,$gp
6892                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6893                nop
6894                <op>     $treg,0($at)
6895                <op>     $treg+1,4($at)
6896              Otherwise we want
6897                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6898                nop
6899                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6900                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6901              If there is a base register we add it to $at before the
6902              lwc1 instructions.  If there is a constant we include it
6903              in the lwc1 instructions.  */
6904           used_at = 1;
6905           expr1.X_add_number = offset_expr.X_add_number;
6906           offset_expr.X_add_number = 0;
6907           if (expr1.X_add_number < -0x8000
6908               || expr1.X_add_number >= 0x8000 - 4)
6909             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6910           if (reg_needs_delay (mips_gp_register))
6911             gpdel = 4;
6912           else
6913             gpdel = 0;
6914           if (breg == 0)
6915             off = 0;
6916           else
6917             off = 4;
6918           frag_grow (56);
6919           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6920                        BFD_RELOC_MIPS_GOT_HI16);
6921           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6922                        AT, AT, mips_gp_register);
6923           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6924                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6925           macro_build (NULL, &icnt, NULL, "nop", "");
6926           if (breg != 0)
6927             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6928                          AT, breg, AT);
6929           /* Itbl support may require additional care here.  */
6930           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6931                        BFD_RELOC_LO16, AT);
6932           expr1.X_add_number += 4;
6933
6934           /* Set mips_optimize to 2 to avoid inserting an undesired
6935              nop.  */
6936           hold_mips_optimize = mips_optimize;
6937           mips_optimize = 2;
6938           /* Itbl support may require additional care here.  */
6939           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6940                        BFD_RELOC_LO16, AT);
6941           mips_optimize = hold_mips_optimize;
6942           expr1.X_add_number -= 4;
6943
6944           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6945                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6946                                       8 + gpdel + off, 1, 0),
6947                         offset_expr.X_add_symbol, 0, NULL);
6948           if (gpdel > 0)
6949             {
6950               macro_build (p, &icnt, NULL, "nop", "");
6951               p += 4;
6952             }
6953           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6954                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6955           p += 4;
6956           macro_build (p, &icnt, NULL, "nop", "");
6957           p += 4;
6958           if (breg != 0)
6959             {
6960               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6961                            AT, breg, AT);
6962               p += 4;
6963             }
6964           /* Itbl support may require additional care here.  */
6965           macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6966                        BFD_RELOC_LO16, AT);
6967           p += 4;
6968           expr1.X_add_number += 4;
6969
6970           /* Set mips_optimize to 2 to avoid inserting an undesired
6971              nop.  */
6972           hold_mips_optimize = mips_optimize;
6973           mips_optimize = 2;
6974           /* Itbl support may require additional care here.  */
6975           macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6976                        BFD_RELOC_LO16, AT);
6977           mips_optimize = hold_mips_optimize;
6978         }
6979       else if (mips_pic == EMBEDDED_PIC)
6980         {
6981           /* If there is no base register, we use
6982                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6983                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6984              If we have a base register, we use
6985                addu     $at,$breg,$gp
6986                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6987                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6988              */
6989           if (breg == 0)
6990             {
6991               tempreg = mips_gp_register;
6992               used_at = 0;
6993             }
6994           else
6995             {
6996               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6997                            AT, breg, mips_gp_register);
6998               tempreg = AT;
6999               used_at = 1;
7000             }
7001
7002           /* Itbl support may require additional care here.  */
7003           macro_build (NULL, &icnt, &offset_expr, s, fmt,
7004                        coproc ? treg + 1 : treg,
7005                        BFD_RELOC_GPREL16, tempreg);
7006           offset_expr.X_add_number += 4;
7007           /* Itbl support may require additional care here.  */
7008           macro_build (NULL, &icnt, &offset_expr, s, fmt,
7009                        coproc ? treg : treg + 1,
7010                        BFD_RELOC_GPREL16, tempreg);
7011         }
7012       else
7013         abort ();
7014
7015       if (! used_at)
7016         return;
7017
7018       break;
7019
7020     case M_LD_OB:
7021       s = "lw";
7022       goto sd_ob;
7023     case M_SD_OB:
7024       s = "sw";
7025     sd_ob:
7026       assert (HAVE_32BIT_ADDRESSES);
7027       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7028                    BFD_RELOC_LO16, breg);
7029       offset_expr.X_add_number += 4;
7030       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7031                    BFD_RELOC_LO16, breg);
7032       return;
7033
7034    /* New code added to support COPZ instructions.
7035       This code builds table entries out of the macros in mip_opcodes.
7036       R4000 uses interlocks to handle coproc delays.
7037       Other chips (like the R3000) require nops to be inserted for delays.
7038
7039       FIXME: Currently, we require that the user handle delays.
7040       In order to fill delay slots for non-interlocked chips,
7041       we must have a way to specify delays based on the coprocessor.
7042       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7043       What are the side-effects of the cop instruction?
7044       What cache support might we have and what are its effects?
7045       Both coprocessor & memory require delays. how long???
7046       What registers are read/set/modified?
7047
7048       If an itbl is provided to interpret cop instructions,
7049       this knowledge can be encoded in the itbl spec.  */
7050
7051     case M_COP0:
7052       s = "c0";
7053       goto copz;
7054     case M_COP1:
7055       s = "c1";
7056       goto copz;
7057     case M_COP2:
7058       s = "c2";
7059       goto copz;
7060     case M_COP3:
7061       s = "c3";
7062     copz:
7063       /* For now we just do C (same as Cz).  The parameter will be
7064          stored in insn_opcode by mips_ip.  */
7065       macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
7066       return;
7067
7068     case M_MOVE:
7069       move_register (&icnt, dreg, sreg);
7070       return;
7071
7072 #ifdef LOSING_COMPILER
7073     default:
7074       /* Try and see if this is a new itbl instruction.
7075          This code builds table entries out of the macros in mip_opcodes.
7076          FIXME: For now we just assemble the expression and pass it's
7077          value along as a 32-bit immediate.
7078          We may want to have the assembler assemble this value,
7079          so that we gain the assembler's knowledge of delay slots,
7080          symbols, etc.
7081          Would it be more efficient to use mask (id) here? */
7082       if (itbl_have_entries
7083           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7084         {
7085           s = ip->insn_mo->name;
7086           s2 = "cop3";
7087           coproc = ITBL_DECODE_PNUM (immed_expr);;
7088           macro_build (NULL, &icnt, &immed_expr, s, "C");
7089           return;
7090         }
7091       macro2 (ip);
7092       return;
7093     }
7094   if (mips_opts.noat)
7095     as_warn (_("Macro used $at after \".set noat\""));
7096 }
7097
7098 static void
7099 macro2 (struct mips_cl_insn *ip)
7100 {
7101   register int treg, sreg, dreg, breg;
7102   int tempreg;
7103   int mask;
7104   int icnt = 0;
7105   int used_at;
7106   expressionS expr1;
7107   const char *s;
7108   const char *s2;
7109   const char *fmt;
7110   int likely = 0;
7111   int dbl = 0;
7112   int coproc = 0;
7113   int lr = 0;
7114   int imm = 0;
7115   int off;
7116   offsetT maxnum;
7117   bfd_reloc_code_real_type r;
7118   char *p;
7119
7120   treg = (ip->insn_opcode >> 16) & 0x1f;
7121   dreg = (ip->insn_opcode >> 11) & 0x1f;
7122   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7123   mask = ip->insn_mo->mask;
7124
7125   expr1.X_op = O_constant;
7126   expr1.X_op_symbol = NULL;
7127   expr1.X_add_symbol = NULL;
7128   expr1.X_add_number = 1;
7129
7130   switch (mask)
7131     {
7132 #endif /* LOSING_COMPILER */
7133
7134     case M_DMUL:
7135       dbl = 1;
7136     case M_MUL:
7137       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7138                    sreg, treg);
7139       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7140       return;
7141
7142     case M_DMUL_I:
7143       dbl = 1;
7144     case M_MUL_I:
7145       /* The MIPS assembler some times generates shifts and adds.  I'm
7146          not trying to be that fancy. GCC should do this for us
7147          anyway.  */
7148       load_register (&icnt, AT, &imm_expr, dbl);
7149       macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7150                    sreg, AT);
7151       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7152       break;
7153
7154     case M_DMULO_I:
7155       dbl = 1;
7156     case M_MULO_I:
7157       imm = 1;
7158       goto do_mulo;
7159
7160     case M_DMULO:
7161       dbl = 1;
7162     case M_MULO:
7163     do_mulo:
7164       mips_emit_delays (TRUE);
7165       ++mips_opts.noreorder;
7166       mips_any_noreorder = 1;
7167       if (imm)
7168         load_register (&icnt, AT, &imm_expr, dbl);
7169       macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7170                    sreg, imm ? AT : treg);
7171       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7172       macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
7173                    dreg, dreg, RA);
7174       macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7175       if (mips_trap)
7176         macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
7177       else
7178         {
7179           expr1.X_add_number = 8;
7180           macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7181           macro_build (NULL, &icnt, NULL, "nop", "", 0);
7182           macro_build (NULL, &icnt, NULL, "break", "c", 6);
7183         }
7184       --mips_opts.noreorder;
7185       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7186       break;
7187
7188     case M_DMULOU_I:
7189       dbl = 1;
7190     case M_MULOU_I:
7191       imm = 1;
7192       goto do_mulou;
7193
7194     case M_DMULOU:
7195       dbl = 1;
7196     case M_MULOU:
7197     do_mulou:
7198       mips_emit_delays (TRUE);
7199       ++mips_opts.noreorder;
7200       mips_any_noreorder = 1;
7201       if (imm)
7202         load_register (&icnt, AT, &imm_expr, dbl);
7203       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7204                    sreg, imm ? AT : treg);
7205       macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7206       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7207       if (mips_trap)
7208         macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
7209       else
7210         {
7211           expr1.X_add_number = 8;
7212           macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7213           macro_build (NULL, &icnt, NULL, "nop", "", 0);
7214           macro_build (NULL, &icnt, NULL, "break", "c", 6);
7215         }
7216       --mips_opts.noreorder;
7217       break;
7218
7219     case M_DROL:
7220       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7221         {
7222           if (dreg == sreg)
7223             {
7224               tempreg = AT;
7225               used_at = 1;
7226             }
7227           else
7228             {
7229               tempreg = dreg;
7230               used_at = 0;
7231             }
7232           macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7233           macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7234                        tempreg);
7235           if (used_at)
7236             break;
7237           return;
7238         }
7239       macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7240       macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7241       macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7242       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7243       break;
7244
7245     case M_ROL:
7246       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7247         {
7248           if (dreg == sreg)
7249             {
7250               tempreg = AT;
7251               used_at = 1;
7252             }
7253           else
7254             {
7255               tempreg = dreg;
7256               used_at = 0;
7257             }
7258           macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7259           macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7260                        tempreg);
7261           if (used_at)
7262             break;
7263           return;
7264         }
7265       macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7266       macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7267       macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7268       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7269       break;
7270
7271     case M_DROL_I:
7272       {
7273         unsigned int rot;
7274         char *l, *r;
7275
7276         if (imm_expr.X_op != O_constant)
7277           as_bad (_("Improper rotate count"));
7278         rot = imm_expr.X_add_number & 0x3f;
7279         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7280           {
7281             rot = (64 - rot) & 0x3f;
7282             if (rot >= 32)
7283               macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7284                            dreg, sreg, rot - 32);
7285             else
7286               macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7287                            dreg, sreg, rot);
7288             return;
7289           }
7290         if (rot == 0)
7291           {
7292             macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7293             return;
7294           }
7295         l = (rot < 0x20) ? "dsll" : "dsll32";
7296         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7297         rot &= 0x1f;
7298         macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7299         macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7300                      (0x20 - rot) & 0x1f);
7301         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7302       }
7303       break;
7304
7305     case M_ROL_I:
7306       {
7307         unsigned int rot;
7308
7309         if (imm_expr.X_op != O_constant)
7310           as_bad (_("Improper rotate count"));
7311         rot = imm_expr.X_add_number & 0x1f;
7312         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7313           {
7314             macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7315                          (32 - rot) & 0x1f);
7316             return;
7317           }
7318         if (rot == 0)
7319           {
7320             macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7321             return;
7322           }
7323         macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7324         macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7325                      (0x20 - rot) & 0x1f);
7326         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7327       }
7328       break;
7329
7330     case M_DROR:
7331       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7332         {
7333           macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
7334           return;
7335         }
7336       macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7337       macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7338       macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7339       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7340       break;
7341
7342     case M_ROR:
7343       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7344         {
7345           macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
7346           return;
7347         }
7348       macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7349       macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7350       macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7351       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7352       break;
7353
7354     case M_DROR_I:
7355       {
7356         unsigned int rot;
7357         char *l, *r;
7358
7359         if (imm_expr.X_op != O_constant)
7360           as_bad (_("Improper rotate count"));
7361         rot = imm_expr.X_add_number & 0x3f;
7362         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7363           {
7364             if (rot >= 32)
7365               macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7366                            dreg, sreg, rot - 32);
7367             else
7368               macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7369                            dreg, sreg, rot);
7370             return;
7371           }
7372         if (rot == 0)
7373           {
7374             macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7375             return;
7376           }
7377         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7378         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7379         rot &= 0x1f;
7380         macro_build (NULL, &icnt, NULL, r, "d,w,<", AT, sreg, rot);
7381         macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7382                      (0x20 - rot) & 0x1f);
7383         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7384       }
7385       break;
7386
7387     case M_ROR_I:
7388       {
7389         unsigned int rot;
7390
7391         if (imm_expr.X_op != O_constant)
7392           as_bad (_("Improper rotate count"));
7393         rot = imm_expr.X_add_number & 0x1f;
7394         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7395           {
7396             macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
7397             return;
7398           }
7399         if (rot == 0)
7400           {
7401             macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7402             return;
7403           }
7404         macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7405         macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7406                      (0x20 - rot) & 0x1f);
7407         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7408       }
7409       break;
7410
7411     case M_S_DOB:
7412       if (mips_opts.arch == CPU_R4650)
7413         {
7414           as_bad (_("opcode not supported on this processor"));
7415           return;
7416         }
7417       assert (mips_opts.isa == ISA_MIPS1);
7418       /* Even on a big endian machine $fn comes before $fn+1.  We have
7419          to adjust when storing to memory.  */
7420       macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7421                    target_big_endian ? treg + 1 : treg,
7422                    BFD_RELOC_LO16, breg);
7423       offset_expr.X_add_number += 4;
7424       macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7425                    target_big_endian ? treg : treg + 1,
7426                    BFD_RELOC_LO16, breg);
7427       return;
7428
7429     case M_SEQ:
7430       if (sreg == 0)
7431         macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7432                      BFD_RELOC_LO16);
7433       else if (treg == 0)
7434         macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7435                      BFD_RELOC_LO16);
7436       else
7437         {
7438           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7439           macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7440                        BFD_RELOC_LO16);
7441         }
7442       return;
7443
7444     case M_SEQ_I:
7445       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7446         {
7447           macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7448                        BFD_RELOC_LO16);
7449           return;
7450         }
7451       if (sreg == 0)
7452         {
7453           as_warn (_("Instruction %s: result is always false"),
7454                    ip->insn_mo->name);
7455           move_register (&icnt, dreg, 0);
7456           return;
7457         }
7458       if (imm_expr.X_op == O_constant
7459           && imm_expr.X_add_number >= 0
7460           && imm_expr.X_add_number < 0x10000)
7461         {
7462           macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7463                        BFD_RELOC_LO16);
7464           used_at = 0;
7465         }
7466       else if (imm_expr.X_op == O_constant
7467                && imm_expr.X_add_number > -0x8000
7468                && imm_expr.X_add_number < 0)
7469         {
7470           imm_expr.X_add_number = -imm_expr.X_add_number;
7471           macro_build (NULL, &icnt, &imm_expr,
7472                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7473                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7474           used_at = 0;
7475         }
7476       else
7477         {
7478           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7479           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7480           used_at = 1;
7481         }
7482       macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7483                    BFD_RELOC_LO16);
7484       if (used_at)
7485         break;
7486       return;
7487
7488     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7489       s = "slt";
7490       goto sge;
7491     case M_SGEU:
7492       s = "sltu";
7493     sge:
7494       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7495       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7496                    BFD_RELOC_LO16);
7497       return;
7498
7499     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7500     case M_SGEU_I:
7501       if (imm_expr.X_op == O_constant
7502           && imm_expr.X_add_number >= -0x8000
7503           && imm_expr.X_add_number < 0x8000)
7504         {
7505           macro_build (NULL, &icnt, &imm_expr,
7506                        mask == M_SGE_I ? "slti" : "sltiu",
7507                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7508           used_at = 0;
7509         }
7510       else
7511         {
7512           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7513           macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7514                        "d,v,t", dreg, sreg, AT);
7515           used_at = 1;
7516         }
7517       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7518                    BFD_RELOC_LO16);
7519       if (used_at)
7520         break;
7521       return;
7522
7523     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7524       s = "slt";
7525       goto sgt;
7526     case M_SGTU:
7527       s = "sltu";
7528     sgt:
7529       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7530       return;
7531
7532     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7533       s = "slt";
7534       goto sgti;
7535     case M_SGTU_I:
7536       s = "sltu";
7537     sgti:
7538       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7539       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7540       break;
7541
7542     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7543       s = "slt";
7544       goto sle;
7545     case M_SLEU:
7546       s = "sltu";
7547     sle:
7548       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7549       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7550                    BFD_RELOC_LO16);
7551       return;
7552
7553     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7554       s = "slt";
7555       goto slei;
7556     case M_SLEU_I:
7557       s = "sltu";
7558     slei:
7559       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7560       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7561       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7562                    BFD_RELOC_LO16);
7563       break;
7564
7565     case M_SLT_I:
7566       if (imm_expr.X_op == O_constant
7567           && imm_expr.X_add_number >= -0x8000
7568           && imm_expr.X_add_number < 0x8000)
7569         {
7570           macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7571                        BFD_RELOC_LO16);
7572           return;
7573         }
7574       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7575       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
7576       break;
7577
7578     case M_SLTU_I:
7579       if (imm_expr.X_op == O_constant
7580           && imm_expr.X_add_number >= -0x8000
7581           && imm_expr.X_add_number < 0x8000)
7582         {
7583           macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7584                        BFD_RELOC_LO16);
7585           return;
7586         }
7587       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7588       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
7589       break;
7590
7591     case M_SNE:
7592       if (sreg == 0)
7593         macro_build (NULL, &icnt, NULL, "sltu","d,v,t", dreg, 0, treg);
7594       else if (treg == 0)
7595         macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7596       else
7597         {
7598           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7599           macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7600         }
7601       return;
7602
7603     case M_SNE_I:
7604       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7605         {
7606           macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7607           return;
7608         }
7609       if (sreg == 0)
7610         {
7611           as_warn (_("Instruction %s: result is always true"),
7612                    ip->insn_mo->name);
7613           macro_build (NULL, &icnt, &expr1,
7614                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7615                        "t,r,j", dreg, 0, BFD_RELOC_LO16);
7616           return;
7617         }
7618       if (imm_expr.X_op == O_constant
7619           && imm_expr.X_add_number >= 0
7620           && imm_expr.X_add_number < 0x10000)
7621         {
7622           macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7623                        BFD_RELOC_LO16);
7624           used_at = 0;
7625         }
7626       else if (imm_expr.X_op == O_constant
7627                && imm_expr.X_add_number > -0x8000
7628                && imm_expr.X_add_number < 0)
7629         {
7630           imm_expr.X_add_number = -imm_expr.X_add_number;
7631           macro_build (NULL, &icnt, &imm_expr,
7632                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7633                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7634           used_at = 0;
7635         }
7636       else
7637         {
7638           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7639           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7640           used_at = 1;
7641         }
7642       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7643       if (used_at)
7644         break;
7645       return;
7646
7647     case M_DSUB_I:
7648       dbl = 1;
7649     case M_SUB_I:
7650       if (imm_expr.X_op == O_constant
7651           && imm_expr.X_add_number > -0x8000
7652           && imm_expr.X_add_number <= 0x8000)
7653         {
7654           imm_expr.X_add_number = -imm_expr.X_add_number;
7655           macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7656                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7657           return;
7658         }
7659       load_register (&icnt, AT, &imm_expr, dbl);
7660       macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7661                    dreg, sreg, AT);
7662       break;
7663
7664     case M_DSUBU_I:
7665       dbl = 1;
7666     case M_SUBU_I:
7667       if (imm_expr.X_op == O_constant
7668           && imm_expr.X_add_number > -0x8000
7669           && imm_expr.X_add_number <= 0x8000)
7670         {
7671           imm_expr.X_add_number = -imm_expr.X_add_number;
7672           macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7673                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7674           return;
7675         }
7676       load_register (&icnt, AT, &imm_expr, dbl);
7677       macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7678                    dreg, sreg, AT);
7679       break;
7680
7681     case M_TEQ_I:
7682       s = "teq";
7683       goto trap;
7684     case M_TGE_I:
7685       s = "tge";
7686       goto trap;
7687     case M_TGEU_I:
7688       s = "tgeu";
7689       goto trap;
7690     case M_TLT_I:
7691       s = "tlt";
7692       goto trap;
7693     case M_TLTU_I:
7694       s = "tltu";
7695       goto trap;
7696     case M_TNE_I:
7697       s = "tne";
7698     trap:
7699       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7700       macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
7701       break;
7702
7703     case M_TRUNCWS:
7704     case M_TRUNCWD:
7705       assert (mips_opts.isa == ISA_MIPS1);
7706       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7707       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7708
7709       /*
7710        * Is the double cfc1 instruction a bug in the mips assembler;
7711        * or is there a reason for it?
7712        */
7713       mips_emit_delays (TRUE);
7714       ++mips_opts.noreorder;
7715       mips_any_noreorder = 1;
7716       macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7717       macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7718       macro_build (NULL, &icnt, NULL, "nop", "");
7719       expr1.X_add_number = 3;
7720       macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7721                    BFD_RELOC_LO16);
7722       expr1.X_add_number = 2;
7723       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7724                    BFD_RELOC_LO16);
7725       macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7726       macro_build (NULL, &icnt, NULL, "nop", "");
7727       macro_build (NULL, &icnt, NULL,
7728                    mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7729                    "D,S", dreg, sreg);
7730       macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7731       macro_build (NULL, &icnt, NULL, "nop", "");
7732       --mips_opts.noreorder;
7733       break;
7734
7735     case M_ULH:
7736       s = "lb";
7737       goto ulh;
7738     case M_ULHU:
7739       s = "lbu";
7740     ulh:
7741       if (offset_expr.X_add_number >= 0x7fff)
7742         as_bad (_("operand overflow"));
7743       if (! target_big_endian)
7744         ++offset_expr.X_add_number;
7745       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7746                    BFD_RELOC_LO16, breg);
7747       if (! target_big_endian)
7748         --offset_expr.X_add_number;
7749       else
7750         ++offset_expr.X_add_number;
7751       macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7752                    BFD_RELOC_LO16, breg);
7753       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7754       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7755       break;
7756
7757     case M_ULD:
7758       s = "ldl";
7759       s2 = "ldr";
7760       off = 7;
7761       goto ulw;
7762     case M_ULW:
7763       s = "lwl";
7764       s2 = "lwr";
7765       off = 3;
7766     ulw:
7767       if (offset_expr.X_add_number >= 0x8000 - off)
7768         as_bad (_("operand overflow"));
7769       if (treg != breg)
7770         tempreg = treg;
7771       else
7772         tempreg = AT;
7773       if (! target_big_endian)
7774         offset_expr.X_add_number += off;
7775       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7776                    BFD_RELOC_LO16, breg);
7777       if (! target_big_endian)
7778         offset_expr.X_add_number -= off;
7779       else
7780         offset_expr.X_add_number += off;
7781       macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7782                    BFD_RELOC_LO16, breg);
7783
7784       /* If necessary, move the result in tempreg the final destination.  */
7785       if (treg == tempreg)
7786         return;
7787       /* Protect second load's delay slot.  */
7788       if (!gpr_interlocks)
7789         macro_build (NULL, &icnt, NULL, "nop", "");
7790       move_register (&icnt, treg, tempreg);
7791       break;
7792
7793     case M_ULD_A:
7794       s = "ldl";
7795       s2 = "ldr";
7796       off = 7;
7797       goto ulwa;
7798     case M_ULW_A:
7799       s = "lwl";
7800       s2 = "lwr";
7801       off = 3;
7802     ulwa:
7803       used_at = 1;
7804       load_address (&icnt, AT, &offset_expr, &used_at);
7805       if (breg != 0)
7806         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7807                      AT, AT, breg);
7808       if (! target_big_endian)
7809         expr1.X_add_number = off;
7810       else
7811         expr1.X_add_number = 0;
7812       macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7813                    BFD_RELOC_LO16, AT);
7814       if (! target_big_endian)
7815         expr1.X_add_number = 0;
7816       else
7817         expr1.X_add_number = off;
7818       macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7819                    BFD_RELOC_LO16, AT);
7820       break;
7821
7822     case M_ULH_A:
7823     case M_ULHU_A:
7824       used_at = 1;
7825       load_address (&icnt, AT, &offset_expr, &used_at);
7826       if (breg != 0)
7827         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7828                      AT, AT, breg);
7829       if (target_big_endian)
7830         expr1.X_add_number = 0;
7831       macro_build (NULL, &icnt, &expr1,
7832                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7833                    treg, BFD_RELOC_LO16, AT);
7834       if (target_big_endian)
7835         expr1.X_add_number = 1;
7836       else
7837         expr1.X_add_number = 0;
7838       macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7839                    AT, BFD_RELOC_LO16, AT);
7840       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7841       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7842       break;
7843
7844     case M_USH:
7845       if (offset_expr.X_add_number >= 0x7fff)
7846         as_bad (_("operand overflow"));
7847       if (target_big_endian)
7848         ++offset_expr.X_add_number;
7849       macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7850                    BFD_RELOC_LO16, breg);
7851       macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
7852       if (target_big_endian)
7853         --offset_expr.X_add_number;
7854       else
7855         ++offset_expr.X_add_number;
7856       macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7857                    BFD_RELOC_LO16, breg);
7858       break;
7859
7860     case M_USD:
7861       s = "sdl";
7862       s2 = "sdr";
7863       off = 7;
7864       goto usw;
7865     case M_USW:
7866       s = "swl";
7867       s2 = "swr";
7868       off = 3;
7869     usw:
7870       if (offset_expr.X_add_number >= 0x8000 - off)
7871         as_bad (_("operand overflow"));
7872       if (! target_big_endian)
7873         offset_expr.X_add_number += off;
7874       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7875                    BFD_RELOC_LO16, breg);
7876       if (! target_big_endian)
7877         offset_expr.X_add_number -= off;
7878       else
7879         offset_expr.X_add_number += off;
7880       macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7881                    BFD_RELOC_LO16, breg);
7882       return;
7883
7884     case M_USD_A:
7885       s = "sdl";
7886       s2 = "sdr";
7887       off = 7;
7888       goto uswa;
7889     case M_USW_A:
7890       s = "swl";
7891       s2 = "swr";
7892       off = 3;
7893     uswa:
7894       used_at = 1;
7895       load_address (&icnt, AT, &offset_expr, &used_at);
7896       if (breg != 0)
7897         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7898                      AT, AT, breg);
7899       if (! target_big_endian)
7900         expr1.X_add_number = off;
7901       else
7902         expr1.X_add_number = 0;
7903       macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7904                    BFD_RELOC_LO16, AT);
7905       if (! target_big_endian)
7906         expr1.X_add_number = 0;
7907       else
7908         expr1.X_add_number = off;
7909       macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7910                    BFD_RELOC_LO16, AT);
7911       break;
7912
7913     case M_USH_A:
7914       used_at = 1;
7915       load_address (&icnt, AT, &offset_expr, &used_at);
7916       if (breg != 0)
7917         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7918                      AT, AT, breg);
7919       if (! target_big_endian)
7920         expr1.X_add_number = 0;
7921       macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7922                    BFD_RELOC_LO16, AT);
7923       macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
7924       if (! target_big_endian)
7925         expr1.X_add_number = 1;
7926       else
7927         expr1.X_add_number = 0;
7928       macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7929                    BFD_RELOC_LO16, AT);
7930       if (! target_big_endian)
7931         expr1.X_add_number = 0;
7932       else
7933         expr1.X_add_number = 1;
7934       macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7935                    BFD_RELOC_LO16, AT);
7936       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7937       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7938       break;
7939
7940     default:
7941       /* FIXME: Check if this is one of the itbl macros, since they
7942          are added dynamically.  */
7943       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7944       break;
7945     }
7946   if (mips_opts.noat)
7947     as_warn (_("Macro used $at after \".set noat\""));
7948 }
7949
7950 /* Implement macros in mips16 mode.  */
7951
7952 static void
7953 mips16_macro (struct mips_cl_insn *ip)
7954 {
7955   int mask;
7956   int xreg, yreg, zreg, tmp;
7957   int icnt;
7958   expressionS expr1;
7959   int dbl;
7960   const char *s, *s2, *s3;
7961
7962   mask = ip->insn_mo->mask;
7963
7964   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7965   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7966   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7967
7968   icnt = 0;
7969
7970   expr1.X_op = O_constant;
7971   expr1.X_op_symbol = NULL;
7972   expr1.X_add_symbol = NULL;
7973   expr1.X_add_number = 1;
7974
7975   dbl = 0;
7976
7977   switch (mask)
7978     {
7979     default:
7980       internalError ();
7981
7982     case M_DDIV_3:
7983       dbl = 1;
7984     case M_DIV_3:
7985       s = "mflo";
7986       goto do_div3;
7987     case M_DREM_3:
7988       dbl = 1;
7989     case M_REM_3:
7990       s = "mfhi";
7991     do_div3:
7992       mips_emit_delays (TRUE);
7993       ++mips_opts.noreorder;
7994       mips_any_noreorder = 1;
7995       macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7996                    xreg, yreg);
7997       expr1.X_add_number = 2;
7998       macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7999       macro_build (NULL, &icnt, NULL, "break", "6", 7);
8000
8001       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8002          since that causes an overflow.  We should do that as well,
8003          but I don't see how to do the comparisons without a temporary
8004          register.  */
8005       --mips_opts.noreorder;
8006       macro_build (NULL, &icnt, NULL, s, "x", zreg);
8007       break;
8008
8009     case M_DIVU_3:
8010       s = "divu";
8011       s2 = "mflo";
8012       goto do_divu3;
8013     case M_REMU_3:
8014       s = "divu";
8015       s2 = "mfhi";
8016       goto do_divu3;
8017     case M_DDIVU_3:
8018       s = "ddivu";
8019       s2 = "mflo";
8020       goto do_divu3;
8021     case M_DREMU_3:
8022       s = "ddivu";
8023       s2 = "mfhi";
8024     do_divu3:
8025       mips_emit_delays (TRUE);
8026       ++mips_opts.noreorder;
8027       mips_any_noreorder = 1;
8028       macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
8029       expr1.X_add_number = 2;
8030       macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8031       macro_build (NULL, &icnt, NULL, "break", "6", 7);
8032       --mips_opts.noreorder;
8033       macro_build (NULL, &icnt, NULL, s2, "x", zreg);
8034       break;
8035
8036     case M_DMUL:
8037       dbl = 1;
8038     case M_MUL:
8039       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
8040                    xreg, yreg);
8041       macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
8042       return;
8043
8044     case M_DSUBU_I:
8045       dbl = 1;
8046       goto do_subu;
8047     case M_SUBU_I:
8048     do_subu:
8049       if (imm_expr.X_op != O_constant)
8050         as_bad (_("Unsupported large constant"));
8051       imm_expr.X_add_number = -imm_expr.X_add_number;
8052       macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
8053                    yreg, xreg);
8054       break;
8055
8056     case M_SUBU_I_2:
8057       if (imm_expr.X_op != O_constant)
8058         as_bad (_("Unsupported large constant"));
8059       imm_expr.X_add_number = -imm_expr.X_add_number;
8060       macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
8061       break;
8062
8063     case M_DSUBU_I_2:
8064       if (imm_expr.X_op != O_constant)
8065         as_bad (_("Unsupported large constant"));
8066       imm_expr.X_add_number = -imm_expr.X_add_number;
8067       macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
8068       break;
8069
8070     case M_BEQ:
8071       s = "cmp";
8072       s2 = "bteqz";
8073       goto do_branch;
8074     case M_BNE:
8075       s = "cmp";
8076       s2 = "btnez";
8077       goto do_branch;
8078     case M_BLT:
8079       s = "slt";
8080       s2 = "btnez";
8081       goto do_branch;
8082     case M_BLTU:
8083       s = "sltu";
8084       s2 = "btnez";
8085       goto do_branch;
8086     case M_BLE:
8087       s = "slt";
8088       s2 = "bteqz";
8089       goto do_reverse_branch;
8090     case M_BLEU:
8091       s = "sltu";
8092       s2 = "bteqz";
8093       goto do_reverse_branch;
8094     case M_BGE:
8095       s = "slt";
8096       s2 = "bteqz";
8097       goto do_branch;
8098     case M_BGEU:
8099       s = "sltu";
8100       s2 = "bteqz";
8101       goto do_branch;
8102     case M_BGT:
8103       s = "slt";
8104       s2 = "btnez";
8105       goto do_reverse_branch;
8106     case M_BGTU:
8107       s = "sltu";
8108       s2 = "btnez";
8109
8110     do_reverse_branch:
8111       tmp = xreg;
8112       xreg = yreg;
8113       yreg = tmp;
8114
8115     do_branch:
8116       macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
8117       macro_build (NULL, &icnt, &offset_expr, s2, "p");
8118       break;
8119
8120     case M_BEQ_I:
8121       s = "cmpi";
8122       s2 = "bteqz";
8123       s3 = "x,U";
8124       goto do_branch_i;
8125     case M_BNE_I:
8126       s = "cmpi";
8127       s2 = "btnez";
8128       s3 = "x,U";
8129       goto do_branch_i;
8130     case M_BLT_I:
8131       s = "slti";
8132       s2 = "btnez";
8133       s3 = "x,8";
8134       goto do_branch_i;
8135     case M_BLTU_I:
8136       s = "sltiu";
8137       s2 = "btnez";
8138       s3 = "x,8";
8139       goto do_branch_i;
8140     case M_BLE_I:
8141       s = "slti";
8142       s2 = "btnez";
8143       s3 = "x,8";
8144       goto do_addone_branch_i;
8145     case M_BLEU_I:
8146       s = "sltiu";
8147       s2 = "btnez";
8148       s3 = "x,8";
8149       goto do_addone_branch_i;
8150     case M_BGE_I:
8151       s = "slti";
8152       s2 = "bteqz";
8153       s3 = "x,8";
8154       goto do_branch_i;
8155     case M_BGEU_I:
8156       s = "sltiu";
8157       s2 = "bteqz";
8158       s3 = "x,8";
8159       goto do_branch_i;
8160     case M_BGT_I:
8161       s = "slti";
8162       s2 = "bteqz";
8163       s3 = "x,8";
8164       goto do_addone_branch_i;
8165     case M_BGTU_I:
8166       s = "sltiu";
8167       s2 = "bteqz";
8168       s3 = "x,8";
8169
8170     do_addone_branch_i:
8171       if (imm_expr.X_op != O_constant)
8172         as_bad (_("Unsupported large constant"));
8173       ++imm_expr.X_add_number;
8174
8175     do_branch_i:
8176       macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8177       macro_build (NULL, &icnt, &offset_expr, s2, "p");
8178       break;
8179
8180     case M_ABS:
8181       expr1.X_add_number = 0;
8182       macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
8183       if (xreg != yreg)
8184         move_register (&icnt, xreg, yreg);
8185       expr1.X_add_number = 2;
8186       macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8187       macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
8188     }
8189 }
8190
8191 /* For consistency checking, verify that all bits are specified either
8192    by the match/mask part of the instruction definition, or by the
8193    operand list.  */
8194 static int
8195 validate_mips_insn (const struct mips_opcode *opc)
8196 {
8197   const char *p = opc->args;
8198   char c;
8199   unsigned long used_bits = opc->mask;
8200
8201   if ((used_bits & opc->match) != opc->match)
8202     {
8203       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8204               opc->name, opc->args);
8205       return 0;
8206     }
8207 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8208   while (*p)
8209     switch (c = *p++)
8210       {
8211       case ',': break;
8212       case '(': break;
8213       case ')': break;
8214       case '+':
8215         switch (c = *p++)
8216           {
8217           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8218           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8219           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8220           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8221                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8222           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8223           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8224           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8225           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8226           case 'I': break;
8227           default:
8228             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8229                     c, opc->name, opc->args);
8230             return 0;
8231           }
8232         break;
8233       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8234       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8235       case 'A': break;
8236       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8237       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8238       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8239       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8240       case 'F': break;
8241       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8242       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8243       case 'I': break;
8244       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8245       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8246       case 'L': break;
8247       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8248       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8249       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8250       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8251                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8252       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8253       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8254       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8255       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8256       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8257       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8258       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8259       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8260       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8261       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8262       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8263       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8264       case 'f': break;
8265       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8266       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8267       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8268       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8269       case 'l': break;
8270       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8271       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8272       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8273       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8274       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8275       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8276       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8277       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8278       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8279       case 'x': break;
8280       case 'z': break;
8281       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8282       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8283                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8284       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8285       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8286       case '[': break;
8287       case ']': break;
8288       default:
8289         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8290                 c, opc->name, opc->args);
8291         return 0;
8292       }
8293 #undef USE_BITS
8294   if (used_bits != 0xffffffff)
8295     {
8296       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8297               ~used_bits & 0xffffffff, opc->name, opc->args);
8298       return 0;
8299     }
8300   return 1;
8301 }
8302
8303 /* This routine assembles an instruction into its binary format.  As a
8304    side effect, it sets one of the global variables imm_reloc or
8305    offset_reloc to the type of relocation to do if one of the operands
8306    is an address expression.  */
8307
8308 static void
8309 mips_ip (char *str, struct mips_cl_insn *ip)
8310 {
8311   char *s;
8312   const char *args;
8313   char c = 0;
8314   struct mips_opcode *insn;
8315   char *argsStart;
8316   unsigned int regno;
8317   unsigned int lastregno = 0;
8318   unsigned int lastpos = 0;
8319   unsigned int limlo, limhi;
8320   char *s_reset;
8321   char save_c = 0;
8322
8323   insn_error = NULL;
8324
8325   /* If the instruction contains a '.', we first try to match an instruction
8326      including the '.'.  Then we try again without the '.'.  */
8327   insn = NULL;
8328   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8329     continue;
8330
8331   /* If we stopped on whitespace, then replace the whitespace with null for
8332      the call to hash_find.  Save the character we replaced just in case we
8333      have to re-parse the instruction.  */
8334   if (ISSPACE (*s))
8335     {
8336       save_c = *s;
8337       *s++ = '\0';
8338     }
8339
8340   insn = (struct mips_opcode *) hash_find (op_hash, str);
8341
8342   /* If we didn't find the instruction in the opcode table, try again, but
8343      this time with just the instruction up to, but not including the
8344      first '.'.  */
8345   if (insn == NULL)
8346     {
8347       /* Restore the character we overwrite above (if any).  */
8348       if (save_c)
8349         *(--s) = save_c;
8350
8351       /* Scan up to the first '.' or whitespace.  */
8352       for (s = str;
8353            *s != '\0' && *s != '.' && !ISSPACE (*s);
8354            ++s)
8355         continue;
8356
8357       /* If we did not find a '.', then we can quit now.  */
8358       if (*s != '.')
8359         {
8360           insn_error = "unrecognized opcode";
8361           return;
8362         }
8363
8364       /* Lookup the instruction in the hash table.  */
8365       *s++ = '\0';
8366       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8367         {
8368           insn_error = "unrecognized opcode";
8369           return;
8370         }
8371     }
8372
8373   argsStart = s;
8374   for (;;)
8375     {
8376       bfd_boolean ok;
8377
8378       assert (strcmp (insn->name, str) == 0);
8379
8380       if (OPCODE_IS_MEMBER (insn,
8381                             (mips_opts.isa
8382                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8383                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8384                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8385                             mips_opts.arch))
8386         ok = TRUE;
8387       else
8388         ok = FALSE;
8389
8390       if (insn->pinfo != INSN_MACRO)
8391         {
8392           if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8393             ok = FALSE;
8394         }
8395
8396       if (! ok)
8397         {
8398           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8399               && strcmp (insn->name, insn[1].name) == 0)
8400             {
8401               ++insn;
8402               continue;
8403             }
8404           else
8405             {
8406               if (!insn_error)
8407                 {
8408                   static char buf[100];
8409                   sprintf (buf,
8410                            _("opcode not supported on this processor: %s (%s)"),
8411                            mips_cpu_info_from_arch (mips_opts.arch)->name,
8412                            mips_cpu_info_from_isa (mips_opts.isa)->name);
8413                   insn_error = buf;
8414                 }
8415               if (save_c)
8416                 *(--s) = save_c;
8417               return;
8418             }
8419         }
8420
8421       ip->insn_mo = insn;
8422       ip->insn_opcode = insn->match;
8423       insn_error = NULL;
8424       for (args = insn->args;; ++args)
8425         {
8426           int is_mdmx;
8427
8428           s += strspn (s, " \t");
8429           is_mdmx = 0;
8430           switch (*args)
8431             {
8432             case '\0':          /* end of args */
8433               if (*s == '\0')
8434                 return;
8435               break;
8436
8437             case ',':
8438               if (*s++ == *args)
8439                 continue;
8440               s--;
8441               switch (*++args)
8442                 {
8443                 case 'r':
8444                 case 'v':
8445                   ip->insn_opcode |= lastregno << OP_SH_RS;
8446                   continue;
8447
8448                 case 'w':
8449                   ip->insn_opcode |= lastregno << OP_SH_RT;
8450                   continue;
8451
8452                 case 'W':
8453                   ip->insn_opcode |= lastregno << OP_SH_FT;
8454                   continue;
8455
8456                 case 'V':
8457                   ip->insn_opcode |= lastregno << OP_SH_FS;
8458                   continue;
8459                 }
8460               break;
8461
8462             case '(':
8463               /* Handle optional base register.
8464                  Either the base register is omitted or
8465                  we must have a left paren.  */
8466               /* This is dependent on the next operand specifier
8467                  is a base register specification.  */
8468               assert (args[1] == 'b' || args[1] == '5'
8469                       || args[1] == '-' || args[1] == '4');
8470               if (*s == '\0')
8471                 return;
8472
8473             case ')':           /* these must match exactly */
8474             case '[':
8475             case ']':
8476               if (*s++ == *args)
8477                 continue;
8478               break;
8479
8480             case '+':           /* Opcode extension character.  */
8481               switch (*++args)
8482                 {
8483                 case 'A':               /* ins/ext position, becomes LSB.  */
8484                   limlo = 0;
8485                   limhi = 31;
8486                   goto do_lsb;
8487                 case 'E':
8488                   limlo = 32;
8489                   limhi = 63;
8490                   goto do_lsb;
8491 do_lsb:
8492                   my_getExpression (&imm_expr, s);
8493                   check_absolute_expr (ip, &imm_expr);
8494                   if ((unsigned long) imm_expr.X_add_number < limlo
8495                       || (unsigned long) imm_expr.X_add_number > limhi)
8496                     {
8497                       as_bad (_("Improper position (%lu)"),
8498                               (unsigned long) imm_expr.X_add_number);
8499                       imm_expr.X_add_number = limlo;
8500                     }
8501                   lastpos = imm_expr.X_add_number;
8502                   ip->insn_opcode |= (imm_expr.X_add_number
8503                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8504                   imm_expr.X_op = O_absent;
8505                   s = expr_end;
8506                   continue;
8507
8508                 case 'B':               /* ins size, becomes MSB.  */
8509                   limlo = 1;
8510                   limhi = 32;
8511                   goto do_msb;
8512                 case 'F':
8513                   limlo = 33;
8514                   limhi = 64;
8515                   goto do_msb;
8516 do_msb:
8517                   my_getExpression (&imm_expr, s);
8518                   check_absolute_expr (ip, &imm_expr);
8519                   /* Check for negative input so that small negative numbers
8520                      will not succeed incorrectly.  The checks against
8521                      (pos+size) transitively check "size" itself,
8522                      assuming that "pos" is reasonable.  */
8523                   if ((long) imm_expr.X_add_number < 0
8524                       || ((unsigned long) imm_expr.X_add_number
8525                           + lastpos) < limlo
8526                       || ((unsigned long) imm_expr.X_add_number
8527                           + lastpos) > limhi)
8528                     {
8529                       as_bad (_("Improper insert size (%lu, position %lu)"),
8530                               (unsigned long) imm_expr.X_add_number,
8531                               (unsigned long) lastpos);
8532                       imm_expr.X_add_number = limlo - lastpos;
8533                     }
8534                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8535                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8536                   imm_expr.X_op = O_absent;
8537                   s = expr_end;
8538                   continue;
8539
8540                 case 'C':               /* ext size, becomes MSBD.  */
8541                   limlo = 1;
8542                   limhi = 32;
8543                   goto do_msbd;
8544                 case 'G':
8545                   limlo = 33;
8546                   limhi = 64;
8547                   goto do_msbd;
8548                 case 'H':
8549                   limlo = 33;
8550                   limhi = 64;
8551                   goto do_msbd;
8552 do_msbd:
8553                   my_getExpression (&imm_expr, s);
8554                   check_absolute_expr (ip, &imm_expr);
8555                   /* Check for negative input so that small negative numbers
8556                      will not succeed incorrectly.  The checks against
8557                      (pos+size) transitively check "size" itself,
8558                      assuming that "pos" is reasonable.  */
8559                   if ((long) imm_expr.X_add_number < 0
8560                       || ((unsigned long) imm_expr.X_add_number
8561                           + lastpos) < limlo
8562                       || ((unsigned long) imm_expr.X_add_number
8563                           + lastpos) > limhi)
8564                     {
8565                       as_bad (_("Improper extract size (%lu, position %lu)"),
8566                               (unsigned long) imm_expr.X_add_number,
8567                               (unsigned long) lastpos);
8568                       imm_expr.X_add_number = limlo - lastpos;
8569                     }
8570                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8571                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8572                   imm_expr.X_op = O_absent;
8573                   s = expr_end;
8574                   continue;
8575
8576                 case 'D':
8577                   /* +D is for disassembly only; never match.  */
8578                   break;
8579
8580                 case 'I':
8581                   /* "+I" is like "I", except that imm2_expr is used.  */
8582                   my_getExpression (&imm2_expr, s);
8583                   if (imm2_expr.X_op != O_big
8584                       && imm2_expr.X_op != O_constant)
8585                   insn_error = _("absolute expression required");
8586                   normalize_constant_expr (&imm2_expr);
8587                   s = expr_end;
8588                   continue;
8589
8590                 default:
8591                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8592                     *args, insn->name, insn->args);
8593                   /* Further processing is fruitless.  */
8594                   return;
8595                 }
8596               break;
8597
8598             case '<':           /* must be at least one digit */
8599               /*
8600                * According to the manual, if the shift amount is greater
8601                * than 31 or less than 0, then the shift amount should be
8602                * mod 32.  In reality the mips assembler issues an error.
8603                * We issue a warning and mask out all but the low 5 bits.
8604                */
8605               my_getExpression (&imm_expr, s);
8606               check_absolute_expr (ip, &imm_expr);
8607               if ((unsigned long) imm_expr.X_add_number > 31)
8608                 {
8609                   as_warn (_("Improper shift amount (%lu)"),
8610                            (unsigned long) imm_expr.X_add_number);
8611                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8612                 }
8613               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8614               imm_expr.X_op = O_absent;
8615               s = expr_end;
8616               continue;
8617
8618             case '>':           /* shift amount minus 32 */
8619               my_getExpression (&imm_expr, s);
8620               check_absolute_expr (ip, &imm_expr);
8621               if ((unsigned long) imm_expr.X_add_number < 32
8622                   || (unsigned long) imm_expr.X_add_number > 63)
8623                 break;
8624               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8625               imm_expr.X_op = O_absent;
8626               s = expr_end;
8627               continue;
8628
8629             case 'k':           /* cache code */
8630             case 'h':           /* prefx code */
8631               my_getExpression (&imm_expr, s);
8632               check_absolute_expr (ip, &imm_expr);
8633               if ((unsigned long) imm_expr.X_add_number > 31)
8634                 {
8635                   as_warn (_("Invalid value for `%s' (%lu)"),
8636                            ip->insn_mo->name,
8637                            (unsigned long) imm_expr.X_add_number);
8638                   imm_expr.X_add_number &= 0x1f;
8639                 }
8640               if (*args == 'k')
8641                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8642               else
8643                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8644               imm_expr.X_op = O_absent;
8645               s = expr_end;
8646               continue;
8647
8648             case 'c':           /* break code */
8649               my_getExpression (&imm_expr, s);
8650               check_absolute_expr (ip, &imm_expr);
8651               if ((unsigned long) imm_expr.X_add_number > 1023)
8652                 {
8653                   as_warn (_("Illegal break code (%lu)"),
8654                            (unsigned long) imm_expr.X_add_number);
8655                   imm_expr.X_add_number &= OP_MASK_CODE;
8656                 }
8657               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8658               imm_expr.X_op = O_absent;
8659               s = expr_end;
8660               continue;
8661
8662             case 'q':           /* lower break code */
8663               my_getExpression (&imm_expr, s);
8664               check_absolute_expr (ip, &imm_expr);
8665               if ((unsigned long) imm_expr.X_add_number > 1023)
8666                 {
8667                   as_warn (_("Illegal lower break code (%lu)"),
8668                            (unsigned long) imm_expr.X_add_number);
8669                   imm_expr.X_add_number &= OP_MASK_CODE2;
8670                 }
8671               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8672               imm_expr.X_op = O_absent;
8673               s = expr_end;
8674               continue;
8675
8676             case 'B':           /* 20-bit syscall/break code.  */
8677               my_getExpression (&imm_expr, s);
8678               check_absolute_expr (ip, &imm_expr);
8679               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8680                 as_warn (_("Illegal 20-bit code (%lu)"),
8681                          (unsigned long) imm_expr.X_add_number);
8682               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8683               imm_expr.X_op = O_absent;
8684               s = expr_end;
8685               continue;
8686
8687             case 'C':           /* Coprocessor code */
8688               my_getExpression (&imm_expr, s);
8689               check_absolute_expr (ip, &imm_expr);
8690               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8691                 {
8692                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8693                            (unsigned long) imm_expr.X_add_number);
8694                   imm_expr.X_add_number &= ((1 << 25) - 1);
8695                 }
8696               ip->insn_opcode |= imm_expr.X_add_number;
8697               imm_expr.X_op = O_absent;
8698               s = expr_end;
8699               continue;
8700
8701             case 'J':           /* 19-bit wait code.  */
8702               my_getExpression (&imm_expr, s);
8703               check_absolute_expr (ip, &imm_expr);
8704               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8705                 as_warn (_("Illegal 19-bit code (%lu)"),
8706                          (unsigned long) imm_expr.X_add_number);
8707               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8708               imm_expr.X_op = O_absent;
8709               s = expr_end;
8710               continue;
8711
8712             case 'P':           /* Performance register */
8713               my_getExpression (&imm_expr, s);
8714               check_absolute_expr (ip, &imm_expr);
8715               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8716                 {
8717                   as_warn (_("Invalid performance register (%lu)"),
8718                            (unsigned long) imm_expr.X_add_number);
8719                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8720                 }
8721               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8722               imm_expr.X_op = O_absent;
8723               s = expr_end;
8724               continue;
8725
8726             case 'b':           /* base register */
8727             case 'd':           /* destination register */
8728             case 's':           /* source register */
8729             case 't':           /* target register */
8730             case 'r':           /* both target and source */
8731             case 'v':           /* both dest and source */
8732             case 'w':           /* both dest and target */
8733             case 'E':           /* coprocessor target register */
8734             case 'G':           /* coprocessor destination register */
8735             case 'K':           /* 'rdhwr' destination register */
8736             case 'x':           /* ignore register name */
8737             case 'z':           /* must be zero register */
8738             case 'U':           /* destination register (clo/clz).  */
8739               s_reset = s;
8740               if (s[0] == '$')
8741                 {
8742
8743                   if (ISDIGIT (s[1]))
8744                     {
8745                       ++s;
8746                       regno = 0;
8747                       do
8748                         {
8749                           regno *= 10;
8750                           regno += *s - '0';
8751                           ++s;
8752                         }
8753                       while (ISDIGIT (*s));
8754                       if (regno > 31)
8755                         as_bad (_("Invalid register number (%d)"), regno);
8756                     }
8757                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8758                     goto notreg;
8759                   else
8760                     {
8761                       if (s[1] == 'r' && s[2] == 'a')
8762                         {
8763                           s += 3;
8764                           regno = RA;
8765                         }
8766                       else if (s[1] == 'f' && s[2] == 'p')
8767                         {
8768                           s += 3;
8769                           regno = FP;
8770                         }
8771                       else if (s[1] == 's' && s[2] == 'p')
8772                         {
8773                           s += 3;
8774                           regno = SP;
8775                         }
8776                       else if (s[1] == 'g' && s[2] == 'p')
8777                         {
8778                           s += 3;
8779                           regno = GP;
8780                         }
8781                       else if (s[1] == 'a' && s[2] == 't')
8782                         {
8783                           s += 3;
8784                           regno = AT;
8785                         }
8786                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8787                         {
8788                           s += 4;
8789                           regno = KT0;
8790                         }
8791                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8792                         {
8793                           s += 4;
8794                           regno = KT1;
8795                         }
8796                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8797                         {
8798                           s += 5;
8799                           regno = ZERO;
8800                         }
8801                       else if (itbl_have_entries)
8802                         {
8803                           char *p, *n;
8804                           unsigned long r;
8805
8806                           p = s + 1;    /* advance past '$' */
8807                           n = itbl_get_field (&p);  /* n is name */
8808
8809                           /* See if this is a register defined in an
8810                              itbl entry.  */
8811                           if (itbl_get_reg_val (n, &r))
8812                             {
8813                               /* Get_field advances to the start of
8814                                  the next field, so we need to back
8815                                  rack to the end of the last field.  */
8816                               if (p)
8817                                 s = p - 1;
8818                               else
8819                                 s = strchr (s, '\0');
8820                               regno = r;
8821                             }
8822                           else
8823                             goto notreg;
8824                         }
8825                       else
8826                         goto notreg;
8827                     }
8828                   if (regno == AT
8829                       && ! mips_opts.noat
8830                       && *args != 'E'
8831                       && *args != 'G'
8832                       && *args != 'K')
8833                     as_warn (_("Used $at without \".set noat\""));
8834                   c = *args;
8835                   if (*s == ' ')
8836                     ++s;
8837                   if (args[1] != *s)
8838                     {
8839                       if (c == 'r' || c == 'v' || c == 'w')
8840                         {
8841                           regno = lastregno;
8842                           s = s_reset;
8843                           ++args;
8844                         }
8845                     }
8846                   /* 'z' only matches $0.  */
8847                   if (c == 'z' && regno != 0)
8848                     break;
8849
8850         /* Now that we have assembled one operand, we use the args string
8851          * to figure out where it goes in the instruction.  */
8852                   switch (c)
8853                     {
8854                     case 'r':
8855                     case 's':
8856                     case 'v':
8857                     case 'b':
8858                       ip->insn_opcode |= regno << OP_SH_RS;
8859                       break;
8860                     case 'd':
8861                     case 'G':
8862                     case 'K':
8863                       ip->insn_opcode |= regno << OP_SH_RD;
8864                       break;
8865                     case 'U':
8866                       ip->insn_opcode |= regno << OP_SH_RD;
8867                       ip->insn_opcode |= regno << OP_SH_RT;
8868                       break;
8869                     case 'w':
8870                     case 't':
8871                     case 'E':
8872                       ip->insn_opcode |= regno << OP_SH_RT;
8873                       break;
8874                     case 'x':
8875                       /* This case exists because on the r3000 trunc
8876                          expands into a macro which requires a gp
8877                          register.  On the r6000 or r4000 it is
8878                          assembled into a single instruction which
8879                          ignores the register.  Thus the insn version
8880                          is MIPS_ISA2 and uses 'x', and the macro
8881                          version is MIPS_ISA1 and uses 't'.  */
8882                       break;
8883                     case 'z':
8884                       /* This case is for the div instruction, which
8885                          acts differently if the destination argument
8886                          is $0.  This only matches $0, and is checked
8887                          outside the switch.  */
8888                       break;
8889                     case 'D':
8890                       /* Itbl operand; not yet implemented. FIXME ?? */
8891                       break;
8892                       /* What about all other operands like 'i', which
8893                          can be specified in the opcode table? */
8894                     }
8895                   lastregno = regno;
8896                   continue;
8897                 }
8898             notreg:
8899               switch (*args++)
8900                 {
8901                 case 'r':
8902                 case 'v':
8903                   ip->insn_opcode |= lastregno << OP_SH_RS;
8904                   continue;
8905                 case 'w':
8906                   ip->insn_opcode |= lastregno << OP_SH_RT;
8907                   continue;
8908                 }
8909               break;
8910
8911             case 'O':           /* MDMX alignment immediate constant.  */
8912               my_getExpression (&imm_expr, s);
8913               check_absolute_expr (ip, &imm_expr);
8914               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8915                 {
8916                   as_warn ("Improper align amount (%ld), using low bits",
8917                            (long) imm_expr.X_add_number);
8918                   imm_expr.X_add_number &= OP_MASK_ALN;
8919                 }
8920               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8921               imm_expr.X_op = O_absent;
8922               s = expr_end;
8923               continue;
8924
8925             case 'Q':           /* MDMX vector, element sel, or const.  */
8926               if (s[0] != '$')
8927                 {
8928                   /* MDMX Immediate.  */
8929                   my_getExpression (&imm_expr, s);
8930                   check_absolute_expr (ip, &imm_expr);
8931                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8932                     {
8933                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8934                                (long) imm_expr.X_add_number);
8935                       imm_expr.X_add_number &= OP_MASK_FT;
8936                     }
8937                   imm_expr.X_add_number &= OP_MASK_FT;
8938                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8939                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8940                   else
8941                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8942                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8943                   imm_expr.X_op = O_absent;
8944                   s = expr_end;
8945                   continue;
8946                 }
8947               /* Not MDMX Immediate.  Fall through.  */
8948             case 'X':           /* MDMX destination register.  */
8949             case 'Y':           /* MDMX source register.  */
8950             case 'Z':           /* MDMX target register.  */
8951               is_mdmx = 1;
8952             case 'D':           /* floating point destination register */
8953             case 'S':           /* floating point source register */
8954             case 'T':           /* floating point target register */
8955             case 'R':           /* floating point source register */
8956             case 'V':
8957             case 'W':
8958               s_reset = s;
8959               /* Accept $fN for FP and MDMX register numbers, and in
8960                  addition accept $vN for MDMX register numbers.  */
8961               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8962                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8963                       && ISDIGIT (s[2])))
8964                 {
8965                   s += 2;
8966                   regno = 0;
8967                   do
8968                     {
8969                       regno *= 10;
8970                       regno += *s - '0';
8971                       ++s;
8972                     }
8973                   while (ISDIGIT (*s));
8974
8975                   if (regno > 31)
8976                     as_bad (_("Invalid float register number (%d)"), regno);
8977
8978                   if ((regno & 1) != 0
8979                       && HAVE_32BIT_FPRS
8980                       && ! (strcmp (str, "mtc1") == 0
8981                             || strcmp (str, "mfc1") == 0
8982                             || strcmp (str, "lwc1") == 0
8983                             || strcmp (str, "swc1") == 0
8984                             || strcmp (str, "l.s") == 0
8985                             || strcmp (str, "s.s") == 0))
8986                     as_warn (_("Float register should be even, was %d"),
8987                              regno);
8988
8989                   c = *args;
8990                   if (*s == ' ')
8991                     ++s;
8992                   if (args[1] != *s)
8993                     {
8994                       if (c == 'V' || c == 'W')
8995                         {
8996                           regno = lastregno;
8997                           s = s_reset;
8998                           ++args;
8999                         }
9000                     }
9001                   switch (c)
9002                     {
9003                     case 'D':
9004                     case 'X':
9005                       ip->insn_opcode |= regno << OP_SH_FD;
9006                       break;
9007                     case 'V':
9008                     case 'S':
9009                     case 'Y':
9010                       ip->insn_opcode |= regno << OP_SH_FS;
9011                       break;
9012                     case 'Q':
9013                       /* This is like 'Z', but also needs to fix the MDMX
9014                          vector/scalar select bits.  Note that the
9015                          scalar immediate case is handled above.  */
9016                       if (*s == '[')
9017                         {
9018                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9019                           int max_el = (is_qh ? 3 : 7);
9020                           s++;
9021                           my_getExpression(&imm_expr, s);
9022                           check_absolute_expr (ip, &imm_expr);
9023                           s = expr_end;
9024                           if (imm_expr.X_add_number > max_el)
9025                             as_bad(_("Bad element selector %ld"),
9026                                    (long) imm_expr.X_add_number);
9027                           imm_expr.X_add_number &= max_el;
9028                           ip->insn_opcode |= (imm_expr.X_add_number
9029                                               << (OP_SH_VSEL +
9030                                                   (is_qh ? 2 : 1)));
9031                           if (*s != ']')
9032                             as_warn(_("Expecting ']' found '%s'"), s);
9033                           else
9034                             s++;
9035                         }
9036                       else
9037                         {
9038                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9039                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9040                                                 << OP_SH_VSEL);
9041                           else
9042                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9043                                                 OP_SH_VSEL);
9044                         }
9045                       /* Fall through */
9046                     case 'W':
9047                     case 'T':
9048                     case 'Z':
9049                       ip->insn_opcode |= regno << OP_SH_FT;
9050                       break;
9051                     case 'R':
9052                       ip->insn_opcode |= regno << OP_SH_FR;
9053                       break;
9054                     }
9055                   lastregno = regno;
9056                   continue;
9057                 }
9058
9059               switch (*args++)
9060                 {
9061                 case 'V':
9062                   ip->insn_opcode |= lastregno << OP_SH_FS;
9063                   continue;
9064                 case 'W':
9065                   ip->insn_opcode |= lastregno << OP_SH_FT;
9066                   continue;
9067                 }
9068               break;
9069
9070             case 'I':
9071               my_getExpression (&imm_expr, s);
9072               if (imm_expr.X_op != O_big
9073                   && imm_expr.X_op != O_constant)
9074                 insn_error = _("absolute expression required");
9075               normalize_constant_expr (&imm_expr);
9076               s = expr_end;
9077               continue;
9078
9079             case 'A':
9080               my_getExpression (&offset_expr, s);
9081               *imm_reloc = BFD_RELOC_32;
9082               s = expr_end;
9083               continue;
9084
9085             case 'F':
9086             case 'L':
9087             case 'f':
9088             case 'l':
9089               {
9090                 int f64;
9091                 int using_gprs;
9092                 char *save_in;
9093                 char *err;
9094                 unsigned char temp[8];
9095                 int len;
9096                 unsigned int length;
9097                 segT seg;
9098                 subsegT subseg;
9099                 char *p;
9100
9101                 /* These only appear as the last operand in an
9102                    instruction, and every instruction that accepts
9103                    them in any variant accepts them in all variants.
9104                    This means we don't have to worry about backing out
9105                    any changes if the instruction does not match.
9106
9107                    The difference between them is the size of the
9108                    floating point constant and where it goes.  For 'F'
9109                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9110                    is 32 bits.  Where the constant is placed is based
9111                    on how the MIPS assembler does things:
9112                     F -- .rdata
9113                     L -- .lit8
9114                     f -- immediate value
9115                     l -- .lit4
9116
9117                     The .lit4 and .lit8 sections are only used if
9118                     permitted by the -G argument.
9119
9120                     When generating embedded PIC code, we use the
9121                     .lit8 section but not the .lit4 section (we can do
9122                     .lit4 inline easily; we need to put .lit8
9123                     somewhere in the data segment, and using .lit8
9124                     permits the linker to eventually combine identical
9125                     .lit8 entries).
9126
9127                     The code below needs to know whether the target register
9128                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9129                     'F' are used with GPR-based instructions and 'l' and
9130                     'L' are used with FPR-based instructions.  */
9131
9132                 f64 = *args == 'F' || *args == 'L';
9133                 using_gprs = *args == 'F' || *args == 'f';
9134
9135                 save_in = input_line_pointer;
9136                 input_line_pointer = s;
9137                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9138                 length = len;
9139                 s = input_line_pointer;
9140                 input_line_pointer = save_in;
9141                 if (err != NULL && *err != '\0')
9142                   {
9143                     as_bad (_("Bad floating point constant: %s"), err);
9144                     memset (temp, '\0', sizeof temp);
9145                     length = f64 ? 8 : 4;
9146                   }
9147
9148                 assert (length == (unsigned) (f64 ? 8 : 4));
9149
9150                 if (*args == 'f'
9151                     || (*args == 'l'
9152                         && (! USE_GLOBAL_POINTER_OPT
9153                             || mips_pic == EMBEDDED_PIC
9154                             || g_switch_value < 4
9155                             || (temp[0] == 0 && temp[1] == 0)
9156                             || (temp[2] == 0 && temp[3] == 0))))
9157                   {
9158                     imm_expr.X_op = O_constant;
9159                     if (! target_big_endian)
9160                       imm_expr.X_add_number = bfd_getl32 (temp);
9161                     else
9162                       imm_expr.X_add_number = bfd_getb32 (temp);
9163                   }
9164                 else if (length > 4
9165                          && ! mips_disable_float_construction
9166                          /* Constants can only be constructed in GPRs and
9167                             copied to FPRs if the GPRs are at least as wide
9168                             as the FPRs.  Force the constant into memory if
9169                             we are using 64-bit FPRs but the GPRs are only
9170                             32 bits wide.  */
9171                          && (using_gprs
9172                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9173                          && ((temp[0] == 0 && temp[1] == 0)
9174                              || (temp[2] == 0 && temp[3] == 0))
9175                          && ((temp[4] == 0 && temp[5] == 0)
9176                              || (temp[6] == 0 && temp[7] == 0)))
9177                   {
9178                     /* The value is simple enough to load with a couple of
9179                        instructions.  If using 32-bit registers, set
9180                        imm_expr to the high order 32 bits and offset_expr to
9181                        the low order 32 bits.  Otherwise, set imm_expr to
9182                        the entire 64 bit constant.  */
9183                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9184                       {
9185                         imm_expr.X_op = O_constant;
9186                         offset_expr.X_op = O_constant;
9187                         if (! target_big_endian)
9188                           {
9189                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9190                             offset_expr.X_add_number = bfd_getl32 (temp);
9191                           }
9192                         else
9193                           {
9194                             imm_expr.X_add_number = bfd_getb32 (temp);
9195                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9196                           }
9197                         if (offset_expr.X_add_number == 0)
9198                           offset_expr.X_op = O_absent;
9199                       }
9200                     else if (sizeof (imm_expr.X_add_number) > 4)
9201                       {
9202                         imm_expr.X_op = O_constant;
9203                         if (! target_big_endian)
9204                           imm_expr.X_add_number = bfd_getl64 (temp);
9205                         else
9206                           imm_expr.X_add_number = bfd_getb64 (temp);
9207                       }
9208                     else
9209                       {
9210                         imm_expr.X_op = O_big;
9211                         imm_expr.X_add_number = 4;
9212                         if (! target_big_endian)
9213                           {
9214                             generic_bignum[0] = bfd_getl16 (temp);
9215                             generic_bignum[1] = bfd_getl16 (temp + 2);
9216                             generic_bignum[2] = bfd_getl16 (temp + 4);
9217                             generic_bignum[3] = bfd_getl16 (temp + 6);
9218                           }
9219                         else
9220                           {
9221                             generic_bignum[0] = bfd_getb16 (temp + 6);
9222                             generic_bignum[1] = bfd_getb16 (temp + 4);
9223                             generic_bignum[2] = bfd_getb16 (temp + 2);
9224                             generic_bignum[3] = bfd_getb16 (temp);
9225                           }
9226                       }
9227                   }
9228                 else
9229                   {
9230                     const char *newname;
9231                     segT new_seg;
9232
9233                     /* Switch to the right section.  */
9234                     seg = now_seg;
9235                     subseg = now_subseg;
9236                     switch (*args)
9237                       {
9238                       default: /* unused default case avoids warnings.  */
9239                       case 'L':
9240                         newname = RDATA_SECTION_NAME;
9241                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9242                             || mips_pic == EMBEDDED_PIC)
9243                           newname = ".lit8";
9244                         break;
9245                       case 'F':
9246                         if (mips_pic == EMBEDDED_PIC)
9247                           newname = ".lit8";
9248                         else
9249                           newname = RDATA_SECTION_NAME;
9250                         break;
9251                       case 'l':
9252                         assert (!USE_GLOBAL_POINTER_OPT
9253                                 || g_switch_value >= 4);
9254                         newname = ".lit4";
9255                         break;
9256                       }
9257                     new_seg = subseg_new (newname, (subsegT) 0);
9258                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9259                       bfd_set_section_flags (stdoutput, new_seg,
9260                                              (SEC_ALLOC
9261                                               | SEC_LOAD
9262                                               | SEC_READONLY
9263                                               | SEC_DATA));
9264                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9265                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9266                         && strcmp (TARGET_OS, "elf") != 0)
9267                       record_alignment (new_seg, 4);
9268                     else
9269                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9270                     if (seg == now_seg)
9271                       as_bad (_("Can't use floating point insn in this section"));
9272
9273                     /* Set the argument to the current address in the
9274                        section.  */
9275                     offset_expr.X_op = O_symbol;
9276                     offset_expr.X_add_symbol =
9277                       symbol_new ("L0\001", now_seg,
9278                                   (valueT) frag_now_fix (), frag_now);
9279                     offset_expr.X_add_number = 0;
9280
9281                     /* Put the floating point number into the section.  */
9282                     p = frag_more ((int) length);
9283                     memcpy (p, temp, length);
9284
9285                     /* Switch back to the original section.  */
9286                     subseg_set (seg, subseg);
9287                   }
9288               }
9289               continue;
9290
9291             case 'i':           /* 16 bit unsigned immediate */
9292             case 'j':           /* 16 bit signed immediate */
9293               *imm_reloc = BFD_RELOC_LO16;
9294               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9295                 {
9296                   int more;
9297                   offsetT minval, maxval;
9298
9299                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9300                           && strcmp (insn->name, insn[1].name) == 0);
9301
9302                   /* If the expression was written as an unsigned number,
9303                      only treat it as signed if there are no more
9304                      alternatives.  */
9305                   if (more
9306                       && *args == 'j'
9307                       && sizeof (imm_expr.X_add_number) <= 4
9308                       && imm_expr.X_op == O_constant
9309                       && imm_expr.X_add_number < 0
9310                       && imm_expr.X_unsigned
9311                       && HAVE_64BIT_GPRS)
9312                     break;
9313
9314                   /* For compatibility with older assemblers, we accept
9315                      0x8000-0xffff as signed 16-bit numbers when only
9316                      signed numbers are allowed.  */
9317                   if (*args == 'i')
9318                     minval = 0, maxval = 0xffff;
9319                   else if (more)
9320                     minval = -0x8000, maxval = 0x7fff;
9321                   else
9322                     minval = -0x8000, maxval = 0xffff;
9323
9324                   if (imm_expr.X_op != O_constant
9325                       || imm_expr.X_add_number < minval
9326                       || imm_expr.X_add_number > maxval)
9327                     {
9328                       if (more)
9329                         break;
9330                       if (imm_expr.X_op == O_constant
9331                           || imm_expr.X_op == O_big)
9332                         as_bad (_("expression out of range"));
9333                     }
9334                 }
9335               s = expr_end;
9336               continue;
9337
9338             case 'o':           /* 16 bit offset */
9339               /* Check whether there is only a single bracketed expression
9340                  left.  If so, it must be the base register and the
9341                  constant must be zero.  */
9342               if (*s == '(' && strchr (s + 1, '(') == 0)
9343                 {
9344                   offset_expr.X_op = O_constant;
9345                   offset_expr.X_add_number = 0;
9346                   continue;
9347                 }
9348
9349               /* If this value won't fit into a 16 bit offset, then go
9350                  find a macro that will generate the 32 bit offset
9351                  code pattern.  */
9352               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9353                   && (offset_expr.X_op != O_constant
9354                       || offset_expr.X_add_number >= 0x8000
9355                       || offset_expr.X_add_number < -0x8000))
9356                 break;
9357
9358               s = expr_end;
9359               continue;
9360
9361             case 'p':           /* pc relative offset */
9362               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9363               my_getExpression (&offset_expr, s);
9364               s = expr_end;
9365               continue;
9366
9367             case 'u':           /* upper 16 bits */
9368               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9369                   && imm_expr.X_op == O_constant
9370                   && (imm_expr.X_add_number < 0
9371                       || imm_expr.X_add_number >= 0x10000))
9372                 as_bad (_("lui expression not in range 0..65535"));
9373               s = expr_end;
9374               continue;
9375
9376             case 'a':           /* 26 bit address */
9377               my_getExpression (&offset_expr, s);
9378               s = expr_end;
9379               *offset_reloc = BFD_RELOC_MIPS_JMP;
9380               continue;
9381
9382             case 'N':           /* 3 bit branch condition code */
9383             case 'M':           /* 3 bit compare condition code */
9384               if (strncmp (s, "$fcc", 4) != 0)
9385                 break;
9386               s += 4;
9387               regno = 0;
9388               do
9389                 {
9390                   regno *= 10;
9391                   regno += *s - '0';
9392                   ++s;
9393                 }
9394               while (ISDIGIT (*s));
9395               if (regno > 7)
9396                 as_bad (_("Invalid condition code register $fcc%d"), regno);
9397               if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9398                    || strcmp(str + strlen(str) - 5, "any2f") == 0
9399                    || strcmp(str + strlen(str) - 5, "any2t") == 0)
9400                   && (regno & 1) != 0)
9401                 as_warn(_("Condition code register should be even for %s, was %d"),
9402                         str, regno);
9403               if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9404                    || strcmp(str + strlen(str) - 5, "any4t") == 0)
9405                   && (regno & 3) != 0)
9406                 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9407                         str, regno);
9408               if (*args == 'N')
9409                 ip->insn_opcode |= regno << OP_SH_BCC;
9410               else
9411                 ip->insn_opcode |= regno << OP_SH_CCC;
9412               continue;
9413
9414             case 'H':
9415               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9416                 s += 2;
9417               if (ISDIGIT (*s))
9418                 {
9419                   c = 0;
9420                   do
9421                     {
9422                       c *= 10;
9423                       c += *s - '0';
9424                       ++s;
9425                     }
9426                   while (ISDIGIT (*s));
9427                 }
9428               else
9429                 c = 8; /* Invalid sel value.  */
9430
9431               if (c > 7)
9432                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9433               ip->insn_opcode |= c;
9434               continue;
9435
9436             case 'e':
9437               /* Must be at least one digit.  */
9438               my_getExpression (&imm_expr, s);
9439               check_absolute_expr (ip, &imm_expr);
9440
9441               if ((unsigned long) imm_expr.X_add_number
9442                   > (unsigned long) OP_MASK_VECBYTE)
9443                 {
9444                   as_bad (_("bad byte vector index (%ld)"),
9445                            (long) imm_expr.X_add_number);
9446                   imm_expr.X_add_number = 0;
9447                 }
9448
9449               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9450               imm_expr.X_op = O_absent;
9451               s = expr_end;
9452               continue;
9453
9454             case '%':
9455               my_getExpression (&imm_expr, s);
9456               check_absolute_expr (ip, &imm_expr);
9457
9458               if ((unsigned long) imm_expr.X_add_number
9459                   > (unsigned long) OP_MASK_VECALIGN)
9460                 {
9461                   as_bad (_("bad byte vector index (%ld)"),
9462                            (long) imm_expr.X_add_number);
9463                   imm_expr.X_add_number = 0;
9464                 }
9465
9466               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9467               imm_expr.X_op = O_absent;
9468               s = expr_end;
9469               continue;
9470
9471             default:
9472               as_bad (_("bad char = '%c'\n"), *args);
9473               internalError ();
9474             }
9475           break;
9476         }
9477       /* Args don't match.  */
9478       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9479           !strcmp (insn->name, insn[1].name))
9480         {
9481           ++insn;
9482           s = argsStart;
9483           insn_error = _("illegal operands");
9484           continue;
9485         }
9486       if (save_c)
9487         *(--s) = save_c;
9488       insn_error = _("illegal operands");
9489       return;
9490     }
9491 }
9492
9493 /* This routine assembles an instruction into its binary format when
9494    assembling for the mips16.  As a side effect, it sets one of the
9495    global variables imm_reloc or offset_reloc to the type of
9496    relocation to do if one of the operands is an address expression.
9497    It also sets mips16_small and mips16_ext if the user explicitly
9498    requested a small or extended instruction.  */
9499
9500 static void
9501 mips16_ip (char *str, struct mips_cl_insn *ip)
9502 {
9503   char *s;
9504   const char *args;
9505   struct mips_opcode *insn;
9506   char *argsstart;
9507   unsigned int regno;
9508   unsigned int lastregno = 0;
9509   char *s_reset;
9510
9511   insn_error = NULL;
9512
9513   mips16_small = FALSE;
9514   mips16_ext = FALSE;
9515
9516   for (s = str; ISLOWER (*s); ++s)
9517     ;
9518   switch (*s)
9519     {
9520     case '\0':
9521       break;
9522
9523     case ' ':
9524       *s++ = '\0';
9525       break;
9526
9527     case '.':
9528       if (s[1] == 't' && s[2] == ' ')
9529         {
9530           *s = '\0';
9531           mips16_small = TRUE;
9532           s += 3;
9533           break;
9534         }
9535       else if (s[1] == 'e' && s[2] == ' ')
9536         {
9537           *s = '\0';
9538           mips16_ext = TRUE;
9539           s += 3;
9540           break;
9541         }
9542       /* Fall through.  */
9543     default:
9544       insn_error = _("unknown opcode");
9545       return;
9546     }
9547
9548   if (mips_opts.noautoextend && ! mips16_ext)
9549     mips16_small = TRUE;
9550
9551   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9552     {
9553       insn_error = _("unrecognized opcode");
9554       return;
9555     }
9556
9557   argsstart = s;
9558   for (;;)
9559     {
9560       assert (strcmp (insn->name, str) == 0);
9561
9562       ip->insn_mo = insn;
9563       ip->insn_opcode = insn->match;
9564       ip->use_extend = FALSE;
9565       imm_expr.X_op = O_absent;
9566       imm_reloc[0] = BFD_RELOC_UNUSED;
9567       imm_reloc[1] = BFD_RELOC_UNUSED;
9568       imm_reloc[2] = BFD_RELOC_UNUSED;
9569       imm2_expr.X_op = O_absent;
9570       offset_expr.X_op = O_absent;
9571       offset_reloc[0] = BFD_RELOC_UNUSED;
9572       offset_reloc[1] = BFD_RELOC_UNUSED;
9573       offset_reloc[2] = BFD_RELOC_UNUSED;
9574       for (args = insn->args; 1; ++args)
9575         {
9576           int c;
9577
9578           if (*s == ' ')
9579             ++s;
9580
9581           /* In this switch statement we call break if we did not find
9582              a match, continue if we did find a match, or return if we
9583              are done.  */
9584
9585           c = *args;
9586           switch (c)
9587             {
9588             case '\0':
9589               if (*s == '\0')
9590                 {
9591                   /* Stuff the immediate value in now, if we can.  */
9592                   if (imm_expr.X_op == O_constant
9593                       && *imm_reloc > BFD_RELOC_UNUSED
9594                       && insn->pinfo != INSN_MACRO)
9595                     {
9596                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9597                                     imm_expr.X_add_number, TRUE, mips16_small,
9598                                     mips16_ext, &ip->insn_opcode,
9599                                     &ip->use_extend, &ip->extend);
9600                       imm_expr.X_op = O_absent;
9601                       *imm_reloc = BFD_RELOC_UNUSED;
9602                     }
9603
9604                   return;
9605                 }
9606               break;
9607
9608             case ',':
9609               if (*s++ == c)
9610                 continue;
9611               s--;
9612               switch (*++args)
9613                 {
9614                 case 'v':
9615                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9616                   continue;
9617                 case 'w':
9618                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9619                   continue;
9620                 }
9621               break;
9622
9623             case '(':
9624             case ')':
9625               if (*s++ == c)
9626                 continue;
9627               break;
9628
9629             case 'v':
9630             case 'w':
9631               if (s[0] != '$')
9632                 {
9633                   if (c == 'v')
9634                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9635                   else
9636                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9637                   ++args;
9638                   continue;
9639                 }
9640               /* Fall through.  */
9641             case 'x':
9642             case 'y':
9643             case 'z':
9644             case 'Z':
9645             case '0':
9646             case 'S':
9647             case 'R':
9648             case 'X':
9649             case 'Y':
9650               if (s[0] != '$')
9651                 break;
9652               s_reset = s;
9653               if (ISDIGIT (s[1]))
9654                 {
9655                   ++s;
9656                   regno = 0;
9657                   do
9658                     {
9659                       regno *= 10;
9660                       regno += *s - '0';
9661                       ++s;
9662                     }
9663                   while (ISDIGIT (*s));
9664                   if (regno > 31)
9665                     {
9666                       as_bad (_("invalid register number (%d)"), regno);
9667                       regno = 2;
9668                     }
9669                 }
9670               else
9671                 {
9672                   if (s[1] == 'r' && s[2] == 'a')
9673                     {
9674                       s += 3;
9675                       regno = RA;
9676                     }
9677                   else if (s[1] == 'f' && s[2] == 'p')
9678                     {
9679                       s += 3;
9680                       regno = FP;
9681                     }
9682                   else if (s[1] == 's' && s[2] == 'p')
9683                     {
9684                       s += 3;
9685                       regno = SP;
9686                     }
9687                   else if (s[1] == 'g' && s[2] == 'p')
9688                     {
9689                       s += 3;
9690                       regno = GP;
9691                     }
9692                   else if (s[1] == 'a' && s[2] == 't')
9693                     {
9694                       s += 3;
9695                       regno = AT;
9696                     }
9697                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9698                     {
9699                       s += 4;
9700                       regno = KT0;
9701                     }
9702                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9703                     {
9704                       s += 4;
9705                       regno = KT1;
9706                     }
9707                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9708                     {
9709                       s += 5;
9710                       regno = ZERO;
9711                     }
9712                   else
9713                     break;
9714                 }
9715
9716               if (*s == ' ')
9717                 ++s;
9718               if (args[1] != *s)
9719                 {
9720                   if (c == 'v' || c == 'w')
9721                     {
9722                       regno = mips16_to_32_reg_map[lastregno];
9723                       s = s_reset;
9724                       ++args;
9725                     }
9726                 }
9727
9728               switch (c)
9729                 {
9730                 case 'x':
9731                 case 'y':
9732                 case 'z':
9733                 case 'v':
9734                 case 'w':
9735                 case 'Z':
9736                   regno = mips32_to_16_reg_map[regno];
9737                   break;
9738
9739                 case '0':
9740                   if (regno != 0)
9741                     regno = ILLEGAL_REG;
9742                   break;
9743
9744                 case 'S':
9745                   if (regno != SP)
9746                     regno = ILLEGAL_REG;
9747                   break;
9748
9749                 case 'R':
9750                   if (regno != RA)
9751                     regno = ILLEGAL_REG;
9752                   break;
9753
9754                 case 'X':
9755                 case 'Y':
9756                   if (regno == AT && ! mips_opts.noat)
9757                     as_warn (_("used $at without \".set noat\""));
9758                   break;
9759
9760                 default:
9761                   internalError ();
9762                 }
9763
9764               if (regno == ILLEGAL_REG)
9765                 break;
9766
9767               switch (c)
9768                 {
9769                 case 'x':
9770                 case 'v':
9771                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9772                   break;
9773                 case 'y':
9774                 case 'w':
9775                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9776                   break;
9777                 case 'z':
9778                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9779                   break;
9780                 case 'Z':
9781                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9782                 case '0':
9783                 case 'S':
9784                 case 'R':
9785                   break;
9786                 case 'X':
9787                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9788                   break;
9789                 case 'Y':
9790                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9791                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9792                   break;
9793                 default:
9794                   internalError ();
9795                 }
9796
9797               lastregno = regno;
9798               continue;
9799
9800             case 'P':
9801               if (strncmp (s, "$pc", 3) == 0)
9802                 {
9803                   s += 3;
9804                   continue;
9805                 }
9806               break;
9807
9808             case '<':
9809             case '>':
9810             case '[':
9811             case ']':
9812             case '4':
9813             case '5':
9814             case 'H':
9815             case 'W':
9816             case 'D':
9817             case 'j':
9818             case '8':
9819             case 'V':
9820             case 'C':
9821             case 'U':
9822             case 'k':
9823             case 'K':
9824               if (s[0] == '%'
9825                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9826                 {
9827                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9828                      and generate the appropriate reloc.  If the text
9829                      inside %gprel is not a symbol name with an
9830                      optional offset, then we generate a normal reloc
9831                      and will probably fail later.  */
9832                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9833                   if (imm_expr.X_op == O_symbol)
9834                     {
9835                       mips16_ext = TRUE;
9836                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9837                       s = expr_end;
9838                       ip->use_extend = TRUE;
9839                       ip->extend = 0;
9840                       continue;
9841                     }
9842                 }
9843               else
9844                 {
9845                   /* Just pick up a normal expression.  */
9846                   my_getExpression (&imm_expr, s);
9847                 }
9848
9849               if (imm_expr.X_op == O_register)
9850                 {
9851                   /* What we thought was an expression turned out to
9852                      be a register.  */
9853
9854                   if (s[0] == '(' && args[1] == '(')
9855                     {
9856                       /* It looks like the expression was omitted
9857                          before a register indirection, which means
9858                          that the expression is implicitly zero.  We
9859                          still set up imm_expr, so that we handle
9860                          explicit extensions correctly.  */
9861                       imm_expr.X_op = O_constant;
9862                       imm_expr.X_add_number = 0;
9863                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9864                       continue;
9865                     }
9866
9867                   break;
9868                 }
9869
9870               /* We need to relax this instruction.  */
9871               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9872               s = expr_end;
9873               continue;
9874
9875             case 'p':
9876             case 'q':
9877             case 'A':
9878             case 'B':
9879             case 'E':
9880               /* We use offset_reloc rather than imm_reloc for the PC
9881                  relative operands.  This lets macros with both
9882                  immediate and address operands work correctly.  */
9883               my_getExpression (&offset_expr, s);
9884
9885               if (offset_expr.X_op == O_register)
9886                 break;
9887
9888               /* We need to relax this instruction.  */
9889               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9890               s = expr_end;
9891               continue;
9892
9893             case '6':           /* break code */
9894               my_getExpression (&imm_expr, s);
9895               check_absolute_expr (ip, &imm_expr);
9896               if ((unsigned long) imm_expr.X_add_number > 63)
9897                 {
9898                   as_warn (_("Invalid value for `%s' (%lu)"),
9899                            ip->insn_mo->name,
9900                            (unsigned long) imm_expr.X_add_number);
9901                   imm_expr.X_add_number &= 0x3f;
9902                 }
9903               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9904               imm_expr.X_op = O_absent;
9905               s = expr_end;
9906               continue;
9907
9908             case 'a':           /* 26 bit address */
9909               my_getExpression (&offset_expr, s);
9910               s = expr_end;
9911               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9912               ip->insn_opcode <<= 16;
9913               continue;
9914
9915             case 'l':           /* register list for entry macro */
9916             case 'L':           /* register list for exit macro */
9917               {
9918                 int mask;
9919
9920                 if (c == 'l')
9921                   mask = 0;
9922                 else
9923                   mask = 7 << 3;
9924                 while (*s != '\0')
9925                   {
9926                     int freg, reg1, reg2;
9927
9928                     while (*s == ' ' || *s == ',')
9929                       ++s;
9930                     if (*s != '$')
9931                       {
9932                         as_bad (_("can't parse register list"));
9933                         break;
9934                       }
9935                     ++s;
9936                     if (*s != 'f')
9937                       freg = 0;
9938                     else
9939                       {
9940                         freg = 1;
9941                         ++s;
9942                       }
9943                     reg1 = 0;
9944                     while (ISDIGIT (*s))
9945                       {
9946                         reg1 *= 10;
9947                         reg1 += *s - '0';
9948                         ++s;
9949                       }
9950                     if (*s == ' ')
9951                       ++s;
9952                     if (*s != '-')
9953                       reg2 = reg1;
9954                     else
9955                       {
9956                         ++s;
9957                         if (*s != '$')
9958                           break;
9959                         ++s;
9960                         if (freg)
9961                           {
9962                             if (*s == 'f')
9963                               ++s;
9964                             else
9965                               {
9966                                 as_bad (_("invalid register list"));
9967                                 break;
9968                               }
9969                           }
9970                         reg2 = 0;
9971                         while (ISDIGIT (*s))
9972                           {
9973                             reg2 *= 10;
9974                             reg2 += *s - '0';
9975                             ++s;
9976                           }
9977                       }
9978                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9979                       {
9980                         mask &= ~ (7 << 3);
9981                         mask |= 5 << 3;
9982                       }
9983                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9984                       {
9985                         mask &= ~ (7 << 3);
9986                         mask |= 6 << 3;
9987                       }
9988                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9989                       mask |= (reg2 - 3) << 3;
9990                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9991                       mask |= (reg2 - 15) << 1;
9992                     else if (reg1 == RA && reg2 == RA)
9993                       mask |= 1;
9994                     else
9995                       {
9996                         as_bad (_("invalid register list"));
9997                         break;
9998                       }
9999                   }
10000                 /* The mask is filled in in the opcode table for the
10001                    benefit of the disassembler.  We remove it before
10002                    applying the actual mask.  */
10003                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10004                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10005               }
10006             continue;
10007
10008             case 'e':           /* extend code */
10009               my_getExpression (&imm_expr, s);
10010               check_absolute_expr (ip, &imm_expr);
10011               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10012                 {
10013                   as_warn (_("Invalid value for `%s' (%lu)"),
10014                            ip->insn_mo->name,
10015                            (unsigned long) imm_expr.X_add_number);
10016                   imm_expr.X_add_number &= 0x7ff;
10017                 }
10018               ip->insn_opcode |= imm_expr.X_add_number;
10019               imm_expr.X_op = O_absent;
10020               s = expr_end;
10021               continue;
10022
10023             default:
10024               internalError ();
10025             }
10026           break;
10027         }
10028
10029       /* Args don't match.  */
10030       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10031           strcmp (insn->name, insn[1].name) == 0)
10032         {
10033           ++insn;
10034           s = argsstart;
10035           continue;
10036         }
10037
10038       insn_error = _("illegal operands");
10039
10040       return;
10041     }
10042 }
10043
10044 /* This structure holds information we know about a mips16 immediate
10045    argument type.  */
10046
10047 struct mips16_immed_operand
10048 {
10049   /* The type code used in the argument string in the opcode table.  */
10050   int type;
10051   /* The number of bits in the short form of the opcode.  */
10052   int nbits;
10053   /* The number of bits in the extended form of the opcode.  */
10054   int extbits;
10055   /* The amount by which the short form is shifted when it is used;
10056      for example, the sw instruction has a shift count of 2.  */
10057   int shift;
10058   /* The amount by which the short form is shifted when it is stored
10059      into the instruction code.  */
10060   int op_shift;
10061   /* Non-zero if the short form is unsigned.  */
10062   int unsp;
10063   /* Non-zero if the extended form is unsigned.  */
10064   int extu;
10065   /* Non-zero if the value is PC relative.  */
10066   int pcrel;
10067 };
10068
10069 /* The mips16 immediate operand types.  */
10070
10071 static const struct mips16_immed_operand mips16_immed_operands[] =
10072 {
10073   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10074   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10075   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10076   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10077   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10078   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10079   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10080   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10081   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10082   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10083   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10084   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10085   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10086   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10087   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10088   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10089   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10090   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10091   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10092   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10093   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10094 };
10095
10096 #define MIPS16_NUM_IMMED \
10097   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10098
10099 /* Handle a mips16 instruction with an immediate value.  This or's the
10100    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10101    whether an extended value is needed; if one is needed, it sets
10102    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10103    If SMALL is true, an unextended opcode was explicitly requested.
10104    If EXT is true, an extended opcode was explicitly requested.  If
10105    WARN is true, warn if EXT does not match reality.  */
10106
10107 static void
10108 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10109               bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10110               unsigned long *insn, bfd_boolean *use_extend,
10111               unsigned short *extend)
10112 {
10113   register const struct mips16_immed_operand *op;
10114   int mintiny, maxtiny;
10115   bfd_boolean needext;
10116
10117   op = mips16_immed_operands;
10118   while (op->type != type)
10119     {
10120       ++op;
10121       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10122     }
10123
10124   if (op->unsp)
10125     {
10126       if (type == '<' || type == '>' || type == '[' || type == ']')
10127         {
10128           mintiny = 1;
10129           maxtiny = 1 << op->nbits;
10130         }
10131       else
10132         {
10133           mintiny = 0;
10134           maxtiny = (1 << op->nbits) - 1;
10135         }
10136     }
10137   else
10138     {
10139       mintiny = - (1 << (op->nbits - 1));
10140       maxtiny = (1 << (op->nbits - 1)) - 1;
10141     }
10142
10143   /* Branch offsets have an implicit 0 in the lowest bit.  */
10144   if (type == 'p' || type == 'q')
10145     val /= 2;
10146
10147   if ((val & ((1 << op->shift) - 1)) != 0
10148       || val < (mintiny << op->shift)
10149       || val > (maxtiny << op->shift))
10150     needext = TRUE;
10151   else
10152     needext = FALSE;
10153
10154   if (warn && ext && ! needext)
10155     as_warn_where (file, line,
10156                    _("extended operand requested but not required"));
10157   if (small && needext)
10158     as_bad_where (file, line, _("invalid unextended operand value"));
10159
10160   if (small || (! ext && ! needext))
10161     {
10162       int insnval;
10163
10164       *use_extend = FALSE;
10165       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10166       insnval <<= op->op_shift;
10167       *insn |= insnval;
10168     }
10169   else
10170     {
10171       long minext, maxext;
10172       int extval;
10173
10174       if (op->extu)
10175         {
10176           minext = 0;
10177           maxext = (1 << op->extbits) - 1;
10178         }
10179       else
10180         {
10181           minext = - (1 << (op->extbits - 1));
10182           maxext = (1 << (op->extbits - 1)) - 1;
10183         }
10184       if (val < minext || val > maxext)
10185         as_bad_where (file, line,
10186                       _("operand value out of range for instruction"));
10187
10188       *use_extend = TRUE;
10189       if (op->extbits == 16)
10190         {
10191           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10192           val &= 0x1f;
10193         }
10194       else if (op->extbits == 15)
10195         {
10196           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10197           val &= 0xf;
10198         }
10199       else
10200         {
10201           extval = ((val & 0x1f) << 6) | (val & 0x20);
10202           val = 0;
10203         }
10204
10205       *extend = (unsigned short) extval;
10206       *insn |= val;
10207     }
10208 }
10209 \f
10210 static const struct percent_op_match
10211 {
10212   const char *str;
10213   bfd_reloc_code_real_type reloc;
10214 } percent_op[] =
10215 {
10216   {"%lo", BFD_RELOC_LO16},
10217 #ifdef OBJ_ELF
10218   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10219   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10220   {"%call16", BFD_RELOC_MIPS_CALL16},
10221   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10222   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10223   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10224   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10225   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10226   {"%got", BFD_RELOC_MIPS_GOT16},
10227   {"%gp_rel", BFD_RELOC_GPREL16},
10228   {"%half", BFD_RELOC_16},
10229   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10230   {"%higher", BFD_RELOC_MIPS_HIGHER},
10231   {"%neg", BFD_RELOC_MIPS_SUB},
10232 #endif
10233   {"%hi", BFD_RELOC_HI16_S}
10234 };
10235
10236
10237 /* Return true if *STR points to a relocation operator.  When returning true,
10238    move *STR over the operator and store its relocation code in *RELOC.
10239    Leave both *STR and *RELOC alone when returning false.  */
10240
10241 static bfd_boolean
10242 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10243 {
10244   size_t i;
10245
10246   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10247     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10248       {
10249         *str += strlen (percent_op[i].str);
10250         *reloc = percent_op[i].reloc;
10251
10252         /* Check whether the output BFD supports this relocation.
10253            If not, issue an error and fall back on something safe.  */
10254         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10255           {
10256             as_bad ("relocation %s isn't supported by the current ABI",
10257                     percent_op[i].str);
10258             *reloc = BFD_RELOC_LO16;
10259           }
10260         return TRUE;
10261       }
10262   return FALSE;
10263 }
10264
10265
10266 /* Parse string STR as a 16-bit relocatable operand.  Store the
10267    expression in *EP and the relocations in the array starting
10268    at RELOC.  Return the number of relocation operators used.
10269
10270    On exit, EXPR_END points to the first character after the expression.
10271    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10272
10273 static size_t
10274 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10275                        char *str)
10276 {
10277   bfd_reloc_code_real_type reversed_reloc[3];
10278   size_t reloc_index, i;
10279   int crux_depth, str_depth;
10280   char *crux;
10281
10282   /* Search for the start of the main expression, recoding relocations
10283      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10284      of the main expression and with CRUX_DEPTH containing the number
10285      of open brackets at that point.  */
10286   reloc_index = -1;
10287   str_depth = 0;
10288   do
10289     {
10290       reloc_index++;
10291       crux = str;
10292       crux_depth = str_depth;
10293
10294       /* Skip over whitespace and brackets, keeping count of the number
10295          of brackets.  */
10296       while (*str == ' ' || *str == '\t' || *str == '(')
10297         if (*str++ == '(')
10298           str_depth++;
10299     }
10300   while (*str == '%'
10301          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10302          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10303
10304   my_getExpression (ep, crux);
10305   str = expr_end;
10306
10307   /* Match every open bracket.  */
10308   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10309     if (*str++ == ')')
10310       crux_depth--;
10311
10312   if (crux_depth > 0)
10313     as_bad ("unclosed '('");
10314
10315   expr_end = str;
10316
10317   if (reloc_index == 0)
10318     reloc[0] = BFD_RELOC_LO16;
10319   else
10320     {
10321       prev_reloc_op_frag = frag_now;
10322       for (i = 0; i < reloc_index; i++)
10323         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10324     }
10325
10326   return reloc_index;
10327 }
10328
10329 static void
10330 my_getExpression (expressionS *ep, char *str)
10331 {
10332   char *save_in;
10333   valueT val;
10334
10335   save_in = input_line_pointer;
10336   input_line_pointer = str;
10337   expression (ep);
10338   expr_end = input_line_pointer;
10339   input_line_pointer = save_in;
10340
10341   /* If we are in mips16 mode, and this is an expression based on `.',
10342      then we bump the value of the symbol by 1 since that is how other
10343      text symbols are handled.  We don't bother to handle complex
10344      expressions, just `.' plus or minus a constant.  */
10345   if (mips_opts.mips16
10346       && ep->X_op == O_symbol
10347       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10348       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10349       && symbol_get_frag (ep->X_add_symbol) == frag_now
10350       && symbol_constant_p (ep->X_add_symbol)
10351       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10352     S_SET_VALUE (ep->X_add_symbol, val + 1);
10353 }
10354
10355 /* Turn a string in input_line_pointer into a floating point constant
10356    of type TYPE, and store the appropriate bytes in *LITP.  The number
10357    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10358    returned, or NULL on OK.  */
10359
10360 char *
10361 md_atof (int type, char *litP, int *sizeP)
10362 {
10363   int prec;
10364   LITTLENUM_TYPE words[4];
10365   char *t;
10366   int i;
10367
10368   switch (type)
10369     {
10370     case 'f':
10371       prec = 2;
10372       break;
10373
10374     case 'd':
10375       prec = 4;
10376       break;
10377
10378     default:
10379       *sizeP = 0;
10380       return _("bad call to md_atof");
10381     }
10382
10383   t = atof_ieee (input_line_pointer, type, words);
10384   if (t)
10385     input_line_pointer = t;
10386
10387   *sizeP = prec * 2;
10388
10389   if (! target_big_endian)
10390     {
10391       for (i = prec - 1; i >= 0; i--)
10392         {
10393           md_number_to_chars (litP, words[i], 2);
10394           litP += 2;
10395         }
10396     }
10397   else
10398     {
10399       for (i = 0; i < prec; i++)
10400         {
10401           md_number_to_chars (litP, words[i], 2);
10402           litP += 2;
10403         }
10404     }
10405
10406   return NULL;
10407 }
10408
10409 void
10410 md_number_to_chars (char *buf, valueT val, int n)
10411 {
10412   if (target_big_endian)
10413     number_to_chars_bigendian (buf, val, n);
10414   else
10415     number_to_chars_littleendian (buf, val, n);
10416 }
10417 \f
10418 #ifdef OBJ_ELF
10419 static int support_64bit_objects(void)
10420 {
10421   const char **list, **l;
10422   int yes;
10423
10424   list = bfd_target_list ();
10425   for (l = list; *l != NULL; l++)
10426 #ifdef TE_TMIPS
10427     /* This is traditional mips */
10428     if (strcmp (*l, "elf64-tradbigmips") == 0
10429         || strcmp (*l, "elf64-tradlittlemips") == 0)
10430 #else
10431     if (strcmp (*l, "elf64-bigmips") == 0
10432         || strcmp (*l, "elf64-littlemips") == 0)
10433 #endif
10434       break;
10435   yes = (*l != NULL);
10436   free (list);
10437   return yes;
10438 }
10439 #endif /* OBJ_ELF */
10440
10441 const char *md_shortopts = "nO::g::G:";
10442
10443 struct option md_longopts[] =
10444 {
10445   /* Options which specify architecture.  */
10446 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
10447 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10448   {"march", required_argument, NULL, OPTION_MARCH},
10449 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10450   {"mtune", required_argument, NULL, OPTION_MTUNE},
10451 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10452   {"mips0", no_argument, NULL, OPTION_MIPS1},
10453   {"mips1", no_argument, NULL, OPTION_MIPS1},
10454 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10455   {"mips2", no_argument, NULL, OPTION_MIPS2},
10456 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10457   {"mips3", no_argument, NULL, OPTION_MIPS3},
10458 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10459   {"mips4", no_argument, NULL, OPTION_MIPS4},
10460 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10461   {"mips5", no_argument, NULL, OPTION_MIPS5},
10462 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10463   {"mips32", no_argument, NULL, OPTION_MIPS32},
10464 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10465   {"mips64", no_argument, NULL, OPTION_MIPS64},
10466 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10467   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10468 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10469   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10470
10471   /* Options which specify Application Specific Extensions (ASEs).  */
10472 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10473 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10474   {"mips16", no_argument, NULL, OPTION_MIPS16},
10475 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10476   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10477 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10478   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10479 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10480   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10481 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10482   {"mdmx", no_argument, NULL, OPTION_MDMX},
10483 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10484   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10485
10486   /* Old-style architecture options.  Don't add more of these.  */
10487 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10488 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10489   {"m4650", no_argument, NULL, OPTION_M4650},
10490 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10491   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10492 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10493   {"m4010", no_argument, NULL, OPTION_M4010},
10494 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10495   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10496 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10497   {"m4100", no_argument, NULL, OPTION_M4100},
10498 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10499   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10500 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10501   {"m3900", no_argument, NULL, OPTION_M3900},
10502 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10503   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10504
10505   /* Options which enable bug fixes.  */
10506 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10507 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10508   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10509 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10510   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10511   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10512 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10513 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10514   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10515   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10516
10517   /* Miscellaneous options.  */
10518 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10519 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10520   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10521 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10522   {"trap", no_argument, NULL, OPTION_TRAP},
10523   {"no-break", no_argument, NULL, OPTION_TRAP},
10524 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10525   {"break", no_argument, NULL, OPTION_BREAK},
10526   {"no-trap", no_argument, NULL, OPTION_BREAK},
10527 #define OPTION_EB (OPTION_MISC_BASE + 3)
10528   {"EB", no_argument, NULL, OPTION_EB},
10529 #define OPTION_EL (OPTION_MISC_BASE + 4)
10530   {"EL", no_argument, NULL, OPTION_EL},
10531 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10532   {"mfp32", no_argument, NULL, OPTION_FP32},
10533 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10534   {"mgp32", no_argument, NULL, OPTION_GP32},
10535 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10536   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10537 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10538   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10539 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10540   {"mfp64", no_argument, NULL, OPTION_FP64},
10541 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10542   {"mgp64", no_argument, NULL, OPTION_GP64},
10543 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10544 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10545   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10546   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10547
10548   /* ELF-specific options.  */
10549 #ifdef OBJ_ELF
10550 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 13)
10551 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10552   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10553   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10554 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10555   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10556 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10557   {"xgot",        no_argument, NULL, OPTION_XGOT},
10558 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10559   {"mabi", required_argument, NULL, OPTION_MABI},
10560 #define OPTION_32          (OPTION_ELF_BASE + 4)
10561   {"32",          no_argument, NULL, OPTION_32},
10562 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10563   {"n32",         no_argument, NULL, OPTION_N32},
10564 #define OPTION_64          (OPTION_ELF_BASE + 6)
10565   {"64",          no_argument, NULL, OPTION_64},
10566 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10567   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10568 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10569   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10570 #define OPTION_PDR         (OPTION_ELF_BASE + 9)
10571   {"mpdr", no_argument, NULL, OPTION_PDR},
10572 #define OPTION_NO_PDR      (OPTION_ELF_BASE + 10)
10573   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10574 #endif /* OBJ_ELF */
10575
10576   {NULL, no_argument, NULL, 0}
10577 };
10578 size_t md_longopts_size = sizeof (md_longopts);
10579
10580 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10581    NEW_VALUE.  Warn if another value was already specified.  Note:
10582    we have to defer parsing the -march and -mtune arguments in order
10583    to handle 'from-abi' correctly, since the ABI might be specified
10584    in a later argument.  */
10585
10586 static void
10587 mips_set_option_string (const char **string_ptr, const char *new_value)
10588 {
10589   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10590     as_warn (_("A different %s was already specified, is now %s"),
10591              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10592              new_value);
10593
10594   *string_ptr = new_value;
10595 }
10596
10597 int
10598 md_parse_option (int c, char *arg)
10599 {
10600   switch (c)
10601     {
10602     case OPTION_CONSTRUCT_FLOATS:
10603       mips_disable_float_construction = 0;
10604       break;
10605
10606     case OPTION_NO_CONSTRUCT_FLOATS:
10607       mips_disable_float_construction = 1;
10608       break;
10609
10610     case OPTION_TRAP:
10611       mips_trap = 1;
10612       break;
10613
10614     case OPTION_BREAK:
10615       mips_trap = 0;
10616       break;
10617
10618     case OPTION_EB:
10619       target_big_endian = 1;
10620       break;
10621
10622     case OPTION_EL:
10623       target_big_endian = 0;
10624       break;
10625
10626     case 'n':
10627       warn_nops = 1;
10628       break;
10629
10630     case 'O':
10631       if (arg && arg[1] == '0')
10632         mips_optimize = 1;
10633       else
10634         mips_optimize = 2;
10635       break;
10636
10637     case 'g':
10638       if (arg == NULL)
10639         mips_debug = 2;
10640       else
10641         mips_debug = atoi (arg);
10642       /* When the MIPS assembler sees -g or -g2, it does not do
10643          optimizations which limit full symbolic debugging.  We take
10644          that to be equivalent to -O0.  */
10645       if (mips_debug == 2)
10646         mips_optimize = 1;
10647       break;
10648
10649     case OPTION_MIPS1:
10650       file_mips_isa = ISA_MIPS1;
10651       break;
10652
10653     case OPTION_MIPS2:
10654       file_mips_isa = ISA_MIPS2;
10655       break;
10656
10657     case OPTION_MIPS3:
10658       file_mips_isa = ISA_MIPS3;
10659       break;
10660
10661     case OPTION_MIPS4:
10662       file_mips_isa = ISA_MIPS4;
10663       break;
10664
10665     case OPTION_MIPS5:
10666       file_mips_isa = ISA_MIPS5;
10667       break;
10668
10669     case OPTION_MIPS32:
10670       file_mips_isa = ISA_MIPS32;
10671       break;
10672
10673     case OPTION_MIPS32R2:
10674       file_mips_isa = ISA_MIPS32R2;
10675       break;
10676
10677     case OPTION_MIPS64R2:
10678       file_mips_isa = ISA_MIPS64R2;
10679       break;
10680
10681     case OPTION_MIPS64:
10682       file_mips_isa = ISA_MIPS64;
10683       break;
10684
10685     case OPTION_MTUNE:
10686       mips_set_option_string (&mips_tune_string, arg);
10687       break;
10688
10689     case OPTION_MARCH:
10690       mips_set_option_string (&mips_arch_string, arg);
10691       break;
10692
10693     case OPTION_M4650:
10694       mips_set_option_string (&mips_arch_string, "4650");
10695       mips_set_option_string (&mips_tune_string, "4650");
10696       break;
10697
10698     case OPTION_NO_M4650:
10699       break;
10700
10701     case OPTION_M4010:
10702       mips_set_option_string (&mips_arch_string, "4010");
10703       mips_set_option_string (&mips_tune_string, "4010");
10704       break;
10705
10706     case OPTION_NO_M4010:
10707       break;
10708
10709     case OPTION_M4100:
10710       mips_set_option_string (&mips_arch_string, "4100");
10711       mips_set_option_string (&mips_tune_string, "4100");
10712       break;
10713
10714     case OPTION_NO_M4100:
10715       break;
10716
10717     case OPTION_M3900:
10718       mips_set_option_string (&mips_arch_string, "3900");
10719       mips_set_option_string (&mips_tune_string, "3900");
10720       break;
10721
10722     case OPTION_NO_M3900:
10723       break;
10724
10725     case OPTION_MDMX:
10726       mips_opts.ase_mdmx = 1;
10727       break;
10728
10729     case OPTION_NO_MDMX:
10730       mips_opts.ase_mdmx = 0;
10731       break;
10732
10733     case OPTION_MIPS16:
10734       mips_opts.mips16 = 1;
10735       mips_no_prev_insn (FALSE);
10736       break;
10737
10738     case OPTION_NO_MIPS16:
10739       mips_opts.mips16 = 0;
10740       mips_no_prev_insn (FALSE);
10741       break;
10742
10743     case OPTION_MIPS3D:
10744       mips_opts.ase_mips3d = 1;
10745       break;
10746
10747     case OPTION_NO_MIPS3D:
10748       mips_opts.ase_mips3d = 0;
10749       break;
10750
10751     case OPTION_MEMBEDDED_PIC:
10752       mips_pic = EMBEDDED_PIC;
10753       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10754         {
10755           as_bad (_("-G may not be used with embedded PIC code"));
10756           return 0;
10757         }
10758       g_switch_value = 0x7fffffff;
10759       break;
10760
10761     case OPTION_FIX_VR4122:
10762       mips_fix_4122_bugs = 1;
10763       break;
10764
10765     case OPTION_NO_FIX_VR4122:
10766       mips_fix_4122_bugs = 0;
10767       break;
10768
10769     case OPTION_RELAX_BRANCH:
10770       mips_relax_branch = 1;
10771       break;
10772
10773     case OPTION_NO_RELAX_BRANCH:
10774       mips_relax_branch = 0;
10775       break;
10776
10777 #ifdef OBJ_ELF
10778       /* When generating ELF code, we permit -KPIC and -call_shared to
10779          select SVR4_PIC, and -non_shared to select no PIC.  This is
10780          intended to be compatible with Irix 5.  */
10781     case OPTION_CALL_SHARED:
10782       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10783         {
10784           as_bad (_("-call_shared is supported only for ELF format"));
10785           return 0;
10786         }
10787       mips_pic = SVR4_PIC;
10788       mips_abicalls = TRUE;
10789       if (g_switch_seen && g_switch_value != 0)
10790         {
10791           as_bad (_("-G may not be used with SVR4 PIC code"));
10792           return 0;
10793         }
10794       g_switch_value = 0;
10795       break;
10796
10797     case OPTION_NON_SHARED:
10798       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10799         {
10800           as_bad (_("-non_shared is supported only for ELF format"));
10801           return 0;
10802         }
10803       mips_pic = NO_PIC;
10804       mips_abicalls = FALSE;
10805       break;
10806
10807       /* The -xgot option tells the assembler to use 32 offsets when
10808          accessing the got in SVR4_PIC mode.  It is for Irix
10809          compatibility.  */
10810     case OPTION_XGOT:
10811       mips_big_got = 1;
10812       break;
10813 #endif /* OBJ_ELF */
10814
10815     case 'G':
10816       if (! USE_GLOBAL_POINTER_OPT)
10817         {
10818           as_bad (_("-G is not supported for this configuration"));
10819           return 0;
10820         }
10821       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10822         {
10823           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10824           return 0;
10825         }
10826       else
10827         g_switch_value = atoi (arg);
10828       g_switch_seen = 1;
10829       break;
10830
10831 #ifdef OBJ_ELF
10832       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10833          and -mabi=64.  */
10834     case OPTION_32:
10835       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10836         {
10837           as_bad (_("-32 is supported for ELF format only"));
10838           return 0;
10839         }
10840       mips_abi = O32_ABI;
10841       break;
10842
10843     case OPTION_N32:
10844       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10845         {
10846           as_bad (_("-n32 is supported for ELF format only"));
10847           return 0;
10848         }
10849       mips_abi = N32_ABI;
10850       break;
10851
10852     case OPTION_64:
10853       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10854         {
10855           as_bad (_("-64 is supported for ELF format only"));
10856           return 0;
10857         }
10858       mips_abi = N64_ABI;
10859       if (! support_64bit_objects())
10860         as_fatal (_("No compiled in support for 64 bit object file format"));
10861       break;
10862 #endif /* OBJ_ELF */
10863
10864     case OPTION_GP32:
10865       file_mips_gp32 = 1;
10866       break;
10867
10868     case OPTION_GP64:
10869       file_mips_gp32 = 0;
10870       break;
10871
10872     case OPTION_FP32:
10873       file_mips_fp32 = 1;
10874       break;
10875
10876     case OPTION_FP64:
10877       file_mips_fp32 = 0;
10878       break;
10879
10880 #ifdef OBJ_ELF
10881     case OPTION_MABI:
10882       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10883         {
10884           as_bad (_("-mabi is supported for ELF format only"));
10885           return 0;
10886         }
10887       if (strcmp (arg, "32") == 0)
10888         mips_abi = O32_ABI;
10889       else if (strcmp (arg, "o64") == 0)
10890         mips_abi = O64_ABI;
10891       else if (strcmp (arg, "n32") == 0)
10892         mips_abi = N32_ABI;
10893       else if (strcmp (arg, "64") == 0)
10894         {
10895           mips_abi = N64_ABI;
10896           if (! support_64bit_objects())
10897             as_fatal (_("No compiled in support for 64 bit object file "
10898                         "format"));
10899         }
10900       else if (strcmp (arg, "eabi") == 0)
10901         mips_abi = EABI_ABI;
10902       else
10903         {
10904           as_fatal (_("invalid abi -mabi=%s"), arg);
10905           return 0;
10906         }
10907       break;
10908 #endif /* OBJ_ELF */
10909
10910     case OPTION_M7000_HILO_FIX:
10911       mips_7000_hilo_fix = TRUE;
10912       break;
10913
10914     case OPTION_MNO_7000_HILO_FIX:
10915       mips_7000_hilo_fix = FALSE;
10916       break;
10917
10918 #ifdef OBJ_ELF
10919     case OPTION_MDEBUG:
10920       mips_flag_mdebug = TRUE;
10921       break;
10922
10923     case OPTION_NO_MDEBUG:
10924       mips_flag_mdebug = FALSE;
10925       break;
10926
10927     case OPTION_PDR:
10928       mips_flag_pdr = TRUE;
10929       break;
10930
10931     case OPTION_NO_PDR:
10932       mips_flag_pdr = FALSE;
10933       break;
10934 #endif /* OBJ_ELF */
10935
10936     default:
10937       return 0;
10938     }
10939
10940   return 1;
10941 }
10942 \f
10943 /* Set up globals to generate code for the ISA or processor
10944    described by INFO.  */
10945
10946 static void
10947 mips_set_architecture (const struct mips_cpu_info *info)
10948 {
10949   if (info != 0)
10950     {
10951       file_mips_arch = info->cpu;
10952       mips_opts.arch = info->cpu;
10953       mips_opts.isa = info->isa;
10954     }
10955 }
10956
10957
10958 /* Likewise for tuning.  */
10959
10960 static void
10961 mips_set_tune (const struct mips_cpu_info *info)
10962 {
10963   if (info != 0)
10964     mips_tune = info->cpu;
10965 }
10966
10967
10968 void
10969 mips_after_parse_args (void)
10970 {
10971   const struct mips_cpu_info *arch_info = 0;
10972   const struct mips_cpu_info *tune_info = 0;
10973
10974   /* GP relative stuff not working for PE */
10975   if (strncmp (TARGET_OS, "pe", 2) == 0
10976       && g_switch_value != 0)
10977     {
10978       if (g_switch_seen)
10979         as_bad (_("-G not supported in this configuration."));
10980       g_switch_value = 0;
10981     }
10982
10983   if (mips_abi == NO_ABI)
10984     mips_abi = MIPS_DEFAULT_ABI;
10985
10986   /* The following code determines the architecture and register size.
10987      Similar code was added to GCC 3.3 (see override_options() in
10988      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10989      as much as possible.  */
10990
10991   if (mips_arch_string != 0)
10992     arch_info = mips_parse_cpu ("-march", mips_arch_string);
10993
10994   if (file_mips_isa != ISA_UNKNOWN)
10995     {
10996       /* Handle -mipsN.  At this point, file_mips_isa contains the
10997          ISA level specified by -mipsN, while arch_info->isa contains
10998          the -march selection (if any).  */
10999       if (arch_info != 0)
11000         {
11001           /* -march takes precedence over -mipsN, since it is more descriptive.
11002              There's no harm in specifying both as long as the ISA levels
11003              are the same.  */
11004           if (file_mips_isa != arch_info->isa)
11005             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11006                     mips_cpu_info_from_isa (file_mips_isa)->name,
11007                     mips_cpu_info_from_isa (arch_info->isa)->name);
11008         }
11009       else
11010         arch_info = mips_cpu_info_from_isa (file_mips_isa);
11011     }
11012
11013   if (arch_info == 0)
11014     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11015
11016   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11017     as_bad ("-march=%s is not compatible with the selected ABI",
11018             arch_info->name);
11019
11020   mips_set_architecture (arch_info);
11021
11022   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
11023   if (mips_tune_string != 0)
11024     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11025
11026   if (tune_info == 0)
11027     mips_set_tune (arch_info);
11028   else
11029     mips_set_tune (tune_info);
11030
11031   if (file_mips_gp32 >= 0)
11032     {
11033       /* The user specified the size of the integer registers.  Make sure
11034          it agrees with the ABI and ISA.  */
11035       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11036         as_bad (_("-mgp64 used with a 32-bit processor"));
11037       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11038         as_bad (_("-mgp32 used with a 64-bit ABI"));
11039       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11040         as_bad (_("-mgp64 used with a 32-bit ABI"));
11041     }
11042   else
11043     {
11044       /* Infer the integer register size from the ABI and processor.
11045          Restrict ourselves to 32-bit registers if that's all the
11046          processor has, or if the ABI cannot handle 64-bit registers.  */
11047       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11048                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11049     }
11050
11051   /* ??? GAS treats single-float processors as though they had 64-bit
11052      float registers (although it complains when double-precision
11053      instructions are used).  As things stand, saying they have 32-bit
11054      registers would lead to spurious "register must be even" messages.
11055      So here we assume float registers are always the same size as
11056      integer ones, unless the user says otherwise.  */
11057   if (file_mips_fp32 < 0)
11058     file_mips_fp32 = file_mips_gp32;
11059
11060   /* End of GCC-shared inference code.  */
11061
11062   /* This flag is set when we have a 64-bit capable CPU but use only
11063      32-bit wide registers.  Note that EABI does not use it.  */
11064   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11065       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11066           || mips_abi == O32_ABI))
11067     mips_32bitmode = 1;
11068
11069   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11070     as_bad (_("trap exception not supported at ISA 1"));
11071
11072   /* If the selected architecture includes support for ASEs, enable
11073      generation of code for them.  */
11074   if (mips_opts.mips16 == -1)
11075     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11076   if (mips_opts.ase_mips3d == -1)
11077     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
11078   if (mips_opts.ase_mdmx == -1)
11079     mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
11080
11081   file_mips_isa = mips_opts.isa;
11082   file_ase_mips16 = mips_opts.mips16;
11083   file_ase_mips3d = mips_opts.ase_mips3d;
11084   file_ase_mdmx = mips_opts.ase_mdmx;
11085   mips_opts.gp32 = file_mips_gp32;
11086   mips_opts.fp32 = file_mips_fp32;
11087
11088   if (mips_flag_mdebug < 0)
11089     {
11090 #ifdef OBJ_MAYBE_ECOFF
11091       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11092         mips_flag_mdebug = 1;
11093       else
11094 #endif /* OBJ_MAYBE_ECOFF */
11095         mips_flag_mdebug = 0;
11096     }
11097 }
11098 \f
11099 void
11100 mips_init_after_args (void)
11101 {
11102   /* initialize opcodes */
11103   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11104   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11105 }
11106
11107 long
11108 md_pcrel_from (fixS *fixP)
11109 {
11110   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11111   switch (fixP->fx_r_type)
11112     {
11113     case BFD_RELOC_16_PCREL_S2:
11114     case BFD_RELOC_MIPS_JMP:
11115       /* Return the address of the delay slot.  */
11116       return addr + 4;
11117     default:
11118       return addr;
11119     }
11120 }
11121
11122 /* This is called before the symbol table is processed.  In order to
11123    work with gcc when using mips-tfile, we must keep all local labels.
11124    However, in other cases, we want to discard them.  If we were
11125    called with -g, but we didn't see any debugging information, it may
11126    mean that gcc is smuggling debugging information through to
11127    mips-tfile, in which case we must generate all local labels.  */
11128
11129 void
11130 mips_frob_file_before_adjust (void)
11131 {
11132 #ifndef NO_ECOFF_DEBUGGING
11133   if (ECOFF_DEBUGGING
11134       && mips_debug != 0
11135       && ! ecoff_debugging_seen)
11136     flag_keep_locals = 1;
11137 #endif
11138 }
11139
11140 /* Sort any unmatched HI16_S relocs so that they immediately precede
11141    the corresponding LO reloc.  This is called before md_apply_fix3 and
11142    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11143    explicit use of the %hi modifier.  */
11144
11145 void
11146 mips_frob_file (void)
11147 {
11148   struct mips_hi_fixup *l;
11149
11150   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11151     {
11152       segment_info_type *seginfo;
11153       int pass;
11154
11155       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11156
11157       /* If a GOT16 relocation turns out to be against a global symbol,
11158          there isn't supposed to be a matching LO.  */
11159       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11160           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11161         continue;
11162
11163       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11164       if (fixup_has_matching_lo_p (l->fixp))
11165         continue;
11166
11167       /* Look through the fixups for this segment for a matching %lo.
11168          When we find one, move the %hi just in front of it.  We do
11169          this in two passes.  In the first pass, we try to find a
11170          unique %lo.  In the second pass, we permit multiple %hi
11171          relocs for a single %lo (this is a GNU extension).  */
11172       seginfo = seg_info (l->seg);
11173       for (pass = 0; pass < 2; pass++)
11174         {
11175           fixS *f, *prev;
11176
11177           prev = NULL;
11178           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11179             {
11180               /* Check whether this is a %lo fixup which matches l->fixp.  */
11181               if (f->fx_r_type == BFD_RELOC_LO16
11182                   && f->fx_addsy == l->fixp->fx_addsy
11183                   && f->fx_offset == l->fixp->fx_offset
11184                   && (pass == 1
11185                       || prev == NULL
11186                       || !reloc_needs_lo_p (prev->fx_r_type)
11187                       || !fixup_has_matching_lo_p (prev)))
11188                 {
11189                   fixS **pf;
11190
11191                   /* Move l->fixp before f.  */
11192                   for (pf = &seginfo->fix_root;
11193                        *pf != l->fixp;
11194                        pf = &(*pf)->fx_next)
11195                     assert (*pf != NULL);
11196
11197                   *pf = l->fixp->fx_next;
11198
11199                   l->fixp->fx_next = f;
11200                   if (prev == NULL)
11201                     seginfo->fix_root = l->fixp;
11202                   else
11203                     prev->fx_next = l->fixp;
11204
11205                   break;
11206                 }
11207
11208               prev = f;
11209             }
11210
11211           if (f != NULL)
11212             break;
11213
11214 #if 0 /* GCC code motion plus incomplete dead code elimination
11215          can leave a %hi without a %lo.  */
11216           if (pass == 1)
11217             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11218                            _("Unmatched %%hi reloc"));
11219 #endif
11220         }
11221     }
11222 }
11223
11224 /* When generating embedded PIC code we need to use a special
11225    relocation to represent the difference of two symbols in the .text
11226    section (switch tables use a difference of this sort).  See
11227    include/coff/mips.h for details.  This macro checks whether this
11228    fixup requires the special reloc.  */
11229 #define SWITCH_TABLE(fixp) \
11230   ((fixp)->fx_r_type == BFD_RELOC_32 \
11231    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11232    && (fixp)->fx_addsy != NULL \
11233    && (fixp)->fx_subsy != NULL \
11234    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11235    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11236
11237 /* When generating embedded PIC code we must keep all PC relative
11238    relocations, in case the linker has to relax a call.  We also need
11239    to keep relocations for switch table entries.
11240
11241    We may have combined relocations without symbols in the N32/N64 ABI.
11242    We have to prevent gas from dropping them.  */
11243
11244 int
11245 mips_force_relocation (fixS *fixp)
11246 {
11247   if (generic_force_reloc (fixp))
11248     return 1;
11249
11250   if (HAVE_NEWABI
11251       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11252       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11253           || fixp->fx_r_type == BFD_RELOC_HI16_S
11254           || fixp->fx_r_type == BFD_RELOC_LO16))
11255     return 1;
11256
11257   return (mips_pic == EMBEDDED_PIC
11258           && (fixp->fx_pcrel
11259               || SWITCH_TABLE (fixp)
11260               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11261               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11262 }
11263
11264 /* This hook is called before a fix is simplified.  We don't really
11265    decide whether to skip a fix here.  Rather, we turn global symbols
11266    used as branch targets into local symbols, such that they undergo
11267    simplification.  We can only do this if the symbol is defined and
11268    it is in the same section as the branch.  If this doesn't hold, we
11269    emit a better error message than just saying the relocation is not
11270    valid for the selected object format.
11271
11272    FIXP is the fix-up we're going to try to simplify, SEG is the
11273    segment in which the fix up occurs.  The return value should be
11274    non-zero to indicate the fix-up is valid for further
11275    simplifications.  */
11276
11277 int
11278 mips_validate_fix (struct fix *fixP, asection *seg)
11279 {
11280   /* There's a lot of discussion on whether it should be possible to
11281      use R_MIPS_PC16 to represent branch relocations.  The outcome
11282      seems to be that it can, but gas/bfd are very broken in creating
11283      RELA relocations for this, so for now we only accept branches to
11284      symbols in the same section.  Anything else is of dubious value,
11285      since there's no guarantee that at link time the symbol would be
11286      in range.  Even for branches to local symbols this is arguably
11287      wrong, since it we assume the symbol is not going to be
11288      overridden, which should be possible per ELF library semantics,
11289      but then, there isn't a dynamic relocation that could be used to
11290      this effect, and the target would likely be out of range as well.
11291
11292      Unfortunately, it seems that there is too much code out there
11293      that relies on branches to symbols that are global to be resolved
11294      as if they were local, like the IRIX tools do, so we do it as
11295      well, but with a warning so that people are reminded to fix their
11296      code.  If we ever get back to using R_MIPS_PC16 for branch
11297      targets, this entire block should go away (and probably the
11298      whole function).  */
11299
11300   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11301       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11302             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11303            && mips_pic != EMBEDDED_PIC)
11304           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11305       && fixP->fx_addsy)
11306     {
11307       if (! S_IS_DEFINED (fixP->fx_addsy))
11308         {
11309           as_bad_where (fixP->fx_file, fixP->fx_line,
11310                         _("Cannot branch to undefined symbol."));
11311           /* Avoid any further errors about this fixup.  */
11312           fixP->fx_done = 1;
11313         }
11314       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11315         {
11316           as_bad_where (fixP->fx_file, fixP->fx_line,
11317                         _("Cannot branch to symbol in another section."));
11318           fixP->fx_done = 1;
11319         }
11320       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11321         {
11322           symbolS *sym = fixP->fx_addsy;
11323
11324           if (mips_pic == SVR4_PIC)
11325             as_warn_where (fixP->fx_file, fixP->fx_line,
11326                            _("Pretending global symbol used as branch target is local."));
11327
11328           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11329                                           S_GET_SEGMENT (sym),
11330                                           S_GET_VALUE (sym),
11331                                           symbol_get_frag (sym));
11332           copy_symbol_attributes (fixP->fx_addsy, sym);
11333           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11334           assert (symbol_resolved_p (sym));
11335           symbol_mark_resolved (fixP->fx_addsy);
11336         }
11337     }
11338
11339   return 1;
11340 }
11341
11342 /* Apply a fixup to the object file.  */
11343
11344 void
11345 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11346 {
11347   bfd_byte *buf;
11348   long insn;
11349   static int previous_fx_r_type = 0;
11350   reloc_howto_type *howto;
11351
11352   /* We ignore generic BFD relocations we don't know about.  */
11353   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11354   if (! howto)
11355     return;
11356
11357   assert (fixP->fx_size == 4
11358           || fixP->fx_r_type == BFD_RELOC_16
11359           || fixP->fx_r_type == BFD_RELOC_64
11360           || fixP->fx_r_type == BFD_RELOC_CTOR
11361           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11362           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11363           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11364
11365   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11366
11367   /* We are not done if this is a composite relocation to set up gp.  */
11368   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11369       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11370            || (fixP->fx_r_type == BFD_RELOC_64
11371                && (previous_fx_r_type == BFD_RELOC_GPREL32
11372                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11373            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11374                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11375                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11376     fixP->fx_done = 1;
11377   previous_fx_r_type = fixP->fx_r_type;
11378
11379   switch (fixP->fx_r_type)
11380     {
11381     case BFD_RELOC_MIPS_JMP:
11382     case BFD_RELOC_MIPS_SHIFT5:
11383     case BFD_RELOC_MIPS_SHIFT6:
11384     case BFD_RELOC_MIPS_GOT_DISP:
11385     case BFD_RELOC_MIPS_GOT_PAGE:
11386     case BFD_RELOC_MIPS_GOT_OFST:
11387     case BFD_RELOC_MIPS_SUB:
11388     case BFD_RELOC_MIPS_INSERT_A:
11389     case BFD_RELOC_MIPS_INSERT_B:
11390     case BFD_RELOC_MIPS_DELETE:
11391     case BFD_RELOC_MIPS_HIGHEST:
11392     case BFD_RELOC_MIPS_HIGHER:
11393     case BFD_RELOC_MIPS_SCN_DISP:
11394     case BFD_RELOC_MIPS_REL16:
11395     case BFD_RELOC_MIPS_RELGOT:
11396     case BFD_RELOC_MIPS_JALR:
11397     case BFD_RELOC_HI16:
11398     case BFD_RELOC_HI16_S:
11399     case BFD_RELOC_GPREL16:
11400     case BFD_RELOC_MIPS_LITERAL:
11401     case BFD_RELOC_MIPS_CALL16:
11402     case BFD_RELOC_MIPS_GOT16:
11403     case BFD_RELOC_GPREL32:
11404     case BFD_RELOC_MIPS_GOT_HI16:
11405     case BFD_RELOC_MIPS_GOT_LO16:
11406     case BFD_RELOC_MIPS_CALL_HI16:
11407     case BFD_RELOC_MIPS_CALL_LO16:
11408     case BFD_RELOC_MIPS16_GPREL:
11409       if (fixP->fx_pcrel)
11410         as_bad_where (fixP->fx_file, fixP->fx_line,
11411                       _("Invalid PC relative reloc"));
11412       /* Nothing needed to do. The value comes from the reloc entry */
11413       break;
11414
11415     case BFD_RELOC_MIPS16_JMP:
11416       /* We currently always generate a reloc against a symbol, which
11417          means that we don't want an addend even if the symbol is
11418          defined.  */
11419       *valP = 0;
11420       break;
11421
11422     case BFD_RELOC_PCREL_HI16_S:
11423       /* The addend for this is tricky if it is internal, so we just
11424          do everything here rather than in bfd_install_relocation.  */
11425       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11426         break;
11427       if (fixP->fx_addsy
11428           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11429         {
11430           /* For an external symbol adjust by the address to make it
11431              pcrel_offset.  We use the address of the RELLO reloc
11432              which follows this one.  */
11433           *valP += (fixP->fx_next->fx_frag->fr_address
11434                     + fixP->fx_next->fx_where);
11435         }
11436       *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11437       if (target_big_endian)
11438         buf += 2;
11439       md_number_to_chars (buf, *valP, 2);
11440       break;
11441
11442     case BFD_RELOC_PCREL_LO16:
11443       /* The addend for this is tricky if it is internal, so we just
11444          do everything here rather than in bfd_install_relocation.  */
11445       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11446         break;
11447       if (fixP->fx_addsy
11448           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11449         *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11450       if (target_big_endian)
11451         buf += 2;
11452       md_number_to_chars (buf, *valP, 2);
11453       break;
11454
11455     case BFD_RELOC_64:
11456       /* This is handled like BFD_RELOC_32, but we output a sign
11457          extended value if we are only 32 bits.  */
11458       if (fixP->fx_done
11459           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11460         {
11461           if (8 <= sizeof (valueT))
11462             md_number_to_chars (buf, *valP, 8);
11463           else
11464             {
11465               valueT hiv;
11466
11467               if ((*valP & 0x80000000) != 0)
11468                 hiv = 0xffffffff;
11469               else
11470                 hiv = 0;
11471               md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11472                                   *valP, 4);
11473               md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11474                                   hiv, 4);
11475             }
11476         }
11477       break;
11478
11479     case BFD_RELOC_RVA:
11480     case BFD_RELOC_32:
11481       /* If we are deleting this reloc entry, we must fill in the
11482          value now.  This can happen if we have a .word which is not
11483          resolved when it appears but is later defined.  We also need
11484          to fill in the value if this is an embedded PIC switch table
11485          entry.  */
11486       if (fixP->fx_done
11487           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11488         md_number_to_chars (buf, *valP, 4);
11489       break;
11490
11491     case BFD_RELOC_16:
11492       /* If we are deleting this reloc entry, we must fill in the
11493          value now.  */
11494       assert (fixP->fx_size == 2);
11495       if (fixP->fx_done)
11496         md_number_to_chars (buf, *valP, 2);
11497       break;
11498
11499     case BFD_RELOC_LO16:
11500       /* When handling an embedded PIC switch statement, we can wind
11501          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11502       if (fixP->fx_done)
11503         {
11504           if (*valP + 0x8000 > 0xffff)
11505             as_bad_where (fixP->fx_file, fixP->fx_line,
11506                           _("relocation overflow"));
11507           if (target_big_endian)
11508             buf += 2;
11509           md_number_to_chars (buf, *valP, 2);
11510         }
11511       break;
11512
11513     case BFD_RELOC_16_PCREL_S2:
11514       if ((*valP & 0x3) != 0)
11515         as_bad_where (fixP->fx_file, fixP->fx_line,
11516                       _("Branch to odd address (%lx)"), (long) *valP);
11517
11518       /*
11519        * We need to save the bits in the instruction since fixup_segment()
11520        * might be deleting the relocation entry (i.e., a branch within
11521        * the current segment).
11522        */
11523       if (! fixP->fx_done)
11524         break;
11525
11526       /* update old instruction data */
11527       if (target_big_endian)
11528         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11529       else
11530         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11531
11532       if (*valP + 0x20000 <= 0x3ffff)
11533         {
11534           insn |= (*valP >> 2) & 0xffff;
11535           md_number_to_chars (buf, insn, 4);
11536         }
11537       else if (mips_pic == NO_PIC
11538                && fixP->fx_done
11539                && fixP->fx_frag->fr_address >= text_section->vma
11540                && (fixP->fx_frag->fr_address
11541                    < text_section->vma + text_section->_raw_size)
11542                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
11543                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
11544                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11545         {
11546           /* The branch offset is too large.  If this is an
11547              unconditional branch, and we are not generating PIC code,
11548              we can convert it to an absolute jump instruction.  */
11549           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
11550             insn = 0x0c000000;  /* jal */
11551           else
11552             insn = 0x08000000;  /* j */
11553           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11554           fixP->fx_done = 0;
11555           fixP->fx_addsy = section_symbol (text_section);
11556           *valP += md_pcrel_from (fixP);
11557           md_number_to_chars (buf, insn, 4);
11558         }
11559       else
11560         {
11561           /* If we got here, we have branch-relaxation disabled,
11562              and there's nothing we can do to fix this instruction
11563              without turning it into a longer sequence.  */
11564           as_bad_where (fixP->fx_file, fixP->fx_line,
11565                         _("Branch out of range"));
11566         }
11567       break;
11568
11569     case BFD_RELOC_VTABLE_INHERIT:
11570       fixP->fx_done = 0;
11571       if (fixP->fx_addsy
11572           && !S_IS_DEFINED (fixP->fx_addsy)
11573           && !S_IS_WEAK (fixP->fx_addsy))
11574         S_SET_WEAK (fixP->fx_addsy);
11575       break;
11576
11577     case BFD_RELOC_VTABLE_ENTRY:
11578       fixP->fx_done = 0;
11579       break;
11580
11581     default:
11582       internalError ();
11583     }
11584
11585   /* Remember value for tc_gen_reloc.  */
11586   fixP->fx_addnumber = *valP;
11587 }
11588
11589 #if 0
11590 void
11591 printInsn (unsigned long oc)
11592 {
11593   const struct mips_opcode *p;
11594   int treg, sreg, dreg, shamt;
11595   short imm;
11596   const char *args;
11597   int i;
11598
11599   for (i = 0; i < NUMOPCODES; ++i)
11600     {
11601       p = &mips_opcodes[i];
11602       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11603         {
11604           printf ("%08lx %s\t", oc, p->name);
11605           treg = (oc >> 16) & 0x1f;
11606           sreg = (oc >> 21) & 0x1f;
11607           dreg = (oc >> 11) & 0x1f;
11608           shamt = (oc >> 6) & 0x1f;
11609           imm = oc;
11610           for (args = p->args;; ++args)
11611             {
11612               switch (*args)
11613                 {
11614                 case '\0':
11615                   printf ("\n");
11616                   break;
11617
11618                 case ',':
11619                 case '(':
11620                 case ')':
11621                   printf ("%c", *args);
11622                   continue;
11623
11624                 case 'r':
11625                   assert (treg == sreg);
11626                   printf ("$%d,$%d", treg, sreg);
11627                   continue;
11628
11629                 case 'd':
11630                 case 'G':
11631                   printf ("$%d", dreg);
11632                   continue;
11633
11634                 case 't':
11635                 case 'E':
11636                   printf ("$%d", treg);
11637                   continue;
11638
11639                 case 'k':
11640                   printf ("0x%x", treg);
11641                   continue;
11642
11643                 case 'b':
11644                 case 's':
11645                   printf ("$%d", sreg);
11646                   continue;
11647
11648                 case 'a':
11649                   printf ("0x%08lx", oc & 0x1ffffff);
11650                   continue;
11651
11652                 case 'i':
11653                 case 'j':
11654                 case 'o':
11655                 case 'u':
11656                   printf ("%d", imm);
11657                   continue;
11658
11659                 case '<':
11660                 case '>':
11661                   printf ("$%d", shamt);
11662                   continue;
11663
11664                 default:
11665                   internalError ();
11666                 }
11667               break;
11668             }
11669           return;
11670         }
11671     }
11672   printf (_("%08lx  UNDEFINED\n"), oc);
11673 }
11674 #endif
11675
11676 static symbolS *
11677 get_symbol (void)
11678 {
11679   int c;
11680   char *name;
11681   symbolS *p;
11682
11683   name = input_line_pointer;
11684   c = get_symbol_end ();
11685   p = (symbolS *) symbol_find_or_make (name);
11686   *input_line_pointer = c;
11687   return p;
11688 }
11689
11690 /* Align the current frag to a given power of two.  The MIPS assembler
11691    also automatically adjusts any preceding label.  */
11692
11693 static void
11694 mips_align (int to, int fill, symbolS *label)
11695 {
11696   mips_emit_delays (FALSE);
11697   frag_align (to, fill, 0);
11698   record_alignment (now_seg, to);
11699   if (label != NULL)
11700     {
11701       assert (S_GET_SEGMENT (label) == now_seg);
11702       symbol_set_frag (label, frag_now);
11703       S_SET_VALUE (label, (valueT) frag_now_fix ());
11704     }
11705 }
11706
11707 /* Align to a given power of two.  .align 0 turns off the automatic
11708    alignment used by the data creating pseudo-ops.  */
11709
11710 static void
11711 s_align (int x ATTRIBUTE_UNUSED)
11712 {
11713   register int temp;
11714   register long temp_fill;
11715   long max_alignment = 15;
11716
11717   /*
11718
11719     o  Note that the assembler pulls down any immediately preceding label
11720        to the aligned address.
11721     o  It's not documented but auto alignment is reinstated by
11722        a .align pseudo instruction.
11723     o  Note also that after auto alignment is turned off the mips assembler
11724        issues an error on attempt to assemble an improperly aligned data item.
11725        We don't.
11726
11727     */
11728
11729   temp = get_absolute_expression ();
11730   if (temp > max_alignment)
11731     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11732   else if (temp < 0)
11733     {
11734       as_warn (_("Alignment negative: 0 assumed."));
11735       temp = 0;
11736     }
11737   if (*input_line_pointer == ',')
11738     {
11739       ++input_line_pointer;
11740       temp_fill = get_absolute_expression ();
11741     }
11742   else
11743     temp_fill = 0;
11744   if (temp)
11745     {
11746       auto_align = 1;
11747       mips_align (temp, (int) temp_fill,
11748                   insn_labels != NULL ? insn_labels->label : NULL);
11749     }
11750   else
11751     {
11752       auto_align = 0;
11753     }
11754
11755   demand_empty_rest_of_line ();
11756 }
11757
11758 void
11759 mips_flush_pending_output (void)
11760 {
11761   mips_emit_delays (FALSE);
11762   mips_clear_insn_labels ();
11763 }
11764
11765 static void
11766 s_change_sec (int sec)
11767 {
11768   segT seg;
11769
11770   /* When generating embedded PIC code, we only use the .text, .lit8,
11771      .sdata and .sbss sections.  We change the .data and .rdata
11772      pseudo-ops to use .sdata.  */
11773   if (mips_pic == EMBEDDED_PIC
11774       && (sec == 'd' || sec == 'r'))
11775     sec = 's';
11776
11777 #ifdef OBJ_ELF
11778   /* The ELF backend needs to know that we are changing sections, so
11779      that .previous works correctly.  We could do something like check
11780      for an obj_section_change_hook macro, but that might be confusing
11781      as it would not be appropriate to use it in the section changing
11782      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11783      This should be cleaner, somehow.  */
11784   obj_elf_section_change_hook ();
11785 #endif
11786
11787   mips_emit_delays (FALSE);
11788   switch (sec)
11789     {
11790     case 't':
11791       s_text (0);
11792       break;
11793     case 'd':
11794       s_data (0);
11795       break;
11796     case 'b':
11797       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11798       demand_empty_rest_of_line ();
11799       break;
11800
11801     case 'r':
11802       if (USE_GLOBAL_POINTER_OPT)
11803         {
11804           seg = subseg_new (RDATA_SECTION_NAME,
11805                             (subsegT) get_absolute_expression ());
11806           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11807             {
11808               bfd_set_section_flags (stdoutput, seg,
11809                                      (SEC_ALLOC
11810                                       | SEC_LOAD
11811                                       | SEC_READONLY
11812                                       | SEC_RELOC
11813                                       | SEC_DATA));
11814               if (strcmp (TARGET_OS, "elf") != 0)
11815                 record_alignment (seg, 4);
11816             }
11817           demand_empty_rest_of_line ();
11818         }
11819       else
11820         {
11821           as_bad (_("No read only data section in this object file format"));
11822           demand_empty_rest_of_line ();
11823           return;
11824         }
11825       break;
11826
11827     case 's':
11828       if (USE_GLOBAL_POINTER_OPT)
11829         {
11830           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11831           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11832             {
11833               bfd_set_section_flags (stdoutput, seg,
11834                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11835                                      | SEC_DATA);
11836               if (strcmp (TARGET_OS, "elf") != 0)
11837                 record_alignment (seg, 4);
11838             }
11839           demand_empty_rest_of_line ();
11840           break;
11841         }
11842       else
11843         {
11844           as_bad (_("Global pointers not supported; recompile -G 0"));
11845           demand_empty_rest_of_line ();
11846           return;
11847         }
11848     }
11849
11850   auto_align = 1;
11851 }
11852
11853 void
11854 s_change_section (int ignore ATTRIBUTE_UNUSED)
11855 {
11856 #ifdef OBJ_ELF
11857   char *section_name;
11858   char c;
11859   char next_c = 0;
11860   int section_type;
11861   int section_flag;
11862   int section_entry_size;
11863   int section_alignment;
11864
11865   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11866     return;
11867
11868   section_name = input_line_pointer;
11869   c = get_symbol_end ();
11870   if (c)
11871     next_c = *(input_line_pointer + 1);
11872
11873   /* Do we have .section Name<,"flags">?  */
11874   if (c != ',' || (c == ',' && next_c == '"'))
11875     {
11876       /* just after name is now '\0'.  */
11877       *input_line_pointer = c;
11878       input_line_pointer = section_name;
11879       obj_elf_section (ignore);
11880       return;
11881     }
11882   input_line_pointer++;
11883
11884   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11885   if (c == ',')
11886     section_type = get_absolute_expression ();
11887   else
11888     section_type = 0;
11889   if (*input_line_pointer++ == ',')
11890     section_flag = get_absolute_expression ();
11891   else
11892     section_flag = 0;
11893   if (*input_line_pointer++ == ',')
11894     section_entry_size = get_absolute_expression ();
11895   else
11896     section_entry_size = 0;
11897   if (*input_line_pointer++ == ',')
11898     section_alignment = get_absolute_expression ();
11899   else
11900     section_alignment = 0;
11901
11902   section_name = xstrdup (section_name);
11903
11904   /* When using the generic form of .section (as implemented by obj-elf.c),
11905      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
11906      traditionally had to fall back on the more common @progbits instead.
11907
11908      There's nothing really harmful in this, since bfd will correct
11909      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
11910      means that, for backwards compatibiltiy, the special_section entries
11911      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11912
11913      Even so, we shouldn't force users of the MIPS .section syntax to
11914      incorrectly label the sections as SHT_PROGBITS.  The best compromise
11915      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11916      generic type-checking code.  */
11917   if (section_type == SHT_MIPS_DWARF)
11918     section_type = SHT_PROGBITS;
11919
11920   obj_elf_change_section (section_name, section_type, section_flag,
11921                           section_entry_size, 0, 0, 0);
11922
11923   if (now_seg->name != section_name)
11924     free (section_name);
11925 #endif /* OBJ_ELF */
11926 }
11927
11928 void
11929 mips_enable_auto_align (void)
11930 {
11931   auto_align = 1;
11932 }
11933
11934 static void
11935 s_cons (int log_size)
11936 {
11937   symbolS *label;
11938
11939   label = insn_labels != NULL ? insn_labels->label : NULL;
11940   mips_emit_delays (FALSE);
11941   if (log_size > 0 && auto_align)
11942     mips_align (log_size, 0, label);
11943   mips_clear_insn_labels ();
11944   cons (1 << log_size);
11945 }
11946
11947 static void
11948 s_float_cons (int type)
11949 {
11950   symbolS *label;
11951
11952   label = insn_labels != NULL ? insn_labels->label : NULL;
11953
11954   mips_emit_delays (FALSE);
11955
11956   if (auto_align)
11957     {
11958       if (type == 'd')
11959         mips_align (3, 0, label);
11960       else
11961         mips_align (2, 0, label);
11962     }
11963
11964   mips_clear_insn_labels ();
11965
11966   float_cons (type);
11967 }
11968
11969 /* Handle .globl.  We need to override it because on Irix 5 you are
11970    permitted to say
11971        .globl foo .text
11972    where foo is an undefined symbol, to mean that foo should be
11973    considered to be the address of a function.  */
11974
11975 static void
11976 s_mips_globl (int x ATTRIBUTE_UNUSED)
11977 {
11978   char *name;
11979   int c;
11980   symbolS *symbolP;
11981   flagword flag;
11982
11983   name = input_line_pointer;
11984   c = get_symbol_end ();
11985   symbolP = symbol_find_or_make (name);
11986   *input_line_pointer = c;
11987   SKIP_WHITESPACE ();
11988
11989   /* On Irix 5, every global symbol that is not explicitly labelled as
11990      being a function is apparently labelled as being an object.  */
11991   flag = BSF_OBJECT;
11992
11993   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11994     {
11995       char *secname;
11996       asection *sec;
11997
11998       secname = input_line_pointer;
11999       c = get_symbol_end ();
12000       sec = bfd_get_section_by_name (stdoutput, secname);
12001       if (sec == NULL)
12002         as_bad (_("%s: no such section"), secname);
12003       *input_line_pointer = c;
12004
12005       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12006         flag = BSF_FUNCTION;
12007     }
12008
12009   symbol_get_bfdsym (symbolP)->flags |= flag;
12010
12011   S_SET_EXTERNAL (symbolP);
12012   demand_empty_rest_of_line ();
12013 }
12014
12015 static void
12016 s_option (int x ATTRIBUTE_UNUSED)
12017 {
12018   char *opt;
12019   char c;
12020
12021   opt = input_line_pointer;
12022   c = get_symbol_end ();
12023
12024   if (*opt == 'O')
12025     {
12026       /* FIXME: What does this mean?  */
12027     }
12028   else if (strncmp (opt, "pic", 3) == 0)
12029     {
12030       int i;
12031
12032       i = atoi (opt + 3);
12033       if (i == 0)
12034         mips_pic = NO_PIC;
12035       else if (i == 2)
12036         {
12037         mips_pic = SVR4_PIC;
12038           mips_abicalls = TRUE;
12039         }
12040       else
12041         as_bad (_(".option pic%d not supported"), i);
12042
12043       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12044         {
12045           if (g_switch_seen && g_switch_value != 0)
12046             as_warn (_("-G may not be used with SVR4 PIC code"));
12047           g_switch_value = 0;
12048           bfd_set_gp_size (stdoutput, 0);
12049         }
12050     }
12051   else
12052     as_warn (_("Unrecognized option \"%s\""), opt);
12053
12054   *input_line_pointer = c;
12055   demand_empty_rest_of_line ();
12056 }
12057
12058 /* This structure is used to hold a stack of .set values.  */
12059
12060 struct mips_option_stack
12061 {
12062   struct mips_option_stack *next;
12063   struct mips_set_options options;
12064 };
12065
12066 static struct mips_option_stack *mips_opts_stack;
12067
12068 /* Handle the .set pseudo-op.  */
12069
12070 static void
12071 s_mipsset (int x ATTRIBUTE_UNUSED)
12072 {
12073   char *name = input_line_pointer, ch;
12074
12075   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12076     ++input_line_pointer;
12077   ch = *input_line_pointer;
12078   *input_line_pointer = '\0';
12079
12080   if (strcmp (name, "reorder") == 0)
12081     {
12082       if (mips_opts.noreorder && prev_nop_frag != NULL)
12083         {
12084           /* If we still have pending nops, we can discard them.  The
12085              usual nop handling will insert any that are still
12086              needed.  */
12087           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12088                                     * (mips_opts.mips16 ? 2 : 4));
12089           prev_nop_frag = NULL;
12090         }
12091       mips_opts.noreorder = 0;
12092     }
12093   else if (strcmp (name, "noreorder") == 0)
12094     {
12095       mips_emit_delays (TRUE);
12096       mips_opts.noreorder = 1;
12097       mips_any_noreorder = 1;
12098     }
12099   else if (strcmp (name, "at") == 0)
12100     {
12101       mips_opts.noat = 0;
12102     }
12103   else if (strcmp (name, "noat") == 0)
12104     {
12105       mips_opts.noat = 1;
12106     }
12107   else if (strcmp (name, "macro") == 0)
12108     {
12109       mips_opts.warn_about_macros = 0;
12110     }
12111   else if (strcmp (name, "nomacro") == 0)
12112     {
12113       if (mips_opts.noreorder == 0)
12114         as_bad (_("`noreorder' must be set before `nomacro'"));
12115       mips_opts.warn_about_macros = 1;
12116     }
12117   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12118     {
12119       mips_opts.nomove = 0;
12120     }
12121   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12122     {
12123       mips_opts.nomove = 1;
12124     }
12125   else if (strcmp (name, "bopt") == 0)
12126     {
12127       mips_opts.nobopt = 0;
12128     }
12129   else if (strcmp (name, "nobopt") == 0)
12130     {
12131       mips_opts.nobopt = 1;
12132     }
12133   else if (strcmp (name, "mips16") == 0
12134            || strcmp (name, "MIPS-16") == 0)
12135     mips_opts.mips16 = 1;
12136   else if (strcmp (name, "nomips16") == 0
12137            || strcmp (name, "noMIPS-16") == 0)
12138     mips_opts.mips16 = 0;
12139   else if (strcmp (name, "mips3d") == 0)
12140     mips_opts.ase_mips3d = 1;
12141   else if (strcmp (name, "nomips3d") == 0)
12142     mips_opts.ase_mips3d = 0;
12143   else if (strcmp (name, "mdmx") == 0)
12144     mips_opts.ase_mdmx = 1;
12145   else if (strcmp (name, "nomdmx") == 0)
12146     mips_opts.ase_mdmx = 0;
12147   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12148     {
12149       int reset = 0;
12150
12151       /* Permit the user to change the ISA and architecture on the fly.
12152          Needless to say, misuse can cause serious problems.  */
12153       if (strcmp (name, "mips0") == 0)
12154         {
12155           reset = 1;
12156           mips_opts.isa = file_mips_isa;
12157         }
12158       else if (strcmp (name, "mips1") == 0)
12159         mips_opts.isa = ISA_MIPS1;
12160       else if (strcmp (name, "mips2") == 0)
12161         mips_opts.isa = ISA_MIPS2;
12162       else if (strcmp (name, "mips3") == 0)
12163         mips_opts.isa = ISA_MIPS3;
12164       else if (strcmp (name, "mips4") == 0)
12165         mips_opts.isa = ISA_MIPS4;
12166       else if (strcmp (name, "mips5") == 0)
12167         mips_opts.isa = ISA_MIPS5;
12168       else if (strcmp (name, "mips32") == 0)
12169         mips_opts.isa = ISA_MIPS32;
12170       else if (strcmp (name, "mips32r2") == 0)
12171         mips_opts.isa = ISA_MIPS32R2;
12172       else if (strcmp (name, "mips64") == 0)
12173         mips_opts.isa = ISA_MIPS64;
12174       else if (strcmp (name, "mips64r2") == 0)
12175         mips_opts.isa = ISA_MIPS64R2;
12176       else if (strcmp (name, "arch=default") == 0)
12177         {
12178           reset = 1;
12179           mips_opts.arch = file_mips_arch;
12180           mips_opts.isa = file_mips_isa;
12181         }
12182       else if (strncmp (name, "arch=", 5) == 0)
12183         {
12184           const struct mips_cpu_info *p;
12185
12186           p = mips_parse_cpu("internal use", name + 5);
12187           if (!p)
12188             as_bad (_("unknown architecture %s"), name + 5);
12189           else
12190             {
12191               mips_opts.arch = p->cpu;
12192               mips_opts.isa = p->isa;
12193             }
12194         }
12195       else
12196         as_bad (_("unknown ISA level %s"), name + 4);
12197
12198       switch (mips_opts.isa)
12199         {
12200         case  0:
12201           break;
12202         case ISA_MIPS1:
12203         case ISA_MIPS2:
12204         case ISA_MIPS32:
12205         case ISA_MIPS32R2:
12206           mips_opts.gp32 = 1;
12207           mips_opts.fp32 = 1;
12208           break;
12209         case ISA_MIPS3:
12210         case ISA_MIPS4:
12211         case ISA_MIPS5:
12212         case ISA_MIPS64:
12213         case ISA_MIPS64R2:
12214           mips_opts.gp32 = 0;
12215           mips_opts.fp32 = 0;
12216           break;
12217         default:
12218           as_bad (_("unknown ISA level %s"), name + 4);
12219           break;
12220         }
12221       if (reset)
12222         {
12223           mips_opts.gp32 = file_mips_gp32;
12224           mips_opts.fp32 = file_mips_fp32;
12225         }
12226     }
12227   else if (strcmp (name, "autoextend") == 0)
12228     mips_opts.noautoextend = 0;
12229   else if (strcmp (name, "noautoextend") == 0)
12230     mips_opts.noautoextend = 1;
12231   else if (strcmp (name, "push") == 0)
12232     {
12233       struct mips_option_stack *s;
12234
12235       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12236       s->next = mips_opts_stack;
12237       s->options = mips_opts;
12238       mips_opts_stack = s;
12239     }
12240   else if (strcmp (name, "pop") == 0)
12241     {
12242       struct mips_option_stack *s;
12243
12244       s = mips_opts_stack;
12245       if (s == NULL)
12246         as_bad (_(".set pop with no .set push"));
12247       else
12248         {
12249           /* If we're changing the reorder mode we need to handle
12250              delay slots correctly.  */
12251           if (s->options.noreorder && ! mips_opts.noreorder)
12252             mips_emit_delays (TRUE);
12253           else if (! s->options.noreorder && mips_opts.noreorder)
12254             {
12255               if (prev_nop_frag != NULL)
12256                 {
12257                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12258                                             * (mips_opts.mips16 ? 2 : 4));
12259                   prev_nop_frag = NULL;
12260                 }
12261             }
12262
12263           mips_opts = s->options;
12264           mips_opts_stack = s->next;
12265           free (s);
12266         }
12267     }
12268   else
12269     {
12270       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12271     }
12272   *input_line_pointer = ch;
12273   demand_empty_rest_of_line ();
12274 }
12275
12276 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12277    .option pic2.  It means to generate SVR4 PIC calls.  */
12278
12279 static void
12280 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12281 {
12282   mips_pic = SVR4_PIC;
12283   mips_abicalls = TRUE;
12284   if (USE_GLOBAL_POINTER_OPT)
12285     {
12286       if (g_switch_seen && g_switch_value != 0)
12287         as_warn (_("-G may not be used with SVR4 PIC code"));
12288       g_switch_value = 0;
12289     }
12290   bfd_set_gp_size (stdoutput, 0);
12291   demand_empty_rest_of_line ();
12292 }
12293
12294 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12295    PIC code.  It sets the $gp register for the function based on the
12296    function address, which is in the register named in the argument.
12297    This uses a relocation against _gp_disp, which is handled specially
12298    by the linker.  The result is:
12299         lui     $gp,%hi(_gp_disp)
12300         addiu   $gp,$gp,%lo(_gp_disp)
12301         addu    $gp,$gp,.cpload argument
12302    The .cpload argument is normally $25 == $t9.  */
12303
12304 static void
12305 s_cpload (int ignore ATTRIBUTE_UNUSED)
12306 {
12307   expressionS ex;
12308   int icnt = 0;
12309
12310   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12311      .cpload is ignored.  */
12312   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12313     {
12314       s_ignore (0);
12315       return;
12316     }
12317
12318   /* .cpload should be in a .set noreorder section.  */
12319   if (mips_opts.noreorder == 0)
12320     as_warn (_(".cpload not in noreorder section"));
12321
12322   ex.X_op = O_symbol;
12323   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12324   ex.X_op_symbol = NULL;
12325   ex.X_add_number = 0;
12326
12327   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12328   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12329
12330   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12331   macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12332                mips_gp_register, BFD_RELOC_LO16);
12333
12334   macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12335                mips_gp_register, tc_get_register (0));
12336
12337   demand_empty_rest_of_line ();
12338 }
12339
12340 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12341      .cpsetup $reg1, offset|$reg2, label
12342
12343    If offset is given, this results in:
12344      sd         $gp, offset($sp)
12345      lui        $gp, %hi(%neg(%gp_rel(label)))
12346      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12347      daddu      $gp, $gp, $reg1
12348
12349    If $reg2 is given, this results in:
12350      daddu      $reg2, $gp, $0
12351      lui        $gp, %hi(%neg(%gp_rel(label)))
12352      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12353      daddu      $gp, $gp, $reg1
12354    $reg1 is normally $25 == $t9.  */
12355 static void
12356 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12357 {
12358   expressionS ex_off;
12359   expressionS ex_sym;
12360   int reg1;
12361   int icnt = 0;
12362   char *f;
12363
12364   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12365      We also need NewABI support.  */
12366   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12367     {
12368       s_ignore (0);
12369       return;
12370     }
12371
12372   reg1 = tc_get_register (0);
12373   SKIP_WHITESPACE ();
12374   if (*input_line_pointer != ',')
12375     {
12376       as_bad (_("missing argument separator ',' for .cpsetup"));
12377       return;
12378     }
12379   else
12380     ++input_line_pointer;
12381   SKIP_WHITESPACE ();
12382   if (*input_line_pointer == '$')
12383     {
12384       mips_cpreturn_register = tc_get_register (0);
12385       mips_cpreturn_offset = -1;
12386     }
12387   else
12388     {
12389       mips_cpreturn_offset = get_absolute_expression ();
12390       mips_cpreturn_register = -1;
12391     }
12392   SKIP_WHITESPACE ();
12393   if (*input_line_pointer != ',')
12394     {
12395       as_bad (_("missing argument separator ',' for .cpsetup"));
12396       return;
12397     }
12398   else
12399     ++input_line_pointer;
12400   SKIP_WHITESPACE ();
12401   expression (&ex_sym);
12402
12403   if (mips_cpreturn_register == -1)
12404     {
12405       ex_off.X_op = O_constant;
12406       ex_off.X_add_symbol = NULL;
12407       ex_off.X_op_symbol = NULL;
12408       ex_off.X_add_number = mips_cpreturn_offset;
12409
12410       macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12411                    BFD_RELOC_LO16, SP);
12412     }
12413   else
12414     macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12415                  mips_gp_register, 0);
12416
12417   /* Ensure there's room for the next two instructions, so that `f'
12418      doesn't end up with an address in the wrong frag.  */
12419   frag_grow (8);
12420   f = frag_more (0);
12421   macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12422                BFD_RELOC_GPREL16);
12423   fix_new (frag_now, f - frag_now->fr_literal,
12424            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12425   fix_new (frag_now, f - frag_now->fr_literal,
12426            4, NULL, 0, 0, BFD_RELOC_HI16_S);
12427
12428   f = frag_more (0);
12429   macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12430                mips_gp_register, BFD_RELOC_GPREL16);
12431   fix_new (frag_now, f - frag_now->fr_literal,
12432            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12433   fix_new (frag_now, f - frag_now->fr_literal,
12434            4, NULL, 0, 0, BFD_RELOC_LO16);
12435
12436   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12437                mips_gp_register, reg1);
12438
12439   demand_empty_rest_of_line ();
12440 }
12441
12442 static void
12443 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12444 {
12445   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12446    .cplocal is ignored.  */
12447   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12448     {
12449       s_ignore (0);
12450       return;
12451     }
12452
12453   mips_gp_register = tc_get_register (0);
12454   demand_empty_rest_of_line ();
12455 }
12456
12457 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12458    offset from $sp.  The offset is remembered, and after making a PIC
12459    call $gp is restored from that location.  */
12460
12461 static void
12462 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12463 {
12464   expressionS ex;
12465   int icnt = 0;
12466
12467   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12468      .cprestore is ignored.  */
12469   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12470     {
12471       s_ignore (0);
12472       return;
12473     }
12474
12475   mips_cprestore_offset = get_absolute_expression ();
12476   mips_cprestore_valid = 1;
12477
12478   ex.X_op = O_constant;
12479   ex.X_add_symbol = NULL;
12480   ex.X_op_symbol = NULL;
12481   ex.X_add_number = mips_cprestore_offset;
12482
12483   macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
12484                                 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
12485
12486   demand_empty_rest_of_line ();
12487 }
12488
12489 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12490    was given in the preceding .cpsetup, it results in:
12491      ld         $gp, offset($sp)
12492
12493    If a register $reg2 was given there, it results in:
12494      daddu      $gp, $reg2, $0
12495  */
12496 static void
12497 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12498 {
12499   expressionS ex;
12500   int icnt = 0;
12501
12502   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12503      We also need NewABI support.  */
12504   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12505     {
12506       s_ignore (0);
12507       return;
12508     }
12509
12510   if (mips_cpreturn_register == -1)
12511     {
12512       ex.X_op = O_constant;
12513       ex.X_add_symbol = NULL;
12514       ex.X_op_symbol = NULL;
12515       ex.X_add_number = mips_cpreturn_offset;
12516
12517       macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12518                    BFD_RELOC_LO16, SP);
12519     }
12520   else
12521     macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12522                  mips_cpreturn_register, 0);
12523
12524   demand_empty_rest_of_line ();
12525 }
12526
12527 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12528    code.  It sets the offset to use in gp_rel relocations.  */
12529
12530 static void
12531 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12532 {
12533   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12534      We also need NewABI support.  */
12535   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12536     {
12537       s_ignore (0);
12538       return;
12539     }
12540
12541   mips_gprel_offset = get_absolute_expression ();
12542
12543   demand_empty_rest_of_line ();
12544 }
12545
12546 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12547    code.  It generates a 32 bit GP relative reloc.  */
12548
12549 static void
12550 s_gpword (int ignore ATTRIBUTE_UNUSED)
12551 {
12552   symbolS *label;
12553   expressionS ex;
12554   char *p;
12555
12556   /* When not generating PIC code, this is treated as .word.  */
12557   if (mips_pic != SVR4_PIC)
12558     {
12559       s_cons (2);
12560       return;
12561     }
12562
12563   label = insn_labels != NULL ? insn_labels->label : NULL;
12564   mips_emit_delays (TRUE);
12565   if (auto_align)
12566     mips_align (2, 0, label);
12567   mips_clear_insn_labels ();
12568
12569   expression (&ex);
12570
12571   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12572     {
12573       as_bad (_("Unsupported use of .gpword"));
12574       ignore_rest_of_line ();
12575     }
12576
12577   p = frag_more (4);
12578   md_number_to_chars (p, 0, 4);
12579   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12580                BFD_RELOC_GPREL32);
12581
12582   demand_empty_rest_of_line ();
12583 }
12584
12585 static void
12586 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12587 {
12588   symbolS *label;
12589   expressionS ex;
12590   char *p;
12591
12592   /* When not generating PIC code, this is treated as .dword.  */
12593   if (mips_pic != SVR4_PIC)
12594     {
12595       s_cons (3);
12596       return;
12597     }
12598
12599   label = insn_labels != NULL ? insn_labels->label : NULL;
12600   mips_emit_delays (TRUE);
12601   if (auto_align)
12602     mips_align (3, 0, label);
12603   mips_clear_insn_labels ();
12604
12605   expression (&ex);
12606
12607   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12608     {
12609       as_bad (_("Unsupported use of .gpdword"));
12610       ignore_rest_of_line ();
12611     }
12612
12613   p = frag_more (8);
12614   md_number_to_chars (p, 0, 8);
12615   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12616                BFD_RELOC_GPREL32);
12617
12618   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12619   ex.X_op = O_absent;
12620   ex.X_add_symbol = 0;
12621   ex.X_add_number = 0;
12622   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12623                BFD_RELOC_64);
12624
12625   demand_empty_rest_of_line ();
12626 }
12627
12628 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12629    tables in SVR4 PIC code.  */
12630
12631 static void
12632 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12633 {
12634   int icnt = 0;
12635   int reg;
12636
12637   /* This is ignored when not generating SVR4 PIC code.  */
12638   if (mips_pic != SVR4_PIC)
12639     {
12640       s_ignore (0);
12641       return;
12642     }
12643
12644   /* Add $gp to the register named as an argument.  */
12645   reg = tc_get_register (0);
12646   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12647                reg, reg, mips_gp_register);
12648
12649   demand_empty_rest_of_line ();
12650 }
12651
12652 /* Handle the .insn pseudo-op.  This marks instruction labels in
12653    mips16 mode.  This permits the linker to handle them specially,
12654    such as generating jalx instructions when needed.  We also make
12655    them odd for the duration of the assembly, in order to generate the
12656    right sort of code.  We will make them even in the adjust_symtab
12657    routine, while leaving them marked.  This is convenient for the
12658    debugger and the disassembler.  The linker knows to make them odd
12659    again.  */
12660
12661 static void
12662 s_insn (int ignore ATTRIBUTE_UNUSED)
12663 {
12664   mips16_mark_labels ();
12665
12666   demand_empty_rest_of_line ();
12667 }
12668
12669 /* Handle a .stabn directive.  We need these in order to mark a label
12670    as being a mips16 text label correctly.  Sometimes the compiler
12671    will emit a label, followed by a .stabn, and then switch sections.
12672    If the label and .stabn are in mips16 mode, then the label is
12673    really a mips16 text label.  */
12674
12675 static void
12676 s_mips_stab (int type)
12677 {
12678   if (type == 'n')
12679     mips16_mark_labels ();
12680
12681   s_stab (type);
12682 }
12683
12684 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12685  */
12686
12687 static void
12688 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12689 {
12690   char *name;
12691   int c;
12692   symbolS *symbolP;
12693   expressionS exp;
12694
12695   name = input_line_pointer;
12696   c = get_symbol_end ();
12697   symbolP = symbol_find_or_make (name);
12698   S_SET_WEAK (symbolP);
12699   *input_line_pointer = c;
12700
12701   SKIP_WHITESPACE ();
12702
12703   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12704     {
12705       if (S_IS_DEFINED (symbolP))
12706         {
12707           as_bad ("ignoring attempt to redefine symbol %s",
12708                   S_GET_NAME (symbolP));
12709           ignore_rest_of_line ();
12710           return;
12711         }
12712
12713       if (*input_line_pointer == ',')
12714         {
12715           ++input_line_pointer;
12716           SKIP_WHITESPACE ();
12717         }
12718
12719       expression (&exp);
12720       if (exp.X_op != O_symbol)
12721         {
12722           as_bad ("bad .weakext directive");
12723           ignore_rest_of_line ();
12724           return;
12725         }
12726       symbol_set_value_expression (symbolP, &exp);
12727     }
12728
12729   demand_empty_rest_of_line ();
12730 }
12731
12732 /* Parse a register string into a number.  Called from the ECOFF code
12733    to parse .frame.  The argument is non-zero if this is the frame
12734    register, so that we can record it in mips_frame_reg.  */
12735
12736 int
12737 tc_get_register (int frame)
12738 {
12739   int reg;
12740
12741   SKIP_WHITESPACE ();
12742   if (*input_line_pointer++ != '$')
12743     {
12744       as_warn (_("expected `$'"));
12745       reg = ZERO;
12746     }
12747   else if (ISDIGIT (*input_line_pointer))
12748     {
12749       reg = get_absolute_expression ();
12750       if (reg < 0 || reg >= 32)
12751         {
12752           as_warn (_("Bad register number"));
12753           reg = ZERO;
12754         }
12755     }
12756   else
12757     {
12758       if (strncmp (input_line_pointer, "ra", 2) == 0)
12759         {
12760           reg = RA;
12761           input_line_pointer += 2;
12762         }
12763       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12764         {
12765           reg = FP;
12766           input_line_pointer += 2;
12767         }
12768       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12769         {
12770           reg = SP;
12771           input_line_pointer += 2;
12772         }
12773       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12774         {
12775           reg = GP;
12776           input_line_pointer += 2;
12777         }
12778       else if (strncmp (input_line_pointer, "at", 2) == 0)
12779         {
12780           reg = AT;
12781           input_line_pointer += 2;
12782         }
12783       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12784         {
12785           reg = KT0;
12786           input_line_pointer += 3;
12787         }
12788       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12789         {
12790           reg = KT1;
12791           input_line_pointer += 3;
12792         }
12793       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12794         {
12795           reg = ZERO;
12796           input_line_pointer += 4;
12797         }
12798       else
12799         {
12800           as_warn (_("Unrecognized register name"));
12801           reg = ZERO;
12802           while (ISALNUM(*input_line_pointer))
12803            input_line_pointer++;
12804         }
12805     }
12806   if (frame)
12807     {
12808       mips_frame_reg = reg != 0 ? reg : SP;
12809       mips_frame_reg_valid = 1;
12810       mips_cprestore_valid = 0;
12811     }
12812   return reg;
12813 }
12814
12815 valueT
12816 md_section_align (asection *seg, valueT addr)
12817 {
12818   int align = bfd_get_section_alignment (stdoutput, seg);
12819
12820 #ifdef OBJ_ELF
12821   /* We don't need to align ELF sections to the full alignment.
12822      However, Irix 5 may prefer that we align them at least to a 16
12823      byte boundary.  We don't bother to align the sections if we are
12824      targeted for an embedded system.  */
12825   if (strcmp (TARGET_OS, "elf") == 0)
12826     return addr;
12827   if (align > 4)
12828     align = 4;
12829 #endif
12830
12831   return ((addr + (1 << align) - 1) & (-1 << align));
12832 }
12833
12834 /* Utility routine, called from above as well.  If called while the
12835    input file is still being read, it's only an approximation.  (For
12836    example, a symbol may later become defined which appeared to be
12837    undefined earlier.)  */
12838
12839 static int
12840 nopic_need_relax (symbolS *sym, int before_relaxing)
12841 {
12842   if (sym == 0)
12843     return 0;
12844
12845   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12846     {
12847       const char *symname;
12848       int change;
12849
12850       /* Find out whether this symbol can be referenced off the $gp
12851          register.  It can be if it is smaller than the -G size or if
12852          it is in the .sdata or .sbss section.  Certain symbols can
12853          not be referenced off the $gp, although it appears as though
12854          they can.  */
12855       symname = S_GET_NAME (sym);
12856       if (symname != (const char *) NULL
12857           && (strcmp (symname, "eprol") == 0
12858               || strcmp (symname, "etext") == 0
12859               || strcmp (symname, "_gp") == 0
12860               || strcmp (symname, "edata") == 0
12861               || strcmp (symname, "_fbss") == 0
12862               || strcmp (symname, "_fdata") == 0
12863               || strcmp (symname, "_ftext") == 0
12864               || strcmp (symname, "end") == 0
12865               || strcmp (symname, "_gp_disp") == 0))
12866         change = 1;
12867       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12868                && (0
12869 #ifndef NO_ECOFF_DEBUGGING
12870                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12871                        && (symbol_get_obj (sym)->ecoff_extern_size
12872                            <= g_switch_value))
12873 #endif
12874                    /* We must defer this decision until after the whole
12875                       file has been read, since there might be a .extern
12876                       after the first use of this symbol.  */
12877                    || (before_relaxing
12878 #ifndef NO_ECOFF_DEBUGGING
12879                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12880 #endif
12881                        && S_GET_VALUE (sym) == 0)
12882                    || (S_GET_VALUE (sym) != 0
12883                        && S_GET_VALUE (sym) <= g_switch_value)))
12884         change = 0;
12885       else
12886         {
12887           const char *segname;
12888
12889           segname = segment_name (S_GET_SEGMENT (sym));
12890           assert (strcmp (segname, ".lit8") != 0
12891                   && strcmp (segname, ".lit4") != 0);
12892           change = (strcmp (segname, ".sdata") != 0
12893                     && strcmp (segname, ".sbss") != 0
12894                     && strncmp (segname, ".sdata.", 7) != 0
12895                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12896         }
12897       return change;
12898     }
12899   else
12900     /* We are not optimizing for the $gp register.  */
12901     return 1;
12902 }
12903
12904
12905 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12906
12907 static bfd_boolean
12908 pic_need_relax (symbolS *sym, asection *segtype)
12909 {
12910   asection *symsec;
12911   bfd_boolean linkonce;
12912
12913   /* Handle the case of a symbol equated to another symbol.  */
12914   while (symbol_equated_reloc_p (sym))
12915     {
12916       symbolS *n;
12917
12918       /* It's possible to get a loop here in a badly written
12919          program.  */
12920       n = symbol_get_value_expression (sym)->X_add_symbol;
12921       if (n == sym)
12922         break;
12923       sym = n;
12924     }
12925
12926   symsec = S_GET_SEGMENT (sym);
12927
12928   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12929   linkonce = FALSE;
12930   if (symsec != segtype && ! S_IS_LOCAL (sym))
12931     {
12932       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12933           != 0)
12934         linkonce = TRUE;
12935
12936       /* The GNU toolchain uses an extension for ELF: a section
12937          beginning with the magic string .gnu.linkonce is a linkonce
12938          section.  */
12939       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12940                    sizeof ".gnu.linkonce" - 1) == 0)
12941         linkonce = TRUE;
12942     }
12943
12944   /* This must duplicate the test in adjust_reloc_syms.  */
12945   return (symsec != &bfd_und_section
12946           && symsec != &bfd_abs_section
12947           && ! bfd_is_com_section (symsec)
12948           && !linkonce
12949 #ifdef OBJ_ELF
12950           /* A global or weak symbol is treated as external.  */
12951           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12952               || (! S_IS_WEAK (sym)
12953                   && (! S_IS_EXTERNAL (sym)
12954                       || mips_pic == EMBEDDED_PIC)))
12955 #endif
12956           );
12957 }
12958
12959
12960 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12961    extended opcode.  SEC is the section the frag is in.  */
12962
12963 static int
12964 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12965 {
12966   int type;
12967   register const struct mips16_immed_operand *op;
12968   offsetT val;
12969   int mintiny, maxtiny;
12970   segT symsec;
12971   fragS *sym_frag;
12972
12973   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12974     return 0;
12975   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12976     return 1;
12977
12978   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12979   op = mips16_immed_operands;
12980   while (op->type != type)
12981     {
12982       ++op;
12983       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12984     }
12985
12986   if (op->unsp)
12987     {
12988       if (type == '<' || type == '>' || type == '[' || type == ']')
12989         {
12990           mintiny = 1;
12991           maxtiny = 1 << op->nbits;
12992         }
12993       else
12994         {
12995           mintiny = 0;
12996           maxtiny = (1 << op->nbits) - 1;
12997         }
12998     }
12999   else
13000     {
13001       mintiny = - (1 << (op->nbits - 1));
13002       maxtiny = (1 << (op->nbits - 1)) - 1;
13003     }
13004
13005   sym_frag = symbol_get_frag (fragp->fr_symbol);
13006   val = S_GET_VALUE (fragp->fr_symbol);
13007   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13008
13009   if (op->pcrel)
13010     {
13011       addressT addr;
13012
13013       /* We won't have the section when we are called from
13014          mips_relax_frag.  However, we will always have been called
13015          from md_estimate_size_before_relax first.  If this is a
13016          branch to a different section, we mark it as such.  If SEC is
13017          NULL, and the frag is not marked, then it must be a branch to
13018          the same section.  */
13019       if (sec == NULL)
13020         {
13021           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13022             return 1;
13023         }
13024       else
13025         {
13026           /* Must have been called from md_estimate_size_before_relax.  */
13027           if (symsec != sec)
13028             {
13029               fragp->fr_subtype =
13030                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13031
13032               /* FIXME: We should support this, and let the linker
13033                  catch branches and loads that are out of range.  */
13034               as_bad_where (fragp->fr_file, fragp->fr_line,
13035                             _("unsupported PC relative reference to different section"));
13036
13037               return 1;
13038             }
13039           if (fragp != sym_frag && sym_frag->fr_address == 0)
13040             /* Assume non-extended on the first relaxation pass.
13041                The address we have calculated will be bogus if this is
13042                a forward branch to another frag, as the forward frag
13043                will have fr_address == 0.  */
13044             return 0;
13045         }
13046
13047       /* In this case, we know for sure that the symbol fragment is in
13048          the same section.  If the relax_marker of the symbol fragment
13049          differs from the relax_marker of this fragment, we have not
13050          yet adjusted the symbol fragment fr_address.  We want to add
13051          in STRETCH in order to get a better estimate of the address.
13052          This particularly matters because of the shift bits.  */
13053       if (stretch != 0
13054           && sym_frag->relax_marker != fragp->relax_marker)
13055         {
13056           fragS *f;
13057
13058           /* Adjust stretch for any alignment frag.  Note that if have
13059              been expanding the earlier code, the symbol may be
13060              defined in what appears to be an earlier frag.  FIXME:
13061              This doesn't handle the fr_subtype field, which specifies
13062              a maximum number of bytes to skip when doing an
13063              alignment.  */
13064           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13065             {
13066               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13067                 {
13068                   if (stretch < 0)
13069                     stretch = - ((- stretch)
13070                                  & ~ ((1 << (int) f->fr_offset) - 1));
13071                   else
13072                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13073                   if (stretch == 0)
13074                     break;
13075                 }
13076             }
13077           if (f != NULL)
13078             val += stretch;
13079         }
13080
13081       addr = fragp->fr_address + fragp->fr_fix;
13082
13083       /* The base address rules are complicated.  The base address of
13084          a branch is the following instruction.  The base address of a
13085          PC relative load or add is the instruction itself, but if it
13086          is in a delay slot (in which case it can not be extended) use
13087          the address of the instruction whose delay slot it is in.  */
13088       if (type == 'p' || type == 'q')
13089         {
13090           addr += 2;
13091
13092           /* If we are currently assuming that this frag should be
13093              extended, then, the current address is two bytes
13094              higher.  */
13095           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13096             addr += 2;
13097
13098           /* Ignore the low bit in the target, since it will be set
13099              for a text label.  */
13100           if ((val & 1) != 0)
13101             --val;
13102         }
13103       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13104         addr -= 4;
13105       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13106         addr -= 2;
13107
13108       val -= addr & ~ ((1 << op->shift) - 1);
13109
13110       /* Branch offsets have an implicit 0 in the lowest bit.  */
13111       if (type == 'p' || type == 'q')
13112         val /= 2;
13113
13114       /* If any of the shifted bits are set, we must use an extended
13115          opcode.  If the address depends on the size of this
13116          instruction, this can lead to a loop, so we arrange to always
13117          use an extended opcode.  We only check this when we are in
13118          the main relaxation loop, when SEC is NULL.  */
13119       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13120         {
13121           fragp->fr_subtype =
13122             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13123           return 1;
13124         }
13125
13126       /* If we are about to mark a frag as extended because the value
13127          is precisely maxtiny + 1, then there is a chance of an
13128          infinite loop as in the following code:
13129              la $4,foo
13130              .skip      1020
13131              .align     2
13132            foo:
13133          In this case when the la is extended, foo is 0x3fc bytes
13134          away, so the la can be shrunk, but then foo is 0x400 away, so
13135          the la must be extended.  To avoid this loop, we mark the
13136          frag as extended if it was small, and is about to become
13137          extended with a value of maxtiny + 1.  */
13138       if (val == ((maxtiny + 1) << op->shift)
13139           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13140           && sec == NULL)
13141         {
13142           fragp->fr_subtype =
13143             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13144           return 1;
13145         }
13146     }
13147   else if (symsec != absolute_section && sec != NULL)
13148     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13149
13150   if ((val & ((1 << op->shift) - 1)) != 0
13151       || val < (mintiny << op->shift)
13152       || val > (maxtiny << op->shift))
13153     return 1;
13154   else
13155     return 0;
13156 }
13157
13158 /* Compute the length of a branch sequence, and adjust the
13159    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13160    worst-case length is computed, with UPDATE being used to indicate
13161    whether an unconditional (-1), branch-likely (+1) or regular (0)
13162    branch is to be computed.  */
13163 static int
13164 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13165 {
13166   bfd_boolean toofar;
13167   int length;
13168
13169   if (fragp
13170       && S_IS_DEFINED (fragp->fr_symbol)
13171       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13172     {
13173       addressT addr;
13174       offsetT val;
13175
13176       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13177
13178       addr = fragp->fr_address + fragp->fr_fix + 4;
13179
13180       val -= addr;
13181
13182       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13183     }
13184   else if (fragp)
13185     /* If the symbol is not defined or it's in a different segment,
13186        assume the user knows what's going on and emit a short
13187        branch.  */
13188     toofar = FALSE;
13189   else
13190     toofar = TRUE;
13191
13192   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13193     fragp->fr_subtype
13194       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13195                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13196                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13197                              toofar);
13198
13199   length = 4;
13200   if (toofar)
13201     {
13202       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13203         length += 8;
13204
13205       if (mips_pic != NO_PIC)
13206         {
13207           /* Additional space for PIC loading of target address.  */
13208           length += 8;
13209           if (mips_opts.isa == ISA_MIPS1)
13210             /* Additional space for $at-stabilizing nop.  */
13211             length += 4;
13212         }
13213
13214       /* If branch is conditional.  */
13215       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13216         length += 8;
13217     }
13218
13219   return length;
13220 }
13221
13222 /* Estimate the size of a frag before relaxing.  Unless this is the
13223    mips16, we are not really relaxing here, and the final size is
13224    encoded in the subtype information.  For the mips16, we have to
13225    decide whether we are using an extended opcode or not.  */
13226
13227 int
13228 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13229 {
13230   int change;
13231
13232   if (RELAX_BRANCH_P (fragp->fr_subtype))
13233     {
13234
13235       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13236
13237       return fragp->fr_var;
13238     }
13239
13240   if (RELAX_MIPS16_P (fragp->fr_subtype))
13241     /* We don't want to modify the EXTENDED bit here; it might get us
13242        into infinite loops.  We change it only in mips_relax_frag().  */
13243     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13244
13245   if (mips_pic == NO_PIC)
13246     change = nopic_need_relax (fragp->fr_symbol, 0);
13247   else if (mips_pic == SVR4_PIC)
13248     change = pic_need_relax (fragp->fr_symbol, segtype);
13249   else
13250     abort ();
13251
13252   if (change)
13253     {
13254       /* Record the offset to the first reloc in the fr_opcode field.
13255          This lets md_convert_frag and tc_gen_reloc know that the code
13256          must be expanded.  */
13257       fragp->fr_opcode = (fragp->fr_literal
13258                           + fragp->fr_fix
13259                           - RELAX_OLD (fragp->fr_subtype)
13260                           + RELAX_RELOC1 (fragp->fr_subtype));
13261       /* FIXME: This really needs as_warn_where.  */
13262       if (RELAX_WARN (fragp->fr_subtype))
13263         as_warn (_("AT used after \".set noat\" or macro used after "
13264                    "\".set nomacro\""));
13265
13266       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13267     }
13268
13269   return 0;
13270 }
13271
13272 /* This is called to see whether a reloc against a defined symbol
13273    should be converted into a reloc against a section.  Don't adjust
13274    MIPS16 jump relocations, so we don't have to worry about the format
13275    of the offset in the .o file.  Don't adjust relocations against
13276    mips16 symbols, so that the linker can find them if it needs to set
13277    up a stub.  */
13278
13279 int
13280 mips_fix_adjustable (fixS *fixp)
13281 {
13282   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13283     return 0;
13284
13285   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13286       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13287     return 0;
13288
13289   if (fixp->fx_addsy == NULL)
13290     return 1;
13291
13292 #ifdef OBJ_ELF
13293   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13294       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13295       && fixp->fx_subsy == NULL)
13296     return 0;
13297 #endif
13298
13299   return 1;
13300 }
13301
13302 /* Translate internal representation of relocation info to BFD target
13303    format.  */
13304
13305 arelent **
13306 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13307 {
13308   static arelent *retval[4];
13309   arelent *reloc;
13310   bfd_reloc_code_real_type code;
13311
13312   memset (retval, 0, sizeof(retval));
13313   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13314   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13315   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13316   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13317
13318   if (mips_pic == EMBEDDED_PIC
13319       && SWITCH_TABLE (fixp))
13320     {
13321       /* For a switch table entry we use a special reloc.  The addend
13322          is actually the difference between the reloc address and the
13323          subtrahend.  */
13324       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13325       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13326         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13327       fixp->fx_r_type = BFD_RELOC_GPREL32;
13328     }
13329   else if (fixp->fx_pcrel)
13330     {
13331       bfd_vma pcrel_address;
13332
13333       /* Set PCREL_ADDRESS to this relocation's "PC".  The PC for high
13334          high-part relocs is the address of the low-part reloc.  */
13335       if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13336         {
13337           assert (fixp->fx_next != NULL
13338                   && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13339           pcrel_address = (fixp->fx_next->fx_where
13340                            + fixp->fx_next->fx_frag->fr_address);
13341         }
13342       else
13343         pcrel_address = reloc->address;
13344
13345       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13346         {
13347           /* At this point, fx_addnumber is "symbol offset - pcrel_address".
13348              Relocations want only the symbol offset.  */
13349           reloc->addend = fixp->fx_addnumber + pcrel_address;
13350         }
13351       else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16
13352                || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13353         {
13354           /* We use a special addend for an internal RELLO or RELHI reloc.  */
13355           if (symbol_section_p (fixp->fx_addsy))
13356             reloc->addend = pcrel_address - S_GET_VALUE (fixp->fx_subsy);
13357           else
13358             reloc->addend = fixp->fx_addnumber + pcrel_address;
13359         }
13360       else
13361         {
13362           if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13363             /* A gruesome hack which is a result of the gruesome gas reloc
13364                handling.  */
13365             reloc->addend = pcrel_address;
13366           else
13367             reloc->addend = -pcrel_address;
13368         }
13369     }
13370   else
13371     reloc->addend = fixp->fx_addnumber;
13372
13373   /* If this is a variant frag, we may need to adjust the existing
13374      reloc and generate a new one.  */
13375   if (fixp->fx_frag->fr_opcode != NULL
13376       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13377            && ! HAVE_NEWABI)
13378           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13379               && HAVE_NEWABI)
13380           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13381           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13382           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13383           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13384           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13385           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13386     )
13387     {
13388       arelent *reloc2;
13389
13390       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13391
13392       /* If this is not the last reloc in this frag, then we have two
13393          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13394          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13395          the second one handle all of them.  */
13396       if (fixp->fx_next != NULL
13397           && fixp->fx_frag == fixp->fx_next->fx_frag)
13398         {
13399           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13400                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13401                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13402                       && (fixp->fx_next->fx_r_type
13403                           == BFD_RELOC_MIPS_GOT_LO16))
13404                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13405                       && (fixp->fx_next->fx_r_type
13406                           == BFD_RELOC_MIPS_CALL_LO16)));
13407           retval[0] = NULL;
13408           return retval;
13409         }
13410
13411       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13412       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13413       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13414       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13415       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13416       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13417       reloc2->address = (reloc->address
13418                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13419                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13420       reloc2->addend = reloc->addend;
13421       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13422       assert (reloc2->howto != NULL);
13423
13424       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13425         {
13426           arelent *reloc3;
13427
13428           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13429           *reloc3 = *reloc2;
13430           reloc3->address += 4;
13431         }
13432
13433       if (mips_pic == NO_PIC)
13434         {
13435           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13436           fixp->fx_r_type = BFD_RELOC_HI16_S;
13437         }
13438       else if (mips_pic == SVR4_PIC)
13439         {
13440           switch (fixp->fx_r_type)
13441             {
13442             default:
13443               abort ();
13444             case BFD_RELOC_MIPS_GOT16:
13445               break;
13446             case BFD_RELOC_MIPS_GOT_LO16:
13447             case BFD_RELOC_MIPS_CALL_LO16:
13448               if (HAVE_NEWABI)
13449                 {
13450                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13451                   reloc2->howto = bfd_reloc_type_lookup
13452                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13453                 }
13454               else
13455                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13456               break;
13457             case BFD_RELOC_MIPS_CALL16:
13458             case BFD_RELOC_MIPS_GOT_OFST:
13459             case BFD_RELOC_MIPS_GOT_DISP:
13460               if (HAVE_NEWABI)
13461                 {
13462                   /* It may seem nonsensical to relax GOT_DISP to
13463                      GOT_DISP, but we're actually turning a GOT_DISP
13464                      without offset into a GOT_DISP with an offset,
13465                      getting rid of the separate addition, which we can
13466                      do when the symbol is found to be local.  */
13467                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13468                   retval[1] = NULL;
13469                 }
13470               else
13471                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13472               break;
13473             }
13474         }
13475       else
13476         abort ();
13477     }
13478
13479   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13480      entry to be used in the relocation's section offset.  */
13481   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13482     {
13483       reloc->address = reloc->addend;
13484       reloc->addend = 0;
13485     }
13486
13487   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13488      fixup_segment converted a non-PC relative reloc into a PC
13489      relative reloc.  In such a case, we need to convert the reloc
13490      code.  */
13491   code = fixp->fx_r_type;
13492   if (fixp->fx_pcrel)
13493     {
13494       switch (code)
13495         {
13496         case BFD_RELOC_8:
13497           code = BFD_RELOC_8_PCREL;
13498           break;
13499         case BFD_RELOC_16:
13500           code = BFD_RELOC_16_PCREL;
13501           break;
13502         case BFD_RELOC_32:
13503           code = BFD_RELOC_32_PCREL;
13504           break;
13505         case BFD_RELOC_64:
13506           code = BFD_RELOC_64_PCREL;
13507           break;
13508         case BFD_RELOC_8_PCREL:
13509         case BFD_RELOC_16_PCREL:
13510         case BFD_RELOC_32_PCREL:
13511         case BFD_RELOC_64_PCREL:
13512         case BFD_RELOC_16_PCREL_S2:
13513         case BFD_RELOC_PCREL_HI16_S:
13514         case BFD_RELOC_PCREL_LO16:
13515           break;
13516         default:
13517           as_bad_where (fixp->fx_file, fixp->fx_line,
13518                         _("Cannot make %s relocation PC relative"),
13519                         bfd_get_reloc_code_name (code));
13520         }
13521     }
13522
13523   /* To support a PC relative reloc when generating embedded PIC code
13524      for ECOFF, we use a Cygnus extension.  We check for that here to
13525      make sure that we don't let such a reloc escape normally.  */
13526   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13527        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13528       && code == BFD_RELOC_16_PCREL_S2
13529       && mips_pic != EMBEDDED_PIC)
13530     reloc->howto = NULL;
13531   else
13532     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13533
13534   if (reloc->howto == NULL)
13535     {
13536       as_bad_where (fixp->fx_file, fixp->fx_line,
13537                     _("Can not represent %s relocation in this object file format"),
13538                     bfd_get_reloc_code_name (code));
13539       retval[0] = NULL;
13540     }
13541
13542   return retval;
13543 }
13544
13545 /* Relax a machine dependent frag.  This returns the amount by which
13546    the current size of the frag should change.  */
13547
13548 int
13549 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13550 {
13551   if (RELAX_BRANCH_P (fragp->fr_subtype))
13552     {
13553       offsetT old_var = fragp->fr_var;
13554
13555       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13556
13557       return fragp->fr_var - old_var;
13558     }
13559
13560   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13561     return 0;
13562
13563   if (mips16_extended_frag (fragp, NULL, stretch))
13564     {
13565       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13566         return 0;
13567       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13568       return 2;
13569     }
13570   else
13571     {
13572       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13573         return 0;
13574       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13575       return -2;
13576     }
13577
13578   return 0;
13579 }
13580
13581 /* Convert a machine dependent frag.  */
13582
13583 void
13584 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13585 {
13586   int old, new;
13587   char *fixptr;
13588
13589   if (RELAX_BRANCH_P (fragp->fr_subtype))
13590     {
13591       bfd_byte *buf;
13592       unsigned long insn;
13593       expressionS exp;
13594       fixS *fixp;
13595
13596       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13597
13598       if (target_big_endian)
13599         insn = bfd_getb32 (buf);
13600       else
13601         insn = bfd_getl32 (buf);
13602
13603       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13604         {
13605           /* We generate a fixup instead of applying it right now
13606              because, if there are linker relaxations, we're going to
13607              need the relocations.  */
13608           exp.X_op = O_symbol;
13609           exp.X_add_symbol = fragp->fr_symbol;
13610           exp.X_add_number = fragp->fr_offset;
13611
13612           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13613                               4, &exp, 1,
13614                               BFD_RELOC_16_PCREL_S2);
13615           fixp->fx_file = fragp->fr_file;
13616           fixp->fx_line = fragp->fr_line;
13617
13618           md_number_to_chars (buf, insn, 4);
13619           buf += 4;
13620         }
13621       else
13622         {
13623           int i;
13624
13625           as_warn_where (fragp->fr_file, fragp->fr_line,
13626                          _("relaxed out-of-range branch into a jump"));
13627
13628           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13629             goto uncond;
13630
13631           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13632             {
13633               /* Reverse the branch.  */
13634               switch ((insn >> 28) & 0xf)
13635                 {
13636                 case 4:
13637                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13638                      have the condition reversed by tweaking a single
13639                      bit, and their opcodes all have 0x4???????.  */
13640                   assert ((insn & 0xf1000000) == 0x41000000);
13641                   insn ^= 0x00010000;
13642                   break;
13643
13644                 case 0:
13645                   /* bltz       0x04000000      bgez    0x04010000
13646                      bltzal     0x04100000      bgezal  0x04110000 */
13647                   assert ((insn & 0xfc0e0000) == 0x04000000);
13648                   insn ^= 0x00010000;
13649                   break;
13650
13651                 case 1:
13652                   /* beq        0x10000000      bne     0x14000000
13653                      blez       0x18000000      bgtz    0x1c000000 */
13654                   insn ^= 0x04000000;
13655                   break;
13656
13657                 default:
13658                   abort ();
13659                 }
13660             }
13661
13662           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13663             {
13664               /* Clear the and-link bit.  */
13665               assert ((insn & 0xfc1c0000) == 0x04100000);
13666
13667               /* bltzal 0x04100000      bgezal  0x04110000
13668                 bltzall 0x04120000     bgezall  0x04130000 */
13669               insn &= ~0x00100000;
13670             }
13671
13672           /* Branch over the branch (if the branch was likely) or the
13673              full jump (not likely case).  Compute the offset from the
13674              current instruction to branch to.  */
13675           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13676             i = 16;
13677           else
13678             {
13679               /* How many bytes in instructions we've already emitted?  */
13680               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13681               /* How many bytes in instructions from here to the end?  */
13682               i = fragp->fr_var - i;
13683             }
13684           /* Convert to instruction count.  */
13685           i >>= 2;
13686           /* Branch counts from the next instruction.  */
13687           i--;
13688           insn |= i;
13689           /* Branch over the jump.  */
13690           md_number_to_chars (buf, insn, 4);
13691           buf += 4;
13692
13693           /* Nop */
13694           md_number_to_chars (buf, 0, 4);
13695           buf += 4;
13696
13697           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13698             {
13699               /* beql $0, $0, 2f */
13700               insn = 0x50000000;
13701               /* Compute the PC offset from the current instruction to
13702                  the end of the variable frag.  */
13703               /* How many bytes in instructions we've already emitted?  */
13704               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13705               /* How many bytes in instructions from here to the end?  */
13706               i = fragp->fr_var - i;
13707               /* Convert to instruction count.  */
13708               i >>= 2;
13709               /* Don't decrement i, because we want to branch over the
13710                  delay slot.  */
13711
13712               insn |= i;
13713               md_number_to_chars (buf, insn, 4);
13714               buf += 4;
13715
13716               md_number_to_chars (buf, 0, 4);
13717               buf += 4;
13718             }
13719
13720         uncond:
13721           if (mips_pic == NO_PIC)
13722             {
13723               /* j or jal.  */
13724               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13725                       ? 0x0c000000 : 0x08000000);
13726               exp.X_op = O_symbol;
13727               exp.X_add_symbol = fragp->fr_symbol;
13728               exp.X_add_number = fragp->fr_offset;
13729
13730               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13731                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13732               fixp->fx_file = fragp->fr_file;
13733               fixp->fx_line = fragp->fr_line;
13734
13735               md_number_to_chars (buf, insn, 4);
13736               buf += 4;
13737             }
13738           else
13739             {
13740               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13741               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13742               exp.X_op = O_symbol;
13743               exp.X_add_symbol = fragp->fr_symbol;
13744               exp.X_add_number = fragp->fr_offset;
13745
13746               if (fragp->fr_offset)
13747                 {
13748                   exp.X_add_symbol = make_expr_symbol (&exp);
13749                   exp.X_add_number = 0;
13750                 }
13751
13752               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13753                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13754               fixp->fx_file = fragp->fr_file;
13755               fixp->fx_line = fragp->fr_line;
13756
13757               md_number_to_chars (buf, insn, 4);
13758               buf += 4;
13759
13760               if (mips_opts.isa == ISA_MIPS1)
13761                 {
13762                   /* nop */
13763                   md_number_to_chars (buf, 0, 4);
13764                   buf += 4;
13765                 }
13766
13767               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13768               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13769
13770               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13771                                   4, &exp, 0, BFD_RELOC_LO16);
13772               fixp->fx_file = fragp->fr_file;
13773               fixp->fx_line = fragp->fr_line;
13774
13775               md_number_to_chars (buf, insn, 4);
13776               buf += 4;
13777
13778               /* j(al)r $at.  */
13779               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13780                 insn = 0x0020f809;
13781               else
13782                 insn = 0x00200008;
13783
13784               md_number_to_chars (buf, insn, 4);
13785               buf += 4;
13786             }
13787         }
13788
13789       assert (buf == (bfd_byte *)fragp->fr_literal
13790               + fragp->fr_fix + fragp->fr_var);
13791
13792       fragp->fr_fix += fragp->fr_var;
13793
13794       return;
13795     }
13796
13797   if (RELAX_MIPS16_P (fragp->fr_subtype))
13798     {
13799       int type;
13800       register const struct mips16_immed_operand *op;
13801       bfd_boolean small, ext;
13802       offsetT val;
13803       bfd_byte *buf;
13804       unsigned long insn;
13805       bfd_boolean use_extend;
13806       unsigned short extend;
13807
13808       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13809       op = mips16_immed_operands;
13810       while (op->type != type)
13811         ++op;
13812
13813       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13814         {
13815           small = FALSE;
13816           ext = TRUE;
13817         }
13818       else
13819         {
13820           small = TRUE;
13821           ext = FALSE;
13822         }
13823
13824       resolve_symbol_value (fragp->fr_symbol);
13825       val = S_GET_VALUE (fragp->fr_symbol);
13826       if (op->pcrel)
13827         {
13828           addressT addr;
13829
13830           addr = fragp->fr_address + fragp->fr_fix;
13831
13832           /* The rules for the base address of a PC relative reloc are
13833              complicated; see mips16_extended_frag.  */
13834           if (type == 'p' || type == 'q')
13835             {
13836               addr += 2;
13837               if (ext)
13838                 addr += 2;
13839               /* Ignore the low bit in the target, since it will be
13840                  set for a text label.  */
13841               if ((val & 1) != 0)
13842                 --val;
13843             }
13844           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13845             addr -= 4;
13846           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13847             addr -= 2;
13848
13849           addr &= ~ (addressT) ((1 << op->shift) - 1);
13850           val -= addr;
13851
13852           /* Make sure the section winds up with the alignment we have
13853              assumed.  */
13854           if (op->shift > 0)
13855             record_alignment (asec, op->shift);
13856         }
13857
13858       if (ext
13859           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13860               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13861         as_warn_where (fragp->fr_file, fragp->fr_line,
13862                        _("extended instruction in delay slot"));
13863
13864       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13865
13866       if (target_big_endian)
13867         insn = bfd_getb16 (buf);
13868       else
13869         insn = bfd_getl16 (buf);
13870
13871       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13872                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13873                     small, ext, &insn, &use_extend, &extend);
13874
13875       if (use_extend)
13876         {
13877           md_number_to_chars (buf, 0xf000 | extend, 2);
13878           fragp->fr_fix += 2;
13879           buf += 2;
13880         }
13881
13882       md_number_to_chars (buf, insn, 2);
13883       fragp->fr_fix += 2;
13884       buf += 2;
13885     }
13886   else
13887     {
13888       if (fragp->fr_opcode == NULL)
13889         return;
13890
13891       old = RELAX_OLD (fragp->fr_subtype);
13892       new = RELAX_NEW (fragp->fr_subtype);
13893       fixptr = fragp->fr_literal + fragp->fr_fix;
13894
13895       if (new > 0)
13896         memmove (fixptr - old, fixptr, new);
13897
13898       fragp->fr_fix += new - old;
13899     }
13900 }
13901
13902 #ifdef OBJ_ELF
13903
13904 /* This function is called after the relocs have been generated.
13905    We've been storing mips16 text labels as odd.  Here we convert them
13906    back to even for the convenience of the debugger.  */
13907
13908 void
13909 mips_frob_file_after_relocs (void)
13910 {
13911   asymbol **syms;
13912   unsigned int count, i;
13913
13914   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13915     return;
13916
13917   syms = bfd_get_outsymbols (stdoutput);
13918   count = bfd_get_symcount (stdoutput);
13919   for (i = 0; i < count; i++, syms++)
13920     {
13921       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13922           && ((*syms)->value & 1) != 0)
13923         {
13924           (*syms)->value &= ~1;
13925           /* If the symbol has an odd size, it was probably computed
13926              incorrectly, so adjust that as well.  */
13927           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13928             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13929         }
13930     }
13931 }
13932
13933 #endif
13934
13935 /* This function is called whenever a label is defined.  It is used
13936    when handling branch delays; if a branch has a label, we assume we
13937    can not move it.  */
13938
13939 void
13940 mips_define_label (symbolS *sym)
13941 {
13942   struct insn_label_list *l;
13943
13944   if (free_insn_labels == NULL)
13945     l = (struct insn_label_list *) xmalloc (sizeof *l);
13946   else
13947     {
13948       l = free_insn_labels;
13949       free_insn_labels = l->next;
13950     }
13951
13952   l->label = sym;
13953   l->next = insn_labels;
13954   insn_labels = l;
13955 }
13956 \f
13957 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13958
13959 /* Some special processing for a MIPS ELF file.  */
13960
13961 void
13962 mips_elf_final_processing (void)
13963 {
13964   /* Write out the register information.  */
13965   if (mips_abi != N64_ABI)
13966     {
13967       Elf32_RegInfo s;
13968
13969       s.ri_gprmask = mips_gprmask;
13970       s.ri_cprmask[0] = mips_cprmask[0];
13971       s.ri_cprmask[1] = mips_cprmask[1];
13972       s.ri_cprmask[2] = mips_cprmask[2];
13973       s.ri_cprmask[3] = mips_cprmask[3];
13974       /* The gp_value field is set by the MIPS ELF backend.  */
13975
13976       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13977                                        ((Elf32_External_RegInfo *)
13978                                         mips_regmask_frag));
13979     }
13980   else
13981     {
13982       Elf64_Internal_RegInfo s;
13983
13984       s.ri_gprmask = mips_gprmask;
13985       s.ri_pad = 0;
13986       s.ri_cprmask[0] = mips_cprmask[0];
13987       s.ri_cprmask[1] = mips_cprmask[1];
13988       s.ri_cprmask[2] = mips_cprmask[2];
13989       s.ri_cprmask[3] = mips_cprmask[3];
13990       /* The gp_value field is set by the MIPS ELF backend.  */
13991
13992       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13993                                        ((Elf64_External_RegInfo *)
13994                                         mips_regmask_frag));
13995     }
13996
13997   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13998      sort of BFD interface for this.  */
13999   if (mips_any_noreorder)
14000     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14001   if (mips_pic != NO_PIC)
14002     {
14003     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14004       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14005     }
14006   if (mips_abicalls)
14007     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14008
14009   /* Set MIPS ELF flags for ASEs.  */
14010   if (file_ase_mips16)
14011     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14012 #if 0 /* XXX FIXME */
14013   if (file_ase_mips3d)
14014     elf_elfheader (stdoutput)->e_flags |= ???;
14015 #endif
14016   if (file_ase_mdmx)
14017     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14018
14019   /* Set the MIPS ELF ABI flags.  */
14020   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14021     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14022   else if (mips_abi == O64_ABI)
14023     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14024   else if (mips_abi == EABI_ABI)
14025     {
14026       if (!file_mips_gp32)
14027         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14028       else
14029         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14030     }
14031   else if (mips_abi == N32_ABI)
14032     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14033
14034   /* Nothing to do for N64_ABI.  */
14035
14036   if (mips_32bitmode)
14037     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14038 }
14039
14040 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14041 \f
14042 typedef struct proc {
14043   symbolS *isym;
14044   unsigned long reg_mask;
14045   unsigned long reg_offset;
14046   unsigned long fpreg_mask;
14047   unsigned long fpreg_offset;
14048   unsigned long frame_offset;
14049   unsigned long frame_reg;
14050   unsigned long pc_reg;
14051 } procS;
14052
14053 static procS cur_proc;
14054 static procS *cur_proc_ptr;
14055 static int numprocs;
14056
14057 /* Fill in an rs_align_code fragment.  */
14058
14059 void
14060 mips_handle_align (fragS *fragp)
14061 {
14062   if (fragp->fr_type != rs_align_code)
14063     return;
14064
14065   if (mips_opts.mips16)
14066     {
14067       static const unsigned char be_nop[] = { 0x65, 0x00 };
14068       static const unsigned char le_nop[] = { 0x00, 0x65 };
14069
14070       int bytes;
14071       char *p;
14072
14073       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14074       p = fragp->fr_literal + fragp->fr_fix;
14075
14076       if (bytes & 1)
14077         {
14078           *p++ = 0;
14079           fragp->fr_fix++;
14080         }
14081
14082       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14083       fragp->fr_var = 2;
14084     }
14085
14086   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14087 }
14088
14089 static void
14090 md_obj_begin (void)
14091 {
14092 }
14093
14094 static void
14095 md_obj_end (void)
14096 {
14097   /* check for premature end, nesting errors, etc */
14098   if (cur_proc_ptr)
14099     as_warn (_("missing .end at end of assembly"));
14100 }
14101
14102 static long
14103 get_number (void)
14104 {
14105   int negative = 0;
14106   long val = 0;
14107
14108   if (*input_line_pointer == '-')
14109     {
14110       ++input_line_pointer;
14111       negative = 1;
14112     }
14113   if (!ISDIGIT (*input_line_pointer))
14114     as_bad (_("expected simple number"));
14115   if (input_line_pointer[0] == '0')
14116     {
14117       if (input_line_pointer[1] == 'x')
14118         {
14119           input_line_pointer += 2;
14120           while (ISXDIGIT (*input_line_pointer))
14121             {
14122               val <<= 4;
14123               val |= hex_value (*input_line_pointer++);
14124             }
14125           return negative ? -val : val;
14126         }
14127       else
14128         {
14129           ++input_line_pointer;
14130           while (ISDIGIT (*input_line_pointer))
14131             {
14132               val <<= 3;
14133               val |= *input_line_pointer++ - '0';
14134             }
14135           return negative ? -val : val;
14136         }
14137     }
14138   if (!ISDIGIT (*input_line_pointer))
14139     {
14140       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14141               *input_line_pointer, *input_line_pointer);
14142       as_warn (_("invalid number"));
14143       return -1;
14144     }
14145   while (ISDIGIT (*input_line_pointer))
14146     {
14147       val *= 10;
14148       val += *input_line_pointer++ - '0';
14149     }
14150   return negative ? -val : val;
14151 }
14152
14153 /* The .file directive; just like the usual .file directive, but there
14154    is an initial number which is the ECOFF file index.  In the non-ECOFF
14155    case .file implies DWARF-2.  */
14156
14157 static void
14158 s_mips_file (int x ATTRIBUTE_UNUSED)
14159 {
14160   static int first_file_directive = 0;
14161
14162   if (ECOFF_DEBUGGING)
14163     {
14164       get_number ();
14165       s_app_file (0);
14166     }
14167   else
14168     {
14169       char *filename;
14170
14171       filename = dwarf2_directive_file (0);
14172
14173       /* Versions of GCC up to 3.1 start files with a ".file"
14174          directive even for stabs output.  Make sure that this
14175          ".file" is handled.  Note that you need a version of GCC
14176          after 3.1 in order to support DWARF-2 on MIPS.  */
14177       if (filename != NULL && ! first_file_directive)
14178         {
14179           (void) new_logical_line (filename, -1);
14180           s_app_file_string (filename);
14181         }
14182       first_file_directive = 1;
14183     }
14184 }
14185
14186 /* The .loc directive, implying DWARF-2.  */
14187
14188 static void
14189 s_mips_loc (int x ATTRIBUTE_UNUSED)
14190 {
14191   if (!ECOFF_DEBUGGING)
14192     dwarf2_directive_loc (0);
14193 }
14194
14195 /* The .end directive.  */
14196
14197 static void
14198 s_mips_end (int x ATTRIBUTE_UNUSED)
14199 {
14200   symbolS *p;
14201
14202   /* Following functions need their own .frame and .cprestore directives.  */
14203   mips_frame_reg_valid = 0;
14204   mips_cprestore_valid = 0;
14205
14206   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14207     {
14208       p = get_symbol ();
14209       demand_empty_rest_of_line ();
14210     }
14211   else
14212     p = NULL;
14213
14214   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14215     as_warn (_(".end not in text section"));
14216
14217   if (!cur_proc_ptr)
14218     {
14219       as_warn (_(".end directive without a preceding .ent directive."));
14220       demand_empty_rest_of_line ();
14221       return;
14222     }
14223
14224   if (p != NULL)
14225     {
14226       assert (S_GET_NAME (p));
14227       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14228         as_warn (_(".end symbol does not match .ent symbol."));
14229
14230       if (debug_type == DEBUG_STABS)
14231         stabs_generate_asm_endfunc (S_GET_NAME (p),
14232                                     S_GET_NAME (p));
14233     }
14234   else
14235     as_warn (_(".end directive missing or unknown symbol"));
14236
14237 #ifdef OBJ_ELF
14238   /* Generate a .pdr section.  */
14239   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14240       && mips_flag_pdr)
14241     {
14242       segT saved_seg = now_seg;
14243       subsegT saved_subseg = now_subseg;
14244       valueT dot;
14245       expressionS exp;
14246       char *fragp;
14247
14248       dot = frag_now_fix ();
14249
14250 #ifdef md_flush_pending_output
14251       md_flush_pending_output ();
14252 #endif
14253
14254       assert (pdr_seg);
14255       subseg_set (pdr_seg, 0);
14256
14257       /* Write the symbol.  */
14258       exp.X_op = O_symbol;
14259       exp.X_add_symbol = p;
14260       exp.X_add_number = 0;
14261       emit_expr (&exp, 4);
14262
14263       fragp = frag_more (7 * 4);
14264
14265       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14266       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14267       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14268       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14269       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14270       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14271       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14272
14273       subseg_set (saved_seg, saved_subseg);
14274     }
14275 #endif /* OBJ_ELF */
14276
14277   cur_proc_ptr = NULL;
14278 }
14279
14280 /* The .aent and .ent directives.  */
14281
14282 static void
14283 s_mips_ent (int aent)
14284 {
14285   symbolS *symbolP;
14286
14287   symbolP = get_symbol ();
14288   if (*input_line_pointer == ',')
14289     ++input_line_pointer;
14290   SKIP_WHITESPACE ();
14291   if (ISDIGIT (*input_line_pointer)
14292       || *input_line_pointer == '-')
14293     get_number ();
14294
14295   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14296     as_warn (_(".ent or .aent not in text section."));
14297
14298   if (!aent && cur_proc_ptr)
14299     as_warn (_("missing .end"));
14300
14301   if (!aent)
14302     {
14303       /* This function needs its own .frame and .cprestore directives.  */
14304       mips_frame_reg_valid = 0;
14305       mips_cprestore_valid = 0;
14306
14307       cur_proc_ptr = &cur_proc;
14308       memset (cur_proc_ptr, '\0', sizeof (procS));
14309
14310       cur_proc_ptr->isym = symbolP;
14311
14312       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14313
14314       ++numprocs;
14315
14316       if (debug_type == DEBUG_STABS)
14317         stabs_generate_asm_func (S_GET_NAME (symbolP),
14318                                  S_GET_NAME (symbolP));
14319     }
14320
14321   demand_empty_rest_of_line ();
14322 }
14323
14324 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14325    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14326    s_mips_frame is used so that we can set the PDR information correctly.
14327    We can't use the ecoff routines because they make reference to the ecoff
14328    symbol table (in the mdebug section).  */
14329
14330 static void
14331 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14332 {
14333 #ifdef OBJ_ELF
14334   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14335     {
14336       long val;
14337
14338       if (cur_proc_ptr == (procS *) NULL)
14339         {
14340           as_warn (_(".frame outside of .ent"));
14341           demand_empty_rest_of_line ();
14342           return;
14343         }
14344
14345       cur_proc_ptr->frame_reg = tc_get_register (1);
14346
14347       SKIP_WHITESPACE ();
14348       if (*input_line_pointer++ != ','
14349           || get_absolute_expression_and_terminator (&val) != ',')
14350         {
14351           as_warn (_("Bad .frame directive"));
14352           --input_line_pointer;
14353           demand_empty_rest_of_line ();
14354           return;
14355         }
14356
14357       cur_proc_ptr->frame_offset = val;
14358       cur_proc_ptr->pc_reg = tc_get_register (0);
14359
14360       demand_empty_rest_of_line ();
14361     }
14362   else
14363 #endif /* OBJ_ELF */
14364     s_ignore (ignore);
14365 }
14366
14367 /* The .fmask and .mask directives. If the mdebug section is present
14368    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14369    embedded targets, s_mips_mask is used so that we can set the PDR
14370    information correctly. We can't use the ecoff routines because they
14371    make reference to the ecoff symbol table (in the mdebug section).  */
14372
14373 static void
14374 s_mips_mask (int reg_type)
14375 {
14376 #ifdef OBJ_ELF
14377   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14378     {
14379       long mask, off;
14380
14381       if (cur_proc_ptr == (procS *) NULL)
14382         {
14383           as_warn (_(".mask/.fmask outside of .ent"));
14384           demand_empty_rest_of_line ();
14385           return;
14386         }
14387
14388       if (get_absolute_expression_and_terminator (&mask) != ',')
14389         {
14390           as_warn (_("Bad .mask/.fmask directive"));
14391           --input_line_pointer;
14392           demand_empty_rest_of_line ();
14393           return;
14394         }
14395
14396       off = get_absolute_expression ();
14397
14398       if (reg_type == 'F')
14399         {
14400           cur_proc_ptr->fpreg_mask = mask;
14401           cur_proc_ptr->fpreg_offset = off;
14402         }
14403       else
14404         {
14405           cur_proc_ptr->reg_mask = mask;
14406           cur_proc_ptr->reg_offset = off;
14407         }
14408
14409       demand_empty_rest_of_line ();
14410     }
14411   else
14412 #endif /* OBJ_ELF */
14413     s_ignore (reg_type);
14414 }
14415
14416 /* The .loc directive.  */
14417
14418 #if 0
14419 static void
14420 s_loc (int x)
14421 {
14422   symbolS *symbolP;
14423   int lineno;
14424   int addroff;
14425
14426   assert (now_seg == text_section);
14427
14428   lineno = get_number ();
14429   addroff = frag_now_fix ();
14430
14431   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14432   S_SET_TYPE (symbolP, N_SLINE);
14433   S_SET_OTHER (symbolP, 0);
14434   S_SET_DESC (symbolP, lineno);
14435   symbolP->sy_segment = now_seg;
14436 }
14437 #endif
14438
14439 /* A table describing all the processors gas knows about.  Names are
14440    matched in the order listed.
14441
14442    To ease comparison, please keep this table in the same order as
14443    gcc's mips_cpu_info_table[].  */
14444 static const struct mips_cpu_info mips_cpu_info_table[] =
14445 {
14446   /* Entries for generic ISAs */
14447   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14448   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14449   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14450   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14451   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14452   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14453   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14454   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14455   { "mips64r2",       1,      ISA_MIPS64R2,   CPU_MIPS64R2 },
14456
14457   /* MIPS I */
14458   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14459   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14460   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14461
14462   /* MIPS II */
14463   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14464
14465   /* MIPS III */
14466   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14467   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14468   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14469   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14470   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14471   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14472   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14473   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14474   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14475   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14476   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14477   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14478
14479   /* MIPS IV */
14480   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14481   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14482   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14483   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14484   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14485   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14486   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14487   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14488   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14489   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14490   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14491   { "rm7000",         0,      ISA_MIPS4,      CPU_RM7000 },
14492   { "rm9000",         0,      ISA_MIPS4,      CPU_RM7000 },
14493
14494   /* MIPS 32 */
14495   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32 },
14496   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14497   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14498
14499   /* MIPS 64 */
14500   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14501   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14502
14503   /* Broadcom SB-1 CPU core */
14504   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14505
14506   /* End marker */
14507   { NULL, 0, 0, 0 }
14508 };
14509
14510
14511 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14512    with a final "000" replaced by "k".  Ignore case.
14513
14514    Note: this function is shared between GCC and GAS.  */
14515
14516 static bfd_boolean
14517 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14518 {
14519   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14520     given++, canonical++;
14521
14522   return ((*given == 0 && *canonical == 0)
14523           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14524 }
14525
14526
14527 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14528    CPU name.  We've traditionally allowed a lot of variation here.
14529
14530    Note: this function is shared between GCC and GAS.  */
14531
14532 static bfd_boolean
14533 mips_matching_cpu_name_p (const char *canonical, const char *given)
14534 {
14535   /* First see if the name matches exactly, or with a final "000"
14536      turned into "k".  */
14537   if (mips_strict_matching_cpu_name_p (canonical, given))
14538     return TRUE;
14539
14540   /* If not, try comparing based on numerical designation alone.
14541      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14542   if (TOLOWER (*given) == 'r')
14543     given++;
14544   if (!ISDIGIT (*given))
14545     return FALSE;
14546
14547   /* Skip over some well-known prefixes in the canonical name,
14548      hoping to find a number there too.  */
14549   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14550     canonical += 2;
14551   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14552     canonical += 2;
14553   else if (TOLOWER (canonical[0]) == 'r')
14554     canonical += 1;
14555
14556   return mips_strict_matching_cpu_name_p (canonical, given);
14557 }
14558
14559
14560 /* Parse an option that takes the name of a processor as its argument.
14561    OPTION is the name of the option and CPU_STRING is the argument.
14562    Return the corresponding processor enumeration if the CPU_STRING is
14563    recognized, otherwise report an error and return null.
14564
14565    A similar function exists in GCC.  */
14566
14567 static const struct mips_cpu_info *
14568 mips_parse_cpu (const char *option, const char *cpu_string)
14569 {
14570   const struct mips_cpu_info *p;
14571
14572   /* 'from-abi' selects the most compatible architecture for the given
14573      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14574      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14575      version.  Look first at the -mgp options, if given, otherwise base
14576      the choice on MIPS_DEFAULT_64BIT.
14577
14578      Treat NO_ABI like the EABIs.  One reason to do this is that the
14579      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14580      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14581      'mips64', just as we did in the days before 'from-abi'.  */
14582   if (strcasecmp (cpu_string, "from-abi") == 0)
14583     {
14584       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14585         return mips_cpu_info_from_isa (ISA_MIPS1);
14586
14587       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14588         return mips_cpu_info_from_isa (ISA_MIPS3);
14589
14590       if (file_mips_gp32 >= 0)
14591         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14592
14593       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14594                                      ? ISA_MIPS3
14595                                      : ISA_MIPS1);
14596     }
14597
14598   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14599   if (strcasecmp (cpu_string, "default") == 0)
14600     return 0;
14601
14602   for (p = mips_cpu_info_table; p->name != 0; p++)
14603     if (mips_matching_cpu_name_p (p->name, cpu_string))
14604       return p;
14605
14606   as_bad ("Bad value (%s) for %s", cpu_string, option);
14607   return 0;
14608 }
14609
14610 /* Return the canonical processor information for ISA (a member of the
14611    ISA_MIPS* enumeration).  */
14612
14613 static const struct mips_cpu_info *
14614 mips_cpu_info_from_isa (int isa)
14615 {
14616   int i;
14617
14618   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14619     if (mips_cpu_info_table[i].is_isa
14620         && isa == mips_cpu_info_table[i].isa)
14621       return (&mips_cpu_info_table[i]);
14622
14623   return NULL;
14624 }
14625
14626 static const struct mips_cpu_info *
14627 mips_cpu_info_from_arch (int arch)
14628 {
14629   int i;
14630
14631   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14632     if (arch == mips_cpu_info_table[i].cpu)
14633       return (&mips_cpu_info_table[i]);
14634
14635   return NULL;
14636 }
14637 \f
14638 static void
14639 show (FILE *stream, const char *string, int *col_p, int *first_p)
14640 {
14641   if (*first_p)
14642     {
14643       fprintf (stream, "%24s", "");
14644       *col_p = 24;
14645     }
14646   else
14647     {
14648       fprintf (stream, ", ");
14649       *col_p += 2;
14650     }
14651
14652   if (*col_p + strlen (string) > 72)
14653     {
14654       fprintf (stream, "\n%24s", "");
14655       *col_p = 24;
14656     }
14657
14658   fprintf (stream, "%s", string);
14659   *col_p += strlen (string);
14660
14661   *first_p = 0;
14662 }
14663
14664 void
14665 md_show_usage (FILE *stream)
14666 {
14667   int column, first;
14668   size_t i;
14669
14670   fprintf (stream, _("\
14671 MIPS options:\n\
14672 -membedded-pic          generate embedded position independent code\n\
14673 -EB                     generate big endian output\n\
14674 -EL                     generate little endian output\n\
14675 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14676 -G NUM                  allow referencing objects up to NUM bytes\n\
14677                         implicitly with the gp register [default 8]\n"));
14678   fprintf (stream, _("\
14679 -mips1                  generate MIPS ISA I instructions\n\
14680 -mips2                  generate MIPS ISA II instructions\n\
14681 -mips3                  generate MIPS ISA III instructions\n\
14682 -mips4                  generate MIPS ISA IV instructions\n\
14683 -mips5                  generate MIPS ISA V instructions\n\
14684 -mips32                 generate MIPS32 ISA instructions\n\
14685 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14686 -mips64                 generate MIPS64 ISA instructions\n\
14687 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
14688 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14689
14690   first = 1;
14691
14692   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14693     show (stream, mips_cpu_info_table[i].name, &column, &first);
14694   show (stream, "from-abi", &column, &first);
14695   fputc ('\n', stream);
14696
14697   fprintf (stream, _("\
14698 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14699 -no-mCPU                don't generate code specific to CPU.\n\
14700                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14701
14702   first = 1;
14703
14704   show (stream, "3900", &column, &first);
14705   show (stream, "4010", &column, &first);
14706   show (stream, "4100", &column, &first);
14707   show (stream, "4650", &column, &first);
14708   fputc ('\n', stream);
14709
14710   fprintf (stream, _("\
14711 -mips16                 generate mips16 instructions\n\
14712 -no-mips16              do not generate mips16 instructions\n"));
14713   fprintf (stream, _("\
14714 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14715 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14716 -O0                     remove unneeded NOPs, do not swap branches\n\
14717 -O                      remove unneeded NOPs and swap branches\n\
14718 -n                      warn about NOPs generated from macros\n\
14719 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14720 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14721 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14722 #ifdef OBJ_ELF
14723   fprintf (stream, _("\
14724 -KPIC, -call_shared     generate SVR4 position independent code\n\
14725 -non_shared             do not generate position independent code\n\
14726 -xgot                   assume a 32 bit GOT\n\
14727 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
14728 -mabi=ABI               create ABI conformant object file for:\n"));
14729
14730   first = 1;
14731
14732   show (stream, "32", &column, &first);
14733   show (stream, "o64", &column, &first);
14734   show (stream, "n32", &column, &first);
14735   show (stream, "64", &column, &first);
14736   show (stream, "eabi", &column, &first);
14737
14738   fputc ('\n', stream);
14739
14740   fprintf (stream, _("\
14741 -32                     create o32 ABI object file (default)\n\
14742 -n32                    create n32 ABI object file\n\
14743 -64                     create 64 ABI object file\n"));
14744 #endif
14745 }
14746
14747 enum dwarf2_format
14748 mips_dwarf2_format (void)
14749 {
14750   if (mips_abi == N64_ABI)
14751     {
14752 #ifdef TE_IRIX
14753       return dwarf2_format_64bit_irix;
14754 #else
14755       return dwarf2_format_64bit;
14756 #endif
14757     }
14758   else
14759     return dwarf2_format_32bit;
14760 }