[ gas/ChangeLog ]
[platform/upstream/binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4    Contributed by the OSF and Ralph Campbell.
5    Written by Keith Knowles and Ralph Campbell, working independently.
6    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7    Support.
8
9    This file is part of GAS.
10
11    GAS is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2, or (at your option)
14    any later version.
15
16    GAS is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with GAS; see the file COPYING.  If not, write to the Free
23    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24    02111-1307, USA.  */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too.  */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about.  */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections.  Off by default on IRIX: the native
78    linker doesn't know about and discards them, but relocations against them
79    remain, leading to rld crashes.  */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT  1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP  28
99 #define SP  29
100 #define FP  30
101 #define RA  31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format.  */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section.  */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
115                             ? ".data" \
116                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
117                             ? ".rdata" \
118                             : OUTPUT_FLAVOR == bfd_target_coff_flavour \
119                             ? ".rdata" \
120                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
121                             ? ".rodata" \
122                             : (abort (), ""))
123
124 /* The ABI to use.  */
125 enum mips_abi_level
126 {
127   NO_ABI = 0,
128   O32_ABI,
129   O64_ABI,
130   N32_ABI,
131   N64_ABI,
132   EABI_ABI
133 };
134
135 /* MIPS ABI we are using for this output file.  */
136 static enum mips_abi_level mips_abi = NO_ABI;
137
138 /* Whether or not we have code that can call pic code.  */
139 int mips_abicalls = FALSE;
140
141 /* This is the set of options which may be modified by the .set
142    pseudo-op.  We use a struct so that .set push and .set pop are more
143    reliable.  */
144
145 struct mips_set_options
146 {
147   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
148      if it has not been initialized.  Changed by `.set mipsN', and the
149      -mipsN command line option, and the default CPU.  */
150   int isa;
151   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
152      if they have not been initialized.  Changed by `.set <asename>', by
153      command line options, and based on the default architecture.  */
154   int ase_mips3d;
155   int ase_mdmx;
156   /* Whether we are assembling for the mips16 processor.  0 if we are
157      not, 1 if we are, and -1 if the value has not been initialized.
158      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
159      -nomips16 command line options, and the default CPU.  */
160   int mips16;
161   /* Non-zero if we should not reorder instructions.  Changed by `.set
162      reorder' and `.set noreorder'.  */
163   int noreorder;
164   /* Non-zero if we should not permit the $at ($1) register to be used
165      in instructions.  Changed by `.set at' and `.set noat'.  */
166   int noat;
167   /* Non-zero if we should warn when a macro instruction expands into
168      more than one machine instruction.  Changed by `.set nomacro' and
169      `.set macro'.  */
170   int warn_about_macros;
171   /* Non-zero if we should not move instructions.  Changed by `.set
172      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
173   int nomove;
174   /* Non-zero if we should not optimize branches by moving the target
175      of the branch into the delay slot.  Actually, we don't perform
176      this optimization anyhow.  Changed by `.set bopt' and `.set
177      nobopt'.  */
178   int nobopt;
179   /* Non-zero if we should not autoextend mips16 instructions.
180      Changed by `.set autoextend' and `.set noautoextend'.  */
181   int noautoextend;
182   /* Restrict general purpose registers and floating point registers
183      to 32 bit.  This is initially determined when -mgp32 or -mfp32
184      is passed but can changed if the assembler code uses .set mipsN.  */
185   int gp32;
186   int fp32;
187   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
188      command line option, and the default CPU.  */
189   int arch;
190 };
191
192 /* True if -mgp32 was passed.  */
193 static int file_mips_gp32 = -1;
194
195 /* True if -mfp32 was passed.  */
196 static int file_mips_fp32 = -1;
197
198 /* This is the struct we use to hold the current set of options.  Note
199    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
200    -1 to indicate that they have not been initialized.  */
201
202 static struct mips_set_options mips_opts =
203 {
204   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
205 };
206
207 /* These variables are filled in with the masks of registers used.
208    The object format code reads them and puts them in the appropriate
209    place.  */
210 unsigned long mips_gprmask;
211 unsigned long mips_cprmask[4];
212
213 /* MIPS ISA we are using for this output file.  */
214 static int file_mips_isa = ISA_UNKNOWN;
215
216 /* True if -mips16 was passed or implied by arguments passed on the
217    command line (e.g., by -march).  */
218 static int file_ase_mips16;
219
220 /* True if -mips3d was passed or implied by arguments passed on the
221    command line (e.g., by -march).  */
222 static int file_ase_mips3d;
223
224 /* True if -mdmx was passed or implied by arguments passed on the
225    command line (e.g., by -march).  */
226 static int file_ase_mdmx;
227
228 /* The argument of the -march= flag.  The architecture we are assembling.  */
229 static int file_mips_arch = CPU_UNKNOWN;
230 static const char *mips_arch_string;
231
232 /* The argument of the -mtune= flag.  The architecture for which we
233    are optimizing.  */
234 static int mips_tune = CPU_UNKNOWN;
235 static const char *mips_tune_string;
236
237 /* True when generating 32-bit code for a 64-bit processor.  */
238 static int mips_32bitmode = 0;
239
240 /* Some ISA's have delay slots for instructions which read or write
241    from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
242    Return true if instructions marked INSN_LOAD_COPROC_DELAY,
243    INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
244    delay slot in this ISA.  The uses of this macro assume that any
245    ISA that has delay slots for one of these, has them for all.  They
246    also assume that ISAs which don't have delays for these insns, don't
247    have delays for the INSN_LOAD_MEMORY_DELAY instructions either.  */
248 #define ISA_HAS_COPROC_DELAYS(ISA) (        \
249    (ISA) == ISA_MIPS1                       \
250    || (ISA) == ISA_MIPS2                    \
251    || (ISA) == ISA_MIPS3                    \
252    )
253
254 /* True if the given ABI requires 32-bit registers.  */
255 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
256
257 /* Likewise 64-bit registers.  */
258 #define ABI_NEEDS_64BIT_REGS(ABI) \
259   ((ABI) == N32_ABI               \
260    || (ABI) == N64_ABI            \
261    || (ABI) == O64_ABI)
262
263 /*  Return true if ISA supports 64 bit gp register instructions.  */
264 #define ISA_HAS_64BIT_REGS(ISA) (    \
265    (ISA) == ISA_MIPS3                \
266    || (ISA) == ISA_MIPS4             \
267    || (ISA) == ISA_MIPS5             \
268    || (ISA) == ISA_MIPS64            \
269    || (ISA) == ISA_MIPS64R2          \
270    )
271
272 /* Return true if ISA supports 64-bit right rotate (dror et al.)
273    instructions.  */
274 #define ISA_HAS_DROR(ISA) (     \
275    (ISA) == ISA_MIPS64R2        \
276    )
277
278 /* Return true if ISA supports 32-bit right rotate (ror et al.)
279    instructions.  */
280 #define ISA_HAS_ROR(ISA) (      \
281    (ISA) == ISA_MIPS32R2        \
282    || (ISA) == ISA_MIPS64R2     \
283    )
284
285 #define HAVE_32BIT_GPRS                            \
286     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
287
288 #define HAVE_32BIT_FPRS                            \
289     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
290
291 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
292 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
293
294 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
295
296 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
297
298 /* We can only have 64bit addresses if the object file format
299    supports it.  */
300 #define HAVE_32BIT_ADDRESSES                           \
301    (HAVE_32BIT_GPRS                                    \
302     || ((bfd_arch_bits_per_address (stdoutput) == 32   \
303          || ! HAVE_64BIT_OBJECTS)                      \
304         && mips_pic != EMBEDDED_PIC))
305
306 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
307
308 /* Addresses are loaded in different ways, depending on the address size
309    in use.  The n32 ABI Documentation also mandates the use of additions
310    with overflow checking, but existing implementations don't follow it.  */
311 #define ADDRESS_ADD_INSN                                                \
312    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
313
314 #define ADDRESS_ADDI_INSN                                               \
315    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
316
317 #define ADDRESS_LOAD_INSN                                               \
318    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
319
320 #define ADDRESS_STORE_INSN                                              \
321    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
322
323 /* Return true if the given CPU supports the MIPS16 ASE.  */
324 #define CPU_HAS_MIPS16(cpu)                                             \
325    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0          \
326     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
327
328 /* Return true if the given CPU supports the MIPS3D ASE.  */
329 #define CPU_HAS_MIPS3D(cpu)     ((cpu) == CPU_SB1      \
330                                  )
331
332 /* Return true if the given CPU supports the MDMX ASE.  */
333 #define CPU_HAS_MDMX(cpu)       (FALSE                 \
334                                  )
335
336 /* True if CPU has a dror instruction.  */
337 #define CPU_HAS_DROR(CPU)       ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
338
339 /* True if CPU has a ror instruction.  */
340 #define CPU_HAS_ROR(CPU)        CPU_HAS_DROR (CPU)
341
342 /* Whether the processor uses hardware interlocks to protect
343    reads from the HI and LO registers, and thus does not
344    require nops to be inserted.  */
345
346 #define hilo_interlocks (mips_opts.arch == CPU_R4010                       \
347                          || mips_opts.arch == CPU_VR5500                   \
348                          || mips_opts.arch == CPU_RM7000                   \
349                          || mips_opts.arch == CPU_SB1                      \
350                          )
351
352 /* Whether the processor uses hardware interlocks to protect reads
353    from the GPRs, and thus does not require nops to be inserted.  */
354 #define gpr_interlocks \
355   (mips_opts.isa != ISA_MIPS1  \
356    || mips_opts.arch == CPU_VR5400  \
357    || mips_opts.arch == CPU_VR5500  \
358    || mips_opts.arch == CPU_R3900)
359
360 /* As with other "interlocks" this is used by hardware that has FP
361    (co-processor) interlocks.  */
362 /* Itbl support may require additional care here.  */
363 #define cop_interlocks (mips_opts.arch == CPU_R4300                        \
364                         || mips_opts.arch == CPU_VR5400                    \
365                         || mips_opts.arch == CPU_VR5500                    \
366                         || mips_opts.arch == CPU_SB1                       \
367                         )
368
369 /* Is this a mfhi or mflo instruction?  */
370 #define MF_HILO_INSN(PINFO) \
371           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
372
373 /* MIPS PIC level.  */
374
375 enum mips_pic_level mips_pic;
376
377 /* Warn about all NOPS that the assembler generates.  */
378 static int warn_nops = 0;
379
380 /* 1 if we should generate 32 bit offsets from the $gp register in
381    SVR4_PIC mode.  Currently has no meaning in other modes.  */
382 static int mips_big_got = 0;
383
384 /* 1 if trap instructions should used for overflow rather than break
385    instructions.  */
386 static int mips_trap = 0;
387
388 /* 1 if double width floating point constants should not be constructed
389    by assembling two single width halves into two single width floating
390    point registers which just happen to alias the double width destination
391    register.  On some architectures this aliasing can be disabled by a bit
392    in the status register, and the setting of this bit cannot be determined
393    automatically at assemble time.  */
394 static int mips_disable_float_construction;
395
396 /* Non-zero if any .set noreorder directives were used.  */
397
398 static int mips_any_noreorder;
399
400 /* Non-zero if nops should be inserted when the register referenced in
401    an mfhi/mflo instruction is read in the next two instructions.  */
402 static int mips_7000_hilo_fix;
403
404 /* The size of the small data section.  */
405 static unsigned int g_switch_value = 8;
406 /* Whether the -G option was used.  */
407 static int g_switch_seen = 0;
408
409 #define N_RMASK 0xc4
410 #define N_VFP   0xd4
411
412 /* If we can determine in advance that GP optimization won't be
413    possible, we can skip the relaxation stuff that tries to produce
414    GP-relative references.  This makes delay slot optimization work
415    better.
416
417    This function can only provide a guess, but it seems to work for
418    gcc output.  It needs to guess right for gcc, otherwise gcc
419    will put what it thinks is a GP-relative instruction in a branch
420    delay slot.
421
422    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
423    fixed it for the non-PIC mode.  KR 95/04/07  */
424 static int nopic_need_relax (symbolS *, int);
425
426 /* handle of the OPCODE hash table */
427 static struct hash_control *op_hash = NULL;
428
429 /* The opcode hash table we use for the mips16.  */
430 static struct hash_control *mips16_op_hash = NULL;
431
432 /* This array holds the chars that always start a comment.  If the
433     pre-processor is disabled, these aren't very useful */
434 const char comment_chars[] = "#";
435
436 /* This array holds the chars that only start a comment at the beginning of
437    a line.  If the line seems to have the form '# 123 filename'
438    .line and .file directives will appear in the pre-processed output */
439 /* Note that input_file.c hand checks for '#' at the beginning of the
440    first line of the input file.  This is because the compiler outputs
441    #NO_APP at the beginning of its output.  */
442 /* Also note that C style comments are always supported.  */
443 const char line_comment_chars[] = "#";
444
445 /* This array holds machine specific line separator characters.  */
446 const char line_separator_chars[] = ";";
447
448 /* Chars that can be used to separate mant from exp in floating point nums */
449 const char EXP_CHARS[] = "eE";
450
451 /* Chars that mean this number is a floating point constant */
452 /* As in 0f12.456 */
453 /* or    0d1.2345e12 */
454 const char FLT_CHARS[] = "rRsSfFdDxXpP";
455
456 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
457    changed in read.c .  Ideally it shouldn't have to know about it at all,
458    but nothing is ideal around here.
459  */
460
461 static char *insn_error;
462
463 static int auto_align = 1;
464
465 /* When outputting SVR4 PIC code, the assembler needs to know the
466    offset in the stack frame from which to restore the $gp register.
467    This is set by the .cprestore pseudo-op, and saved in this
468    variable.  */
469 static offsetT mips_cprestore_offset = -1;
470
471 /* Similiar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
472    more optimizations, it can use a register value instead of a memory-saved
473    offset and even an other register than $gp as global pointer.  */
474 static offsetT mips_cpreturn_offset = -1;
475 static int mips_cpreturn_register = -1;
476 static int mips_gp_register = GP;
477 static int mips_gprel_offset = 0;
478
479 /* Whether mips_cprestore_offset has been set in the current function
480    (or whether it has already been warned about, if not).  */
481 static int mips_cprestore_valid = 0;
482
483 /* This is the register which holds the stack frame, as set by the
484    .frame pseudo-op.  This is needed to implement .cprestore.  */
485 static int mips_frame_reg = SP;
486
487 /* Whether mips_frame_reg has been set in the current function
488    (or whether it has already been warned about, if not).  */
489 static int mips_frame_reg_valid = 0;
490
491 /* To output NOP instructions correctly, we need to keep information
492    about the previous two instructions.  */
493
494 /* Whether we are optimizing.  The default value of 2 means to remove
495    unneeded NOPs and swap branch instructions when possible.  A value
496    of 1 means to not swap branches.  A value of 0 means to always
497    insert NOPs.  */
498 static int mips_optimize = 2;
499
500 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
501    equivalent to seeing no -g option at all.  */
502 static int mips_debug = 0;
503
504 /* The previous instruction.  */
505 static struct mips_cl_insn prev_insn;
506
507 /* The instruction before prev_insn.  */
508 static struct mips_cl_insn prev_prev_insn;
509
510 /* If we don't want information for prev_insn or prev_prev_insn, we
511    point the insn_mo field at this dummy integer.  */
512 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
513
514 /* Non-zero if prev_insn is valid.  */
515 static int prev_insn_valid;
516
517 /* The frag for the previous instruction.  */
518 static struct frag *prev_insn_frag;
519
520 /* The offset into prev_insn_frag for the previous instruction.  */
521 static long prev_insn_where;
522
523 /* The reloc type for the previous instruction, if any.  */
524 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
525
526 /* The reloc for the previous instruction, if any.  */
527 static fixS *prev_insn_fixp[3];
528
529 /* Non-zero if the previous instruction was in a delay slot.  */
530 static int prev_insn_is_delay_slot;
531
532 /* Non-zero if the previous instruction was in a .set noreorder.  */
533 static int prev_insn_unreordered;
534
535 /* Non-zero if the previous instruction uses an extend opcode (if
536    mips16).  */
537 static int prev_insn_extended;
538
539 /* Non-zero if the previous previous instruction was in a .set
540    noreorder.  */
541 static int prev_prev_insn_unreordered;
542
543 /* If this is set, it points to a frag holding nop instructions which
544    were inserted before the start of a noreorder section.  If those
545    nops turn out to be unnecessary, the size of the frag can be
546    decreased.  */
547 static fragS *prev_nop_frag;
548
549 /* The number of nop instructions we created in prev_nop_frag.  */
550 static int prev_nop_frag_holds;
551
552 /* The number of nop instructions that we know we need in
553    prev_nop_frag.  */
554 static int prev_nop_frag_required;
555
556 /* The number of instructions we've seen since prev_nop_frag.  */
557 static int prev_nop_frag_since;
558
559 /* For ECOFF and ELF, relocations against symbols are done in two
560    parts, with a HI relocation and a LO relocation.  Each relocation
561    has only 16 bits of space to store an addend.  This means that in
562    order for the linker to handle carries correctly, it must be able
563    to locate both the HI and the LO relocation.  This means that the
564    relocations must appear in order in the relocation table.
565
566    In order to implement this, we keep track of each unmatched HI
567    relocation.  We then sort them so that they immediately precede the
568    corresponding LO relocation.  */
569
570 struct mips_hi_fixup
571 {
572   /* Next HI fixup.  */
573   struct mips_hi_fixup *next;
574   /* This fixup.  */
575   fixS *fixp;
576   /* The section this fixup is in.  */
577   segT seg;
578 };
579
580 /* The list of unmatched HI relocs.  */
581
582 static struct mips_hi_fixup *mips_hi_fixup_list;
583
584 /* The frag containing the last explicit relocation operator.
585    Null if explicit relocations have not been used.  */
586
587 static fragS *prev_reloc_op_frag;
588
589 /* Map normal MIPS register numbers to mips16 register numbers.  */
590
591 #define X ILLEGAL_REG
592 static const int mips32_to_16_reg_map[] =
593 {
594   X, X, 2, 3, 4, 5, 6, 7,
595   X, X, X, X, X, X, X, X,
596   0, 1, X, X, X, X, X, X,
597   X, X, X, X, X, X, X, X
598 };
599 #undef X
600
601 /* Map mips16 register numbers to normal MIPS register numbers.  */
602
603 static const unsigned int mips16_to_32_reg_map[] =
604 {
605   16, 17, 2, 3, 4, 5, 6, 7
606 };
607
608 static int mips_fix_4122_bugs;
609
610 /* We don't relax branches by default, since this causes us to expand
611    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
612    fail to compute the offset before expanding the macro to the most
613    efficient expansion.  */
614
615 static int mips_relax_branch;
616 \f
617 /* Since the MIPS does not have multiple forms of PC relative
618    instructions, we do not have to do relaxing as is done on other
619    platforms.  However, we do have to handle GP relative addressing
620    correctly, which turns out to be a similar problem.
621
622    Every macro that refers to a symbol can occur in (at least) two
623    forms, one with GP relative addressing and one without.  For
624    example, loading a global variable into a register generally uses
625    a macro instruction like this:
626      lw $4,i
627    If i can be addressed off the GP register (this is true if it is in
628    the .sbss or .sdata section, or if it is known to be smaller than
629    the -G argument) this will generate the following instruction:
630      lw $4,i($gp)
631    This instruction will use a GPREL reloc.  If i can not be addressed
632    off the GP register, the following instruction sequence will be used:
633      lui $at,i
634      lw $4,i($at)
635    In this case the first instruction will have a HI16 reloc, and the
636    second reloc will have a LO16 reloc.  Both relocs will be against
637    the symbol i.
638
639    The issue here is that we may not know whether i is GP addressable
640    until after we see the instruction that uses it.  Therefore, we
641    want to be able to choose the final instruction sequence only at
642    the end of the assembly.  This is similar to the way other
643    platforms choose the size of a PC relative instruction only at the
644    end of assembly.
645
646    When generating position independent code we do not use GP
647    addressing in quite the same way, but the issue still arises as
648    external symbols and local symbols must be handled differently.
649
650    We handle these issues by actually generating both possible
651    instruction sequences.  The longer one is put in a frag_var with
652    type rs_machine_dependent.  We encode what to do with the frag in
653    the subtype field.  We encode (1) the number of existing bytes to
654    replace, (2) the number of new bytes to use, (3) the offset from
655    the start of the existing bytes to the first reloc we must generate
656    (that is, the offset is applied from the start of the existing
657    bytes after they are replaced by the new bytes, if any), (4) the
658    offset from the start of the existing bytes to the second reloc,
659    (5) whether a third reloc is needed (the third reloc is always four
660    bytes after the second reloc), and (6) whether to warn if this
661    variant is used (this is sometimes needed if .set nomacro or .set
662    noat is in effect).  All these numbers are reasonably small.
663
664    Generating two instruction sequences must be handled carefully to
665    ensure that delay slots are handled correctly.  Fortunately, there
666    are a limited number of cases.  When the second instruction
667    sequence is generated, append_insn is directed to maintain the
668    existing delay slot information, so it continues to apply to any
669    code after the second instruction sequence.  This means that the
670    second instruction sequence must not impose any requirements not
671    required by the first instruction sequence.
672
673    These variant frags are then handled in functions called by the
674    machine independent code.  md_estimate_size_before_relax returns
675    the final size of the frag.  md_convert_frag sets up the final form
676    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
677    one if needed.  */
678 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
679   ((relax_substateT) \
680    (((old) << 23) \
681     | ((new) << 16) \
682     | (((reloc1) + 64) << 9) \
683     | (((reloc2) + 64) << 2) \
684     | ((reloc3) ? (1 << 1) : 0) \
685     | ((warn) ? 1 : 0)))
686 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
687 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
688 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
689 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
690 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
691 #define RELAX_WARN(i) ((i) & 1)
692
693 /* Branch without likely bit.  If label is out of range, we turn:
694
695         beq reg1, reg2, label
696         delay slot
697
698    into
699
700         bne reg1, reg2, 0f
701         nop
702         j label
703      0: delay slot
704
705    with the following opcode replacements:
706
707         beq <-> bne
708         blez <-> bgtz
709         bltz <-> bgez
710         bc1f <-> bc1t
711
712         bltzal <-> bgezal  (with jal label instead of j label)
713
714    Even though keeping the delay slot instruction in the delay slot of
715    the branch would be more efficient, it would be very tricky to do
716    correctly, because we'd have to introduce a variable frag *after*
717    the delay slot instruction, and expand that instead.  Let's do it
718    the easy way for now, even if the branch-not-taken case now costs
719    one additional instruction.  Out-of-range branches are not supposed
720    to be common, anyway.
721
722    Branch likely.  If label is out of range, we turn:
723
724         beql reg1, reg2, label
725         delay slot (annulled if branch not taken)
726
727    into
728
729         beql reg1, reg2, 1f
730         nop
731         beql $0, $0, 2f
732         nop
733      1: j[al] label
734         delay slot (executed only if branch taken)
735      2:
736
737    It would be possible to generate a shorter sequence by losing the
738    likely bit, generating something like:
739
740         bne reg1, reg2, 0f
741         nop
742         j[al] label
743         delay slot (executed only if branch taken)
744      0:
745
746         beql -> bne
747         bnel -> beq
748         blezl -> bgtz
749         bgtzl -> blez
750         bltzl -> bgez
751         bgezl -> bltz
752         bc1fl -> bc1t
753         bc1tl -> bc1f
754
755         bltzall -> bgezal  (with jal label instead of j label)
756         bgezall -> bltzal  (ditto)
757
758
759    but it's not clear that it would actually improve performance.  */
760 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
761   ((relax_substateT) \
762    (0xc0000000 \
763     | ((toofar) ? 1 : 0) \
764     | ((link) ? 2 : 0) \
765     | ((likely) ? 4 : 0) \
766     | ((uncond) ? 8 : 0)))
767 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
768 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
769 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
770 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
771 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
772
773 /* For mips16 code, we use an entirely different form of relaxation.
774    mips16 supports two versions of most instructions which take
775    immediate values: a small one which takes some small value, and a
776    larger one which takes a 16 bit value.  Since branches also follow
777    this pattern, relaxing these values is required.
778
779    We can assemble both mips16 and normal MIPS code in a single
780    object.  Therefore, we need to support this type of relaxation at
781    the same time that we support the relaxation described above.  We
782    use the high bit of the subtype field to distinguish these cases.
783
784    The information we store for this type of relaxation is the
785    argument code found in the opcode file for this relocation, whether
786    the user explicitly requested a small or extended form, and whether
787    the relocation is in a jump or jal delay slot.  That tells us the
788    size of the value, and how it should be stored.  We also store
789    whether the fragment is considered to be extended or not.  We also
790    store whether this is known to be a branch to a different section,
791    whether we have tried to relax this frag yet, and whether we have
792    ever extended a PC relative fragment because of a shift count.  */
793 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
794   (0x80000000                                                   \
795    | ((type) & 0xff)                                            \
796    | ((small) ? 0x100 : 0)                                      \
797    | ((ext) ? 0x200 : 0)                                        \
798    | ((dslot) ? 0x400 : 0)                                      \
799    | ((jal_dslot) ? 0x800 : 0))
800 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
801 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
802 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
803 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
804 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
805 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
806 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
807 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
808 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
809 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
810 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
811 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
812
813 /* Is the given value a sign-extended 32-bit value?  */
814 #define IS_SEXT_32BIT_NUM(x)                                            \
815   (((x) &~ (offsetT) 0x7fffffff) == 0                                   \
816    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
817
818 /* Is the given value a sign-extended 16-bit value?  */
819 #define IS_SEXT_16BIT_NUM(x)                                            \
820   (((x) &~ (offsetT) 0x7fff) == 0                                       \
821    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
822
823 \f
824 /* Prototypes for static functions.  */
825
826 #define internalError()                                                 \
827     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
828
829 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
830
831 static void append_insn
832   (char *place, struct mips_cl_insn *ip, expressionS *p,
833    bfd_reloc_code_real_type *r);
834 static void mips_no_prev_insn (int);
835 static void mips16_macro_build
836   (char *, int *, expressionS *, const char *, const char *, va_list);
837 static void load_register (int *, int, expressionS *, int);
838 static void macro (struct mips_cl_insn * ip);
839 static void mips16_macro (struct mips_cl_insn * ip);
840 #ifdef LOSING_COMPILER
841 static void macro2 (struct mips_cl_insn * ip);
842 #endif
843 static void mips_ip (char *str, struct mips_cl_insn * ip);
844 static void mips16_ip (char *str, struct mips_cl_insn * ip);
845 static void mips16_immed
846   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
847    unsigned long *, bfd_boolean *, unsigned short *);
848 static size_t my_getSmallExpression
849   (expressionS *, bfd_reloc_code_real_type *, char *);
850 static void my_getExpression (expressionS *, char *);
851 static void s_align (int);
852 static void s_change_sec (int);
853 static void s_change_section (int);
854 static void s_cons (int);
855 static void s_float_cons (int);
856 static void s_mips_globl (int);
857 static void s_option (int);
858 static void s_mipsset (int);
859 static void s_abicalls (int);
860 static void s_cpload (int);
861 static void s_cpsetup (int);
862 static void s_cplocal (int);
863 static void s_cprestore (int);
864 static void s_cpreturn (int);
865 static void s_gpvalue (int);
866 static void s_gpword (int);
867 static void s_gpdword (int);
868 static void s_cpadd (int);
869 static void s_insn (int);
870 static void md_obj_begin (void);
871 static void md_obj_end (void);
872 static void s_mips_ent (int);
873 static void s_mips_end (int);
874 static void s_mips_frame (int);
875 static void s_mips_mask (int reg_type);
876 static void s_mips_stab (int);
877 static void s_mips_weakext (int);
878 static void s_mips_file (int);
879 static void s_mips_loc (int);
880 static bfd_boolean pic_need_relax (symbolS *, asection *);
881 static int relaxed_branch_length (fragS *, asection *, int);
882 static int validate_mips_insn (const struct mips_opcode *);
883
884 /* Table and functions used to map between CPU/ISA names, and
885    ISA levels, and CPU numbers.  */
886
887 struct mips_cpu_info
888 {
889   const char *name;           /* CPU or ISA name.  */
890   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
891   int isa;                    /* ISA level.  */
892   int cpu;                    /* CPU number (default CPU if ISA).  */
893 };
894
895 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
896 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
897 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
898 \f
899 /* Pseudo-op table.
900
901    The following pseudo-ops from the Kane and Heinrich MIPS book
902    should be defined here, but are currently unsupported: .alias,
903    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
904
905    The following pseudo-ops from the Kane and Heinrich MIPS book are
906    specific to the type of debugging information being generated, and
907    should be defined by the object format: .aent, .begin, .bend,
908    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
909    .vreg.
910
911    The following pseudo-ops from the Kane and Heinrich MIPS book are
912    not MIPS CPU specific, but are also not specific to the object file
913    format.  This file is probably the best place to define them, but
914    they are not currently supported: .asm0, .endr, .lab, .repeat,
915    .struct.  */
916
917 static const pseudo_typeS mips_pseudo_table[] =
918 {
919   /* MIPS specific pseudo-ops.  */
920   {"option", s_option, 0},
921   {"set", s_mipsset, 0},
922   {"rdata", s_change_sec, 'r'},
923   {"sdata", s_change_sec, 's'},
924   {"livereg", s_ignore, 0},
925   {"abicalls", s_abicalls, 0},
926   {"cpload", s_cpload, 0},
927   {"cpsetup", s_cpsetup, 0},
928   {"cplocal", s_cplocal, 0},
929   {"cprestore", s_cprestore, 0},
930   {"cpreturn", s_cpreturn, 0},
931   {"gpvalue", s_gpvalue, 0},
932   {"gpword", s_gpword, 0},
933   {"gpdword", s_gpdword, 0},
934   {"cpadd", s_cpadd, 0},
935   {"insn", s_insn, 0},
936
937   /* Relatively generic pseudo-ops that happen to be used on MIPS
938      chips.  */
939   {"asciiz", stringer, 1},
940   {"bss", s_change_sec, 'b'},
941   {"err", s_err, 0},
942   {"half", s_cons, 1},
943   {"dword", s_cons, 3},
944   {"weakext", s_mips_weakext, 0},
945
946   /* These pseudo-ops are defined in read.c, but must be overridden
947      here for one reason or another.  */
948   {"align", s_align, 0},
949   {"byte", s_cons, 0},
950   {"data", s_change_sec, 'd'},
951   {"double", s_float_cons, 'd'},
952   {"float", s_float_cons, 'f'},
953   {"globl", s_mips_globl, 0},
954   {"global", s_mips_globl, 0},
955   {"hword", s_cons, 1},
956   {"int", s_cons, 2},
957   {"long", s_cons, 2},
958   {"octa", s_cons, 4},
959   {"quad", s_cons, 3},
960   {"section", s_change_section, 0},
961   {"short", s_cons, 1},
962   {"single", s_float_cons, 'f'},
963   {"stabn", s_mips_stab, 'n'},
964   {"text", s_change_sec, 't'},
965   {"word", s_cons, 2},
966
967   { "extern", ecoff_directive_extern, 0},
968
969   { NULL, NULL, 0 },
970 };
971
972 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
973 {
974   /* These pseudo-ops should be defined by the object file format.
975      However, a.out doesn't support them, so we have versions here.  */
976   {"aent", s_mips_ent, 1},
977   {"bgnb", s_ignore, 0},
978   {"end", s_mips_end, 0},
979   {"endb", s_ignore, 0},
980   {"ent", s_mips_ent, 0},
981   {"file", s_mips_file, 0},
982   {"fmask", s_mips_mask, 'F'},
983   {"frame", s_mips_frame, 0},
984   {"loc", s_mips_loc, 0},
985   {"mask", s_mips_mask, 'R'},
986   {"verstamp", s_ignore, 0},
987   { NULL, NULL, 0 },
988 };
989
990 extern void pop_insert (const pseudo_typeS *);
991
992 void
993 mips_pop_insert (void)
994 {
995   pop_insert (mips_pseudo_table);
996   if (! ECOFF_DEBUGGING)
997     pop_insert (mips_nonecoff_pseudo_table);
998 }
999 \f
1000 /* Symbols labelling the current insn.  */
1001
1002 struct insn_label_list
1003 {
1004   struct insn_label_list *next;
1005   symbolS *label;
1006 };
1007
1008 static struct insn_label_list *insn_labels;
1009 static struct insn_label_list *free_insn_labels;
1010
1011 static void mips_clear_insn_labels (void);
1012
1013 static inline void
1014 mips_clear_insn_labels (void)
1015 {
1016   register struct insn_label_list **pl;
1017
1018   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1019     ;
1020   *pl = insn_labels;
1021   insn_labels = NULL;
1022 }
1023 \f
1024 static char *expr_end;
1025
1026 /* Expressions which appear in instructions.  These are set by
1027    mips_ip.  */
1028
1029 static expressionS imm_expr;
1030 static expressionS imm2_expr;
1031 static expressionS offset_expr;
1032
1033 /* Relocs associated with imm_expr and offset_expr.  */
1034
1035 static bfd_reloc_code_real_type imm_reloc[3]
1036   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1037 static bfd_reloc_code_real_type offset_reloc[3]
1038   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1039
1040 /* These are set by mips16_ip if an explicit extension is used.  */
1041
1042 static bfd_boolean mips16_small, mips16_ext;
1043
1044 #ifdef OBJ_ELF
1045 /* The pdr segment for per procedure frame/regmask info.  Not used for
1046    ECOFF debugging.  */
1047
1048 static segT pdr_seg;
1049 #endif
1050
1051 /* The default target format to use.  */
1052
1053 const char *
1054 mips_target_format (void)
1055 {
1056   switch (OUTPUT_FLAVOR)
1057     {
1058     case bfd_target_aout_flavour:
1059       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1060     case bfd_target_ecoff_flavour:
1061       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1062     case bfd_target_coff_flavour:
1063       return "pe-mips";
1064     case bfd_target_elf_flavour:
1065 #ifdef TE_TMIPS
1066       /* This is traditional mips.  */
1067       return (target_big_endian
1068               ? (HAVE_64BIT_OBJECTS
1069                  ? "elf64-tradbigmips"
1070                  : (HAVE_NEWABI
1071                     ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1072               : (HAVE_64BIT_OBJECTS
1073                  ? "elf64-tradlittlemips"
1074                  : (HAVE_NEWABI
1075                     ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1076 #else
1077       return (target_big_endian
1078               ? (HAVE_64BIT_OBJECTS
1079                  ? "elf64-bigmips"
1080                  : (HAVE_NEWABI
1081                     ? "elf32-nbigmips" : "elf32-bigmips"))
1082               : (HAVE_64BIT_OBJECTS
1083                  ? "elf64-littlemips"
1084                  : (HAVE_NEWABI
1085                     ? "elf32-nlittlemips" : "elf32-littlemips")));
1086 #endif
1087     default:
1088       abort ();
1089       return NULL;
1090     }
1091 }
1092
1093 /* This function is called once, at assembler startup time.  It should
1094    set up all the tables, etc. that the MD part of the assembler will need.  */
1095
1096 void
1097 md_begin (void)
1098 {
1099   register const char *retval = NULL;
1100   int i = 0;
1101   int broken = 0;
1102
1103   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1104     as_warn (_("Could not set architecture and machine"));
1105
1106   op_hash = hash_new ();
1107
1108   for (i = 0; i < NUMOPCODES;)
1109     {
1110       const char *name = mips_opcodes[i].name;
1111
1112       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1113       if (retval != NULL)
1114         {
1115           fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1116                    mips_opcodes[i].name, retval);
1117           /* Probably a memory allocation problem?  Give up now.  */
1118           as_fatal (_("Broken assembler.  No assembly attempted."));
1119         }
1120       do
1121         {
1122           if (mips_opcodes[i].pinfo != INSN_MACRO)
1123             {
1124               if (!validate_mips_insn (&mips_opcodes[i]))
1125                 broken = 1;
1126             }
1127           ++i;
1128         }
1129       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1130     }
1131
1132   mips16_op_hash = hash_new ();
1133
1134   i = 0;
1135   while (i < bfd_mips16_num_opcodes)
1136     {
1137       const char *name = mips16_opcodes[i].name;
1138
1139       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1140       if (retval != NULL)
1141         as_fatal (_("internal: can't hash `%s': %s"),
1142                   mips16_opcodes[i].name, retval);
1143       do
1144         {
1145           if (mips16_opcodes[i].pinfo != INSN_MACRO
1146               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1147                   != mips16_opcodes[i].match))
1148             {
1149               fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1150                        mips16_opcodes[i].name, mips16_opcodes[i].args);
1151               broken = 1;
1152             }
1153           ++i;
1154         }
1155       while (i < bfd_mips16_num_opcodes
1156              && strcmp (mips16_opcodes[i].name, name) == 0);
1157     }
1158
1159   if (broken)
1160     as_fatal (_("Broken assembler.  No assembly attempted."));
1161
1162   /* We add all the general register names to the symbol table.  This
1163      helps us detect invalid uses of them.  */
1164   for (i = 0; i < 32; i++)
1165     {
1166       char buf[5];
1167
1168       sprintf (buf, "$%d", i);
1169       symbol_table_insert (symbol_new (buf, reg_section, i,
1170                                        &zero_address_frag));
1171     }
1172   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1173                                    &zero_address_frag));
1174   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1175                                    &zero_address_frag));
1176   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1177                                    &zero_address_frag));
1178   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1179                                    &zero_address_frag));
1180   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1181                                    &zero_address_frag));
1182   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1183                                    &zero_address_frag));
1184   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1185                                    &zero_address_frag));
1186   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1187                                    &zero_address_frag));
1188   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1189                                    &zero_address_frag));
1190
1191   /* If we don't add these register names to the symbol table, they
1192      may end up being added as regular symbols by operand(), and then
1193      make it to the object file as undefined in case they're not
1194      regarded as local symbols.  They're local in o32, since `$' is a
1195      local symbol prefix, but not in n32 or n64.  */
1196   for (i = 0; i < 8; i++)
1197     {
1198       char buf[6];
1199
1200       sprintf (buf, "$fcc%i", i);
1201       symbol_table_insert (symbol_new (buf, reg_section, -1,
1202                                        &zero_address_frag));
1203     }
1204
1205   mips_no_prev_insn (FALSE);
1206
1207   mips_gprmask = 0;
1208   mips_cprmask[0] = 0;
1209   mips_cprmask[1] = 0;
1210   mips_cprmask[2] = 0;
1211   mips_cprmask[3] = 0;
1212
1213   /* set the default alignment for the text section (2**2) */
1214   record_alignment (text_section, 2);
1215
1216   if (USE_GLOBAL_POINTER_OPT)
1217     bfd_set_gp_size (stdoutput, g_switch_value);
1218
1219   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1220     {
1221       /* On a native system, sections must be aligned to 16 byte
1222          boundaries.  When configured for an embedded ELF target, we
1223          don't bother.  */
1224       if (strcmp (TARGET_OS, "elf") != 0)
1225         {
1226           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1227           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1228           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1229         }
1230
1231       /* Create a .reginfo section for register masks and a .mdebug
1232          section for debugging information.  */
1233       {
1234         segT seg;
1235         subsegT subseg;
1236         flagword flags;
1237         segT sec;
1238
1239         seg = now_seg;
1240         subseg = now_subseg;
1241
1242         /* The ABI says this section should be loaded so that the
1243            running program can access it.  However, we don't load it
1244            if we are configured for an embedded target */
1245         flags = SEC_READONLY | SEC_DATA;
1246         if (strcmp (TARGET_OS, "elf") != 0)
1247           flags |= SEC_ALLOC | SEC_LOAD;
1248
1249         if (mips_abi != N64_ABI)
1250           {
1251             sec = subseg_new (".reginfo", (subsegT) 0);
1252
1253             bfd_set_section_flags (stdoutput, sec, flags);
1254             bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1255
1256 #ifdef OBJ_ELF
1257             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1258 #endif
1259           }
1260         else
1261           {
1262             /* The 64-bit ABI uses a .MIPS.options section rather than
1263                .reginfo section.  */
1264             sec = subseg_new (".MIPS.options", (subsegT) 0);
1265             bfd_set_section_flags (stdoutput, sec, flags);
1266             bfd_set_section_alignment (stdoutput, sec, 3);
1267
1268 #ifdef OBJ_ELF
1269             /* Set up the option header.  */
1270             {
1271               Elf_Internal_Options opthdr;
1272               char *f;
1273
1274               opthdr.kind = ODK_REGINFO;
1275               opthdr.size = (sizeof (Elf_External_Options)
1276                              + sizeof (Elf64_External_RegInfo));
1277               opthdr.section = 0;
1278               opthdr.info = 0;
1279               f = frag_more (sizeof (Elf_External_Options));
1280               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1281                                              (Elf_External_Options *) f);
1282
1283               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1284             }
1285 #endif
1286           }
1287
1288         if (ECOFF_DEBUGGING)
1289           {
1290             sec = subseg_new (".mdebug", (subsegT) 0);
1291             (void) bfd_set_section_flags (stdoutput, sec,
1292                                           SEC_HAS_CONTENTS | SEC_READONLY);
1293             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1294           }
1295 #ifdef OBJ_ELF
1296         else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1297           {
1298             pdr_seg = subseg_new (".pdr", (subsegT) 0);
1299             (void) bfd_set_section_flags (stdoutput, pdr_seg,
1300                                           SEC_READONLY | SEC_RELOC
1301                                           | SEC_DEBUGGING);
1302             (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1303           }
1304 #endif
1305
1306         subseg_set (seg, subseg);
1307       }
1308     }
1309
1310   if (! ECOFF_DEBUGGING)
1311     md_obj_begin ();
1312 }
1313
1314 void
1315 md_mips_end (void)
1316 {
1317   if (! ECOFF_DEBUGGING)
1318     md_obj_end ();
1319 }
1320
1321 void
1322 md_assemble (char *str)
1323 {
1324   struct mips_cl_insn insn;
1325   bfd_reloc_code_real_type unused_reloc[3]
1326     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1327
1328   imm_expr.X_op = O_absent;
1329   imm2_expr.X_op = O_absent;
1330   offset_expr.X_op = O_absent;
1331   imm_reloc[0] = BFD_RELOC_UNUSED;
1332   imm_reloc[1] = BFD_RELOC_UNUSED;
1333   imm_reloc[2] = BFD_RELOC_UNUSED;
1334   offset_reloc[0] = BFD_RELOC_UNUSED;
1335   offset_reloc[1] = BFD_RELOC_UNUSED;
1336   offset_reloc[2] = BFD_RELOC_UNUSED;
1337
1338   if (mips_opts.mips16)
1339     mips16_ip (str, &insn);
1340   else
1341     {
1342       mips_ip (str, &insn);
1343       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1344             str, insn.insn_opcode));
1345     }
1346
1347   if (insn_error)
1348     {
1349       as_bad ("%s `%s'", insn_error, str);
1350       return;
1351     }
1352
1353   if (insn.insn_mo->pinfo == INSN_MACRO)
1354     {
1355       if (mips_opts.mips16)
1356         mips16_macro (&insn);
1357       else
1358         macro (&insn);
1359     }
1360   else
1361     {
1362       if (imm_expr.X_op != O_absent)
1363         append_insn (NULL, &insn, &imm_expr, imm_reloc);
1364       else if (offset_expr.X_op != O_absent)
1365         append_insn (NULL, &insn, &offset_expr, offset_reloc);
1366       else
1367         append_insn (NULL, &insn, NULL, unused_reloc);
1368     }
1369 }
1370
1371 /* Return true if the given relocation might need a matching %lo().
1372    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1373    applied to local symbols.  */
1374
1375 static inline bfd_boolean
1376 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1377 {
1378   return (reloc == BFD_RELOC_HI16_S
1379           || reloc == BFD_RELOC_MIPS_GOT16);
1380 }
1381
1382 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1383    relocation.  */
1384
1385 static inline bfd_boolean
1386 fixup_has_matching_lo_p (fixS *fixp)
1387 {
1388   return (fixp->fx_next != NULL
1389           && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1390           && fixp->fx_addsy == fixp->fx_next->fx_addsy
1391           && fixp->fx_offset == fixp->fx_next->fx_offset);
1392 }
1393
1394 /* See whether instruction IP reads register REG.  CLASS is the type
1395    of register.  */
1396
1397 static int
1398 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1399                enum mips_regclass class)
1400 {
1401   if (class == MIPS16_REG)
1402     {
1403       assert (mips_opts.mips16);
1404       reg = mips16_to_32_reg_map[reg];
1405       class = MIPS_GR_REG;
1406     }
1407
1408   /* Don't report on general register ZERO, since it never changes.  */
1409   if (class == MIPS_GR_REG && reg == ZERO)
1410     return 0;
1411
1412   if (class == MIPS_FP_REG)
1413     {
1414       assert (! mips_opts.mips16);
1415       /* If we are called with either $f0 or $f1, we must check $f0.
1416          This is not optimal, because it will introduce an unnecessary
1417          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1418          need to distinguish reading both $f0 and $f1 or just one of
1419          them.  Note that we don't have to check the other way,
1420          because there is no instruction that sets both $f0 and $f1
1421          and requires a delay.  */
1422       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1423           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1424               == (reg &~ (unsigned) 1)))
1425         return 1;
1426       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1427           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1428               == (reg &~ (unsigned) 1)))
1429         return 1;
1430     }
1431   else if (! mips_opts.mips16)
1432     {
1433       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1434           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1435         return 1;
1436       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1437           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1438         return 1;
1439     }
1440   else
1441     {
1442       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1443           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1444                                     & MIPS16OP_MASK_RX)]
1445               == reg))
1446         return 1;
1447       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1448           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1449                                     & MIPS16OP_MASK_RY)]
1450               == reg))
1451         return 1;
1452       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1453           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1454                                     & MIPS16OP_MASK_MOVE32Z)]
1455               == reg))
1456         return 1;
1457       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1458         return 1;
1459       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1460         return 1;
1461       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1462         return 1;
1463       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1464           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1465               & MIPS16OP_MASK_REGR32) == reg)
1466         return 1;
1467     }
1468
1469   return 0;
1470 }
1471
1472 /* This function returns true if modifying a register requires a
1473    delay.  */
1474
1475 static int
1476 reg_needs_delay (unsigned int reg)
1477 {
1478   unsigned long prev_pinfo;
1479
1480   prev_pinfo = prev_insn.insn_mo->pinfo;
1481   if (! mips_opts.noreorder
1482       && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1483       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484           || (! gpr_interlocks
1485               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1486     {
1487       /* A load from a coprocessor or from memory.  All load
1488          delays delay the use of general register rt for one
1489          instruction on the r3000.  The r6000 and r4000 use
1490          interlocks.  */
1491       /* Itbl support may require additional care here.  */
1492       know (prev_pinfo & INSN_WRITE_GPR_T);
1493       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1494         return 1;
1495     }
1496
1497   return 0;
1498 }
1499
1500 /* Mark instruction labels in mips16 mode.  This permits the linker to
1501    handle them specially, such as generating jalx instructions when
1502    needed.  We also make them odd for the duration of the assembly, in
1503    order to generate the right sort of code.  We will make them even
1504    in the adjust_symtab routine, while leaving them marked.  This is
1505    convenient for the debugger and the disassembler.  The linker knows
1506    to make them odd again.  */
1507
1508 static void
1509 mips16_mark_labels (void)
1510 {
1511   if (mips_opts.mips16)
1512     {
1513       struct insn_label_list *l;
1514       valueT val;
1515
1516       for (l = insn_labels; l != NULL; l = l->next)
1517         {
1518 #ifdef OBJ_ELF
1519           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1520             S_SET_OTHER (l->label, STO_MIPS16);
1521 #endif
1522           val = S_GET_VALUE (l->label);
1523           if ((val & 1) == 0)
1524             S_SET_VALUE (l->label, val + 1);
1525         }
1526     }
1527 }
1528
1529 /* Output an instruction.  PLACE is where to put the instruction; if
1530    it is NULL, this uses frag_more to get room.  IP is the instruction
1531    information.  ADDRESS_EXPR is an operand of the instruction to be
1532    used with RELOC_TYPE.  */
1533
1534 static void
1535 append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1536              bfd_reloc_code_real_type *reloc_type)
1537 {
1538   register unsigned long prev_pinfo, pinfo;
1539   char *f;
1540   fixS *fixp[3];
1541   int nops = 0;
1542   bfd_boolean force_new_frag = FALSE;
1543
1544   /* Mark instruction labels in mips16 mode.  */
1545   mips16_mark_labels ();
1546
1547   prev_pinfo = prev_insn.insn_mo->pinfo;
1548   pinfo = ip->insn_mo->pinfo;
1549
1550   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1551     {
1552       int prev_prev_nop;
1553
1554       /* If the previous insn required any delay slots, see if we need
1555          to insert a NOP or two.  There are eight kinds of possible
1556          hazards, of which an instruction can have at most one type.
1557          (1) a load from memory delay
1558          (2) a load from a coprocessor delay
1559          (3) an unconditional branch delay
1560          (4) a conditional branch delay
1561          (5) a move to coprocessor register delay
1562          (6) a load coprocessor register from memory delay
1563          (7) a coprocessor condition code delay
1564          (8) a HI/LO special register delay
1565
1566          There are a lot of optimizations we could do that we don't.
1567          In particular, we do not, in general, reorder instructions.
1568          If you use gcc with optimization, it will reorder
1569          instructions and generally do much more optimization then we
1570          do here; repeating all that work in the assembler would only
1571          benefit hand written assembly code, and does not seem worth
1572          it.  */
1573
1574       /* This is how a NOP is emitted.  */
1575 #define emit_nop()                                      \
1576   (mips_opts.mips16                                     \
1577    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1578    : md_number_to_chars (frag_more (4), 0, 4))
1579
1580       /* The previous insn might require a delay slot, depending upon
1581          the contents of the current insn.  */
1582       if (! mips_opts.mips16
1583           && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1584           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1585                && ! cop_interlocks)
1586               || (! gpr_interlocks
1587                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1588         {
1589           /* A load from a coprocessor or from memory.  All load
1590              delays delay the use of general register rt for one
1591              instruction on the r3000.  The r6000 and r4000 use
1592              interlocks.  */
1593           /* Itbl support may require additional care here.  */
1594           know (prev_pinfo & INSN_WRITE_GPR_T);
1595           if (mips_optimize == 0
1596               || insn_uses_reg (ip,
1597                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1598                                  & OP_MASK_RT),
1599                                 MIPS_GR_REG))
1600             ++nops;
1601         }
1602       else if (! mips_opts.mips16
1603                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1604                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1605                     && ! cop_interlocks)
1606                    || (mips_opts.isa == ISA_MIPS1
1607                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1608         {
1609           /* A generic coprocessor delay.  The previous instruction
1610              modified a coprocessor general or control register.  If
1611              it modified a control register, we need to avoid any
1612              coprocessor instruction (this is probably not always
1613              required, but it sometimes is).  If it modified a general
1614              register, we avoid using that register.
1615
1616              On the r6000 and r4000 loading a coprocessor register
1617              from memory is interlocked, and does not require a delay.
1618
1619              This case is not handled very well.  There is no special
1620              knowledge of CP0 handling, and the coprocessors other
1621              than the floating point unit are not distinguished at
1622              all.  */
1623           /* Itbl support may require additional care here. FIXME!
1624              Need to modify this to include knowledge about
1625              user specified delays!  */
1626           if (prev_pinfo & INSN_WRITE_FPR_T)
1627             {
1628               if (mips_optimize == 0
1629                   || insn_uses_reg (ip,
1630                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1631                                      & OP_MASK_FT),
1632                                     MIPS_FP_REG))
1633                 ++nops;
1634             }
1635           else if (prev_pinfo & INSN_WRITE_FPR_S)
1636             {
1637               if (mips_optimize == 0
1638                   || insn_uses_reg (ip,
1639                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1640                                      & OP_MASK_FS),
1641                                     MIPS_FP_REG))
1642                 ++nops;
1643             }
1644           else
1645             {
1646               /* We don't know exactly what the previous instruction
1647                  does.  If the current instruction uses a coprocessor
1648                  register, we must insert a NOP.  If previous
1649                  instruction may set the condition codes, and the
1650                  current instruction uses them, we must insert two
1651                  NOPS.  */
1652               /* Itbl support may require additional care here.  */
1653               if (mips_optimize == 0
1654                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1655                       && (pinfo & INSN_READ_COND_CODE)))
1656                 nops += 2;
1657               else if (pinfo & INSN_COP)
1658                 ++nops;
1659             }
1660         }
1661       else if (! mips_opts.mips16
1662                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1663                && (prev_pinfo & INSN_WRITE_COND_CODE)
1664                && ! cop_interlocks)
1665         {
1666           /* The previous instruction sets the coprocessor condition
1667              codes, but does not require a general coprocessor delay
1668              (this means it is a floating point comparison
1669              instruction).  If this instruction uses the condition
1670              codes, we need to insert a single NOP.  */
1671           /* Itbl support may require additional care here.  */
1672           if (mips_optimize == 0
1673               || (pinfo & INSN_READ_COND_CODE))
1674             ++nops;
1675         }
1676
1677       /* If we're fixing up mfhi/mflo for the r7000 and the
1678          previous insn was an mfhi/mflo and the current insn
1679          reads the register that the mfhi/mflo wrote to, then
1680          insert two nops.  */
1681
1682       else if (mips_7000_hilo_fix
1683                && MF_HILO_INSN (prev_pinfo)
1684                && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1685                                       & OP_MASK_RD),
1686                                  MIPS_GR_REG))
1687         {
1688           nops += 2;
1689         }
1690
1691       /* If we're fixing up mfhi/mflo for the r7000 and the
1692          2nd previous insn was an mfhi/mflo and the current insn
1693          reads the register that the mfhi/mflo wrote to, then
1694          insert one nop.  */
1695
1696       else if (mips_7000_hilo_fix
1697                && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1698                && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1699                                        & OP_MASK_RD),
1700                                     MIPS_GR_REG))
1701
1702         {
1703           ++nops;
1704         }
1705
1706       else if (prev_pinfo & INSN_READ_LO)
1707         {
1708           /* The previous instruction reads the LO register; if the
1709              current instruction writes to the LO register, we must
1710              insert two NOPS.  Some newer processors have interlocks.
1711              Also the tx39's multiply instructions can be exectuted
1712              immediatly after a read from HI/LO (without the delay),
1713              though the tx39's divide insns still do require the
1714              delay.  */
1715           if (! (hilo_interlocks
1716                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1717               && (mips_optimize == 0
1718                   || (pinfo & INSN_WRITE_LO)))
1719             nops += 2;
1720           /* Most mips16 branch insns don't have a delay slot.
1721              If a read from LO is immediately followed by a branch
1722              to a write to LO we have a read followed by a write
1723              less than 2 insns away.  We assume the target of
1724              a branch might be a write to LO, and insert a nop
1725              between a read and an immediately following branch.  */
1726           else if (mips_opts.mips16
1727                    && (mips_optimize == 0
1728                        || (pinfo & MIPS16_INSN_BRANCH)))
1729             ++nops;
1730         }
1731       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1732         {
1733           /* The previous instruction reads the HI register; if the
1734              current instruction writes to the HI register, we must
1735              insert a NOP.  Some newer processors have interlocks.
1736              Also the note tx39's multiply above.  */
1737           if (! (hilo_interlocks
1738                  || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1739               && (mips_optimize == 0
1740                   || (pinfo & INSN_WRITE_HI)))
1741             nops += 2;
1742           /* Most mips16 branch insns don't have a delay slot.
1743              If a read from HI is immediately followed by a branch
1744              to a write to HI we have a read followed by a write
1745              less than 2 insns away.  We assume the target of
1746              a branch might be a write to HI, and insert a nop
1747              between a read and an immediately following branch.  */
1748           else if (mips_opts.mips16
1749                    && (mips_optimize == 0
1750                        || (pinfo & MIPS16_INSN_BRANCH)))
1751             ++nops;
1752         }
1753
1754       /* If the previous instruction was in a noreorder section, then
1755          we don't want to insert the nop after all.  */
1756       /* Itbl support may require additional care here.  */
1757       if (prev_insn_unreordered)
1758         nops = 0;
1759
1760       /* There are two cases which require two intervening
1761          instructions: 1) setting the condition codes using a move to
1762          coprocessor instruction which requires a general coprocessor
1763          delay and then reading the condition codes 2) reading the HI
1764          or LO register and then writing to it (except on processors
1765          which have interlocks).  If we are not already emitting a NOP
1766          instruction, we must check for these cases compared to the
1767          instruction previous to the previous instruction.  */
1768       if ((! mips_opts.mips16
1769            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1770            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1771            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1772            && (pinfo & INSN_READ_COND_CODE)
1773            && ! cop_interlocks)
1774           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1775               && (pinfo & INSN_WRITE_LO)
1776               && ! (hilo_interlocks
1777                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1778           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1779               && (pinfo & INSN_WRITE_HI)
1780               && ! (hilo_interlocks
1781                     || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1782         prev_prev_nop = 1;
1783       else
1784         prev_prev_nop = 0;
1785
1786       if (prev_prev_insn_unreordered)
1787         prev_prev_nop = 0;
1788
1789       if (prev_prev_nop && nops == 0)
1790         ++nops;
1791
1792       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1793         {
1794           /* We're out of bits in pinfo, so we must resort to string
1795              ops here.  Shortcuts are selected based on opcodes being
1796              limited to the VR4122 instruction set.  */
1797           int min_nops = 0;
1798           const char *pn = prev_insn.insn_mo->name;
1799           const char *tn = ip->insn_mo->name;
1800           if (strncmp(pn, "macc", 4) == 0
1801               || strncmp(pn, "dmacc", 5) == 0)
1802             {
1803               /* Errata 21 - [D]DIV[U] after [D]MACC */
1804               if (strstr (tn, "div"))
1805                 {
1806                   min_nops = 1;
1807                 }
1808
1809               /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1810               if (pn[0] == 'd' /* dmacc */
1811                   && (strncmp(tn, "dmult", 5) == 0
1812                       || strncmp(tn, "dmacc", 5) == 0))
1813                 {
1814                   min_nops = 1;
1815                 }
1816
1817               /* Errata 24 - MT{LO,HI} after [D]MACC */
1818               if (strcmp (tn, "mtlo") == 0
1819                   || strcmp (tn, "mthi") == 0)
1820                 {
1821                   min_nops = 1;
1822                 }
1823
1824             }
1825           else if (strncmp(pn, "dmult", 5) == 0
1826                    && (strncmp(tn, "dmult", 5) == 0
1827                        || strncmp(tn, "dmacc", 5) == 0))
1828             {
1829               /* Here is the rest of errata 23.  */
1830               min_nops = 1;
1831             }
1832           if (nops < min_nops)
1833             nops = min_nops;
1834         }
1835
1836       /* If we are being given a nop instruction, don't bother with
1837          one of the nops we would otherwise output.  This will only
1838          happen when a nop instruction is used with mips_optimize set
1839          to 0.  */
1840       if (nops > 0
1841           && ! mips_opts.noreorder
1842           && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1843         --nops;
1844
1845       /* Now emit the right number of NOP instructions.  */
1846       if (nops > 0 && ! mips_opts.noreorder)
1847         {
1848           fragS *old_frag;
1849           unsigned long old_frag_offset;
1850           int i;
1851           struct insn_label_list *l;
1852
1853           old_frag = frag_now;
1854           old_frag_offset = frag_now_fix ();
1855
1856           for (i = 0; i < nops; i++)
1857             emit_nop ();
1858
1859           if (listing)
1860             {
1861               listing_prev_line ();
1862               /* We may be at the start of a variant frag.  In case we
1863                  are, make sure there is enough space for the frag
1864                  after the frags created by listing_prev_line.  The
1865                  argument to frag_grow here must be at least as large
1866                  as the argument to all other calls to frag_grow in
1867                  this file.  We don't have to worry about being in the
1868                  middle of a variant frag, because the variants insert
1869                  all needed nop instructions themselves.  */
1870               frag_grow (40);
1871             }
1872
1873           for (l = insn_labels; l != NULL; l = l->next)
1874             {
1875               valueT val;
1876
1877               assert (S_GET_SEGMENT (l->label) == now_seg);
1878               symbol_set_frag (l->label, frag_now);
1879               val = (valueT) frag_now_fix ();
1880               /* mips16 text labels are stored as odd.  */
1881               if (mips_opts.mips16)
1882                 ++val;
1883               S_SET_VALUE (l->label, val);
1884             }
1885
1886 #ifndef NO_ECOFF_DEBUGGING
1887           if (ECOFF_DEBUGGING)
1888             ecoff_fix_loc (old_frag, old_frag_offset);
1889 #endif
1890         }
1891       else if (prev_nop_frag != NULL)
1892         {
1893           /* We have a frag holding nops we may be able to remove.  If
1894              we don't need any nops, we can decrease the size of
1895              prev_nop_frag by the size of one instruction.  If we do
1896              need some nops, we count them in prev_nops_required.  */
1897           if (prev_nop_frag_since == 0)
1898             {
1899               if (nops == 0)
1900                 {
1901                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1902                   --prev_nop_frag_holds;
1903                 }
1904               else
1905                 prev_nop_frag_required += nops;
1906             }
1907           else
1908             {
1909               if (prev_prev_nop == 0)
1910                 {
1911                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1912                   --prev_nop_frag_holds;
1913                 }
1914               else
1915                 ++prev_nop_frag_required;
1916             }
1917
1918           if (prev_nop_frag_holds <= prev_nop_frag_required)
1919             prev_nop_frag = NULL;
1920
1921           ++prev_nop_frag_since;
1922
1923           /* Sanity check: by the time we reach the second instruction
1924              after prev_nop_frag, we should have used up all the nops
1925              one way or another.  */
1926           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1927         }
1928     }
1929
1930   if (place == NULL
1931       && address_expr
1932       && *reloc_type == BFD_RELOC_16_PCREL_S2
1933       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1934           || pinfo & INSN_COND_BRANCH_LIKELY)
1935       && mips_relax_branch
1936       /* Don't try branch relaxation within .set nomacro, or within
1937          .set noat if we use $at for PIC computations.  If it turns
1938          out that the branch was out-of-range, we'll get an error.  */
1939       && !mips_opts.warn_about_macros
1940       && !(mips_opts.noat && mips_pic != NO_PIC)
1941       && !mips_opts.mips16)
1942     {
1943       f = frag_var (rs_machine_dependent,
1944                     relaxed_branch_length
1945                     (NULL, NULL,
1946                      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1947                      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1948                     RELAX_BRANCH_ENCODE
1949                     (pinfo & INSN_UNCOND_BRANCH_DELAY,
1950                      pinfo & INSN_COND_BRANCH_LIKELY,
1951                      pinfo & INSN_WRITE_GPR_31,
1952                      0),
1953                     address_expr->X_add_symbol,
1954                     address_expr->X_add_number,
1955                     0);
1956       *reloc_type = BFD_RELOC_UNUSED;
1957     }
1958   else if (*reloc_type > BFD_RELOC_UNUSED)
1959     {
1960       /* We need to set up a variant frag.  */
1961       assert (mips_opts.mips16 && address_expr != NULL);
1962       f = frag_var (rs_machine_dependent, 4, 0,
1963                     RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1964                                          mips16_small, mips16_ext,
1965                                          (prev_pinfo
1966                                           & INSN_UNCOND_BRANCH_DELAY),
1967                                          (*prev_insn_reloc_type
1968                                           == BFD_RELOC_MIPS16_JMP)),
1969                     make_expr_symbol (address_expr), 0, NULL);
1970     }
1971   else if (place != NULL)
1972     f = place;
1973   else if (mips_opts.mips16
1974            && ! ip->use_extend
1975            && *reloc_type != BFD_RELOC_MIPS16_JMP)
1976     {
1977       /* Make sure there is enough room to swap this instruction with
1978          a following jump instruction.  */
1979       frag_grow (6);
1980       f = frag_more (2);
1981     }
1982   else
1983     {
1984       if (mips_opts.mips16
1985           && mips_opts.noreorder
1986           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1987         as_warn (_("extended instruction in delay slot"));
1988
1989       f = frag_more (4);
1990     }
1991
1992   fixp[0] = fixp[1] = fixp[2] = NULL;
1993   if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1994     {
1995       if (address_expr->X_op == O_constant)
1996         {
1997           valueT tmp;
1998
1999           switch (*reloc_type)
2000             {
2001             case BFD_RELOC_32:
2002               ip->insn_opcode |= address_expr->X_add_number;
2003               break;
2004
2005             case BFD_RELOC_MIPS_HIGHEST:
2006               tmp = (address_expr->X_add_number
2007                      + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2008               tmp >>= 16;
2009               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2010               break;
2011
2012             case BFD_RELOC_MIPS_HIGHER:
2013               tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2014               ip->insn_opcode |= (tmp >> 16) & 0xffff;
2015               break;
2016
2017             case BFD_RELOC_HI16_S:
2018               ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2019                                   >> 16) & 0xffff;
2020               break;
2021
2022             case BFD_RELOC_HI16:
2023               ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2024               break;
2025
2026             case BFD_RELOC_LO16:
2027             case BFD_RELOC_MIPS_GOT_DISP:
2028               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2029               break;
2030
2031             case BFD_RELOC_MIPS_JMP:
2032               if ((address_expr->X_add_number & 3) != 0)
2033                 as_bad (_("jump to misaligned address (0x%lx)"),
2034                         (unsigned long) address_expr->X_add_number);
2035               if (address_expr->X_add_number & ~0xfffffff)
2036                 as_bad (_("jump address range overflow (0x%lx)"),
2037                         (unsigned long) address_expr->X_add_number);
2038               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2039               break;
2040
2041             case BFD_RELOC_MIPS16_JMP:
2042               if ((address_expr->X_add_number & 3) != 0)
2043                 as_bad (_("jump to misaligned address (0x%lx)"),
2044                         (unsigned long) address_expr->X_add_number);
2045               if (address_expr->X_add_number & ~0xfffffff)
2046                 as_bad (_("jump address range overflow (0x%lx)"),
2047                         (unsigned long) address_expr->X_add_number);
2048               ip->insn_opcode |=
2049                 (((address_expr->X_add_number & 0x7c0000) << 3)
2050                  | ((address_expr->X_add_number & 0xf800000) >> 7)
2051                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
2052               break;
2053
2054             case BFD_RELOC_16_PCREL_S2:
2055               goto need_reloc;
2056
2057             default:
2058               internalError ();
2059             }
2060         }
2061       else
2062         {
2063         need_reloc:
2064           /* Don't generate a reloc if we are writing into a variant frag.  */
2065           if (place == NULL)
2066             {
2067               reloc_howto_type *howto;
2068               int i;
2069
2070               /* In a compound relocation, it is the final (outermost)
2071                  operator that determines the relocated field.  */
2072               for (i = 1; i < 3; i++)
2073                 if (reloc_type[i] == BFD_RELOC_UNUSED)
2074                   break;
2075
2076               howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2077               fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2078                                      bfd_get_reloc_size(howto),
2079                                      address_expr,
2080                                      reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2081                                      reloc_type[0]);
2082
2083               /* These relocations can have an addend that won't fit in
2084                  4 octets for 64bit assembly.  */
2085               if (HAVE_64BIT_GPRS
2086                   && ! howto->partial_inplace
2087                   && (reloc_type[0] == BFD_RELOC_16
2088                       || reloc_type[0] == BFD_RELOC_32
2089                       || reloc_type[0] == BFD_RELOC_MIPS_JMP
2090                       || reloc_type[0] == BFD_RELOC_HI16_S
2091                       || reloc_type[0] == BFD_RELOC_LO16
2092                       || reloc_type[0] == BFD_RELOC_GPREL16
2093                       || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2094                       || reloc_type[0] == BFD_RELOC_GPREL32
2095                       || reloc_type[0] == BFD_RELOC_64
2096                       || reloc_type[0] == BFD_RELOC_CTOR
2097                       || reloc_type[0] == BFD_RELOC_MIPS_SUB
2098                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2099                       || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2100                       || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2101                       || reloc_type[0] == BFD_RELOC_MIPS_REL16
2102                       || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2103                 fixp[0]->fx_no_overflow = 1;
2104
2105               if (reloc_needs_lo_p (*reloc_type))
2106                 {
2107                   struct mips_hi_fixup *hi_fixup;
2108
2109                   /* Reuse the last entry if it already has a matching %lo.  */
2110                   hi_fixup = mips_hi_fixup_list;
2111                   if (hi_fixup == 0
2112                       || !fixup_has_matching_lo_p (hi_fixup->fixp))
2113                     {
2114                       hi_fixup = ((struct mips_hi_fixup *)
2115                                   xmalloc (sizeof (struct mips_hi_fixup)));
2116                       hi_fixup->next = mips_hi_fixup_list;
2117                       mips_hi_fixup_list = hi_fixup;
2118                     }
2119                   hi_fixup->fixp = fixp[0];
2120                   hi_fixup->seg = now_seg;
2121                 }
2122
2123               /* Add fixups for the second and third relocations, if given.
2124                  Note that the ABI allows the second relocation to be
2125                  against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2126                  moment we only use RSS_UNDEF, but we could add support
2127                  for the others if it ever becomes necessary.  */
2128               for (i = 1; i < 3; i++)
2129                 if (reloc_type[i] != BFD_RELOC_UNUSED)
2130                   {
2131                     address_expr->X_op = O_absent;
2132                     address_expr->X_add_symbol = 0;
2133                     address_expr->X_add_number = 0;
2134
2135                     fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2136                                            fixp[0]->fx_size, address_expr,
2137                                            FALSE, reloc_type[i]);
2138                   }
2139             }
2140         }
2141     }
2142
2143   if (! mips_opts.mips16)
2144     {
2145       md_number_to_chars (f, ip->insn_opcode, 4);
2146 #ifdef OBJ_ELF
2147       dwarf2_emit_insn (4);
2148 #endif
2149     }
2150   else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2151     {
2152       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2153       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2154 #ifdef OBJ_ELF
2155       dwarf2_emit_insn (4);
2156 #endif
2157     }
2158   else
2159     {
2160       if (ip->use_extend)
2161         {
2162           md_number_to_chars (f, 0xf000 | ip->extend, 2);
2163           f += 2;
2164         }
2165       md_number_to_chars (f, ip->insn_opcode, 2);
2166 #ifdef OBJ_ELF
2167       dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2168 #endif
2169     }
2170
2171   /* Update the register mask information.  */
2172   if (! mips_opts.mips16)
2173     {
2174       if (pinfo & INSN_WRITE_GPR_D)
2175         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2176       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2177         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2178       if (pinfo & INSN_READ_GPR_S)
2179         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2180       if (pinfo & INSN_WRITE_GPR_31)
2181         mips_gprmask |= 1 << RA;
2182       if (pinfo & INSN_WRITE_FPR_D)
2183         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2184       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2185         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2186       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2187         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2188       if ((pinfo & INSN_READ_FPR_R) != 0)
2189         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2190       if (pinfo & INSN_COP)
2191         {
2192           /* We don't keep enough information to sort these cases out.
2193              The itbl support does keep this information however, although
2194              we currently don't support itbl fprmats as part of the cop
2195              instruction.  May want to add this support in the future.  */
2196         }
2197       /* Never set the bit for $0, which is always zero.  */
2198       mips_gprmask &= ~1 << 0;
2199     }
2200   else
2201     {
2202       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2203         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2204                               & MIPS16OP_MASK_RX);
2205       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2206         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2207                               & MIPS16OP_MASK_RY);
2208       if (pinfo & MIPS16_INSN_WRITE_Z)
2209         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2210                               & MIPS16OP_MASK_RZ);
2211       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2212         mips_gprmask |= 1 << TREG;
2213       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2214         mips_gprmask |= 1 << SP;
2215       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2216         mips_gprmask |= 1 << RA;
2217       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2218         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2219       if (pinfo & MIPS16_INSN_READ_Z)
2220         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2221                               & MIPS16OP_MASK_MOVE32Z);
2222       if (pinfo & MIPS16_INSN_READ_GPR_X)
2223         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2224                               & MIPS16OP_MASK_REGR32);
2225     }
2226
2227   if (place == NULL && ! mips_opts.noreorder)
2228     {
2229       /* Filling the branch delay slot is more complex.  We try to
2230          switch the branch with the previous instruction, which we can
2231          do if the previous instruction does not set up a condition
2232          that the branch tests and if the branch is not itself the
2233          target of any branch.  */
2234       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2235           || (pinfo & INSN_COND_BRANCH_DELAY))
2236         {
2237           if (mips_optimize < 2
2238               /* If we have seen .set volatile or .set nomove, don't
2239                  optimize.  */
2240               || mips_opts.nomove != 0
2241               /* If we had to emit any NOP instructions, then we
2242                  already know we can not swap.  */
2243               || nops != 0
2244               /* If we don't even know the previous insn, we can not
2245                  swap.  */
2246               || ! prev_insn_valid
2247               /* If the previous insn is already in a branch delay
2248                  slot, then we can not swap.  */
2249               || prev_insn_is_delay_slot
2250               /* If the previous previous insn was in a .set
2251                  noreorder, we can't swap.  Actually, the MIPS
2252                  assembler will swap in this situation.  However, gcc
2253                  configured -with-gnu-as will generate code like
2254                    .set noreorder
2255                    lw   $4,XXX
2256                    .set reorder
2257                    INSN
2258                    bne  $4,$0,foo
2259                  in which we can not swap the bne and INSN.  If gcc is
2260                  not configured -with-gnu-as, it does not output the
2261                  .set pseudo-ops.  We don't have to check
2262                  prev_insn_unreordered, because prev_insn_valid will
2263                  be 0 in that case.  We don't want to use
2264                  prev_prev_insn_valid, because we do want to be able
2265                  to swap at the start of a function.  */
2266               || prev_prev_insn_unreordered
2267               /* If the branch is itself the target of a branch, we
2268                  can not swap.  We cheat on this; all we check for is
2269                  whether there is a label on this instruction.  If
2270                  there are any branches to anything other than a
2271                  label, users must use .set noreorder.  */
2272               || insn_labels != NULL
2273               /* If the previous instruction is in a variant frag, we
2274                  can not do the swap.  This does not apply to the
2275                  mips16, which uses variant frags for different
2276                  purposes.  */
2277               || (! mips_opts.mips16
2278                   && prev_insn_frag->fr_type == rs_machine_dependent)
2279               /* If the branch reads the condition codes, we don't
2280                  even try to swap, because in the sequence
2281                    ctc1 $X,$31
2282                    INSN
2283                    INSN
2284                    bc1t LABEL
2285                  we can not swap, and I don't feel like handling that
2286                  case.  */
2287               || (! mips_opts.mips16
2288                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2289                   && (pinfo & INSN_READ_COND_CODE))
2290               /* We can not swap with an instruction that requires a
2291                  delay slot, becase the target of the branch might
2292                  interfere with that instruction.  */
2293               || (! mips_opts.mips16
2294                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2295                   && (prev_pinfo
2296               /* Itbl support may require additional care here.  */
2297                       & (INSN_LOAD_COPROC_DELAY
2298                          | INSN_COPROC_MOVE_DELAY
2299                          | INSN_WRITE_COND_CODE)))
2300               || (! (hilo_interlocks
2301                      || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2302                   && (prev_pinfo
2303                       & (INSN_READ_LO
2304                          | INSN_READ_HI)))
2305               || (! mips_opts.mips16
2306                   && ! gpr_interlocks
2307                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2308               || (! mips_opts.mips16
2309                   && mips_opts.isa == ISA_MIPS1
2310                   /* Itbl support may require additional care here.  */
2311                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2312               /* We can not swap with a branch instruction.  */
2313               || (prev_pinfo
2314                   & (INSN_UNCOND_BRANCH_DELAY
2315                      | INSN_COND_BRANCH_DELAY
2316                      | INSN_COND_BRANCH_LIKELY))
2317               /* We do not swap with a trap instruction, since it
2318                  complicates trap handlers to have the trap
2319                  instruction be in a delay slot.  */
2320               || (prev_pinfo & INSN_TRAP)
2321               /* If the branch reads a register that the previous
2322                  instruction sets, we can not swap.  */
2323               || (! mips_opts.mips16
2324                   && (prev_pinfo & INSN_WRITE_GPR_T)
2325                   && insn_uses_reg (ip,
2326                                     ((prev_insn.insn_opcode >> OP_SH_RT)
2327                                      & OP_MASK_RT),
2328                                     MIPS_GR_REG))
2329               || (! mips_opts.mips16
2330                   && (prev_pinfo & INSN_WRITE_GPR_D)
2331                   && insn_uses_reg (ip,
2332                                     ((prev_insn.insn_opcode >> OP_SH_RD)
2333                                      & OP_MASK_RD),
2334                                     MIPS_GR_REG))
2335               || (mips_opts.mips16
2336                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2337                        && insn_uses_reg (ip,
2338                                          ((prev_insn.insn_opcode
2339                                            >> MIPS16OP_SH_RX)
2340                                           & MIPS16OP_MASK_RX),
2341                                          MIPS16_REG))
2342                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2343                           && insn_uses_reg (ip,
2344                                             ((prev_insn.insn_opcode
2345                                               >> MIPS16OP_SH_RY)
2346                                              & MIPS16OP_MASK_RY),
2347                                             MIPS16_REG))
2348                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2349                           && insn_uses_reg (ip,
2350                                             ((prev_insn.insn_opcode
2351                                               >> MIPS16OP_SH_RZ)
2352                                              & MIPS16OP_MASK_RZ),
2353                                             MIPS16_REG))
2354                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2355                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2356                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2357                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
2358                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2359                           && insn_uses_reg (ip,
2360                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
2361                                                                      insn_opcode),
2362                                             MIPS_GR_REG))))
2363               /* If the branch writes a register that the previous
2364                  instruction sets, we can not swap (we know that
2365                  branches write only to RD or to $31).  */
2366               || (! mips_opts.mips16
2367                   && (prev_pinfo & INSN_WRITE_GPR_T)
2368                   && (((pinfo & INSN_WRITE_GPR_D)
2369                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2370                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2371                       || ((pinfo & INSN_WRITE_GPR_31)
2372                           && (((prev_insn.insn_opcode >> OP_SH_RT)
2373                                & OP_MASK_RT)
2374                               == RA))))
2375               || (! mips_opts.mips16
2376                   && (prev_pinfo & INSN_WRITE_GPR_D)
2377                   && (((pinfo & INSN_WRITE_GPR_D)
2378                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2379                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2380                       || ((pinfo & INSN_WRITE_GPR_31)
2381                           && (((prev_insn.insn_opcode >> OP_SH_RD)
2382                                & OP_MASK_RD)
2383                               == RA))))
2384               || (mips_opts.mips16
2385                   && (pinfo & MIPS16_INSN_WRITE_31)
2386                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2387                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2388                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2389                               == RA))))
2390               /* If the branch writes a register that the previous
2391                  instruction reads, we can not swap (we know that
2392                  branches only write to RD or to $31).  */
2393               || (! mips_opts.mips16
2394                   && (pinfo & INSN_WRITE_GPR_D)
2395                   && insn_uses_reg (&prev_insn,
2396                                     ((ip->insn_opcode >> OP_SH_RD)
2397                                      & OP_MASK_RD),
2398                                     MIPS_GR_REG))
2399               || (! mips_opts.mips16
2400                   && (pinfo & INSN_WRITE_GPR_31)
2401                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2402               || (mips_opts.mips16
2403                   && (pinfo & MIPS16_INSN_WRITE_31)
2404                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2405               /* If we are generating embedded PIC code, the branch
2406                  might be expanded into a sequence which uses $at, so
2407                  we can't swap with an instruction which reads it.  */
2408               || (mips_pic == EMBEDDED_PIC
2409                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2410               /* If the previous previous instruction has a load
2411                  delay, and sets a register that the branch reads, we
2412                  can not swap.  */
2413               || (! mips_opts.mips16
2414                   && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2415               /* Itbl support may require additional care here.  */
2416                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2417                       || (! gpr_interlocks
2418                           && (prev_prev_insn.insn_mo->pinfo
2419                               & INSN_LOAD_MEMORY_DELAY)))
2420                   && insn_uses_reg (ip,
2421                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2422                                      & OP_MASK_RT),
2423                                     MIPS_GR_REG))
2424               /* If one instruction sets a condition code and the
2425                  other one uses a condition code, we can not swap.  */
2426               || ((pinfo & INSN_READ_COND_CODE)
2427                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2428               || ((pinfo & INSN_WRITE_COND_CODE)
2429                   && (prev_pinfo & INSN_READ_COND_CODE))
2430               /* If the previous instruction uses the PC, we can not
2431                  swap.  */
2432               || (mips_opts.mips16
2433                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2434               /* If the previous instruction was extended, we can not
2435                  swap.  */
2436               || (mips_opts.mips16 && prev_insn_extended)
2437               /* If the previous instruction had a fixup in mips16
2438                  mode, we can not swap.  This normally means that the
2439                  previous instruction was a 4 byte branch anyhow.  */
2440               || (mips_opts.mips16 && prev_insn_fixp[0])
2441               /* If the previous instruction is a sync, sync.l, or
2442                  sync.p, we can not swap.  */
2443               || (prev_pinfo & INSN_SYNC))
2444             {
2445               /* We could do even better for unconditional branches to
2446                  portions of this object file; we could pick up the
2447                  instruction at the destination, put it in the delay
2448                  slot, and bump the destination address.  */
2449               emit_nop ();
2450               /* Update the previous insn information.  */
2451               prev_prev_insn = *ip;
2452               prev_insn.insn_mo = &dummy_opcode;
2453             }
2454           else
2455             {
2456               /* It looks like we can actually do the swap.  */
2457               if (! mips_opts.mips16)
2458                 {
2459                   char *prev_f;
2460                   char temp[4];
2461
2462                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2463                   memcpy (temp, prev_f, 4);
2464                   memcpy (prev_f, f, 4);
2465                   memcpy (f, temp, 4);
2466                   if (prev_insn_fixp[0])
2467                     {
2468                       prev_insn_fixp[0]->fx_frag = frag_now;
2469                       prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2470                     }
2471                   if (prev_insn_fixp[1])
2472                     {
2473                       prev_insn_fixp[1]->fx_frag = frag_now;
2474                       prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2475                     }
2476                   if (prev_insn_fixp[2])
2477                     {
2478                       prev_insn_fixp[2]->fx_frag = frag_now;
2479                       prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2480                     }
2481                   if (prev_insn_fixp[0] && HAVE_NEWABI
2482                       && prev_insn_frag != frag_now
2483                       && (prev_insn_fixp[0]->fx_r_type
2484                           == BFD_RELOC_MIPS_GOT_DISP
2485                           || (prev_insn_fixp[0]->fx_r_type
2486                               == BFD_RELOC_MIPS_CALL16)))
2487                     {
2488                       /* To avoid confusion in tc_gen_reloc, we must
2489                          ensure that this does not become a variant
2490                          frag.  */
2491                       force_new_frag = TRUE;
2492                     }
2493                   if (fixp[0])
2494                     {
2495                       fixp[0]->fx_frag = prev_insn_frag;
2496                       fixp[0]->fx_where = prev_insn_where;
2497                     }
2498                   if (fixp[1])
2499                     {
2500                       fixp[1]->fx_frag = prev_insn_frag;
2501                       fixp[1]->fx_where = prev_insn_where;
2502                     }
2503                   if (fixp[2])
2504                     {
2505                       fixp[2]->fx_frag = prev_insn_frag;
2506                       fixp[2]->fx_where = prev_insn_where;
2507                     }
2508                 }
2509               else
2510                 {
2511                   char *prev_f;
2512                   char temp[2];
2513
2514                   assert (prev_insn_fixp[0] == NULL);
2515                   assert (prev_insn_fixp[1] == NULL);
2516                   assert (prev_insn_fixp[2] == NULL);
2517                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2518                   memcpy (temp, prev_f, 2);
2519                   memcpy (prev_f, f, 2);
2520                   if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2521                     {
2522                       assert (*reloc_type == BFD_RELOC_UNUSED);
2523                       memcpy (f, temp, 2);
2524                     }
2525                   else
2526                     {
2527                       memcpy (f, f + 2, 2);
2528                       memcpy (f + 2, temp, 2);
2529                     }
2530                   if (fixp[0])
2531                     {
2532                       fixp[0]->fx_frag = prev_insn_frag;
2533                       fixp[0]->fx_where = prev_insn_where;
2534                     }
2535                   if (fixp[1])
2536                     {
2537                       fixp[1]->fx_frag = prev_insn_frag;
2538                       fixp[1]->fx_where = prev_insn_where;
2539                     }
2540                   if (fixp[2])
2541                     {
2542                       fixp[2]->fx_frag = prev_insn_frag;
2543                       fixp[2]->fx_where = prev_insn_where;
2544                     }
2545                 }
2546
2547               /* Update the previous insn information; leave prev_insn
2548                  unchanged.  */
2549               prev_prev_insn = *ip;
2550             }
2551           prev_insn_is_delay_slot = 1;
2552
2553           /* If that was an unconditional branch, forget the previous
2554              insn information.  */
2555           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2556             {
2557               prev_prev_insn.insn_mo = &dummy_opcode;
2558               prev_insn.insn_mo = &dummy_opcode;
2559             }
2560
2561           prev_insn_fixp[0] = NULL;
2562           prev_insn_fixp[1] = NULL;
2563           prev_insn_fixp[2] = NULL;
2564           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2565           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2566           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2567           prev_insn_extended = 0;
2568         }
2569       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2570         {
2571           /* We don't yet optimize a branch likely.  What we should do
2572              is look at the target, copy the instruction found there
2573              into the delay slot, and increment the branch to jump to
2574              the next instruction.  */
2575           emit_nop ();
2576           /* Update the previous insn information.  */
2577           prev_prev_insn = *ip;
2578           prev_insn.insn_mo = &dummy_opcode;
2579           prev_insn_fixp[0] = NULL;
2580           prev_insn_fixp[1] = NULL;
2581           prev_insn_fixp[2] = NULL;
2582           prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2583           prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2584           prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2585           prev_insn_extended = 0;
2586         }
2587       else
2588         {
2589           /* Update the previous insn information.  */
2590           if (nops > 0)
2591             prev_prev_insn.insn_mo = &dummy_opcode;
2592           else
2593             prev_prev_insn = prev_insn;
2594           prev_insn = *ip;
2595
2596           /* Any time we see a branch, we always fill the delay slot
2597              immediately; since this insn is not a branch, we know it
2598              is not in a delay slot.  */
2599           prev_insn_is_delay_slot = 0;
2600
2601           prev_insn_fixp[0] = fixp[0];
2602           prev_insn_fixp[1] = fixp[1];
2603           prev_insn_fixp[2] = fixp[2];
2604           prev_insn_reloc_type[0] = reloc_type[0];
2605           prev_insn_reloc_type[1] = reloc_type[1];
2606           prev_insn_reloc_type[2] = reloc_type[2];
2607           if (mips_opts.mips16)
2608             prev_insn_extended = (ip->use_extend
2609                                   || *reloc_type > BFD_RELOC_UNUSED);
2610         }
2611
2612       prev_prev_insn_unreordered = prev_insn_unreordered;
2613       prev_insn_unreordered = 0;
2614       prev_insn_frag = frag_now;
2615       prev_insn_where = f - frag_now->fr_literal;
2616       prev_insn_valid = 1;
2617     }
2618   else if (place == NULL)
2619     {
2620       /* We need to record a bit of information even when we are not
2621          reordering, in order to determine the base address for mips16
2622          PC relative relocs.  */
2623       prev_prev_insn = prev_insn;
2624       prev_insn = *ip;
2625       prev_insn_reloc_type[0] = reloc_type[0];
2626       prev_insn_reloc_type[1] = reloc_type[1];
2627       prev_insn_reloc_type[2] = reloc_type[2];
2628       prev_prev_insn_unreordered = prev_insn_unreordered;
2629       prev_insn_unreordered = 1;
2630     }
2631
2632   /* We just output an insn, so the next one doesn't have a label.  */
2633   mips_clear_insn_labels ();
2634
2635   /* We must ensure that the frag to which an instruction that was
2636      moved from a non-variant frag doesn't become a variant frag,
2637      otherwise tc_gen_reloc may get confused.  */
2638   if (force_new_frag)
2639     {
2640       frag_wane (frag_now);
2641       frag_new (0);
2642     }
2643 }
2644
2645 /* This function forgets that there was any previous instruction or
2646    label.  If PRESERVE is non-zero, it remembers enough information to
2647    know whether nops are needed before a noreorder section.  */
2648
2649 static void
2650 mips_no_prev_insn (int preserve)
2651 {
2652   if (! preserve)
2653     {
2654       prev_insn.insn_mo = &dummy_opcode;
2655       prev_prev_insn.insn_mo = &dummy_opcode;
2656       prev_nop_frag = NULL;
2657       prev_nop_frag_holds = 0;
2658       prev_nop_frag_required = 0;
2659       prev_nop_frag_since = 0;
2660     }
2661   prev_insn_valid = 0;
2662   prev_insn_is_delay_slot = 0;
2663   prev_insn_unreordered = 0;
2664   prev_insn_extended = 0;
2665   prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2666   prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2667   prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2668   prev_prev_insn_unreordered = 0;
2669   mips_clear_insn_labels ();
2670 }
2671
2672 /* This function must be called whenever we turn on noreorder or emit
2673    something other than instructions.  It inserts any NOPS which might
2674    be needed by the previous instruction, and clears the information
2675    kept for the previous instructions.  The INSNS parameter is true if
2676    instructions are to follow.  */
2677
2678 static void
2679 mips_emit_delays (bfd_boolean insns)
2680 {
2681   if (! mips_opts.noreorder)
2682     {
2683       int nops;
2684
2685       nops = 0;
2686       if ((! mips_opts.mips16
2687            && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2688            && (! cop_interlocks
2689                && (prev_insn.insn_mo->pinfo
2690                    & (INSN_LOAD_COPROC_DELAY
2691                       | INSN_COPROC_MOVE_DELAY
2692                       | INSN_WRITE_COND_CODE))))
2693           || (! hilo_interlocks
2694               && (prev_insn.insn_mo->pinfo
2695                   & (INSN_READ_LO
2696                      | INSN_READ_HI)))
2697           || (! mips_opts.mips16
2698               && ! gpr_interlocks
2699               && (prev_insn.insn_mo->pinfo
2700                   & INSN_LOAD_MEMORY_DELAY))
2701           || (! mips_opts.mips16
2702               && mips_opts.isa == ISA_MIPS1
2703               && (prev_insn.insn_mo->pinfo
2704                   & INSN_COPROC_MEMORY_DELAY)))
2705         {
2706           /* Itbl support may require additional care here.  */
2707           ++nops;
2708           if ((! mips_opts.mips16
2709                && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2710                && (! cop_interlocks
2711                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2712               || (! hilo_interlocks
2713                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2714                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2715             ++nops;
2716
2717           if (prev_insn_unreordered)
2718             nops = 0;
2719         }
2720       else if ((! mips_opts.mips16
2721                 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2722                 && (! cop_interlocks
2723                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2724                || (! hilo_interlocks
2725                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2726                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2727         {
2728           /* Itbl support may require additional care here.  */
2729           if (! prev_prev_insn_unreordered)
2730             ++nops;
2731         }
2732
2733       if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2734         {
2735           int min_nops = 0;
2736           const char *pn = prev_insn.insn_mo->name;
2737           if (strncmp(pn, "macc", 4) == 0
2738               || strncmp(pn, "dmacc", 5) == 0
2739               || strncmp(pn, "dmult", 5) == 0)
2740             {
2741               min_nops = 1;
2742             }
2743           if (nops < min_nops)
2744             nops = min_nops;
2745         }
2746
2747       if (nops > 0)
2748         {
2749           struct insn_label_list *l;
2750
2751           if (insns)
2752             {
2753               /* Record the frag which holds the nop instructions, so
2754                  that we can remove them if we don't need them.  */
2755               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2756               prev_nop_frag = frag_now;
2757               prev_nop_frag_holds = nops;
2758               prev_nop_frag_required = 0;
2759               prev_nop_frag_since = 0;
2760             }
2761
2762           for (; nops > 0; --nops)
2763             emit_nop ();
2764
2765           if (insns)
2766             {
2767               /* Move on to a new frag, so that it is safe to simply
2768                  decrease the size of prev_nop_frag.  */
2769               frag_wane (frag_now);
2770               frag_new (0);
2771             }
2772
2773           for (l = insn_labels; l != NULL; l = l->next)
2774             {
2775               valueT val;
2776
2777               assert (S_GET_SEGMENT (l->label) == now_seg);
2778               symbol_set_frag (l->label, frag_now);
2779               val = (valueT) frag_now_fix ();
2780               /* mips16 text labels are stored as odd.  */
2781               if (mips_opts.mips16)
2782                 ++val;
2783               S_SET_VALUE (l->label, val);
2784             }
2785         }
2786     }
2787
2788   /* Mark instruction labels in mips16 mode.  */
2789   if (insns)
2790     mips16_mark_labels ();
2791
2792   mips_no_prev_insn (insns);
2793 }
2794
2795 /* Build an instruction created by a macro expansion.  This is passed
2796    a pointer to the count of instructions created so far, an
2797    expression, the name of the instruction to build, an operand format
2798    string, and corresponding arguments.  */
2799
2800 static void
2801 macro_build (char *place, int *counter, expressionS *ep, const char *name,
2802              const char *fmt, ...)
2803 {
2804   struct mips_cl_insn insn;
2805   bfd_reloc_code_real_type r[3];
2806   va_list args;
2807
2808   va_start (args, fmt);
2809
2810   /*
2811    * If the macro is about to expand into a second instruction,
2812    * print a warning if needed. We need to pass ip as a parameter
2813    * to generate a better warning message here...
2814    */
2815   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2816     as_warn (_("Macro instruction expanded into multiple instructions"));
2817
2818   /*
2819    * If the macro is about to expand into a second instruction,
2820    * and it is in a delay slot, print a warning.
2821    */
2822   if (place == NULL
2823       && *counter == 1
2824       && mips_opts.noreorder
2825       && (prev_prev_insn.insn_mo->pinfo
2826           & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2827              | INSN_COND_BRANCH_LIKELY)) != 0)
2828     as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2829
2830   if (place == NULL)
2831     ++*counter;         /* bump instruction counter */
2832
2833   if (mips_opts.mips16)
2834     {
2835       mips16_macro_build (place, counter, ep, name, fmt, args);
2836       va_end (args);
2837       return;
2838     }
2839
2840   r[0] = BFD_RELOC_UNUSED;
2841   r[1] = BFD_RELOC_UNUSED;
2842   r[2] = BFD_RELOC_UNUSED;
2843   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2844   assert (insn.insn_mo);
2845   assert (strcmp (name, insn.insn_mo->name) == 0);
2846
2847   /* Search until we get a match for NAME.  */
2848   while (1)
2849     {
2850       /* It is assumed here that macros will never generate
2851          MDMX or MIPS-3D instructions.  */
2852       if (strcmp (fmt, insn.insn_mo->args) == 0
2853           && insn.insn_mo->pinfo != INSN_MACRO
2854           && OPCODE_IS_MEMBER (insn.insn_mo,
2855                                (mips_opts.isa
2856                                 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2857                                mips_opts.arch)
2858           && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2859         break;
2860
2861       ++insn.insn_mo;
2862       assert (insn.insn_mo->name);
2863       assert (strcmp (name, insn.insn_mo->name) == 0);
2864     }
2865
2866   insn.insn_opcode = insn.insn_mo->match;
2867   for (;;)
2868     {
2869       switch (*fmt++)
2870         {
2871         case '\0':
2872           break;
2873
2874         case ',':
2875         case '(':
2876         case ')':
2877           continue;
2878
2879         case '+':
2880           switch (*fmt++)
2881             {
2882             case 'A':
2883             case 'E':
2884               insn.insn_opcode |= (va_arg (args, int)
2885                                    & OP_MASK_SHAMT) << OP_SH_SHAMT;
2886               continue;
2887
2888             case 'B':
2889             case 'F':
2890               /* Note that in the macro case, these arguments are already
2891                  in MSB form.  (When handling the instruction in the
2892                  non-macro case, these arguments are sizes from which
2893                  MSB values must be calculated.)  */
2894               insn.insn_opcode |= (va_arg (args, int)
2895                                    & OP_MASK_INSMSB) << OP_SH_INSMSB;
2896               continue;
2897
2898             case 'C':
2899             case 'G':
2900             case 'H':
2901               /* Note that in the macro case, these arguments are already
2902                  in MSBD form.  (When handling the instruction in the
2903                  non-macro case, these arguments are sizes from which
2904                  MSBD values must be calculated.)  */
2905               insn.insn_opcode |= (va_arg (args, int)
2906                                    & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
2907               continue;
2908
2909             default:
2910               internalError ();
2911             }
2912           continue;
2913
2914         case 't':
2915         case 'w':
2916         case 'E':
2917           insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2918           continue;
2919
2920         case 'c':
2921           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2922           continue;
2923
2924         case 'T':
2925         case 'W':
2926           insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2927           continue;
2928
2929         case 'd':
2930         case 'G':
2931         case 'K':
2932           insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2933           continue;
2934
2935         case 'U':
2936           {
2937             int tmp = va_arg (args, int);
2938
2939             insn.insn_opcode |= tmp << OP_SH_RT;
2940             insn.insn_opcode |= tmp << OP_SH_RD;
2941             continue;
2942           }
2943
2944         case 'V':
2945         case 'S':
2946           insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2947           continue;
2948
2949         case 'z':
2950           continue;
2951
2952         case '<':
2953           insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2954           continue;
2955
2956         case 'D':
2957           insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2958           continue;
2959
2960         case 'B':
2961           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2962           continue;
2963
2964         case 'J':
2965           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2966           continue;
2967
2968         case 'q':
2969           insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2970           continue;
2971
2972         case 'b':
2973         case 's':
2974         case 'r':
2975         case 'v':
2976           insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2977           continue;
2978
2979         case 'i':
2980         case 'j':
2981         case 'o':
2982           *r = (bfd_reloc_code_real_type) va_arg (args, int);
2983           assert (*r == BFD_RELOC_GPREL16
2984                   || *r == BFD_RELOC_MIPS_LITERAL
2985                   || *r == BFD_RELOC_MIPS_HIGHER
2986                   || *r == BFD_RELOC_HI16_S
2987                   || *r == BFD_RELOC_LO16
2988                   || *r == BFD_RELOC_MIPS_GOT16
2989                   || *r == BFD_RELOC_MIPS_CALL16
2990                   || *r == BFD_RELOC_MIPS_GOT_DISP
2991                   || *r == BFD_RELOC_MIPS_GOT_PAGE
2992                   || *r == BFD_RELOC_MIPS_GOT_OFST
2993                   || *r == BFD_RELOC_MIPS_GOT_LO16
2994                   || *r == BFD_RELOC_MIPS_CALL_LO16
2995                   || (ep->X_op == O_subtract
2996                       && *r == BFD_RELOC_PCREL_LO16));
2997           continue;
2998
2999         case 'u':
3000           *r = (bfd_reloc_code_real_type) va_arg (args, int);
3001           assert (ep != NULL
3002                   && (ep->X_op == O_constant
3003                       || (ep->X_op == O_symbol
3004                           && (*r == BFD_RELOC_MIPS_HIGHEST
3005                               || *r == BFD_RELOC_HI16_S
3006                               || *r == BFD_RELOC_HI16
3007                               || *r == BFD_RELOC_GPREL16
3008                               || *r == BFD_RELOC_MIPS_GOT_HI16
3009                               || *r == BFD_RELOC_MIPS_CALL_HI16))
3010                       || (ep->X_op == O_subtract
3011                           && *r == BFD_RELOC_PCREL_HI16_S)));
3012           continue;
3013
3014         case 'p':
3015           assert (ep != NULL);
3016           /*
3017            * This allows macro() to pass an immediate expression for
3018            * creating short branches without creating a symbol.
3019            * Note that the expression still might come from the assembly
3020            * input, in which case the value is not checked for range nor
3021            * is a relocation entry generated (yuck).
3022            */
3023           if (ep->X_op == O_constant)
3024             {
3025               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3026               ep = NULL;
3027             }
3028           else
3029             *r = BFD_RELOC_16_PCREL_S2;
3030           continue;
3031
3032         case 'a':
3033           assert (ep != NULL);
3034           *r = BFD_RELOC_MIPS_JMP;
3035           continue;
3036
3037         case 'C':
3038           insn.insn_opcode |= va_arg (args, unsigned long);
3039           continue;
3040
3041         default:
3042           internalError ();
3043         }
3044       break;
3045     }
3046   va_end (args);
3047   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3048
3049   append_insn (place, &insn, ep, r);
3050 }
3051
3052 static void
3053 mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3054                     expressionS *ep, const char *name, const char *fmt,
3055                     va_list args)
3056 {
3057   struct mips_cl_insn insn;
3058   bfd_reloc_code_real_type r[3]
3059     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3060
3061   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3062   assert (insn.insn_mo);
3063   assert (strcmp (name, insn.insn_mo->name) == 0);
3064
3065   while (strcmp (fmt, insn.insn_mo->args) != 0
3066          || insn.insn_mo->pinfo == INSN_MACRO)
3067     {
3068       ++insn.insn_mo;
3069       assert (insn.insn_mo->name);
3070       assert (strcmp (name, insn.insn_mo->name) == 0);
3071     }
3072
3073   insn.insn_opcode = insn.insn_mo->match;
3074   insn.use_extend = FALSE;
3075
3076   for (;;)
3077     {
3078       int c;
3079
3080       c = *fmt++;
3081       switch (c)
3082         {
3083         case '\0':
3084           break;
3085
3086         case ',':
3087         case '(':
3088         case ')':
3089           continue;
3090
3091         case 'y':
3092         case 'w':
3093           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3094           continue;
3095
3096         case 'x':
3097         case 'v':
3098           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3099           continue;
3100
3101         case 'z':
3102           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3103           continue;
3104
3105         case 'Z':
3106           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3107           continue;
3108
3109         case '0':
3110         case 'S':
3111         case 'P':
3112         case 'R':
3113           continue;
3114
3115         case 'X':
3116           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3117           continue;
3118
3119         case 'Y':
3120           {
3121             int regno;
3122
3123             regno = va_arg (args, int);
3124             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3125             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3126           }
3127           continue;
3128
3129         case '<':
3130         case '>':
3131         case '4':
3132         case '5':
3133         case 'H':
3134         case 'W':
3135         case 'D':
3136         case 'j':
3137         case '8':
3138         case 'V':
3139         case 'C':
3140         case 'U':
3141         case 'k':
3142         case 'K':
3143         case 'p':
3144         case 'q':
3145           {
3146             assert (ep != NULL);
3147
3148             if (ep->X_op != O_constant)
3149               *r = (int) BFD_RELOC_UNUSED + c;
3150             else
3151               {
3152                 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3153                               FALSE, &insn.insn_opcode, &insn.use_extend,
3154                               &insn.extend);
3155                 ep = NULL;
3156                 *r = BFD_RELOC_UNUSED;
3157               }
3158           }
3159           continue;
3160
3161         case '6':
3162           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3163           continue;
3164         }
3165
3166       break;
3167     }
3168
3169   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3170
3171   append_insn (place, &insn, ep, r);
3172 }
3173
3174 /*
3175  * Generate a "jalr" instruction with a relocation hint to the called
3176  * function.  This occurs in NewABI PIC code.
3177  */
3178 static void
3179 macro_build_jalr (int icnt, expressionS *ep)
3180 {
3181   char *f = NULL;
3182
3183   if (HAVE_NEWABI)
3184     {
3185       frag_grow (4);
3186       f = frag_more (0);
3187     }
3188   macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3189   if (HAVE_NEWABI)
3190     fix_new_exp (frag_now, f - frag_now->fr_literal,
3191                  4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3192 }
3193
3194 /*
3195  * Generate a "lui" instruction.
3196  */
3197 static void
3198 macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
3199 {
3200   expressionS high_expr;
3201   struct mips_cl_insn insn;
3202   bfd_reloc_code_real_type r[3]
3203     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3204   const char *name = "lui";
3205   const char *fmt = "t,u";
3206
3207   assert (! mips_opts.mips16);
3208
3209   if (place == NULL)
3210     high_expr = *ep;
3211   else
3212     {
3213       high_expr.X_op = O_constant;
3214       high_expr.X_add_number = ep->X_add_number;
3215     }
3216
3217   if (high_expr.X_op == O_constant)
3218     {
3219       /* we can compute the instruction now without a relocation entry */
3220       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3221                                 >> 16) & 0xffff;
3222       *r = BFD_RELOC_UNUSED;
3223     }
3224   else
3225     {
3226       assert (ep->X_op == O_symbol);
3227       /* _gp_disp is a special case, used from s_cpload.  */
3228       assert (mips_pic == NO_PIC
3229               || (! HAVE_NEWABI
3230                   && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3231       *r = BFD_RELOC_HI16_S;
3232     }
3233
3234   /*
3235    * If the macro is about to expand into a second instruction,
3236    * print a warning if needed. We need to pass ip as a parameter
3237    * to generate a better warning message here...
3238    */
3239   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3240     as_warn (_("Macro instruction expanded into multiple instructions"));
3241
3242   if (place == NULL)
3243     ++*counter;         /* bump instruction counter */
3244
3245   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3246   assert (insn.insn_mo);
3247   assert (strcmp (name, insn.insn_mo->name) == 0);
3248   assert (strcmp (fmt, insn.insn_mo->args) == 0);
3249
3250   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3251   if (*r == BFD_RELOC_UNUSED)
3252     {
3253       insn.insn_opcode |= high_expr.X_add_number;
3254       append_insn (place, &insn, NULL, r);
3255     }
3256   else
3257     append_insn (place, &insn, &high_expr, r);
3258 }
3259
3260 /* Generate a sequence of instructions to do a load or store from a constant
3261    offset off of a base register (breg) into/from a target register (treg),
3262    using AT if necessary.  */
3263 static void
3264 macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
3265                               const char *op, int treg, int breg, int dbl)
3266 {
3267   assert (ep->X_op == O_constant);
3268
3269   /* Sign-extending 32-bit constants makes their handling easier.  */
3270   if (! dbl)
3271     {
3272       if (ep->X_add_number & ~((bfd_vma) 0xffffffff)
3273           && ~(ep->X_add_number | 0xffffffff))
3274         as_bad (_("too large constant specified"));
3275
3276     ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3277                         - 0x80000000);
3278     }
3279
3280   /* Right now, this routine can only handle signed 32-bit contants.  */
3281   if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3282     as_warn (_("operand overflow"));
3283
3284   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3285     {
3286       /* Signed 16-bit offset will fit in the op.  Easy!  */
3287       macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3288                    breg);
3289     }
3290   else
3291     {
3292       /* 32-bit offset, need multiple instructions and AT, like:
3293            lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3294            addu     $tempreg,$tempreg,$breg
3295            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3296          to handle the complete offset.  */
3297       macro_build_lui (place, counter, ep, AT);
3298       if (place != NULL)
3299         place += 4;
3300       macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3301                    breg);
3302       if (place != NULL)
3303         place += 4;
3304       macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3305                    AT);
3306
3307       if (mips_opts.noat)
3308         as_warn (_("Macro used $at after \".set noat\""));
3309     }
3310 }
3311
3312 /*                      set_at()
3313  * Generates code to set the $at register to true (one)
3314  * if reg is less than the immediate expression.
3315  */
3316 static void
3317 set_at (int *counter, int reg, int unsignedp)
3318 {
3319   if (imm_expr.X_op == O_constant
3320       && imm_expr.X_add_number >= -0x8000
3321       && imm_expr.X_add_number < 0x8000)
3322     macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3323                  "t,r,j", AT, reg, BFD_RELOC_LO16);
3324   else
3325     {
3326       load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3327       macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
3328                    "d,v,t", AT, reg, AT);
3329     }
3330 }
3331
3332 /* Warn if an expression is not a constant.  */
3333
3334 static void
3335 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3336 {
3337   if (ex->X_op == O_big)
3338     as_bad (_("unsupported large constant"));
3339   else if (ex->X_op != O_constant)
3340     as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3341 }
3342
3343 /* Count the leading zeroes by performing a binary chop. This is a
3344    bulky bit of source, but performance is a LOT better for the
3345    majority of values than a simple loop to count the bits:
3346        for (lcnt = 0; (lcnt < 32); lcnt++)
3347          if ((v) & (1 << (31 - lcnt)))
3348            break;
3349   However it is not code size friendly, and the gain will drop a bit
3350   on certain cached systems.
3351 */
3352 #define COUNT_TOP_ZEROES(v)             \
3353   (((v) & ~0xffff) == 0                 \
3354    ? ((v) & ~0xff) == 0                 \
3355      ? ((v) & ~0xf) == 0                \
3356        ? ((v) & ~0x3) == 0              \
3357          ? ((v) & ~0x1) == 0            \
3358            ? !(v)                       \
3359              ? 32                       \
3360              : 31                       \
3361            : 30                         \
3362          : ((v) & ~0x7) == 0            \
3363            ? 29                         \
3364            : 28                         \
3365        : ((v) & ~0x3f) == 0             \
3366          ? ((v) & ~0x1f) == 0           \
3367            ? 27                         \
3368            : 26                         \
3369          : ((v) & ~0x7f) == 0           \
3370            ? 25                         \
3371            : 24                         \
3372      : ((v) & ~0xfff) == 0              \
3373        ? ((v) & ~0x3ff) == 0            \
3374          ? ((v) & ~0x1ff) == 0          \
3375            ? 23                         \
3376            : 22                         \
3377          : ((v) & ~0x7ff) == 0          \
3378            ? 21                         \
3379            : 20                         \
3380        : ((v) & ~0x3fff) == 0           \
3381          ? ((v) & ~0x1fff) == 0         \
3382            ? 19                         \
3383            : 18                         \
3384          : ((v) & ~0x7fff) == 0         \
3385            ? 17                         \
3386            : 16                         \
3387    : ((v) & ~0xffffff) == 0             \
3388      ? ((v) & ~0xfffff) == 0            \
3389        ? ((v) & ~0x3ffff) == 0          \
3390          ? ((v) & ~0x1ffff) == 0        \
3391            ? 15                         \
3392            : 14                         \
3393          : ((v) & ~0x7ffff) == 0        \
3394            ? 13                         \
3395            : 12                         \
3396        : ((v) & ~0x3fffff) == 0         \
3397          ? ((v) & ~0x1fffff) == 0       \
3398            ? 11                         \
3399            : 10                         \
3400          : ((v) & ~0x7fffff) == 0       \
3401            ? 9                          \
3402            : 8                          \
3403      : ((v) & ~0xfffffff) == 0          \
3404        ? ((v) & ~0x3ffffff) == 0        \
3405          ? ((v) & ~0x1ffffff) == 0      \
3406            ? 7                          \
3407            : 6                          \
3408          : ((v) & ~0x7ffffff) == 0      \
3409            ? 5                          \
3410            : 4                          \
3411        : ((v) & ~0x3fffffff) == 0       \
3412          ? ((v) & ~0x1fffffff) == 0     \
3413            ? 3                          \
3414            : 2                          \
3415          : ((v) & ~0x7fffffff) == 0     \
3416            ? 1                          \
3417            : 0)
3418
3419 /*                      load_register()
3420  *  This routine generates the least number of instructions neccessary to load
3421  *  an absolute expression value into a register.
3422  */
3423 static void
3424 load_register (int *counter, int reg, expressionS *ep, int dbl)
3425 {
3426   int freg;
3427   expressionS hi32, lo32;
3428
3429   if (ep->X_op != O_big)
3430     {
3431       assert (ep->X_op == O_constant);
3432
3433       /* Sign-extending 32-bit constants makes their handling easier.  */
3434       if (! dbl)
3435         {
3436           if (ep->X_add_number & ~((bfd_vma) 0xffffffff)
3437               && ~(ep->X_add_number | 0xffffffff))
3438             as_bad (_("too large constant specified"));
3439
3440         ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3441                             - 0x80000000);
3442         }
3443
3444       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3445         {
3446           /* We can handle 16 bit signed values with an addiu to
3447              $zero.  No need to ever use daddiu here, since $zero and
3448              the result are always correct in 32 bit mode.  */
3449           macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3450                        BFD_RELOC_LO16);
3451           return;
3452         }
3453       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3454         {
3455           /* We can handle 16 bit unsigned values with an ori to
3456              $zero.  */
3457           macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3458                        BFD_RELOC_LO16);
3459           return;
3460         }
3461       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3462         {
3463           /* 32 bit values require an lui.  */
3464           macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3465           if ((ep->X_add_number & 0xffff) != 0)
3466             macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3467                          BFD_RELOC_LO16);
3468           return;
3469         }
3470     }
3471
3472   /* The value is larger than 32 bits.  */
3473
3474   if (HAVE_32BIT_GPRS)
3475     {
3476       as_bad (_("Number (0x%lx) larger than 32 bits"),
3477               (unsigned long) ep->X_add_number);
3478       macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3479                    BFD_RELOC_LO16);
3480       return;
3481     }
3482
3483   if (ep->X_op != O_big)
3484     {
3485       hi32 = *ep;
3486       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3487       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3488       hi32.X_add_number &= 0xffffffff;
3489       lo32 = *ep;
3490       lo32.X_add_number &= 0xffffffff;
3491     }
3492   else
3493     {
3494       assert (ep->X_add_number > 2);
3495       if (ep->X_add_number == 3)
3496         generic_bignum[3] = 0;
3497       else if (ep->X_add_number > 4)
3498         as_bad (_("Number larger than 64 bits"));
3499       lo32.X_op = O_constant;
3500       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3501       hi32.X_op = O_constant;
3502       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3503     }
3504
3505   if (hi32.X_add_number == 0)
3506     freg = 0;
3507   else
3508     {
3509       int shift, bit;
3510       unsigned long hi, lo;
3511
3512       if (hi32.X_add_number == (offsetT) 0xffffffff)
3513         {
3514           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3515             {
3516               macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3517                            BFD_RELOC_LO16);
3518               return;
3519             }
3520           if (lo32.X_add_number & 0x80000000)
3521             {
3522               macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3523                            BFD_RELOC_HI16);
3524               if (lo32.X_add_number & 0xffff)
3525                 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3526                              BFD_RELOC_LO16);
3527               return;
3528             }
3529         }
3530
3531       /* Check for 16bit shifted constant.  We know that hi32 is
3532          non-zero, so start the mask on the first bit of the hi32
3533          value.  */
3534       shift = 17;
3535       do
3536         {
3537           unsigned long himask, lomask;
3538
3539           if (shift < 32)
3540             {
3541               himask = 0xffff >> (32 - shift);
3542               lomask = (0xffff << shift) & 0xffffffff;
3543             }
3544           else
3545             {
3546               himask = 0xffff << (shift - 32);
3547               lomask = 0;
3548             }
3549           if ((hi32.X_add_number & ~(offsetT) himask) == 0
3550               && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3551             {
3552               expressionS tmp;
3553
3554               tmp.X_op = O_constant;
3555               if (shift < 32)
3556                 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3557                                     | (lo32.X_add_number >> shift));
3558               else
3559                 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3560               macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3561                            BFD_RELOC_LO16);
3562               macro_build (NULL, counter, NULL,
3563                            (shift >= 32) ? "dsll32" : "dsll",
3564                            "d,w,<", reg, reg,
3565                            (shift >= 32) ? shift - 32 : shift);
3566               return;
3567             }
3568           ++shift;
3569         }
3570       while (shift <= (64 - 16));
3571
3572       /* Find the bit number of the lowest one bit, and store the
3573          shifted value in hi/lo.  */
3574       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3575       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3576       if (lo != 0)
3577         {
3578           bit = 0;
3579           while ((lo & 1) == 0)
3580             {
3581               lo >>= 1;
3582               ++bit;
3583             }
3584           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3585           hi >>= bit;
3586         }
3587       else
3588         {
3589           bit = 32;
3590           while ((hi & 1) == 0)
3591             {
3592               hi >>= 1;
3593               ++bit;
3594             }
3595           lo = hi;
3596           hi = 0;
3597         }
3598
3599       /* Optimize if the shifted value is a (power of 2) - 1.  */
3600       if ((hi == 0 && ((lo + 1) & lo) == 0)
3601           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3602         {
3603           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3604           if (shift != 0)
3605             {
3606               expressionS tmp;
3607
3608               /* This instruction will set the register to be all
3609                  ones.  */
3610               tmp.X_op = O_constant;
3611               tmp.X_add_number = (offsetT) -1;
3612               macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3613                            BFD_RELOC_LO16);
3614               if (bit != 0)
3615                 {
3616                   bit += shift;
3617                   macro_build (NULL, counter, NULL,
3618                                (bit >= 32) ? "dsll32" : "dsll",
3619                                "d,w,<", reg, reg,
3620                                (bit >= 32) ? bit - 32 : bit);
3621                 }
3622               macro_build (NULL, counter, NULL,
3623                            (shift >= 32) ? "dsrl32" : "dsrl",
3624                            "d,w,<", reg, reg,
3625                            (shift >= 32) ? shift - 32 : shift);
3626               return;
3627             }
3628         }
3629
3630       /* Sign extend hi32 before calling load_register, because we can
3631          generally get better code when we load a sign extended value.  */
3632       if ((hi32.X_add_number & 0x80000000) != 0)
3633         hi32.X_add_number |= ~(offsetT) 0xffffffff;
3634       load_register (counter, reg, &hi32, 0);
3635       freg = reg;
3636     }
3637   if ((lo32.X_add_number & 0xffff0000) == 0)
3638     {
3639       if (freg != 0)
3640         {
3641           macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
3642           freg = reg;
3643         }
3644     }
3645   else
3646     {
3647       expressionS mid16;
3648
3649       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3650         {
3651           macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3652                        BFD_RELOC_HI16);
3653           macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
3654           return;
3655         }
3656
3657       if (freg != 0)
3658         {
3659           macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
3660           freg = reg;
3661         }
3662       mid16 = lo32;
3663       mid16.X_add_number >>= 16;
3664       macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3665                    BFD_RELOC_LO16);
3666       macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3667       freg = reg;
3668     }
3669   if ((lo32.X_add_number & 0xffff) != 0)
3670     macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3671                  BFD_RELOC_LO16);
3672 }
3673
3674 /* Load an address into a register.  */
3675
3676 static void
3677 load_address (int *counter, int reg, expressionS *ep, int *used_at)
3678 {
3679   char *p = NULL;
3680
3681   if (ep->X_op != O_constant
3682       && ep->X_op != O_symbol)
3683     {
3684       as_bad (_("expression too complex"));
3685       ep->X_op = O_constant;
3686     }
3687
3688   if (ep->X_op == O_constant)
3689     {
3690       load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3691       return;
3692     }
3693
3694   if (mips_pic == NO_PIC)
3695     {
3696       /* If this is a reference to a GP relative symbol, we want
3697            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3698          Otherwise we want
3699            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3700            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3701          If we have an addend, we always use the latter form.
3702
3703          With 64bit address space and a usable $at we want
3704            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3705            lui          $at,<sym>               (BFD_RELOC_HI16_S)
3706            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3707            daddiu       $at,<sym>               (BFD_RELOC_LO16)
3708            dsll32       $reg,0
3709            daddu        $reg,$reg,$at
3710
3711          If $at is already in use, we use a path which is suboptimal
3712          on superscalar processors.
3713            lui          $reg,<sym>              (BFD_RELOC_MIPS_HIGHEST)
3714            daddiu       $reg,<sym>              (BFD_RELOC_MIPS_HIGHER)
3715            dsll         $reg,16
3716            daddiu       $reg,<sym>              (BFD_RELOC_HI16_S)
3717            dsll         $reg,16
3718            daddiu       $reg,<sym>              (BFD_RELOC_LO16)
3719        */
3720       if (HAVE_64BIT_ADDRESSES)
3721         {
3722           /* We don't do GP optimization for now because RELAX_ENCODE can't
3723              hold the data for such large chunks.  */
3724
3725           if (*used_at == 0 && ! mips_opts.noat)
3726             {
3727               macro_build (p, counter, ep, "lui", "t,u",
3728                            reg, BFD_RELOC_MIPS_HIGHEST);
3729               macro_build (p, counter, ep, "lui", "t,u",
3730                            AT, BFD_RELOC_HI16_S);
3731               macro_build (p, counter, ep, "daddiu", "t,r,j",
3732                            reg, reg, BFD_RELOC_MIPS_HIGHER);
3733               macro_build (p, counter, ep, "daddiu", "t,r,j",
3734                            AT, AT, BFD_RELOC_LO16);
3735               macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3736               macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
3737               *used_at = 1;
3738             }
3739           else
3740             {
3741               macro_build (p, counter, ep, "lui", "t,u",
3742                            reg, BFD_RELOC_MIPS_HIGHEST);
3743               macro_build (p, counter, ep, "daddiu", "t,r,j",
3744                            reg, reg, BFD_RELOC_MIPS_HIGHER);
3745               macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3746               macro_build (p, counter, ep, "daddiu", "t,r,j",
3747                            reg, reg, BFD_RELOC_HI16_S);
3748               macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3749               macro_build (p, counter, ep, "daddiu", "t,r,j",
3750                            reg, reg, BFD_RELOC_LO16);
3751             }
3752         }
3753       else
3754         {
3755           if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3756               && ! nopic_need_relax (ep->X_add_symbol, 1))
3757             {
3758               frag_grow (20);
3759               macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3760                            mips_gp_register, BFD_RELOC_GPREL16);
3761               p = frag_var (rs_machine_dependent, 8, 0,
3762                             RELAX_ENCODE (4, 8, 0, 4, 0,
3763                                           mips_opts.warn_about_macros),
3764                             ep->X_add_symbol, 0, NULL);
3765             }
3766           macro_build_lui (p, counter, ep, reg);
3767           if (p != NULL)
3768             p += 4;
3769           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3770                        BFD_RELOC_LO16);
3771         }
3772     }
3773   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3774     {
3775       expressionS ex;
3776
3777       /* If this is a reference to an external symbol, we want
3778            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3779          Otherwise we want
3780            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3781            nop
3782            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3783          If there is a constant, it must be added in after.
3784
3785          If we have NewABI, we want
3786            lw           $reg,<sym+cst>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
3787          unless we're referencing a global symbol with a non-zero
3788          offset, in which case cst must be added separately.  */
3789       if (HAVE_NEWABI)
3790         {
3791           frag_grow (12);
3792
3793           if (ep->X_add_number)
3794             {
3795               frag_now->tc_frag_data.tc_fr_offset =
3796                 ex.X_add_number = ep->X_add_number;
3797               ep->X_add_number = 0;
3798               macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3799                            reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3800               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3801                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3802               ex.X_op = O_constant;
3803               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3804                            reg, reg, BFD_RELOC_LO16);
3805               p = frag_var (rs_machine_dependent, 8, 0,
3806                             RELAX_ENCODE (8, 4, 0, 0, 0,
3807                                           mips_opts.warn_about_macros),
3808                             ep->X_add_symbol, 0, NULL);
3809               ep->X_add_number = ex.X_add_number;
3810             }
3811
3812           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3813                        BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3814
3815           if (! p)
3816             {
3817               /* To avoid confusion in tc_gen_reloc, we must ensure
3818                  that this does not become a variant frag.  */
3819               frag_wane (frag_now);
3820               frag_new (0);
3821             }
3822         }
3823       else
3824         {
3825           ex.X_add_number = ep->X_add_number;
3826           ep->X_add_number = 0;
3827           frag_grow (20);
3828           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3829                        BFD_RELOC_MIPS_GOT16,
3830                        mips_gp_register);
3831           macro_build (NULL, counter, NULL, "nop", "");
3832           p = frag_var (rs_machine_dependent, 4, 0,
3833                         RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3834                         ep->X_add_symbol, 0, NULL);
3835           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3836                        BFD_RELOC_LO16);
3837
3838           if (ex.X_add_number != 0)
3839             {
3840               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3841                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3842               ex.X_op = O_constant;
3843               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3844                            reg, reg, BFD_RELOC_LO16);
3845             }
3846         }
3847     }
3848   else if (mips_pic == SVR4_PIC)
3849     {
3850       expressionS ex;
3851       int off;
3852
3853       /* This is the large GOT case.  If this is a reference to an
3854          external symbol, we want
3855            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3856            addu         $reg,$reg,$gp
3857            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3858
3859          Otherwise, for a reference to a local symbol in old ABI, we want
3860            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3861            nop
3862            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3863          If there is a constant, it must be added in after.
3864
3865          In the NewABI, for local symbols, with or without offsets, we want:
3866            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
3867            addiu        $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
3868       */
3869       if (HAVE_NEWABI)
3870         {
3871           frag_grow (24);
3872
3873           frag_now->tc_frag_data.tc_fr_offset =
3874             ex.X_add_number = ep->X_add_number;
3875           ep->X_add_number = 0;
3876           macro_build (NULL, counter, ep, "lui", "t,u", reg,
3877                        BFD_RELOC_MIPS_GOT_HI16);
3878           macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3879                        reg, mips_gp_register);
3880           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3881                        BFD_RELOC_MIPS_GOT_LO16, reg);
3882           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3883             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3884           else if (ex.X_add_number)
3885             {
3886               ex.X_op = O_constant;
3887               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3888                            reg, reg, BFD_RELOC_LO16);
3889             }
3890
3891           ep->X_add_number = ex.X_add_number;
3892           p = frag_var (rs_machine_dependent, 8, 0,
3893                         RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3894                                       mips_opts.warn_about_macros),
3895                         ep->X_add_symbol, 0, NULL);
3896           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3897                        BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3898           macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3899                        reg, BFD_RELOC_MIPS_GOT_OFST);
3900         }
3901       else
3902         {
3903           ex.X_add_number = ep->X_add_number;
3904           ep->X_add_number = 0;
3905           if (reg_needs_delay (mips_gp_register))
3906             off = 4;
3907           else
3908             off = 0;
3909           frag_grow (32);
3910           macro_build (NULL, counter, ep, "lui", "t,u", reg,
3911                        BFD_RELOC_MIPS_GOT_HI16);
3912           macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3913                        reg, mips_gp_register);
3914           macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3915                        BFD_RELOC_MIPS_GOT_LO16, reg);
3916           p = frag_var (rs_machine_dependent, 12 + off, 0,
3917                         RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3918                                       mips_opts.warn_about_macros),
3919                         ep->X_add_symbol, 0, NULL);
3920           if (off > 0)
3921             {
3922               /* We need a nop before loading from $gp.  This special
3923                  check is required because the lui which starts the main
3924                  instruction stream does not refer to $gp, and so will not
3925                  insert the nop which may be required.  */
3926               macro_build (p, counter, NULL, "nop", "");
3927                 p += 4;
3928             }
3929           macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3930                        BFD_RELOC_MIPS_GOT16, mips_gp_register);
3931           p += 4;
3932           macro_build (p, counter, NULL, "nop", "");
3933           p += 4;
3934           macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3935                        BFD_RELOC_LO16);
3936
3937           if (ex.X_add_number != 0)
3938             {
3939               if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940                 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941               ex.X_op = O_constant;
3942               macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3943                            reg, reg, BFD_RELOC_LO16);
3944             }
3945         }
3946     }
3947   else if (mips_pic == EMBEDDED_PIC)
3948     {
3949       /* We always do
3950            addiu        $reg,$gp,<sym>          (BFD_RELOC_GPREL16)
3951        */
3952       macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3953                    mips_gp_register, BFD_RELOC_GPREL16);
3954     }
3955   else
3956     abort ();
3957 }
3958
3959 /* Move the contents of register SOURCE into register DEST.  */
3960
3961 static void
3962 move_register (int *counter, int dest, int source)
3963 {
3964   macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
3965                "d,v,t", dest, source, 0);
3966 }
3967
3968 /*
3969  *                      Build macros
3970  *   This routine implements the seemingly endless macro or synthesized
3971  * instructions and addressing modes in the mips assembly language. Many
3972  * of these macros are simple and are similar to each other. These could
3973  * probably be handled by some kind of table or grammer aproach instead of
3974  * this verbose method. Others are not simple macros but are more like
3975  * optimizing code generation.
3976  *   One interesting optimization is when several store macros appear
3977  * consecutivly that would load AT with the upper half of the same address.
3978  * The ensuing load upper instructions are ommited. This implies some kind
3979  * of global optimization. We currently only optimize within a single macro.
3980  *   For many of the load and store macros if the address is specified as a
3981  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3982  * first load register 'at' with zero and use it as the base register. The
3983  * mips assembler simply uses register $zero. Just one tiny optimization
3984  * we're missing.
3985  */
3986 static void
3987 macro (struct mips_cl_insn *ip)
3988 {
3989   register int treg, sreg, dreg, breg;
3990   int tempreg;
3991   int mask;
3992   int icnt = 0;
3993   int used_at = 0;
3994   expressionS expr1;
3995   const char *s;
3996   const char *s2;
3997   const char *fmt;
3998   int likely = 0;
3999   int dbl = 0;
4000   int coproc = 0;
4001   int lr = 0;
4002   int imm = 0;
4003   offsetT maxnum;
4004   int off;
4005   bfd_reloc_code_real_type r;
4006   int hold_mips_optimize;
4007
4008   assert (! mips_opts.mips16);
4009
4010   treg = (ip->insn_opcode >> 16) & 0x1f;
4011   dreg = (ip->insn_opcode >> 11) & 0x1f;
4012   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4013   mask = ip->insn_mo->mask;
4014
4015   expr1.X_op = O_constant;
4016   expr1.X_op_symbol = NULL;
4017   expr1.X_add_symbol = NULL;
4018   expr1.X_add_number = 1;
4019
4020   /* Umatched fixups should not be put in the same frag as a relaxable
4021      macro.  For example, suppose we have:
4022
4023         lui $4,%hi(l1)          # 1
4024         la $5,l2                # 2
4025         addiu $4,$4,%lo(l1)     # 3
4026
4027      If instructions 1 and 2 were put in the same frag, md_frob_file would
4028      move the fixup for #1 after the fixups for the "unrelaxed" version of
4029      #2.  This would confuse tc_gen_reloc, which expects the relocations
4030      for #2 to be the last for that frag.
4031
4032      Also, if tc_gen_reloc sees certain relocations in a variant frag,
4033      it assumes that they belong to a relaxable macro.  We mustn't put
4034      other uses of such relocations into a variant frag.
4035
4036      To avoid both problems, finish the current frag it contains a
4037      %reloc() operator.  The macro then goes into a new frag.  */
4038   if (prev_reloc_op_frag == frag_now)
4039     {
4040       frag_wane (frag_now);
4041       frag_new (0);
4042     }
4043
4044   switch (mask)
4045     {
4046     case M_DABS:
4047       dbl = 1;
4048     case M_ABS:
4049       /* bgez $a0,.+12
4050          move v0,$a0
4051          sub v0,$zero,$a0
4052          */
4053
4054       mips_emit_delays (TRUE);
4055       ++mips_opts.noreorder;
4056       mips_any_noreorder = 1;
4057
4058       expr1.X_add_number = 8;
4059       macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4060       if (dreg == sreg)
4061         macro_build (NULL, &icnt, NULL, "nop", "", 0);
4062       else
4063         move_register (&icnt, dreg, sreg);
4064       macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4065                    sreg);
4066
4067       --mips_opts.noreorder;
4068       return;
4069
4070     case M_ADD_I:
4071       s = "addi";
4072       s2 = "add";
4073       goto do_addi;
4074     case M_ADDU_I:
4075       s = "addiu";
4076       s2 = "addu";
4077       goto do_addi;
4078     case M_DADD_I:
4079       dbl = 1;
4080       s = "daddi";
4081       s2 = "dadd";
4082       goto do_addi;
4083     case M_DADDU_I:
4084       dbl = 1;
4085       s = "daddiu";
4086       s2 = "daddu";
4087     do_addi:
4088       if (imm_expr.X_op == O_constant
4089           && imm_expr.X_add_number >= -0x8000
4090           && imm_expr.X_add_number < 0x8000)
4091         {
4092           macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4093                        BFD_RELOC_LO16);
4094           return;
4095         }
4096       load_register (&icnt, AT, &imm_expr, dbl);
4097       macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4098       break;
4099
4100     case M_AND_I:
4101       s = "andi";
4102       s2 = "and";
4103       goto do_bit;
4104     case M_OR_I:
4105       s = "ori";
4106       s2 = "or";
4107       goto do_bit;
4108     case M_NOR_I:
4109       s = "";
4110       s2 = "nor";
4111       goto do_bit;
4112     case M_XOR_I:
4113       s = "xori";
4114       s2 = "xor";
4115     do_bit:
4116       if (imm_expr.X_op == O_constant
4117           && imm_expr.X_add_number >= 0
4118           && imm_expr.X_add_number < 0x10000)
4119         {
4120           if (mask != M_NOR_I)
4121             macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4122                          BFD_RELOC_LO16);
4123           else
4124             {
4125               macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4126                            BFD_RELOC_LO16);
4127               macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
4128             }
4129           return;
4130         }
4131
4132       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4133       macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4134       break;
4135
4136     case M_BEQ_I:
4137       s = "beq";
4138       goto beq_i;
4139     case M_BEQL_I:
4140       s = "beql";
4141       likely = 1;
4142       goto beq_i;
4143     case M_BNE_I:
4144       s = "bne";
4145       goto beq_i;
4146     case M_BNEL_I:
4147       s = "bnel";
4148       likely = 1;
4149     beq_i:
4150       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4151         {
4152           macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
4153           return;
4154         }
4155       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4156       macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4157       break;
4158
4159     case M_BGEL:
4160       likely = 1;
4161     case M_BGE:
4162       if (treg == 0)
4163         {
4164           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4165                        "s,p", sreg);
4166           return;
4167         }
4168       if (sreg == 0)
4169         {
4170           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4171                        "s,p", treg);
4172           return;
4173         }
4174       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4175       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4176                    "s,t,p", AT, 0);
4177       break;
4178
4179     case M_BGTL_I:
4180       likely = 1;
4181     case M_BGT_I:
4182       /* check for > max integer */
4183       maxnum = 0x7fffffff;
4184       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4185         {
4186           maxnum <<= 16;
4187           maxnum |= 0xffff;
4188           maxnum <<= 16;
4189           maxnum |= 0xffff;
4190         }
4191       if (imm_expr.X_op == O_constant
4192           && imm_expr.X_add_number >= maxnum
4193           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4194         {
4195         do_false:
4196           /* result is always false */
4197           if (! likely)
4198             {
4199               if (warn_nops)
4200                 as_warn (_("Branch %s is always false (nop)"),
4201                          ip->insn_mo->name);
4202               macro_build (NULL, &icnt, NULL, "nop", "", 0);
4203             }
4204           else
4205             {
4206               if (warn_nops)
4207                 as_warn (_("Branch likely %s is always false"),
4208                          ip->insn_mo->name);
4209               macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
4210             }
4211           return;
4212         }
4213       if (imm_expr.X_op != O_constant)
4214         as_bad (_("Unsupported large constant"));
4215       ++imm_expr.X_add_number;
4216       /* FALLTHROUGH */
4217     case M_BGE_I:
4218     case M_BGEL_I:
4219       if (mask == M_BGEL_I)
4220         likely = 1;
4221       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4222         {
4223           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4224                        "s,p", sreg);
4225           return;
4226         }
4227       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4228         {
4229           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4230                        "s,p", sreg);
4231           return;
4232         }
4233       maxnum = 0x7fffffff;
4234       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4235         {
4236           maxnum <<= 16;
4237           maxnum |= 0xffff;
4238           maxnum <<= 16;
4239           maxnum |= 0xffff;
4240         }
4241       maxnum = - maxnum - 1;
4242       if (imm_expr.X_op == O_constant
4243           && imm_expr.X_add_number <= maxnum
4244           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4245         {
4246         do_true:
4247           /* result is always true */
4248           as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4249           macro_build (NULL, &icnt, &offset_expr, "b", "p");
4250           return;
4251         }
4252       set_at (&icnt, sreg, 0);
4253       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4254                    "s,t,p", AT, 0);
4255       break;
4256
4257     case M_BGEUL:
4258       likely = 1;
4259     case M_BGEU:
4260       if (treg == 0)
4261         goto do_true;
4262       if (sreg == 0)
4263         {
4264           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4265                        "s,t,p", 0, treg);
4266           return;
4267         }
4268       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4269       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4270                    "s,t,p", AT, 0);
4271       break;
4272
4273     case M_BGTUL_I:
4274       likely = 1;
4275     case M_BGTU_I:
4276       if (sreg == 0
4277           || (HAVE_32BIT_GPRS
4278               && imm_expr.X_op == O_constant
4279               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4280         goto do_false;
4281       if (imm_expr.X_op != O_constant)
4282         as_bad (_("Unsupported large constant"));
4283       ++imm_expr.X_add_number;
4284       /* FALLTHROUGH */
4285     case M_BGEU_I:
4286     case M_BGEUL_I:
4287       if (mask == M_BGEUL_I)
4288         likely = 1;
4289       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4290         goto do_true;
4291       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4292         {
4293           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4294                        "s,t,p", sreg, 0);
4295           return;
4296         }
4297       set_at (&icnt, sreg, 1);
4298       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4299                    "s,t,p", AT, 0);
4300       break;
4301
4302     case M_BGTL:
4303       likely = 1;
4304     case M_BGT:
4305       if (treg == 0)
4306         {
4307           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4308                        "s,p", sreg);
4309           return;
4310         }
4311       if (sreg == 0)
4312         {
4313           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4314                        "s,p", treg);
4315           return;
4316         }
4317       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4318       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4319                    "s,t,p", AT, 0);
4320       break;
4321
4322     case M_BGTUL:
4323       likely = 1;
4324     case M_BGTU:
4325       if (treg == 0)
4326         {
4327           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4328                        "s,t,p", sreg, 0);
4329           return;
4330         }
4331       if (sreg == 0)
4332         goto do_false;
4333       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4334       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4335                    "s,t,p", AT, 0);
4336       break;
4337
4338     case M_BLEL:
4339       likely = 1;
4340     case M_BLE:
4341       if (treg == 0)
4342         {
4343           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4344                        "s,p", sreg);
4345           return;
4346         }
4347       if (sreg == 0)
4348         {
4349           macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4350                        "s,p", treg);
4351           return;
4352         }
4353       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4354       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4355                    "s,t,p", AT, 0);
4356       break;
4357
4358     case M_BLEL_I:
4359       likely = 1;
4360     case M_BLE_I:
4361       maxnum = 0x7fffffff;
4362       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4363         {
4364           maxnum <<= 16;
4365           maxnum |= 0xffff;
4366           maxnum <<= 16;
4367           maxnum |= 0xffff;
4368         }
4369       if (imm_expr.X_op == O_constant
4370           && imm_expr.X_add_number >= maxnum
4371           && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4372         goto do_true;
4373       if (imm_expr.X_op != O_constant)
4374         as_bad (_("Unsupported large constant"));
4375       ++imm_expr.X_add_number;
4376       /* FALLTHROUGH */
4377     case M_BLT_I:
4378     case M_BLTL_I:
4379       if (mask == M_BLTL_I)
4380         likely = 1;
4381       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4382         {
4383           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4384                        "s,p", sreg);
4385           return;
4386         }
4387       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4388         {
4389           macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4390                        "s,p", sreg);
4391           return;
4392         }
4393       set_at (&icnt, sreg, 0);
4394       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4395                    "s,t,p", AT, 0);
4396       break;
4397
4398     case M_BLEUL:
4399       likely = 1;
4400     case M_BLEU:
4401       if (treg == 0)
4402         {
4403           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4404                        "s,t,p", sreg, 0);
4405           return;
4406         }
4407       if (sreg == 0)
4408         goto do_true;
4409       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4410       macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4411                    "s,t,p", AT, 0);
4412       break;
4413
4414     case M_BLEUL_I:
4415       likely = 1;
4416     case M_BLEU_I:
4417       if (sreg == 0
4418           || (HAVE_32BIT_GPRS
4419               && imm_expr.X_op == O_constant
4420               && imm_expr.X_add_number == (offsetT) 0xffffffff))
4421         goto do_true;
4422       if (imm_expr.X_op != O_constant)
4423         as_bad (_("Unsupported large constant"));
4424       ++imm_expr.X_add_number;
4425       /* FALLTHROUGH */
4426     case M_BLTU_I:
4427     case M_BLTUL_I:
4428       if (mask == M_BLTUL_I)
4429         likely = 1;
4430       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4431         goto do_false;
4432       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4433         {
4434           macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4435                        "s,t,p", sreg, 0);
4436           return;
4437         }
4438       set_at (&icnt, sreg, 1);
4439       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4440                    "s,t,p", AT, 0);
4441       break;
4442
4443     case M_BLTL:
4444       likely = 1;
4445     case M_BLT:
4446       if (treg == 0)
4447         {
4448           macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4449                        "s,p", sreg);
4450           return;
4451         }
4452       if (sreg == 0)
4453         {
4454           macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4455                        "s,p", treg);
4456           return;
4457         }
4458       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4459       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4460                    "s,t,p", AT, 0);
4461       break;
4462
4463     case M_BLTUL:
4464       likely = 1;
4465     case M_BLTU:
4466       if (treg == 0)
4467         goto do_false;
4468       if (sreg == 0)
4469         {
4470           macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4471                        "s,t,p", 0, treg);
4472           return;
4473         }
4474       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4475       macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4476                    "s,t,p", AT, 0);
4477       break;
4478
4479     case M_DEXT:
4480       {
4481         unsigned long pos;
4482         unsigned long size;
4483
4484         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4485           {
4486             as_bad (_("Unsupported large constant"));
4487             pos = size = 1;
4488           }
4489         else
4490           {
4491             pos = (unsigned long) imm_expr.X_add_number;
4492             size = (unsigned long) imm2_expr.X_add_number;
4493           }
4494
4495         if (pos > 63)
4496           {
4497             as_bad (_("Improper position (%lu)"), pos);
4498             pos = 1;
4499           }
4500         if (size == 0 || size > 64
4501             || (pos + size - 1) > 63)
4502           {
4503             as_bad (_("Improper extract size (%lu, position %lu)"),
4504                     size, pos);
4505             size = 1;
4506           }
4507
4508         if (size <= 32 && pos < 32)
4509           {
4510             s = "dext";
4511             fmt = "t,r,+A,+C";
4512           }
4513         else if (size <= 32)
4514           {
4515             s = "dextu";
4516             fmt = "t,r,+E,+H";
4517           }
4518         else
4519           {
4520             s = "dextm";
4521             fmt = "t,r,+A,+G";
4522           }
4523         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4524                      fmt, treg, sreg, pos, size - 1);
4525       }
4526       return;
4527
4528     case M_DINS:
4529       {
4530         unsigned long pos;
4531         unsigned long size;
4532
4533         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4534           {
4535             as_bad (_("Unsupported large constant"));
4536             pos = size = 1;
4537           }
4538         else
4539           {
4540             pos = (unsigned long) imm_expr.X_add_number;
4541             size = (unsigned long) imm2_expr.X_add_number;
4542           }
4543
4544         if (pos > 63)
4545           {
4546             as_bad (_("Improper position (%lu)"), pos);
4547             pos = 1;
4548           }
4549         if (size == 0 || size > 64
4550             || (pos + size - 1) > 63)
4551           {
4552             as_bad (_("Improper insert size (%lu, position %lu)"),
4553                     size, pos);
4554             size = 1;
4555           }
4556
4557         if (pos < 32 && (pos + size - 1) < 32)
4558           {
4559             s = "dins";
4560             fmt = "t,r,+A,+B";
4561           }
4562         else if (pos >= 32)
4563           {
4564             s = "dinsu";
4565             fmt = "t,r,+E,+F";
4566           }
4567         else
4568           {
4569             s = "dinsm";
4570             fmt = "t,r,+A,+F";
4571           }
4572         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4573                      fmt, treg, sreg, pos, pos + size - 1);
4574       }
4575       return;
4576
4577     case M_DDIV_3:
4578       dbl = 1;
4579     case M_DIV_3:
4580       s = "mflo";
4581       goto do_div3;
4582     case M_DREM_3:
4583       dbl = 1;
4584     case M_REM_3:
4585       s = "mfhi";
4586     do_div3:
4587       if (treg == 0)
4588         {
4589           as_warn (_("Divide by zero."));
4590           if (mips_trap)
4591             macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4592           else
4593             macro_build (NULL, &icnt, NULL, "break", "c", 7);
4594           return;
4595         }
4596
4597       mips_emit_delays (TRUE);
4598       ++mips_opts.noreorder;
4599       mips_any_noreorder = 1;
4600       if (mips_trap)
4601         {
4602           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4603           macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4604                        sreg, treg);
4605         }
4606       else
4607         {
4608           expr1.X_add_number = 8;
4609           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4610           macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4611                        sreg, treg);
4612           macro_build (NULL, &icnt,NULL, "break", "c", 7);
4613         }
4614       expr1.X_add_number = -1;
4615       macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4616                    AT, 0, BFD_RELOC_LO16);
4617       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4618       macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4619       if (dbl)
4620         {
4621           expr1.X_add_number = 1;
4622           macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4623                        BFD_RELOC_LO16);
4624           macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
4625         }
4626       else
4627         {
4628           expr1.X_add_number = 0x80000000;
4629           macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4630                        BFD_RELOC_HI16);
4631         }
4632       if (mips_trap)
4633         {
4634           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
4635           /* We want to close the noreorder block as soon as possible, so
4636              that later insns are available for delay slot filling.  */
4637           --mips_opts.noreorder;
4638         }
4639       else
4640         {
4641           expr1.X_add_number = 8;
4642           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4643           macro_build (NULL, &icnt, NULL, "nop", "", 0);
4644
4645           /* We want to close the noreorder block as soon as possible, so
4646              that later insns are available for delay slot filling.  */
4647           --mips_opts.noreorder;
4648
4649           macro_build (NULL, &icnt, NULL, "break", "c", 6);
4650         }
4651       macro_build (NULL, &icnt, NULL, s, "d", dreg);
4652       break;
4653
4654     case M_DIV_3I:
4655       s = "div";
4656       s2 = "mflo";
4657       goto do_divi;
4658     case M_DIVU_3I:
4659       s = "divu";
4660       s2 = "mflo";
4661       goto do_divi;
4662     case M_REM_3I:
4663       s = "div";
4664       s2 = "mfhi";
4665       goto do_divi;
4666     case M_REMU_3I:
4667       s = "divu";
4668       s2 = "mfhi";
4669       goto do_divi;
4670     case M_DDIV_3I:
4671       dbl = 1;
4672       s = "ddiv";
4673       s2 = "mflo";
4674       goto do_divi;
4675     case M_DDIVU_3I:
4676       dbl = 1;
4677       s = "ddivu";
4678       s2 = "mflo";
4679       goto do_divi;
4680     case M_DREM_3I:
4681       dbl = 1;
4682       s = "ddiv";
4683       s2 = "mfhi";
4684       goto do_divi;
4685     case M_DREMU_3I:
4686       dbl = 1;
4687       s = "ddivu";
4688       s2 = "mfhi";
4689     do_divi:
4690       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4691         {
4692           as_warn (_("Divide by zero."));
4693           if (mips_trap)
4694             macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4695           else
4696             macro_build (NULL, &icnt, NULL, "break", "c", 7);
4697           return;
4698         }
4699       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4700         {
4701           if (strcmp (s2, "mflo") == 0)
4702             move_register (&icnt, dreg, sreg);
4703           else
4704             move_register (&icnt, dreg, 0);
4705           return;
4706         }
4707       if (imm_expr.X_op == O_constant
4708           && imm_expr.X_add_number == -1
4709           && s[strlen (s) - 1] != 'u')
4710         {
4711           if (strcmp (s2, "mflo") == 0)
4712             {
4713               macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4714                            dreg, sreg);
4715             }
4716           else
4717             move_register (&icnt, dreg, 0);
4718           return;
4719         }
4720
4721       load_register (&icnt, AT, &imm_expr, dbl);
4722       macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4723       macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4724       break;
4725
4726     case M_DIVU_3:
4727       s = "divu";
4728       s2 = "mflo";
4729       goto do_divu3;
4730     case M_REMU_3:
4731       s = "divu";
4732       s2 = "mfhi";
4733       goto do_divu3;
4734     case M_DDIVU_3:
4735       s = "ddivu";
4736       s2 = "mflo";
4737       goto do_divu3;
4738     case M_DREMU_3:
4739       s = "ddivu";
4740       s2 = "mfhi";
4741     do_divu3:
4742       mips_emit_delays (TRUE);
4743       ++mips_opts.noreorder;
4744       mips_any_noreorder = 1;
4745       if (mips_trap)
4746         {
4747           macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4748           macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4749           /* We want to close the noreorder block as soon as possible, so
4750              that later insns are available for delay slot filling.  */
4751           --mips_opts.noreorder;
4752         }
4753       else
4754         {
4755           expr1.X_add_number = 8;
4756           macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4757           macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4758
4759           /* We want to close the noreorder block as soon as possible, so
4760              that later insns are available for delay slot filling.  */
4761           --mips_opts.noreorder;
4762           macro_build (NULL, &icnt, NULL, "break", "c", 7);
4763         }
4764       macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4765       return;
4766
4767     case M_DLA_AB:
4768       dbl = 1;
4769     case M_LA_AB:
4770       /* Load the address of a symbol into a register.  If breg is not
4771          zero, we then add a base register to it.  */
4772
4773       if (dbl && HAVE_32BIT_GPRS)
4774         as_warn (_("dla used to load 32-bit register"));
4775
4776       if (! dbl && HAVE_64BIT_OBJECTS)
4777         as_warn (_("la used to load 64-bit address"));
4778
4779       if (offset_expr.X_op == O_constant
4780           && offset_expr.X_add_number >= -0x8000
4781           && offset_expr.X_add_number < 0x8000)
4782         {
4783           macro_build (NULL, &icnt, &offset_expr,
4784                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4785                        "t,r,j", treg, sreg, BFD_RELOC_LO16);
4786           return;
4787         }
4788
4789       if (treg == breg)
4790         {
4791           tempreg = AT;
4792           used_at = 1;
4793         }
4794       else
4795         {
4796           tempreg = treg;
4797           used_at = 0;
4798         }
4799
4800       /* When generating embedded PIC code, we permit expressions of
4801          the form
4802            la   $treg,foo-bar
4803            la   $treg,foo-bar($breg)
4804          where bar is an address in the current section.  These are used
4805          when getting the addresses of functions.  We don't permit
4806          X_add_number to be non-zero, because if the symbol is
4807          external the relaxing code needs to know that any addend is
4808          purely the offset to X_op_symbol.  */
4809       if (mips_pic == EMBEDDED_PIC
4810           && offset_expr.X_op == O_subtract
4811           && (symbol_constant_p (offset_expr.X_op_symbol)
4812               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4813               : (symbol_equated_p (offset_expr.X_op_symbol)
4814                  && (S_GET_SEGMENT
4815                      (symbol_get_value_expression (offset_expr.X_op_symbol)
4816                       ->X_add_symbol)
4817                      == now_seg)))
4818           && (offset_expr.X_add_number == 0
4819               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4820         {
4821           if (breg == 0)
4822             {
4823               tempreg = treg;
4824               used_at = 0;
4825               macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4826                            BFD_RELOC_PCREL_HI16_S);
4827             }
4828           else
4829             {
4830               macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4831                            BFD_RELOC_PCREL_HI16_S);
4832               macro_build (NULL, &icnt, NULL,
4833                            (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4834                            "d,v,t", tempreg, tempreg, breg);
4835             }
4836           macro_build (NULL, &icnt, &offset_expr,
4837                        (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4838                        "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
4839           if (! used_at)
4840             return;
4841           break;
4842         }
4843
4844       if (offset_expr.X_op != O_symbol
4845           && offset_expr.X_op != O_constant)
4846         {
4847           as_bad (_("expression too complex"));
4848           offset_expr.X_op = O_constant;
4849         }
4850
4851       if (offset_expr.X_op == O_constant)
4852         load_register (&icnt, tempreg, &offset_expr,
4853                        ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4854                         ? (dbl || HAVE_64BIT_ADDRESSES)
4855                         : HAVE_64BIT_ADDRESSES));
4856       else if (mips_pic == NO_PIC)
4857         {
4858           /* If this is a reference to a GP relative symbol, we want
4859                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
4860              Otherwise we want
4861                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4862                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4863              If we have a constant, we need two instructions anyhow,
4864              so we may as well always use the latter form.
4865
4866             With 64bit address space and a usable $at we want
4867               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4868               lui       $at,<sym>               (BFD_RELOC_HI16_S)
4869               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4870               daddiu    $at,<sym>               (BFD_RELOC_LO16)
4871               dsll32    $tempreg,0
4872               daddu     $tempreg,$tempreg,$at
4873
4874             If $at is already in use, we use a path which is suboptimal
4875             on superscalar processors.
4876               lui       $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
4877               daddiu    $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
4878               dsll      $tempreg,16
4879               daddiu    $tempreg,<sym>          (BFD_RELOC_HI16_S)
4880               dsll      $tempreg,16
4881               daddiu    $tempreg,<sym>          (BFD_RELOC_LO16)
4882           */
4883           char *p = NULL;
4884           if (HAVE_64BIT_ADDRESSES)
4885             {
4886               /* We don't do GP optimization for now because RELAX_ENCODE can't
4887                  hold the data for such large chunks.  */
4888
4889               if (used_at == 0 && ! mips_opts.noat)
4890                 {
4891                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4892                                tempreg, BFD_RELOC_MIPS_HIGHEST);
4893                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4894                                AT, BFD_RELOC_HI16_S);
4895                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4896                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4897                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4898                                AT, AT, BFD_RELOC_LO16);
4899                   macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4900                                tempreg, tempreg, 0);
4901                   macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4902                                tempreg, tempreg, AT);
4903                   used_at = 1;
4904                 }
4905               else
4906                 {
4907                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4908                                tempreg, BFD_RELOC_MIPS_HIGHEST);
4909                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4910                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4911                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4912                                tempreg, tempreg, 16);
4913                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4914                                tempreg, tempreg, BFD_RELOC_HI16_S);
4915                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4916                                tempreg, tempreg, 16);
4917                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4918                                tempreg, tempreg, BFD_RELOC_LO16);
4919                 }
4920             }
4921           else
4922             {
4923               if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4924                   && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4925                 {
4926                   frag_grow (20);
4927                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4928                                "t,r,j", tempreg, mips_gp_register,
4929                                BFD_RELOC_GPREL16);
4930                   p = frag_var (rs_machine_dependent, 8, 0,
4931                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4932                                               mips_opts.warn_about_macros),
4933                                 offset_expr.X_add_symbol, 0, NULL);
4934                 }
4935               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4936               if (p != NULL)
4937                 p += 4;
4938               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4939                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4940             }
4941         }
4942       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4943         {
4944           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4945
4946           /* If this is a reference to an external symbol, and there
4947              is no constant, we want
4948                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4949              or if tempreg is PIC_CALL_REG
4950                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
4951              For a local symbol, we want
4952                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4953                nop
4954                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4955
4956              If we have a small constant, and this is a reference to
4957              an external symbol, we want
4958                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4959                nop
4960                addiu    $tempreg,$tempreg,<constant>
4961              For a local symbol, we want the same instruction
4962              sequence, but we output a BFD_RELOC_LO16 reloc on the
4963              addiu instruction.
4964
4965              If we have a large constant, and this is a reference to
4966              an external symbol, we want
4967                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4968                lui      $at,<hiconstant>
4969                addiu    $at,$at,<loconstant>
4970                addu     $tempreg,$tempreg,$at
4971              For a local symbol, we want the same instruction
4972              sequence, but we output a BFD_RELOC_LO16 reloc on the
4973              addiu instruction.
4974            */
4975
4976           expr1.X_add_number = offset_expr.X_add_number;
4977           offset_expr.X_add_number = 0;
4978           frag_grow (32);
4979           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4980             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4981           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4982                        tempreg, lw_reloc_type, mips_gp_register);
4983           if (expr1.X_add_number == 0)
4984             {
4985               int off;
4986               char *p;
4987
4988               if (breg == 0)
4989                 off = 0;
4990               else
4991                 {
4992                   /* We're going to put in an addu instruction using
4993                      tempreg, so we may as well insert the nop right
4994                      now.  */
4995                   macro_build (NULL, &icnt, NULL, "nop", "");
4996                   off = 4;
4997                 }
4998               p = frag_var (rs_machine_dependent, 8 - off, 0,
4999                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5000                                           (breg == 0
5001                                            ? mips_opts.warn_about_macros
5002                                            : 0)),
5003                             offset_expr.X_add_symbol, 0, NULL);
5004               if (breg == 0)
5005                 {
5006                   macro_build (p, &icnt, NULL, "nop", "");
5007                   p += 4;
5008                 }
5009               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5010                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5011               /* FIXME: If breg == 0, and the next instruction uses
5012                  $tempreg, then if this variant case is used an extra
5013                  nop will be generated.  */
5014             }
5015           else if (expr1.X_add_number >= -0x8000
5016                    && expr1.X_add_number < 0x8000)
5017             {
5018               macro_build (NULL, &icnt, NULL, "nop", "");
5019               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5020                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5021               frag_var (rs_machine_dependent, 0, 0,
5022                         RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5023                         offset_expr.X_add_symbol, 0, NULL);
5024             }
5025           else
5026             {
5027               int off1;
5028
5029               /* If we are going to add in a base register, and the
5030                  target register and the base register are the same,
5031                  then we are using AT as a temporary register.  Since
5032                  we want to load the constant into AT, we add our
5033                  current AT (from the global offset table) and the
5034                  register into the register now, and pretend we were
5035                  not using a base register.  */
5036               if (breg != treg)
5037                 off1 = 0;
5038               else
5039                 {
5040                   macro_build (NULL, &icnt, NULL, "nop", "");
5041                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5042                                treg, AT, breg);
5043                   breg = 0;
5044                   tempreg = treg;
5045                   off1 = -8;
5046                 }
5047
5048               /* Set mips_optimize around the lui instruction to avoid
5049                  inserting an unnecessary nop after the lw.  */
5050               hold_mips_optimize = mips_optimize;
5051               mips_optimize = 2;
5052               macro_build_lui (NULL, &icnt, &expr1, AT);
5053               mips_optimize = hold_mips_optimize;
5054
5055               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5056                            AT, AT, BFD_RELOC_LO16);
5057               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5058                            tempreg, tempreg, AT);
5059               frag_var (rs_machine_dependent, 0, 0,
5060                         RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5061                         offset_expr.X_add_symbol, 0, NULL);
5062               used_at = 1;
5063             }
5064         }
5065       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5066         {
5067           char *p = NULL;
5068           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5069           int adj = 0;
5070
5071           /* If this is a reference to an external, and there is no
5072              constant, or local symbol (*), with or without a
5073              constant, we want
5074                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5075              or if tempreg is PIC_CALL_REG
5076                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_CALL16)
5077
5078              If we have a small constant, and this is a reference to
5079              an external symbol, we want
5080                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5081                addiu    $tempreg,$tempreg,<constant>
5082
5083              If we have a large constant, and this is a reference to
5084              an external symbol, we want
5085                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_DISP)
5086                lui      $at,<hiconstant>
5087                addiu    $at,$at,<loconstant>
5088                addu     $tempreg,$tempreg,$at
5089
5090              (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5091              local symbols, even though it introduces an additional
5092              instruction.  */
5093
5094           frag_grow (28);
5095           if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5096             lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5097           if (offset_expr.X_add_number)
5098             {
5099               frag_now->tc_frag_data.tc_fr_offset =
5100                 expr1.X_add_number = offset_expr.X_add_number;
5101               offset_expr.X_add_number = 0;
5102
5103               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5104                            "t,o(b)", tempreg, lw_reloc_type,
5105                            mips_gp_register);
5106
5107               if (expr1.X_add_number >= -0x8000
5108                   && expr1.X_add_number < 0x8000)
5109                 {
5110                   macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5111                                "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5112                   p = frag_var (rs_machine_dependent, 4, 0,
5113                                 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5114                                 offset_expr.X_add_symbol, 0, NULL);
5115                 }
5116               else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5117                 {
5118                   int dreg;
5119
5120                   /* If we are going to add in a base register, and the
5121                      target register and the base register are the same,
5122                      then we are using AT as a temporary register.  Since
5123                      we want to load the constant into AT, we add our
5124                      current AT (from the global offset table) and the
5125                      register into the register now, and pretend we were
5126                      not using a base register.  */
5127                   if (breg != treg)
5128                     dreg = tempreg;
5129                   else
5130                     {
5131                       assert (tempreg == AT);
5132                       macro_build (NULL, &icnt,NULL, ADDRESS_ADD_INSN,
5133                                    "d,v,t", treg, AT, breg);
5134                       dreg = treg;
5135                       adj = 4;
5136                     }
5137
5138                   macro_build_lui (NULL, &icnt, &expr1, AT);
5139                   macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5140                                "t,r,j", AT, AT, BFD_RELOC_LO16);
5141                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5142                                dreg, dreg, AT);
5143
5144                   p = frag_var (rs_machine_dependent, 4 + adj, 0,
5145                                 RELAX_ENCODE (16 + adj, 4 + adj,
5146                                               0, 0, 0, 0),
5147                                 offset_expr.X_add_symbol, 0, NULL);
5148
5149                   used_at = 1;
5150                 }
5151               else
5152                 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5153
5154               offset_expr.X_add_number = expr1.X_add_number;
5155
5156               macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5157                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
5158                            mips_gp_register);
5159               if (adj)
5160                 {
5161                   macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5162                                treg, tempreg, breg);
5163                   breg = 0;
5164                   tempreg = treg;
5165                 }
5166             }
5167           else
5168             {
5169               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5170                            "t,o(b)", tempreg, lw_reloc_type,
5171                            mips_gp_register);
5172               if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5173                 p = frag_var (rs_machine_dependent, 0, 0,
5174                               RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5175                               offset_expr.X_add_symbol, 0, NULL);
5176             }
5177
5178           if (! p)
5179             {
5180               /* To avoid confusion in tc_gen_reloc, we must ensure
5181                  that this does not become a variant frag.  */
5182               frag_wane (frag_now);
5183               frag_new (0);
5184             }
5185         }
5186       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5187         {
5188           int gpdel;
5189           char *p;
5190           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5191           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5192           int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5193
5194           /* This is the large GOT case.  If this is a reference to an
5195              external symbol, and there is no constant, we want
5196                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5197                addu     $tempreg,$tempreg,$gp
5198                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5199              or if tempreg is PIC_CALL_REG
5200                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5201                addu     $tempreg,$tempreg,$gp
5202                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5203              For a local symbol, we want
5204                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5205                nop
5206                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5207
5208              If we have a small constant, and this is a reference to
5209              an external symbol, we want
5210                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5211                addu     $tempreg,$tempreg,$gp
5212                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5213                nop
5214                addiu    $tempreg,$tempreg,<constant>
5215              For a local symbol, we want
5216                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5217                nop
5218                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5219
5220              If we have a large constant, and this is a reference to
5221              an external symbol, we want
5222                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5223                addu     $tempreg,$tempreg,$gp
5224                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5225                lui      $at,<hiconstant>
5226                addiu    $at,$at,<loconstant>
5227                addu     $tempreg,$tempreg,$at
5228              For a local symbol, we want
5229                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
5230                lui      $at,<hiconstant>
5231                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
5232                addu     $tempreg,$tempreg,$at
5233           */
5234
5235           expr1.X_add_number = offset_expr.X_add_number;
5236           offset_expr.X_add_number = 0;
5237           frag_grow (52);
5238           if (reg_needs_delay (mips_gp_register))
5239             gpdel = 4;
5240           else
5241             gpdel = 0;
5242           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5243             {
5244               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5245               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5246             }
5247           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5248                        tempreg, lui_reloc_type);
5249           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5250                        tempreg, tempreg, mips_gp_register);
5251           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5252                        tempreg, lw_reloc_type, tempreg);
5253           if (expr1.X_add_number == 0)
5254             {
5255               int off;
5256
5257               if (breg == 0)
5258                 off = 0;
5259               else
5260                 {
5261                   /* We're going to put in an addu instruction using
5262                      tempreg, so we may as well insert the nop right
5263                      now.  */
5264                   macro_build (NULL, &icnt, NULL, "nop", "");
5265                   off = 4;
5266                 }
5267
5268               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5269                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5270                                           8 + gpdel, 0,
5271                                           (breg == 0
5272                                            ? mips_opts.warn_about_macros
5273                                            : 0)),
5274                             offset_expr.X_add_symbol, 0, NULL);
5275             }
5276           else if (expr1.X_add_number >= -0x8000
5277                    && expr1.X_add_number < 0x8000)
5278             {
5279               macro_build (NULL, &icnt, NULL, "nop", "");
5280               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5281                            tempreg, tempreg, BFD_RELOC_LO16);
5282
5283               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5284                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5285                                           (breg == 0
5286                                            ? mips_opts.warn_about_macros
5287                                            : 0)),
5288                             offset_expr.X_add_symbol, 0, NULL);
5289             }
5290           else
5291             {
5292               int adj, dreg;
5293
5294               /* If we are going to add in a base register, and the
5295                  target register and the base register are the same,
5296                  then we are using AT as a temporary register.  Since
5297                  we want to load the constant into AT, we add our
5298                  current AT (from the global offset table) and the
5299                  register into the register now, and pretend we were
5300                  not using a base register.  */
5301               if (breg != treg)
5302                 {
5303                   adj = 0;
5304                   dreg = tempreg;
5305                 }
5306               else
5307                 {
5308                   assert (tempreg == AT);
5309                   macro_build (NULL, &icnt, NULL, "nop", "");
5310                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5311                                treg, AT, breg);
5312                   dreg = treg;
5313                   adj = 8;
5314                 }
5315
5316               /* Set mips_optimize around the lui instruction to avoid
5317                  inserting an unnecessary nop after the lw.  */
5318               hold_mips_optimize = mips_optimize;
5319               mips_optimize = 2;
5320               macro_build_lui (NULL, &icnt, &expr1, AT);
5321               mips_optimize = hold_mips_optimize;
5322
5323               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5324                            AT, AT, BFD_RELOC_LO16);
5325               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5326                            dreg, dreg, AT);
5327
5328               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5329                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5330                                           8 + gpdel, 0,
5331                                           (breg == 0
5332                                            ? mips_opts.warn_about_macros
5333                                            : 0)),
5334                             offset_expr.X_add_symbol, 0, NULL);
5335
5336               used_at = 1;
5337             }
5338
5339           if (gpdel > 0)
5340             {
5341               /* This is needed because this instruction uses $gp, but
5342                  the first instruction on the main stream does not.  */
5343               macro_build (p, &icnt, NULL, "nop", "");
5344               p += 4;
5345             }
5346
5347           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5348                        tempreg, local_reloc_type, mips_gp_register);
5349           p += 4;
5350           if (expr1.X_add_number >= -0x8000
5351               && expr1.X_add_number < 0x8000)
5352             {
5353               macro_build (p, &icnt, NULL, "nop", "");
5354               p += 4;
5355               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5356                            "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5357               /* FIXME: If add_number is 0, and there was no base
5358                  register, the external symbol case ended with a load,
5359                  so if the symbol turns out to not be external, and
5360                  the next instruction uses tempreg, an unnecessary nop
5361                  will be inserted.  */
5362             }
5363           else
5364             {
5365               if (breg == treg)
5366                 {
5367                   /* We must add in the base register now, as in the
5368                      external symbol case.  */
5369                   assert (tempreg == AT);
5370                   macro_build (p, &icnt, NULL, "nop", "");
5371                   p += 4;
5372                   macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5373                                treg, AT, breg);
5374                   p += 4;
5375                   tempreg = treg;
5376                   /* We set breg to 0 because we have arranged to add
5377                      it in in both cases.  */
5378                   breg = 0;
5379                 }
5380
5381               macro_build_lui (p, &icnt, &expr1, AT);
5382               p += 4;
5383               macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5384                            AT, AT, BFD_RELOC_LO16);
5385               p += 4;
5386               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5387                            tempreg, tempreg, AT);
5388               p += 4;
5389             }
5390         }
5391       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5392         {
5393           char *p = NULL;
5394           int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5395           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5396           int adj = 0;
5397
5398           /* This is the large GOT case.  If this is a reference to an
5399              external symbol, and there is no constant, we want
5400                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5401                add      $tempreg,$tempreg,$gp
5402                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5403              or if tempreg is PIC_CALL_REG
5404                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_CALL_HI16)
5405                add      $tempreg,$tempreg,$gp
5406                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5407
5408              If we have a small constant, and this is a reference to
5409              an external symbol, we want
5410                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5411                add      $tempreg,$tempreg,$gp
5412                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5413                addi     $tempreg,$tempreg,<constant>
5414
5415              If we have a large constant, and this is a reference to
5416              an external symbol, we want
5417                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
5418                addu     $tempreg,$tempreg,$gp
5419                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5420                lui      $at,<hiconstant>
5421                addi     $at,$at,<loconstant>
5422                add      $tempreg,$tempreg,$at
5423
5424              If we have NewABI, and we know it's a local symbol, we want
5425                lw       $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT_PAGE)
5426                addiu    $reg,$reg,<sym>         (BFD_RELOC_MIPS_GOT_OFST)
5427              otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5428
5429           frag_grow (40);
5430
5431           frag_now->tc_frag_data.tc_fr_offset =
5432             expr1.X_add_number = offset_expr.X_add_number;
5433           offset_expr.X_add_number = 0;
5434
5435           if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5436             {
5437               lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5438               lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5439             }
5440           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5441                        tempreg, lui_reloc_type);
5442           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5443                        tempreg, tempreg, mips_gp_register);
5444           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5445                        "t,o(b)", tempreg, lw_reloc_type, tempreg);
5446
5447           if (expr1.X_add_number == 0)
5448             {
5449               p = frag_var (rs_machine_dependent, 8, 0,
5450                             RELAX_ENCODE (12, 8, 0, 4, 0,
5451                                           mips_opts.warn_about_macros),
5452                             offset_expr.X_add_symbol, 0, NULL);
5453             }
5454           else if (expr1.X_add_number >= -0x8000
5455                    && expr1.X_add_number < 0x8000)
5456             {
5457               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5458                            tempreg, tempreg, BFD_RELOC_LO16);
5459               p = frag_var (rs_machine_dependent, 8, 0,
5460                             RELAX_ENCODE (16, 8, 0, 4, 0,
5461                                           mips_opts.warn_about_macros),
5462                             offset_expr.X_add_symbol, 0, NULL);
5463             }
5464           else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5465             {
5466               int dreg;
5467
5468               /* If we are going to add in a base register, and the
5469                  target register and the base register are the same,
5470                  then we are using AT as a temporary register.  Since
5471                  we want to load the constant into AT, we add our
5472                  current AT (from the global offset table) and the
5473                  register into the register now, and pretend we were
5474                  not using a base register.  */
5475               if (breg != treg)
5476                 dreg = tempreg;
5477               else
5478                 {
5479                   assert (tempreg == AT);
5480                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5481                                treg, AT, breg);
5482                   dreg = treg;
5483                   adj = 4;
5484                 }
5485
5486               /* Set mips_optimize around the lui instruction to avoid
5487                  inserting an unnecessary nop after the lw.  */
5488               macro_build_lui (NULL, &icnt, &expr1, AT);
5489               macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5490                            "t,r,j", AT, AT, BFD_RELOC_LO16);
5491               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5492                            dreg, dreg, AT);
5493
5494               p = frag_var (rs_machine_dependent, 8 + adj, 0,
5495                             RELAX_ENCODE (24 + adj, 8 + adj,
5496                                           0, 4, 0,
5497                                           (breg == 0
5498                                            ? mips_opts.warn_about_macros
5499                                            : 0)),
5500                             offset_expr.X_add_symbol, 0, NULL);
5501
5502               used_at = 1;
5503             }
5504           else
5505             as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5506
5507           offset_expr.X_add_number = expr1.X_add_number;
5508           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5509                        tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5510           macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5511                        tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
5512           if (adj)
5513             {
5514               macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5515                            treg, tempreg, breg);
5516               breg = 0;
5517               tempreg = treg;
5518             }
5519         }
5520       else if (mips_pic == EMBEDDED_PIC)
5521         {
5522           /* We use
5523                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_GPREL16)
5524              */
5525           macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5526                        tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5527         }
5528       else
5529         abort ();
5530
5531       if (breg != 0)
5532         {
5533           char *s;
5534
5535           if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5536             s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5537           else
5538             s = ADDRESS_ADD_INSN;
5539
5540           macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
5541         }
5542
5543       if (! used_at)
5544         return;
5545
5546       break;
5547
5548     case M_J_A:
5549       /* The j instruction may not be used in PIC code, since it
5550          requires an absolute address.  We convert it to a b
5551          instruction.  */
5552       if (mips_pic == NO_PIC)
5553         macro_build (NULL, &icnt, &offset_expr, "j", "a");
5554       else
5555         macro_build (NULL, &icnt, &offset_expr, "b", "p");
5556       return;
5557
5558       /* The jal instructions must be handled as macros because when
5559          generating PIC code they expand to multi-instruction
5560          sequences.  Normally they are simple instructions.  */
5561     case M_JAL_1:
5562       dreg = RA;
5563       /* Fall through.  */
5564     case M_JAL_2:
5565       if (mips_pic == NO_PIC
5566           || mips_pic == EMBEDDED_PIC)
5567         macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5568       else if (mips_pic == SVR4_PIC)
5569         {
5570           if (sreg != PIC_CALL_REG)
5571             as_warn (_("MIPS PIC call to register other than $25"));
5572
5573           macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5574           if (! HAVE_NEWABI)
5575             {
5576               if (mips_cprestore_offset < 0)
5577                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5578               else
5579                 {
5580                   if (! mips_frame_reg_valid)
5581                     {
5582                       as_warn (_("No .frame pseudo-op used in PIC code"));
5583                       /* Quiet this warning.  */
5584                       mips_frame_reg_valid = 1;
5585                     }
5586                   if (! mips_cprestore_valid)
5587                     {
5588                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5589                       /* Quiet this warning.  */
5590                       mips_cprestore_valid = 1;
5591                     }
5592                   expr1.X_add_number = mips_cprestore_offset;
5593                   macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5594                                                 ADDRESS_LOAD_INSN,
5595                                                 mips_gp_register,
5596                                                 mips_frame_reg,
5597                                                 HAVE_64BIT_ADDRESSES);
5598                 }
5599             }
5600         }
5601       else
5602         abort ();
5603
5604       return;
5605
5606     case M_JAL_A:
5607       if (mips_pic == NO_PIC)
5608         macro_build (NULL, &icnt, &offset_expr, "jal", "a");
5609       else if (mips_pic == SVR4_PIC)
5610         {
5611           char *p;
5612
5613           /* If this is a reference to an external symbol, and we are
5614              using a small GOT, we want
5615                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
5616                nop
5617                jalr     $ra,$25
5618                nop
5619                lw       $gp,cprestore($sp)
5620              The cprestore value is set using the .cprestore
5621              pseudo-op.  If we are using a big GOT, we want
5622                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
5623                addu     $25,$25,$gp
5624                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
5625                nop
5626                jalr     $ra,$25
5627                nop
5628                lw       $gp,cprestore($sp)
5629              If the symbol is not external, we want
5630                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5631                nop
5632                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
5633                jalr     $ra,$25
5634                nop
5635                lw $gp,cprestore($sp)
5636
5637              For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5638              sequences above, minus nops, unless the symbol is local,
5639              which enables us to use GOT_PAGE/GOT_OFST (big got) or
5640              GOT_DISP.  */
5641           if (HAVE_NEWABI)
5642             {
5643               if (! mips_big_got)
5644                 {
5645                   frag_grow (4);
5646                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5647                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5648                                mips_gp_register);
5649                   frag_var (rs_machine_dependent, 0, 0,
5650                             RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5651                             offset_expr.X_add_symbol, 0, NULL);
5652                 }
5653               else
5654                 {
5655                   frag_grow (20);
5656                   macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5657                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5658                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5659                                PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5660                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5661                                "t,o(b)", PIC_CALL_REG,
5662                                BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5663                   p = frag_var (rs_machine_dependent, 8, 0,
5664                                 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5665                                 offset_expr.X_add_symbol, 0, NULL);
5666                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5667                                "t,o(b)", PIC_CALL_REG,
5668                                BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5669                   macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5670                                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5671                                BFD_RELOC_MIPS_GOT_OFST);
5672                 }
5673
5674               macro_build_jalr (icnt, &offset_expr);
5675             }
5676           else
5677             {
5678               frag_grow (40);
5679               if (! mips_big_got)
5680                 {
5681                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5682                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5683                                mips_gp_register);
5684                   macro_build (NULL, &icnt, NULL, "nop", "");
5685                   p = frag_var (rs_machine_dependent, 4, 0,
5686                                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5687                                 offset_expr.X_add_symbol, 0, NULL);
5688                 }
5689               else
5690                 {
5691                   int gpdel;
5692
5693                   if (reg_needs_delay (mips_gp_register))
5694                     gpdel = 4;
5695                   else
5696                     gpdel = 0;
5697                   macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5698                                PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5699                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5700                                PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5701                   macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5702                                "t,o(b)", PIC_CALL_REG,
5703                                BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5704                   macro_build (NULL, &icnt, NULL, "nop", "");
5705                   p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5706                                 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5707                                               8 + gpdel, 0, 0),
5708                                 offset_expr.X_add_symbol, 0, NULL);
5709                   if (gpdel > 0)
5710                     {
5711                       macro_build (p, &icnt, NULL, "nop", "");
5712                       p += 4;
5713                     }
5714                   macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5715                                "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5716                                mips_gp_register);
5717                   p += 4;
5718                   macro_build (p, &icnt, NULL, "nop", "");
5719                   p += 4;
5720                 }
5721               macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5722                            "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5723                            BFD_RELOC_LO16);
5724               macro_build_jalr (icnt, &offset_expr);
5725
5726               if (mips_cprestore_offset < 0)
5727                 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5728               else
5729                 {
5730                   if (! mips_frame_reg_valid)
5731                     {
5732                       as_warn (_("No .frame pseudo-op used in PIC code"));
5733                       /* Quiet this warning.  */
5734                       mips_frame_reg_valid = 1;
5735                     }
5736                   if (! mips_cprestore_valid)
5737                     {
5738                       as_warn (_("No .cprestore pseudo-op used in PIC code"));
5739                       /* Quiet this warning.  */
5740                       mips_cprestore_valid = 1;
5741                     }
5742                   if (mips_opts.noreorder)
5743                     macro_build (NULL, &icnt, NULL, "nop", "");
5744                   expr1.X_add_number = mips_cprestore_offset;
5745                   macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5746                                                 ADDRESS_LOAD_INSN,
5747                                                 mips_gp_register,
5748                                                 mips_frame_reg,
5749                                                 HAVE_64BIT_ADDRESSES);
5750                 }
5751             }
5752         }
5753       else if (mips_pic == EMBEDDED_PIC)
5754         {
5755           macro_build (NULL, &icnt, &offset_expr, "bal", "p");
5756           /* The linker may expand the call to a longer sequence which
5757              uses $at, so we must break rather than return.  */
5758           break;
5759         }
5760       else
5761         abort ();
5762
5763       return;
5764
5765     case M_LB_AB:
5766       s = "lb";
5767       goto ld;
5768     case M_LBU_AB:
5769       s = "lbu";
5770       goto ld;
5771     case M_LH_AB:
5772       s = "lh";
5773       goto ld;
5774     case M_LHU_AB:
5775       s = "lhu";
5776       goto ld;
5777     case M_LW_AB:
5778       s = "lw";
5779       goto ld;
5780     case M_LWC0_AB:
5781       s = "lwc0";
5782       /* Itbl support may require additional care here.  */
5783       coproc = 1;
5784       goto ld;
5785     case M_LWC1_AB:
5786       s = "lwc1";
5787       /* Itbl support may require additional care here.  */
5788       coproc = 1;
5789       goto ld;
5790     case M_LWC2_AB:
5791       s = "lwc2";
5792       /* Itbl support may require additional care here.  */
5793       coproc = 1;
5794       goto ld;
5795     case M_LWC3_AB:
5796       s = "lwc3";
5797       /* Itbl support may require additional care here.  */
5798       coproc = 1;
5799       goto ld;
5800     case M_LWL_AB:
5801       s = "lwl";
5802       lr = 1;
5803       goto ld;
5804     case M_LWR_AB:
5805       s = "lwr";
5806       lr = 1;
5807       goto ld;
5808     case M_LDC1_AB:
5809       if (mips_opts.arch == CPU_R4650)
5810         {
5811           as_bad (_("opcode not supported on this processor"));
5812           return;
5813         }
5814       s = "ldc1";
5815       /* Itbl support may require additional care here.  */
5816       coproc = 1;
5817       goto ld;
5818     case M_LDC2_AB:
5819       s = "ldc2";
5820       /* Itbl support may require additional care here.  */
5821       coproc = 1;
5822       goto ld;
5823     case M_LDC3_AB:
5824       s = "ldc3";
5825       /* Itbl support may require additional care here.  */
5826       coproc = 1;
5827       goto ld;
5828     case M_LDL_AB:
5829       s = "ldl";
5830       lr = 1;
5831       goto ld;
5832     case M_LDR_AB:
5833       s = "ldr";
5834       lr = 1;
5835       goto ld;
5836     case M_LL_AB:
5837       s = "ll";
5838       goto ld;
5839     case M_LLD_AB:
5840       s = "lld";
5841       goto ld;
5842     case M_LWU_AB:
5843       s = "lwu";
5844     ld:
5845       if (breg == treg || coproc || lr)
5846         {
5847           tempreg = AT;
5848           used_at = 1;
5849         }
5850       else
5851         {
5852           tempreg = treg;
5853           used_at = 0;
5854         }
5855       goto ld_st;
5856     case M_SB_AB:
5857       s = "sb";
5858       goto st;
5859     case M_SH_AB:
5860       s = "sh";
5861       goto st;
5862     case M_SW_AB:
5863       s = "sw";
5864       goto st;
5865     case M_SWC0_AB:
5866       s = "swc0";
5867       /* Itbl support may require additional care here.  */
5868       coproc = 1;
5869       goto st;
5870     case M_SWC1_AB:
5871       s = "swc1";
5872       /* Itbl support may require additional care here.  */
5873       coproc = 1;
5874       goto st;
5875     case M_SWC2_AB:
5876       s = "swc2";
5877       /* Itbl support may require additional care here.  */
5878       coproc = 1;
5879       goto st;
5880     case M_SWC3_AB:
5881       s = "swc3";
5882       /* Itbl support may require additional care here.  */
5883       coproc = 1;
5884       goto st;
5885     case M_SWL_AB:
5886       s = "swl";
5887       goto st;
5888     case M_SWR_AB:
5889       s = "swr";
5890       goto st;
5891     case M_SC_AB:
5892       s = "sc";
5893       goto st;
5894     case M_SCD_AB:
5895       s = "scd";
5896       goto st;
5897     case M_SDC1_AB:
5898       if (mips_opts.arch == CPU_R4650)
5899         {
5900           as_bad (_("opcode not supported on this processor"));
5901           return;
5902         }
5903       s = "sdc1";
5904       coproc = 1;
5905       /* Itbl support may require additional care here.  */
5906       goto st;
5907     case M_SDC2_AB:
5908       s = "sdc2";
5909       /* Itbl support may require additional care here.  */
5910       coproc = 1;
5911       goto st;
5912     case M_SDC3_AB:
5913       s = "sdc3";
5914       /* Itbl support may require additional care here.  */
5915       coproc = 1;
5916       goto st;
5917     case M_SDL_AB:
5918       s = "sdl";
5919       goto st;
5920     case M_SDR_AB:
5921       s = "sdr";
5922     st:
5923       tempreg = AT;
5924       used_at = 1;
5925     ld_st:
5926       /* Itbl support may require additional care here.  */
5927       if (mask == M_LWC1_AB
5928           || mask == M_SWC1_AB
5929           || mask == M_LDC1_AB
5930           || mask == M_SDC1_AB
5931           || mask == M_L_DAB
5932           || mask == M_S_DAB)
5933         fmt = "T,o(b)";
5934       else if (coproc)
5935         fmt = "E,o(b)";
5936       else
5937         fmt = "t,o(b)";
5938
5939       /* Sign-extending 32-bit constants makes their handling easier.
5940          The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5941          described below.  */
5942       if ((! HAVE_64BIT_ADDRESSES
5943            && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
5944           && (offset_expr.X_op == O_constant))
5945         {
5946           if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff)
5947               && ~(offset_expr.X_add_number | 0xffffffff))
5948             as_bad (_("too large constant specified"));
5949
5950         offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5951                                      ^ 0x80000000) - 0x80000000);
5952         }
5953
5954       /* For embedded PIC, we allow loads where the offset is calculated
5955          by subtracting a symbol in the current segment from an unknown
5956          symbol, relative to a base register, e.g.:
5957                 <op>    $treg, <sym>-<localsym>($breg)
5958          This is used by the compiler for switch statements.  */
5959       if (mips_pic == EMBEDDED_PIC
5960           && offset_expr.X_op == O_subtract
5961           && (symbol_constant_p (offset_expr.X_op_symbol)
5962               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5963               : (symbol_equated_p (offset_expr.X_op_symbol)
5964                  && (S_GET_SEGMENT
5965                      (symbol_get_value_expression (offset_expr.X_op_symbol)
5966                       ->X_add_symbol)
5967                      == now_seg)))
5968           && breg != 0
5969           && (offset_expr.X_add_number == 0
5970               || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5971         {
5972           /* For this case, we output the instructions:
5973                 lui     $tempreg,<sym>          (BFD_RELOC_PCREL_HI16_S)
5974                 addiu   $tempreg,$tempreg,$breg
5975                 <op>    $treg,<sym>($tempreg)   (BFD_RELOC_PCREL_LO16)
5976              If the relocation would fit entirely in 16 bits, it would be
5977              nice to emit:
5978                 <op>    $treg,<sym>($breg)      (BFD_RELOC_PCREL_LO16)
5979              instead, but that seems quite difficult.  */
5980           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
5981                        BFD_RELOC_PCREL_HI16_S);
5982           macro_build (NULL, &icnt, NULL,
5983                        ((bfd_arch_bits_per_address (stdoutput) == 32
5984                          || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5985                         ? "addu" : "daddu"),
5986                        "d,v,t", tempreg, tempreg, breg);
5987           macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5988                        BFD_RELOC_PCREL_LO16, tempreg);
5989           if (! used_at)
5990             return;
5991           break;
5992         }
5993
5994       if (offset_expr.X_op != O_constant
5995           && offset_expr.X_op != O_symbol)
5996         {
5997           as_bad (_("expression too complex"));
5998           offset_expr.X_op = O_constant;
5999         }
6000
6001       /* A constant expression in PIC code can be handled just as it
6002          is in non PIC code.  */
6003       if (mips_pic == NO_PIC
6004           || offset_expr.X_op == O_constant)
6005         {
6006           char *p;
6007
6008           /* If this is a reference to a GP relative symbol, and there
6009              is no base register, we want
6010                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6011              Otherwise, if there is no base register, we want
6012                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6013                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6014              If we have a constant, we need two instructions anyhow,
6015              so we always use the latter form.
6016
6017              If we have a base register, and this is a reference to a
6018              GP relative symbol, we want
6019                addu     $tempreg,$breg,$gp
6020                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6021              Otherwise we want
6022                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
6023                addu     $tempreg,$tempreg,$breg
6024                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6025              With a constant we always use the latter case.
6026
6027              With 64bit address space and no base register and $at usable,
6028              we want
6029                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6030                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6031                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6032                dsll32   $tempreg,0
6033                daddu    $tempreg,$at
6034                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6035              If we have a base register, we want
6036                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6037                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6038                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6039                daddu    $at,$breg
6040                dsll32   $tempreg,0
6041                daddu    $tempreg,$at
6042                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6043
6044              Without $at we can't generate the optimal path for superscalar
6045              processors here since this would require two temporary registers.
6046                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6047                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6048                dsll     $tempreg,16
6049                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6050                dsll     $tempreg,16
6051                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6052              If we have a base register, we want
6053                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHEST)
6054                daddiu   $tempreg,<sym>          (BFD_RELOC_MIPS_HIGHER)
6055                dsll     $tempreg,16
6056                daddiu   $tempreg,<sym>          (BFD_RELOC_HI16_S)
6057                dsll     $tempreg,16
6058                daddu    $tempreg,$tempreg,$breg
6059                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
6060
6061              If we have 64-bit addresses, as an optimization, for
6062              addresses which are 32-bit constants (e.g. kseg0/kseg1
6063              addresses) we fall back to the 32-bit address generation
6064              mechanism since it is more efficient.  Note that due to
6065              the signed offset used by memory operations, the 32-bit
6066              range is shifted down by 32768 here.  This code should
6067              probably attempt to generate 64-bit constants more
6068              efficiently in general.
6069
6070              As an extension for architectures with 64-bit registers,
6071              we don't truncate 64-bit addresses given as literal
6072              constants down to 32 bits, to support existing practice
6073              in the mips64 Linux (the kernel), that compiles source
6074              files with -mabi=64, assembling them as o32 or n32 (with
6075              -Wa,-32 or -Wa,-n32).  This is not beautiful, but since
6076              the whole kernel is loaded into a memory region that is
6077              addressible with sign-extended 32-bit addresses, it is
6078              wasteful to compute the upper 32 bits of every
6079              non-literal address, that takes more space and time.
6080              Some day this should probably be implemented as an
6081              assembler option, such that the kernel doesn't have to
6082              use such ugly hacks, even though it will still have to
6083              end up converting the binary to ELF32 for a number of
6084              platforms whose boot loaders don't support ELF64
6085              binaries.  */
6086           if ((HAVE_64BIT_ADDRESSES
6087                && ! (offset_expr.X_op == O_constant
6088                      && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6089               || (HAVE_64BIT_GPRS
6090                   && offset_expr.X_op == O_constant
6091                   && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6092             {
6093               p = NULL;
6094
6095               /* We don't do GP optimization for now because RELAX_ENCODE can't
6096                  hold the data for such large chunks.  */
6097
6098               if (used_at == 0 && ! mips_opts.noat)
6099                 {
6100                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6101                                tempreg, BFD_RELOC_MIPS_HIGHEST);
6102                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6103                                AT, BFD_RELOC_HI16_S);
6104                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6105                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6106                   if (breg != 0)
6107                     macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6108                                  AT, AT, breg);
6109                   macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
6110                                tempreg, tempreg, 0);
6111                   macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6112                                tempreg, tempreg, AT);
6113                   macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6114                                BFD_RELOC_LO16, tempreg);
6115                   used_at = 1;
6116                 }
6117               else
6118                 {
6119                   macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6120                                tempreg, BFD_RELOC_MIPS_HIGHEST);
6121                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6122                                tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6123                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6124                                tempreg, tempreg, 16);
6125                   macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6126                                tempreg, tempreg, BFD_RELOC_HI16_S);
6127                   macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6128                                tempreg, tempreg, 16);
6129                   if (breg != 0)
6130                     macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6131                                  tempreg, tempreg, breg);
6132                   macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6133                                BFD_RELOC_LO16, tempreg);
6134                 }
6135
6136               return;
6137             }
6138
6139           if (offset_expr.X_op == O_constant
6140               && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6141             as_bad (_("load/store address overflow (max 32 bits)"));
6142
6143           if (breg == 0)
6144             {
6145               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6146                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6147                 p = NULL;
6148               else
6149                 {
6150                   frag_grow (20);
6151                   macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6152                                BFD_RELOC_GPREL16, mips_gp_register);
6153                   p = frag_var (rs_machine_dependent, 8, 0,
6154                                 RELAX_ENCODE (4, 8, 0, 4, 0,
6155                                               (mips_opts.warn_about_macros
6156                                                || (used_at
6157                                                    && mips_opts.noat))),
6158                                 offset_expr.X_add_symbol, 0, NULL);
6159                   used_at = 0;
6160                 }
6161               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6162               if (p != NULL)
6163                 p += 4;
6164               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6165                            BFD_RELOC_LO16, tempreg);
6166             }
6167           else
6168             {
6169               if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6170                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
6171                 p = NULL;
6172               else
6173                 {
6174                   frag_grow (28);
6175                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6176                                tempreg, breg, mips_gp_register);
6177                   macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6178                                BFD_RELOC_GPREL16, tempreg);
6179                   p = frag_var (rs_machine_dependent, 12, 0,
6180                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6181                                 offset_expr.X_add_symbol, 0, NULL);
6182                 }
6183               macro_build_lui (p, &icnt, &offset_expr, tempreg);
6184               if (p != NULL)
6185                 p += 4;
6186               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6187                            tempreg, tempreg, breg);
6188               if (p != NULL)
6189                 p += 4;
6190               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6191                            BFD_RELOC_LO16, tempreg);
6192             }
6193         }
6194       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6195         {
6196           char *p;
6197           int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6198
6199           /* If this is a reference to an external symbol, we want
6200                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6201                nop
6202                <op>     $treg,0($tempreg)
6203              Otherwise we want
6204                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6205                nop
6206                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6207                <op>     $treg,0($tempreg)
6208
6209              For NewABI, we want
6210                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6211                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
6212
6213              If there is a base register, we add it to $tempreg before
6214              the <op>.  If there is a constant, we stick it in the
6215              <op> instruction.  We don't handle constants larger than
6216              16 bits, because we have no way to load the upper 16 bits
6217              (actually, we could handle them for the subset of cases
6218              in which we are not using $at).  */
6219           assert (offset_expr.X_op == O_symbol);
6220           if (HAVE_NEWABI)
6221             {
6222               macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6223                            "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6224                            mips_gp_register);
6225               if (breg != 0)
6226                 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6227                              tempreg, tempreg, breg);
6228               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6229                            BFD_RELOC_MIPS_GOT_OFST, tempreg);
6230
6231               if (! used_at)
6232                 return;
6233
6234               break;
6235             }
6236           expr1.X_add_number = offset_expr.X_add_number;
6237           offset_expr.X_add_number = 0;
6238           if (expr1.X_add_number < -0x8000
6239               || expr1.X_add_number >= 0x8000)
6240             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6241           frag_grow (20);
6242           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6243                        tempreg, lw_reloc_type, mips_gp_register);
6244           macro_build (NULL, &icnt, NULL, "nop", "");
6245           p = frag_var (rs_machine_dependent, 4, 0,
6246                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6247                         offset_expr.X_add_symbol, 0, NULL);
6248           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6249                        "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
6250           if (breg != 0)
6251             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6252                          tempreg, tempreg, breg);
6253           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6254                        tempreg);
6255         }
6256       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6257         {
6258           int gpdel;
6259           char *p;
6260
6261           /* If this is a reference to an external symbol, we want
6262                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6263                addu     $tempreg,$tempreg,$gp
6264                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6265                <op>     $treg,0($tempreg)
6266              Otherwise we want
6267                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
6268                nop
6269                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6270                <op>     $treg,0($tempreg)
6271              If there is a base register, we add it to $tempreg before
6272              the <op>.  If there is a constant, we stick it in the
6273              <op> instruction.  We don't handle constants larger than
6274              16 bits, because we have no way to load the upper 16 bits
6275              (actually, we could handle them for the subset of cases
6276              in which we are not using $at).  */
6277           assert (offset_expr.X_op == O_symbol);
6278           expr1.X_add_number = offset_expr.X_add_number;
6279           offset_expr.X_add_number = 0;
6280           if (expr1.X_add_number < -0x8000
6281               || expr1.X_add_number >= 0x8000)
6282             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6283           if (reg_needs_delay (mips_gp_register))
6284             gpdel = 4;
6285           else
6286             gpdel = 0;
6287           frag_grow (36);
6288           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6289                        BFD_RELOC_MIPS_GOT_HI16);
6290           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6291                        tempreg, tempreg, mips_gp_register);
6292           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6293                        tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6294           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6295                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6296                         offset_expr.X_add_symbol, 0, NULL);
6297           if (gpdel > 0)
6298             {
6299               macro_build (p, &icnt, NULL, "nop", "");
6300               p += 4;
6301             }
6302           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6303                        tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6304           p += 4;
6305           macro_build (p, &icnt, NULL, "nop", "");
6306           p += 4;
6307           macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6308                        tempreg, tempreg, BFD_RELOC_LO16);
6309           if (breg != 0)
6310             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6311                          tempreg, tempreg, breg);
6312           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6313                        tempreg);
6314         }
6315       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6316         {
6317           char *p;
6318           int bregsz = breg != 0 ? 4 : 0;
6319
6320           /* If this is a reference to an external symbol, we want
6321                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
6322                add      $tempreg,$tempreg,$gp
6323                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6324                <op>     $treg,<ofst>($tempreg)
6325              Otherwise, for local symbols, we want:
6326                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT_PAGE)
6327                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6328           assert (offset_expr.X_op == O_symbol);
6329           frag_now->tc_frag_data.tc_fr_offset =
6330             expr1.X_add_number = offset_expr.X_add_number;
6331           offset_expr.X_add_number = 0;
6332           if (expr1.X_add_number < -0x8000
6333               || expr1.X_add_number >= 0x8000)
6334             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6335           frag_grow (36);
6336           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6337                        BFD_RELOC_MIPS_GOT_HI16);
6338           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6339                        tempreg, tempreg, mips_gp_register);
6340           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6341                        tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6342           if (breg != 0)
6343             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6344                          tempreg, tempreg, breg);
6345           macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6346                        tempreg);
6347
6348           offset_expr.X_add_number = expr1.X_add_number;
6349           p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6350                         RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6351                                       0, 4 + bregsz, 0, 0),
6352                         offset_expr.X_add_symbol, 0, NULL);
6353           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6354                        tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6355           if (breg != 0)
6356             macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6357                          tempreg, tempreg, breg);
6358           macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6359                        BFD_RELOC_MIPS_GOT_OFST, tempreg);
6360         }
6361       else if (mips_pic == EMBEDDED_PIC)
6362         {
6363           /* If there is no base register, we want
6364                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6365              If there is a base register, we want
6366                addu     $tempreg,$breg,$gp
6367                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_GPREL16)
6368              */
6369           assert (offset_expr.X_op == O_symbol);
6370           if (breg == 0)
6371             {
6372               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6373                            BFD_RELOC_GPREL16, mips_gp_register);
6374               used_at = 0;
6375             }
6376           else
6377             {
6378               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6379                            tempreg, breg, mips_gp_register);
6380               macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6381                            BFD_RELOC_GPREL16, tempreg);
6382             }
6383         }
6384       else
6385         abort ();
6386
6387       if (! used_at)
6388         return;
6389
6390       break;
6391
6392     case M_LI:
6393     case M_LI_S:
6394       load_register (&icnt, treg, &imm_expr, 0);
6395       return;
6396
6397     case M_DLI:
6398       load_register (&icnt, treg, &imm_expr, 1);
6399       return;
6400
6401     case M_LI_SS:
6402       if (imm_expr.X_op == O_constant)
6403         {
6404           load_register (&icnt, AT, &imm_expr, 0);
6405           macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6406           break;
6407         }
6408       else
6409         {
6410           assert (offset_expr.X_op == O_symbol
6411                   && strcmp (segment_name (S_GET_SEGMENT
6412                                            (offset_expr.X_add_symbol)),
6413                              ".lit4") == 0
6414                   && offset_expr.X_add_number == 0);
6415           macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6416                        BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6417           return;
6418         }
6419
6420     case M_LI_D:
6421       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6422          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6423          order 32 bits of the value and the low order 32 bits are either
6424          zero or in OFFSET_EXPR.  */
6425       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6426         {
6427           if (HAVE_64BIT_GPRS)
6428             load_register (&icnt, treg, &imm_expr, 1);
6429           else
6430             {
6431               int hreg, lreg;
6432
6433               if (target_big_endian)
6434                 {
6435                   hreg = treg;
6436                   lreg = treg + 1;
6437                 }
6438               else
6439                 {
6440                   hreg = treg + 1;
6441                   lreg = treg;
6442                 }
6443
6444               if (hreg <= 31)
6445                 load_register (&icnt, hreg, &imm_expr, 0);
6446               if (lreg <= 31)
6447                 {
6448                   if (offset_expr.X_op == O_absent)
6449                     move_register (&icnt, lreg, 0);
6450                   else
6451                     {
6452                       assert (offset_expr.X_op == O_constant);
6453                       load_register (&icnt, lreg, &offset_expr, 0);
6454                     }
6455                 }
6456             }
6457           return;
6458         }
6459
6460       /* We know that sym is in the .rdata section.  First we get the
6461          upper 16 bits of the address.  */
6462       if (mips_pic == NO_PIC)
6463         {
6464           macro_build_lui (NULL, &icnt, &offset_expr, AT);
6465         }
6466       else if (mips_pic == SVR4_PIC)
6467         {
6468           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6469                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6470         }
6471       else if (mips_pic == EMBEDDED_PIC)
6472         {
6473           /* For embedded PIC we pick up the entire address off $gp in
6474              a single instruction.  */
6475           macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6476                        AT, mips_gp_register, BFD_RELOC_GPREL16);
6477           offset_expr.X_op = O_constant;
6478           offset_expr.X_add_number = 0;
6479         }
6480       else
6481         abort ();
6482
6483       /* Now we load the register(s).  */
6484       if (HAVE_64BIT_GPRS)
6485         macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6486                      BFD_RELOC_LO16, AT);
6487       else
6488         {
6489           macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6490                        BFD_RELOC_LO16, AT);
6491           if (treg != RA)
6492             {
6493               /* FIXME: How in the world do we deal with the possible
6494                  overflow here?  */
6495               offset_expr.X_add_number += 4;
6496               macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6497                            treg + 1, BFD_RELOC_LO16, AT);
6498             }
6499         }
6500
6501       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6502          does not become a variant frag.  */
6503       frag_wane (frag_now);
6504       frag_new (0);
6505
6506       break;
6507
6508     case M_LI_DD:
6509       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6510          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6511          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6512          the value and the low order 32 bits are either zero or in
6513          OFFSET_EXPR.  */
6514       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6515         {
6516           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6517           if (HAVE_64BIT_FPRS)
6518             {
6519               assert (HAVE_64BIT_GPRS);
6520               macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
6521             }
6522           else
6523             {
6524               macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
6525               if (offset_expr.X_op == O_absent)
6526                 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
6527               else
6528                 {
6529                   assert (offset_expr.X_op == O_constant);
6530                   load_register (&icnt, AT, &offset_expr, 0);
6531                   macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6532                 }
6533             }
6534           break;
6535         }
6536
6537       assert (offset_expr.X_op == O_symbol
6538               && offset_expr.X_add_number == 0);
6539       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6540       if (strcmp (s, ".lit8") == 0)
6541         {
6542           if (mips_opts.isa != ISA_MIPS1)
6543             {
6544               macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6545                            BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6546               return;
6547             }
6548           breg = mips_gp_register;
6549           r = BFD_RELOC_MIPS_LITERAL;
6550           goto dob;
6551         }
6552       else
6553         {
6554           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6555           if (mips_pic == SVR4_PIC)
6556             macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6557                          "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6558                          mips_gp_register);
6559           else
6560             {
6561               /* FIXME: This won't work for a 64 bit address.  */
6562               macro_build_lui (NULL, &icnt, &offset_expr, AT);
6563             }
6564
6565           if (mips_opts.isa != ISA_MIPS1)
6566             {
6567               macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6568                            BFD_RELOC_LO16, AT);
6569
6570               /* To avoid confusion in tc_gen_reloc, we must ensure
6571                  that this does not become a variant frag.  */
6572               frag_wane (frag_now);
6573               frag_new (0);
6574
6575               break;
6576             }
6577           breg = AT;
6578           r = BFD_RELOC_LO16;
6579           goto dob;
6580         }
6581
6582     case M_L_DOB:
6583       if (mips_opts.arch == CPU_R4650)
6584         {
6585           as_bad (_("opcode not supported on this processor"));
6586           return;
6587         }
6588       /* Even on a big endian machine $fn comes before $fn+1.  We have
6589          to adjust when loading from memory.  */
6590       r = BFD_RELOC_LO16;
6591     dob:
6592       assert (mips_opts.isa == ISA_MIPS1);
6593       macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6594                    target_big_endian ? treg + 1 : treg, r, breg);
6595       /* FIXME: A possible overflow which I don't know how to deal
6596          with.  */
6597       offset_expr.X_add_number += 4;
6598       macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6599                    target_big_endian ? treg : treg + 1, r, breg);
6600
6601       /* To avoid confusion in tc_gen_reloc, we must ensure that this
6602          does not become a variant frag.  */
6603       frag_wane (frag_now);
6604       frag_new (0);
6605
6606       if (breg != AT)
6607         return;
6608       break;
6609
6610     case M_L_DAB:
6611       /*
6612        * The MIPS assembler seems to check for X_add_number not
6613        * being double aligned and generating:
6614        *        lui     at,%hi(foo+1)
6615        *        addu    at,at,v1
6616        *        addiu   at,at,%lo(foo+1)
6617        *        lwc1    f2,0(at)
6618        *        lwc1    f3,4(at)
6619        * But, the resulting address is the same after relocation so why
6620        * generate the extra instruction?
6621        */
6622       if (mips_opts.arch == CPU_R4650)
6623         {
6624           as_bad (_("opcode not supported on this processor"));
6625           return;
6626         }
6627       /* Itbl support may require additional care here.  */
6628       coproc = 1;
6629       if (mips_opts.isa != ISA_MIPS1)
6630         {
6631           s = "ldc1";
6632           goto ld;
6633         }
6634
6635       s = "lwc1";
6636       fmt = "T,o(b)";
6637       goto ldd_std;
6638
6639     case M_S_DAB:
6640       if (mips_opts.arch == CPU_R4650)
6641         {
6642           as_bad (_("opcode not supported on this processor"));
6643           return;
6644         }
6645
6646       if (mips_opts.isa != ISA_MIPS1)
6647         {
6648           s = "sdc1";
6649           goto st;
6650         }
6651
6652       s = "swc1";
6653       fmt = "T,o(b)";
6654       /* Itbl support may require additional care here.  */
6655       coproc = 1;
6656       goto ldd_std;
6657
6658     case M_LD_AB:
6659       if (HAVE_64BIT_GPRS)
6660         {
6661           s = "ld";
6662           goto ld;
6663         }
6664
6665       s = "lw";
6666       fmt = "t,o(b)";
6667       goto ldd_std;
6668
6669     case M_SD_AB:
6670       if (HAVE_64BIT_GPRS)
6671         {
6672           s = "sd";
6673           goto st;
6674         }
6675
6676       s = "sw";
6677       fmt = "t,o(b)";
6678
6679     ldd_std:
6680       /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6681          loads for the case of doing a pair of loads to simulate an 'ld'.
6682          This is not currently done by the compiler, and assembly coders
6683          writing embedded-pic code can cope.  */
6684
6685       if (offset_expr.X_op != O_symbol
6686           && offset_expr.X_op != O_constant)
6687         {
6688           as_bad (_("expression too complex"));
6689           offset_expr.X_op = O_constant;
6690         }
6691
6692       /* Even on a big endian machine $fn comes before $fn+1.  We have
6693          to adjust when loading from memory.  We set coproc if we must
6694          load $fn+1 first.  */
6695       /* Itbl support may require additional care here.  */
6696       if (! target_big_endian)
6697         coproc = 0;
6698
6699       if (mips_pic == NO_PIC
6700           || offset_expr.X_op == O_constant)
6701         {
6702           char *p;
6703
6704           /* If this is a reference to a GP relative symbol, we want
6705                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6706                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6707              If we have a base register, we use this
6708                addu     $at,$breg,$gp
6709                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6710                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6711              If this is not a GP relative symbol, we want
6712                lui      $at,<sym>               (BFD_RELOC_HI16_S)
6713                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6714                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6715              If there is a base register, we add it to $at after the
6716              lui instruction.  If there is a constant, we always use
6717              the last case.  */
6718           if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6719               || nopic_need_relax (offset_expr.X_add_symbol, 1))
6720             {
6721               p = NULL;
6722               used_at = 1;
6723             }
6724           else
6725             {
6726               int off;
6727
6728               if (breg == 0)
6729                 {
6730                   frag_grow (28);
6731                   tempreg = mips_gp_register;
6732                   off = 0;
6733                   used_at = 0;
6734                 }
6735               else
6736                 {
6737                   frag_grow (36);
6738                   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6739                                AT, breg, mips_gp_register);
6740                   tempreg = AT;
6741                   off = 4;
6742                   used_at = 1;
6743                 }
6744
6745               /* Itbl support may require additional care here.  */
6746               macro_build (NULL, &icnt, &offset_expr, s, fmt,
6747                            coproc ? treg + 1 : treg,
6748                            BFD_RELOC_GPREL16, tempreg);
6749               offset_expr.X_add_number += 4;
6750
6751               /* Set mips_optimize to 2 to avoid inserting an
6752                  undesired nop.  */
6753               hold_mips_optimize = mips_optimize;
6754               mips_optimize = 2;
6755               /* Itbl support may require additional care here.  */
6756               macro_build (NULL, &icnt, &offset_expr, s, fmt,
6757                            coproc ? treg : treg + 1,
6758                            BFD_RELOC_GPREL16, tempreg);
6759               mips_optimize = hold_mips_optimize;
6760
6761               p = frag_var (rs_machine_dependent, 12 + off, 0,
6762                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6763                                           used_at && mips_opts.noat),
6764                             offset_expr.X_add_symbol, 0, NULL);
6765
6766               /* We just generated two relocs.  When tc_gen_reloc
6767                  handles this case, it will skip the first reloc and
6768                  handle the second.  The second reloc already has an
6769                  extra addend of 4, which we added above.  We must
6770                  subtract it out, and then subtract another 4 to make
6771                  the first reloc come out right.  The second reloc
6772                  will come out right because we are going to add 4 to
6773                  offset_expr when we build its instruction below.
6774
6775                  If we have a symbol, then we don't want to include
6776                  the offset, because it will wind up being included
6777                  when we generate the reloc.  */
6778
6779               if (offset_expr.X_op == O_constant)
6780                 offset_expr.X_add_number -= 8;
6781               else
6782                 {
6783                   offset_expr.X_add_number = -4;
6784                   offset_expr.X_op = O_constant;
6785                 }
6786             }
6787           macro_build_lui (p, &icnt, &offset_expr, AT);
6788           if (p != NULL)
6789             p += 4;
6790           if (breg != 0)
6791             {
6792               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6793                            AT, breg, AT);
6794               if (p != NULL)
6795                 p += 4;
6796             }
6797           /* Itbl support may require additional care here.  */
6798           macro_build (p, &icnt, &offset_expr, s, fmt,
6799                        coproc ? treg + 1 : treg,
6800                        BFD_RELOC_LO16, AT);
6801           if (p != NULL)
6802             p += 4;
6803           /* FIXME: How do we handle overflow here?  */
6804           offset_expr.X_add_number += 4;
6805           /* Itbl support may require additional care here.  */
6806           macro_build (p, &icnt, &offset_expr, s, fmt,
6807                        coproc ? treg : treg + 1,
6808                        BFD_RELOC_LO16, AT);
6809         }
6810       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6811         {
6812           int off;
6813
6814           /* If this is a reference to an external symbol, we want
6815                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6816                nop
6817                <op>     $treg,0($at)
6818                <op>     $treg+1,4($at)
6819              Otherwise we want
6820                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6821                nop
6822                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6823                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6824              If there is a base register we add it to $at before the
6825              lwc1 instructions.  If there is a constant we include it
6826              in the lwc1 instructions.  */
6827           used_at = 1;
6828           expr1.X_add_number = offset_expr.X_add_number;
6829           offset_expr.X_add_number = 0;
6830           if (expr1.X_add_number < -0x8000
6831               || expr1.X_add_number >= 0x8000 - 4)
6832             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6833           if (breg == 0)
6834             off = 0;
6835           else
6836             off = 4;
6837           frag_grow (24 + off);
6838           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6839                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6840           macro_build (NULL, &icnt, NULL, "nop", "");
6841           if (breg != 0)
6842             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6843                          AT, breg, AT);
6844           /* Itbl support may require additional care here.  */
6845           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6846                        BFD_RELOC_LO16, AT);
6847           expr1.X_add_number += 4;
6848
6849           /* Set mips_optimize to 2 to avoid inserting an undesired
6850              nop.  */
6851           hold_mips_optimize = mips_optimize;
6852           mips_optimize = 2;
6853           /* Itbl support may require additional care here.  */
6854           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6855                        BFD_RELOC_LO16, AT);
6856           mips_optimize = hold_mips_optimize;
6857
6858           (void) frag_var (rs_machine_dependent, 0, 0,
6859                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6860                            offset_expr.X_add_symbol, 0, NULL);
6861         }
6862       else if (mips_pic == SVR4_PIC)
6863         {
6864           int gpdel, off;
6865           char *p;
6866
6867           /* If this is a reference to an external symbol, we want
6868                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
6869                addu     $at,$at,$gp
6870                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
6871                nop
6872                <op>     $treg,0($at)
6873                <op>     $treg+1,4($at)
6874              Otherwise we want
6875                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
6876                nop
6877                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
6878                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
6879              If there is a base register we add it to $at before the
6880              lwc1 instructions.  If there is a constant we include it
6881              in the lwc1 instructions.  */
6882           used_at = 1;
6883           expr1.X_add_number = offset_expr.X_add_number;
6884           offset_expr.X_add_number = 0;
6885           if (expr1.X_add_number < -0x8000
6886               || expr1.X_add_number >= 0x8000 - 4)
6887             as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6888           if (reg_needs_delay (mips_gp_register))
6889             gpdel = 4;
6890           else
6891             gpdel = 0;
6892           if (breg == 0)
6893             off = 0;
6894           else
6895             off = 4;
6896           frag_grow (56);
6897           macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6898                        BFD_RELOC_MIPS_GOT_HI16);
6899           macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6900                        AT, AT, mips_gp_register);
6901           macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6902                        AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6903           macro_build (NULL, &icnt, NULL, "nop", "");
6904           if (breg != 0)
6905             macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6906                          AT, breg, AT);
6907           /* Itbl support may require additional care here.  */
6908           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6909                        BFD_RELOC_LO16, AT);
6910           expr1.X_add_number += 4;
6911
6912           /* Set mips_optimize to 2 to avoid inserting an undesired
6913              nop.  */
6914           hold_mips_optimize = mips_optimize;
6915           mips_optimize = 2;
6916           /* Itbl support may require additional care here.  */
6917           macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6918                        BFD_RELOC_LO16, AT);
6919           mips_optimize = hold_mips_optimize;
6920           expr1.X_add_number -= 4;
6921
6922           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6923                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6924                                       8 + gpdel + off, 1, 0),
6925                         offset_expr.X_add_symbol, 0, NULL);
6926           if (gpdel > 0)
6927             {
6928               macro_build (p, &icnt, NULL, "nop", "");
6929               p += 4;
6930             }
6931           macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6932                        AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6933           p += 4;
6934           macro_build (p, &icnt, NULL, "nop", "");
6935           p += 4;
6936           if (breg != 0)
6937             {
6938               macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6939                            AT, breg, AT);
6940               p += 4;
6941             }
6942           /* Itbl support may require additional care here.  */
6943           macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6944                        BFD_RELOC_LO16, AT);
6945           p += 4;
6946           expr1.X_add_number += 4;
6947
6948           /* Set mips_optimize to 2 to avoid inserting an undesired
6949              nop.  */
6950           hold_mips_optimize = mips_optimize;
6951           mips_optimize = 2;
6952           /* Itbl support may require additional care here.  */
6953           macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6954                        BFD_RELOC_LO16, AT);
6955           mips_optimize = hold_mips_optimize;
6956         }
6957       else if (mips_pic == EMBEDDED_PIC)
6958         {
6959           /* If there is no base register, we use
6960                <op>     $treg,<sym>($gp)        (BFD_RELOC_GPREL16)
6961                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_GPREL16)
6962              If we have a base register, we use
6963                addu     $at,$breg,$gp
6964                <op>     $treg,<sym>($at)        (BFD_RELOC_GPREL16)
6965                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_GPREL16)
6966              */
6967           if (breg == 0)
6968             {
6969               tempreg = mips_gp_register;
6970               used_at = 0;
6971             }
6972           else
6973             {
6974               macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6975                            AT, breg, mips_gp_register);
6976               tempreg = AT;
6977               used_at = 1;
6978             }
6979
6980           /* Itbl support may require additional care here.  */
6981           macro_build (NULL, &icnt, &offset_expr, s, fmt,
6982                        coproc ? treg + 1 : treg,
6983                        BFD_RELOC_GPREL16, tempreg);
6984           offset_expr.X_add_number += 4;
6985           /* Itbl support may require additional care here.  */
6986           macro_build (NULL, &icnt, &offset_expr, s, fmt,
6987                        coproc ? treg : treg + 1,
6988                        BFD_RELOC_GPREL16, tempreg);
6989         }
6990       else
6991         abort ();
6992
6993       if (! used_at)
6994         return;
6995
6996       break;
6997
6998     case M_LD_OB:
6999       s = "lw";
7000       goto sd_ob;
7001     case M_SD_OB:
7002       s = "sw";
7003     sd_ob:
7004       assert (HAVE_32BIT_ADDRESSES);
7005       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7006                    BFD_RELOC_LO16, breg);
7007       offset_expr.X_add_number += 4;
7008       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7009                    BFD_RELOC_LO16, breg);
7010       return;
7011
7012    /* New code added to support COPZ instructions.
7013       This code builds table entries out of the macros in mip_opcodes.
7014       R4000 uses interlocks to handle coproc delays.
7015       Other chips (like the R3000) require nops to be inserted for delays.
7016
7017       FIXME: Currently, we require that the user handle delays.
7018       In order to fill delay slots for non-interlocked chips,
7019       we must have a way to specify delays based on the coprocessor.
7020       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7021       What are the side-effects of the cop instruction?
7022       What cache support might we have and what are its effects?
7023       Both coprocessor & memory require delays. how long???
7024       What registers are read/set/modified?
7025
7026       If an itbl is provided to interpret cop instructions,
7027       this knowledge can be encoded in the itbl spec.  */
7028
7029     case M_COP0:
7030       s = "c0";
7031       goto copz;
7032     case M_COP1:
7033       s = "c1";
7034       goto copz;
7035     case M_COP2:
7036       s = "c2";
7037       goto copz;
7038     case M_COP3:
7039       s = "c3";
7040     copz:
7041       /* For now we just do C (same as Cz).  The parameter will be
7042          stored in insn_opcode by mips_ip.  */
7043       macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
7044       return;
7045
7046     case M_MOVE:
7047       move_register (&icnt, dreg, sreg);
7048       return;
7049
7050 #ifdef LOSING_COMPILER
7051     default:
7052       /* Try and see if this is a new itbl instruction.
7053          This code builds table entries out of the macros in mip_opcodes.
7054          FIXME: For now we just assemble the expression and pass it's
7055          value along as a 32-bit immediate.
7056          We may want to have the assembler assemble this value,
7057          so that we gain the assembler's knowledge of delay slots,
7058          symbols, etc.
7059          Would it be more efficient to use mask (id) here? */
7060       if (itbl_have_entries
7061           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7062         {
7063           s = ip->insn_mo->name;
7064           s2 = "cop3";
7065           coproc = ITBL_DECODE_PNUM (immed_expr);;
7066           macro_build (NULL, &icnt, &immed_expr, s, "C");
7067           return;
7068         }
7069       macro2 (ip);
7070       return;
7071     }
7072   if (mips_opts.noat)
7073     as_warn (_("Macro used $at after \".set noat\""));
7074 }
7075
7076 static void
7077 macro2 (struct mips_cl_insn *ip)
7078 {
7079   register int treg, sreg, dreg, breg;
7080   int tempreg;
7081   int mask;
7082   int icnt = 0;
7083   int used_at;
7084   expressionS expr1;
7085   const char *s;
7086   const char *s2;
7087   const char *fmt;
7088   int likely = 0;
7089   int dbl = 0;
7090   int coproc = 0;
7091   int lr = 0;
7092   int imm = 0;
7093   int off;
7094   offsetT maxnum;
7095   bfd_reloc_code_real_type r;
7096   char *p;
7097
7098   treg = (ip->insn_opcode >> 16) & 0x1f;
7099   dreg = (ip->insn_opcode >> 11) & 0x1f;
7100   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7101   mask = ip->insn_mo->mask;
7102
7103   expr1.X_op = O_constant;
7104   expr1.X_op_symbol = NULL;
7105   expr1.X_add_symbol = NULL;
7106   expr1.X_add_number = 1;
7107
7108   switch (mask)
7109     {
7110 #endif /* LOSING_COMPILER */
7111
7112     case M_DMUL:
7113       dbl = 1;
7114     case M_MUL:
7115       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7116                    sreg, treg);
7117       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7118       return;
7119
7120     case M_DMUL_I:
7121       dbl = 1;
7122     case M_MUL_I:
7123       /* The MIPS assembler some times generates shifts and adds.  I'm
7124          not trying to be that fancy. GCC should do this for us
7125          anyway.  */
7126       load_register (&icnt, AT, &imm_expr, dbl);
7127       macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7128                    sreg, AT);
7129       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7130       break;
7131
7132     case M_DMULO_I:
7133       dbl = 1;
7134     case M_MULO_I:
7135       imm = 1;
7136       goto do_mulo;
7137
7138     case M_DMULO:
7139       dbl = 1;
7140     case M_MULO:
7141     do_mulo:
7142       mips_emit_delays (TRUE);
7143       ++mips_opts.noreorder;
7144       mips_any_noreorder = 1;
7145       if (imm)
7146         load_register (&icnt, AT, &imm_expr, dbl);
7147       macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7148                    sreg, imm ? AT : treg);
7149       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7150       macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
7151                    dreg, dreg, RA);
7152       macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7153       if (mips_trap)
7154         macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
7155       else
7156         {
7157           expr1.X_add_number = 8;
7158           macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7159           macro_build (NULL, &icnt, NULL, "nop", "", 0);
7160           macro_build (NULL, &icnt, NULL, "break", "c", 6);
7161         }
7162       --mips_opts.noreorder;
7163       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7164       break;
7165
7166     case M_DMULOU_I:
7167       dbl = 1;
7168     case M_MULOU_I:
7169       imm = 1;
7170       goto do_mulou;
7171
7172     case M_DMULOU:
7173       dbl = 1;
7174     case M_MULOU:
7175     do_mulou:
7176       mips_emit_delays (TRUE);
7177       ++mips_opts.noreorder;
7178       mips_any_noreorder = 1;
7179       if (imm)
7180         load_register (&icnt, AT, &imm_expr, dbl);
7181       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7182                    sreg, imm ? AT : treg);
7183       macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7184       macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7185       if (mips_trap)
7186         macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
7187       else
7188         {
7189           expr1.X_add_number = 8;
7190           macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7191           macro_build (NULL, &icnt, NULL, "nop", "", 0);
7192           macro_build (NULL, &icnt, NULL, "break", "c", 6);
7193         }
7194       --mips_opts.noreorder;
7195       break;
7196
7197     case M_DROL:
7198       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7199         {
7200           if (dreg == sreg)
7201             {
7202               tempreg = AT;
7203               used_at = 1;
7204             }
7205           else
7206             {
7207               tempreg = dreg;
7208               used_at = 0;
7209             }
7210           macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7211           macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7212                        tempreg);
7213           if (used_at)
7214             break;
7215           return;
7216         }
7217       macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7218       macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7219       macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7220       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7221       break;
7222
7223     case M_ROL:
7224       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7225         {
7226           if (dreg == sreg)
7227             {
7228               tempreg = AT;
7229               used_at = 1;
7230             }
7231           else
7232             {
7233               tempreg = dreg;
7234               used_at = 0;
7235             }
7236           macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7237           macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7238                        tempreg);
7239           if (used_at)
7240             break;
7241           return;
7242         }
7243       macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7244       macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7245       macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7246       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7247       break;
7248
7249     case M_DROL_I:
7250       {
7251         unsigned int rot;
7252         char *l, *r;
7253
7254         if (imm_expr.X_op != O_constant)
7255           as_bad (_("Improper rotate count"));
7256         rot = imm_expr.X_add_number & 0x3f;
7257         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7258           {
7259             rot = (64 - rot) & 0x3f;
7260             if (rot >= 32)
7261               macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7262                            dreg, sreg, rot - 32);
7263             else
7264               macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7265                            dreg, sreg, rot);
7266             return;
7267           }
7268         if (rot == 0)
7269           {
7270             macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7271             return;
7272           }
7273         l = (rot < 0x20) ? "dsll" : "dsll32";
7274         r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7275         rot &= 0x1f;
7276         macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7277         macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7278                      (0x20 - rot) & 0x1f);
7279         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7280       }
7281       break;
7282
7283     case M_ROL_I:
7284       {
7285         unsigned int rot;
7286
7287         if (imm_expr.X_op != O_constant)
7288           as_bad (_("Improper rotate count"));
7289         rot = imm_expr.X_add_number & 0x1f;
7290         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7291           {
7292             macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7293                          (32 - rot) & 0x1f);
7294             return;
7295           }
7296         if (rot == 0)
7297           {
7298             macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7299             return;
7300           }
7301         macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7302         macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7303                      (0x20 - rot) & 0x1f);
7304         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7305       }
7306       break;
7307
7308     case M_DROR:
7309       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7310         {
7311           macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
7312           return;
7313         }
7314       macro_build (NULL, &icnt,NULL, "dsubu", "d,v,t", AT, 0, treg);
7315       macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7316       macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7317       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7318       break;
7319
7320     case M_ROR:
7321       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7322         {
7323           macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
7324           return;
7325         }
7326       macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7327       macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7328       macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7329       macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7330       break;
7331
7332     case M_DROR_I:
7333       {
7334         unsigned int rot;
7335         char *l, *r;
7336
7337         if (imm_expr.X_op != O_constant)
7338           as_bad (_("Improper rotate count"));
7339         rot = imm_expr.X_add_number & 0x3f;
7340         if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7341           {
7342             if (rot >= 32)
7343               macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7344                            dreg, sreg, rot - 32);
7345             else
7346               macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7347                            dreg, sreg, rot);
7348             return;
7349           }
7350         if (rot == 0)
7351           {
7352             macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7353             return;
7354           }
7355         r = (rot < 0x20) ? "dsrl" : "dsrl32";
7356         l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7357         rot &= 0x1f;
7358         macro_build ( NULL, &icnt,NULL, r, "d,w,<", AT, sreg, rot);
7359         macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7360                      (0x20 - rot) & 0x1f);
7361         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7362       }
7363       break;
7364
7365     case M_ROR_I:
7366       {
7367         unsigned int rot;
7368
7369         if (imm_expr.X_op != O_constant)
7370           as_bad (_("Improper rotate count"));
7371         rot = imm_expr.X_add_number & 0x1f;
7372         if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7373           {
7374             macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
7375             return;
7376           }
7377         if (rot == 0)
7378           {
7379             macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7380             return;
7381           }
7382         macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7383         macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7384                      (0x20 - rot) & 0x1f);
7385         macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7386       }
7387       break;
7388
7389     case M_S_DOB:
7390       if (mips_opts.arch == CPU_R4650)
7391         {
7392           as_bad (_("opcode not supported on this processor"));
7393           return;
7394         }
7395       assert (mips_opts.isa == ISA_MIPS1);
7396       /* Even on a big endian machine $fn comes before $fn+1.  We have
7397          to adjust when storing to memory.  */
7398       macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7399                    target_big_endian ? treg + 1 : treg,
7400                    BFD_RELOC_LO16, breg);
7401       offset_expr.X_add_number += 4;
7402       macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7403                    target_big_endian ? treg : treg + 1,
7404                    BFD_RELOC_LO16, breg);
7405       return;
7406
7407     case M_SEQ:
7408       if (sreg == 0)
7409         macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7410                      BFD_RELOC_LO16);
7411       else if (treg == 0)
7412         macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7413                      BFD_RELOC_LO16);
7414       else
7415         {
7416           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7417           macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7418                        BFD_RELOC_LO16);
7419         }
7420       return;
7421
7422     case M_SEQ_I:
7423       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7424         {
7425           macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7426                        BFD_RELOC_LO16);
7427           return;
7428         }
7429       if (sreg == 0)
7430         {
7431           as_warn (_("Instruction %s: result is always false"),
7432                    ip->insn_mo->name);
7433           move_register (&icnt, dreg, 0);
7434           return;
7435         }
7436       if (imm_expr.X_op == O_constant
7437           && imm_expr.X_add_number >= 0
7438           && imm_expr.X_add_number < 0x10000)
7439         {
7440           macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7441                        BFD_RELOC_LO16);
7442           used_at = 0;
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 (NULL, &icnt, &imm_expr,
7450                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7451                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7452           used_at = 0;
7453         }
7454       else
7455         {
7456           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7457           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7458           used_at = 1;
7459         }
7460       macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7461                    BFD_RELOC_LO16);
7462       if (used_at)
7463         break;
7464       return;
7465
7466     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
7467       s = "slt";
7468       goto sge;
7469     case M_SGEU:
7470       s = "sltu";
7471     sge:
7472       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7473       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7474                    BFD_RELOC_LO16);
7475       return;
7476
7477     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
7478     case M_SGEU_I:
7479       if (imm_expr.X_op == O_constant
7480           && imm_expr.X_add_number >= -0x8000
7481           && imm_expr.X_add_number < 0x8000)
7482         {
7483           macro_build (NULL, &icnt, &imm_expr,
7484                        mask == M_SGE_I ? "slti" : "sltiu",
7485                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7486           used_at = 0;
7487         }
7488       else
7489         {
7490           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7491           macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7492                        "d,v,t", dreg, sreg, AT);
7493           used_at = 1;
7494         }
7495       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7496                    BFD_RELOC_LO16);
7497       if (used_at)
7498         break;
7499       return;
7500
7501     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
7502       s = "slt";
7503       goto sgt;
7504     case M_SGTU:
7505       s = "sltu";
7506     sgt:
7507       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7508       return;
7509
7510     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
7511       s = "slt";
7512       goto sgti;
7513     case M_SGTU_I:
7514       s = "sltu";
7515     sgti:
7516       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7517       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7518       break;
7519
7520     case M_SLE: /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7521       s = "slt";
7522       goto sle;
7523     case M_SLEU:
7524       s = "sltu";
7525     sle:
7526       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7527       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7528                    BFD_RELOC_LO16);
7529       return;
7530
7531     case M_SLE_I:       /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7532       s = "slt";
7533       goto slei;
7534     case M_SLEU_I:
7535       s = "sltu";
7536     slei:
7537       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7538       macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7539       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7540                    BFD_RELOC_LO16);
7541       break;
7542
7543     case M_SLT_I:
7544       if (imm_expr.X_op == O_constant
7545           && imm_expr.X_add_number >= -0x8000
7546           && imm_expr.X_add_number < 0x8000)
7547         {
7548           macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7549                        BFD_RELOC_LO16);
7550           return;
7551         }
7552       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7553       macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
7554       break;
7555
7556     case M_SLTU_I:
7557       if (imm_expr.X_op == O_constant
7558           && imm_expr.X_add_number >= -0x8000
7559           && imm_expr.X_add_number < 0x8000)
7560         {
7561           macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7562                        BFD_RELOC_LO16);
7563           return;
7564         }
7565       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7566       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
7567       break;
7568
7569     case M_SNE:
7570       if (sreg == 0)
7571         macro_build (NULL, &icnt,NULL, "sltu","d,v,t", dreg, 0, treg);
7572       else if (treg == 0)
7573         macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7574       else
7575         {
7576           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7577           macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7578         }
7579       return;
7580
7581     case M_SNE_I:
7582       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7583         {
7584           macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7585           return;
7586         }
7587       if (sreg == 0)
7588         {
7589           as_warn (_("Instruction %s: result is always true"),
7590                    ip->insn_mo->name);
7591           macro_build (NULL, &icnt, &expr1,
7592                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7593                        "t,r,j", dreg, 0, BFD_RELOC_LO16);
7594           return;
7595         }
7596       if (imm_expr.X_op == O_constant
7597           && imm_expr.X_add_number >= 0
7598           && imm_expr.X_add_number < 0x10000)
7599         {
7600           macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7601                        BFD_RELOC_LO16);
7602           used_at = 0;
7603         }
7604       else if (imm_expr.X_op == O_constant
7605                && imm_expr.X_add_number > -0x8000
7606                && imm_expr.X_add_number < 0)
7607         {
7608           imm_expr.X_add_number = -imm_expr.X_add_number;
7609           macro_build (NULL, &icnt, &imm_expr,
7610                        HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7611                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7612           used_at = 0;
7613         }
7614       else
7615         {
7616           load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7617           macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7618           used_at = 1;
7619         }
7620       macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7621       if (used_at)
7622         break;
7623       return;
7624
7625     case M_DSUB_I:
7626       dbl = 1;
7627     case M_SUB_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 (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7634                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7635           return;
7636         }
7637       load_register (&icnt, AT, &imm_expr, dbl);
7638       macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7639                    dreg, sreg, AT);
7640       break;
7641
7642     case M_DSUBU_I:
7643       dbl = 1;
7644     case M_SUBU_I:
7645       if (imm_expr.X_op == O_constant
7646           && imm_expr.X_add_number > -0x8000
7647           && imm_expr.X_add_number <= 0x8000)
7648         {
7649           imm_expr.X_add_number = -imm_expr.X_add_number;
7650           macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7651                        "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7652           return;
7653         }
7654       load_register (&icnt, AT, &imm_expr, dbl);
7655       macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7656                    dreg, sreg, AT);
7657       break;
7658
7659     case M_TEQ_I:
7660       s = "teq";
7661       goto trap;
7662     case M_TGE_I:
7663       s = "tge";
7664       goto trap;
7665     case M_TGEU_I:
7666       s = "tgeu";
7667       goto trap;
7668     case M_TLT_I:
7669       s = "tlt";
7670       goto trap;
7671     case M_TLTU_I:
7672       s = "tltu";
7673       goto trap;
7674     case M_TNE_I:
7675       s = "tne";
7676     trap:
7677       load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7678       macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
7679       break;
7680
7681     case M_TRUNCWS:
7682     case M_TRUNCWD:
7683       assert (mips_opts.isa == ISA_MIPS1);
7684       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
7685       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
7686
7687       /*
7688        * Is the double cfc1 instruction a bug in the mips assembler;
7689        * or is there a reason for it?
7690        */
7691       mips_emit_delays (TRUE);
7692       ++mips_opts.noreorder;
7693       mips_any_noreorder = 1;
7694       macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7695       macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7696       macro_build (NULL, &icnt, NULL, "nop", "");
7697       expr1.X_add_number = 3;
7698       macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7699                    BFD_RELOC_LO16);
7700       expr1.X_add_number = 2;
7701       macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7702                    BFD_RELOC_LO16);
7703       macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7704       macro_build (NULL, &icnt, NULL, "nop", "");
7705       macro_build (NULL, &icnt, NULL,
7706                    mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7707                    "D,S", dreg, sreg);
7708       macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7709       macro_build (NULL, &icnt, NULL, "nop", "");
7710       --mips_opts.noreorder;
7711       break;
7712
7713     case M_ULH:
7714       s = "lb";
7715       goto ulh;
7716     case M_ULHU:
7717       s = "lbu";
7718     ulh:
7719       if (offset_expr.X_add_number >= 0x7fff)
7720         as_bad (_("operand overflow"));
7721       if (! target_big_endian)
7722         ++offset_expr.X_add_number;
7723       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7724                    BFD_RELOC_LO16, breg);
7725       if (! target_big_endian)
7726         --offset_expr.X_add_number;
7727       else
7728         ++offset_expr.X_add_number;
7729       macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7730                    BFD_RELOC_LO16, breg);
7731       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7732       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7733       break;
7734
7735     case M_ULD:
7736       s = "ldl";
7737       s2 = "ldr";
7738       off = 7;
7739       goto ulw;
7740     case M_ULW:
7741       s = "lwl";
7742       s2 = "lwr";
7743       off = 3;
7744     ulw:
7745       if (offset_expr.X_add_number >= 0x8000 - off)
7746         as_bad (_("operand overflow"));
7747       if (treg != breg)
7748         tempreg = treg;
7749       else
7750         tempreg = AT;
7751       if (! target_big_endian)
7752         offset_expr.X_add_number += off;
7753       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7754                    BFD_RELOC_LO16, breg);
7755       if (! target_big_endian)
7756         offset_expr.X_add_number -= off;
7757       else
7758         offset_expr.X_add_number += off;
7759       macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7760                    BFD_RELOC_LO16, breg);
7761
7762       /* If necessary, move the result in tempreg the final destination.  */
7763       if (treg == tempreg)
7764         return;
7765       /* Protect second load's delay slot.  */
7766       if (!gpr_interlocks)
7767         macro_build (NULL, &icnt, NULL, "nop", "");
7768       move_register (&icnt, treg, tempreg);
7769       break;
7770
7771     case M_ULD_A:
7772       s = "ldl";
7773       s2 = "ldr";
7774       off = 7;
7775       goto ulwa;
7776     case M_ULW_A:
7777       s = "lwl";
7778       s2 = "lwr";
7779       off = 3;
7780     ulwa:
7781       used_at = 1;
7782       load_address (&icnt, AT, &offset_expr, &used_at);
7783       if (breg != 0)
7784         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7785                      AT, AT, breg);
7786       if (! target_big_endian)
7787         expr1.X_add_number = off;
7788       else
7789         expr1.X_add_number = 0;
7790       macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7791                    BFD_RELOC_LO16, AT);
7792       if (! target_big_endian)
7793         expr1.X_add_number = 0;
7794       else
7795         expr1.X_add_number = off;
7796       macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7797                    BFD_RELOC_LO16, AT);
7798       break;
7799
7800     case M_ULH_A:
7801     case M_ULHU_A:
7802       used_at = 1;
7803       load_address (&icnt, AT, &offset_expr, &used_at);
7804       if (breg != 0)
7805         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7806                      AT, AT, breg);
7807       if (target_big_endian)
7808         expr1.X_add_number = 0;
7809       macro_build (NULL, &icnt, &expr1,
7810                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7811                    treg, BFD_RELOC_LO16, AT);
7812       if (target_big_endian)
7813         expr1.X_add_number = 1;
7814       else
7815         expr1.X_add_number = 0;
7816       macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7817                    AT, BFD_RELOC_LO16, AT);
7818       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7819       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7820       break;
7821
7822     case M_USH:
7823       if (offset_expr.X_add_number >= 0x7fff)
7824         as_bad (_("operand overflow"));
7825       if (target_big_endian)
7826         ++offset_expr.X_add_number;
7827       macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7828                    BFD_RELOC_LO16, breg);
7829       macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
7830       if (target_big_endian)
7831         --offset_expr.X_add_number;
7832       else
7833         ++offset_expr.X_add_number;
7834       macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7835                    BFD_RELOC_LO16, breg);
7836       break;
7837
7838     case M_USD:
7839       s = "sdl";
7840       s2 = "sdr";
7841       off = 7;
7842       goto usw;
7843     case M_USW:
7844       s = "swl";
7845       s2 = "swr";
7846       off = 3;
7847     usw:
7848       if (offset_expr.X_add_number >= 0x8000 - off)
7849         as_bad (_("operand overflow"));
7850       if (! target_big_endian)
7851         offset_expr.X_add_number += off;
7852       macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7853                    BFD_RELOC_LO16, breg);
7854       if (! target_big_endian)
7855         offset_expr.X_add_number -= off;
7856       else
7857         offset_expr.X_add_number += off;
7858       macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7859                    BFD_RELOC_LO16, breg);
7860       return;
7861
7862     case M_USD_A:
7863       s = "sdl";
7864       s2 = "sdr";
7865       off = 7;
7866       goto uswa;
7867     case M_USW_A:
7868       s = "swl";
7869       s2 = "swr";
7870       off = 3;
7871     uswa:
7872       used_at = 1;
7873       load_address (&icnt, AT, &offset_expr, &used_at);
7874       if (breg != 0)
7875         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7876                      AT, AT, breg);
7877       if (! target_big_endian)
7878         expr1.X_add_number = off;
7879       else
7880         expr1.X_add_number = 0;
7881       macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7882                    BFD_RELOC_LO16, AT);
7883       if (! target_big_endian)
7884         expr1.X_add_number = 0;
7885       else
7886         expr1.X_add_number = off;
7887       macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7888                    BFD_RELOC_LO16, AT);
7889       break;
7890
7891     case M_USH_A:
7892       used_at = 1;
7893       load_address (&icnt, AT, &offset_expr, &used_at);
7894       if (breg != 0)
7895         macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7896                      AT, AT, breg);
7897       if (! target_big_endian)
7898         expr1.X_add_number = 0;
7899       macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7900                    BFD_RELOC_LO16, AT);
7901       macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
7902       if (! target_big_endian)
7903         expr1.X_add_number = 1;
7904       else
7905         expr1.X_add_number = 0;
7906       macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7907                    BFD_RELOC_LO16, AT);
7908       if (! target_big_endian)
7909         expr1.X_add_number = 0;
7910       else
7911         expr1.X_add_number = 1;
7912       macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7913                    BFD_RELOC_LO16, AT);
7914       macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7915       macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7916       break;
7917
7918     default:
7919       /* FIXME: Check if this is one of the itbl macros, since they
7920          are added dynamically.  */
7921       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7922       break;
7923     }
7924   if (mips_opts.noat)
7925     as_warn (_("Macro used $at after \".set noat\""));
7926 }
7927
7928 /* Implement macros in mips16 mode.  */
7929
7930 static void
7931 mips16_macro (struct mips_cl_insn *ip)
7932 {
7933   int mask;
7934   int xreg, yreg, zreg, tmp;
7935   int icnt;
7936   expressionS expr1;
7937   int dbl;
7938   const char *s, *s2, *s3;
7939
7940   mask = ip->insn_mo->mask;
7941
7942   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7943   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7944   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7945
7946   icnt = 0;
7947
7948   expr1.X_op = O_constant;
7949   expr1.X_op_symbol = NULL;
7950   expr1.X_add_symbol = NULL;
7951   expr1.X_add_number = 1;
7952
7953   dbl = 0;
7954
7955   switch (mask)
7956     {
7957     default:
7958       internalError ();
7959
7960     case M_DDIV_3:
7961       dbl = 1;
7962     case M_DIV_3:
7963       s = "mflo";
7964       goto do_div3;
7965     case M_DREM_3:
7966       dbl = 1;
7967     case M_REM_3:
7968       s = "mfhi";
7969     do_div3:
7970       mips_emit_delays (TRUE);
7971       ++mips_opts.noreorder;
7972       mips_any_noreorder = 1;
7973       macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7974                    xreg, yreg);
7975       expr1.X_add_number = 2;
7976       macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7977       macro_build (NULL, &icnt, NULL, "break", "6", 7);
7978
7979       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7980          since that causes an overflow.  We should do that as well,
7981          but I don't see how to do the comparisons without a temporary
7982          register.  */
7983       --mips_opts.noreorder;
7984       macro_build (NULL, &icnt, NULL, s, "x", zreg);
7985       break;
7986
7987     case M_DIVU_3:
7988       s = "divu";
7989       s2 = "mflo";
7990       goto do_divu3;
7991     case M_REMU_3:
7992       s = "divu";
7993       s2 = "mfhi";
7994       goto do_divu3;
7995     case M_DDIVU_3:
7996       s = "ddivu";
7997       s2 = "mflo";
7998       goto do_divu3;
7999     case M_DREMU_3:
8000       s = "ddivu";
8001       s2 = "mfhi";
8002     do_divu3:
8003       mips_emit_delays (TRUE);
8004       ++mips_opts.noreorder;
8005       mips_any_noreorder = 1;
8006       macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
8007       expr1.X_add_number = 2;
8008       macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8009       macro_build (NULL, &icnt, NULL, "break", "6", 7);
8010       --mips_opts.noreorder;
8011       macro_build (NULL, &icnt, NULL, s2, "x", zreg);
8012       break;
8013
8014     case M_DMUL:
8015       dbl = 1;
8016     case M_MUL:
8017       macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
8018                    xreg, yreg);
8019       macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
8020       return;
8021
8022     case M_DSUBU_I:
8023       dbl = 1;
8024       goto do_subu;
8025     case M_SUBU_I:
8026     do_subu:
8027       if (imm_expr.X_op != O_constant)
8028         as_bad (_("Unsupported large constant"));
8029       imm_expr.X_add_number = -imm_expr.X_add_number;
8030       macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
8031                    yreg, xreg);
8032       break;
8033
8034     case M_SUBU_I_2:
8035       if (imm_expr.X_op != O_constant)
8036         as_bad (_("Unsupported large constant"));
8037       imm_expr.X_add_number = -imm_expr.X_add_number;
8038       macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
8039       break;
8040
8041     case M_DSUBU_I_2:
8042       if (imm_expr.X_op != O_constant)
8043         as_bad (_("Unsupported large constant"));
8044       imm_expr.X_add_number = -imm_expr.X_add_number;
8045       macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
8046       break;
8047
8048     case M_BEQ:
8049       s = "cmp";
8050       s2 = "bteqz";
8051       goto do_branch;
8052     case M_BNE:
8053       s = "cmp";
8054       s2 = "btnez";
8055       goto do_branch;
8056     case M_BLT:
8057       s = "slt";
8058       s2 = "btnez";
8059       goto do_branch;
8060     case M_BLTU:
8061       s = "sltu";
8062       s2 = "btnez";
8063       goto do_branch;
8064     case M_BLE:
8065       s = "slt";
8066       s2 = "bteqz";
8067       goto do_reverse_branch;
8068     case M_BLEU:
8069       s = "sltu";
8070       s2 = "bteqz";
8071       goto do_reverse_branch;
8072     case M_BGE:
8073       s = "slt";
8074       s2 = "bteqz";
8075       goto do_branch;
8076     case M_BGEU:
8077       s = "sltu";
8078       s2 = "bteqz";
8079       goto do_branch;
8080     case M_BGT:
8081       s = "slt";
8082       s2 = "btnez";
8083       goto do_reverse_branch;
8084     case M_BGTU:
8085       s = "sltu";
8086       s2 = "btnez";
8087
8088     do_reverse_branch:
8089       tmp = xreg;
8090       xreg = yreg;
8091       yreg = tmp;
8092
8093     do_branch:
8094       macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
8095       macro_build (NULL, &icnt, &offset_expr, s2, "p");
8096       break;
8097
8098     case M_BEQ_I:
8099       s = "cmpi";
8100       s2 = "bteqz";
8101       s3 = "x,U";
8102       goto do_branch_i;
8103     case M_BNE_I:
8104       s = "cmpi";
8105       s2 = "btnez";
8106       s3 = "x,U";
8107       goto do_branch_i;
8108     case M_BLT_I:
8109       s = "slti";
8110       s2 = "btnez";
8111       s3 = "x,8";
8112       goto do_branch_i;
8113     case M_BLTU_I:
8114       s = "sltiu";
8115       s2 = "btnez";
8116       s3 = "x,8";
8117       goto do_branch_i;
8118     case M_BLE_I:
8119       s = "slti";
8120       s2 = "btnez";
8121       s3 = "x,8";
8122       goto do_addone_branch_i;
8123     case M_BLEU_I:
8124       s = "sltiu";
8125       s2 = "btnez";
8126       s3 = "x,8";
8127       goto do_addone_branch_i;
8128     case M_BGE_I:
8129       s = "slti";
8130       s2 = "bteqz";
8131       s3 = "x,8";
8132       goto do_branch_i;
8133     case M_BGEU_I:
8134       s = "sltiu";
8135       s2 = "bteqz";
8136       s3 = "x,8";
8137       goto do_branch_i;
8138     case M_BGT_I:
8139       s = "slti";
8140       s2 = "bteqz";
8141       s3 = "x,8";
8142       goto do_addone_branch_i;
8143     case M_BGTU_I:
8144       s = "sltiu";
8145       s2 = "bteqz";
8146       s3 = "x,8";
8147
8148     do_addone_branch_i:
8149       if (imm_expr.X_op != O_constant)
8150         as_bad (_("Unsupported large constant"));
8151       ++imm_expr.X_add_number;
8152
8153     do_branch_i:
8154       macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8155       macro_build (NULL, &icnt, &offset_expr, s2, "p");
8156       break;
8157
8158     case M_ABS:
8159       expr1.X_add_number = 0;
8160       macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
8161       if (xreg != yreg)
8162         move_register (&icnt, xreg, yreg);
8163       expr1.X_add_number = 2;
8164       macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8165       macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
8166     }
8167 }
8168
8169 /* For consistency checking, verify that all bits are specified either
8170    by the match/mask part of the instruction definition, or by the
8171    operand list.  */
8172 static int
8173 validate_mips_insn (const struct mips_opcode *opc)
8174 {
8175   const char *p = opc->args;
8176   char c;
8177   unsigned long used_bits = opc->mask;
8178
8179   if ((used_bits & opc->match) != opc->match)
8180     {
8181       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8182               opc->name, opc->args);
8183       return 0;
8184     }
8185 #define USE_BITS(mask,shift)    (used_bits |= ((mask) << (shift)))
8186   while (*p)
8187     switch (c = *p++)
8188       {
8189       case ',': break;
8190       case '(': break;
8191       case ')': break;
8192       case '+':
8193         switch (c = *p++)
8194           {
8195           case 'A': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8196           case 'B': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8197           case 'C': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8198           case 'D': USE_BITS (OP_MASK_RD,       OP_SH_RD);
8199                     USE_BITS (OP_MASK_SEL,      OP_SH_SEL);     break;
8200           case 'E': USE_BITS (OP_MASK_SHAMT,    OP_SH_SHAMT);   break;
8201           case 'F': USE_BITS (OP_MASK_INSMSB,   OP_SH_INSMSB);  break;
8202           case 'G': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8203           case 'H': USE_BITS (OP_MASK_EXTMSBD,  OP_SH_EXTMSBD); break;
8204           case 'I': break;
8205           default:
8206             as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8207                     c, opc->name, opc->args);
8208             return 0;
8209           }
8210         break;
8211       case '<': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8212       case '>': USE_BITS (OP_MASK_SHAMT,        OP_SH_SHAMT);   break;
8213       case 'A': break;
8214       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
8215       case 'C': USE_BITS (OP_MASK_COPZ,         OP_SH_COPZ);    break;
8216       case 'D': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8217       case 'E': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8218       case 'F': break;
8219       case 'G': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8220       case 'H': USE_BITS (OP_MASK_SEL,          OP_SH_SEL);     break;
8221       case 'I': break;
8222       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
8223       case 'K': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8224       case 'L': break;
8225       case 'M': USE_BITS (OP_MASK_CCC,          OP_SH_CCC);     break;
8226       case 'N': USE_BITS (OP_MASK_BCC,          OP_SH_BCC);     break;
8227       case 'O': USE_BITS (OP_MASK_ALN,          OP_SH_ALN);     break;
8228       case 'Q': USE_BITS (OP_MASK_VSEL,         OP_SH_VSEL);
8229                 USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8230       case 'R': USE_BITS (OP_MASK_FR,           OP_SH_FR);      break;
8231       case 'S': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8232       case 'T': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8233       case 'V': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8234       case 'W': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8235       case 'X': USE_BITS (OP_MASK_FD,           OP_SH_FD);      break;
8236       case 'Y': USE_BITS (OP_MASK_FS,           OP_SH_FS);      break;
8237       case 'Z': USE_BITS (OP_MASK_FT,           OP_SH_FT);      break;
8238       case 'a': USE_BITS (OP_MASK_TARGET,       OP_SH_TARGET);  break;
8239       case 'b': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8240       case 'c': USE_BITS (OP_MASK_CODE,         OP_SH_CODE);    break;
8241       case 'd': USE_BITS (OP_MASK_RD,           OP_SH_RD);      break;
8242       case 'f': break;
8243       case 'h': USE_BITS (OP_MASK_PREFX,        OP_SH_PREFX);   break;
8244       case 'i': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8245       case 'j': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8246       case 'k': USE_BITS (OP_MASK_CACHE,        OP_SH_CACHE);   break;
8247       case 'l': break;
8248       case 'o': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8249       case 'p': USE_BITS (OP_MASK_DELTA,        OP_SH_DELTA);   break;
8250       case 'q': USE_BITS (OP_MASK_CODE2,        OP_SH_CODE2);   break;
8251       case 'r': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8252       case 's': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8253       case 't': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8254       case 'u': USE_BITS (OP_MASK_IMMEDIATE,    OP_SH_IMMEDIATE); break;
8255       case 'v': USE_BITS (OP_MASK_RS,           OP_SH_RS);      break;
8256       case 'w': USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8257       case 'x': break;
8258       case 'z': break;
8259       case 'P': USE_BITS (OP_MASK_PERFREG,      OP_SH_PERFREG); break;
8260       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
8261                 USE_BITS (OP_MASK_RT,           OP_SH_RT);      break;
8262       case 'e': USE_BITS (OP_MASK_VECBYTE,      OP_SH_VECBYTE); break;
8263       case '%': USE_BITS (OP_MASK_VECALIGN,     OP_SH_VECALIGN); break;
8264       case '[': break;
8265       case ']': break;
8266       default:
8267         as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8268                 c, opc->name, opc->args);
8269         return 0;
8270       }
8271 #undef USE_BITS
8272   if (used_bits != 0xffffffff)
8273     {
8274       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8275               ~used_bits & 0xffffffff, opc->name, opc->args);
8276       return 0;
8277     }
8278   return 1;
8279 }
8280
8281 /* This routine assembles an instruction into its binary format.  As a
8282    side effect, it sets one of the global variables imm_reloc or
8283    offset_reloc to the type of relocation to do if one of the operands
8284    is an address expression.  */
8285
8286 static void
8287 mips_ip (char *str, struct mips_cl_insn *ip)
8288 {
8289   char *s;
8290   const char *args;
8291   char c = 0;
8292   struct mips_opcode *insn;
8293   char *argsStart;
8294   unsigned int regno;
8295   unsigned int lastregno = 0;
8296   unsigned int lastpos = 0;
8297   unsigned int limlo, limhi;
8298   char *s_reset;
8299   char save_c = 0;
8300
8301   insn_error = NULL;
8302
8303   /* If the instruction contains a '.', we first try to match an instruction
8304      including the '.'.  Then we try again without the '.'.  */
8305   insn = NULL;
8306   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8307     continue;
8308
8309   /* If we stopped on whitespace, then replace the whitespace with null for
8310      the call to hash_find.  Save the character we replaced just in case we
8311      have to re-parse the instruction.  */
8312   if (ISSPACE (*s))
8313     {
8314       save_c = *s;
8315       *s++ = '\0';
8316     }
8317
8318   insn = (struct mips_opcode *) hash_find (op_hash, str);
8319
8320   /* If we didn't find the instruction in the opcode table, try again, but
8321      this time with just the instruction up to, but not including the
8322      first '.'.  */
8323   if (insn == NULL)
8324     {
8325       /* Restore the character we overwrite above (if any).  */
8326       if (save_c)
8327         *(--s) = save_c;
8328
8329       /* Scan up to the first '.' or whitespace.  */
8330       for (s = str;
8331            *s != '\0' && *s != '.' && !ISSPACE (*s);
8332            ++s)
8333         continue;
8334
8335       /* If we did not find a '.', then we can quit now.  */
8336       if (*s != '.')
8337         {
8338           insn_error = "unrecognized opcode";
8339           return;
8340         }
8341
8342       /* Lookup the instruction in the hash table.  */
8343       *s++ = '\0';
8344       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8345         {
8346           insn_error = "unrecognized opcode";
8347           return;
8348         }
8349     }
8350
8351   argsStart = s;
8352   for (;;)
8353     {
8354       bfd_boolean ok;
8355
8356       assert (strcmp (insn->name, str) == 0);
8357
8358       if (OPCODE_IS_MEMBER (insn,
8359                             (mips_opts.isa
8360                              | (file_ase_mips16 ? INSN_MIPS16 : 0)
8361                              | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8362                              | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8363                             mips_opts.arch))
8364         ok = TRUE;
8365       else
8366         ok = FALSE;
8367
8368       if (insn->pinfo != INSN_MACRO)
8369         {
8370           if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8371             ok = FALSE;
8372         }
8373
8374       if (! ok)
8375         {
8376           if (insn + 1 < &mips_opcodes[NUMOPCODES]
8377               && strcmp (insn->name, insn[1].name) == 0)
8378             {
8379               ++insn;
8380               continue;
8381             }
8382           else
8383             {
8384               if (!insn_error)
8385                 {
8386                   static char buf[100];
8387                   sprintf (buf,
8388                            _("opcode not supported on this processor: %s (%s)"),
8389                            mips_cpu_info_from_arch (mips_opts.arch)->name,
8390                            mips_cpu_info_from_isa (mips_opts.isa)->name);
8391                   insn_error = buf;
8392                 }
8393               if (save_c)
8394                 *(--s) = save_c;
8395               return;
8396             }
8397         }
8398
8399       ip->insn_mo = insn;
8400       ip->insn_opcode = insn->match;
8401       insn_error = NULL;
8402       for (args = insn->args;; ++args)
8403         {
8404           int is_mdmx;
8405
8406           s += strspn (s, " \t");
8407           is_mdmx = 0;
8408           switch (*args)
8409             {
8410             case '\0':          /* end of args */
8411               if (*s == '\0')
8412                 return;
8413               break;
8414
8415             case ',':
8416               if (*s++ == *args)
8417                 continue;
8418               s--;
8419               switch (*++args)
8420                 {
8421                 case 'r':
8422                 case 'v':
8423                   ip->insn_opcode |= lastregno << OP_SH_RS;
8424                   continue;
8425
8426                 case 'w':
8427                   ip->insn_opcode |= lastregno << OP_SH_RT;
8428                   continue;
8429
8430                 case 'W':
8431                   ip->insn_opcode |= lastregno << OP_SH_FT;
8432                   continue;
8433
8434                 case 'V':
8435                   ip->insn_opcode |= lastregno << OP_SH_FS;
8436                   continue;
8437                 }
8438               break;
8439
8440             case '(':
8441               /* Handle optional base register.
8442                  Either the base register is omitted or
8443                  we must have a left paren.  */
8444               /* This is dependent on the next operand specifier
8445                  is a base register specification.  */
8446               assert (args[1] == 'b' || args[1] == '5'
8447                       || args[1] == '-' || args[1] == '4');
8448               if (*s == '\0')
8449                 return;
8450
8451             case ')':           /* these must match exactly */
8452             case '[':
8453             case ']':
8454               if (*s++ == *args)
8455                 continue;
8456               break;
8457
8458             case '+':           /* Opcode extension character.  */
8459               switch (*++args)
8460                 {
8461                 case 'A':               /* ins/ext position, becomes LSB.  */
8462                   limlo = 0;
8463                   limhi = 31;
8464                   goto do_lsb;
8465                 case 'E':
8466                   limlo = 32;
8467                   limhi = 63;
8468                   goto do_lsb;
8469 do_lsb:
8470                   my_getExpression (&imm_expr, s);
8471                   check_absolute_expr (ip, &imm_expr);
8472                   if ((unsigned long) imm_expr.X_add_number < limlo
8473                       || (unsigned long) imm_expr.X_add_number > limhi)
8474                     {
8475                       as_bad (_("Improper position (%lu)"),
8476                               (unsigned long) imm_expr.X_add_number);
8477                       imm_expr.X_add_number = limlo;
8478                     }
8479                   lastpos = imm_expr.X_add_number;
8480                   ip->insn_opcode |= (imm_expr.X_add_number
8481                                       & OP_MASK_SHAMT) << OP_SH_SHAMT;
8482                   imm_expr.X_op = O_absent;
8483                   s = expr_end;
8484                   continue;
8485
8486                 case 'B':               /* ins size, becomes MSB.  */
8487                   limlo = 1;
8488                   limhi = 32;
8489                   goto do_msb;
8490                 case 'F':
8491                   limlo = 33;
8492                   limhi = 64;
8493                   goto do_msb;
8494 do_msb:
8495                   my_getExpression (&imm_expr, s);
8496                   check_absolute_expr (ip, &imm_expr);
8497                   /* Check for negative input so that small negative numbers
8498                      will not succeed incorrectly.  The checks against
8499                      (pos+size) transitively check "size" itself,
8500                      assuming that "pos" is reasonable.  */
8501                   if ((long) imm_expr.X_add_number < 0
8502                       || ((unsigned long) imm_expr.X_add_number
8503                           + lastpos) < limlo
8504                       || ((unsigned long) imm_expr.X_add_number
8505                           + lastpos) > limhi)
8506                     {
8507                       as_bad (_("Improper insert size (%lu, position %lu)"),
8508                               (unsigned long) imm_expr.X_add_number,
8509                               (unsigned long) lastpos);
8510                       imm_expr.X_add_number = limlo - lastpos;
8511                     }
8512                   ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8513                                       & OP_MASK_INSMSB) << OP_SH_INSMSB;
8514                   imm_expr.X_op = O_absent;
8515                   s = expr_end;
8516                   continue;
8517
8518                 case 'C':               /* ext size, becomes MSBD.  */
8519                   limlo = 1;
8520                   limhi = 32;
8521                   goto do_msbd;
8522                 case 'G':
8523                   limlo = 33;
8524                   limhi = 64;
8525                   goto do_msbd;
8526                 case 'H':
8527                   limlo = 33;
8528                   limhi = 64;
8529                   goto do_msbd;
8530 do_msbd:
8531                   my_getExpression (&imm_expr, s);
8532                   check_absolute_expr (ip, &imm_expr);
8533                   /* Check for negative input so that small negative numbers
8534                      will not succeed incorrectly.  The checks against
8535                      (pos+size) transitively check "size" itself,
8536                      assuming that "pos" is reasonable.  */
8537                   if ((long) imm_expr.X_add_number < 0
8538                       || ((unsigned long) imm_expr.X_add_number
8539                           + lastpos) < limlo
8540                       || ((unsigned long) imm_expr.X_add_number
8541                           + lastpos) > limhi)
8542                     {
8543                       as_bad (_("Improper extract size (%lu, position %lu)"),
8544                               (unsigned long) imm_expr.X_add_number,
8545                               (unsigned long) lastpos);
8546                       imm_expr.X_add_number = limlo - lastpos;
8547                     }
8548                   ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8549                                       & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8550                   imm_expr.X_op = O_absent;
8551                   s = expr_end;
8552                   continue;
8553
8554                 case 'D':
8555                   /* +D is for disassembly only; never match.  */
8556                   break;
8557
8558                 case 'I':
8559                   /* "+I" is like "I", except that imm2_expr is used.  */
8560                   my_getExpression (&imm2_expr, s);
8561                   if (imm2_expr.X_op != O_big
8562                       && imm2_expr.X_op != O_constant)
8563                   insn_error = _("absolute expression required");
8564                   s = expr_end;
8565                   continue;
8566
8567                 default:
8568                   as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8569                     *args, insn->name, insn->args);
8570                   /* Further processing is fruitless.  */
8571                   return;
8572                 }
8573               break;
8574
8575             case '<':           /* must be at least one digit */
8576               /*
8577                * According to the manual, if the shift amount is greater
8578                * than 31 or less than 0, then the shift amount should be
8579                * mod 32.  In reality the mips assembler issues an error.
8580                * We issue a warning and mask out all but the low 5 bits.
8581                */
8582               my_getExpression (&imm_expr, s);
8583               check_absolute_expr (ip, &imm_expr);
8584               if ((unsigned long) imm_expr.X_add_number > 31)
8585                 {
8586                   as_warn (_("Improper shift amount (%lu)"),
8587                            (unsigned long) imm_expr.X_add_number);
8588                   imm_expr.X_add_number &= OP_MASK_SHAMT;
8589                 }
8590               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8591               imm_expr.X_op = O_absent;
8592               s = expr_end;
8593               continue;
8594
8595             case '>':           /* shift amount minus 32 */
8596               my_getExpression (&imm_expr, s);
8597               check_absolute_expr (ip, &imm_expr);
8598               if ((unsigned long) imm_expr.X_add_number < 32
8599                   || (unsigned long) imm_expr.X_add_number > 63)
8600                 break;
8601               ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8602               imm_expr.X_op = O_absent;
8603               s = expr_end;
8604               continue;
8605
8606             case 'k':           /* cache code */
8607             case 'h':           /* prefx code */
8608               my_getExpression (&imm_expr, s);
8609               check_absolute_expr (ip, &imm_expr);
8610               if ((unsigned long) imm_expr.X_add_number > 31)
8611                 {
8612                   as_warn (_("Invalid value for `%s' (%lu)"),
8613                            ip->insn_mo->name,
8614                            (unsigned long) imm_expr.X_add_number);
8615                   imm_expr.X_add_number &= 0x1f;
8616                 }
8617               if (*args == 'k')
8618                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8619               else
8620                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8621               imm_expr.X_op = O_absent;
8622               s = expr_end;
8623               continue;
8624
8625             case 'c':           /* break code */
8626               my_getExpression (&imm_expr, s);
8627               check_absolute_expr (ip, &imm_expr);
8628               if ((unsigned long) imm_expr.X_add_number > 1023)
8629                 {
8630                   as_warn (_("Illegal break code (%lu)"),
8631                            (unsigned long) imm_expr.X_add_number);
8632                   imm_expr.X_add_number &= OP_MASK_CODE;
8633                 }
8634               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8635               imm_expr.X_op = O_absent;
8636               s = expr_end;
8637               continue;
8638
8639             case 'q':           /* lower break code */
8640               my_getExpression (&imm_expr, s);
8641               check_absolute_expr (ip, &imm_expr);
8642               if ((unsigned long) imm_expr.X_add_number > 1023)
8643                 {
8644                   as_warn (_("Illegal lower break code (%lu)"),
8645                            (unsigned long) imm_expr.X_add_number);
8646                   imm_expr.X_add_number &= OP_MASK_CODE2;
8647                 }
8648               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8649               imm_expr.X_op = O_absent;
8650               s = expr_end;
8651               continue;
8652
8653             case 'B':           /* 20-bit syscall/break code.  */
8654               my_getExpression (&imm_expr, s);
8655               check_absolute_expr (ip, &imm_expr);
8656               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8657                 as_warn (_("Illegal 20-bit code (%lu)"),
8658                          (unsigned long) imm_expr.X_add_number);
8659               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8660               imm_expr.X_op = O_absent;
8661               s = expr_end;
8662               continue;
8663
8664             case 'C':           /* Coprocessor code */
8665               my_getExpression (&imm_expr, s);
8666               check_absolute_expr (ip, &imm_expr);
8667               if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8668                 {
8669                   as_warn (_("Coproccesor code > 25 bits (%lu)"),
8670                            (unsigned long) imm_expr.X_add_number);
8671                   imm_expr.X_add_number &= ((1 << 25) - 1);
8672                 }
8673               ip->insn_opcode |= imm_expr.X_add_number;
8674               imm_expr.X_op = O_absent;
8675               s = expr_end;
8676               continue;
8677
8678             case 'J':           /* 19-bit wait code.  */
8679               my_getExpression (&imm_expr, s);
8680               check_absolute_expr (ip, &imm_expr);
8681               if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8682                 as_warn (_("Illegal 19-bit code (%lu)"),
8683                          (unsigned long) imm_expr.X_add_number);
8684               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8685               imm_expr.X_op = O_absent;
8686               s = expr_end;
8687               continue;
8688
8689             case 'P':           /* Performance register */
8690               my_getExpression (&imm_expr, s);
8691               check_absolute_expr (ip, &imm_expr);
8692               if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8693                 {
8694                   as_warn (_("Invalid performance register (%lu)"),
8695                            (unsigned long) imm_expr.X_add_number);
8696                   imm_expr.X_add_number &= OP_MASK_PERFREG;
8697                 }
8698               ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8699               imm_expr.X_op = O_absent;
8700               s = expr_end;
8701               continue;
8702
8703             case 'b':           /* base register */
8704             case 'd':           /* destination register */
8705             case 's':           /* source register */
8706             case 't':           /* target register */
8707             case 'r':           /* both target and source */
8708             case 'v':           /* both dest and source */
8709             case 'w':           /* both dest and target */
8710             case 'E':           /* coprocessor target register */
8711             case 'G':           /* coprocessor destination register */
8712             case 'K':           /* 'rdhwr' destination register */
8713             case 'x':           /* ignore register name */
8714             case 'z':           /* must be zero register */
8715             case 'U':           /* destination register (clo/clz).  */
8716               s_reset = s;
8717               if (s[0] == '$')
8718                 {
8719
8720                   if (ISDIGIT (s[1]))
8721                     {
8722                       ++s;
8723                       regno = 0;
8724                       do
8725                         {
8726                           regno *= 10;
8727                           regno += *s - '0';
8728                           ++s;
8729                         }
8730                       while (ISDIGIT (*s));
8731                       if (regno > 31)
8732                         as_bad (_("Invalid register number (%d)"), regno);
8733                     }
8734                   else if (*args == 'E' || *args == 'G' || *args == 'K')
8735                     goto notreg;
8736                   else
8737                     {
8738                       if (s[1] == 'r' && s[2] == 'a')
8739                         {
8740                           s += 3;
8741                           regno = RA;
8742                         }
8743                       else if (s[1] == 'f' && s[2] == 'p')
8744                         {
8745                           s += 3;
8746                           regno = FP;
8747                         }
8748                       else if (s[1] == 's' && s[2] == 'p')
8749                         {
8750                           s += 3;
8751                           regno = SP;
8752                         }
8753                       else if (s[1] == 'g' && s[2] == 'p')
8754                         {
8755                           s += 3;
8756                           regno = GP;
8757                         }
8758                       else if (s[1] == 'a' && s[2] == 't')
8759                         {
8760                           s += 3;
8761                           regno = AT;
8762                         }
8763                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8764                         {
8765                           s += 4;
8766                           regno = KT0;
8767                         }
8768                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8769                         {
8770                           s += 4;
8771                           regno = KT1;
8772                         }
8773                       else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8774                         {
8775                           s += 5;
8776                           regno = ZERO;
8777                         }
8778                       else if (itbl_have_entries)
8779                         {
8780                           char *p, *n;
8781                           unsigned long r;
8782
8783                           p = s + 1;    /* advance past '$' */
8784                           n = itbl_get_field (&p);  /* n is name */
8785
8786                           /* See if this is a register defined in an
8787                              itbl entry.  */
8788                           if (itbl_get_reg_val (n, &r))
8789                             {
8790                               /* Get_field advances to the start of
8791                                  the next field, so we need to back
8792                                  rack to the end of the last field.  */
8793                               if (p)
8794                                 s = p - 1;
8795                               else
8796                                 s = strchr (s, '\0');
8797                               regno = r;
8798                             }
8799                           else
8800                             goto notreg;
8801                         }
8802                       else
8803                         goto notreg;
8804                     }
8805                   if (regno == AT
8806                       && ! mips_opts.noat
8807                       && *args != 'E'
8808                       && *args != 'G'
8809                       && *args != 'K')
8810                     as_warn (_("Used $at without \".set noat\""));
8811                   c = *args;
8812                   if (*s == ' ')
8813                     ++s;
8814                   if (args[1] != *s)
8815                     {
8816                       if (c == 'r' || c == 'v' || c == 'w')
8817                         {
8818                           regno = lastregno;
8819                           s = s_reset;
8820                           ++args;
8821                         }
8822                     }
8823                   /* 'z' only matches $0.  */
8824                   if (c == 'z' && regno != 0)
8825                     break;
8826
8827         /* Now that we have assembled one operand, we use the args string
8828          * to figure out where it goes in the instruction.  */
8829                   switch (c)
8830                     {
8831                     case 'r':
8832                     case 's':
8833                     case 'v':
8834                     case 'b':
8835                       ip->insn_opcode |= regno << OP_SH_RS;
8836                       break;
8837                     case 'd':
8838                     case 'G':
8839                     case 'K':
8840                       ip->insn_opcode |= regno << OP_SH_RD;
8841                       break;
8842                     case 'U':
8843                       ip->insn_opcode |= regno << OP_SH_RD;
8844                       ip->insn_opcode |= regno << OP_SH_RT;
8845                       break;
8846                     case 'w':
8847                     case 't':
8848                     case 'E':
8849                       ip->insn_opcode |= regno << OP_SH_RT;
8850                       break;
8851                     case 'x':
8852                       /* This case exists because on the r3000 trunc
8853                          expands into a macro which requires a gp
8854                          register.  On the r6000 or r4000 it is
8855                          assembled into a single instruction which
8856                          ignores the register.  Thus the insn version
8857                          is MIPS_ISA2 and uses 'x', and the macro
8858                          version is MIPS_ISA1 and uses 't'.  */
8859                       break;
8860                     case 'z':
8861                       /* This case is for the div instruction, which
8862                          acts differently if the destination argument
8863                          is $0.  This only matches $0, and is checked
8864                          outside the switch.  */
8865                       break;
8866                     case 'D':
8867                       /* Itbl operand; not yet implemented. FIXME ?? */
8868                       break;
8869                       /* What about all other operands like 'i', which
8870                          can be specified in the opcode table? */
8871                     }
8872                   lastregno = regno;
8873                   continue;
8874                 }
8875             notreg:
8876               switch (*args++)
8877                 {
8878                 case 'r':
8879                 case 'v':
8880                   ip->insn_opcode |= lastregno << OP_SH_RS;
8881                   continue;
8882                 case 'w':
8883                   ip->insn_opcode |= lastregno << OP_SH_RT;
8884                   continue;
8885                 }
8886               break;
8887
8888             case 'O':           /* MDMX alignment immediate constant.  */
8889               my_getExpression (&imm_expr, s);
8890               check_absolute_expr (ip, &imm_expr);
8891               if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8892                 {
8893                   as_warn ("Improper align amount (%ld), using low bits",
8894                            (long) imm_expr.X_add_number);
8895                   imm_expr.X_add_number &= OP_MASK_ALN;
8896                 }
8897               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8898               imm_expr.X_op = O_absent;
8899               s = expr_end;
8900               continue;
8901
8902             case 'Q':           /* MDMX vector, element sel, or const.  */
8903               if (s[0] != '$')
8904                 {
8905                   /* MDMX Immediate.  */
8906                   my_getExpression (&imm_expr, s);
8907                   check_absolute_expr (ip, &imm_expr);
8908                   if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8909                     {
8910                       as_warn (_("Invalid MDMX Immediate (%ld)"),
8911                                (long) imm_expr.X_add_number);
8912                       imm_expr.X_add_number &= OP_MASK_FT;
8913                     }
8914                   imm_expr.X_add_number &= OP_MASK_FT;
8915                   if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8916                     ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8917                   else
8918                     ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8919                   ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8920                   imm_expr.X_op = O_absent;
8921                   s = expr_end;
8922                   continue;
8923                 }
8924               /* Not MDMX Immediate.  Fall through.  */
8925             case 'X':           /* MDMX destination register.  */
8926             case 'Y':           /* MDMX source register.  */
8927             case 'Z':           /* MDMX target register.  */
8928               is_mdmx = 1;
8929             case 'D':           /* floating point destination register */
8930             case 'S':           /* floating point source register */
8931             case 'T':           /* floating point target register */
8932             case 'R':           /* floating point source register */
8933             case 'V':
8934             case 'W':
8935               s_reset = s;
8936               /* Accept $fN for FP and MDMX register numbers, and in
8937                  addition accept $vN for MDMX register numbers.  */
8938               if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8939                   || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8940                       && ISDIGIT (s[2])))
8941                 {
8942                   s += 2;
8943                   regno = 0;
8944                   do
8945                     {
8946                       regno *= 10;
8947                       regno += *s - '0';
8948                       ++s;
8949                     }
8950                   while (ISDIGIT (*s));
8951
8952                   if (regno > 31)
8953                     as_bad (_("Invalid float register number (%d)"), regno);
8954
8955                   if ((regno & 1) != 0
8956                       && HAVE_32BIT_FPRS
8957                       && ! (strcmp (str, "mtc1") == 0
8958                             || strcmp (str, "mfc1") == 0
8959                             || strcmp (str, "lwc1") == 0
8960                             || strcmp (str, "swc1") == 0
8961                             || strcmp (str, "l.s") == 0
8962                             || strcmp (str, "s.s") == 0))
8963                     as_warn (_("Float register should be even, was %d"),
8964                              regno);
8965
8966                   c = *args;
8967                   if (*s == ' ')
8968                     ++s;
8969                   if (args[1] != *s)
8970                     {
8971                       if (c == 'V' || c == 'W')
8972                         {
8973                           regno = lastregno;
8974                           s = s_reset;
8975                           ++args;
8976                         }
8977                     }
8978                   switch (c)
8979                     {
8980                     case 'D':
8981                     case 'X':
8982                       ip->insn_opcode |= regno << OP_SH_FD;
8983                       break;
8984                     case 'V':
8985                     case 'S':
8986                     case 'Y':
8987                       ip->insn_opcode |= regno << OP_SH_FS;
8988                       break;
8989                     case 'Q':
8990                       /* This is like 'Z', but also needs to fix the MDMX
8991                          vector/scalar select bits.  Note that the
8992                          scalar immediate case is handled above.  */
8993                       if (*s == '[')
8994                         {
8995                           int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8996                           int max_el = (is_qh ? 3 : 7);
8997                           s++;
8998                           my_getExpression(&imm_expr, s);
8999                           check_absolute_expr (ip, &imm_expr);
9000                           s = expr_end;
9001                           if (imm_expr.X_add_number > max_el)
9002                             as_bad(_("Bad element selector %ld"),
9003                                    (long) imm_expr.X_add_number);
9004                           imm_expr.X_add_number &= max_el;
9005                           ip->insn_opcode |= (imm_expr.X_add_number
9006                                               << (OP_SH_VSEL +
9007                                                   (is_qh ? 2 : 1)));
9008                           if (*s != ']')
9009                             as_warn(_("Expecting ']' found '%s'"), s);
9010                           else
9011                             s++;
9012                         }
9013                       else
9014                         {
9015                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9016                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9017                                                 << OP_SH_VSEL);
9018                           else
9019                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9020                                                 OP_SH_VSEL);
9021                         }
9022                       /* Fall through */
9023                     case 'W':
9024                     case 'T':
9025                     case 'Z':
9026                       ip->insn_opcode |= regno << OP_SH_FT;
9027                       break;
9028                     case 'R':
9029                       ip->insn_opcode |= regno << OP_SH_FR;
9030                       break;
9031                     }
9032                   lastregno = regno;
9033                   continue;
9034                 }
9035
9036               switch (*args++)
9037                 {
9038                 case 'V':
9039                   ip->insn_opcode |= lastregno << OP_SH_FS;
9040                   continue;
9041                 case 'W':
9042                   ip->insn_opcode |= lastregno << OP_SH_FT;
9043                   continue;
9044                 }
9045               break;
9046
9047             case 'I':
9048               my_getExpression (&imm_expr, s);
9049               if (imm_expr.X_op != O_big
9050                   && imm_expr.X_op != O_constant)
9051                 insn_error = _("absolute expression required");
9052               s = expr_end;
9053               continue;
9054
9055             case 'A':
9056               my_getExpression (&offset_expr, s);
9057               *imm_reloc = BFD_RELOC_32;
9058               s = expr_end;
9059               continue;
9060
9061             case 'F':
9062             case 'L':
9063             case 'f':
9064             case 'l':
9065               {
9066                 int f64;
9067                 int using_gprs;
9068                 char *save_in;
9069                 char *err;
9070                 unsigned char temp[8];
9071                 int len;
9072                 unsigned int length;
9073                 segT seg;
9074                 subsegT subseg;
9075                 char *p;
9076
9077                 /* These only appear as the last operand in an
9078                    instruction, and every instruction that accepts
9079                    them in any variant accepts them in all variants.
9080                    This means we don't have to worry about backing out
9081                    any changes if the instruction does not match.
9082
9083                    The difference between them is the size of the
9084                    floating point constant and where it goes.  For 'F'
9085                    and 'L' the constant is 64 bits; for 'f' and 'l' it
9086                    is 32 bits.  Where the constant is placed is based
9087                    on how the MIPS assembler does things:
9088                     F -- .rdata
9089                     L -- .lit8
9090                     f -- immediate value
9091                     l -- .lit4
9092
9093                     The .lit4 and .lit8 sections are only used if
9094                     permitted by the -G argument.
9095
9096                     When generating embedded PIC code, we use the
9097                     .lit8 section but not the .lit4 section (we can do
9098                     .lit4 inline easily; we need to put .lit8
9099                     somewhere in the data segment, and using .lit8
9100                     permits the linker to eventually combine identical
9101                     .lit8 entries).
9102
9103                     The code below needs to know whether the target register
9104                     is 32 or 64 bits wide.  It relies on the fact 'f' and
9105                     'F' are used with GPR-based instructions and 'l' and
9106                     'L' are used with FPR-based instructions.  */
9107
9108                 f64 = *args == 'F' || *args == 'L';
9109                 using_gprs = *args == 'F' || *args == 'f';
9110
9111                 save_in = input_line_pointer;
9112                 input_line_pointer = s;
9113                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9114                 length = len;
9115                 s = input_line_pointer;
9116                 input_line_pointer = save_in;
9117                 if (err != NULL && *err != '\0')
9118                   {
9119                     as_bad (_("Bad floating point constant: %s"), err);
9120                     memset (temp, '\0', sizeof temp);
9121                     length = f64 ? 8 : 4;
9122                   }
9123
9124                 assert (length == (unsigned) (f64 ? 8 : 4));
9125
9126                 if (*args == 'f'
9127                     || (*args == 'l'
9128                         && (! USE_GLOBAL_POINTER_OPT
9129                             || mips_pic == EMBEDDED_PIC
9130                             || g_switch_value < 4
9131                             || (temp[0] == 0 && temp[1] == 0)
9132                             || (temp[2] == 0 && temp[3] == 0))))
9133                   {
9134                     imm_expr.X_op = O_constant;
9135                     if (! target_big_endian)
9136                       imm_expr.X_add_number = bfd_getl32 (temp);
9137                     else
9138                       imm_expr.X_add_number = bfd_getb32 (temp);
9139                   }
9140                 else if (length > 4
9141                          && ! mips_disable_float_construction
9142                          /* Constants can only be constructed in GPRs and
9143                             copied to FPRs if the GPRs are at least as wide
9144                             as the FPRs.  Force the constant into memory if
9145                             we are using 64-bit FPRs but the GPRs are only
9146                             32 bits wide.  */
9147                          && (using_gprs
9148                              || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9149                          && ((temp[0] == 0 && temp[1] == 0)
9150                              || (temp[2] == 0 && temp[3] == 0))
9151                          && ((temp[4] == 0 && temp[5] == 0)
9152                              || (temp[6] == 0 && temp[7] == 0)))
9153                   {
9154                     /* The value is simple enough to load with a couple of
9155                        instructions.  If using 32-bit registers, set
9156                        imm_expr to the high order 32 bits and offset_expr to
9157                        the low order 32 bits.  Otherwise, set imm_expr to
9158                        the entire 64 bit constant.  */
9159                     if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9160                       {
9161                         imm_expr.X_op = O_constant;
9162                         offset_expr.X_op = O_constant;
9163                         if (! target_big_endian)
9164                           {
9165                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
9166                             offset_expr.X_add_number = bfd_getl32 (temp);
9167                           }
9168                         else
9169                           {
9170                             imm_expr.X_add_number = bfd_getb32 (temp);
9171                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
9172                           }
9173                         if (offset_expr.X_add_number == 0)
9174                           offset_expr.X_op = O_absent;
9175                       }
9176                     else if (sizeof (imm_expr.X_add_number) > 4)
9177                       {
9178                         imm_expr.X_op = O_constant;
9179                         if (! target_big_endian)
9180                           imm_expr.X_add_number = bfd_getl64 (temp);
9181                         else
9182                           imm_expr.X_add_number = bfd_getb64 (temp);
9183                       }
9184                     else
9185                       {
9186                         imm_expr.X_op = O_big;
9187                         imm_expr.X_add_number = 4;
9188                         if (! target_big_endian)
9189                           {
9190                             generic_bignum[0] = bfd_getl16 (temp);
9191                             generic_bignum[1] = bfd_getl16 (temp + 2);
9192                             generic_bignum[2] = bfd_getl16 (temp + 4);
9193                             generic_bignum[3] = bfd_getl16 (temp + 6);
9194                           }
9195                         else
9196                           {
9197                             generic_bignum[0] = bfd_getb16 (temp + 6);
9198                             generic_bignum[1] = bfd_getb16 (temp + 4);
9199                             generic_bignum[2] = bfd_getb16 (temp + 2);
9200                             generic_bignum[3] = bfd_getb16 (temp);
9201                           }
9202                       }
9203                   }
9204                 else
9205                   {
9206                     const char *newname;
9207                     segT new_seg;
9208
9209                     /* Switch to the right section.  */
9210                     seg = now_seg;
9211                     subseg = now_subseg;
9212                     switch (*args)
9213                       {
9214                       default: /* unused default case avoids warnings.  */
9215                       case 'L':
9216                         newname = RDATA_SECTION_NAME;
9217                         if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9218                             || mips_pic == EMBEDDED_PIC)
9219                           newname = ".lit8";
9220                         break;
9221                       case 'F':
9222                         if (mips_pic == EMBEDDED_PIC)
9223                           newname = ".lit8";
9224                         else
9225                           newname = RDATA_SECTION_NAME;
9226                         break;
9227                       case 'l':
9228                         assert (!USE_GLOBAL_POINTER_OPT
9229                                 || g_switch_value >= 4);
9230                         newname = ".lit4";
9231                         break;
9232                       }
9233                     new_seg = subseg_new (newname, (subsegT) 0);
9234                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9235                       bfd_set_section_flags (stdoutput, new_seg,
9236                                              (SEC_ALLOC
9237                                               | SEC_LOAD
9238                                               | SEC_READONLY
9239                                               | SEC_DATA));
9240                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
9241                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9242                         && strcmp (TARGET_OS, "elf") != 0)
9243                       record_alignment (new_seg, 4);
9244                     else
9245                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
9246                     if (seg == now_seg)
9247                       as_bad (_("Can't use floating point insn in this section"));
9248
9249                     /* Set the argument to the current address in the
9250                        section.  */
9251                     offset_expr.X_op = O_symbol;
9252                     offset_expr.X_add_symbol =
9253                       symbol_new ("L0\001", now_seg,
9254                                   (valueT) frag_now_fix (), frag_now);
9255                     offset_expr.X_add_number = 0;
9256
9257                     /* Put the floating point number into the section.  */
9258                     p = frag_more ((int) length);
9259                     memcpy (p, temp, length);
9260
9261                     /* Switch back to the original section.  */
9262                     subseg_set (seg, subseg);
9263                   }
9264               }
9265               continue;
9266
9267             case 'i':           /* 16 bit unsigned immediate */
9268             case 'j':           /* 16 bit signed immediate */
9269               *imm_reloc = BFD_RELOC_LO16;
9270               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9271                 {
9272                   int more;
9273                   offsetT minval, maxval;
9274
9275                   more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9276                           && strcmp (insn->name, insn[1].name) == 0);
9277
9278                   /* If the expression was written as an unsigned number,
9279                      only treat it as signed if there are no more
9280                      alternatives.  */
9281                   if (more
9282                       && *args == 'j'
9283                       && sizeof (imm_expr.X_add_number) <= 4
9284                       && imm_expr.X_op == O_constant
9285                       && imm_expr.X_add_number < 0
9286                       && imm_expr.X_unsigned
9287                       && HAVE_64BIT_GPRS)
9288                     break;
9289
9290                   /* For compatibility with older assemblers, we accept
9291                      0x8000-0xffff as signed 16-bit numbers when only
9292                      signed numbers are allowed.  */
9293                   if (*args == 'i')
9294                     minval = 0, maxval = 0xffff;
9295                   else if (more)
9296                     minval = -0x8000, maxval = 0x7fff;
9297                   else
9298                     minval = -0x8000, maxval = 0xffff;
9299
9300                   if (imm_expr.X_op != O_constant
9301                       || imm_expr.X_add_number < minval
9302                       || imm_expr.X_add_number > maxval)
9303                     {
9304                       if (more)
9305                         break;
9306                       if (imm_expr.X_op == O_constant
9307                           || imm_expr.X_op == O_big)
9308                         as_bad (_("expression out of range"));
9309                     }
9310                 }
9311               s = expr_end;
9312               continue;
9313
9314             case 'o':           /* 16 bit offset */
9315               /* Check whether there is only a single bracketed expression
9316                  left.  If so, it must be the base register and the
9317                  constant must be zero.  */
9318               if (*s == '(' && strchr (s + 1, '(') == 0)
9319                 {
9320                   offset_expr.X_op = O_constant;
9321                   offset_expr.X_add_number = 0;
9322                   continue;
9323                 }
9324
9325               /* If this value won't fit into a 16 bit offset, then go
9326                  find a macro that will generate the 32 bit offset
9327                  code pattern.  */
9328               if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9329                   && (offset_expr.X_op != O_constant
9330                       || offset_expr.X_add_number >= 0x8000
9331                       || offset_expr.X_add_number < -0x8000))
9332                 break;
9333
9334               s = expr_end;
9335               continue;
9336
9337             case 'p':           /* pc relative offset */
9338               *offset_reloc = BFD_RELOC_16_PCREL_S2;
9339               my_getExpression (&offset_expr, s);
9340               s = expr_end;
9341               continue;
9342
9343             case 'u':           /* upper 16 bits */
9344               if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9345                   && imm_expr.X_op == O_constant
9346                   && (imm_expr.X_add_number < 0
9347                       || imm_expr.X_add_number >= 0x10000))
9348                 as_bad (_("lui expression not in range 0..65535"));
9349               s = expr_end;
9350               continue;
9351
9352             case 'a':           /* 26 bit address */
9353               my_getExpression (&offset_expr, s);
9354               s = expr_end;
9355               *offset_reloc = BFD_RELOC_MIPS_JMP;
9356               continue;
9357
9358             case 'N':           /* 3 bit branch condition code */
9359             case 'M':           /* 3 bit compare condition code */
9360               if (strncmp (s, "$fcc", 4) != 0)
9361                 break;
9362               s += 4;
9363               regno = 0;
9364               do
9365                 {
9366                   regno *= 10;
9367                   regno += *s - '0';
9368                   ++s;
9369                 }
9370               while (ISDIGIT (*s));
9371               if (regno > 7)
9372                 as_bad (_("Invalid condition code register $fcc%d"), regno);
9373               if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9374                    || strcmp(str + strlen(str) - 5, "any2f") == 0
9375                    || strcmp(str + strlen(str) - 5, "any2t") == 0)
9376                   && (regno & 1) != 0)
9377                 as_warn(_("Condition code register should be even for %s, was %d"),
9378                         str, regno);
9379               if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9380                    || strcmp(str + strlen(str) - 5, "any4t") == 0)
9381                   && (regno & 3) != 0)
9382                 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9383                         str, regno);
9384               if (*args == 'N')
9385                 ip->insn_opcode |= regno << OP_SH_BCC;
9386               else
9387                 ip->insn_opcode |= regno << OP_SH_CCC;
9388               continue;
9389
9390             case 'H':
9391               if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9392                 s += 2;
9393               if (ISDIGIT (*s))
9394                 {
9395                   c = 0;
9396                   do
9397                     {
9398                       c *= 10;
9399                       c += *s - '0';
9400                       ++s;
9401                     }
9402                   while (ISDIGIT (*s));
9403                 }
9404               else
9405                 c = 8; /* Invalid sel value.  */
9406
9407               if (c > 7)
9408                 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9409               ip->insn_opcode |= c;
9410               continue;
9411
9412             case 'e':
9413               /* Must be at least one digit.  */
9414               my_getExpression (&imm_expr, s);
9415               check_absolute_expr (ip, &imm_expr);
9416
9417               if ((unsigned long) imm_expr.X_add_number
9418                   > (unsigned long) OP_MASK_VECBYTE)
9419                 {
9420                   as_bad (_("bad byte vector index (%ld)"),
9421                            (long) imm_expr.X_add_number);
9422                   imm_expr.X_add_number = 0;
9423                 }
9424
9425               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9426               imm_expr.X_op = O_absent;
9427               s = expr_end;
9428               continue;
9429
9430             case '%':
9431               my_getExpression (&imm_expr, s);
9432               check_absolute_expr (ip, &imm_expr);
9433
9434               if ((unsigned long) imm_expr.X_add_number
9435                   > (unsigned long) OP_MASK_VECALIGN)
9436                 {
9437                   as_bad (_("bad byte vector index (%ld)"),
9438                            (long) imm_expr.X_add_number);
9439                   imm_expr.X_add_number = 0;
9440                 }
9441
9442               ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9443               imm_expr.X_op = O_absent;
9444               s = expr_end;
9445               continue;
9446
9447             default:
9448               as_bad (_("bad char = '%c'\n"), *args);
9449               internalError ();
9450             }
9451           break;
9452         }
9453       /* Args don't match.  */
9454       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9455           !strcmp (insn->name, insn[1].name))
9456         {
9457           ++insn;
9458           s = argsStart;
9459           insn_error = _("illegal operands");
9460           continue;
9461         }
9462       if (save_c)
9463         *(--s) = save_c;
9464       insn_error = _("illegal operands");
9465       return;
9466     }
9467 }
9468
9469 /* This routine assembles an instruction into its binary format when
9470    assembling for the mips16.  As a side effect, it sets one of the
9471    global variables imm_reloc or offset_reloc to the type of
9472    relocation to do if one of the operands is an address expression.
9473    It also sets mips16_small and mips16_ext if the user explicitly
9474    requested a small or extended instruction.  */
9475
9476 static void
9477 mips16_ip (char *str, struct mips_cl_insn *ip)
9478 {
9479   char *s;
9480   const char *args;
9481   struct mips_opcode *insn;
9482   char *argsstart;
9483   unsigned int regno;
9484   unsigned int lastregno = 0;
9485   char *s_reset;
9486
9487   insn_error = NULL;
9488
9489   mips16_small = FALSE;
9490   mips16_ext = FALSE;
9491
9492   for (s = str; ISLOWER (*s); ++s)
9493     ;
9494   switch (*s)
9495     {
9496     case '\0':
9497       break;
9498
9499     case ' ':
9500       *s++ = '\0';
9501       break;
9502
9503     case '.':
9504       if (s[1] == 't' && s[2] == ' ')
9505         {
9506           *s = '\0';
9507           mips16_small = TRUE;
9508           s += 3;
9509           break;
9510         }
9511       else if (s[1] == 'e' && s[2] == ' ')
9512         {
9513           *s = '\0';
9514           mips16_ext = TRUE;
9515           s += 3;
9516           break;
9517         }
9518       /* Fall through.  */
9519     default:
9520       insn_error = _("unknown opcode");
9521       return;
9522     }
9523
9524   if (mips_opts.noautoextend && ! mips16_ext)
9525     mips16_small = TRUE;
9526
9527   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9528     {
9529       insn_error = _("unrecognized opcode");
9530       return;
9531     }
9532
9533   argsstart = s;
9534   for (;;)
9535     {
9536       assert (strcmp (insn->name, str) == 0);
9537
9538       ip->insn_mo = insn;
9539       ip->insn_opcode = insn->match;
9540       ip->use_extend = FALSE;
9541       imm_expr.X_op = O_absent;
9542       imm_reloc[0] = BFD_RELOC_UNUSED;
9543       imm_reloc[1] = BFD_RELOC_UNUSED;
9544       imm_reloc[2] = BFD_RELOC_UNUSED;
9545       imm2_expr.X_op = O_absent;
9546       offset_expr.X_op = O_absent;
9547       offset_reloc[0] = BFD_RELOC_UNUSED;
9548       offset_reloc[1] = BFD_RELOC_UNUSED;
9549       offset_reloc[2] = BFD_RELOC_UNUSED;
9550       for (args = insn->args; 1; ++args)
9551         {
9552           int c;
9553
9554           if (*s == ' ')
9555             ++s;
9556
9557           /* In this switch statement we call break if we did not find
9558              a match, continue if we did find a match, or return if we
9559              are done.  */
9560
9561           c = *args;
9562           switch (c)
9563             {
9564             case '\0':
9565               if (*s == '\0')
9566                 {
9567                   /* Stuff the immediate value in now, if we can.  */
9568                   if (imm_expr.X_op == O_constant
9569                       && *imm_reloc > BFD_RELOC_UNUSED
9570                       && insn->pinfo != INSN_MACRO)
9571                     {
9572                       mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9573                                     imm_expr.X_add_number, TRUE, mips16_small,
9574                                     mips16_ext, &ip->insn_opcode,
9575                                     &ip->use_extend, &ip->extend);
9576                       imm_expr.X_op = O_absent;
9577                       *imm_reloc = BFD_RELOC_UNUSED;
9578                     }
9579
9580                   return;
9581                 }
9582               break;
9583
9584             case ',':
9585               if (*s++ == c)
9586                 continue;
9587               s--;
9588               switch (*++args)
9589                 {
9590                 case 'v':
9591                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9592                   continue;
9593                 case 'w':
9594                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9595                   continue;
9596                 }
9597               break;
9598
9599             case '(':
9600             case ')':
9601               if (*s++ == c)
9602                 continue;
9603               break;
9604
9605             case 'v':
9606             case 'w':
9607               if (s[0] != '$')
9608                 {
9609                   if (c == 'v')
9610                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9611                   else
9612                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9613                   ++args;
9614                   continue;
9615                 }
9616               /* Fall through.  */
9617             case 'x':
9618             case 'y':
9619             case 'z':
9620             case 'Z':
9621             case '0':
9622             case 'S':
9623             case 'R':
9624             case 'X':
9625             case 'Y':
9626               if (s[0] != '$')
9627                 break;
9628               s_reset = s;
9629               if (ISDIGIT (s[1]))
9630                 {
9631                   ++s;
9632                   regno = 0;
9633                   do
9634                     {
9635                       regno *= 10;
9636                       regno += *s - '0';
9637                       ++s;
9638                     }
9639                   while (ISDIGIT (*s));
9640                   if (regno > 31)
9641                     {
9642                       as_bad (_("invalid register number (%d)"), regno);
9643                       regno = 2;
9644                     }
9645                 }
9646               else
9647                 {
9648                   if (s[1] == 'r' && s[2] == 'a')
9649                     {
9650                       s += 3;
9651                       regno = RA;
9652                     }
9653                   else if (s[1] == 'f' && s[2] == 'p')
9654                     {
9655                       s += 3;
9656                       regno = FP;
9657                     }
9658                   else if (s[1] == 's' && s[2] == 'p')
9659                     {
9660                       s += 3;
9661                       regno = SP;
9662                     }
9663                   else if (s[1] == 'g' && s[2] == 'p')
9664                     {
9665                       s += 3;
9666                       regno = GP;
9667                     }
9668                   else if (s[1] == 'a' && s[2] == 't')
9669                     {
9670                       s += 3;
9671                       regno = AT;
9672                     }
9673                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9674                     {
9675                       s += 4;
9676                       regno = KT0;
9677                     }
9678                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9679                     {
9680                       s += 4;
9681                       regno = KT1;
9682                     }
9683                   else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9684                     {
9685                       s += 5;
9686                       regno = ZERO;
9687                     }
9688                   else
9689                     break;
9690                 }
9691
9692               if (*s == ' ')
9693                 ++s;
9694               if (args[1] != *s)
9695                 {
9696                   if (c == 'v' || c == 'w')
9697                     {
9698                       regno = mips16_to_32_reg_map[lastregno];
9699                       s = s_reset;
9700                       ++args;
9701                     }
9702                 }
9703
9704               switch (c)
9705                 {
9706                 case 'x':
9707                 case 'y':
9708                 case 'z':
9709                 case 'v':
9710                 case 'w':
9711                 case 'Z':
9712                   regno = mips32_to_16_reg_map[regno];
9713                   break;
9714
9715                 case '0':
9716                   if (regno != 0)
9717                     regno = ILLEGAL_REG;
9718                   break;
9719
9720                 case 'S':
9721                   if (regno != SP)
9722                     regno = ILLEGAL_REG;
9723                   break;
9724
9725                 case 'R':
9726                   if (regno != RA)
9727                     regno = ILLEGAL_REG;
9728                   break;
9729
9730                 case 'X':
9731                 case 'Y':
9732                   if (regno == AT && ! mips_opts.noat)
9733                     as_warn (_("used $at without \".set noat\""));
9734                   break;
9735
9736                 default:
9737                   internalError ();
9738                 }
9739
9740               if (regno == ILLEGAL_REG)
9741                 break;
9742
9743               switch (c)
9744                 {
9745                 case 'x':
9746                 case 'v':
9747                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9748                   break;
9749                 case 'y':
9750                 case 'w':
9751                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9752                   break;
9753                 case 'z':
9754                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9755                   break;
9756                 case 'Z':
9757                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9758                 case '0':
9759                 case 'S':
9760                 case 'R':
9761                   break;
9762                 case 'X':
9763                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9764                   break;
9765                 case 'Y':
9766                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9767                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9768                   break;
9769                 default:
9770                   internalError ();
9771                 }
9772
9773               lastregno = regno;
9774               continue;
9775
9776             case 'P':
9777               if (strncmp (s, "$pc", 3) == 0)
9778                 {
9779                   s += 3;
9780                   continue;
9781                 }
9782               break;
9783
9784             case '<':
9785             case '>':
9786             case '[':
9787             case ']':
9788             case '4':
9789             case '5':
9790             case 'H':
9791             case 'W':
9792             case 'D':
9793             case 'j':
9794             case '8':
9795             case 'V':
9796             case 'C':
9797             case 'U':
9798             case 'k':
9799             case 'K':
9800               if (s[0] == '%'
9801                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9802                 {
9803                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
9804                      and generate the appropriate reloc.  If the text
9805                      inside %gprel is not a symbol name with an
9806                      optional offset, then we generate a normal reloc
9807                      and will probably fail later.  */
9808                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9809                   if (imm_expr.X_op == O_symbol)
9810                     {
9811                       mips16_ext = TRUE;
9812                       *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9813                       s = expr_end;
9814                       ip->use_extend = TRUE;
9815                       ip->extend = 0;
9816                       continue;
9817                     }
9818                 }
9819               else
9820                 {
9821                   /* Just pick up a normal expression.  */
9822                   my_getExpression (&imm_expr, s);
9823                 }
9824
9825               if (imm_expr.X_op == O_register)
9826                 {
9827                   /* What we thought was an expression turned out to
9828                      be a register.  */
9829
9830                   if (s[0] == '(' && args[1] == '(')
9831                     {
9832                       /* It looks like the expression was omitted
9833                          before a register indirection, which means
9834                          that the expression is implicitly zero.  We
9835                          still set up imm_expr, so that we handle
9836                          explicit extensions correctly.  */
9837                       imm_expr.X_op = O_constant;
9838                       imm_expr.X_add_number = 0;
9839                       *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9840                       continue;
9841                     }
9842
9843                   break;
9844                 }
9845
9846               /* We need to relax this instruction.  */
9847               *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9848               s = expr_end;
9849               continue;
9850
9851             case 'p':
9852             case 'q':
9853             case 'A':
9854             case 'B':
9855             case 'E':
9856               /* We use offset_reloc rather than imm_reloc for the PC
9857                  relative operands.  This lets macros with both
9858                  immediate and address operands work correctly.  */
9859               my_getExpression (&offset_expr, s);
9860
9861               if (offset_expr.X_op == O_register)
9862                 break;
9863
9864               /* We need to relax this instruction.  */
9865               *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9866               s = expr_end;
9867               continue;
9868
9869             case '6':           /* break code */
9870               my_getExpression (&imm_expr, s);
9871               check_absolute_expr (ip, &imm_expr);
9872               if ((unsigned long) imm_expr.X_add_number > 63)
9873                 {
9874                   as_warn (_("Invalid value for `%s' (%lu)"),
9875                            ip->insn_mo->name,
9876                            (unsigned long) imm_expr.X_add_number);
9877                   imm_expr.X_add_number &= 0x3f;
9878                 }
9879               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9880               imm_expr.X_op = O_absent;
9881               s = expr_end;
9882               continue;
9883
9884             case 'a':           /* 26 bit address */
9885               my_getExpression (&offset_expr, s);
9886               s = expr_end;
9887               *offset_reloc = BFD_RELOC_MIPS16_JMP;
9888               ip->insn_opcode <<= 16;
9889               continue;
9890
9891             case 'l':           /* register list for entry macro */
9892             case 'L':           /* register list for exit macro */
9893               {
9894                 int mask;
9895
9896                 if (c == 'l')
9897                   mask = 0;
9898                 else
9899                   mask = 7 << 3;
9900                 while (*s != '\0')
9901                   {
9902                     int freg, reg1, reg2;
9903
9904                     while (*s == ' ' || *s == ',')
9905                       ++s;
9906                     if (*s != '$')
9907                       {
9908                         as_bad (_("can't parse register list"));
9909                         break;
9910                       }
9911                     ++s;
9912                     if (*s != 'f')
9913                       freg = 0;
9914                     else
9915                       {
9916                         freg = 1;
9917                         ++s;
9918                       }
9919                     reg1 = 0;
9920                     while (ISDIGIT (*s))
9921                       {
9922                         reg1 *= 10;
9923                         reg1 += *s - '0';
9924                         ++s;
9925                       }
9926                     if (*s == ' ')
9927                       ++s;
9928                     if (*s != '-')
9929                       reg2 = reg1;
9930                     else
9931                       {
9932                         ++s;
9933                         if (*s != '$')
9934                           break;
9935                         ++s;
9936                         if (freg)
9937                           {
9938                             if (*s == 'f')
9939                               ++s;
9940                             else
9941                               {
9942                                 as_bad (_("invalid register list"));
9943                                 break;
9944                               }
9945                           }
9946                         reg2 = 0;
9947                         while (ISDIGIT (*s))
9948                           {
9949                             reg2 *= 10;
9950                             reg2 += *s - '0';
9951                             ++s;
9952                           }
9953                       }
9954                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9955                       {
9956                         mask &= ~ (7 << 3);
9957                         mask |= 5 << 3;
9958                       }
9959                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9960                       {
9961                         mask &= ~ (7 << 3);
9962                         mask |= 6 << 3;
9963                       }
9964                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9965                       mask |= (reg2 - 3) << 3;
9966                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9967                       mask |= (reg2 - 15) << 1;
9968                     else if (reg1 == RA && reg2 == RA)
9969                       mask |= 1;
9970                     else
9971                       {
9972                         as_bad (_("invalid register list"));
9973                         break;
9974                       }
9975                   }
9976                 /* The mask is filled in in the opcode table for the
9977                    benefit of the disassembler.  We remove it before
9978                    applying the actual mask.  */
9979                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9980                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9981               }
9982             continue;
9983
9984             case 'e':           /* extend code */
9985               my_getExpression (&imm_expr, s);
9986               check_absolute_expr (ip, &imm_expr);
9987               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9988                 {
9989                   as_warn (_("Invalid value for `%s' (%lu)"),
9990                            ip->insn_mo->name,
9991                            (unsigned long) imm_expr.X_add_number);
9992                   imm_expr.X_add_number &= 0x7ff;
9993                 }
9994               ip->insn_opcode |= imm_expr.X_add_number;
9995               imm_expr.X_op = O_absent;
9996               s = expr_end;
9997               continue;
9998
9999             default:
10000               internalError ();
10001             }
10002           break;
10003         }
10004
10005       /* Args don't match.  */
10006       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10007           strcmp (insn->name, insn[1].name) == 0)
10008         {
10009           ++insn;
10010           s = argsstart;
10011           continue;
10012         }
10013
10014       insn_error = _("illegal operands");
10015
10016       return;
10017     }
10018 }
10019
10020 /* This structure holds information we know about a mips16 immediate
10021    argument type.  */
10022
10023 struct mips16_immed_operand
10024 {
10025   /* The type code used in the argument string in the opcode table.  */
10026   int type;
10027   /* The number of bits in the short form of the opcode.  */
10028   int nbits;
10029   /* The number of bits in the extended form of the opcode.  */
10030   int extbits;
10031   /* The amount by which the short form is shifted when it is used;
10032      for example, the sw instruction has a shift count of 2.  */
10033   int shift;
10034   /* The amount by which the short form is shifted when it is stored
10035      into the instruction code.  */
10036   int op_shift;
10037   /* Non-zero if the short form is unsigned.  */
10038   int unsp;
10039   /* Non-zero if the extended form is unsigned.  */
10040   int extu;
10041   /* Non-zero if the value is PC relative.  */
10042   int pcrel;
10043 };
10044
10045 /* The mips16 immediate operand types.  */
10046
10047 static const struct mips16_immed_operand mips16_immed_operands[] =
10048 {
10049   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10050   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10051   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
10052   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
10053   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10054   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10055   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10056   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10057   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10058   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10059   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10060   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10061   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10062   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10063   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10064   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10065   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10066   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10067   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10068   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10069   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10070 };
10071
10072 #define MIPS16_NUM_IMMED \
10073   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10074
10075 /* Handle a mips16 instruction with an immediate value.  This or's the
10076    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
10077    whether an extended value is needed; if one is needed, it sets
10078    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
10079    If SMALL is true, an unextended opcode was explicitly requested.
10080    If EXT is true, an extended opcode was explicitly requested.  If
10081    WARN is true, warn if EXT does not match reality.  */
10082
10083 static void
10084 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10085               bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10086               unsigned long *insn, bfd_boolean *use_extend,
10087               unsigned short *extend)
10088 {
10089   register const struct mips16_immed_operand *op;
10090   int mintiny, maxtiny;
10091   bfd_boolean needext;
10092
10093   op = mips16_immed_operands;
10094   while (op->type != type)
10095     {
10096       ++op;
10097       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10098     }
10099
10100   if (op->unsp)
10101     {
10102       if (type == '<' || type == '>' || type == '[' || type == ']')
10103         {
10104           mintiny = 1;
10105           maxtiny = 1 << op->nbits;
10106         }
10107       else
10108         {
10109           mintiny = 0;
10110           maxtiny = (1 << op->nbits) - 1;
10111         }
10112     }
10113   else
10114     {
10115       mintiny = - (1 << (op->nbits - 1));
10116       maxtiny = (1 << (op->nbits - 1)) - 1;
10117     }
10118
10119   /* Branch offsets have an implicit 0 in the lowest bit.  */
10120   if (type == 'p' || type == 'q')
10121     val /= 2;
10122
10123   if ((val & ((1 << op->shift) - 1)) != 0
10124       || val < (mintiny << op->shift)
10125       || val > (maxtiny << op->shift))
10126     needext = TRUE;
10127   else
10128     needext = FALSE;
10129
10130   if (warn && ext && ! needext)
10131     as_warn_where (file, line,
10132                    _("extended operand requested but not required"));
10133   if (small && needext)
10134     as_bad_where (file, line, _("invalid unextended operand value"));
10135
10136   if (small || (! ext && ! needext))
10137     {
10138       int insnval;
10139
10140       *use_extend = FALSE;
10141       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10142       insnval <<= op->op_shift;
10143       *insn |= insnval;
10144     }
10145   else
10146     {
10147       long minext, maxext;
10148       int extval;
10149
10150       if (op->extu)
10151         {
10152           minext = 0;
10153           maxext = (1 << op->extbits) - 1;
10154         }
10155       else
10156         {
10157           minext = - (1 << (op->extbits - 1));
10158           maxext = (1 << (op->extbits - 1)) - 1;
10159         }
10160       if (val < minext || val > maxext)
10161         as_bad_where (file, line,
10162                       _("operand value out of range for instruction"));
10163
10164       *use_extend = TRUE;
10165       if (op->extbits == 16)
10166         {
10167           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10168           val &= 0x1f;
10169         }
10170       else if (op->extbits == 15)
10171         {
10172           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10173           val &= 0xf;
10174         }
10175       else
10176         {
10177           extval = ((val & 0x1f) << 6) | (val & 0x20);
10178           val = 0;
10179         }
10180
10181       *extend = (unsigned short) extval;
10182       *insn |= val;
10183     }
10184 }
10185 \f
10186 static const struct percent_op_match
10187 {
10188   const char *str;
10189   bfd_reloc_code_real_type reloc;
10190 } percent_op[] =
10191 {
10192   {"%lo", BFD_RELOC_LO16},
10193 #ifdef OBJ_ELF
10194   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10195   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10196   {"%call16", BFD_RELOC_MIPS_CALL16},
10197   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10198   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10199   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10200   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10201   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10202   {"%got", BFD_RELOC_MIPS_GOT16},
10203   {"%gp_rel", BFD_RELOC_GPREL16},
10204   {"%half", BFD_RELOC_16},
10205   {"%highest", BFD_RELOC_MIPS_HIGHEST},
10206   {"%higher", BFD_RELOC_MIPS_HIGHER},
10207   {"%neg", BFD_RELOC_MIPS_SUB},
10208 #endif
10209   {"%hi", BFD_RELOC_HI16_S}
10210 };
10211
10212
10213 /* Return true if *STR points to a relocation operator.  When returning true,
10214    move *STR over the operator and store its relocation code in *RELOC.
10215    Leave both *STR and *RELOC alone when returning false.  */
10216
10217 static bfd_boolean
10218 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10219 {
10220   size_t i;
10221
10222   for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10223     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10224       {
10225         *str += strlen (percent_op[i].str);
10226         *reloc = percent_op[i].reloc;
10227
10228         /* Check whether the output BFD supports this relocation.
10229            If not, issue an error and fall back on something safe.  */
10230         if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10231           {
10232             as_bad ("relocation %s isn't supported by the current ABI",
10233                     percent_op[i].str);
10234             *reloc = BFD_RELOC_LO16;
10235           }
10236         return TRUE;
10237       }
10238   return FALSE;
10239 }
10240
10241
10242 /* Parse string STR as a 16-bit relocatable operand.  Store the
10243    expression in *EP and the relocations in the array starting
10244    at RELOC.  Return the number of relocation operators used.
10245
10246    On exit, EXPR_END points to the first character after the expression.
10247    If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16.  */
10248
10249 static size_t
10250 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10251                        char *str)
10252 {
10253   bfd_reloc_code_real_type reversed_reloc[3];
10254   size_t reloc_index, i;
10255   int crux_depth, str_depth;
10256   char *crux;
10257
10258   /* Search for the start of the main expression, recoding relocations
10259      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
10260      of the main expression and with CRUX_DEPTH containing the number
10261      of open brackets at that point.  */
10262   reloc_index = -1;
10263   str_depth = 0;
10264   do
10265     {
10266       reloc_index++;
10267       crux = str;
10268       crux_depth = str_depth;
10269
10270       /* Skip over whitespace and brackets, keeping count of the number
10271          of brackets.  */
10272       while (*str == ' ' || *str == '\t' || *str == '(')
10273         if (*str++ == '(')
10274           str_depth++;
10275     }
10276   while (*str == '%'
10277          && reloc_index < (HAVE_NEWABI ? 3 : 1)
10278          && parse_relocation (&str, &reversed_reloc[reloc_index]));
10279
10280   my_getExpression (ep, crux);
10281   str = expr_end;
10282
10283   /* Match every open bracket.  */
10284   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10285     if (*str++ == ')')
10286       crux_depth--;
10287
10288   if (crux_depth > 0)
10289     as_bad ("unclosed '('");
10290
10291   expr_end = str;
10292
10293   if (reloc_index == 0)
10294     reloc[0] = BFD_RELOC_LO16;
10295   else
10296     {
10297       prev_reloc_op_frag = frag_now;
10298       for (i = 0; i < reloc_index; i++)
10299         reloc[i] = reversed_reloc[reloc_index - 1 - i];
10300     }
10301
10302   return reloc_index;
10303 }
10304
10305 static void
10306 my_getExpression (expressionS *ep, char *str)
10307 {
10308   char *save_in;
10309   valueT val;
10310
10311   save_in = input_line_pointer;
10312   input_line_pointer = str;
10313   expression (ep);
10314   expr_end = input_line_pointer;
10315   input_line_pointer = save_in;
10316
10317   /* If we are in mips16 mode, and this is an expression based on `.',
10318      then we bump the value of the symbol by 1 since that is how other
10319      text symbols are handled.  We don't bother to handle complex
10320      expressions, just `.' plus or minus a constant.  */
10321   if (mips_opts.mips16
10322       && ep->X_op == O_symbol
10323       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10324       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10325       && symbol_get_frag (ep->X_add_symbol) == frag_now
10326       && symbol_constant_p (ep->X_add_symbol)
10327       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10328     S_SET_VALUE (ep->X_add_symbol, val + 1);
10329 }
10330
10331 /* Turn a string in input_line_pointer into a floating point constant
10332    of type TYPE, and store the appropriate bytes in *LITP.  The number
10333    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
10334    returned, or NULL on OK.  */
10335
10336 char *
10337 md_atof (int type, char *litP, int *sizeP)
10338 {
10339   int prec;
10340   LITTLENUM_TYPE words[4];
10341   char *t;
10342   int i;
10343
10344   switch (type)
10345     {
10346     case 'f':
10347       prec = 2;
10348       break;
10349
10350     case 'd':
10351       prec = 4;
10352       break;
10353
10354     default:
10355       *sizeP = 0;
10356       return _("bad call to md_atof");
10357     }
10358
10359   t = atof_ieee (input_line_pointer, type, words);
10360   if (t)
10361     input_line_pointer = t;
10362
10363   *sizeP = prec * 2;
10364
10365   if (! target_big_endian)
10366     {
10367       for (i = prec - 1; i >= 0; i--)
10368         {
10369           md_number_to_chars (litP, words[i], 2);
10370           litP += 2;
10371         }
10372     }
10373   else
10374     {
10375       for (i = 0; i < prec; i++)
10376         {
10377           md_number_to_chars (litP, words[i], 2);
10378           litP += 2;
10379         }
10380     }
10381
10382   return NULL;
10383 }
10384
10385 void
10386 md_number_to_chars (char *buf, valueT val, int n)
10387 {
10388   if (target_big_endian)
10389     number_to_chars_bigendian (buf, val, n);
10390   else
10391     number_to_chars_littleendian (buf, val, n);
10392 }
10393 \f
10394 #ifdef OBJ_ELF
10395 static int support_64bit_objects(void)
10396 {
10397   const char **list, **l;
10398   int yes;
10399
10400   list = bfd_target_list ();
10401   for (l = list; *l != NULL; l++)
10402 #ifdef TE_TMIPS
10403     /* This is traditional mips */
10404     if (strcmp (*l, "elf64-tradbigmips") == 0
10405         || strcmp (*l, "elf64-tradlittlemips") == 0)
10406 #else
10407     if (strcmp (*l, "elf64-bigmips") == 0
10408         || strcmp (*l, "elf64-littlemips") == 0)
10409 #endif
10410       break;
10411   yes = (*l != NULL);
10412   free (list);
10413   return yes;
10414 }
10415 #endif /* OBJ_ELF */
10416
10417 const char *md_shortopts = "nO::g::G:";
10418
10419 struct option md_longopts[] =
10420 {
10421   /* Options which specify architecture.  */
10422 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
10423 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10424   {"march", required_argument, NULL, OPTION_MARCH},
10425 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10426   {"mtune", required_argument, NULL, OPTION_MTUNE},
10427 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10428   {"mips0", no_argument, NULL, OPTION_MIPS1},
10429   {"mips1", no_argument, NULL, OPTION_MIPS1},
10430 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10431   {"mips2", no_argument, NULL, OPTION_MIPS2},
10432 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10433   {"mips3", no_argument, NULL, OPTION_MIPS3},
10434 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10435   {"mips4", no_argument, NULL, OPTION_MIPS4},
10436 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10437   {"mips5", no_argument, NULL, OPTION_MIPS5},
10438 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10439   {"mips32", no_argument, NULL, OPTION_MIPS32},
10440 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10441   {"mips64", no_argument, NULL, OPTION_MIPS64},
10442 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10443   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10444 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10445   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10446
10447   /* Options which specify Application Specific Extensions (ASEs).  */
10448 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10449 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10450   {"mips16", no_argument, NULL, OPTION_MIPS16},
10451 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10452   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10453 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10454   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10455 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10456   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10457 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10458   {"mdmx", no_argument, NULL, OPTION_MDMX},
10459 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10460   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10461
10462   /* Old-style architecture options.  Don't add more of these.  */
10463 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10464 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10465   {"m4650", no_argument, NULL, OPTION_M4650},
10466 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10467   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10468 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10469   {"m4010", no_argument, NULL, OPTION_M4010},
10470 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10471   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10472 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10473   {"m4100", no_argument, NULL, OPTION_M4100},
10474 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10475   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10476 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10477   {"m3900", no_argument, NULL, OPTION_M3900},
10478 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10479   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10480
10481   /* Options which enable bug fixes.  */
10482 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10483 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10484   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10485 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10486   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10487   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10488 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10489 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10490   {"mfix-vr4122-bugs",    no_argument, NULL, OPTION_FIX_VR4122},
10491   {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10492
10493   /* Miscellaneous options.  */
10494 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10495 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10496   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10497 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10498   {"trap", no_argument, NULL, OPTION_TRAP},
10499   {"no-break", no_argument, NULL, OPTION_TRAP},
10500 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10501   {"break", no_argument, NULL, OPTION_BREAK},
10502   {"no-trap", no_argument, NULL, OPTION_BREAK},
10503 #define OPTION_EB (OPTION_MISC_BASE + 3)
10504   {"EB", no_argument, NULL, OPTION_EB},
10505 #define OPTION_EL (OPTION_MISC_BASE + 4)
10506   {"EL", no_argument, NULL, OPTION_EL},
10507 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10508   {"mfp32", no_argument, NULL, OPTION_FP32},
10509 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10510   {"mgp32", no_argument, NULL, OPTION_GP32},
10511 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10512   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10513 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10514   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10515 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10516   {"mfp64", no_argument, NULL, OPTION_FP64},
10517 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10518   {"mgp64", no_argument, NULL, OPTION_GP64},
10519 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10520 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10521   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10522   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10523
10524   /* ELF-specific options.  */
10525 #ifdef OBJ_ELF
10526 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 13)
10527 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10528   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10529   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10530 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10531   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10532 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10533   {"xgot",        no_argument, NULL, OPTION_XGOT},
10534 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10535   {"mabi", required_argument, NULL, OPTION_MABI},
10536 #define OPTION_32          (OPTION_ELF_BASE + 4)
10537   {"32",          no_argument, NULL, OPTION_32},
10538 #define OPTION_N32         (OPTION_ELF_BASE + 5)
10539   {"n32",         no_argument, NULL, OPTION_N32},
10540 #define OPTION_64          (OPTION_ELF_BASE + 6)
10541   {"64",          no_argument, NULL, OPTION_64},
10542 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10543   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10544 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10545   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10546 #define OPTION_PDR         (OPTION_ELF_BASE + 9)
10547   {"mpdr", no_argument, NULL, OPTION_PDR},
10548 #define OPTION_NO_PDR      (OPTION_ELF_BASE + 10)
10549   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10550 #endif /* OBJ_ELF */
10551
10552   {NULL, no_argument, NULL, 0}
10553 };
10554 size_t md_longopts_size = sizeof (md_longopts);
10555
10556 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10557    NEW_VALUE.  Warn if another value was already specified.  Note:
10558    we have to defer parsing the -march and -mtune arguments in order
10559    to handle 'from-abi' correctly, since the ABI might be specified
10560    in a later argument.  */
10561
10562 static void
10563 mips_set_option_string (const char **string_ptr, const char *new_value)
10564 {
10565   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10566     as_warn (_("A different %s was already specified, is now %s"),
10567              string_ptr == &mips_arch_string ? "-march" : "-mtune",
10568              new_value);
10569
10570   *string_ptr = new_value;
10571 }
10572
10573 int
10574 md_parse_option (int c, char *arg)
10575 {
10576   switch (c)
10577     {
10578     case OPTION_CONSTRUCT_FLOATS:
10579       mips_disable_float_construction = 0;
10580       break;
10581
10582     case OPTION_NO_CONSTRUCT_FLOATS:
10583       mips_disable_float_construction = 1;
10584       break;
10585
10586     case OPTION_TRAP:
10587       mips_trap = 1;
10588       break;
10589
10590     case OPTION_BREAK:
10591       mips_trap = 0;
10592       break;
10593
10594     case OPTION_EB:
10595       target_big_endian = 1;
10596       break;
10597
10598     case OPTION_EL:
10599       target_big_endian = 0;
10600       break;
10601
10602     case 'n':
10603       warn_nops = 1;
10604       break;
10605
10606     case 'O':
10607       if (arg && arg[1] == '0')
10608         mips_optimize = 1;
10609       else
10610         mips_optimize = 2;
10611       break;
10612
10613     case 'g':
10614       if (arg == NULL)
10615         mips_debug = 2;
10616       else
10617         mips_debug = atoi (arg);
10618       /* When the MIPS assembler sees -g or -g2, it does not do
10619          optimizations which limit full symbolic debugging.  We take
10620          that to be equivalent to -O0.  */
10621       if (mips_debug == 2)
10622         mips_optimize = 1;
10623       break;
10624
10625     case OPTION_MIPS1:
10626       file_mips_isa = ISA_MIPS1;
10627       break;
10628
10629     case OPTION_MIPS2:
10630       file_mips_isa = ISA_MIPS2;
10631       break;
10632
10633     case OPTION_MIPS3:
10634       file_mips_isa = ISA_MIPS3;
10635       break;
10636
10637     case OPTION_MIPS4:
10638       file_mips_isa = ISA_MIPS4;
10639       break;
10640
10641     case OPTION_MIPS5:
10642       file_mips_isa = ISA_MIPS5;
10643       break;
10644
10645     case OPTION_MIPS32:
10646       file_mips_isa = ISA_MIPS32;
10647       break;
10648
10649     case OPTION_MIPS32R2:
10650       file_mips_isa = ISA_MIPS32R2;
10651       break;
10652
10653     case OPTION_MIPS64R2:
10654       file_mips_isa = ISA_MIPS64R2;
10655       break;
10656
10657     case OPTION_MIPS64:
10658       file_mips_isa = ISA_MIPS64;
10659       break;
10660
10661     case OPTION_MTUNE:
10662       mips_set_option_string (&mips_tune_string, arg);
10663       break;
10664
10665     case OPTION_MARCH:
10666       mips_set_option_string (&mips_arch_string, arg);
10667       break;
10668
10669     case OPTION_M4650:
10670       mips_set_option_string (&mips_arch_string, "4650");
10671       mips_set_option_string (&mips_tune_string, "4650");
10672       break;
10673
10674     case OPTION_NO_M4650:
10675       break;
10676
10677     case OPTION_M4010:
10678       mips_set_option_string (&mips_arch_string, "4010");
10679       mips_set_option_string (&mips_tune_string, "4010");
10680       break;
10681
10682     case OPTION_NO_M4010:
10683       break;
10684
10685     case OPTION_M4100:
10686       mips_set_option_string (&mips_arch_string, "4100");
10687       mips_set_option_string (&mips_tune_string, "4100");
10688       break;
10689
10690     case OPTION_NO_M4100:
10691       break;
10692
10693     case OPTION_M3900:
10694       mips_set_option_string (&mips_arch_string, "3900");
10695       mips_set_option_string (&mips_tune_string, "3900");
10696       break;
10697
10698     case OPTION_NO_M3900:
10699       break;
10700
10701     case OPTION_MDMX:
10702       mips_opts.ase_mdmx = 1;
10703       break;
10704
10705     case OPTION_NO_MDMX:
10706       mips_opts.ase_mdmx = 0;
10707       break;
10708
10709     case OPTION_MIPS16:
10710       mips_opts.mips16 = 1;
10711       mips_no_prev_insn (FALSE);
10712       break;
10713
10714     case OPTION_NO_MIPS16:
10715       mips_opts.mips16 = 0;
10716       mips_no_prev_insn (FALSE);
10717       break;
10718
10719     case OPTION_MIPS3D:
10720       mips_opts.ase_mips3d = 1;
10721       break;
10722
10723     case OPTION_NO_MIPS3D:
10724       mips_opts.ase_mips3d = 0;
10725       break;
10726
10727     case OPTION_MEMBEDDED_PIC:
10728       mips_pic = EMBEDDED_PIC;
10729       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10730         {
10731           as_bad (_("-G may not be used with embedded PIC code"));
10732           return 0;
10733         }
10734       g_switch_value = 0x7fffffff;
10735       break;
10736
10737     case OPTION_FIX_VR4122:
10738       mips_fix_4122_bugs = 1;
10739       break;
10740
10741     case OPTION_NO_FIX_VR4122:
10742       mips_fix_4122_bugs = 0;
10743       break;
10744
10745     case OPTION_RELAX_BRANCH:
10746       mips_relax_branch = 1;
10747       break;
10748
10749     case OPTION_NO_RELAX_BRANCH:
10750       mips_relax_branch = 0;
10751       break;
10752
10753 #ifdef OBJ_ELF
10754       /* When generating ELF code, we permit -KPIC and -call_shared to
10755          select SVR4_PIC, and -non_shared to select no PIC.  This is
10756          intended to be compatible with Irix 5.  */
10757     case OPTION_CALL_SHARED:
10758       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10759         {
10760           as_bad (_("-call_shared is supported only for ELF format"));
10761           return 0;
10762         }
10763       mips_pic = SVR4_PIC;
10764       mips_abicalls = TRUE;
10765       if (g_switch_seen && g_switch_value != 0)
10766         {
10767           as_bad (_("-G may not be used with SVR4 PIC code"));
10768           return 0;
10769         }
10770       g_switch_value = 0;
10771       break;
10772
10773     case OPTION_NON_SHARED:
10774       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10775         {
10776           as_bad (_("-non_shared is supported only for ELF format"));
10777           return 0;
10778         }
10779       mips_pic = NO_PIC;
10780       mips_abicalls = FALSE;
10781       break;
10782
10783       /* The -xgot option tells the assembler to use 32 offsets when
10784          accessing the got in SVR4_PIC mode.  It is for Irix
10785          compatibility.  */
10786     case OPTION_XGOT:
10787       mips_big_got = 1;
10788       break;
10789 #endif /* OBJ_ELF */
10790
10791     case 'G':
10792       if (! USE_GLOBAL_POINTER_OPT)
10793         {
10794           as_bad (_("-G is not supported for this configuration"));
10795           return 0;
10796         }
10797       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10798         {
10799           as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10800           return 0;
10801         }
10802       else
10803         g_switch_value = atoi (arg);
10804       g_switch_seen = 1;
10805       break;
10806
10807 #ifdef OBJ_ELF
10808       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10809          and -mabi=64.  */
10810     case OPTION_32:
10811       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10812         {
10813           as_bad (_("-32 is supported for ELF format only"));
10814           return 0;
10815         }
10816       mips_abi = O32_ABI;
10817       break;
10818
10819     case OPTION_N32:
10820       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10821         {
10822           as_bad (_("-n32 is supported for ELF format only"));
10823           return 0;
10824         }
10825       mips_abi = N32_ABI;
10826       break;
10827
10828     case OPTION_64:
10829       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10830         {
10831           as_bad (_("-64 is supported for ELF format only"));
10832           return 0;
10833         }
10834       mips_abi = N64_ABI;
10835       if (! support_64bit_objects())
10836         as_fatal (_("No compiled in support for 64 bit object file format"));
10837       break;
10838 #endif /* OBJ_ELF */
10839
10840     case OPTION_GP32:
10841       file_mips_gp32 = 1;
10842       break;
10843
10844     case OPTION_GP64:
10845       file_mips_gp32 = 0;
10846       break;
10847
10848     case OPTION_FP32:
10849       file_mips_fp32 = 1;
10850       break;
10851
10852     case OPTION_FP64:
10853       file_mips_fp32 = 0;
10854       break;
10855
10856 #ifdef OBJ_ELF
10857     case OPTION_MABI:
10858       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10859         {
10860           as_bad (_("-mabi is supported for ELF format only"));
10861           return 0;
10862         }
10863       if (strcmp (arg, "32") == 0)
10864         mips_abi = O32_ABI;
10865       else if (strcmp (arg, "o64") == 0)
10866         mips_abi = O64_ABI;
10867       else if (strcmp (arg, "n32") == 0)
10868         mips_abi = N32_ABI;
10869       else if (strcmp (arg, "64") == 0)
10870         {
10871           mips_abi = N64_ABI;
10872           if (! support_64bit_objects())
10873             as_fatal (_("No compiled in support for 64 bit object file "
10874                         "format"));
10875         }
10876       else if (strcmp (arg, "eabi") == 0)
10877         mips_abi = EABI_ABI;
10878       else
10879         {
10880           as_fatal (_("invalid abi -mabi=%s"), arg);
10881           return 0;
10882         }
10883       break;
10884 #endif /* OBJ_ELF */
10885
10886     case OPTION_M7000_HILO_FIX:
10887       mips_7000_hilo_fix = TRUE;
10888       break;
10889
10890     case OPTION_MNO_7000_HILO_FIX:
10891       mips_7000_hilo_fix = FALSE;
10892       break;
10893
10894 #ifdef OBJ_ELF
10895     case OPTION_MDEBUG:
10896       mips_flag_mdebug = TRUE;
10897       break;
10898
10899     case OPTION_NO_MDEBUG:
10900       mips_flag_mdebug = FALSE;
10901       break;
10902
10903     case OPTION_PDR:
10904       mips_flag_pdr = TRUE;
10905       break;
10906
10907     case OPTION_NO_PDR:
10908       mips_flag_pdr = FALSE;
10909       break;
10910 #endif /* OBJ_ELF */
10911
10912     default:
10913       return 0;
10914     }
10915
10916   return 1;
10917 }
10918 \f
10919 /* Set up globals to generate code for the ISA or processor
10920    described by INFO.  */
10921
10922 static void
10923 mips_set_architecture (const struct mips_cpu_info *info)
10924 {
10925   if (info != 0)
10926     {
10927       file_mips_arch = info->cpu;
10928       mips_opts.arch = info->cpu;
10929       mips_opts.isa = info->isa;
10930     }
10931 }
10932
10933
10934 /* Likewise for tuning.  */
10935
10936 static void
10937 mips_set_tune (const struct mips_cpu_info *info)
10938 {
10939   if (info != 0)
10940     mips_tune = info->cpu;
10941 }
10942
10943
10944 void
10945 mips_after_parse_args (void)
10946 {
10947   const struct mips_cpu_info *arch_info = 0;
10948   const struct mips_cpu_info *tune_info = 0;
10949
10950   /* GP relative stuff not working for PE */
10951   if (strncmp (TARGET_OS, "pe", 2) == 0
10952       && g_switch_value != 0)
10953     {
10954       if (g_switch_seen)
10955         as_bad (_("-G not supported in this configuration."));
10956       g_switch_value = 0;
10957     }
10958
10959   if (mips_abi == NO_ABI)
10960     mips_abi = MIPS_DEFAULT_ABI;
10961
10962   /* The following code determines the architecture and register size.
10963      Similar code was added to GCC 3.3 (see override_options() in
10964      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10965      as much as possible.  */
10966
10967   if (mips_arch_string != 0)
10968     arch_info = mips_parse_cpu ("-march", mips_arch_string);
10969
10970   if (file_mips_isa != ISA_UNKNOWN)
10971     {
10972       /* Handle -mipsN.  At this point, file_mips_isa contains the
10973          ISA level specified by -mipsN, while arch_info->isa contains
10974          the -march selection (if any).  */
10975       if (arch_info != 0)
10976         {
10977           /* -march takes precedence over -mipsN, since it is more descriptive.
10978              There's no harm in specifying both as long as the ISA levels
10979              are the same.  */
10980           if (file_mips_isa != arch_info->isa)
10981             as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10982                     mips_cpu_info_from_isa (file_mips_isa)->name,
10983                     mips_cpu_info_from_isa (arch_info->isa)->name);
10984         }
10985       else
10986         arch_info = mips_cpu_info_from_isa (file_mips_isa);
10987     }
10988
10989   if (arch_info == 0)
10990     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10991
10992   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10993     as_bad ("-march=%s is not compatible with the selected ABI",
10994             arch_info->name);
10995
10996   mips_set_architecture (arch_info);
10997
10998   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
10999   if (mips_tune_string != 0)
11000     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11001
11002   if (tune_info == 0)
11003     mips_set_tune (arch_info);
11004   else
11005     mips_set_tune (tune_info);
11006
11007   if (file_mips_gp32 >= 0)
11008     {
11009       /* The user specified the size of the integer registers.  Make sure
11010          it agrees with the ABI and ISA.  */
11011       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11012         as_bad (_("-mgp64 used with a 32-bit processor"));
11013       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11014         as_bad (_("-mgp32 used with a 64-bit ABI"));
11015       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11016         as_bad (_("-mgp64 used with a 32-bit ABI"));
11017     }
11018   else
11019     {
11020       /* Infer the integer register size from the ABI and processor.
11021          Restrict ourselves to 32-bit registers if that's all the
11022          processor has, or if the ABI cannot handle 64-bit registers.  */
11023       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11024                         || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11025     }
11026
11027   /* ??? GAS treats single-float processors as though they had 64-bit
11028      float registers (although it complains when double-precision
11029      instructions are used).  As things stand, saying they have 32-bit
11030      registers would lead to spurious "register must be even" messages.
11031      So here we assume float registers are always the same size as
11032      integer ones, unless the user says otherwise.  */
11033   if (file_mips_fp32 < 0)
11034     file_mips_fp32 = file_mips_gp32;
11035
11036   /* End of GCC-shared inference code.  */
11037
11038   /* This flag is set when we have a 64-bit capable CPU but use only
11039      32-bit wide registers.  Note that EABI does not use it.  */
11040   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11041       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11042           || mips_abi == O32_ABI))
11043     mips_32bitmode = 1;
11044
11045   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11046     as_bad (_("trap exception not supported at ISA 1"));
11047
11048   /* If the selected architecture includes support for ASEs, enable
11049      generation of code for them.  */
11050   if (mips_opts.mips16 == -1)
11051     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11052   if (mips_opts.ase_mips3d == -1)
11053     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
11054   if (mips_opts.ase_mdmx == -1)
11055     mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
11056
11057   file_mips_isa = mips_opts.isa;
11058   file_ase_mips16 = mips_opts.mips16;
11059   file_ase_mips3d = mips_opts.ase_mips3d;
11060   file_ase_mdmx = mips_opts.ase_mdmx;
11061   mips_opts.gp32 = file_mips_gp32;
11062   mips_opts.fp32 = file_mips_fp32;
11063
11064   if (mips_flag_mdebug < 0)
11065     {
11066 #ifdef OBJ_MAYBE_ECOFF
11067       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11068         mips_flag_mdebug = 1;
11069       else
11070 #endif /* OBJ_MAYBE_ECOFF */
11071         mips_flag_mdebug = 0;
11072     }
11073 }
11074 \f
11075 void
11076 mips_init_after_args (void)
11077 {
11078   /* initialize opcodes */
11079   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11080   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11081 }
11082
11083 long
11084 md_pcrel_from (fixS *fixP)
11085 {
11086   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11087   switch (fixP->fx_r_type)
11088     {
11089     case BFD_RELOC_16_PCREL_S2:
11090     case BFD_RELOC_MIPS_JMP:
11091       /* Return the address of the delay slot.  */
11092       return addr + 4;
11093     default:
11094       return addr;
11095     }
11096 }
11097
11098 /* This is called before the symbol table is processed.  In order to
11099    work with gcc when using mips-tfile, we must keep all local labels.
11100    However, in other cases, we want to discard them.  If we were
11101    called with -g, but we didn't see any debugging information, it may
11102    mean that gcc is smuggling debugging information through to
11103    mips-tfile, in which case we must generate all local labels.  */
11104
11105 void
11106 mips_frob_file_before_adjust (void)
11107 {
11108 #ifndef NO_ECOFF_DEBUGGING
11109   if (ECOFF_DEBUGGING
11110       && mips_debug != 0
11111       && ! ecoff_debugging_seen)
11112     flag_keep_locals = 1;
11113 #endif
11114 }
11115
11116 /* Sort any unmatched HI16_S relocs so that they immediately precede
11117    the corresponding LO reloc.  This is called before md_apply_fix3 and
11118    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
11119    explicit use of the %hi modifier.  */
11120
11121 void
11122 mips_frob_file (void)
11123 {
11124   struct mips_hi_fixup *l;
11125
11126   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11127     {
11128       segment_info_type *seginfo;
11129       int pass;
11130
11131       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11132
11133       /* If a GOT16 relocation turns out to be against a global symbol,
11134          there isn't supposed to be a matching LO.  */
11135       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11136           && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11137         continue;
11138
11139       /* Check quickly whether the next fixup happens to be a matching %lo.  */
11140       if (fixup_has_matching_lo_p (l->fixp))
11141         continue;
11142
11143       /* Look through the fixups for this segment for a matching %lo.
11144          When we find one, move the %hi just in front of it.  We do
11145          this in two passes.  In the first pass, we try to find a
11146          unique %lo.  In the second pass, we permit multiple %hi
11147          relocs for a single %lo (this is a GNU extension).  */
11148       seginfo = seg_info (l->seg);
11149       for (pass = 0; pass < 2; pass++)
11150         {
11151           fixS *f, *prev;
11152
11153           prev = NULL;
11154           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11155             {
11156               /* Check whether this is a %lo fixup which matches l->fixp.  */
11157               if (f->fx_r_type == BFD_RELOC_LO16
11158                   && f->fx_addsy == l->fixp->fx_addsy
11159                   && f->fx_offset == l->fixp->fx_offset
11160                   && (pass == 1
11161                       || prev == NULL
11162                       || !reloc_needs_lo_p (prev->fx_r_type)
11163                       || !fixup_has_matching_lo_p (prev)))
11164                 {
11165                   fixS **pf;
11166
11167                   /* Move l->fixp before f.  */
11168                   for (pf = &seginfo->fix_root;
11169                        *pf != l->fixp;
11170                        pf = &(*pf)->fx_next)
11171                     assert (*pf != NULL);
11172
11173                   *pf = l->fixp->fx_next;
11174
11175                   l->fixp->fx_next = f;
11176                   if (prev == NULL)
11177                     seginfo->fix_root = l->fixp;
11178                   else
11179                     prev->fx_next = l->fixp;
11180
11181                   break;
11182                 }
11183
11184               prev = f;
11185             }
11186
11187           if (f != NULL)
11188             break;
11189
11190 #if 0 /* GCC code motion plus incomplete dead code elimination
11191          can leave a %hi without a %lo.  */
11192           if (pass == 1)
11193             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11194                            _("Unmatched %%hi reloc"));
11195 #endif
11196         }
11197     }
11198 }
11199
11200 /* When generating embedded PIC code we need to use a special
11201    relocation to represent the difference of two symbols in the .text
11202    section (switch tables use a difference of this sort).  See
11203    include/coff/mips.h for details.  This macro checks whether this
11204    fixup requires the special reloc.  */
11205 #define SWITCH_TABLE(fixp) \
11206   ((fixp)->fx_r_type == BFD_RELOC_32 \
11207    && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11208    && (fixp)->fx_addsy != NULL \
11209    && (fixp)->fx_subsy != NULL \
11210    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11211    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11212
11213 /* When generating embedded PIC code we must keep all PC relative
11214    relocations, in case the linker has to relax a call.  We also need
11215    to keep relocations for switch table entries.
11216
11217    We may have combined relocations without symbols in the N32/N64 ABI.
11218    We have to prevent gas from dropping them.  */
11219
11220 int
11221 mips_force_relocation (fixS *fixp)
11222 {
11223   if (generic_force_reloc (fixp))
11224     return 1;
11225
11226   if (HAVE_NEWABI
11227       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11228       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11229           || fixp->fx_r_type == BFD_RELOC_HI16_S
11230           || fixp->fx_r_type == BFD_RELOC_LO16))
11231     return 1;
11232
11233   return (mips_pic == EMBEDDED_PIC
11234           && (fixp->fx_pcrel
11235               || SWITCH_TABLE (fixp)
11236               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11237               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11238 }
11239
11240 /* This hook is called before a fix is simplified.  We don't really
11241    decide whether to skip a fix here.  Rather, we turn global symbols
11242    used as branch targets into local symbols, such that they undergo
11243    simplification.  We can only do this if the symbol is defined and
11244    it is in the same section as the branch.  If this doesn't hold, we
11245    emit a better error message than just saying the relocation is not
11246    valid for the selected object format.
11247
11248    FIXP is the fix-up we're going to try to simplify, SEG is the
11249    segment in which the fix up occurs.  The return value should be
11250    non-zero to indicate the fix-up is valid for further
11251    simplifications.  */
11252
11253 int
11254 mips_validate_fix (struct fix *fixP, asection *seg)
11255 {
11256   /* There's a lot of discussion on whether it should be possible to
11257      use R_MIPS_PC16 to represent branch relocations.  The outcome
11258      seems to be that it can, but gas/bfd are very broken in creating
11259      RELA relocations for this, so for now we only accept branches to
11260      symbols in the same section.  Anything else is of dubious value,
11261      since there's no guarantee that at link time the symbol would be
11262      in range.  Even for branches to local symbols this is arguably
11263      wrong, since it we assume the symbol is not going to be
11264      overridden, which should be possible per ELF library semantics,
11265      but then, there isn't a dynamic relocation that could be used to
11266      this effect, and the target would likely be out of range as well.
11267
11268      Unfortunately, it seems that there is too much code out there
11269      that relies on branches to symbols that are global to be resolved
11270      as if they were local, like the IRIX tools do, so we do it as
11271      well, but with a warning so that people are reminded to fix their
11272      code.  If we ever get back to using R_MIPS_PC16 for branch
11273      targets, this entire block should go away (and probably the
11274      whole function).  */
11275
11276   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11277       && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11278             || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11279            && mips_pic != EMBEDDED_PIC)
11280           || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11281       && fixP->fx_addsy)
11282     {
11283       if (! S_IS_DEFINED (fixP->fx_addsy))
11284         {
11285           as_bad_where (fixP->fx_file, fixP->fx_line,
11286                         _("Cannot branch to undefined symbol."));
11287           /* Avoid any further errors about this fixup.  */
11288           fixP->fx_done = 1;
11289         }
11290       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11291         {
11292           as_bad_where (fixP->fx_file, fixP->fx_line,
11293                         _("Cannot branch to symbol in another section."));
11294           fixP->fx_done = 1;
11295         }
11296       else if (S_IS_EXTERNAL (fixP->fx_addsy))
11297         {
11298           symbolS *sym = fixP->fx_addsy;
11299
11300           if (mips_pic == SVR4_PIC)
11301             as_warn_where (fixP->fx_file, fixP->fx_line,
11302                            _("Pretending global symbol used as branch target is local."));
11303
11304           fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11305                                           S_GET_SEGMENT (sym),
11306                                           S_GET_VALUE (sym),
11307                                           symbol_get_frag (sym));
11308           copy_symbol_attributes (fixP->fx_addsy, sym);
11309           S_CLEAR_EXTERNAL (fixP->fx_addsy);
11310           assert (symbol_resolved_p (sym));
11311           symbol_mark_resolved (fixP->fx_addsy);
11312         }
11313     }
11314
11315   return 1;
11316 }
11317
11318 #ifdef OBJ_ELF
11319 static int
11320 mips_need_elf_addend_fixup (fixS *fixP)
11321 {
11322   if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11323     return 1;
11324   if (mips_pic == EMBEDDED_PIC
11325       && S_IS_WEAK (fixP->fx_addsy))
11326     return 1;
11327   if (mips_pic != EMBEDDED_PIC
11328       && (S_IS_WEAK (fixP->fx_addsy)
11329           || S_IS_EXTERNAL (fixP->fx_addsy))
11330       && !S_IS_COMMON (fixP->fx_addsy))
11331     return 1;
11332   if (((bfd_get_section_flags (stdoutput,
11333                                S_GET_SEGMENT (fixP->fx_addsy))
11334         & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11335       || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11336                    ".gnu.linkonce",
11337                    sizeof (".gnu.linkonce") - 1))
11338     return 1;
11339   return 0;
11340 }
11341 #endif
11342
11343 /* Apply a fixup to the object file.  */
11344
11345 void
11346 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11347 {
11348   bfd_byte *buf;
11349   long insn;
11350   static int previous_fx_r_type = 0;
11351   reloc_howto_type *howto;
11352
11353   /* We ignore generic BFD relocations we don't know about.  */
11354   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11355   if (! howto)
11356     return;
11357
11358   assert (fixP->fx_size == 4
11359           || fixP->fx_r_type == BFD_RELOC_16
11360           || fixP->fx_r_type == BFD_RELOC_64
11361           || fixP->fx_r_type == BFD_RELOC_CTOR
11362           || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11363           || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11364           || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11365
11366   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11367
11368   /* If we aren't adjusting this fixup to be against the section
11369      symbol, we need to adjust the value.  */
11370 #ifdef OBJ_ELF
11371   if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11372     {
11373       if (mips_need_elf_addend_fixup (fixP)
11374           && howto->partial_inplace
11375           && fixP->fx_r_type != BFD_RELOC_GPREL16
11376           && fixP->fx_r_type != BFD_RELOC_GPREL32
11377           && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11378         {
11379           /* In this case, the bfd_install_relocation routine will
11380              incorrectly add the symbol value back in.  We just want
11381              the addend to appear in the object file.
11382
11383              The condition above used to include
11384              "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11385
11386              However, howto can't be trusted here, because we
11387              might change the reloc type in tc_gen_reloc.  We can
11388              check howto->partial_inplace because that conversion
11389              happens to preserve howto->partial_inplace; but it
11390              does not preserve howto->pcrel_offset.  I've just
11391              eliminated the check, because all MIPS PC-relative
11392              relocations are marked howto->pcrel_offset.
11393
11394              howto->pcrel_offset was originally added for
11395              R_MIPS_PC16, which is generated for code like
11396
11397                     globl g1 .text
11398                     .text
11399                     .space 20
11400              g1:
11401              x:
11402                     bal g1
11403            */
11404           *valP -= S_GET_VALUE (fixP->fx_addsy);
11405         }
11406
11407       /* This code was generated using trial and error and so is
11408          fragile and not trustworthy.  If you change it, you should
11409          rerun the elf-rel, elf-rel2, and empic testcases and ensure
11410          they still pass.  */
11411       if (fixP->fx_pcrel)
11412         {
11413           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11414
11415           /* BFD's REL handling, for MIPS, is _very_ weird.
11416              This gives the right results, but it can't possibly
11417              be the way things are supposed to work.  */
11418           *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11419         }
11420     }
11421 #endif
11422
11423   /* We are not done if this is a composite relocation to set up gp.  */
11424   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11425       && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11426            || (fixP->fx_r_type == BFD_RELOC_64
11427                && (previous_fx_r_type == BFD_RELOC_GPREL32
11428                    || previous_fx_r_type == BFD_RELOC_GPREL16))
11429            || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11430                && (fixP->fx_r_type == BFD_RELOC_HI16_S
11431                    || fixP->fx_r_type == BFD_RELOC_LO16))))
11432     fixP->fx_done = 1;
11433   previous_fx_r_type = fixP->fx_r_type;
11434
11435   switch (fixP->fx_r_type)
11436     {
11437     case BFD_RELOC_MIPS_JMP:
11438     case BFD_RELOC_MIPS_SHIFT5:
11439     case BFD_RELOC_MIPS_SHIFT6:
11440     case BFD_RELOC_MIPS_GOT_DISP:
11441     case BFD_RELOC_MIPS_GOT_PAGE:
11442     case BFD_RELOC_MIPS_GOT_OFST:
11443     case BFD_RELOC_MIPS_SUB:
11444     case BFD_RELOC_MIPS_INSERT_A:
11445     case BFD_RELOC_MIPS_INSERT_B:
11446     case BFD_RELOC_MIPS_DELETE:
11447     case BFD_RELOC_MIPS_HIGHEST:
11448     case BFD_RELOC_MIPS_HIGHER:
11449     case BFD_RELOC_MIPS_SCN_DISP:
11450     case BFD_RELOC_MIPS_REL16:
11451     case BFD_RELOC_MIPS_RELGOT:
11452     case BFD_RELOC_MIPS_JALR:
11453     case BFD_RELOC_HI16:
11454     case BFD_RELOC_HI16_S:
11455     case BFD_RELOC_GPREL16:
11456     case BFD_RELOC_MIPS_LITERAL:
11457     case BFD_RELOC_MIPS_CALL16:
11458     case BFD_RELOC_MIPS_GOT16:
11459     case BFD_RELOC_GPREL32:
11460     case BFD_RELOC_MIPS_GOT_HI16:
11461     case BFD_RELOC_MIPS_GOT_LO16:
11462     case BFD_RELOC_MIPS_CALL_HI16:
11463     case BFD_RELOC_MIPS_CALL_LO16:
11464     case BFD_RELOC_MIPS16_GPREL:
11465       if (fixP->fx_pcrel)
11466         as_bad_where (fixP->fx_file, fixP->fx_line,
11467                       _("Invalid PC relative reloc"));
11468       /* Nothing needed to do. The value comes from the reloc entry */
11469       break;
11470
11471     case BFD_RELOC_MIPS16_JMP:
11472       /* We currently always generate a reloc against a symbol, which
11473          means that we don't want an addend even if the symbol is
11474          defined.  */
11475       *valP = 0;
11476       break;
11477
11478     case BFD_RELOC_PCREL_HI16_S:
11479       /* The addend for this is tricky if it is internal, so we just
11480          do everything here rather than in bfd_install_relocation.  */
11481       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11482         break;
11483       if (fixP->fx_addsy
11484           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11485         {
11486           /* For an external symbol adjust by the address to make it
11487              pcrel_offset.  We use the address of the RELLO reloc
11488              which follows this one.  */
11489           *valP += (fixP->fx_next->fx_frag->fr_address
11490                     + fixP->fx_next->fx_where);
11491         }
11492       *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11493       if (target_big_endian)
11494         buf += 2;
11495       md_number_to_chars (buf, *valP, 2);
11496       break;
11497
11498     case BFD_RELOC_PCREL_LO16:
11499       /* The addend for this is tricky if it is internal, so we just
11500          do everything here rather than in bfd_install_relocation.  */
11501       if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11502         break;
11503       if (fixP->fx_addsy
11504           && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11505         *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11506       if (target_big_endian)
11507         buf += 2;
11508       md_number_to_chars (buf, *valP, 2);
11509       break;
11510
11511     case BFD_RELOC_64:
11512       /* This is handled like BFD_RELOC_32, but we output a sign
11513          extended value if we are only 32 bits.  */
11514       if (fixP->fx_done
11515           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11516         {
11517           if (8 <= sizeof (valueT))
11518             md_number_to_chars (buf, *valP, 8);
11519           else
11520             {
11521               valueT hiv;
11522
11523               if ((*valP & 0x80000000) != 0)
11524                 hiv = 0xffffffff;
11525               else
11526                 hiv = 0;
11527               md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11528                                   *valP, 4);
11529               md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11530                                   hiv, 4);
11531             }
11532         }
11533       break;
11534
11535     case BFD_RELOC_RVA:
11536     case BFD_RELOC_32:
11537       /* If we are deleting this reloc entry, we must fill in the
11538          value now.  This can happen if we have a .word which is not
11539          resolved when it appears but is later defined.  We also need
11540          to fill in the value if this is an embedded PIC switch table
11541          entry.  */
11542       if (fixP->fx_done
11543           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11544         md_number_to_chars (buf, *valP, 4);
11545       break;
11546
11547     case BFD_RELOC_16:
11548       /* If we are deleting this reloc entry, we must fill in the
11549          value now.  */
11550       assert (fixP->fx_size == 2);
11551       if (fixP->fx_done)
11552         md_number_to_chars (buf, *valP, 2);
11553       break;
11554
11555     case BFD_RELOC_LO16:
11556       /* When handling an embedded PIC switch statement, we can wind
11557          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11558       if (fixP->fx_done)
11559         {
11560           if (*valP + 0x8000 > 0xffff)
11561             as_bad_where (fixP->fx_file, fixP->fx_line,
11562                           _("relocation overflow"));
11563           if (target_big_endian)
11564             buf += 2;
11565           md_number_to_chars (buf, *valP, 2);
11566         }
11567       break;
11568
11569     case BFD_RELOC_16_PCREL_S2:
11570       if ((*valP & 0x3) != 0)
11571         as_bad_where (fixP->fx_file, fixP->fx_line,
11572                       _("Branch to odd address (%lx)"), (long) *valP);
11573
11574       /*
11575        * We need to save the bits in the instruction since fixup_segment()
11576        * might be deleting the relocation entry (i.e., a branch within
11577        * the current segment).
11578        */
11579       if (! fixP->fx_done)
11580         break;
11581
11582       /* update old instruction data */
11583       if (target_big_endian)
11584         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11585       else
11586         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11587
11588       if (*valP + 0x20000 <= 0x3ffff)
11589         {
11590           insn |= (*valP >> 2) & 0xffff;
11591           md_number_to_chars (buf, insn, 4);
11592         }
11593       else if (mips_pic == NO_PIC
11594                && fixP->fx_done
11595                && fixP->fx_frag->fr_address >= text_section->vma
11596                && (fixP->fx_frag->fr_address
11597                    < text_section->vma + text_section->_raw_size)
11598                && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
11599                    || (insn & 0xffff0000) == 0x04010000  /* bgez $0 */
11600                    || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11601         {
11602           /* The branch offset is too large.  If this is an
11603              unconditional branch, and we are not generating PIC code,
11604              we can convert it to an absolute jump instruction.  */
11605           if ((insn & 0xffff0000) == 0x04110000)         /* bgezal $0 */
11606             insn = 0x0c000000;  /* jal */
11607           else
11608             insn = 0x08000000;  /* j */
11609           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11610           fixP->fx_done = 0;
11611           fixP->fx_addsy = section_symbol (text_section);
11612           *valP += md_pcrel_from (fixP);
11613           md_number_to_chars (buf, insn, 4);
11614         }
11615       else
11616         {
11617           /* If we got here, we have branch-relaxation disabled,
11618              and there's nothing we can do to fix this instruction
11619              without turning it into a longer sequence.  */
11620           as_bad_where (fixP->fx_file, fixP->fx_line,
11621                         _("Branch out of range"));
11622         }
11623       break;
11624
11625     case BFD_RELOC_VTABLE_INHERIT:
11626       fixP->fx_done = 0;
11627       if (fixP->fx_addsy
11628           && !S_IS_DEFINED (fixP->fx_addsy)
11629           && !S_IS_WEAK (fixP->fx_addsy))
11630         S_SET_WEAK (fixP->fx_addsy);
11631       break;
11632
11633     case BFD_RELOC_VTABLE_ENTRY:
11634       fixP->fx_done = 0;
11635       break;
11636
11637     default:
11638       internalError ();
11639     }
11640
11641   /* Remember value for tc_gen_reloc.  */
11642   fixP->fx_addnumber = *valP;
11643 }
11644
11645 #if 0
11646 void
11647 printInsn (unsigned long oc)
11648 {
11649   const struct mips_opcode *p;
11650   int treg, sreg, dreg, shamt;
11651   short imm;
11652   const char *args;
11653   int i;
11654
11655   for (i = 0; i < NUMOPCODES; ++i)
11656     {
11657       p = &mips_opcodes[i];
11658       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11659         {
11660           printf ("%08lx %s\t", oc, p->name);
11661           treg = (oc >> 16) & 0x1f;
11662           sreg = (oc >> 21) & 0x1f;
11663           dreg = (oc >> 11) & 0x1f;
11664           shamt = (oc >> 6) & 0x1f;
11665           imm = oc;
11666           for (args = p->args;; ++args)
11667             {
11668               switch (*args)
11669                 {
11670                 case '\0':
11671                   printf ("\n");
11672                   break;
11673
11674                 case ',':
11675                 case '(':
11676                 case ')':
11677                   printf ("%c", *args);
11678                   continue;
11679
11680                 case 'r':
11681                   assert (treg == sreg);
11682                   printf ("$%d,$%d", treg, sreg);
11683                   continue;
11684
11685                 case 'd':
11686                 case 'G':
11687                   printf ("$%d", dreg);
11688                   continue;
11689
11690                 case 't':
11691                 case 'E':
11692                   printf ("$%d", treg);
11693                   continue;
11694
11695                 case 'k':
11696                   printf ("0x%x", treg);
11697                   continue;
11698
11699                 case 'b':
11700                 case 's':
11701                   printf ("$%d", sreg);
11702                   continue;
11703
11704                 case 'a':
11705                   printf ("0x%08lx", oc & 0x1ffffff);
11706                   continue;
11707
11708                 case 'i':
11709                 case 'j':
11710                 case 'o':
11711                 case 'u':
11712                   printf ("%d", imm);
11713                   continue;
11714
11715                 case '<':
11716                 case '>':
11717                   printf ("$%d", shamt);
11718                   continue;
11719
11720                 default:
11721                   internalError ();
11722                 }
11723               break;
11724             }
11725           return;
11726         }
11727     }
11728   printf (_("%08lx  UNDEFINED\n"), oc);
11729 }
11730 #endif
11731
11732 static symbolS *
11733 get_symbol (void)
11734 {
11735   int c;
11736   char *name;
11737   symbolS *p;
11738
11739   name = input_line_pointer;
11740   c = get_symbol_end ();
11741   p = (symbolS *) symbol_find_or_make (name);
11742   *input_line_pointer = c;
11743   return p;
11744 }
11745
11746 /* Align the current frag to a given power of two.  The MIPS assembler
11747    also automatically adjusts any preceding label.  */
11748
11749 static void
11750 mips_align (int to, int fill, symbolS *label)
11751 {
11752   mips_emit_delays (FALSE);
11753   frag_align (to, fill, 0);
11754   record_alignment (now_seg, to);
11755   if (label != NULL)
11756     {
11757       assert (S_GET_SEGMENT (label) == now_seg);
11758       symbol_set_frag (label, frag_now);
11759       S_SET_VALUE (label, (valueT) frag_now_fix ());
11760     }
11761 }
11762
11763 /* Align to a given power of two.  .align 0 turns off the automatic
11764    alignment used by the data creating pseudo-ops.  */
11765
11766 static void
11767 s_align (int x ATTRIBUTE_UNUSED)
11768 {
11769   register int temp;
11770   register long temp_fill;
11771   long max_alignment = 15;
11772
11773   /*
11774
11775     o  Note that the assembler pulls down any immediately preceeding label
11776        to the aligned address.
11777     o  It's not documented but auto alignment is reinstated by
11778        a .align pseudo instruction.
11779     o  Note also that after auto alignment is turned off the mips assembler
11780        issues an error on attempt to assemble an improperly aligned data item.
11781        We don't.
11782
11783     */
11784
11785   temp = get_absolute_expression ();
11786   if (temp > max_alignment)
11787     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11788   else if (temp < 0)
11789     {
11790       as_warn (_("Alignment negative: 0 assumed."));
11791       temp = 0;
11792     }
11793   if (*input_line_pointer == ',')
11794     {
11795       ++input_line_pointer;
11796       temp_fill = get_absolute_expression ();
11797     }
11798   else
11799     temp_fill = 0;
11800   if (temp)
11801     {
11802       auto_align = 1;
11803       mips_align (temp, (int) temp_fill,
11804                   insn_labels != NULL ? insn_labels->label : NULL);
11805     }
11806   else
11807     {
11808       auto_align = 0;
11809     }
11810
11811   demand_empty_rest_of_line ();
11812 }
11813
11814 void
11815 mips_flush_pending_output (void)
11816 {
11817   mips_emit_delays (FALSE);
11818   mips_clear_insn_labels ();
11819 }
11820
11821 static void
11822 s_change_sec (int sec)
11823 {
11824   segT seg;
11825
11826   /* When generating embedded PIC code, we only use the .text, .lit8,
11827      .sdata and .sbss sections.  We change the .data and .rdata
11828      pseudo-ops to use .sdata.  */
11829   if (mips_pic == EMBEDDED_PIC
11830       && (sec == 'd' || sec == 'r'))
11831     sec = 's';
11832
11833 #ifdef OBJ_ELF
11834   /* The ELF backend needs to know that we are changing sections, so
11835      that .previous works correctly.  We could do something like check
11836      for an obj_section_change_hook macro, but that might be confusing
11837      as it would not be appropriate to use it in the section changing
11838      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11839      This should be cleaner, somehow.  */
11840   obj_elf_section_change_hook ();
11841 #endif
11842
11843   mips_emit_delays (FALSE);
11844   switch (sec)
11845     {
11846     case 't':
11847       s_text (0);
11848       break;
11849     case 'd':
11850       s_data (0);
11851       break;
11852     case 'b':
11853       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11854       demand_empty_rest_of_line ();
11855       break;
11856
11857     case 'r':
11858       if (USE_GLOBAL_POINTER_OPT)
11859         {
11860           seg = subseg_new (RDATA_SECTION_NAME,
11861                             (subsegT) get_absolute_expression ());
11862           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11863             {
11864               bfd_set_section_flags (stdoutput, seg,
11865                                      (SEC_ALLOC
11866                                       | SEC_LOAD
11867                                       | SEC_READONLY
11868                                       | SEC_RELOC
11869                                       | SEC_DATA));
11870               if (strcmp (TARGET_OS, "elf") != 0)
11871                 record_alignment (seg, 4);
11872             }
11873           demand_empty_rest_of_line ();
11874         }
11875       else
11876         {
11877           as_bad (_("No read only data section in this object file format"));
11878           demand_empty_rest_of_line ();
11879           return;
11880         }
11881       break;
11882
11883     case 's':
11884       if (USE_GLOBAL_POINTER_OPT)
11885         {
11886           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11887           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11888             {
11889               bfd_set_section_flags (stdoutput, seg,
11890                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
11891                                      | SEC_DATA);
11892               if (strcmp (TARGET_OS, "elf") != 0)
11893                 record_alignment (seg, 4);
11894             }
11895           demand_empty_rest_of_line ();
11896           break;
11897         }
11898       else
11899         {
11900           as_bad (_("Global pointers not supported; recompile -G 0"));
11901           demand_empty_rest_of_line ();
11902           return;
11903         }
11904     }
11905
11906   auto_align = 1;
11907 }
11908
11909 void
11910 s_change_section (int ignore ATTRIBUTE_UNUSED)
11911 {
11912 #ifdef OBJ_ELF
11913   char *section_name;
11914   char c;
11915   char next_c = 0;
11916   int section_type;
11917   int section_flag;
11918   int section_entry_size;
11919   int section_alignment;
11920
11921   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11922     return;
11923
11924   section_name = input_line_pointer;
11925   c = get_symbol_end ();
11926   if (c)
11927     next_c = *(input_line_pointer + 1);
11928
11929   /* Do we have .section Name<,"flags">?  */
11930   if (c != ',' || (c == ',' && next_c == '"'))
11931     {
11932       /* just after name is now '\0'.  */
11933       *input_line_pointer = c;
11934       input_line_pointer = section_name;
11935       obj_elf_section (ignore);
11936       return;
11937     }
11938   input_line_pointer++;
11939
11940   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11941   if (c == ',')
11942     section_type = get_absolute_expression ();
11943   else
11944     section_type = 0;
11945   if (*input_line_pointer++ == ',')
11946     section_flag = get_absolute_expression ();
11947   else
11948     section_flag = 0;
11949   if (*input_line_pointer++ == ',')
11950     section_entry_size = get_absolute_expression ();
11951   else
11952     section_entry_size = 0;
11953   if (*input_line_pointer++ == ',')
11954     section_alignment = get_absolute_expression ();
11955   else
11956     section_alignment = 0;
11957
11958   section_name = xstrdup (section_name);
11959
11960   obj_elf_change_section (section_name, section_type, section_flag,
11961                           section_entry_size, 0, 0, 0);
11962
11963   if (now_seg->name != section_name)
11964     free (section_name);
11965 #endif /* OBJ_ELF */
11966 }
11967
11968 void
11969 mips_enable_auto_align (void)
11970 {
11971   auto_align = 1;
11972 }
11973
11974 static void
11975 s_cons (int log_size)
11976 {
11977   symbolS *label;
11978
11979   label = insn_labels != NULL ? insn_labels->label : NULL;
11980   mips_emit_delays (FALSE);
11981   if (log_size > 0 && auto_align)
11982     mips_align (log_size, 0, label);
11983   mips_clear_insn_labels ();
11984   cons (1 << log_size);
11985 }
11986
11987 static void
11988 s_float_cons (int type)
11989 {
11990   symbolS *label;
11991
11992   label = insn_labels != NULL ? insn_labels->label : NULL;
11993
11994   mips_emit_delays (FALSE);
11995
11996   if (auto_align)
11997     {
11998       if (type == 'd')
11999         mips_align (3, 0, label);
12000       else
12001         mips_align (2, 0, label);
12002     }
12003
12004   mips_clear_insn_labels ();
12005
12006   float_cons (type);
12007 }
12008
12009 /* Handle .globl.  We need to override it because on Irix 5 you are
12010    permitted to say
12011        .globl foo .text
12012    where foo is an undefined symbol, to mean that foo should be
12013    considered to be the address of a function.  */
12014
12015 static void
12016 s_mips_globl (int x ATTRIBUTE_UNUSED)
12017 {
12018   char *name;
12019   int c;
12020   symbolS *symbolP;
12021   flagword flag;
12022
12023   name = input_line_pointer;
12024   c = get_symbol_end ();
12025   symbolP = symbol_find_or_make (name);
12026   *input_line_pointer = c;
12027   SKIP_WHITESPACE ();
12028
12029   /* On Irix 5, every global symbol that is not explicitly labelled as
12030      being a function is apparently labelled as being an object.  */
12031   flag = BSF_OBJECT;
12032
12033   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12034     {
12035       char *secname;
12036       asection *sec;
12037
12038       secname = input_line_pointer;
12039       c = get_symbol_end ();
12040       sec = bfd_get_section_by_name (stdoutput, secname);
12041       if (sec == NULL)
12042         as_bad (_("%s: no such section"), secname);
12043       *input_line_pointer = c;
12044
12045       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12046         flag = BSF_FUNCTION;
12047     }
12048
12049   symbol_get_bfdsym (symbolP)->flags |= flag;
12050
12051   S_SET_EXTERNAL (symbolP);
12052   demand_empty_rest_of_line ();
12053 }
12054
12055 static void
12056 s_option (int x ATTRIBUTE_UNUSED)
12057 {
12058   char *opt;
12059   char c;
12060
12061   opt = input_line_pointer;
12062   c = get_symbol_end ();
12063
12064   if (*opt == 'O')
12065     {
12066       /* FIXME: What does this mean?  */
12067     }
12068   else if (strncmp (opt, "pic", 3) == 0)
12069     {
12070       int i;
12071
12072       i = atoi (opt + 3);
12073       if (i == 0)
12074         mips_pic = NO_PIC;
12075       else if (i == 2)
12076         {
12077         mips_pic = SVR4_PIC;
12078           mips_abicalls = TRUE;
12079         }
12080       else
12081         as_bad (_(".option pic%d not supported"), i);
12082
12083       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12084         {
12085           if (g_switch_seen && g_switch_value != 0)
12086             as_warn (_("-G may not be used with SVR4 PIC code"));
12087           g_switch_value = 0;
12088           bfd_set_gp_size (stdoutput, 0);
12089         }
12090     }
12091   else
12092     as_warn (_("Unrecognized option \"%s\""), opt);
12093
12094   *input_line_pointer = c;
12095   demand_empty_rest_of_line ();
12096 }
12097
12098 /* This structure is used to hold a stack of .set values.  */
12099
12100 struct mips_option_stack
12101 {
12102   struct mips_option_stack *next;
12103   struct mips_set_options options;
12104 };
12105
12106 static struct mips_option_stack *mips_opts_stack;
12107
12108 /* Handle the .set pseudo-op.  */
12109
12110 static void
12111 s_mipsset (int x ATTRIBUTE_UNUSED)
12112 {
12113   char *name = input_line_pointer, ch;
12114
12115   while (!is_end_of_line[(unsigned char) *input_line_pointer])
12116     ++input_line_pointer;
12117   ch = *input_line_pointer;
12118   *input_line_pointer = '\0';
12119
12120   if (strcmp (name, "reorder") == 0)
12121     {
12122       if (mips_opts.noreorder && prev_nop_frag != NULL)
12123         {
12124           /* If we still have pending nops, we can discard them.  The
12125              usual nop handling will insert any that are still
12126              needed.  */
12127           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12128                                     * (mips_opts.mips16 ? 2 : 4));
12129           prev_nop_frag = NULL;
12130         }
12131       mips_opts.noreorder = 0;
12132     }
12133   else if (strcmp (name, "noreorder") == 0)
12134     {
12135       mips_emit_delays (TRUE);
12136       mips_opts.noreorder = 1;
12137       mips_any_noreorder = 1;
12138     }
12139   else if (strcmp (name, "at") == 0)
12140     {
12141       mips_opts.noat = 0;
12142     }
12143   else if (strcmp (name, "noat") == 0)
12144     {
12145       mips_opts.noat = 1;
12146     }
12147   else if (strcmp (name, "macro") == 0)
12148     {
12149       mips_opts.warn_about_macros = 0;
12150     }
12151   else if (strcmp (name, "nomacro") == 0)
12152     {
12153       if (mips_opts.noreorder == 0)
12154         as_bad (_("`noreorder' must be set before `nomacro'"));
12155       mips_opts.warn_about_macros = 1;
12156     }
12157   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12158     {
12159       mips_opts.nomove = 0;
12160     }
12161   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12162     {
12163       mips_opts.nomove = 1;
12164     }
12165   else if (strcmp (name, "bopt") == 0)
12166     {
12167       mips_opts.nobopt = 0;
12168     }
12169   else if (strcmp (name, "nobopt") == 0)
12170     {
12171       mips_opts.nobopt = 1;
12172     }
12173   else if (strcmp (name, "mips16") == 0
12174            || strcmp (name, "MIPS-16") == 0)
12175     mips_opts.mips16 = 1;
12176   else if (strcmp (name, "nomips16") == 0
12177            || strcmp (name, "noMIPS-16") == 0)
12178     mips_opts.mips16 = 0;
12179   else if (strcmp (name, "mips3d") == 0)
12180     mips_opts.ase_mips3d = 1;
12181   else if (strcmp (name, "nomips3d") == 0)
12182     mips_opts.ase_mips3d = 0;
12183   else if (strcmp (name, "mdmx") == 0)
12184     mips_opts.ase_mdmx = 1;
12185   else if (strcmp (name, "nomdmx") == 0)
12186     mips_opts.ase_mdmx = 0;
12187   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12188     {
12189       int reset = 0;
12190
12191       /* Permit the user to change the ISA and architecture on the fly.
12192          Needless to say, misuse can cause serious problems.  */
12193       if (strcmp (name, "mips0") == 0)
12194         {
12195           reset = 1;
12196           mips_opts.isa = file_mips_isa;
12197         }
12198       else if (strcmp (name, "mips1") == 0)
12199         mips_opts.isa = ISA_MIPS1;
12200       else if (strcmp (name, "mips2") == 0)
12201         mips_opts.isa = ISA_MIPS2;
12202       else if (strcmp (name, "mips3") == 0)
12203         mips_opts.isa = ISA_MIPS3;
12204       else if (strcmp (name, "mips4") == 0)
12205         mips_opts.isa = ISA_MIPS4;
12206       else if (strcmp (name, "mips5") == 0)
12207         mips_opts.isa = ISA_MIPS5;
12208       else if (strcmp (name, "mips32") == 0)
12209         mips_opts.isa = ISA_MIPS32;
12210       else if (strcmp (name, "mips32r2") == 0)
12211         mips_opts.isa = ISA_MIPS32R2;
12212       else if (strcmp (name, "mips64") == 0)
12213         mips_opts.isa = ISA_MIPS64;
12214       else if (strcmp (name, "mips64r2") == 0)
12215         mips_opts.isa = ISA_MIPS64R2;
12216       else if (strcmp (name, "arch=default") == 0)
12217         {
12218           reset = 1;
12219           mips_opts.arch = file_mips_arch;
12220           mips_opts.isa = file_mips_isa;
12221         }
12222       else if (strncmp (name, "arch=", 5) == 0)
12223         {
12224           const struct mips_cpu_info *p;
12225
12226           p = mips_parse_cpu("internal use", name + 5);
12227           if (!p)
12228             as_bad (_("unknown architecture %s"), name + 5);
12229           else
12230             {
12231               mips_opts.arch = p->cpu;
12232               mips_opts.isa = p->isa;
12233             }
12234         }
12235       else
12236         as_bad (_("unknown ISA level %s"), name + 4);
12237
12238       switch (mips_opts.isa)
12239         {
12240         case  0:
12241           break;
12242         case ISA_MIPS1:
12243         case ISA_MIPS2:
12244         case ISA_MIPS32:
12245         case ISA_MIPS32R2:
12246           mips_opts.gp32 = 1;
12247           mips_opts.fp32 = 1;
12248           break;
12249         case ISA_MIPS3:
12250         case ISA_MIPS4:
12251         case ISA_MIPS5:
12252         case ISA_MIPS64:
12253         case ISA_MIPS64R2:
12254           mips_opts.gp32 = 0;
12255           mips_opts.fp32 = 0;
12256           break;
12257         default:
12258           as_bad (_("unknown ISA level %s"), name + 4);
12259           break;
12260         }
12261       if (reset)
12262         {
12263           mips_opts.gp32 = file_mips_gp32;
12264           mips_opts.fp32 = file_mips_fp32;
12265         }
12266     }
12267   else if (strcmp (name, "autoextend") == 0)
12268     mips_opts.noautoextend = 0;
12269   else if (strcmp (name, "noautoextend") == 0)
12270     mips_opts.noautoextend = 1;
12271   else if (strcmp (name, "push") == 0)
12272     {
12273       struct mips_option_stack *s;
12274
12275       s = (struct mips_option_stack *) xmalloc (sizeof *s);
12276       s->next = mips_opts_stack;
12277       s->options = mips_opts;
12278       mips_opts_stack = s;
12279     }
12280   else if (strcmp (name, "pop") == 0)
12281     {
12282       struct mips_option_stack *s;
12283
12284       s = mips_opts_stack;
12285       if (s == NULL)
12286         as_bad (_(".set pop with no .set push"));
12287       else
12288         {
12289           /* If we're changing the reorder mode we need to handle
12290              delay slots correctly.  */
12291           if (s->options.noreorder && ! mips_opts.noreorder)
12292             mips_emit_delays (TRUE);
12293           else if (! s->options.noreorder && mips_opts.noreorder)
12294             {
12295               if (prev_nop_frag != NULL)
12296                 {
12297                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12298                                             * (mips_opts.mips16 ? 2 : 4));
12299                   prev_nop_frag = NULL;
12300                 }
12301             }
12302
12303           mips_opts = s->options;
12304           mips_opts_stack = s->next;
12305           free (s);
12306         }
12307     }
12308   else
12309     {
12310       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12311     }
12312   *input_line_pointer = ch;
12313   demand_empty_rest_of_line ();
12314 }
12315
12316 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
12317    .option pic2.  It means to generate SVR4 PIC calls.  */
12318
12319 static void
12320 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12321 {
12322   mips_pic = SVR4_PIC;
12323   mips_abicalls = TRUE;
12324   if (USE_GLOBAL_POINTER_OPT)
12325     {
12326       if (g_switch_seen && g_switch_value != 0)
12327         as_warn (_("-G may not be used with SVR4 PIC code"));
12328       g_switch_value = 0;
12329     }
12330   bfd_set_gp_size (stdoutput, 0);
12331   demand_empty_rest_of_line ();
12332 }
12333
12334 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
12335    PIC code.  It sets the $gp register for the function based on the
12336    function address, which is in the register named in the argument.
12337    This uses a relocation against _gp_disp, which is handled specially
12338    by the linker.  The result is:
12339         lui     $gp,%hi(_gp_disp)
12340         addiu   $gp,$gp,%lo(_gp_disp)
12341         addu    $gp,$gp,.cpload argument
12342    The .cpload argument is normally $25 == $t9.  */
12343
12344 static void
12345 s_cpload (int ignore ATTRIBUTE_UNUSED)
12346 {
12347   expressionS ex;
12348   int icnt = 0;
12349
12350   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12351      .cpload is ignored.  */
12352   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12353     {
12354       s_ignore (0);
12355       return;
12356     }
12357
12358   /* .cpload should be in a .set noreorder section.  */
12359   if (mips_opts.noreorder == 0)
12360     as_warn (_(".cpload not in noreorder section"));
12361
12362   ex.X_op = O_symbol;
12363   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12364   ex.X_op_symbol = NULL;
12365   ex.X_add_number = 0;
12366
12367   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
12368   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12369
12370   macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12371   macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12372                mips_gp_register, BFD_RELOC_LO16);
12373
12374   macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12375                mips_gp_register, tc_get_register (0));
12376
12377   demand_empty_rest_of_line ();
12378 }
12379
12380 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
12381      .cpsetup $reg1, offset|$reg2, label
12382
12383    If offset is given, this results in:
12384      sd         $gp, offset($sp)
12385      lui        $gp, %hi(%neg(%gp_rel(label)))
12386      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12387      daddu      $gp, $gp, $reg1
12388
12389    If $reg2 is given, this results in:
12390      daddu      $reg2, $gp, $0
12391      lui        $gp, %hi(%neg(%gp_rel(label)))
12392      addiu      $gp, $gp, %lo(%neg(%gp_rel(label)))
12393      daddu      $gp, $gp, $reg1
12394    $reg1 is normally $25 == $t9.  */
12395 static void
12396 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12397 {
12398   expressionS ex_off;
12399   expressionS ex_sym;
12400   int reg1;
12401   int icnt = 0;
12402   char *f;
12403
12404   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12405      We also need NewABI support.  */
12406   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12407     {
12408       s_ignore (0);
12409       return;
12410     }
12411
12412   reg1 = tc_get_register (0);
12413   SKIP_WHITESPACE ();
12414   if (*input_line_pointer != ',')
12415     {
12416       as_bad (_("missing argument separator ',' for .cpsetup"));
12417       return;
12418     }
12419   else
12420     ++input_line_pointer;
12421   SKIP_WHITESPACE ();
12422   if (*input_line_pointer == '$')
12423     {
12424       mips_cpreturn_register = tc_get_register (0);
12425       mips_cpreturn_offset = -1;
12426     }
12427   else
12428     {
12429       mips_cpreturn_offset = get_absolute_expression ();
12430       mips_cpreturn_register = -1;
12431     }
12432   SKIP_WHITESPACE ();
12433   if (*input_line_pointer != ',')
12434     {
12435       as_bad (_("missing argument separator ',' for .cpsetup"));
12436       return;
12437     }
12438   else
12439     ++input_line_pointer;
12440   SKIP_WHITESPACE ();
12441   expression (&ex_sym);
12442
12443   if (mips_cpreturn_register == -1)
12444     {
12445       ex_off.X_op = O_constant;
12446       ex_off.X_add_symbol = NULL;
12447       ex_off.X_op_symbol = NULL;
12448       ex_off.X_add_number = mips_cpreturn_offset;
12449
12450       macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12451                    BFD_RELOC_LO16, SP);
12452     }
12453   else
12454     macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12455                  mips_gp_register, 0);
12456
12457   /* Ensure there's room for the next two instructions, so that `f'
12458      doesn't end up with an address in the wrong frag.  */
12459   frag_grow (8);
12460   f = frag_more (0);
12461   macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12462                BFD_RELOC_GPREL16);
12463   fix_new (frag_now, f - frag_now->fr_literal,
12464            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12465   fix_new (frag_now, f - frag_now->fr_literal,
12466            4, NULL, 0, 0, BFD_RELOC_HI16_S);
12467
12468   f = frag_more (0);
12469   macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12470                mips_gp_register, BFD_RELOC_GPREL16);
12471   fix_new (frag_now, f - frag_now->fr_literal,
12472            8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12473   fix_new (frag_now, f - frag_now->fr_literal,
12474            4, NULL, 0, 0, BFD_RELOC_LO16);
12475
12476   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12477                mips_gp_register, reg1);
12478
12479   demand_empty_rest_of_line ();
12480 }
12481
12482 static void
12483 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12484 {
12485   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12486    .cplocal is ignored.  */
12487   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12488     {
12489       s_ignore (0);
12490       return;
12491     }
12492
12493   mips_gp_register = tc_get_register (0);
12494   demand_empty_rest_of_line ();
12495 }
12496
12497 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
12498    offset from $sp.  The offset is remembered, and after making a PIC
12499    call $gp is restored from that location.  */
12500
12501 static void
12502 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12503 {
12504   expressionS ex;
12505   int icnt = 0;
12506
12507   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12508      .cprestore is ignored.  */
12509   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12510     {
12511       s_ignore (0);
12512       return;
12513     }
12514
12515   mips_cprestore_offset = get_absolute_expression ();
12516   mips_cprestore_valid = 1;
12517
12518   ex.X_op = O_constant;
12519   ex.X_add_symbol = NULL;
12520   ex.X_op_symbol = NULL;
12521   ex.X_add_number = mips_cprestore_offset;
12522
12523   macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
12524                                 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
12525
12526   demand_empty_rest_of_line ();
12527 }
12528
12529 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12530    was given in the preceeding .gpsetup, it results in:
12531      ld         $gp, offset($sp)
12532
12533    If a register $reg2 was given there, it results in:
12534      daddiu     $gp, $gp, $reg2
12535  */
12536 static void
12537 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12538 {
12539   expressionS ex;
12540   int icnt = 0;
12541
12542   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12543      We also need NewABI support.  */
12544   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12545     {
12546       s_ignore (0);
12547       return;
12548     }
12549
12550   if (mips_cpreturn_register == -1)
12551     {
12552       ex.X_op = O_constant;
12553       ex.X_add_symbol = NULL;
12554       ex.X_op_symbol = NULL;
12555       ex.X_add_number = mips_cpreturn_offset;
12556
12557       macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12558                    BFD_RELOC_LO16, SP);
12559     }
12560   else
12561     macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12562                  mips_cpreturn_register, 0);
12563
12564   demand_empty_rest_of_line ();
12565 }
12566
12567 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
12568    code.  It sets the offset to use in gp_rel relocations.  */
12569
12570 static void
12571 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12572 {
12573   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12574      We also need NewABI support.  */
12575   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12576     {
12577       s_ignore (0);
12578       return;
12579     }
12580
12581   mips_gprel_offset = get_absolute_expression ();
12582
12583   demand_empty_rest_of_line ();
12584 }
12585
12586 /* Handle the .gpword pseudo-op.  This is used when generating PIC
12587    code.  It generates a 32 bit GP relative reloc.  */
12588
12589 static void
12590 s_gpword (int ignore ATTRIBUTE_UNUSED)
12591 {
12592   symbolS *label;
12593   expressionS ex;
12594   char *p;
12595
12596   /* When not generating PIC code, this is treated as .word.  */
12597   if (mips_pic != SVR4_PIC)
12598     {
12599       s_cons (2);
12600       return;
12601     }
12602
12603   label = insn_labels != NULL ? insn_labels->label : NULL;
12604   mips_emit_delays (TRUE);
12605   if (auto_align)
12606     mips_align (2, 0, label);
12607   mips_clear_insn_labels ();
12608
12609   expression (&ex);
12610
12611   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12612     {
12613       as_bad (_("Unsupported use of .gpword"));
12614       ignore_rest_of_line ();
12615     }
12616
12617   p = frag_more (4);
12618   md_number_to_chars (p, 0, 4);
12619   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12620                BFD_RELOC_GPREL32);
12621
12622   demand_empty_rest_of_line ();
12623 }
12624
12625 static void
12626 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12627 {
12628   symbolS *label;
12629   expressionS ex;
12630   char *p;
12631
12632   /* When not generating PIC code, this is treated as .dword.  */
12633   if (mips_pic != SVR4_PIC)
12634     {
12635       s_cons (3);
12636       return;
12637     }
12638
12639   label = insn_labels != NULL ? insn_labels->label : NULL;
12640   mips_emit_delays (TRUE);
12641   if (auto_align)
12642     mips_align (3, 0, label);
12643   mips_clear_insn_labels ();
12644
12645   expression (&ex);
12646
12647   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12648     {
12649       as_bad (_("Unsupported use of .gpdword"));
12650       ignore_rest_of_line ();
12651     }
12652
12653   p = frag_more (8);
12654   md_number_to_chars (p, 0, 8);
12655   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12656                BFD_RELOC_GPREL32);
12657
12658   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12659   ex.X_op = O_absent;
12660   ex.X_add_symbol = 0;
12661   ex.X_add_number = 0;
12662   fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12663                BFD_RELOC_64);
12664
12665   demand_empty_rest_of_line ();
12666 }
12667
12668 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12669    tables in SVR4 PIC code.  */
12670
12671 static void
12672 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12673 {
12674   int icnt = 0;
12675   int reg;
12676
12677   /* This is ignored when not generating SVR4 PIC code.  */
12678   if (mips_pic != SVR4_PIC)
12679     {
12680       s_ignore (0);
12681       return;
12682     }
12683
12684   /* Add $gp to the register named as an argument.  */
12685   reg = tc_get_register (0);
12686   macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12687                reg, reg, mips_gp_register);
12688
12689   demand_empty_rest_of_line ();
12690 }
12691
12692 /* Handle the .insn pseudo-op.  This marks instruction labels in
12693    mips16 mode.  This permits the linker to handle them specially,
12694    such as generating jalx instructions when needed.  We also make
12695    them odd for the duration of the assembly, in order to generate the
12696    right sort of code.  We will make them even in the adjust_symtab
12697    routine, while leaving them marked.  This is convenient for the
12698    debugger and the disassembler.  The linker knows to make them odd
12699    again.  */
12700
12701 static void
12702 s_insn (int ignore ATTRIBUTE_UNUSED)
12703 {
12704   mips16_mark_labels ();
12705
12706   demand_empty_rest_of_line ();
12707 }
12708
12709 /* Handle a .stabn directive.  We need these in order to mark a label
12710    as being a mips16 text label correctly.  Sometimes the compiler
12711    will emit a label, followed by a .stabn, and then switch sections.
12712    If the label and .stabn are in mips16 mode, then the label is
12713    really a mips16 text label.  */
12714
12715 static void
12716 s_mips_stab (int type)
12717 {
12718   if (type == 'n')
12719     mips16_mark_labels ();
12720
12721   s_stab (type);
12722 }
12723
12724 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12725  */
12726
12727 static void
12728 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12729 {
12730   char *name;
12731   int c;
12732   symbolS *symbolP;
12733   expressionS exp;
12734
12735   name = input_line_pointer;
12736   c = get_symbol_end ();
12737   symbolP = symbol_find_or_make (name);
12738   S_SET_WEAK (symbolP);
12739   *input_line_pointer = c;
12740
12741   SKIP_WHITESPACE ();
12742
12743   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12744     {
12745       if (S_IS_DEFINED (symbolP))
12746         {
12747           as_bad ("ignoring attempt to redefine symbol %s",
12748                   S_GET_NAME (symbolP));
12749           ignore_rest_of_line ();
12750           return;
12751         }
12752
12753       if (*input_line_pointer == ',')
12754         {
12755           ++input_line_pointer;
12756           SKIP_WHITESPACE ();
12757         }
12758
12759       expression (&exp);
12760       if (exp.X_op != O_symbol)
12761         {
12762           as_bad ("bad .weakext directive");
12763           ignore_rest_of_line ();
12764           return;
12765         }
12766       symbol_set_value_expression (symbolP, &exp);
12767     }
12768
12769   demand_empty_rest_of_line ();
12770 }
12771
12772 /* Parse a register string into a number.  Called from the ECOFF code
12773    to parse .frame.  The argument is non-zero if this is the frame
12774    register, so that we can record it in mips_frame_reg.  */
12775
12776 int
12777 tc_get_register (int frame)
12778 {
12779   int reg;
12780
12781   SKIP_WHITESPACE ();
12782   if (*input_line_pointer++ != '$')
12783     {
12784       as_warn (_("expected `$'"));
12785       reg = ZERO;
12786     }
12787   else if (ISDIGIT (*input_line_pointer))
12788     {
12789       reg = get_absolute_expression ();
12790       if (reg < 0 || reg >= 32)
12791         {
12792           as_warn (_("Bad register number"));
12793           reg = ZERO;
12794         }
12795     }
12796   else
12797     {
12798       if (strncmp (input_line_pointer, "ra", 2) == 0)
12799         {
12800           reg = RA;
12801           input_line_pointer += 2;
12802         }
12803       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12804         {
12805           reg = FP;
12806           input_line_pointer += 2;
12807         }
12808       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12809         {
12810           reg = SP;
12811           input_line_pointer += 2;
12812         }
12813       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12814         {
12815           reg = GP;
12816           input_line_pointer += 2;
12817         }
12818       else if (strncmp (input_line_pointer, "at", 2) == 0)
12819         {
12820           reg = AT;
12821           input_line_pointer += 2;
12822         }
12823       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12824         {
12825           reg = KT0;
12826           input_line_pointer += 3;
12827         }
12828       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12829         {
12830           reg = KT1;
12831           input_line_pointer += 3;
12832         }
12833       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12834         {
12835           reg = ZERO;
12836           input_line_pointer += 4;
12837         }
12838       else
12839         {
12840           as_warn (_("Unrecognized register name"));
12841           reg = ZERO;
12842           while (ISALNUM(*input_line_pointer))
12843            input_line_pointer++;
12844         }
12845     }
12846   if (frame)
12847     {
12848       mips_frame_reg = reg != 0 ? reg : SP;
12849       mips_frame_reg_valid = 1;
12850       mips_cprestore_valid = 0;
12851     }
12852   return reg;
12853 }
12854
12855 valueT
12856 md_section_align (asection *seg, valueT addr)
12857 {
12858   int align = bfd_get_section_alignment (stdoutput, seg);
12859
12860 #ifdef OBJ_ELF
12861   /* We don't need to align ELF sections to the full alignment.
12862      However, Irix 5 may prefer that we align them at least to a 16
12863      byte boundary.  We don't bother to align the sections if we are
12864      targeted for an embedded system.  */
12865   if (strcmp (TARGET_OS, "elf") == 0)
12866     return addr;
12867   if (align > 4)
12868     align = 4;
12869 #endif
12870
12871   return ((addr + (1 << align) - 1) & (-1 << align));
12872 }
12873
12874 /* Utility routine, called from above as well.  If called while the
12875    input file is still being read, it's only an approximation.  (For
12876    example, a symbol may later become defined which appeared to be
12877    undefined earlier.)  */
12878
12879 static int
12880 nopic_need_relax (symbolS *sym, int before_relaxing)
12881 {
12882   if (sym == 0)
12883     return 0;
12884
12885   if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12886     {
12887       const char *symname;
12888       int change;
12889
12890       /* Find out whether this symbol can be referenced off the $gp
12891          register.  It can be if it is smaller than the -G size or if
12892          it is in the .sdata or .sbss section.  Certain symbols can
12893          not be referenced off the $gp, although it appears as though
12894          they can.  */
12895       symname = S_GET_NAME (sym);
12896       if (symname != (const char *) NULL
12897           && (strcmp (symname, "eprol") == 0
12898               || strcmp (symname, "etext") == 0
12899               || strcmp (symname, "_gp") == 0
12900               || strcmp (symname, "edata") == 0
12901               || strcmp (symname, "_fbss") == 0
12902               || strcmp (symname, "_fdata") == 0
12903               || strcmp (symname, "_ftext") == 0
12904               || strcmp (symname, "end") == 0
12905               || strcmp (symname, "_gp_disp") == 0))
12906         change = 1;
12907       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12908                && (0
12909 #ifndef NO_ECOFF_DEBUGGING
12910                    || (symbol_get_obj (sym)->ecoff_extern_size != 0
12911                        && (symbol_get_obj (sym)->ecoff_extern_size
12912                            <= g_switch_value))
12913 #endif
12914                    /* We must defer this decision until after the whole
12915                       file has been read, since there might be a .extern
12916                       after the first use of this symbol.  */
12917                    || (before_relaxing
12918 #ifndef NO_ECOFF_DEBUGGING
12919                        && symbol_get_obj (sym)->ecoff_extern_size == 0
12920 #endif
12921                        && S_GET_VALUE (sym) == 0)
12922                    || (S_GET_VALUE (sym) != 0
12923                        && S_GET_VALUE (sym) <= g_switch_value)))
12924         change = 0;
12925       else
12926         {
12927           const char *segname;
12928
12929           segname = segment_name (S_GET_SEGMENT (sym));
12930           assert (strcmp (segname, ".lit8") != 0
12931                   && strcmp (segname, ".lit4") != 0);
12932           change = (strcmp (segname, ".sdata") != 0
12933                     && strcmp (segname, ".sbss") != 0
12934                     && strncmp (segname, ".sdata.", 7) != 0
12935                     && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12936         }
12937       return change;
12938     }
12939   else
12940     /* We are not optimizing for the $gp register.  */
12941     return 1;
12942 }
12943
12944
12945 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12946
12947 static bfd_boolean
12948 pic_need_relax (symbolS *sym, asection *segtype)
12949 {
12950   asection *symsec;
12951   bfd_boolean linkonce;
12952
12953   /* Handle the case of a symbol equated to another symbol.  */
12954   while (symbol_equated_reloc_p (sym))
12955     {
12956       symbolS *n;
12957
12958       /* It's possible to get a loop here in a badly written
12959          program.  */
12960       n = symbol_get_value_expression (sym)->X_add_symbol;
12961       if (n == sym)
12962         break;
12963       sym = n;
12964     }
12965
12966   symsec = S_GET_SEGMENT (sym);
12967
12968   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12969   linkonce = FALSE;
12970   if (symsec != segtype && ! S_IS_LOCAL (sym))
12971     {
12972       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12973           != 0)
12974         linkonce = TRUE;
12975
12976       /* The GNU toolchain uses an extension for ELF: a section
12977          beginning with the magic string .gnu.linkonce is a linkonce
12978          section.  */
12979       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12980                    sizeof ".gnu.linkonce" - 1) == 0)
12981         linkonce = TRUE;
12982     }
12983
12984   /* This must duplicate the test in adjust_reloc_syms.  */
12985   return (symsec != &bfd_und_section
12986           && symsec != &bfd_abs_section
12987           && ! bfd_is_com_section (symsec)
12988           && !linkonce
12989 #ifdef OBJ_ELF
12990           /* A global or weak symbol is treated as external.  */
12991           && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12992               || (! S_IS_WEAK (sym)
12993                   && (! S_IS_EXTERNAL (sym)
12994                       || mips_pic == EMBEDDED_PIC)))
12995 #endif
12996           );
12997 }
12998
12999
13000 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13001    extended opcode.  SEC is the section the frag is in.  */
13002
13003 static int
13004 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
13005 {
13006   int type;
13007   register const struct mips16_immed_operand *op;
13008   offsetT val;
13009   int mintiny, maxtiny;
13010   segT symsec;
13011   fragS *sym_frag;
13012
13013   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13014     return 0;
13015   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13016     return 1;
13017
13018   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13019   op = mips16_immed_operands;
13020   while (op->type != type)
13021     {
13022       ++op;
13023       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13024     }
13025
13026   if (op->unsp)
13027     {
13028       if (type == '<' || type == '>' || type == '[' || type == ']')
13029         {
13030           mintiny = 1;
13031           maxtiny = 1 << op->nbits;
13032         }
13033       else
13034         {
13035           mintiny = 0;
13036           maxtiny = (1 << op->nbits) - 1;
13037         }
13038     }
13039   else
13040     {
13041       mintiny = - (1 << (op->nbits - 1));
13042       maxtiny = (1 << (op->nbits - 1)) - 1;
13043     }
13044
13045   sym_frag = symbol_get_frag (fragp->fr_symbol);
13046   val = S_GET_VALUE (fragp->fr_symbol);
13047   symsec = S_GET_SEGMENT (fragp->fr_symbol);
13048
13049   if (op->pcrel)
13050     {
13051       addressT addr;
13052
13053       /* We won't have the section when we are called from
13054          mips_relax_frag.  However, we will always have been called
13055          from md_estimate_size_before_relax first.  If this is a
13056          branch to a different section, we mark it as such.  If SEC is
13057          NULL, and the frag is not marked, then it must be a branch to
13058          the same section.  */
13059       if (sec == NULL)
13060         {
13061           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13062             return 1;
13063         }
13064       else
13065         {
13066           /* Must have been called from md_estimate_size_before_relax.  */
13067           if (symsec != sec)
13068             {
13069               fragp->fr_subtype =
13070                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13071
13072               /* FIXME: We should support this, and let the linker
13073                  catch branches and loads that are out of range.  */
13074               as_bad_where (fragp->fr_file, fragp->fr_line,
13075                             _("unsupported PC relative reference to different section"));
13076
13077               return 1;
13078             }
13079           if (fragp != sym_frag && sym_frag->fr_address == 0)
13080             /* Assume non-extended on the first relaxation pass.
13081                The address we have calculated will be bogus if this is
13082                a forward branch to another frag, as the forward frag
13083                will have fr_address == 0.  */
13084             return 0;
13085         }
13086
13087       /* In this case, we know for sure that the symbol fragment is in
13088          the same section.  If the relax_marker of the symbol fragment
13089          differs from the relax_marker of this fragment, we have not
13090          yet adjusted the symbol fragment fr_address.  We want to add
13091          in STRETCH in order to get a better estimate of the address.
13092          This particularly matters because of the shift bits.  */
13093       if (stretch != 0
13094           && sym_frag->relax_marker != fragp->relax_marker)
13095         {
13096           fragS *f;
13097
13098           /* Adjust stretch for any alignment frag.  Note that if have
13099              been expanding the earlier code, the symbol may be
13100              defined in what appears to be an earlier frag.  FIXME:
13101              This doesn't handle the fr_subtype field, which specifies
13102              a maximum number of bytes to skip when doing an
13103              alignment.  */
13104           for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13105             {
13106               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13107                 {
13108                   if (stretch < 0)
13109                     stretch = - ((- stretch)
13110                                  & ~ ((1 << (int) f->fr_offset) - 1));
13111                   else
13112                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13113                   if (stretch == 0)
13114                     break;
13115                 }
13116             }
13117           if (f != NULL)
13118             val += stretch;
13119         }
13120
13121       addr = fragp->fr_address + fragp->fr_fix;
13122
13123       /* The base address rules are complicated.  The base address of
13124          a branch is the following instruction.  The base address of a
13125          PC relative load or add is the instruction itself, but if it
13126          is in a delay slot (in which case it can not be extended) use
13127          the address of the instruction whose delay slot it is in.  */
13128       if (type == 'p' || type == 'q')
13129         {
13130           addr += 2;
13131
13132           /* If we are currently assuming that this frag should be
13133              extended, then, the current address is two bytes
13134              higher.  */
13135           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13136             addr += 2;
13137
13138           /* Ignore the low bit in the target, since it will be set
13139              for a text label.  */
13140           if ((val & 1) != 0)
13141             --val;
13142         }
13143       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13144         addr -= 4;
13145       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13146         addr -= 2;
13147
13148       val -= addr & ~ ((1 << op->shift) - 1);
13149
13150       /* Branch offsets have an implicit 0 in the lowest bit.  */
13151       if (type == 'p' || type == 'q')
13152         val /= 2;
13153
13154       /* If any of the shifted bits are set, we must use an extended
13155          opcode.  If the address depends on the size of this
13156          instruction, this can lead to a loop, so we arrange to always
13157          use an extended opcode.  We only check this when we are in
13158          the main relaxation loop, when SEC is NULL.  */
13159       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13160         {
13161           fragp->fr_subtype =
13162             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13163           return 1;
13164         }
13165
13166       /* If we are about to mark a frag as extended because the value
13167          is precisely maxtiny + 1, then there is a chance of an
13168          infinite loop as in the following code:
13169              la $4,foo
13170              .skip      1020
13171              .align     2
13172            foo:
13173          In this case when the la is extended, foo is 0x3fc bytes
13174          away, so the la can be shrunk, but then foo is 0x400 away, so
13175          the la must be extended.  To avoid this loop, we mark the
13176          frag as extended if it was small, and is about to become
13177          extended with a value of maxtiny + 1.  */
13178       if (val == ((maxtiny + 1) << op->shift)
13179           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13180           && sec == NULL)
13181         {
13182           fragp->fr_subtype =
13183             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13184           return 1;
13185         }
13186     }
13187   else if (symsec != absolute_section && sec != NULL)
13188     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13189
13190   if ((val & ((1 << op->shift) - 1)) != 0
13191       || val < (mintiny << op->shift)
13192       || val > (maxtiny << op->shift))
13193     return 1;
13194   else
13195     return 0;
13196 }
13197
13198 /* Compute the length of a branch sequence, and adjust the
13199    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
13200    worst-case length is computed, with UPDATE being used to indicate
13201    whether an unconditional (-1), branch-likely (+1) or regular (0)
13202    branch is to be computed.  */
13203 static int
13204 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13205 {
13206   bfd_boolean toofar;
13207   int length;
13208
13209   if (fragp
13210       && S_IS_DEFINED (fragp->fr_symbol)
13211       && sec == S_GET_SEGMENT (fragp->fr_symbol))
13212     {
13213       addressT addr;
13214       offsetT val;
13215
13216       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13217
13218       addr = fragp->fr_address + fragp->fr_fix + 4;
13219
13220       val -= addr;
13221
13222       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13223     }
13224   else if (fragp)
13225     /* If the symbol is not defined or it's in a different segment,
13226        assume the user knows what's going on and emit a short
13227        branch.  */
13228     toofar = FALSE;
13229   else
13230     toofar = TRUE;
13231
13232   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13233     fragp->fr_subtype
13234       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13235                              RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13236                              RELAX_BRANCH_LINK (fragp->fr_subtype),
13237                              toofar);
13238
13239   length = 4;
13240   if (toofar)
13241     {
13242       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13243         length += 8;
13244
13245       if (mips_pic != NO_PIC)
13246         {
13247           /* Additional space for PIC loading of target address.  */
13248           length += 8;
13249           if (mips_opts.isa == ISA_MIPS1)
13250             /* Additional space for $at-stabilizing nop.  */
13251             length += 4;
13252         }
13253
13254       /* If branch is conditional.  */
13255       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13256         length += 8;
13257     }
13258
13259   return length;
13260 }
13261
13262 /* Estimate the size of a frag before relaxing.  Unless this is the
13263    mips16, we are not really relaxing here, and the final size is
13264    encoded in the subtype information.  For the mips16, we have to
13265    decide whether we are using an extended opcode or not.  */
13266
13267 int
13268 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13269 {
13270   int change;
13271
13272   if (RELAX_BRANCH_P (fragp->fr_subtype))
13273     {
13274
13275       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13276
13277       return fragp->fr_var;
13278     }
13279
13280   if (RELAX_MIPS16_P (fragp->fr_subtype))
13281     /* We don't want to modify the EXTENDED bit here; it might get us
13282        into infinite loops.  We change it only in mips_relax_frag().  */
13283     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13284
13285   if (mips_pic == NO_PIC)
13286     change = nopic_need_relax (fragp->fr_symbol, 0);
13287   else if (mips_pic == SVR4_PIC)
13288     change = pic_need_relax (fragp->fr_symbol, segtype);
13289   else
13290     abort ();
13291
13292   if (change)
13293     {
13294       /* Record the offset to the first reloc in the fr_opcode field.
13295          This lets md_convert_frag and tc_gen_reloc know that the code
13296          must be expanded.  */
13297       fragp->fr_opcode = (fragp->fr_literal
13298                           + fragp->fr_fix
13299                           - RELAX_OLD (fragp->fr_subtype)
13300                           + RELAX_RELOC1 (fragp->fr_subtype));
13301       /* FIXME: This really needs as_warn_where.  */
13302       if (RELAX_WARN (fragp->fr_subtype))
13303         as_warn (_("AT used after \".set noat\" or macro used after "
13304                    "\".set nomacro\""));
13305
13306       return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13307     }
13308
13309   return 0;
13310 }
13311
13312 /* This is called to see whether a reloc against a defined symbol
13313    should be converted into a reloc against a section.  Don't adjust
13314    MIPS16 jump relocations, so we don't have to worry about the format
13315    of the offset in the .o file.  Don't adjust relocations against
13316    mips16 symbols, so that the linker can find them if it needs to set
13317    up a stub.  */
13318
13319 int
13320 mips_fix_adjustable (fixS *fixp)
13321 {
13322   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13323     return 0;
13324
13325   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13326       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13327     return 0;
13328
13329   if (fixp->fx_addsy == NULL)
13330     return 1;
13331
13332 #ifdef OBJ_ELF
13333   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13334       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13335       && fixp->fx_subsy == NULL)
13336     return 0;
13337 #endif
13338
13339   return 1;
13340 }
13341
13342 /* Translate internal representation of relocation info to BFD target
13343    format.  */
13344
13345 arelent **
13346 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13347 {
13348   static arelent *retval[4];
13349   arelent *reloc;
13350   bfd_reloc_code_real_type code;
13351
13352   memset (retval, 0, sizeof(retval));
13353   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13354   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13355   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13356   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13357
13358   if (mips_pic == EMBEDDED_PIC
13359       && SWITCH_TABLE (fixp))
13360     {
13361       /* For a switch table entry we use a special reloc.  The addend
13362          is actually the difference between the reloc address and the
13363          subtrahend.  */
13364       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13365       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13366         as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13367       fixp->fx_r_type = BFD_RELOC_GPREL32;
13368     }
13369   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13370     {
13371       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13372         reloc->addend = fixp->fx_addnumber;
13373       else
13374         {
13375           /* We use a special addend for an internal RELLO reloc.  */
13376           if (symbol_section_p (fixp->fx_addsy))
13377             reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13378           else
13379             reloc->addend = fixp->fx_addnumber + reloc->address;
13380         }
13381     }
13382   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13383     {
13384       assert (fixp->fx_next != NULL
13385               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13386
13387       /* The reloc is relative to the RELLO; adjust the addend
13388          accordingly.  */
13389       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13390         reloc->addend = fixp->fx_next->fx_addnumber;
13391       else
13392         {
13393           /* We use a special addend for an internal RELHI reloc.  */
13394           if (symbol_section_p (fixp->fx_addsy))
13395             reloc->addend = (fixp->fx_next->fx_frag->fr_address
13396                              + fixp->fx_next->fx_where
13397                              - S_GET_VALUE (fixp->fx_subsy));
13398           else
13399             reloc->addend = (fixp->fx_addnumber
13400                              + fixp->fx_next->fx_frag->fr_address
13401                              + fixp->fx_next->fx_where);
13402         }
13403     }
13404   else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13405     reloc->addend = fixp->fx_addnumber;
13406   else
13407     {
13408       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13409         /* A gruesome hack which is a result of the gruesome gas reloc
13410            handling.  */
13411         reloc->addend = reloc->address;
13412       else
13413         reloc->addend = -reloc->address;
13414     }
13415
13416   /* If this is a variant frag, we may need to adjust the existing
13417      reloc and generate a new one.  */
13418   if (fixp->fx_frag->fr_opcode != NULL
13419       && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13420            && ! HAVE_NEWABI)
13421           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13422               && HAVE_NEWABI)
13423           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13424           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13425           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13426           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13427           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13428           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13429     )
13430     {
13431       arelent *reloc2;
13432
13433       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13434
13435       /* If this is not the last reloc in this frag, then we have two
13436          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13437          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
13438          the second one handle all of them.  */
13439       if (fixp->fx_next != NULL
13440           && fixp->fx_frag == fixp->fx_next->fx_frag)
13441         {
13442           assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13443                    && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13444                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13445                       && (fixp->fx_next->fx_r_type
13446                           == BFD_RELOC_MIPS_GOT_LO16))
13447                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13448                       && (fixp->fx_next->fx_r_type
13449                           == BFD_RELOC_MIPS_CALL_LO16)));
13450           retval[0] = NULL;
13451           return retval;
13452         }
13453
13454       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13455       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13456       reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13457       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13458       reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13459       *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13460       reloc2->address = (reloc->address
13461                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13462                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13463       reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13464         + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13465       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13466       assert (reloc2->howto != NULL);
13467
13468       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13469         {
13470           arelent *reloc3;
13471
13472           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13473           *reloc3 = *reloc2;
13474           reloc3->address += 4;
13475         }
13476
13477       if (mips_pic == NO_PIC)
13478         {
13479           assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13480           fixp->fx_r_type = BFD_RELOC_HI16_S;
13481         }
13482       else if (mips_pic == SVR4_PIC)
13483         {
13484           switch (fixp->fx_r_type)
13485             {
13486             default:
13487               abort ();
13488             case BFD_RELOC_MIPS_GOT16:
13489               break;
13490             case BFD_RELOC_MIPS_GOT_LO16:
13491             case BFD_RELOC_MIPS_CALL_LO16:
13492               if (HAVE_NEWABI)
13493                 {
13494                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13495                   reloc2->howto = bfd_reloc_type_lookup
13496                     (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13497                 }
13498               else
13499                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13500               break;
13501             case BFD_RELOC_MIPS_CALL16:
13502             case BFD_RELOC_MIPS_GOT_OFST:
13503             case BFD_RELOC_MIPS_GOT_DISP:
13504               if (HAVE_NEWABI)
13505                 {
13506                   /* It may seem nonsensical to relax GOT_DISP to
13507                      GOT_DISP, but we're actually turning a GOT_DISP
13508                      without offset into a GOT_DISP with an offset,
13509                      getting rid of the separate addition, which we can
13510                      do when the symbol is found to be local.  */
13511                   fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13512                   retval[1] = NULL;
13513                 }
13514               else
13515                 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13516               break;
13517             }
13518         }
13519       else
13520         abort ();
13521     }
13522
13523   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13524      entry to be used in the relocation's section offset.  */
13525   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13526     {
13527       reloc->address = reloc->addend;
13528       reloc->addend = 0;
13529     }
13530
13531   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13532      fixup_segment converted a non-PC relative reloc into a PC
13533      relative reloc.  In such a case, we need to convert the reloc
13534      code.  */
13535   code = fixp->fx_r_type;
13536   if (fixp->fx_pcrel)
13537     {
13538       switch (code)
13539         {
13540         case BFD_RELOC_8:
13541           code = BFD_RELOC_8_PCREL;
13542           break;
13543         case BFD_RELOC_16:
13544           code = BFD_RELOC_16_PCREL;
13545           break;
13546         case BFD_RELOC_32:
13547           code = BFD_RELOC_32_PCREL;
13548           break;
13549         case BFD_RELOC_64:
13550           code = BFD_RELOC_64_PCREL;
13551           break;
13552         case BFD_RELOC_8_PCREL:
13553         case BFD_RELOC_16_PCREL:
13554         case BFD_RELOC_32_PCREL:
13555         case BFD_RELOC_64_PCREL:
13556         case BFD_RELOC_16_PCREL_S2:
13557         case BFD_RELOC_PCREL_HI16_S:
13558         case BFD_RELOC_PCREL_LO16:
13559           break;
13560         default:
13561           as_bad_where (fixp->fx_file, fixp->fx_line,
13562                         _("Cannot make %s relocation PC relative"),
13563                         bfd_get_reloc_code_name (code));
13564         }
13565     }
13566
13567   /* To support a PC relative reloc when generating embedded PIC code
13568      for ECOFF, we use a Cygnus extension.  We check for that here to
13569      make sure that we don't let such a reloc escape normally.  */
13570   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13571        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13572       && code == BFD_RELOC_16_PCREL_S2
13573       && mips_pic != EMBEDDED_PIC)
13574     reloc->howto = NULL;
13575   else
13576     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13577
13578   if (reloc->howto == NULL)
13579     {
13580       as_bad_where (fixp->fx_file, fixp->fx_line,
13581                     _("Can not represent %s relocation in this object file format"),
13582                     bfd_get_reloc_code_name (code));
13583       retval[0] = NULL;
13584     }
13585
13586   return retval;
13587 }
13588
13589 /* Relax a machine dependent frag.  This returns the amount by which
13590    the current size of the frag should change.  */
13591
13592 int
13593 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13594 {
13595   if (RELAX_BRANCH_P (fragp->fr_subtype))
13596     {
13597       offsetT old_var = fragp->fr_var;
13598
13599       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13600
13601       return fragp->fr_var - old_var;
13602     }
13603
13604   if (! RELAX_MIPS16_P (fragp->fr_subtype))
13605     return 0;
13606
13607   if (mips16_extended_frag (fragp, NULL, stretch))
13608     {
13609       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13610         return 0;
13611       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13612       return 2;
13613     }
13614   else
13615     {
13616       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13617         return 0;
13618       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13619       return -2;
13620     }
13621
13622   return 0;
13623 }
13624
13625 /* Convert a machine dependent frag.  */
13626
13627 void
13628 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13629 {
13630   int old, new;
13631   char *fixptr;
13632
13633   if (RELAX_BRANCH_P (fragp->fr_subtype))
13634     {
13635       bfd_byte *buf;
13636       unsigned long insn;
13637       expressionS exp;
13638       fixS *fixp;
13639
13640       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13641
13642       if (target_big_endian)
13643         insn = bfd_getb32 (buf);
13644       else
13645         insn = bfd_getl32 (buf);
13646
13647       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13648         {
13649           /* We generate a fixup instead of applying it right now
13650              because, if there are linker relaxations, we're going to
13651              need the relocations.  */
13652           exp.X_op = O_symbol;
13653           exp.X_add_symbol = fragp->fr_symbol;
13654           exp.X_add_number = fragp->fr_offset;
13655
13656           fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13657                               4, &exp, 1,
13658                               BFD_RELOC_16_PCREL_S2);
13659           fixp->fx_file = fragp->fr_file;
13660           fixp->fx_line = fragp->fr_line;
13661
13662           md_number_to_chars (buf, insn, 4);
13663           buf += 4;
13664         }
13665       else
13666         {
13667           int i;
13668
13669           as_warn_where (fragp->fr_file, fragp->fr_line,
13670                          _("relaxed out-of-range branch into a jump"));
13671
13672           if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13673             goto uncond;
13674
13675           if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13676             {
13677               /* Reverse the branch.  */
13678               switch ((insn >> 28) & 0xf)
13679                 {
13680                 case 4:
13681                   /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13682                      have the condition reversed by tweaking a single
13683                      bit, and their opcodes all have 0x4???????.  */
13684                   assert ((insn & 0xf1000000) == 0x41000000);
13685                   insn ^= 0x00010000;
13686                   break;
13687
13688                 case 0:
13689                   /* bltz       0x04000000      bgez    0x04010000
13690                      bltzal     0x04100000      bgezal  0x04110000 */
13691                   assert ((insn & 0xfc0e0000) == 0x04000000);
13692                   insn ^= 0x00010000;
13693                   break;
13694
13695                 case 1:
13696                   /* beq        0x10000000      bne     0x14000000
13697                      blez       0x18000000      bgtz    0x1c000000 */
13698                   insn ^= 0x04000000;
13699                   break;
13700
13701                 default:
13702                   abort ();
13703                 }
13704             }
13705
13706           if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13707             {
13708               /* Clear the and-link bit.  */
13709               assert ((insn & 0xfc1c0000) == 0x04100000);
13710
13711               /* bltzal 0x04100000      bgezal  0x04110000
13712                 bltzall 0x04120000     bgezall  0x04130000 */
13713               insn &= ~0x00100000;
13714             }
13715
13716           /* Branch over the branch (if the branch was likely) or the
13717              full jump (not likely case).  Compute the offset from the
13718              current instruction to branch to.  */
13719           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13720             i = 16;
13721           else
13722             {
13723               /* How many bytes in instructions we've already emitted?  */
13724               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13725               /* How many bytes in instructions from here to the end?  */
13726               i = fragp->fr_var - i;
13727             }
13728           /* Convert to instruction count.  */
13729           i >>= 2;
13730           /* Branch counts from the next instruction.  */
13731           i--;
13732           insn |= i;
13733           /* Branch over the jump.  */
13734           md_number_to_chars (buf, insn, 4);
13735           buf += 4;
13736
13737           /* Nop */
13738           md_number_to_chars (buf, 0, 4);
13739           buf += 4;
13740
13741           if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13742             {
13743               /* beql $0, $0, 2f */
13744               insn = 0x50000000;
13745               /* Compute the PC offset from the current instruction to
13746                  the end of the variable frag.  */
13747               /* How many bytes in instructions we've already emitted?  */
13748               i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13749               /* How many bytes in instructions from here to the end?  */
13750               i = fragp->fr_var - i;
13751               /* Convert to instruction count.  */
13752               i >>= 2;
13753               /* Don't decrement i, because we want to branch over the
13754                  delay slot.  */
13755
13756               insn |= i;
13757               md_number_to_chars (buf, insn, 4);
13758               buf += 4;
13759
13760               md_number_to_chars (buf, 0, 4);
13761               buf += 4;
13762             }
13763
13764         uncond:
13765           if (mips_pic == NO_PIC)
13766             {
13767               /* j or jal.  */
13768               insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13769                       ? 0x0c000000 : 0x08000000);
13770               exp.X_op = O_symbol;
13771               exp.X_add_symbol = fragp->fr_symbol;
13772               exp.X_add_number = fragp->fr_offset;
13773
13774               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13775                                   4, &exp, 0, BFD_RELOC_MIPS_JMP);
13776               fixp->fx_file = fragp->fr_file;
13777               fixp->fx_line = fragp->fr_line;
13778
13779               md_number_to_chars (buf, insn, 4);
13780               buf += 4;
13781             }
13782           else
13783             {
13784               /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
13785               insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13786               exp.X_op = O_symbol;
13787               exp.X_add_symbol = fragp->fr_symbol;
13788               exp.X_add_number = fragp->fr_offset;
13789
13790               if (fragp->fr_offset)
13791                 {
13792                   exp.X_add_symbol = make_expr_symbol (&exp);
13793                   exp.X_add_number = 0;
13794                 }
13795
13796               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13797                                   4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13798               fixp->fx_file = fragp->fr_file;
13799               fixp->fx_line = fragp->fr_line;
13800
13801               md_number_to_chars (buf, insn, 4);
13802               buf += 4;
13803
13804               if (mips_opts.isa == ISA_MIPS1)
13805                 {
13806                   /* nop */
13807                   md_number_to_chars (buf, 0, 4);
13808                   buf += 4;
13809                 }
13810
13811               /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13812               insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13813
13814               fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13815                                   4, &exp, 0, BFD_RELOC_LO16);
13816               fixp->fx_file = fragp->fr_file;
13817               fixp->fx_line = fragp->fr_line;
13818
13819               md_number_to_chars (buf, insn, 4);
13820               buf += 4;
13821
13822               /* j(al)r $at.  */
13823               if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13824                 insn = 0x0020f809;
13825               else
13826                 insn = 0x00200008;
13827
13828               md_number_to_chars (buf, insn, 4);
13829               buf += 4;
13830             }
13831         }
13832
13833       assert (buf == (bfd_byte *)fragp->fr_literal
13834               + fragp->fr_fix + fragp->fr_var);
13835
13836       fragp->fr_fix += fragp->fr_var;
13837
13838       return;
13839     }
13840
13841   if (RELAX_MIPS16_P (fragp->fr_subtype))
13842     {
13843       int type;
13844       register const struct mips16_immed_operand *op;
13845       bfd_boolean small, ext;
13846       offsetT val;
13847       bfd_byte *buf;
13848       unsigned long insn;
13849       bfd_boolean use_extend;
13850       unsigned short extend;
13851
13852       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13853       op = mips16_immed_operands;
13854       while (op->type != type)
13855         ++op;
13856
13857       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13858         {
13859           small = FALSE;
13860           ext = TRUE;
13861         }
13862       else
13863         {
13864           small = TRUE;
13865           ext = FALSE;
13866         }
13867
13868       resolve_symbol_value (fragp->fr_symbol);
13869       val = S_GET_VALUE (fragp->fr_symbol);
13870       if (op->pcrel)
13871         {
13872           addressT addr;
13873
13874           addr = fragp->fr_address + fragp->fr_fix;
13875
13876           /* The rules for the base address of a PC relative reloc are
13877              complicated; see mips16_extended_frag.  */
13878           if (type == 'p' || type == 'q')
13879             {
13880               addr += 2;
13881               if (ext)
13882                 addr += 2;
13883               /* Ignore the low bit in the target, since it will be
13884                  set for a text label.  */
13885               if ((val & 1) != 0)
13886                 --val;
13887             }
13888           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13889             addr -= 4;
13890           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13891             addr -= 2;
13892
13893           addr &= ~ (addressT) ((1 << op->shift) - 1);
13894           val -= addr;
13895
13896           /* Make sure the section winds up with the alignment we have
13897              assumed.  */
13898           if (op->shift > 0)
13899             record_alignment (asec, op->shift);
13900         }
13901
13902       if (ext
13903           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13904               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13905         as_warn_where (fragp->fr_file, fragp->fr_line,
13906                        _("extended instruction in delay slot"));
13907
13908       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13909
13910       if (target_big_endian)
13911         insn = bfd_getb16 (buf);
13912       else
13913         insn = bfd_getl16 (buf);
13914
13915       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13916                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13917                     small, ext, &insn, &use_extend, &extend);
13918
13919       if (use_extend)
13920         {
13921           md_number_to_chars (buf, 0xf000 | extend, 2);
13922           fragp->fr_fix += 2;
13923           buf += 2;
13924         }
13925
13926       md_number_to_chars (buf, insn, 2);
13927       fragp->fr_fix += 2;
13928       buf += 2;
13929     }
13930   else
13931     {
13932       if (fragp->fr_opcode == NULL)
13933         return;
13934
13935       old = RELAX_OLD (fragp->fr_subtype);
13936       new = RELAX_NEW (fragp->fr_subtype);
13937       fixptr = fragp->fr_literal + fragp->fr_fix;
13938
13939       if (new > 0)
13940         memmove (fixptr - old, fixptr, new);
13941
13942       fragp->fr_fix += new - old;
13943     }
13944 }
13945
13946 #ifdef OBJ_ELF
13947
13948 /* This function is called after the relocs have been generated.
13949    We've been storing mips16 text labels as odd.  Here we convert them
13950    back to even for the convenience of the debugger.  */
13951
13952 void
13953 mips_frob_file_after_relocs (void)
13954 {
13955   asymbol **syms;
13956   unsigned int count, i;
13957
13958   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13959     return;
13960
13961   syms = bfd_get_outsymbols (stdoutput);
13962   count = bfd_get_symcount (stdoutput);
13963   for (i = 0; i < count; i++, syms++)
13964     {
13965       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13966           && ((*syms)->value & 1) != 0)
13967         {
13968           (*syms)->value &= ~1;
13969           /* If the symbol has an odd size, it was probably computed
13970              incorrectly, so adjust that as well.  */
13971           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13972             ++elf_symbol (*syms)->internal_elf_sym.st_size;
13973         }
13974     }
13975 }
13976
13977 #endif
13978
13979 /* This function is called whenever a label is defined.  It is used
13980    when handling branch delays; if a branch has a label, we assume we
13981    can not move it.  */
13982
13983 void
13984 mips_define_label (symbolS *sym)
13985 {
13986   struct insn_label_list *l;
13987
13988   if (free_insn_labels == NULL)
13989     l = (struct insn_label_list *) xmalloc (sizeof *l);
13990   else
13991     {
13992       l = free_insn_labels;
13993       free_insn_labels = l->next;
13994     }
13995
13996   l->label = sym;
13997   l->next = insn_labels;
13998   insn_labels = l;
13999 }
14000 \f
14001 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14002
14003 /* Some special processing for a MIPS ELF file.  */
14004
14005 void
14006 mips_elf_final_processing (void)
14007 {
14008   /* Write out the register information.  */
14009   if (mips_abi != N64_ABI)
14010     {
14011       Elf32_RegInfo s;
14012
14013       s.ri_gprmask = mips_gprmask;
14014       s.ri_cprmask[0] = mips_cprmask[0];
14015       s.ri_cprmask[1] = mips_cprmask[1];
14016       s.ri_cprmask[2] = mips_cprmask[2];
14017       s.ri_cprmask[3] = mips_cprmask[3];
14018       /* The gp_value field is set by the MIPS ELF backend.  */
14019
14020       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14021                                        ((Elf32_External_RegInfo *)
14022                                         mips_regmask_frag));
14023     }
14024   else
14025     {
14026       Elf64_Internal_RegInfo s;
14027
14028       s.ri_gprmask = mips_gprmask;
14029       s.ri_pad = 0;
14030       s.ri_cprmask[0] = mips_cprmask[0];
14031       s.ri_cprmask[1] = mips_cprmask[1];
14032       s.ri_cprmask[2] = mips_cprmask[2];
14033       s.ri_cprmask[3] = mips_cprmask[3];
14034       /* The gp_value field is set by the MIPS ELF backend.  */
14035
14036       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14037                                        ((Elf64_External_RegInfo *)
14038                                         mips_regmask_frag));
14039     }
14040
14041   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
14042      sort of BFD interface for this.  */
14043   if (mips_any_noreorder)
14044     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14045   if (mips_pic != NO_PIC)
14046     {
14047     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14048       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14049     }
14050   if (mips_abicalls)
14051     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14052
14053   /* Set MIPS ELF flags for ASEs.  */
14054   if (file_ase_mips16)
14055     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14056 #if 0 /* XXX FIXME */
14057   if (file_ase_mips3d)
14058     elf_elfheader (stdoutput)->e_flags |= ???;
14059 #endif
14060   if (file_ase_mdmx)
14061     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14062
14063   /* Set the MIPS ELF ABI flags.  */
14064   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14065     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14066   else if (mips_abi == O64_ABI)
14067     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14068   else if (mips_abi == EABI_ABI)
14069     {
14070       if (!file_mips_gp32)
14071         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14072       else
14073         elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14074     }
14075   else if (mips_abi == N32_ABI)
14076     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14077
14078   /* Nothing to do for N64_ABI.  */
14079
14080   if (mips_32bitmode)
14081     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14082 }
14083
14084 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14085 \f
14086 typedef struct proc {
14087   symbolS *isym;
14088   unsigned long reg_mask;
14089   unsigned long reg_offset;
14090   unsigned long fpreg_mask;
14091   unsigned long fpreg_offset;
14092   unsigned long frame_offset;
14093   unsigned long frame_reg;
14094   unsigned long pc_reg;
14095 } procS;
14096
14097 static procS cur_proc;
14098 static procS *cur_proc_ptr;
14099 static int numprocs;
14100
14101 /* Fill in an rs_align_code fragment.  */
14102
14103 void
14104 mips_handle_align (fragS *fragp)
14105 {
14106   if (fragp->fr_type != rs_align_code)
14107     return;
14108
14109   if (mips_opts.mips16)
14110     {
14111       static const unsigned char be_nop[] = { 0x65, 0x00 };
14112       static const unsigned char le_nop[] = { 0x00, 0x65 };
14113
14114       int bytes;
14115       char *p;
14116
14117       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14118       p = fragp->fr_literal + fragp->fr_fix;
14119
14120       if (bytes & 1)
14121         {
14122           *p++ = 0;
14123           fragp->fr_fix++;
14124         }
14125
14126       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14127       fragp->fr_var = 2;
14128     }
14129
14130   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
14131 }
14132
14133 static void
14134 md_obj_begin (void)
14135 {
14136 }
14137
14138 static void
14139 md_obj_end (void)
14140 {
14141   /* check for premature end, nesting errors, etc */
14142   if (cur_proc_ptr)
14143     as_warn (_("missing .end at end of assembly"));
14144 }
14145
14146 static long
14147 get_number (void)
14148 {
14149   int negative = 0;
14150   long val = 0;
14151
14152   if (*input_line_pointer == '-')
14153     {
14154       ++input_line_pointer;
14155       negative = 1;
14156     }
14157   if (!ISDIGIT (*input_line_pointer))
14158     as_bad (_("expected simple number"));
14159   if (input_line_pointer[0] == '0')
14160     {
14161       if (input_line_pointer[1] == 'x')
14162         {
14163           input_line_pointer += 2;
14164           while (ISXDIGIT (*input_line_pointer))
14165             {
14166               val <<= 4;
14167               val |= hex_value (*input_line_pointer++);
14168             }
14169           return negative ? -val : val;
14170         }
14171       else
14172         {
14173           ++input_line_pointer;
14174           while (ISDIGIT (*input_line_pointer))
14175             {
14176               val <<= 3;
14177               val |= *input_line_pointer++ - '0';
14178             }
14179           return negative ? -val : val;
14180         }
14181     }
14182   if (!ISDIGIT (*input_line_pointer))
14183     {
14184       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14185               *input_line_pointer, *input_line_pointer);
14186       as_warn (_("invalid number"));
14187       return -1;
14188     }
14189   while (ISDIGIT (*input_line_pointer))
14190     {
14191       val *= 10;
14192       val += *input_line_pointer++ - '0';
14193     }
14194   return negative ? -val : val;
14195 }
14196
14197 /* The .file directive; just like the usual .file directive, but there
14198    is an initial number which is the ECOFF file index.  In the non-ECOFF
14199    case .file implies DWARF-2.  */
14200
14201 static void
14202 s_mips_file (int x ATTRIBUTE_UNUSED)
14203 {
14204   static int first_file_directive = 0;
14205
14206   if (ECOFF_DEBUGGING)
14207     {
14208       get_number ();
14209       s_app_file (0);
14210     }
14211   else
14212     {
14213       char *filename;
14214
14215       filename = dwarf2_directive_file (0);
14216
14217       /* Versions of GCC up to 3.1 start files with a ".file"
14218          directive even for stabs output.  Make sure that this
14219          ".file" is handled.  Note that you need a version of GCC
14220          after 3.1 in order to support DWARF-2 on MIPS.  */
14221       if (filename != NULL && ! first_file_directive)
14222         {
14223           (void) new_logical_line (filename, -1);
14224           s_app_file_string (filename);
14225         }
14226       first_file_directive = 1;
14227     }
14228 }
14229
14230 /* The .loc directive, implying DWARF-2.  */
14231
14232 static void
14233 s_mips_loc (int x ATTRIBUTE_UNUSED)
14234 {
14235   if (!ECOFF_DEBUGGING)
14236     dwarf2_directive_loc (0);
14237 }
14238
14239 /* The .end directive.  */
14240
14241 static void
14242 s_mips_end (int x ATTRIBUTE_UNUSED)
14243 {
14244   symbolS *p;
14245
14246   /* Following functions need their own .frame and .cprestore directives.  */
14247   mips_frame_reg_valid = 0;
14248   mips_cprestore_valid = 0;
14249
14250   if (!is_end_of_line[(unsigned char) *input_line_pointer])
14251     {
14252       p = get_symbol ();
14253       demand_empty_rest_of_line ();
14254     }
14255   else
14256     p = NULL;
14257
14258   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14259     as_warn (_(".end not in text section"));
14260
14261   if (!cur_proc_ptr)
14262     {
14263       as_warn (_(".end directive without a preceding .ent directive."));
14264       demand_empty_rest_of_line ();
14265       return;
14266     }
14267
14268   if (p != NULL)
14269     {
14270       assert (S_GET_NAME (p));
14271       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14272         as_warn (_(".end symbol does not match .ent symbol."));
14273
14274       if (debug_type == DEBUG_STABS)
14275         stabs_generate_asm_endfunc (S_GET_NAME (p),
14276                                     S_GET_NAME (p));
14277     }
14278   else
14279     as_warn (_(".end directive missing or unknown symbol"));
14280
14281 #ifdef OBJ_ELF
14282   /* Generate a .pdr section.  */
14283   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14284       && mips_flag_pdr)
14285     {
14286       segT saved_seg = now_seg;
14287       subsegT saved_subseg = now_subseg;
14288       valueT dot;
14289       expressionS exp;
14290       char *fragp;
14291
14292       dot = frag_now_fix ();
14293
14294 #ifdef md_flush_pending_output
14295       md_flush_pending_output ();
14296 #endif
14297
14298       assert (pdr_seg);
14299       subseg_set (pdr_seg, 0);
14300
14301       /* Write the symbol.  */
14302       exp.X_op = O_symbol;
14303       exp.X_add_symbol = p;
14304       exp.X_add_number = 0;
14305       emit_expr (&exp, 4);
14306
14307       fragp = frag_more (7 * 4);
14308
14309       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14310       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14311       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14312       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14313       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14314       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14315       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14316
14317       subseg_set (saved_seg, saved_subseg);
14318     }
14319 #endif /* OBJ_ELF */
14320
14321   cur_proc_ptr = NULL;
14322 }
14323
14324 /* The .aent and .ent directives.  */
14325
14326 static void
14327 s_mips_ent (int aent)
14328 {
14329   symbolS *symbolP;
14330
14331   symbolP = get_symbol ();
14332   if (*input_line_pointer == ',')
14333     ++input_line_pointer;
14334   SKIP_WHITESPACE ();
14335   if (ISDIGIT (*input_line_pointer)
14336       || *input_line_pointer == '-')
14337     get_number ();
14338
14339   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14340     as_warn (_(".ent or .aent not in text section."));
14341
14342   if (!aent && cur_proc_ptr)
14343     as_warn (_("missing .end"));
14344
14345   if (!aent)
14346     {
14347       /* This function needs its own .frame and .cprestore directives.  */
14348       mips_frame_reg_valid = 0;
14349       mips_cprestore_valid = 0;
14350
14351       cur_proc_ptr = &cur_proc;
14352       memset (cur_proc_ptr, '\0', sizeof (procS));
14353
14354       cur_proc_ptr->isym = symbolP;
14355
14356       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14357
14358       ++numprocs;
14359
14360       if (debug_type == DEBUG_STABS)
14361         stabs_generate_asm_func (S_GET_NAME (symbolP),
14362                                  S_GET_NAME (symbolP));
14363     }
14364
14365   demand_empty_rest_of_line ();
14366 }
14367
14368 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14369    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14370    s_mips_frame is used so that we can set the PDR information correctly.
14371    We can't use the ecoff routines because they make reference to the ecoff
14372    symbol table (in the mdebug section).  */
14373
14374 static void
14375 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14376 {
14377 #ifdef OBJ_ELF
14378   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14379     {
14380       long val;
14381
14382       if (cur_proc_ptr == (procS *) NULL)
14383         {
14384           as_warn (_(".frame outside of .ent"));
14385           demand_empty_rest_of_line ();
14386           return;
14387         }
14388
14389       cur_proc_ptr->frame_reg = tc_get_register (1);
14390
14391       SKIP_WHITESPACE ();
14392       if (*input_line_pointer++ != ','
14393           || get_absolute_expression_and_terminator (&val) != ',')
14394         {
14395           as_warn (_("Bad .frame directive"));
14396           --input_line_pointer;
14397           demand_empty_rest_of_line ();
14398           return;
14399         }
14400
14401       cur_proc_ptr->frame_offset = val;
14402       cur_proc_ptr->pc_reg = tc_get_register (0);
14403
14404       demand_empty_rest_of_line ();
14405     }
14406   else
14407 #endif /* OBJ_ELF */
14408     s_ignore (ignore);
14409 }
14410
14411 /* The .fmask and .mask directives. If the mdebug section is present
14412    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14413    embedded targets, s_mips_mask is used so that we can set the PDR
14414    information correctly. We can't use the ecoff routines because they
14415    make reference to the ecoff symbol table (in the mdebug section).  */
14416
14417 static void
14418 s_mips_mask (int reg_type)
14419 {
14420 #ifdef OBJ_ELF
14421   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14422     {
14423       long mask, off;
14424
14425       if (cur_proc_ptr == (procS *) NULL)
14426         {
14427           as_warn (_(".mask/.fmask outside of .ent"));
14428           demand_empty_rest_of_line ();
14429           return;
14430         }
14431
14432       if (get_absolute_expression_and_terminator (&mask) != ',')
14433         {
14434           as_warn (_("Bad .mask/.fmask directive"));
14435           --input_line_pointer;
14436           demand_empty_rest_of_line ();
14437           return;
14438         }
14439
14440       off = get_absolute_expression ();
14441
14442       if (reg_type == 'F')
14443         {
14444           cur_proc_ptr->fpreg_mask = mask;
14445           cur_proc_ptr->fpreg_offset = off;
14446         }
14447       else
14448         {
14449           cur_proc_ptr->reg_mask = mask;
14450           cur_proc_ptr->reg_offset = off;
14451         }
14452
14453       demand_empty_rest_of_line ();
14454     }
14455   else
14456 #endif /* OBJ_ELF */
14457     s_ignore (reg_type);
14458 }
14459
14460 /* The .loc directive.  */
14461
14462 #if 0
14463 static void
14464 s_loc (int x)
14465 {
14466   symbolS *symbolP;
14467   int lineno;
14468   int addroff;
14469
14470   assert (now_seg == text_section);
14471
14472   lineno = get_number ();
14473   addroff = frag_now_fix ();
14474
14475   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14476   S_SET_TYPE (symbolP, N_SLINE);
14477   S_SET_OTHER (symbolP, 0);
14478   S_SET_DESC (symbolP, lineno);
14479   symbolP->sy_segment = now_seg;
14480 }
14481 #endif
14482
14483 /* A table describing all the processors gas knows about.  Names are
14484    matched in the order listed.
14485
14486    To ease comparison, please keep this table in the same order as
14487    gcc's mips_cpu_info_table[].  */
14488 static const struct mips_cpu_info mips_cpu_info_table[] =
14489 {
14490   /* Entries for generic ISAs */
14491   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
14492   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
14493   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
14494   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
14495   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
14496   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
14497   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
14498   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
14499   { "mips64r2",       1,      ISA_MIPS64R2,   CPU_MIPS64R2 },
14500
14501   /* MIPS I */
14502   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
14503   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
14504   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
14505
14506   /* MIPS II */
14507   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
14508
14509   /* MIPS III */
14510   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
14511   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
14512   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
14513   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
14514   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
14515   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
14516   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
14517   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
14518   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
14519   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
14520   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
14521   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
14522
14523   /* MIPS IV */
14524   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
14525   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
14526   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
14527   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
14528   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
14529   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
14530   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
14531   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
14532   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
14533   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
14534   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
14535   { "rm7000",         0,      ISA_MIPS4,      CPU_RM7000 },
14536   { "rm9000",         0,      ISA_MIPS4,      CPU_RM7000 },
14537
14538   /* MIPS 32 */
14539   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32 },
14540   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
14541   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
14542
14543   /* MIPS 64 */
14544   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
14545   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
14546
14547   /* Broadcom SB-1 CPU core */
14548   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
14549
14550   /* End marker */
14551   { NULL, 0, 0, 0 }
14552 };
14553
14554
14555 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14556    with a final "000" replaced by "k".  Ignore case.
14557
14558    Note: this function is shared between GCC and GAS.  */
14559
14560 static bfd_boolean
14561 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14562 {
14563   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14564     given++, canonical++;
14565
14566   return ((*given == 0 && *canonical == 0)
14567           || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14568 }
14569
14570
14571 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14572    CPU name.  We've traditionally allowed a lot of variation here.
14573
14574    Note: this function is shared between GCC and GAS.  */
14575
14576 static bfd_boolean
14577 mips_matching_cpu_name_p (const char *canonical, const char *given)
14578 {
14579   /* First see if the name matches exactly, or with a final "000"
14580      turned into "k".  */
14581   if (mips_strict_matching_cpu_name_p (canonical, given))
14582     return TRUE;
14583
14584   /* If not, try comparing based on numerical designation alone.
14585      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
14586   if (TOLOWER (*given) == 'r')
14587     given++;
14588   if (!ISDIGIT (*given))
14589     return FALSE;
14590
14591   /* Skip over some well-known prefixes in the canonical name,
14592      hoping to find a number there too.  */
14593   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14594     canonical += 2;
14595   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14596     canonical += 2;
14597   else if (TOLOWER (canonical[0]) == 'r')
14598     canonical += 1;
14599
14600   return mips_strict_matching_cpu_name_p (canonical, given);
14601 }
14602
14603
14604 /* Parse an option that takes the name of a processor as its argument.
14605    OPTION is the name of the option and CPU_STRING is the argument.
14606    Return the corresponding processor enumeration if the CPU_STRING is
14607    recognized, otherwise report an error and return null.
14608
14609    A similar function exists in GCC.  */
14610
14611 static const struct mips_cpu_info *
14612 mips_parse_cpu (const char *option, const char *cpu_string)
14613 {
14614   const struct mips_cpu_info *p;
14615
14616   /* 'from-abi' selects the most compatible architecture for the given
14617      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
14618      EABIs, we have to decide whether we're using the 32-bit or 64-bit
14619      version.  Look first at the -mgp options, if given, otherwise base
14620      the choice on MIPS_DEFAULT_64BIT.
14621
14622      Treat NO_ABI like the EABIs.  One reason to do this is that the
14623      plain 'mips' and 'mips64' configs have 'from-abi' as their default
14624      architecture.  This code picks MIPS I for 'mips' and MIPS III for
14625      'mips64', just as we did in the days before 'from-abi'.  */
14626   if (strcasecmp (cpu_string, "from-abi") == 0)
14627     {
14628       if (ABI_NEEDS_32BIT_REGS (mips_abi))
14629         return mips_cpu_info_from_isa (ISA_MIPS1);
14630
14631       if (ABI_NEEDS_64BIT_REGS (mips_abi))
14632         return mips_cpu_info_from_isa (ISA_MIPS3);
14633
14634       if (file_mips_gp32 >= 0)
14635         return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14636
14637       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14638                                      ? ISA_MIPS3
14639                                      : ISA_MIPS1);
14640     }
14641
14642   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
14643   if (strcasecmp (cpu_string, "default") == 0)
14644     return 0;
14645
14646   for (p = mips_cpu_info_table; p->name != 0; p++)
14647     if (mips_matching_cpu_name_p (p->name, cpu_string))
14648       return p;
14649
14650   as_bad ("Bad value (%s) for %s", cpu_string, option);
14651   return 0;
14652 }
14653
14654 /* Return the canonical processor information for ISA (a member of the
14655    ISA_MIPS* enumeration).  */
14656
14657 static const struct mips_cpu_info *
14658 mips_cpu_info_from_isa (int isa)
14659 {
14660   int i;
14661
14662   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14663     if (mips_cpu_info_table[i].is_isa
14664         && isa == mips_cpu_info_table[i].isa)
14665       return (&mips_cpu_info_table[i]);
14666
14667   return NULL;
14668 }
14669
14670 static const struct mips_cpu_info *
14671 mips_cpu_info_from_arch (int arch)
14672 {
14673   int i;
14674
14675   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14676     if (arch == mips_cpu_info_table[i].cpu)
14677       return (&mips_cpu_info_table[i]);
14678
14679   return NULL;
14680 }
14681 \f
14682 static void
14683 show (FILE *stream, const char *string, int *col_p, int *first_p)
14684 {
14685   if (*first_p)
14686     {
14687       fprintf (stream, "%24s", "");
14688       *col_p = 24;
14689     }
14690   else
14691     {
14692       fprintf (stream, ", ");
14693       *col_p += 2;
14694     }
14695
14696   if (*col_p + strlen (string) > 72)
14697     {
14698       fprintf (stream, "\n%24s", "");
14699       *col_p = 24;
14700     }
14701
14702   fprintf (stream, "%s", string);
14703   *col_p += strlen (string);
14704
14705   *first_p = 0;
14706 }
14707
14708 void
14709 md_show_usage (FILE *stream)
14710 {
14711   int column, first;
14712   size_t i;
14713
14714   fprintf (stream, _("\
14715 MIPS options:\n\
14716 -membedded-pic          generate embedded position independent code\n\
14717 -EB                     generate big endian output\n\
14718 -EL                     generate little endian output\n\
14719 -g, -g2                 do not remove unneeded NOPs or swap branches\n\
14720 -G NUM                  allow referencing objects up to NUM bytes\n\
14721                         implicitly with the gp register [default 8]\n"));
14722   fprintf (stream, _("\
14723 -mips1                  generate MIPS ISA I instructions\n\
14724 -mips2                  generate MIPS ISA II instructions\n\
14725 -mips3                  generate MIPS ISA III instructions\n\
14726 -mips4                  generate MIPS ISA IV instructions\n\
14727 -mips5                  generate MIPS ISA V instructions\n\
14728 -mips32                 generate MIPS32 ISA instructions\n\
14729 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
14730 -mips64                 generate MIPS64 ISA instructions\n\
14731 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
14732 -march=CPU/-mtune=CPU   generate code/schedule for CPU, where CPU is one of:\n"));
14733
14734   first = 1;
14735
14736   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14737     show (stream, mips_cpu_info_table[i].name, &column, &first);
14738   show (stream, "from-abi", &column, &first);
14739   fputc ('\n', stream);
14740
14741   fprintf (stream, _("\
14742 -mCPU                   equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14743 -no-mCPU                don't generate code specific to CPU.\n\
14744                         For -mCPU and -no-mCPU, CPU must be one of:\n"));
14745
14746   first = 1;
14747
14748   show (stream, "3900", &column, &first);
14749   show (stream, "4010", &column, &first);
14750   show (stream, "4100", &column, &first);
14751   show (stream, "4650", &column, &first);
14752   fputc ('\n', stream);
14753
14754   fprintf (stream, _("\
14755 -mips16                 generate mips16 instructions\n\
14756 -no-mips16              do not generate mips16 instructions\n"));
14757   fprintf (stream, _("\
14758 -mgp32                  use 32-bit GPRs, regardless of the chosen ISA\n\
14759 -mfp32                  use 32-bit FPRs, regardless of the chosen ISA\n\
14760 -O0                     remove unneeded NOPs, do not swap branches\n\
14761 -O                      remove unneeded NOPs and swap branches\n\
14762 -n                      warn about NOPs generated from macros\n\
14763 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14764 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
14765 --break, --no-trap      break exception on div by 0 and mult overflow\n"));
14766 #ifdef OBJ_ELF
14767   fprintf (stream, _("\
14768 -KPIC, -call_shared     generate SVR4 position independent code\n\
14769 -non_shared             do not generate position independent code\n\
14770 -xgot                   assume a 32 bit GOT\n\
14771 -mpdr, -mno-pdr         enable/disable creation of .pdr sections\n\
14772 -mabi=ABI               create ABI conformant object file for:\n"));
14773
14774   first = 1;
14775
14776   show (stream, "32", &column, &first);
14777   show (stream, "o64", &column, &first);
14778   show (stream, "n32", &column, &first);
14779   show (stream, "64", &column, &first);
14780   show (stream, "eabi", &column, &first);
14781
14782   fputc ('\n', stream);
14783
14784   fprintf (stream, _("\
14785 -32                     create o32 ABI object file (default)\n\
14786 -n32                    create n32 ABI object file\n\
14787 -64                     create 64 ABI object file\n"));
14788 #endif
14789 }
14790
14791 enum dwarf2_format
14792 mips_dwarf2_format (void)
14793 {
14794   if (mips_abi == N64_ABI)
14795     {
14796 #ifdef TE_IRIX
14797       return dwarf2_format_64bit_irix;
14798 #else
14799       return dwarf2_format_64bit;
14800 #endif
14801     }
14802   else
14803     return dwarf2_format_32bit;
14804 }