OSDN Git Service

82c2cd22ed003c72b6097155f663bd940ac52577
[pf3gnuchains/pf3gnuchains4x.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,
3    2003, 2004, 2005, 2006 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, 51 Franklin Street - Fifth Floor, Boston, MA
24    02110-1301, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include "opcode/mips.h"
32 #include "itbl-ops.h"
33 #include "dwarf2dbg.h"
34 #include "dw2gencfi.h"
35
36 #ifdef DEBUG
37 #define DBG(x) printf x
38 #else
39 #define DBG(x)
40 #endif
41
42 #ifdef OBJ_MAYBE_ELF
43 /* Clean up namespace so we can include obj-elf.h too.  */
44 static int mips_output_flavor (void);
45 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
46 #undef OBJ_PROCESS_STAB
47 #undef OUTPUT_FLAVOR
48 #undef S_GET_ALIGN
49 #undef S_GET_SIZE
50 #undef S_SET_ALIGN
51 #undef S_SET_SIZE
52 #undef obj_frob_file
53 #undef obj_frob_file_after_relocs
54 #undef obj_frob_symbol
55 #undef obj_pop_insert
56 #undef obj_sec_sym_ok_for_reloc
57 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
58
59 #include "obj-elf.h"
60 /* Fix any of them that we actually care about.  */
61 #undef OUTPUT_FLAVOR
62 #define OUTPUT_FLAVOR mips_output_flavor()
63 #endif
64
65 #if defined (OBJ_ELF)
66 #include "elf/mips.h"
67 #endif
68
69 #ifndef ECOFF_DEBUGGING
70 #define NO_ECOFF_DEBUGGING
71 #define ECOFF_DEBUGGING 0
72 #endif
73
74 int mips_flag_mdebug = -1;
75
76 /* Control generation of .pdr sections.  Off by default on IRIX: the native
77    linker doesn't know about and discards them, but relocations against them
78    remain, leading to rld crashes.  */
79 #ifdef TE_IRIX
80 int mips_flag_pdr = FALSE;
81 #else
82 int mips_flag_pdr = TRUE;
83 #endif
84
85 #include "ecoff.h"
86
87 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
88 static char *mips_regmask_frag;
89 #endif
90
91 #define ZERO 0
92 #define AT  1
93 #define TREG 24
94 #define PIC_CALL_REG 25
95 #define KT0 26
96 #define KT1 27
97 #define GP  28
98 #define SP  29
99 #define FP  30
100 #define RA  31
101
102 #define ILLEGAL_REG (32)
103
104 /* Allow override of standard little-endian ECOFF format.  */
105
106 #ifndef ECOFF_LITTLE_FORMAT
107 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
108 #endif
109
110 extern int target_big_endian;
111
112 /* The name of the readonly data section.  */
113 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
114                             ? ".rdata" \
115                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
116                             ? ".rdata" \
117                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
118                             ? ".rodata" \
119                             : (abort (), ""))
120
121 /* Information about an instruction, including its format, operands
122    and fixups.  */
123 struct mips_cl_insn
124 {
125   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
126   const struct mips_opcode *insn_mo;
127
128   /* True if this is a mips16 instruction and if we want the extended
129      form of INSN_MO.  */
130   bfd_boolean use_extend;
131
132   /* The 16-bit extension instruction to use when USE_EXTEND is true.  */
133   unsigned short extend;
134
135   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
136      a copy of INSN_MO->match with the operands filled in.  */
137   unsigned long insn_opcode;
138
139   /* The frag that contains the instruction.  */
140   struct frag *frag;
141
142   /* The offset into FRAG of the first instruction byte.  */
143   long where;
144
145   /* The relocs associated with the instruction, if any.  */
146   fixS *fixp[3];
147
148   /* True if this entry cannot be moved from its current position.  */
149   unsigned int fixed_p : 1;
150
151   /* True if this instruction occurred in a .set noreorder block.  */
152   unsigned int noreorder_p : 1;
153
154   /* True for mips16 instructions that jump to an absolute address.  */
155   unsigned int mips16_absolute_jump_p : 1;
156 };
157
158 /* The ABI to use.  */
159 enum mips_abi_level
160 {
161   NO_ABI = 0,
162   O32_ABI,
163   O64_ABI,
164   N32_ABI,
165   N64_ABI,
166   EABI_ABI
167 };
168
169 /* MIPS ABI we are using for this output file.  */
170 static enum mips_abi_level mips_abi = NO_ABI;
171
172 /* Whether or not we have code that can call pic code.  */
173 int mips_abicalls = FALSE;
174
175 /* Whether or not we have code which can be put into a shared
176    library.  */
177 static bfd_boolean mips_in_shared = TRUE;
178
179 /* This is the set of options which may be modified by the .set
180    pseudo-op.  We use a struct so that .set push and .set pop are more
181    reliable.  */
182
183 struct mips_set_options
184 {
185   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
186      if it has not been initialized.  Changed by `.set mipsN', and the
187      -mipsN command line option, and the default CPU.  */
188   int isa;
189   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
190      if they have not been initialized.  Changed by `.set <asename>', by
191      command line options, and based on the default architecture.  */
192   int ase_mips3d;
193   int ase_mdmx;
194   int ase_smartmips;
195   int ase_dsp;
196   int ase_mt;
197   /* Whether we are assembling for the mips16 processor.  0 if we are
198      not, 1 if we are, and -1 if the value has not been initialized.
199      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
200      -nomips16 command line options, and the default CPU.  */
201   int mips16;
202   /* Non-zero if we should not reorder instructions.  Changed by `.set
203      reorder' and `.set noreorder'.  */
204   int noreorder;
205   /* Non-zero if we should not permit the $at ($1) register to be used
206      in instructions.  Changed by `.set at' and `.set noat'.  */
207   int noat;
208   /* Non-zero if we should warn when a macro instruction expands into
209      more than one machine instruction.  Changed by `.set nomacro' and
210      `.set macro'.  */
211   int warn_about_macros;
212   /* Non-zero if we should not move instructions.  Changed by `.set
213      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
214   int nomove;
215   /* Non-zero if we should not optimize branches by moving the target
216      of the branch into the delay slot.  Actually, we don't perform
217      this optimization anyhow.  Changed by `.set bopt' and `.set
218      nobopt'.  */
219   int nobopt;
220   /* Non-zero if we should not autoextend mips16 instructions.
221      Changed by `.set autoextend' and `.set noautoextend'.  */
222   int noautoextend;
223   /* Restrict general purpose registers and floating point registers
224      to 32 bit.  This is initially determined when -mgp32 or -mfp32
225      is passed but can changed if the assembler code uses .set mipsN.  */
226   int gp32;
227   int fp32;
228   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
229      command line option, and the default CPU.  */
230   int arch;
231   /* True if ".set sym32" is in effect.  */
232   bfd_boolean sym32;
233 };
234
235 /* True if -mgp32 was passed.  */
236 static int file_mips_gp32 = -1;
237
238 /* True if -mfp32 was passed.  */
239 static int file_mips_fp32 = -1;
240
241 /* This is the struct we use to hold the current set of options.  Note
242    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
243    -1 to indicate that they have not been initialized.  */
244
245 static struct mips_set_options mips_opts =
246 {
247   ISA_UNKNOWN, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
248 };
249
250 /* These variables are filled in with the masks of registers used.
251    The object format code reads them and puts them in the appropriate
252    place.  */
253 unsigned long mips_gprmask;
254 unsigned long mips_cprmask[4];
255
256 /* MIPS ISA we are using for this output file.  */
257 static int file_mips_isa = ISA_UNKNOWN;
258
259 /* True if -mips16 was passed or implied by arguments passed on the
260    command line (e.g., by -march).  */
261 static int file_ase_mips16;
262
263 #define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32               \
264                               || mips_opts.isa == ISA_MIPS32R2          \
265                               || mips_opts.isa == ISA_MIPS64            \
266                               || mips_opts.isa == ISA_MIPS64R2)
267
268 /* True if -mips3d was passed or implied by arguments passed on the
269    command line (e.g., by -march).  */
270 static int file_ase_mips3d;
271
272 /* True if -mdmx was passed or implied by arguments passed on the
273    command line (e.g., by -march).  */
274 static int file_ase_mdmx;
275
276 /* True if -msmartmips was passed or implied by arguments passed on the
277    command line (e.g., by -march).  */
278 static int file_ase_smartmips;
279
280 #define ISA_SUPPORTS_SMARTMIPS (mips_opts.isa == ISA_MIPS32             \
281                                 || mips_opts.isa == ISA_MIPS32R2)
282
283 /* True if -mdsp was passed or implied by arguments passed on the
284    command line (e.g., by -march).  */
285 static int file_ase_dsp;
286
287 #define ISA_SUPPORTS_DSP_ASE (mips_opts.isa == ISA_MIPS32R2             \
288                               || mips_opts.isa == ISA_MIPS64R2)
289
290 #define ISA_SUPPORTS_DSP64_ASE (mips_opts.isa == ISA_MIPS64R2)
291
292 /* True if -mmt was passed or implied by arguments passed on the
293    command line (e.g., by -march).  */
294 static int file_ase_mt;
295
296 #define ISA_SUPPORTS_MT_ASE (mips_opts.isa == ISA_MIPS32R2              \
297                              || mips_opts.isa == ISA_MIPS64R2)
298
299 /* The argument of the -march= flag.  The architecture we are assembling.  */
300 static int file_mips_arch = CPU_UNKNOWN;
301 static const char *mips_arch_string;
302
303 /* The argument of the -mtune= flag.  The architecture for which we
304    are optimizing.  */
305 static int mips_tune = CPU_UNKNOWN;
306 static const char *mips_tune_string;
307
308 /* True when generating 32-bit code for a 64-bit processor.  */
309 static int mips_32bitmode = 0;
310
311 /* True if the given ABI requires 32-bit registers.  */
312 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
313
314 /* Likewise 64-bit registers.  */
315 #define ABI_NEEDS_64BIT_REGS(ABI)       \
316   ((ABI) == N32_ABI                     \
317    || (ABI) == N64_ABI                  \
318    || (ABI) == O64_ABI)
319
320 /*  Return true if ISA supports 64 bit wide gp registers.  */
321 #define ISA_HAS_64BIT_REGS(ISA)         \
322   ((ISA) == ISA_MIPS3                   \
323    || (ISA) == ISA_MIPS4                \
324    || (ISA) == ISA_MIPS5                \
325    || (ISA) == ISA_MIPS64               \
326    || (ISA) == ISA_MIPS64R2)
327
328 /*  Return true if ISA supports 64 bit wide float registers.  */
329 #define ISA_HAS_64BIT_FPRS(ISA)         \
330   ((ISA) == ISA_MIPS3                   \
331    || (ISA) == ISA_MIPS4                \
332    || (ISA) == ISA_MIPS5                \
333    || (ISA) == ISA_MIPS32R2             \
334    || (ISA) == ISA_MIPS64               \
335    || (ISA) == ISA_MIPS64R2)
336
337 /* Return true if ISA supports 64-bit right rotate (dror et al.)
338    instructions.  */
339 #define ISA_HAS_DROR(ISA)               \
340   ((ISA) == ISA_MIPS64R2)
341
342 /* Return true if ISA supports 32-bit right rotate (ror et al.)
343    instructions.  */
344 #define ISA_HAS_ROR(ISA)                \
345   ((ISA) == ISA_MIPS32R2                \
346    || (ISA) == ISA_MIPS64R2             \
347    || mips_opts.ase_smartmips)
348
349 /* Return true if ISA supports single-precision floats in odd registers.  */
350 #define ISA_HAS_ODD_SINGLE_FPR(ISA)     \
351   ((ISA) == ISA_MIPS32                  \
352    || (ISA) == ISA_MIPS32R2             \
353    || (ISA) == ISA_MIPS64               \
354    || (ISA) == ISA_MIPS64R2)
355
356 /* Return true if ISA supports move to/from high part of a 64-bit
357    floating-point register. */
358 #define ISA_HAS_MXHC1(ISA)              \
359   ((ISA) == ISA_MIPS32R2                \
360    || (ISA) == ISA_MIPS64R2)
361
362 #define HAVE_32BIT_GPRS                            \
363     (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
364
365 #define HAVE_32BIT_FPRS                            \
366     (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
367
368 #define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
369 #define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
370
371 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
372
373 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
374
375 /* True if relocations are stored in-place.  */
376 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
377
378 /* The ABI-derived address size.  */
379 #define HAVE_64BIT_ADDRESSES \
380   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
381 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
382
383 /* The size of symbolic constants (i.e., expressions of the form
384    "SYMBOL" or "SYMBOL + OFFSET").  */
385 #define HAVE_32BIT_SYMBOLS \
386   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
387 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
388
389 /* Addresses are loaded in different ways, depending on the address size
390    in use.  The n32 ABI Documentation also mandates the use of additions
391    with overflow checking, but existing implementations don't follow it.  */
392 #define ADDRESS_ADD_INSN                                                \
393    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
394
395 #define ADDRESS_ADDI_INSN                                               \
396    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
397
398 #define ADDRESS_LOAD_INSN                                               \
399    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
400
401 #define ADDRESS_STORE_INSN                                              \
402    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
403
404 /* Return true if the given CPU supports the MIPS16 ASE.  */
405 #define CPU_HAS_MIPS16(cpu)                                             \
406    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
407     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
408
409 /* True if CPU has a dror instruction.  */
410 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
411
412 /* True if CPU has a ror instruction.  */
413 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
414
415 /* True if mflo and mfhi can be immediately followed by instructions
416    which write to the HI and LO registers.
417
418    According to MIPS specifications, MIPS ISAs I, II, and III need
419    (at least) two instructions between the reads of HI/LO and
420    instructions which write them, and later ISAs do not.  Contradicting
421    the MIPS specifications, some MIPS IV processor user manuals (e.g.
422    the UM for the NEC Vr5000) document needing the instructions between
423    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
424    MIPS64 and later ISAs to have the interlocks, plus any specific
425    earlier-ISA CPUs for which CPU documentation declares that the
426    instructions are really interlocked.  */
427 #define hilo_interlocks \
428   (mips_opts.isa == ISA_MIPS32                        \
429    || mips_opts.isa == ISA_MIPS32R2                   \
430    || mips_opts.isa == ISA_MIPS64                     \
431    || mips_opts.isa == ISA_MIPS64R2                   \
432    || mips_opts.arch == CPU_R4010                     \
433    || mips_opts.arch == CPU_R10000                    \
434    || mips_opts.arch == CPU_R12000                    \
435    || mips_opts.arch == CPU_RM7000                    \
436    || mips_opts.arch == CPU_VR5500                    \
437    )
438
439 /* Whether the processor uses hardware interlocks to protect reads
440    from the GPRs after they are loaded from memory, and thus does not
441    require nops to be inserted.  This applies to instructions marked
442    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
443    level I.  */
444 #define gpr_interlocks \
445   (mips_opts.isa != ISA_MIPS1  \
446    || mips_opts.arch == CPU_R3900)
447
448 /* Whether the processor uses hardware interlocks to avoid delays
449    required by coprocessor instructions, and thus does not require
450    nops to be inserted.  This applies to instructions marked
451    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
452    between instructions marked INSN_WRITE_COND_CODE and ones marked
453    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
454    levels I, II, and III.  */
455 /* Itbl support may require additional care here.  */
456 #define cop_interlocks                                \
457   ((mips_opts.isa != ISA_MIPS1                        \
458     && mips_opts.isa != ISA_MIPS2                     \
459     && mips_opts.isa != ISA_MIPS3)                    \
460    || mips_opts.arch == CPU_R4300                     \
461    )
462
463 /* Whether the processor uses hardware interlocks to protect reads
464    from coprocessor registers after they are loaded from memory, and
465    thus does not require nops to be inserted.  This applies to
466    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
467    requires at MIPS ISA level I.  */
468 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
469
470 /* Is this a mfhi or mflo instruction?  */
471 #define MF_HILO_INSN(PINFO) \
472           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
473
474 /* MIPS PIC level.  */
475
476 enum mips_pic_level mips_pic;
477
478 /* 1 if we should generate 32 bit offsets from the $gp register in
479    SVR4_PIC mode.  Currently has no meaning in other modes.  */
480 static int mips_big_got = 0;
481
482 /* 1 if trap instructions should used for overflow rather than break
483    instructions.  */
484 static int mips_trap = 0;
485
486 /* 1 if double width floating point constants should not be constructed
487    by assembling two single width halves into two single width floating
488    point registers which just happen to alias the double width destination
489    register.  On some architectures this aliasing can be disabled by a bit
490    in the status register, and the setting of this bit cannot be determined
491    automatically at assemble time.  */
492 static int mips_disable_float_construction;
493
494 /* Non-zero if any .set noreorder directives were used.  */
495
496 static int mips_any_noreorder;
497
498 /* Non-zero if nops should be inserted when the register referenced in
499    an mfhi/mflo instruction is read in the next two instructions.  */
500 static int mips_7000_hilo_fix;
501
502 /* The size of the small data section.  */
503 static unsigned int g_switch_value = 8;
504 /* Whether the -G option was used.  */
505 static int g_switch_seen = 0;
506
507 #define N_RMASK 0xc4
508 #define N_VFP   0xd4
509
510 /* If we can determine in advance that GP optimization won't be
511    possible, we can skip the relaxation stuff that tries to produce
512    GP-relative references.  This makes delay slot optimization work
513    better.
514
515    This function can only provide a guess, but it seems to work for
516    gcc output.  It needs to guess right for gcc, otherwise gcc
517    will put what it thinks is a GP-relative instruction in a branch
518    delay slot.
519
520    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
521    fixed it for the non-PIC mode.  KR 95/04/07  */
522 static int nopic_need_relax (symbolS *, int);
523
524 /* handle of the OPCODE hash table */
525 static struct hash_control *op_hash = NULL;
526
527 /* The opcode hash table we use for the mips16.  */
528 static struct hash_control *mips16_op_hash = NULL;
529
530 /* This array holds the chars that always start a comment.  If the
531     pre-processor is disabled, these aren't very useful */
532 const char comment_chars[] = "#";
533
534 /* This array holds the chars that only start a comment at the beginning of
535    a line.  If the line seems to have the form '# 123 filename'
536    .line and .file directives will appear in the pre-processed output */
537 /* Note that input_file.c hand checks for '#' at the beginning of the
538    first line of the input file.  This is because the compiler outputs
539    #NO_APP at the beginning of its output.  */
540 /* Also note that C style comments are always supported.  */
541 const char line_comment_chars[] = "#";
542
543 /* This array holds machine specific line separator characters.  */
544 const char line_separator_chars[] = ";";
545
546 /* Chars that can be used to separate mant from exp in floating point nums */
547 const char EXP_CHARS[] = "eE";
548
549 /* Chars that mean this number is a floating point constant */
550 /* As in 0f12.456 */
551 /* or    0d1.2345e12 */
552 const char FLT_CHARS[] = "rRsSfFdDxXpP";
553
554 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
555    changed in read.c .  Ideally it shouldn't have to know about it at all,
556    but nothing is ideal around here.
557  */
558
559 static char *insn_error;
560
561 static int auto_align = 1;
562
563 /* When outputting SVR4 PIC code, the assembler needs to know the
564    offset in the stack frame from which to restore the $gp register.
565    This is set by the .cprestore pseudo-op, and saved in this
566    variable.  */
567 static offsetT mips_cprestore_offset = -1;
568
569 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
570    more optimizations, it can use a register value instead of a memory-saved
571    offset and even an other register than $gp as global pointer.  */
572 static offsetT mips_cpreturn_offset = -1;
573 static int mips_cpreturn_register = -1;
574 static int mips_gp_register = GP;
575 static int mips_gprel_offset = 0;
576
577 /* Whether mips_cprestore_offset has been set in the current function
578    (or whether it has already been warned about, if not).  */
579 static int mips_cprestore_valid = 0;
580
581 /* This is the register which holds the stack frame, as set by the
582    .frame pseudo-op.  This is needed to implement .cprestore.  */
583 static int mips_frame_reg = SP;
584
585 /* Whether mips_frame_reg has been set in the current function
586    (or whether it has already been warned about, if not).  */
587 static int mips_frame_reg_valid = 0;
588
589 /* To output NOP instructions correctly, we need to keep information
590    about the previous two instructions.  */
591
592 /* Whether we are optimizing.  The default value of 2 means to remove
593    unneeded NOPs and swap branch instructions when possible.  A value
594    of 1 means to not swap branches.  A value of 0 means to always
595    insert NOPs.  */
596 static int mips_optimize = 2;
597
598 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
599    equivalent to seeing no -g option at all.  */
600 static int mips_debug = 0;
601
602 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
603 #define MAX_VR4130_NOPS 4
604
605 /* The maximum number of NOPs needed to fill delay slots.  */
606 #define MAX_DELAY_NOPS 2
607
608 /* The maximum number of NOPs needed for any purpose.  */
609 #define MAX_NOPS 4
610
611 /* A list of previous instructions, with index 0 being the most recent.
612    We need to look back MAX_NOPS instructions when filling delay slots
613    or working around processor errata.  We need to look back one
614    instruction further if we're thinking about using history[0] to
615    fill a branch delay slot.  */
616 static struct mips_cl_insn history[1 + MAX_NOPS];
617
618 /* Nop instructions used by emit_nop.  */
619 static struct mips_cl_insn nop_insn, mips16_nop_insn;
620
621 /* The appropriate nop for the current mode.  */
622 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
623
624 /* If this is set, it points to a frag holding nop instructions which
625    were inserted before the start of a noreorder section.  If those
626    nops turn out to be unnecessary, the size of the frag can be
627    decreased.  */
628 static fragS *prev_nop_frag;
629
630 /* The number of nop instructions we created in prev_nop_frag.  */
631 static int prev_nop_frag_holds;
632
633 /* The number of nop instructions that we know we need in
634    prev_nop_frag.  */
635 static int prev_nop_frag_required;
636
637 /* The number of instructions we've seen since prev_nop_frag.  */
638 static int prev_nop_frag_since;
639
640 /* For ECOFF and ELF, relocations against symbols are done in two
641    parts, with a HI relocation and a LO relocation.  Each relocation
642    has only 16 bits of space to store an addend.  This means that in
643    order for the linker to handle carries correctly, it must be able
644    to locate both the HI and the LO relocation.  This means that the
645    relocations must appear in order in the relocation table.
646
647    In order to implement this, we keep track of each unmatched HI
648    relocation.  We then sort them so that they immediately precede the
649    corresponding LO relocation.  */
650
651 struct mips_hi_fixup
652 {
653   /* Next HI fixup.  */
654   struct mips_hi_fixup *next;
655   /* This fixup.  */
656   fixS *fixp;
657   /* The section this fixup is in.  */
658   segT seg;
659 };
660
661 /* The list of unmatched HI relocs.  */
662
663 static struct mips_hi_fixup *mips_hi_fixup_list;
664
665 /* The frag containing the last explicit relocation operator.
666    Null if explicit relocations have not been used.  */
667
668 static fragS *prev_reloc_op_frag;
669
670 /* Map normal MIPS register numbers to mips16 register numbers.  */
671
672 #define X ILLEGAL_REG
673 static const int mips32_to_16_reg_map[] =
674 {
675   X, X, 2, 3, 4, 5, 6, 7,
676   X, X, X, X, X, X, X, X,
677   0, 1, X, X, X, X, X, X,
678   X, X, X, X, X, X, X, X
679 };
680 #undef X
681
682 /* Map mips16 register numbers to normal MIPS register numbers.  */
683
684 static const unsigned int mips16_to_32_reg_map[] =
685 {
686   16, 17, 2, 3, 4, 5, 6, 7
687 };
688
689 /* Classifies the kind of instructions we're interested in when
690    implementing -mfix-vr4120.  */
691 enum fix_vr4120_class {
692   FIX_VR4120_MACC,
693   FIX_VR4120_DMACC,
694   FIX_VR4120_MULT,
695   FIX_VR4120_DMULT,
696   FIX_VR4120_DIV,
697   FIX_VR4120_MTHILO,
698   NUM_FIX_VR4120_CLASSES
699 };
700
701 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
702    there must be at least one other instruction between an instruction
703    of type X and an instruction of type Y.  */
704 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
705
706 /* True if -mfix-vr4120 is in force.  */
707 static int mips_fix_vr4120;
708
709 /* ...likewise -mfix-vr4130.  */
710 static int mips_fix_vr4130;
711
712 /* We don't relax branches by default, since this causes us to expand
713    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
714    fail to compute the offset before expanding the macro to the most
715    efficient expansion.  */
716
717 static int mips_relax_branch;
718 \f
719 /* The expansion of many macros depends on the type of symbol that
720    they refer to.  For example, when generating position-dependent code,
721    a macro that refers to a symbol may have two different expansions,
722    one which uses GP-relative addresses and one which uses absolute
723    addresses.  When generating SVR4-style PIC, a macro may have
724    different expansions for local and global symbols.
725
726    We handle these situations by generating both sequences and putting
727    them in variant frags.  In position-dependent code, the first sequence
728    will be the GP-relative one and the second sequence will be the
729    absolute one.  In SVR4 PIC, the first sequence will be for global
730    symbols and the second will be for local symbols.
731
732    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
733    SECOND are the lengths of the two sequences in bytes.  These fields
734    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
735    the subtype has the following flags:
736
737    RELAX_USE_SECOND
738         Set if it has been decided that we should use the second
739         sequence instead of the first.
740
741    RELAX_SECOND_LONGER
742         Set in the first variant frag if the macro's second implementation
743         is longer than its first.  This refers to the macro as a whole,
744         not an individual relaxation.
745
746    RELAX_NOMACRO
747         Set in the first variant frag if the macro appeared in a .set nomacro
748         block and if one alternative requires a warning but the other does not.
749
750    RELAX_DELAY_SLOT
751         Like RELAX_NOMACRO, but indicates that the macro appears in a branch
752         delay slot.
753
754    The frag's "opcode" points to the first fixup for relaxable code.
755
756    Relaxable macros are generated using a sequence such as:
757
758       relax_start (SYMBOL);
759       ... generate first expansion ...
760       relax_switch ();
761       ... generate second expansion ...
762       relax_end ();
763
764    The code and fixups for the unwanted alternative are discarded
765    by md_convert_frag.  */
766 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
767
768 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
769 #define RELAX_SECOND(X) ((X) & 0xff)
770 #define RELAX_USE_SECOND 0x10000
771 #define RELAX_SECOND_LONGER 0x20000
772 #define RELAX_NOMACRO 0x40000
773 #define RELAX_DELAY_SLOT 0x80000
774
775 /* Branch without likely bit.  If label is out of range, we turn:
776
777         beq reg1, reg2, label
778         delay slot
779
780    into
781
782         bne reg1, reg2, 0f
783         nop
784         j label
785      0: delay slot
786
787    with the following opcode replacements:
788
789         beq <-> bne
790         blez <-> bgtz
791         bltz <-> bgez
792         bc1f <-> bc1t
793
794         bltzal <-> bgezal  (with jal label instead of j label)
795
796    Even though keeping the delay slot instruction in the delay slot of
797    the branch would be more efficient, it would be very tricky to do
798    correctly, because we'd have to introduce a variable frag *after*
799    the delay slot instruction, and expand that instead.  Let's do it
800    the easy way for now, even if the branch-not-taken case now costs
801    one additional instruction.  Out-of-range branches are not supposed
802    to be common, anyway.
803
804    Branch likely.  If label is out of range, we turn:
805
806         beql reg1, reg2, label
807         delay slot (annulled if branch not taken)
808
809    into
810
811         beql reg1, reg2, 1f
812         nop
813         beql $0, $0, 2f
814         nop
815      1: j[al] label
816         delay slot (executed only if branch taken)
817      2:
818
819    It would be possible to generate a shorter sequence by losing the
820    likely bit, generating something like:
821
822         bne reg1, reg2, 0f
823         nop
824         j[al] label
825         delay slot (executed only if branch taken)
826      0:
827
828         beql -> bne
829         bnel -> beq
830         blezl -> bgtz
831         bgtzl -> blez
832         bltzl -> bgez
833         bgezl -> bltz
834         bc1fl -> bc1t
835         bc1tl -> bc1f
836
837         bltzall -> bgezal  (with jal label instead of j label)
838         bgezall -> bltzal  (ditto)
839
840
841    but it's not clear that it would actually improve performance.  */
842 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
843   ((relax_substateT) \
844    (0xc0000000 \
845     | ((toofar) ? 1 : 0) \
846     | ((link) ? 2 : 0) \
847     | ((likely) ? 4 : 0) \
848     | ((uncond) ? 8 : 0)))
849 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
850 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
851 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
852 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
853 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
854
855 /* For mips16 code, we use an entirely different form of relaxation.
856    mips16 supports two versions of most instructions which take
857    immediate values: a small one which takes some small value, and a
858    larger one which takes a 16 bit value.  Since branches also follow
859    this pattern, relaxing these values is required.
860
861    We can assemble both mips16 and normal MIPS code in a single
862    object.  Therefore, we need to support this type of relaxation at
863    the same time that we support the relaxation described above.  We
864    use the high bit of the subtype field to distinguish these cases.
865
866    The information we store for this type of relaxation is the
867    argument code found in the opcode file for this relocation, whether
868    the user explicitly requested a small or extended form, and whether
869    the relocation is in a jump or jal delay slot.  That tells us the
870    size of the value, and how it should be stored.  We also store
871    whether the fragment is considered to be extended or not.  We also
872    store whether this is known to be a branch to a different section,
873    whether we have tried to relax this frag yet, and whether we have
874    ever extended a PC relative fragment because of a shift count.  */
875 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
876   (0x80000000                                                   \
877    | ((type) & 0xff)                                            \
878    | ((small) ? 0x100 : 0)                                      \
879    | ((ext) ? 0x200 : 0)                                        \
880    | ((dslot) ? 0x400 : 0)                                      \
881    | ((jal_dslot) ? 0x800 : 0))
882 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
883 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
884 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
885 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
886 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
887 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
888 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
889 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
890 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
891 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
892 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
893 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
894
895 /* Is the given value a sign-extended 32-bit value?  */
896 #define IS_SEXT_32BIT_NUM(x)                                            \
897   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
898    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
899
900 /* Is the given value a sign-extended 16-bit value?  */
901 #define IS_SEXT_16BIT_NUM(x)                                            \
902   (((x) &~ (offsetT) 0x7fff) == 0                                       \
903    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
904
905 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
906 #define IS_ZEXT_32BIT_NUM(x)                                            \
907   (((x) &~ (offsetT) 0xffffffff) == 0                                   \
908    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
909
910 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
911    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
912 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
913   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
914               | (((VALUE) & (MASK)) << (SHIFT)))
915
916 /* Extract bits MASK << SHIFT from STRUCT and shift them right
917    SHIFT places.  */
918 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
919   (((STRUCT) >> (SHIFT)) & (MASK))
920
921 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
922    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
923
924    include/opcode/mips.h specifies operand fields using the macros
925    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
926    with "MIPS16OP" instead of "OP".  */
927 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
928   INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
929 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
930   INSERT_BITS ((INSN).insn_opcode, VALUE, \
931                 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
932
933 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
934 #define EXTRACT_OPERAND(FIELD, INSN) \
935   EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
936 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
937   EXTRACT_BITS ((INSN).insn_opcode, \
938                 MIPS16OP_MASK_##FIELD, \
939                 MIPS16OP_SH_##FIELD)
940 \f
941 /* Global variables used when generating relaxable macros.  See the
942    comment above RELAX_ENCODE for more details about how relaxation
943    is used.  */
944 static struct {
945   /* 0 if we're not emitting a relaxable macro.
946      1 if we're emitting the first of the two relaxation alternatives.
947      2 if we're emitting the second alternative.  */
948   int sequence;
949
950   /* The first relaxable fixup in the current frag.  (In other words,
951      the first fixup that refers to relaxable code.)  */
952   fixS *first_fixup;
953
954   /* sizes[0] says how many bytes of the first alternative are stored in
955      the current frag.  Likewise sizes[1] for the second alternative.  */
956   unsigned int sizes[2];
957
958   /* The symbol on which the choice of sequence depends.  */
959   symbolS *symbol;
960 } mips_relax;
961 \f
962 /* Global variables used to decide whether a macro needs a warning.  */
963 static struct {
964   /* True if the macro is in a branch delay slot.  */
965   bfd_boolean delay_slot_p;
966
967   /* For relaxable macros, sizes[0] is the length of the first alternative
968      in bytes and sizes[1] is the length of the second alternative.
969      For non-relaxable macros, both elements give the length of the
970      macro in bytes.  */
971   unsigned int sizes[2];
972
973   /* The first variant frag for this macro.  */
974   fragS *first_frag;
975 } mips_macro_warning;
976 \f
977 /* Prototypes for static functions.  */
978
979 #define internalError()                                                 \
980     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
981
982 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
983
984 static void append_insn
985   (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
986 static void mips_no_prev_insn (void);
987 static void mips16_macro_build
988   (expressionS *, const char *, const char *, va_list);
989 static void load_register (int, expressionS *, int);
990 static void macro_start (void);
991 static void macro_end (void);
992 static void macro (struct mips_cl_insn * ip);
993 static void mips16_macro (struct mips_cl_insn * ip);
994 #ifdef LOSING_COMPILER
995 static void macro2 (struct mips_cl_insn * ip);
996 #endif
997 static void mips_ip (char *str, struct mips_cl_insn * ip);
998 static void mips16_ip (char *str, struct mips_cl_insn * ip);
999 static void mips16_immed
1000   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
1001    unsigned long *, bfd_boolean *, unsigned short *);
1002 static size_t my_getSmallExpression
1003   (expressionS *, bfd_reloc_code_real_type *, char *);
1004 static void my_getExpression (expressionS *, char *);
1005 static void s_align (int);
1006 static void s_change_sec (int);
1007 static void s_change_section (int);
1008 static void s_cons (int);
1009 static void s_float_cons (int);
1010 static void s_mips_globl (int);
1011 static void s_option (int);
1012 static void s_mipsset (int);
1013 static void s_abicalls (int);
1014 static void s_cpload (int);
1015 static void s_cpsetup (int);
1016 static void s_cplocal (int);
1017 static void s_cprestore (int);
1018 static void s_cpreturn (int);
1019 static void s_gpvalue (int);
1020 static void s_gpword (int);
1021 static void s_gpdword (int);
1022 static void s_cpadd (int);
1023 static void s_insn (int);
1024 static void md_obj_begin (void);
1025 static void md_obj_end (void);
1026 static void s_mips_ent (int);
1027 static void s_mips_end (int);
1028 static void s_mips_frame (int);
1029 static void s_mips_mask (int reg_type);
1030 static void s_mips_stab (int);
1031 static void s_mips_weakext (int);
1032 static void s_mips_file (int);
1033 static void s_mips_loc (int);
1034 static bfd_boolean pic_need_relax (symbolS *, asection *);
1035 static int relaxed_branch_length (fragS *, asection *, int);
1036 static int validate_mips_insn (const struct mips_opcode *);
1037
1038 /* Table and functions used to map between CPU/ISA names, and
1039    ISA levels, and CPU numbers.  */
1040
1041 struct mips_cpu_info
1042 {
1043   const char *name;           /* CPU or ISA name.  */
1044   int flags;                  /* ASEs available, or ISA flag.  */
1045   int isa;                    /* ISA level.  */
1046   int cpu;                    /* CPU number (default CPU if ISA).  */
1047 };
1048
1049 #define MIPS_CPU_IS_ISA         0x0001  /* Is this an ISA?  (If 0, a CPU.) */
1050 #define MIPS_CPU_ASE_SMARTMIPS  0x0002  /* CPU implements SmartMIPS ASE */
1051 #define MIPS_CPU_ASE_DSP        0x0004  /* CPU implements DSP ASE */
1052 #define MIPS_CPU_ASE_MT         0x0008  /* CPU implements MT ASE */
1053 #define MIPS_CPU_ASE_MIPS3D     0x0010  /* CPU implements MIPS-3D ASE */
1054 #define MIPS_CPU_ASE_MDMX       0x0020  /* CPU implements MDMX ASE */
1055
1056 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1057 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1058 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1059 \f
1060 /* Pseudo-op table.
1061
1062    The following pseudo-ops from the Kane and Heinrich MIPS book
1063    should be defined here, but are currently unsupported: .alias,
1064    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1065
1066    The following pseudo-ops from the Kane and Heinrich MIPS book are
1067    specific to the type of debugging information being generated, and
1068    should be defined by the object format: .aent, .begin, .bend,
1069    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1070    .vreg.
1071
1072    The following pseudo-ops from the Kane and Heinrich MIPS book are
1073    not MIPS CPU specific, but are also not specific to the object file
1074    format.  This file is probably the best place to define them, but
1075    they are not currently supported: .asm0, .endr, .lab, .repeat,
1076    .struct.  */
1077
1078 static const pseudo_typeS mips_pseudo_table[] =
1079 {
1080   /* MIPS specific pseudo-ops.  */
1081   {"option", s_option, 0},
1082   {"set", s_mipsset, 0},
1083   {"rdata", s_change_sec, 'r'},
1084   {"sdata", s_change_sec, 's'},
1085   {"livereg", s_ignore, 0},
1086   {"abicalls", s_abicalls, 0},
1087   {"cpload", s_cpload, 0},
1088   {"cpsetup", s_cpsetup, 0},
1089   {"cplocal", s_cplocal, 0},
1090   {"cprestore", s_cprestore, 0},
1091   {"cpreturn", s_cpreturn, 0},
1092   {"gpvalue", s_gpvalue, 0},
1093   {"gpword", s_gpword, 0},
1094   {"gpdword", s_gpdword, 0},
1095   {"cpadd", s_cpadd, 0},
1096   {"insn", s_insn, 0},
1097
1098   /* Relatively generic pseudo-ops that happen to be used on MIPS
1099      chips.  */
1100   {"asciiz", stringer, 1},
1101   {"bss", s_change_sec, 'b'},
1102   {"err", s_err, 0},
1103   {"half", s_cons, 1},
1104   {"dword", s_cons, 3},
1105   {"weakext", s_mips_weakext, 0},
1106   {"origin", s_org, 0},
1107   {"repeat", s_rept, 0},
1108
1109   /* These pseudo-ops are defined in read.c, but must be overridden
1110      here for one reason or another.  */
1111   {"align", s_align, 0},
1112   {"byte", s_cons, 0},
1113   {"data", s_change_sec, 'd'},
1114   {"double", s_float_cons, 'd'},
1115   {"float", s_float_cons, 'f'},
1116   {"globl", s_mips_globl, 0},
1117   {"global", s_mips_globl, 0},
1118   {"hword", s_cons, 1},
1119   {"int", s_cons, 2},
1120   {"long", s_cons, 2},
1121   {"octa", s_cons, 4},
1122   {"quad", s_cons, 3},
1123   {"section", s_change_section, 0},
1124   {"short", s_cons, 1},
1125   {"single", s_float_cons, 'f'},
1126   {"stabn", s_mips_stab, 'n'},
1127   {"text", s_change_sec, 't'},
1128   {"word", s_cons, 2},
1129
1130   { "extern", ecoff_directive_extern, 0},
1131
1132   { NULL, NULL, 0 },
1133 };
1134
1135 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1136 {
1137   /* These pseudo-ops should be defined by the object file format.
1138      However, a.out doesn't support them, so we have versions here.  */
1139   {"aent", s_mips_ent, 1},
1140   {"bgnb", s_ignore, 0},
1141   {"end", s_mips_end, 0},
1142   {"endb", s_ignore, 0},
1143   {"ent", s_mips_ent, 0},
1144   {"file", s_mips_file, 0},
1145   {"fmask", s_mips_mask, 'F'},
1146   {"frame", s_mips_frame, 0},
1147   {"loc", s_mips_loc, 0},
1148   {"mask", s_mips_mask, 'R'},
1149   {"verstamp", s_ignore, 0},
1150   { NULL, NULL, 0 },
1151 };
1152
1153 extern void pop_insert (const pseudo_typeS *);
1154
1155 void
1156 mips_pop_insert (void)
1157 {
1158   pop_insert (mips_pseudo_table);
1159   if (! ECOFF_DEBUGGING)
1160     pop_insert (mips_nonecoff_pseudo_table);
1161 }
1162 \f
1163 /* Symbols labelling the current insn.  */
1164
1165 struct insn_label_list
1166 {
1167   struct insn_label_list *next;
1168   symbolS *label;
1169 };
1170
1171 static struct insn_label_list *free_insn_labels;
1172 #define label_list tc_segment_info_data
1173
1174 static void mips_clear_insn_labels (void);
1175
1176 static inline void
1177 mips_clear_insn_labels (void)
1178 {
1179   register struct insn_label_list **pl;
1180   segment_info_type *si;
1181
1182   if (now_seg)
1183     {
1184       for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1185         ;
1186       
1187       si = seg_info (now_seg);
1188       *pl = si->label_list;
1189       si->label_list = NULL;
1190     }
1191 }
1192
1193 \f
1194 static char *expr_end;
1195
1196 /* Expressions which appear in instructions.  These are set by
1197    mips_ip.  */
1198
1199 static expressionS imm_expr;
1200 static expressionS imm2_expr;
1201 static expressionS offset_expr;
1202
1203 /* Relocs associated with imm_expr and offset_expr.  */
1204
1205 static bfd_reloc_code_real_type imm_reloc[3]
1206   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1207 static bfd_reloc_code_real_type offset_reloc[3]
1208   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1209
1210 /* These are set by mips16_ip if an explicit extension is used.  */
1211
1212 static bfd_boolean mips16_small, mips16_ext;
1213
1214 #ifdef OBJ_ELF
1215 /* The pdr segment for per procedure frame/regmask info.  Not used for
1216    ECOFF debugging.  */
1217
1218 static segT pdr_seg;
1219 #endif
1220
1221 /* The default target format to use.  */
1222
1223 const char *
1224 mips_target_format (void)
1225 {
1226   switch (OUTPUT_FLAVOR)
1227     {
1228     case bfd_target_ecoff_flavour:
1229       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1230     case bfd_target_coff_flavour:
1231       return "pe-mips";
1232     case bfd_target_elf_flavour:
1233 #ifdef TE_VXWORKS
1234       if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1235         return (target_big_endian
1236                 ? "elf32-bigmips-vxworks"
1237                 : "elf32-littlemips-vxworks");
1238 #endif
1239 #ifdef TE_TMIPS
1240       /* This is traditional mips.  */
1241       return (target_big_endian
1242               ? (HAVE_64BIT_OBJECTS
1243                  ? "elf64-tradbigmips"
1244                  : (HAVE_NEWABI
1245                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1246               : (HAVE_64BIT_OBJECTS
1247                  ? "elf64-tradlittlemips"
1248                  : (HAVE_NEWABI
1249                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1250 #else
1251       return (target_big_endian
1252               ? (HAVE_64BIT_OBJECTS
1253                  ? "elf64-bigmips"
1254                  : (HAVE_NEWABI
1255                     ? "elf32-nbigmips" : "elf32-bigmips"))
1256               : (HAVE_64BIT_OBJECTS
1257                  ? "elf64-littlemips"
1258                  : (HAVE_NEWABI
1259                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1260 #endif
1261     default:
1262       abort ();
1263       return NULL;
1264     }
1265 }
1266
1267 /* Return the length of instruction INSN.  */
1268
1269 static inline unsigned int
1270 insn_length (const struct mips_cl_insn *insn)
1271 {
1272   if (!mips_opts.mips16)
1273     return 4;
1274   return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1275 }
1276
1277 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1278
1279 static void
1280 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1281 {
1282   size_t i;
1283
1284   insn->insn_mo = mo;
1285   insn->use_extend = FALSE;
1286   insn->extend = 0;
1287   insn->insn_opcode = mo->match;
1288   insn->frag = NULL;
1289   insn->where = 0;
1290   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1291     insn->fixp[i] = NULL;
1292   insn->fixed_p = (mips_opts.noreorder > 0);
1293   insn->noreorder_p = (mips_opts.noreorder > 0);
1294   insn->mips16_absolute_jump_p = 0;
1295 }
1296
1297 /* Install INSN at the location specified by its "frag" and "where" fields.  */
1298
1299 static void
1300 install_insn (const struct mips_cl_insn *insn)
1301 {
1302   char *f = insn->frag->fr_literal + insn->where;
1303   if (!mips_opts.mips16)
1304     md_number_to_chars (f, insn->insn_opcode, 4);
1305   else if (insn->mips16_absolute_jump_p)
1306     {
1307       md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1308       md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1309     }
1310   else
1311     {
1312       if (insn->use_extend)
1313         {
1314           md_number_to_chars (f, 0xf000 | insn->extend, 2);
1315           f += 2;
1316         }
1317       md_number_to_chars (f, insn->insn_opcode, 2);
1318     }
1319 }
1320
1321 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
1322    and install the opcode in the new location.  */
1323
1324 static void
1325 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1326 {
1327   size_t i;
1328
1329   insn->frag = frag;
1330   insn->where = where;
1331   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1332     if (insn->fixp[i] != NULL)
1333       {
1334         insn->fixp[i]->fx_frag = frag;
1335         insn->fixp[i]->fx_where = where;
1336       }
1337   install_insn (insn);
1338 }
1339
1340 /* Add INSN to the end of the output.  */
1341
1342 static void
1343 add_fixed_insn (struct mips_cl_insn *insn)
1344 {
1345   char *f = frag_more (insn_length (insn));
1346   move_insn (insn, frag_now, f - frag_now->fr_literal);
1347 }
1348
1349 /* Start a variant frag and move INSN to the start of the variant part,
1350    marking it as fixed.  The other arguments are as for frag_var.  */
1351
1352 static void
1353 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1354                   relax_substateT subtype, symbolS *symbol, offsetT offset)
1355 {
1356   frag_grow (max_chars);
1357   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1358   insn->fixed_p = 1;
1359   frag_var (rs_machine_dependent, max_chars, var,
1360             subtype, symbol, offset, NULL);
1361 }
1362
1363 /* Insert N copies of INSN into the history buffer, starting at
1364    position FIRST.  Neither FIRST nor N need to be clipped.  */
1365
1366 static void
1367 insert_into_history (unsigned int first, unsigned int n,
1368                      const struct mips_cl_insn *insn)
1369 {
1370   if (mips_relax.sequence != 2)
1371     {
1372       unsigned int i;
1373
1374       for (i = ARRAY_SIZE (history); i-- > first;)
1375         if (i >= first + n)
1376           history[i] = history[i - n];
1377         else
1378           history[i] = *insn;
1379     }
1380 }
1381
1382 /* Emit a nop instruction, recording it in the history buffer.  */
1383
1384 static void
1385 emit_nop (void)
1386 {
1387   add_fixed_insn (NOP_INSN);
1388   insert_into_history (0, 1, NOP_INSN);
1389 }
1390
1391 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
1392    the idea is to make it obvious at a glance that each errata is
1393    included.  */
1394
1395 static void
1396 init_vr4120_conflicts (void)
1397 {
1398 #define CONFLICT(FIRST, SECOND) \
1399     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1400
1401   /* Errata 21 - [D]DIV[U] after [D]MACC */
1402   CONFLICT (MACC, DIV);
1403   CONFLICT (DMACC, DIV);
1404
1405   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
1406   CONFLICT (DMULT, DMULT);
1407   CONFLICT (DMULT, DMACC);
1408   CONFLICT (DMACC, DMULT);
1409   CONFLICT (DMACC, DMACC);
1410
1411   /* Errata 24 - MT{LO,HI} after [D]MACC */
1412   CONFLICT (MACC, MTHILO);
1413   CONFLICT (DMACC, MTHILO);
1414
1415   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1416      instruction is executed immediately after a MACC or DMACC
1417      instruction, the result of [either instruction] is incorrect."  */
1418   CONFLICT (MACC, MULT);
1419   CONFLICT (MACC, DMULT);
1420   CONFLICT (DMACC, MULT);
1421   CONFLICT (DMACC, DMULT);
1422
1423   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1424      executed immediately after a DMULT, DMULTU, DIV, DIVU,
1425      DDIV or DDIVU instruction, the result of the MACC or
1426      DMACC instruction is incorrect.".  */
1427   CONFLICT (DMULT, MACC);
1428   CONFLICT (DMULT, DMACC);
1429   CONFLICT (DIV, MACC);
1430   CONFLICT (DIV, DMACC);
1431
1432 #undef CONFLICT
1433 }
1434
1435 struct regname {
1436   const char *name;
1437   unsigned int num;
1438 };
1439
1440 #define RTYPE_MASK      0x1ff00
1441 #define RTYPE_NUM       0x00100
1442 #define RTYPE_FPU       0x00200
1443 #define RTYPE_FCC       0x00400
1444 #define RTYPE_VEC       0x00800
1445 #define RTYPE_GP        0x01000
1446 #define RTYPE_CP0       0x02000
1447 #define RTYPE_PC        0x04000
1448 #define RTYPE_ACC       0x08000
1449 #define RTYPE_CCC       0x10000
1450 #define RNUM_MASK       0x000ff
1451 #define RWARN           0x80000
1452
1453 #define GENERIC_REGISTER_NUMBERS \
1454     {"$0",      RTYPE_NUM | 0},  \
1455     {"$1",      RTYPE_NUM | 1},  \
1456     {"$2",      RTYPE_NUM | 2},  \
1457     {"$3",      RTYPE_NUM | 3},  \
1458     {"$4",      RTYPE_NUM | 4},  \
1459     {"$5",      RTYPE_NUM | 5},  \
1460     {"$6",      RTYPE_NUM | 6},  \
1461     {"$7",      RTYPE_NUM | 7},  \
1462     {"$8",      RTYPE_NUM | 8},  \
1463     {"$9",      RTYPE_NUM | 9},  \
1464     {"$10",     RTYPE_NUM | 10}, \
1465     {"$11",     RTYPE_NUM | 11}, \
1466     {"$12",     RTYPE_NUM | 12}, \
1467     {"$13",     RTYPE_NUM | 13}, \
1468     {"$14",     RTYPE_NUM | 14}, \
1469     {"$15",     RTYPE_NUM | 15}, \
1470     {"$16",     RTYPE_NUM | 16}, \
1471     {"$17",     RTYPE_NUM | 17}, \
1472     {"$18",     RTYPE_NUM | 18}, \
1473     {"$19",     RTYPE_NUM | 19}, \
1474     {"$20",     RTYPE_NUM | 20}, \
1475     {"$21",     RTYPE_NUM | 21}, \
1476     {"$22",     RTYPE_NUM | 22}, \
1477     {"$23",     RTYPE_NUM | 23}, \
1478     {"$24",     RTYPE_NUM | 24}, \
1479     {"$25",     RTYPE_NUM | 25}, \
1480     {"$26",     RTYPE_NUM | 26}, \
1481     {"$27",     RTYPE_NUM | 27}, \
1482     {"$28",     RTYPE_NUM | 28}, \
1483     {"$29",     RTYPE_NUM | 29}, \
1484     {"$30",     RTYPE_NUM | 30}, \
1485     {"$31",     RTYPE_NUM | 31} 
1486
1487 #define FPU_REGISTER_NAMES       \
1488     {"$f0",     RTYPE_FPU | 0},  \
1489     {"$f1",     RTYPE_FPU | 1},  \
1490     {"$f2",     RTYPE_FPU | 2},  \
1491     {"$f3",     RTYPE_FPU | 3},  \
1492     {"$f4",     RTYPE_FPU | 4},  \
1493     {"$f5",     RTYPE_FPU | 5},  \
1494     {"$f6",     RTYPE_FPU | 6},  \
1495     {"$f7",     RTYPE_FPU | 7},  \
1496     {"$f8",     RTYPE_FPU | 8},  \
1497     {"$f9",     RTYPE_FPU | 9},  \
1498     {"$f10",    RTYPE_FPU | 10}, \
1499     {"$f11",    RTYPE_FPU | 11}, \
1500     {"$f12",    RTYPE_FPU | 12}, \
1501     {"$f13",    RTYPE_FPU | 13}, \
1502     {"$f14",    RTYPE_FPU | 14}, \
1503     {"$f15",    RTYPE_FPU | 15}, \
1504     {"$f16",    RTYPE_FPU | 16}, \
1505     {"$f17",    RTYPE_FPU | 17}, \
1506     {"$f18",    RTYPE_FPU | 18}, \
1507     {"$f19",    RTYPE_FPU | 19}, \
1508     {"$f20",    RTYPE_FPU | 20}, \
1509     {"$f21",    RTYPE_FPU | 21}, \
1510     {"$f22",    RTYPE_FPU | 22}, \
1511     {"$f23",    RTYPE_FPU | 23}, \
1512     {"$f24",    RTYPE_FPU | 24}, \
1513     {"$f25",    RTYPE_FPU | 25}, \
1514     {"$f26",    RTYPE_FPU | 26}, \
1515     {"$f27",    RTYPE_FPU | 27}, \
1516     {"$f28",    RTYPE_FPU | 28}, \
1517     {"$f29",    RTYPE_FPU | 29}, \
1518     {"$f30",    RTYPE_FPU | 30}, \
1519     {"$f31",    RTYPE_FPU | 31}
1520
1521 #define FPU_CONDITION_CODE_NAMES \
1522     {"$fcc0",   RTYPE_FCC | 0},  \
1523     {"$fcc1",   RTYPE_FCC | 1},  \
1524     {"$fcc2",   RTYPE_FCC | 2},  \
1525     {"$fcc3",   RTYPE_FCC | 3},  \
1526     {"$fcc4",   RTYPE_FCC | 4},  \
1527     {"$fcc5",   RTYPE_FCC | 5},  \
1528     {"$fcc6",   RTYPE_FCC | 6},  \
1529     {"$fcc7",   RTYPE_FCC | 7}
1530
1531 #define COPROC_CONDITION_CODE_NAMES         \
1532     {"$cc0",    RTYPE_FCC | RTYPE_CCC | 0}, \
1533     {"$cc1",    RTYPE_FCC | RTYPE_CCC | 1}, \
1534     {"$cc2",    RTYPE_FCC | RTYPE_CCC | 2}, \
1535     {"$cc3",    RTYPE_FCC | RTYPE_CCC | 3}, \
1536     {"$cc4",    RTYPE_FCC | RTYPE_CCC | 4}, \
1537     {"$cc5",    RTYPE_FCC | RTYPE_CCC | 5}, \
1538     {"$cc6",    RTYPE_FCC | RTYPE_CCC | 6}, \
1539     {"$cc7",    RTYPE_FCC | RTYPE_CCC | 7}
1540
1541 #define N32N64_SYMBOLIC_REGISTER_NAMES \
1542     {"$a4",     RTYPE_GP | 8},  \
1543     {"$a5",     RTYPE_GP | 9},  \
1544     {"$a6",     RTYPE_GP | 10}, \
1545     {"$a7",     RTYPE_GP | 11}, \
1546     {"$ta0",    RTYPE_GP | 8},  /* alias for $a4 */ \
1547     {"$ta1",    RTYPE_GP | 9},  /* alias for $a5 */ \
1548     {"$ta2",    RTYPE_GP | 10}, /* alias for $a6 */ \
1549     {"$ta3",    RTYPE_GP | 11}, /* alias for $a7 */ \
1550     {"$t0",     RTYPE_GP | 12}, \
1551     {"$t1",     RTYPE_GP | 13}, \
1552     {"$t2",     RTYPE_GP | 14}, \
1553     {"$t3",     RTYPE_GP | 15}
1554
1555 #define O32_SYMBOLIC_REGISTER_NAMES \
1556     {"$t0",     RTYPE_GP | 8},  \
1557     {"$t1",     RTYPE_GP | 9},  \
1558     {"$t2",     RTYPE_GP | 10}, \
1559     {"$t3",     RTYPE_GP | 11}, \
1560     {"$t4",     RTYPE_GP | 12}, \
1561     {"$t5",     RTYPE_GP | 13}, \
1562     {"$t6",     RTYPE_GP | 14}, \
1563     {"$t7",     RTYPE_GP | 15}, \
1564     {"$ta0",    RTYPE_GP | 12}, /* alias for $t4 */ \
1565     {"$ta1",    RTYPE_GP | 13}, /* alias for $t5 */ \
1566     {"$ta2",    RTYPE_GP | 14}, /* alias for $t6 */ \
1567     {"$ta3",    RTYPE_GP | 15}  /* alias for $t7 */ 
1568
1569 /* Remaining symbolic register names */
1570 #define SYMBOLIC_REGISTER_NAMES \
1571     {"$zero",   RTYPE_GP | 0},  \
1572     {"$at",     RTYPE_GP | 1},  \
1573     {"$AT",     RTYPE_GP | 1},  \
1574     {"$v0",     RTYPE_GP | 2},  \
1575     {"$v1",     RTYPE_GP | 3},  \
1576     {"$a0",     RTYPE_GP | 4},  \
1577     {"$a1",     RTYPE_GP | 5},  \
1578     {"$a2",     RTYPE_GP | 6},  \
1579     {"$a3",     RTYPE_GP | 7},  \
1580     {"$s0",     RTYPE_GP | 16}, \
1581     {"$s1",     RTYPE_GP | 17}, \
1582     {"$s2",     RTYPE_GP | 18}, \
1583     {"$s3",     RTYPE_GP | 19}, \
1584     {"$s4",     RTYPE_GP | 20}, \
1585     {"$s5",     RTYPE_GP | 21}, \
1586     {"$s6",     RTYPE_GP | 22}, \
1587     {"$s7",     RTYPE_GP | 23}, \
1588     {"$t8",     RTYPE_GP | 24}, \
1589     {"$t9",     RTYPE_GP | 25}, \
1590     {"$k0",     RTYPE_GP | 26}, \
1591     {"$kt0",    RTYPE_GP | 26}, \
1592     {"$k1",     RTYPE_GP | 27}, \
1593     {"$kt1",    RTYPE_GP | 27}, \
1594     {"$gp",     RTYPE_GP | 28}, \
1595     {"$sp",     RTYPE_GP | 29}, \
1596     {"$s8",     RTYPE_GP | 30}, \
1597     {"$fp",     RTYPE_GP | 30}, \
1598     {"$ra",     RTYPE_GP | 31}
1599
1600 #define MIPS16_SPECIAL_REGISTER_NAMES \
1601     {"$pc",     RTYPE_PC | 0}
1602
1603 #define MDMX_VECTOR_REGISTER_NAMES \
1604     /* {"$v0",  RTYPE_VEC | 0},  clash with REG 2 above */ \
1605     /* {"$v1",  RTYPE_VEC | 1},  clash with REG 3 above */ \
1606     {"$v2",     RTYPE_VEC | 2},  \
1607     {"$v3",     RTYPE_VEC | 3},  \
1608     {"$v4",     RTYPE_VEC | 4},  \
1609     {"$v5",     RTYPE_VEC | 5},  \
1610     {"$v6",     RTYPE_VEC | 6},  \
1611     {"$v7",     RTYPE_VEC | 7},  \
1612     {"$v8",     RTYPE_VEC | 8},  \
1613     {"$v9",     RTYPE_VEC | 9},  \
1614     {"$v10",    RTYPE_VEC | 10}, \
1615     {"$v11",    RTYPE_VEC | 11}, \
1616     {"$v12",    RTYPE_VEC | 12}, \
1617     {"$v13",    RTYPE_VEC | 13}, \
1618     {"$v14",    RTYPE_VEC | 14}, \
1619     {"$v15",    RTYPE_VEC | 15}, \
1620     {"$v16",    RTYPE_VEC | 16}, \
1621     {"$v17",    RTYPE_VEC | 17}, \
1622     {"$v18",    RTYPE_VEC | 18}, \
1623     {"$v19",    RTYPE_VEC | 19}, \
1624     {"$v20",    RTYPE_VEC | 20}, \
1625     {"$v21",    RTYPE_VEC | 21}, \
1626     {"$v22",    RTYPE_VEC | 22}, \
1627     {"$v23",    RTYPE_VEC | 23}, \
1628     {"$v24",    RTYPE_VEC | 24}, \
1629     {"$v25",    RTYPE_VEC | 25}, \
1630     {"$v26",    RTYPE_VEC | 26}, \
1631     {"$v27",    RTYPE_VEC | 27}, \
1632     {"$v28",    RTYPE_VEC | 28}, \
1633     {"$v29",    RTYPE_VEC | 29}, \
1634     {"$v30",    RTYPE_VEC | 30}, \
1635     {"$v31",    RTYPE_VEC | 31}
1636
1637 #define MIPS_DSP_ACCUMULATOR_NAMES \
1638     {"$ac0",    RTYPE_ACC | 0}, \
1639     {"$ac1",    RTYPE_ACC | 1}, \
1640     {"$ac2",    RTYPE_ACC | 2}, \
1641     {"$ac3",    RTYPE_ACC | 3}
1642
1643 static const struct regname reg_names[] = {
1644   GENERIC_REGISTER_NUMBERS,
1645   FPU_REGISTER_NAMES,
1646   FPU_CONDITION_CODE_NAMES,
1647   COPROC_CONDITION_CODE_NAMES,
1648
1649   /* The $txx registers depends on the abi,
1650      these will be added later into the symbol table from
1651      one of the tables below once mips_abi is set after 
1652      parsing of arguments from the command line. */
1653   SYMBOLIC_REGISTER_NAMES,
1654
1655   MIPS16_SPECIAL_REGISTER_NAMES,
1656   MDMX_VECTOR_REGISTER_NAMES,
1657   MIPS_DSP_ACCUMULATOR_NAMES,
1658   {0, 0}
1659 };
1660
1661 static const struct regname reg_names_o32[] = {
1662   O32_SYMBOLIC_REGISTER_NAMES,
1663   {0, 0}
1664 };
1665
1666 static const struct regname reg_names_n32n64[] = {
1667   N32N64_SYMBOLIC_REGISTER_NAMES,
1668   {0, 0}
1669 };
1670
1671 static int
1672 reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1673 {
1674   symbolS *symbolP;
1675   char *e;
1676   char save_c;
1677   int reg = -1;
1678
1679   /* Find end of name.  */
1680   e = *s;
1681   if (is_name_beginner (*e))
1682     ++e;
1683   while (is_part_of_name (*e))
1684     ++e;
1685
1686   /* Terminate name.  */
1687   save_c = *e;
1688   *e = '\0';
1689
1690   /* Look for a register symbol.  */
1691   if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1692     {
1693       int r = S_GET_VALUE (symbolP);
1694       if (r & types)
1695         reg = r & RNUM_MASK;
1696       else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1697         /* Convert GP reg $v0/1 to MDMX reg $v0/1!  */
1698         reg = (r & RNUM_MASK) - 2;
1699     }
1700   /* Else see if this is a register defined in an itbl entry.  */
1701   else if ((types & RTYPE_GP) && itbl_have_entries)
1702     {
1703       char *n = *s;
1704       unsigned long r;
1705
1706       if (*n == '$')
1707         ++n;
1708       if (itbl_get_reg_val (n, &r))
1709         reg = r & RNUM_MASK;
1710     }
1711
1712   /* Advance to next token if a register was recognised.  */
1713   if (reg >= 0)
1714     *s = e;
1715   else if (types & RWARN)
1716     as_warn ("Unrecognized register name `%s'", *s);
1717
1718   *e = save_c;
1719   if (regnop)
1720     *regnop = reg;
1721   return reg >= 0;
1722 }
1723
1724 /* This function is called once, at assembler startup time.  It should set up
1725    all the tables, etc. that the MD part of the assembler will need.  */
1726
1727 void
1728 md_begin (void)
1729 {
1730   const char *retval = NULL;
1731   int i = 0;
1732   int broken = 0;
1733
1734   if (mips_pic != NO_PIC)
1735     {
1736       if (g_switch_seen && g_switch_value != 0)
1737         as_bad (_("-G may not be used in position-independent code"));
1738       g_switch_value = 0;
1739     }
1740
1741   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1742     as_warn (_("Could not set architecture and machine"));
1743
1744   op_hash = hash_new ();
1745
1746   for (i = 0; i < NUMOPCODES;)
1747     {
1748       const char *name = mips_opcodes[i].name;
1749
1750       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1751       if (retval != NULL)
1752         {
1753           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1754                    mips_opcodes[i].name, retval);
1755           /* Probably a memory allocation problem?  Give up now.  */
1756           as_fatal (_("Broken assembler.  No assembly attempted."));
1757         }
1758       do
1759         {
1760           if (mips_opcodes[i].pinfo != INSN_MACRO)
1761             {
1762               if (!validate_mips_insn (&mips_opcodes[i]))
1763                 broken = 1;
1764               if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1765                 {
1766                   create_insn (&nop_insn, mips_opcodes + i);
1767                   nop_insn.fixed_p = 1;
1768                 }
1769             }
1770           ++i;
1771         }
1772       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1773     }
1774
1775   mips16_op_hash = hash_new ();
1776
1777   i = 0;
1778   while (i < bfd_mips16_num_opcodes)
1779     {
1780       const char *name = mips16_opcodes[i].name;
1781
1782       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1783       if (retval != NULL)
1784         as_fatal (_("internal: can't hash `%s': %s"),
1785                   mips16_opcodes[i].name, retval);
1786       do
1787         {
1788           if (mips16_opcodes[i].pinfo != INSN_MACRO
1789               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1790                   != mips16_opcodes[i].match))
1791             {
1792               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1793                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1794               broken = 1;
1795             }
1796           if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1797             {
1798               create_insn (&mips16_nop_insn, mips16_opcodes + i);
1799               mips16_nop_insn.fixed_p = 1;
1800             }
1801           ++i;
1802         }
1803       while (i < bfd_mips16_num_opcodes
1804              && strcmp (mips16_opcodes[i].name, name) == 0);
1805     }
1806
1807   if (broken)
1808     as_fatal (_("Broken assembler.  No assembly attempted."));
1809
1810   /* We add all the general register names to the symbol table.  This
1811      helps us detect invalid uses of them.  */
1812   for (i = 0; reg_names[i].name; i++) 
1813     symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
1814                                      reg_names[i].num, // & RNUM_MASK,
1815                                      &zero_address_frag));
1816   if (HAVE_NEWABI)
1817     for (i = 0; reg_names_n32n64[i].name; i++) 
1818       symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
1819                                        reg_names_n32n64[i].num, // & RNUM_MASK,
1820                                        &zero_address_frag));
1821   else
1822     for (i = 0; reg_names_o32[i].name; i++) 
1823       symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
1824                                        reg_names_o32[i].num, // & RNUM_MASK,
1825                                        &zero_address_frag));
1826
1827   mips_no_prev_insn ();
1828
1829   mips_gprmask = 0;
1830   mips_cprmask[0] = 0;
1831   mips_cprmask[1] = 0;
1832   mips_cprmask[2] = 0;
1833   mips_cprmask[3] = 0;
1834
1835   /* set the default alignment for the text section (2**2) */
1836   record_alignment (text_section, 2);
1837
1838   bfd_set_gp_size (stdoutput, g_switch_value);
1839
1840 #ifdef OBJ_ELF
1841   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1842     {
1843       /* On a native system other than VxWorks, sections must be aligned
1844          to 16 byte boundaries.  When configured for an embedded ELF
1845          target, we don't bother.  */
1846       if (strcmp (TARGET_OS, "elf") != 0
1847           && strcmp (TARGET_OS, "vxworks") != 0)
1848         {
1849           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1850           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1851           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1852         }
1853
1854       /* Create a .reginfo section for register masks and a .mdebug
1855          section for debugging information.  */
1856       {
1857         segT seg;
1858         subsegT subseg;
1859         flagword flags;
1860         segT sec;
1861
1862         seg = now_seg;
1863         subseg = now_subseg;
1864
1865         /* The ABI says this section should be loaded so that the
1866            running program can access it.  However, we don't load it
1867            if we are configured for an embedded target */
1868         flags = SEC_READONLY | SEC_DATA;
1869         if (strcmp (TARGET_OS, "elf") != 0)
1870           flags |= SEC_ALLOC | SEC_LOAD;
1871
1872         if (mips_abi != N64_ABI)
1873           {
1874             sec = subseg_new (".reginfo", (subsegT) 0);
1875
1876             bfd_set_section_flags (stdoutput, sec, flags);
1877             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1878
1879             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1880           }
1881         else
1882           {
1883             /* The 64-bit ABI uses a .MIPS.options section rather than
1884                .reginfo section.  */
1885             sec = subseg_new (".MIPS.options", (subsegT) 0);
1886             bfd_set_section_flags (stdoutput, sec, flags);
1887             bfd_set_section_alignment (stdoutput, sec, 3);
1888
1889             /* Set up the option header.  */
1890             {
1891               Elf_Internal_Options opthdr;
1892               char *f;
1893
1894               opthdr.kind = ODK_REGINFO;
1895               opthdr.size = (sizeof (Elf_External_Options)
1896                              + sizeof (Elf64_External_RegInfo));
1897               opthdr.section = 0;
1898               opthdr.info = 0;
1899               f = frag_more (sizeof (Elf_External_Options));
1900               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1901                                              (Elf_External_Options *) f);
1902
1903               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1904             }
1905           }
1906
1907         if (ECOFF_DEBUGGING)
1908           {
1909             sec = subseg_new (".mdebug", (subsegT) 0);
1910             (void) bfd_set_section_flags (stdoutput, sec,
1911                                           SEC_HAS_CONTENTS | SEC_READONLY);
1912             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1913           }
1914         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1915           {
1916             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1917             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1918                                           SEC_READONLY | SEC_RELOC
1919                                           | SEC_DEBUGGING);
1920             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1921           }
1922
1923         subseg_set (seg, subseg);
1924       }
1925     }
1926 #endif /* OBJ_ELF */
1927
1928   if (! ECOFF_DEBUGGING)
1929     md_obj_begin ();
1930
1931   if (mips_fix_vr4120)
1932     init_vr4120_conflicts ();
1933 }
1934
1935 void
1936 md_mips_end (void)
1937 {
1938   if (! ECOFF_DEBUGGING)
1939     md_obj_end ();
1940 }
1941
1942 void
1943 md_assemble (char *str)
1944 {
1945   struct mips_cl_insn insn;
1946   bfd_reloc_code_real_type unused_reloc[3]
1947     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1948
1949   imm_expr.X_op = O_absent;
1950   imm2_expr.X_op = O_absent;
1951   offset_expr.X_op = O_absent;
1952   imm_reloc[0] = BFD_RELOC_UNUSED;
1953   imm_reloc[1] = BFD_RELOC_UNUSED;
1954   imm_reloc[2] = BFD_RELOC_UNUSED;
1955   offset_reloc[0] = BFD_RELOC_UNUSED;
1956   offset_reloc[1] = BFD_RELOC_UNUSED;
1957   offset_reloc[2] = BFD_RELOC_UNUSED;
1958
1959   if (mips_opts.mips16)
1960     mips16_ip (str, &insn);
1961   else
1962     {
1963       mips_ip (str, &insn);
1964       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1965             str, insn.insn_opcode));
1966     }
1967
1968   if (insn_error)
1969     {
1970       as_bad ("%s `%s'", insn_error, str);
1971       return;
1972     }
1973
1974   if (insn.insn_mo->pinfo == INSN_MACRO)
1975     {
1976       macro_start ();
1977       if (mips_opts.mips16)
1978         mips16_macro (&insn);
1979       else
1980         macro (&insn);
1981       macro_end ();
1982     }
1983   else
1984     {
1985       if (imm_expr.X_op != O_absent)
1986         append_insn (&insn, &imm_expr, imm_reloc);
1987       else if (offset_expr.X_op != O_absent)
1988         append_insn (&insn, &offset_expr, offset_reloc);
1989       else
1990         append_insn (&insn, NULL, unused_reloc);
1991     }
1992 }
1993
1994 /* Return true if the given relocation might need a matching %lo().
1995    This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1996    need a matching %lo() when applied to local symbols.  */
1997
1998 static inline bfd_boolean
1999 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
2000 {
2001   return (HAVE_IN_PLACE_ADDENDS
2002           && (reloc == BFD_RELOC_HI16_S
2003               || reloc == BFD_RELOC_MIPS16_HI16_S
2004               /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
2005                  all GOT16 relocations evaluate to "G".  */
2006               || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
2007 }
2008
2009 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
2010    relocation.  */
2011
2012 static inline bfd_boolean
2013 fixup_has_matching_lo_p (fixS *fixp)
2014 {
2015   return (fixp->fx_next != NULL
2016           && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
2017              || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
2018           && fixp->fx_addsy == fixp->fx_next->fx_addsy
2019           && fixp->fx_offset == fixp->fx_next->fx_offset);
2020 }
2021
2022 /* See whether instruction IP reads register REG.  CLASS is the type
2023    of register.  */
2024
2025 static int
2026 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
2027                enum mips_regclass class)
2028 {
2029   if (class == MIPS16_REG)
2030     {
2031       assert (mips_opts.mips16);
2032       reg = mips16_to_32_reg_map[reg];
2033       class = MIPS_GR_REG;
2034     }
2035
2036   /* Don't report on general register ZERO, since it never changes.  */
2037   if (class == MIPS_GR_REG && reg == ZERO)
2038     return 0;
2039
2040   if (class == MIPS_FP_REG)
2041     {
2042       assert (! mips_opts.mips16);
2043       /* If we are called with either $f0 or $f1, we must check $f0.
2044          This is not optimal, because it will introduce an unnecessary
2045          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
2046          need to distinguish reading both $f0 and $f1 or just one of
2047          them.  Note that we don't have to check the other way,
2048          because there is no instruction that sets both $f0 and $f1
2049          and requires a delay.  */
2050       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
2051           && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
2052               == (reg &~ (unsigned) 1)))
2053         return 1;
2054       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
2055           && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
2056               == (reg &~ (unsigned) 1)))
2057         return 1;
2058     }
2059   else if (! mips_opts.mips16)
2060     {
2061       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
2062           && EXTRACT_OPERAND (RS, *ip) == reg)
2063         return 1;
2064       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
2065           && EXTRACT_OPERAND (RT, *ip) == reg)
2066         return 1;
2067     }
2068   else
2069     {
2070       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
2071           && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
2072         return 1;
2073       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
2074           && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
2075         return 1;
2076       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
2077           && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
2078               == reg))
2079         return 1;
2080       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2081         return 1;
2082       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2083         return 1;
2084       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2085         return 1;
2086       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
2087           && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
2088         return 1;
2089     }
2090
2091   return 0;
2092 }
2093
2094 /* This function returns true if modifying a register requires a
2095    delay.  */
2096
2097 static int
2098 reg_needs_delay (unsigned int reg)
2099 {
2100   unsigned long prev_pinfo;
2101
2102   prev_pinfo = history[0].insn_mo->pinfo;
2103   if (! mips_opts.noreorder
2104       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2105            && ! gpr_interlocks)
2106           || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2107               && ! cop_interlocks)))
2108     {
2109       /* A load from a coprocessor or from memory.  All load delays
2110          delay the use of general register rt for one instruction.  */
2111       /* Itbl support may require additional care here.  */
2112       know (prev_pinfo & INSN_WRITE_GPR_T);
2113       if (reg == EXTRACT_OPERAND (RT, history[0]))
2114         return 1;
2115     }
2116
2117   return 0;
2118 }
2119
2120 /* Move all labels in insn_labels to the current insertion point.  */
2121
2122 static void
2123 mips_move_labels (void)
2124 {
2125   segment_info_type *si = seg_info (now_seg);
2126   struct insn_label_list *l;
2127   valueT val;
2128
2129   for (l = si->label_list; l != NULL; l = l->next)
2130     {
2131       assert (S_GET_SEGMENT (l->label) == now_seg);
2132       symbol_set_frag (l->label, frag_now);
2133       val = (valueT) frag_now_fix ();
2134       /* mips16 text labels are stored as odd.  */
2135       if (mips_opts.mips16)
2136         ++val;
2137       S_SET_VALUE (l->label, val);
2138     }
2139 }
2140
2141 static bfd_boolean
2142 s_is_linkonce (symbolS *sym, segT from_seg)
2143 {
2144   bfd_boolean linkonce = FALSE;
2145   segT symseg = S_GET_SEGMENT (sym);
2146
2147   if (symseg != from_seg && !S_IS_LOCAL (sym))
2148     {
2149       if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
2150         linkonce = TRUE;
2151 #ifdef OBJ_ELF
2152       /* The GNU toolchain uses an extension for ELF: a section
2153          beginning with the magic string .gnu.linkonce is a
2154          linkonce section.  */
2155       if (strncmp (segment_name (symseg), ".gnu.linkonce",
2156                    sizeof ".gnu.linkonce" - 1) == 0)
2157         linkonce = TRUE;
2158 #endif
2159     }
2160   return linkonce;
2161 }
2162
2163 /* Mark instruction labels in mips16 mode.  This permits the linker to
2164    handle them specially, such as generating jalx instructions when
2165    needed.  We also make them odd for the duration of the assembly, in
2166    order to generate the right sort of code.  We will make them even
2167    in the adjust_symtab routine, while leaving them marked.  This is
2168    convenient for the debugger and the disassembler.  The linker knows
2169    to make them odd again.  */
2170
2171 static void
2172 mips16_mark_labels (void)
2173 {
2174   segment_info_type *si = seg_info (now_seg);
2175   struct insn_label_list *l;
2176
2177   if (!mips_opts.mips16)
2178     return;
2179
2180   for (l = si->label_list; l != NULL; l = l->next)
2181    {
2182       symbolS *label = l->label;
2183
2184 #if defined(OBJ_ELF) || defined(OBJ_MAYBE_ELF)
2185       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
2186         S_SET_OTHER (label, STO_MIPS16);
2187 #endif
2188       if ((S_GET_VALUE (label) & 1) == 0
2189         /* Don't adjust the address if the label is global or weak, or
2190            in a link-once section, since we'll be emitting symbol reloc
2191            references to it which will be patched up by the linker, and
2192            the final value of the symbol may or may not be MIPS16.  */
2193           && ! S_IS_WEAK (label)
2194           && ! S_IS_EXTERNAL (label)
2195           && ! s_is_linkonce (label, now_seg))
2196         S_SET_VALUE (label, S_GET_VALUE (label) | 1);
2197     }
2198 }
2199
2200 /* End the current frag.  Make it a variant frag and record the
2201    relaxation info.  */
2202
2203 static void
2204 relax_close_frag (void)
2205 {
2206   mips_macro_warning.first_frag = frag_now;
2207   frag_var (rs_machine_dependent, 0, 0,
2208             RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
2209             mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2210
2211   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2212   mips_relax.first_fixup = 0;
2213 }
2214
2215 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
2216    See the comment above RELAX_ENCODE for more details.  */
2217
2218 static void
2219 relax_start (symbolS *symbol)
2220 {
2221   assert (mips_relax.sequence == 0);
2222   mips_relax.sequence = 1;
2223   mips_relax.symbol = symbol;
2224 }
2225
2226 /* Start generating the second version of a relaxable sequence.
2227    See the comment above RELAX_ENCODE for more details.  */
2228
2229 static void
2230 relax_switch (void)
2231 {
2232   assert (mips_relax.sequence == 1);
2233   mips_relax.sequence = 2;
2234 }
2235
2236 /* End the current relaxable sequence.  */
2237
2238 static void
2239 relax_end (void)
2240 {
2241   assert (mips_relax.sequence == 2);
2242   relax_close_frag ();
2243   mips_relax.sequence = 0;
2244 }
2245
2246 /* Classify an instruction according to the FIX_VR4120_* enumeration.
2247    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2248    by VR4120 errata.  */
2249
2250 static unsigned int
2251 classify_vr4120_insn (const char *name)
2252 {
2253   if (strncmp (name, "macc", 4) == 0)
2254     return FIX_VR4120_MACC;
2255   if (strncmp (name, "dmacc", 5) == 0)
2256     return FIX_VR4120_DMACC;
2257   if (strncmp (name, "mult", 4) == 0)
2258     return FIX_VR4120_MULT;
2259   if (strncmp (name, "dmult", 5) == 0)
2260     return FIX_VR4120_DMULT;
2261   if (strstr (name, "div"))
2262     return FIX_VR4120_DIV;
2263   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2264     return FIX_VR4120_MTHILO;
2265   return NUM_FIX_VR4120_CLASSES;
2266 }
2267
2268 /* Return the number of instructions that must separate INSN1 and INSN2,
2269    where INSN1 is the earlier instruction.  Return the worst-case value
2270    for any INSN2 if INSN2 is null.  */
2271
2272 static unsigned int
2273 insns_between (const struct mips_cl_insn *insn1,
2274                const struct mips_cl_insn *insn2)
2275 {
2276   unsigned long pinfo1, pinfo2;
2277
2278   /* This function needs to know which pinfo flags are set for INSN2
2279      and which registers INSN2 uses.  The former is stored in PINFO2 and
2280      the latter is tested via INSN2_USES_REG.  If INSN2 is null, PINFO2
2281      will have every flag set and INSN2_USES_REG will always return true.  */
2282   pinfo1 = insn1->insn_mo->pinfo;
2283   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
2284
2285 #define INSN2_USES_REG(REG, CLASS) \
2286    (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2287
2288   /* For most targets, write-after-read dependencies on the HI and LO
2289      registers must be separated by at least two instructions.  */
2290   if (!hilo_interlocks)
2291     {
2292       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2293         return 2;
2294       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2295         return 2;
2296     }
2297
2298   /* If we're working around r7000 errata, there must be two instructions
2299      between an mfhi or mflo and any instruction that uses the result.  */
2300   if (mips_7000_hilo_fix
2301       && MF_HILO_INSN (pinfo1)
2302       && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2303     return 2;
2304
2305   /* If working around VR4120 errata, check for combinations that need
2306      a single intervening instruction.  */
2307   if (mips_fix_vr4120)
2308     {
2309       unsigned int class1, class2;
2310
2311       class1 = classify_vr4120_insn (insn1->insn_mo->name);
2312       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
2313         {
2314           if (insn2 == NULL)
2315             return 1;
2316           class2 = classify_vr4120_insn (insn2->insn_mo->name);
2317           if (vr4120_conflicts[class1] & (1 << class2))
2318             return 1;
2319         }
2320     }
2321
2322   if (!mips_opts.mips16)
2323     {
2324       /* Check for GPR or coprocessor load delays.  All such delays
2325          are on the RT register.  */
2326       /* Itbl support may require additional care here.  */
2327       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2328           || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
2329         {
2330           know (pinfo1 & INSN_WRITE_GPR_T);
2331           if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2332             return 1;
2333         }
2334
2335       /* Check for generic coprocessor hazards.
2336
2337          This case is not handled very well.  There is no special
2338          knowledge of CP0 handling, and the coprocessors other than
2339          the floating point unit are not distinguished at all.  */
2340       /* Itbl support may require additional care here. FIXME!
2341          Need to modify this to include knowledge about
2342          user specified delays!  */
2343       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2344                || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2345         {
2346           /* Handle cases where INSN1 writes to a known general coprocessor
2347              register.  There must be a one instruction delay before INSN2
2348              if INSN2 reads that register, otherwise no delay is needed.  */
2349           if (pinfo1 & INSN_WRITE_FPR_T)
2350             {
2351               if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2352                 return 1;
2353             }
2354           else if (pinfo1 & INSN_WRITE_FPR_S)
2355             {
2356               if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2357                 return 1;
2358             }
2359           else
2360             {
2361               /* Read-after-write dependencies on the control registers
2362                  require a two-instruction gap.  */
2363               if ((pinfo1 & INSN_WRITE_COND_CODE)
2364                   && (pinfo2 & INSN_READ_COND_CODE))
2365                 return 2;
2366
2367               /* We don't know exactly what INSN1 does.  If INSN2 is
2368                  also a coprocessor instruction, assume there must be
2369                  a one instruction gap.  */
2370               if (pinfo2 & INSN_COP)
2371                 return 1;
2372             }
2373         }
2374
2375       /* Check for read-after-write dependencies on the coprocessor
2376          control registers in cases where INSN1 does not need a general
2377          coprocessor delay.  This means that INSN1 is a floating point
2378          comparison instruction.  */
2379       /* Itbl support may require additional care here.  */
2380       else if (!cop_interlocks
2381                && (pinfo1 & INSN_WRITE_COND_CODE)
2382                && (pinfo2 & INSN_READ_COND_CODE))
2383         return 1;
2384     }
2385
2386 #undef INSN2_USES_REG
2387
2388   return 0;
2389 }
2390
2391 /* Return the number of nops that would be needed to work around the
2392    VR4130 mflo/mfhi errata if instruction INSN immediately followed
2393    the MAX_VR4130_NOPS instructions described by HISTORY.  */
2394
2395 static int
2396 nops_for_vr4130 (const struct mips_cl_insn *history,
2397                  const struct mips_cl_insn *insn)
2398 {
2399   int i, j, reg;
2400
2401   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
2402      are not affected by the errata.  */
2403   if (insn != 0
2404       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2405           || strcmp (insn->insn_mo->name, "mtlo") == 0
2406           || strcmp (insn->insn_mo->name, "mthi") == 0))
2407     return 0;
2408
2409   /* Search for the first MFLO or MFHI.  */
2410   for (i = 0; i < MAX_VR4130_NOPS; i++)
2411     if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2412       {
2413         /* Extract the destination register.  */
2414         if (mips_opts.mips16)
2415           reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2416         else
2417           reg = EXTRACT_OPERAND (RD, history[i]);
2418
2419         /* No nops are needed if INSN reads that register.  */
2420         if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2421           return 0;
2422
2423         /* ...or if any of the intervening instructions do.  */
2424         for (j = 0; j < i; j++)
2425           if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2426             return 0;
2427
2428         return MAX_VR4130_NOPS - i;
2429       }
2430   return 0;
2431 }
2432
2433 /* Return the number of nops that would be needed if instruction INSN
2434    immediately followed the MAX_NOPS instructions given by HISTORY,
2435    where HISTORY[0] is the most recent instruction.  If INSN is null,
2436    return the worse-case number of nops for any instruction.  */
2437
2438 static int
2439 nops_for_insn (const struct mips_cl_insn *history,
2440                const struct mips_cl_insn *insn)
2441 {
2442   int i, nops, tmp_nops;
2443
2444   nops = 0;
2445   for (i = 0; i < MAX_DELAY_NOPS; i++)
2446     if (!history[i].noreorder_p)
2447       {
2448         tmp_nops = insns_between (history + i, insn) - i;
2449         if (tmp_nops > nops)
2450           nops = tmp_nops;
2451       }
2452
2453   if (mips_fix_vr4130)
2454     {
2455       tmp_nops = nops_for_vr4130 (history, insn);
2456       if (tmp_nops > nops)
2457         nops = tmp_nops;
2458     }
2459
2460   return nops;
2461 }
2462
2463 /* The variable arguments provide NUM_INSNS extra instructions that
2464    might be added to HISTORY.  Return the largest number of nops that
2465    would be needed after the extended sequence.  */
2466
2467 static int
2468 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2469 {
2470   va_list args;
2471   struct mips_cl_insn buffer[MAX_NOPS];
2472   struct mips_cl_insn *cursor;
2473   int nops;
2474
2475   va_start (args, history);
2476   cursor = buffer + num_insns;
2477   memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2478   while (cursor > buffer)
2479     *--cursor = *va_arg (args, const struct mips_cl_insn *);
2480
2481   nops = nops_for_insn (buffer, NULL);
2482   va_end (args);
2483   return nops;
2484 }
2485
2486 /* Like nops_for_insn, but if INSN is a branch, take into account the
2487    worst-case delay for the branch target.  */
2488
2489 static int
2490 nops_for_insn_or_target (const struct mips_cl_insn *history,
2491                          const struct mips_cl_insn *insn)
2492 {
2493   int nops, tmp_nops;
2494
2495   nops = nops_for_insn (history, insn);
2496   if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2497                               | INSN_COND_BRANCH_DELAY
2498                               | INSN_COND_BRANCH_LIKELY))
2499     {
2500       tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2501       if (tmp_nops > nops)
2502         nops = tmp_nops;
2503     }
2504   else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2505     {
2506       tmp_nops = nops_for_sequence (1, history, insn);
2507       if (tmp_nops > nops)
2508         nops = tmp_nops;
2509     }
2510   return nops;
2511 }
2512
2513 /* Output an instruction.  IP is the instruction information.
2514    ADDRESS_EXPR is an operand of the instruction to be used with
2515    RELOC_TYPE.  */
2516
2517 static void
2518 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2519              bfd_reloc_code_real_type *reloc_type)
2520 {
2521   unsigned long prev_pinfo, pinfo;
2522   relax_stateT prev_insn_frag_type = 0;
2523   bfd_boolean relaxed_branch = FALSE;
2524   segment_info_type *si = seg_info (now_seg);
2525
2526   /* Mark instruction labels in mips16 mode.  */
2527   mips16_mark_labels ();
2528
2529   prev_pinfo = history[0].insn_mo->pinfo;
2530   pinfo = ip->insn_mo->pinfo;
2531
2532   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2533     {
2534       /* There are a lot of optimizations we could do that we don't.
2535          In particular, we do not, in general, reorder instructions.
2536          If you use gcc with optimization, it will reorder
2537          instructions and generally do much more optimization then we
2538          do here; repeating all that work in the assembler would only
2539          benefit hand written assembly code, and does not seem worth
2540          it.  */
2541       int nops = (mips_optimize == 0
2542                   ? nops_for_insn (history, NULL)
2543                   : nops_for_insn_or_target (history, ip));
2544       if (nops > 0)
2545         {
2546           fragS *old_frag;
2547           unsigned long old_frag_offset;
2548           int i;
2549
2550           old_frag = frag_now;
2551           old_frag_offset = frag_now_fix ();
2552
2553           for (i = 0; i < nops; i++)
2554             emit_nop ();
2555
2556           if (listing)
2557             {
2558               listing_prev_line ();
2559               /* We may be at the start of a variant frag.  In case we
2560                  are, make sure there is enough space for the frag
2561                  after the frags created by listing_prev_line.  The
2562                  argument to frag_grow here must be at least as large
2563                  as the argument to all other calls to frag_grow in
2564                  this file.  We don't have to worry about being in the
2565                  middle of a variant frag, because the variants insert
2566                  all needed nop instructions themselves.  */
2567               frag_grow (40);
2568             }
2569
2570           mips_move_labels ();
2571
2572 #ifndef NO_ECOFF_DEBUGGING
2573           if (ECOFF_DEBUGGING)
2574             ecoff_fix_loc (old_frag, old_frag_offset);
2575 #endif
2576         }
2577     }
2578   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2579     {
2580       /* Work out how many nops in prev_nop_frag are needed by IP.  */
2581       int nops = nops_for_insn_or_target (history, ip);
2582       assert (nops <= prev_nop_frag_holds);
2583
2584       /* Enforce NOPS as a minimum.  */
2585       if (nops > prev_nop_frag_required)
2586         prev_nop_frag_required = nops;
2587
2588       if (prev_nop_frag_holds == prev_nop_frag_required)
2589         {
2590           /* Settle for the current number of nops.  Update the history
2591              accordingly (for the benefit of any future .set reorder code).  */
2592           prev_nop_frag = NULL;
2593           insert_into_history (prev_nop_frag_since,
2594                                prev_nop_frag_holds, NOP_INSN);
2595         }
2596       else
2597         {
2598           /* Allow this instruction to replace one of the nops that was
2599              tentatively added to prev_nop_frag.  */
2600           prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2601           prev_nop_frag_holds--;
2602           prev_nop_frag_since++;
2603         }
2604     }
2605
2606 #ifdef OBJ_ELF
2607   /* The value passed to dwarf2_emit_insn is the distance between
2608      the beginning of the current instruction and the address that
2609      should be recorded in the debug tables.  For MIPS16 debug info
2610      we want to use ISA-encoded addresses, so we pass -1 for an
2611      address higher by one than the current.  */
2612   dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2613 #endif
2614
2615   /* Record the frag type before frag_var.  */
2616   if (history[0].frag)
2617     prev_insn_frag_type = history[0].frag->fr_type;
2618
2619   if (address_expr
2620       && *reloc_type == BFD_RELOC_16_PCREL_S2
2621       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2622           || pinfo & INSN_COND_BRANCH_LIKELY)
2623       && mips_relax_branch
2624       /* Don't try branch relaxation within .set nomacro, or within
2625          .set noat if we use $at for PIC computations.  If it turns
2626          out that the branch was out-of-range, we'll get an error.  */
2627       && !mips_opts.warn_about_macros
2628       && !(mips_opts.noat && mips_pic != NO_PIC)
2629       && !mips_opts.mips16)
2630     {
2631       relaxed_branch = TRUE;
2632       add_relaxed_insn (ip, (relaxed_branch_length
2633                              (NULL, NULL,
2634                               (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2635                               : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2636                               : 0)), 4,
2637                         RELAX_BRANCH_ENCODE
2638                         (pinfo & INSN_UNCOND_BRANCH_DELAY,
2639                          pinfo & INSN_COND_BRANCH_LIKELY,
2640                          pinfo & INSN_WRITE_GPR_31,
2641                          0),
2642                         address_expr->X_add_symbol,
2643                         address_expr->X_add_number);
2644       *reloc_type = BFD_RELOC_UNUSED;
2645     }
2646   else if (*reloc_type > BFD_RELOC_UNUSED)
2647     {
2648       /* We need to set up a variant frag.  */
2649       assert (mips_opts.mips16 && address_expr != NULL);
2650       add_relaxed_insn (ip, 4, 0,
2651                         RELAX_MIPS16_ENCODE
2652                         (*reloc_type - BFD_RELOC_UNUSED,
2653                          mips16_small, mips16_ext,
2654                          prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2655                          history[0].mips16_absolute_jump_p),
2656                         make_expr_symbol (address_expr), 0);
2657     }
2658   else if (mips_opts.mips16
2659            && ! ip->use_extend
2660            && *reloc_type != BFD_RELOC_MIPS16_JMP)
2661     {
2662       if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2663         /* Make sure there is enough room to swap this instruction with
2664            a following jump instruction.  */
2665         frag_grow (6);
2666       add_fixed_insn (ip);
2667     }
2668   else
2669     {
2670       if (mips_opts.mips16
2671           && mips_opts.noreorder
2672           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2673         as_warn (_("extended instruction in delay slot"));
2674
2675       if (mips_relax.sequence)
2676         {
2677           /* If we've reached the end of this frag, turn it into a variant
2678              frag and record the information for the instructions we've
2679              written so far.  */
2680           if (frag_room () < 4)
2681             relax_close_frag ();
2682           mips_relax.sizes[mips_relax.sequence - 1] += 4;
2683         }
2684
2685       if (mips_relax.sequence != 2)
2686         mips_macro_warning.sizes[0] += 4;
2687       if (mips_relax.sequence != 1)
2688         mips_macro_warning.sizes[1] += 4;
2689
2690       if (mips_opts.mips16)
2691         {
2692           ip->fixed_p = 1;
2693           ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2694         }
2695       add_fixed_insn (ip);
2696     }
2697
2698   if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2699     {
2700       if (address_expr->X_op == O_constant)
2701         {
2702           unsigned int tmp;
2703
2704           switch (*reloc_type)
2705             {
2706             case BFD_RELOC_32:
2707               ip->insn_opcode |= address_expr->X_add_number;
2708               break;
2709
2710             case BFD_RELOC_MIPS_HIGHEST:
2711               tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2712               ip->insn_opcode |= tmp & 0xffff;
2713               break;
2714
2715             case BFD_RELOC_MIPS_HIGHER:
2716               tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2717               ip->insn_opcode |= tmp & 0xffff;
2718               break;
2719
2720             case BFD_RELOC_HI16_S:
2721               tmp = (address_expr->X_add_number + 0x8000) >> 16;
2722               ip->insn_opcode |= tmp & 0xffff;
2723               break;
2724
2725             case BFD_RELOC_HI16:
2726               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2727               break;
2728
2729             case BFD_RELOC_UNUSED:
2730             case BFD_RELOC_LO16:
2731             case BFD_RELOC_MIPS_GOT_DISP:
2732               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2733               break;
2734
2735             case BFD_RELOC_MIPS_JMP:
2736               if ((address_expr->X_add_number & 3) != 0)
2737                 as_bad (_("jump to misaligned address (0x%lx)"),
2738                         (unsigned long) address_expr->X_add_number);
2739               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2740               break;
2741
2742             case BFD_RELOC_MIPS16_JMP:
2743               if ((address_expr->X_add_number & 3) != 0)
2744                 as_bad (_("jump to misaligned address (0x%lx)"),
2745                         (unsigned long) address_expr->X_add_number);
2746               ip->insn_opcode |=
2747                 (((address_expr->X_add_number & 0x7c0000) << 3)
2748                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2749                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2750               break;
2751
2752             case BFD_RELOC_16_PCREL_S2:
2753               if ((address_expr->X_add_number & 3) != 0)
2754                 as_bad (_("branch to misaligned address (0x%lx)"),
2755                         (unsigned long) address_expr->X_add_number);
2756               if (mips_relax_branch)
2757                 goto need_reloc;
2758               if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2759                 as_bad (_("branch address range overflow (0x%lx)"),
2760                         (unsigned long) address_expr->X_add_number);
2761               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2762               break;
2763
2764             default:
2765               internalError ();
2766             }
2767         }
2768       else if (*reloc_type < BFD_RELOC_UNUSED)
2769         need_reloc:
2770         {
2771           reloc_howto_type *howto;
2772           int i;
2773
2774           /* In a compound relocation, it is the final (outermost)
2775              operator that determines the relocated field.  */
2776           for (i = 1; i < 3; i++)
2777             if (reloc_type[i] == BFD_RELOC_UNUSED)
2778               break;
2779
2780           howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2781           ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2782                                      bfd_get_reloc_size (howto),
2783                                      address_expr,
2784                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2785                                      reloc_type[0]);
2786
2787           /* These relocations can have an addend that won't fit in
2788              4 octets for 64bit assembly.  */
2789           if (HAVE_64BIT_GPRS
2790               && ! howto->partial_inplace
2791               && (reloc_type[0] == BFD_RELOC_16
2792                   || reloc_type[0] == BFD_RELOC_32
2793                   || reloc_type[0] == BFD_RELOC_MIPS_JMP
2794                   || reloc_type[0] == BFD_RELOC_HI16_S
2795                   || reloc_type[0] == BFD_RELOC_LO16
2796                   || reloc_type[0] == BFD_RELOC_GPREL16
2797                   || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2798                   || reloc_type[0] == BFD_RELOC_GPREL32
2799                   || reloc_type[0] == BFD_RELOC_64
2800                   || reloc_type[0] == BFD_RELOC_CTOR
2801                   || reloc_type[0] == BFD_RELOC_MIPS_SUB
2802                   || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2803                   || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2804                   || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2805                   || reloc_type[0] == BFD_RELOC_MIPS_REL16
2806                   || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2807                   || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2808                   || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2809                   || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2810             ip->fixp[0]->fx_no_overflow = 1;
2811
2812           if (mips_relax.sequence)
2813             {
2814               if (mips_relax.first_fixup == 0)
2815                 mips_relax.first_fixup = ip->fixp[0];
2816             }
2817           else if (reloc_needs_lo_p (*reloc_type))
2818             {
2819               struct mips_hi_fixup *hi_fixup;
2820
2821               /* Reuse the last entry if it already has a matching %lo.  */
2822               hi_fixup = mips_hi_fixup_list;
2823               if (hi_fixup == 0
2824                   || !fixup_has_matching_lo_p (hi_fixup->fixp))
2825                 {
2826                   hi_fixup = ((struct mips_hi_fixup *)
2827                               xmalloc (sizeof (struct mips_hi_fixup)));
2828                   hi_fixup->next = mips_hi_fixup_list;
2829                   mips_hi_fixup_list = hi_fixup;
2830                 }
2831               hi_fixup->fixp = ip->fixp[0];
2832               hi_fixup->seg = now_seg;
2833             }
2834
2835           /* Add fixups for the second and third relocations, if given.
2836              Note that the ABI allows the second relocation to be
2837              against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2838              moment we only use RSS_UNDEF, but we could add support
2839              for the others if it ever becomes necessary.  */
2840           for (i = 1; i < 3; i++)
2841             if (reloc_type[i] != BFD_RELOC_UNUSED)
2842               {
2843                 ip->fixp[i] = fix_new (ip->frag, ip->where,
2844                                        ip->fixp[0]->fx_size, NULL, 0,
2845                                        FALSE, reloc_type[i]);
2846
2847                 /* Use fx_tcbit to mark compound relocs.  */
2848                 ip->fixp[0]->fx_tcbit = 1;
2849                 ip->fixp[i]->fx_tcbit = 1;
2850               }
2851         }
2852     }
2853   install_insn (ip);
2854
2855   /* Update the register mask information.  */
2856   if (! mips_opts.mips16)
2857     {
2858       if (pinfo & INSN_WRITE_GPR_D)
2859         mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2860       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2861         mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2862       if (pinfo & INSN_READ_GPR_S)
2863         mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2864       if (pinfo & INSN_WRITE_GPR_31)
2865         mips_gprmask |= 1 << RA;
2866       if (pinfo & INSN_WRITE_FPR_D)
2867         mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2868       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2869         mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2870       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2871         mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2872       if ((pinfo & INSN_READ_FPR_R) != 0)
2873         mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2874       if (pinfo & INSN_COP)
2875         {
2876           /* We don't keep enough information to sort these cases out.
2877              The itbl support does keep this information however, although
2878              we currently don't support itbl fprmats as part of the cop
2879              instruction.  May want to add this support in the future.  */
2880         }
2881       /* Never set the bit for $0, which is always zero.  */
2882       mips_gprmask &= ~1 << 0;
2883     }
2884   else
2885     {
2886       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2887         mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2888       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2889         mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2890       if (pinfo & MIPS16_INSN_WRITE_Z)
2891         mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2892       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2893         mips_gprmask |= 1 << TREG;
2894       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2895         mips_gprmask |= 1 << SP;
2896       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2897         mips_gprmask |= 1 << RA;
2898       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2899         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2900       if (pinfo & MIPS16_INSN_READ_Z)
2901         mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2902       if (pinfo & MIPS16_INSN_READ_GPR_X)
2903         mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2904     }
2905
2906   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2907     {
2908       /* Filling the branch delay slot is more complex.  We try to
2909          switch the branch with the previous instruction, which we can
2910          do if the previous instruction does not set up a condition
2911          that the branch tests and if the branch is not itself the
2912          target of any branch.  */
2913       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2914           || (pinfo & INSN_COND_BRANCH_DELAY))
2915         {
2916           if (mips_optimize < 2
2917               /* If we have seen .set volatile or .set nomove, don't
2918                  optimize.  */
2919               || mips_opts.nomove != 0
2920               /* We can't swap if the previous instruction's position
2921                  is fixed.  */
2922               || history[0].fixed_p
2923               /* If the previous previous insn was in a .set
2924                  noreorder, we can't swap.  Actually, the MIPS
2925                  assembler will swap in this situation.  However, gcc
2926                  configured -with-gnu-as will generate code like
2927                    .set noreorder
2928                    lw   $4,XXX
2929                    .set reorder
2930                    INSN
2931                    bne  $4,$0,foo
2932                  in which we can not swap the bne and INSN.  If gcc is
2933                  not configured -with-gnu-as, it does not output the
2934                  .set pseudo-ops.  */
2935               || history[1].noreorder_p
2936               /* If the branch is itself the target of a branch, we
2937                  can not swap.  We cheat on this; all we check for is
2938                  whether there is a label on this instruction.  If
2939                  there are any branches to anything other than a
2940                  label, users must use .set noreorder.  */
2941               || si->label_list != NULL
2942               /* If the previous instruction is in a variant frag
2943                  other than this branch's one, we cannot do the swap.
2944                  This does not apply to the mips16, which uses variant
2945                  frags for different purposes.  */
2946               || (! mips_opts.mips16
2947                   && prev_insn_frag_type == rs_machine_dependent)
2948               /* Check for conflicts between the branch and the instructions
2949                  before the candidate delay slot.  */
2950               || nops_for_insn (history + 1, ip) > 0
2951               /* Check for conflicts between the swapped sequence and the
2952                  target of the branch.  */
2953               || nops_for_sequence (2, history + 1, ip, history) > 0
2954               /* We do not swap with a trap instruction, since it
2955                  complicates trap handlers to have the trap
2956                  instruction be in a delay slot.  */
2957               || (prev_pinfo & INSN_TRAP)
2958               /* If the branch reads a register that the previous
2959                  instruction sets, we can not swap.  */
2960               || (! mips_opts.mips16
2961                   && (prev_pinfo & INSN_WRITE_GPR_T)
2962                   && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2963                                     MIPS_GR_REG))
2964               || (! mips_opts.mips16
2965                   && (prev_pinfo & INSN_WRITE_GPR_D)
2966                   && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2967                                     MIPS_GR_REG))
2968               || (mips_opts.mips16
2969                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2970                        && (insn_uses_reg
2971                            (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2972                             MIPS16_REG)))
2973                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2974                           && (insn_uses_reg
2975                               (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2976                                MIPS16_REG)))
2977                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2978                           && (insn_uses_reg
2979                               (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2980                                MIPS16_REG)))
2981                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2982                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2983                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2984                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2985                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2986                           && insn_uses_reg (ip,
2987                                             MIPS16OP_EXTRACT_REG32R
2988                                               (history[0].insn_opcode),
2989                                             MIPS_GR_REG))))
2990               /* If the branch writes a register that the previous
2991                  instruction sets, we can not swap (we know that
2992                  branches write only to RD or to $31).  */
2993               || (! mips_opts.mips16
2994                   && (prev_pinfo & INSN_WRITE_GPR_T)
2995                   && (((pinfo & INSN_WRITE_GPR_D)
2996                        && (EXTRACT_OPERAND (RT, history[0])
2997                            == EXTRACT_OPERAND (RD, *ip)))
2998                       || ((pinfo & INSN_WRITE_GPR_31)
2999                           && EXTRACT_OPERAND (RT, history[0]) == RA)))
3000               || (! mips_opts.mips16
3001                   && (prev_pinfo & INSN_WRITE_GPR_D)
3002                   && (((pinfo & INSN_WRITE_GPR_D)
3003                        && (EXTRACT_OPERAND (RD, history[0])
3004                            == EXTRACT_OPERAND (RD, *ip)))
3005                       || ((pinfo & INSN_WRITE_GPR_31)
3006                           && EXTRACT_OPERAND (RD, history[0]) == RA)))
3007               || (mips_opts.mips16
3008                   && (pinfo & MIPS16_INSN_WRITE_31)
3009                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
3010                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
3011                           && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
3012                               == RA))))
3013               /* If the branch writes a register that the previous
3014                  instruction reads, we can not swap (we know that
3015                  branches only write to RD or to $31).  */
3016               || (! mips_opts.mips16
3017                   && (pinfo & INSN_WRITE_GPR_D)
3018                   && insn_uses_reg (&history[0],
3019                                     EXTRACT_OPERAND (RD, *ip),
3020                                     MIPS_GR_REG))
3021               || (! mips_opts.mips16
3022                   && (pinfo & INSN_WRITE_GPR_31)
3023                   && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
3024               || (mips_opts.mips16
3025                   && (pinfo & MIPS16_INSN_WRITE_31)
3026                   && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
3027               /* If one instruction sets a condition code and the
3028                  other one uses a condition code, we can not swap.  */
3029               || ((pinfo & INSN_READ_COND_CODE)
3030                   && (prev_pinfo & INSN_WRITE_COND_CODE))
3031               || ((pinfo & INSN_WRITE_COND_CODE)
3032                   && (prev_pinfo & INSN_READ_COND_CODE))
3033               /* If the previous instruction uses the PC, we can not
3034                  swap.  */
3035               || (mips_opts.mips16
3036                   && (prev_pinfo & MIPS16_INSN_READ_PC))
3037               /* If the previous instruction had a fixup in mips16
3038                  mode, we can not swap.  This normally means that the
3039                  previous instruction was a 4 byte branch anyhow.  */
3040               || (mips_opts.mips16 && history[0].fixp[0])
3041               /* If the previous instruction is a sync, sync.l, or
3042                  sync.p, we can not swap.  */
3043               || (prev_pinfo & INSN_SYNC))
3044             {
3045               if (mips_opts.mips16
3046                   && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3047                   && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3048                   && ISA_SUPPORTS_MIPS16E)
3049                 {
3050                   /* Convert MIPS16 jr/jalr into a "compact" jump.  */
3051                   ip->insn_opcode |= 0x0080;
3052                   install_insn (ip);
3053                   insert_into_history (0, 1, ip);
3054                 } 
3055               else
3056                 {
3057                   /* We could do even better for unconditional branches to
3058                      portions of this object file; we could pick up the
3059                      instruction at the destination, put it in the delay
3060                      slot, and bump the destination address.  */
3061                   insert_into_history (0, 1, ip);
3062                   emit_nop ();
3063                 }
3064                 
3065               if (mips_relax.sequence)
3066                 mips_relax.sizes[mips_relax.sequence - 1] += 4;
3067             }
3068           else
3069             {
3070               /* It looks like we can actually do the swap.  */
3071               struct mips_cl_insn delay = history[0];
3072               if (mips_opts.mips16)
3073                 {
3074                   know (delay.frag == ip->frag);
3075                   move_insn (ip, delay.frag, delay.where);
3076                   move_insn (&delay, ip->frag, ip->where + insn_length (ip));
3077                 }
3078               else if (relaxed_branch)
3079                 {
3080                   /* Add the delay slot instruction to the end of the
3081                      current frag and shrink the fixed part of the
3082                      original frag.  If the branch occupies the tail of
3083                      the latter, move it backwards to cover the gap.  */
3084                   delay.frag->fr_fix -= 4;
3085                   if (delay.frag == ip->frag)
3086                     move_insn (ip, ip->frag, ip->where - 4);
3087                   add_fixed_insn (&delay);
3088                 }
3089               else
3090                 {
3091                   move_insn (&delay, ip->frag, ip->where);
3092                   move_insn (ip, history[0].frag, history[0].where);
3093                 }
3094               history[0] = *ip;
3095               delay.fixed_p = 1;
3096               insert_into_history (0, 1, &delay);
3097             }
3098
3099           /* If that was an unconditional branch, forget the previous
3100              insn information.  */
3101           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
3102             mips_no_prev_insn ();
3103         }
3104       else if (pinfo & INSN_COND_BRANCH_LIKELY)
3105         {
3106           /* We don't yet optimize a branch likely.  What we should do
3107              is look at the target, copy the instruction found there
3108              into the delay slot, and increment the branch to jump to
3109              the next instruction.  */
3110           insert_into_history (0, 1, ip);
3111           emit_nop ();
3112         }
3113       else
3114         insert_into_history (0, 1, ip);
3115     }
3116   else
3117     insert_into_history (0, 1, ip);
3118
3119   /* We just output an insn, so the next one doesn't have a label.  */
3120   mips_clear_insn_labels ();
3121 }
3122
3123 /* Forget that there was any previous instruction or label.  */
3124
3125 static void
3126 mips_no_prev_insn (void)
3127 {
3128   prev_nop_frag = NULL;
3129   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
3130   mips_clear_insn_labels ();
3131 }
3132
3133 /* This function must be called before we emit something other than
3134    instructions.  It is like mips_no_prev_insn except that it inserts
3135    any NOPS that might be needed by previous instructions.  */
3136
3137 void
3138 mips_emit_delays (void)
3139 {
3140   if (! mips_opts.noreorder)
3141     {
3142       int nops = nops_for_insn (history, NULL);
3143       if (nops > 0)
3144         {
3145           while (nops-- > 0)
3146             add_fixed_insn (NOP_INSN);
3147           mips_move_labels ();
3148         }
3149     }
3150   mips_no_prev_insn ();
3151 }
3152
3153 /* Start a (possibly nested) noreorder block.  */
3154
3155 static void
3156 start_noreorder (void)
3157 {
3158   if (mips_opts.noreorder == 0)
3159     {
3160       unsigned int i;
3161       int nops;
3162
3163       /* None of the instructions before the .set noreorder can be moved.  */
3164       for (i = 0; i < ARRAY_SIZE (history); i++)
3165         history[i].fixed_p = 1;
3166
3167       /* Insert any nops that might be needed between the .set noreorder
3168          block and the previous instructions.  We will later remove any
3169          nops that turn out not to be needed.  */
3170       nops = nops_for_insn (history, NULL);
3171       if (nops > 0)
3172         {
3173           if (mips_optimize != 0)
3174             {
3175               /* Record the frag which holds the nop instructions, so
3176                  that we can remove them if we don't need them.  */
3177               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3178               prev_nop_frag = frag_now;
3179               prev_nop_frag_holds = nops;
3180               prev_nop_frag_required = 0;
3181               prev_nop_frag_since = 0;
3182             }
3183
3184           for (; nops > 0; --nops)
3185             add_fixed_insn (NOP_INSN);
3186
3187           /* Move on to a new frag, so that it is safe to simply
3188              decrease the size of prev_nop_frag.  */
3189           frag_wane (frag_now);
3190           frag_new (0);
3191           mips_move_labels ();
3192         }
3193       mips16_mark_labels ();
3194       mips_clear_insn_labels ();
3195     }
3196   mips_opts.noreorder++;
3197   mips_any_noreorder = 1;
3198 }
3199
3200 /* End a nested noreorder block.  */
3201
3202 static void
3203 end_noreorder (void)
3204 {
3205   mips_opts.noreorder--;
3206   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3207     {
3208       /* Commit to inserting prev_nop_frag_required nops and go back to
3209          handling nop insertion the .set reorder way.  */
3210       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3211                                 * (mips_opts.mips16 ? 2 : 4));
3212       insert_into_history (prev_nop_frag_since,
3213                            prev_nop_frag_required, NOP_INSN);
3214       prev_nop_frag = NULL;
3215     }
3216 }
3217
3218 /* Set up global variables for the start of a new macro.  */
3219
3220 static void
3221 macro_start (void)
3222 {
3223   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3224   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
3225                                      && (history[0].insn_mo->pinfo
3226                                          & (INSN_UNCOND_BRANCH_DELAY
3227                                             | INSN_COND_BRANCH_DELAY
3228                                             | INSN_COND_BRANCH_LIKELY)) != 0);
3229 }
3230
3231 /* Given that a macro is longer than 4 bytes, return the appropriate warning
3232    for it.  Return null if no warning is needed.  SUBTYPE is a bitmask of
3233    RELAX_DELAY_SLOT and RELAX_NOMACRO.  */
3234
3235 static const char *
3236 macro_warning (relax_substateT subtype)
3237 {
3238   if (subtype & RELAX_DELAY_SLOT)
3239     return _("Macro instruction expanded into multiple instructions"
3240              " in a branch delay slot");
3241   else if (subtype & RELAX_NOMACRO)
3242     return _("Macro instruction expanded into multiple instructions");
3243   else
3244     return 0;
3245 }
3246
3247 /* Finish up a macro.  Emit warnings as appropriate.  */
3248
3249 static void
3250 macro_end (void)
3251 {
3252   if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3253     {
3254       relax_substateT subtype;
3255
3256       /* Set up the relaxation warning flags.  */
3257       subtype = 0;
3258       if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3259         subtype |= RELAX_SECOND_LONGER;
3260       if (mips_opts.warn_about_macros)
3261         subtype |= RELAX_NOMACRO;
3262       if (mips_macro_warning.delay_slot_p)
3263         subtype |= RELAX_DELAY_SLOT;
3264
3265       if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3266         {
3267           /* Either the macro has a single implementation or both
3268              implementations are longer than 4 bytes.  Emit the
3269              warning now.  */
3270           const char *msg = macro_warning (subtype);
3271           if (msg != 0)
3272             as_warn (msg);
3273         }
3274       else
3275         {
3276           /* One implementation might need a warning but the other
3277              definitely doesn't.  */
3278           mips_macro_warning.first_frag->fr_subtype |= subtype;
3279         }
3280     }
3281 }
3282
3283 /* Read a macro's relocation codes from *ARGS and store them in *R.
3284    The first argument in *ARGS will be either the code for a single
3285    relocation or -1 followed by the three codes that make up a
3286    composite relocation.  */
3287
3288 static void
3289 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3290 {
3291   int i, next;
3292
3293   next = va_arg (*args, int);
3294   if (next >= 0)
3295     r[0] = (bfd_reloc_code_real_type) next;
3296   else
3297     for (i = 0; i < 3; i++)
3298       r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3299 }
3300
3301 /* Build an instruction created by a macro expansion.  This is passed
3302    a pointer to the count of instructions created so far, an
3303    expression, the name of the instruction to build, an operand format
3304    string, and corresponding arguments.  */
3305
3306 static void
3307 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3308 {
3309   const struct mips_opcode *mo;
3310   struct mips_cl_insn insn;
3311   bfd_reloc_code_real_type r[3];
3312   va_list args;
3313
3314   va_start (args, fmt);
3315
3316   if (mips_opts.mips16)
3317     {
3318       mips16_macro_build (ep, name, fmt, args);
3319       va_end (args);
3320       return;
3321     }
3322
3323   r[0] = BFD_RELOC_UNUSED;
3324   r[1] = BFD_RELOC_UNUSED;
3325   r[2] = BFD_RELOC_UNUSED;
3326   mo = (struct mips_opcode *) hash_find (op_hash, name);
3327   assert (mo);
3328   assert (strcmp (name, mo->name) == 0);
3329
3330   /* Search until we get a match for NAME.  It is assumed here that
3331      macros will never generate MDMX, MIPS-3D, DSP or MT instructions.  */
3332   while (strcmp (fmt, mo->args) != 0
3333          || mo->pinfo == INSN_MACRO
3334          || !OPCODE_IS_MEMBER (mo,
3335                                (mips_opts.isa
3336                                 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
3337                                 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
3338                                mips_opts.arch)
3339          || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3340     {
3341       ++mo;
3342       assert (mo->name);
3343       assert (strcmp (name, mo->name) == 0);
3344     }
3345
3346   create_insn (&insn, mo);
3347   for (;;)
3348     {
3349       switch (*fmt++)
3350         {
3351         case '\0':
3352           break;
3353
3354         case ',':
3355         case '(':
3356         case ')':
3357           continue;
3358
3359         case '+':
3360           switch (*fmt++)
3361             {
3362             case 'A':
3363             case 'E':
3364               INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3365               continue;
3366
3367             case 'B':
3368             case 'F':
3369               /* Note that in the macro case, these arguments are already
3370                  in MSB form.  (When handling the instruction in the
3371                  non-macro case, these arguments are sizes from which
3372                  MSB values must be calculated.)  */
3373               INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
3374               continue;
3375
3376             case 'C':
3377             case 'G':
3378             case 'H':
3379               /* Note that in the macro case, these arguments are already
3380                  in MSBD form.  (When handling the instruction in the
3381                  non-macro case, these arguments are sizes from which
3382                  MSBD values must be calculated.)  */
3383               INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
3384               continue;
3385
3386             default:
3387               internalError ();
3388             }
3389           continue;
3390
3391         case 't':
3392         case 'w':
3393         case 'E':
3394           INSERT_OPERAND (RT, insn, va_arg (args, int));
3395           continue;
3396
3397         case 'c':
3398           INSERT_OPERAND (CODE, insn, va_arg (args, int));
3399           continue;
3400
3401         case 'T':
3402         case 'W':
3403           INSERT_OPERAND (FT, insn, va_arg (args, int));
3404           continue;
3405
3406         case 'd':
3407         case 'G':
3408         case 'K':
3409           INSERT_OPERAND (RD, insn, va_arg (args, int));
3410           continue;
3411
3412         case 'U':
3413           {
3414             int tmp = va_arg (args, int);
3415
3416             INSERT_OPERAND (RT, insn, tmp);
3417             INSERT_OPERAND (RD, insn, tmp);
3418             continue;
3419           }
3420
3421         case 'V':
3422         case 'S':
3423           INSERT_OPERAND (FS, insn, va_arg (args, int));
3424           continue;
3425
3426         case 'z':
3427           continue;
3428
3429         case '<':
3430           INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3431           continue;
3432
3433         case 'D':
3434           INSERT_OPERAND (FD, insn, va_arg (args, int));
3435           continue;
3436
3437         case 'B':
3438           INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3439           continue;
3440
3441         case 'J':
3442           INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3443           continue;
3444
3445         case 'q':
3446           INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3447           continue;
3448
3449         case 'b':
3450         case 's':
3451         case 'r':
3452         case 'v':
3453           INSERT_OPERAND (RS, insn, va_arg (args, int));
3454           continue;
3455
3456         case 'i':
3457         case 'j':
3458         case 'o':
3459           macro_read_relocs (&args, r);
3460           assert (*r == BFD_RELOC_GPREL16
3461                   || *r == BFD_RELOC_MIPS_LITERAL
3462                   || *r == BFD_RELOC_MIPS_HIGHER
3463                   || *r == BFD_RELOC_HI16_S
3464                   || *r == BFD_RELOC_LO16
3465                   || *r == BFD_RELOC_MIPS_GOT16
3466                   || *r == BFD_RELOC_MIPS_CALL16
3467                   || *r == BFD_RELOC_MIPS_GOT_DISP
3468                   || *r == BFD_RELOC_MIPS_GOT_PAGE
3469                   || *r == BFD_RELOC_MIPS_GOT_OFST
3470                   || *r == BFD_RELOC_MIPS_GOT_LO16
3471                   || *r == BFD_RELOC_MIPS_CALL_LO16);
3472           continue;
3473
3474         case 'u':
3475           macro_read_relocs (&args, r);
3476           assert (ep != NULL
3477                   && (ep->X_op == O_constant
3478                       || (ep->X_op == O_symbol
3479                           && (*r == BFD_RELOC_MIPS_HIGHEST
3480                               || *r == BFD_RELOC_HI16_S
3481                               || *r == BFD_RELOC_HI16
3482                               || *r == BFD_RELOC_GPREL16
3483                               || *r == BFD_RELOC_MIPS_GOT_HI16
3484                               || *r == BFD_RELOC_MIPS_CALL_HI16))));
3485           continue;
3486
3487         case 'p':
3488           assert (ep != NULL);
3489
3490           /*
3491            * This allows macro() to pass an immediate expression for
3492            * creating short branches without creating a symbol.
3493            *
3494            * We don't allow branch relaxation for these branches, as
3495            * they should only appear in ".set nomacro" anyway.
3496            */
3497           if (ep->X_op == O_constant)
3498             {
3499               if ((ep->X_add_number & 3) != 0)
3500                 as_bad (_("branch to misaligned address (0x%lx)"),
3501                         (unsigned long) ep->X_add_number);
3502               if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3503                 as_bad (_("branch address range overflow (0x%lx)"),
3504                         (unsigned long) ep->X_add_number);
3505               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3506               ep = NULL;
3507             }
3508           else
3509             *r = BFD_RELOC_16_PCREL_S2;
3510           continue;
3511
3512         case 'a':
3513           assert (ep != NULL);
3514           *r = BFD_RELOC_MIPS_JMP;
3515           continue;
3516
3517         case 'C':
3518           INSERT_OPERAND (COPZ, insn, va_arg (args, unsigned long));
3519           continue;
3520
3521         case 'k':
3522           INSERT_OPERAND (CACHE, insn, va_arg (args, unsigned long));
3523           continue;
3524
3525         default:
3526           internalError ();
3527         }
3528       break;
3529     }
3530   va_end (args);
3531   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3532
3533   append_insn (&insn, ep, r);
3534 }
3535
3536 static void
3537 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3538                     va_list args)
3539 {
3540   struct mips_opcode *mo;
3541   struct mips_cl_insn insn;
3542   bfd_reloc_code_real_type r[3]
3543     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3544
3545   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3546   assert (mo);
3547   assert (strcmp (name, mo->name) == 0);
3548
3549   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3550     {
3551       ++mo;
3552       assert (mo->name);
3553       assert (strcmp (name, mo->name) == 0);
3554     }
3555
3556   create_insn (&insn, mo);
3557   for (;;)
3558     {
3559       int c;
3560
3561       c = *fmt++;
3562       switch (c)
3563         {
3564         case '\0':
3565           break;
3566
3567         case ',':
3568         case '(':
3569         case ')':
3570           continue;
3571
3572         case 'y':
3573         case 'w':
3574           MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3575           continue;
3576
3577         case 'x':
3578         case 'v':
3579           MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3580           continue;
3581
3582         case 'z':
3583           MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3584           continue;
3585
3586         case 'Z':
3587           MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3588           continue;
3589
3590         case '0':
3591         case 'S':
3592         case 'P':
3593         case 'R':
3594           continue;
3595
3596         case 'X':
3597           MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3598           continue;
3599
3600         case 'Y':
3601           {
3602             int regno;
3603
3604             regno = va_arg (args, int);
3605             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3606             MIPS16_INSERT_OPERAND (REG32R, insn, regno);
3607           }
3608           continue;
3609
3610         case '<':
3611         case '>':
3612         case '4':
3613         case '5':
3614         case 'H':
3615         case 'W':
3616         case 'D':
3617         case 'j':
3618         case '8':
3619         case 'V':
3620         case 'C':
3621         case 'U':
3622         case 'k':
3623         case 'K':
3624         case 'p':
3625         case 'q':
3626           {
3627             assert (ep != NULL);
3628
3629             if (ep->X_op != O_constant)
3630               *r = (int) BFD_RELOC_UNUSED + c;
3631             else
3632               {
3633                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3634                               FALSE, &insn.insn_opcode, &insn.use_extend,
3635                               &insn.extend);
3636                 ep = NULL;
3637                 *r = BFD_RELOC_UNUSED;
3638               }
3639           }
3640           continue;
3641
3642         case '6':
3643           MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3644           continue;
3645         }
3646
3647       break;
3648     }
3649
3650   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3651
3652   append_insn (&insn, ep, r);
3653 }
3654
3655 /*
3656  * Sign-extend 32-bit mode constants that have bit 31 set and all
3657  * higher bits unset.
3658  */
3659 static void
3660 normalize_constant_expr (expressionS *ex)
3661 {
3662   if (ex->X_op == O_constant
3663       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3664     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3665                         - 0x80000000);
3666 }
3667
3668 /*
3669  * Sign-extend 32-bit mode address offsets that have bit 31 set and
3670  * all higher bits unset.
3671  */
3672 static void
3673 normalize_address_expr (expressionS *ex)
3674 {
3675   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3676         || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3677       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3678     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3679                         - 0x80000000);
3680 }
3681
3682 /*
3683  * Generate a "jalr" instruction with a relocation hint to the called
3684  * function.  This occurs in NewABI PIC code.
3685  */
3686 static void
3687 macro_build_jalr (expressionS *ep)
3688 {
3689   char *f = NULL;
3690
3691   if (HAVE_NEWABI)
3692     {
3693       frag_grow (8);
3694       f = frag_more (0);
3695     }
3696   macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3697   if (HAVE_NEWABI)
3698     fix_new_exp (frag_now, f - frag_now->fr_literal,
3699                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3700 }
3701
3702 /*
3703  * Generate a "lui" instruction.
3704  */
3705 static void
3706 macro_build_lui (expressionS *ep, int regnum)
3707 {
3708   expressionS high_expr;
3709   const struct mips_opcode *mo;
3710   struct mips_cl_insn insn;
3711   bfd_reloc_code_real_type r[3]
3712     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3713   const char *name = "lui";
3714   const char *fmt = "t,u";
3715
3716   assert (! mips_opts.mips16);
3717
3718   high_expr = *ep;
3719
3720   if (high_expr.X_op == O_constant)
3721     {
3722       /* we can compute the instruction now without a relocation entry */
3723       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3724                                 >> 16) & 0xffff;
3725       *r = BFD_RELOC_UNUSED;
3726     }
3727   else
3728     {
3729       assert (ep->X_op == O_symbol);
3730       /* _gp_disp is a special case, used from s_cpload.
3731          __gnu_local_gp is used if mips_no_shared.  */
3732       assert (mips_pic == NO_PIC
3733               || (! HAVE_NEWABI
3734                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3735               || (! mips_in_shared
3736                   && strcmp (S_GET_NAME (ep->X_add_symbol),
3737                              "__gnu_local_gp") == 0));
3738       *r = BFD_RELOC_HI16_S;
3739     }
3740
3741   mo = hash_find (op_hash, name);
3742   assert (strcmp (name, mo->name) == 0);
3743   assert (strcmp (fmt, mo->args) == 0);
3744   create_insn (&insn, mo);
3745
3746   insn.insn_opcode = insn.insn_mo->match;
3747   INSERT_OPERAND (RT, insn, regnum);
3748   if (*r == BFD_RELOC_UNUSED)
3749     {
3750       insn.insn_opcode |= high_expr.X_add_number;
3751       append_insn (&insn, NULL, r);
3752     }
3753   else
3754     append_insn (&insn, &high_expr, r);
3755 }
3756
3757 /* Generate a sequence of instructions to do a load or store from a constant
3758    offset off of a base register (breg) into/from a target register (treg),
3759    using AT if necessary.  */
3760 static void
3761 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3762                               int treg, int breg, int dbl)
3763 {
3764   assert (ep->X_op == O_constant);
3765
3766   /* Sign-extending 32-bit constants makes their handling easier.  */
3767   if (!dbl)
3768     normalize_constant_expr (ep);
3769
3770   /* Right now, this routine can only handle signed 32-bit constants.  */
3771   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3772     as_warn (_("operand overflow"));
3773
3774   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3775     {
3776       /* Signed 16-bit offset will fit in the op.  Easy!  */
3777       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3778     }
3779   else
3780     {
3781       /* 32-bit offset, need multiple instructions and AT, like:
3782            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3783            addu     $tempreg,$tempreg,$breg
3784            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3785          to handle the complete offset.  */
3786       macro_build_lui (ep, AT);
3787       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3788       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3789
3790       if (mips_opts.noat)
3791         as_bad (_("Macro used $at after \".set noat\""));
3792     }
3793 }
3794
3795 /*                      set_at()
3796  * Generates code to set the $at register to true (one)
3797  * if reg is less than the immediate expression.
3798  */
3799 static void
3800 set_at (int reg, int unsignedp)
3801 {
3802   if (imm_expr.X_op == O_constant
3803       && imm_expr.X_add_number >= -0x8000
3804       && imm_expr.X_add_number < 0x8000)
3805     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3806                  AT, reg, BFD_RELOC_LO16);
3807   else
3808     {
3809       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3810       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3811     }
3812 }
3813
3814 /* Warn if an expression is not a constant.  */
3815
3816 static void
3817 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3818 {
3819   if (ex->X_op == O_big)
3820     as_bad (_("unsupported large constant"));
3821   else if (ex->X_op != O_constant)
3822     as_bad (_("Instruction %s requires absolute expression"),
3823             ip->insn_mo->name);
3824
3825   if (HAVE_32BIT_GPRS)
3826     normalize_constant_expr (ex);
3827 }
3828
3829 /* Count the leading zeroes by performing a binary chop. This is a
3830    bulky bit of source, but performance is a LOT better for the
3831    majority of values than a simple loop to count the bits:
3832        for (lcnt = 0; (lcnt < 32); lcnt++)
3833          if ((v) & (1 << (31 - lcnt)))
3834            break;
3835   However it is not code size friendly, and the gain will drop a bit
3836   on certain cached systems.
3837 */
3838 #define COUNT_TOP_ZEROES(v)             \
3839   (((v) & ~0xffff) == 0                 \
3840    ? ((v) & ~0xff) == 0                 \
3841      ? ((v) & ~0xf) == 0                \
3842        ? ((v) & ~0x3) == 0              \
3843          ? ((v) & ~0x1) == 0            \
3844            ? !(v)                       \
3845              ? 32                       \
3846              : 31                       \
3847            : 30                         \
3848          : ((v) & ~0x7) == 0            \
3849            ? 29                         \
3850            : 28                         \
3851        : ((v) & ~0x3f) == 0             \
3852          ? ((v) & ~0x1f) == 0           \
3853            ? 27                         \
3854            : 26                         \
3855          : ((v) & ~0x7f) == 0           \
3856            ? 25                         \
3857            : 24                         \
3858      : ((v) & ~0xfff) == 0              \
3859        ? ((v) & ~0x3ff) == 0            \
3860          ? ((v) & ~0x1ff) == 0          \
3861            ? 23                         \
3862            : 22                         \
3863          : ((v) & ~0x7ff) == 0          \
3864            ? 21                         \
3865            : 20                         \
3866        : ((v) & ~0x3fff) == 0           \
3867          ? ((v) & ~0x1fff) == 0         \
3868            ? 19                         \
3869            : 18                         \
3870          : ((v) & ~0x7fff) == 0         \
3871            ? 17                         \
3872            : 16                         \
3873    : ((v) & ~0xffffff) == 0             \
3874      ? ((v) & ~0xfffff) == 0            \
3875        ? ((v) & ~0x3ffff) == 0          \
3876          ? ((v) & ~0x1ffff) == 0        \
3877            ? 15                         \
3878            : 14                         \
3879          : ((v) & ~0x7ffff) == 0        \
3880            ? 13                         \
3881            : 12                         \
3882        : ((v) & ~0x3fffff) == 0         \
3883          ? ((v) & ~0x1fffff) == 0       \
3884            ? 11                         \
3885            : 10                         \
3886          : ((v) & ~0x7fffff) == 0       \
3887            ? 9                          \
3888            : 8                          \
3889      : ((v) & ~0xfffffff) == 0          \
3890        ? ((v) & ~0x3ffffff) == 0        \
3891          ? ((v) & ~0x1ffffff) == 0      \
3892            ? 7                          \
3893            : 6                          \
3894          : ((v) & ~0x7ffffff) == 0      \
3895            ? 5                          \
3896            : 4                          \
3897        : ((v) & ~0x3fffffff) == 0       \
3898          ? ((v) & ~0x1fffffff) == 0     \
3899            ? 3                          \
3900            : 2                          \
3901          : ((v) & ~0x7fffffff) == 0     \
3902            ? 1                          \
3903            : 0)
3904
3905 /*                      load_register()
3906  *  This routine generates the least number of instructions necessary to load
3907  *  an absolute expression value into a register.
3908  */
3909 static void
3910 load_register (int reg, expressionS *ep, int dbl)
3911 {
3912   int freg;
3913   expressionS hi32, lo32;
3914
3915   if (ep->X_op != O_big)
3916     {
3917       assert (ep->X_op == O_constant);
3918
3919       /* Sign-extending 32-bit constants makes their handling easier.  */
3920       if (!dbl)
3921         normalize_constant_expr (ep);
3922
3923       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3924         {
3925           /* We can handle 16 bit signed values with an addiu to
3926              $zero.  No need to ever use daddiu here, since $zero and
3927              the result are always correct in 32 bit mode.  */
3928           macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3929           return;
3930         }
3931       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3932         {
3933           /* We can handle 16 bit unsigned values with an ori to
3934              $zero.  */
3935           macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3936           return;
3937         }
3938       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3939         {
3940           /* 32 bit values require an lui.  */
3941           macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3942           if ((ep->X_add_number & 0xffff) != 0)
3943             macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3944           return;
3945         }
3946     }
3947
3948   /* The value is larger than 32 bits.  */
3949
3950   if (!dbl || HAVE_32BIT_GPRS)
3951     {
3952       char value[32];
3953
3954       sprintf_vma (value, ep->X_add_number);
3955       as_bad (_("Number (0x%s) larger than 32 bits"), value);
3956       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3957       return;
3958     }
3959
3960   if (ep->X_op != O_big)
3961     {
3962       hi32 = *ep;
3963       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3964       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3965       hi32.X_add_number &= 0xffffffff;
3966       lo32 = *ep;
3967       lo32.X_add_number &= 0xffffffff;
3968     }
3969   else
3970     {
3971       assert (ep->X_add_number > 2);
3972       if (ep->X_add_number == 3)
3973         generic_bignum[3] = 0;
3974       else if (ep->X_add_number > 4)
3975         as_bad (_("Number larger than 64 bits"));
3976       lo32.X_op = O_constant;
3977       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3978       hi32.X_op = O_constant;
3979       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3980     }
3981
3982   if (hi32.X_add_number == 0)
3983     freg = 0;
3984   else
3985     {
3986       int shift, bit;
3987       unsigned long hi, lo;
3988
3989       if (hi32.X_add_number == (offsetT) 0xffffffff)
3990         {
3991           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3992             {
3993               macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3994               return;
3995             }
3996           if (lo32.X_add_number & 0x80000000)
3997             {
3998               macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3999               if (lo32.X_add_number & 0xffff)
4000                 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
4001               return;
4002             }
4003         }
4004
4005       /* Check for 16bit shifted constant.  We know that hi32 is
4006          non-zero, so start the mask on the first bit of the hi32
4007          value.  */
4008       shift = 17;
4009       do
4010         {
4011           unsigned long himask, lomask;
4012
4013           if (shift < 32)
4014             {
4015               himask = 0xffff >> (32 - shift);
4016               lomask = (0xffff << shift) & 0xffffffff;
4017             }
4018           else
4019             {
4020               himask = 0xffff << (shift - 32);
4021               lomask = 0;
4022             }
4023           if ((hi32.X_add_number & ~(offsetT) himask) == 0
4024               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
4025             {
4026               expressionS tmp;
4027
4028               tmp.X_op = O_constant;
4029               if (shift < 32)
4030                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
4031                                     | (lo32.X_add_number >> shift));
4032               else
4033                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
4034               macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
4035               macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
4036                            reg, reg, (shift >= 32) ? shift - 32 : shift);
4037               return;
4038             }
4039           ++shift;
4040         }
4041       while (shift <= (64 - 16));
4042
4043       /* Find the bit number of the lowest one bit, and store the
4044          shifted value in hi/lo.  */
4045       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4046       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4047       if (lo != 0)
4048         {
4049           bit = 0;
4050           while ((lo & 1) == 0)
4051             {
4052               lo >>= 1;
4053               ++bit;
4054             }
4055           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4056           hi >>= bit;
4057         }
4058       else
4059         {
4060           bit = 32;
4061           while ((hi & 1) == 0)
4062             {
4063               hi >>= 1;
4064               ++bit;
4065             }
4066           lo = hi;
4067           hi = 0;
4068         }
4069
4070       /* Optimize if the shifted value is a (power of 2) - 1.  */
4071       if ((hi == 0 && ((lo + 1) & lo) == 0)
4072           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
4073         {
4074           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
4075           if (shift != 0)
4076             {
4077               expressionS tmp;
4078
4079               /* This instruction will set the register to be all
4080                  ones.  */
4081               tmp.X_op = O_constant;
4082               tmp.X_add_number = (offsetT) -1;
4083               macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
4084               if (bit != 0)
4085                 {
4086                   bit += shift;
4087                   macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4088                                reg, reg, (bit >= 32) ? bit - 32 : bit);
4089                 }
4090               macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4091                            reg, reg, (shift >= 32) ? shift - 32 : shift);
4092               return;
4093             }
4094         }
4095
4096       /* Sign extend hi32 before calling load_register, because we can
4097          generally get better code when we load a sign extended value.  */
4098       if ((hi32.X_add_number & 0x80000000) != 0)
4099         hi32.X_add_number |= ~(offsetT) 0xffffffff;
4100       load_register (reg, &hi32, 0);
4101       freg = reg;
4102     }
4103   if ((lo32.X_add_number & 0xffff0000) == 0)
4104     {
4105       if (freg != 0)
4106         {
4107           macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
4108           freg = reg;
4109         }
4110     }
4111   else
4112     {
4113       expressionS mid16;
4114
4115       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
4116         {
4117           macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4118           macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
4119           return;
4120         }
4121
4122       if (freg != 0)
4123         {
4124           macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
4125           freg = reg;
4126         }
4127       mid16 = lo32;
4128       mid16.X_add_number >>= 16;
4129       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4130       macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4131       freg = reg;
4132     }
4133   if ((lo32.X_add_number & 0xffff) != 0)
4134     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4135 }
4136
4137 static inline void
4138 load_delay_nop (void)
4139 {
4140   if (!gpr_interlocks)
4141     macro_build (NULL, "nop", "");
4142 }
4143
4144 /* Load an address into a register.  */
4145
4146 static void
4147 load_address (int reg, expressionS *ep, int *used_at)
4148 {
4149   if (ep->X_op != O_constant
4150       && ep->X_op != O_symbol)
4151     {
4152       as_bad (_("expression too complex"));
4153       ep->X_op = O_constant;
4154     }
4155
4156   if (ep->X_op == O_constant)
4157     {
4158       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
4159       return;
4160     }
4161
4162   if (mips_pic == NO_PIC)
4163     {
4164       /* If this is a reference to a GP relative symbol, we want
4165            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
4166          Otherwise we want
4167            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
4168            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
4169          If we have an addend, we always use the latter form.
4170
4171          With 64bit address space and a usable $at we want
4172            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
4173            lui          $at,<sym>               (BFD_RELOC_HI16_S)
4174            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
4175            daddiu       $at,<sym>               (BFD_RELOC_LO16)
4176            dsll32       $reg,0
4177            daddu        $reg,$reg,$at
4178
4179          If $at is already in use, we use a path which is suboptimal
4180          on superscalar processors.
4181            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
4182            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
4183            dsll         $reg,16
4184            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
4185            dsll         $reg,16
4186            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
4187
4188          For GP relative symbols in 64bit address space we can use
4189          the same sequence as in 32bit address space.  */
4190       if (HAVE_64BIT_SYMBOLS)
4191         {
4192           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4193               && !nopic_need_relax (ep->X_add_symbol, 1))
4194             {
4195               relax_start (ep->X_add_symbol);
4196               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4197                            mips_gp_register, BFD_RELOC_GPREL16);
4198               relax_switch ();
4199             }
4200
4201           if (*used_at == 0 && !mips_opts.noat)
4202             {
4203               macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4204               macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4205               macro_build (ep, "daddiu", "t,r,j", reg, reg,
4206                            BFD_RELOC_MIPS_HIGHER);
4207               macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4208               macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4209               macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
4210               *used_at = 1;
4211             }
4212           else
4213             {
4214               macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4215               macro_build (ep, "daddiu", "t,r,j", reg, reg,
4216                            BFD_RELOC_MIPS_HIGHER);
4217               macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4218               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4219               macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4220               macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
4221             }
4222
4223           if (mips_relax.sequence)
4224             relax_end ();
4225         }
4226       else
4227         {
4228           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4229               && !nopic_need_relax (ep->X_add_symbol, 1))
4230             {
4231               relax_start (ep->X_add_symbol);
4232               macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4233                            mips_gp_register, BFD_RELOC_GPREL16);
4234               relax_switch ();
4235             }
4236           macro_build_lui (ep, reg);
4237           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4238                        reg, reg, BFD_RELOC_LO16);
4239           if (mips_relax.sequence)
4240             relax_end ();
4241         }
4242     }
4243   else if (!mips_big_got)
4244     {
4245       expressionS ex;
4246
4247       /* If this is a reference to an external symbol, we want
4248            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
4249          Otherwise we want
4250            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
4251            nop
4252            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
4253          If there is a constant, it must be added in after.
4254
4255          If we have NewABI, we want
4256            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
4257          unless we're referencing a global symbol with a non-zero
4258          offset, in which case cst must be added separately.  */
4259       if (HAVE_NEWABI)
4260         {
4261           if (ep->X_add_number)
4262             {
4263               ex.X_add_number = ep->X_add_number;
4264               ep->X_add_number = 0;
4265               relax_start (ep->X_add_symbol);
4266               macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4267                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4268               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4269                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4270               ex.X_op = O_constant;
4271               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4272                            reg, reg, BFD_RELOC_LO16);
4273               ep->X_add_number = ex.X_add_number;
4274               relax_switch ();
4275             }
4276           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4277                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4278           if (mips_relax.sequence)
4279             relax_end ();
4280         }
4281       else
4282         {
4283           ex.X_add_number = ep->X_add_number;
4284           ep->X_add_number = 0;
4285           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4286                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
4287           load_delay_nop ();
4288           relax_start (ep->X_add_symbol);
4289           relax_switch ();
4290           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4291                        BFD_RELOC_LO16);
4292           relax_end ();
4293
4294           if (ex.X_add_number != 0)
4295             {
4296               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4297                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4298               ex.X_op = O_constant;
4299               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4300                            reg, reg, BFD_RELOC_LO16);
4301             }
4302         }
4303     }
4304   else if (mips_big_got)
4305     {
4306       expressionS ex;
4307
4308       /* This is the large GOT case.  If this is a reference to an
4309          external symbol, we want
4310            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
4311            addu         $reg,$reg,$gp
4312            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
4313
4314          Otherwise, for a reference to a local symbol in old ABI, we want
4315            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
4316            nop
4317            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
4318          If there is a constant, it must be added in after.
4319
4320          In the NewABI, for local symbols, with or without offsets, we want:
4321            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
4322            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
4323       */
4324       if (HAVE_NEWABI)
4325         {
4326           ex.X_add_number = ep->X_add_number;
4327           ep->X_add_number = 0;
4328           relax_start (ep->X_add_symbol);
4329           macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4330           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4331                        reg, reg, mips_gp_register);
4332           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4333                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4334           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4335             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4336           else if (ex.X_add_number)
4337             {
4338               ex.X_op = O_constant;
4339               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4340                            BFD_RELOC_LO16);
4341             }
4342
4343           ep->X_add_number = ex.X_add_number;
4344           relax_switch ();
4345           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4346                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4347           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4348                        BFD_RELOC_MIPS_GOT_OFST);
4349           relax_end ();
4350         }
4351       else
4352         {
4353           ex.X_add_number = ep->X_add_number;
4354           ep->X_add_number = 0;
4355           relax_start (ep->X_add_symbol);
4356           macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4357           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4358                        reg, reg, mips_gp_register);
4359           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4360                        reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4361           relax_switch ();
4362           if (reg_needs_delay (mips_gp_register))
4363             {
4364               /* We need a nop before loading from $gp.  This special
4365                  check is required because the lui which starts the main
4366                  instruction stream does not refer to $gp, and so will not
4367                  insert the nop which may be required.  */
4368               macro_build (NULL, "nop", "");
4369             }
4370           macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4371                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
4372           load_delay_nop ();
4373           macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4374                        BFD_RELOC_LO16);
4375           relax_end ();
4376
4377           if (ex.X_add_number != 0)
4378             {
4379               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4380                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4381               ex.X_op = O_constant;
4382               macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4383                            BFD_RELOC_LO16);
4384             }
4385         }
4386     }
4387   else
4388     abort ();
4389
4390   if (mips_opts.noat && *used_at == 1)
4391     as_bad (_("Macro used $at after \".set noat\""));
4392 }
4393
4394 /* Move the contents of register SOURCE into register DEST.  */
4395
4396 static void
4397 move_register (int dest, int source)
4398 {
4399   macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4400                dest, source, 0);
4401 }
4402
4403 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4404    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4405    The two alternatives are:
4406
4407    Global symbol                Local sybmol
4408    -------------                ------------
4409    lw DEST,%got(SYMBOL)         lw DEST,%got(SYMBOL + OFFSET)
4410    ...                          ...
4411    addiu DEST,DEST,OFFSET       addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4412
4413    load_got_offset emits the first instruction and add_got_offset
4414    emits the second for a 16-bit offset or add_got_offset_hilo emits
4415    a sequence to add a 32-bit offset using a scratch register.  */
4416
4417 static void
4418 load_got_offset (int dest, expressionS *local)
4419 {
4420   expressionS global;
4421
4422   global = *local;
4423   global.X_add_number = 0;
4424
4425   relax_start (local->X_add_symbol);
4426   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4427                BFD_RELOC_MIPS_GOT16, mips_gp_register);
4428   relax_switch ();
4429   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4430                BFD_RELOC_MIPS_GOT16, mips_gp_register);
4431   relax_end ();
4432 }
4433
4434 static void
4435 add_got_offset (int dest, expressionS *local)
4436 {
4437   expressionS global;
4438
4439   global.X_op = O_constant;
4440   global.X_op_symbol = NULL;
4441   global.X_add_symbol = NULL;
4442   global.X_add_number = local->X_add_number;
4443
4444   relax_start (local->X_add_symbol);
4445   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4446                dest, dest, BFD_RELOC_LO16);
4447   relax_switch ();
4448   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4449   relax_end ();
4450 }
4451
4452 static void
4453 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4454 {
4455   expressionS global;
4456   int hold_mips_optimize;
4457
4458   global.X_op = O_constant;
4459   global.X_op_symbol = NULL;
4460   global.X_add_symbol = NULL;
4461   global.X_add_number = local->X_add_number;
4462
4463   relax_start (local->X_add_symbol);
4464   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4465   relax_switch ();
4466   /* Set mips_optimize around the lui instruction to avoid
4467      inserting an unnecessary nop after the lw.  */
4468   hold_mips_optimize = mips_optimize;
4469   mips_optimize = 2;
4470   macro_build_lui (&global, tmp);
4471   mips_optimize = hold_mips_optimize;
4472   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4473   relax_end ();
4474
4475   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4476 }
4477
4478 /*
4479  *                      Build macros
4480  *   This routine implements the seemingly endless macro or synthesized
4481  * instructions and addressing modes in the mips assembly language. Many
4482  * of these macros are simple and are similar to each other. These could
4483  * probably be handled by some kind of table or grammar approach instead of
4484  * this verbose method. Others are not simple macros but are more like
4485  * optimizing code generation.
4486  *   One interesting optimization is when several store macros appear
4487  * consecutively that would load AT with the upper half of the same address.
4488  * The ensuing load upper instructions are ommited. This implies some kind
4489  * of global optimization. We currently only optimize within a single macro.
4490  *   For many of the load and store macros if the address is specified as a
4491  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4492  * first load register 'at' with zero and use it as the base register. The
4493  * mips assembler simply uses register $zero. Just one tiny optimization
4494  * we're missing.
4495  */
4496 static void
4497 macro (struct mips_cl_insn *ip)
4498 {
4499   int treg, sreg, dreg, breg;
4500   int tempreg;
4501   int mask;
4502   int used_at = 0;
4503   expressionS expr1;
4504   const char *s;
4505   const char *s2;
4506   const char *fmt;
4507   int likely = 0;
4508   int dbl = 0;
4509   int coproc = 0;
4510   int lr = 0;
4511   int imm = 0;
4512   int call = 0;
4513   int off;
4514   offsetT maxnum;
4515   bfd_reloc_code_real_type r;
4516   int hold_mips_optimize;
4517
4518   assert (! mips_opts.mips16);
4519
4520   treg = (ip->insn_opcode >> 16) & 0x1f;
4521   dreg = (ip->insn_opcode >> 11) & 0x1f;
4522   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4523   mask = ip->insn_mo->mask;
4524
4525   expr1.X_op = O_constant;
4526   expr1.X_op_symbol = NULL;
4527   expr1.X_add_symbol = NULL;
4528   expr1.X_add_number = 1;
4529
4530   switch (mask)
4531     {
4532     case M_DABS:
4533       dbl = 1;
4534     case M_ABS:
4535       /* bgez $a0,.+12
4536          move v0,$a0
4537          sub v0,$zero,$a0
4538          */
4539
4540       start_noreorder ();
4541
4542       expr1.X_add_number = 8;
4543       macro_build (&expr1, "bgez", "s,p", sreg);
4544       if (dreg == sreg)
4545         macro_build (NULL, "nop", "", 0);
4546       else
4547         move_register (dreg, sreg);
4548       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4549
4550       end_noreorder ();
4551       break;
4552
4553     case M_ADD_I:
4554       s = "addi";
4555       s2 = "add";
4556       goto do_addi;
4557     case M_ADDU_I:
4558       s = "addiu";
4559       s2 = "addu";
4560       goto do_addi;
4561     case M_DADD_I:
4562       dbl = 1;
4563       s = "daddi";
4564       s2 = "dadd";
4565       goto do_addi;
4566     case M_DADDU_I:
4567       dbl = 1;
4568       s = "daddiu";
4569       s2 = "daddu";
4570     do_addi:
4571       if (imm_expr.X_op == O_constant
4572           && imm_expr.X_add_number >= -0x8000
4573           && imm_expr.X_add_number < 0x8000)
4574         {
4575           macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4576           break;
4577         }
4578       used_at = 1;
4579       load_register (AT, &imm_expr, dbl);
4580       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4581       break;
4582
4583     case M_AND_I:
4584       s = "andi";
4585       s2 = "and";
4586       goto do_bit;
4587     case M_OR_I:
4588       s = "ori";
4589       s2 = "or";
4590       goto do_bit;
4591     case M_NOR_I:
4592       s = "";
4593       s2 = "nor";
4594       goto do_bit;
4595     case M_XOR_I:
4596       s = "xori";
4597       s2 = "xor";
4598     do_bit:
4599       if (imm_expr.X_op == O_constant
4600           && imm_expr.X_add_number >= 0
4601           && imm_expr.X_add_number < 0x10000)
4602         {
4603           if (mask != M_NOR_I)
4604             macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4605           else
4606             {
4607               macro_build (&imm_expr, "ori", "t,r,i",
4608                            treg, sreg, BFD_RELOC_LO16);
4609               macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4610             }
4611           break;
4612         }
4613
4614       used_at = 1;
4615       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4616       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4617       break;
4618
4619     case M_BEQ_I:
4620       s = "beq";
4621       goto beq_i;
4622     case M_BEQL_I:
4623       s = "beql";
4624       likely = 1;
4625       goto beq_i;
4626     case M_BNE_I:
4627       s = "bne";
4628       goto beq_i;
4629     case M_BNEL_I:
4630       s = "bnel";
4631       likely = 1;
4632     beq_i:
4633       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4634         {
4635           macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4636           break;
4637         }
4638       used_at = 1;
4639       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4640       macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4641       break;
4642
4643     case M_BGEL:
4644       likely = 1;
4645     case M_BGE:
4646       if (treg == 0)
4647         {
4648           macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4649           break;
4650         }
4651       if (sreg == 0)
4652         {
4653           macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4654           break;
4655         }
4656       used_at = 1;
4657       macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4658       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4659       break;
4660
4661     case M_BGTL_I:
4662       likely = 1;
4663     case M_BGT_I:
4664       /* check for > max integer */
4665       maxnum = 0x7fffffff;
4666       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4667         {
4668           maxnum <<= 16;
4669           maxnum |= 0xffff;
4670           maxnum <<= 16;
4671           maxnum |= 0xffff;
4672         }
4673       if (imm_expr.X_op == O_constant
4674           && imm_expr.X_add_number >= maxnum
4675           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4676         {
4677         do_false:
4678           /* result is always false */
4679           if (! likely)
4680             macro_build (NULL, "nop", "", 0);
4681           else
4682             macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4683           break;
4684         }
4685       if (imm_expr.X_op != O_constant)
4686         as_bad (_("Unsupported large constant"));
4687       ++imm_expr.X_add_number;
4688       /* FALLTHROUGH */
4689     case M_BGE_I:
4690     case M_BGEL_I:
4691       if (mask == M_BGEL_I)
4692         likely = 1;
4693       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4694         {
4695           macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4696           break;
4697         }
4698       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4699         {
4700           macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4701           break;
4702         }
4703       maxnum = 0x7fffffff;
4704       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4705         {
4706           maxnum <<= 16;
4707           maxnum |= 0xffff;
4708           maxnum <<= 16;
4709           maxnum |= 0xffff;
4710         }
4711       maxnum = - maxnum - 1;
4712       if (imm_expr.X_op == O_constant
4713           && imm_expr.X_add_number <= maxnum
4714           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4715         {
4716         do_true:
4717           /* result is always true */
4718           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4719           macro_build (&offset_expr, "b", "p");
4720           break;
4721         }
4722       used_at = 1;
4723       set_at (sreg, 0);
4724       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4725       break;
4726
4727     case M_BGEUL:
4728       likely = 1;
4729     case M_BGEU:
4730       if (treg == 0)
4731         goto do_true;
4732       if (sreg == 0)
4733         {
4734           macro_build (&offset_expr, likely ? "beql" : "beq",
4735                        "s,t,p", 0, treg);
4736           break;
4737         }
4738       used_at = 1;
4739       macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4740       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4741       break;
4742
4743     case M_BGTUL_I:
4744       likely = 1;
4745     case M_BGTU_I:
4746       if (sreg == 0
4747           || (HAVE_32BIT_GPRS
4748               && imm_expr.X_op == O_constant
4749               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4750         goto do_false;
4751       if (imm_expr.X_op != O_constant)
4752         as_bad (_("Unsupported large constant"));
4753       ++imm_expr.X_add_number;
4754       /* FALLTHROUGH */
4755     case M_BGEU_I:
4756     case M_BGEUL_I:
4757       if (mask == M_BGEUL_I)
4758         likely = 1;
4759       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4760         goto do_true;
4761       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4762         {
4763           macro_build (&offset_expr, likely ? "bnel" : "bne",
4764                        "s,t,p", sreg, 0);
4765           break;
4766         }
4767       used_at = 1;
4768       set_at (sreg, 1);
4769       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4770       break;
4771
4772     case M_BGTL:
4773       likely = 1;
4774     case M_BGT:
4775       if (treg == 0)
4776         {
4777           macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4778           break;
4779         }
4780       if (sreg == 0)
4781         {
4782           macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4783           break;
4784         }
4785       used_at = 1;
4786       macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4787       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4788       break;
4789
4790     case M_BGTUL:
4791       likely = 1;
4792     case M_BGTU:
4793       if (treg == 0)
4794         {
4795           macro_build (&offset_expr, likely ? "bnel" : "bne",
4796                        "s,t,p", sreg, 0);
4797           break;
4798         }
4799       if (sreg == 0)
4800         goto do_false;
4801       used_at = 1;
4802       macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4803       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4804       break;
4805
4806     case M_BLEL:
4807       likely = 1;
4808     case M_BLE:
4809       if (treg == 0)
4810         {
4811           macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4812           break;
4813         }
4814       if (sreg == 0)
4815         {
4816           macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4817           break;
4818         }
4819       used_at = 1;
4820       macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4821       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4822       break;
4823
4824     case M_BLEL_I:
4825       likely = 1;
4826     case M_BLE_I:
4827       maxnum = 0x7fffffff;
4828       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4829         {
4830           maxnum <<= 16;
4831           maxnum |= 0xffff;
4832           maxnum <<= 16;
4833           maxnum |= 0xffff;
4834         }
4835       if (imm_expr.X_op == O_constant
4836           && imm_expr.X_add_number >= maxnum
4837           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4838         goto do_true;
4839       if (imm_expr.X_op != O_constant)
4840         as_bad (_("Unsupported large constant"));
4841       ++imm_expr.X_add_number;
4842       /* FALLTHROUGH */
4843     case M_BLT_I:
4844     case M_BLTL_I:
4845       if (mask == M_BLTL_I)
4846         likely = 1;
4847       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4848         {
4849           macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4850           break;
4851         }
4852       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4853         {
4854           macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4855           break;
4856         }
4857       used_at = 1;
4858       set_at (sreg, 0);
4859       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4860       break;
4861
4862     case M_BLEUL:
4863       likely = 1;
4864     case M_BLEU:
4865       if (treg == 0)
4866         {
4867           macro_build (&offset_expr, likely ? "beql" : "beq",
4868                        "s,t,p", sreg, 0);
4869           break;
4870         }
4871       if (sreg == 0)
4872         goto do_true;
4873       used_at = 1;
4874       macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4875       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4876       break;
4877
4878     case M_BLEUL_I:
4879       likely = 1;
4880     case M_BLEU_I:
4881       if (sreg == 0
4882           || (HAVE_32BIT_GPRS
4883               && imm_expr.X_op == O_constant
4884               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4885         goto do_true;
4886       if (imm_expr.X_op != O_constant)
4887         as_bad (_("Unsupported large constant"));
4888       ++imm_expr.X_add_number;
4889       /* FALLTHROUGH */
4890     case M_BLTU_I:
4891     case M_BLTUL_I:
4892       if (mask == M_BLTUL_I)
4893         likely = 1;
4894       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4895         goto do_false;
4896       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4897         {
4898           macro_build (&offset_expr, likely ? "beql" : "beq",
4899                        "s,t,p", sreg, 0);
4900           break;
4901         }
4902       used_at = 1;
4903       set_at (sreg, 1);
4904       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4905       break;
4906
4907     case M_BLTL:
4908       likely = 1;
4909     case M_BLT:
4910       if (treg == 0)
4911         {
4912           macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4913           break;
4914         }
4915       if (sreg == 0)
4916         {
4917           macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4918           break;
4919         }
4920       used_at = 1;
4921       macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4922       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4923       break;
4924
4925     case M_BLTUL:
4926       likely = 1;
4927     case M_BLTU:
4928       if (treg == 0)
4929         goto do_false;
4930       if (sreg == 0)
4931         {
4932           macro_build (&offset_expr, likely ? "bnel" : "bne",
4933                        "s,t,p", 0, treg);
4934           break;
4935         }
4936       used_at = 1;
4937       macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4938       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4939       break;
4940
4941     case M_DEXT:
4942       {
4943         unsigned long pos;
4944         unsigned long size;
4945
4946         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4947           {
4948             as_bad (_("Unsupported large constant"));
4949             pos = size = 1;
4950           }
4951         else
4952           {
4953             pos = (unsigned long) imm_expr.X_add_number;
4954             size = (unsigned long) imm2_expr.X_add_number;
4955           }
4956
4957         if (pos > 63)
4958           {
4959             as_bad (_("Improper position (%lu)"), pos);
4960             pos = 1;
4961           }
4962         if (size == 0 || size > 64
4963             || (pos + size - 1) > 63)
4964           {
4965             as_bad (_("Improper extract size (%lu, position %lu)"),
4966                     size, pos);
4967             size = 1;
4968           }
4969
4970         if (size <= 32 && pos < 32)
4971           {
4972             s = "dext";
4973             fmt = "t,r,+A,+C";
4974           }
4975         else if (size <= 32)
4976           {
4977             s = "dextu";
4978             fmt = "t,r,+E,+H";
4979           }
4980         else
4981           {
4982             s = "dextm";
4983             fmt = "t,r,+A,+G";
4984           }
4985         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4986       }
4987       break;
4988
4989     case M_DINS:
4990       {
4991         unsigned long pos;
4992         unsigned long size;
4993
4994         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4995           {
4996             as_bad (_("Unsupported large constant"));
4997             pos = size = 1;
4998           }
4999         else
5000           {
5001             pos = (unsigned long) imm_expr.X_add_number;
5002             size = (unsigned long) imm2_expr.X_add_number;
5003           }
5004
5005         if (pos > 63)
5006           {
5007             as_bad (_("Improper position (%lu)"), pos);
5008             pos = 1;
5009           }
5010         if (size == 0 || size > 64
5011             || (pos + size - 1) > 63)
5012           {
5013             as_bad (_("Improper insert size (%lu, position %lu)"),
5014                     size, pos);
5015             size = 1;
5016           }
5017
5018         if (pos < 32 && (pos + size - 1) < 32)
5019           {
5020             s = "dins";
5021             fmt = "t,r,+A,+B";
5022           }
5023         else if (pos >= 32)
5024           {
5025             s = "dinsu";
5026             fmt = "t,r,+E,+F";
5027           }
5028         else
5029           {
5030             s = "dinsm";
5031             fmt = "t,r,+A,+F";
5032           }
5033         macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
5034                      pos + size - 1);
5035       }
5036       break;
5037
5038     case M_DDIV_3:
5039       dbl = 1;
5040     case M_DIV_3:
5041       s = "mflo";
5042       goto do_div3;
5043     case M_DREM_3:
5044       dbl = 1;
5045     case M_REM_3:
5046       s = "mfhi";
5047     do_div3:
5048       if (treg == 0)
5049         {
5050           as_warn (_("Divide by zero."));
5051           if (mips_trap)
5052             macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
5053           else
5054             macro_build (NULL, "break", "c", 7);
5055           break;
5056         }
5057
5058       start_noreorder ();
5059       if (mips_trap)
5060         {
5061           macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5062           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5063         }
5064       else
5065         {
5066           expr1.X_add_number = 8;
5067           macro_build (&expr1, "bne", "s,t,p", treg, 0);
5068           macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5069           macro_build (NULL, "break", "c", 7);
5070         }
5071       expr1.X_add_number = -1;
5072       used_at = 1;
5073       load_register (AT, &expr1, dbl);
5074       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
5075       macro_build (&expr1, "bne", "s,t,p", treg, AT);
5076       if (dbl)
5077         {
5078           expr1.X_add_number = 1;
5079           load_register (AT, &expr1, dbl);
5080           macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
5081         }
5082       else
5083         {
5084           expr1.X_add_number = 0x80000000;
5085           macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
5086         }
5087       if (mips_trap)
5088         {
5089           macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
5090           /* We want to close the noreorder block as soon as possible, so
5091              that later insns are available for delay slot filling.  */
5092           end_noreorder ();
5093         }
5094       else
5095         {
5096           expr1.X_add_number = 8;
5097           macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5098           macro_build (NULL, "nop", "", 0);
5099
5100           /* We want to close the noreorder block as soon as possible, so
5101              that later insns are available for delay slot filling.  */
5102           end_noreorder ();
5103
5104           macro_build (NULL, "break", "c", 6);
5105         }
5106       macro_build (NULL, s, "d", dreg);
5107       break;
5108
5109     case M_DIV_3I:
5110       s = "div";
5111       s2 = "mflo";
5112       goto do_divi;
5113     case M_DIVU_3I:
5114       s = "divu";
5115       s2 = "mflo";
5116       goto do_divi;
5117     case M_REM_3I:
5118       s = "div";
5119       s2 = "mfhi";
5120       goto do_divi;
5121     case M_REMU_3I:
5122       s = "divu";
5123       s2 = "mfhi";
5124       goto do_divi;
5125     case M_DDIV_3I:
5126       dbl = 1;
5127       s = "ddiv";
5128       s2 = "mflo";
5129       goto do_divi;
5130     case M_DDIVU_3I:
5131       dbl = 1;
5132       s = "ddivu";
5133       s2 = "mflo";
5134       goto do_divi;
5135     case M_DREM_3I:
5136       dbl = 1;
5137       s = "ddiv";
5138       s2 = "mfhi";
5139       goto do_divi;
5140     case M_DREMU_3I:
5141       dbl = 1;
5142       s = "ddivu";
5143       s2 = "mfhi";
5144     do_divi:
5145       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5146         {
5147           as_warn (_("Divide by zero."));
5148           if (mips_trap)
5149             macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
5150           else
5151             macro_build (NULL, "break", "c", 7);
5152           break;
5153         }
5154       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5155         {
5156           if (strcmp (s2, "mflo") == 0)
5157             move_register (dreg, sreg);
5158           else
5159             move_register (dreg, 0);
5160           break;
5161         }
5162       if (imm_expr.X_op == O_constant
5163           && imm_expr.X_add_number == -1
5164           && s[strlen (s) - 1] != 'u')
5165         {
5166           if (strcmp (s2, "mflo") == 0)
5167             {
5168               macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
5169             }
5170           else
5171             move_register (dreg, 0);
5172           break;
5173         }
5174
5175       used_at = 1;
5176       load_register (AT, &imm_expr, dbl);
5177       macro_build (NULL, s, "z,s,t", sreg, AT);
5178       macro_build (NULL, s2, "d", dreg);
5179       break;
5180
5181     case M_DIVU_3:
5182       s = "divu";
5183       s2 = "mflo";
5184       goto do_divu3;
5185     case M_REMU_3:
5186       s = "divu";
5187       s2 = "mfhi";
5188       goto do_divu3;
5189     case M_DDIVU_3:
5190       s = "ddivu";
5191       s2 = "mflo";
5192       goto do_divu3;
5193     case M_DREMU_3:
5194       s = "ddivu";
5195       s2 = "mfhi";
5196     do_divu3:
5197       start_noreorder ();
5198       if (mips_trap)
5199         {
5200           macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5201           macro_build (NULL, s, "z,s,t", sreg, treg);
5202           /* We want to close the noreorder block as soon as possible, so
5203              that later insns are available for delay slot filling.  */
5204           end_noreorder ();
5205         }
5206       else
5207         {
5208           expr1.X_add_number = 8;
5209           macro_build (&expr1, "bne", "s,t,p", treg, 0);
5210           macro_build (NULL, s, "z,s,t", sreg, treg);
5211
5212           /* We want to close the noreorder block as soon as possible, so
5213              that later insns are available for delay slot filling.  */
5214           end_noreorder ();
5215           macro_build (NULL, "break", "c", 7);
5216         }
5217       macro_build (NULL, s2, "d", dreg);
5218       break;
5219
5220     case M_DLCA_AB:
5221       dbl = 1;
5222     case M_LCA_AB:
5223       call = 1;
5224       goto do_la;
5225     case M_DLA_AB:
5226       dbl = 1;
5227     case M_LA_AB:
5228     do_la:
5229       /* Load the address of a symbol into a register.  If breg is not
5230          zero, we then add a base register to it.  */
5231
5232       if (dbl && HAVE_32BIT_GPRS)
5233         as_warn (_("dla used to load 32-bit register"));
5234
5235       if (! dbl && HAVE_64BIT_OBJECTS)
5236         as_warn (_("la used to load 64-bit address"));
5237
5238       if (offset_expr.X_op == O_constant
5239           && offset_expr.X_add_number >= -0x8000
5240           && offset_expr.X_add_number < 0x8000)
5241         {
5242           macro_build (&offset_expr, ADDRESS_ADDI_INSN,
5243                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
5244           break;
5245         }
5246
5247       if (!mips_opts.noat && (treg == breg))
5248         {
5249           tempreg = AT;
5250           used_at = 1;
5251         }
5252       else
5253         {
5254           tempreg = treg;
5255         }
5256
5257       if (offset_expr.X_op != O_symbol
5258           && offset_expr.X_op != O_constant)
5259         {
5260           as_bad (_("expression too complex"));
5261           offset_expr.X_op = O_constant;
5262         }
5263
5264       if (offset_expr.X_op == O_constant)
5265         load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
5266       else if (mips_pic == NO_PIC)
5267         {
5268           /* If this is a reference to a GP relative symbol, we want
5269                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5270              Otherwise we want
5271                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
5272                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5273              If we have a constant, we need two instructions anyhow,
5274              so we may as well always use the latter form.
5275
5276              With 64bit address space and a usable $at we want
5277                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5278                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5279                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5280                daddiu   $at,<sym>               (BFD_RELOC_LO16)
5281                dsll32   $tempreg,0
5282                daddu    $tempreg,$tempreg,$at
5283
5284              If $at is already in use, we use a path which is suboptimal
5285              on superscalar processors.
5286                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
5287                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
5288                dsll     $tempreg,16
5289                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
5290                dsll     $tempreg,16
5291                daddiu   $tempreg,<sym>          (BFD_RELOC_LO16)
5292
5293              For GP relative symbols in 64bit address space we can use
5294              the same sequence as in 32bit address space.  */
5295           if (HAVE_64BIT_SYMBOLS)
5296             {
5297               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5298                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5299                 {
5300                   relax_start (offset_expr.X_add_symbol);
5301                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5302                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5303                   relax_switch ();
5304                 }
5305
5306               if (used_at == 0 && !mips_opts.noat)
5307                 {
5308                   macro_build (&offset_expr, "lui", "t,u",
5309                                tempreg, BFD_RELOC_MIPS_HIGHEST);
5310                   macro_build (&offset_expr, "lui", "t,u",
5311                                AT, BFD_RELOC_HI16_S);
5312                   macro_build (&offset_expr, "daddiu", "t,r,j",
5313                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5314                   macro_build (&offset_expr, "daddiu", "t,r,j",
5315                                AT, AT, BFD_RELOC_LO16);
5316                   macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5317                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5318                   used_at = 1;
5319                 }
5320               else
5321                 {
5322                   macro_build (&offset_expr, "lui", "t,u",
5323                                tempreg, BFD_RELOC_MIPS_HIGHEST);
5324                   macro_build (&offset_expr, "daddiu", "t,r,j",
5325                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5326                   macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5327                   macro_build (&offset_expr, "daddiu", "t,r,j",
5328                                tempreg, tempreg, BFD_RELOC_HI16_S);
5329                   macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5330                   macro_build (&offset_expr, "daddiu", "t,r,j",
5331                                tempreg, tempreg, BFD_RELOC_LO16);
5332                 }
5333
5334               if (mips_relax.sequence)
5335                 relax_end ();
5336             }
5337           else
5338             {
5339               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5340                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5341                 {
5342                   relax_start (offset_expr.X_add_symbol);
5343                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5344                                tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5345                   relax_switch ();
5346                 }
5347               if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5348                 as_bad (_("offset too large"));
5349               macro_build_lui (&offset_expr, tempreg);
5350               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5351                            tempreg, tempreg, BFD_RELOC_LO16);
5352               if (mips_relax.sequence)
5353                 relax_end ();
5354             }
5355         }
5356       else if (!mips_big_got && !HAVE_NEWABI)
5357         {
5358           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5359
5360           /* If this is a reference to an external symbol, and there
5361              is no constant, we want
5362                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5363              or for lca or if tempreg is PIC_CALL_REG
5364                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5365              For a local symbol, we want
5366                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5367                nop
5368                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5369
5370              If we have a small constant, and this is a reference to
5371              an external symbol, we want
5372                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5373                nop
5374                addiu    $tempreg,$tempreg,<constant>
5375              For a local symbol, we want the same instruction
5376              sequence, but we output a BFD_RELOC_LO16 reloc on the
5377              addiu instruction.
5378
5379              If we have a large constant, and this is a reference to
5380              an external symbol, we want
5381                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5382                lui      $at,<hiconstant>
5383                addiu    $at,$at,<loconstant>
5384                addu     $tempreg,$tempreg,$at
5385              For a local symbol, we want the same instruction
5386              sequence, but we output a BFD_RELOC_LO16 reloc on the
5387              addiu instruction.
5388            */
5389
5390           if (offset_expr.X_add_number == 0)
5391             {
5392               if (mips_pic == SVR4_PIC
5393                   && breg == 0
5394                   && (call || tempreg == PIC_CALL_REG))
5395                 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5396
5397               relax_start (offset_expr.X_add_symbol);
5398               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5399                            lw_reloc_type, mips_gp_register);
5400               if (breg != 0)
5401                 {
5402                   /* We're going to put in an addu instruction using
5403                      tempreg, so we may as well insert the nop right
5404                      now.  */
5405                   load_delay_nop ();
5406                 }
5407               relax_switch ();
5408               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5409                            tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5410               load_delay_nop ();
5411               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5412                            tempreg, tempreg, BFD_RELOC_LO16);
5413               relax_end ();
5414               /* FIXME: If breg == 0, and the next instruction uses
5415                  $tempreg, then if this variant case is used an extra
5416                  nop will be generated.  */
5417             }
5418           else if (offset_expr.X_add_number >= -0x8000
5419                    && offset_expr.X_add_number < 0x8000)
5420             {
5421               load_got_offset (tempreg, &offset_expr);
5422               load_delay_nop ();
5423               add_got_offset (tempreg, &offset_expr);
5424             }
5425           else
5426             {
5427               expr1.X_add_number = offset_expr.X_add_number;
5428               offset_expr.X_add_number =
5429                 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5430               load_got_offset (tempreg, &offset_expr);
5431               offset_expr.X_add_number = expr1.X_add_number;
5432               /* If we are going to add in a base register, and the
5433                  target register and the base register are the same,
5434                  then we are using AT as a temporary register.  Since
5435                  we want to load the constant into AT, we add our
5436                  current AT (from the global offset table) and the
5437                  register into the register now, and pretend we were
5438                  not using a base register.  */
5439               if (breg == treg)
5440                 {
5441                   load_delay_nop ();
5442                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5443                                treg, AT, breg);
5444                   breg = 0;
5445                   tempreg = treg;
5446                 }
5447               add_got_offset_hilo (tempreg, &offset_expr, AT);
5448               used_at = 1;
5449             }
5450         }
5451       else if (!mips_big_got && HAVE_NEWABI)
5452         {
5453           int add_breg_early = 0;
5454
5455           /* If this is a reference to an external, and there is no
5456              constant, or local symbol (*), with or without a
5457              constant, we want
5458                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5459              or for lca or if tempreg is PIC_CALL_REG
5460                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5461
5462              If we have a small constant, and this is a reference to
5463              an external symbol, we want
5464                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5465                addiu    $tempreg,$tempreg,<constant>
5466
5467              If we have a large constant, and this is a reference to
5468              an external symbol, we want
5469                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5470                lui      $at,<hiconstant>
5471                addiu    $at,$at,<loconstant>
5472                addu     $tempreg,$tempreg,$at
5473
5474              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5475              local symbols, even though it introduces an additional
5476              instruction.  */
5477
5478           if (offset_expr.X_add_number)
5479             {
5480               expr1.X_add_number = offset_expr.X_add_number;
5481               offset_expr.X_add_number = 0;
5482
5483               relax_start (offset_expr.X_add_symbol);
5484               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5485                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5486
5487               if (expr1.X_add_number >= -0x8000
5488                   && expr1.X_add_number < 0x8000)
5489                 {
5490                   macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5491                                tempreg, tempreg, BFD_RELOC_LO16);
5492                 }
5493               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5494                 {
5495                   int dreg;
5496
5497                   /* If we are going to add in a base register, and the
5498                      target register and the base register are the same,
5499                      then we are using AT as a temporary register.  Since
5500                      we want to load the constant into AT, we add our
5501                      current AT (from the global offset table) and the
5502                      register into the register now, and pretend we were
5503                      not using a base register.  */
5504                   if (breg != treg)
5505                     dreg = tempreg;
5506                   else
5507                     {
5508                       assert (tempreg == AT);
5509                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5510                                    treg, AT, breg);
5511                       dreg = treg;
5512                       add_breg_early = 1;
5513                     }
5514
5515                   load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5516                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5517                                dreg, dreg, AT);
5518
5519                   used_at = 1;
5520                 }
5521               else
5522                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5523
5524               relax_switch ();
5525               offset_expr.X_add_number = expr1.X_add_number;
5526
5527               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5528                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5529               if (add_breg_early)
5530                 {
5531                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5532                                treg, tempreg, breg);
5533                   breg = 0;
5534                   tempreg = treg;
5535                 }
5536               relax_end ();
5537             }
5538           else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5539             {
5540               relax_start (offset_expr.X_add_symbol);
5541               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5542                            BFD_RELOC_MIPS_CALL16, mips_gp_register);
5543               relax_switch ();
5544               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5545                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5546               relax_end ();
5547             }
5548           else
5549             {
5550               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5551                            BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5552             }
5553         }
5554       else if (mips_big_got && !HAVE_NEWABI)
5555         {
5556           int gpdelay;
5557           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5558           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5559           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5560
5561           /* This is the large GOT case.  If this is a reference to an
5562              external symbol, and there is no constant, we want
5563                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5564                addu     $tempreg,$tempreg,$gp
5565                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5566              or for lca or if tempreg is PIC_CALL_REG
5567                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5568                addu     $tempreg,$tempreg,$gp
5569                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5570              For a local symbol, we want
5571                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5572                nop
5573                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5574
5575              If we have a small constant, and this is a reference to
5576              an external symbol, we want
5577                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5578                addu     $tempreg,$tempreg,$gp
5579                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5580                nop
5581                addiu    $tempreg,$tempreg,<constant>
5582              For a local symbol, we want
5583                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5584                nop
5585                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5586
5587              If we have a large constant, and this is a reference to
5588              an external symbol, we want
5589                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5590                addu     $tempreg,$tempreg,$gp
5591                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5592                lui      $at,<hiconstant>
5593                addiu    $at,$at,<loconstant>
5594                addu     $tempreg,$tempreg,$at
5595              For a local symbol, we want
5596                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5597                lui      $at,<hiconstant>
5598                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5599                addu     $tempreg,$tempreg,$at
5600           */
5601
5602           expr1.X_add_number = offset_expr.X_add_number;
5603           offset_expr.X_add_number = 0;
5604           relax_start (offset_expr.X_add_symbol);
5605           gpdelay = reg_needs_delay (mips_gp_register);
5606           if (expr1.X_add_number == 0 && breg == 0
5607               && (call || tempreg == PIC_CALL_REG))
5608             {
5609               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5610               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5611             }
5612           macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5613           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5614                        tempreg, tempreg, mips_gp_register);
5615           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5616                        tempreg, lw_reloc_type, tempreg);
5617           if (expr1.X_add_number == 0)
5618             {
5619               if (breg != 0)
5620                 {
5621                   /* We're going to put in an addu instruction using
5622                      tempreg, so we may as well insert the nop right
5623                      now.  */
5624                   load_delay_nop ();
5625                 }
5626             }
5627           else if (expr1.X_add_number >= -0x8000
5628                    && expr1.X_add_number < 0x8000)
5629             {
5630               load_delay_nop ();
5631               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5632                            tempreg, tempreg, BFD_RELOC_LO16);
5633             }
5634           else
5635             {
5636               int dreg;
5637
5638               /* If we are going to add in a base register, and the
5639                  target register and the base register are the same,
5640                  then we are using AT as a temporary register.  Since
5641                  we want to load the constant into AT, we add our
5642                  current AT (from the global offset table) and the
5643                  register into the register now, and pretend we were
5644                  not using a base register.  */
5645               if (breg != treg)
5646                 dreg = tempreg;
5647               else
5648                 {
5649                   assert (tempreg == AT);
5650                   load_delay_nop ();
5651                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5652                                treg, AT, breg);
5653                   dreg = treg;
5654                 }
5655
5656               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5657               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5658
5659               used_at = 1;
5660             }
5661           offset_expr.X_add_number =
5662             ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5663           relax_switch ();
5664
5665           if (gpdelay)
5666             {
5667               /* This is needed because this instruction uses $gp, but
5668                  the first instruction on the main stream does not.  */
5669               macro_build (NULL, "nop", "");
5670             }
5671
5672           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5673                        local_reloc_type, mips_gp_register);
5674           if (expr1.X_add_number >= -0x8000
5675               && expr1.X_add_number < 0x8000)
5676             {
5677               load_delay_nop ();
5678               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5679                            tempreg, tempreg, BFD_RELOC_LO16);
5680               /* FIXME: If add_number is 0, and there was no base
5681                  register, the external symbol case ended with a load,
5682                  so if the symbol turns out to not be external, and
5683                  the next instruction uses tempreg, an unnecessary nop
5684                  will be inserted.  */
5685             }
5686           else
5687             {
5688               if (breg == treg)
5689                 {
5690                   /* We must add in the base register now, as in the
5691                      external symbol case.  */
5692                   assert (tempreg == AT);
5693                   load_delay_nop ();
5694                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5695                                treg, AT, breg);
5696                   tempreg = treg;
5697                   /* We set breg to 0 because we have arranged to add
5698                      it in in both cases.  */
5699                   breg = 0;
5700                 }
5701
5702               macro_build_lui (&expr1, AT);
5703               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5704                            AT, AT, BFD_RELOC_LO16);
5705               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5706                            tempreg, tempreg, AT);
5707               used_at = 1;
5708             }
5709           relax_end ();
5710         }
5711       else if (mips_big_got && HAVE_NEWABI)
5712         {
5713           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5714           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5715           int add_breg_early = 0;
5716
5717           /* This is the large GOT case.  If this is a reference to an
5718              external symbol, and there is no constant, we want
5719                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5720                add      $tempreg,$tempreg,$gp
5721                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5722              or for lca or if tempreg is PIC_CALL_REG
5723                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5724                add      $tempreg,$tempreg,$gp
5725                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5726
5727              If we have a small constant, and this is a reference to
5728              an external symbol, we want
5729                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5730                add      $tempreg,$tempreg,$gp
5731                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5732                addi     $tempreg,$tempreg,<constant>
5733
5734              If we have a large constant, and this is a reference to
5735              an external symbol, we want
5736                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5737                addu     $tempreg,$tempreg,$gp
5738                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5739                lui      $at,<hiconstant>
5740                addi     $at,$at,<loconstant>
5741                add      $tempreg,$tempreg,$at
5742
5743              If we have NewABI, and we know it's a local symbol, we want
5744                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5745                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5746              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5747
5748           relax_start (offset_expr.X_add_symbol);
5749
5750           expr1.X_add_number = offset_expr.X_add_number;
5751           offset_expr.X_add_number = 0;
5752
5753           if (expr1.X_add_number == 0 && breg == 0
5754               && (call || tempreg == PIC_CALL_REG))
5755             {
5756               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5757               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5758             }
5759           macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5760           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5761                        tempreg, tempreg, mips_gp_register);
5762           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5763                        tempreg, lw_reloc_type, tempreg);
5764
5765           if (expr1.X_add_number == 0)
5766             ;
5767           else if (expr1.X_add_number >= -0x8000
5768                    && expr1.X_add_number < 0x8000)
5769             {
5770               macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5771                            tempreg, tempreg, BFD_RELOC_LO16);
5772             }
5773           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5774             {
5775               int dreg;
5776
5777               /* If we are going to add in a base register, and the
5778                  target register and the base register are the same,
5779                  then we are using AT as a temporary register.  Since
5780                  we want to load the constant into AT, we add our
5781                  current AT (from the global offset table) and the
5782                  register into the register now, and pretend we were
5783                  not using a base register.  */
5784               if (breg != treg)
5785                 dreg = tempreg;
5786               else
5787                 {
5788                   assert (tempreg == AT);
5789                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5790                                treg, AT, breg);
5791                   dreg = treg;
5792                   add_breg_early = 1;
5793                 }
5794
5795               load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5796               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5797
5798               used_at = 1;
5799             }
5800           else
5801             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5802
5803           relax_switch ();
5804           offset_expr.X_add_number = expr1.X_add_number;
5805           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5806                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5807           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5808                        tempreg, BFD_RELOC_MIPS_GOT_OFST);
5809           if (add_breg_early)
5810             {
5811               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5812                            treg, tempreg, breg);
5813               breg = 0;
5814               tempreg = treg;
5815             }
5816           relax_end ();
5817         }
5818       else
5819         abort ();
5820
5821       if (breg != 0)
5822         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5823       break;
5824
5825     case M_J_A:
5826       /* The j instruction may not be used in PIC code, since it
5827          requires an absolute address.  We convert it to a b
5828          instruction.  */
5829       if (mips_pic == NO_PIC)
5830         macro_build (&offset_expr, "j", "a");
5831       else
5832         macro_build (&offset_expr, "b", "p");
5833       break;
5834
5835       /* The jal instructions must be handled as macros because when
5836          generating PIC code they expand to multi-instruction
5837          sequences.  Normally they are simple instructions.  */
5838     case M_JAL_1:
5839       dreg = RA;
5840       /* Fall through.  */
5841     case M_JAL_2:
5842       if (mips_pic == NO_PIC)
5843         macro_build (NULL, "jalr", "d,s", dreg, sreg);
5844       else
5845         {
5846           if (sreg != PIC_CALL_REG)
5847             as_warn (_("MIPS PIC call to register other than $25"));
5848
5849           macro_build (NULL, "jalr", "d,s", dreg, sreg);
5850           if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
5851             {
5852               if (mips_cprestore_offset < 0)
5853                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5854               else
5855                 {
5856                   if (! mips_frame_reg_valid)
5857                     {
5858                       as_warn (_("No .frame pseudo-op used in PIC code"));
5859                       /* Quiet this warning.  */
5860                       mips_frame_reg_valid = 1;
5861                     }
5862                   if (! mips_cprestore_valid)
5863                     {
5864                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5865                       /* Quiet this warning.  */
5866                       mips_cprestore_valid = 1;
5867                     }
5868                   expr1.X_add_number = mips_cprestore_offset;
5869                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5870                                                 mips_gp_register,
5871                                                 mips_frame_reg,
5872                                                 HAVE_64BIT_ADDRESSES);
5873                 }
5874             }
5875         }
5876
5877       break;
5878
5879     case M_JAL_A:
5880       if (mips_pic == NO_PIC)
5881         macro_build (&offset_expr, "jal", "a");
5882       else if (mips_pic == SVR4_PIC)
5883         {
5884           /* If this is a reference to an external symbol, and we are
5885              using a small GOT, we want
5886                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5887                nop
5888                jalr     $ra,$25
5889                nop
5890                lw       $gp,cprestore($sp)
5891              The cprestore value is set using the .cprestore
5892              pseudo-op.  If we are using a big GOT, we want
5893                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5894                addu     $25,$25,$gp
5895                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5896                nop
5897                jalr     $ra,$25
5898                nop
5899                lw       $gp,cprestore($sp)
5900              If the symbol is not external, we want
5901                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5902                nop
5903                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5904                jalr     $ra,$25
5905                nop
5906                lw $gp,cprestore($sp)
5907
5908              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5909              sequences above, minus nops, unless the symbol is local,
5910              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5911              GOT_DISP.  */
5912           if (HAVE_NEWABI)
5913             {
5914               if (! mips_big_got)
5915                 {
5916                   relax_start (offset_expr.X_add_symbol);
5917                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5918                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5919                                mips_gp_register);
5920                   relax_switch ();
5921                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5922                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5923                                mips_gp_register);
5924                   relax_end ();
5925                 }
5926               else
5927                 {
5928                   relax_start (offset_expr.X_add_symbol);
5929                   macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5930                                BFD_RELOC_MIPS_CALL_HI16);
5931                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5932                                PIC_CALL_REG, mips_gp_register);
5933                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5934                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5935                                PIC_CALL_REG);
5936                   relax_switch ();
5937                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5938                                PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5939                                mips_gp_register);
5940                   macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5941                                PIC_CALL_REG, PIC_CALL_REG,
5942                                BFD_RELOC_MIPS_GOT_OFST);
5943                   relax_end ();
5944                 }
5945
5946               macro_build_jalr (&offset_expr);
5947             }
5948           else
5949             {
5950               relax_start (offset_expr.X_add_symbol);
5951               if (! mips_big_got)
5952                 {
5953                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5954                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5955                                mips_gp_register);
5956                   load_delay_nop ();
5957                   relax_switch ();
5958                 }
5959               else
5960                 {
5961                   int gpdelay;
5962
5963                   gpdelay = reg_needs_delay (mips_gp_register);
5964                   macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5965                                BFD_RELOC_MIPS_CALL_HI16);
5966                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5967                                PIC_CALL_REG, mips_gp_register);
5968                   macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5969                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5970                                PIC_CALL_REG);
5971                   load_delay_nop ();
5972                   relax_switch ();
5973                   if (gpdelay)
5974                     macro_build (NULL, "nop", "");
5975                 }
5976               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5977                            PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5978                            mips_gp_register);
5979               load_delay_nop ();
5980               macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5981                            PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5982               relax_end ();
5983               macro_build_jalr (&offset_expr);
5984
5985               if (mips_cprestore_offset < 0)
5986                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5987               else
5988                 {
5989                   if (! mips_frame_reg_valid)
5990                     {
5991                       as_warn (_("No .frame pseudo-op used in PIC code"));
5992                       /* Quiet this warning.  */
5993                       mips_frame_reg_valid = 1;
5994                     }
5995                   if (! mips_cprestore_valid)
5996                     {
5997                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5998                       /* Quiet this warning.  */
5999                       mips_cprestore_valid = 1;
6000                     }
6001                   if (mips_opts.noreorder)
6002                     macro_build (NULL, "nop", "");
6003                   expr1.X_add_number = mips_cprestore_offset;
6004                   macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
6005                                                 mips_gp_register,
6006                                                 mips_frame_reg,
6007                                                 HAVE_64BIT_ADDRESSES);
6008                 }
6009             }
6010         }
6011       else if (mips_pic == VXWORKS_PIC)
6012         as_bad (_("Non-PIC jump used in PIC library"));
6013       else
6014         abort ();
6015
6016       break;
6017
6018     case M_LB_AB:
6019       s = "lb";
6020       goto ld;
6021     case M_LBU_AB:
6022       s = "lbu";
6023       goto ld;
6024     case M_LH_AB:
6025       s = "lh";
6026       goto ld;
6027     case M_LHU_AB:
6028       s = "lhu";
6029       goto ld;
6030     case M_LW_AB:
6031       s = "lw";
6032       goto ld;
6033     case M_LWC0_AB:
6034       s = "lwc0";
6035       /* Itbl support may require additional care here.  */
6036       coproc = 1;
6037       goto ld;
6038     case M_LWC1_AB:
6039       s = "lwc1";
6040       /* Itbl support may require additional care here.  */
6041       coproc = 1;
6042       goto ld;
6043     case M_LWC2_AB:
6044       s = "lwc2";
6045       /* Itbl support may require additional care here.  */
6046       coproc = 1;
6047       goto ld;
6048     case M_LWC3_AB:
6049       s = "lwc3";
6050       /* Itbl support may require additional care here.  */
6051       coproc = 1;
6052       goto ld;
6053     case M_LWL_AB:
6054       s = "lwl";
6055       lr = 1;
6056       goto ld;
6057     case M_LWR_AB:
6058       s = "lwr";
6059       lr = 1;
6060       goto ld;
6061     case M_LDC1_AB:
6062       if (mips_opts.arch == CPU_R4650)
6063         {
6064           as_bad (_("opcode not supported on this processor"));
6065           break;
6066         }
6067       s = "ldc1";
6068       /* Itbl support may require additional care here.  */
6069       coproc = 1;
6070       goto ld;
6071     case M_LDC2_AB:
6072       s = "ldc2";
6073       /* Itbl support may require additional care here.  */
6074       coproc = 1;
6075       goto ld;
6076     case M_LDC3_AB:
6077       s = "ldc3";
6078       /* Itbl support may require additional care here.  */
6079       coproc = 1;
6080       goto ld;
6081     case M_LDL_AB:
6082       s = "ldl";
6083       lr = 1;
6084       goto ld;
6085     case M_LDR_AB:
6086       s = "ldr";
6087       lr = 1;
6088       goto ld;
6089     case M_LL_AB:
6090       s = "ll";
6091       goto ld;
6092     case M_LLD_AB:
6093       s = "lld";
6094       goto ld;
6095     case M_LWU_AB:
6096       s = "lwu";
6097     ld:
6098       if (breg == treg || coproc || lr)
6099         {
6100           tempreg = AT;
6101           used_at = 1;
6102         }
6103       else
6104         {
6105           tempreg = treg;
6106         }
6107       goto ld_st;
6108     case M_SB_AB:
6109       s = "sb";
6110       goto st;
6111     case M_SH_AB:
6112       s = "sh";
6113       goto st;
6114     case M_SW_AB:
6115       s = "sw";
6116       goto st;
6117     case M_SWC0_AB:
6118       s = "swc0";
6119       /* Itbl support may require additional care here.  */
6120       coproc = 1;
6121       goto st;
6122     case M_SWC1_AB:
6123       s = "swc1";
6124       /* Itbl support may require additional care here.  */
6125       coproc = 1;
6126       goto st;
6127     case M_SWC2_AB:
6128       s = "swc2";
6129       /* Itbl support may require additional care here.  */
6130       coproc = 1;
6131       goto st;
6132     case M_SWC3_AB:
6133       s = "swc3";
6134       /* Itbl support may require additional care here.  */
6135       coproc = 1;
6136       goto st;
6137     case M_SWL_AB:
6138       s = "swl";
6139       goto st;
6140     case M_SWR_AB:
6141       s = "swr";
6142       goto st;
6143     case M_SC_AB:
6144       s = "sc";
6145       goto st;
6146     case M_SCD_AB:
6147       s = "scd";
6148       goto st;
6149     case M_CACHE_AB:
6150       s = "cache";
6151       goto st;
6152     case M_SDC1_AB:
6153       if (mips_opts.arch == CPU_R4650)
6154         {
6155           as_bad (_("opcode not supported on this processor"));
6156           break;
6157         }
6158       s = "sdc1";
6159       coproc = 1;
6160       /* Itbl support may require additional care here.  */
6161       goto st;
6162     case M_SDC2_AB:
6163       s = "sdc2";
6164       /* Itbl support may require additional care here.  */
6165       coproc = 1;
6166       goto st;
6167     case M_SDC3_AB:
6168       s = "sdc3";
6169       /* Itbl support may require additional care here.  */
6170       coproc = 1;
6171       goto st;
6172     case M_SDL_AB:
6173       s = "sdl";
6174       goto st;
6175     case M_SDR_AB:
6176       s = "sdr";
6177     st:
6178       tempreg = AT;
6179       used_at = 1;
6180     ld_st:
6181       /* Itbl support may require additional care here.  */
6182       if (mask == M_LWC1_AB
6183           || mask == M_SWC1_AB
6184           || mask == M_LDC1_AB
6185           || mask == M_SDC1_AB
6186           || mask == M_L_DAB
6187           || mask == M_S_DAB)
6188         fmt = "T,o(b)";
6189       else if (mask == M_CACHE_AB)
6190         fmt = "k,o(b)";
6191       else if (coproc)
6192         fmt = "E,o(b)";
6193       else
6194         fmt = "t,o(b)";
6195
6196       if (offset_expr.X_op != O_constant
6197           && offset_expr.X_op != O_symbol)
6198         {
6199           as_bad (_("expression too complex"));
6200           offset_expr.X_op = O_constant;
6201         }
6202
6203       if (HAVE_32BIT_ADDRESSES
6204           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6205         {
6206           char value [32];
6207
6208           sprintf_vma (value, offset_expr.X_add_number);
6209           as_bad (_("Number (0x%s) larger than 32 bits"), value);
6210         }
6211
6212       /* A constant expression in PIC code can be handled just as it
6213          is in non PIC code.  */
6214       if (offset_expr.X_op == O_constant)
6215         {
6216           expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6217                                 & ~(bfd_vma) 0xffff);
6218           normalize_address_expr (&expr1);
6219           load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6220           if (breg != 0)
6221             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6222                          tempreg, tempreg, breg);
6223           macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6224         }
6225       else if (mips_pic == NO_PIC)
6226         {
6227           /* If this is a reference to a GP relative symbol, and there
6228              is no base register, we want
6229                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6230              Otherwise, if there is no base register, we want
6231                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6232                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6233              If we have a constant, we need two instructions anyhow,
6234              so we always use the latter form.
6235
6236              If we have a base register, and this is a reference to a
6237              GP relative symbol, we want
6238                addu     $tempreg,$breg,$gp
6239                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6240              Otherwise we want
6241                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6242                addu     $tempreg,$tempreg,$breg
6243                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6244              With a constant we always use the latter case.
6245
6246              With 64bit address space and no base register and $at usable,
6247              we want
6248                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6249                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6250                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6251                dsll32   $tempreg,0
6252                daddu    $tempreg,$at
6253                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6254              If we have a base register, we want
6255                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6256                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6257                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6258                daddu    $at,$breg
6259                dsll32   $tempreg,0
6260                daddu    $tempreg,$at
6261                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6262
6263              Without $at we can't generate the optimal path for superscalar
6264              processors here since this would require two temporary registers.
6265                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6266                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6267                dsll     $tempreg,16
6268                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6269                dsll     $tempreg,16
6270                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6271              If we have a base register, we want
6272                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6273                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6274                dsll     $tempreg,16
6275                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6276                dsll     $tempreg,16
6277                daddu    $tempreg,$tempreg,$breg
6278                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6279
6280              For GP relative symbols in 64bit address space we can use
6281              the same sequence as in 32bit address space.  */
6282           if (HAVE_64BIT_SYMBOLS)
6283             {
6284               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6285                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6286                 {
6287                   relax_start (offset_expr.X_add_symbol);
6288                   if (breg == 0)
6289                     {
6290                       macro_build (&offset_expr, s, fmt, treg,
6291                                    BFD_RELOC_GPREL16, mips_gp_register);
6292                     }
6293                   else
6294                     {
6295                       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6296                                    tempreg, breg, mips_gp_register);
6297                       macro_build (&offset_expr, s, fmt, treg,
6298                                    BFD_RELOC_GPREL16, tempreg);
6299                     }
6300                   relax_switch ();
6301                 }
6302
6303               if (used_at == 0 && !mips_opts.noat)
6304                 {
6305                   macro_build (&offset_expr, "lui", "t,u", tempreg,
6306                                BFD_RELOC_MIPS_HIGHEST);
6307                   macro_build (&offset_expr, "lui", "t,u", AT,
6308                                BFD_RELOC_HI16_S);
6309                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6310                                tempreg, BFD_RELOC_MIPS_HIGHER);
6311                   if (breg != 0)
6312                     macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6313                   macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6314                   macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6315                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6316                                tempreg);
6317                   used_at = 1;
6318                 }
6319               else
6320                 {
6321                   macro_build (&offset_expr, "lui", "t,u", tempreg,
6322                                BFD_RELOC_MIPS_HIGHEST);
6323                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6324                                tempreg, BFD_RELOC_MIPS_HIGHER);
6325                   macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6326                   macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6327                                tempreg, BFD_RELOC_HI16_S);
6328                   macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6329                   if (breg != 0)
6330                     macro_build (NULL, "daddu", "d,v,t",
6331                                  tempreg, tempreg, breg);
6332                   macro_build (&offset_expr, s, fmt, treg,
6333                                BFD_RELOC_LO16, tempreg);
6334                 }
6335
6336               if (mips_relax.sequence)
6337                 relax_end ();
6338               break;
6339             }
6340
6341           if (breg == 0)
6342             {
6343               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6344                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6345                 {
6346                   relax_start (offset_expr.X_add_symbol);
6347                   macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6348                                mips_gp_register);
6349                   relax_switch ();
6350                 }
6351               macro_build_lui (&offset_expr, tempreg);
6352               macro_build (&offset_expr, s, fmt, treg,
6353                            BFD_RELOC_LO16, tempreg);
6354               if (mips_relax.sequence)
6355                 relax_end ();
6356             }
6357           else
6358             {
6359               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6360                   && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6361                 {
6362                   relax_start (offset_expr.X_add_symbol);
6363                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6364                                tempreg, breg, mips_gp_register);
6365                   macro_build (&offset_expr, s, fmt, treg,
6366                                BFD_RELOC_GPREL16, tempreg);
6367                   relax_switch ();
6368                 }
6369               macro_build_lui (&offset_expr, tempreg);
6370               macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6371                            tempreg, tempreg, breg);
6372               macro_build (&offset_expr, s, fmt, treg,
6373                            BFD_RELOC_LO16, tempreg);
6374               if (mips_relax.sequence)
6375                 relax_end ();
6376             }
6377         }
6378       else if (!mips_big_got)
6379         {
6380           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6381
6382           /* If this is a reference to an external symbol, we want
6383                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6384                nop
6385                <op>     $treg,0($tempreg)
6386              Otherwise we want
6387                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6388                nop
6389                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6390                <op>     $treg,0($tempreg)
6391
6392              For NewABI, we want
6393                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6394                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6395
6396              If there is a base register, we add it to $tempreg before
6397              the <op>.  If there is a constant, we stick it in the
6398              <op> instruction.  We don't handle constants larger than
6399              16 bits, because we have no way to load the upper 16 bits
6400              (actually, we could handle them for the subset of cases
6401              in which we are not using $at).  */
6402           assert (offset_expr.X_op == O_symbol);
6403           if (HAVE_NEWABI)
6404             {
6405               macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6406                            BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6407               if (breg != 0)
6408                 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6409                              tempreg, tempreg, breg);
6410               macro_build (&offset_expr, s, fmt, treg,
6411                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
6412               break;
6413             }
6414           expr1.X_add_number = offset_expr.X_add_number;
6415           offset_expr.X_add_number = 0;
6416           if (expr1.X_add_number < -0x8000
6417               || expr1.X_add_number >= 0x8000)
6418             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6419           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6420                        lw_reloc_type, mips_gp_register);
6421           load_delay_nop ();
6422           relax_start (offset_expr.X_add_symbol);
6423           relax_switch ();
6424           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6425                        tempreg, BFD_RELOC_LO16);
6426           relax_end ();
6427           if (breg != 0)
6428             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6429                          tempreg, tempreg, breg);
6430           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6431         }
6432       else if (mips_big_got && !HAVE_NEWABI)
6433         {
6434           int gpdelay;
6435
6436           /* If this is a reference to an external symbol, we want
6437                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6438                addu     $tempreg,$tempreg,$gp
6439                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6440                <op>     $treg,0($tempreg)
6441              Otherwise we want
6442                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6443                nop
6444                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6445                <op>     $treg,0($tempreg)
6446              If there is a base register, we add it to $tempreg before
6447              the <op>.  If there is a constant, we stick it in the
6448              <op> instruction.  We don't handle constants larger than
6449              16 bits, because we have no way to load the upper 16 bits
6450              (actually, we could handle them for the subset of cases
6451              in which we are not using $at).  */
6452           assert (offset_expr.X_op == O_symbol);
6453           expr1.X_add_number = offset_expr.X_add_number;
6454           offset_expr.X_add_number = 0;
6455           if (expr1.X_add_number < -0x8000
6456               || expr1.X_add_number >= 0x8000)
6457             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6458           gpdelay = reg_needs_delay (mips_gp_register);
6459           relax_start (offset_expr.X_add_symbol);
6460           macro_build (&offset_expr, "lui", "t,u", tempreg,
6461                        BFD_RELOC_MIPS_GOT_HI16);
6462           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6463                        mips_gp_register);
6464           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6465                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
6466           relax_switch ();
6467           if (gpdelay)
6468             macro_build (NULL, "nop", "");
6469           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6470                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
6471           load_delay_nop ();
6472           macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6473                        tempreg, BFD_RELOC_LO16);
6474           relax_end ();
6475
6476           if (breg != 0)
6477             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6478                          tempreg, tempreg, breg);
6479           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6480         }
6481       else if (mips_big_got && HAVE_NEWABI)
6482         {
6483           /* If this is a reference to an external symbol, we want
6484                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6485                add      $tempreg,$tempreg,$gp
6486                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6487                <op>     $treg,<ofst>($tempreg)
6488              Otherwise, for local symbols, we want:
6489                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6490                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6491           assert (offset_expr.X_op == O_symbol);
6492           expr1.X_add_number = offset_expr.X_add_number;
6493           offset_expr.X_add_number = 0;
6494           if (expr1.X_add_number < -0x8000
6495               || expr1.X_add_number >= 0x8000)
6496             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6497           relax_start (offset_expr.X_add_symbol);
6498           macro_build (&offset_expr, "lui", "t,u", tempreg,
6499                        BFD_RELOC_MIPS_GOT_HI16);
6500           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6501                        mips_gp_register);
6502           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6503                        BFD_RELOC_MIPS_GOT_LO16, tempreg);
6504           if (breg != 0)
6505             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6506                          tempreg, tempreg, breg);
6507           macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6508
6509           relax_switch ();
6510           offset_expr.X_add_number = expr1.X_add_number;
6511           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6512                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6513           if (breg != 0)
6514             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6515                          tempreg, tempreg, breg);
6516           macro_build (&offset_expr, s, fmt, treg,
6517                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
6518           relax_end ();
6519         }
6520       else
6521         abort ();
6522
6523       break;
6524
6525     case M_LI:
6526     case M_LI_S:
6527       load_register (treg, &imm_expr, 0);
6528       break;
6529
6530     case M_DLI:
6531       load_register (treg, &imm_expr, 1);
6532       break;
6533
6534     case M_LI_SS:
6535       if (imm_expr.X_op == O_constant)
6536         {
6537           used_at = 1;
6538           load_register (AT, &imm_expr, 0);
6539           macro_build (NULL, "mtc1", "t,G", AT, treg);
6540           break;
6541         }
6542       else
6543         {
6544           assert (offset_expr.X_op == O_symbol
6545                   && strcmp (segment_name (S_GET_SEGMENT
6546                                            (offset_expr.X_add_symbol)),
6547                              ".lit4") == 0
6548                   && offset_expr.X_add_number == 0);
6549           macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6550                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6551           break;
6552         }
6553
6554     case M_LI_D:
6555       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6556          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6557          order 32 bits of the value and the low order 32 bits are either
6558          zero or in OFFSET_EXPR.  */
6559       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6560         {
6561           if (HAVE_64BIT_GPRS)
6562             load_register (treg, &imm_expr, 1);
6563           else
6564             {
6565               int hreg, lreg;
6566
6567               if (target_big_endian)
6568                 {
6569                   hreg = treg;
6570                   lreg = treg + 1;
6571                 }
6572               else
6573                 {
6574                   hreg = treg + 1;
6575                   lreg = treg;
6576                 }
6577
6578               if (hreg <= 31)
6579                 load_register (hreg, &imm_expr, 0);
6580               if (lreg <= 31)
6581                 {
6582                   if (offset_expr.X_op == O_absent)
6583                     move_register (lreg, 0);
6584                   else
6585                     {
6586                       assert (offset_expr.X_op == O_constant);
6587                       load_register (lreg, &offset_expr, 0);
6588                     }
6589                 }
6590             }
6591           break;
6592         }
6593
6594       /* We know that sym is in the .rdata section.  First we get the
6595          upper 16 bits of the address.  */
6596       if (mips_pic == NO_PIC)
6597         {
6598           macro_build_lui (&offset_expr, AT);
6599           used_at = 1;
6600         }
6601       else
6602         {
6603           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6604                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
6605           used_at = 1;
6606         }
6607
6608       /* Now we load the register(s).  */
6609       if (HAVE_64BIT_GPRS)
6610         {
6611           used_at = 1;
6612           macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6613         }
6614       else
6615         {
6616           used_at = 1;
6617           macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6618           if (treg != RA)
6619             {
6620               /* FIXME: How in the world do we deal with the possible
6621                  overflow here?  */
6622               offset_expr.X_add_number += 4;
6623               macro_build (&offset_expr, "lw", "t,o(b)",
6624                            treg + 1, BFD_RELOC_LO16, AT);
6625             }
6626         }
6627       break;
6628
6629     case M_LI_DD:
6630       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6631          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6632          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6633          the value and the low order 32 bits are either zero or in
6634          OFFSET_EXPR.  */
6635       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6636         {
6637           used_at = 1;
6638           load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6639           if (HAVE_64BIT_FPRS)
6640             {
6641               assert (HAVE_64BIT_GPRS);
6642               macro_build (NULL, "dmtc1", "t,S", AT, treg);
6643             }
6644           else
6645             {
6646               macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6647               if (offset_expr.X_op == O_absent)
6648                 macro_build (NULL, "mtc1", "t,G", 0, treg);
6649               else
6650                 {
6651                   assert (offset_expr.X_op == O_constant);
6652                   load_register (AT, &offset_expr, 0);
6653                   macro_build (NULL, "mtc1", "t,G", AT, treg);
6654                 }
6655             }
6656           break;
6657         }
6658
6659       assert (offset_expr.X_op == O_symbol
6660               && offset_expr.X_add_number == 0);
6661       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6662       if (strcmp (s, ".lit8") == 0)
6663         {
6664           if (mips_opts.isa != ISA_MIPS1)
6665             {
6666               macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6667                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6668               break;
6669             }
6670           breg = mips_gp_register;
6671           r = BFD_RELOC_MIPS_LITERAL;
6672           goto dob;
6673         }
6674       else
6675         {
6676           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6677           used_at = 1;
6678           if (mips_pic != NO_PIC)
6679             macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6680                          BFD_RELOC_MIPS_GOT16, mips_gp_register);
6681           else
6682             {
6683               /* FIXME: This won't work for a 64 bit address.  */
6684               macro_build_lui (&offset_expr, AT);
6685             }
6686
6687           if (mips_opts.isa != ISA_MIPS1)
6688             {
6689               macro_build (&offset_expr, "ldc1", "T,o(b)",
6690                            treg, BFD_RELOC_LO16, AT);
6691               break;
6692             }
6693           breg = AT;
6694           r = BFD_RELOC_LO16;
6695           goto dob;
6696         }
6697
6698     case M_L_DOB:
6699       if (mips_opts.arch == CPU_R4650)
6700         {
6701           as_bad (_("opcode not supported on this processor"));
6702           break;
6703         }
6704       /* Even on a big endian machine $fn comes before $fn+1.  We have
6705          to adjust when loading from memory.  */
6706       r = BFD_RELOC_LO16;
6707     dob:
6708       assert (mips_opts.isa == ISA_MIPS1);
6709       macro_build (&offset_expr, "lwc1", "T,o(b)",
6710                    target_big_endian ? treg + 1 : treg, r, breg);
6711       /* FIXME: A possible overflow which I don't know how to deal
6712          with.  */
6713       offset_expr.X_add_number += 4;
6714       macro_build (&offset_expr, "lwc1", "T,o(b)",
6715                    target_big_endian ? treg : treg + 1, r, breg);
6716       break;
6717
6718     case M_L_DAB:
6719       /*
6720        * The MIPS assembler seems to check for X_add_number not
6721        * being double aligned and generating:
6722        *        lui     at,%hi(foo+1)
6723        *        addu    at,at,v1
6724        *        addiu   at,at,%lo(foo+1)
6725        *        lwc1    f2,0(at)
6726        *        lwc1    f3,4(at)
6727        * But, the resulting address is the same after relocation so why
6728        * generate the extra instruction?
6729        */
6730       if (mips_opts.arch == CPU_R4650)
6731         {
6732           as_bad (_("opcode not supported on this processor"));
6733           break;
6734         }
6735       /* Itbl support may require additional care here.  */
6736       coproc = 1;
6737       if (mips_opts.isa != ISA_MIPS1)
6738         {
6739           s = "ldc1";
6740           goto ld;
6741         }
6742
6743       s = "lwc1";
6744       fmt = "T,o(b)";
6745       goto ldd_std;
6746
6747     case M_S_DAB:
6748       if (mips_opts.arch == CPU_R4650)
6749         {
6750           as_bad (_("opcode not supported on this processor"));
6751           break;
6752         }
6753
6754       if (mips_opts.isa != ISA_MIPS1)
6755         {
6756           s = "sdc1";
6757           goto st;
6758         }
6759
6760       s = "swc1";
6761       fmt = "T,o(b)";
6762       /* Itbl support may require additional care here.  */
6763       coproc = 1;
6764       goto ldd_std;
6765
6766     case M_LD_AB:
6767       if (HAVE_64BIT_GPRS)
6768         {
6769           s = "ld";
6770           goto ld;
6771         }
6772
6773       s = "lw";
6774       fmt = "t,o(b)";
6775       goto ldd_std;
6776
6777     case M_SD_AB:
6778       if (HAVE_64BIT_GPRS)
6779         {
6780           s = "sd";
6781           goto st;
6782         }
6783
6784       s = "sw";
6785       fmt = "t,o(b)";
6786
6787     ldd_std:
6788       if (offset_expr.X_op != O_symbol
6789           && offset_expr.X_op != O_constant)
6790         {
6791           as_bad (_("expression too complex"));
6792           offset_expr.X_op = O_constant;
6793         }
6794
6795       if (HAVE_32BIT_ADDRESSES
6796           && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6797         {
6798           char value [32];
6799
6800           sprintf_vma (value, offset_expr.X_add_number);
6801           as_bad (_("Number (0x%s) larger than 32 bits"), value);
6802         }
6803
6804       /* Even on a big endian machine $fn comes before $fn+1.  We have
6805          to adjust when loading from memory.  We set coproc if we must
6806          load $fn+1 first.  */
6807       /* Itbl support may require additional care here.  */
6808       if (! target_big_endian)
6809         coproc = 0;
6810
6811       if (mips_pic == NO_PIC
6812           || offset_expr.X_op == O_constant)
6813         {
6814           /* If this is a reference to a GP relative symbol, we want
6815                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6816                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6817              If we have a base register, we use this
6818                addu     $at,$breg,$gp
6819                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6820                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6821              If this is not a GP relative symbol, we want
6822                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6823                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6824                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6825              If there is a base register, we add it to $at after the
6826              lui instruction.  If there is a constant, we always use
6827              the last case.  */
6828           if (offset_expr.X_op == O_symbol
6829               && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6830               && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6831             {
6832               relax_start (offset_expr.X_add_symbol);
6833               if (breg == 0)
6834                 {
6835                   tempreg = mips_gp_register;
6836                 }
6837               else
6838                 {
6839                   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6840                                AT, breg, mips_gp_register);
6841                   tempreg = AT;
6842                   used_at = 1;
6843                 }
6844
6845               /* Itbl support may require additional care here.  */
6846               macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6847                            BFD_RELOC_GPREL16, tempreg);
6848               offset_expr.X_add_number += 4;
6849
6850               /* Set mips_optimize to 2 to avoid inserting an
6851                  undesired nop.  */
6852               hold_mips_optimize = mips_optimize;
6853               mips_optimize = 2;
6854               /* Itbl support may require additional care here.  */
6855               macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6856                            BFD_RELOC_GPREL16, tempreg);
6857               mips_optimize = hold_mips_optimize;
6858
6859               relax_switch ();
6860
6861               /* We just generated two relocs.  When tc_gen_reloc
6862                  handles this case, it will skip the first reloc and
6863                  handle the second.  The second reloc already has an
6864                  extra addend of 4, which we added above.  We must
6865                  subtract it out, and then subtract another 4 to make
6866                  the first reloc come out right.  The second reloc
6867                  will come out right because we are going to add 4 to
6868                  offset_expr when we build its instruction below.
6869
6870                  If we have a symbol, then we don't want to include
6871                  the offset, because it will wind up being included
6872                  when we generate the reloc.  */
6873
6874               if (offset_expr.X_op == O_constant)
6875                 offset_expr.X_add_number -= 8;
6876               else
6877                 {
6878                   offset_expr.X_add_number = -4;
6879                   offset_expr.X_op = O_constant;
6880                 }
6881             }
6882           used_at = 1;
6883           macro_build_lui (&offset_expr, AT);
6884           if (breg != 0)
6885             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6886           /* Itbl support may require additional care here.  */
6887           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6888                        BFD_RELOC_LO16, AT);
6889           /* FIXME: How do we handle overflow here?  */
6890           offset_expr.X_add_number += 4;
6891           /* Itbl support may require additional care here.  */
6892           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6893                        BFD_RELOC_LO16, AT);
6894           if (mips_relax.sequence)
6895             relax_end ();
6896         }
6897       else if (!mips_big_got)
6898         {
6899           /* If this is a reference to an external symbol, we want
6900                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6901                nop
6902                <op>     $treg,0($at)
6903                <op>     $treg+1,4($at)
6904              Otherwise we want
6905                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6906                nop
6907                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6908                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6909              If there is a base register we add it to $at before the
6910              lwc1 instructions.  If there is a constant we include it
6911              in the lwc1 instructions.  */
6912           used_at = 1;
6913           expr1.X_add_number = offset_expr.X_add_number;
6914           if (expr1.X_add_number < -0x8000
6915               || expr1.X_add_number >= 0x8000 - 4)
6916             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6917           load_got_offset (AT, &offset_expr);
6918           load_delay_nop ();
6919           if (breg != 0)
6920             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6921
6922           /* Set mips_optimize to 2 to avoid inserting an undesired
6923              nop.  */
6924           hold_mips_optimize = mips_optimize;
6925           mips_optimize = 2;
6926
6927           /* Itbl support may require additional care here.  */
6928           relax_start (offset_expr.X_add_symbol);
6929           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6930                        BFD_RELOC_LO16, AT);
6931           expr1.X_add_number += 4;
6932           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6933                        BFD_RELOC_LO16, AT);
6934           relax_switch ();
6935           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6936                        BFD_RELOC_LO16, AT);
6937           offset_expr.X_add_number += 4;
6938           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6939                        BFD_RELOC_LO16, AT);
6940           relax_end ();
6941
6942           mips_optimize = hold_mips_optimize;
6943         }
6944       else if (mips_big_got)
6945         {
6946           int gpdelay;
6947
6948           /* If this is a reference to an external symbol, we want
6949                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6950                addu     $at,$at,$gp
6951                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6952                nop
6953                <op>     $treg,0($at)
6954                <op>     $treg+1,4($at)
6955              Otherwise we want
6956                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6957                nop
6958                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6959                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6960              If there is a base register we add it to $at before the
6961              lwc1 instructions.  If there is a constant we include it
6962              in the lwc1 instructions.  */
6963           used_at = 1;
6964           expr1.X_add_number = offset_expr.X_add_number;
6965           offset_expr.X_add_number = 0;
6966           if (expr1.X_add_number < -0x8000
6967               || expr1.X_add_number >= 0x8000 - 4)
6968             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6969           gpdelay = reg_needs_delay (mips_gp_register);
6970           relax_start (offset_expr.X_add_symbol);
6971           macro_build (&offset_expr, "lui", "t,u",
6972                        AT, BFD_RELOC_MIPS_GOT_HI16);
6973           macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6974                        AT, AT, mips_gp_register);
6975           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6976                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6977           load_delay_nop ();
6978           if (breg != 0)
6979             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6980           /* Itbl support may require additional care here.  */
6981           macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6982                        BFD_RELOC_LO16, AT);
6983           expr1.X_add_number += 4;
6984
6985           /* Set mips_optimize to 2 to avoid inserting an undesired
6986              nop.  */
6987           hold_mips_optimize = mips_optimize;
6988           mips_optimize = 2;
6989           /* Itbl support may require additional care here.  */
6990           macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6991                        BFD_RELOC_LO16, AT);
6992           mips_optimize = hold_mips_optimize;
6993           expr1.X_add_number -= 4;
6994
6995           relax_switch ();
6996           offset_expr.X_add_number = expr1.X_add_number;
6997           if (gpdelay)
6998             macro_build (NULL, "nop", "");
6999           macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
7000                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
7001           load_delay_nop ();
7002           if (breg != 0)
7003             macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
7004           /* Itbl support may require additional care here.  */
7005           macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
7006                        BFD_RELOC_LO16, AT);
7007           offset_expr.X_add_number += 4;
7008
7009           /* Set mips_optimize to 2 to avoid inserting an undesired
7010              nop.  */
7011           hold_mips_optimize = mips_optimize;
7012           mips_optimize = 2;
7013           /* Itbl support may require additional care here.  */
7014           macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
7015                        BFD_RELOC_LO16, AT);
7016           mips_optimize = hold_mips_optimize;
7017           relax_end ();
7018         }
7019       else
7020         abort ();
7021
7022       break;
7023
7024     case M_LD_OB:
7025       s = "lw";
7026       goto sd_ob;
7027     case M_SD_OB:
7028       s = "sw";
7029     sd_ob:
7030       assert (HAVE_32BIT_ADDRESSES);
7031       macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7032       offset_expr.X_add_number += 4;
7033       macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
7034       break;
7035
7036    /* New code added to support COPZ instructions.
7037       This code builds table entries out of the macros in mip_opcodes.
7038       R4000 uses interlocks to handle coproc delays.
7039       Other chips (like the R3000) require nops to be inserted for delays.
7040
7041       FIXME: Currently, we require that the user handle delays.
7042       In order to fill delay slots for non-interlocked chips,
7043       we must have a way to specify delays based on the coprocessor.
7044       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7045       What are the side-effects of the cop instruction?
7046       What cache support might we have and what are its effects?
7047       Both coprocessor & memory require delays. how long???
7048       What registers are read/set/modified?
7049
7050       If an itbl is provided to interpret cop instructions,
7051       this knowledge can be encoded in the itbl spec.  */
7052
7053     case M_COP0:
7054       s = "c0";
7055       goto copz;
7056     case M_COP1:
7057       s = "c1";
7058       goto copz;
7059     case M_COP2:
7060       s = "c2";
7061       goto copz;
7062     case M_COP3:
7063       s = "c3";
7064     copz:
7065       /* For now we just do C (same as Cz).  The parameter will be
7066          stored in insn_opcode by mips_ip.  */
7067       macro_build (NULL, s, "C", ip->insn_opcode);
7068       break;
7069
7070     case M_MOVE:
7071       move_register (dreg, sreg);
7072       break;
7073
7074 #ifdef LOSING_COMPILER
7075     default:
7076       /* Try and see if this is a new itbl instruction.
7077          This code builds table entries out of the macros in mip_opcodes.
7078          FIXME: For now we just assemble the expression and pass it's
7079          value along as a 32-bit immediate.
7080          We may want to have the assembler assemble this value,
7081          so that we gain the assembler's knowledge of delay slots,
7082          symbols, etc.
7083          Would it be more efficient to use mask (id) here? */
7084       if (itbl_have_entries
7085           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7086         {
7087           s = ip->insn_mo->name;
7088           s2 = "cop3";
7089           coproc = ITBL_DECODE_PNUM (immed_expr);;
7090           macro_build (&immed_expr, s, "C");
7091           break;
7092         }
7093       macro2 (ip);
7094       break;
7095     }
7096   if (mips_opts.noat && used_at)
7097     as_bad (_("Macro used $at after \".set noat\""));
7098 }
7099
7100 static void
7101 macro2 (struct mips_cl_insn *ip)
7102 {
7103   int treg, sreg, dreg, breg;
7104   int tempreg;
7105   int mask;
7106   int used_at;
7107   expressionS expr1;
7108   const char *s;
7109   const char *s2;
7110   const char *fmt;
7111   int likely = 0;
7112   int dbl = 0;
7113   int coproc = 0;
7114   int lr = 0;
7115   int imm = 0;
7116   int off;
7117   offsetT maxnum;
7118   bfd_reloc_code_real_type r;
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, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7138       macro_build (NULL, "mflo", "d", dreg);
7139       break;
7140
7141     case M_DMUL_I:
7142       dbl = 1;
7143     case M_MUL_I:
7144       /* The MIPS assembler some times generates shifts and adds.  I'm
7145          not trying to be that fancy. GCC should do this for us
7146          anyway.  */
7147       used_at = 1;
7148       load_register (AT, &imm_expr, dbl);
7149       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7150       macro_build (NULL, "mflo", "d", dreg);
7151       break;
7152
7153     case M_DMULO_I:
7154       dbl = 1;
7155     case M_MULO_I:
7156       imm = 1;
7157       goto do_mulo;
7158
7159     case M_DMULO:
7160       dbl = 1;
7161     case M_MULO:
7162     do_mulo:
7163       start_noreorder ();
7164       used_at = 1;
7165       if (imm)
7166         load_register (AT, &imm_expr, dbl);
7167       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7168       macro_build (NULL, "mflo", "d", dreg);
7169       macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7170       macro_build (NULL, "mfhi", "d", AT);
7171       if (mips_trap)
7172         macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
7173       else
7174         {
7175           expr1.X_add_number = 8;
7176           macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7177           macro_build (NULL, "nop", "", 0);
7178           macro_build (NULL, "break", "c", 6);
7179         }
7180       end_noreorder ();
7181       macro_build (NULL, "mflo", "d", dreg);
7182       break;
7183
7184     case M_DMULOU_I:
7185       dbl = 1;
7186     case M_MULOU_I:
7187       imm = 1;
7188       goto do_mulou;
7189
7190     case M_DMULOU:
7191       dbl = 1;
7192     case M_MULOU:
7193     do_mulou:
7194       start_noreorder ();
7195       used_at = 1;
7196       if (imm)
7197         load_register (AT, &imm_expr, dbl);
7198       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
7199                    sreg, imm ? AT : treg);
7200       macro_build (NULL, "mfhi", "d", AT);
7201       macro_build (NULL, "mflo", "d", dreg);
7202       if (mips_trap)
7203         macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
7204       else
7205         {
7206           expr1.X_add_number = 8;
7207           macro_build (&expr1, "beq", "s,t,p", AT, 0);
7208           macro_build (NULL, "nop", "", 0);
7209           macro_build (NULL, "break", "c", 6);
7210         }
7211       end_noreorder ();
7212       break;
7213
7214     case M_DROL:
7215       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7216         {
7217           if (dreg == sreg)
7218             {
7219               tempreg = AT;
7220               used_at = 1;
7221             }
7222           else
7223             {
7224               tempreg = dreg;
7225             }
7226           macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7227           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
7228           break;
7229         }
7230       used_at = 1;
7231       macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7232       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7233       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7234       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7235       break;
7236
7237     case M_ROL:
7238       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7239         {
7240           if (dreg == sreg)
7241             {
7242               tempreg = AT;
7243               used_at = 1;
7244             }
7245           else
7246             {
7247               tempreg = dreg;
7248             }
7249           macro_build (NULL, "negu", "d,w", tempreg, treg);
7250           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
7251           break;
7252         }
7253       used_at = 1;
7254       macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7255       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7256       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7257       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7258       break;
7259
7260     case M_DROL_I:
7261       {
7262         unsigned int rot;
7263         char *l, *r;
7264
7265         if (imm_expr.X_op != O_constant)
7266           as_bad (_("Improper rotate count"));
7267         rot = imm_expr.X_add_number & 0x3f;
7268         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7269           {
7270             rot = (64 - rot) & 0x3f;
7271             if (rot >= 32)
7272               macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7273             else
7274               macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7275             break;
7276           }
7277         if (rot == 0)
7278           {
7279             macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7280             break;
7281           }
7282         l = (rot < 0x20) ? "dsll" : "dsll32";
7283         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7284         rot &= 0x1f;
7285         used_at = 1;
7286         macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7287         macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7288         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7289       }
7290       break;
7291
7292     case M_ROL_I:
7293       {
7294         unsigned int rot;
7295
7296         if (imm_expr.X_op != O_constant)
7297           as_bad (_("Improper rotate count"));
7298         rot = imm_expr.X_add_number & 0x1f;
7299         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7300           {
7301             macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7302             break;
7303           }
7304         if (rot == 0)
7305           {
7306             macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7307             break;
7308           }
7309         used_at = 1;
7310         macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7311         macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7312         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7313       }
7314       break;
7315
7316     case M_DROR:
7317       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7318         {
7319           macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
7320           break;
7321         }
7322       used_at = 1;
7323       macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7324       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7325       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7326       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7327       break;
7328
7329     case M_ROR:
7330       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7331         {
7332           macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7333           break;
7334         }
7335       used_at = 1;
7336       macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7337       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7338       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7339       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7340       break;
7341
7342     case M_DROR_I:
7343       {
7344         unsigned int rot;
7345         char *l, *r;
7346
7347         if (imm_expr.X_op != O_constant)
7348           as_bad (_("Improper rotate count"));
7349         rot = imm_expr.X_add_number & 0x3f;
7350         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7351           {
7352             if (rot >= 32)
7353               macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7354             else
7355               macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7356             break;
7357           }
7358         if (rot == 0)
7359           {
7360             macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7361             break;
7362           }
7363         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7364         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7365         rot &= 0x1f;
7366         used_at = 1;
7367         macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7368         macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7369         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7370       }
7371       break;
7372
7373     case M_ROR_I:
7374       {
7375         unsigned int rot;
7376
7377         if (imm_expr.X_op != O_constant)
7378           as_bad (_("Improper rotate count"));
7379         rot = imm_expr.X_add_number & 0x1f;
7380         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7381           {
7382             macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7383             break;
7384           }
7385         if (rot == 0)
7386           {
7387             macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7388             break;
7389           }
7390         used_at = 1;
7391         macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7392         macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7393         macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7394       }
7395       break;
7396
7397     case M_S_DOB:
7398       if (mips_opts.arch == CPU_R4650)
7399         {
7400           as_bad (_("opcode not supported on this processor"));
7401           break;
7402         }
7403       assert (mips_opts.isa == ISA_MIPS1);
7404       /* Even on a big endian machine $fn comes before $fn+1.  We have
7405          to adjust when storing to memory.  */
7406       macro_build (&offset_expr, "swc1", "T,o(b)",
7407                    target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7408       offset_expr.X_add_number += 4;
7409       macro_build (&offset_expr, "swc1", "T,o(b)",
7410                    target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7411       break;
7412
7413     case M_SEQ:
7414       if (sreg == 0)
7415         macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7416       else if (treg == 0)
7417         macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7418       else
7419         {
7420           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7421           macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7422         }
7423       break;
7424
7425     case M_SEQ_I:
7426       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7427         {
7428           macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7429           break;
7430         }
7431       if (sreg == 0)
7432         {
7433           as_warn (_("Instruction %s: result is always false"),
7434                    ip->insn_mo->name);
7435           move_register (dreg, 0);
7436           break;
7437         }
7438       if (imm_expr.X_op == O_constant
7439           && imm_expr.X_add_number >= 0
7440           && imm_expr.X_add_number < 0x10000)
7441         {
7442           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7443         }
7444       else if (imm_expr.X_op == O_constant
7445                && imm_expr.X_add_number > -0x8000
7446                && imm_expr.X_add_number < 0)
7447         {
7448           imm_expr.X_add_number = -imm_expr.X_add_number;
7449           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7450                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7451         }
7452       else
7453         {
7454           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7455           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7456           used_at = 1;
7457         }
7458       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7459       break;
7460
7461     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7462       s = "slt";
7463       goto sge;
7464     case M_SGEU:
7465       s = "sltu";
7466     sge:
7467       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7468       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7469       break;
7470
7471     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7472     case M_SGEU_I:
7473       if (imm_expr.X_op == O_constant
7474           && imm_expr.X_add_number >= -0x8000
7475           && imm_expr.X_add_number < 0x8000)
7476         {
7477           macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7478                        dreg, sreg, BFD_RELOC_LO16);
7479         }
7480       else
7481         {
7482           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7483           macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7484                        dreg, sreg, AT);
7485           used_at = 1;
7486         }
7487       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7488       break;
7489
7490     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7491       s = "slt";
7492       goto sgt;
7493     case M_SGTU:
7494       s = "sltu";
7495     sgt:
7496       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7497       break;
7498
7499     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7500       s = "slt";
7501       goto sgti;
7502     case M_SGTU_I:
7503       s = "sltu";
7504     sgti:
7505       used_at = 1;
7506       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7507       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7508       break;
7509
7510     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7511       s = "slt";
7512       goto sle;
7513     case M_SLEU:
7514       s = "sltu";
7515     sle:
7516       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7517       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7518       break;
7519
7520     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7521       s = "slt";
7522       goto slei;
7523     case M_SLEU_I:
7524       s = "sltu";
7525     slei:
7526       used_at = 1;
7527       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7528       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7529       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7530       break;
7531
7532     case M_SLT_I:
7533       if (imm_expr.X_op == O_constant
7534           && imm_expr.X_add_number >= -0x8000
7535           && imm_expr.X_add_number < 0x8000)
7536         {
7537           macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7538           break;
7539         }
7540       used_at = 1;
7541       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7542       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7543       break;
7544
7545     case M_SLTU_I:
7546       if (imm_expr.X_op == O_constant
7547           && imm_expr.X_add_number >= -0x8000
7548           && imm_expr.X_add_number < 0x8000)
7549         {
7550           macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7551                        BFD_RELOC_LO16);
7552           break;
7553         }
7554       used_at = 1;
7555       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7556       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7557       break;
7558
7559     case M_SNE:
7560       if (sreg == 0)
7561         macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7562       else if (treg == 0)
7563         macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7564       else
7565         {
7566           macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7567           macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7568         }
7569       break;
7570
7571     case M_SNE_I:
7572       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7573         {
7574           macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7575           break;
7576         }
7577       if (sreg == 0)
7578         {
7579           as_warn (_("Instruction %s: result is always true"),
7580                    ip->insn_mo->name);
7581           macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7582                        dreg, 0, BFD_RELOC_LO16);
7583           break;
7584         }
7585       if (imm_expr.X_op == O_constant
7586           && imm_expr.X_add_number >= 0
7587           && imm_expr.X_add_number < 0x10000)
7588         {
7589           macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7590         }
7591       else if (imm_expr.X_op == O_constant
7592                && imm_expr.X_add_number > -0x8000
7593                && imm_expr.X_add_number < 0)
7594         {
7595           imm_expr.X_add_number = -imm_expr.X_add_number;
7596           macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7597                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7598         }
7599       else
7600         {
7601           load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7602           macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7603           used_at = 1;
7604         }
7605       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7606       break;
7607
7608     case M_DSUB_I:
7609       dbl = 1;
7610     case M_SUB_I:
7611       if (imm_expr.X_op == O_constant
7612           && imm_expr.X_add_number > -0x8000
7613           && imm_expr.X_add_number <= 0x8000)
7614         {
7615           imm_expr.X_add_number = -imm_expr.X_add_number;
7616           macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7617                        dreg, sreg, BFD_RELOC_LO16);
7618           break;
7619         }
7620       used_at = 1;
7621       load_register (AT, &imm_expr, dbl);
7622       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7623       break;
7624
7625     case M_DSUBU_I:
7626       dbl = 1;
7627     case M_SUBU_I:
7628       if (imm_expr.X_op == O_constant
7629           && imm_expr.X_add_number > -0x8000
7630           && imm_expr.X_add_number <= 0x8000)
7631         {
7632           imm_expr.X_add_number = -imm_expr.X_add_number;
7633           macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7634                        dreg, sreg, BFD_RELOC_LO16);
7635           break;
7636         }
7637       used_at = 1;
7638       load_register (AT, &imm_expr, dbl);
7639       macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7640       break;
7641
7642     case M_TEQ_I:
7643       s = "teq";
7644       goto trap;
7645     case M_TGE_I:
7646       s = "tge";
7647       goto trap;
7648     case M_TGEU_I:
7649       s = "tgeu";
7650       goto trap;
7651     case M_TLT_I:
7652       s = "tlt";
7653       goto trap;
7654     case M_TLTU_I:
7655       s = "tltu";
7656       goto trap;
7657     case M_TNE_I:
7658       s = "tne";
7659     trap:
7660       used_at = 1;
7661       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7662       macro_build (NULL, s, "s,t", sreg, AT);
7663       break;
7664
7665     case M_TRUNCWS:
7666     case M_TRUNCWD:
7667       assert (mips_opts.isa == ISA_MIPS1);
7668       used_at = 1;
7669       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7670       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7671
7672       /*
7673        * Is the double cfc1 instruction a bug in the mips assembler;
7674        * or is there a reason for it?
7675        */
7676       start_noreorder ();
7677       macro_build (NULL, "cfc1", "t,G", treg, RA);
7678       macro_build (NULL, "cfc1", "t,G", treg, RA);
7679       macro_build (NULL, "nop", "");
7680       expr1.X_add_number = 3;
7681       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7682       expr1.X_add_number = 2;
7683       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7684       macro_build (NULL, "ctc1", "t,G", AT, RA);
7685       macro_build (NULL, "nop", "");
7686       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7687                    dreg, sreg);
7688       macro_build (NULL, "ctc1", "t,G", treg, RA);
7689       macro_build (NULL, "nop", "");
7690       end_noreorder ();
7691       break;
7692
7693     case M_ULH:
7694       s = "lb";
7695       goto ulh;
7696     case M_ULHU:
7697       s = "lbu";
7698     ulh:
7699       used_at = 1;
7700       if (offset_expr.X_add_number >= 0x7fff)
7701         as_bad (_("operand overflow"));
7702       if (! target_big_endian)
7703         ++offset_expr.X_add_number;
7704       macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7705       if (! target_big_endian)
7706         --offset_expr.X_add_number;
7707       else
7708         ++offset_expr.X_add_number;
7709       macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7710       macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7711       macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7712       break;
7713
7714     case M_ULD:
7715       s = "ldl";
7716       s2 = "ldr";
7717       off = 7;
7718       goto ulw;
7719     case M_ULW:
7720       s = "lwl";
7721       s2 = "lwr";
7722       off = 3;
7723     ulw:
7724       if (offset_expr.X_add_number >= 0x8000 - off)
7725         as_bad (_("operand overflow"));
7726       if (treg != breg)
7727         tempreg = treg;
7728       else
7729         {
7730           used_at = 1;
7731           tempreg = AT;
7732         }
7733       if (! target_big_endian)
7734         offset_expr.X_add_number += off;
7735       macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7736       if (! target_big_endian)
7737         offset_expr.X_add_number -= off;
7738       else
7739         offset_expr.X_add_number += off;
7740       macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7741
7742       /* If necessary, move the result in tempreg the final destination.  */
7743       if (treg == tempreg)
7744         break;
7745       /* Protect second load's delay slot.  */
7746       load_delay_nop ();
7747       move_register (treg, tempreg);
7748       break;
7749
7750     case M_ULD_A:
7751       s = "ldl";
7752       s2 = "ldr";
7753       off = 7;
7754       goto ulwa;
7755     case M_ULW_A:
7756       s = "lwl";
7757       s2 = "lwr";
7758       off = 3;
7759     ulwa:
7760       used_at = 1;
7761       load_address (AT, &offset_expr, &used_at);
7762       if (breg != 0)
7763         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7764       if (! target_big_endian)
7765         expr1.X_add_number = off;
7766       else
7767         expr1.X_add_number = 0;
7768       macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7769       if (! target_big_endian)
7770         expr1.X_add_number = 0;
7771       else
7772         expr1.X_add_number = off;
7773       macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7774       break;
7775
7776     case M_ULH_A:
7777     case M_ULHU_A:
7778       used_at = 1;
7779       load_address (AT, &offset_expr, &used_at);
7780       if (breg != 0)
7781         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7782       if (target_big_endian)
7783         expr1.X_add_number = 0;
7784       macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7785                    treg, BFD_RELOC_LO16, AT);
7786       if (target_big_endian)
7787         expr1.X_add_number = 1;
7788       else
7789         expr1.X_add_number = 0;
7790       macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7791       macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7792       macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7793       break;
7794
7795     case M_USH:
7796       used_at = 1;
7797       if (offset_expr.X_add_number >= 0x7fff)
7798         as_bad (_("operand overflow"));
7799       if (target_big_endian)
7800         ++offset_expr.X_add_number;
7801       macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7802       macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7803       if (target_big_endian)
7804         --offset_expr.X_add_number;
7805       else
7806         ++offset_expr.X_add_number;
7807       macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7808       break;
7809
7810     case M_USD:
7811       s = "sdl";
7812       s2 = "sdr";
7813       off = 7;
7814       goto usw;
7815     case M_USW:
7816       s = "swl";
7817       s2 = "swr";
7818       off = 3;
7819     usw:
7820       if (offset_expr.X_add_number >= 0x8000 - off)
7821         as_bad (_("operand overflow"));
7822       if (! target_big_endian)
7823         offset_expr.X_add_number += off;
7824       macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7825       if (! target_big_endian)
7826         offset_expr.X_add_number -= off;
7827       else
7828         offset_expr.X_add_number += off;
7829       macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7830       break;
7831
7832     case M_USD_A:
7833       s = "sdl";
7834       s2 = "sdr";
7835       off = 7;
7836       goto uswa;
7837     case M_USW_A:
7838       s = "swl";
7839       s2 = "swr";
7840       off = 3;
7841     uswa:
7842       used_at = 1;
7843       load_address (AT, &offset_expr, &used_at);
7844       if (breg != 0)
7845         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7846       if (! target_big_endian)
7847         expr1.X_add_number = off;
7848       else
7849         expr1.X_add_number = 0;
7850       macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7851       if (! target_big_endian)
7852         expr1.X_add_number = 0;
7853       else
7854         expr1.X_add_number = off;
7855       macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7856       break;
7857
7858     case M_USH_A:
7859       used_at = 1;
7860       load_address (AT, &offset_expr, &used_at);
7861       if (breg != 0)
7862         macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7863       if (! target_big_endian)
7864         expr1.X_add_number = 0;
7865       macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7866       macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7867       if (! target_big_endian)
7868         expr1.X_add_number = 1;
7869       else
7870         expr1.X_add_number = 0;
7871       macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7872       if (! target_big_endian)
7873         expr1.X_add_number = 0;
7874       else
7875         expr1.X_add_number = 1;
7876       macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7877       macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7878       macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7879       break;
7880
7881     default:
7882       /* FIXME: Check if this is one of the itbl macros, since they
7883          are added dynamically.  */
7884       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7885       break;
7886     }
7887   if (mips_opts.noat && used_at)
7888     as_bad (_("Macro used $at after \".set noat\""));
7889 }
7890
7891 /* Implement macros in mips16 mode.  */
7892
7893 static void
7894 mips16_macro (struct mips_cl_insn *ip)
7895 {
7896   int mask;
7897   int xreg, yreg, zreg, tmp;
7898   expressionS expr1;
7899   int dbl;
7900   const char *s, *s2, *s3;
7901
7902   mask = ip->insn_mo->mask;
7903
7904   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7905   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7906   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7907
7908   expr1.X_op = O_constant;
7909   expr1.X_op_symbol = NULL;
7910   expr1.X_add_symbol = NULL;
7911   expr1.X_add_number = 1;
7912
7913   dbl = 0;
7914
7915   switch (mask)
7916     {
7917     default:
7918       internalError ();
7919
7920     case M_DDIV_3:
7921       dbl = 1;
7922     case M_DIV_3:
7923       s = "mflo";
7924       goto do_div3;
7925     case M_DREM_3:
7926       dbl = 1;
7927     case M_REM_3:
7928       s = "mfhi";
7929     do_div3:
7930       start_noreorder ();
7931       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7932       expr1.X_add_number = 2;
7933       macro_build (&expr1, "bnez", "x,p", yreg);
7934       macro_build (NULL, "break", "6", 7);
7935
7936       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7937          since that causes an overflow.  We should do that as well,
7938          but I don't see how to do the comparisons without a temporary
7939          register.  */
7940       end_noreorder ();
7941       macro_build (NULL, s, "x", zreg);
7942       break;
7943
7944     case M_DIVU_3:
7945       s = "divu";
7946       s2 = "mflo";
7947       goto do_divu3;
7948     case M_REMU_3:
7949       s = "divu";
7950       s2 = "mfhi";
7951       goto do_divu3;
7952     case M_DDIVU_3:
7953       s = "ddivu";
7954       s2 = "mflo";
7955       goto do_divu3;
7956     case M_DREMU_3:
7957       s = "ddivu";
7958       s2 = "mfhi";
7959     do_divu3:
7960       start_noreorder ();
7961       macro_build (NULL, s, "0,x,y", xreg, yreg);
7962       expr1.X_add_number = 2;
7963       macro_build (&expr1, "bnez", "x,p", yreg);
7964       macro_build (NULL, "break", "6", 7);
7965       end_noreorder ();
7966       macro_build (NULL, s2, "x", zreg);
7967       break;
7968
7969     case M_DMUL:
7970       dbl = 1;
7971     case M_MUL:
7972       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7973       macro_build (NULL, "mflo", "x", zreg);
7974       break;
7975
7976     case M_DSUBU_I:
7977       dbl = 1;
7978       goto do_subu;
7979     case M_SUBU_I:
7980     do_subu:
7981       if (imm_expr.X_op != O_constant)
7982         as_bad (_("Unsupported large constant"));
7983       imm_expr.X_add_number = -imm_expr.X_add_number;
7984       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7985       break;
7986
7987     case M_SUBU_I_2:
7988       if (imm_expr.X_op != O_constant)
7989         as_bad (_("Unsupported large constant"));
7990       imm_expr.X_add_number = -imm_expr.X_add_number;
7991       macro_build (&imm_expr, "addiu", "x,k", xreg);
7992       break;
7993
7994     case M_DSUBU_I_2:
7995       if (imm_expr.X_op != O_constant)
7996         as_bad (_("Unsupported large constant"));
7997       imm_expr.X_add_number = -imm_expr.X_add_number;
7998       macro_build (&imm_expr, "daddiu", "y,j", yreg);
7999       break;
8000
8001     case M_BEQ:
8002       s = "cmp";
8003       s2 = "bteqz";
8004       goto do_branch;
8005     case M_BNE:
8006       s = "cmp";
8007       s2 = "btnez";
8008       goto do_branch;
8009     case M_BLT:
8010       s = "slt";
8011       s2 = "btnez";
8012       goto do_branch;
8013     case M_BLTU:
8014       s = "sltu";
8015       s2 = "btnez";
8016       goto do_branch;
8017     case M_BLE:
8018       s = "slt";
8019       s2 = "bteqz";
8020       goto do_reverse_branch;
8021     case M_BLEU:
8022       s = "sltu";
8023       s2 = "bteqz";
8024       goto do_reverse_branch;
8025     case M_BGE:
8026       s = "slt";
8027       s2 = "bteqz";
8028       goto do_branch;
8029     case M_BGEU:
8030       s = "sltu";
8031       s2 = "bteqz";
8032       goto do_branch;
8033     case M_BGT:
8034       s = "slt";
8035       s2 = "btnez";
8036       goto do_reverse_branch;
8037     case M_BGTU:
8038       s = "sltu";
8039       s2 = "btnez";
8040
8041     do_reverse_branch:
8042       tmp = xreg;
8043       xreg = yreg;
8044       yreg = tmp;
8045
8046     do_branch:
8047       macro_build (NULL, s, "x,y", xreg, yreg);
8048       macro_build (&offset_expr, s2, "p");
8049       break;
8050
8051     case M_BEQ_I:
8052       s = "cmpi";
8053       s2 = "bteqz";
8054       s3 = "x,U";
8055       goto do_branch_i;
8056     case M_BNE_I:
8057       s = "cmpi";
8058       s2 = "btnez";
8059       s3 = "x,U";
8060       goto do_branch_i;
8061     case M_BLT_I:
8062       s = "slti";
8063       s2 = "btnez";
8064       s3 = "x,8";
8065       goto do_branch_i;
8066     case M_BLTU_I:
8067       s = "sltiu";
8068       s2 = "btnez";
8069       s3 = "x,8";
8070       goto do_branch_i;
8071     case M_BLE_I:
8072       s = "slti";
8073       s2 = "btnez";
8074       s3 = "x,8";
8075       goto do_addone_branch_i;
8076     case M_BLEU_I:
8077       s = "sltiu";
8078       s2 = "btnez";
8079       s3 = "x,8";
8080       goto do_addone_branch_i;
8081     case M_BGE_I:
8082       s = "slti";
8083       s2 = "bteqz";
8084       s3 = "x,8";
8085       goto do_branch_i;
8086     case M_BGEU_I:
8087       s = "sltiu";
8088       s2 = "bteqz";
8089       s3 = "x,8";
8090       goto do_branch_i;
8091     case M_BGT_I:
8092       s = "slti";
8093       s2 = "bteqz";
8094       s3 = "x,8";
8095       goto do_addone_branch_i;
8096     case M_BGTU_I:
8097       s = "sltiu";
8098       s2 = "bteqz";
8099       s3 = "x,8";
8100
8101     do_addone_branch_i:
8102       if (imm_expr.X_op != O_constant)
8103         as_bad (_("Unsupported large constant"));
8104       ++imm_expr.X_add_number;
8105
8106     do_branch_i:
8107       macro_build (&imm_expr, s, s3, xreg);
8108       macro_build (&offset_expr, s2, "p");
8109       break;
8110
8111     case M_ABS:
8112       expr1.X_add_number = 0;
8113       macro_build (&expr1, "slti", "x,8", yreg);
8114       if (xreg != yreg)
8115         move_register (xreg, yreg);
8116       expr1.X_add_number = 2;
8117       macro_build (&expr1, "bteqz", "p");
8118       macro_build (NULL, "neg", "x,w", xreg, xreg);
8119     }
8120 }
8121
8122 /* For consistency checking, verify that all bits are specified either
8123    by the match/mask part of the instruction definition, or by the
8124    operand list.  */
8125 static int
8126 validate_mips_insn (const struct mips_opcode *opc)
8127 {
8128   const char *p = opc->args;
8129   char c;
8130   unsigned long used_bits = opc->mask;
8131
8132   if ((used_bits & opc->match) != opc->match)
8133     {
8134       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8135               opc->name, opc->args);
8136       return 0;
8137     }
8138 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8139   while (*p)
8140     switch (c = *p++)
8141       {
8142       case ',': break;
8143       case '(': break;
8144       case ')': break;
8145       case '+':
8146         switch (c = *p++)
8147           {
8148           case '1': USE_BITS (OP_MASK_UDI1,     OP_SH_UDI1);    break;
8149           case '2': USE_BITS (OP_MASK_UDI2,     OP_SH_UDI2);    break;
8150           case '3': USE_BITS (OP_MASK_UDI3,     OP_SH_UDI3);    break;
8151           case '4': USE_BITS (OP_MASK_UDI4,     OP_SH_UDI4);    break;
8152           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8153           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8154           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8155           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8156                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8157           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8158           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8159           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8160           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8161           case 'I': break;
8162           case 't': USE_BITS (OP_MASK_RT,       OP_SH_RT);      break;
8163           case 'T': USE_BITS (OP_MASK_RT,       OP_SH_RT);
8164                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8165           default:
8166             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8167                     c, opc->name, opc->args);
8168             return 0;
8169           }
8170         break;
8171       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8172       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8173       case 'A': break;
8174       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8175       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8176       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8177       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8178       case 'F': break;
8179       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8180       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8181       case 'I': break;
8182       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8183       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8184       case 'L': break;
8185       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8186       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8187       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8188       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8189                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8190       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8191       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8192       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8193       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8194       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8195       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8196       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8197       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8198       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8199       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8200       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8201       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8202       case 'f': break;
8203       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8204       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8205       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8206       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8207       case 'l': break;
8208       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8209       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8210       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8211       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8212       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8213       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8214       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8215       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8216       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8217       case 'x': break;
8218       case 'z': break;
8219       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8220       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8221                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8222       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8223       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8224       case '[': break;
8225       case ']': break;
8226       case '3': USE_BITS (OP_MASK_SA3,          OP_SH_SA3);     break;
8227       case '4': USE_BITS (OP_MASK_SA4,          OP_SH_SA4);     break;
8228       case '5': USE_BITS (OP_MASK_IMM8,         OP_SH_IMM8);    break;
8229       case '6': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8230       case '7': USE_BITS (OP_MASK_DSPACC,       OP_SH_DSPACC);  break;
8231       case '8': USE_BITS (OP_MASK_WRDSP,        OP_SH_WRDSP);   break;
8232       case '9': USE_BITS (OP_MASK_DSPACC_S,     OP_SH_DSPACC_S);break;
8233       case '0': USE_BITS (OP_MASK_DSPSFT,       OP_SH_DSPSFT);  break;
8234       case '\'': USE_BITS (OP_MASK_RDDSP,       OP_SH_RDDSP);   break;
8235       case ':': USE_BITS (OP_MASK_DSPSFT_7,     OP_SH_DSPSFT_7);break;
8236       case '@': USE_BITS (OP_MASK_IMM10,        OP_SH_IMM10);   break;
8237       case '!': USE_BITS (OP_MASK_MT_U,         OP_SH_MT_U);    break;
8238       case '$': USE_BITS (OP_MASK_MT_H,         OP_SH_MT_H);    break;
8239       case '*': USE_BITS (OP_MASK_MTACC_T,      OP_SH_MTACC_T); break;
8240       case '&': USE_BITS (OP_MASK_MTACC_D,      OP_SH_MTACC_D); break;
8241       case 'g': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8242       default:
8243         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8244                 c, opc->name, opc->args);
8245         return 0;
8246       }
8247 #undef USE_BITS
8248   if (used_bits != 0xffffffff)
8249     {
8250       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8251               ~used_bits & 0xffffffff, opc->name, opc->args);
8252       return 0;
8253     }
8254   return 1;
8255 }
8256
8257 /* UDI immediates.  */
8258 struct mips_immed {
8259   char          type;
8260   unsigned int  shift;
8261   unsigned long mask;
8262   const char *  desc;
8263 };
8264
8265 static const struct mips_immed mips_immed[] = {
8266   { '1',        OP_SH_UDI1,     OP_MASK_UDI1,           0},
8267   { '2',        OP_SH_UDI2,     OP_MASK_UDI2,           0},
8268   { '3',        OP_SH_UDI3,     OP_MASK_UDI3,           0},
8269   { '4',        OP_SH_UDI4,     OP_MASK_UDI4,           0},
8270   { 0,0,0,0 }
8271 };
8272
8273 /* Check whether an odd floating-point register is allowed.  */
8274 static int
8275 mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8276 {
8277   const char *s = insn->name;
8278
8279   if (insn->pinfo == INSN_MACRO)
8280     /* Let a macro pass, we'll catch it later when it is expanded.  */
8281     return 1;
8282
8283   if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8284     {
8285       /* Allow odd registers for single-precision ops.  */
8286       switch (insn->pinfo & (FP_S | FP_D))
8287         {
8288         case FP_S:
8289         case 0:
8290           return 1;     /* both single precision - ok */
8291         case FP_D:
8292           return 0;     /* both double precision - fail */
8293         default:
8294           break;
8295         }
8296
8297       /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand.  */
8298       s = strchr (insn->name, '.');
8299       if (argnum == 2)
8300         s = s != NULL ? strchr (s + 1, '.') : NULL;
8301       return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8302     } 
8303
8304   /* Single-precision coprocessor loads and moves are OK too.  */
8305   if ((insn->pinfo & FP_S)
8306       && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8307                          | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8308     return 1;
8309
8310   return 0;
8311 }
8312
8313 /* This routine assembles an instruction into its binary format.  As a
8314    side effect, it sets one of the global variables imm_reloc or
8315    offset_reloc to the type of relocation to do if one of the operands
8316    is an address expression.  */
8317
8318 static void
8319 mips_ip (char *str, struct mips_cl_insn *ip)
8320 {
8321   char *s;
8322   const char *args;
8323   char c = 0;
8324   struct mips_opcode *insn;
8325   char *argsStart;
8326   unsigned int regno;
8327   unsigned int lastregno = 0;
8328   unsigned int lastpos = 0;
8329   unsigned int limlo, limhi;
8330   char *s_reset;
8331   char save_c = 0;
8332   offsetT min_range, max_range;
8333   int argnum;
8334   unsigned int rtype;
8335
8336   insn_error = NULL;
8337
8338   /* If the instruction contains a '.', we first try to match an instruction
8339      including the '.'.  Then we try again without the '.'.  */
8340   insn = NULL;
8341   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8342     continue;
8343
8344   /* If we stopped on whitespace, then replace the whitespace with null for
8345      the call to hash_find.  Save the character we replaced just in case we
8346      have to re-parse the instruction.  */
8347   if (ISSPACE (*s))
8348     {
8349       save_c = *s;
8350       *s++ = '\0';
8351     }
8352
8353   insn = (struct mips_opcode *) hash_find (op_hash, str);
8354
8355   /* If we didn't find the instruction in the opcode table, try again, but
8356      this time with just the instruction up to, but not including the
8357      first '.'.  */
8358   if (insn == NULL)
8359     {
8360       /* Restore the character we overwrite above (if any).  */
8361       if (save_c)
8362         *(--s) = save_c;
8363
8364       /* Scan up to the first '.' or whitespace.  */
8365       for (s = str;
8366            *s != '\0' && *s != '.' && !ISSPACE (*s);
8367            ++s)
8368         continue;
8369
8370       /* If we did not find a '.', then we can quit now.  */
8371       if (*s != '.')
8372         {
8373           insn_error = "unrecognized opcode";
8374           return;
8375         }
8376
8377       /* Lookup the instruction in the hash table.  */
8378       *s++ = '\0';
8379       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8380         {
8381           insn_error = "unrecognized opcode";
8382           return;
8383         }
8384     }
8385
8386   argsStart = s;
8387   for (;;)
8388     {
8389       bfd_boolean ok;
8390
8391       assert (strcmp (insn->name, str) == 0);
8392
8393       if (OPCODE_IS_MEMBER (insn,
8394                             (mips_opts.isa
8395                              /* We don't check for mips_opts.mips16 here since
8396                                 we want to allow jalx if -mips16 was specified
8397                                 on the command line.  */
8398                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8399                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8400                              | (mips_opts.ase_dsp ? INSN_DSP : 0)
8401                              | ((mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
8402                                 ? INSN_DSP64 : 0)
8403                              | (mips_opts.ase_mt ? INSN_MT : 0)
8404                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)
8405                              | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
8406                             mips_opts.arch))
8407         ok = TRUE;
8408       else
8409         ok = FALSE;
8410
8411       if (insn->pinfo != INSN_MACRO)
8412         {
8413           if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8414             ok = FALSE;
8415         }
8416
8417       if (! ok)
8418         {
8419           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8420               && strcmp (insn->name, insn[1].name) == 0)
8421             {
8422               ++insn;
8423               continue;
8424             }
8425           else
8426             {
8427               if (!insn_error)
8428                 {
8429                   static char buf[100];
8430                   sprintf (buf,
8431                            _("opcode not supported on this processor: %s (%s)"),
8432                            mips_cpu_info_from_arch (mips_opts.arch)->name,
8433                            mips_cpu_info_from_isa (mips_opts.isa)->name);
8434                   insn_error = buf;
8435                 }
8436               if (save_c)
8437                 *(--s) = save_c;
8438               return;
8439             }
8440         }
8441
8442       create_insn (ip, insn);
8443       insn_error = NULL;
8444       argnum = 1;
8445       for (args = insn->args;; ++args)
8446         {
8447           int is_mdmx;
8448
8449           s += strspn (s, " \t");
8450           is_mdmx = 0;
8451           switch (*args)
8452             {
8453             case '\0':          /* end of args */
8454               if (*s == '\0')
8455                 return;
8456               break;
8457
8458             case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8459               my_getExpression (&imm_expr, s);
8460               check_absolute_expr (ip, &imm_expr);
8461               if (imm_expr.X_add_number & ~OP_MASK_SA3)
8462                 {
8463                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8464                           OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8465                 }
8466               INSERT_OPERAND (SA3, *ip, imm_expr.X_add_number);
8467               imm_expr.X_op = O_absent;
8468               s = expr_end;
8469               continue;
8470
8471             case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8472               my_getExpression (&imm_expr, s);
8473               check_absolute_expr (ip, &imm_expr);
8474               if (imm_expr.X_add_number & ~OP_MASK_SA4)
8475                 {
8476                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8477                           OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8478                 }
8479               INSERT_OPERAND (SA4, *ip, imm_expr.X_add_number);
8480               imm_expr.X_op = O_absent;
8481               s = expr_end;
8482               continue;
8483
8484             case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8485               my_getExpression (&imm_expr, s);
8486               check_absolute_expr (ip, &imm_expr);
8487               if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8488                 {
8489                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8490                           OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8491                 }
8492               INSERT_OPERAND (IMM8, *ip, imm_expr.X_add_number);
8493               imm_expr.X_op = O_absent;
8494               s = expr_end;
8495               continue;
8496
8497             case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8498               my_getExpression (&imm_expr, s);
8499               check_absolute_expr (ip, &imm_expr);
8500               if (imm_expr.X_add_number & ~OP_MASK_RS)
8501                 {
8502                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8503                           OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8504                 }
8505               INSERT_OPERAND (RS, *ip, imm_expr.X_add_number);
8506               imm_expr.X_op = O_absent;
8507               s = expr_end;
8508               continue;
8509
8510             case '7': /* four dsp accumulators in bits 11,12 */ 
8511               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8512                   s[3] >= '0' && s[3] <= '3')
8513                 {
8514                   regno = s[3] - '0';
8515                   s += 4;
8516                   INSERT_OPERAND (DSPACC, *ip, regno);
8517                   continue;
8518                 }
8519               else
8520                 as_bad (_("Invalid dsp acc register"));
8521               break;
8522
8523             case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8524               my_getExpression (&imm_expr, s);
8525               check_absolute_expr (ip, &imm_expr);
8526               if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8527                 {
8528                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8529                           OP_MASK_WRDSP,
8530                           (unsigned long) imm_expr.X_add_number);
8531                 }
8532               INSERT_OPERAND (WRDSP, *ip, imm_expr.X_add_number);
8533               imm_expr.X_op = O_absent;
8534               s = expr_end;
8535               continue;
8536
8537             case '9': /* four dsp accumulators in bits 21,22 */
8538               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8539                   s[3] >= '0' && s[3] <= '3')
8540                 {
8541                   regno = s[3] - '0';
8542                   s += 4;
8543                   INSERT_OPERAND (DSPACC_S, *ip, regno);
8544                   continue;
8545                 }
8546               else
8547                 as_bad (_("Invalid dsp acc register"));
8548               break;
8549
8550             case '0': /* dsp 6-bit signed immediate in bit 20 */
8551               my_getExpression (&imm_expr, s);
8552               check_absolute_expr (ip, &imm_expr);
8553               min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8554               max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8555               if (imm_expr.X_add_number < min_range ||
8556                   imm_expr.X_add_number > max_range)
8557                 {
8558                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8559                           (long) min_range, (long) max_range,
8560                           (long) imm_expr.X_add_number);
8561                 }
8562               INSERT_OPERAND (DSPSFT, *ip, imm_expr.X_add_number);
8563               imm_expr.X_op = O_absent;
8564               s = expr_end;
8565               continue;
8566
8567             case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8568               my_getExpression (&imm_expr, s);
8569               check_absolute_expr (ip, &imm_expr);
8570               if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8571                 {
8572                   as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8573                           OP_MASK_RDDSP,
8574                           (unsigned long) imm_expr.X_add_number);
8575                 }
8576               INSERT_OPERAND (RDDSP, *ip, imm_expr.X_add_number);
8577               imm_expr.X_op = O_absent;
8578               s = expr_end;
8579               continue;
8580
8581             case ':': /* dsp 7-bit signed immediate in bit 19 */
8582               my_getExpression (&imm_expr, s);
8583               check_absolute_expr (ip, &imm_expr);
8584               min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8585               max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8586               if (imm_expr.X_add_number < min_range ||
8587                   imm_expr.X_add_number > max_range)
8588                 {
8589                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8590                           (long) min_range, (long) max_range,
8591                           (long) imm_expr.X_add_number);
8592                 }
8593               INSERT_OPERAND (DSPSFT_7, *ip, imm_expr.X_add_number);
8594               imm_expr.X_op = O_absent;
8595               s = expr_end;
8596               continue;
8597
8598             case '@': /* dsp 10-bit signed immediate in bit 16 */
8599               my_getExpression (&imm_expr, s);
8600               check_absolute_expr (ip, &imm_expr);
8601               min_range = -((OP_MASK_IMM10 + 1) >> 1);
8602               max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8603               if (imm_expr.X_add_number < min_range ||
8604                   imm_expr.X_add_number > max_range)
8605                 {
8606                   as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8607                           (long) min_range, (long) max_range,
8608                           (long) imm_expr.X_add_number);
8609                 }
8610               INSERT_OPERAND (IMM10, *ip, imm_expr.X_add_number);
8611               imm_expr.X_op = O_absent;
8612               s = expr_end;
8613               continue;
8614
8615             case '!': /* MT usermode flag bit.  */
8616               my_getExpression (&imm_expr, s);
8617               check_absolute_expr (ip, &imm_expr);
8618               if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8619                 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
8620                         (unsigned long) imm_expr.X_add_number);
8621               INSERT_OPERAND (MT_U, *ip, imm_expr.X_add_number);
8622               imm_expr.X_op = O_absent;
8623               s = expr_end;
8624               continue;
8625
8626             case '$': /* MT load high flag bit.  */
8627               my_getExpression (&imm_expr, s);
8628               check_absolute_expr (ip, &imm_expr);
8629               if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8630                 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
8631                         (unsigned long) imm_expr.X_add_number);
8632               INSERT_OPERAND (MT_H, *ip, imm_expr.X_add_number);
8633               imm_expr.X_op = O_absent;
8634               s = expr_end;
8635               continue;
8636
8637             case '*': /* four dsp accumulators in bits 18,19 */ 
8638               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8639                   s[3] >= '0' && s[3] <= '3')
8640                 {
8641                   regno = s[3] - '0';
8642                   s += 4;
8643                   INSERT_OPERAND (MTACC_T, *ip, regno);
8644                   continue;
8645                 }
8646               else
8647                 as_bad (_("Invalid dsp/smartmips acc register"));
8648               break;
8649
8650             case '&': /* four dsp accumulators in bits 13,14 */ 
8651               if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8652                   s[3] >= '0' && s[3] <= '3')
8653                 {
8654                   regno = s[3] - '0';
8655                   s += 4;
8656                   INSERT_OPERAND (MTACC_D, *ip, regno);
8657                   continue;
8658                 }
8659               else
8660                 as_bad (_("Invalid dsp/smartmips acc register"));
8661               break;
8662
8663             case ',':
8664               ++argnum;
8665               if (*s++ == *args)
8666                 continue;
8667               s--;
8668               switch (*++args)
8669                 {
8670                 case 'r':
8671                 case 'v':
8672                   INSERT_OPERAND (RS, *ip, lastregno);
8673                   continue;
8674
8675                 case 'w':
8676                   INSERT_OPERAND (RT, *ip, lastregno);
8677                   continue;
8678
8679                 case 'W':
8680                   INSERT_OPERAND (FT, *ip, lastregno);
8681                   continue;
8682
8683                 case 'V':
8684                   INSERT_OPERAND (FS, *ip, lastregno);
8685                   continue;
8686                 }
8687               break;
8688
8689             case '(':
8690               /* Handle optional base register.
8691                  Either the base register is omitted or
8692                  we must have a left paren.  */
8693               /* This is dependent on the next operand specifier
8694                  is a base register specification.  */
8695               assert (args[1] == 'b' || args[1] == '5'
8696                       || args[1] == '-' || args[1] == '4');
8697               if (*s == '\0')
8698                 return;
8699
8700             case ')':           /* these must match exactly */
8701             case '[':
8702             case ']':
8703               if (*s++ == *args)
8704                 continue;
8705               break;
8706
8707             case '+':           /* Opcode extension character.  */
8708               switch (*++args)
8709                 {
8710                 case '1':       /* UDI immediates.  */
8711                 case '2':
8712                 case '3':
8713                 case '4':
8714                   {
8715                     const struct mips_immed *imm = mips_immed;
8716
8717                     while (imm->type && imm->type != *args)
8718                       ++imm;
8719                     if (! imm->type)
8720                       internalError ();
8721                     my_getExpression (&imm_expr, s);
8722                     check_absolute_expr (ip, &imm_expr);
8723                     if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8724                       {
8725                         as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8726                                  imm->desc ? imm->desc : ip->insn_mo->name,
8727                                  (unsigned long) imm_expr.X_add_number,
8728                                  (unsigned long) imm_expr.X_add_number);
8729                               imm_expr.X_add_number &= imm->mask;
8730                       }
8731                     ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8732                                         << imm->shift);
8733                     imm_expr.X_op = O_absent;
8734                     s = expr_end;
8735                   }
8736                   continue;
8737                   
8738                 case 'A':               /* ins/ext position, becomes LSB.  */
8739                   limlo = 0;
8740                   limhi = 31;
8741                   goto do_lsb;
8742                 case 'E':
8743                   limlo = 32;
8744                   limhi = 63;
8745                   goto do_lsb;
8746 do_lsb:
8747                   my_getExpression (&imm_expr, s);
8748                   check_absolute_expr (ip, &imm_expr);
8749                   if ((unsigned long) imm_expr.X_add_number < limlo
8750                       || (unsigned long) imm_expr.X_add_number > limhi)
8751                     {
8752                       as_bad (_("Improper position (%lu)"),
8753                               (unsigned long) imm_expr.X_add_number);
8754                       imm_expr.X_add_number = limlo;
8755                     }
8756                   lastpos = imm_expr.X_add_number;
8757                   INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8758                   imm_expr.X_op = O_absent;
8759                   s = expr_end;
8760                   continue;
8761
8762                 case 'B':               /* ins size, becomes MSB.  */
8763                   limlo = 1;
8764                   limhi = 32;
8765                   goto do_msb;
8766                 case 'F':
8767                   limlo = 33;
8768                   limhi = 64;
8769                   goto do_msb;
8770 do_msb:
8771                   my_getExpression (&imm_expr, s);
8772                   check_absolute_expr (ip, &imm_expr);
8773                   /* Check for negative input so that small negative numbers
8774                      will not succeed incorrectly.  The checks against
8775                      (pos+size) transitively check "size" itself,
8776                      assuming that "pos" is reasonable.  */
8777                   if ((long) imm_expr.X_add_number < 0
8778                       || ((unsigned long) imm_expr.X_add_number
8779                           + lastpos) < limlo
8780                       || ((unsigned long) imm_expr.X_add_number
8781                           + lastpos) > limhi)
8782                     {
8783                       as_bad (_("Improper insert size (%lu, position %lu)"),
8784                               (unsigned long) imm_expr.X_add_number,
8785                               (unsigned long) lastpos);
8786                       imm_expr.X_add_number = limlo - lastpos;
8787                     }
8788                   INSERT_OPERAND (INSMSB, *ip,
8789                                  lastpos + imm_expr.X_add_number - 1);
8790                   imm_expr.X_op = O_absent;
8791                   s = expr_end;
8792                   continue;
8793
8794                 case 'C':               /* ext size, becomes MSBD.  */
8795                   limlo = 1;
8796                   limhi = 32;
8797                   goto do_msbd;
8798                 case 'G':
8799                   limlo = 33;
8800                   limhi = 64;
8801                   goto do_msbd;
8802                 case 'H':
8803                   limlo = 33;
8804                   limhi = 64;
8805                   goto do_msbd;
8806 do_msbd:
8807                   my_getExpression (&imm_expr, s);
8808                   check_absolute_expr (ip, &imm_expr);
8809                   /* Check for negative input so that small negative numbers
8810                      will not succeed incorrectly.  The checks against
8811                      (pos+size) transitively check "size" itself,
8812                      assuming that "pos" is reasonable.  */
8813                   if ((long) imm_expr.X_add_number < 0
8814                       || ((unsigned long) imm_expr.X_add_number
8815                           + lastpos) < limlo
8816                       || ((unsigned long) imm_expr.X_add_number
8817                           + lastpos) > limhi)
8818                     {
8819                       as_bad (_("Improper extract size (%lu, position %lu)"),
8820                               (unsigned long) imm_expr.X_add_number,
8821                               (unsigned long) lastpos);
8822                       imm_expr.X_add_number = limlo - lastpos;
8823                     }
8824                   INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8825                   imm_expr.X_op = O_absent;
8826                   s = expr_end;
8827                   continue;
8828
8829                 case 'D':
8830                   /* +D is for disassembly only; never match.  */
8831                   break;
8832
8833                 case 'I':
8834                   /* "+I" is like "I", except that imm2_expr is used.  */
8835                   my_getExpression (&imm2_expr, s);
8836                   if (imm2_expr.X_op != O_big
8837                       && imm2_expr.X_op != O_constant)
8838                   insn_error = _("absolute expression required");
8839                   if (HAVE_32BIT_GPRS)
8840                     normalize_constant_expr (&imm2_expr);
8841                   s = expr_end;
8842                   continue;
8843
8844                 case 'T': /* Coprocessor register.  */
8845                   /* +T is for disassembly only; never match.  */
8846                   break;
8847
8848                 case 't': /* Coprocessor register number.  */
8849                   if (s[0] == '$' && ISDIGIT (s[1]))
8850                     {
8851                       ++s;
8852                       regno = 0;
8853                       do
8854                         {
8855                           regno *= 10;
8856                           regno += *s - '0';
8857                           ++s;
8858                         }
8859                       while (ISDIGIT (*s));
8860                       if (regno > 31)
8861                         as_bad (_("Invalid register number (%d)"), regno);
8862                       else
8863                         {
8864                           INSERT_OPERAND (RT, *ip, regno);
8865                           continue;
8866                         }
8867                     }
8868                   else
8869                     as_bad (_("Invalid coprocessor 0 register number"));
8870                   break;
8871
8872                 default:
8873                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8874                     *args, insn->name, insn->args);
8875                   /* Further processing is fruitless.  */
8876                   return;
8877                 }
8878               break;
8879
8880             case '<':           /* must be at least one digit */
8881               /*
8882                * According to the manual, if the shift amount is greater
8883                * than 31 or less than 0, then the shift amount should be
8884                * mod 32.  In reality the mips assembler issues an error.
8885                * We issue a warning and mask out all but the low 5 bits.
8886                */
8887               my_getExpression (&imm_expr, s);
8888               check_absolute_expr (ip, &imm_expr);
8889               if ((unsigned long) imm_expr.X_add_number > 31)
8890                 as_warn (_("Improper shift amount (%lu)"),
8891                          (unsigned long) imm_expr.X_add_number);
8892               INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8893               imm_expr.X_op = O_absent;
8894               s = expr_end;
8895               continue;
8896
8897             case '>':           /* shift amount minus 32 */
8898               my_getExpression (&imm_expr, s);
8899               check_absolute_expr (ip, &imm_expr);
8900               if ((unsigned long) imm_expr.X_add_number < 32
8901                   || (unsigned long) imm_expr.X_add_number > 63)
8902                 break;
8903               INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8904               imm_expr.X_op = O_absent;
8905               s = expr_end;
8906               continue;
8907
8908             case 'k':           /* cache code */
8909             case 'h':           /* prefx code */
8910               my_getExpression (&imm_expr, s);
8911               check_absolute_expr (ip, &imm_expr);
8912               if ((unsigned long) imm_expr.X_add_number > 31)
8913                 as_warn (_("Invalid value for `%s' (%lu)"),
8914                          ip->insn_mo->name,
8915                          (unsigned long) imm_expr.X_add_number);
8916               if (*args == 'k')
8917                 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8918               else
8919                 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8920               imm_expr.X_op = O_absent;
8921               s = expr_end;
8922               continue;
8923
8924             case 'c':           /* break code */
8925               my_getExpression (&imm_expr, s);
8926               check_absolute_expr (ip, &imm_expr);
8927               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE)
8928                 as_warn (_("Code for %s not in range 0..1023 (%lu)"),
8929                          ip->insn_mo->name,
8930                          (unsigned long) imm_expr.X_add_number);
8931               INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8932               imm_expr.X_op = O_absent;
8933               s = expr_end;
8934               continue;
8935
8936             case 'q':           /* lower break code */
8937               my_getExpression (&imm_expr, s);
8938               check_absolute_expr (ip, &imm_expr);
8939               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE2)
8940                 as_warn (_("Lower code for %s not in range 0..1023 (%lu)"),
8941                          ip->insn_mo->name,
8942                          (unsigned long) imm_expr.X_add_number);
8943               INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8944               imm_expr.X_op = O_absent;
8945               s = expr_end;
8946               continue;
8947
8948             case 'B':           /* 20-bit syscall/break code.  */
8949               my_getExpression (&imm_expr, s);
8950               check_absolute_expr (ip, &imm_expr);
8951               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8952                 as_warn (_("Code for %s not in range 0..1048575 (%lu)"),
8953                          ip->insn_mo->name,
8954                          (unsigned long) imm_expr.X_add_number);
8955               INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8956               imm_expr.X_op = O_absent;
8957               s = expr_end;
8958               continue;
8959
8960             case 'C':           /* Coprocessor code */
8961               my_getExpression (&imm_expr, s);
8962               check_absolute_expr (ip, &imm_expr);
8963               if ((unsigned long) imm_expr.X_add_number > OP_MASK_COPZ)
8964                 {
8965                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8966                            (unsigned long) imm_expr.X_add_number);
8967                   imm_expr.X_add_number &= OP_MASK_COPZ;
8968                 }
8969               INSERT_OPERAND (COPZ, *ip, imm_expr.X_add_number);
8970               imm_expr.X_op = O_absent;
8971               s = expr_end;
8972               continue;
8973
8974             case 'J':           /* 19-bit wait code.  */
8975               my_getExpression (&imm_expr, s);
8976               check_absolute_expr (ip, &imm_expr);
8977               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8978                 {
8979                   as_warn (_("Illegal 19-bit code (%lu)"),
8980                            (unsigned long) imm_expr.X_add_number);
8981                   imm_expr.X_add_number &= OP_MASK_CODE19;
8982                 }
8983               INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8984               imm_expr.X_op = O_absent;
8985               s = expr_end;
8986               continue;
8987
8988             case 'P':           /* Performance register.  */
8989               my_getExpression (&imm_expr, s);
8990               check_absolute_expr (ip, &imm_expr);
8991               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8992                 as_warn (_("Invalid performance register (%lu)"),
8993                          (unsigned long) imm_expr.X_add_number);
8994               INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8995               imm_expr.X_op = O_absent;
8996               s = expr_end;
8997               continue;
8998
8999             case 'G':           /* Coprocessor destination register.  */
9000               if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
9001                 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
9002               else
9003                 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
9004               INSERT_OPERAND (RD, *ip, regno);
9005               if (ok) 
9006                 {
9007                   lastregno = regno;
9008                   continue;
9009                 }
9010               else
9011                 break;
9012
9013             case 'b':           /* base register */
9014             case 'd':           /* destination register */
9015             case 's':           /* source register */
9016             case 't':           /* target register */
9017             case 'r':           /* both target and source */
9018             case 'v':           /* both dest and source */
9019             case 'w':           /* both dest and target */
9020             case 'E':           /* coprocessor target register */
9021             case 'K':           /* 'rdhwr' destination register */
9022             case 'x':           /* ignore register name */
9023             case 'z':           /* must be zero register */
9024             case 'U':           /* destination register (clo/clz).  */
9025             case 'g':           /* coprocessor destination register */
9026               s_reset = s;            
9027               if (*args == 'E' || *args == 'K')
9028                 ok = reg_lookup (&s, RTYPE_NUM, &regno);
9029               else
9030                 {
9031                   ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
9032                   if (regno == AT && ! mips_opts.noat)
9033                     as_warn ("Used $at without \".set noat\"");
9034                 }
9035               if (ok)
9036                 {
9037                   c = *args;
9038                   if (*s == ' ')
9039                     ++s;
9040                   if (args[1] != *s)
9041                     {
9042                       if (c == 'r' || c == 'v' || c == 'w')
9043                         {
9044                           regno = lastregno;
9045                           s = s_reset;
9046                           ++args;
9047                         }
9048                     }
9049                   /* 'z' only matches $0.  */
9050                   if (c == 'z' && regno != 0)
9051                     break;
9052
9053         /* Now that we have assembled one operand, we use the args string
9054          * to figure out where it goes in the instruction.  */
9055                   switch (c)
9056                     {
9057                     case 'r':
9058                     case 's':
9059                     case 'v':
9060                     case 'b':
9061                       INSERT_OPERAND (RS, *ip, regno);
9062                       break;
9063                     case 'd':
9064                     case 'G':
9065                     case 'K':
9066                     case 'g':
9067                       INSERT_OPERAND (RD, *ip, regno);
9068                       break;
9069                     case 'U':
9070                       INSERT_OPERAND (RD, *ip, regno);
9071                       INSERT_OPERAND (RT, *ip, regno);
9072                       break;
9073                     case 'w':
9074                     case 't':
9075                     case 'E':
9076                       INSERT_OPERAND (RT, *ip, regno);
9077                       break;
9078                     case 'x':
9079                       /* This case exists because on the r3000 trunc
9080                          expands into a macro which requires a gp
9081                          register.  On the r6000 or r4000 it is
9082                          assembled into a single instruction which
9083                          ignores the register.  Thus the insn version
9084                          is MIPS_ISA2 and uses 'x', and the macro
9085                          version is MIPS_ISA1 and uses 't'.  */
9086                       break;
9087                     case 'z':
9088                       /* This case is for the div instruction, which
9089                          acts differently if the destination argument
9090                          is $0.  This only matches $0, and is checked
9091                          outside the switch.  */
9092                       break;
9093                     case 'D':
9094                       /* Itbl operand; not yet implemented. FIXME ?? */
9095                       break;
9096                       /* What about all other operands like 'i', which
9097                          can be specified in the opcode table? */
9098                     }
9099                   lastregno = regno;
9100                   continue;
9101                 }
9102               switch (*args++)
9103                 {
9104                 case 'r':
9105                 case 'v':
9106                   INSERT_OPERAND (RS, *ip, lastregno);
9107                   continue;
9108                 case 'w':
9109                   INSERT_OPERAND (RT, *ip, lastregno);
9110                   continue;
9111                 }
9112               break;
9113
9114             case 'O':           /* MDMX alignment immediate constant.  */
9115               my_getExpression (&imm_expr, s);
9116               check_absolute_expr (ip, &imm_expr);
9117               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9118                 as_warn ("Improper align amount (%ld), using low bits",
9119                          (long) imm_expr.X_add_number);
9120               INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
9121               imm_expr.X_op = O_absent;
9122               s = expr_end;
9123               continue;
9124
9125             case 'Q':           /* MDMX vector, element sel, or const.  */
9126               if (s[0] != '$')
9127                 {
9128                   /* MDMX Immediate.  */
9129                   my_getExpression (&imm_expr, s);
9130                   check_absolute_expr (ip, &imm_expr);
9131                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9132                     as_warn (_("Invalid MDMX Immediate (%ld)"),
9133                              (long) imm_expr.X_add_number);
9134                   INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
9135                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9136                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9137                   else
9138                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9139                   imm_expr.X_op = O_absent;
9140                   s = expr_end;
9141                   continue;
9142                 }
9143               /* Not MDMX Immediate.  Fall through.  */
9144             case 'X':           /* MDMX destination register.  */
9145             case 'Y':           /* MDMX source register.  */
9146             case 'Z':           /* MDMX target register.  */
9147               is_mdmx = 1;
9148             case 'D':           /* floating point destination register */
9149             case 'S':           /* floating point source register */
9150             case 'T':           /* floating point target register */
9151             case 'R':           /* floating point source register */
9152             case 'V':
9153             case 'W':
9154               rtype = RTYPE_FPU;
9155               if (is_mdmx
9156                   || (mips_opts.ase_mdmx
9157                       && (ip->insn_mo->pinfo & FP_D)
9158                       && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9159                                                 | INSN_COPROC_MEMORY_DELAY
9160                                                 | INSN_LOAD_COPROC_DELAY
9161                                                 | INSN_LOAD_MEMORY_DELAY
9162                                                 | INSN_STORE_MEMORY))))
9163                 rtype |= RTYPE_VEC;
9164               s_reset = s;
9165               if (reg_lookup (&s, rtype, &regno))
9166                 {
9167                   if ((regno & 1) != 0
9168                       && HAVE_32BIT_FPRS
9169                       && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
9170                     as_warn (_("Float register should be even, was %d"),
9171                              regno);
9172
9173                   c = *args;
9174                   if (*s == ' ')
9175                     ++s;
9176                   if (args[1] != *s)
9177                     {
9178                       if (c == 'V' || c == 'W')
9179                         {
9180                           regno = lastregno;
9181                           s = s_reset;
9182                           ++args;
9183                         }
9184                     }
9185                   switch (c)
9186                     {
9187                     case 'D':
9188                     case 'X':
9189                       INSERT_OPERAND (FD, *ip, regno);
9190                       break;
9191                     case 'V':
9192                     case 'S':
9193                     case 'Y':
9194                       INSERT_OPERAND (FS, *ip, regno);
9195                       break;
9196                     case 'Q':
9197                       /* This is like 'Z', but also needs to fix the MDMX
9198                          vector/scalar select bits.  Note that the
9199                          scalar immediate case is handled above.  */
9200                       if (*s == '[')
9201                         {
9202                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9203                           int max_el = (is_qh ? 3 : 7);
9204                           s++;
9205                           my_getExpression(&imm_expr, s);
9206                           check_absolute_expr (ip, &imm_expr);
9207                           s = expr_end;
9208                           if (imm_expr.X_add_number > max_el)
9209                             as_bad(_("Bad element selector %ld"),
9210                                    (long) imm_expr.X_add_number);
9211                           imm_expr.X_add_number &= max_el;
9212                           ip->insn_opcode |= (imm_expr.X_add_number
9213                                               << (OP_SH_VSEL +
9214                                                   (is_qh ? 2 : 1)));
9215                           imm_expr.X_op = O_absent;
9216                           if (*s != ']')
9217                             as_warn(_("Expecting ']' found '%s'"), s);
9218                           else
9219                             s++;
9220                         }
9221                       else
9222                         {
9223                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9224                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9225                                                 << OP_SH_VSEL);
9226                           else
9227                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9228                                                 OP_SH_VSEL);
9229                         }
9230                       /* Fall through */
9231                     case 'W':
9232                     case 'T':
9233                     case 'Z':
9234                       INSERT_OPERAND (FT, *ip, regno);
9235                       break;
9236                     case 'R':
9237                       INSERT_OPERAND (FR, *ip, regno);
9238                       break;
9239                     }
9240                   lastregno = regno;
9241                   continue;
9242                 }
9243
9244               switch (*args++)
9245                 {
9246                 case 'V':
9247                   INSERT_OPERAND (FS, *ip, lastregno);
9248                   continue;
9249                 case 'W':
9250                   INSERT_OPERAND (FT, *ip, lastregno);
9251                   continue;
9252                 }
9253               break;
9254
9255             case 'I':
9256               my_getExpression (&imm_expr, s);
9257               if (imm_expr.X_op != O_big
9258                   && imm_expr.X_op != O_constant)
9259                 insn_error = _("absolute expression required");
9260               if (HAVE_32BIT_GPRS)
9261                 normalize_constant_expr (&imm_expr);
9262               s = expr_end;
9263               continue;
9264
9265             case 'A':
9266               my_getExpression (&offset_expr, s);
9267               normalize_address_expr (&offset_expr);
9268               *imm_reloc = BFD_RELOC_32;
9269               s = expr_end;
9270               continue;
9271
9272             case 'F':
9273             case 'L':
9274             case 'f':
9275             case 'l':
9276               {
9277                 int f64;
9278                 int using_gprs;
9279                 char *save_in;
9280                 char *err;
9281                 unsigned char temp[8];
9282                 int len;
9283                 unsigned int length;
9284                 segT seg;
9285                 subsegT subseg;
9286                 char *p;
9287
9288                 /* These only appear as the last operand in an
9289                    instruction, and every instruction that accepts
9290                    them in any variant accepts them in all variants.
9291                    This means we don't have to worry about backing out
9292                    any changes if the instruction does not match.
9293
9294                    The difference between them is the size of the
9295                    floating point constant and where it goes.  For 'F'
9296                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9297                    is 32 bits.  Where the constant is placed is based
9298                    on how the MIPS assembler does things:
9299                     F -- .rdata
9300                     L -- .lit8
9301                     f -- immediate value
9302                     l -- .lit4
9303
9304                     The .lit4 and .lit8 sections are only used if
9305                     permitted by the -G argument.
9306
9307                     The code below needs to know whether the target register
9308                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9309                     'F' are used with GPR-based instructions and 'l' and
9310                     'L' are used with FPR-based instructions.  */
9311
9312                 f64 = *args == 'F' || *args == 'L';
9313                 using_gprs = *args == 'F' || *args == 'f';
9314
9315                 save_in = input_line_pointer;
9316                 input_line_pointer = s;
9317                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9318                 length = len;
9319                 s = input_line_pointer;
9320                 input_line_pointer = save_in;
9321                 if (err != NULL && *err != '\0')
9322                   {
9323                     as_bad (_("Bad floating point constant: %s"), err);
9324                     memset (temp, '\0', sizeof temp);
9325                     length = f64 ? 8 : 4;
9326                   }
9327
9328                 assert (length == (unsigned) (f64 ? 8 : 4));
9329
9330                 if (*args == 'f'
9331                     || (*args == 'l'
9332                         && (g_switch_value < 4
9333                             || (temp[0] == 0 && temp[1] == 0)
9334                             || (temp[2] == 0 && temp[3] == 0))))
9335                   {
9336                     imm_expr.X_op = O_constant;
9337                     if (! target_big_endian)
9338                       imm_expr.X_add_number = bfd_getl32 (temp);
9339                     else
9340                       imm_expr.X_add_number = bfd_getb32 (temp);
9341                   }
9342                 else if (length > 4
9343                          && ! mips_disable_float_construction
9344                          /* Constants can only be constructed in GPRs and
9345                             copied to FPRs if the GPRs are at least as wide
9346                             as the FPRs.  Force the constant into memory if
9347                             we are using 64-bit FPRs but the GPRs are only
9348                             32 bits wide.  */
9349                          && (using_gprs
9350                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9351                          && ((temp[0] == 0 && temp[1] == 0)
9352                              || (temp[2] == 0 && temp[3] == 0))
9353                          && ((temp[4] == 0 && temp[5] == 0)
9354                              || (temp[6] == 0 && temp[7] == 0)))
9355                   {
9356                     /* The value is simple enough to load with a couple of
9357                        instructions.  If using 32-bit registers, set
9358                        imm_expr to the high order 32 bits and offset_expr to
9359                        the low order 32 bits.  Otherwise, set imm_expr to
9360                        the entire 64 bit constant.  */
9361                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9362                       {
9363                         imm_expr.X_op = O_constant;
9364                         offset_expr.X_op = O_constant;
9365                         if (! target_big_endian)
9366                           {
9367                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9368                             offset_expr.X_add_number = bfd_getl32 (temp);
9369                           }
9370                         else
9371                           {
9372                             imm_expr.X_add_number = bfd_getb32 (temp);
9373                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9374                           }
9375                         if (offset_expr.X_add_number == 0)
9376                           offset_expr.X_op = O_absent;
9377                       }
9378                     else if (sizeof (imm_expr.X_add_number) > 4)
9379                       {
9380                         imm_expr.X_op = O_constant;
9381                         if (! target_big_endian)
9382                           imm_expr.X_add_number = bfd_getl64 (temp);
9383                         else
9384                           imm_expr.X_add_number = bfd_getb64 (temp);
9385                       }
9386                     else
9387                       {
9388                         imm_expr.X_op = O_big;
9389                         imm_expr.X_add_number = 4;
9390                         if (! target_big_endian)
9391                           {
9392                             generic_bignum[0] = bfd_getl16 (temp);
9393                             generic_bignum[1] = bfd_getl16 (temp + 2);
9394                             generic_bignum[2] = bfd_getl16 (temp + 4);
9395                             generic_bignum[3] = bfd_getl16 (temp + 6);
9396                           }
9397                         else
9398                           {
9399                             generic_bignum[0] = bfd_getb16 (temp + 6);
9400                             generic_bignum[1] = bfd_getb16 (temp + 4);
9401                             generic_bignum[2] = bfd_getb16 (temp + 2);
9402                             generic_bignum[3] = bfd_getb16 (temp);
9403                           }
9404                       }
9405                   }
9406                 else
9407                   {
9408                     const char *newname;
9409                     segT new_seg;
9410
9411                     /* Switch to the right section.  */
9412                     seg = now_seg;
9413                     subseg = now_subseg;
9414                     switch (*args)
9415                       {
9416                       default: /* unused default case avoids warnings.  */
9417                       case 'L':
9418                         newname = RDATA_SECTION_NAME;
9419                         if (g_switch_value >= 8)
9420                           newname = ".lit8";
9421                         break;
9422                       case 'F':
9423                         newname = RDATA_SECTION_NAME;
9424                         break;
9425                       case 'l':
9426                         assert (g_switch_value >= 4);
9427                         newname = ".lit4";
9428                         break;
9429                       }
9430                     new_seg = subseg_new (newname, (subsegT) 0);
9431                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9432                       bfd_set_section_flags (stdoutput, new_seg,
9433                                              (SEC_ALLOC
9434                                               | SEC_LOAD
9435                                               | SEC_READONLY
9436                                               | SEC_DATA));
9437                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9438                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9439                         && strcmp (TARGET_OS, "elf") != 0)
9440                       record_alignment (new_seg, 4);
9441                     else
9442                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9443                     if (seg == now_seg)
9444                       as_bad (_("Can't use floating point insn in this section"));
9445
9446                     /* Set the argument to the current address in the
9447                        section.  */
9448                     offset_expr.X_op = O_symbol;
9449                     offset_expr.X_add_symbol =
9450                       symbol_new ("L0\001", now_seg,
9451                                   (valueT) frag_now_fix (), frag_now);
9452                     offset_expr.X_add_number = 0;
9453
9454                     /* Put the floating point number into the section.  */
9455                     p = frag_more ((int) length);
9456                     memcpy (p, temp, length);
9457
9458                     /* Switch back to the original section.  */
9459                     subseg_set (seg, subseg);
9460                   }
9461               }
9462               continue;
9463
9464             case 'i':           /* 16 bit unsigned immediate */
9465             case 'j':           /* 16 bit signed immediate */
9466               *imm_reloc = BFD_RELOC_LO16;
9467               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9468                 {
9469                   int more;
9470                   offsetT minval, maxval;
9471
9472                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9473                           && strcmp (insn->name, insn[1].name) == 0);
9474
9475                   /* If the expression was written as an unsigned number,
9476                      only treat it as signed if there are no more
9477                      alternatives.  */
9478                   if (more
9479                       && *args == 'j'
9480                       && sizeof (imm_expr.X_add_number) <= 4
9481                       && imm_expr.X_op == O_constant
9482                       && imm_expr.X_add_number < 0
9483                       && imm_expr.X_unsigned
9484                       && HAVE_64BIT_GPRS)
9485                     break;
9486
9487                   /* For compatibility with older assemblers, we accept
9488                      0x8000-0xffff as signed 16-bit numbers when only
9489                      signed numbers are allowed.  */
9490                   if (*args == 'i')
9491                     minval = 0, maxval = 0xffff;
9492                   else if (more)
9493                     minval = -0x8000, maxval = 0x7fff;
9494                   else
9495                     minval = -0x8000, maxval = 0xffff;
9496
9497                   if (imm_expr.X_op != O_constant
9498                       || imm_expr.X_add_number < minval
9499                       || imm_expr.X_add_number > maxval)
9500                     {
9501                       if (more)
9502                         break;
9503                       if (imm_expr.X_op == O_constant
9504                           || imm_expr.X_op == O_big)
9505                         as_bad (_("expression out of range"));
9506                     }
9507                 }
9508               s = expr_end;
9509               continue;
9510
9511             case 'o':           /* 16 bit offset */
9512               /* Check whether there is only a single bracketed expression
9513                  left.  If so, it must be the base register and the
9514                  constant must be zero.  */
9515               if (*s == '(' && strchr (s + 1, '(') == 0)
9516                 {
9517                   offset_expr.X_op = O_constant;
9518                   offset_expr.X_add_number = 0;
9519                   continue;
9520                 }
9521
9522               /* If this value won't fit into a 16 bit offset, then go
9523                  find a macro that will generate the 32 bit offset
9524                  code pattern.  */
9525               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9526                   && (offset_expr.X_op != O_constant
9527                       || offset_expr.X_add_number >= 0x8000
9528                       || offset_expr.X_add_number < -0x8000))
9529                 break;
9530
9531               s = expr_end;
9532               continue;
9533
9534             case 'p':           /* pc relative offset */
9535               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9536               my_getExpression (&offset_expr, s);
9537               s = expr_end;
9538               continue;
9539
9540             case 'u':           /* upper 16 bits */
9541               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9542                   && imm_expr.X_op == O_constant
9543                   && (imm_expr.X_add_number < 0
9544                       || imm_expr.X_add_number >= 0x10000))
9545                 as_bad (_("lui expression not in range 0..65535"));
9546               s = expr_end;
9547               continue;
9548
9549             case 'a':           /* 26 bit address */
9550               my_getExpression (&offset_expr, s);
9551               s = expr_end;
9552               *offset_reloc = BFD_RELOC_MIPS_JMP;
9553               continue;
9554
9555             case 'N':           /* 3 bit branch condition code */
9556             case 'M':           /* 3 bit compare condition code */
9557               rtype = RTYPE_CCC;
9558               if (ip->insn_mo->pinfo & (FP_D| FP_S))
9559                 rtype |= RTYPE_FCC;
9560               if (!reg_lookup (&s, rtype, &regno))
9561                 break;
9562               if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9563                    || strcmp(str + strlen(str) - 5, "any2f") == 0
9564                    || strcmp(str + strlen(str) - 5, "any2t") == 0)
9565                   && (regno & 1) != 0)
9566                 as_warn(_("Condition code register should be even for %s, was %d"),
9567                         str, regno);
9568               if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9569                    || strcmp(str + strlen(str) - 5, "any4t") == 0)
9570                   && (regno & 3) != 0)
9571                 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9572                         str, regno);
9573               if (*args == 'N')
9574                 INSERT_OPERAND (BCC, *ip, regno);
9575               else
9576                 INSERT_OPERAND (CCC, *ip, regno);
9577               continue;
9578
9579             case 'H':
9580               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9581                 s += 2;
9582               if (ISDIGIT (*s))
9583                 {
9584                   c = 0;
9585                   do
9586                     {
9587                       c *= 10;
9588                       c += *s - '0';
9589                       ++s;
9590                     }
9591                   while (ISDIGIT (*s));
9592                 }
9593               else
9594                 c = 8; /* Invalid sel value.  */
9595
9596               if (c > 7)
9597                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9598               ip->insn_opcode |= c;
9599               continue;
9600
9601             case 'e':
9602               /* Must be at least one digit.  */
9603               my_getExpression (&imm_expr, s);
9604               check_absolute_expr (ip, &imm_expr);
9605
9606               if ((unsigned long) imm_expr.X_add_number
9607                   > (unsigned long) OP_MASK_VECBYTE)
9608                 {
9609                   as_bad (_("bad byte vector index (%ld)"),
9610                            (long) imm_expr.X_add_number);
9611                   imm_expr.X_add_number = 0;
9612                 }
9613
9614               INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
9615               imm_expr.X_op = O_absent;
9616               s = expr_end;
9617               continue;
9618
9619             case '%':
9620               my_getExpression (&imm_expr, s);
9621               check_absolute_expr (ip, &imm_expr);
9622
9623               if ((unsigned long) imm_expr.X_add_number
9624                   > (unsigned long) OP_MASK_VECALIGN)
9625                 {
9626                   as_bad (_("bad byte vector index (%ld)"),
9627                            (long) imm_expr.X_add_number);
9628                   imm_expr.X_add_number = 0;
9629                 }
9630
9631               INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
9632               imm_expr.X_op = O_absent;
9633               s = expr_end;
9634               continue;
9635
9636             default:
9637               as_bad (_("bad char = '%c'\n"), *args);
9638               internalError ();
9639             }
9640           break;
9641         }
9642       /* Args don't match.  */
9643       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9644           !strcmp (insn->name, insn[1].name))
9645         {
9646           ++insn;
9647           s = argsStart;
9648           insn_error = _("illegal operands");
9649           continue;
9650         }
9651       if (save_c)
9652         *(--s) = save_c;
9653       insn_error = _("illegal operands");
9654       return;
9655     }
9656 }
9657
9658 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9659
9660 /* This routine assembles an instruction into its binary format when
9661    assembling for the mips16.  As a side effect, it sets one of the
9662    global variables imm_reloc or offset_reloc to the type of
9663    relocation to do if one of the operands is an address expression.
9664    It also sets mips16_small and mips16_ext if the user explicitly
9665    requested a small or extended instruction.  */
9666
9667 static void
9668 mips16_ip (char *str, struct mips_cl_insn *ip)
9669 {
9670   char *s;
9671   const char *args;
9672   struct mips_opcode *insn;
9673   char *argsstart;
9674   unsigned int regno;
9675   unsigned int lastregno = 0;
9676   char *s_reset;
9677   size_t i;
9678
9679   insn_error = NULL;
9680
9681   mips16_small = FALSE;
9682   mips16_ext = FALSE;
9683
9684   for (s = str; ISLOWER (*s); ++s)
9685     ;
9686   switch (*s)
9687     {
9688     case '\0':
9689       break;
9690
9691     case ' ':
9692       *s++ = '\0';
9693       break;
9694
9695     case '.':
9696       if (s[1] == 't' && s[2] == ' ')
9697         {
9698           *s = '\0';
9699           mips16_small = TRUE;
9700           s += 3;
9701           break;
9702         }
9703       else if (s[1] == 'e' && s[2] == ' ')
9704         {
9705           *s = '\0';
9706           mips16_ext = TRUE;
9707           s += 3;
9708           break;
9709         }
9710       /* Fall through.  */
9711     default:
9712       insn_error = _("unknown opcode");
9713       return;
9714     }
9715
9716   if (mips_opts.noautoextend && ! mips16_ext)
9717     mips16_small = TRUE;
9718
9719   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9720     {
9721       insn_error = _("unrecognized opcode");
9722       return;
9723     }
9724
9725   argsstart = s;
9726   for (;;)
9727     {
9728       bfd_boolean ok;
9729
9730       assert (strcmp (insn->name, str) == 0);
9731
9732       if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_opts.arch))
9733         ok = TRUE;
9734       else
9735         ok = FALSE;
9736
9737       if (! ok)
9738         {
9739           if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
9740               && strcmp (insn->name, insn[1].name) == 0)
9741             {
9742               ++insn;
9743               continue;
9744             }
9745           else
9746             {
9747               if (!insn_error)
9748                 {
9749                   static char buf[100];
9750                   sprintf (buf,
9751                            _("opcode not supported on this processor: %s (%s)"),
9752                            mips_cpu_info_from_arch (mips_opts.arch)->name,
9753                            mips_cpu_info_from_isa (mips_opts.isa)->name);
9754                   insn_error = buf;
9755                 }
9756               return;
9757             }
9758         }
9759
9760       create_insn (ip, insn);
9761       imm_expr.X_op = O_absent;
9762       imm_reloc[0] = BFD_RELOC_UNUSED;
9763       imm_reloc[1] = BFD_RELOC_UNUSED;
9764       imm_reloc[2] = BFD_RELOC_UNUSED;
9765       imm2_expr.X_op = O_absent;
9766       offset_expr.X_op = O_absent;
9767       offset_reloc[0] = BFD_RELOC_UNUSED;
9768       offset_reloc[1] = BFD_RELOC_UNUSED;
9769       offset_reloc[2] = BFD_RELOC_UNUSED;
9770       for (args = insn->args; 1; ++args)
9771         {
9772           int c;
9773
9774           if (*s == ' ')
9775             ++s;
9776
9777           /* In this switch statement we call break if we did not find
9778              a match, continue if we did find a match, or return if we
9779              are done.  */
9780
9781           c = *args;
9782           switch (c)
9783             {
9784             case '\0':
9785               if (*s == '\0')
9786                 {
9787                   /* Stuff the immediate value in now, if we can.  */
9788                   if (imm_expr.X_op == O_constant
9789                       && *imm_reloc > BFD_RELOC_UNUSED
9790                       && insn->pinfo != INSN_MACRO)
9791                     {
9792                       valueT tmp;
9793
9794                       switch (*offset_reloc)
9795                         {
9796                           case BFD_RELOC_MIPS16_HI16_S:
9797                             tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9798                             break;
9799
9800                           case BFD_RELOC_MIPS16_HI16:
9801                             tmp = imm_expr.X_add_number >> 16;
9802                             break;
9803
9804                           case BFD_RELOC_MIPS16_LO16:
9805                             tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9806                                   - 0x8000;
9807                             break;
9808
9809                           case BFD_RELOC_UNUSED:
9810                             tmp = imm_expr.X_add_number;
9811                             break;
9812
9813                           default:
9814                             internalError ();
9815                         }
9816                       *offset_reloc = BFD_RELOC_UNUSED;
9817
9818                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9819                                     tmp, TRUE, mips16_small,
9820                                     mips16_ext, &ip->insn_opcode,
9821                                     &ip->use_extend, &ip->extend);
9822                       imm_expr.X_op = O_absent;
9823                       *imm_reloc = BFD_RELOC_UNUSED;
9824                     }
9825
9826                   return;
9827                 }
9828               break;
9829
9830             case ',':
9831               if (*s++ == c)
9832                 continue;
9833               s--;
9834               switch (*++args)
9835                 {
9836                 case 'v':
9837                   MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9838                   continue;
9839                 case 'w':
9840                   MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9841                   continue;
9842                 }
9843               break;
9844
9845             case '(':
9846             case ')':
9847               if (*s++ == c)
9848                 continue;
9849               break;
9850
9851             case 'v':
9852             case 'w':
9853               if (s[0] != '$')
9854                 {
9855                   if (c == 'v')
9856                     MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9857                   else
9858                     MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9859                   ++args;
9860                   continue;
9861                 }
9862               /* Fall through.  */
9863             case 'x':
9864             case 'y':
9865             case 'z':
9866             case 'Z':
9867             case '0':
9868             case 'S':
9869             case 'R':
9870             case 'X':
9871             case 'Y':
9872               s_reset = s;
9873               if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
9874                 {
9875                   if (c == 'v' || c == 'w')
9876                     {
9877                       if (c == 'v')
9878                         MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9879                       else
9880                         MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9881                       ++args;
9882                       continue;
9883                     }
9884                   break;
9885                 }
9886
9887               if (*s == ' ')
9888                 ++s;
9889               if (args[1] != *s)
9890                 {
9891                   if (c == 'v' || c == 'w')
9892                     {
9893                       regno = mips16_to_32_reg_map[lastregno];
9894                       s = s_reset;
9895                       ++args;
9896                     }
9897                 }
9898
9899               switch (c)
9900                 {
9901                 case 'x':
9902                 case 'y':
9903                 case 'z':
9904                 case 'v':
9905                 case 'w':
9906                 case 'Z':
9907                   regno = mips32_to_16_reg_map[regno];
9908                   break;
9909
9910                 case '0':
9911                   if (regno != 0)
9912                     regno = ILLEGAL_REG;
9913                   break;
9914
9915                 case 'S':
9916                   if (regno != SP)
9917                     regno = ILLEGAL_REG;
9918                   break;
9919
9920                 case 'R':
9921                   if (regno != RA)
9922                     regno = ILLEGAL_REG;
9923                   break;
9924
9925                 case 'X':
9926                 case 'Y':
9927                   if (regno == AT && ! mips_opts.noat)
9928                     as_warn (_("used $at without \".set noat\""));
9929                   break;
9930
9931                 default:
9932                   internalError ();
9933                 }
9934
9935               if (regno == ILLEGAL_REG)
9936                 break;
9937
9938               switch (c)
9939                 {
9940                 case 'x':
9941                 case 'v':
9942                   MIPS16_INSERT_OPERAND (RX, *ip, regno);
9943                   break;
9944                 case 'y':
9945                 case 'w':
9946                   MIPS16_INSERT_OPERAND (RY, *ip, regno);
9947                   break;
9948                 case 'z':
9949                   MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9950                   break;
9951                 case 'Z':
9952                   MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9953                 case '0':
9954                 case 'S':
9955                 case 'R':
9956                   break;
9957                 case 'X':
9958                   MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9959                   break;
9960                 case 'Y':
9961                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9962                   MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9963                   break;
9964                 default:
9965                   internalError ();
9966                 }
9967
9968               lastregno = regno;
9969               continue;
9970
9971             case 'P':
9972               if (strncmp (s, "$pc", 3) == 0)
9973                 {
9974                   s += 3;
9975                   continue;
9976                 }
9977               break;
9978
9979             case '5':
9980             case 'H':
9981             case 'W':
9982             case 'D':
9983             case 'j':
9984             case 'V':
9985             case 'C':
9986             case 'U':
9987             case 'k':
9988             case 'K':
9989               i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9990               if (i > 0)
9991                 {
9992                   if (imm_expr.X_op != O_constant)
9993                     {
9994                       mips16_ext = TRUE;
9995                       ip->use_extend = TRUE;
9996                       ip->extend = 0;
9997                     }
9998                   else
9999                     {
10000                       /* We need to relax this instruction.  */
10001                       *offset_reloc = *imm_reloc;
10002                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10003                     }
10004                   s = expr_end;
10005                   continue;
10006                 }
10007               *imm_reloc = BFD_RELOC_UNUSED;
10008               /* Fall through.  */
10009             case '<':
10010             case '>':
10011             case '[':
10012             case ']':
10013             case '4':
10014             case '8':
10015               my_getExpression (&imm_expr, s);
10016               if (imm_expr.X_op == O_register)
10017                 {
10018                   /* What we thought was an expression turned out to
10019                      be a register.  */
10020
10021                   if (s[0] == '(' && args[1] == '(')
10022                     {
10023                       /* It looks like the expression was omitted
10024                          before a register indirection, which means
10025                          that the expression is implicitly zero.  We
10026                          still set up imm_expr, so that we handle
10027                          explicit extensions correctly.  */
10028                       imm_expr.X_op = O_constant;
10029                       imm_expr.X_add_number = 0;
10030                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10031                       continue;
10032                     }
10033
10034                   break;
10035                 }
10036
10037               /* We need to relax this instruction.  */
10038               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10039               s = expr_end;
10040               continue;
10041
10042             case 'p':
10043             case 'q':
10044             case 'A':
10045             case 'B':
10046             case 'E':
10047               /* We use offset_reloc rather than imm_reloc for the PC
10048                  relative operands.  This lets macros with both
10049                  immediate and address operands work correctly.  */
10050               my_getExpression (&offset_expr, s);
10051
10052               if (offset_expr.X_op == O_register)
10053                 break;
10054
10055               /* We need to relax this instruction.  */
10056               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10057               s = expr_end;
10058               continue;
10059
10060             case '6':           /* break code */
10061               my_getExpression (&imm_expr, s);
10062               check_absolute_expr (ip, &imm_expr);
10063               if ((unsigned long) imm_expr.X_add_number > 63)
10064                 as_warn (_("Invalid value for `%s' (%lu)"),
10065                          ip->insn_mo->name,
10066                          (unsigned long) imm_expr.X_add_number);
10067               MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
10068               imm_expr.X_op = O_absent;
10069               s = expr_end;
10070               continue;
10071
10072             case 'a':           /* 26 bit address */
10073               my_getExpression (&offset_expr, s);
10074               s = expr_end;
10075               *offset_reloc = BFD_RELOC_MIPS16_JMP;
10076               ip->insn_opcode <<= 16;
10077               continue;
10078
10079             case 'l':           /* register list for entry macro */
10080             case 'L':           /* register list for exit macro */
10081               {
10082                 int mask;
10083
10084                 if (c == 'l')
10085                   mask = 0;
10086                 else
10087                   mask = 7 << 3;
10088                 while (*s != '\0')
10089                   {
10090                     unsigned int freg, reg1, reg2;
10091
10092                     while (*s == ' ' || *s == ',')
10093                       ++s;
10094                     if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
10095                       freg = 0;
10096                     else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10097                       freg = 1;
10098                     else
10099                       {
10100                         as_bad (_("can't parse register list"));
10101                         break;
10102                       }
10103                     if (*s == ' ')
10104                       ++s;
10105                     if (*s != '-')
10106                       reg2 = reg1;
10107                     else
10108                       {
10109                         ++s;
10110                         if (!reg_lookup (&s, freg ? RTYPE_FPU 
10111                                          : (RTYPE_GP | RTYPE_NUM), &reg2))
10112                           {
10113                             as_bad (_("invalid register list"));
10114                             break;
10115                           }
10116                       }
10117                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10118                       {
10119                         mask &= ~ (7 << 3);
10120                         mask |= 5 << 3;
10121                       }
10122                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10123                       {
10124                         mask &= ~ (7 << 3);
10125                         mask |= 6 << 3;
10126                       }
10127                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10128                       mask |= (reg2 - 3) << 3;
10129                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10130                       mask |= (reg2 - 15) << 1;
10131                     else if (reg1 == RA && reg2 == RA)
10132                       mask |= 1;
10133                     else
10134                       {
10135                         as_bad (_("invalid register list"));
10136                         break;
10137                       }
10138                   }
10139                 /* The mask is filled in in the opcode table for the
10140                    benefit of the disassembler.  We remove it before
10141                    applying the actual mask.  */
10142                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10143                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10144               }
10145             continue;
10146
10147             case 'm':           /* Register list for save insn.  */
10148             case 'M':           /* Register list for restore insn.  */
10149               {
10150                 int opcode = 0;
10151                 int framesz = 0, seen_framesz = 0;
10152                 int args = 0, statics = 0, sregs = 0;
10153
10154                 while (*s != '\0')
10155                   {
10156                     unsigned int reg1, reg2;
10157
10158                     SKIP_SPACE_TABS (s);
10159                     while (*s == ',')
10160                       ++s;
10161                     SKIP_SPACE_TABS (s);
10162
10163                     my_getExpression (&imm_expr, s);
10164                     if (imm_expr.X_op == O_constant)
10165                       {
10166                         /* Handle the frame size.  */
10167                         if (seen_framesz)
10168                           {
10169                             as_bad (_("more than one frame size in list"));
10170                             break;
10171                           }
10172                         seen_framesz = 1;
10173                         framesz = imm_expr.X_add_number;
10174                         imm_expr.X_op = O_absent;
10175                         s = expr_end;
10176                         continue;
10177                       }
10178
10179                     if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
10180                       {
10181                         as_bad (_("can't parse register list"));
10182                         break;
10183                       }
10184
10185                     while (*s == ' ')
10186                       ++s;
10187
10188                     if (*s != '-')
10189                       reg2 = reg1;
10190                     else
10191                       {
10192                         ++s;
10193                         if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10194                             || reg2 < reg1)
10195                           {
10196                             as_bad (_("can't parse register list"));
10197                             break;
10198                           }
10199                       }
10200
10201                     while (reg1 <= reg2)
10202                       {
10203                         if (reg1 >= 4 && reg1 <= 7)
10204                           {
10205                             if (c == 'm' && !seen_framesz)
10206                                 /* args $a0-$a3 */
10207                                 args |= 1 << (reg1 - 4);
10208                             else
10209                                 /* statics $a0-$a3 */
10210                                 statics |= 1 << (reg1 - 4);
10211                           }
10212                         else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10213                           {
10214                             /* $s0-$s8 */
10215                             sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10216                           }
10217                         else if (reg1 == 31)
10218                           {
10219                             /* Add $ra to insn.  */
10220                             opcode |= 0x40;
10221                           }
10222                         else
10223                           {
10224                             as_bad (_("unexpected register in list"));
10225                             break;
10226                           }
10227                         if (++reg1 == 24)
10228                           reg1 = 30;
10229                       }
10230                   }
10231
10232                 /* Encode args/statics combination.  */
10233                 if (args & statics)
10234                   as_bad (_("arg/static registers overlap"));
10235                 else if (args == 0xf)
10236                   /* All $a0-$a3 are args.  */
10237                   opcode |= MIPS16_ALL_ARGS << 16;
10238                 else if (statics == 0xf)
10239                   /* All $a0-$a3 are statics.  */
10240                   opcode |= MIPS16_ALL_STATICS << 16;
10241                 else 
10242                   {
10243                     int narg = 0, nstat = 0;
10244
10245                     /* Count arg registers.  */
10246                     while (args & 0x1)
10247                       {
10248                         args >>= 1;
10249                         narg++;
10250                       }
10251                     if (args != 0)
10252                       as_bad (_("invalid arg register list"));
10253
10254                     /* Count static registers.  */
10255                     while (statics & 0x8)
10256                       {
10257                         statics = (statics << 1) & 0xf;
10258                         nstat++;
10259                       }
10260                     if (statics != 0) 
10261                       as_bad (_("invalid static register list"));
10262
10263                     /* Encode args/statics.  */
10264                     opcode |= ((narg << 2) | nstat) << 16;
10265                   }
10266
10267                 /* Encode $s0/$s1.  */
10268                 if (sregs & (1 << 0))           /* $s0 */
10269                   opcode |= 0x20;
10270                 if (sregs & (1 << 1))           /* $s1 */
10271                   opcode |= 0x10;
10272                 sregs >>= 2;
10273
10274                 if (sregs != 0)
10275                   {
10276                     /* Count regs $s2-$s8.  */
10277                     int nsreg = 0;
10278                     while (sregs & 1)
10279                       {
10280                         sregs >>= 1;
10281                         nsreg++;
10282                       }
10283                     if (sregs != 0)
10284                       as_bad (_("invalid static register list"));
10285                     /* Encode $s2-$s8. */
10286                     opcode |= nsreg << 24;
10287                   }
10288
10289                 /* Encode frame size.  */
10290                 if (!seen_framesz)
10291                   as_bad (_("missing frame size"));
10292                 else if ((framesz & 7) != 0 || framesz < 0
10293                          || framesz > 0xff * 8)
10294                   as_bad (_("invalid frame size"));
10295                 else if (framesz != 128 || (opcode >> 16) != 0)
10296                   {
10297                     framesz /= 8;
10298                     opcode |= (((framesz & 0xf0) << 16)
10299                              | (framesz & 0x0f));
10300                   }
10301
10302                 /* Finally build the instruction.  */
10303                 if ((opcode >> 16) != 0 || framesz == 0)
10304                   {
10305                     ip->use_extend = TRUE;
10306                     ip->extend = opcode >> 16;
10307                   }
10308                 ip->insn_opcode |= opcode & 0x7f;
10309               }
10310             continue;
10311
10312             case 'e':           /* extend code */
10313               my_getExpression (&imm_expr, s);
10314               check_absolute_expr (ip, &imm_expr);
10315               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10316                 {
10317                   as_warn (_("Invalid value for `%s' (%lu)"),
10318                            ip->insn_mo->name,
10319                            (unsigned long) imm_expr.X_add_number);
10320                   imm_expr.X_add_number &= 0x7ff;
10321                 }
10322               ip->insn_opcode |= imm_expr.X_add_number;
10323               imm_expr.X_op = O_absent;
10324               s = expr_end;
10325               continue;
10326
10327             default:
10328               internalError ();
10329             }
10330           break;
10331         }
10332
10333       /* Args don't match.  */
10334       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10335           strcmp (insn->name, insn[1].name) == 0)
10336         {
10337           ++insn;
10338           s = argsstart;
10339           continue;
10340         }
10341
10342       insn_error = _("illegal operands");
10343
10344       return;
10345     }
10346 }
10347
10348 /* This structure holds information we know about a mips16 immediate
10349    argument type.  */
10350
10351 struct mips16_immed_operand
10352 {
10353   /* The type code used in the argument string in the opcode table.  */
10354   int type;
10355   /* The number of bits in the short form of the opcode.  */
10356   int nbits;
10357   /* The number of bits in the extended form of the opcode.  */
10358   int extbits;
10359   /* The amount by which the short form is shifted when it is used;
10360      for example, the sw instruction has a shift count of 2.  */
10361   int shift;
10362   /* The amount by which the short form is shifted when it is stored
10363      into the instruction code.  */
10364   int op_shift;
10365   /* Non-zero if the short form is unsigned.  */
10366   int unsp;
10367   /* Non-zero if the extended form is unsigned.  */
10368   int extu;
10369   /* Non-zero if the value is PC relative.  */
10370   int pcrel;
10371 };
10372
10373 /* The mips16 immediate operand types.  */
10374
10375 static const struct mips16_immed_operand mips16_immed_operands[] =
10376 {
10377   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10378   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10379   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10380   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10381   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10382   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10383   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10384   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10385   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10386   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10387   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10388   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10389   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10390   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10391   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10392   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10393   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10394   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10395   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10396   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10397   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10398 };
10399
10400 #define MIPS16_NUM_IMMED \
10401   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10402
10403 /* Handle a mips16 instruction with an immediate value.  This or's the
10404    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10405    whether an extended value is needed; if one is needed, it sets
10406    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10407    If SMALL is true, an unextended opcode was explicitly requested.
10408    If EXT is true, an extended opcode was explicitly requested.  If
10409    WARN is true, warn if EXT does not match reality.  */
10410
10411 static void
10412 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10413               bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10414               unsigned long *insn, bfd_boolean *use_extend,
10415               unsigned short *extend)
10416 {
10417   const struct mips16_immed_operand *op;
10418   int mintiny, maxtiny;
10419   bfd_boolean needext;
10420
10421   op = mips16_immed_operands;
10422   while (op->type != type)
10423     {
10424       ++op;
10425       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10426     }
10427
10428   if (op->unsp)
10429     {
10430       if (type == '<' || type == '>' || type == '[' || type == ']')
10431         {
10432           mintiny = 1;
10433           maxtiny = 1 << op->nbits;
10434         }
10435       else
10436         {
10437           mintiny = 0;
10438           maxtiny = (1 << op->nbits) - 1;
10439         }
10440     }
10441   else
10442     {
10443       mintiny = - (1 << (op->nbits - 1));
10444       maxtiny = (1 << (op->nbits - 1)) - 1;
10445     }
10446
10447   /* Branch offsets have an implicit 0 in the lowest bit.  */
10448   if (type == 'p' || type == 'q')
10449     val /= 2;
10450
10451   if ((val & ((1 << op->shift) - 1)) != 0
10452       || val < (mintiny << op->shift)
10453       || val > (maxtiny << op->shift))
10454     needext = TRUE;
10455   else
10456     needext = FALSE;
10457
10458   if (warn && ext && ! needext)
10459     as_warn_where (file, line,
10460                    _("extended operand requested but not required"));
10461   if (small && needext)
10462     as_bad_where (file, line, _("invalid unextended operand value"));
10463
10464   if (small || (! ext && ! needext))
10465     {
10466       int insnval;
10467
10468       *use_extend = FALSE;
10469       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10470       insnval <<= op->op_shift;
10471       *insn |= insnval;
10472     }
10473   else
10474     {
10475       long minext, maxext;
10476       int extval;
10477
10478       if (op->extu)
10479         {
10480           minext = 0;
10481           maxext = (1 << op->extbits) - 1;
10482         }
10483       else
10484         {
10485           minext = - (1 << (op->extbits - 1));
10486           maxext = (1 << (op->extbits - 1)) - 1;
10487         }
10488       if (val < minext || val > maxext)
10489         as_bad_where (file, line,
10490                       _("operand value out of range for instruction"));
10491
10492       *use_extend = TRUE;
10493       if (op->extbits == 16)
10494         {
10495           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10496           val &= 0x1f;
10497         }
10498       else if (op->extbits == 15)
10499         {
10500           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10501           val &= 0xf;
10502         }
10503       else
10504         {
10505           extval = ((val & 0x1f) << 6) | (val & 0x20);
10506           val = 0;
10507         }
10508
10509       *extend = (unsigned short) extval;
10510       *insn |= val;
10511     }
10512 }
10513 \f
10514 struct percent_op_match
10515 {
10516   const char *str;
10517   bfd_reloc_code_real_type reloc;
10518 };
10519
10520 static const struct percent_op_match mips_percent_op[] =
10521 {
10522   {"%lo", BFD_RELOC_LO16},
10523 #ifdef OBJ_ELF
10524   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10525   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10526   {"%call16", BFD_RELOC_MIPS_CALL16},
10527   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10528   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10529   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10530   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10531   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10532   {"%got", BFD_RELOC_MIPS_GOT16},
10533   {"%gp_rel", BFD_RELOC_GPREL16},
10534   {"%half", BFD_RELOC_16},
10535   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10536   {"%higher", BFD_RELOC_MIPS_HIGHER},
10537   {"%neg", BFD_RELOC_MIPS_SUB},
10538   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10539   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10540   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10541   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10542   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10543   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10544   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
10545 #endif
10546   {"%hi", BFD_RELOC_HI16_S}
10547 };
10548
10549 static const struct percent_op_match mips16_percent_op[] =
10550 {
10551   {"%lo", BFD_RELOC_MIPS16_LO16},
10552   {"%gprel", BFD_RELOC_MIPS16_GPREL},
10553   {"%hi", BFD_RELOC_MIPS16_HI16_S}
10554 };
10555
10556
10557 /* Return true if *STR points to a relocation operator.  When returning true,
10558    move *STR over the operator and store its relocation code in *RELOC.
10559    Leave both *STR and *RELOC alone when returning false.  */
10560
10561 static bfd_boolean
10562 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10563 {
10564   const struct percent_op_match *percent_op;
10565   size_t limit, i;
10566
10567   if (mips_opts.mips16)
10568     {
10569       percent_op = mips16_percent_op;
10570       limit = ARRAY_SIZE (mips16_percent_op);
10571     }
10572   else
10573     {
10574       percent_op = mips_percent_op;
10575       limit = ARRAY_SIZE (mips_percent_op);
10576     }
10577
10578   for (i = 0; i < limit; i++)
10579     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10580       {
10581         int len = strlen (percent_op[i].str);
10582
10583         if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10584           continue;
10585
10586         *str += strlen (percent_op[i].str);
10587         *reloc = percent_op[i].reloc;
10588
10589         /* Check whether the output BFD supports this relocation.
10590            If not, issue an error and fall back on something safe.  */
10591         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10592           {
10593             as_bad ("relocation %s isn't supported by the current ABI",
10594                     percent_op[i].str);
10595             *reloc = BFD_RELOC_UNUSED;
10596           }
10597         return TRUE;
10598       }
10599   return FALSE;
10600 }
10601
10602
10603 /* Parse string STR as a 16-bit relocatable operand.  Store the
10604    expression in *EP and the relocations in the array starting
10605    at RELOC.  Return the number of relocation operators used.
10606
10607    On exit, EXPR_END points to the first character after the expression.  */
10608
10609 static size_t
10610 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10611                        char *str)
10612 {
10613   bfd_reloc_code_real_type reversed_reloc[3];
10614   size_t reloc_index, i;
10615   int crux_depth, str_depth;
10616   char *crux;
10617
10618   /* Search for the start of the main expression, recoding relocations
10619      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10620      of the main expression and with CRUX_DEPTH containing the number
10621      of open brackets at that point.  */
10622   reloc_index = -1;
10623   str_depth = 0;
10624   do
10625     {
10626       reloc_index++;
10627       crux = str;
10628       crux_depth = str_depth;
10629
10630       /* Skip over whitespace and brackets, keeping count of the number
10631          of brackets.  */
10632       while (*str == ' ' || *str == '\t' || *str == '(')
10633         if (*str++ == '(')
10634           str_depth++;
10635     }
10636   while (*str == '%'
10637          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10638          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10639
10640   my_getExpression (ep, crux);
10641   str = expr_end;
10642
10643   /* Match every open bracket.  */
10644   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10645     if (*str++ == ')')
10646       crux_depth--;
10647
10648   if (crux_depth > 0)
10649     as_bad ("unclosed '('");
10650
10651   expr_end = str;
10652
10653   if (reloc_index != 0)
10654     {
10655       prev_reloc_op_frag = frag_now;
10656       for (i = 0; i < reloc_index; i++)
10657         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10658     }
10659
10660   return reloc_index;
10661 }
10662
10663 static void
10664 my_getExpression (expressionS *ep, char *str)
10665 {
10666   char *save_in;
10667   valueT val;
10668
10669   save_in = input_line_pointer;
10670   input_line_pointer = str;
10671   expression (ep);
10672   expr_end = input_line_pointer;
10673   input_line_pointer = save_in;
10674
10675   /* If we are in mips16 mode, and this is an expression based on `.',
10676      then we bump the value of the symbol by 1 since that is how other
10677      text symbols are handled.  We don't bother to handle complex
10678      expressions, just `.' plus or minus a constant.  */
10679   if (mips_opts.mips16
10680       && ep->X_op == O_symbol
10681       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10682       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10683       && symbol_get_frag (ep->X_add_symbol) == frag_now
10684       && symbol_constant_p (ep->X_add_symbol)
10685       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10686     S_SET_VALUE (ep->X_add_symbol, val + 1);
10687 }
10688
10689 /* Turn a string in input_line_pointer into a floating point constant
10690    of type TYPE, and store the appropriate bytes in *LITP.  The number
10691    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10692    returned, or NULL on OK.  */
10693
10694 char *
10695 md_atof (int type, char *litP, int *sizeP)
10696 {
10697   int prec;
10698   LITTLENUM_TYPE words[4];
10699   char *t;
10700   int i;
10701
10702   switch (type)
10703     {
10704     case 'f':
10705       prec = 2;
10706       break;
10707
10708     case 'd':
10709       prec = 4;
10710       break;
10711
10712     default:
10713       *sizeP = 0;
10714       return _("bad call to md_atof");
10715     }
10716
10717   t = atof_ieee (input_line_pointer, type, words);
10718   if (t)
10719     input_line_pointer = t;
10720
10721   *sizeP = prec * 2;
10722
10723   if (! target_big_endian)
10724     {
10725       for (i = prec - 1; i >= 0; i--)
10726         {
10727           md_number_to_chars (litP, words[i], 2);
10728           litP += 2;
10729         }
10730     }
10731   else
10732     {
10733       for (i = 0; i < prec; i++)
10734         {
10735           md_number_to_chars (litP, words[i], 2);
10736           litP += 2;
10737         }
10738     }
10739
10740   return NULL;
10741 }
10742
10743 void
10744 md_number_to_chars (char *buf, valueT val, int n)
10745 {
10746   if (target_big_endian)
10747     number_to_chars_bigendian (buf, val, n);
10748   else
10749     number_to_chars_littleendian (buf, val, n);
10750 }
10751 \f
10752 #ifdef OBJ_ELF
10753 static int support_64bit_objects(void)
10754 {
10755   const char **list, **l;
10756   int yes;
10757
10758   list = bfd_target_list ();
10759   for (l = list; *l != NULL; l++)
10760 #ifdef TE_TMIPS
10761     /* This is traditional mips */
10762     if (strcmp (*l, "elf64-tradbigmips") == 0
10763         || strcmp (*l, "elf64-tradlittlemips") == 0)
10764 #else
10765     if (strcmp (*l, "elf64-bigmips") == 0
10766         || strcmp (*l, "elf64-littlemips") == 0)
10767 #endif
10768       break;
10769   yes = (*l != NULL);
10770   free (list);
10771   return yes;
10772 }
10773 #endif /* OBJ_ELF */
10774
10775 const char *md_shortopts = "O::g::G:";
10776
10777 struct option md_longopts[] =
10778 {
10779   /* Options which specify architecture.  */
10780 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
10781 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10782   {"march", required_argument, NULL, OPTION_MARCH},
10783 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10784   {"mtune", required_argument, NULL, OPTION_MTUNE},
10785 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10786   {"mips0", no_argument, NULL, OPTION_MIPS1},
10787   {"mips1", no_argument, NULL, OPTION_MIPS1},
10788 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10789   {"mips2", no_argument, NULL, OPTION_MIPS2},
10790 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10791   {"mips3", no_argument, NULL, OPTION_MIPS3},
10792 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10793   {"mips4", no_argument, NULL, OPTION_MIPS4},
10794 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10795   {"mips5", no_argument, NULL, OPTION_MIPS5},
10796 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10797   {"mips32", no_argument, NULL, OPTION_MIPS32},
10798 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10799   {"mips64", no_argument, NULL, OPTION_MIPS64},
10800 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10801   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10802 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10803   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10804
10805   /* Options which specify Application Specific Extensions (ASEs).  */
10806 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10807 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10808   {"mips16", no_argument, NULL, OPTION_MIPS16},
10809 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10810   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10811 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10812   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10813 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10814   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10815 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10816   {"mdmx", no_argument, NULL, OPTION_MDMX},
10817 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10818   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10819 #define OPTION_DSP (OPTION_ASE_BASE + 6)
10820   {"mdsp", no_argument, NULL, OPTION_DSP},
10821 #define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10822   {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
10823 #define OPTION_MT (OPTION_ASE_BASE + 8)
10824   {"mmt", no_argument, NULL, OPTION_MT},
10825 #define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10826   {"mno-mt", no_argument, NULL, OPTION_NO_MT},
10827 #define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
10828   {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
10829 #define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
10830   {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
10831
10832   /* Old-style architecture options.  Don't add more of these.  */
10833 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 12)
10834 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10835   {"m4650", no_argument, NULL, OPTION_M4650},
10836 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10837   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10838 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10839   {"m4010", no_argument, NULL, OPTION_M4010},
10840 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10841   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10842 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10843   {"m4100", no_argument, NULL, OPTION_M4100},
10844 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10845   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10846 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10847   {"m3900", no_argument, NULL, OPTION_M3900},
10848 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10849   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10850
10851   /* Options which enable bug fixes.  */
10852 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10853 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10854   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10855 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10856   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10857   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10858 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10859 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10860   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
10861   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10862 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10863 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10864   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
10865   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10866
10867   /* Miscellaneous options.  */
10868 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10869 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10870   {"trap", no_argument, NULL, OPTION_TRAP},
10871   {"no-break", no_argument, NULL, OPTION_TRAP},
10872 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10873   {"break", no_argument, NULL, OPTION_BREAK},
10874   {"no-trap", no_argument, NULL, OPTION_BREAK},
10875 #define OPTION_EB (OPTION_MISC_BASE + 2)
10876   {"EB", no_argument, NULL, OPTION_EB},
10877 #define OPTION_EL (OPTION_MISC_BASE + 3)
10878   {"EL", no_argument, NULL, OPTION_EL},
10879 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10880   {"mfp32", no_argument, NULL, OPTION_FP32},
10881 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10882   {"mgp32", no_argument, NULL, OPTION_GP32},
10883 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10884   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10885 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10886   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10887 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10888   {"mfp64", no_argument, NULL, OPTION_FP64},
10889 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10890   {"mgp64", no_argument, NULL, OPTION_GP64},
10891 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10892 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10893   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10894   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10895 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10896 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10897   {"mshared", no_argument, NULL, OPTION_MSHARED},
10898   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10899 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10900 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10901   {"msym32", no_argument, NULL, OPTION_MSYM32},
10902   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10903
10904   /* ELF-specific options.  */
10905 #ifdef OBJ_ELF
10906 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 16)
10907 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10908   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10909   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10910 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10911   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10912 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10913   {"xgot",        no_argument, NULL, OPTION_XGOT},
10914 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10915   {"mabi", required_argument, NULL, OPTION_MABI},
10916 #define OPTION_32          (OPTION_ELF_BASE + 4)
10917   {"32",          no_argument, NULL, OPTION_32},
10918 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10919   {"n32",         no_argument, NULL, OPTION_N32},
10920 #define OPTION_64          (OPTION_ELF_BASE + 6)
10921   {"64",          no_argument, NULL, OPTION_64},
10922 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10923   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10924 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10925   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10926 #define OPTION_PDR         (OPTION_ELF_BASE + 9)
10927   {"mpdr", no_argument, NULL, OPTION_PDR},
10928 #define OPTION_NO_PDR      (OPTION_ELF_BASE + 10)
10929   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10930 #define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10931   {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
10932 #endif /* OBJ_ELF */
10933
10934   {NULL, no_argument, NULL, 0}
10935 };
10936 size_t md_longopts_size = sizeof (md_longopts);
10937
10938 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10939    NEW_VALUE.  Warn if another value was already specified.  Note:
10940    we have to defer parsing the -march and -mtune arguments in order
10941    to handle 'from-abi' correctly, since the ABI might be specified
10942    in a later argument.  */
10943
10944 static void
10945 mips_set_option_string (const char **string_ptr, const char *new_value)
10946 {
10947   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10948     as_warn (_("A different %s was already specified, is now %s"),
10949              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10950              new_value);
10951
10952   *string_ptr = new_value;
10953 }
10954
10955 int
10956 md_parse_option (int c, char *arg)
10957 {
10958   switch (c)
10959     {
10960     case OPTION_CONSTRUCT_FLOATS:
10961       mips_disable_float_construction = 0;
10962       break;
10963
10964     case OPTION_NO_CONSTRUCT_FLOATS:
10965       mips_disable_float_construction = 1;
10966       break;
10967
10968     case OPTION_TRAP:
10969       mips_trap = 1;
10970       break;
10971
10972     case OPTION_BREAK:
10973       mips_trap = 0;
10974       break;
10975
10976     case OPTION_EB:
10977       target_big_endian = 1;
10978       break;
10979
10980     case OPTION_EL:
10981       target_big_endian = 0;
10982       break;
10983
10984     case 'O':
10985       if (arg && arg[1] == '0')
10986         mips_optimize = 1;
10987       else
10988         mips_optimize = 2;
10989       break;
10990
10991     case 'g':
10992       if (arg == NULL)
10993         mips_debug = 2;
10994       else
10995         mips_debug = atoi (arg);
10996       /* When the MIPS assembler sees -g or -g2, it does not do
10997          optimizations which limit full symbolic debugging.  We take
10998          that to be equivalent to -O0.  */
10999       if (mips_debug == 2)
11000         mips_optimize = 1;
11001       break;
11002
11003     case OPTION_MIPS1:
11004       file_mips_isa = ISA_MIPS1;
11005       break;
11006
11007     case OPTION_MIPS2:
11008       file_mips_isa = ISA_MIPS2;
11009       break;
11010
11011     case OPTION_MIPS3:
11012       file_mips_isa = ISA_MIPS3;
11013       break;
11014
11015     case OPTION_MIPS4:
11016       file_mips_isa = ISA_MIPS4;
11017       break;
11018
11019     case OPTION_MIPS5:
11020       file_mips_isa = ISA_MIPS5;
11021       break;
11022
11023     case OPTION_MIPS32:
11024       file_mips_isa = ISA_MIPS32;
11025       break;
11026
11027     case OPTION_MIPS32R2:
11028       file_mips_isa = ISA_MIPS32R2;
11029       break;
11030
11031     case OPTION_MIPS64R2:
11032       file_mips_isa = ISA_MIPS64R2;
11033       break;
11034
11035     case OPTION_MIPS64:
11036       file_mips_isa = ISA_MIPS64;
11037       break;
11038
11039     case OPTION_MTUNE:
11040       mips_set_option_string (&mips_tune_string, arg);
11041       break;
11042
11043     case OPTION_MARCH:
11044       mips_set_option_string (&mips_arch_string, arg);
11045       break;
11046
11047     case OPTION_M4650:
11048       mips_set_option_string (&mips_arch_string, "4650");
11049       mips_set_option_string (&mips_tune_string, "4650");
11050       break;
11051
11052     case OPTION_NO_M4650:
11053       break;
11054
11055     case OPTION_M4010:
11056       mips_set_option_string (&mips_arch_string, "4010");
11057       mips_set_option_string (&mips_tune_string, "4010");
11058       break;
11059
11060     case OPTION_NO_M4010:
11061       break;
11062
11063     case OPTION_M4100:
11064       mips_set_option_string (&mips_arch_string, "4100");
11065       mips_set_option_string (&mips_tune_string, "4100");
11066       break;
11067
11068     case OPTION_NO_M4100:
11069       break;
11070
11071     case OPTION_M3900:
11072       mips_set_option_string (&mips_arch_string, "3900");
11073       mips_set_option_string (&mips_tune_string, "3900");
11074       break;
11075
11076     case OPTION_NO_M3900:
11077       break;
11078
11079     case OPTION_MDMX:
11080       mips_opts.ase_mdmx = 1;
11081       break;
11082
11083     case OPTION_NO_MDMX:
11084       mips_opts.ase_mdmx = 0;
11085       break;
11086
11087     case OPTION_DSP:
11088       mips_opts.ase_dsp = 1;
11089       break;
11090
11091     case OPTION_NO_DSP:
11092       mips_opts.ase_dsp = 0;
11093       break;
11094
11095     case OPTION_MT:
11096       mips_opts.ase_mt = 1;
11097       break;
11098
11099     case OPTION_NO_MT:
11100       mips_opts.ase_mt = 0;
11101       break;
11102
11103     case OPTION_MIPS16:
11104       mips_opts.mips16 = 1;
11105       mips_no_prev_insn ();
11106       break;
11107
11108     case OPTION_NO_MIPS16:
11109       mips_opts.mips16 = 0;
11110       mips_no_prev_insn ();
11111       break;
11112
11113     case OPTION_MIPS3D:
11114       mips_opts.ase_mips3d = 1;
11115       break;
11116
11117     case OPTION_NO_MIPS3D:
11118       mips_opts.ase_mips3d = 0;
11119       break;
11120
11121     case OPTION_SMARTMIPS:
11122       mips_opts.ase_smartmips = 1;
11123       break;
11124
11125     case OPTION_NO_SMARTMIPS:
11126       mips_opts.ase_smartmips = 0;
11127       break;
11128
11129     case OPTION_FIX_VR4120:
11130       mips_fix_vr4120 = 1;
11131       break;
11132
11133     case OPTION_NO_FIX_VR4120:
11134       mips_fix_vr4120 = 0;
11135       break;
11136
11137     case OPTION_FIX_VR4130:
11138       mips_fix_vr4130 = 1;
11139       break;
11140
11141     case OPTION_NO_FIX_VR4130:
11142       mips_fix_vr4130 = 0;
11143       break;
11144
11145     case OPTION_RELAX_BRANCH:
11146       mips_relax_branch = 1;
11147       break;
11148
11149     case OPTION_NO_RELAX_BRANCH:
11150       mips_relax_branch = 0;
11151       break;
11152
11153     case OPTION_MSHARED:
11154       mips_in_shared = TRUE;
11155       break;
11156
11157     case OPTION_MNO_SHARED:
11158       mips_in_shared = FALSE;
11159       break;
11160
11161     case OPTION_MSYM32:
11162       mips_opts.sym32 = TRUE;
11163       break;
11164
11165     case OPTION_MNO_SYM32:
11166       mips_opts.sym32 = FALSE;
11167       break;
11168
11169 #ifdef OBJ_ELF
11170       /* When generating ELF code, we permit -KPIC and -call_shared to
11171          select SVR4_PIC, and -non_shared to select no PIC.  This is
11172          intended to be compatible with Irix 5.  */
11173     case OPTION_CALL_SHARED:
11174       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11175         {
11176           as_bad (_("-call_shared is supported only for ELF format"));
11177           return 0;
11178         }
11179       mips_pic = SVR4_PIC;
11180       mips_abicalls = TRUE;
11181       break;
11182
11183     case OPTION_NON_SHARED:
11184       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11185         {
11186           as_bad (_("-non_shared is supported only for ELF format"));
11187           return 0;
11188         }
11189       mips_pic = NO_PIC;
11190       mips_abicalls = FALSE;
11191       break;
11192
11193       /* The -xgot option tells the assembler to use 32 bit offsets
11194          when accessing the got in SVR4_PIC mode.  It is for Irix
11195          compatibility.  */
11196     case OPTION_XGOT:
11197       mips_big_got = 1;
11198       break;
11199 #endif /* OBJ_ELF */
11200
11201     case 'G':
11202       g_switch_value = atoi (arg);
11203       g_switch_seen = 1;
11204       break;
11205
11206 #ifdef OBJ_ELF
11207       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11208          and -mabi=64.  */
11209     case OPTION_32:
11210       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11211         {
11212           as_bad (_("-32 is supported for ELF format only"));
11213           return 0;
11214         }
11215       mips_abi = O32_ABI;
11216       break;
11217
11218     case OPTION_N32:
11219       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11220         {
11221           as_bad (_("-n32 is supported for ELF format only"));
11222           return 0;
11223         }
11224       mips_abi = N32_ABI;
11225       break;
11226
11227     case OPTION_64:
11228       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11229         {
11230           as_bad (_("-64 is supported for ELF format only"));
11231           return 0;
11232         }
11233       mips_abi = N64_ABI;
11234       if (! support_64bit_objects())
11235         as_fatal (_("No compiled in support for 64 bit object file format"));
11236       break;
11237 #endif /* OBJ_ELF */
11238
11239     case OPTION_GP32:
11240       file_mips_gp32 = 1;
11241       break;
11242
11243     case OPTION_GP64:
11244       file_mips_gp32 = 0;
11245       break;
11246
11247     case OPTION_FP32:
11248       file_mips_fp32 = 1;
11249       break;
11250
11251     case OPTION_FP64:
11252       file_mips_fp32 = 0;
11253       break;
11254
11255 #ifdef OBJ_ELF
11256     case OPTION_MABI:
11257       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11258         {
11259           as_bad (_("-mabi is supported for ELF format only"));
11260           return 0;
11261         }
11262       if (strcmp (arg, "32") == 0)
11263         mips_abi = O32_ABI;
11264       else if (strcmp (arg, "o64") == 0)
11265         mips_abi = O64_ABI;
11266       else if (strcmp (arg, "n32") == 0)
11267         mips_abi = N32_ABI;
11268       else if (strcmp (arg, "64") == 0)
11269         {
11270           mips_abi = N64_ABI;
11271           if (! support_64bit_objects())
11272             as_fatal (_("No compiled in support for 64 bit object file "
11273                         "format"));
11274         }
11275       else if (strcmp (arg, "eabi") == 0)
11276         mips_abi = EABI_ABI;
11277       else
11278         {
11279           as_fatal (_("invalid abi -mabi=%s"), arg);
11280           return 0;
11281         }
11282       break;
11283 #endif /* OBJ_ELF */
11284
11285     case OPTION_M7000_HILO_FIX:
11286       mips_7000_hilo_fix = TRUE;
11287       break;
11288
11289     case OPTION_MNO_7000_HILO_FIX:
11290       mips_7000_hilo_fix = FALSE;
11291       break;
11292
11293 #ifdef OBJ_ELF
11294     case OPTION_MDEBUG:
11295       mips_flag_mdebug = TRUE;
11296       break;
11297
11298     case OPTION_NO_MDEBUG:
11299       mips_flag_mdebug = FALSE;
11300       break;
11301
11302     case OPTION_PDR:
11303       mips_flag_pdr = TRUE;
11304       break;
11305
11306     case OPTION_NO_PDR:
11307       mips_flag_pdr = FALSE;
11308       break;
11309
11310     case OPTION_MVXWORKS_PIC:
11311       mips_pic = VXWORKS_PIC;
11312       break;
11313 #endif /* OBJ_ELF */
11314
11315     default:
11316       return 0;
11317     }
11318
11319   return 1;
11320 }
11321 \f
11322 /* Set up globals to generate code for the ISA or processor
11323    described by INFO.  */
11324
11325 static void
11326 mips_set_architecture (const struct mips_cpu_info *info)
11327 {
11328   if (info != 0)
11329     {
11330       file_mips_arch = info->cpu;
11331       mips_opts.arch = info->cpu;
11332       mips_opts.isa = info->isa;
11333     }
11334 }
11335
11336
11337 /* Likewise for tuning.  */
11338
11339 static void
11340 mips_set_tune (const struct mips_cpu_info *info)
11341 {
11342   if (info != 0)
11343     mips_tune = info->cpu;
11344 }
11345
11346
11347 void
11348 mips_after_parse_args (void)
11349 {
11350   const struct mips_cpu_info *arch_info = 0;
11351   const struct mips_cpu_info *tune_info = 0;
11352
11353   /* GP relative stuff not working for PE */
11354   if (strncmp (TARGET_OS, "pe", 2) == 0)
11355     {
11356       if (g_switch_seen && g_switch_value != 0)
11357         as_bad (_("-G not supported in this configuration."));
11358       g_switch_value = 0;
11359     }
11360
11361   if (mips_abi == NO_ABI)
11362     mips_abi = MIPS_DEFAULT_ABI;
11363
11364   /* The following code determines the architecture and register size.
11365      Similar code was added to GCC 3.3 (see override_options() in
11366      config/mips/mips.c).  The GAS and GCC code should be kept in sync
11367      as much as possible.  */
11368
11369   if (mips_arch_string != 0)
11370     arch_info = mips_parse_cpu ("-march", mips_arch_string);
11371
11372   if (file_mips_isa != ISA_UNKNOWN)
11373     {
11374       /* Handle -mipsN.  At this point, file_mips_isa contains the
11375          ISA level specified by -mipsN, while arch_info->isa contains
11376          the -march selection (if any).  */
11377       if (arch_info != 0)
11378         {
11379           /* -march takes precedence over -mipsN, since it is more descriptive.
11380              There's no harm in specifying both as long as the ISA levels
11381              are the same.  */
11382           if (file_mips_isa != arch_info->isa)
11383             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11384                     mips_cpu_info_from_isa (file_mips_isa)->name,
11385                     mips_cpu_info_from_isa (arch_info->isa)->name);
11386         }
11387       else
11388         arch_info = mips_cpu_info_from_isa (file_mips_isa);
11389     }
11390
11391   if (arch_info == 0)
11392     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11393
11394   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11395     as_bad ("-march=%s is not compatible with the selected ABI",
11396             arch_info->name);
11397
11398   mips_set_architecture (arch_info);
11399
11400   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
11401   if (mips_tune_string != 0)
11402     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11403
11404   if (tune_info == 0)
11405     mips_set_tune (arch_info);
11406   else
11407     mips_set_tune (tune_info);
11408
11409   if (file_mips_gp32 >= 0)
11410     {
11411       /* The user specified the size of the integer registers.  Make sure
11412          it agrees with the ABI and ISA.  */
11413       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11414         as_bad (_("-mgp64 used with a 32-bit processor"));
11415       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11416         as_bad (_("-mgp32 used with a 64-bit ABI"));
11417       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11418         as_bad (_("-mgp64 used with a 32-bit ABI"));
11419     }
11420   else
11421     {
11422       /* Infer the integer register size from the ABI and processor.
11423          Restrict ourselves to 32-bit registers if that's all the
11424          processor has, or if the ABI cannot handle 64-bit registers.  */
11425       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11426                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11427     }
11428
11429   switch (file_mips_fp32)
11430     {
11431     default:
11432     case -1:
11433       /* No user specified float register size.
11434          ??? GAS treats single-float processors as though they had 64-bit
11435          float registers (although it complains when double-precision
11436          instructions are used).  As things stand, saying they have 32-bit
11437          registers would lead to spurious "register must be even" messages.
11438          So here we assume float registers are never smaller than the
11439          integer ones.  */
11440       if (file_mips_gp32 == 0)
11441         /* 64-bit integer registers implies 64-bit float registers.  */
11442         file_mips_fp32 = 0;
11443       else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11444                && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11445         /* -mips3d and -mdmx imply 64-bit float registers, if possible.  */
11446         file_mips_fp32 = 0;
11447       else
11448         /* 32-bit float registers.  */
11449         file_mips_fp32 = 1;
11450       break;
11451
11452     /* The user specified the size of the float registers.  Check if it
11453        agrees with the ABI and ISA.  */
11454     case 0:
11455       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11456         as_bad (_("-mfp64 used with a 32-bit fpu"));
11457       else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11458                && !ISA_HAS_MXHC1 (mips_opts.isa))
11459         as_warn (_("-mfp64 used with a 32-bit ABI"));
11460       break;
11461     case 1:
11462       if (ABI_NEEDS_64BIT_REGS (mips_abi))
11463         as_warn (_("-mfp32 used with a 64-bit ABI"));
11464       break;
11465     }
11466
11467   /* End of GCC-shared inference code.  */
11468
11469   /* This flag is set when we have a 64-bit capable CPU but use only
11470      32-bit wide registers.  Note that EABI does not use it.  */
11471   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11472       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11473           || mips_abi == O32_ABI))
11474     mips_32bitmode = 1;
11475
11476   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11477     as_bad (_("trap exception not supported at ISA 1"));
11478
11479   /* If the selected architecture includes support for ASEs, enable
11480      generation of code for them.  */
11481   if (mips_opts.mips16 == -1)
11482     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11483   if (mips_opts.ase_mips3d == -1)
11484     mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
11485                             && file_mips_fp32 == 0) ? 1 : 0;
11486   if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11487     as_bad (_("-mfp32 used with -mips3d"));
11488
11489   if (mips_opts.ase_mdmx == -1)
11490     mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
11491                           && file_mips_fp32 == 0) ? 1 : 0;
11492   if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11493     as_bad (_("-mfp32 used with -mdmx"));
11494
11495   if (mips_opts.ase_smartmips == -1)
11496     mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11497   if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
11498       as_warn ("%s ISA does not support SmartMIPS", 
11499                mips_cpu_info_from_isa (mips_opts.isa)->name);
11500
11501   if (mips_opts.ase_dsp == -1)
11502     mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11503   if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
11504       as_warn ("%s ISA does not support DSP ASE", 
11505                mips_cpu_info_from_isa (mips_opts.isa)->name);
11506
11507   if (mips_opts.ase_mt == -1)
11508     mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11509   if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
11510       as_warn ("%s ISA does not support MT ASE", 
11511                mips_cpu_info_from_isa (mips_opts.isa)->name);
11512
11513   file_mips_isa = mips_opts.isa;
11514   file_ase_mips16 = mips_opts.mips16;
11515   file_ase_mips3d = mips_opts.ase_mips3d;
11516   file_ase_mdmx = mips_opts.ase_mdmx;
11517   file_ase_smartmips = mips_opts.ase_smartmips;
11518   file_ase_dsp = mips_opts.ase_dsp;
11519   file_ase_mt = mips_opts.ase_mt;
11520   mips_opts.gp32 = file_mips_gp32;
11521   mips_opts.fp32 = file_mips_fp32;
11522
11523   if (mips_flag_mdebug < 0)
11524     {
11525 #ifdef OBJ_MAYBE_ECOFF
11526       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11527         mips_flag_mdebug = 1;
11528       else
11529 #endif /* OBJ_MAYBE_ECOFF */
11530         mips_flag_mdebug = 0;
11531     }
11532 }
11533 \f
11534 void
11535 mips_init_after_args (void)
11536 {
11537   /* initialize opcodes */
11538   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11539   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11540 }
11541
11542 long
11543 md_pcrel_from (fixS *fixP)
11544 {
11545   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11546   switch (fixP->fx_r_type)
11547     {
11548     case BFD_RELOC_16_PCREL_S2:
11549     case BFD_RELOC_MIPS_JMP:
11550       /* Return the address of the delay slot.  */
11551       return addr + 4;
11552     default:
11553       return addr;
11554     }
11555 }
11556
11557 /* This is called before the symbol table is processed.  In order to
11558    work with gcc when using mips-tfile, we must keep all local labels.
11559    However, in other cases, we want to discard them.  If we were
11560    called with -g, but we didn't see any debugging information, it may
11561    mean that gcc is smuggling debugging information through to
11562    mips-tfile, in which case we must generate all local labels.  */
11563
11564 void
11565 mips_frob_file_before_adjust (void)
11566 {
11567 #ifndef NO_ECOFF_DEBUGGING
11568   if (ECOFF_DEBUGGING
11569       && mips_debug != 0
11570       && ! ecoff_debugging_seen)
11571     flag_keep_locals = 1;
11572 #endif
11573 }
11574
11575 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
11576    the corresponding LO16 reloc.  This is called before md_apply_fix and
11577    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
11578    relocation operators.
11579
11580    For our purposes, a %lo() expression matches a %got() or %hi()
11581    expression if:
11582
11583       (a) it refers to the same symbol; and
11584       (b) the offset applied in the %lo() expression is no lower than
11585           the offset applied in the %got() or %hi().
11586
11587    (b) allows us to cope with code like:
11588
11589         lui     $4,%hi(foo)
11590         lh      $4,%lo(foo+2)($4)
11591
11592    ...which is legal on RELA targets, and has a well-defined behaviour
11593    if the user knows that adding 2 to "foo" will not induce a carry to
11594    the high 16 bits.
11595
11596    When several %lo()s match a particular %got() or %hi(), we use the
11597    following rules to distinguish them:
11598
11599      (1) %lo()s with smaller offsets are a better match than %lo()s with
11600          higher offsets.
11601
11602      (2) %lo()s with no matching %got() or %hi() are better than those
11603          that already have a matching %got() or %hi().
11604
11605      (3) later %lo()s are better than earlier %lo()s.
11606
11607    These rules are applied in order.
11608
11609    (1) means, among other things, that %lo()s with identical offsets are
11610    chosen if they exist.
11611
11612    (2) means that we won't associate several high-part relocations with
11613    the same low-part relocation unless there's no alternative.  Having
11614    several high parts for the same low part is a GNU extension; this rule
11615    allows careful users to avoid it.
11616
11617    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
11618    with the last high-part relocation being at the front of the list.
11619    It therefore makes sense to choose the last matching low-part
11620    relocation, all other things being equal.  It's also easier
11621    to code that way.  */
11622
11623 void
11624 mips_frob_file (void)
11625 {
11626   struct mips_hi_fixup *l;
11627
11628   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11629     {
11630       segment_info_type *seginfo;
11631       bfd_boolean matched_lo_p;
11632       fixS **hi_pos, **lo_pos, **pos;
11633
11634       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11635
11636       /* If a GOT16 relocation turns out to be against a global symbol,
11637          there isn't supposed to be a matching LO.  */
11638       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11639           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11640         continue;
11641
11642       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11643       if (fixup_has_matching_lo_p (l->fixp))
11644         continue;
11645
11646       seginfo = seg_info (l->seg);
11647
11648       /* Set HI_POS to the position of this relocation in the chain.
11649          Set LO_POS to the position of the chosen low-part relocation.
11650          MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11651          relocation that matches an immediately-preceding high-part
11652          relocation.  */
11653       hi_pos = NULL;
11654       lo_pos = NULL;
11655       matched_lo_p = FALSE;
11656       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11657         {
11658           if (*pos == l->fixp)
11659             hi_pos = pos;
11660
11661           if (((*pos)->fx_r_type == BFD_RELOC_LO16
11662                || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
11663               && (*pos)->fx_addsy == l->fixp->fx_addsy
11664               && (*pos)->fx_offset >= l->fixp->fx_offset
11665               && (lo_pos == NULL
11666                   || (*pos)->fx_offset < (*lo_pos)->fx_offset
11667                   || (!matched_lo_p
11668                       && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11669             lo_pos = pos;
11670
11671           matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11672                           && fixup_has_matching_lo_p (*pos));
11673         }
11674
11675       /* If we found a match, remove the high-part relocation from its
11676          current position and insert it before the low-part relocation.
11677          Make the offsets match so that fixup_has_matching_lo_p()
11678          will return true.
11679
11680          We don't warn about unmatched high-part relocations since some
11681          versions of gcc have been known to emit dead "lui ...%hi(...)"
11682          instructions.  */
11683       if (lo_pos != NULL)
11684         {
11685           l->fixp->fx_offset = (*lo_pos)->fx_offset;
11686           if (l->fixp->fx_next != *lo_pos)
11687             {
11688               *hi_pos = l->fixp->fx_next;
11689               l->fixp->fx_next = *lo_pos;
11690               *lo_pos = l->fixp;
11691             }
11692         }
11693     }
11694 }
11695
11696 /* We may have combined relocations without symbols in the N32/N64 ABI.
11697    We have to prevent gas from dropping them.  */
11698
11699 int
11700 mips_force_relocation (fixS *fixp)
11701 {
11702   if (generic_force_reloc (fixp))
11703     return 1;
11704
11705   if (HAVE_NEWABI
11706       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11707       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11708           || fixp->fx_r_type == BFD_RELOC_HI16_S
11709           || fixp->fx_r_type == BFD_RELOC_LO16))
11710     return 1;
11711
11712   return 0;
11713 }
11714
11715 /* Apply a fixup to the object file.  */
11716
11717 void
11718 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11719 {
11720   bfd_byte *buf;
11721   long insn;
11722   reloc_howto_type *howto;
11723
11724   /* We ignore generic BFD relocations we don't know about.  */
11725   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11726   if (! howto)
11727     return;
11728
11729   assert (fixP->fx_size == 4
11730           || fixP->fx_r_type == BFD_RELOC_16
11731           || fixP->fx_r_type == BFD_RELOC_64
11732           || fixP->fx_r_type == BFD_RELOC_CTOR
11733           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11734           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11735           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11736
11737   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11738
11739   assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
11740
11741   /* Don't treat parts of a composite relocation as done.  There are two
11742      reasons for this:
11743
11744      (1) The second and third parts will be against 0 (RSS_UNDEF) but
11745          should nevertheless be emitted if the first part is.
11746
11747      (2) In normal usage, composite relocations are never assembly-time
11748          constants.  The easiest way of dealing with the pathological
11749          exceptions is to generate a relocation against STN_UNDEF and
11750          leave everything up to the linker.  */
11751   if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
11752     fixP->fx_done = 1;
11753
11754   switch (fixP->fx_r_type)
11755     {
11756     case BFD_RELOC_MIPS_TLS_GD:
11757     case BFD_RELOC_MIPS_TLS_LDM:
11758     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11759     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11760     case BFD_RELOC_MIPS_TLS_GOTTPREL:
11761     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11762     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11763       S_SET_THREAD_LOCAL (fixP->fx_addsy);
11764       /* fall through */
11765
11766     case BFD_RELOC_MIPS_JMP:
11767     case BFD_RELOC_MIPS_SHIFT5:
11768     case BFD_RELOC_MIPS_SHIFT6:
11769     case BFD_RELOC_MIPS_GOT_DISP:
11770     case BFD_RELOC_MIPS_GOT_PAGE:
11771     case BFD_RELOC_MIPS_GOT_OFST:
11772     case BFD_RELOC_MIPS_SUB:
11773     case BFD_RELOC_MIPS_INSERT_A:
11774     case BFD_RELOC_MIPS_INSERT_B:
11775     case BFD_RELOC_MIPS_DELETE:
11776     case BFD_RELOC_MIPS_HIGHEST:
11777     case BFD_RELOC_MIPS_HIGHER:
11778     case BFD_RELOC_MIPS_SCN_DISP:
11779     case BFD_RELOC_MIPS_REL16:
11780     case BFD_RELOC_MIPS_RELGOT:
11781     case BFD_RELOC_MIPS_JALR:
11782     case BFD_RELOC_HI16:
11783     case BFD_RELOC_HI16_S:
11784     case BFD_RELOC_GPREL16:
11785     case BFD_RELOC_MIPS_LITERAL:
11786     case BFD_RELOC_MIPS_CALL16:
11787     case BFD_RELOC_MIPS_GOT16:
11788     case BFD_RELOC_GPREL32:
11789     case BFD_RELOC_MIPS_GOT_HI16:
11790     case BFD_RELOC_MIPS_GOT_LO16:
11791     case BFD_RELOC_MIPS_CALL_HI16:
11792     case BFD_RELOC_MIPS_CALL_LO16:
11793     case BFD_RELOC_MIPS16_GPREL:
11794     case BFD_RELOC_MIPS16_HI16:
11795     case BFD_RELOC_MIPS16_HI16_S:
11796       /* Nothing needed to do. The value comes from the reloc entry */
11797       break;
11798
11799     case BFD_RELOC_MIPS16_JMP:
11800       /* We currently always generate a reloc against a symbol, which
11801          means that we don't want an addend even if the symbol is
11802          defined.  */
11803       *valP = 0;
11804       break;
11805
11806     case BFD_RELOC_64:
11807       /* This is handled like BFD_RELOC_32, but we output a sign
11808          extended value if we are only 32 bits.  */
11809       if (fixP->fx_done)
11810         {
11811           if (8 <= sizeof (valueT))
11812             md_number_to_chars ((char *) buf, *valP, 8);
11813           else
11814             {
11815               valueT hiv;
11816
11817               if ((*valP & 0x80000000) != 0)
11818                 hiv = 0xffffffff;
11819               else
11820                 hiv = 0;
11821               md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
11822                                   *valP, 4);
11823               md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
11824                                   hiv, 4);
11825             }
11826         }
11827       break;
11828
11829     case BFD_RELOC_RVA:
11830     case BFD_RELOC_32:
11831       /* If we are deleting this reloc entry, we must fill in the
11832          value now.  This can happen if we have a .word which is not
11833          resolved when it appears but is later defined.   */
11834       if (fixP->fx_done)
11835         md_number_to_chars ((char *) buf, *valP, 4);
11836       break;
11837
11838     case BFD_RELOC_16:
11839       /* If we are deleting this reloc entry, we must fill in the
11840          value now.  */
11841       if (fixP->fx_done)
11842         md_number_to_chars ((char *) buf, *valP, 2);
11843       break;
11844
11845     case BFD_RELOC_LO16:
11846     case BFD_RELOC_MIPS16_LO16:
11847       /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11848          may be safe to remove, but if so it's not obvious.  */
11849       /* When handling an embedded PIC switch statement, we can wind
11850          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11851       if (fixP->fx_done)
11852         {
11853           if (*valP + 0x8000 > 0xffff)
11854             as_bad_where (fixP->fx_file, fixP->fx_line,
11855                           _("relocation overflow"));
11856           if (target_big_endian)
11857             buf += 2;
11858           md_number_to_chars ((char *) buf, *valP, 2);
11859         }
11860       break;
11861
11862     case BFD_RELOC_16_PCREL_S2:
11863       if ((*valP & 0x3) != 0)
11864         as_bad_where (fixP->fx_file, fixP->fx_line,
11865                       _("Branch to misaligned address (%lx)"), (long) *valP);
11866
11867       /*
11868        * We need to save the bits in the instruction since fixup_segment()
11869        * might be deleting the relocation entry (i.e., a branch within
11870        * the current segment).
11871        */
11872       if (! fixP->fx_done)
11873         break;
11874
11875       /* update old instruction data */
11876       if (target_big_endian)
11877         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11878       else
11879         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11880
11881       if (*valP + 0x20000 <= 0x3ffff)
11882         {
11883           insn |= (*valP >> 2) & 0xffff;
11884           md_number_to_chars ((char *) buf, insn, 4);
11885         }
11886       else if (mips_pic == NO_PIC
11887                && fixP->fx_done
11888                && fixP->fx_frag->fr_address >= text_section->vma
11889                && (fixP->fx_frag->fr_address
11890                    < text_section->vma + bfd_get_section_size (text_section))
11891                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
11892                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
11893                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11894         {
11895           /* The branch offset is too large.  If this is an
11896              unconditional branch, and we are not generating PIC code,
11897              we can convert it to an absolute jump instruction.  */
11898           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
11899             insn = 0x0c000000;  /* jal */
11900           else
11901             insn = 0x08000000;  /* j */
11902           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11903           fixP->fx_done = 0;
11904           fixP->fx_addsy = section_symbol (text_section);
11905           *valP += md_pcrel_from (fixP);
11906           md_number_to_chars ((char *) buf, insn, 4);
11907         }
11908       else
11909         {
11910           /* If we got here, we have branch-relaxation disabled,
11911              and there's nothing we can do to fix this instruction
11912              without turning it into a longer sequence.  */
11913           as_bad_where (fixP->fx_file, fixP->fx_line,
11914                         _("Branch out of range"));
11915         }
11916       break;
11917
11918     case BFD_RELOC_VTABLE_INHERIT:
11919       fixP->fx_done = 0;
11920       if (fixP->fx_addsy
11921           && !S_IS_DEFINED (fixP->fx_addsy)
11922           && !S_IS_WEAK (fixP->fx_addsy))
11923         S_SET_WEAK (fixP->fx_addsy);
11924       break;
11925
11926     case BFD_RELOC_VTABLE_ENTRY:
11927       fixP->fx_done = 0;
11928       break;
11929
11930     default:
11931       internalError ();
11932     }
11933
11934   /* Remember value for tc_gen_reloc.  */
11935   fixP->fx_addnumber = *valP;
11936 }
11937
11938 static symbolS *
11939 get_symbol (void)
11940 {
11941   int c;
11942   char *name;
11943   symbolS *p;
11944
11945   name = input_line_pointer;
11946   c = get_symbol_end ();
11947   p = (symbolS *) symbol_find_or_make (name);
11948   *input_line_pointer = c;
11949   return p;
11950 }
11951
11952 /* Align the current frag to a given power of two.  The MIPS assembler
11953    also automatically adjusts any preceding label.  */
11954
11955 static void
11956 mips_align (int to, int fill, symbolS *label)
11957 {
11958   mips_emit_delays ();
11959   frag_align (to, fill, 0);
11960   record_alignment (now_seg, to);
11961   if (label != NULL)
11962     {
11963       assert (S_GET_SEGMENT (label) == now_seg);
11964       symbol_set_frag (label, frag_now);
11965       S_SET_VALUE (label, (valueT) frag_now_fix ());
11966     }
11967 }
11968
11969 /* Align to a given power of two.  .align 0 turns off the automatic
11970    alignment used by the data creating pseudo-ops.  */
11971
11972 static void
11973 s_align (int x ATTRIBUTE_UNUSED)
11974 {
11975   int temp;
11976   long temp_fill;
11977   long max_alignment = 15;
11978
11979   /*
11980
11981     o  Note that the assembler pulls down any immediately preceding label
11982        to the aligned address.
11983     o  It's not documented but auto alignment is reinstated by
11984        a .align pseudo instruction.
11985     o  Note also that after auto alignment is turned off the mips assembler
11986        issues an error on attempt to assemble an improperly aligned data item.
11987        We don't.
11988
11989     */
11990
11991   temp = get_absolute_expression ();
11992   if (temp > max_alignment)
11993     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11994   else if (temp < 0)
11995     {
11996       as_warn (_("Alignment negative: 0 assumed."));
11997       temp = 0;
11998     }
11999   if (*input_line_pointer == ',')
12000     {
12001       ++input_line_pointer;
12002       temp_fill = get_absolute_expression ();
12003     }
12004   else
12005     temp_fill = 0;
12006   if (temp)
12007     {
12008       segment_info_type *si = seg_info (now_seg);
12009       struct insn_label_list *l = si->label_list;
12010       /* Auto alignment should be switched on by next section change */
12011       auto_align = 1;
12012       mips_align (temp, (int) temp_fill, l != NULL ? l->label : NULL);
12013     }
12014   else
12015     {
12016       auto_align = 0;
12017     }
12018
12019   demand_empty_rest_of_line ();
12020 }
12021
12022 static void
12023 s_change_sec (int sec)
12024 {
12025   segT seg;
12026
12027 #ifdef OBJ_ELF
12028   /* The ELF backend needs to know that we are changing sections, so
12029      that .previous works correctly.  We could do something like check
12030      for an obj_section_change_hook macro, but that might be confusing
12031      as it would not be appropriate to use it in the section changing
12032      functions in read.c, since obj-elf.c intercepts those.  FIXME:
12033      This should be cleaner, somehow.  */
12034   obj_elf_section_change_hook ();
12035 #endif
12036
12037   mips_emit_delays ();
12038   switch (sec)
12039     {
12040     case 't':
12041       s_text (0);
12042       break;
12043     case 'd':
12044       s_data (0);
12045       break;
12046     case 'b':
12047       subseg_set (bss_section, (subsegT) get_absolute_expression ());
12048       demand_empty_rest_of_line ();
12049       break;
12050
12051     case 'r':
12052       seg = subseg_new (RDATA_SECTION_NAME,
12053                         (subsegT) get_absolute_expression ());
12054       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12055         {
12056           bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12057                                                   | SEC_READONLY | SEC_RELOC
12058                                                   | SEC_DATA));
12059           if (strcmp (TARGET_OS, "elf") != 0)
12060             record_alignment (seg, 4);
12061         }
12062       demand_empty_rest_of_line ();
12063       break;
12064
12065     case 's':
12066       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12067       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12068         {
12069           bfd_set_section_flags (stdoutput, seg,
12070                                  SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
12071           if (strcmp (TARGET_OS, "elf") != 0)
12072             record_alignment (seg, 4);
12073         }
12074       demand_empty_rest_of_line ();
12075       break;
12076     }
12077
12078   auto_align = 1;
12079 }
12080
12081 void
12082 s_change_section (int ignore ATTRIBUTE_UNUSED)
12083 {
12084 #ifdef OBJ_ELF
12085   char *section_name;
12086   char c;
12087   char next_c = 0;
12088   int section_type;
12089   int section_flag;
12090   int section_entry_size;
12091   int section_alignment;
12092
12093   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12094     return;
12095
12096   section_name = input_line_pointer;
12097   c = get_symbol_end ();
12098   if (c)
12099     next_c = *(input_line_pointer + 1);
12100
12101   /* Do we have .section Name<,"flags">?  */
12102   if (c != ',' || (c == ',' && next_c == '"'))
12103     {
12104       /* just after name is now '\0'.  */
12105       *input_line_pointer = c;
12106       input_line_pointer = section_name;
12107       obj_elf_section (ignore);
12108       return;
12109     }
12110   input_line_pointer++;
12111
12112   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
12113   if (c == ',')
12114     section_type = get_absolute_expression ();
12115   else
12116     section_type = 0;
12117   if (*input_line_pointer++ == ',')
12118     section_flag = get_absolute_expression ();
12119   else
12120     section_flag = 0;
12121   if (*input_line_pointer++ == ',')
12122     section_entry_size = get_absolute_expression ();
12123   else
12124     section_entry_size = 0;
12125   if (*input_line_pointer++ == ',')
12126     section_alignment = get_absolute_expression ();
12127   else
12128     section_alignment = 0;
12129
12130   section_name = xstrdup (section_name);
12131
12132   /* When using the generic form of .section (as implemented by obj-elf.c),
12133      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
12134      traditionally had to fall back on the more common @progbits instead.
12135
12136      There's nothing really harmful in this, since bfd will correct
12137      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
12138      means that, for backwards compatibility, the special_section entries
12139      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12140
12141      Even so, we shouldn't force users of the MIPS .section syntax to
12142      incorrectly label the sections as SHT_PROGBITS.  The best compromise
12143      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12144      generic type-checking code.  */
12145   if (section_type == SHT_MIPS_DWARF)
12146     section_type = SHT_PROGBITS;
12147
12148   obj_elf_change_section (section_name, section_type, section_flag,
12149                           section_entry_size, 0, 0, 0);
12150
12151   if (now_seg->name != section_name)
12152     free (section_name);
12153 #endif /* OBJ_ELF */
12154 }
12155
12156 void
12157 mips_enable_auto_align (void)
12158 {
12159   auto_align = 1;
12160 }
12161
12162 static void
12163 s_cons (int log_size)
12164 {
12165   segment_info_type *si = seg_info (now_seg);
12166   struct insn_label_list *l = si->label_list;
12167   symbolS *label;
12168
12169   label = l != NULL ? l->label : NULL;
12170   mips_emit_delays ();
12171   if (log_size > 0 && auto_align)
12172     mips_align (log_size, 0, label);
12173   mips_clear_insn_labels ();
12174   cons (1 << log_size);
12175 }
12176
12177 static void
12178 s_float_cons (int type)
12179 {
12180   segment_info_type *si = seg_info (now_seg);
12181   struct insn_label_list *l = si->label_list;
12182   symbolS *label;
12183
12184   label = l != NULL ? l->label : NULL;
12185
12186   mips_emit_delays ();
12187
12188   if (auto_align)
12189     {
12190       if (type == 'd')
12191         mips_align (3, 0, label);
12192       else
12193         mips_align (2, 0, label);
12194     }
12195
12196   mips_clear_insn_labels ();
12197
12198   float_cons (type);
12199 }
12200
12201 /* Handle .globl.  We need to override it because on Irix 5 you are
12202    permitted to say
12203        .globl foo .text
12204    where foo is an undefined symbol, to mean that foo should be
12205    considered to be the address of a function.  */
12206
12207 static void
12208 s_mips_globl (int x ATTRIBUTE_UNUSED)
12209 {
12210   char *name;
12211   int c;
12212   symbolS *symbolP;
12213   flagword flag;
12214
12215   do
12216     {
12217       name = input_line_pointer;
12218       c = get_symbol_end ();
12219       symbolP = symbol_find_or_make (name);
12220       S_SET_EXTERNAL (symbolP);
12221
12222       *input_line_pointer = c;
12223       SKIP_WHITESPACE ();
12224
12225       /* On Irix 5, every global symbol that is not explicitly labelled as
12226          being a function is apparently labelled as being an object.  */
12227       flag = BSF_OBJECT;
12228
12229       if (!is_end_of_line[(unsigned char) *input_line_pointer]
12230           && (*input_line_pointer != ','))
12231         {
12232           char *secname;
12233           asection *sec;
12234
12235           secname = input_line_pointer;
12236           c = get_symbol_end ();
12237           sec = bfd_get_section_by_name (stdoutput, secname);
12238           if (sec == NULL)
12239             as_bad (_("%s: no such section"), secname);
12240           *input_line_pointer = c;
12241
12242           if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12243             flag = BSF_FUNCTION;
12244         }
12245
12246       symbol_get_bfdsym (symbolP)->flags |= flag;
12247
12248       c = *input_line_pointer;
12249       if (c == ',')
12250         {
12251           input_line_pointer++;
12252           SKIP_WHITESPACE ();
12253           if (is_end_of_line[(unsigned char) *input_line_pointer])
12254             c = '\n';
12255         }
12256     }
12257   while (c == ',');
12258
12259   demand_empty_rest_of_line ();
12260 }
12261
12262 static void
12263 s_option (int x ATTRIBUTE_UNUSED)
12264 {
12265   char *opt;
12266   char c;
12267
12268   opt = input_line_pointer;
12269   c = get_symbol_end ();
12270
12271   if (*opt == 'O')
12272     {
12273       /* FIXME: What does this mean?  */
12274     }
12275   else if (strncmp (opt, "pic", 3) == 0)
12276     {
12277       int i;
12278
12279       i = atoi (opt + 3);
12280       if (i == 0)
12281         mips_pic = NO_PIC;
12282       else if (i == 2)
12283         {
12284         mips_pic = SVR4_PIC;
12285           mips_abicalls = TRUE;
12286         }
12287       else
12288         as_bad (_(".option pic%d not supported"), i);
12289
12290       if (mips_pic == SVR4_PIC)
12291         {
12292           if (g_switch_seen && g_switch_value != 0)
12293             as_warn (_("-G may not be used with SVR4 PIC code"));
12294           g_switch_value = 0;
12295           bfd_set_gp_size (stdoutput, 0);
12296         }
12297     }
12298   else
12299     as_warn (_("Unrecognized option \"%s\""), opt);
12300
12301   *input_line_pointer = c;
12302   demand_empty_rest_of_line ();
12303 }
12304
12305 /* This structure is used to hold a stack of .set values.  */
12306
12307 struct mips_option_stack
12308 {
12309   struct mips_option_stack *next;
12310   struct mips_set_options options;
12311 };
12312
12313 static struct mips_option_stack *mips_opts_stack;
12314
12315 /* Handle the .set pseudo-op.  */
12316
12317 static void
12318 s_mipsset (int x ATTRIBUTE_UNUSED)
12319 {
12320   char *name = input_line_pointer, ch;
12321
12322   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12323     ++input_line_pointer;
12324   ch = *input_line_pointer;
12325   *input_line_pointer = '\0';
12326
12327   if (strcmp (name, "reorder") == 0)
12328     {
12329       if (mips_opts.noreorder)
12330         end_noreorder ();
12331     }
12332   else if (strcmp (name, "noreorder") == 0)
12333     {
12334       if (!mips_opts.noreorder)
12335         start_noreorder ();
12336     }
12337   else if (strcmp (name, "at") == 0)
12338     {
12339       mips_opts.noat = 0;
12340     }
12341   else if (strcmp (name, "noat") == 0)
12342     {
12343       mips_opts.noat = 1;
12344     }
12345   else if (strcmp (name, "macro") == 0)
12346     {
12347       mips_opts.warn_about_macros = 0;
12348     }
12349   else if (strcmp (name, "nomacro") == 0)
12350     {
12351       if (mips_opts.noreorder == 0)
12352         as_bad (_("`noreorder' must be set before `nomacro'"));
12353       mips_opts.warn_about_macros = 1;
12354     }
12355   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12356     {
12357       mips_opts.nomove = 0;
12358     }
12359   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12360     {
12361       mips_opts.nomove = 1;
12362     }
12363   else if (strcmp (name, "bopt") == 0)
12364     {
12365       mips_opts.nobopt = 0;
12366     }
12367   else if (strcmp (name, "nobopt") == 0)
12368     {
12369       mips_opts.nobopt = 1;
12370     }
12371   else if (strcmp (name, "gp=default") == 0)
12372     mips_opts.gp32 = file_mips_gp32;
12373   else if (strcmp (name, "gp=32") == 0)
12374     mips_opts.gp32 = 1;
12375   else if (strcmp (name, "gp=64") == 0)
12376     {
12377       if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
12378         as_warn ("%s isa does not support 64-bit registers",
12379                  mips_cpu_info_from_isa (mips_opts.isa)->name);
12380       mips_opts.gp32 = 0;
12381     }
12382   else if (strcmp (name, "fp=default") == 0)
12383     mips_opts.fp32 = file_mips_fp32;
12384   else if (strcmp (name, "fp=32") == 0)
12385     mips_opts.fp32 = 1;
12386   else if (strcmp (name, "fp=64") == 0)
12387     {
12388       if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
12389         as_warn ("%s isa does not support 64-bit floating point registers",
12390                  mips_cpu_info_from_isa (mips_opts.isa)->name);
12391       mips_opts.fp32 = 0;
12392     }
12393   else if (strcmp (name, "mips16") == 0
12394            || strcmp (name, "MIPS-16") == 0)
12395     mips_opts.mips16 = 1;
12396   else if (strcmp (name, "nomips16") == 0
12397            || strcmp (name, "noMIPS-16") == 0)
12398     mips_opts.mips16 = 0;
12399   else if (strcmp (name, "smartmips") == 0)
12400     {
12401       if (!ISA_SUPPORTS_SMARTMIPS)
12402         as_warn ("%s ISA does not support SmartMIPS ASE", 
12403                  mips_cpu_info_from_isa (mips_opts.isa)->name);
12404       mips_opts.ase_smartmips = 1;
12405     }
12406   else if (strcmp (name, "nosmartmips") == 0)
12407     mips_opts.ase_smartmips = 0;
12408   else if (strcmp (name, "mips3d") == 0)
12409     mips_opts.ase_mips3d = 1;
12410   else if (strcmp (name, "nomips3d") == 0)
12411     mips_opts.ase_mips3d = 0;
12412   else if (strcmp (name, "mdmx") == 0)
12413     mips_opts.ase_mdmx = 1;
12414   else if (strcmp (name, "nomdmx") == 0)
12415     mips_opts.ase_mdmx = 0;
12416   else if (strcmp (name, "dsp") == 0)
12417     {
12418       if (!ISA_SUPPORTS_DSP_ASE)
12419         as_warn ("%s ISA does not support DSP ASE", 
12420                  mips_cpu_info_from_isa (mips_opts.isa)->name);
12421       mips_opts.ase_dsp = 1;
12422     }
12423   else if (strcmp (name, "nodsp") == 0)
12424     mips_opts.ase_dsp = 0;
12425   else if (strcmp (name, "mt") == 0)
12426     {
12427       if (!ISA_SUPPORTS_MT_ASE)
12428         as_warn ("%s ISA does not support MT ASE", 
12429                  mips_cpu_info_from_isa (mips_opts.isa)->name);
12430       mips_opts.ase_mt = 1;
12431     }
12432   else if (strcmp (name, "nomt") == 0)
12433     mips_opts.ase_mt = 0;
12434   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12435     {
12436       int reset = 0;
12437
12438       /* Permit the user to change the ISA and architecture on the fly.
12439          Needless to say, misuse can cause serious problems.  */
12440       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
12441         {
12442           reset = 1;
12443           mips_opts.isa = file_mips_isa;
12444           mips_opts.arch = file_mips_arch;
12445         }
12446       else if (strncmp (name, "arch=", 5) == 0)
12447         {
12448           const struct mips_cpu_info *p;
12449
12450           p = mips_parse_cpu("internal use", name + 5);
12451           if (!p)
12452             as_bad (_("unknown architecture %s"), name + 5);
12453           else
12454             {
12455               mips_opts.arch = p->cpu;
12456               mips_opts.isa = p->isa;
12457             }
12458         }
12459       else if (strncmp (name, "mips", 4) == 0)
12460         {
12461           const struct mips_cpu_info *p;
12462
12463           p = mips_parse_cpu("internal use", name);
12464           if (!p)
12465             as_bad (_("unknown ISA level %s"), name + 4);
12466           else
12467             {
12468               mips_opts.arch = p->cpu;
12469               mips_opts.isa = p->isa;
12470             }
12471         }
12472       else
12473         as_bad (_("unknown ISA or architecture %s"), name);
12474
12475       switch (mips_opts.isa)
12476         {
12477         case  0:
12478           break;
12479         case ISA_MIPS1:
12480         case ISA_MIPS2:
12481         case ISA_MIPS32:
12482         case ISA_MIPS32R2:
12483           mips_opts.gp32 = 1;
12484           mips_opts.fp32 = 1;
12485           break;
12486         case ISA_MIPS3:
12487         case ISA_MIPS4:
12488         case ISA_MIPS5:
12489         case ISA_MIPS64:
12490         case ISA_MIPS64R2:
12491           mips_opts.gp32 = 0;
12492           mips_opts.fp32 = 0;
12493           break;
12494         default:
12495           as_bad (_("unknown ISA level %s"), name + 4);
12496           break;
12497         }
12498       if (reset)
12499         {
12500           mips_opts.gp32 = file_mips_gp32;
12501           mips_opts.fp32 = file_mips_fp32;
12502         }
12503     }
12504   else if (strcmp (name, "autoextend") == 0)
12505     mips_opts.noautoextend = 0;
12506   else if (strcmp (name, "noautoextend") == 0)
12507     mips_opts.noautoextend = 1;
12508   else if (strcmp (name, "push") == 0)
12509     {
12510       struct mips_option_stack *s;
12511
12512       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12513       s->next = mips_opts_stack;
12514       s->options = mips_opts;
12515       mips_opts_stack = s;
12516     }
12517   else if (strcmp (name, "pop") == 0)
12518     {
12519       struct mips_option_stack *s;
12520
12521       s = mips_opts_stack;
12522       if (s == NULL)
12523         as_bad (_(".set pop with no .set push"));
12524       else
12525         {
12526           /* If we're changing the reorder mode we need to handle
12527              delay slots correctly.  */
12528           if (s->options.noreorder && ! mips_opts.noreorder)
12529             start_noreorder ();
12530           else if (! s->options.noreorder && mips_opts.noreorder)
12531             end_noreorder ();
12532
12533           mips_opts = s->options;
12534           mips_opts_stack = s->next;
12535           free (s);
12536         }
12537     }
12538   else if (strcmp (name, "sym32") == 0)
12539     mips_opts.sym32 = TRUE;
12540   else if (strcmp (name, "nosym32") == 0)
12541     mips_opts.sym32 = FALSE;
12542   else
12543     {
12544       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12545     }
12546   *input_line_pointer = ch;
12547   demand_empty_rest_of_line ();
12548 }
12549
12550 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12551    .option pic2.  It means to generate SVR4 PIC calls.  */
12552
12553 static void
12554 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12555 {
12556   mips_pic = SVR4_PIC;
12557   mips_abicalls = TRUE;
12558
12559   if (g_switch_seen && g_switch_value != 0)
12560     as_warn (_("-G may not be used with SVR4 PIC code"));
12561   g_switch_value = 0;
12562
12563   bfd_set_gp_size (stdoutput, 0);
12564   demand_empty_rest_of_line ();
12565 }
12566
12567 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12568    PIC code.  It sets the $gp register for the function based on the
12569    function address, which is in the register named in the argument.
12570    This uses a relocation against _gp_disp, which is handled specially
12571    by the linker.  The result is:
12572         lui     $gp,%hi(_gp_disp)
12573         addiu   $gp,$gp,%lo(_gp_disp)
12574         addu    $gp,$gp,.cpload argument
12575    The .cpload argument is normally $25 == $t9.
12576
12577    The -mno-shared option changes this to:
12578         lui     $gp,%hi(__gnu_local_gp)
12579         addiu   $gp,$gp,%lo(__gnu_local_gp)
12580    and the argument is ignored.  This saves an instruction, but the
12581    resulting code is not position independent; it uses an absolute
12582    address for __gnu_local_gp.  Thus code assembled with -mno-shared
12583    can go into an ordinary executable, but not into a shared library.  */
12584
12585 static void
12586 s_cpload (int ignore ATTRIBUTE_UNUSED)
12587 {
12588   expressionS ex;
12589   int reg;
12590   int in_shared;
12591
12592   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12593      .cpload is ignored.  */
12594   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12595     {
12596       s_ignore (0);
12597       return;
12598     }
12599
12600   /* .cpload should be in a .set noreorder section.  */
12601   if (mips_opts.noreorder == 0)
12602     as_warn (_(".cpload not in noreorder section"));
12603
12604   reg = tc_get_register (0);
12605
12606   /* If we need to produce a 64-bit address, we are better off using
12607      the default instruction sequence.  */
12608   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
12609
12610   ex.X_op = O_symbol;
12611   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12612                                          "__gnu_local_gp");
12613   ex.X_op_symbol = NULL;
12614   ex.X_add_number = 0;
12615
12616   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12617   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12618
12619   macro_start ();
12620   macro_build_lui (&ex, mips_gp_register);
12621   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12622                mips_gp_register, BFD_RELOC_LO16);
12623   if (in_shared)
12624     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12625                  mips_gp_register, reg);
12626   macro_end ();
12627
12628   demand_empty_rest_of_line ();
12629 }
12630
12631 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12632      .cpsetup $reg1, offset|$reg2, label
12633
12634    If offset is given, this results in:
12635      sd         $gp, offset($sp)
12636      lui        $gp, %hi(%neg(%gp_rel(label)))
12637      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12638      daddu      $gp, $gp, $reg1
12639
12640    If $reg2 is given, this results in:
12641      daddu      $reg2, $gp, $0
12642      lui        $gp, %hi(%neg(%gp_rel(label)))
12643      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12644      daddu      $gp, $gp, $reg1
12645    $reg1 is normally $25 == $t9.
12646
12647    The -mno-shared option replaces the last three instructions with
12648         lui     $gp,%hi(_gp)
12649         addiu   $gp,$gp,%lo(_gp)
12650    */
12651
12652 static void
12653 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12654 {
12655   expressionS ex_off;
12656   expressionS ex_sym;
12657   int reg1;
12658
12659   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12660      We also need NewABI support.  */
12661   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12662     {
12663       s_ignore (0);
12664       return;
12665     }
12666
12667   reg1 = tc_get_register (0);
12668   SKIP_WHITESPACE ();
12669   if (*input_line_pointer != ',')
12670     {
12671       as_bad (_("missing argument separator ',' for .cpsetup"));
12672       return;
12673     }
12674   else
12675     ++input_line_pointer;
12676   SKIP_WHITESPACE ();
12677   if (*input_line_pointer == '$')
12678     {
12679       mips_cpreturn_register = tc_get_register (0);
12680       mips_cpreturn_offset = -1;
12681     }
12682   else
12683     {
12684       mips_cpreturn_offset = get_absolute_expression ();
12685       mips_cpreturn_register = -1;
12686     }
12687   SKIP_WHITESPACE ();
12688   if (*input_line_pointer != ',')
12689     {
12690       as_bad (_("missing argument separator ',' for .cpsetup"));
12691       return;
12692     }
12693   else
12694     ++input_line_pointer;
12695   SKIP_WHITESPACE ();
12696   expression (&ex_sym);
12697
12698   macro_start ();
12699   if (mips_cpreturn_register == -1)
12700     {
12701       ex_off.X_op = O_constant;
12702       ex_off.X_add_symbol = NULL;
12703       ex_off.X_op_symbol = NULL;
12704       ex_off.X_add_number = mips_cpreturn_offset;
12705
12706       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
12707                    BFD_RELOC_LO16, SP);
12708     }
12709   else
12710     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
12711                  mips_gp_register, 0);
12712
12713   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
12714     {
12715       macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12716                    -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12717                    BFD_RELOC_HI16_S);
12718
12719       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12720                    mips_gp_register, -1, BFD_RELOC_GPREL16,
12721                    BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12722
12723       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12724                    mips_gp_register, reg1);
12725     }
12726   else
12727     {
12728       expressionS ex;
12729
12730       ex.X_op = O_symbol;
12731       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
12732       ex.X_op_symbol = NULL;
12733       ex.X_add_number = 0;
12734
12735       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12736       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12737
12738       macro_build_lui (&ex, mips_gp_register);
12739       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12740                    mips_gp_register, BFD_RELOC_LO16);
12741     }
12742
12743   macro_end ();
12744
12745   demand_empty_rest_of_line ();
12746 }
12747
12748 static void
12749 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12750 {
12751   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12752    .cplocal is ignored.  */
12753   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12754     {
12755       s_ignore (0);
12756       return;
12757     }
12758
12759   mips_gp_register = tc_get_register (0);
12760   demand_empty_rest_of_line ();
12761 }
12762
12763 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12764    offset from $sp.  The offset is remembered, and after making a PIC
12765    call $gp is restored from that location.  */
12766
12767 static void
12768 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12769 {
12770   expressionS ex;
12771
12772   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12773      .cprestore is ignored.  */
12774   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12775     {
12776       s_ignore (0);
12777       return;
12778     }
12779
12780   mips_cprestore_offset = get_absolute_expression ();
12781   mips_cprestore_valid = 1;
12782
12783   ex.X_op = O_constant;
12784   ex.X_add_symbol = NULL;
12785   ex.X_op_symbol = NULL;
12786   ex.X_add_number = mips_cprestore_offset;
12787
12788   macro_start ();
12789   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12790                                 SP, HAVE_64BIT_ADDRESSES);
12791   macro_end ();
12792
12793   demand_empty_rest_of_line ();
12794 }
12795
12796 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12797    was given in the preceding .cpsetup, it results in:
12798      ld         $gp, offset($sp)
12799
12800    If a register $reg2 was given there, it results in:
12801      daddu      $gp, $reg2, $0
12802  */
12803 static void
12804 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12805 {
12806   expressionS ex;
12807
12808   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12809      We also need NewABI support.  */
12810   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12811     {
12812       s_ignore (0);
12813       return;
12814     }
12815
12816   macro_start ();
12817   if (mips_cpreturn_register == -1)
12818     {
12819       ex.X_op = O_constant;
12820       ex.X_add_symbol = NULL;
12821       ex.X_op_symbol = NULL;
12822       ex.X_add_number = mips_cpreturn_offset;
12823
12824       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12825     }
12826   else
12827     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12828                  mips_cpreturn_register, 0);
12829   macro_end ();
12830
12831   demand_empty_rest_of_line ();
12832 }
12833
12834 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12835    code.  It sets the offset to use in gp_rel relocations.  */
12836
12837 static void
12838 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12839 {
12840   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12841      We also need NewABI support.  */
12842   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12843     {
12844       s_ignore (0);
12845       return;
12846     }
12847
12848   mips_gprel_offset = get_absolute_expression ();
12849
12850   demand_empty_rest_of_line ();
12851 }
12852
12853 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12854    code.  It generates a 32 bit GP relative reloc.  */
12855
12856 static void
12857 s_gpword (int ignore ATTRIBUTE_UNUSED)
12858 {
12859   segment_info_type *si;
12860   struct insn_label_list *l;
12861   symbolS *label;
12862   expressionS ex;
12863   char *p;
12864
12865   /* When not generating PIC code, this is treated as .word.  */
12866   if (mips_pic != SVR4_PIC)
12867     {
12868       s_cons (2);
12869       return;
12870     }
12871
12872   si = seg_info (now_seg);
12873   l = si->label_list;
12874   label = l != NULL ? l->label : NULL;
12875   mips_emit_delays ();
12876   if (auto_align)
12877     mips_align (2, 0, label);
12878   mips_clear_insn_labels ();
12879
12880   expression (&ex);
12881
12882   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12883     {
12884       as_bad (_("Unsupported use of .gpword"));
12885       ignore_rest_of_line ();
12886     }
12887
12888   p = frag_more (4);
12889   md_number_to_chars (p, 0, 4);
12890   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12891                BFD_RELOC_GPREL32);
12892
12893   demand_empty_rest_of_line ();
12894 }
12895
12896 static void
12897 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12898 {
12899   segment_info_type *si;
12900   struct insn_label_list *l;
12901   symbolS *label;
12902   expressionS ex;
12903   char *p;
12904
12905   /* When not generating PIC code, this is treated as .dword.  */
12906   if (mips_pic != SVR4_PIC)
12907     {
12908       s_cons (3);
12909       return;
12910     }
12911
12912   si = seg_info (now_seg);
12913   l = si->label_list;
12914   label = l != NULL ? l->label : NULL;
12915   mips_emit_delays ();
12916   if (auto_align)
12917     mips_align (3, 0, label);
12918   mips_clear_insn_labels ();
12919
12920   expression (&ex);
12921
12922   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12923     {
12924       as_bad (_("Unsupported use of .gpdword"));
12925       ignore_rest_of_line ();
12926     }
12927
12928   p = frag_more (8);
12929   md_number_to_chars (p, 0, 8);
12930   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12931                BFD_RELOC_GPREL32)->fx_tcbit = 1;
12932
12933   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12934   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12935            FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12936
12937   demand_empty_rest_of_line ();
12938 }
12939
12940 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12941    tables in SVR4 PIC code.  */
12942
12943 static void
12944 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12945 {
12946   int reg;
12947
12948   /* This is ignored when not generating SVR4 PIC code.  */
12949   if (mips_pic != SVR4_PIC)
12950     {
12951       s_ignore (0);
12952       return;
12953     }
12954
12955   /* Add $gp to the register named as an argument.  */
12956   macro_start ();
12957   reg = tc_get_register (0);
12958   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12959   macro_end ();
12960
12961   demand_empty_rest_of_line ();
12962 }
12963
12964 /* Handle the .insn pseudo-op.  This marks instruction labels in
12965    mips16 mode.  This permits the linker to handle them specially,
12966    such as generating jalx instructions when needed.  We also make
12967    them odd for the duration of the assembly, in order to generate the
12968    right sort of code.  We will make them even in the adjust_symtab
12969    routine, while leaving them marked.  This is convenient for the
12970    debugger and the disassembler.  The linker knows to make them odd
12971    again.  */
12972
12973 static void
12974 s_insn (int ignore ATTRIBUTE_UNUSED)
12975 {
12976   mips16_mark_labels ();
12977
12978   demand_empty_rest_of_line ();
12979 }
12980
12981 /* Handle a .stabn directive.  We need these in order to mark a label
12982    as being a mips16 text label correctly.  Sometimes the compiler
12983    will emit a label, followed by a .stabn, and then switch sections.
12984    If the label and .stabn are in mips16 mode, then the label is
12985    really a mips16 text label.  */
12986
12987 static void
12988 s_mips_stab (int type)
12989 {
12990   if (type == 'n')
12991     mips16_mark_labels ();
12992
12993   s_stab (type);
12994 }
12995
12996 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12997  */
12998
12999 static void
13000 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
13001 {
13002   char *name;
13003   int c;
13004   symbolS *symbolP;
13005   expressionS exp;
13006
13007   name = input_line_pointer;
13008   c = get_symbol_end ();
13009   symbolP = symbol_find_or_make (name);
13010   S_SET_WEAK (symbolP);
13011   *input_line_pointer = c;
13012
13013   SKIP_WHITESPACE ();
13014
13015   if (! is_end_of_line[(unsigned char) *input_line_pointer])
13016     {
13017       if (S_IS_DEFINED (symbolP))
13018         {
13019           as_bad ("ignoring attempt to redefine symbol %s",
13020                   S_GET_NAME (symbolP));
13021           ignore_rest_of_line ();
13022           return;
13023         }
13024
13025       if (*input_line_pointer == ',')
13026         {
13027           ++input_line_pointer;
13028           SKIP_WHITESPACE ();
13029         }
13030
13031       expression (&exp);
13032       if (exp.X_op != O_symbol)
13033         {
13034           as_bad ("bad .weakext directive");
13035           ignore_rest_of_line ();
13036           return;
13037         }
13038       symbol_set_value_expression (symbolP, &exp);
13039     }
13040
13041   demand_empty_rest_of_line ();
13042 }
13043
13044 /* Parse a register string into a number.  Called from the ECOFF code
13045    to parse .frame.  The argument is non-zero if this is the frame
13046    register, so that we can record it in mips_frame_reg.  */
13047
13048 int
13049 tc_get_register (int frame)
13050 {
13051   unsigned int reg;
13052
13053   SKIP_WHITESPACE ();
13054   if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13055     reg = 0;
13056   if (frame)
13057     {
13058       mips_frame_reg = reg != 0 ? reg : SP;
13059       mips_frame_reg_valid = 1;
13060       mips_cprestore_valid = 0;
13061     }
13062   return reg;
13063 }
13064
13065 valueT
13066 md_section_align (asection *seg, valueT addr)
13067 {
13068   int align = bfd_get_section_alignment (stdoutput, seg);
13069
13070 #ifdef OBJ_ELF
13071   /* We don't need to align ELF sections to the full alignment.
13072      However, Irix 5 may prefer that we align them at least to a 16
13073      byte boundary.  We don't bother to align the sections if we are
13074      targeted for an embedded system.  */
13075   if (strcmp (TARGET_OS, "elf") == 0)
13076     return addr;
13077   if (align > 4)
13078     align = 4;
13079 #endif
13080
13081   return ((addr + (1 << align) - 1) & (-1 << align));
13082 }
13083
13084 /* Utility routine, called from above as well.  If called while the
13085    input file is still being read, it's only an approximation.  (For
13086    example, a symbol may later become defined which appeared to be
13087    undefined earlier.)  */
13088
13089 static int
13090 nopic_need_relax (symbolS *sym, int before_relaxing)
13091 {
13092   if (sym == 0)
13093     return 0;
13094
13095   if (g_switch_value > 0)
13096     {
13097       const char *symname;
13098       int change;
13099
13100       /* Find out whether this symbol can be referenced off the $gp
13101          register.  It can be if it is smaller than the -G size or if
13102          it is in the .sdata or .sbss section.  Certain symbols can
13103          not be referenced off the $gp, although it appears as though
13104          they can.  */
13105       symname = S_GET_NAME (sym);
13106       if (symname != (const char *) NULL
13107           && (strcmp (symname, "eprol") == 0
13108               || strcmp (symname, "etext") == 0
13109               || strcmp (symname, "_gp") == 0
13110               || strcmp (symname, "edata") == 0
13111               || strcmp (symname, "_fbss") == 0
13112               || strcmp (symname, "_fdata") == 0
13113               || strcmp (symname, "_ftext") == 0
13114               || strcmp (symname, "end") == 0
13115               || strcmp (symname, "_gp_disp") == 0))
13116         change = 1;
13117       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13118                && (0
13119 #ifndef NO_ECOFF_DEBUGGING
13120                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
13121                        && (symbol_get_obj (sym)->ecoff_extern_size
13122                            <= g_switch_value))
13123 #endif
13124                    /* We must defer this decision until after the whole
13125                       file has been read, since there might be a .extern
13126                       after the first use of this symbol.  */
13127                    || (before_relaxing
13128 #ifndef NO_ECOFF_DEBUGGING
13129                        && symbol_get_obj (sym)->ecoff_extern_size == 0
13130 #endif
13131                        && S_GET_VALUE (sym) == 0)
13132                    || (S_GET_VALUE (sym) != 0
13133                        && S_GET_VALUE (sym) <= g_switch_value)))
13134         change = 0;
13135       else
13136         {
13137           const char *segname;
13138
13139           segname = segment_name (S_GET_SEGMENT (sym));
13140           assert (strcmp (segname, ".lit8") != 0
13141                   && strcmp (segname, ".lit4") != 0);
13142           change = (strcmp (segname, ".sdata") != 0
13143                     && strcmp (segname, ".sbss") != 0
13144                     && strncmp (segname, ".sdata.", 7) != 0
13145                     && strncmp (segname, ".sbss.", 6) != 0
13146                     && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
13147                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13148         }
13149       return change;
13150     }
13151   else
13152     /* We are not optimizing for the $gp register.  */
13153     return 1;
13154 }
13155
13156
13157 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
13158
13159 static bfd_boolean
13160 pic_need_relax (symbolS *sym, asection *segtype)
13161 {
13162   asection *symsec;
13163
13164   /* Handle the case of a symbol equated to another symbol.  */
13165   while (symbol_equated_reloc_p (sym))
13166     {
13167       symbolS *n;
13168
13169       /* It's possible to get a loop here in a badly written program.  */
13170       n = symbol_get_value_expression (sym)->X_add_symbol;
13171       if (n == sym)
13172         break;
13173       sym = n;
13174     }
13175
13176   symsec = S_GET_SEGMENT (sym);
13177
13178   /* This must duplicate the test in adjust_reloc_syms.  */
13179   return (symsec != &bfd_und_section
13180           && symsec != &bfd_abs_section
13181           && !bfd_is_com_section (symsec)
13182           && !s_is_linkonce (sym, segtype)
13183 #ifdef OBJ_ELF
13184           /* A global or weak symbol is treated as external.  */
13185           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13186               || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
13187 #endif
13188           );
13189 }
13190
13191
13192 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13193    extended opcode.  SEC is the section the frag is in.  */
13194
13195 static int
13196 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
13197 {
13198   int type;
13199   const struct mips16_immed_operand *op;
13200   offsetT val;
13201   int mintiny, maxtiny;
13202   segT symsec;
13203   fragS *sym_frag;
13204
13205   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13206     return 0;
13207   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13208     return 1;
13209
13210   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13211   op = mips16_immed_operands;
13212   while (op->type != type)
13213     {
13214       ++op;
13215       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13216     }
13217
13218   if (op->unsp)
13219     {
13220       if (type == '<' || type == '>' || type == '[' || type == ']')
13221         {
13222           mintiny = 1;
13223           maxtiny = 1 << op->nbits;
13224         }
13225       else
13226         {
13227           mintiny = 0;
13228           maxtiny = (1 << op->nbits) - 1;
13229         }
13230     }
13231   else
13232     {
13233       mintiny = - (1 << (op->nbits - 1));
13234       maxtiny = (1 << (op->nbits - 1)) - 1;
13235     }
13236
13237   sym_frag = symbol_get_frag (fragp->fr_symbol);
13238   val = S_GET_VALUE (fragp->fr_symbol);
13239   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13240
13241   if (op->pcrel)
13242     {
13243       addressT addr;
13244
13245       /* We won't have the section when we are called from
13246          mips_relax_frag.  However, we will always have been called
13247          from md_estimate_size_before_relax first.  If this is a
13248          branch to a different section, we mark it as such.  If SEC is
13249          NULL, and the frag is not marked, then it must be a branch to
13250          the same section.  */
13251       if (sec == NULL)
13252         {
13253           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13254             return 1;
13255         }
13256       else
13257         {
13258           /* Must have been called from md_estimate_size_before_relax.  */
13259           if (symsec != sec)
13260             {
13261               fragp->fr_subtype =
13262                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13263
13264               /* FIXME: We should support this, and let the linker
13265                  catch branches and loads that are out of range.  */
13266               as_bad_where (fragp->fr_file, fragp->fr_line,
13267                             _("unsupported PC relative reference to different section"));
13268
13269               return 1;
13270             }
13271           if (fragp != sym_frag && sym_frag->fr_address == 0)
13272             /* Assume non-extended on the first relaxation pass.
13273                The address we have calculated will be bogus if this is
13274                a forward branch to another frag, as the forward frag
13275                will have fr_address == 0.  */
13276             return 0;
13277         }
13278
13279       /* In this case, we know for sure that the symbol fragment is in
13280          the same section.  If the relax_marker of the symbol fragment
13281          differs from the relax_marker of this fragment, we have not
13282          yet adjusted the symbol fragment fr_address.  We want to add
13283          in STRETCH in order to get a better estimate of the address.
13284          This particularly matters because of the shift bits.  */
13285       if (stretch != 0
13286           && sym_frag->relax_marker != fragp->relax_marker)
13287         {
13288           fragS *f;
13289
13290           /* Adjust stretch for any alignment frag.  Note that if have
13291              been expanding the earlier code, the symbol may be
13292              defined in what appears to be an earlier frag.  FIXME:
13293              This doesn't handle the fr_subtype field, which specifies
13294              a maximum number of bytes to skip when doing an
13295              alignment.  */
13296           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13297             {
13298               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13299                 {
13300                   if (stretch < 0)
13301                     stretch = - ((- stretch)
13302                                  & ~ ((1 << (int) f->fr_offset) - 1));
13303                   else
13304                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13305                   if (stretch == 0)
13306                     break;
13307                 }
13308             }
13309           if (f != NULL)
13310             val += stretch;
13311         }
13312
13313       addr = fragp->fr_address + fragp->fr_fix;
13314
13315       /* The base address rules are complicated.  The base address of
13316          a branch is the following instruction.  The base address of a
13317          PC relative load or add is the instruction itself, but if it
13318          is in a delay slot (in which case it can not be extended) use
13319          the address of the instruction whose delay slot it is in.  */
13320       if (type == 'p' || type == 'q')
13321         {
13322           addr += 2;
13323
13324           /* If we are currently assuming that this frag should be
13325              extended, then, the current address is two bytes
13326              higher.  */
13327           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13328             addr += 2;
13329
13330           /* Ignore the low bit in the target, since it will be set
13331              for a text label.  */
13332           if ((val & 1) != 0)
13333             --val;
13334         }
13335       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13336         addr -= 4;
13337       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13338         addr -= 2;
13339
13340       val -= addr & ~ ((1 << op->shift) - 1);
13341
13342       /* Branch offsets have an implicit 0 in the lowest bit.  */
13343       if (type == 'p' || type == 'q')
13344         val /= 2;
13345
13346       /* If any of the shifted bits are set, we must use an extended
13347          opcode.  If the address depends on the size of this
13348          instruction, this can lead to a loop, so we arrange to always
13349          use an extended opcode.  We only check this when we are in
13350          the main relaxation loop, when SEC is NULL.  */
13351       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13352         {
13353           fragp->fr_subtype =
13354             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13355           return 1;
13356         }
13357
13358       /* If we are about to mark a frag as extended because the value
13359          is precisely maxtiny + 1, then there is a chance of an
13360          infinite loop as in the following code:
13361              la $4,foo
13362              .skip      1020
13363              .align     2
13364            foo:
13365          In this case when the la is extended, foo is 0x3fc bytes
13366          away, so the la can be shrunk, but then foo is 0x400 away, so
13367          the la must be extended.  To avoid this loop, we mark the
13368          frag as extended if it was small, and is about to become
13369          extended with a value of maxtiny + 1.  */
13370       if (val == ((maxtiny + 1) << op->shift)
13371           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13372           && sec == NULL)
13373         {
13374           fragp->fr_subtype =
13375             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13376           return 1;
13377         }
13378     }
13379   else if (symsec != absolute_section && sec != NULL)
13380     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13381
13382   if ((val & ((1 << op->shift) - 1)) != 0
13383       || val < (mintiny << op->shift)
13384       || val > (maxtiny << op->shift))
13385     return 1;
13386   else
13387     return 0;
13388 }
13389
13390 /* Compute the length of a branch sequence, and adjust the
13391    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13392    worst-case length is computed, with UPDATE being used to indicate
13393    whether an unconditional (-1), branch-likely (+1) or regular (0)
13394    branch is to be computed.  */
13395 static int
13396 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13397 {
13398   bfd_boolean toofar;
13399   int length;
13400
13401   if (fragp
13402       && S_IS_DEFINED (fragp->fr_symbol)
13403       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13404     {
13405       addressT addr;
13406       offsetT val;
13407
13408       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13409
13410       addr = fragp->fr_address + fragp->fr_fix + 4;
13411
13412       val -= addr;
13413
13414       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13415     }
13416   else if (fragp)
13417     /* If the symbol is not defined or it's in a different segment,
13418        assume the user knows what's going on and emit a short
13419        branch.  */
13420     toofar = FALSE;
13421   else
13422     toofar = TRUE;
13423
13424   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13425     fragp->fr_subtype
13426       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13427                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13428                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13429                              toofar);
13430
13431   length = 4;
13432   if (toofar)
13433     {
13434       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13435         length += 8;
13436
13437       if (mips_pic != NO_PIC)
13438         {
13439           /* Additional space for PIC loading of target address.  */
13440           length += 8;
13441           if (mips_opts.isa == ISA_MIPS1)
13442             /* Additional space for $at-stabilizing nop.  */
13443             length += 4;
13444         }
13445
13446       /* If branch is conditional.  */
13447       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13448         length += 8;
13449     }
13450
13451   return length;
13452 }
13453
13454 /* Estimate the size of a frag before relaxing.  Unless this is the
13455    mips16, we are not really relaxing here, and the final size is
13456    encoded in the subtype information.  For the mips16, we have to
13457    decide whether we are using an extended opcode or not.  */
13458
13459 int
13460 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13461 {
13462   int change;
13463
13464   if (RELAX_BRANCH_P (fragp->fr_subtype))
13465     {
13466
13467       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13468
13469       return fragp->fr_var;
13470     }
13471
13472   if (RELAX_MIPS16_P (fragp->fr_subtype))
13473     /* We don't want to modify the EXTENDED bit here; it might get us
13474        into infinite loops.  We change it only in mips_relax_frag().  */
13475     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13476
13477   if (mips_pic == NO_PIC)
13478     change = nopic_need_relax (fragp->fr_symbol, 0);
13479   else if (mips_pic == SVR4_PIC)
13480     change = pic_need_relax (fragp->fr_symbol, segtype);
13481   else if (mips_pic == VXWORKS_PIC)
13482     /* For vxworks, GOT16 relocations never have a corresponding LO16.  */
13483     change = 0;
13484   else
13485     abort ();
13486
13487   if (change)
13488     {
13489       fragp->fr_subtype |= RELAX_USE_SECOND;
13490       return -RELAX_FIRST (fragp->fr_subtype);
13491     }
13492   else
13493     return -RELAX_SECOND (fragp->fr_subtype);
13494 }
13495
13496 /* This is called to see whether a reloc against a defined symbol
13497    should be converted into a reloc against a section.  */
13498
13499 int
13500 mips_fix_adjustable (fixS *fixp)
13501 {
13502   /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13503      about the format of the offset in the .o file. */
13504   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13505     return 0;
13506
13507   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13508       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13509     return 0;
13510
13511   if (fixp->fx_addsy == NULL)
13512     return 1;
13513
13514   /* If symbol SYM is in a mergeable section, relocations of the form
13515      SYM + 0 can usually be made section-relative.  The mergeable data
13516      is then identified by the section offset rather than by the symbol.
13517
13518      However, if we're generating REL LO16 relocations, the offset is split
13519      between the LO16 and parterning high part relocation.  The linker will
13520      need to recalculate the complete offset in order to correctly identify
13521      the merge data.
13522
13523      The linker has traditionally not looked for the parterning high part
13524      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13525      placed anywhere.  Rather than break backwards compatibility by changing
13526      this, it seems better not to force the issue, and instead keep the
13527      original symbol.  This will work with either linker behavior.  */
13528   if ((fixp->fx_r_type == BFD_RELOC_LO16
13529        || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13530        || reloc_needs_lo_p (fixp->fx_r_type))
13531       && HAVE_IN_PLACE_ADDENDS
13532       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13533     return 0;
13534
13535 #ifdef OBJ_ELF
13536   /* Don't adjust relocations against mips16 symbols, so that the linker
13537      can find them if it needs to set up a stub.  */
13538   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13539       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13540       && fixp->fx_subsy == NULL)
13541     return 0;
13542 #endif
13543
13544   return 1;
13545 }
13546
13547 /* Translate internal representation of relocation info to BFD target
13548    format.  */
13549
13550 arelent **
13551 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13552 {
13553   static arelent *retval[4];
13554   arelent *reloc;
13555   bfd_reloc_code_real_type code;
13556
13557   memset (retval, 0, sizeof(retval));
13558   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13559   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13560   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13561   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13562
13563   if (fixp->fx_pcrel)
13564     {
13565       assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13566
13567       /* At this point, fx_addnumber is "symbol offset - pcrel address".
13568          Relocations want only the symbol offset.  */
13569       reloc->addend = fixp->fx_addnumber + reloc->address;
13570       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13571         {
13572           /* A gruesome hack which is a result of the gruesome gas
13573              reloc handling.  What's worse, for COFF (as opposed to
13574              ECOFF), we might need yet another copy of reloc->address.
13575              See bfd_install_relocation.  */
13576           reloc->addend += reloc->address;
13577         }
13578     }
13579   else
13580     reloc->addend = fixp->fx_addnumber;
13581
13582   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13583      entry to be used in the relocation's section offset.  */
13584   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13585     {
13586       reloc->address = reloc->addend;
13587       reloc->addend = 0;
13588     }
13589
13590   code = fixp->fx_r_type;
13591
13592   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13593   if (reloc->howto == NULL)
13594     {
13595       as_bad_where (fixp->fx_file, fixp->fx_line,
13596                     _("Can not represent %s relocation in this object file format"),
13597                     bfd_get_reloc_code_name (code));
13598       retval[0] = NULL;
13599     }
13600
13601   return retval;
13602 }
13603
13604 /* Relax a machine dependent frag.  This returns the amount by which
13605    the current size of the frag should change.  */
13606
13607 int
13608 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13609 {
13610   if (RELAX_BRANCH_P (fragp->fr_subtype))
13611     {
13612       offsetT old_var = fragp->fr_var;
13613
13614       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13615
13616       return fragp->fr_var - old_var;
13617     }
13618
13619   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13620     return 0;
13621
13622   if (mips16_extended_frag (fragp, NULL, stretch))
13623     {
13624       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13625         return 0;
13626       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13627       return 2;
13628     }
13629   else
13630     {
13631       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13632         return 0;
13633       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13634       return -2;
13635     }
13636
13637   return 0;
13638 }
13639
13640 /* Convert a machine dependent frag.  */
13641
13642 void
13643 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13644 {
13645   if (RELAX_BRANCH_P (fragp->fr_subtype))
13646     {
13647       bfd_byte *buf;
13648       unsigned long insn;
13649       expressionS exp;
13650       fixS *fixp;
13651
13652       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13653
13654       if (target_big_endian)
13655         insn = bfd_getb32 (buf);
13656       else
13657         insn = bfd_getl32 (buf);
13658
13659       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13660         {
13661           /* We generate a fixup instead of applying it right now
13662              because, if there are linker relaxations, we're going to
13663              need the relocations.  */
13664           exp.X_op = O_symbol;
13665           exp.X_add_symbol = fragp->fr_symbol;
13666           exp.X_add_number = fragp->fr_offset;
13667
13668           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13669                               4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
13670           fixp->fx_file = fragp->fr_file;
13671           fixp->fx_line = fragp->fr_line;
13672
13673           md_number_to_chars ((char *) buf, insn, 4);
13674           buf += 4;
13675         }
13676       else
13677         {
13678           int i;
13679
13680           as_warn_where (fragp->fr_file, fragp->fr_line,
13681                          _("relaxed out-of-range branch into a jump"));
13682
13683           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13684             goto uncond;
13685
13686           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13687             {
13688               /* Reverse the branch.  */
13689               switch ((insn >> 28) & 0xf)
13690                 {
13691                 case 4:
13692                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13693                      have the condition reversed by tweaking a single
13694                      bit, and their opcodes all have 0x4???????.  */
13695                   assert ((insn & 0xf1000000) == 0x41000000);
13696                   insn ^= 0x00010000;
13697                   break;
13698
13699                 case 0:
13700                   /* bltz       0x04000000      bgez    0x04010000
13701                      bltzal     0x04100000      bgezal  0x04110000 */
13702                   assert ((insn & 0xfc0e0000) == 0x04000000);
13703                   insn ^= 0x00010000;
13704                   break;
13705
13706                 case 1:
13707                   /* beq        0x10000000      bne     0x14000000
13708                      blez       0x18000000      bgtz    0x1c000000 */
13709                   insn ^= 0x04000000;
13710                   break;
13711
13712                 default:
13713                   abort ();
13714                 }
13715             }
13716
13717           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13718             {
13719               /* Clear the and-link bit.  */
13720               assert ((insn & 0xfc1c0000) == 0x04100000);
13721
13722               /* bltzal 0x04100000      bgezal  0x04110000
13723                 bltzall 0x04120000     bgezall  0x04130000 */
13724               insn &= ~0x00100000;
13725             }
13726
13727           /* Branch over the branch (if the branch was likely) or the
13728              full jump (not likely case).  Compute the offset from the
13729              current instruction to branch to.  */
13730           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13731             i = 16;
13732           else
13733             {
13734               /* How many bytes in instructions we've already emitted?  */
13735               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13736               /* How many bytes in instructions from here to the end?  */
13737               i = fragp->fr_var - i;
13738             }
13739           /* Convert to instruction count.  */
13740           i >>= 2;
13741           /* Branch counts from the next instruction.  */
13742           i--;
13743           insn |= i;
13744           /* Branch over the jump.  */
13745           md_number_to_chars ((char *) buf, insn, 4);
13746           buf += 4;
13747
13748           /* Nop */
13749           md_number_to_chars ((char *) buf, 0, 4);
13750           buf += 4;
13751
13752           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13753             {
13754               /* beql $0, $0, 2f */
13755               insn = 0x50000000;
13756               /* Compute the PC offset from the current instruction to
13757                  the end of the variable frag.  */
13758               /* How many bytes in instructions we've already emitted?  */
13759               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13760               /* How many bytes in instructions from here to the end?  */
13761               i = fragp->fr_var - i;
13762               /* Convert to instruction count.  */
13763               i >>= 2;
13764               /* Don't decrement i, because we want to branch over the
13765                  delay slot.  */
13766
13767               insn |= i;
13768               md_number_to_chars ((char *) buf, insn, 4);
13769               buf += 4;
13770
13771               md_number_to_chars ((char *) buf, 0, 4);
13772               buf += 4;
13773             }
13774
13775         uncond:
13776           if (mips_pic == NO_PIC)
13777             {
13778               /* j or jal.  */
13779               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13780                       ? 0x0c000000 : 0x08000000);
13781               exp.X_op = O_symbol;
13782               exp.X_add_symbol = fragp->fr_symbol;
13783               exp.X_add_number = fragp->fr_offset;
13784
13785               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13786                                   4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
13787               fixp->fx_file = fragp->fr_file;
13788               fixp->fx_line = fragp->fr_line;
13789
13790               md_number_to_chars ((char *) buf, insn, 4);
13791               buf += 4;
13792             }
13793           else
13794             {
13795               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13796               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13797               exp.X_op = O_symbol;
13798               exp.X_add_symbol = fragp->fr_symbol;
13799               exp.X_add_number = fragp->fr_offset;
13800
13801               if (fragp->fr_offset)
13802                 {
13803                   exp.X_add_symbol = make_expr_symbol (&exp);
13804                   exp.X_add_number = 0;
13805                 }
13806
13807               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13808                                   4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
13809               fixp->fx_file = fragp->fr_file;
13810               fixp->fx_line = fragp->fr_line;
13811
13812               md_number_to_chars ((char *) buf, insn, 4);
13813               buf += 4;
13814
13815               if (mips_opts.isa == ISA_MIPS1)
13816                 {
13817                   /* nop */
13818                   md_number_to_chars ((char *) buf, 0, 4);
13819                   buf += 4;
13820                 }
13821
13822               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13823               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13824
13825               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13826                                   4, &exp, FALSE, BFD_RELOC_LO16);
13827               fixp->fx_file = fragp->fr_file;
13828               fixp->fx_line = fragp->fr_line;
13829
13830               md_number_to_chars ((char *) buf, insn, 4);
13831               buf += 4;
13832
13833               /* j(al)r $at.  */
13834               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13835                 insn = 0x0020f809;
13836               else
13837                 insn = 0x00200008;
13838
13839               md_number_to_chars ((char *) buf, insn, 4);
13840               buf += 4;
13841             }
13842         }
13843
13844       assert (buf == (bfd_byte *)fragp->fr_literal
13845               + fragp->fr_fix + fragp->fr_var);
13846
13847       fragp->fr_fix += fragp->fr_var;
13848
13849       return;
13850     }
13851
13852   if (RELAX_MIPS16_P (fragp->fr_subtype))
13853     {
13854       int type;
13855       const struct mips16_immed_operand *op;
13856       bfd_boolean small, ext;
13857       offsetT val;
13858       bfd_byte *buf;
13859       unsigned long insn;
13860       bfd_boolean use_extend;
13861       unsigned short extend;
13862
13863       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13864       op = mips16_immed_operands;
13865       while (op->type != type)
13866         ++op;
13867
13868       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13869         {
13870           small = FALSE;
13871           ext = TRUE;
13872         }
13873       else
13874         {
13875           small = TRUE;
13876           ext = FALSE;
13877         }
13878
13879       resolve_symbol_value (fragp->fr_symbol);
13880       val = S_GET_VALUE (fragp->fr_symbol);
13881       if (op->pcrel)
13882         {
13883           addressT addr;
13884
13885           addr = fragp->fr_address + fragp->fr_fix;
13886
13887           /* The rules for the base address of a PC relative reloc are
13888              complicated; see mips16_extended_frag.  */
13889           if (type == 'p' || type == 'q')
13890             {
13891               addr += 2;
13892               if (ext)
13893                 addr += 2;
13894               /* Ignore the low bit in the target, since it will be
13895                  set for a text label.  */
13896               if ((val & 1) != 0)
13897                 --val;
13898             }
13899           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13900             addr -= 4;
13901           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13902             addr -= 2;
13903
13904           addr &= ~ (addressT) ((1 << op->shift) - 1);
13905           val -= addr;
13906
13907           /* Make sure the section winds up with the alignment we have
13908              assumed.  */
13909           if (op->shift > 0)
13910             record_alignment (asec, op->shift);
13911         }
13912
13913       if (ext
13914           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13915               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13916         as_warn_where (fragp->fr_file, fragp->fr_line,
13917                        _("extended instruction in delay slot"));
13918
13919       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13920
13921       if (target_big_endian)
13922         insn = bfd_getb16 (buf);
13923       else
13924         insn = bfd_getl16 (buf);
13925
13926       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13927                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13928                     small, ext, &insn, &use_extend, &extend);
13929
13930       if (use_extend)
13931         {
13932           md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13933           fragp->fr_fix += 2;
13934           buf += 2;
13935         }
13936
13937       md_number_to_chars ((char *) buf, insn, 2);
13938       fragp->fr_fix += 2;
13939       buf += 2;
13940     }
13941   else
13942     {
13943       int first, second;
13944       fixS *fixp;
13945
13946       first = RELAX_FIRST (fragp->fr_subtype);
13947       second = RELAX_SECOND (fragp->fr_subtype);
13948       fixp = (fixS *) fragp->fr_opcode;
13949
13950       /* Possibly emit a warning if we've chosen the longer option.  */
13951       if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13952           == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13953         {
13954           const char *msg = macro_warning (fragp->fr_subtype);
13955           if (msg != 0)
13956             as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13957         }
13958
13959       /* Go through all the fixups for the first sequence.  Disable them
13960          (by marking them as done) if we're going to use the second
13961          sequence instead.  */
13962       while (fixp
13963              && fixp->fx_frag == fragp
13964              && fixp->fx_where < fragp->fr_fix - second)
13965         {
13966           if (fragp->fr_subtype & RELAX_USE_SECOND)
13967             fixp->fx_done = 1;
13968           fixp = fixp->fx_next;
13969         }
13970
13971       /* Go through the fixups for the second sequence.  Disable them if
13972          we're going to use the first sequence, otherwise adjust their
13973          addresses to account for the relaxation.  */
13974       while (fixp && fixp->fx_frag == fragp)
13975         {
13976           if (fragp->fr_subtype & RELAX_USE_SECOND)
13977             fixp->fx_where -= first;
13978           else
13979             fixp->fx_done = 1;
13980           fixp = fixp->fx_next;
13981         }
13982
13983       /* Now modify the frag contents.  */
13984       if (fragp->fr_subtype & RELAX_USE_SECOND)
13985         {
13986           char *start;
13987
13988           start = fragp->fr_literal + fragp->fr_fix - first - second;
13989           memmove (start, start + first, second);
13990           fragp->fr_fix -= first;
13991         }
13992       else
13993         fragp->fr_fix -= second;
13994     }
13995 }
13996
13997 #ifdef OBJ_ELF
13998
13999 /* This function is called after the relocs have been generated.
14000    We've been storing mips16 text labels as odd.  Here we convert them
14001    back to even for the convenience of the debugger.  */
14002
14003 void
14004 mips_frob_file_after_relocs (void)
14005 {
14006   asymbol **syms;
14007   unsigned int count, i;
14008
14009   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14010     return;
14011
14012   syms = bfd_get_outsymbols (stdoutput);
14013   count = bfd_get_symcount (stdoutput);
14014   for (i = 0; i < count; i++, syms++)
14015     {
14016       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14017           && ((*syms)->value & 1) != 0)
14018         {
14019           (*syms)->value &= ~1;
14020           /* If the symbol has an odd size, it was probably computed
14021              incorrectly, so adjust that as well.  */
14022           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14023             ++elf_symbol (*syms)->internal_elf_sym.st_size;
14024         }
14025     }
14026 }
14027
14028 #endif
14029
14030 /* This function is called whenever a label is defined.  It is used
14031    when handling branch delays; if a branch has a label, we assume we
14032    can not move it.  */
14033
14034 void
14035 mips_define_label (symbolS *sym)
14036 {
14037   segment_info_type *si = seg_info (now_seg);
14038   struct insn_label_list *l;
14039
14040   if (free_insn_labels == NULL)
14041     l = (struct insn_label_list *) xmalloc (sizeof *l);
14042   else
14043     {
14044       l = free_insn_labels;
14045       free_insn_labels = l->next;
14046     }
14047
14048   l->label = sym;
14049   l->next = si->label_list;
14050   si->label_list = l;
14051
14052 #ifdef OBJ_ELF
14053   dwarf2_emit_label (sym);
14054 #endif
14055 }
14056 \f
14057 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14058
14059 /* Some special processing for a MIPS ELF file.  */
14060
14061 void
14062 mips_elf_final_processing (void)
14063 {
14064   /* Write out the register information.  */
14065   if (mips_abi != N64_ABI)
14066     {
14067       Elf32_RegInfo s;
14068
14069       s.ri_gprmask = mips_gprmask;
14070       s.ri_cprmask[0] = mips_cprmask[0];
14071       s.ri_cprmask[1] = mips_cprmask[1];
14072       s.ri_cprmask[2] = mips_cprmask[2];
14073       s.ri_cprmask[3] = mips_cprmask[3];
14074       /* The gp_value field is set by the MIPS ELF backend.  */
14075
14076       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14077                                        ((Elf32_External_RegInfo *)
14078                                         mips_regmask_frag));
14079     }
14080   else
14081     {
14082       Elf64_Internal_RegInfo s;
14083
14084       s.ri_gprmask = mips_gprmask;
14085       s.ri_pad = 0;
14086       s.ri_cprmask[0] = mips_cprmask[0];
14087       s.ri_cprmask[1] = mips_cprmask[1];
14088       s.ri_cprmask[2] = mips_cprmask[2];
14089       s.ri_cprmask[3] = mips_cprmask[3];
14090       /* The gp_value field is set by the MIPS ELF backend.  */
14091
14092       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14093                                        ((Elf64_External_RegInfo *)
14094                                         mips_regmask_frag));
14095     }
14096
14097   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14098      sort of BFD interface for this.  */
14099   if (mips_any_noreorder)
14100     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14101   if (mips_pic != NO_PIC)
14102     {
14103     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14104       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14105     }
14106   if (mips_abicalls)
14107     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14108
14109   /* Set MIPS ELF flags for ASEs.  */
14110   /* We may need to define a new flag for DSP ASE, and set this flag when
14111      file_ase_dsp is true.  */
14112   /* We may need to define a new flag for MT ASE, and set this flag when
14113      file_ase_mt is true.  */
14114   if (file_ase_mips16)
14115     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14116 #if 0 /* XXX FIXME */
14117   if (file_ase_mips3d)
14118     elf_elfheader (stdoutput)->e_flags |= ???;
14119 #endif
14120   if (file_ase_mdmx)
14121     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14122
14123   /* Set the MIPS ELF ABI flags.  */
14124   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14125     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14126   else if (mips_abi == O64_ABI)
14127     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14128   else if (mips_abi == EABI_ABI)
14129     {
14130       if (!file_mips_gp32)
14131         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14132       else
14133         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14134     }
14135   else if (mips_abi == N32_ABI)
14136     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14137
14138   /* Nothing to do for N64_ABI.  */
14139
14140   if (mips_32bitmode)
14141     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14142
14143 #if 0 /* XXX FIXME */
14144   /* 32 bit code with 64 bit FP registers.  */
14145   if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14146     elf_elfheader (stdoutput)->e_flags |= ???;
14147 #endif
14148 }
14149
14150 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14151 \f
14152 typedef struct proc {
14153   symbolS *func_sym;
14154   symbolS *func_end_sym;
14155   unsigned long reg_mask;
14156   unsigned long reg_offset;
14157   unsigned long fpreg_mask;
14158   unsigned long fpreg_offset;
14159   unsigned long frame_offset;
14160   unsigned long frame_reg;
14161   unsigned long pc_reg;
14162 } procS;
14163
14164 static procS cur_proc;
14165 static procS *cur_proc_ptr;
14166 static int numprocs;
14167
14168 /* Fill in an rs_align_code fragment.  */
14169
14170 void
14171 mips_handle_align (fragS *fragp)
14172 {
14173   if (fragp->fr_type != rs_align_code)
14174     return;
14175
14176   if (mips_opts.mips16)
14177     {
14178       static const unsigned char be_nop[] = { 0x65, 0x00 };
14179       static const unsigned char le_nop[] = { 0x00, 0x65 };
14180
14181       int bytes;
14182       char *p;
14183
14184       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14185       p = fragp->fr_literal + fragp->fr_fix;
14186
14187       if (bytes & 1)
14188         {
14189           *p++ = 0;
14190           fragp->fr_fix++;
14191         }
14192
14193       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14194       fragp->fr_var = 2;
14195     }
14196
14197   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14198 }
14199
14200 static void
14201 md_obj_begin (void)
14202 {
14203 }
14204
14205 static void
14206 md_obj_end (void)
14207 {
14208   /* check for premature end, nesting errors, etc */
14209   if (cur_proc_ptr)
14210     as_warn (_("missing .end at end of assembly"));
14211 }
14212
14213 static long
14214 get_number (void)
14215 {
14216   int negative = 0;
14217   long val = 0;
14218
14219   if (*input_line_pointer == '-')
14220     {
14221       ++input_line_pointer;
14222       negative = 1;
14223     }
14224   if (!ISDIGIT (*input_line_pointer))
14225     as_bad (_("expected simple number"));
14226   if (input_line_pointer[0] == '0')
14227     {
14228       if (input_line_pointer[1] == 'x')
14229         {
14230           input_line_pointer += 2;
14231           while (ISXDIGIT (*input_line_pointer))
14232             {
14233               val <<= 4;
14234               val |= hex_value (*input_line_pointer++);
14235             }
14236           return negative ? -val : val;
14237         }
14238       else
14239         {
14240           ++input_line_pointer;
14241           while (ISDIGIT (*input_line_pointer))
14242             {
14243               val <<= 3;
14244               val |= *input_line_pointer++ - '0';
14245             }
14246           return negative ? -val : val;
14247         }
14248     }
14249   if (!ISDIGIT (*input_line_pointer))
14250     {
14251       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14252               *input_line_pointer, *input_line_pointer);
14253       as_warn (_("invalid number"));
14254       return -1;
14255     }
14256   while (ISDIGIT (*input_line_pointer))
14257     {
14258       val *= 10;
14259       val += *input_line_pointer++ - '0';
14260     }
14261   return negative ? -val : val;
14262 }
14263
14264 /* The .file directive; just like the usual .file directive, but there
14265    is an initial number which is the ECOFF file index.  In the non-ECOFF
14266    case .file implies DWARF-2.  */
14267
14268 static void
14269 s_mips_file (int x ATTRIBUTE_UNUSED)
14270 {
14271   static int first_file_directive = 0;
14272
14273   if (ECOFF_DEBUGGING)
14274     {
14275       get_number ();
14276       s_app_file (0);
14277     }
14278   else
14279     {
14280       char *filename;
14281
14282       filename = dwarf2_directive_file (0);
14283
14284       /* Versions of GCC up to 3.1 start files with a ".file"
14285          directive even for stabs output.  Make sure that this
14286          ".file" is handled.  Note that you need a version of GCC
14287          after 3.1 in order to support DWARF-2 on MIPS.  */
14288       if (filename != NULL && ! first_file_directive)
14289         {
14290           (void) new_logical_line (filename, -1);
14291           s_app_file_string (filename, 0);
14292         }
14293       first_file_directive = 1;
14294     }
14295 }
14296
14297 /* The .loc directive, implying DWARF-2.  */
14298
14299 static void
14300 s_mips_loc (int x ATTRIBUTE_UNUSED)
14301 {
14302   if (!ECOFF_DEBUGGING)
14303     dwarf2_directive_loc (0);
14304 }
14305
14306 /* The .end directive.  */
14307
14308 static void
14309 s_mips_end (int x ATTRIBUTE_UNUSED)
14310 {
14311   symbolS *p;
14312
14313   /* Following functions need their own .frame and .cprestore directives.  */
14314   mips_frame_reg_valid = 0;
14315   mips_cprestore_valid = 0;
14316
14317   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14318     {
14319       p = get_symbol ();
14320       demand_empty_rest_of_line ();
14321     }
14322   else
14323     p = NULL;
14324
14325   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14326     as_warn (_(".end not in text section"));
14327
14328   if (!cur_proc_ptr)
14329     {
14330       as_warn (_(".end directive without a preceding .ent directive."));
14331       demand_empty_rest_of_line ();
14332       return;
14333     }
14334
14335   if (p != NULL)
14336     {
14337       assert (S_GET_NAME (p));
14338       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
14339         as_warn (_(".end symbol does not match .ent symbol."));
14340
14341       if (debug_type == DEBUG_STABS)
14342         stabs_generate_asm_endfunc (S_GET_NAME (p),
14343                                     S_GET_NAME (p));
14344     }
14345   else
14346     as_warn (_(".end directive missing or unknown symbol"));
14347
14348 #ifdef OBJ_ELF
14349   /* Create an expression to calculate the size of the function.  */
14350   if (p && cur_proc_ptr)
14351     {
14352       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14353       expressionS *exp = xmalloc (sizeof (expressionS));
14354
14355       obj->size = exp;
14356       exp->X_op = O_subtract;
14357       exp->X_add_symbol = symbol_temp_new_now ();
14358       exp->X_op_symbol = p;
14359       exp->X_add_number = 0;
14360
14361       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14362     }
14363
14364   /* Generate a .pdr section.  */
14365   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14366       && mips_flag_pdr)
14367     {
14368       segT saved_seg = now_seg;
14369       subsegT saved_subseg = now_subseg;
14370       valueT dot;
14371       expressionS exp;
14372       char *fragp;
14373
14374       dot = frag_now_fix ();
14375
14376 #ifdef md_flush_pending_output
14377       md_flush_pending_output ();
14378 #endif
14379
14380       assert (pdr_seg);
14381       subseg_set (pdr_seg, 0);
14382
14383       /* Write the symbol.  */
14384       exp.X_op = O_symbol;
14385       exp.X_add_symbol = p;
14386       exp.X_add_number = 0;
14387       emit_expr (&exp, 4);
14388
14389       fragp = frag_more (7 * 4);
14390
14391       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14392       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14393       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14394       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14395       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14396       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14397       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14398
14399       subseg_set (saved_seg, saved_subseg);
14400     }
14401 #endif /* OBJ_ELF */
14402
14403   cur_proc_ptr = NULL;
14404 }
14405
14406 /* The .aent and .ent directives.  */
14407
14408 static void
14409 s_mips_ent (int aent)
14410 {
14411   symbolS *symbolP;
14412
14413   symbolP = get_symbol ();
14414   if (*input_line_pointer == ',')
14415     ++input_line_pointer;
14416   SKIP_WHITESPACE ();
14417   if (ISDIGIT (*input_line_pointer)
14418       || *input_line_pointer == '-')
14419     get_number ();
14420
14421   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14422     as_warn (_(".ent or .aent not in text section."));
14423
14424   if (!aent && cur_proc_ptr)
14425     as_warn (_("missing .end"));
14426
14427   if (!aent)
14428     {
14429       /* This function needs its own .frame and .cprestore directives.  */
14430       mips_frame_reg_valid = 0;
14431       mips_cprestore_valid = 0;
14432
14433       cur_proc_ptr = &cur_proc;
14434       memset (cur_proc_ptr, '\0', sizeof (procS));
14435
14436       cur_proc_ptr->func_sym = symbolP;
14437
14438       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14439
14440       ++numprocs;
14441
14442       if (debug_type == DEBUG_STABS)
14443         stabs_generate_asm_func (S_GET_NAME (symbolP),
14444                                  S_GET_NAME (symbolP));
14445     }
14446
14447   demand_empty_rest_of_line ();
14448 }
14449
14450 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14451    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14452    s_mips_frame is used so that we can set the PDR information correctly.
14453    We can't use the ecoff routines because they make reference to the ecoff
14454    symbol table (in the mdebug section).  */
14455
14456 static void
14457 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14458 {
14459 #ifdef OBJ_ELF
14460   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14461     {
14462       long val;
14463
14464       if (cur_proc_ptr == (procS *) NULL)
14465         {
14466           as_warn (_(".frame outside of .ent"));
14467           demand_empty_rest_of_line ();
14468           return;
14469         }
14470
14471       cur_proc_ptr->frame_reg = tc_get_register (1);
14472
14473       SKIP_WHITESPACE ();
14474       if (*input_line_pointer++ != ','
14475           || get_absolute_expression_and_terminator (&val) != ',')
14476         {
14477           as_warn (_("Bad .frame directive"));
14478           --input_line_pointer;
14479           demand_empty_rest_of_line ();
14480           return;
14481         }
14482
14483       cur_proc_ptr->frame_offset = val;
14484       cur_proc_ptr->pc_reg = tc_get_register (0);
14485
14486       demand_empty_rest_of_line ();
14487     }
14488   else
14489 #endif /* OBJ_ELF */
14490     s_ignore (ignore);
14491 }
14492
14493 /* The .fmask and .mask directives. If the mdebug section is present
14494    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14495    embedded targets, s_mips_mask is used so that we can set the PDR
14496    information correctly. We can't use the ecoff routines because they
14497    make reference to the ecoff symbol table (in the mdebug section).  */
14498
14499 static void
14500 s_mips_mask (int reg_type)
14501 {
14502 #ifdef OBJ_ELF
14503   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14504     {
14505       long mask, off;
14506
14507       if (cur_proc_ptr == (procS *) NULL)
14508         {
14509           as_warn (_(".mask/.fmask outside of .ent"));
14510           demand_empty_rest_of_line ();
14511           return;
14512         }
14513
14514       if (get_absolute_expression_and_terminator (&mask) != ',')
14515         {
14516           as_warn (_("Bad .mask/.fmask directive"));
14517           --input_line_pointer;
14518           demand_empty_rest_of_line ();
14519           return;
14520         }
14521
14522       off = get_absolute_expression ();
14523
14524       if (reg_type == 'F')
14525         {
14526           cur_proc_ptr->fpreg_mask = mask;
14527           cur_proc_ptr->fpreg_offset = off;
14528         }
14529       else
14530         {
14531           cur_proc_ptr->reg_mask = mask;
14532           cur_proc_ptr->reg_offset = off;
14533         }
14534
14535       demand_empty_rest_of_line ();
14536     }
14537   else
14538 #endif /* OBJ_ELF */
14539     s_ignore (reg_type);
14540 }
14541
14542 /* A table describing all the processors gas knows about.  Names are
14543    matched in the order listed.
14544
14545    To ease comparison, please keep this table in the same order as
14546    gcc's mips_cpu_info_table[].  */
14547 static const struct mips_cpu_info mips_cpu_info_table[] =
14548 {
14549   /* Entries for generic ISAs */
14550   { "mips1",          MIPS_CPU_IS_ISA,          ISA_MIPS1,      CPU_R3000 },
14551   { "mips2",          MIPS_CPU_IS_ISA,          ISA_MIPS2,      CPU_R6000 },
14552   { "mips3",          MIPS_CPU_IS_ISA,          ISA_MIPS3,      CPU_R4000 },
14553   { "mips4",          MIPS_CPU_IS_ISA,          ISA_MIPS4,      CPU_R8000 },
14554   { "mips5",          MIPS_CPU_IS_ISA,          ISA_MIPS5,      CPU_MIPS5 },
14555   { "mips32",         MIPS_CPU_IS_ISA,          ISA_MIPS32,     CPU_MIPS32 },
14556   { "mips32r2",       MIPS_CPU_IS_ISA,          ISA_MIPS32R2,   CPU_MIPS32R2 },
14557   { "mips64",         MIPS_CPU_IS_ISA,          ISA_MIPS64,     CPU_MIPS64 },
14558   { "mips64r2",       MIPS_CPU_IS_ISA,          ISA_MIPS64R2,   CPU_MIPS64R2 },
14559
14560   /* MIPS I */
14561   { "r3000",          0,                        ISA_MIPS1,      CPU_R3000 },
14562   { "r2000",          0,                        ISA_MIPS1,      CPU_R3000 },
14563   { "r3900",          0,                        ISA_MIPS1,      CPU_R3900 },
14564
14565   /* MIPS II */
14566   { "r6000",          0,                        ISA_MIPS2,      CPU_R6000 },
14567
14568   /* MIPS III */
14569   { "r4000",          0,                        ISA_MIPS3,      CPU_R4000 },
14570   { "r4010",          0,                        ISA_MIPS2,      CPU_R4010 },
14571   { "vr4100",         0,                        ISA_MIPS3,      CPU_VR4100 },
14572   { "vr4111",         0,                        ISA_MIPS3,      CPU_R4111 },
14573   { "vr4120",         0,                        ISA_MIPS3,      CPU_VR4120 },
14574   { "vr4130",         0,                        ISA_MIPS3,      CPU_VR4120 },
14575   { "vr4181",         0,                        ISA_MIPS3,      CPU_R4111 },
14576   { "vr4300",         0,                        ISA_MIPS3,      CPU_R4300 },
14577   { "r4400",          0,                        ISA_MIPS3,      CPU_R4400 },
14578   { "r4600",          0,                        ISA_MIPS3,      CPU_R4600 },
14579   { "orion",          0,                        ISA_MIPS3,      CPU_R4600 },
14580   { "r4650",          0,                        ISA_MIPS3,      CPU_R4650 },
14581
14582   /* MIPS IV */
14583   { "r8000",          0,                        ISA_MIPS4,      CPU_R8000 },
14584   { "r10000",         0,                        ISA_MIPS4,      CPU_R10000 },
14585   { "r12000",         0,                        ISA_MIPS4,      CPU_R12000 },
14586   { "vr5000",         0,                        ISA_MIPS4,      CPU_R5000 },
14587   { "vr5400",         0,                        ISA_MIPS4,      CPU_VR5400 },
14588   { "vr5500",         0,                        ISA_MIPS4,      CPU_VR5500 },
14589   { "rm5200",         0,                        ISA_MIPS4,      CPU_R5000 },
14590   { "rm5230",         0,                        ISA_MIPS4,      CPU_R5000 },
14591   { "rm5231",         0,                        ISA_MIPS4,      CPU_R5000 },
14592   { "rm5261",         0,                        ISA_MIPS4,      CPU_R5000 },
14593   { "rm5721",         0,                        ISA_MIPS4,      CPU_R5000 },
14594   { "rm7000",         0,                        ISA_MIPS4,      CPU_RM7000 },
14595   { "rm9000",         0,                        ISA_MIPS4,      CPU_RM9000 },
14596
14597   /* MIPS 32 */
14598   { "4kc",            0,                        ISA_MIPS32,     CPU_MIPS32 },
14599   { "4km",            0,                        ISA_MIPS32,     CPU_MIPS32 },
14600   { "4kp",            0,                        ISA_MIPS32,     CPU_MIPS32 },
14601   { "4ksc",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32,     CPU_MIPS32 },
14602
14603   /* MIPS 32 Release 2 */
14604   { "4kec",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14605   { "4kem",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14606   { "4kep",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14607   { "4ksd",           MIPS_CPU_ASE_SMARTMIPS,   ISA_MIPS32R2,   CPU_MIPS32R2 },
14608   { "m4k",            0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14609   { "m4kp",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14610   { "24k",            0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14611   { "24kc",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14612   { "24kf",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14613   { "24kx",           0,                        ISA_MIPS32R2,   CPU_MIPS32R2 },
14614   /* 24ke is a 24k with DSP ASE, other ASEs are optional.  */
14615   { "24ke",           MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
14616   { "24kec",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
14617   { "24kef",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
14618   { "24kex",          MIPS_CPU_ASE_DSP,         ISA_MIPS32R2,   CPU_MIPS32R2 },
14619   /* 34k is a 24k with MT ASE, other ASEs are optional.  */
14620   { "34kc",           MIPS_CPU_ASE_MT,          ISA_MIPS32R2,   CPU_MIPS32R2 },
14621   { "34kf",           MIPS_CPU_ASE_MT,          ISA_MIPS32R2,   CPU_MIPS32R2 },
14622   { "34kx",           MIPS_CPU_ASE_MT,          ISA_MIPS32R2,   CPU_MIPS32R2 },
14623
14624   /* MIPS 64 */
14625   { "5kc",            0,                        ISA_MIPS64,     CPU_MIPS64 },
14626   { "5kf",            0,                        ISA_MIPS64,     CPU_MIPS64 },
14627   { "20kc",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64,     CPU_MIPS64 },
14628
14629   /* MIPS 64 Release 2 */
14630   { "25kf",           MIPS_CPU_ASE_MIPS3D,      ISA_MIPS64R2,   CPU_MIPS64R2 },
14631
14632   /* Broadcom SB-1 CPU core */
14633   { "sb1",            MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14634                                                 ISA_MIPS64,     CPU_SB1 },
14635   /* Broadcom SB-1A CPU core */
14636   { "sb1a",           MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14637                                                 ISA_MIPS64,     CPU_SB1 },
14638
14639   /* End marker */
14640   { NULL, 0, 0, 0 }
14641 };
14642
14643
14644 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14645    with a final "000" replaced by "k".  Ignore case.
14646
14647    Note: this function is shared between GCC and GAS.  */
14648
14649 static bfd_boolean
14650 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14651 {
14652   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14653     given++, canonical++;
14654
14655   return ((*given == 0 && *canonical == 0)
14656           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14657 }
14658
14659
14660 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14661    CPU name.  We've traditionally allowed a lot of variation here.
14662
14663    Note: this function is shared between GCC and GAS.  */
14664
14665 static bfd_boolean
14666 mips_matching_cpu_name_p (const char *canonical, const char *given)
14667 {
14668   /* First see if the name matches exactly, or with a final "000"
14669      turned into "k".  */
14670   if (mips_strict_matching_cpu_name_p (canonical, given))
14671     return TRUE;
14672
14673   /* If not, try comparing based on numerical designation alone.
14674      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14675   if (TOLOWER (*given) == 'r')
14676     given++;
14677   if (!ISDIGIT (*given))
14678     return FALSE;
14679
14680   /* Skip over some well-known prefixes in the canonical name,
14681      hoping to find a number there too.  */
14682   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14683     canonical += 2;
14684   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14685     canonical += 2;
14686   else if (TOLOWER (canonical[0]) == 'r')
14687     canonical += 1;
14688
14689   return mips_strict_matching_cpu_name_p (canonical, given);
14690 }
14691
14692
14693 /* Parse an option that takes the name of a processor as its argument.
14694    OPTION is the name of the option and CPU_STRING is the argument.
14695    Return the corresponding processor enumeration if the CPU_STRING is
14696    recognized, otherwise report an error and return null.
14697
14698    A similar function exists in GCC.  */
14699
14700 static const struct mips_cpu_info *
14701 mips_parse_cpu (const char *option, const char *cpu_string)
14702 {
14703   const struct mips_cpu_info *p;
14704
14705   /* 'from-abi' selects the most compatible architecture for the given
14706      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14707      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14708      version.  Look first at the -mgp options, if given, otherwise base
14709      the choice on MIPS_DEFAULT_64BIT.
14710
14711      Treat NO_ABI like the EABIs.  One reason to do this is that the
14712      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14713      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14714      'mips64', just as we did in the days before 'from-abi'.  */
14715   if (strcasecmp (cpu_string, "from-abi") == 0)
14716     {
14717       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14718         return mips_cpu_info_from_isa (ISA_MIPS1);
14719
14720       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14721         return mips_cpu_info_from_isa (ISA_MIPS3);
14722
14723       if (file_mips_gp32 >= 0)
14724         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14725
14726       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14727                                      ? ISA_MIPS3
14728                                      : ISA_MIPS1);
14729     }
14730
14731   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14732   if (strcasecmp (cpu_string, "default") == 0)
14733     return 0;
14734
14735   for (p = mips_cpu_info_table; p->name != 0; p++)
14736     if (mips_matching_cpu_name_p (p->name, cpu_string))
14737       return p;
14738
14739   as_bad ("Bad value (%s) for %s", cpu_string, option);
14740   return 0;
14741 }
14742
14743 /* Return the canonical processor information for ISA (a member of the
14744    ISA_MIPS* enumeration).  */
14745
14746 static const struct mips_cpu_info *
14747 mips_cpu_info_from_isa (int isa)
14748 {
14749   int i;
14750
14751   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14752     if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
14753         && isa == mips_cpu_info_table[i].isa)
14754       return (&mips_cpu_info_table[i]);
14755
14756   return NULL;
14757 }
14758
14759 static const struct mips_cpu_info *
14760 mips_cpu_info_from_arch (int arch)
14761 {
14762   int i;
14763
14764   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14765     if (arch == mips_cpu_info_table[i].cpu)
14766       return (&mips_cpu_info_table[i]);
14767
14768   return NULL;
14769 }
14770 \f
14771 static void
14772 show (FILE *stream, const char *string, int *col_p, int *first_p)
14773 {
14774   if (*first_p)
14775     {
14776       fprintf (stream, "%24s", "");
14777       *col_p = 24;
14778     }
14779   else
14780     {
14781       fprintf (stream, ", ");
14782       *col_p += 2;
14783     }
14784
14785   if (*col_p + strlen (string) > 72)
14786     {
14787       fprintf (stream, "\n%24s", "");
14788       *col_p = 24;
14789     }
14790
14791   fprintf (stream, "%s", string);
14792   *col_p += strlen (string);
14793
14794   *first_p = 0;
14795 }
14796
14797 void
14798 md_show_usage (FILE *stream)
14799 {
14800   int column, first;
14801   size_t i;
14802
14803   fprintf (stream, _("\
14804 MIPS options:\n\
14805 -EB                     generate big endian output\n\
14806 -EL                     generate little endian output\n\
14807 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14808 -G NUM                  allow referencing objects up to NUM bytes\n\
14809                         implicitly with the gp register [default 8]\n"));
14810   fprintf (stream, _("\
14811 -mips1                  generate MIPS ISA I instructions\n\
14812 -mips2                  generate MIPS ISA II instructions\n\
14813 -mips3                  generate MIPS ISA III instructions\n\
14814 -mips4                  generate MIPS ISA IV instructions\n\
14815 -mips5                  generate MIPS ISA V instructions\n\
14816 -mips32                 generate MIPS32 ISA instructions\n\
14817 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14818 -mips64                 generate MIPS64 ISA instructions\n\
14819 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
14820 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14821
14822   first = 1;
14823
14824   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14825     show (stream, mips_cpu_info_table[i].name, &column, &first);
14826   show (stream, "from-abi", &column, &first);
14827   fputc ('\n', stream);
14828
14829   fprintf (stream, _("\
14830 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14831 -no-mCPU                don't generate code specific to CPU.\n\
14832                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14833
14834   first = 1;
14835
14836   show (stream, "3900", &column, &first);
14837   show (stream, "4010", &column, &first);
14838   show (stream, "4100", &column, &first);
14839   show (stream, "4650", &column, &first);
14840   fputc ('\n', stream);
14841
14842   fprintf (stream, _("\
14843 -mips16                 generate mips16 instructions\n\
14844 -no-mips16              do not generate mips16 instructions\n"));
14845   fprintf (stream, _("\
14846 -msmartmips             generate smartmips instructions\n\
14847 -mno-smartmips          do not generate smartmips instructions\n"));  
14848   fprintf (stream, _("\
14849 -mdsp                   generate DSP instructions\n\
14850 -mno-dsp                do not generate DSP instructions\n"));
14851   fprintf (stream, _("\
14852 -mmt                    generate MT instructions\n\
14853 -mno-mt                 do not generate MT instructions\n"));
14854   fprintf (stream, _("\
14855 -mfix-vr4120            work around certain VR4120 errata\n\
14856 -mfix-vr4130            work around VR4130 mflo/mfhi errata\n\
14857 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14858 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14859 -mno-shared             optimize output for executables\n\
14860 -msym32                 assume all symbols have 32-bit values\n\
14861 -O0                     remove unneeded NOPs, do not swap branches\n\
14862 -O                      remove unneeded NOPs and swap branches\n\
14863 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14864 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14865 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14866 #ifdef OBJ_ELF
14867   fprintf (stream, _("\
14868 -KPIC, -call_shared     generate SVR4 position independent code\n\
14869 -non_shared             do not generate position independent code\n\
14870 -xgot                   assume a 32 bit GOT\n\
14871 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
14872 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
14873                         non-shared code\n\
14874 -mabi=ABI               create ABI conformant object file for:\n"));
14875
14876   first = 1;
14877
14878   show (stream, "32", &column, &first);
14879   show (stream, "o64", &column, &first);
14880   show (stream, "n32", &column, &first);
14881   show (stream, "64", &column, &first);
14882   show (stream, "eabi", &column, &first);
14883
14884   fputc ('\n', stream);
14885
14886   fprintf (stream, _("\
14887 -32                     create o32 ABI object file (default)\n\
14888 -n32                    create n32 ABI object file\n\
14889 -64                     create 64 ABI object file\n"));
14890 #endif
14891 }
14892
14893 enum dwarf2_format
14894 mips_dwarf2_format (void)
14895 {
14896   if (mips_abi == N64_ABI)
14897     {
14898 #ifdef TE_IRIX
14899       return dwarf2_format_64bit_irix;
14900 #else
14901       return dwarf2_format_64bit;
14902 #endif
14903     }
14904   else
14905     return dwarf2_format_32bit;
14906 }
14907
14908 int
14909 mips_dwarf2_addr_size (void)
14910 {
14911   if (mips_abi == N64_ABI)
14912     return 8;
14913   else
14914     return 4;
14915 }
14916
14917 /* Standard calling conventions leave the CFA at SP on entry.  */
14918 void
14919 mips_cfi_frame_initial_instructions (void)
14920 {
14921   cfi_add_CFA_def_cfa_register (SP);
14922 }
14923
14924 int
14925 tc_mips_regname_to_dw2regnum (char *regname)
14926 {
14927   unsigned int regnum = -1;
14928   unsigned int reg;
14929
14930   if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
14931     regnum = reg;
14932
14933   return regnum;
14934 }