1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005 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 #include "dw2gencfi.h"
37 
38 #ifdef DEBUG
39 #define DBG(x) printf x
40 #else
41 #define DBG(x)
42 #endif
43 
44 #ifdef OBJ_MAYBE_ELF
45 /* Clean up namespace so we can include obj-elf.h too.  */
46 static int mips_output_flavor (void);
mips_output_flavor(void)47 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
48 #undef OBJ_PROCESS_STAB
49 #undef OUTPUT_FLAVOR
50 #undef S_GET_ALIGN
51 #undef S_GET_SIZE
52 #undef S_SET_ALIGN
53 #undef S_SET_SIZE
54 #undef obj_frob_file
55 #undef obj_frob_file_after_relocs
56 #undef obj_frob_symbol
57 #undef obj_pop_insert
58 #undef obj_sec_sym_ok_for_reloc
59 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
60 
61 #include "obj-elf.h"
62 /* Fix any of them that we actually care about.  */
63 #undef OUTPUT_FLAVOR
64 #define OUTPUT_FLAVOR mips_output_flavor()
65 #endif
66 
67 #if defined (OBJ_ELF)
68 #include "elf/mips.h"
69 #endif
70 
71 #ifndef ECOFF_DEBUGGING
72 #define NO_ECOFF_DEBUGGING
73 #define ECOFF_DEBUGGING 0
74 #endif
75 
76 int mips_flag_mdebug = -1;
77 
78 /* Control generation of .pdr sections.  Off by default on IRIX: the native
79    linker doesn't know about and discards them, but relocations against them
80    remain, leading to rld crashes.  */
81 #ifdef TE_IRIX
82 int mips_flag_pdr = FALSE;
83 #else
84 int mips_flag_pdr = TRUE;
85 #endif
86 
87 #include "ecoff.h"
88 
89 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
90 static char *mips_regmask_frag;
91 #endif
92 
93 #define ZERO 0
94 #define AT  1
95 #define TREG 24
96 #define PIC_CALL_REG 25
97 #define KT0 26
98 #define KT1 27
99 #define GP  28
100 #define SP  29
101 #define FP  30
102 #define RA  31
103 
104 #define ILLEGAL_REG (32)
105 
106 /* Allow override of standard little-endian ECOFF format.  */
107 
108 #ifndef ECOFF_LITTLE_FORMAT
109 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
110 #endif
111 
112 extern int target_big_endian;
113 
114 /* The name of the readonly data section.  */
115 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
116 			    ? ".rdata" \
117 			    : OUTPUT_FLAVOR == bfd_target_coff_flavour \
118 			    ? ".rdata" \
119 			    : OUTPUT_FLAVOR == bfd_target_elf_flavour \
120 			    ? ".rodata" \
121 			    : (abort (), ""))
122 
123 /* Information about an instruction, including its format, operands
124    and fixups.  */
125 struct mips_cl_insn
126 {
127   /* The opcode's entry in mips_opcodes or mips16_opcodes.  */
128   const struct mips_opcode *insn_mo;
129 
130   /* True if this is a mips16 instruction and if we want the extended
131      form of INSN_MO.  */
132   bfd_boolean use_extend;
133 
134   /* The 16-bit extension instruction to use when USE_EXTEND is true.  */
135   unsigned short extend;
136 
137   /* The 16-bit or 32-bit bitstring of the instruction itself.  This is
138      a copy of INSN_MO->match with the operands filled in.  */
139   unsigned long insn_opcode;
140 
141   /* The frag that contains the instruction.  */
142   struct frag *frag;
143 
144   /* The offset into FRAG of the first instruction byte.  */
145   long where;
146 
147   /* The relocs associated with the instruction, if any.  */
148   fixS *fixp[3];
149 
150   /* True if this entry cannot be moved from its current position.  */
151   unsigned int fixed_p : 1;
152 
153   /* True if this instruction occured in a .set noreorder block.  */
154   unsigned int noreorder_p : 1;
155 
156   /* True for mips16 instructions that jump to an absolute address.  */
157   unsigned int mips16_absolute_jump_p : 1;
158 };
159 
160 /* The ABI to use.  */
161 enum mips_abi_level
162 {
163   NO_ABI = 0,
164   O32_ABI,
165   O64_ABI,
166   N32_ABI,
167   N64_ABI,
168   EABI_ABI
169 };
170 
171 /* MIPS ABI we are using for this output file.  */
172 static enum mips_abi_level mips_abi = NO_ABI;
173 
174 /* Whether or not we have code that can call pic code.  */
175 int mips_abicalls = FALSE;
176 
177 /* Whether or not we have code which can be put into a shared
178    library.  */
179 static bfd_boolean mips_in_shared = TRUE;
180 
181 /* This is the set of options which may be modified by the .set
182    pseudo-op.  We use a struct so that .set push and .set pop are more
183    reliable.  */
184 
185 struct mips_set_options
186 {
187   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
188      if it has not been initialized.  Changed by `.set mipsN', and the
189      -mipsN command line option, and the default CPU.  */
190   int isa;
191   /* Enabled Application Specific Extensions (ASEs).  These are set to -1
192      if they have not been initialized.  Changed by `.set <asename>', by
193      command line options, and based on the default architecture.  */
194   int ase_mips3d;
195   int ase_mdmx;
196   /* Whether we are assembling for the mips16 processor.  0 if we are
197      not, 1 if we are, and -1 if the value has not been initialized.
198      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
199      -nomips16 command line options, and the default CPU.  */
200   int mips16;
201   /* Non-zero if we should not reorder instructions.  Changed by `.set
202      reorder' and `.set noreorder'.  */
203   int noreorder;
204   /* Non-zero if we should not permit the $at ($1) register to be used
205      in instructions.  Changed by `.set at' and `.set noat'.  */
206   int noat;
207   /* Non-zero if we should warn when a macro instruction expands into
208      more than one machine instruction.  Changed by `.set nomacro' and
209      `.set macro'.  */
210   int warn_about_macros;
211   /* Non-zero if we should not move instructions.  Changed by `.set
212      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
213   int nomove;
214   /* Non-zero if we should not optimize branches by moving the target
215      of the branch into the delay slot.  Actually, we don't perform
216      this optimization anyhow.  Changed by `.set bopt' and `.set
217      nobopt'.  */
218   int nobopt;
219   /* Non-zero if we should not autoextend mips16 instructions.
220      Changed by `.set autoextend' and `.set noautoextend'.  */
221   int noautoextend;
222   /* Restrict general purpose registers and floating point registers
223      to 32 bit.  This is initially determined when -mgp32 or -mfp32
224      is passed but can changed if the assembler code uses .set mipsN.  */
225   int gp32;
226   int fp32;
227   /* MIPS architecture (CPU) type.  Changed by .set arch=FOO, the -march
228      command line option, and the default CPU.  */
229   int arch;
230   /* True if ".set sym32" is in effect.  */
231   bfd_boolean sym32;
232 };
233 
234 /* True if -mgp32 was passed.  */
235 static int file_mips_gp32 = -1;
236 
237 /* True if -mfp32 was passed.  */
238 static int file_mips_fp32 = -1;
239 
240 /* This is the struct we use to hold the current set of options.  Note
241    that we must set the isa field to ISA_UNKNOWN and the ASE fields to
242    -1 to indicate that they have not been initialized.  */
243 
244 static struct mips_set_options mips_opts =
245 {
246   ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
247 };
248 
249 /* These variables are filled in with the masks of registers used.
250    The object format code reads them and puts them in the appropriate
251    place.  */
252 unsigned long mips_gprmask;
253 unsigned long mips_cprmask[4];
254 
255 /* MIPS ISA we are using for this output file.  */
256 static int file_mips_isa = ISA_UNKNOWN;
257 
258 /* True if -mips16 was passed or implied by arguments passed on the
259    command line (e.g., by -march).  */
260 static int file_ase_mips16;
261 
262 /* True if -mips3d was passed or implied by arguments passed on the
263    command line (e.g., by -march).  */
264 static int file_ase_mips3d;
265 
266 /* True if -mdmx was passed or implied by arguments passed on the
267    command line (e.g., by -march).  */
268 static int file_ase_mdmx;
269 
270 /* The argument of the -march= flag.  The architecture we are assembling.  */
271 static int file_mips_arch = CPU_UNKNOWN;
272 static const char *mips_arch_string;
273 
274 /* The argument of the -mtune= flag.  The architecture for which we
275    are optimizing.  */
276 static int mips_tune = CPU_UNKNOWN;
277 static const char *mips_tune_string;
278 
279 /* True when generating 32-bit code for a 64-bit processor.  */
280 static int mips_32bitmode = 0;
281 
282 /* True if the given ABI requires 32-bit registers.  */
283 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
284 
285 /* Likewise 64-bit registers.  */
286 #define ABI_NEEDS_64BIT_REGS(ABI) \
287   ((ABI) == N32_ABI 		  \
288    || (ABI) == N64_ABI		  \
289    || (ABI) == O64_ABI)
290 
291 /*  Return true if ISA supports 64 bit gp register instructions.  */
292 #define ISA_HAS_64BIT_REGS(ISA) (    \
293    (ISA) == ISA_MIPS3                \
294    || (ISA) == ISA_MIPS4             \
295    || (ISA) == ISA_MIPS5             \
296    || (ISA) == ISA_MIPS64            \
297    || (ISA) == ISA_MIPS64R2          \
298    )
299 
300 /* Return true if ISA supports 64-bit right rotate (dror et al.)
301    instructions.  */
302 #define ISA_HAS_DROR(ISA) (	\
303    (ISA) == ISA_MIPS64R2	\
304    )
305 
306 /* Return true if ISA supports 32-bit right rotate (ror et al.)
307    instructions.  */
308 #define ISA_HAS_ROR(ISA) (	\
309    (ISA) == ISA_MIPS32R2	\
310    || (ISA) == ISA_MIPS64R2	\
311    )
312 
313 #define HAVE_32BIT_GPRS		                   \
314     (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
315 
316 #define HAVE_32BIT_FPRS                            \
317     (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
318 
319 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
320 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
321 
322 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
323 
324 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
325 
326 /* True if relocations are stored in-place.  */
327 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
328 
329 /* The ABI-derived address size.  */
330 #define HAVE_64BIT_ADDRESSES \
331   (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
332 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
333 
334 /* The size of symbolic constants (i.e., expressions of the form
335    "SYMBOL" or "SYMBOL + OFFSET").  */
336 #define HAVE_32BIT_SYMBOLS \
337   (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
338 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
339 
340 /* Addresses are loaded in different ways, depending on the address size
341    in use.  The n32 ABI Documentation also mandates the use of additions
342    with overflow checking, but existing implementations don't follow it.  */
343 #define ADDRESS_ADD_INSN						\
344    (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
345 
346 #define ADDRESS_ADDI_INSN						\
347    (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
348 
349 #define ADDRESS_LOAD_INSN						\
350    (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
351 
352 #define ADDRESS_STORE_INSN						\
353    (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
354 
355 /* Return true if the given CPU supports the MIPS16 ASE.  */
356 #define CPU_HAS_MIPS16(cpu)						\
357    (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0		\
358     || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
359 
360 /* Return true if the given CPU supports the MIPS3D ASE.  */
361 #define CPU_HAS_MIPS3D(cpu)	((cpu) == CPU_SB1      \
362 				 )
363 
364 /* Return true if the given CPU supports the MDMX ASE.  */
365 #define CPU_HAS_MDMX(cpu)	(FALSE                 \
366 				 )
367 
368 /* True if CPU has a dror instruction.  */
369 #define CPU_HAS_DROR(CPU)	((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
370 
371 /* True if CPU has a ror instruction.  */
372 #define CPU_HAS_ROR(CPU)	CPU_HAS_DROR (CPU)
373 
374 /* True if mflo and mfhi can be immediately followed by instructions
375    which write to the HI and LO registers.
376 
377    According to MIPS specifications, MIPS ISAs I, II, and III need
378    (at least) two instructions between the reads of HI/LO and
379    instructions which write them, and later ISAs do not.  Contradicting
380    the MIPS specifications, some MIPS IV processor user manuals (e.g.
381    the UM for the NEC Vr5000) document needing the instructions between
382    HI/LO reads and writes, as well.  Therefore, we declare only MIPS32,
383    MIPS64 and later ISAs to have the interlocks, plus any specific
384    earlier-ISA CPUs for which CPU documentation declares that the
385    instructions are really interlocked.  */
386 #define hilo_interlocks \
387   (mips_opts.isa == ISA_MIPS32                        \
388    || mips_opts.isa == ISA_MIPS32R2                   \
389    || mips_opts.isa == ISA_MIPS64                     \
390    || mips_opts.isa == ISA_MIPS64R2                   \
391    || mips_opts.arch == CPU_R4010                     \
392    || mips_opts.arch == CPU_R10000                    \
393    || mips_opts.arch == CPU_R12000                    \
394    || mips_opts.arch == CPU_RM7000                    \
395    || mips_opts.arch == CPU_VR5500                    \
396    )
397 
398 /* Whether the processor uses hardware interlocks to protect reads
399    from the GPRs after they are loaded from memory, and thus does not
400    require nops to be inserted.  This applies to instructions marked
401    INSN_LOAD_MEMORY_DELAY.  These nops are only required at MIPS ISA
402    level I.  */
403 #define gpr_interlocks \
404   (mips_opts.isa != ISA_MIPS1  \
405    || mips_opts.arch == CPU_R3900)
406 
407 /* Whether the processor uses hardware interlocks to avoid delays
408    required by coprocessor instructions, and thus does not require
409    nops to be inserted.  This applies to instructions marked
410    INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
411    between instructions marked INSN_WRITE_COND_CODE and ones marked
412    INSN_READ_COND_CODE.  These nops are only required at MIPS ISA
413    levels I, II, and III.  */
414 /* Itbl support may require additional care here.  */
415 #define cop_interlocks                                \
416   ((mips_opts.isa != ISA_MIPS1                        \
417     && mips_opts.isa != ISA_MIPS2                     \
418     && mips_opts.isa != ISA_MIPS3)                    \
419    || mips_opts.arch == CPU_R4300                     \
420    )
421 
422 /* Whether the processor uses hardware interlocks to protect reads
423    from coprocessor registers after they are loaded from memory, and
424    thus does not require nops to be inserted.  This applies to
425    instructions marked INSN_COPROC_MEMORY_DELAY.  These nops are only
426    requires at MIPS ISA level I.  */
427 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
428 
429 /* Is this a mfhi or mflo instruction?  */
430 #define MF_HILO_INSN(PINFO) \
431           ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
432 
433 /* MIPS PIC level.  */
434 
435 enum mips_pic_level mips_pic;
436 
437 /* 1 if we should generate 32 bit offsets from the $gp register in
438    SVR4_PIC mode.  Currently has no meaning in other modes.  */
439 static int mips_big_got = 0;
440 
441 /* 1 if trap instructions should used for overflow rather than break
442    instructions.  */
443 static int mips_trap = 0;
444 
445 /* 1 if double width floating point constants should not be constructed
446    by assembling two single width halves into two single width floating
447    point registers which just happen to alias the double width destination
448    register.  On some architectures this aliasing can be disabled by a bit
449    in the status register, and the setting of this bit cannot be determined
450    automatically at assemble time.  */
451 static int mips_disable_float_construction;
452 
453 /* Non-zero if any .set noreorder directives were used.  */
454 
455 static int mips_any_noreorder;
456 
457 /* Non-zero if nops should be inserted when the register referenced in
458    an mfhi/mflo instruction is read in the next two instructions.  */
459 static int mips_7000_hilo_fix;
460 
461 /* The size of the small data section.  */
462 static unsigned int g_switch_value = 8;
463 /* Whether the -G option was used.  */
464 static int g_switch_seen = 0;
465 
466 #define N_RMASK 0xc4
467 #define N_VFP   0xd4
468 
469 /* If we can determine in advance that GP optimization won't be
470    possible, we can skip the relaxation stuff that tries to produce
471    GP-relative references.  This makes delay slot optimization work
472    better.
473 
474    This function can only provide a guess, but it seems to work for
475    gcc output.  It needs to guess right for gcc, otherwise gcc
476    will put what it thinks is a GP-relative instruction in a branch
477    delay slot.
478 
479    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
480    fixed it for the non-PIC mode.  KR 95/04/07  */
481 static int nopic_need_relax (symbolS *, int);
482 
483 /* handle of the OPCODE hash table */
484 static struct hash_control *op_hash = NULL;
485 
486 /* The opcode hash table we use for the mips16.  */
487 static struct hash_control *mips16_op_hash = NULL;
488 
489 /* This array holds the chars that always start a comment.  If the
490     pre-processor is disabled, these aren't very useful */
491 const char comment_chars[] = "#";
492 
493 /* This array holds the chars that only start a comment at the beginning of
494    a line.  If the line seems to have the form '# 123 filename'
495    .line and .file directives will appear in the pre-processed output */
496 /* Note that input_file.c hand checks for '#' at the beginning of the
497    first line of the input file.  This is because the compiler outputs
498    #NO_APP at the beginning of its output.  */
499 /* Also note that C style comments are always supported.  */
500 const char line_comment_chars[] = "#";
501 
502 /* This array holds machine specific line separator characters.  */
503 const char line_separator_chars[] = ";";
504 
505 /* Chars that can be used to separate mant from exp in floating point nums */
506 const char EXP_CHARS[] = "eE";
507 
508 /* Chars that mean this number is a floating point constant */
509 /* As in 0f12.456 */
510 /* or    0d1.2345e12 */
511 const char FLT_CHARS[] = "rRsSfFdDxXpP";
512 
513 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
514    changed in read.c .  Ideally it shouldn't have to know about it at all,
515    but nothing is ideal around here.
516  */
517 
518 static char *insn_error;
519 
520 static int auto_align = 1;
521 
522 /* When outputting SVR4 PIC code, the assembler needs to know the
523    offset in the stack frame from which to restore the $gp register.
524    This is set by the .cprestore pseudo-op, and saved in this
525    variable.  */
526 static offsetT mips_cprestore_offset = -1;
527 
528 /* Similar for NewABI PIC code, where $gp is callee-saved.  NewABI has some
529    more optimizations, it can use a register value instead of a memory-saved
530    offset and even an other register than $gp as global pointer.  */
531 static offsetT mips_cpreturn_offset = -1;
532 static int mips_cpreturn_register = -1;
533 static int mips_gp_register = GP;
534 static int mips_gprel_offset = 0;
535 
536 /* Whether mips_cprestore_offset has been set in the current function
537    (or whether it has already been warned about, if not).  */
538 static int mips_cprestore_valid = 0;
539 
540 /* This is the register which holds the stack frame, as set by the
541    .frame pseudo-op.  This is needed to implement .cprestore.  */
542 static int mips_frame_reg = SP;
543 
544 /* Whether mips_frame_reg has been set in the current function
545    (or whether it has already been warned about, if not).  */
546 static int mips_frame_reg_valid = 0;
547 
548 /* To output NOP instructions correctly, we need to keep information
549    about the previous two instructions.  */
550 
551 /* Whether we are optimizing.  The default value of 2 means to remove
552    unneeded NOPs and swap branch instructions when possible.  A value
553    of 1 means to not swap branches.  A value of 0 means to always
554    insert NOPs.  */
555 static int mips_optimize = 2;
556 
557 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
558    equivalent to seeing no -g option at all.  */
559 static int mips_debug = 0;
560 
561 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata.  */
562 #define MAX_VR4130_NOPS 4
563 
564 /* The maximum number of NOPs needed to fill delay slots.  */
565 #define MAX_DELAY_NOPS 2
566 
567 /* The maximum number of NOPs needed for any purpose.  */
568 #define MAX_NOPS 4
569 
570 /* A list of previous instructions, with index 0 being the most recent.
571    We need to look back MAX_NOPS instructions when filling delay slots
572    or working around processor errata.  We need to look back one
573    instruction further if we're thinking about using history[0] to
574    fill a branch delay slot.  */
575 static struct mips_cl_insn history[1 + MAX_NOPS];
576 
577 /* Nop instructions used by emit_nop.  */
578 static struct mips_cl_insn nop_insn, mips16_nop_insn;
579 
580 /* The appropriate nop for the current mode.  */
581 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
582 
583 /* If this is set, it points to a frag holding nop instructions which
584    were inserted before the start of a noreorder section.  If those
585    nops turn out to be unnecessary, the size of the frag can be
586    decreased.  */
587 static fragS *prev_nop_frag;
588 
589 /* The number of nop instructions we created in prev_nop_frag.  */
590 static int prev_nop_frag_holds;
591 
592 /* The number of nop instructions that we know we need in
593    prev_nop_frag.  */
594 static int prev_nop_frag_required;
595 
596 /* The number of instructions we've seen since prev_nop_frag.  */
597 static int prev_nop_frag_since;
598 
599 /* For ECOFF and ELF, relocations against symbols are done in two
600    parts, with a HI relocation and a LO relocation.  Each relocation
601    has only 16 bits of space to store an addend.  This means that in
602    order for the linker to handle carries correctly, it must be able
603    to locate both the HI and the LO relocation.  This means that the
604    relocations must appear in order in the relocation table.
605 
606    In order to implement this, we keep track of each unmatched HI
607    relocation.  We then sort them so that they immediately precede the
608    corresponding LO relocation.  */
609 
610 struct mips_hi_fixup
611 {
612   /* Next HI fixup.  */
613   struct mips_hi_fixup *next;
614   /* This fixup.  */
615   fixS *fixp;
616   /* The section this fixup is in.  */
617   segT seg;
618 };
619 
620 /* The list of unmatched HI relocs.  */
621 
622 static struct mips_hi_fixup *mips_hi_fixup_list;
623 
624 /* The frag containing the last explicit relocation operator.
625    Null if explicit relocations have not been used.  */
626 
627 static fragS *prev_reloc_op_frag;
628 
629 /* Map normal MIPS register numbers to mips16 register numbers.  */
630 
631 #define X ILLEGAL_REG
632 static const int mips32_to_16_reg_map[] =
633 {
634   X, X, 2, 3, 4, 5, 6, 7,
635   X, X, X, X, X, X, X, X,
636   0, 1, X, X, X, X, X, X,
637   X, X, X, X, X, X, X, X
638 };
639 #undef X
640 
641 /* Map mips16 register numbers to normal MIPS register numbers.  */
642 
643 static const unsigned int mips16_to_32_reg_map[] =
644 {
645   16, 17, 2, 3, 4, 5, 6, 7
646 };
647 
648 /* Classifies the kind of instructions we're interested in when
649    implementing -mfix-vr4120.  */
650 enum fix_vr4120_class {
651   FIX_VR4120_MACC,
652   FIX_VR4120_DMACC,
653   FIX_VR4120_MULT,
654   FIX_VR4120_DMULT,
655   FIX_VR4120_DIV,
656   FIX_VR4120_MTHILO,
657   NUM_FIX_VR4120_CLASSES
658 };
659 
660 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
661    there must be at least one other instruction between an instruction
662    of type X and an instruction of type Y.  */
663 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
664 
665 /* True if -mfix-vr4120 is in force.  */
666 static int mips_fix_vr4120;
667 
668 /* ...likewise -mfix-vr4130.  */
669 static int mips_fix_vr4130;
670 
671 /* We don't relax branches by default, since this causes us to expand
672    `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
673    fail to compute the offset before expanding the macro to the most
674    efficient expansion.  */
675 
676 static int mips_relax_branch;
677 
678 /* The expansion of many macros depends on the type of symbol that
679    they refer to.  For example, when generating position-dependent code,
680    a macro that refers to a symbol may have two different expansions,
681    one which uses GP-relative addresses and one which uses absolute
682    addresses.  When generating SVR4-style PIC, a macro may have
683    different expansions for local and global symbols.
684 
685    We handle these situations by generating both sequences and putting
686    them in variant frags.  In position-dependent code, the first sequence
687    will be the GP-relative one and the second sequence will be the
688    absolute one.  In SVR4 PIC, the first sequence will be for global
689    symbols and the second will be for local symbols.
690 
691    The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
692    SECOND are the lengths of the two sequences in bytes.  These fields
693    can be extracted using RELAX_FIRST() and RELAX_SECOND().  In addition,
694    the subtype has the following flags:
695 
696    RELAX_USE_SECOND
697 	Set if it has been decided that we should use the second
698 	sequence instead of the first.
699 
700    RELAX_SECOND_LONGER
701 	Set in the first variant frag if the macro's second implementation
702 	is longer than its first.  This refers to the macro as a whole,
703 	not an individual relaxation.
704 
705    RELAX_NOMACRO
706 	Set in the first variant frag if the macro appeared in a .set nomacro
707 	block and if one alternative requires a warning but the other does not.
708 
709    RELAX_DELAY_SLOT
710 	Like RELAX_NOMACRO, but indicates that the macro appears in a branch
711 	delay slot.
712 
713    The frag's "opcode" points to the first fixup for relaxable code.
714 
715    Relaxable macros are generated using a sequence such as:
716 
717       relax_start (SYMBOL);
718       ... generate first expansion ...
719       relax_switch ();
720       ... generate second expansion ...
721       relax_end ();
722 
723    The code and fixups for the unwanted alternative are discarded
724    by md_convert_frag.  */
725 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
726 
727 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
728 #define RELAX_SECOND(X) ((X) & 0xff)
729 #define RELAX_USE_SECOND 0x10000
730 #define RELAX_SECOND_LONGER 0x20000
731 #define RELAX_NOMACRO 0x40000
732 #define RELAX_DELAY_SLOT 0x80000
733 
734 /* Branch without likely bit.  If label is out of range, we turn:
735 
736  	beq reg1, reg2, label
737 	delay slot
738 
739    into
740 
741         bne reg1, reg2, 0f
742         nop
743         j label
744      0: delay slot
745 
746    with the following opcode replacements:
747 
748 	beq <-> bne
749 	blez <-> bgtz
750 	bltz <-> bgez
751 	bc1f <-> bc1t
752 
753 	bltzal <-> bgezal  (with jal label instead of j label)
754 
755    Even though keeping the delay slot instruction in the delay slot of
756    the branch would be more efficient, it would be very tricky to do
757    correctly, because we'd have to introduce a variable frag *after*
758    the delay slot instruction, and expand that instead.  Let's do it
759    the easy way for now, even if the branch-not-taken case now costs
760    one additional instruction.  Out-of-range branches are not supposed
761    to be common, anyway.
762 
763    Branch likely.  If label is out of range, we turn:
764 
765 	beql reg1, reg2, label
766 	delay slot (annulled if branch not taken)
767 
768    into
769 
770         beql reg1, reg2, 1f
771         nop
772         beql $0, $0, 2f
773         nop
774      1: j[al] label
775         delay slot (executed only if branch taken)
776      2:
777 
778    It would be possible to generate a shorter sequence by losing the
779    likely bit, generating something like:
780 
781 	bne reg1, reg2, 0f
782 	nop
783 	j[al] label
784 	delay slot (executed only if branch taken)
785      0:
786 
787 	beql -> bne
788 	bnel -> beq
789 	blezl -> bgtz
790 	bgtzl -> blez
791 	bltzl -> bgez
792 	bgezl -> bltz
793 	bc1fl -> bc1t
794 	bc1tl -> bc1f
795 
796 	bltzall -> bgezal  (with jal label instead of j label)
797 	bgezall -> bltzal  (ditto)
798 
799 
800    but it's not clear that it would actually improve performance.  */
801 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
802   ((relax_substateT) \
803    (0xc0000000 \
804     | ((toofar) ? 1 : 0) \
805     | ((link) ? 2 : 0) \
806     | ((likely) ? 4 : 0) \
807     | ((uncond) ? 8 : 0)))
808 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
809 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
810 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
811 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
812 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
813 
814 /* For mips16 code, we use an entirely different form of relaxation.
815    mips16 supports two versions of most instructions which take
816    immediate values: a small one which takes some small value, and a
817    larger one which takes a 16 bit value.  Since branches also follow
818    this pattern, relaxing these values is required.
819 
820    We can assemble both mips16 and normal MIPS code in a single
821    object.  Therefore, we need to support this type of relaxation at
822    the same time that we support the relaxation described above.  We
823    use the high bit of the subtype field to distinguish these cases.
824 
825    The information we store for this type of relaxation is the
826    argument code found in the opcode file for this relocation, whether
827    the user explicitly requested a small or extended form, and whether
828    the relocation is in a jump or jal delay slot.  That tells us the
829    size of the value, and how it should be stored.  We also store
830    whether the fragment is considered to be extended or not.  We also
831    store whether this is known to be a branch to a different section,
832    whether we have tried to relax this frag yet, and whether we have
833    ever extended a PC relative fragment because of a shift count.  */
834 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot)	\
835   (0x80000000							\
836    | ((type) & 0xff)						\
837    | ((small) ? 0x100 : 0)					\
838    | ((ext) ? 0x200 : 0)					\
839    | ((dslot) ? 0x400 : 0)					\
840    | ((jal_dslot) ? 0x800 : 0))
841 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
842 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
843 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
844 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
845 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
846 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
847 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
848 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
849 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
850 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
851 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
852 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
853 
854 /* Is the given value a sign-extended 32-bit value?  */
855 #define IS_SEXT_32BIT_NUM(x)						\
856   (((x) &~ (offsetT) 0x7fffffff) == 0					\
857    || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
858 
859 /* Is the given value a sign-extended 16-bit value?  */
860 #define IS_SEXT_16BIT_NUM(x)						\
861   (((x) &~ (offsetT) 0x7fff) == 0					\
862    || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
863 
864 /* Is the given value a zero-extended 32-bit value?  Or a negated one?  */
865 #define IS_ZEXT_32BIT_NUM(x)						\
866   (((x) &~ (offsetT) 0xffffffff) == 0					\
867    || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
868 
869 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
870    VALUE << SHIFT.  VALUE is evaluated exactly once.  */
871 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
872   (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
873 	      | (((VALUE) & (MASK)) << (SHIFT)))
874 
875 /* Extract bits MASK << SHIFT from STRUCT and shift them right
876    SHIFT places.  */
877 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
878   (((STRUCT) >> (SHIFT)) & (MASK))
879 
880 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
881    INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
882 
883    include/opcode/mips.h specifies operand fields using the macros
884    OP_MASK_<FIELD> and OP_SH_<FIELD>.  The MIPS16 equivalents start
885    with "MIPS16OP" instead of "OP".  */
886 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
887   INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
888 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
889   INSERT_BITS ((INSN).insn_opcode, VALUE, \
890 		MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
891 
892 /* Extract the operand given by FIELD from mips_cl_insn INSN.  */
893 #define EXTRACT_OPERAND(FIELD, INSN) \
894   EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
895 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
896   EXTRACT_BITS ((INSN).insn_opcode, \
897 		MIPS16OP_MASK_##FIELD, \
898 		MIPS16OP_SH_##FIELD)
899 
900 /* Global variables used when generating relaxable macros.  See the
901    comment above RELAX_ENCODE for more details about how relaxation
902    is used.  */
903 static struct {
904   /* 0 if we're not emitting a relaxable macro.
905      1 if we're emitting the first of the two relaxation alternatives.
906      2 if we're emitting the second alternative.  */
907   int sequence;
908 
909   /* The first relaxable fixup in the current frag.  (In other words,
910      the first fixup that refers to relaxable code.)  */
911   fixS *first_fixup;
912 
913   /* sizes[0] says how many bytes of the first alternative are stored in
914      the current frag.  Likewise sizes[1] for the second alternative.  */
915   unsigned int sizes[2];
916 
917   /* The symbol on which the choice of sequence depends.  */
918   symbolS *symbol;
919 } mips_relax;
920 
921 /* Global variables used to decide whether a macro needs a warning.  */
922 static struct {
923   /* True if the macro is in a branch delay slot.  */
924   bfd_boolean delay_slot_p;
925 
926   /* For relaxable macros, sizes[0] is the length of the first alternative
927      in bytes and sizes[1] is the length of the second alternative.
928      For non-relaxable macros, both elements give the length of the
929      macro in bytes.  */
930   unsigned int sizes[2];
931 
932   /* The first variant frag for this macro.  */
933   fragS *first_frag;
934 } mips_macro_warning;
935 
936 /* Prototypes for static functions.  */
937 
938 #define internalError()							\
939     as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
940 
941 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
942 
943 static void append_insn
944   (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
945 static void mips_no_prev_insn (void);
946 static void mips16_macro_build
947   (expressionS *, const char *, const char *, va_list);
948 static void load_register (int, expressionS *, int);
949 static void macro_start (void);
950 static void macro_end (void);
951 static void macro (struct mips_cl_insn * ip);
952 static void mips16_macro (struct mips_cl_insn * ip);
953 #ifdef LOSING_COMPILER
954 static void macro2 (struct mips_cl_insn * ip);
955 #endif
956 static void mips_ip (char *str, struct mips_cl_insn * ip);
957 static void mips16_ip (char *str, struct mips_cl_insn * ip);
958 static void mips16_immed
959   (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
960    unsigned long *, bfd_boolean *, unsigned short *);
961 static size_t my_getSmallExpression
962   (expressionS *, bfd_reloc_code_real_type *, char *);
963 static void my_getExpression (expressionS *, char *);
964 static void s_align (int);
965 static void s_change_sec (int);
966 static void s_change_section (int);
967 static void s_cons (int);
968 static void s_float_cons (int);
969 static void s_mips_globl (int);
970 static void s_option (int);
971 static void s_mipsset (int);
972 static void s_abicalls (int);
973 static void s_cpload (int);
974 static void s_cpsetup (int);
975 static void s_cplocal (int);
976 static void s_cprestore (int);
977 static void s_cpreturn (int);
978 static void s_gpvalue (int);
979 static void s_gpword (int);
980 static void s_gpdword (int);
981 static void s_cpadd (int);
982 static void s_insn (int);
983 static void md_obj_begin (void);
984 static void md_obj_end (void);
985 static void s_mips_ent (int);
986 static void s_mips_end (int);
987 static void s_mips_frame (int);
988 static void s_mips_mask (int reg_type);
989 static void s_mips_stab (int);
990 static void s_mips_weakext (int);
991 static void s_mips_file (int);
992 static void s_mips_loc (int);
993 static bfd_boolean pic_need_relax (symbolS *, asection *);
994 static int relaxed_branch_length (fragS *, asection *, int);
995 static int validate_mips_insn (const struct mips_opcode *);
996 
997 /* Table and functions used to map between CPU/ISA names, and
998    ISA levels, and CPU numbers.  */
999 
1000 struct mips_cpu_info
1001 {
1002   const char *name;           /* CPU or ISA name.  */
1003   int is_isa;                 /* Is this an ISA?  (If 0, a CPU.) */
1004   int isa;                    /* ISA level.  */
1005   int cpu;                    /* CPU number (default CPU if ISA).  */
1006 };
1007 
1008 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1009 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1010 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1011 
1012 /* Pseudo-op table.
1013 
1014    The following pseudo-ops from the Kane and Heinrich MIPS book
1015    should be defined here, but are currently unsupported: .alias,
1016    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1017 
1018    The following pseudo-ops from the Kane and Heinrich MIPS book are
1019    specific to the type of debugging information being generated, and
1020    should be defined by the object format: .aent, .begin, .bend,
1021    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1022    .vreg.
1023 
1024    The following pseudo-ops from the Kane and Heinrich MIPS book are
1025    not MIPS CPU specific, but are also not specific to the object file
1026    format.  This file is probably the best place to define them, but
1027    they are not currently supported: .asm0, .endr, .lab, .repeat,
1028    .struct.  */
1029 
1030 static const pseudo_typeS mips_pseudo_table[] =
1031 {
1032   /* MIPS specific pseudo-ops.  */
1033   {"option", s_option, 0},
1034   {"set", s_mipsset, 0},
1035   {"rdata", s_change_sec, 'r'},
1036   {"sdata", s_change_sec, 's'},
1037   {"livereg", s_ignore, 0},
1038   {"abicalls", s_abicalls, 0},
1039   {"cpload", s_cpload, 0},
1040   {"cpsetup", s_cpsetup, 0},
1041   {"cplocal", s_cplocal, 0},
1042   {"cprestore", s_cprestore, 0},
1043   {"cpreturn", s_cpreturn, 0},
1044   {"gpvalue", s_gpvalue, 0},
1045   {"gpword", s_gpword, 0},
1046   {"gpdword", s_gpdword, 0},
1047   {"cpadd", s_cpadd, 0},
1048   {"insn", s_insn, 0},
1049 
1050   /* Relatively generic pseudo-ops that happen to be used on MIPS
1051      chips.  */
1052   {"asciiz", stringer, 1},
1053   {"bss", s_change_sec, 'b'},
1054   {"err", s_err, 0},
1055   {"half", s_cons, 1},
1056   {"dword", s_cons, 3},
1057   {"weakext", s_mips_weakext, 0},
1058 
1059   /* These pseudo-ops are defined in read.c, but must be overridden
1060      here for one reason or another.  */
1061   {"align", s_align, 0},
1062   {"byte", s_cons, 0},
1063   {"data", s_change_sec, 'd'},
1064   {"double", s_float_cons, 'd'},
1065   {"float", s_float_cons, 'f'},
1066   {"globl", s_mips_globl, 0},
1067   {"global", s_mips_globl, 0},
1068   {"hword", s_cons, 1},
1069   {"int", s_cons, 2},
1070   {"long", s_cons, 2},
1071   {"octa", s_cons, 4},
1072   {"quad", s_cons, 3},
1073   {"section", s_change_section, 0},
1074   {"short", s_cons, 1},
1075   {"single", s_float_cons, 'f'},
1076   {"stabn", s_mips_stab, 'n'},
1077   {"text", s_change_sec, 't'},
1078   {"word", s_cons, 2},
1079 
1080   { "extern", ecoff_directive_extern, 0},
1081 
1082   { NULL, NULL, 0 },
1083 };
1084 
1085 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1086 {
1087   /* These pseudo-ops should be defined by the object file format.
1088      However, a.out doesn't support them, so we have versions here.  */
1089   {"aent", s_mips_ent, 1},
1090   {"bgnb", s_ignore, 0},
1091   {"end", s_mips_end, 0},
1092   {"endb", s_ignore, 0},
1093   {"ent", s_mips_ent, 0},
1094   {"file", s_mips_file, 0},
1095   {"fmask", s_mips_mask, 'F'},
1096   {"frame", s_mips_frame, 0},
1097   {"loc", s_mips_loc, 0},
1098   {"mask", s_mips_mask, 'R'},
1099   {"verstamp", s_ignore, 0},
1100   { NULL, NULL, 0 },
1101 };
1102 
1103 extern void pop_insert (const pseudo_typeS *);
1104 
1105 void
mips_pop_insert(void)1106 mips_pop_insert (void)
1107 {
1108   pop_insert (mips_pseudo_table);
1109   if (! ECOFF_DEBUGGING)
1110     pop_insert (mips_nonecoff_pseudo_table);
1111 }
1112 
1113 /* Symbols labelling the current insn.  */
1114 
1115 struct insn_label_list
1116 {
1117   struct insn_label_list *next;
1118   symbolS *label;
1119 };
1120 
1121 static struct insn_label_list *insn_labels;
1122 static struct insn_label_list *free_insn_labels;
1123 
1124 static void mips_clear_insn_labels (void);
1125 
1126 static inline void
mips_clear_insn_labels(void)1127 mips_clear_insn_labels (void)
1128 {
1129   register struct insn_label_list **pl;
1130 
1131   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1132     ;
1133   *pl = insn_labels;
1134   insn_labels = NULL;
1135 }
1136 
1137 static char *expr_end;
1138 
1139 /* Expressions which appear in instructions.  These are set by
1140    mips_ip.  */
1141 
1142 static expressionS imm_expr;
1143 static expressionS imm2_expr;
1144 static expressionS offset_expr;
1145 
1146 /* Relocs associated with imm_expr and offset_expr.  */
1147 
1148 static bfd_reloc_code_real_type imm_reloc[3]
1149   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1150 static bfd_reloc_code_real_type offset_reloc[3]
1151   = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1152 
1153 /* These are set by mips16_ip if an explicit extension is used.  */
1154 
1155 static bfd_boolean mips16_small, mips16_ext;
1156 
1157 #ifdef OBJ_ELF
1158 /* The pdr segment for per procedure frame/regmask info.  Not used for
1159    ECOFF debugging.  */
1160 
1161 static segT pdr_seg;
1162 #endif
1163 
1164 /* The default target format to use.  */
1165 
1166 const char *
mips_target_format(void)1167 mips_target_format (void)
1168 {
1169   switch (OUTPUT_FLAVOR)
1170     {
1171     case bfd_target_ecoff_flavour:
1172       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1173     case bfd_target_coff_flavour:
1174       return "pe-mips";
1175     case bfd_target_elf_flavour:
1176 #ifdef TE_TMIPS
1177       /* This is traditional mips.  */
1178       return (target_big_endian
1179 	      ? (HAVE_64BIT_OBJECTS
1180 		 ? "elf64-tradbigmips"
1181 		 : (HAVE_NEWABI
1182 		    ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1183 	      : (HAVE_64BIT_OBJECTS
1184 		 ? "elf64-tradlittlemips"
1185 		 : (HAVE_NEWABI
1186 		    ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1187 #else
1188       return (target_big_endian
1189 	      ? (HAVE_64BIT_OBJECTS
1190 		 ? "elf64-bigmips"
1191 		 : (HAVE_NEWABI
1192 		    ? "elf32-nbigmips" : "elf32-bigmips"))
1193 	      : (HAVE_64BIT_OBJECTS
1194 		 ? "elf64-littlemips"
1195 		 : (HAVE_NEWABI
1196 		    ? "elf32-nlittlemips" : "elf32-littlemips")));
1197 #endif
1198     default:
1199       abort ();
1200       return NULL;
1201     }
1202 }
1203 
1204 /* Return the length of instruction INSN.  */
1205 
1206 static inline unsigned int
insn_length(const struct mips_cl_insn * insn)1207 insn_length (const struct mips_cl_insn *insn)
1208 {
1209   if (!mips_opts.mips16)
1210     return 4;
1211   return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1212 }
1213 
1214 /* Initialise INSN from opcode entry MO.  Leave its position unspecified.  */
1215 
1216 static void
create_insn(struct mips_cl_insn * insn,const struct mips_opcode * mo)1217 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1218 {
1219   size_t i;
1220 
1221   insn->insn_mo = mo;
1222   insn->use_extend = FALSE;
1223   insn->extend = 0;
1224   insn->insn_opcode = mo->match;
1225   insn->frag = NULL;
1226   insn->where = 0;
1227   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1228     insn->fixp[i] = NULL;
1229   insn->fixed_p = (mips_opts.noreorder > 0);
1230   insn->noreorder_p = (mips_opts.noreorder > 0);
1231   insn->mips16_absolute_jump_p = 0;
1232 }
1233 
1234 /* Install INSN at the location specified by its "frag" and "where" fields.  */
1235 
1236 static void
install_insn(const struct mips_cl_insn * insn)1237 install_insn (const struct mips_cl_insn *insn)
1238 {
1239   char *f = insn->frag->fr_literal + insn->where;
1240   if (!mips_opts.mips16)
1241     md_number_to_chars (f, insn->insn_opcode, 4);
1242   else if (insn->mips16_absolute_jump_p)
1243     {
1244       md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1245       md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1246     }
1247   else
1248     {
1249       if (insn->use_extend)
1250 	{
1251 	  md_number_to_chars (f, 0xf000 | insn->extend, 2);
1252 	  f += 2;
1253 	}
1254       md_number_to_chars (f, insn->insn_opcode, 2);
1255     }
1256 }
1257 
1258 /* Move INSN to offset WHERE in FRAG.  Adjust the fixups accordingly
1259    and install the opcode in the new location.  */
1260 
1261 static void
move_insn(struct mips_cl_insn * insn,fragS * frag,long where)1262 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1263 {
1264   size_t i;
1265 
1266   insn->frag = frag;
1267   insn->where = where;
1268   for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1269     if (insn->fixp[i] != NULL)
1270       {
1271 	insn->fixp[i]->fx_frag = frag;
1272 	insn->fixp[i]->fx_where = where;
1273       }
1274   install_insn (insn);
1275 }
1276 
1277 /* Add INSN to the end of the output.  */
1278 
1279 static void
add_fixed_insn(struct mips_cl_insn * insn)1280 add_fixed_insn (struct mips_cl_insn *insn)
1281 {
1282   char *f = frag_more (insn_length (insn));
1283   move_insn (insn, frag_now, f - frag_now->fr_literal);
1284 }
1285 
1286 /* Start a variant frag and move INSN to the start of the variant part,
1287    marking it as fixed.  The other arguments are as for frag_var.  */
1288 
1289 static void
add_relaxed_insn(struct mips_cl_insn * insn,int max_chars,int var,relax_substateT subtype,symbolS * symbol,offsetT offset)1290 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1291 		  relax_substateT subtype, symbolS *symbol, offsetT offset)
1292 {
1293   frag_grow (max_chars);
1294   move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1295   insn->fixed_p = 1;
1296   frag_var (rs_machine_dependent, max_chars, var,
1297 	    subtype, symbol, offset, NULL);
1298 }
1299 
1300 /* Insert N copies of INSN into the history buffer, starting at
1301    position FIRST.  Neither FIRST nor N need to be clipped.  */
1302 
1303 static void
insert_into_history(unsigned int first,unsigned int n,const struct mips_cl_insn * insn)1304 insert_into_history (unsigned int first, unsigned int n,
1305 		     const struct mips_cl_insn *insn)
1306 {
1307   if (mips_relax.sequence != 2)
1308     {
1309       unsigned int i;
1310 
1311       for (i = ARRAY_SIZE (history); i-- > first;)
1312 	if (i >= first + n)
1313 	  history[i] = history[i - n];
1314 	else
1315 	  history[i] = *insn;
1316     }
1317 }
1318 
1319 /* Emit a nop instruction, recording it in the history buffer.  */
1320 
1321 static void
emit_nop(void)1322 emit_nop (void)
1323 {
1324   add_fixed_insn (NOP_INSN);
1325   insert_into_history (0, 1, NOP_INSN);
1326 }
1327 
1328 /* Initialize vr4120_conflicts.  There is a bit of duplication here:
1329    the idea is to make it obvious at a glance that each errata is
1330    included.  */
1331 
1332 static void
init_vr4120_conflicts(void)1333 init_vr4120_conflicts (void)
1334 {
1335 #define CONFLICT(FIRST, SECOND) \
1336     vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1337 
1338   /* Errata 21 - [D]DIV[U] after [D]MACC */
1339   CONFLICT (MACC, DIV);
1340   CONFLICT (DMACC, DIV);
1341 
1342   /* Errata 23 - Continuous DMULT[U]/DMACC instructions.  */
1343   CONFLICT (DMULT, DMULT);
1344   CONFLICT (DMULT, DMACC);
1345   CONFLICT (DMACC, DMULT);
1346   CONFLICT (DMACC, DMACC);
1347 
1348   /* Errata 24 - MT{LO,HI} after [D]MACC */
1349   CONFLICT (MACC, MTHILO);
1350   CONFLICT (DMACC, MTHILO);
1351 
1352   /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1353      instruction is executed immediately after a MACC or DMACC
1354      instruction, the result of [either instruction] is incorrect."  */
1355   CONFLICT (MACC, MULT);
1356   CONFLICT (MACC, DMULT);
1357   CONFLICT (DMACC, MULT);
1358   CONFLICT (DMACC, DMULT);
1359 
1360   /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1361      executed immediately after a DMULT, DMULTU, DIV, DIVU,
1362      DDIV or DDIVU instruction, the result of the MACC or
1363      DMACC instruction is incorrect.".  */
1364   CONFLICT (DMULT, MACC);
1365   CONFLICT (DMULT, DMACC);
1366   CONFLICT (DIV, MACC);
1367   CONFLICT (DIV, DMACC);
1368 
1369 #undef CONFLICT
1370 }
1371 
1372 /* This function is called once, at assembler startup time.  It should
1373    set up all the tables, etc. that the MD part of the assembler will need.  */
1374 
1375 void
md_begin(void)1376 md_begin (void)
1377 {
1378   register const char *retval = NULL;
1379   int i = 0;
1380   int broken = 0;
1381 
1382   if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1383     as_warn (_("Could not set architecture and machine"));
1384 
1385   op_hash = hash_new ();
1386 
1387   for (i = 0; i < NUMOPCODES;)
1388     {
1389       const char *name = mips_opcodes[i].name;
1390 
1391       retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1392       if (retval != NULL)
1393 	{
1394 	  fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1395 		   mips_opcodes[i].name, retval);
1396 	  /* Probably a memory allocation problem?  Give up now.  */
1397 	  as_fatal (_("Broken assembler.  No assembly attempted."));
1398 	}
1399       do
1400 	{
1401 	  if (mips_opcodes[i].pinfo != INSN_MACRO)
1402 	    {
1403 	      if (!validate_mips_insn (&mips_opcodes[i]))
1404 		broken = 1;
1405 	      if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1406 		{
1407 		  create_insn (&nop_insn, mips_opcodes + i);
1408 		  nop_insn.fixed_p = 1;
1409 		}
1410 	    }
1411 	  ++i;
1412 	}
1413       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1414     }
1415 
1416   mips16_op_hash = hash_new ();
1417 
1418   i = 0;
1419   while (i < bfd_mips16_num_opcodes)
1420     {
1421       const char *name = mips16_opcodes[i].name;
1422 
1423       retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1424       if (retval != NULL)
1425 	as_fatal (_("internal: can't hash `%s': %s"),
1426 		  mips16_opcodes[i].name, retval);
1427       do
1428 	{
1429 	  if (mips16_opcodes[i].pinfo != INSN_MACRO
1430 	      && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1431 		  != mips16_opcodes[i].match))
1432 	    {
1433 	      fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1434 		       mips16_opcodes[i].name, mips16_opcodes[i].args);
1435 	      broken = 1;
1436 	    }
1437 	  if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1438 	    {
1439 	      create_insn (&mips16_nop_insn, mips16_opcodes + i);
1440 	      mips16_nop_insn.fixed_p = 1;
1441 	    }
1442 	  ++i;
1443 	}
1444       while (i < bfd_mips16_num_opcodes
1445 	     && strcmp (mips16_opcodes[i].name, name) == 0);
1446     }
1447 
1448   if (broken)
1449     as_fatal (_("Broken assembler.  No assembly attempted."));
1450 
1451   /* We add all the general register names to the symbol table.  This
1452      helps us detect invalid uses of them.  */
1453   for (i = 0; i < 32; i++)
1454     {
1455       char buf[5];
1456 
1457       sprintf (buf, "$%d", i);
1458       symbol_table_insert (symbol_new (buf, reg_section, i,
1459 				       &zero_address_frag));
1460     }
1461   symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1462 				   &zero_address_frag));
1463   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1464 				   &zero_address_frag));
1465   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1466 				   &zero_address_frag));
1467   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1468 				   &zero_address_frag));
1469   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1470 				   &zero_address_frag));
1471   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1472 				   &zero_address_frag));
1473   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1474 				   &zero_address_frag));
1475   symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1476 				   &zero_address_frag));
1477   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1478 				   &zero_address_frag));
1479 
1480   /* If we don't add these register names to the symbol table, they
1481      may end up being added as regular symbols by operand(), and then
1482      make it to the object file as undefined in case they're not
1483      regarded as local symbols.  They're local in o32, since `$' is a
1484      local symbol prefix, but not in n32 or n64.  */
1485   for (i = 0; i < 8; i++)
1486     {
1487       char buf[6];
1488 
1489       sprintf (buf, "$fcc%i", i);
1490       symbol_table_insert (symbol_new (buf, reg_section, -1,
1491 				       &zero_address_frag));
1492     }
1493 
1494   mips_no_prev_insn ();
1495 
1496   mips_gprmask = 0;
1497   mips_cprmask[0] = 0;
1498   mips_cprmask[1] = 0;
1499   mips_cprmask[2] = 0;
1500   mips_cprmask[3] = 0;
1501 
1502   /* set the default alignment for the text section (2**2) */
1503   record_alignment (text_section, 2);
1504 
1505   bfd_set_gp_size (stdoutput, g_switch_value);
1506 
1507   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1508     {
1509       /* On a native system, sections must be aligned to 16 byte
1510 	 boundaries.  When configured for an embedded ELF target, we
1511 	 don't bother.  */
1512       if (strcmp (TARGET_OS, "elf") != 0)
1513 	{
1514 	  (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1515 	  (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1516 	  (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1517 	}
1518 
1519       /* Create a .reginfo section for register masks and a .mdebug
1520 	 section for debugging information.  */
1521       {
1522 	segT seg;
1523 	subsegT subseg;
1524 	flagword flags;
1525 	segT sec;
1526 
1527 	seg = now_seg;
1528 	subseg = now_subseg;
1529 
1530 	/* The ABI says this section should be loaded so that the
1531 	   running program can access it.  However, we don't load it
1532 	   if we are configured for an embedded target */
1533 	flags = SEC_READONLY | SEC_DATA;
1534 	if (strcmp (TARGET_OS, "elf") != 0)
1535 	  flags |= SEC_ALLOC | SEC_LOAD;
1536 
1537 	if (mips_abi != N64_ABI)
1538 	  {
1539 	    sec = subseg_new (".reginfo", (subsegT) 0);
1540 
1541 	    bfd_set_section_flags (stdoutput, sec, flags);
1542 	    bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1543 
1544 #ifdef OBJ_ELF
1545 	    mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1546 #endif
1547 	  }
1548 	else
1549 	  {
1550 	    /* The 64-bit ABI uses a .MIPS.options section rather than
1551                .reginfo section.  */
1552 	    sec = subseg_new (".MIPS.options", (subsegT) 0);
1553 	    bfd_set_section_flags (stdoutput, sec, flags);
1554 	    bfd_set_section_alignment (stdoutput, sec, 3);
1555 
1556 #ifdef OBJ_ELF
1557 	    /* Set up the option header.  */
1558 	    {
1559 	      Elf_Internal_Options opthdr;
1560 	      char *f;
1561 
1562 	      opthdr.kind = ODK_REGINFO;
1563 	      opthdr.size = (sizeof (Elf_External_Options)
1564 			     + sizeof (Elf64_External_RegInfo));
1565 	      opthdr.section = 0;
1566 	      opthdr.info = 0;
1567 	      f = frag_more (sizeof (Elf_External_Options));
1568 	      bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1569 					     (Elf_External_Options *) f);
1570 
1571 	      mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1572 	    }
1573 #endif
1574 	  }
1575 
1576 	if (ECOFF_DEBUGGING)
1577 	  {
1578 	    sec = subseg_new (".mdebug", (subsegT) 0);
1579 	    (void) bfd_set_section_flags (stdoutput, sec,
1580 					  SEC_HAS_CONTENTS | SEC_READONLY);
1581 	    (void) bfd_set_section_alignment (stdoutput, sec, 2);
1582 	  }
1583 #ifdef OBJ_ELF
1584 	else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1585 	  {
1586 	    pdr_seg = subseg_new (".pdr", (subsegT) 0);
1587 	    (void) bfd_set_section_flags (stdoutput, pdr_seg,
1588 					  SEC_READONLY | SEC_RELOC
1589 					  | SEC_DEBUGGING);
1590 	    (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1591 	  }
1592 #endif
1593 
1594 	subseg_set (seg, subseg);
1595       }
1596     }
1597 
1598   if (! ECOFF_DEBUGGING)
1599     md_obj_begin ();
1600 
1601   if (mips_fix_vr4120)
1602     init_vr4120_conflicts ();
1603 }
1604 
1605 void
md_mips_end(void)1606 md_mips_end (void)
1607 {
1608   if (! ECOFF_DEBUGGING)
1609     md_obj_end ();
1610 }
1611 
1612 void
md_assemble(char * str)1613 md_assemble (char *str)
1614 {
1615   struct mips_cl_insn insn;
1616   bfd_reloc_code_real_type unused_reloc[3]
1617     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1618 
1619   imm_expr.X_op = O_absent;
1620   imm2_expr.X_op = O_absent;
1621   offset_expr.X_op = O_absent;
1622   imm_reloc[0] = BFD_RELOC_UNUSED;
1623   imm_reloc[1] = BFD_RELOC_UNUSED;
1624   imm_reloc[2] = BFD_RELOC_UNUSED;
1625   offset_reloc[0] = BFD_RELOC_UNUSED;
1626   offset_reloc[1] = BFD_RELOC_UNUSED;
1627   offset_reloc[2] = BFD_RELOC_UNUSED;
1628 
1629   if (mips_opts.mips16)
1630     mips16_ip (str, &insn);
1631   else
1632     {
1633       mips_ip (str, &insn);
1634       DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1635 	    str, insn.insn_opcode));
1636     }
1637 
1638   if (insn_error)
1639     {
1640       as_bad ("%s `%s'", insn_error, str);
1641       return;
1642     }
1643 
1644   if (insn.insn_mo->pinfo == INSN_MACRO)
1645     {
1646       macro_start ();
1647       if (mips_opts.mips16)
1648 	mips16_macro (&insn);
1649       else
1650 	macro (&insn);
1651       macro_end ();
1652     }
1653   else
1654     {
1655       if (imm_expr.X_op != O_absent)
1656 	append_insn (&insn, &imm_expr, imm_reloc);
1657       else if (offset_expr.X_op != O_absent)
1658 	append_insn (&insn, &offset_expr, offset_reloc);
1659       else
1660 	append_insn (&insn, NULL, unused_reloc);
1661     }
1662 }
1663 
1664 /* Return true if the given relocation might need a matching %lo().
1665    Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1666    applied to local symbols.  */
1667 
1668 static inline bfd_boolean
reloc_needs_lo_p(bfd_reloc_code_real_type reloc)1669 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1670 {
1671   return (HAVE_IN_PLACE_ADDENDS
1672 	  && (reloc == BFD_RELOC_HI16_S
1673 	      || reloc == BFD_RELOC_MIPS_GOT16
1674 	      || reloc == BFD_RELOC_MIPS16_HI16_S));
1675 }
1676 
1677 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1678    relocation.  */
1679 
1680 static inline bfd_boolean
fixup_has_matching_lo_p(fixS * fixp)1681 fixup_has_matching_lo_p (fixS *fixp)
1682 {
1683   return (fixp->fx_next != NULL
1684 	  && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1685 	     || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1686 	  && fixp->fx_addsy == fixp->fx_next->fx_addsy
1687 	  && fixp->fx_offset == fixp->fx_next->fx_offset);
1688 }
1689 
1690 /* See whether instruction IP reads register REG.  CLASS is the type
1691    of register.  */
1692 
1693 static int
insn_uses_reg(const struct mips_cl_insn * ip,unsigned int reg,enum mips_regclass class)1694 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
1695 	       enum mips_regclass class)
1696 {
1697   if (class == MIPS16_REG)
1698     {
1699       assert (mips_opts.mips16);
1700       reg = mips16_to_32_reg_map[reg];
1701       class = MIPS_GR_REG;
1702     }
1703 
1704   /* Don't report on general register ZERO, since it never changes.  */
1705   if (class == MIPS_GR_REG && reg == ZERO)
1706     return 0;
1707 
1708   if (class == MIPS_FP_REG)
1709     {
1710       assert (! mips_opts.mips16);
1711       /* If we are called with either $f0 or $f1, we must check $f0.
1712 	 This is not optimal, because it will introduce an unnecessary
1713 	 NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1714 	 need to distinguish reading both $f0 and $f1 or just one of
1715 	 them.  Note that we don't have to check the other way,
1716 	 because there is no instruction that sets both $f0 and $f1
1717 	 and requires a delay.  */
1718       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1719 	  && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
1720 	      == (reg &~ (unsigned) 1)))
1721 	return 1;
1722       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1723 	  && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
1724 	      == (reg &~ (unsigned) 1)))
1725 	return 1;
1726     }
1727   else if (! mips_opts.mips16)
1728     {
1729       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1730 	  && EXTRACT_OPERAND (RS, *ip) == reg)
1731 	return 1;
1732       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1733 	  && EXTRACT_OPERAND (RT, *ip) == reg)
1734 	return 1;
1735     }
1736   else
1737     {
1738       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1739 	  && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
1740 	return 1;
1741       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1742 	  && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
1743 	return 1;
1744       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1745 	  && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
1746 	      == reg))
1747 	return 1;
1748       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1749 	return 1;
1750       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1751 	return 1;
1752       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1753 	return 1;
1754       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1755 	  && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
1756 	return 1;
1757     }
1758 
1759   return 0;
1760 }
1761 
1762 /* This function returns true if modifying a register requires a
1763    delay.  */
1764 
1765 static int
reg_needs_delay(unsigned int reg)1766 reg_needs_delay (unsigned int reg)
1767 {
1768   unsigned long prev_pinfo;
1769 
1770   prev_pinfo = history[0].insn_mo->pinfo;
1771   if (! mips_opts.noreorder
1772       && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1773 	   && ! gpr_interlocks)
1774 	  || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1775 	      && ! cop_interlocks)))
1776     {
1777       /* A load from a coprocessor or from memory.  All load delays
1778 	 delay the use of general register rt for one instruction.  */
1779       /* Itbl support may require additional care here.  */
1780       know (prev_pinfo & INSN_WRITE_GPR_T);
1781       if (reg == EXTRACT_OPERAND (RT, history[0]))
1782 	return 1;
1783     }
1784 
1785   return 0;
1786 }
1787 
1788 /* Move all labels in insn_labels to the current insertion point.  */
1789 
1790 static void
mips_move_labels(void)1791 mips_move_labels (void)
1792 {
1793   struct insn_label_list *l;
1794   valueT val;
1795 
1796   for (l = insn_labels; l != NULL; l = l->next)
1797     {
1798       assert (S_GET_SEGMENT (l->label) == now_seg);
1799       symbol_set_frag (l->label, frag_now);
1800       val = (valueT) frag_now_fix ();
1801       /* mips16 text labels are stored as odd.  */
1802       if (mips_opts.mips16)
1803 	++val;
1804       S_SET_VALUE (l->label, val);
1805     }
1806 }
1807 
1808 /* Mark instruction labels in mips16 mode.  This permits the linker to
1809    handle them specially, such as generating jalx instructions when
1810    needed.  We also make them odd for the duration of the assembly, in
1811    order to generate the right sort of code.  We will make them even
1812    in the adjust_symtab routine, while leaving them marked.  This is
1813    convenient for the debugger and the disassembler.  The linker knows
1814    to make them odd again.  */
1815 
1816 static void
mips16_mark_labels(void)1817 mips16_mark_labels (void)
1818 {
1819   if (mips_opts.mips16)
1820     {
1821       struct insn_label_list *l;
1822       valueT val;
1823 
1824       for (l = insn_labels; l != NULL; l = l->next)
1825 	{
1826 #ifdef OBJ_ELF
1827 	  if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1828 	    S_SET_OTHER (l->label, STO_MIPS16);
1829 #endif
1830 	  val = S_GET_VALUE (l->label);
1831 	  if ((val & 1) == 0)
1832 	    S_SET_VALUE (l->label, val + 1);
1833 	}
1834     }
1835 }
1836 
1837 /* End the current frag.  Make it a variant frag and record the
1838    relaxation info.  */
1839 
1840 static void
relax_close_frag(void)1841 relax_close_frag (void)
1842 {
1843   mips_macro_warning.first_frag = frag_now;
1844   frag_var (rs_machine_dependent, 0, 0,
1845 	    RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1846 	    mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1847 
1848   memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1849   mips_relax.first_fixup = 0;
1850 }
1851 
1852 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1853    See the comment above RELAX_ENCODE for more details.  */
1854 
1855 static void
relax_start(symbolS * symbol)1856 relax_start (symbolS *symbol)
1857 {
1858   assert (mips_relax.sequence == 0);
1859   mips_relax.sequence = 1;
1860   mips_relax.symbol = symbol;
1861 }
1862 
1863 /* Start generating the second version of a relaxable sequence.
1864    See the comment above RELAX_ENCODE for more details.  */
1865 
1866 static void
relax_switch(void)1867 relax_switch (void)
1868 {
1869   assert (mips_relax.sequence == 1);
1870   mips_relax.sequence = 2;
1871 }
1872 
1873 /* End the current relaxable sequence.  */
1874 
1875 static void
relax_end(void)1876 relax_end (void)
1877 {
1878   assert (mips_relax.sequence == 2);
1879   relax_close_frag ();
1880   mips_relax.sequence = 0;
1881 }
1882 
1883 /* Classify an instruction according to the FIX_VR4120_* enumeration.
1884    Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1885    by VR4120 errata.  */
1886 
1887 static unsigned int
classify_vr4120_insn(const char * name)1888 classify_vr4120_insn (const char *name)
1889 {
1890   if (strncmp (name, "macc", 4) == 0)
1891     return FIX_VR4120_MACC;
1892   if (strncmp (name, "dmacc", 5) == 0)
1893     return FIX_VR4120_DMACC;
1894   if (strncmp (name, "mult", 4) == 0)
1895     return FIX_VR4120_MULT;
1896   if (strncmp (name, "dmult", 5) == 0)
1897     return FIX_VR4120_DMULT;
1898   if (strstr (name, "div"))
1899     return FIX_VR4120_DIV;
1900   if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1901     return FIX_VR4120_MTHILO;
1902   return NUM_FIX_VR4120_CLASSES;
1903 }
1904 
1905 /* Return the number of instructions that must separate INSN1 and INSN2,
1906    where INSN1 is the earlier instruction.  Return the worst-case value
1907    for any INSN2 if INSN2 is null.  */
1908 
1909 static unsigned int
insns_between(const struct mips_cl_insn * insn1,const struct mips_cl_insn * insn2)1910 insns_between (const struct mips_cl_insn *insn1,
1911 	       const struct mips_cl_insn *insn2)
1912 {
1913   unsigned long pinfo1, pinfo2;
1914 
1915   /* This function needs to know which pinfo flags are set for INSN2
1916      and which registers INSN2 uses.  The former is stored in PINFO2 and
1917      the latter is tested via INSN2_USES_REG.  If INSN2 is null, PINFO2
1918      will have every flag set and INSN2_USES_REG will always return true.  */
1919   pinfo1 = insn1->insn_mo->pinfo;
1920   pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
1921 
1922 #define INSN2_USES_REG(REG, CLASS) \
1923    (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1924 
1925   /* For most targets, write-after-read dependencies on the HI and LO
1926      registers must be separated by at least two instructions.  */
1927   if (!hilo_interlocks)
1928     {
1929       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1930 	return 2;
1931       if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1932 	return 2;
1933     }
1934 
1935   /* If we're working around r7000 errata, there must be two instructions
1936      between an mfhi or mflo and any instruction that uses the result.  */
1937   if (mips_7000_hilo_fix
1938       && MF_HILO_INSN (pinfo1)
1939       && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1940     return 2;
1941 
1942   /* If working around VR4120 errata, check for combinations that need
1943      a single intervening instruction.  */
1944   if (mips_fix_vr4120)
1945     {
1946       unsigned int class1, class2;
1947 
1948       class1 = classify_vr4120_insn (insn1->insn_mo->name);
1949       if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
1950 	{
1951 	  if (insn2 == NULL)
1952 	    return 1;
1953 	  class2 = classify_vr4120_insn (insn2->insn_mo->name);
1954 	  if (vr4120_conflicts[class1] & (1 << class2))
1955 	    return 1;
1956 	}
1957     }
1958 
1959   if (!mips_opts.mips16)
1960     {
1961       /* Check for GPR or coprocessor load delays.  All such delays
1962 	 are on the RT register.  */
1963       /* Itbl support may require additional care here.  */
1964       if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
1965 	  || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
1966 	{
1967 	  know (pinfo1 & INSN_WRITE_GPR_T);
1968 	  if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
1969 	    return 1;
1970 	}
1971 
1972       /* Check for generic coprocessor hazards.
1973 
1974 	 This case is not handled very well.  There is no special
1975 	 knowledge of CP0 handling, and the coprocessors other than
1976 	 the floating point unit are not distinguished at all.  */
1977       /* Itbl support may require additional care here. FIXME!
1978 	 Need to modify this to include knowledge about
1979 	 user specified delays!  */
1980       else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
1981 	       || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
1982 	{
1983 	  /* Handle cases where INSN1 writes to a known general coprocessor
1984 	     register.  There must be a one instruction delay before INSN2
1985 	     if INSN2 reads that register, otherwise no delay is needed.  */
1986 	  if (pinfo1 & INSN_WRITE_FPR_T)
1987 	    {
1988 	      if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
1989 		return 1;
1990 	    }
1991 	  else if (pinfo1 & INSN_WRITE_FPR_S)
1992 	    {
1993 	      if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
1994 		return 1;
1995 	    }
1996 	  else
1997 	    {
1998 	      /* Read-after-write dependencies on the control registers
1999 		 require a two-instruction gap.  */
2000 	      if ((pinfo1 & INSN_WRITE_COND_CODE)
2001 		  && (pinfo2 & INSN_READ_COND_CODE))
2002 		return 2;
2003 
2004 	      /* We don't know exactly what INSN1 does.  If INSN2 is
2005 		 also a coprocessor instruction, assume there must be
2006 		 a one instruction gap.  */
2007 	      if (pinfo2 & INSN_COP)
2008 		return 1;
2009 	    }
2010 	}
2011 
2012       /* Check for read-after-write dependencies on the coprocessor
2013 	 control registers in cases where INSN1 does not need a general
2014 	 coprocessor delay.  This means that INSN1 is a floating point
2015 	 comparison instruction.  */
2016       /* Itbl support may require additional care here.  */
2017       else if (!cop_interlocks
2018 	       && (pinfo1 & INSN_WRITE_COND_CODE)
2019 	       && (pinfo2 & INSN_READ_COND_CODE))
2020 	return 1;
2021     }
2022 
2023 #undef INSN2_USES_REG
2024 
2025   return 0;
2026 }
2027 
2028 /* Return the number of nops that would be needed to work around the
2029    VR4130 mflo/mfhi errata if instruction INSN immediately followed
2030    the MAX_VR4130_NOPS instructions described by HISTORY.  */
2031 
2032 static int
nops_for_vr4130(const struct mips_cl_insn * history,const struct mips_cl_insn * insn)2033 nops_for_vr4130 (const struct mips_cl_insn *history,
2034 		 const struct mips_cl_insn *insn)
2035 {
2036   int i, j, reg;
2037 
2038   /* Check if the instruction writes to HI or LO.  MTHI and MTLO
2039      are not affected by the errata.  */
2040   if (insn != 0
2041       && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2042 	  || strcmp (insn->insn_mo->name, "mtlo") == 0
2043 	  || strcmp (insn->insn_mo->name, "mthi") == 0))
2044     return 0;
2045 
2046   /* Search for the first MFLO or MFHI.  */
2047   for (i = 0; i < MAX_VR4130_NOPS; i++)
2048     if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2049       {
2050 	/* Extract the destination register.  */
2051 	if (mips_opts.mips16)
2052 	  reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2053 	else
2054 	  reg = EXTRACT_OPERAND (RD, history[i]);
2055 
2056 	/* No nops are needed if INSN reads that register.  */
2057 	if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2058 	  return 0;
2059 
2060 	/* ...or if any of the intervening instructions do.  */
2061 	for (j = 0; j < i; j++)
2062 	  if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2063 	    return 0;
2064 
2065 	return MAX_VR4130_NOPS - i;
2066       }
2067   return 0;
2068 }
2069 
2070 /* Return the number of nops that would be needed if instruction INSN
2071    immediately followed the MAX_NOPS instructions given by HISTORY,
2072    where HISTORY[0] is the most recent instruction.  If INSN is null,
2073    return the worse-case number of nops for any instruction.  */
2074 
2075 static int
nops_for_insn(const struct mips_cl_insn * history,const struct mips_cl_insn * insn)2076 nops_for_insn (const struct mips_cl_insn *history,
2077 	       const struct mips_cl_insn *insn)
2078 {
2079   int i, nops, tmp_nops;
2080 
2081   nops = 0;
2082   for (i = 0; i < MAX_DELAY_NOPS; i++)
2083     if (!history[i].noreorder_p)
2084       {
2085 	tmp_nops = insns_between (history + i, insn) - i;
2086 	if (tmp_nops > nops)
2087 	  nops = tmp_nops;
2088       }
2089 
2090   if (mips_fix_vr4130)
2091     {
2092       tmp_nops = nops_for_vr4130 (history, insn);
2093       if (tmp_nops > nops)
2094 	nops = tmp_nops;
2095     }
2096 
2097   return nops;
2098 }
2099 
2100 /* The variable arguments provide NUM_INSNS extra instructions that
2101    might be added to HISTORY.  Return the largest number of nops that
2102    would be needed after the extended sequence.  */
2103 
2104 static int
nops_for_sequence(int num_insns,const struct mips_cl_insn * history,...)2105 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2106 {
2107   va_list args;
2108   struct mips_cl_insn buffer[MAX_NOPS];
2109   struct mips_cl_insn *cursor;
2110   int nops;
2111 
2112   va_start (args, history);
2113   cursor = buffer + num_insns;
2114   memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2115   while (cursor > buffer)
2116     *--cursor = *va_arg (args, const struct mips_cl_insn *);
2117 
2118   nops = nops_for_insn (buffer, NULL);
2119   va_end (args);
2120   return nops;
2121 }
2122 
2123 /* Like nops_for_insn, but if INSN is a branch, take into account the
2124    worst-case delay for the branch target.  */
2125 
2126 static int
nops_for_insn_or_target(const struct mips_cl_insn * history,const struct mips_cl_insn * insn)2127 nops_for_insn_or_target (const struct mips_cl_insn *history,
2128 			 const struct mips_cl_insn *insn)
2129 {
2130   int nops, tmp_nops;
2131 
2132   nops = nops_for_insn (history, insn);
2133   if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2134 			      | INSN_COND_BRANCH_DELAY
2135 			      | INSN_COND_BRANCH_LIKELY))
2136     {
2137       tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2138       if (tmp_nops > nops)
2139 	nops = tmp_nops;
2140     }
2141   else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2142     {
2143       tmp_nops = nops_for_sequence (1, history, insn);
2144       if (tmp_nops > nops)
2145 	nops = tmp_nops;
2146     }
2147   return nops;
2148 }
2149 
2150 /* Output an instruction.  IP is the instruction information.
2151    ADDRESS_EXPR is an operand of the instruction to be used with
2152    RELOC_TYPE.  */
2153 
2154 static void
append_insn(struct mips_cl_insn * ip,expressionS * address_expr,bfd_reloc_code_real_type * reloc_type)2155 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2156 	     bfd_reloc_code_real_type *reloc_type)
2157 {
2158   register unsigned long prev_pinfo, pinfo;
2159   relax_stateT prev_insn_frag_type = 0;
2160   bfd_boolean relaxed_branch = FALSE;
2161 
2162   /* Mark instruction labels in mips16 mode.  */
2163   mips16_mark_labels ();
2164 
2165   prev_pinfo = history[0].insn_mo->pinfo;
2166   pinfo = ip->insn_mo->pinfo;
2167 
2168   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2169     {
2170       /* There are a lot of optimizations we could do that we don't.
2171 	 In particular, we do not, in general, reorder instructions.
2172 	 If you use gcc with optimization, it will reorder
2173 	 instructions and generally do much more optimization then we
2174 	 do here; repeating all that work in the assembler would only
2175 	 benefit hand written assembly code, and does not seem worth
2176 	 it.  */
2177       int nops = (mips_optimize == 0
2178 		  ? nops_for_insn (history, NULL)
2179 		  : nops_for_insn_or_target (history, ip));
2180       if (nops > 0)
2181 	{
2182 	  fragS *old_frag;
2183 	  unsigned long old_frag_offset;
2184 	  int i;
2185 
2186 	  old_frag = frag_now;
2187 	  old_frag_offset = frag_now_fix ();
2188 
2189 	  for (i = 0; i < nops; i++)
2190 	    emit_nop ();
2191 
2192 	  if (listing)
2193 	    {
2194 	      listing_prev_line ();
2195 	      /* We may be at the start of a variant frag.  In case we
2196                  are, make sure there is enough space for the frag
2197                  after the frags created by listing_prev_line.  The
2198                  argument to frag_grow here must be at least as large
2199                  as the argument to all other calls to frag_grow in
2200                  this file.  We don't have to worry about being in the
2201                  middle of a variant frag, because the variants insert
2202                  all needed nop instructions themselves.  */
2203 	      frag_grow (40);
2204 	    }
2205 
2206 	  mips_move_labels ();
2207 
2208 #ifndef NO_ECOFF_DEBUGGING
2209 	  if (ECOFF_DEBUGGING)
2210 	    ecoff_fix_loc (old_frag, old_frag_offset);
2211 #endif
2212 	}
2213     }
2214   else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2215     {
2216       /* Work out how many nops in prev_nop_frag are needed by IP.  */
2217       int nops = nops_for_insn_or_target (history, ip);
2218       assert (nops <= prev_nop_frag_holds);
2219 
2220       /* Enforce NOPS as a minimum.  */
2221       if (nops > prev_nop_frag_required)
2222 	prev_nop_frag_required = nops;
2223 
2224       if (prev_nop_frag_holds == prev_nop_frag_required)
2225 	{
2226 	  /* Settle for the current number of nops.  Update the history
2227 	     accordingly (for the benefit of any future .set reorder code).  */
2228 	  prev_nop_frag = NULL;
2229 	  insert_into_history (prev_nop_frag_since,
2230 			       prev_nop_frag_holds, NOP_INSN);
2231 	}
2232       else
2233 	{
2234 	  /* Allow this instruction to replace one of the nops that was
2235 	     tentatively added to prev_nop_frag.  */
2236 	  prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2237 	  prev_nop_frag_holds--;
2238 	  prev_nop_frag_since++;
2239 	}
2240     }
2241 
2242 #ifdef OBJ_ELF
2243   /* The value passed to dwarf2_emit_insn is the distance between
2244      the beginning of the current instruction and the address that
2245      should be recorded in the debug tables.  For MIPS16 debug info
2246      we want to use ISA-encoded addresses, so we pass -1 for an
2247      address higher by one than the current.  */
2248   dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2249 #endif
2250 
2251   /* Record the frag type before frag_var.  */
2252   if (history[0].frag)
2253     prev_insn_frag_type = history[0].frag->fr_type;
2254 
2255   if (address_expr
2256       && *reloc_type == BFD_RELOC_16_PCREL_S2
2257       && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2258 	  || pinfo & INSN_COND_BRANCH_LIKELY)
2259       && mips_relax_branch
2260       /* Don't try branch relaxation within .set nomacro, or within
2261 	 .set noat if we use $at for PIC computations.  If it turns
2262 	 out that the branch was out-of-range, we'll get an error.  */
2263       && !mips_opts.warn_about_macros
2264       && !(mips_opts.noat && mips_pic != NO_PIC)
2265       && !mips_opts.mips16)
2266     {
2267       relaxed_branch = TRUE;
2268       add_relaxed_insn (ip, (relaxed_branch_length
2269 			     (NULL, NULL,
2270 			      (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2271 			      : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2272 			      : 0)), 4,
2273 			RELAX_BRANCH_ENCODE
2274 			(pinfo & INSN_UNCOND_BRANCH_DELAY,
2275 			 pinfo & INSN_COND_BRANCH_LIKELY,
2276 			 pinfo & INSN_WRITE_GPR_31,
2277 			 0),
2278 			address_expr->X_add_symbol,
2279 			address_expr->X_add_number);
2280       *reloc_type = BFD_RELOC_UNUSED;
2281     }
2282   else if (*reloc_type > BFD_RELOC_UNUSED)
2283     {
2284       /* We need to set up a variant frag.  */
2285       assert (mips_opts.mips16 && address_expr != NULL);
2286       add_relaxed_insn (ip, 4, 0,
2287 			RELAX_MIPS16_ENCODE
2288 			(*reloc_type - BFD_RELOC_UNUSED,
2289 			 mips16_small, mips16_ext,
2290 			 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2291 			 history[0].mips16_absolute_jump_p),
2292 			make_expr_symbol (address_expr), 0);
2293     }
2294   else if (mips_opts.mips16
2295 	   && ! ip->use_extend
2296 	   && *reloc_type != BFD_RELOC_MIPS16_JMP)
2297     {
2298       /* Make sure there is enough room to swap this instruction with
2299          a following jump instruction.  */
2300       frag_grow (6);
2301       add_fixed_insn (ip);
2302     }
2303   else
2304     {
2305       if (mips_opts.mips16
2306 	  && mips_opts.noreorder
2307 	  && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2308 	as_warn (_("extended instruction in delay slot"));
2309 
2310       if (mips_relax.sequence)
2311 	{
2312 	  /* If we've reached the end of this frag, turn it into a variant
2313 	     frag and record the information for the instructions we've
2314 	     written so far.  */
2315 	  if (frag_room () < 4)
2316 	    relax_close_frag ();
2317 	  mips_relax.sizes[mips_relax.sequence - 1] += 4;
2318 	}
2319 
2320       if (mips_relax.sequence != 2)
2321 	mips_macro_warning.sizes[0] += 4;
2322       if (mips_relax.sequence != 1)
2323 	mips_macro_warning.sizes[1] += 4;
2324 
2325       if (mips_opts.mips16)
2326 	{
2327 	  ip->fixed_p = 1;
2328 	  ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2329 	}
2330       add_fixed_insn (ip);
2331     }
2332 
2333   if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2334     {
2335       if (address_expr->X_op == O_constant)
2336 	{
2337 	  unsigned int tmp;
2338 
2339 	  switch (*reloc_type)
2340 	    {
2341 	    case BFD_RELOC_32:
2342 	      ip->insn_opcode |= address_expr->X_add_number;
2343 	      break;
2344 
2345 	    case BFD_RELOC_MIPS_HIGHEST:
2346 	      tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2347 	      ip->insn_opcode |= tmp & 0xffff;
2348 	      break;
2349 
2350 	    case BFD_RELOC_MIPS_HIGHER:
2351 	      tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2352 	      ip->insn_opcode |= tmp & 0xffff;
2353 	      break;
2354 
2355 	    case BFD_RELOC_HI16_S:
2356 	      tmp = (address_expr->X_add_number + 0x8000) >> 16;
2357 	      ip->insn_opcode |= tmp & 0xffff;
2358 	      break;
2359 
2360 	    case BFD_RELOC_HI16:
2361 	      ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2362 	      break;
2363 
2364 	    case BFD_RELOC_UNUSED:
2365 	    case BFD_RELOC_LO16:
2366 	    case BFD_RELOC_MIPS_GOT_DISP:
2367 	      ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2368 	      break;
2369 
2370 	    case BFD_RELOC_MIPS_JMP:
2371 	      if ((address_expr->X_add_number & 3) != 0)
2372 		as_bad (_("jump to misaligned address (0x%lx)"),
2373 			(unsigned long) address_expr->X_add_number);
2374 	      if (address_expr->X_add_number & ~0xfffffff)
2375 		as_bad (_("jump address range overflow (0x%lx)"),
2376 			(unsigned long) address_expr->X_add_number);
2377 	      ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2378 	      break;
2379 
2380 	    case BFD_RELOC_MIPS16_JMP:
2381 	      if ((address_expr->X_add_number & 3) != 0)
2382 		as_bad (_("jump to misaligned address (0x%lx)"),
2383 			(unsigned long) address_expr->X_add_number);
2384 	      if (address_expr->X_add_number & ~0xfffffff)
2385 		as_bad (_("jump address range overflow (0x%lx)"),
2386 			(unsigned long) address_expr->X_add_number);
2387 	      ip->insn_opcode |=
2388 		(((address_expr->X_add_number & 0x7c0000) << 3)
2389 		 | ((address_expr->X_add_number & 0xf800000) >> 7)
2390 		 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2391 	      break;
2392 
2393 	    case BFD_RELOC_16_PCREL_S2:
2394 	      goto need_reloc;
2395 
2396 	    default:
2397 	      internalError ();
2398 	    }
2399 	}
2400       else if (*reloc_type < BFD_RELOC_UNUSED)
2401 	need_reloc:
2402 	{
2403 	  reloc_howto_type *howto;
2404 	  int i;
2405 
2406 	  /* In a compound relocation, it is the final (outermost)
2407 	     operator that determines the relocated field.  */
2408 	  for (i = 1; i < 3; i++)
2409 	    if (reloc_type[i] == BFD_RELOC_UNUSED)
2410 	      break;
2411 
2412 	  howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2413 	  ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2414 				     bfd_get_reloc_size (howto),
2415 				     address_expr,
2416 				     reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2417 				     reloc_type[0]);
2418 
2419 	  /* These relocations can have an addend that won't fit in
2420 	     4 octets for 64bit assembly.  */
2421 	  if (HAVE_64BIT_GPRS
2422 	      && ! howto->partial_inplace
2423 	      && (reloc_type[0] == BFD_RELOC_16
2424 		  || reloc_type[0] == BFD_RELOC_32
2425 		  || reloc_type[0] == BFD_RELOC_MIPS_JMP
2426 		  || reloc_type[0] == BFD_RELOC_HI16_S
2427 		  || reloc_type[0] == BFD_RELOC_LO16
2428 		  || reloc_type[0] == BFD_RELOC_GPREL16
2429 		  || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2430 		  || reloc_type[0] == BFD_RELOC_GPREL32
2431 		  || reloc_type[0] == BFD_RELOC_64
2432 		  || reloc_type[0] == BFD_RELOC_CTOR
2433 		  || reloc_type[0] == BFD_RELOC_MIPS_SUB
2434 		  || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2435 		  || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2436 		  || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2437 		  || reloc_type[0] == BFD_RELOC_MIPS_REL16
2438 		  || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2439 		  || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2440 		  || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2441 		  || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2442 	    ip->fixp[0]->fx_no_overflow = 1;
2443 
2444 	  if (mips_relax.sequence)
2445 	    {
2446 	      if (mips_relax.first_fixup == 0)
2447 		mips_relax.first_fixup = ip->fixp[0];
2448 	    }
2449 	  else if (reloc_needs_lo_p (*reloc_type))
2450 	    {
2451 	      struct mips_hi_fixup *hi_fixup;
2452 
2453 	      /* Reuse the last entry if it already has a matching %lo.  */
2454 	      hi_fixup = mips_hi_fixup_list;
2455 	      if (hi_fixup == 0
2456 		  || !fixup_has_matching_lo_p (hi_fixup->fixp))
2457 		{
2458 		  hi_fixup = ((struct mips_hi_fixup *)
2459 			      xmalloc (sizeof (struct mips_hi_fixup)));
2460 		  hi_fixup->next = mips_hi_fixup_list;
2461 		  mips_hi_fixup_list = hi_fixup;
2462 		}
2463 	      hi_fixup->fixp = ip->fixp[0];
2464 	      hi_fixup->seg = now_seg;
2465 	    }
2466 
2467 	  /* Add fixups for the second and third relocations, if given.
2468 	     Note that the ABI allows the second relocation to be
2469 	     against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC.  At the
2470 	     moment we only use RSS_UNDEF, but we could add support
2471 	     for the others if it ever becomes necessary.  */
2472 	  for (i = 1; i < 3; i++)
2473 	    if (reloc_type[i] != BFD_RELOC_UNUSED)
2474 	      {
2475 		ip->fixp[i] = fix_new (ip->frag, ip->where,
2476 				       ip->fixp[0]->fx_size, NULL, 0,
2477 				       FALSE, reloc_type[i]);
2478 
2479 		/* Use fx_tcbit to mark compound relocs.  */
2480 		ip->fixp[0]->fx_tcbit = 1;
2481 		ip->fixp[i]->fx_tcbit = 1;
2482 	      }
2483 	}
2484     }
2485   install_insn (ip);
2486 
2487   /* Update the register mask information.  */
2488   if (! mips_opts.mips16)
2489     {
2490       if (pinfo & INSN_WRITE_GPR_D)
2491 	mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2492       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2493 	mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2494       if (pinfo & INSN_READ_GPR_S)
2495 	mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2496       if (pinfo & INSN_WRITE_GPR_31)
2497 	mips_gprmask |= 1 << RA;
2498       if (pinfo & INSN_WRITE_FPR_D)
2499 	mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2500       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2501 	mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2502       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2503 	mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2504       if ((pinfo & INSN_READ_FPR_R) != 0)
2505 	mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2506       if (pinfo & INSN_COP)
2507 	{
2508 	  /* We don't keep enough information to sort these cases out.
2509 	     The itbl support does keep this information however, although
2510 	     we currently don't support itbl fprmats as part of the cop
2511 	     instruction.  May want to add this support in the future.  */
2512 	}
2513       /* Never set the bit for $0, which is always zero.  */
2514       mips_gprmask &= ~1 << 0;
2515     }
2516   else
2517     {
2518       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2519 	mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2520       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2521 	mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2522       if (pinfo & MIPS16_INSN_WRITE_Z)
2523 	mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2524       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2525 	mips_gprmask |= 1 << TREG;
2526       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2527 	mips_gprmask |= 1 << SP;
2528       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2529 	mips_gprmask |= 1 << RA;
2530       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2531 	mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2532       if (pinfo & MIPS16_INSN_READ_Z)
2533 	mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2534       if (pinfo & MIPS16_INSN_READ_GPR_X)
2535 	mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2536     }
2537 
2538   if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2539     {
2540       /* Filling the branch delay slot is more complex.  We try to
2541 	 switch the branch with the previous instruction, which we can
2542 	 do if the previous instruction does not set up a condition
2543 	 that the branch tests and if the branch is not itself the
2544 	 target of any branch.  */
2545       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2546 	  || (pinfo & INSN_COND_BRANCH_DELAY))
2547 	{
2548 	  if (mips_optimize < 2
2549 	      /* If we have seen .set volatile or .set nomove, don't
2550 		 optimize.  */
2551 	      || mips_opts.nomove != 0
2552 	      /* We can't swap if the previous instruction's position
2553 		 is fixed.  */
2554 	      || history[0].fixed_p
2555 	      /* If the previous previous insn was in a .set
2556 		 noreorder, we can't swap.  Actually, the MIPS
2557 		 assembler will swap in this situation.  However, gcc
2558 		 configured -with-gnu-as will generate code like
2559 		   .set noreorder
2560 		   lw	$4,XXX
2561 		   .set	reorder
2562 		   INSN
2563 		   bne	$4,$0,foo
2564 		 in which we can not swap the bne and INSN.  If gcc is
2565 		 not configured -with-gnu-as, it does not output the
2566 		 .set pseudo-ops.  */
2567 	      || history[1].noreorder_p
2568 	      /* If the branch is itself the target of a branch, we
2569 		 can not swap.  We cheat on this; all we check for is
2570 		 whether there is a label on this instruction.  If
2571 		 there are any branches to anything other than a
2572 		 label, users must use .set noreorder.  */
2573 	      || insn_labels != NULL
2574 	      /* If the previous instruction is in a variant frag
2575 		 other than this branch's one, we cannot do the swap.
2576 		 This does not apply to the mips16, which uses variant
2577 		 frags for different purposes.  */
2578 	      || (! mips_opts.mips16
2579 		  && prev_insn_frag_type == rs_machine_dependent)
2580 	      /* Check for conflicts between the branch and the instructions
2581 		 before the candidate delay slot.  */
2582 	      || nops_for_insn (history + 1, ip) > 0
2583 	      /* Check for conflicts between the swapped sequence and the
2584 		 target of the branch.  */
2585 	      || nops_for_sequence (2, history + 1, ip, history) > 0
2586 	      /* We do not swap with a trap instruction, since it
2587 		 complicates trap handlers to have the trap
2588 		 instruction be in a delay slot.  */
2589 	      || (prev_pinfo & INSN_TRAP)
2590 	      /* If the branch reads a register that the previous
2591 		 instruction sets, we can not swap.  */
2592 	      || (! mips_opts.mips16
2593 		  && (prev_pinfo & INSN_WRITE_GPR_T)
2594 		  && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2595 				    MIPS_GR_REG))
2596 	      || (! mips_opts.mips16
2597 		  && (prev_pinfo & INSN_WRITE_GPR_D)
2598 		  && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2599 				    MIPS_GR_REG))
2600 	      || (mips_opts.mips16
2601 		  && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2602 		       && (insn_uses_reg
2603 			   (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2604 			    MIPS16_REG)))
2605 		      || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2606 			  && (insn_uses_reg
2607 			      (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2608 			       MIPS16_REG)))
2609 		      || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2610 			  && (insn_uses_reg
2611 			      (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2612 			       MIPS16_REG)))
2613 		      || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2614 			  && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2615 		      || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2616 			  && insn_uses_reg (ip, RA, MIPS_GR_REG))
2617 		      || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2618 			  && insn_uses_reg (ip,
2619 					    MIPS16OP_EXTRACT_REG32R
2620 					      (history[0].insn_opcode),
2621 					    MIPS_GR_REG))))
2622 	      /* If the branch writes a register that the previous
2623 		 instruction sets, we can not swap (we know that
2624 		 branches write only to RD or to $31).  */
2625 	      || (! mips_opts.mips16
2626 		  && (prev_pinfo & INSN_WRITE_GPR_T)
2627 		  && (((pinfo & INSN_WRITE_GPR_D)
2628 		       && (EXTRACT_OPERAND (RT, history[0])
2629 			   == EXTRACT_OPERAND (RD, *ip)))
2630 		      || ((pinfo & INSN_WRITE_GPR_31)
2631 			  && EXTRACT_OPERAND (RT, history[0]) == RA)))
2632 	      || (! mips_opts.mips16
2633 		  && (prev_pinfo & INSN_WRITE_GPR_D)
2634 		  && (((pinfo & INSN_WRITE_GPR_D)
2635 		       && (EXTRACT_OPERAND (RD, history[0])
2636 			   == EXTRACT_OPERAND (RD, *ip)))
2637 		      || ((pinfo & INSN_WRITE_GPR_31)
2638 			  && EXTRACT_OPERAND (RD, history[0]) == RA)))
2639 	      || (mips_opts.mips16
2640 		  && (pinfo & MIPS16_INSN_WRITE_31)
2641 		  && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2642 		      || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2643 			  && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2644 			      == RA))))
2645 	      /* If the branch writes a register that the previous
2646 		 instruction reads, we can not swap (we know that
2647 		 branches only write to RD or to $31).  */
2648 	      || (! mips_opts.mips16
2649 		  && (pinfo & INSN_WRITE_GPR_D)
2650 		  && insn_uses_reg (&history[0],
2651 				    EXTRACT_OPERAND (RD, *ip),
2652 				    MIPS_GR_REG))
2653 	      || (! mips_opts.mips16
2654 		  && (pinfo & INSN_WRITE_GPR_31)
2655 		  && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2656 	      || (mips_opts.mips16
2657 		  && (pinfo & MIPS16_INSN_WRITE_31)
2658 		  && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2659 	      /* If one instruction sets a condition code and the
2660                  other one uses a condition code, we can not swap.  */
2661 	      || ((pinfo & INSN_READ_COND_CODE)
2662 		  && (prev_pinfo & INSN_WRITE_COND_CODE))
2663 	      || ((pinfo & INSN_WRITE_COND_CODE)
2664 		  && (prev_pinfo & INSN_READ_COND_CODE))
2665 	      /* If the previous instruction uses the PC, we can not
2666                  swap.  */
2667 	      || (mips_opts.mips16
2668 		  && (prev_pinfo & MIPS16_INSN_READ_PC))
2669 	      /* If the previous instruction had a fixup in mips16
2670                  mode, we can not swap.  This normally means that the
2671                  previous instruction was a 4 byte branch anyhow.  */
2672 	      || (mips_opts.mips16 && history[0].fixp[0])
2673 	      /* If the previous instruction is a sync, sync.l, or
2674 		 sync.p, we can not swap.  */
2675 	      || (prev_pinfo & INSN_SYNC))
2676 	    {
2677 	      /* We could do even better for unconditional branches to
2678 		 portions of this object file; we could pick up the
2679 		 instruction at the destination, put it in the delay
2680 		 slot, and bump the destination address.  */
2681 	      insert_into_history (0, 1, ip);
2682 	      emit_nop ();
2683 	      if (mips_relax.sequence)
2684 		mips_relax.sizes[mips_relax.sequence - 1] += 4;
2685 	    }
2686 	  else
2687 	    {
2688 	      /* It looks like we can actually do the swap.  */
2689 	      struct mips_cl_insn delay = history[0];
2690 	      if (mips_opts.mips16)
2691 		{
2692 		  know (delay.frag == ip->frag);
2693 		  move_insn (ip, delay.frag, delay.where);
2694 		  move_insn (&delay, ip->frag, ip->where + insn_length (ip));
2695 		}
2696 	      else if (relaxed_branch)
2697 		{
2698 		  /* Add the delay slot instruction to the end of the
2699 		     current frag and shrink the fixed part of the
2700 		     original frag.  If the branch occupies the tail of
2701 		     the latter, move it backwards to cover the gap.  */
2702 		  delay.frag->fr_fix -= 4;
2703 		  if (delay.frag == ip->frag)
2704 		    move_insn (ip, ip->frag, ip->where - 4);
2705 		  add_fixed_insn (&delay);
2706 		}
2707 	      else
2708 		{
2709 		  move_insn (&delay, ip->frag, ip->where);
2710 		  move_insn (ip, history[0].frag, history[0].where);
2711 		}
2712 	      history[0] = *ip;
2713 	      delay.fixed_p = 1;
2714 	      insert_into_history (0, 1, &delay);
2715 	    }
2716 
2717 	  /* If that was an unconditional branch, forget the previous
2718 	     insn information.  */
2719 	  if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2720 	    mips_no_prev_insn ();
2721 	}
2722       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2723 	{
2724 	  /* We don't yet optimize a branch likely.  What we should do
2725 	     is look at the target, copy the instruction found there
2726 	     into the delay slot, and increment the branch to jump to
2727 	     the next instruction.  */
2728 	  insert_into_history (0, 1, ip);
2729 	  emit_nop ();
2730 	}
2731       else
2732 	insert_into_history (0, 1, ip);
2733     }
2734   else
2735     insert_into_history (0, 1, ip);
2736 
2737   /* We just output an insn, so the next one doesn't have a label.  */
2738   mips_clear_insn_labels ();
2739 }
2740 
2741 /* Forget that there was any previous instruction or label.  */
2742 
2743 static void
mips_no_prev_insn(void)2744 mips_no_prev_insn (void)
2745 {
2746   prev_nop_frag = NULL;
2747   insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
2748   mips_clear_insn_labels ();
2749 }
2750 
2751 /* This function must be called before we emit something other than
2752    instructions.  It is like mips_no_prev_insn except that it inserts
2753    any NOPS that might be needed by previous instructions.  */
2754 
2755 void
mips_emit_delays(void)2756 mips_emit_delays (void)
2757 {
2758   if (! mips_opts.noreorder)
2759     {
2760       int nops = nops_for_insn (history, NULL);
2761       if (nops > 0)
2762 	{
2763 	  while (nops-- > 0)
2764 	    add_fixed_insn (NOP_INSN);
2765 	  mips_move_labels ();
2766 	}
2767     }
2768   mips_no_prev_insn ();
2769 }
2770 
2771 /* Start a (possibly nested) noreorder block.  */
2772 
2773 static void
start_noreorder(void)2774 start_noreorder (void)
2775 {
2776   if (mips_opts.noreorder == 0)
2777     {
2778       unsigned int i;
2779       int nops;
2780 
2781       /* None of the instructions before the .set noreorder can be moved.  */
2782       for (i = 0; i < ARRAY_SIZE (history); i++)
2783 	history[i].fixed_p = 1;
2784 
2785       /* Insert any nops that might be needed between the .set noreorder
2786 	 block and the previous instructions.  We will later remove any
2787 	 nops that turn out not to be needed.  */
2788       nops = nops_for_insn (history, NULL);
2789       if (nops > 0)
2790 	{
2791 	  if (mips_optimize != 0)
2792 	    {
2793 	      /* Record the frag which holds the nop instructions, so
2794                  that we can remove them if we don't need them.  */
2795 	      frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2796 	      prev_nop_frag = frag_now;
2797 	      prev_nop_frag_holds = nops;
2798 	      prev_nop_frag_required = 0;
2799 	      prev_nop_frag_since = 0;
2800 	    }
2801 
2802 	  for (; nops > 0; --nops)
2803 	    add_fixed_insn (NOP_INSN);
2804 
2805 	  /* Move on to a new frag, so that it is safe to simply
2806 	     decrease the size of prev_nop_frag.  */
2807 	  frag_wane (frag_now);
2808 	  frag_new (0);
2809 	  mips_move_labels ();
2810 	}
2811       mips16_mark_labels ();
2812       mips_clear_insn_labels ();
2813     }
2814   mips_opts.noreorder++;
2815   mips_any_noreorder = 1;
2816 }
2817 
2818 /* End a nested noreorder block.  */
2819 
2820 static void
end_noreorder(void)2821 end_noreorder (void)
2822 {
2823   mips_opts.noreorder--;
2824   if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2825     {
2826       /* Commit to inserting prev_nop_frag_required nops and go back to
2827 	 handling nop insertion the .set reorder way.  */
2828       prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2829 				* (mips_opts.mips16 ? 2 : 4));
2830       insert_into_history (prev_nop_frag_since,
2831 			   prev_nop_frag_required, NOP_INSN);
2832       prev_nop_frag = NULL;
2833     }
2834 }
2835 
2836 /* Set up global variables for the start of a new macro.  */
2837 
2838 static void
macro_start(void)2839 macro_start (void)
2840 {
2841   memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2842   mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2843 				     && (history[0].insn_mo->pinfo
2844 					 & (INSN_UNCOND_BRANCH_DELAY
2845 					    | INSN_COND_BRANCH_DELAY
2846 					    | INSN_COND_BRANCH_LIKELY)) != 0);
2847 }
2848 
2849 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2850    for it.  Return null if no warning is needed.  SUBTYPE is a bitmask of
2851    RELAX_DELAY_SLOT and RELAX_NOMACRO.  */
2852 
2853 static const char *
macro_warning(relax_substateT subtype)2854 macro_warning (relax_substateT subtype)
2855 {
2856   if (subtype & RELAX_DELAY_SLOT)
2857     return _("Macro instruction expanded into multiple instructions"
2858 	     " in a branch delay slot");
2859   else if (subtype & RELAX_NOMACRO)
2860     return _("Macro instruction expanded into multiple instructions");
2861   else
2862     return 0;
2863 }
2864 
2865 /* Finish up a macro.  Emit warnings as appropriate.  */
2866 
2867 static void
macro_end(void)2868 macro_end (void)
2869 {
2870   if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2871     {
2872       relax_substateT subtype;
2873 
2874       /* Set up the relaxation warning flags.  */
2875       subtype = 0;
2876       if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2877 	subtype |= RELAX_SECOND_LONGER;
2878       if (mips_opts.warn_about_macros)
2879 	subtype |= RELAX_NOMACRO;
2880       if (mips_macro_warning.delay_slot_p)
2881 	subtype |= RELAX_DELAY_SLOT;
2882 
2883       if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2884 	{
2885 	  /* Either the macro has a single implementation or both
2886 	     implementations are longer than 4 bytes.  Emit the
2887 	     warning now.  */
2888 	  const char *msg = macro_warning (subtype);
2889 	  if (msg != 0)
2890 	    as_warn (msg);
2891 	}
2892       else
2893 	{
2894 	  /* One implementation might need a warning but the other
2895 	     definitely doesn't.  */
2896 	  mips_macro_warning.first_frag->fr_subtype |= subtype;
2897 	}
2898     }
2899 }
2900 
2901 /* Read a macro's relocation codes from *ARGS and store them in *R.
2902    The first argument in *ARGS will be either the code for a single
2903    relocation or -1 followed by the three codes that make up a
2904    composite relocation.  */
2905 
2906 static void
macro_read_relocs(va_list * args,bfd_reloc_code_real_type * r)2907 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2908 {
2909   int i, next;
2910 
2911   next = va_arg (*args, int);
2912   if (next >= 0)
2913     r[0] = (bfd_reloc_code_real_type) next;
2914   else
2915     for (i = 0; i < 3; i++)
2916       r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2917 }
2918 
2919 /* Build an instruction created by a macro expansion.  This is passed
2920    a pointer to the count of instructions created so far, an
2921    expression, the name of the instruction to build, an operand format
2922    string, and corresponding arguments.  */
2923 
2924 static void
macro_build(expressionS * ep,const char * name,const char * fmt,...)2925 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2926 {
2927   const struct mips_opcode *mo;
2928   struct mips_cl_insn insn;
2929   bfd_reloc_code_real_type r[3];
2930   va_list args;
2931 
2932   va_start (args, fmt);
2933 
2934   if (mips_opts.mips16)
2935     {
2936       mips16_macro_build (ep, name, fmt, args);
2937       va_end (args);
2938       return;
2939     }
2940 
2941   r[0] = BFD_RELOC_UNUSED;
2942   r[1] = BFD_RELOC_UNUSED;
2943   r[2] = BFD_RELOC_UNUSED;
2944   mo = (struct mips_opcode *) hash_find (op_hash, name);
2945   assert (mo);
2946   assert (strcmp (name, mo->name) == 0);
2947 
2948   /* Search until we get a match for NAME.  It is assumed here that
2949      macros will never generate MDMX or MIPS-3D instructions.  */
2950   while (strcmp (fmt, mo->args) != 0
2951 	 || mo->pinfo == INSN_MACRO
2952 	 || !OPCODE_IS_MEMBER (mo,
2953 			       (mips_opts.isa
2954 				| (file_ase_mips16 ? INSN_MIPS16 : 0)),
2955 			       mips_opts.arch)
2956 	 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
2957     {
2958       ++mo;
2959       assert (mo->name);
2960       assert (strcmp (name, mo->name) == 0);
2961     }
2962 
2963   create_insn (&insn, mo);
2964   for (;;)
2965     {
2966       switch (*fmt++)
2967 	{
2968 	case '\0':
2969 	  break;
2970 
2971 	case ',':
2972 	case '(':
2973 	case ')':
2974 	  continue;
2975 
2976 	case '+':
2977 	  switch (*fmt++)
2978 	    {
2979 	    case 'A':
2980 	    case 'E':
2981 	      INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
2982 	      continue;
2983 
2984 	    case 'B':
2985 	    case 'F':
2986 	      /* Note that in the macro case, these arguments are already
2987 		 in MSB form.  (When handling the instruction in the
2988 		 non-macro case, these arguments are sizes from which
2989 		 MSB values must be calculated.)  */
2990 	      INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
2991 	      continue;
2992 
2993 	    case 'C':
2994 	    case 'G':
2995 	    case 'H':
2996 	      /* Note that in the macro case, these arguments are already
2997 		 in MSBD form.  (When handling the instruction in the
2998 		 non-macro case, these arguments are sizes from which
2999 		 MSBD values must be calculated.)  */
3000 	      INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
3001 	      continue;
3002 
3003 	    default:
3004 	      internalError ();
3005 	    }
3006 	  continue;
3007 
3008 	case 't':
3009 	case 'w':
3010 	case 'E':
3011 	  INSERT_OPERAND (RT, insn, va_arg (args, int));
3012 	  continue;
3013 
3014 	case 'c':
3015 	  INSERT_OPERAND (CODE, insn, va_arg (args, int));
3016 	  continue;
3017 
3018 	case 'T':
3019 	case 'W':
3020 	  INSERT_OPERAND (FT, insn, va_arg (args, int));
3021 	  continue;
3022 
3023 	case 'd':
3024 	case 'G':
3025 	case 'K':
3026 	  INSERT_OPERAND (RD, insn, va_arg (args, int));
3027 	  continue;
3028 
3029 	case 'U':
3030 	  {
3031 	    int tmp = va_arg (args, int);
3032 
3033 	    INSERT_OPERAND (RT, insn, tmp);
3034 	    INSERT_OPERAND (RD, insn, tmp);
3035 	    continue;
3036 	  }
3037 
3038 	case 'V':
3039 	case 'S':
3040 	  INSERT_OPERAND (FS, insn, va_arg (args, int));
3041 	  continue;
3042 
3043 	case 'z':
3044 	  continue;
3045 
3046 	case '<':
3047 	  INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3048 	  continue;
3049 
3050 	case 'D':
3051 	  INSERT_OPERAND (FD, insn, va_arg (args, int));
3052 	  continue;
3053 
3054 	case 'B':
3055 	  INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3056 	  continue;
3057 
3058 	case 'J':
3059 	  INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3060 	  continue;
3061 
3062 	case 'q':
3063 	  INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3064 	  continue;
3065 
3066 	case 'b':
3067 	case 's':
3068 	case 'r':
3069 	case 'v':
3070 	  INSERT_OPERAND (RS, insn, va_arg (args, int));
3071 	  continue;
3072 
3073 	case 'i':
3074 	case 'j':
3075 	case 'o':
3076 	  macro_read_relocs (&args, r);
3077 	  assert (*r == BFD_RELOC_GPREL16
3078 		  || *r == BFD_RELOC_MIPS_LITERAL
3079 		  || *r == BFD_RELOC_MIPS_HIGHER
3080 		  || *r == BFD_RELOC_HI16_S
3081 		  || *r == BFD_RELOC_LO16
3082 		  || *r == BFD_RELOC_MIPS_GOT16
3083 		  || *r == BFD_RELOC_MIPS_CALL16
3084 		  || *r == BFD_RELOC_MIPS_GOT_DISP
3085 		  || *r == BFD_RELOC_MIPS_GOT_PAGE
3086 		  || *r == BFD_RELOC_MIPS_GOT_OFST
3087 		  || *r == BFD_RELOC_MIPS_GOT_LO16
3088 		  || *r == BFD_RELOC_MIPS_CALL_LO16);
3089 	  continue;
3090 
3091 	case 'u':
3092 	  macro_read_relocs (&args, r);
3093 	  assert (ep != NULL
3094 		  && (ep->X_op == O_constant
3095 		      || (ep->X_op == O_symbol
3096 			  && (*r == BFD_RELOC_MIPS_HIGHEST
3097 			      || *r == BFD_RELOC_HI16_S
3098 			      || *r == BFD_RELOC_HI16
3099 			      || *r == BFD_RELOC_GPREL16
3100 			      || *r == BFD_RELOC_MIPS_GOT_HI16
3101 			      || *r == BFD_RELOC_MIPS_CALL_HI16))));
3102 	  continue;
3103 
3104 	case 'p':
3105 	  assert (ep != NULL);
3106 	  /*
3107 	   * This allows macro() to pass an immediate expression for
3108 	   * creating short branches without creating a symbol.
3109 	   * Note that the expression still might come from the assembly
3110 	   * input, in which case the value is not checked for range nor
3111 	   * is a relocation entry generated (yuck).
3112 	   */
3113 	  if (ep->X_op == O_constant)
3114 	    {
3115 	      insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3116 	      ep = NULL;
3117 	    }
3118 	  else
3119 	    *r = BFD_RELOC_16_PCREL_S2;
3120 	  continue;
3121 
3122 	case 'a':
3123 	  assert (ep != NULL);
3124 	  *r = BFD_RELOC_MIPS_JMP;
3125 	  continue;
3126 
3127 	case 'C':
3128 	  insn.insn_opcode |= va_arg (args, unsigned long);
3129 	  continue;
3130 
3131 	default:
3132 	  internalError ();
3133 	}
3134       break;
3135     }
3136   va_end (args);
3137   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3138 
3139   append_insn (&insn, ep, r);
3140 }
3141 
3142 static void
mips16_macro_build(expressionS * ep,const char * name,const char * fmt,va_list args)3143 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3144 		    va_list args)
3145 {
3146   struct mips_opcode *mo;
3147   struct mips_cl_insn insn;
3148   bfd_reloc_code_real_type r[3]
3149     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3150 
3151   mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3152   assert (mo);
3153   assert (strcmp (name, mo->name) == 0);
3154 
3155   while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3156     {
3157       ++mo;
3158       assert (mo->name);
3159       assert (strcmp (name, mo->name) == 0);
3160     }
3161 
3162   create_insn (&insn, mo);
3163   for (;;)
3164     {
3165       int c;
3166 
3167       c = *fmt++;
3168       switch (c)
3169 	{
3170 	case '\0':
3171 	  break;
3172 
3173 	case ',':
3174 	case '(':
3175 	case ')':
3176 	  continue;
3177 
3178 	case 'y':
3179 	case 'w':
3180 	  MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3181 	  continue;
3182 
3183 	case 'x':
3184 	case 'v':
3185 	  MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3186 	  continue;
3187 
3188 	case 'z':
3189 	  MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3190 	  continue;
3191 
3192 	case 'Z':
3193 	  MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3194 	  continue;
3195 
3196 	case '0':
3197 	case 'S':
3198 	case 'P':
3199 	case 'R':
3200 	  continue;
3201 
3202 	case 'X':
3203 	  MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3204 	  continue;
3205 
3206 	case 'Y':
3207 	  {
3208 	    int regno;
3209 
3210 	    regno = va_arg (args, int);
3211 	    regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3212 	    insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3213 	  }
3214 	  continue;
3215 
3216 	case '<':
3217 	case '>':
3218 	case '4':
3219 	case '5':
3220 	case 'H':
3221 	case 'W':
3222 	case 'D':
3223 	case 'j':
3224 	case '8':
3225 	case 'V':
3226 	case 'C':
3227 	case 'U':
3228 	case 'k':
3229 	case 'K':
3230 	case 'p':
3231 	case 'q':
3232 	  {
3233 	    assert (ep != NULL);
3234 
3235 	    if (ep->X_op != O_constant)
3236 	      *r = (int) BFD_RELOC_UNUSED + c;
3237 	    else
3238 	      {
3239 		mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3240 			      FALSE, &insn.insn_opcode, &insn.use_extend,
3241 			      &insn.extend);
3242 		ep = NULL;
3243 		*r = BFD_RELOC_UNUSED;
3244 	      }
3245 	  }
3246 	  continue;
3247 
3248 	case '6':
3249 	  MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3250 	  continue;
3251 	}
3252 
3253       break;
3254     }
3255 
3256   assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3257 
3258   append_insn (&insn, ep, r);
3259 }
3260 
3261 /*
3262  * Sign-extend 32-bit mode constants that have bit 31 set and all
3263  * higher bits unset.
3264  */
3265 static void
normalize_constant_expr(expressionS * ex)3266 normalize_constant_expr (expressionS *ex)
3267 {
3268   if (ex->X_op == O_constant
3269       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3270     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3271 			- 0x80000000);
3272 }
3273 
3274 /*
3275  * Sign-extend 32-bit mode address offsets that have bit 31 set and
3276  * all higher bits unset.
3277  */
3278 static void
normalize_address_expr(expressionS * ex)3279 normalize_address_expr (expressionS *ex)
3280 {
3281   if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3282 	|| (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3283       && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3284     ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3285 			- 0x80000000);
3286 }
3287 
3288 /*
3289  * Generate a "jalr" instruction with a relocation hint to the called
3290  * function.  This occurs in NewABI PIC code.
3291  */
3292 static void
macro_build_jalr(expressionS * ep)3293 macro_build_jalr (expressionS *ep)
3294 {
3295   char *f = NULL;
3296 
3297   if (HAVE_NEWABI)
3298     {
3299       frag_grow (8);
3300       f = frag_more (0);
3301     }
3302   macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3303   if (HAVE_NEWABI)
3304     fix_new_exp (frag_now, f - frag_now->fr_literal,
3305 		 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3306 }
3307 
3308 /*
3309  * Generate a "lui" instruction.
3310  */
3311 static void
macro_build_lui(expressionS * ep,int regnum)3312 macro_build_lui (expressionS *ep, int regnum)
3313 {
3314   expressionS high_expr;
3315   const struct mips_opcode *mo;
3316   struct mips_cl_insn insn;
3317   bfd_reloc_code_real_type r[3]
3318     = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3319   const char *name = "lui";
3320   const char *fmt = "t,u";
3321 
3322   assert (! mips_opts.mips16);
3323 
3324   high_expr = *ep;
3325 
3326   if (high_expr.X_op == O_constant)
3327     {
3328       /* we can compute the instruction now without a relocation entry */
3329       high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3330 				>> 16) & 0xffff;
3331       *r = BFD_RELOC_UNUSED;
3332     }
3333   else
3334     {
3335       assert (ep->X_op == O_symbol);
3336       /* _gp_disp is a special case, used from s_cpload.
3337 	 __gnu_local_gp is used if mips_no_shared.  */
3338       assert (mips_pic == NO_PIC
3339 	      || (! HAVE_NEWABI
3340 		  && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3341 	      || (! mips_in_shared
3342 		  && strcmp (S_GET_NAME (ep->X_add_symbol),
3343                              "__gnu_local_gp") == 0));
3344       *r = BFD_RELOC_HI16_S;
3345     }
3346 
3347   mo = hash_find (op_hash, name);
3348   assert (strcmp (name, mo->name) == 0);
3349   assert (strcmp (fmt, mo->args) == 0);
3350   create_insn (&insn, mo);
3351 
3352   insn.insn_opcode = insn.insn_mo->match;
3353   INSERT_OPERAND (RT, insn, regnum);
3354   if (*r == BFD_RELOC_UNUSED)
3355     {
3356       insn.insn_opcode |= high_expr.X_add_number;
3357       append_insn (&insn, NULL, r);
3358     }
3359   else
3360     append_insn (&insn, &high_expr, r);
3361 }
3362 
3363 /* Generate a sequence of instructions to do a load or store from a constant
3364    offset off of a base register (breg) into/from a target register (treg),
3365    using AT if necessary.  */
3366 static void
macro_build_ldst_constoffset(expressionS * ep,const char * op,int treg,int breg,int dbl)3367 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3368 			      int treg, int breg, int dbl)
3369 {
3370   assert (ep->X_op == O_constant);
3371 
3372   /* Sign-extending 32-bit constants makes their handling easier.  */
3373   if (!dbl)
3374     normalize_constant_expr (ep);
3375 
3376   /* Right now, this routine can only handle signed 32-bit constants.  */
3377   if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3378     as_warn (_("operand overflow"));
3379 
3380   if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3381     {
3382       /* Signed 16-bit offset will fit in the op.  Easy!  */
3383       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3384     }
3385   else
3386     {
3387       /* 32-bit offset, need multiple instructions and AT, like:
3388 	   lui      $tempreg,const_hi       (BFD_RELOC_HI16_S)
3389 	   addu     $tempreg,$tempreg,$breg
3390            <op>     $treg,const_lo($tempreg)   (BFD_RELOC_LO16)
3391          to handle the complete offset.  */
3392       macro_build_lui (ep, AT);
3393       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3394       macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3395 
3396       if (mips_opts.noat)
3397 	as_bad (_("Macro used $at after \".set noat\""));
3398     }
3399 }
3400 
3401 /*			set_at()
3402  * Generates code to set the $at register to true (one)
3403  * if reg is less than the immediate expression.
3404  */
3405 static void
set_at(int reg,int unsignedp)3406 set_at (int reg, int unsignedp)
3407 {
3408   if (imm_expr.X_op == O_constant
3409       && imm_expr.X_add_number >= -0x8000
3410       && imm_expr.X_add_number < 0x8000)
3411     macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3412 		 AT, reg, BFD_RELOC_LO16);
3413   else
3414     {
3415       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3416       macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3417     }
3418 }
3419 
3420 /* Warn if an expression is not a constant.  */
3421 
3422 static void
check_absolute_expr(struct mips_cl_insn * ip,expressionS * ex)3423 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3424 {
3425   if (ex->X_op == O_big)
3426     as_bad (_("unsupported large constant"));
3427   else if (ex->X_op != O_constant)
3428     as_bad (_("Instruction %s requires absolute expression"),
3429 	    ip->insn_mo->name);
3430 
3431   if (HAVE_32BIT_GPRS)
3432     normalize_constant_expr (ex);
3433 }
3434 
3435 /* Count the leading zeroes by performing a binary chop. This is a
3436    bulky bit of source, but performance is a LOT better for the
3437    majority of values than a simple loop to count the bits:
3438        for (lcnt = 0; (lcnt < 32); lcnt++)
3439          if ((v) & (1 << (31 - lcnt)))
3440            break;
3441   However it is not code size friendly, and the gain will drop a bit
3442   on certain cached systems.
3443 */
3444 #define COUNT_TOP_ZEROES(v)             \
3445   (((v) & ~0xffff) == 0                 \
3446    ? ((v) & ~0xff) == 0                 \
3447      ? ((v) & ~0xf) == 0                \
3448        ? ((v) & ~0x3) == 0              \
3449          ? ((v) & ~0x1) == 0            \
3450            ? !(v)                       \
3451              ? 32                       \
3452              : 31                       \
3453            : 30                         \
3454          : ((v) & ~0x7) == 0            \
3455            ? 29                         \
3456            : 28                         \
3457        : ((v) & ~0x3f) == 0             \
3458          ? ((v) & ~0x1f) == 0           \
3459            ? 27                         \
3460            : 26                         \
3461          : ((v) & ~0x7f) == 0           \
3462            ? 25                         \
3463            : 24                         \
3464      : ((v) & ~0xfff) == 0              \
3465        ? ((v) & ~0x3ff) == 0            \
3466          ? ((v) & ~0x1ff) == 0          \
3467            ? 23                         \
3468            : 22                         \
3469          : ((v) & ~0x7ff) == 0          \
3470            ? 21                         \
3471            : 20                         \
3472        : ((v) & ~0x3fff) == 0           \
3473          ? ((v) & ~0x1fff) == 0         \
3474            ? 19                         \
3475            : 18                         \
3476          : ((v) & ~0x7fff) == 0         \
3477            ? 17                         \
3478            : 16                         \
3479    : ((v) & ~0xffffff) == 0             \
3480      ? ((v) & ~0xfffff) == 0            \
3481        ? ((v) & ~0x3ffff) == 0          \
3482          ? ((v) & ~0x1ffff) == 0        \
3483            ? 15                         \
3484            : 14                         \
3485          : ((v) & ~0x7ffff) == 0        \
3486            ? 13                         \
3487            : 12                         \
3488        : ((v) & ~0x3fffff) == 0         \
3489          ? ((v) & ~0x1fffff) == 0       \
3490            ? 11                         \
3491            : 10                         \
3492          : ((v) & ~0x7fffff) == 0       \
3493            ? 9                          \
3494            : 8                          \
3495      : ((v) & ~0xfffffff) == 0          \
3496        ? ((v) & ~0x3ffffff) == 0        \
3497          ? ((v) & ~0x1ffffff) == 0      \
3498            ? 7                          \
3499            : 6                          \
3500          : ((v) & ~0x7ffffff) == 0      \
3501            ? 5                          \
3502            : 4                          \
3503        : ((v) & ~0x3fffffff) == 0       \
3504          ? ((v) & ~0x1fffffff) == 0     \
3505            ? 3                          \
3506            : 2                          \
3507          : ((v) & ~0x7fffffff) == 0     \
3508            ? 1                          \
3509            : 0)
3510 
3511 /*			load_register()
3512  *  This routine generates the least number of instructions necessary to load
3513  *  an absolute expression value into a register.
3514  */
3515 static void
load_register(int reg,expressionS * ep,int dbl)3516 load_register (int reg, expressionS *ep, int dbl)
3517 {
3518   int freg;
3519   expressionS hi32, lo32;
3520 
3521   if (ep->X_op != O_big)
3522     {
3523       assert (ep->X_op == O_constant);
3524 
3525       /* Sign-extending 32-bit constants makes their handling easier.  */
3526       if (!dbl)
3527 	normalize_constant_expr (ep);
3528 
3529       if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3530 	{
3531 	  /* We can handle 16 bit signed values with an addiu to
3532 	     $zero.  No need to ever use daddiu here, since $zero and
3533 	     the result are always correct in 32 bit mode.  */
3534 	  macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3535 	  return;
3536 	}
3537       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3538 	{
3539 	  /* We can handle 16 bit unsigned values with an ori to
3540              $zero.  */
3541 	  macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3542 	  return;
3543 	}
3544       else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3545 	{
3546 	  /* 32 bit values require an lui.  */
3547 	  macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3548 	  if ((ep->X_add_number & 0xffff) != 0)
3549 	    macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3550 	  return;
3551 	}
3552     }
3553 
3554   /* The value is larger than 32 bits.  */
3555 
3556   if (!dbl || HAVE_32BIT_GPRS)
3557     {
3558       char value[32];
3559 
3560       sprintf_vma (value, ep->X_add_number);
3561       as_bad (_("Number (0x%s) larger than 32 bits"), value);
3562       macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3563       return;
3564     }
3565 
3566   if (ep->X_op != O_big)
3567     {
3568       hi32 = *ep;
3569       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3570       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3571       hi32.X_add_number &= 0xffffffff;
3572       lo32 = *ep;
3573       lo32.X_add_number &= 0xffffffff;
3574     }
3575   else
3576     {
3577       assert (ep->X_add_number > 2);
3578       if (ep->X_add_number == 3)
3579 	generic_bignum[3] = 0;
3580       else if (ep->X_add_number > 4)
3581 	as_bad (_("Number larger than 64 bits"));
3582       lo32.X_op = O_constant;
3583       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3584       hi32.X_op = O_constant;
3585       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3586     }
3587 
3588   if (hi32.X_add_number == 0)
3589     freg = 0;
3590   else
3591     {
3592       int shift, bit;
3593       unsigned long hi, lo;
3594 
3595       if (hi32.X_add_number == (offsetT) 0xffffffff)
3596 	{
3597 	  if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3598 	    {
3599 	      macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3600 	      return;
3601 	    }
3602 	  if (lo32.X_add_number & 0x80000000)
3603 	    {
3604 	      macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3605 	      if (lo32.X_add_number & 0xffff)
3606 		macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3607 	      return;
3608 	    }
3609 	}
3610 
3611       /* Check for 16bit shifted constant.  We know that hi32 is
3612          non-zero, so start the mask on the first bit of the hi32
3613          value.  */
3614       shift = 17;
3615       do
3616 	{
3617 	  unsigned long himask, lomask;
3618 
3619 	  if (shift < 32)
3620 	    {
3621 	      himask = 0xffff >> (32 - shift);
3622 	      lomask = (0xffff << shift) & 0xffffffff;
3623 	    }
3624 	  else
3625 	    {
3626 	      himask = 0xffff << (shift - 32);
3627 	      lomask = 0;
3628 	    }
3629 	  if ((hi32.X_add_number & ~(offsetT) himask) == 0
3630 	      && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3631 	    {
3632 	      expressionS tmp;
3633 
3634 	      tmp.X_op = O_constant;
3635 	      if (shift < 32)
3636 		tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3637 				    | (lo32.X_add_number >> shift));
3638 	      else
3639 		tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3640 	      macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3641 	      macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3642 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
3643 	      return;
3644 	    }
3645 	  ++shift;
3646 	}
3647       while (shift <= (64 - 16));
3648 
3649       /* Find the bit number of the lowest one bit, and store the
3650          shifted value in hi/lo.  */
3651       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3652       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3653       if (lo != 0)
3654 	{
3655 	  bit = 0;
3656 	  while ((lo & 1) == 0)
3657 	    {
3658 	      lo >>= 1;
3659 	      ++bit;
3660 	    }
3661 	  lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3662 	  hi >>= bit;
3663 	}
3664       else
3665 	{
3666 	  bit = 32;
3667 	  while ((hi & 1) == 0)
3668 	    {
3669 	      hi >>= 1;
3670 	      ++bit;
3671 	    }
3672 	  lo = hi;
3673 	  hi = 0;
3674 	}
3675 
3676       /* Optimize if the shifted value is a (power of 2) - 1.  */
3677       if ((hi == 0 && ((lo + 1) & lo) == 0)
3678 	  || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3679 	{
3680 	  shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3681 	  if (shift != 0)
3682 	    {
3683 	      expressionS tmp;
3684 
3685 	      /* This instruction will set the register to be all
3686                  ones.  */
3687 	      tmp.X_op = O_constant;
3688 	      tmp.X_add_number = (offsetT) -1;
3689 	      macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3690 	      if (bit != 0)
3691 		{
3692 		  bit += shift;
3693 		  macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3694 			       reg, reg, (bit >= 32) ? bit - 32 : bit);
3695 		}
3696 	      macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3697 			   reg, reg, (shift >= 32) ? shift - 32 : shift);
3698 	      return;
3699 	    }
3700 	}
3701 
3702       /* Sign extend hi32 before calling load_register, because we can
3703          generally get better code when we load a sign extended value.  */
3704       if ((hi32.X_add_number & 0x80000000) != 0)
3705 	hi32.X_add_number |= ~(offsetT) 0xffffffff;
3706       load_register (reg, &hi32, 0);
3707       freg = reg;
3708     }
3709   if ((lo32.X_add_number & 0xffff0000) == 0)
3710     {
3711       if (freg != 0)
3712 	{
3713 	  macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3714 	  freg = reg;
3715 	}
3716     }
3717   else
3718     {
3719       expressionS mid16;
3720 
3721       if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3722 	{
3723 	  macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3724 	  macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3725 	  return;
3726 	}
3727 
3728       if (freg != 0)
3729 	{
3730 	  macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3731 	  freg = reg;
3732 	}
3733       mid16 = lo32;
3734       mid16.X_add_number >>= 16;
3735       macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3736       macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3737       freg = reg;
3738     }
3739   if ((lo32.X_add_number & 0xffff) != 0)
3740     macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3741 }
3742 
3743 static inline void
load_delay_nop(void)3744 load_delay_nop (void)
3745 {
3746   if (!gpr_interlocks)
3747     macro_build (NULL, "nop", "");
3748 }
3749 
3750 /* Load an address into a register.  */
3751 
3752 static void
load_address(int reg,expressionS * ep,int * used_at)3753 load_address (int reg, expressionS *ep, int *used_at)
3754 {
3755   if (ep->X_op != O_constant
3756       && ep->X_op != O_symbol)
3757     {
3758       as_bad (_("expression too complex"));
3759       ep->X_op = O_constant;
3760     }
3761 
3762   if (ep->X_op == O_constant)
3763     {
3764       load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3765       return;
3766     }
3767 
3768   if (mips_pic == NO_PIC)
3769     {
3770       /* If this is a reference to a GP relative symbol, we want
3771 	   addiu	$reg,$gp,<sym>		(BFD_RELOC_GPREL16)
3772 	 Otherwise we want
3773 	   lui		$reg,<sym>		(BFD_RELOC_HI16_S)
3774 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
3775 	 If we have an addend, we always use the latter form.
3776 
3777 	 With 64bit address space and a usable $at we want
3778 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
3779 	   lui		$at,<sym>		(BFD_RELOC_HI16_S)
3780 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
3781 	   daddiu	$at,<sym>		(BFD_RELOC_LO16)
3782 	   dsll32	$reg,0
3783 	   daddu	$reg,$reg,$at
3784 
3785 	 If $at is already in use, we use a path which is suboptimal
3786 	 on superscalar processors.
3787 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
3788 	   daddiu	$reg,<sym>		(BFD_RELOC_MIPS_HIGHER)
3789 	   dsll		$reg,16
3790 	   daddiu	$reg,<sym>		(BFD_RELOC_HI16_S)
3791 	   dsll		$reg,16
3792 	   daddiu	$reg,<sym>		(BFD_RELOC_LO16)
3793 
3794 	 For GP relative symbols in 64bit address space we can use
3795 	 the same sequence as in 32bit address space.  */
3796       if (HAVE_64BIT_SYMBOLS)
3797 	{
3798 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3799 	      && !nopic_need_relax (ep->X_add_symbol, 1))
3800 	    {
3801 	      relax_start (ep->X_add_symbol);
3802 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3803 			   mips_gp_register, BFD_RELOC_GPREL16);
3804 	      relax_switch ();
3805 	    }
3806 
3807 	  if (*used_at == 0 && !mips_opts.noat)
3808 	    {
3809 	      macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3810 	      macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3811 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
3812 			   BFD_RELOC_MIPS_HIGHER);
3813 	      macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3814 	      macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3815 	      macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3816 	      *used_at = 1;
3817 	    }
3818 	  else
3819 	    {
3820 	      macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3821 	      macro_build (ep, "daddiu", "t,r,j", reg, reg,
3822 			   BFD_RELOC_MIPS_HIGHER);
3823 	      macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3824 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3825 	      macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3826 	      macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3827 	    }
3828 
3829 	  if (mips_relax.sequence)
3830 	    relax_end ();
3831 	}
3832       else
3833 	{
3834 	  if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3835 	      && !nopic_need_relax (ep->X_add_symbol, 1))
3836 	    {
3837 	      relax_start (ep->X_add_symbol);
3838 	      macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3839 			   mips_gp_register, BFD_RELOC_GPREL16);
3840 	      relax_switch ();
3841 	    }
3842 	  macro_build_lui (ep, reg);
3843 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3844 		       reg, reg, BFD_RELOC_LO16);
3845 	  if (mips_relax.sequence)
3846 	    relax_end ();
3847 	}
3848     }
3849   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3850     {
3851       expressionS ex;
3852 
3853       /* If this is a reference to an external symbol, we want
3854 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
3855 	 Otherwise we want
3856 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
3857 	   nop
3858 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
3859 	 If there is a constant, it must be added in after.
3860 
3861 	 If we have NewABI, we want
3862 	   lw		$reg,<sym+cst>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
3863          unless we're referencing a global symbol with a non-zero
3864          offset, in which case cst must be added separately.  */
3865       if (HAVE_NEWABI)
3866 	{
3867 	  if (ep->X_add_number)
3868 	    {
3869 	      ex.X_add_number = ep->X_add_number;
3870 	      ep->X_add_number = 0;
3871 	      relax_start (ep->X_add_symbol);
3872 	      macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3873 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3874 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3875 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3876 	      ex.X_op = O_constant;
3877 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3878 			   reg, reg, BFD_RELOC_LO16);
3879 	      ep->X_add_number = ex.X_add_number;
3880 	      relax_switch ();
3881 	    }
3882 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3883 		       BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3884 	  if (mips_relax.sequence)
3885 	    relax_end ();
3886 	}
3887       else
3888 	{
3889 	  ex.X_add_number = ep->X_add_number;
3890 	  ep->X_add_number = 0;
3891 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3892 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
3893 	  load_delay_nop ();
3894 	  relax_start (ep->X_add_symbol);
3895 	  relax_switch ();
3896 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3897 		       BFD_RELOC_LO16);
3898 	  relax_end ();
3899 
3900 	  if (ex.X_add_number != 0)
3901 	    {
3902 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3903 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3904 	      ex.X_op = O_constant;
3905 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3906 			   reg, reg, BFD_RELOC_LO16);
3907 	    }
3908 	}
3909     }
3910   else if (mips_pic == SVR4_PIC)
3911     {
3912       expressionS ex;
3913 
3914       /* This is the large GOT case.  If this is a reference to an
3915 	 external symbol, we want
3916 	   lui		$reg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
3917 	   addu		$reg,$reg,$gp
3918 	   lw		$reg,<sym>($reg)	(BFD_RELOC_MIPS_GOT_LO16)
3919 
3920 	 Otherwise, for a reference to a local symbol in old ABI, we want
3921 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
3922 	   nop
3923 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_LO16)
3924 	 If there is a constant, it must be added in after.
3925 
3926 	 In the NewABI, for local symbols, with or without offsets, we want:
3927 	   lw		$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
3928 	   addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
3929       */
3930       if (HAVE_NEWABI)
3931 	{
3932 	  ex.X_add_number = ep->X_add_number;
3933 	  ep->X_add_number = 0;
3934 	  relax_start (ep->X_add_symbol);
3935 	  macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3936 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3937 		       reg, reg, mips_gp_register);
3938 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3939 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3940 	  if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3941 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3942 	  else if (ex.X_add_number)
3943 	    {
3944 	      ex.X_op = O_constant;
3945 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3946 			   BFD_RELOC_LO16);
3947 	    }
3948 
3949 	  ep->X_add_number = ex.X_add_number;
3950 	  relax_switch ();
3951 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3952 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3953 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3954 		       BFD_RELOC_MIPS_GOT_OFST);
3955 	  relax_end ();
3956 	}
3957       else
3958 	{
3959 	  ex.X_add_number = ep->X_add_number;
3960 	  ep->X_add_number = 0;
3961 	  relax_start (ep->X_add_symbol);
3962 	  macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3963 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3964 		       reg, reg, mips_gp_register);
3965 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3966 		       reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3967 	  relax_switch ();
3968 	  if (reg_needs_delay (mips_gp_register))
3969 	    {
3970 	      /* We need a nop before loading from $gp.  This special
3971 		 check is required because the lui which starts the main
3972 		 instruction stream does not refer to $gp, and so will not
3973 		 insert the nop which may be required.  */
3974 	      macro_build (NULL, "nop", "");
3975 	    }
3976 	  macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3977 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
3978 	  load_delay_nop ();
3979 	  macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3980 		       BFD_RELOC_LO16);
3981 	  relax_end ();
3982 
3983 	  if (ex.X_add_number != 0)
3984 	    {
3985 	      if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3986 		as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3987 	      ex.X_op = O_constant;
3988 	      macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3989 			   BFD_RELOC_LO16);
3990 	    }
3991 	}
3992     }
3993   else
3994     abort ();
3995 
3996   if (mips_opts.noat && *used_at == 1)
3997     as_bad (_("Macro used $at after \".set noat\""));
3998 }
3999 
4000 /* Move the contents of register SOURCE into register DEST.  */
4001 
4002 static void
move_register(int dest,int source)4003 move_register (int dest, int source)
4004 {
4005   macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4006 	       dest, source, 0);
4007 }
4008 
4009 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4010    LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4011    The two alternatives are:
4012 
4013    Global symbol		Local sybmol
4014    -------------		------------
4015    lw DEST,%got(SYMBOL)		lw DEST,%got(SYMBOL + OFFSET)
4016    ...				...
4017    addiu DEST,DEST,OFFSET	addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4018 
4019    load_got_offset emits the first instruction and add_got_offset
4020    emits the second for a 16-bit offset or add_got_offset_hilo emits
4021    a sequence to add a 32-bit offset using a scratch register.  */
4022 
4023 static void
load_got_offset(int dest,expressionS * local)4024 load_got_offset (int dest, expressionS *local)
4025 {
4026   expressionS global;
4027 
4028   global = *local;
4029   global.X_add_number = 0;
4030 
4031   relax_start (local->X_add_symbol);
4032   macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4033 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
4034   relax_switch ();
4035   macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4036 	       BFD_RELOC_MIPS_GOT16, mips_gp_register);
4037   relax_end ();
4038 }
4039 
4040 static void
add_got_offset(int dest,expressionS * local)4041 add_got_offset (int dest, expressionS *local)
4042 {
4043   expressionS global;
4044 
4045   global.X_op = O_constant;
4046   global.X_op_symbol = NULL;
4047   global.X_add_symbol = NULL;
4048   global.X_add_number = local->X_add_number;
4049 
4050   relax_start (local->X_add_symbol);
4051   macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4052 	       dest, dest, BFD_RELOC_LO16);
4053   relax_switch ();
4054   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4055   relax_end ();
4056 }
4057 
4058 static void
add_got_offset_hilo(int dest,expressionS * local,int tmp)4059 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4060 {
4061   expressionS global;
4062   int hold_mips_optimize;
4063 
4064   global.X_op = O_constant;
4065   global.X_op_symbol = NULL;
4066   global.X_add_symbol = NULL;
4067   global.X_add_number = local->X_add_number;
4068 
4069   relax_start (local->X_add_symbol);
4070   load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4071   relax_switch ();
4072   /* Set mips_optimize around the lui instruction to avoid
4073      inserting an unnecessary nop after the lw.  */
4074   hold_mips_optimize = mips_optimize;
4075   mips_optimize = 2;
4076   macro_build_lui (&global, tmp);
4077   mips_optimize = hold_mips_optimize;
4078   macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4079   relax_end ();
4080 
4081   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4082 }
4083 
4084 /*
4085  *			Build macros
4086  *   This routine implements the seemingly endless macro or synthesized
4087  * instructions and addressing modes in the mips assembly language. Many
4088  * of these macros are simple and are similar to each other. These could
4089  * probably be handled by some kind of table or grammar approach instead of
4090  * this verbose method. Others are not simple macros but are more like
4091  * optimizing code generation.
4092  *   One interesting optimization is when several store macros appear
4093  * consecutively that would load AT with the upper half of the same address.
4094  * The ensuing load upper instructions are ommited. This implies some kind
4095  * of global optimization. We currently only optimize within a single macro.
4096  *   For many of the load and store macros if the address is specified as a
4097  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4098  * first load register 'at' with zero and use it as the base register. The
4099  * mips assembler simply uses register $zero. Just one tiny optimization
4100  * we're missing.
4101  */
4102 static void
macro(struct mips_cl_insn * ip)4103 macro (struct mips_cl_insn *ip)
4104 {
4105   register int treg, sreg, dreg, breg;
4106   int tempreg;
4107   int mask;
4108   int used_at = 0;
4109   expressionS expr1;
4110   const char *s;
4111   const char *s2;
4112   const char *fmt;
4113   int likely = 0;
4114   int dbl = 0;
4115   int coproc = 0;
4116   int lr = 0;
4117   int imm = 0;
4118   int call = 0;
4119   int off;
4120   offsetT maxnum;
4121   bfd_reloc_code_real_type r;
4122   int hold_mips_optimize;
4123 
4124   assert (! mips_opts.mips16);
4125 
4126   treg = (ip->insn_opcode >> 16) & 0x1f;
4127   dreg = (ip->insn_opcode >> 11) & 0x1f;
4128   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4129   mask = ip->insn_mo->mask;
4130 
4131   expr1.X_op = O_constant;
4132   expr1.X_op_symbol = NULL;
4133   expr1.X_add_symbol = NULL;
4134   expr1.X_add_number = 1;
4135 
4136   switch (mask)
4137     {
4138     case M_DABS:
4139       dbl = 1;
4140     case M_ABS:
4141       /* bgez $a0,.+12
4142 	 move v0,$a0
4143 	 sub v0,$zero,$a0
4144 	 */
4145 
4146       start_noreorder ();
4147 
4148       expr1.X_add_number = 8;
4149       macro_build (&expr1, "bgez", "s,p", sreg);
4150       if (dreg == sreg)
4151 	macro_build (NULL, "nop", "", 0);
4152       else
4153 	move_register (dreg, sreg);
4154       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4155 
4156       end_noreorder ();
4157       break;
4158 
4159     case M_ADD_I:
4160       s = "addi";
4161       s2 = "add";
4162       goto do_addi;
4163     case M_ADDU_I:
4164       s = "addiu";
4165       s2 = "addu";
4166       goto do_addi;
4167     case M_DADD_I:
4168       dbl = 1;
4169       s = "daddi";
4170       s2 = "dadd";
4171       goto do_addi;
4172     case M_DADDU_I:
4173       dbl = 1;
4174       s = "daddiu";
4175       s2 = "daddu";
4176     do_addi:
4177       if (imm_expr.X_op == O_constant
4178 	  && imm_expr.X_add_number >= -0x8000
4179 	  && imm_expr.X_add_number < 0x8000)
4180 	{
4181 	  macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4182 	  break;
4183 	}
4184       used_at = 1;
4185       load_register (AT, &imm_expr, dbl);
4186       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4187       break;
4188 
4189     case M_AND_I:
4190       s = "andi";
4191       s2 = "and";
4192       goto do_bit;
4193     case M_OR_I:
4194       s = "ori";
4195       s2 = "or";
4196       goto do_bit;
4197     case M_NOR_I:
4198       s = "";
4199       s2 = "nor";
4200       goto do_bit;
4201     case M_XOR_I:
4202       s = "xori";
4203       s2 = "xor";
4204     do_bit:
4205       if (imm_expr.X_op == O_constant
4206 	  && imm_expr.X_add_number >= 0
4207 	  && imm_expr.X_add_number < 0x10000)
4208 	{
4209 	  if (mask != M_NOR_I)
4210 	    macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4211 	  else
4212 	    {
4213 	      macro_build (&imm_expr, "ori", "t,r,i",
4214 			   treg, sreg, BFD_RELOC_LO16);
4215 	      macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4216 	    }
4217 	  break;
4218 	}
4219 
4220       used_at = 1;
4221       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4222       macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4223       break;
4224 
4225     case M_BEQ_I:
4226       s = "beq";
4227       goto beq_i;
4228     case M_BEQL_I:
4229       s = "beql";
4230       likely = 1;
4231       goto beq_i;
4232     case M_BNE_I:
4233       s = "bne";
4234       goto beq_i;
4235     case M_BNEL_I:
4236       s = "bnel";
4237       likely = 1;
4238     beq_i:
4239       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4240 	{
4241 	  macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4242 	  break;
4243 	}
4244       used_at = 1;
4245       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4246       macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4247       break;
4248 
4249     case M_BGEL:
4250       likely = 1;
4251     case M_BGE:
4252       if (treg == 0)
4253 	{
4254 	  macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4255 	  break;
4256 	}
4257       if (sreg == 0)
4258 	{
4259 	  macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4260 	  break;
4261 	}
4262       used_at = 1;
4263       macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4264       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4265       break;
4266 
4267     case M_BGTL_I:
4268       likely = 1;
4269     case M_BGT_I:
4270       /* check for > max integer */
4271       maxnum = 0x7fffffff;
4272       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4273 	{
4274 	  maxnum <<= 16;
4275 	  maxnum |= 0xffff;
4276 	  maxnum <<= 16;
4277 	  maxnum |= 0xffff;
4278 	}
4279       if (imm_expr.X_op == O_constant
4280 	  && imm_expr.X_add_number >= maxnum
4281 	  && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4282 	{
4283 	do_false:
4284 	  /* result is always false */
4285 	  if (! likely)
4286 	    macro_build (NULL, "nop", "", 0);
4287 	  else
4288 	    macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4289 	  break;
4290 	}
4291       if (imm_expr.X_op != O_constant)
4292 	as_bad (_("Unsupported large constant"));
4293       ++imm_expr.X_add_number;
4294       /* FALLTHROUGH */
4295     case M_BGE_I:
4296     case M_BGEL_I:
4297       if (mask == M_BGEL_I)
4298 	likely = 1;
4299       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4300 	{
4301 	  macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4302 	  break;
4303 	}
4304       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4305 	{
4306 	  macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4307 	  break;
4308 	}
4309       maxnum = 0x7fffffff;
4310       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4311 	{
4312 	  maxnum <<= 16;
4313 	  maxnum |= 0xffff;
4314 	  maxnum <<= 16;
4315 	  maxnum |= 0xffff;
4316 	}
4317       maxnum = - maxnum - 1;
4318       if (imm_expr.X_op == O_constant
4319 	  && imm_expr.X_add_number <= maxnum
4320 	  && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4321 	{
4322 	do_true:
4323 	  /* result is always true */
4324 	  as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4325 	  macro_build (&offset_expr, "b", "p");
4326 	  break;
4327 	}
4328       used_at = 1;
4329       set_at (sreg, 0);
4330       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4331       break;
4332 
4333     case M_BGEUL:
4334       likely = 1;
4335     case M_BGEU:
4336       if (treg == 0)
4337 	goto do_true;
4338       if (sreg == 0)
4339 	{
4340 	  macro_build (&offset_expr, likely ? "beql" : "beq",
4341 		       "s,t,p", 0, treg);
4342 	  break;
4343 	}
4344       used_at = 1;
4345       macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4346       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4347       break;
4348 
4349     case M_BGTUL_I:
4350       likely = 1;
4351     case M_BGTU_I:
4352       if (sreg == 0
4353 	  || (HAVE_32BIT_GPRS
4354 	      && imm_expr.X_op == O_constant
4355 	      && imm_expr.X_add_number == (offsetT) 0xffffffff))
4356 	goto do_false;
4357       if (imm_expr.X_op != O_constant)
4358 	as_bad (_("Unsupported large constant"));
4359       ++imm_expr.X_add_number;
4360       /* FALLTHROUGH */
4361     case M_BGEU_I:
4362     case M_BGEUL_I:
4363       if (mask == M_BGEUL_I)
4364 	likely = 1;
4365       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4366 	goto do_true;
4367       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4368 	{
4369 	  macro_build (&offset_expr, likely ? "bnel" : "bne",
4370 		       "s,t,p", sreg, 0);
4371 	  break;
4372 	}
4373       used_at = 1;
4374       set_at (sreg, 1);
4375       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4376       break;
4377 
4378     case M_BGTL:
4379       likely = 1;
4380     case M_BGT:
4381       if (treg == 0)
4382 	{
4383 	  macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4384 	  break;
4385 	}
4386       if (sreg == 0)
4387 	{
4388 	  macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4389 	  break;
4390 	}
4391       used_at = 1;
4392       macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4393       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4394       break;
4395 
4396     case M_BGTUL:
4397       likely = 1;
4398     case M_BGTU:
4399       if (treg == 0)
4400 	{
4401 	  macro_build (&offset_expr, likely ? "bnel" : "bne",
4402 		       "s,t,p", sreg, 0);
4403 	  break;
4404 	}
4405       if (sreg == 0)
4406 	goto do_false;
4407       used_at = 1;
4408       macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4409       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4410       break;
4411 
4412     case M_BLEL:
4413       likely = 1;
4414     case M_BLE:
4415       if (treg == 0)
4416 	{
4417 	  macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4418 	  break;
4419 	}
4420       if (sreg == 0)
4421 	{
4422 	  macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4423 	  break;
4424 	}
4425       used_at = 1;
4426       macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4427       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4428       break;
4429 
4430     case M_BLEL_I:
4431       likely = 1;
4432     case M_BLE_I:
4433       maxnum = 0x7fffffff;
4434       if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4435 	{
4436 	  maxnum <<= 16;
4437 	  maxnum |= 0xffff;
4438 	  maxnum <<= 16;
4439 	  maxnum |= 0xffff;
4440 	}
4441       if (imm_expr.X_op == O_constant
4442 	  && imm_expr.X_add_number >= maxnum
4443 	  && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4444 	goto do_true;
4445       if (imm_expr.X_op != O_constant)
4446 	as_bad (_("Unsupported large constant"));
4447       ++imm_expr.X_add_number;
4448       /* FALLTHROUGH */
4449     case M_BLT_I:
4450     case M_BLTL_I:
4451       if (mask == M_BLTL_I)
4452 	likely = 1;
4453       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4454 	{
4455 	  macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4456 	  break;
4457 	}
4458       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4459 	{
4460 	  macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4461 	  break;
4462 	}
4463       used_at = 1;
4464       set_at (sreg, 0);
4465       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4466       break;
4467 
4468     case M_BLEUL:
4469       likely = 1;
4470     case M_BLEU:
4471       if (treg == 0)
4472 	{
4473 	  macro_build (&offset_expr, likely ? "beql" : "beq",
4474 		       "s,t,p", sreg, 0);
4475 	  break;
4476 	}
4477       if (sreg == 0)
4478 	goto do_true;
4479       used_at = 1;
4480       macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4481       macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4482       break;
4483 
4484     case M_BLEUL_I:
4485       likely = 1;
4486     case M_BLEU_I:
4487       if (sreg == 0
4488 	  || (HAVE_32BIT_GPRS
4489 	      && imm_expr.X_op == O_constant
4490 	      && imm_expr.X_add_number == (offsetT) 0xffffffff))
4491 	goto do_true;
4492       if (imm_expr.X_op != O_constant)
4493 	as_bad (_("Unsupported large constant"));
4494       ++imm_expr.X_add_number;
4495       /* FALLTHROUGH */
4496     case M_BLTU_I:
4497     case M_BLTUL_I:
4498       if (mask == M_BLTUL_I)
4499 	likely = 1;
4500       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4501 	goto do_false;
4502       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4503 	{
4504 	  macro_build (&offset_expr, likely ? "beql" : "beq",
4505 		       "s,t,p", sreg, 0);
4506 	  break;
4507 	}
4508       used_at = 1;
4509       set_at (sreg, 1);
4510       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4511       break;
4512 
4513     case M_BLTL:
4514       likely = 1;
4515     case M_BLT:
4516       if (treg == 0)
4517 	{
4518 	  macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4519 	  break;
4520 	}
4521       if (sreg == 0)
4522 	{
4523 	  macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4524 	  break;
4525 	}
4526       used_at = 1;
4527       macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4528       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4529       break;
4530 
4531     case M_BLTUL:
4532       likely = 1;
4533     case M_BLTU:
4534       if (treg == 0)
4535 	goto do_false;
4536       if (sreg == 0)
4537 	{
4538 	  macro_build (&offset_expr, likely ? "bnel" : "bne",
4539 		       "s,t,p", 0, treg);
4540 	  break;
4541 	}
4542       used_at = 1;
4543       macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4544       macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4545       break;
4546 
4547     case M_DEXT:
4548       {
4549 	unsigned long pos;
4550 	unsigned long size;
4551 
4552         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4553 	  {
4554 	    as_bad (_("Unsupported large constant"));
4555 	    pos = size = 1;
4556 	  }
4557 	else
4558 	  {
4559 	    pos = (unsigned long) imm_expr.X_add_number;
4560 	    size = (unsigned long) imm2_expr.X_add_number;
4561 	  }
4562 
4563 	if (pos > 63)
4564 	  {
4565 	    as_bad (_("Improper position (%lu)"), pos);
4566 	    pos = 1;
4567 	  }
4568         if (size == 0 || size > 64
4569 	    || (pos + size - 1) > 63)
4570 	  {
4571 	    as_bad (_("Improper extract size (%lu, position %lu)"),
4572 		    size, pos);
4573 	    size = 1;
4574 	  }
4575 
4576 	if (size <= 32 && pos < 32)
4577 	  {
4578 	    s = "dext";
4579 	    fmt = "t,r,+A,+C";
4580 	  }
4581 	else if (size <= 32)
4582 	  {
4583 	    s = "dextu";
4584 	    fmt = "t,r,+E,+H";
4585 	  }
4586 	else
4587 	  {
4588 	    s = "dextm";
4589 	    fmt = "t,r,+A,+G";
4590 	  }
4591 	macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4592       }
4593       break;
4594 
4595     case M_DINS:
4596       {
4597 	unsigned long pos;
4598 	unsigned long size;
4599 
4600         if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4601 	  {
4602 	    as_bad (_("Unsupported large constant"));
4603 	    pos = size = 1;
4604 	  }
4605 	else
4606 	  {
4607 	    pos = (unsigned long) imm_expr.X_add_number;
4608 	    size = (unsigned long) imm2_expr.X_add_number;
4609 	  }
4610 
4611 	if (pos > 63)
4612 	  {
4613 	    as_bad (_("Improper position (%lu)"), pos);
4614 	    pos = 1;
4615 	  }
4616         if (size == 0 || size > 64
4617 	    || (pos + size - 1) > 63)
4618 	  {
4619 	    as_bad (_("Improper insert size (%lu, position %lu)"),
4620 		    size, pos);
4621 	    size = 1;
4622 	  }
4623 
4624 	if (pos < 32 && (pos + size - 1) < 32)
4625 	  {
4626 	    s = "dins";
4627 	    fmt = "t,r,+A,+B";
4628 	  }
4629 	else if (pos >= 32)
4630 	  {
4631 	    s = "dinsu";
4632 	    fmt = "t,r,+E,+F";
4633 	  }
4634 	else
4635 	  {
4636 	    s = "dinsm";
4637 	    fmt = "t,r,+A,+F";
4638 	  }
4639 	macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4640 		     pos + size - 1);
4641       }
4642       break;
4643 
4644     case M_DDIV_3:
4645       dbl = 1;
4646     case M_DIV_3:
4647       s = "mflo";
4648       goto do_div3;
4649     case M_DREM_3:
4650       dbl = 1;
4651     case M_REM_3:
4652       s = "mfhi";
4653     do_div3:
4654       if (treg == 0)
4655 	{
4656 	  as_warn (_("Divide by zero."));
4657 	  if (mips_trap)
4658 	    macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4659 	  else
4660 	    macro_build (NULL, "break", "c", 7);
4661 	  break;
4662 	}
4663 
4664       start_noreorder ();
4665       if (mips_trap)
4666 	{
4667 	  macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4668 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4669 	}
4670       else
4671 	{
4672 	  expr1.X_add_number = 8;
4673 	  macro_build (&expr1, "bne", "s,t,p", treg, 0);
4674 	  macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4675 	  macro_build (NULL, "break", "c", 7);
4676 	}
4677       expr1.X_add_number = -1;
4678       used_at = 1;
4679       load_register (AT, &expr1, dbl);
4680       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4681       macro_build (&expr1, "bne", "s,t,p", treg, AT);
4682       if (dbl)
4683 	{
4684 	  expr1.X_add_number = 1;
4685 	  load_register (AT, &expr1, dbl);
4686 	  macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4687 	}
4688       else
4689 	{
4690 	  expr1.X_add_number = 0x80000000;
4691 	  macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4692 	}
4693       if (mips_trap)
4694 	{
4695 	  macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4696 	  /* We want to close the noreorder block as soon as possible, so
4697 	     that later insns are available for delay slot filling.  */
4698 	  end_noreorder ();
4699 	}
4700       else
4701 	{
4702 	  expr1.X_add_number = 8;
4703 	  macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4704 	  macro_build (NULL, "nop", "", 0);
4705 
4706 	  /* We want to close the noreorder block as soon as possible, so
4707 	     that later insns are available for delay slot filling.  */
4708 	  end_noreorder ();
4709 
4710 	  macro_build (NULL, "break", "c", 6);
4711 	}
4712       macro_build (NULL, s, "d", dreg);
4713       break;
4714 
4715     case M_DIV_3I:
4716       s = "div";
4717       s2 = "mflo";
4718       goto do_divi;
4719     case M_DIVU_3I:
4720       s = "divu";
4721       s2 = "mflo";
4722       goto do_divi;
4723     case M_REM_3I:
4724       s = "div";
4725       s2 = "mfhi";
4726       goto do_divi;
4727     case M_REMU_3I:
4728       s = "divu";
4729       s2 = "mfhi";
4730       goto do_divi;
4731     case M_DDIV_3I:
4732       dbl = 1;
4733       s = "ddiv";
4734       s2 = "mflo";
4735       goto do_divi;
4736     case M_DDIVU_3I:
4737       dbl = 1;
4738       s = "ddivu";
4739       s2 = "mflo";
4740       goto do_divi;
4741     case M_DREM_3I:
4742       dbl = 1;
4743       s = "ddiv";
4744       s2 = "mfhi";
4745       goto do_divi;
4746     case M_DREMU_3I:
4747       dbl = 1;
4748       s = "ddivu";
4749       s2 = "mfhi";
4750     do_divi:
4751       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4752 	{
4753 	  as_warn (_("Divide by zero."));
4754 	  if (mips_trap)
4755 	    macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4756 	  else
4757 	    macro_build (NULL, "break", "c", 7);
4758 	  break;
4759 	}
4760       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4761 	{
4762 	  if (strcmp (s2, "mflo") == 0)
4763 	    move_register (dreg, sreg);
4764 	  else
4765 	    move_register (dreg, 0);
4766 	  break;
4767 	}
4768       if (imm_expr.X_op == O_constant
4769 	  && imm_expr.X_add_number == -1
4770 	  && s[strlen (s) - 1] != 'u')
4771 	{
4772 	  if (strcmp (s2, "mflo") == 0)
4773 	    {
4774 	      macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4775 	    }
4776 	  else
4777 	    move_register (dreg, 0);
4778 	  break;
4779 	}
4780 
4781       used_at = 1;
4782       load_register (AT, &imm_expr, dbl);
4783       macro_build (NULL, s, "z,s,t", sreg, AT);
4784       macro_build (NULL, s2, "d", dreg);
4785       break;
4786 
4787     case M_DIVU_3:
4788       s = "divu";
4789       s2 = "mflo";
4790       goto do_divu3;
4791     case M_REMU_3:
4792       s = "divu";
4793       s2 = "mfhi";
4794       goto do_divu3;
4795     case M_DDIVU_3:
4796       s = "ddivu";
4797       s2 = "mflo";
4798       goto do_divu3;
4799     case M_DREMU_3:
4800       s = "ddivu";
4801       s2 = "mfhi";
4802     do_divu3:
4803       start_noreorder ();
4804       if (mips_trap)
4805 	{
4806 	  macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4807 	  macro_build (NULL, s, "z,s,t", sreg, treg);
4808 	  /* We want to close the noreorder block as soon as possible, so
4809 	     that later insns are available for delay slot filling.  */
4810 	  end_noreorder ();
4811 	}
4812       else
4813 	{
4814 	  expr1.X_add_number = 8;
4815 	  macro_build (&expr1, "bne", "s,t,p", treg, 0);
4816 	  macro_build (NULL, s, "z,s,t", sreg, treg);
4817 
4818 	  /* We want to close the noreorder block as soon as possible, so
4819 	     that later insns are available for delay slot filling.  */
4820 	  end_noreorder ();
4821 	  macro_build (NULL, "break", "c", 7);
4822 	}
4823       macro_build (NULL, s2, "d", dreg);
4824       break;
4825 
4826     case M_DLCA_AB:
4827       dbl = 1;
4828     case M_LCA_AB:
4829       call = 1;
4830       goto do_la;
4831     case M_DLA_AB:
4832       dbl = 1;
4833     case M_LA_AB:
4834     do_la:
4835       /* Load the address of a symbol into a register.  If breg is not
4836 	 zero, we then add a base register to it.  */
4837 
4838       if (dbl && HAVE_32BIT_GPRS)
4839 	as_warn (_("dla used to load 32-bit register"));
4840 
4841       if (! dbl && HAVE_64BIT_OBJECTS)
4842 	as_warn (_("la used to load 64-bit address"));
4843 
4844       if (offset_expr.X_op == O_constant
4845 	  && offset_expr.X_add_number >= -0x8000
4846 	  && offset_expr.X_add_number < 0x8000)
4847 	{
4848 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4849 		       "t,r,j", treg, sreg, BFD_RELOC_LO16);
4850 	  break;
4851 	}
4852 
4853       if (!mips_opts.noat && (treg == breg))
4854 	{
4855 	  tempreg = AT;
4856 	  used_at = 1;
4857 	}
4858       else
4859 	{
4860 	  tempreg = treg;
4861 	}
4862 
4863       if (offset_expr.X_op != O_symbol
4864 	  && offset_expr.X_op != O_constant)
4865 	{
4866 	  as_bad (_("expression too complex"));
4867 	  offset_expr.X_op = O_constant;
4868 	}
4869 
4870       if (offset_expr.X_op == O_constant)
4871 	load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4872       else if (mips_pic == NO_PIC)
4873 	{
4874 	  /* If this is a reference to a GP relative symbol, we want
4875 	       addiu	$tempreg,$gp,<sym>	(BFD_RELOC_GPREL16)
4876 	     Otherwise we want
4877 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
4878 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
4879 	     If we have a constant, we need two instructions anyhow,
4880 	     so we may as well always use the latter form.
4881 
4882 	     With 64bit address space and a usable $at we want
4883 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
4884 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
4885 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
4886 	       daddiu	$at,<sym>		(BFD_RELOC_LO16)
4887 	       dsll32	$tempreg,0
4888 	       daddu	$tempreg,$tempreg,$at
4889 
4890 	     If $at is already in use, we use a path which is suboptimal
4891 	     on superscalar processors.
4892 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
4893 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
4894 	       dsll	$tempreg,16
4895 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
4896 	       dsll	$tempreg,16
4897 	       daddiu	$tempreg,<sym>		(BFD_RELOC_LO16)
4898 
4899 	     For GP relative symbols in 64bit address space we can use
4900 	     the same sequence as in 32bit address space.  */
4901 	  if (HAVE_64BIT_SYMBOLS)
4902 	    {
4903 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4904 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4905 		{
4906 		  relax_start (offset_expr.X_add_symbol);
4907 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4908 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4909 		  relax_switch ();
4910 		}
4911 
4912 	      if (used_at == 0 && !mips_opts.noat)
4913 		{
4914 		  macro_build (&offset_expr, "lui", "t,u",
4915 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
4916 		  macro_build (&offset_expr, "lui", "t,u",
4917 			       AT, BFD_RELOC_HI16_S);
4918 		  macro_build (&offset_expr, "daddiu", "t,r,j",
4919 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4920 		  macro_build (&offset_expr, "daddiu", "t,r,j",
4921 			       AT, AT, BFD_RELOC_LO16);
4922 		  macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4923 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4924 		  used_at = 1;
4925 		}
4926 	      else
4927 		{
4928 		  macro_build (&offset_expr, "lui", "t,u",
4929 			       tempreg, BFD_RELOC_MIPS_HIGHEST);
4930 		  macro_build (&offset_expr, "daddiu", "t,r,j",
4931 			       tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4932 		  macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4933 		  macro_build (&offset_expr, "daddiu", "t,r,j",
4934 			       tempreg, tempreg, BFD_RELOC_HI16_S);
4935 		  macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4936 		  macro_build (&offset_expr, "daddiu", "t,r,j",
4937 			       tempreg, tempreg, BFD_RELOC_LO16);
4938 		}
4939 
4940 	      if (mips_relax.sequence)
4941 		relax_end ();
4942 	    }
4943 	  else
4944 	    {
4945 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4946 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4947 		{
4948 		  relax_start (offset_expr.X_add_symbol);
4949 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4950 			       tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4951 		  relax_switch ();
4952 		}
4953 	      if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
4954 		as_bad (_("offset too large"));
4955 	      macro_build_lui (&offset_expr, tempreg);
4956 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4957 			   tempreg, tempreg, BFD_RELOC_LO16);
4958 	      if (mips_relax.sequence)
4959 		relax_end ();
4960 	    }
4961 	}
4962       else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4963 	{
4964 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4965 
4966 	  /* If this is a reference to an external symbol, and there
4967 	     is no constant, we want
4968 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
4969 	     or for lca or if tempreg is PIC_CALL_REG
4970 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
4971 	     For a local symbol, we want
4972 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
4973 	       nop
4974 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
4975 
4976 	     If we have a small constant, and this is a reference to
4977 	     an external symbol, we want
4978 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
4979 	       nop
4980 	       addiu	$tempreg,$tempreg,<constant>
4981 	     For a local symbol, we want the same instruction
4982 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
4983 	     addiu instruction.
4984 
4985 	     If we have a large constant, and this is a reference to
4986 	     an external symbol, we want
4987 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
4988 	       lui	$at,<hiconstant>
4989 	       addiu	$at,$at,<loconstant>
4990 	       addu	$tempreg,$tempreg,$at
4991 	     For a local symbol, we want the same instruction
4992 	     sequence, but we output a BFD_RELOC_LO16 reloc on the
4993 	     addiu instruction.
4994 	   */
4995 
4996 	  if (offset_expr.X_add_number == 0)
4997 	    {
4998 	      if (breg == 0 && (call || tempreg == PIC_CALL_REG))
4999 		lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5000 
5001 	      relax_start (offset_expr.X_add_symbol);
5002 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5003 			   lw_reloc_type, mips_gp_register);
5004 	      if (breg != 0)
5005 		{
5006 		  /* We're going to put in an addu instruction using
5007 		     tempreg, so we may as well insert the nop right
5008 		     now.  */
5009 		  load_delay_nop ();
5010 		}
5011 	      relax_switch ();
5012 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5013 			   tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5014 	      load_delay_nop ();
5015 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5016 			   tempreg, tempreg, BFD_RELOC_LO16);
5017 	      relax_end ();
5018 	      /* FIXME: If breg == 0, and the next instruction uses
5019 		 $tempreg, then if this variant case is used an extra
5020 		 nop will be generated.  */
5021 	    }
5022 	  else if (offset_expr.X_add_number >= -0x8000
5023 		   && offset_expr.X_add_number < 0x8000)
5024 	    {
5025 	      load_got_offset (tempreg, &offset_expr);
5026 	      load_delay_nop ();
5027 	      add_got_offset (tempreg, &offset_expr);
5028 	    }
5029 	  else
5030 	    {
5031 	      expr1.X_add_number = offset_expr.X_add_number;
5032 	      offset_expr.X_add_number =
5033 		((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5034 	      load_got_offset (tempreg, &offset_expr);
5035 	      offset_expr.X_add_number = expr1.X_add_number;
5036 	      /* If we are going to add in a base register, and the
5037 		 target register and the base register are the same,
5038 		 then we are using AT as a temporary register.  Since
5039 		 we want to load the constant into AT, we add our
5040 		 current AT (from the global offset table) and the
5041 		 register into the register now, and pretend we were
5042 		 not using a base register.  */
5043 	      if (breg == treg)
5044 		{
5045 		  load_delay_nop ();
5046 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5047 			       treg, AT, breg);
5048 		  breg = 0;
5049 		  tempreg = treg;
5050 		}
5051 	      add_got_offset_hilo (tempreg, &offset_expr, AT);
5052 	      used_at = 1;
5053 	    }
5054 	}
5055       else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5056 	{
5057 	  int add_breg_early = 0;
5058 
5059 	  /* If this is a reference to an external, and there is no
5060 	     constant, or local symbol (*), with or without a
5061 	     constant, we want
5062 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
5063 	     or for lca or if tempreg is PIC_CALL_REG
5064 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_CALL16)
5065 
5066 	     If we have a small constant, and this is a reference to
5067 	     an external symbol, we want
5068 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
5069 	       addiu	$tempreg,$tempreg,<constant>
5070 
5071 	     If we have a large constant, and this is a reference to
5072 	     an external symbol, we want
5073 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_DISP)
5074 	       lui	$at,<hiconstant>
5075 	       addiu	$at,$at,<loconstant>
5076 	       addu	$tempreg,$tempreg,$at
5077 
5078 	     (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5079 	     local symbols, even though it introduces an additional
5080 	     instruction.  */
5081 
5082 	  if (offset_expr.X_add_number)
5083 	    {
5084 	      expr1.X_add_number = offset_expr.X_add_number;
5085 	      offset_expr.X_add_number = 0;
5086 
5087 	      relax_start (offset_expr.X_add_symbol);
5088 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5089 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5090 
5091 	      if (expr1.X_add_number >= -0x8000
5092 		  && expr1.X_add_number < 0x8000)
5093 		{
5094 		  macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5095 			       tempreg, tempreg, BFD_RELOC_LO16);
5096 		}
5097 	      else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5098 		{
5099 		  int dreg;
5100 
5101 		  /* If we are going to add in a base register, and the
5102 		     target register and the base register are the same,
5103 		     then we are using AT as a temporary register.  Since
5104 		     we want to load the constant into AT, we add our
5105 		     current AT (from the global offset table) and the
5106 		     register into the register now, and pretend we were
5107 		     not using a base register.  */
5108 		  if (breg != treg)
5109 		    dreg = tempreg;
5110 		  else
5111 		    {
5112 		      assert (tempreg == AT);
5113 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5114 				   treg, AT, breg);
5115 		      dreg = treg;
5116 		      add_breg_early = 1;
5117 		    }
5118 
5119 		  load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5120 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5121 			       dreg, dreg, AT);
5122 
5123 		  used_at = 1;
5124 		}
5125 	      else
5126 		as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5127 
5128 	      relax_switch ();
5129 	      offset_expr.X_add_number = expr1.X_add_number;
5130 
5131 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5132 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5133 	      if (add_breg_early)
5134 		{
5135 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5136 			       treg, tempreg, breg);
5137 		  breg = 0;
5138 		  tempreg = treg;
5139 		}
5140 	      relax_end ();
5141 	    }
5142 	  else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5143 	    {
5144 	      relax_start (offset_expr.X_add_symbol);
5145 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5146 			   BFD_RELOC_MIPS_CALL16, mips_gp_register);
5147 	      relax_switch ();
5148 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5149 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5150 	      relax_end ();
5151 	    }
5152 	  else
5153 	    {
5154 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5155 			   BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5156 	    }
5157 	}
5158       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5159 	{
5160 	  int gpdelay;
5161 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5162 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5163 	  int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5164 
5165 	  /* This is the large GOT case.  If this is a reference to an
5166 	     external symbol, and there is no constant, we want
5167 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5168 	       addu	$tempreg,$tempreg,$gp
5169 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5170 	     or for lca or if tempreg is PIC_CALL_REG
5171 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
5172 	       addu	$tempreg,$tempreg,$gp
5173 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5174 	     For a local symbol, we want
5175 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
5176 	       nop
5177 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
5178 
5179 	     If we have a small constant, and this is a reference to
5180 	     an external symbol, we want
5181 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5182 	       addu	$tempreg,$tempreg,$gp
5183 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5184 	       nop
5185 	       addiu	$tempreg,$tempreg,<constant>
5186 	     For a local symbol, we want
5187 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
5188 	       nop
5189 	       addiu	$tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5190 
5191 	     If we have a large constant, and this is a reference to
5192 	     an external symbol, we want
5193 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5194 	       addu	$tempreg,$tempreg,$gp
5195 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5196 	       lui	$at,<hiconstant>
5197 	       addiu	$at,$at,<loconstant>
5198 	       addu	$tempreg,$tempreg,$at
5199 	     For a local symbol, we want
5200 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
5201 	       lui	$at,<hiconstant>
5202 	       addiu	$at,$at,<loconstant>	(BFD_RELOC_LO16)
5203 	       addu	$tempreg,$tempreg,$at
5204 	  */
5205 
5206 	  expr1.X_add_number = offset_expr.X_add_number;
5207 	  offset_expr.X_add_number = 0;
5208 	  relax_start (offset_expr.X_add_symbol);
5209 	  gpdelay = reg_needs_delay (mips_gp_register);
5210 	  if (expr1.X_add_number == 0 && breg == 0
5211 	      && (call || tempreg == PIC_CALL_REG))
5212 	    {
5213 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5214 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5215 	    }
5216 	  macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5217 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5218 		       tempreg, tempreg, mips_gp_register);
5219 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5220 		       tempreg, lw_reloc_type, tempreg);
5221 	  if (expr1.X_add_number == 0)
5222 	    {
5223 	      if (breg != 0)
5224 		{
5225 		  /* We're going to put in an addu instruction using
5226 		     tempreg, so we may as well insert the nop right
5227 		     now.  */
5228 		  load_delay_nop ();
5229 		}
5230 	    }
5231 	  else if (expr1.X_add_number >= -0x8000
5232 		   && expr1.X_add_number < 0x8000)
5233 	    {
5234 	      load_delay_nop ();
5235 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5236 			   tempreg, tempreg, BFD_RELOC_LO16);
5237 	    }
5238 	  else
5239 	    {
5240 	      int dreg;
5241 
5242 	      /* If we are going to add in a base register, and the
5243 		 target register and the base register are the same,
5244 		 then we are using AT as a temporary register.  Since
5245 		 we want to load the constant into AT, we add our
5246 		 current AT (from the global offset table) and the
5247 		 register into the register now, and pretend we were
5248 		 not using a base register.  */
5249 	      if (breg != treg)
5250 		dreg = tempreg;
5251 	      else
5252 		{
5253 		  assert (tempreg == AT);
5254 		  load_delay_nop ();
5255 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5256 			       treg, AT, breg);
5257 		  dreg = treg;
5258 		}
5259 
5260 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5261 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5262 
5263 	      used_at = 1;
5264 	    }
5265 	  offset_expr.X_add_number =
5266 	    ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5267 	  relax_switch ();
5268 
5269 	  if (gpdelay)
5270 	    {
5271 	      /* This is needed because this instruction uses $gp, but
5272 		 the first instruction on the main stream does not.  */
5273 	      macro_build (NULL, "nop", "");
5274 	    }
5275 
5276 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5277 		       local_reloc_type, mips_gp_register);
5278 	  if (expr1.X_add_number >= -0x8000
5279 	      && expr1.X_add_number < 0x8000)
5280 	    {
5281 	      load_delay_nop ();
5282 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5283 			   tempreg, tempreg, BFD_RELOC_LO16);
5284 	      /* FIXME: If add_number is 0, and there was no base
5285 		 register, the external symbol case ended with a load,
5286 		 so if the symbol turns out to not be external, and
5287 		 the next instruction uses tempreg, an unnecessary nop
5288 		 will be inserted.  */
5289 	    }
5290 	  else
5291 	    {
5292 	      if (breg == treg)
5293 		{
5294 		  /* We must add in the base register now, as in the
5295 		     external symbol case.  */
5296 		  assert (tempreg == AT);
5297 		  load_delay_nop ();
5298 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5299 			       treg, AT, breg);
5300 		  tempreg = treg;
5301 		  /* We set breg to 0 because we have arranged to add
5302 		     it in in both cases.  */
5303 		  breg = 0;
5304 		}
5305 
5306 	      macro_build_lui (&expr1, AT);
5307 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5308 			   AT, AT, BFD_RELOC_LO16);
5309 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5310 			   tempreg, tempreg, AT);
5311 	      used_at = 1;
5312 	    }
5313 	  relax_end ();
5314 	}
5315       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5316 	{
5317 	  int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5318 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5319 	  int add_breg_early = 0;
5320 
5321 	  /* This is the large GOT case.  If this is a reference to an
5322 	     external symbol, and there is no constant, we want
5323 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5324 	       add	$tempreg,$tempreg,$gp
5325 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5326 	     or for lca or if tempreg is PIC_CALL_REG
5327 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
5328 	       add	$tempreg,$tempreg,$gp
5329 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5330 
5331 	     If we have a small constant, and this is a reference to
5332 	     an external symbol, we want
5333 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5334 	       add	$tempreg,$tempreg,$gp
5335 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5336 	       addi	$tempreg,$tempreg,<constant>
5337 
5338 	     If we have a large constant, and this is a reference to
5339 	     an external symbol, we want
5340 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
5341 	       addu	$tempreg,$tempreg,$gp
5342 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5343 	       lui	$at,<hiconstant>
5344 	       addi	$at,$at,<loconstant>
5345 	       add	$tempreg,$tempreg,$at
5346 
5347 	     If we have NewABI, and we know it's a local symbol, we want
5348 	       lw	$reg,<sym>($gp)		(BFD_RELOC_MIPS_GOT_PAGE)
5349 	       addiu	$reg,$reg,<sym>		(BFD_RELOC_MIPS_GOT_OFST)
5350 	     otherwise we have to resort to GOT_HI16/GOT_LO16.  */
5351 
5352 	  relax_start (offset_expr.X_add_symbol);
5353 
5354 	  expr1.X_add_number = offset_expr.X_add_number;
5355 	  offset_expr.X_add_number = 0;
5356 
5357 	  if (expr1.X_add_number == 0 && breg == 0
5358 	      && (call || tempreg == PIC_CALL_REG))
5359 	    {
5360 	      lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5361 	      lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5362 	    }
5363 	  macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5364 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5365 		       tempreg, tempreg, mips_gp_register);
5366 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5367 		       tempreg, lw_reloc_type, tempreg);
5368 
5369 	  if (expr1.X_add_number == 0)
5370 	    ;
5371 	  else if (expr1.X_add_number >= -0x8000
5372 		   && expr1.X_add_number < 0x8000)
5373 	    {
5374 	      macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5375 			   tempreg, tempreg, BFD_RELOC_LO16);
5376 	    }
5377 	  else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5378 	    {
5379 	      int dreg;
5380 
5381 	      /* If we are going to add in a base register, and the
5382 		 target register and the base register are the same,
5383 		 then we are using AT as a temporary register.  Since
5384 		 we want to load the constant into AT, we add our
5385 		 current AT (from the global offset table) and the
5386 		 register into the register now, and pretend we were
5387 		 not using a base register.  */
5388 	      if (breg != treg)
5389 		dreg = tempreg;
5390 	      else
5391 		{
5392 		  assert (tempreg == AT);
5393 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5394 			       treg, AT, breg);
5395 		  dreg = treg;
5396 		  add_breg_early = 1;
5397 		}
5398 
5399 	      load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5400 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5401 
5402 	      used_at = 1;
5403 	    }
5404 	  else
5405 	    as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5406 
5407 	  relax_switch ();
5408 	  offset_expr.X_add_number = expr1.X_add_number;
5409 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5410 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5411 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5412 		       tempreg, BFD_RELOC_MIPS_GOT_OFST);
5413 	  if (add_breg_early)
5414 	    {
5415 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5416 			   treg, tempreg, breg);
5417 	      breg = 0;
5418 	      tempreg = treg;
5419 	    }
5420 	  relax_end ();
5421 	}
5422       else
5423 	abort ();
5424 
5425       if (breg != 0)
5426 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5427       break;
5428 
5429     case M_J_A:
5430       /* The j instruction may not be used in PIC code, since it
5431 	 requires an absolute address.  We convert it to a b
5432 	 instruction.  */
5433       if (mips_pic == NO_PIC)
5434 	macro_build (&offset_expr, "j", "a");
5435       else
5436 	macro_build (&offset_expr, "b", "p");
5437       break;
5438 
5439       /* The jal instructions must be handled as macros because when
5440 	 generating PIC code they expand to multi-instruction
5441 	 sequences.  Normally they are simple instructions.  */
5442     case M_JAL_1:
5443       dreg = RA;
5444       /* Fall through.  */
5445     case M_JAL_2:
5446       if (mips_pic == NO_PIC)
5447 	macro_build (NULL, "jalr", "d,s", dreg, sreg);
5448       else if (mips_pic == SVR4_PIC)
5449 	{
5450 	  if (sreg != PIC_CALL_REG)
5451 	    as_warn (_("MIPS PIC call to register other than $25"));
5452 
5453 	  macro_build (NULL, "jalr", "d,s", dreg, sreg);
5454 	  if (! HAVE_NEWABI)
5455 	    {
5456 	      if (mips_cprestore_offset < 0)
5457 		as_warn (_("No .cprestore pseudo-op used in PIC code"));
5458 	      else
5459 		{
5460 		  if (! mips_frame_reg_valid)
5461 		    {
5462 		      as_warn (_("No .frame pseudo-op used in PIC code"));
5463 		      /* Quiet this warning.  */
5464 		      mips_frame_reg_valid = 1;
5465 		    }
5466 		  if (! mips_cprestore_valid)
5467 		    {
5468 		      as_warn (_("No .cprestore pseudo-op used in PIC code"));
5469 		      /* Quiet this warning.  */
5470 		      mips_cprestore_valid = 1;
5471 		    }
5472 		  expr1.X_add_number = mips_cprestore_offset;
5473   		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5474 						mips_gp_register,
5475 						mips_frame_reg,
5476 						HAVE_64BIT_ADDRESSES);
5477 		}
5478 	    }
5479 	}
5480       else
5481 	abort ();
5482 
5483       break;
5484 
5485     case M_JAL_A:
5486       if (mips_pic == NO_PIC)
5487 	macro_build (&offset_expr, "jal", "a");
5488       else if (mips_pic == SVR4_PIC)
5489 	{
5490 	  /* If this is a reference to an external symbol, and we are
5491 	     using a small GOT, we want
5492 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_CALL16)
5493 	       nop
5494 	       jalr	$ra,$25
5495 	       nop
5496 	       lw	$gp,cprestore($sp)
5497 	     The cprestore value is set using the .cprestore
5498 	     pseudo-op.  If we are using a big GOT, we want
5499 	       lui	$25,<sym>		(BFD_RELOC_MIPS_CALL_HI16)
5500 	       addu	$25,$25,$gp
5501 	       lw	$25,<sym>($25)		(BFD_RELOC_MIPS_CALL_LO16)
5502 	       nop
5503 	       jalr	$ra,$25
5504 	       nop
5505 	       lw	$gp,cprestore($sp)
5506 	     If the symbol is not external, we want
5507 	       lw	$25,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
5508 	       nop
5509 	       addiu	$25,$25,<sym>		(BFD_RELOC_LO16)
5510 	       jalr	$ra,$25
5511 	       nop
5512 	       lw $gp,cprestore($sp)
5513 
5514 	     For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5515 	     sequences above, minus nops, unless the symbol is local,
5516 	     which enables us to use GOT_PAGE/GOT_OFST (big got) or
5517 	     GOT_DISP.  */
5518 	  if (HAVE_NEWABI)
5519 	    {
5520 	      if (! mips_big_got)
5521 		{
5522 		  relax_start (offset_expr.X_add_symbol);
5523 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5524 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5525 			       mips_gp_register);
5526 		  relax_switch ();
5527 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5528 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5529 			       mips_gp_register);
5530 		  relax_end ();
5531 		}
5532 	      else
5533 		{
5534 		  relax_start (offset_expr.X_add_symbol);
5535 		  macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5536 			       BFD_RELOC_MIPS_CALL_HI16);
5537 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5538 			       PIC_CALL_REG, mips_gp_register);
5539 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5540 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5541 			       PIC_CALL_REG);
5542 		  relax_switch ();
5543 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5544 			       PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5545 			       mips_gp_register);
5546 		  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5547 			       PIC_CALL_REG, PIC_CALL_REG,
5548 			       BFD_RELOC_MIPS_GOT_OFST);
5549 		  relax_end ();
5550 		}
5551 
5552 	      macro_build_jalr (&offset_expr);
5553 	    }
5554 	  else
5555 	    {
5556 	      relax_start (offset_expr.X_add_symbol);
5557 	      if (! mips_big_got)
5558 		{
5559 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5560 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5561 			       mips_gp_register);
5562 		  load_delay_nop ();
5563 		  relax_switch ();
5564 		}
5565 	      else
5566 		{
5567 		  int gpdelay;
5568 
5569 		  gpdelay = reg_needs_delay (mips_gp_register);
5570 		  macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5571 			       BFD_RELOC_MIPS_CALL_HI16);
5572 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5573 			       PIC_CALL_REG, mips_gp_register);
5574 		  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5575 			       PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5576 			       PIC_CALL_REG);
5577 		  load_delay_nop ();
5578 		  relax_switch ();
5579 		  if (gpdelay)
5580 		    macro_build (NULL, "nop", "");
5581 		}
5582 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5583 			   PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5584 			   mips_gp_register);
5585 	      load_delay_nop ();
5586 	      macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5587 			   PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5588 	      relax_end ();
5589 	      macro_build_jalr (&offset_expr);
5590 
5591 	      if (mips_cprestore_offset < 0)
5592 		as_warn (_("No .cprestore pseudo-op used in PIC code"));
5593 	      else
5594 		{
5595 		  if (! mips_frame_reg_valid)
5596 		    {
5597 		      as_warn (_("No .frame pseudo-op used in PIC code"));
5598 		      /* Quiet this warning.  */
5599 		      mips_frame_reg_valid = 1;
5600 		    }
5601 		  if (! mips_cprestore_valid)
5602 		    {
5603 		      as_warn (_("No .cprestore pseudo-op used in PIC code"));
5604 		      /* Quiet this warning.  */
5605 		      mips_cprestore_valid = 1;
5606 		    }
5607 		  if (mips_opts.noreorder)
5608 		    macro_build (NULL, "nop", "");
5609 		  expr1.X_add_number = mips_cprestore_offset;
5610   		  macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5611 						mips_gp_register,
5612 						mips_frame_reg,
5613 						HAVE_64BIT_ADDRESSES);
5614 		}
5615 	    }
5616 	}
5617       else
5618 	abort ();
5619 
5620       break;
5621 
5622     case M_LB_AB:
5623       s = "lb";
5624       goto ld;
5625     case M_LBU_AB:
5626       s = "lbu";
5627       goto ld;
5628     case M_LH_AB:
5629       s = "lh";
5630       goto ld;
5631     case M_LHU_AB:
5632       s = "lhu";
5633       goto ld;
5634     case M_LW_AB:
5635       s = "lw";
5636       goto ld;
5637     case M_LWC0_AB:
5638       s = "lwc0";
5639       /* Itbl support may require additional care here.  */
5640       coproc = 1;
5641       goto ld;
5642     case M_LWC1_AB:
5643       s = "lwc1";
5644       /* Itbl support may require additional care here.  */
5645       coproc = 1;
5646       goto ld;
5647     case M_LWC2_AB:
5648       s = "lwc2";
5649       /* Itbl support may require additional care here.  */
5650       coproc = 1;
5651       goto ld;
5652     case M_LWC3_AB:
5653       s = "lwc3";
5654       /* Itbl support may require additional care here.  */
5655       coproc = 1;
5656       goto ld;
5657     case M_LWL_AB:
5658       s = "lwl";
5659       lr = 1;
5660       goto ld;
5661     case M_LWR_AB:
5662       s = "lwr";
5663       lr = 1;
5664       goto ld;
5665     case M_LDC1_AB:
5666       if (mips_opts.arch == CPU_R4650)
5667 	{
5668 	  as_bad (_("opcode not supported on this processor"));
5669 	  break;
5670 	}
5671       s = "ldc1";
5672       /* Itbl support may require additional care here.  */
5673       coproc = 1;
5674       goto ld;
5675     case M_LDC2_AB:
5676       s = "ldc2";
5677       /* Itbl support may require additional care here.  */
5678       coproc = 1;
5679       goto ld;
5680     case M_LDC3_AB:
5681       s = "ldc3";
5682       /* Itbl support may require additional care here.  */
5683       coproc = 1;
5684       goto ld;
5685     case M_LDL_AB:
5686       s = "ldl";
5687       lr = 1;
5688       goto ld;
5689     case M_LDR_AB:
5690       s = "ldr";
5691       lr = 1;
5692       goto ld;
5693     case M_LL_AB:
5694       s = "ll";
5695       goto ld;
5696     case M_LLD_AB:
5697       s = "lld";
5698       goto ld;
5699     case M_LWU_AB:
5700       s = "lwu";
5701     ld:
5702       if (breg == treg || coproc || lr)
5703 	{
5704 	  tempreg = AT;
5705 	  used_at = 1;
5706 	}
5707       else
5708 	{
5709 	  tempreg = treg;
5710 	}
5711       goto ld_st;
5712     case M_SB_AB:
5713       s = "sb";
5714       goto st;
5715     case M_SH_AB:
5716       s = "sh";
5717       goto st;
5718     case M_SW_AB:
5719       s = "sw";
5720       goto st;
5721     case M_SWC0_AB:
5722       s = "swc0";
5723       /* Itbl support may require additional care here.  */
5724       coproc = 1;
5725       goto st;
5726     case M_SWC1_AB:
5727       s = "swc1";
5728       /* Itbl support may require additional care here.  */
5729       coproc = 1;
5730       goto st;
5731     case M_SWC2_AB:
5732       s = "swc2";
5733       /* Itbl support may require additional care here.  */
5734       coproc = 1;
5735       goto st;
5736     case M_SWC3_AB:
5737       s = "swc3";
5738       /* Itbl support may require additional care here.  */
5739       coproc = 1;
5740       goto st;
5741     case M_SWL_AB:
5742       s = "swl";
5743       goto st;
5744     case M_SWR_AB:
5745       s = "swr";
5746       goto st;
5747     case M_SC_AB:
5748       s = "sc";
5749       goto st;
5750     case M_SCD_AB:
5751       s = "scd";
5752       goto st;
5753     case M_SDC1_AB:
5754       if (mips_opts.arch == CPU_R4650)
5755 	{
5756 	  as_bad (_("opcode not supported on this processor"));
5757 	  break;
5758 	}
5759       s = "sdc1";
5760       coproc = 1;
5761       /* Itbl support may require additional care here.  */
5762       goto st;
5763     case M_SDC2_AB:
5764       s = "sdc2";
5765       /* Itbl support may require additional care here.  */
5766       coproc = 1;
5767       goto st;
5768     case M_SDC3_AB:
5769       s = "sdc3";
5770       /* Itbl support may require additional care here.  */
5771       coproc = 1;
5772       goto st;
5773     case M_SDL_AB:
5774       s = "sdl";
5775       goto st;
5776     case M_SDR_AB:
5777       s = "sdr";
5778     st:
5779       tempreg = AT;
5780       used_at = 1;
5781     ld_st:
5782       /* Itbl support may require additional care here.  */
5783       if (mask == M_LWC1_AB
5784 	  || mask == M_SWC1_AB
5785 	  || mask == M_LDC1_AB
5786 	  || mask == M_SDC1_AB
5787 	  || mask == M_L_DAB
5788 	  || mask == M_S_DAB)
5789 	fmt = "T,o(b)";
5790       else if (coproc)
5791 	fmt = "E,o(b)";
5792       else
5793 	fmt = "t,o(b)";
5794 
5795       if (offset_expr.X_op != O_constant
5796 	  && offset_expr.X_op != O_symbol)
5797 	{
5798 	  as_bad (_("expression too complex"));
5799 	  offset_expr.X_op = O_constant;
5800 	}
5801 
5802       if (HAVE_32BIT_ADDRESSES
5803 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5804 	{
5805 	  char value [32];
5806 
5807 	  sprintf_vma (value, offset_expr.X_add_number);
5808 	  as_bad (_("Number (0x%s) larger than 32 bits"), value);
5809 	}
5810 
5811       /* A constant expression in PIC code can be handled just as it
5812 	 is in non PIC code.  */
5813       if (offset_expr.X_op == O_constant)
5814 	{
5815 	  expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5816 				& ~(bfd_vma) 0xffff);
5817 	  normalize_address_expr (&expr1);
5818 	  load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5819 	  if (breg != 0)
5820 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5821 			 tempreg, tempreg, breg);
5822 	  macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5823 	}
5824       else if (mips_pic == NO_PIC)
5825 	{
5826 	  /* If this is a reference to a GP relative symbol, and there
5827 	     is no base register, we want
5828 	       <op>	$treg,<sym>($gp)	(BFD_RELOC_GPREL16)
5829 	     Otherwise, if there is no base register, we want
5830 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
5831 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
5832 	     If we have a constant, we need two instructions anyhow,
5833 	     so we always use the latter form.
5834 
5835 	     If we have a base register, and this is a reference to a
5836 	     GP relative symbol, we want
5837 	       addu	$tempreg,$breg,$gp
5838 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_GPREL16)
5839 	     Otherwise we want
5840 	       lui	$tempreg,<sym>		(BFD_RELOC_HI16_S)
5841 	       addu	$tempreg,$tempreg,$breg
5842 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
5843 	     With a constant we always use the latter case.
5844 
5845 	     With 64bit address space and no base register and $at usable,
5846 	     we want
5847 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
5848 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
5849 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
5850 	       dsll32	$tempreg,0
5851 	       daddu	$tempreg,$at
5852 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
5853 	     If we have a base register, we want
5854 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
5855 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
5856 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
5857 	       daddu	$at,$breg
5858 	       dsll32	$tempreg,0
5859 	       daddu	$tempreg,$at
5860 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
5861 
5862 	     Without $at we can't generate the optimal path for superscalar
5863 	     processors here since this would require two temporary registers.
5864 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
5865 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
5866 	       dsll	$tempreg,16
5867 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
5868 	       dsll	$tempreg,16
5869 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
5870 	     If we have a base register, we want
5871 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHEST)
5872 	       daddiu	$tempreg,<sym>		(BFD_RELOC_MIPS_HIGHER)
5873 	       dsll	$tempreg,16
5874 	       daddiu	$tempreg,<sym>		(BFD_RELOC_HI16_S)
5875 	       dsll	$tempreg,16
5876 	       daddu	$tempreg,$tempreg,$breg
5877 	       <op>	$treg,<sym>($tempreg)	(BFD_RELOC_LO16)
5878 
5879 	     For GP relative symbols in 64bit address space we can use
5880 	     the same sequence as in 32bit address space.  */
5881 	  if (HAVE_64BIT_SYMBOLS)
5882 	    {
5883 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5884 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5885 		{
5886 		  relax_start (offset_expr.X_add_symbol);
5887 		  if (breg == 0)
5888 		    {
5889 		      macro_build (&offset_expr, s, fmt, treg,
5890 				   BFD_RELOC_GPREL16, mips_gp_register);
5891 		    }
5892 		  else
5893 		    {
5894 		      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5895 				   tempreg, breg, mips_gp_register);
5896 		      macro_build (&offset_expr, s, fmt, treg,
5897 				   BFD_RELOC_GPREL16, tempreg);
5898 		    }
5899 		  relax_switch ();
5900 		}
5901 
5902 	      if (used_at == 0 && !mips_opts.noat)
5903 		{
5904 		  macro_build (&offset_expr, "lui", "t,u", tempreg,
5905 			       BFD_RELOC_MIPS_HIGHEST);
5906 		  macro_build (&offset_expr, "lui", "t,u", AT,
5907 			       BFD_RELOC_HI16_S);
5908 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5909 			       tempreg, BFD_RELOC_MIPS_HIGHER);
5910 		  if (breg != 0)
5911 		    macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5912 		  macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5913 		  macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5914 		  macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5915 			       tempreg);
5916 		  used_at = 1;
5917 		}
5918 	      else
5919 		{
5920 		  macro_build (&offset_expr, "lui", "t,u", tempreg,
5921 			       BFD_RELOC_MIPS_HIGHEST);
5922 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5923 			       tempreg, BFD_RELOC_MIPS_HIGHER);
5924 		  macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5925 		  macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5926 			       tempreg, BFD_RELOC_HI16_S);
5927 		  macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5928 		  if (breg != 0)
5929 		    macro_build (NULL, "daddu", "d,v,t",
5930 				 tempreg, tempreg, breg);
5931 		  macro_build (&offset_expr, s, fmt, treg,
5932 			       BFD_RELOC_LO16, tempreg);
5933 		}
5934 
5935 	      if (mips_relax.sequence)
5936 		relax_end ();
5937 	      break;
5938 	    }
5939 
5940 	  if (breg == 0)
5941 	    {
5942 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5943 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5944 		{
5945 		  relax_start (offset_expr.X_add_symbol);
5946 		  macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5947 			       mips_gp_register);
5948 		  relax_switch ();
5949 		}
5950 	      macro_build_lui (&offset_expr, tempreg);
5951 	      macro_build (&offset_expr, s, fmt, treg,
5952 			   BFD_RELOC_LO16, tempreg);
5953 	      if (mips_relax.sequence)
5954 		relax_end ();
5955 	    }
5956 	  else
5957 	    {
5958 	      if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5959 		  && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5960 		{
5961 		  relax_start (offset_expr.X_add_symbol);
5962 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5963 			       tempreg, breg, mips_gp_register);
5964 		  macro_build (&offset_expr, s, fmt, treg,
5965 			       BFD_RELOC_GPREL16, tempreg);
5966 		  relax_switch ();
5967 		}
5968 	      macro_build_lui (&offset_expr, tempreg);
5969 	      macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5970 			   tempreg, tempreg, breg);
5971 	      macro_build (&offset_expr, s, fmt, treg,
5972 			   BFD_RELOC_LO16, tempreg);
5973 	      if (mips_relax.sequence)
5974 		relax_end ();
5975 	    }
5976 	}
5977       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5978 	{
5979 	  int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5980 
5981 	  /* If this is a reference to an external symbol, we want
5982 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
5983 	       nop
5984 	       <op>	$treg,0($tempreg)
5985 	     Otherwise we want
5986 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
5987 	       nop
5988 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
5989 	       <op>	$treg,0($tempreg)
5990 
5991 	     For NewABI, we want
5992 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
5993 	       <op>	$treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)
5994 
5995 	     If there is a base register, we add it to $tempreg before
5996 	     the <op>.  If there is a constant, we stick it in the
5997 	     <op> instruction.  We don't handle constants larger than
5998 	     16 bits, because we have no way to load the upper 16 bits
5999 	     (actually, we could handle them for the subset of cases
6000 	     in which we are not using $at).  */
6001 	  assert (offset_expr.X_op == O_symbol);
6002 	  if (HAVE_NEWABI)
6003 	    {
6004 	      macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6005 			   BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6006 	      if (breg != 0)
6007 		macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6008 			     tempreg, tempreg, breg);
6009 	      macro_build (&offset_expr, s, fmt, treg,
6010 			   BFD_RELOC_MIPS_GOT_OFST, tempreg);
6011 	      break;
6012 	    }
6013 	  expr1.X_add_number = offset_expr.X_add_number;
6014 	  offset_expr.X_add_number = 0;
6015 	  if (expr1.X_add_number < -0x8000
6016 	      || expr1.X_add_number >= 0x8000)
6017 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6018 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6019 		       lw_reloc_type, mips_gp_register);
6020 	  load_delay_nop ();
6021 	  relax_start (offset_expr.X_add_symbol);
6022 	  relax_switch ();
6023 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6024 		       tempreg, BFD_RELOC_LO16);
6025 	  relax_end ();
6026 	  if (breg != 0)
6027 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6028 			 tempreg, tempreg, breg);
6029 	  macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6030 	}
6031       else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6032 	{
6033 	  int gpdelay;
6034 
6035 	  /* If this is a reference to an external symbol, we want
6036 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
6037 	       addu	$tempreg,$tempreg,$gp
6038 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6039 	       <op>	$treg,0($tempreg)
6040 	     Otherwise we want
6041 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT16)
6042 	       nop
6043 	       addiu	$tempreg,$tempreg,<sym>	(BFD_RELOC_LO16)
6044 	       <op>	$treg,0($tempreg)
6045 	     If there is a base register, we add it to $tempreg before
6046 	     the <op>.  If there is a constant, we stick it in the
6047 	     <op> instruction.  We don't handle constants larger than
6048 	     16 bits, because we have no way to load the upper 16 bits
6049 	     (actually, we could handle them for the subset of cases
6050 	     in which we are not using $at).  */
6051 	  assert (offset_expr.X_op == O_symbol);
6052 	  expr1.X_add_number = offset_expr.X_add_number;
6053 	  offset_expr.X_add_number = 0;
6054 	  if (expr1.X_add_number < -0x8000
6055 	      || expr1.X_add_number >= 0x8000)
6056 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6057 	  gpdelay = reg_needs_delay (mips_gp_register);
6058 	  relax_start (offset_expr.X_add_symbol);
6059 	  macro_build (&offset_expr, "lui", "t,u", tempreg,
6060 		       BFD_RELOC_MIPS_GOT_HI16);
6061 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6062 		       mips_gp_register);
6063 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6064 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
6065 	  relax_switch ();
6066 	  if (gpdelay)
6067 	    macro_build (NULL, "nop", "");
6068 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6069 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
6070 	  load_delay_nop ();
6071 	  macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6072 		       tempreg, BFD_RELOC_LO16);
6073 	  relax_end ();
6074 
6075 	  if (breg != 0)
6076 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6077 			 tempreg, tempreg, breg);
6078 	  macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6079 	}
6080       else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6081 	{
6082 	  /* If this is a reference to an external symbol, we want
6083 	       lui	$tempreg,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
6084 	       add	$tempreg,$tempreg,$gp
6085 	       lw	$tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6086 	       <op>	$treg,<ofst>($tempreg)
6087 	     Otherwise, for local symbols, we want:
6088 	       lw	$tempreg,<sym>($gp)	(BFD_RELOC_MIPS_GOT_PAGE)
6089 	       <op>	$treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GOT_OFST)  */
6090 	  assert (offset_expr.X_op == O_symbol);
6091 	  expr1.X_add_number = offset_expr.X_add_number;
6092 	  offset_expr.X_add_number = 0;
6093 	  if (expr1.X_add_number < -0x8000
6094 	      || expr1.X_add_number >= 0x8000)
6095 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6096 	  relax_start (offset_expr.X_add_symbol);
6097 	  macro_build (&offset_expr, "lui", "t,u", tempreg,
6098 		       BFD_RELOC_MIPS_GOT_HI16);
6099 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6100 		       mips_gp_register);
6101 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6102 		       BFD_RELOC_MIPS_GOT_LO16, tempreg);
6103 	  if (breg != 0)
6104 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6105 			 tempreg, tempreg, breg);
6106 	  macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6107 
6108 	  relax_switch ();
6109 	  offset_expr.X_add_number = expr1.X_add_number;
6110 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6111 		       BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6112 	  if (breg != 0)
6113 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6114 			 tempreg, tempreg, breg);
6115 	  macro_build (&offset_expr, s, fmt, treg,
6116 		       BFD_RELOC_MIPS_GOT_OFST, tempreg);
6117 	  relax_end ();
6118 	}
6119       else
6120 	abort ();
6121 
6122       break;
6123 
6124     case M_LI:
6125     case M_LI_S:
6126       load_register (treg, &imm_expr, 0);
6127       break;
6128 
6129     case M_DLI:
6130       load_register (treg, &imm_expr, 1);
6131       break;
6132 
6133     case M_LI_SS:
6134       if (imm_expr.X_op == O_constant)
6135 	{
6136 	  used_at = 1;
6137 	  load_register (AT, &imm_expr, 0);
6138 	  macro_build (NULL, "mtc1", "t,G", AT, treg);
6139 	  break;
6140 	}
6141       else
6142 	{
6143 	  assert (offset_expr.X_op == O_symbol
6144 		  && strcmp (segment_name (S_GET_SEGMENT
6145 					   (offset_expr.X_add_symbol)),
6146 			     ".lit4") == 0
6147 		  && offset_expr.X_add_number == 0);
6148 	  macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6149 		       BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6150 	  break;
6151 	}
6152 
6153     case M_LI_D:
6154       /* Check if we have a constant in IMM_EXPR.  If the GPRs are 64 bits
6155          wide, IMM_EXPR is the entire value.  Otherwise IMM_EXPR is the high
6156          order 32 bits of the value and the low order 32 bits are either
6157          zero or in OFFSET_EXPR.  */
6158       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6159 	{
6160 	  if (HAVE_64BIT_GPRS)
6161 	    load_register (treg, &imm_expr, 1);
6162 	  else
6163 	    {
6164 	      int hreg, lreg;
6165 
6166 	      if (target_big_endian)
6167 		{
6168 		  hreg = treg;
6169 		  lreg = treg + 1;
6170 		}
6171 	      else
6172 		{
6173 		  hreg = treg + 1;
6174 		  lreg = treg;
6175 		}
6176 
6177 	      if (hreg <= 31)
6178 		load_register (hreg, &imm_expr, 0);
6179 	      if (lreg <= 31)
6180 		{
6181 		  if (offset_expr.X_op == O_absent)
6182 		    move_register (lreg, 0);
6183 		  else
6184 		    {
6185 		      assert (offset_expr.X_op == O_constant);
6186 		      load_register (lreg, &offset_expr, 0);
6187 		    }
6188 		}
6189 	    }
6190 	  break;
6191 	}
6192 
6193       /* We know that sym is in the .rdata section.  First we get the
6194 	 upper 16 bits of the address.  */
6195       if (mips_pic == NO_PIC)
6196 	{
6197 	  macro_build_lui (&offset_expr, AT);
6198 	  used_at = 1;
6199 	}
6200       else if (mips_pic == SVR4_PIC)
6201 	{
6202 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6203 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
6204 	  used_at = 1;
6205 	}
6206       else
6207 	abort ();
6208 
6209       /* Now we load the register(s).  */
6210       if (HAVE_64BIT_GPRS)
6211 	{
6212 	  used_at = 1;
6213 	  macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6214 	}
6215       else
6216 	{
6217 	  used_at = 1;
6218 	  macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6219 	  if (treg != RA)
6220 	    {
6221 	      /* FIXME: How in the world do we deal with the possible
6222 		 overflow here?  */
6223 	      offset_expr.X_add_number += 4;
6224 	      macro_build (&offset_expr, "lw", "t,o(b)",
6225 			   treg + 1, BFD_RELOC_LO16, AT);
6226 	    }
6227 	}
6228       break;
6229 
6230     case M_LI_DD:
6231       /* Check if we have a constant in IMM_EXPR.  If the FPRs are 64 bits
6232          wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6233          bits wide as well.  Otherwise IMM_EXPR is the high order 32 bits of
6234          the value and the low order 32 bits are either zero or in
6235          OFFSET_EXPR.  */
6236       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6237 	{
6238 	  used_at = 1;
6239 	  load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6240 	  if (HAVE_64BIT_FPRS)
6241 	    {
6242 	      assert (HAVE_64BIT_GPRS);
6243 	      macro_build (NULL, "dmtc1", "t,S", AT, treg);
6244 	    }
6245 	  else
6246 	    {
6247 	      macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6248 	      if (offset_expr.X_op == O_absent)
6249 		macro_build (NULL, "mtc1", "t,G", 0, treg);
6250 	      else
6251 		{
6252 		  assert (offset_expr.X_op == O_constant);
6253 		  load_register (AT, &offset_expr, 0);
6254 		  macro_build (NULL, "mtc1", "t,G", AT, treg);
6255 		}
6256 	    }
6257 	  break;
6258 	}
6259 
6260       assert (offset_expr.X_op == O_symbol
6261 	      && offset_expr.X_add_number == 0);
6262       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6263       if (strcmp (s, ".lit8") == 0)
6264 	{
6265 	  if (mips_opts.isa != ISA_MIPS1)
6266 	    {
6267 	      macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6268 			   BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6269 	      break;
6270 	    }
6271 	  breg = mips_gp_register;
6272 	  r = BFD_RELOC_MIPS_LITERAL;
6273 	  goto dob;
6274 	}
6275       else
6276 	{
6277 	  assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6278 	  used_at = 1;
6279 	  if (mips_pic == SVR4_PIC)
6280 	    macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6281 			 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6282 	  else
6283 	    {
6284 	      /* FIXME: This won't work for a 64 bit address.  */
6285 	      macro_build_lui (&offset_expr, AT);
6286 	    }
6287 
6288 	  if (mips_opts.isa != ISA_MIPS1)
6289 	    {
6290 	      macro_build (&offset_expr, "ldc1", "T,o(b)",
6291 			   treg, BFD_RELOC_LO16, AT);
6292 	      break;
6293 	    }
6294 	  breg = AT;
6295 	  r = BFD_RELOC_LO16;
6296 	  goto dob;
6297 	}
6298 
6299     case M_L_DOB:
6300       if (mips_opts.arch == CPU_R4650)
6301 	{
6302 	  as_bad (_("opcode not supported on this processor"));
6303 	  break;
6304 	}
6305       /* Even on a big endian machine $fn comes before $fn+1.  We have
6306 	 to adjust when loading from memory.  */
6307       r = BFD_RELOC_LO16;
6308     dob:
6309       assert (mips_opts.isa == ISA_MIPS1);
6310       macro_build (&offset_expr, "lwc1", "T,o(b)",
6311 		   target_big_endian ? treg + 1 : treg, r, breg);
6312       /* FIXME: A possible overflow which I don't know how to deal
6313 	 with.  */
6314       offset_expr.X_add_number += 4;
6315       macro_build (&offset_expr, "lwc1", "T,o(b)",
6316 		   target_big_endian ? treg : treg + 1, r, breg);
6317       break;
6318 
6319     case M_L_DAB:
6320       /*
6321        * The MIPS assembler seems to check for X_add_number not
6322        * being double aligned and generating:
6323        *	lui	at,%hi(foo+1)
6324        *	addu	at,at,v1
6325        *	addiu	at,at,%lo(foo+1)
6326        *	lwc1	f2,0(at)
6327        *	lwc1	f3,4(at)
6328        * But, the resulting address is the same after relocation so why
6329        * generate the extra instruction?
6330        */
6331       if (mips_opts.arch == CPU_R4650)
6332 	{
6333 	  as_bad (_("opcode not supported on this processor"));
6334 	  break;
6335 	}
6336       /* Itbl support may require additional care here.  */
6337       coproc = 1;
6338       if (mips_opts.isa != ISA_MIPS1)
6339 	{
6340 	  s = "ldc1";
6341 	  goto ld;
6342 	}
6343 
6344       s = "lwc1";
6345       fmt = "T,o(b)";
6346       goto ldd_std;
6347 
6348     case M_S_DAB:
6349       if (mips_opts.arch == CPU_R4650)
6350 	{
6351 	  as_bad (_("opcode not supported on this processor"));
6352 	  break;
6353 	}
6354 
6355       if (mips_opts.isa != ISA_MIPS1)
6356 	{
6357 	  s = "sdc1";
6358 	  goto st;
6359 	}
6360 
6361       s = "swc1";
6362       fmt = "T,o(b)";
6363       /* Itbl support may require additional care here.  */
6364       coproc = 1;
6365       goto ldd_std;
6366 
6367     case M_LD_AB:
6368       if (HAVE_64BIT_GPRS)
6369 	{
6370 	  s = "ld";
6371 	  goto ld;
6372 	}
6373 
6374       s = "lw";
6375       fmt = "t,o(b)";
6376       goto ldd_std;
6377 
6378     case M_SD_AB:
6379       if (HAVE_64BIT_GPRS)
6380 	{
6381 	  s = "sd";
6382 	  goto st;
6383 	}
6384 
6385       s = "sw";
6386       fmt = "t,o(b)";
6387 
6388     ldd_std:
6389       if (offset_expr.X_op != O_symbol
6390 	  && offset_expr.X_op != O_constant)
6391 	{
6392 	  as_bad (_("expression too complex"));
6393 	  offset_expr.X_op = O_constant;
6394 	}
6395 
6396       if (HAVE_32BIT_ADDRESSES
6397 	  && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6398 	{
6399 	  char value [32];
6400 
6401 	  sprintf_vma (value, offset_expr.X_add_number);
6402 	  as_bad (_("Number (0x%s) larger than 32 bits"), value);
6403 	}
6404 
6405       /* Even on a big endian machine $fn comes before $fn+1.  We have
6406 	 to adjust when loading from memory.  We set coproc if we must
6407 	 load $fn+1 first.  */
6408       /* Itbl support may require additional care here.  */
6409       if (! target_big_endian)
6410 	coproc = 0;
6411 
6412       if (mips_pic == NO_PIC
6413 	  || offset_expr.X_op == O_constant)
6414 	{
6415 	  /* If this is a reference to a GP relative symbol, we want
6416 	       <op>	$treg,<sym>($gp)	(BFD_RELOC_GPREL16)
6417 	       <op>	$treg+1,<sym>+4($gp)	(BFD_RELOC_GPREL16)
6418 	     If we have a base register, we use this
6419 	       addu	$at,$breg,$gp
6420 	       <op>	$treg,<sym>($at)	(BFD_RELOC_GPREL16)
6421 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_GPREL16)
6422 	     If this is not a GP relative symbol, we want
6423 	       lui	$at,<sym>		(BFD_RELOC_HI16_S)
6424 	       <op>	$treg,<sym>($at)	(BFD_RELOC_LO16)
6425 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_LO16)
6426 	     If there is a base register, we add it to $at after the
6427 	     lui instruction.  If there is a constant, we always use
6428 	     the last case.  */
6429 	  if (offset_expr.X_op == O_symbol
6430 	      && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6431 	      && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6432 	    {
6433 	      relax_start (offset_expr.X_add_symbol);
6434 	      if (breg == 0)
6435 		{
6436 		  tempreg = mips_gp_register;
6437 		}
6438 	      else
6439 		{
6440 		  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6441 			       AT, breg, mips_gp_register);
6442 		  tempreg = AT;
6443 		  used_at = 1;
6444 		}
6445 
6446 	      /* Itbl support may require additional care here.  */
6447 	      macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6448 			   BFD_RELOC_GPREL16, tempreg);
6449 	      offset_expr.X_add_number += 4;
6450 
6451 	      /* Set mips_optimize to 2 to avoid inserting an
6452                  undesired nop.  */
6453 	      hold_mips_optimize = mips_optimize;
6454 	      mips_optimize = 2;
6455 	      /* Itbl support may require additional care here.  */
6456 	      macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6457 			   BFD_RELOC_GPREL16, tempreg);
6458 	      mips_optimize = hold_mips_optimize;
6459 
6460 	      relax_switch ();
6461 
6462 	      /* We just generated two relocs.  When tc_gen_reloc
6463 		 handles this case, it will skip the first reloc and
6464 		 handle the second.  The second reloc already has an
6465 		 extra addend of 4, which we added above.  We must
6466 		 subtract it out, and then subtract another 4 to make
6467 		 the first reloc come out right.  The second reloc
6468 		 will come out right because we are going to add 4 to
6469 		 offset_expr when we build its instruction below.
6470 
6471 		 If we have a symbol, then we don't want to include
6472 		 the offset, because it will wind up being included
6473 		 when we generate the reloc.  */
6474 
6475 	      if (offset_expr.X_op == O_constant)
6476 		offset_expr.X_add_number -= 8;
6477 	      else
6478 		{
6479 		  offset_expr.X_add_number = -4;
6480 		  offset_expr.X_op = O_constant;
6481 		}
6482 	    }
6483 	  used_at = 1;
6484 	  macro_build_lui (&offset_expr, AT);
6485 	  if (breg != 0)
6486 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6487 	  /* Itbl support may require additional care here.  */
6488 	  macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6489 		       BFD_RELOC_LO16, AT);
6490 	  /* FIXME: How do we handle overflow here?  */
6491 	  offset_expr.X_add_number += 4;
6492 	  /* Itbl support may require additional care here.  */
6493 	  macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6494 		       BFD_RELOC_LO16, AT);
6495 	  if (mips_relax.sequence)
6496 	    relax_end ();
6497 	}
6498       else if (mips_pic == SVR4_PIC && ! mips_big_got)
6499 	{
6500 	  /* If this is a reference to an external symbol, we want
6501 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
6502 	       nop
6503 	       <op>	$treg,0($at)
6504 	       <op>	$treg+1,4($at)
6505 	     Otherwise we want
6506 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
6507 	       nop
6508 	       <op>	$treg,<sym>($at)	(BFD_RELOC_LO16)
6509 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_LO16)
6510 	     If there is a base register we add it to $at before the
6511 	     lwc1 instructions.  If there is a constant we include it
6512 	     in the lwc1 instructions.  */
6513 	  used_at = 1;
6514 	  expr1.X_add_number = offset_expr.X_add_number;
6515 	  if (expr1.X_add_number < -0x8000
6516 	      || expr1.X_add_number >= 0x8000 - 4)
6517 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6518 	  load_got_offset (AT, &offset_expr);
6519 	  load_delay_nop ();
6520 	  if (breg != 0)
6521 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6522 
6523 	  /* Set mips_optimize to 2 to avoid inserting an undesired
6524              nop.  */
6525 	  hold_mips_optimize = mips_optimize;
6526 	  mips_optimize = 2;
6527 
6528 	  /* Itbl support may require additional care here.  */
6529 	  relax_start (offset_expr.X_add_symbol);
6530 	  macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6531 		       BFD_RELOC_LO16, AT);
6532 	  expr1.X_add_number += 4;
6533 	  macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6534 		       BFD_RELOC_LO16, AT);
6535 	  relax_switch ();
6536 	  macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6537 		       BFD_RELOC_LO16, AT);
6538 	  offset_expr.X_add_number += 4;
6539 	  macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6540 		       BFD_RELOC_LO16, AT);
6541 	  relax_end ();
6542 
6543 	  mips_optimize = hold_mips_optimize;
6544 	}
6545       else if (mips_pic == SVR4_PIC)
6546 	{
6547 	  int gpdelay;
6548 
6549 	  /* If this is a reference to an external symbol, we want
6550 	       lui	$at,<sym>		(BFD_RELOC_MIPS_GOT_HI16)
6551 	       addu	$at,$at,$gp
6552 	       lw	$at,<sym>($at)		(BFD_RELOC_MIPS_GOT_LO16)
6553 	       nop
6554 	       <op>	$treg,0($at)
6555 	       <op>	$treg+1,4($at)
6556 	     Otherwise we want
6557 	       lw	$at,<sym>($gp)		(BFD_RELOC_MIPS_GOT16)
6558 	       nop
6559 	       <op>	$treg,<sym>($at)	(BFD_RELOC_LO16)
6560 	       <op>	$treg+1,<sym>+4($at)	(BFD_RELOC_LO16)
6561 	     If there is a base register we add it to $at before the
6562 	     lwc1 instructions.  If there is a constant we include it
6563 	     in the lwc1 instructions.  */
6564 	  used_at = 1;
6565 	  expr1.X_add_number = offset_expr.X_add_number;
6566 	  offset_expr.X_add_number = 0;
6567 	  if (expr1.X_add_number < -0x8000
6568 	      || expr1.X_add_number >= 0x8000 - 4)
6569 	    as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6570 	  gpdelay = reg_needs_delay (mips_gp_register);
6571 	  relax_start (offset_expr.X_add_symbol);
6572 	  macro_build (&offset_expr, "lui", "t,u",
6573 		       AT, BFD_RELOC_MIPS_GOT_HI16);
6574 	  macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6575 		       AT, AT, mips_gp_register);
6576 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6577 		       AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6578 	  load_delay_nop ();
6579 	  if (breg != 0)
6580 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6581 	  /* Itbl support may require additional care here.  */
6582 	  macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6583 		       BFD_RELOC_LO16, AT);
6584 	  expr1.X_add_number += 4;
6585 
6586 	  /* Set mips_optimize to 2 to avoid inserting an undesired
6587              nop.  */
6588 	  hold_mips_optimize = mips_optimize;
6589 	  mips_optimize = 2;
6590 	  /* Itbl support may require additional care here.  */
6591 	  macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6592 		       BFD_RELOC_LO16, AT);
6593 	  mips_optimize = hold_mips_optimize;
6594 	  expr1.X_add_number -= 4;
6595 
6596 	  relax_switch ();
6597 	  offset_expr.X_add_number = expr1.X_add_number;
6598 	  if (gpdelay)
6599 	    macro_build (NULL, "nop", "");
6600 	  macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6601 		       BFD_RELOC_MIPS_GOT16, mips_gp_register);
6602 	  load_delay_nop ();
6603 	  if (breg != 0)
6604 	    macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6605 	  /* Itbl support may require additional care here.  */
6606 	  macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6607 		       BFD_RELOC_LO16, AT);
6608 	  offset_expr.X_add_number += 4;
6609 
6610 	  /* Set mips_optimize to 2 to avoid inserting an undesired
6611              nop.  */
6612 	  hold_mips_optimize = mips_optimize;
6613 	  mips_optimize = 2;
6614 	  /* Itbl support may require additional care here.  */
6615 	  macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6616 		       BFD_RELOC_LO16, AT);
6617 	  mips_optimize = hold_mips_optimize;
6618 	  relax_end ();
6619 	}
6620       else
6621 	abort ();
6622 
6623       break;
6624 
6625     case M_LD_OB:
6626       s = "lw";
6627       goto sd_ob;
6628     case M_SD_OB:
6629       s = "sw";
6630     sd_ob:
6631       assert (HAVE_32BIT_ADDRESSES);
6632       macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6633       offset_expr.X_add_number += 4;
6634       macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6635       break;
6636 
6637    /* New code added to support COPZ instructions.
6638       This code builds table entries out of the macros in mip_opcodes.
6639       R4000 uses interlocks to handle coproc delays.
6640       Other chips (like the R3000) require nops to be inserted for delays.
6641 
6642       FIXME: Currently, we require that the user handle delays.
6643       In order to fill delay slots for non-interlocked chips,
6644       we must have a way to specify delays based on the coprocessor.
6645       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6646       What are the side-effects of the cop instruction?
6647       What cache support might we have and what are its effects?
6648       Both coprocessor & memory require delays. how long???
6649       What registers are read/set/modified?
6650 
6651       If an itbl is provided to interpret cop instructions,
6652       this knowledge can be encoded in the itbl spec.  */
6653 
6654     case M_COP0:
6655       s = "c0";
6656       goto copz;
6657     case M_COP1:
6658       s = "c1";
6659       goto copz;
6660     case M_COP2:
6661       s = "c2";
6662       goto copz;
6663     case M_COP3:
6664       s = "c3";
6665     copz:
6666       /* For now we just do C (same as Cz).  The parameter will be
6667          stored in insn_opcode by mips_ip.  */
6668       macro_build (NULL, s, "C", ip->insn_opcode);
6669       break;
6670 
6671     case M_MOVE:
6672       move_register (dreg, sreg);
6673       break;
6674 
6675 #ifdef LOSING_COMPILER
6676     default:
6677       /* Try and see if this is a new itbl instruction.
6678          This code builds table entries out of the macros in mip_opcodes.
6679          FIXME: For now we just assemble the expression and pass it's
6680          value along as a 32-bit immediate.
6681          We may want to have the assembler assemble this value,
6682          so that we gain the assembler's knowledge of delay slots,
6683          symbols, etc.
6684          Would it be more efficient to use mask (id) here? */
6685       if (itbl_have_entries
6686 	  && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6687 	{
6688 	  s = ip->insn_mo->name;
6689 	  s2 = "cop3";
6690 	  coproc = ITBL_DECODE_PNUM (immed_expr);;
6691 	  macro_build (&immed_expr, s, "C");
6692 	  break;
6693 	}
6694       macro2 (ip);
6695       break;
6696     }
6697   if (mips_opts.noat && used_at)
6698     as_bad (_("Macro used $at after \".set noat\""));
6699 }
6700 
6701 static void
macro2(struct mips_cl_insn * ip)6702 macro2 (struct mips_cl_insn *ip)
6703 {
6704   register int treg, sreg, dreg, breg;
6705   int tempreg;
6706   int mask;
6707   int used_at;
6708   expressionS expr1;
6709   const char *s;
6710   const char *s2;
6711   const char *fmt;
6712   int likely = 0;
6713   int dbl = 0;
6714   int coproc = 0;
6715   int lr = 0;
6716   int imm = 0;
6717   int off;
6718   offsetT maxnum;
6719   bfd_reloc_code_real_type r;
6720 
6721   treg = (ip->insn_opcode >> 16) & 0x1f;
6722   dreg = (ip->insn_opcode >> 11) & 0x1f;
6723   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6724   mask = ip->insn_mo->mask;
6725 
6726   expr1.X_op = O_constant;
6727   expr1.X_op_symbol = NULL;
6728   expr1.X_add_symbol = NULL;
6729   expr1.X_add_number = 1;
6730 
6731   switch (mask)
6732     {
6733 #endif /* LOSING_COMPILER */
6734 
6735     case M_DMUL:
6736       dbl = 1;
6737     case M_MUL:
6738       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6739       macro_build (NULL, "mflo", "d", dreg);
6740       break;
6741 
6742     case M_DMUL_I:
6743       dbl = 1;
6744     case M_MUL_I:
6745       /* The MIPS assembler some times generates shifts and adds.  I'm
6746 	 not trying to be that fancy. GCC should do this for us
6747 	 anyway.  */
6748       used_at = 1;
6749       load_register (AT, &imm_expr, dbl);
6750       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6751       macro_build (NULL, "mflo", "d", dreg);
6752       break;
6753 
6754     case M_DMULO_I:
6755       dbl = 1;
6756     case M_MULO_I:
6757       imm = 1;
6758       goto do_mulo;
6759 
6760     case M_DMULO:
6761       dbl = 1;
6762     case M_MULO:
6763     do_mulo:
6764       start_noreorder ();
6765       used_at = 1;
6766       if (imm)
6767 	load_register (AT, &imm_expr, dbl);
6768       macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6769       macro_build (NULL, "mflo", "d", dreg);
6770       macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6771       macro_build (NULL, "mfhi", "d", AT);
6772       if (mips_trap)
6773 	macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6774       else
6775 	{
6776 	  expr1.X_add_number = 8;
6777 	  macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6778 	  macro_build (NULL, "nop", "", 0);
6779 	  macro_build (NULL, "break", "c", 6);
6780 	}
6781       end_noreorder ();
6782       macro_build (NULL, "mflo", "d", dreg);
6783       break;
6784 
6785     case M_DMULOU_I:
6786       dbl = 1;
6787     case M_MULOU_I:
6788       imm = 1;
6789       goto do_mulou;
6790 
6791     case M_DMULOU:
6792       dbl = 1;
6793     case M_MULOU:
6794     do_mulou:
6795       start_noreorder ();
6796       used_at = 1;
6797       if (imm)
6798 	load_register (AT, &imm_expr, dbl);
6799       macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6800 		   sreg, imm ? AT : treg);
6801       macro_build (NULL, "mfhi", "d", AT);
6802       macro_build (NULL, "mflo", "d", dreg);
6803       if (mips_trap)
6804 	macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6805       else
6806 	{
6807 	  expr1.X_add_number = 8;
6808 	  macro_build (&expr1, "beq", "s,t,p", AT, 0);
6809 	  macro_build (NULL, "nop", "", 0);
6810 	  macro_build (NULL, "break", "c", 6);
6811 	}
6812       end_noreorder ();
6813       break;
6814 
6815     case M_DROL:
6816       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6817 	{
6818 	  if (dreg == sreg)
6819 	    {
6820 	      tempreg = AT;
6821 	      used_at = 1;
6822 	    }
6823 	  else
6824 	    {
6825 	      tempreg = dreg;
6826 	    }
6827 	  macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6828 	  macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6829 	  break;
6830 	}
6831       used_at = 1;
6832       macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6833       macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6834       macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6835       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6836       break;
6837 
6838     case M_ROL:
6839       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6840 	{
6841 	  if (dreg == sreg)
6842 	    {
6843 	      tempreg = AT;
6844 	      used_at = 1;
6845 	    }
6846 	  else
6847 	    {
6848 	      tempreg = dreg;
6849 	    }
6850 	  macro_build (NULL, "negu", "d,w", tempreg, treg);
6851 	  macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6852 	  break;
6853 	}
6854       used_at = 1;
6855       macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6856       macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6857       macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6858       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6859       break;
6860 
6861     case M_DROL_I:
6862       {
6863 	unsigned int rot;
6864 	char *l, *r;
6865 
6866 	if (imm_expr.X_op != O_constant)
6867 	  as_bad (_("Improper rotate count"));
6868 	rot = imm_expr.X_add_number & 0x3f;
6869 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6870 	  {
6871 	    rot = (64 - rot) & 0x3f;
6872 	    if (rot >= 32)
6873 	      macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6874 	    else
6875 	      macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6876 	    break;
6877 	  }
6878 	if (rot == 0)
6879 	  {
6880 	    macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6881 	    break;
6882 	  }
6883 	l = (rot < 0x20) ? "dsll" : "dsll32";
6884 	r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6885 	rot &= 0x1f;
6886 	used_at = 1;
6887 	macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6888 	macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6889 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6890       }
6891       break;
6892 
6893     case M_ROL_I:
6894       {
6895 	unsigned int rot;
6896 
6897 	if (imm_expr.X_op != O_constant)
6898 	  as_bad (_("Improper rotate count"));
6899 	rot = imm_expr.X_add_number & 0x1f;
6900 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6901 	  {
6902 	    macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6903 	    break;
6904 	  }
6905 	if (rot == 0)
6906 	  {
6907 	    macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6908 	    break;
6909 	  }
6910 	used_at = 1;
6911 	macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6912 	macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6913 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6914       }
6915       break;
6916 
6917     case M_DROR:
6918       if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6919 	{
6920 	  macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6921 	  break;
6922 	}
6923       used_at = 1;
6924       macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6925       macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6926       macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6927       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6928       break;
6929 
6930     case M_ROR:
6931       if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6932 	{
6933 	  macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
6934 	  break;
6935 	}
6936       used_at = 1;
6937       macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6938       macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6939       macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6940       macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6941       break;
6942 
6943     case M_DROR_I:
6944       {
6945 	unsigned int rot;
6946 	char *l, *r;
6947 
6948 	if (imm_expr.X_op != O_constant)
6949 	  as_bad (_("Improper rotate count"));
6950 	rot = imm_expr.X_add_number & 0x3f;
6951 	if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6952 	  {
6953 	    if (rot >= 32)
6954 	      macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6955 	    else
6956 	      macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6957 	    break;
6958 	  }
6959 	if (rot == 0)
6960 	  {
6961 	    macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6962 	    break;
6963 	  }
6964 	r = (rot < 0x20) ? "dsrl" : "dsrl32";
6965 	l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6966 	rot &= 0x1f;
6967 	used_at = 1;
6968 	macro_build (NULL, r, "d,w,<", AT, sreg, rot);
6969 	macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6970 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6971       }
6972       break;
6973 
6974     case M_ROR_I:
6975       {
6976 	unsigned int rot;
6977 
6978 	if (imm_expr.X_op != O_constant)
6979 	  as_bad (_("Improper rotate count"));
6980 	rot = imm_expr.X_add_number & 0x1f;
6981 	if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6982 	  {
6983 	    macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
6984 	    break;
6985 	  }
6986 	if (rot == 0)
6987 	  {
6988 	    macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6989 	    break;
6990 	  }
6991 	used_at = 1;
6992 	macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
6993 	macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6994 	macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6995       }
6996       break;
6997 
6998     case M_S_DOB:
6999       if (mips_opts.arch == CPU_R4650)
7000 	{
7001 	  as_bad (_("opcode not supported on this processor"));
7002 	  break;
7003 	}
7004       assert (mips_opts.isa == ISA_MIPS1);
7005       /* Even on a big endian machine $fn comes before $fn+1.  We have
7006 	 to adjust when storing to memory.  */
7007       macro_build (&offset_expr, "swc1", "T,o(b)",
7008 		   target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7009       offset_expr.X_add_number += 4;
7010       macro_build (&offset_expr, "swc1", "T,o(b)",
7011 		   target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7012       break;
7013 
7014     case M_SEQ:
7015       if (sreg == 0)
7016 	macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7017       else if (treg == 0)
7018 	macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7019       else
7020 	{
7021 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7022 	  macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7023 	}
7024       break;
7025 
7026     case M_SEQ_I:
7027       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7028 	{
7029 	  macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7030 	  break;
7031 	}
7032       if (sreg == 0)
7033 	{
7034 	  as_warn (_("Instruction %s: result is always false"),
7035 		   ip->insn_mo->name);
7036 	  move_register (dreg, 0);
7037 	  break;
7038 	}
7039       if (imm_expr.X_op == O_constant
7040 	  && imm_expr.X_add_number >= 0
7041 	  && imm_expr.X_add_number < 0x10000)
7042 	{
7043 	  macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7044 	}
7045       else if (imm_expr.X_op == O_constant
7046 	       && imm_expr.X_add_number > -0x8000
7047 	       && imm_expr.X_add_number < 0)
7048 	{
7049 	  imm_expr.X_add_number = -imm_expr.X_add_number;
7050 	  macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7051 		       "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7052 	}
7053       else
7054 	{
7055 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7056 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7057 	  used_at = 1;
7058 	}
7059       macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7060       break;
7061 
7062     case M_SGE:		/* sreg >= treg <==> not (sreg < treg) */
7063       s = "slt";
7064       goto sge;
7065     case M_SGEU:
7066       s = "sltu";
7067     sge:
7068       macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7069       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7070       break;
7071 
7072     case M_SGE_I:		/* sreg >= I <==> not (sreg < I) */
7073     case M_SGEU_I:
7074       if (imm_expr.X_op == O_constant
7075 	  && imm_expr.X_add_number >= -0x8000
7076 	  && imm_expr.X_add_number < 0x8000)
7077 	{
7078 	  macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7079 		       dreg, sreg, BFD_RELOC_LO16);
7080 	}
7081       else
7082 	{
7083 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7084 	  macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7085 		       dreg, sreg, AT);
7086 	  used_at = 1;
7087 	}
7088       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7089       break;
7090 
7091     case M_SGT:		/* sreg > treg  <==>  treg < sreg */
7092       s = "slt";
7093       goto sgt;
7094     case M_SGTU:
7095       s = "sltu";
7096     sgt:
7097       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7098       break;
7099 
7100     case M_SGT_I:		/* sreg > I  <==>  I < sreg */
7101       s = "slt";
7102       goto sgti;
7103     case M_SGTU_I:
7104       s = "sltu";
7105     sgti:
7106       used_at = 1;
7107       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7108       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7109       break;
7110 
7111     case M_SLE:	/* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
7112       s = "slt";
7113       goto sle;
7114     case M_SLEU:
7115       s = "sltu";
7116     sle:
7117       macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7118       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7119       break;
7120 
7121     case M_SLE_I:	/* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7122       s = "slt";
7123       goto slei;
7124     case M_SLEU_I:
7125       s = "sltu";
7126     slei:
7127       used_at = 1;
7128       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7129       macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7130       macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7131       break;
7132 
7133     case M_SLT_I:
7134       if (imm_expr.X_op == O_constant
7135 	  && imm_expr.X_add_number >= -0x8000
7136 	  && imm_expr.X_add_number < 0x8000)
7137 	{
7138 	  macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7139 	  break;
7140 	}
7141       used_at = 1;
7142       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7143       macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7144       break;
7145 
7146     case M_SLTU_I:
7147       if (imm_expr.X_op == O_constant
7148 	  && imm_expr.X_add_number >= -0x8000
7149 	  && imm_expr.X_add_number < 0x8000)
7150 	{
7151 	  macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7152 		       BFD_RELOC_LO16);
7153 	  break;
7154 	}
7155       used_at = 1;
7156       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7157       macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7158       break;
7159 
7160     case M_SNE:
7161       if (sreg == 0)
7162 	macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7163       else if (treg == 0)
7164 	macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7165       else
7166 	{
7167 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7168 	  macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7169 	}
7170       break;
7171 
7172     case M_SNE_I:
7173       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7174 	{
7175 	  macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7176 	  break;
7177 	}
7178       if (sreg == 0)
7179 	{
7180 	  as_warn (_("Instruction %s: result is always true"),
7181 		   ip->insn_mo->name);
7182 	  macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7183 		       dreg, 0, BFD_RELOC_LO16);
7184 	  break;
7185 	}
7186       if (imm_expr.X_op == O_constant
7187 	  && imm_expr.X_add_number >= 0
7188 	  && imm_expr.X_add_number < 0x10000)
7189 	{
7190 	  macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7191 	}
7192       else if (imm_expr.X_op == O_constant
7193 	       && imm_expr.X_add_number > -0x8000
7194 	       && imm_expr.X_add_number < 0)
7195 	{
7196 	  imm_expr.X_add_number = -imm_expr.X_add_number;
7197 	  macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7198 		       "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7199 	}
7200       else
7201 	{
7202 	  load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7203 	  macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7204 	  used_at = 1;
7205 	}
7206       macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7207       break;
7208 
7209     case M_DSUB_I:
7210       dbl = 1;
7211     case M_SUB_I:
7212       if (imm_expr.X_op == O_constant
7213 	  && imm_expr.X_add_number > -0x8000
7214 	  && imm_expr.X_add_number <= 0x8000)
7215 	{
7216 	  imm_expr.X_add_number = -imm_expr.X_add_number;
7217 	  macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7218 		       dreg, sreg, BFD_RELOC_LO16);
7219 	  break;
7220 	}
7221       used_at = 1;
7222       load_register (AT, &imm_expr, dbl);
7223       macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7224       break;
7225 
7226     case M_DSUBU_I:
7227       dbl = 1;
7228     case M_SUBU_I:
7229       if (imm_expr.X_op == O_constant
7230 	  && imm_expr.X_add_number > -0x8000
7231 	  && imm_expr.X_add_number <= 0x8000)
7232 	{
7233 	  imm_expr.X_add_number = -imm_expr.X_add_number;
7234 	  macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7235 		       dreg, sreg, BFD_RELOC_LO16);
7236 	  break;
7237 	}
7238       used_at = 1;
7239       load_register (AT, &imm_expr, dbl);
7240       macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7241       break;
7242 
7243     case M_TEQ_I:
7244       s = "teq";
7245       goto trap;
7246     case M_TGE_I:
7247       s = "tge";
7248       goto trap;
7249     case M_TGEU_I:
7250       s = "tgeu";
7251       goto trap;
7252     case M_TLT_I:
7253       s = "tlt";
7254       goto trap;
7255     case M_TLTU_I:
7256       s = "tltu";
7257       goto trap;
7258     case M_TNE_I:
7259       s = "tne";
7260     trap:
7261       used_at = 1;
7262       load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7263       macro_build (NULL, s, "s,t", sreg, AT);
7264       break;
7265 
7266     case M_TRUNCWS:
7267     case M_TRUNCWD:
7268       assert (mips_opts.isa == ISA_MIPS1);
7269       used_at = 1;
7270       sreg = (ip->insn_opcode >> 11) & 0x1f;	/* floating reg */
7271       dreg = (ip->insn_opcode >> 06) & 0x1f;	/* floating reg */
7272 
7273       /*
7274        * Is the double cfc1 instruction a bug in the mips assembler;
7275        * or is there a reason for it?
7276        */
7277       start_noreorder ();
7278       macro_build (NULL, "cfc1", "t,G", treg, RA);
7279       macro_build (NULL, "cfc1", "t,G", treg, RA);
7280       macro_build (NULL, "nop", "");
7281       expr1.X_add_number = 3;
7282       macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7283       expr1.X_add_number = 2;
7284       macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7285       macro_build (NULL, "ctc1", "t,G", AT, RA);
7286       macro_build (NULL, "nop", "");
7287       macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7288 		   dreg, sreg);
7289       macro_build (NULL, "ctc1", "t,G", treg, RA);
7290       macro_build (NULL, "nop", "");
7291       end_noreorder ();
7292       break;
7293 
7294     case M_ULH:
7295       s = "lb";
7296       goto ulh;
7297     case M_ULHU:
7298       s = "lbu";
7299     ulh:
7300       used_at = 1;
7301       if (offset_expr.X_add_number >= 0x7fff)
7302 	as_bad (_("operand overflow"));
7303       if (! target_big_endian)
7304 	++offset_expr.X_add_number;
7305       macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7306       if (! target_big_endian)
7307 	--offset_expr.X_add_number;
7308       else
7309 	++offset_expr.X_add_number;
7310       macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7311       macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7312       macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7313       break;
7314 
7315     case M_ULD:
7316       s = "ldl";
7317       s2 = "ldr";
7318       off = 7;
7319       goto ulw;
7320     case M_ULW:
7321       s = "lwl";
7322       s2 = "lwr";
7323       off = 3;
7324     ulw:
7325       if (offset_expr.X_add_number >= 0x8000 - off)
7326 	as_bad (_("operand overflow"));
7327       if (treg != breg)
7328 	tempreg = treg;
7329       else
7330 	{
7331 	  used_at = 1;
7332 	  tempreg = AT;
7333 	}
7334       if (! target_big_endian)
7335 	offset_expr.X_add_number += off;
7336       macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7337       if (! target_big_endian)
7338 	offset_expr.X_add_number -= off;
7339       else
7340 	offset_expr.X_add_number += off;
7341       macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7342 
7343       /* If necessary, move the result in tempreg the final destination.  */
7344       if (treg == tempreg)
7345         break;
7346       /* Protect second load's delay slot.  */
7347       load_delay_nop ();
7348       move_register (treg, tempreg);
7349       break;
7350 
7351     case M_ULD_A:
7352       s = "ldl";
7353       s2 = "ldr";
7354       off = 7;
7355       goto ulwa;
7356     case M_ULW_A:
7357       s = "lwl";
7358       s2 = "lwr";
7359       off = 3;
7360     ulwa:
7361       used_at = 1;
7362       load_address (AT, &offset_expr, &used_at);
7363       if (breg != 0)
7364 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7365       if (! target_big_endian)
7366 	expr1.X_add_number = off;
7367       else
7368 	expr1.X_add_number = 0;
7369       macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7370       if (! target_big_endian)
7371 	expr1.X_add_number = 0;
7372       else
7373 	expr1.X_add_number = off;
7374       macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7375       break;
7376 
7377     case M_ULH_A:
7378     case M_ULHU_A:
7379       used_at = 1;
7380       load_address (AT, &offset_expr, &used_at);
7381       if (breg != 0)
7382 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7383       if (target_big_endian)
7384 	expr1.X_add_number = 0;
7385       macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7386 		   treg, BFD_RELOC_LO16, AT);
7387       if (target_big_endian)
7388 	expr1.X_add_number = 1;
7389       else
7390 	expr1.X_add_number = 0;
7391       macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7392       macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7393       macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7394       break;
7395 
7396     case M_USH:
7397       used_at = 1;
7398       if (offset_expr.X_add_number >= 0x7fff)
7399 	as_bad (_("operand overflow"));
7400       if (target_big_endian)
7401 	++offset_expr.X_add_number;
7402       macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7403       macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7404       if (target_big_endian)
7405 	--offset_expr.X_add_number;
7406       else
7407 	++offset_expr.X_add_number;
7408       macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7409       break;
7410 
7411     case M_USD:
7412       s = "sdl";
7413       s2 = "sdr";
7414       off = 7;
7415       goto usw;
7416     case M_USW:
7417       s = "swl";
7418       s2 = "swr";
7419       off = 3;
7420     usw:
7421       if (offset_expr.X_add_number >= 0x8000 - off)
7422 	as_bad (_("operand overflow"));
7423       if (! target_big_endian)
7424 	offset_expr.X_add_number += off;
7425       macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7426       if (! target_big_endian)
7427 	offset_expr.X_add_number -= off;
7428       else
7429 	offset_expr.X_add_number += off;
7430       macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7431       break;
7432 
7433     case M_USD_A:
7434       s = "sdl";
7435       s2 = "sdr";
7436       off = 7;
7437       goto uswa;
7438     case M_USW_A:
7439       s = "swl";
7440       s2 = "swr";
7441       off = 3;
7442     uswa:
7443       used_at = 1;
7444       load_address (AT, &offset_expr, &used_at);
7445       if (breg != 0)
7446 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7447       if (! target_big_endian)
7448 	expr1.X_add_number = off;
7449       else
7450 	expr1.X_add_number = 0;
7451       macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7452       if (! target_big_endian)
7453 	expr1.X_add_number = 0;
7454       else
7455 	expr1.X_add_number = off;
7456       macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7457       break;
7458 
7459     case M_USH_A:
7460       used_at = 1;
7461       load_address (AT, &offset_expr, &used_at);
7462       if (breg != 0)
7463 	macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7464       if (! target_big_endian)
7465 	expr1.X_add_number = 0;
7466       macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7467       macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7468       if (! target_big_endian)
7469 	expr1.X_add_number = 1;
7470       else
7471 	expr1.X_add_number = 0;
7472       macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7473       if (! target_big_endian)
7474 	expr1.X_add_number = 0;
7475       else
7476 	expr1.X_add_number = 1;
7477       macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7478       macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7479       macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7480       break;
7481 
7482     default:
7483       /* FIXME: Check if this is one of the itbl macros, since they
7484 	 are added dynamically.  */
7485       as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7486       break;
7487     }
7488   if (mips_opts.noat && used_at)
7489     as_bad (_("Macro used $at after \".set noat\""));
7490 }
7491 
7492 /* Implement macros in mips16 mode.  */
7493 
7494 static void
mips16_macro(struct mips_cl_insn * ip)7495 mips16_macro (struct mips_cl_insn *ip)
7496 {
7497   int mask;
7498   int xreg, yreg, zreg, tmp;
7499   expressionS expr1;
7500   int dbl;
7501   const char *s, *s2, *s3;
7502 
7503   mask = ip->insn_mo->mask;
7504 
7505   xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7506   yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7507   zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7508 
7509   expr1.X_op = O_constant;
7510   expr1.X_op_symbol = NULL;
7511   expr1.X_add_symbol = NULL;
7512   expr1.X_add_number = 1;
7513 
7514   dbl = 0;
7515 
7516   switch (mask)
7517     {
7518     default:
7519       internalError ();
7520 
7521     case M_DDIV_3:
7522       dbl = 1;
7523     case M_DIV_3:
7524       s = "mflo";
7525       goto do_div3;
7526     case M_DREM_3:
7527       dbl = 1;
7528     case M_REM_3:
7529       s = "mfhi";
7530     do_div3:
7531       start_noreorder ();
7532       macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7533       expr1.X_add_number = 2;
7534       macro_build (&expr1, "bnez", "x,p", yreg);
7535       macro_build (NULL, "break", "6", 7);
7536 
7537       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7538          since that causes an overflow.  We should do that as well,
7539          but I don't see how to do the comparisons without a temporary
7540          register.  */
7541       end_noreorder ();
7542       macro_build (NULL, s, "x", zreg);
7543       break;
7544 
7545     case M_DIVU_3:
7546       s = "divu";
7547       s2 = "mflo";
7548       goto do_divu3;
7549     case M_REMU_3:
7550       s = "divu";
7551       s2 = "mfhi";
7552       goto do_divu3;
7553     case M_DDIVU_3:
7554       s = "ddivu";
7555       s2 = "mflo";
7556       goto do_divu3;
7557     case M_DREMU_3:
7558       s = "ddivu";
7559       s2 = "mfhi";
7560     do_divu3:
7561       start_noreorder ();
7562       macro_build (NULL, s, "0,x,y", xreg, yreg);
7563       expr1.X_add_number = 2;
7564       macro_build (&expr1, "bnez", "x,p", yreg);
7565       macro_build (NULL, "break", "6", 7);
7566       end_noreorder ();
7567       macro_build (NULL, s2, "x", zreg);
7568       break;
7569 
7570     case M_DMUL:
7571       dbl = 1;
7572     case M_MUL:
7573       macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7574       macro_build (NULL, "mflo", "x", zreg);
7575       break;
7576 
7577     case M_DSUBU_I:
7578       dbl = 1;
7579       goto do_subu;
7580     case M_SUBU_I:
7581     do_subu:
7582       if (imm_expr.X_op != O_constant)
7583 	as_bad (_("Unsupported large constant"));
7584       imm_expr.X_add_number = -imm_expr.X_add_number;
7585       macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7586       break;
7587 
7588     case M_SUBU_I_2:
7589       if (imm_expr.X_op != O_constant)
7590 	as_bad (_("Unsupported large constant"));
7591       imm_expr.X_add_number = -imm_expr.X_add_number;
7592       macro_build (&imm_expr, "addiu", "x,k", xreg);
7593       break;
7594 
7595     case M_DSUBU_I_2:
7596       if (imm_expr.X_op != O_constant)
7597 	as_bad (_("Unsupported large constant"));
7598       imm_expr.X_add_number = -imm_expr.X_add_number;
7599       macro_build (&imm_expr, "daddiu", "y,j", yreg);
7600       break;
7601 
7602     case M_BEQ:
7603       s = "cmp";
7604       s2 = "bteqz";
7605       goto do_branch;
7606     case M_BNE:
7607       s = "cmp";
7608       s2 = "btnez";
7609       goto do_branch;
7610     case M_BLT:
7611       s = "slt";
7612       s2 = "btnez";
7613       goto do_branch;
7614     case M_BLTU:
7615       s = "sltu";
7616       s2 = "btnez";
7617       goto do_branch;
7618     case M_BLE:
7619       s = "slt";
7620       s2 = "bteqz";
7621       goto do_reverse_branch;
7622     case M_BLEU:
7623       s = "sltu";
7624       s2 = "bteqz";
7625       goto do_reverse_branch;
7626     case M_BGE:
7627       s = "slt";
7628       s2 = "bteqz";
7629       goto do_branch;
7630     case M_BGEU:
7631       s = "sltu";
7632       s2 = "bteqz";
7633       goto do_branch;
7634     case M_BGT:
7635       s = "slt";
7636       s2 = "btnez";
7637       goto do_reverse_branch;
7638     case M_BGTU:
7639       s = "sltu";
7640       s2 = "btnez";
7641 
7642     do_reverse_branch:
7643       tmp = xreg;
7644       xreg = yreg;
7645       yreg = tmp;
7646 
7647     do_branch:
7648       macro_build (NULL, s, "x,y", xreg, yreg);
7649       macro_build (&offset_expr, s2, "p");
7650       break;
7651 
7652     case M_BEQ_I:
7653       s = "cmpi";
7654       s2 = "bteqz";
7655       s3 = "x,U";
7656       goto do_branch_i;
7657     case M_BNE_I:
7658       s = "cmpi";
7659       s2 = "btnez";
7660       s3 = "x,U";
7661       goto do_branch_i;
7662     case M_BLT_I:
7663       s = "slti";
7664       s2 = "btnez";
7665       s3 = "x,8";
7666       goto do_branch_i;
7667     case M_BLTU_I:
7668       s = "sltiu";
7669       s2 = "btnez";
7670       s3 = "x,8";
7671       goto do_branch_i;
7672     case M_BLE_I:
7673       s = "slti";
7674       s2 = "btnez";
7675       s3 = "x,8";
7676       goto do_addone_branch_i;
7677     case M_BLEU_I:
7678       s = "sltiu";
7679       s2 = "btnez";
7680       s3 = "x,8";
7681       goto do_addone_branch_i;
7682     case M_BGE_I:
7683       s = "slti";
7684       s2 = "bteqz";
7685       s3 = "x,8";
7686       goto do_branch_i;
7687     case M_BGEU_I:
7688       s = "sltiu";
7689       s2 = "bteqz";
7690       s3 = "x,8";
7691       goto do_branch_i;
7692     case M_BGT_I:
7693       s = "slti";
7694       s2 = "bteqz";
7695       s3 = "x,8";
7696       goto do_addone_branch_i;
7697     case M_BGTU_I:
7698       s = "sltiu";
7699       s2 = "bteqz";
7700       s3 = "x,8";
7701 
7702     do_addone_branch_i:
7703       if (imm_expr.X_op != O_constant)
7704 	as_bad (_("Unsupported large constant"));
7705       ++imm_expr.X_add_number;
7706 
7707     do_branch_i:
7708       macro_build (&imm_expr, s, s3, xreg);
7709       macro_build (&offset_expr, s2, "p");
7710       break;
7711 
7712     case M_ABS:
7713       expr1.X_add_number = 0;
7714       macro_build (&expr1, "slti", "x,8", yreg);
7715       if (xreg != yreg)
7716 	move_register (xreg, yreg);
7717       expr1.X_add_number = 2;
7718       macro_build (&expr1, "bteqz", "p");
7719       macro_build (NULL, "neg", "x,w", xreg, xreg);
7720     }
7721 }
7722 
7723 /* For consistency checking, verify that all bits are specified either
7724    by the match/mask part of the instruction definition, or by the
7725    operand list.  */
7726 static int
validate_mips_insn(const struct mips_opcode * opc)7727 validate_mips_insn (const struct mips_opcode *opc)
7728 {
7729   const char *p = opc->args;
7730   char c;
7731   unsigned long used_bits = opc->mask;
7732 
7733   if ((used_bits & opc->match) != opc->match)
7734     {
7735       as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7736 	      opc->name, opc->args);
7737       return 0;
7738     }
7739 #define USE_BITS(mask,shift)	(used_bits |= ((mask) << (shift)))
7740   while (*p)
7741     switch (c = *p++)
7742       {
7743       case ',': break;
7744       case '(': break;
7745       case ')': break;
7746       case '+':
7747     	switch (c = *p++)
7748 	  {
7749 	  case 'A': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
7750 	  case 'B': USE_BITS (OP_MASK_INSMSB,	OP_SH_INSMSB);	break;
7751 	  case 'C': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
7752 	  case 'D': USE_BITS (OP_MASK_RD,	OP_SH_RD);
7753 		    USE_BITS (OP_MASK_SEL,	OP_SH_SEL);	break;
7754 	  case 'E': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
7755 	  case 'F': USE_BITS (OP_MASK_INSMSB,	OP_SH_INSMSB);	break;
7756 	  case 'G': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
7757 	  case 'H': USE_BITS (OP_MASK_EXTMSBD,	OP_SH_EXTMSBD);	break;
7758 	  case 'I': break;
7759 	  default:
7760 	    as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7761 		    c, opc->name, opc->args);
7762 	    return 0;
7763 	  }
7764 	break;
7765       case '<': USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
7766       case '>':	USE_BITS (OP_MASK_SHAMT,	OP_SH_SHAMT);	break;
7767       case 'A': break;
7768       case 'B': USE_BITS (OP_MASK_CODE20,       OP_SH_CODE20);  break;
7769       case 'C':	USE_BITS (OP_MASK_COPZ,		OP_SH_COPZ);	break;
7770       case 'D':	USE_BITS (OP_MASK_FD,		OP_SH_FD);	break;
7771       case 'E':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
7772       case 'F': break;
7773       case 'G':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
7774       case 'H': USE_BITS (OP_MASK_SEL,		OP_SH_SEL);	break;
7775       case 'I': break;
7776       case 'J': USE_BITS (OP_MASK_CODE19,       OP_SH_CODE19);  break;
7777       case 'K':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
7778       case 'L': break;
7779       case 'M':	USE_BITS (OP_MASK_CCC,		OP_SH_CCC);	break;
7780       case 'N':	USE_BITS (OP_MASK_BCC,		OP_SH_BCC);	break;
7781       case 'O':	USE_BITS (OP_MASK_ALN,		OP_SH_ALN);	break;
7782       case 'Q':	USE_BITS (OP_MASK_VSEL,		OP_SH_VSEL);
7783 		USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
7784       case 'R':	USE_BITS (OP_MASK_FR,		OP_SH_FR);	break;
7785       case 'S':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
7786       case 'T':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
7787       case 'V':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
7788       case 'W':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
7789       case 'X':	USE_BITS (OP_MASK_FD,		OP_SH_FD);	break;
7790       case 'Y':	USE_BITS (OP_MASK_FS,		OP_SH_FS);	break;
7791       case 'Z':	USE_BITS (OP_MASK_FT,		OP_SH_FT);	break;
7792       case 'a':	USE_BITS (OP_MASK_TARGET,	OP_SH_TARGET);	break;
7793       case 'b':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
7794       case 'c':	USE_BITS (OP_MASK_CODE,		OP_SH_CODE);	break;
7795       case 'd':	USE_BITS (OP_MASK_RD,		OP_SH_RD);	break;
7796       case 'f': break;
7797       case 'h':	USE_BITS (OP_MASK_PREFX,	OP_SH_PREFX);	break;
7798       case 'i':	USE_BITS (OP_MASK_IMMEDIATE,	OP_SH_IMMEDIATE); break;
7799       case 'j':	USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
7800       case 'k':	USE_BITS (OP_MASK_CACHE,	OP_SH_CACHE);	break;
7801       case 'l': break;
7802       case 'o': USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
7803       case 'p':	USE_BITS (OP_MASK_DELTA,	OP_SH_DELTA);	break;
7804       case 'q':	USE_BITS (OP_MASK_CODE2,	OP_SH_CODE2);	break;
7805       case 'r': USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
7806       case 's':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
7807       case 't':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
7808       case 'u':	USE_BITS (OP_MASK_IMMEDIATE,	OP_SH_IMMEDIATE); break;
7809       case 'v':	USE_BITS (OP_MASK_RS,		OP_SH_RS);	break;
7810       case 'w':	USE_BITS (OP_MASK_RT,		OP_SH_RT);	break;
7811       case 'x': break;
7812       case 'z': break;
7813       case 'P': USE_BITS (OP_MASK_PERFREG,	OP_SH_PERFREG);	break;
7814       case 'U': USE_BITS (OP_MASK_RD,           OP_SH_RD);
7815 	        USE_BITS (OP_MASK_RT,           OP_SH_RT);	break;
7816       case 'e': USE_BITS (OP_MASK_VECBYTE,	OP_SH_VECBYTE);	break;
7817       case '%': USE_BITS (OP_MASK_VECALIGN,	OP_SH_VECALIGN); break;
7818       case '[': break;
7819       case ']': break;
7820       default:
7821 	as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7822 		c, opc->name, opc->args);
7823 	return 0;
7824       }
7825 #undef USE_BITS
7826   if (used_bits != 0xffffffff)
7827     {
7828       as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7829 	      ~used_bits & 0xffffffff, opc->name, opc->args);
7830       return 0;
7831     }
7832   return 1;
7833 }
7834 
7835 /* This routine assembles an instruction into its binary format.  As a
7836    side effect, it sets one of the global variables imm_reloc or
7837    offset_reloc to the type of relocation to do if one of the operands
7838    is an address expression.  */
7839 
7840 static void
mips_ip(char * str,struct mips_cl_insn * ip)7841 mips_ip (char *str, struct mips_cl_insn *ip)
7842 {
7843   char *s;
7844   const char *args;
7845   char c = 0;
7846   struct mips_opcode *insn;
7847   char *argsStart;
7848   unsigned int regno;
7849   unsigned int lastregno = 0;
7850   unsigned int lastpos = 0;
7851   unsigned int limlo, limhi;
7852   char *s_reset;
7853   char save_c = 0;
7854 
7855   insn_error = NULL;
7856 
7857   /* If the instruction contains a '.', we first try to match an instruction
7858      including the '.'.  Then we try again without the '.'.  */
7859   insn = NULL;
7860   for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7861     continue;
7862 
7863   /* If we stopped on whitespace, then replace the whitespace with null for
7864      the call to hash_find.  Save the character we replaced just in case we
7865      have to re-parse the instruction.  */
7866   if (ISSPACE (*s))
7867     {
7868       save_c = *s;
7869       *s++ = '\0';
7870     }
7871 
7872   insn = (struct mips_opcode *) hash_find (op_hash, str);
7873 
7874   /* If we didn't find the instruction in the opcode table, try again, but
7875      this time with just the instruction up to, but not including the
7876      first '.'.  */
7877   if (insn == NULL)
7878     {
7879       /* Restore the character we overwrite above (if any).  */
7880       if (save_c)
7881 	*(--s) = save_c;
7882 
7883       /* Scan up to the first '.' or whitespace.  */
7884       for (s = str;
7885 	   *s != '\0' && *s != '.' && !ISSPACE (*s);
7886 	   ++s)
7887 	continue;
7888 
7889       /* If we did not find a '.', then we can quit now.  */
7890       if (*s != '.')
7891 	{
7892 	  insn_error = "unrecognized opcode";
7893 	  return;
7894 	}
7895 
7896       /* Lookup the instruction in the hash table.  */
7897       *s++ = '\0';
7898       if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7899 	{
7900 	  insn_error = "unrecognized opcode";
7901 	  return;
7902 	}
7903     }
7904 
7905   argsStart = s;
7906   for (;;)
7907     {
7908       bfd_boolean ok;
7909 
7910       assert (strcmp (insn->name, str) == 0);
7911 
7912       if (OPCODE_IS_MEMBER (insn,
7913 			    (mips_opts.isa
7914 			     | (file_ase_mips16 ? INSN_MIPS16 : 0)
7915 	      		     | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7916 			     | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7917 			    mips_opts.arch))
7918 	ok = TRUE;
7919       else
7920 	ok = FALSE;
7921 
7922       if (insn->pinfo != INSN_MACRO)
7923 	{
7924 	  if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7925 	    ok = FALSE;
7926 	}
7927 
7928       if (! ok)
7929 	{
7930 	  if (insn + 1 < &mips_opcodes[NUMOPCODES]
7931 	      && strcmp (insn->name, insn[1].name) == 0)
7932 	    {
7933 	      ++insn;
7934 	      continue;
7935 	    }
7936 	  else
7937 	    {
7938 	      if (!insn_error)
7939 		{
7940 		  static char buf[100];
7941 		  sprintf (buf,
7942 			   _("opcode not supported on this processor: %s (%s)"),
7943 			   mips_cpu_info_from_arch (mips_opts.arch)->name,
7944 			   mips_cpu_info_from_isa (mips_opts.isa)->name);
7945 		  insn_error = buf;
7946 		}
7947 	      if (save_c)
7948 		*(--s) = save_c;
7949 	      return;
7950 	    }
7951 	}
7952 
7953       create_insn (ip, insn);
7954       insn_error = NULL;
7955       for (args = insn->args;; ++args)
7956 	{
7957 	  int is_mdmx;
7958 
7959 	  s += strspn (s, " \t");
7960 	  is_mdmx = 0;
7961 	  switch (*args)
7962 	    {
7963 	    case '\0':		/* end of args */
7964 	      if (*s == '\0')
7965 		return;
7966 	      break;
7967 
7968 	    case ',':
7969 	      if (*s++ == *args)
7970 		continue;
7971 	      s--;
7972 	      switch (*++args)
7973 		{
7974 		case 'r':
7975 		case 'v':
7976 		  INSERT_OPERAND (RS, *ip, lastregno);
7977 		  continue;
7978 
7979 		case 'w':
7980 		  INSERT_OPERAND (RT, *ip, lastregno);
7981 		  continue;
7982 
7983 		case 'W':
7984 		  INSERT_OPERAND (FT, *ip, lastregno);
7985 		  continue;
7986 
7987 		case 'V':
7988 		  INSERT_OPERAND (FS, *ip, lastregno);
7989 		  continue;
7990 		}
7991 	      break;
7992 
7993 	    case '(':
7994 	      /* Handle optional base register.
7995 		 Either the base register is omitted or
7996 		 we must have a left paren.  */
7997 	      /* This is dependent on the next operand specifier
7998 		 is a base register specification.  */
7999 	      assert (args[1] == 'b' || args[1] == '5'
8000 		      || args[1] == '-' || args[1] == '4');
8001 	      if (*s == '\0')
8002 		return;
8003 
8004 	    case ')':		/* these must match exactly */
8005 	    case '[':
8006 	    case ']':
8007 	      if (*s++ == *args)
8008 		continue;
8009 	      break;
8010 
8011 	    case '+':		/* Opcode extension character.  */
8012 	      switch (*++args)
8013 		{
8014 		case 'A':		/* ins/ext position, becomes LSB.  */
8015 		  limlo = 0;
8016 		  limhi = 31;
8017 		  goto do_lsb;
8018 		case 'E':
8019 		  limlo = 32;
8020 		  limhi = 63;
8021 		  goto do_lsb;
8022 do_lsb:
8023 		  my_getExpression (&imm_expr, s);
8024 		  check_absolute_expr (ip, &imm_expr);
8025 		  if ((unsigned long) imm_expr.X_add_number < limlo
8026 		      || (unsigned long) imm_expr.X_add_number > limhi)
8027 		    {
8028 		      as_bad (_("Improper position (%lu)"),
8029 			      (unsigned long) imm_expr.X_add_number);
8030 		      imm_expr.X_add_number = limlo;
8031 		    }
8032 		  lastpos = imm_expr.X_add_number;
8033 		  INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8034 		  imm_expr.X_op = O_absent;
8035 		  s = expr_end;
8036 		  continue;
8037 
8038 		case 'B':		/* ins size, becomes MSB.  */
8039 		  limlo = 1;
8040 		  limhi = 32;
8041 		  goto do_msb;
8042 		case 'F':
8043 		  limlo = 33;
8044 		  limhi = 64;
8045 		  goto do_msb;
8046 do_msb:
8047 		  my_getExpression (&imm_expr, s);
8048 		  check_absolute_expr (ip, &imm_expr);
8049 		  /* Check for negative input so that small negative numbers
8050 		     will not succeed incorrectly.  The checks against
8051 		     (pos+size) transitively check "size" itself,
8052 		     assuming that "pos" is reasonable.  */
8053 		  if ((long) imm_expr.X_add_number < 0
8054 		      || ((unsigned long) imm_expr.X_add_number
8055 			  + lastpos) < limlo
8056 		      || ((unsigned long) imm_expr.X_add_number
8057 			  + lastpos) > limhi)
8058 		    {
8059 		      as_bad (_("Improper insert size (%lu, position %lu)"),
8060 			      (unsigned long) imm_expr.X_add_number,
8061 			      (unsigned long) lastpos);
8062 		      imm_expr.X_add_number = limlo - lastpos;
8063 		    }
8064 		  INSERT_OPERAND (INSMSB, *ip,
8065 				 lastpos + imm_expr.X_add_number - 1);
8066 		  imm_expr.X_op = O_absent;
8067 		  s = expr_end;
8068 		  continue;
8069 
8070 		case 'C':		/* ext size, becomes MSBD.  */
8071 		  limlo = 1;
8072 		  limhi = 32;
8073 		  goto do_msbd;
8074 		case 'G':
8075 		  limlo = 33;
8076 		  limhi = 64;
8077 		  goto do_msbd;
8078 		case 'H':
8079 		  limlo = 33;
8080 		  limhi = 64;
8081 		  goto do_msbd;
8082 do_msbd:
8083 		  my_getExpression (&imm_expr, s);
8084 		  check_absolute_expr (ip, &imm_expr);
8085 		  /* Check for negative input so that small negative numbers
8086 		     will not succeed incorrectly.  The checks against
8087 		     (pos+size) transitively check "size" itself,
8088 		     assuming that "pos" is reasonable.  */
8089 		  if ((long) imm_expr.X_add_number < 0
8090 		      || ((unsigned long) imm_expr.X_add_number
8091 			  + lastpos) < limlo
8092 		      || ((unsigned long) imm_expr.X_add_number
8093 			  + lastpos) > limhi)
8094 		    {
8095 		      as_bad (_("Improper extract size (%lu, position %lu)"),
8096 			      (unsigned long) imm_expr.X_add_number,
8097 			      (unsigned long) lastpos);
8098 		      imm_expr.X_add_number = limlo - lastpos;
8099 		    }
8100 		  INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8101 		  imm_expr.X_op = O_absent;
8102 		  s = expr_end;
8103 		  continue;
8104 
8105 		case 'D':
8106 		  /* +D is for disassembly only; never match.  */
8107 		  break;
8108 
8109 		case 'I':
8110 		  /* "+I" is like "I", except that imm2_expr is used.  */
8111 		  my_getExpression (&imm2_expr, s);
8112 		  if (imm2_expr.X_op != O_big
8113 		      && imm2_expr.X_op != O_constant)
8114 		  insn_error = _("absolute expression required");
8115 		  if (HAVE_32BIT_GPRS)
8116 		    normalize_constant_expr (&imm2_expr);
8117 		  s = expr_end;
8118 		  continue;
8119 
8120 		default:
8121 		  as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8122 		    *args, insn->name, insn->args);
8123 		  /* Further processing is fruitless.  */
8124 		  return;
8125 		}
8126 	      break;
8127 
8128 	    case '<':		/* must be at least one digit */
8129 	      /*
8130 	       * According to the manual, if the shift amount is greater
8131 	       * than 31 or less than 0, then the shift amount should be
8132 	       * mod 32.  In reality the mips assembler issues an error.
8133 	       * We issue a warning and mask out all but the low 5 bits.
8134 	       */
8135 	      my_getExpression (&imm_expr, s);
8136 	      check_absolute_expr (ip, &imm_expr);
8137 	      if ((unsigned long) imm_expr.X_add_number > 31)
8138 		as_warn (_("Improper shift amount (%lu)"),
8139 			 (unsigned long) imm_expr.X_add_number);
8140 	      INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8141 	      imm_expr.X_op = O_absent;
8142 	      s = expr_end;
8143 	      continue;
8144 
8145 	    case '>':		/* shift amount minus 32 */
8146 	      my_getExpression (&imm_expr, s);
8147 	      check_absolute_expr (ip, &imm_expr);
8148 	      if ((unsigned long) imm_expr.X_add_number < 32
8149 		  || (unsigned long) imm_expr.X_add_number > 63)
8150 		break;
8151 	      INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8152 	      imm_expr.X_op = O_absent;
8153 	      s = expr_end;
8154 	      continue;
8155 
8156 	    case 'k':		/* cache code */
8157 	    case 'h':		/* prefx code */
8158 	      my_getExpression (&imm_expr, s);
8159 	      check_absolute_expr (ip, &imm_expr);
8160 	      if ((unsigned long) imm_expr.X_add_number > 31)
8161 		as_warn (_("Invalid value for `%s' (%lu)"),
8162 			 ip->insn_mo->name,
8163 			 (unsigned long) imm_expr.X_add_number);
8164 	      if (*args == 'k')
8165 		INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8166 	      else
8167 		INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8168 	      imm_expr.X_op = O_absent;
8169 	      s = expr_end;
8170 	      continue;
8171 
8172 	    case 'c':		/* break code */
8173 	      my_getExpression (&imm_expr, s);
8174 	      check_absolute_expr (ip, &imm_expr);
8175 	      if ((unsigned long) imm_expr.X_add_number > 1023)
8176 		as_warn (_("Illegal break code (%lu)"),
8177 			 (unsigned long) imm_expr.X_add_number);
8178 	      INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8179 	      imm_expr.X_op = O_absent;
8180 	      s = expr_end;
8181 	      continue;
8182 
8183 	    case 'q':		/* lower break code */
8184 	      my_getExpression (&imm_expr, s);
8185 	      check_absolute_expr (ip, &imm_expr);
8186 	      if ((unsigned long) imm_expr.X_add_number > 1023)
8187 		as_warn (_("Illegal lower break code (%lu)"),
8188 			 (unsigned long) imm_expr.X_add_number);
8189 	      INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8190 	      imm_expr.X_op = O_absent;
8191 	      s = expr_end;
8192 	      continue;
8193 
8194 	    case 'B':           /* 20-bit syscall/break code.  */
8195 	      my_getExpression (&imm_expr, s);
8196 	      check_absolute_expr (ip, &imm_expr);
8197 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8198 		as_warn (_("Illegal 20-bit code (%lu)"),
8199 			 (unsigned long) imm_expr.X_add_number);
8200 	      INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8201 	      imm_expr.X_op = O_absent;
8202 	      s = expr_end;
8203 	      continue;
8204 
8205 	    case 'C':           /* Coprocessor code */
8206 	      my_getExpression (&imm_expr, s);
8207 	      check_absolute_expr (ip, &imm_expr);
8208 	      if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8209 		{
8210 		  as_warn (_("Coproccesor code > 25 bits (%lu)"),
8211 			   (unsigned long) imm_expr.X_add_number);
8212 		  imm_expr.X_add_number &= ((1 << 25) - 1);
8213 		}
8214 	      ip->insn_opcode |= imm_expr.X_add_number;
8215 	      imm_expr.X_op = O_absent;
8216 	      s = expr_end;
8217 	      continue;
8218 
8219 	    case 'J':           /* 19-bit wait code.  */
8220 	      my_getExpression (&imm_expr, s);
8221 	      check_absolute_expr (ip, &imm_expr);
8222 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8223 		as_warn (_("Illegal 19-bit code (%lu)"),
8224 			 (unsigned long) imm_expr.X_add_number);
8225 	      INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8226 	      imm_expr.X_op = O_absent;
8227 	      s = expr_end;
8228 	      continue;
8229 
8230 	    case 'P':		/* Performance register */
8231 	      my_getExpression (&imm_expr, s);
8232 	      check_absolute_expr (ip, &imm_expr);
8233 	      if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8234 		as_warn (_("Invalid performance register (%lu)"),
8235 			 (unsigned long) imm_expr.X_add_number);
8236 	      INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8237 	      imm_expr.X_op = O_absent;
8238 	      s = expr_end;
8239 	      continue;
8240 
8241 	    case 'b':		/* base register */
8242 	    case 'd':		/* destination register */
8243 	    case 's':		/* source register */
8244 	    case 't':		/* target register */
8245 	    case 'r':		/* both target and source */
8246 	    case 'v':		/* both dest and source */
8247 	    case 'w':		/* both dest and target */
8248 	    case 'E':		/* coprocessor target register */
8249 	    case 'G':		/* coprocessor destination register */
8250 	    case 'K':		/* 'rdhwr' destination register */
8251 	    case 'x':		/* ignore register name */
8252 	    case 'z':		/* must be zero register */
8253 	    case 'U':           /* destination register (clo/clz).  */
8254 	      s_reset = s;
8255 	      if (s[0] == '$')
8256 		{
8257 
8258 		  if (ISDIGIT (s[1]))
8259 		    {
8260 		      ++s;
8261 		      regno = 0;
8262 		      do
8263 			{
8264 			  regno *= 10;
8265 			  regno += *s - '0';
8266 			  ++s;
8267 			}
8268 		      while (ISDIGIT (*s));
8269 		      if (regno > 31)
8270 			as_bad (_("Invalid register number (%d)"), regno);
8271 		    }
8272 		  else if (*args == 'E' || *args == 'G' || *args == 'K')
8273 		    goto notreg;
8274 		  else
8275 		    {
8276 		      if (s[1] == 'r' && s[2] == 'a')
8277 			{
8278 			  s += 3;
8279 			  regno = RA;
8280 			}
8281 		      else if (s[1] == 'f' && s[2] == 'p')
8282 			{
8283 			  s += 3;
8284 			  regno = FP;
8285 			}
8286 		      else if (s[1] == 's' && s[2] == 'p')
8287 			{
8288 			  s += 3;
8289 			  regno = SP;
8290 			}
8291 		      else if (s[1] == 'g' && s[2] == 'p')
8292 			{
8293 			  s += 3;
8294 			  regno = GP;
8295 			}
8296 		      else if (s[1] == 'a' && s[2] == 't')
8297 			{
8298 			  s += 3;
8299 			  regno = AT;
8300 			}
8301 		      else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8302 			{
8303 			  s += 4;
8304 			  regno = KT0;
8305 			}
8306 		      else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8307 			{
8308 			  s += 4;
8309 			  regno = KT1;
8310 			}
8311 		      else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8312 			{
8313 			  s += 5;
8314 			  regno = ZERO;
8315 			}
8316 		      else if (itbl_have_entries)
8317 			{
8318 			  char *p, *n;
8319 			  unsigned long r;
8320 
8321 			  p = s + 1; 	/* advance past '$' */
8322 			  n = itbl_get_field (&p);  /* n is name */
8323 
8324 			  /* See if this is a register defined in an
8325 			     itbl entry.  */
8326 			  if (itbl_get_reg_val (n, &r))
8327 			    {
8328 			      /* Get_field advances to the start of
8329 				 the next field, so we need to back
8330 				 rack to the end of the last field.  */
8331 			      if (p)
8332 				s = p - 1;
8333 			      else
8334 				s = strchr (s, '\0');
8335 			      regno = r;
8336 			    }
8337 			  else
8338 			    goto notreg;
8339 			}
8340 		      else
8341 			goto notreg;
8342 		    }
8343 		  if (regno == AT
8344 		      && ! mips_opts.noat
8345 		      && *args != 'E'
8346 		      && *args != 'G'
8347 		      && *args != 'K')
8348 		    as_warn (_("Used $at without \".set noat\""));
8349 		  c = *args;
8350 		  if (*s == ' ')
8351 		    ++s;
8352 		  if (args[1] != *s)
8353 		    {
8354 		      if (c == 'r' || c == 'v' || c == 'w')
8355 			{
8356 			  regno = lastregno;
8357 			  s = s_reset;
8358 			  ++args;
8359 			}
8360 		    }
8361 		  /* 'z' only matches $0.  */
8362 		  if (c == 'z' && regno != 0)
8363 		    break;
8364 
8365 	/* Now that we have assembled one operand, we use the args string
8366 	 * to figure out where it goes in the instruction.  */
8367 		  switch (c)
8368 		    {
8369 		    case 'r':
8370 		    case 's':
8371 		    case 'v':
8372 		    case 'b':
8373 		      INSERT_OPERAND (RS, *ip, regno);
8374 		      break;
8375 		    case 'd':
8376 		    case 'G':
8377 		    case 'K':
8378 		      INSERT_OPERAND (RD, *ip, regno);
8379 		      break;
8380 		    case 'U':
8381 		      INSERT_OPERAND (RD, *ip, regno);
8382 		      INSERT_OPERAND (RT, *ip, regno);
8383 		      break;
8384 		    case 'w':
8385 		    case 't':
8386 		    case 'E':
8387 		      INSERT_OPERAND (RT, *ip, regno);
8388 		      break;
8389 		    case 'x':
8390 		      /* This case exists because on the r3000 trunc
8391 			 expands into a macro which requires a gp
8392 			 register.  On the r6000 or r4000 it is
8393 			 assembled into a single instruction which
8394 			 ignores the register.  Thus the insn version
8395 			 is MIPS_ISA2 and uses 'x', and the macro
8396 			 version is MIPS_ISA1 and uses 't'.  */
8397 		      break;
8398 		    case 'z':
8399 		      /* This case is for the div instruction, which
8400 			 acts differently if the destination argument
8401 			 is $0.  This only matches $0, and is checked
8402 			 outside the switch.  */
8403 		      break;
8404 		    case 'D':
8405 		      /* Itbl operand; not yet implemented. FIXME ?? */
8406 		      break;
8407 		      /* What about all other operands like 'i', which
8408 			 can be specified in the opcode table? */
8409 		    }
8410 		  lastregno = regno;
8411 		  continue;
8412 		}
8413 	    notreg:
8414 	      switch (*args++)
8415 		{
8416 		case 'r':
8417 		case 'v':
8418 		  INSERT_OPERAND (RS, *ip, lastregno);
8419 		  continue;
8420 		case 'w':
8421 		  INSERT_OPERAND (RT, *ip, lastregno);
8422 		  continue;
8423 		}
8424 	      break;
8425 
8426 	    case 'O':		/* MDMX alignment immediate constant.  */
8427 	      my_getExpression (&imm_expr, s);
8428 	      check_absolute_expr (ip, &imm_expr);
8429 	      if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8430 		as_warn ("Improper align amount (%ld), using low bits",
8431 			 (long) imm_expr.X_add_number);
8432 	      INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
8433 	      imm_expr.X_op = O_absent;
8434 	      s = expr_end;
8435 	      continue;
8436 
8437 	    case 'Q':		/* MDMX vector, element sel, or const.  */
8438 	      if (s[0] != '$')
8439 		{
8440 		  /* MDMX Immediate.  */
8441 		  my_getExpression (&imm_expr, s);
8442 		  check_absolute_expr (ip, &imm_expr);
8443 		  if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8444 		    as_warn (_("Invalid MDMX Immediate (%ld)"),
8445 			     (long) imm_expr.X_add_number);
8446 		  INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
8447 		  if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8448 		    ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8449 		  else
8450 		    ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8451 		  imm_expr.X_op = O_absent;
8452 		  s = expr_end;
8453 		  continue;
8454 		}
8455 	      /* Not MDMX Immediate.  Fall through.  */
8456 	    case 'X':           /* MDMX destination register.  */
8457 	    case 'Y':           /* MDMX source register.  */
8458 	    case 'Z':           /* MDMX target register.  */
8459 	      is_mdmx = 1;
8460 	    case 'D':		/* floating point destination register */
8461 	    case 'S':		/* floating point source register */
8462 	    case 'T':		/* floating point target register */
8463 	    case 'R':		/* floating point source register */
8464 	    case 'V':
8465 	    case 'W':
8466 	      s_reset = s;
8467 	      /* Accept $fN for FP and MDMX register numbers, and in
8468                  addition accept $vN for MDMX register numbers.  */
8469 	      if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8470 		  || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8471 		      && ISDIGIT (s[2])))
8472 		{
8473 		  s += 2;
8474 		  regno = 0;
8475 		  do
8476 		    {
8477 		      regno *= 10;
8478 		      regno += *s - '0';
8479 		      ++s;
8480 		    }
8481 		  while (ISDIGIT (*s));
8482 
8483 		  if (regno > 31)
8484 		    as_bad (_("Invalid float register number (%d)"), regno);
8485 
8486 		  if ((regno & 1) != 0
8487 		      && HAVE_32BIT_FPRS
8488 		      && ! (strcmp (str, "mtc1") == 0
8489 			    || strcmp (str, "mfc1") == 0
8490 			    || strcmp (str, "lwc1") == 0
8491 			    || strcmp (str, "swc1") == 0
8492 			    || strcmp (str, "l.s") == 0
8493 			    || strcmp (str, "s.s") == 0))
8494 		    as_warn (_("Float register should be even, was %d"),
8495 			     regno);
8496 
8497 		  c = *args;
8498 		  if (*s == ' ')
8499 		    ++s;
8500 		  if (args[1] != *s)
8501 		    {
8502 		      if (c == 'V' || c == 'W')
8503 			{
8504 			  regno = lastregno;
8505 			  s = s_reset;
8506 			  ++args;
8507 			}
8508 		    }
8509 		  switch (c)
8510 		    {
8511 		    case 'D':
8512 		    case 'X':
8513 		      INSERT_OPERAND (FD, *ip, regno);
8514 		      break;
8515 		    case 'V':
8516 		    case 'S':
8517 		    case 'Y':
8518 		      INSERT_OPERAND (FS, *ip, regno);
8519 		      break;
8520 		    case 'Q':
8521 		      /* This is like 'Z', but also needs to fix the MDMX
8522 			 vector/scalar select bits.  Note that the
8523 			 scalar immediate case is handled above.  */
8524 		      if (*s == '[')
8525 			{
8526 			  int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8527 			  int max_el = (is_qh ? 3 : 7);
8528 			  s++;
8529 			  my_getExpression(&imm_expr, s);
8530 			  check_absolute_expr (ip, &imm_expr);
8531 			  s = expr_end;
8532 			  if (imm_expr.X_add_number > max_el)
8533 			    as_bad(_("Bad element selector %ld"),
8534 				   (long) imm_expr.X_add_number);
8535 			  imm_expr.X_add_number &= max_el;
8536 			  ip->insn_opcode |= (imm_expr.X_add_number
8537 					      << (OP_SH_VSEL +
8538 						  (is_qh ? 2 : 1)));
8539 			  imm_expr.X_op = O_absent;
8540 			  if (*s != ']')
8541 			    as_warn(_("Expecting ']' found '%s'"), s);
8542 			  else
8543 			    s++;
8544 			}
8545 		      else
8546                         {
8547                           if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8548                             ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8549 						<< OP_SH_VSEL);
8550 			  else
8551 			    ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8552 						OP_SH_VSEL);
8553 			}
8554                       /* Fall through */
8555 		    case 'W':
8556 		    case 'T':
8557 		    case 'Z':
8558 		      INSERT_OPERAND (FT, *ip, regno);
8559 		      break;
8560 		    case 'R':
8561 		      INSERT_OPERAND (FR, *ip, regno);
8562 		      break;
8563 		    }
8564 		  lastregno = regno;
8565 		  continue;
8566 		}
8567 
8568 	      switch (*args++)
8569 		{
8570 		case 'V':
8571 		  INSERT_OPERAND (FS, *ip, lastregno);
8572 		  continue;
8573 		case 'W':
8574 		  INSERT_OPERAND (FT, *ip, lastregno);
8575 		  continue;
8576 		}
8577 	      break;
8578 
8579 	    case 'I':
8580 	      my_getExpression (&imm_expr, s);
8581 	      if (imm_expr.X_op != O_big
8582 		  && imm_expr.X_op != O_constant)
8583 		insn_error = _("absolute expression required");
8584 	      if (HAVE_32BIT_GPRS)
8585 		normalize_constant_expr (&imm_expr);
8586 	      s = expr_end;
8587 	      continue;
8588 
8589 	    case 'A':
8590 	      my_getExpression (&offset_expr, s);
8591 	      normalize_address_expr (&offset_expr);
8592 	      *imm_reloc = BFD_RELOC_32;
8593 	      s = expr_end;
8594 	      continue;
8595 
8596 	    case 'F':
8597 	    case 'L':
8598 	    case 'f':
8599 	    case 'l':
8600 	      {
8601 		int f64;
8602 		int using_gprs;
8603 		char *save_in;
8604 		char *err;
8605 		unsigned char temp[8];
8606 		int len;
8607 		unsigned int length;
8608 		segT seg;
8609 		subsegT subseg;
8610 		char *p;
8611 
8612 		/* These only appear as the last operand in an
8613 		   instruction, and every instruction that accepts
8614 		   them in any variant accepts them in all variants.
8615 		   This means we don't have to worry about backing out
8616 		   any changes if the instruction does not match.
8617 
8618 		   The difference between them is the size of the
8619 		   floating point constant and where it goes.  For 'F'
8620 		   and 'L' the constant is 64 bits; for 'f' and 'l' it
8621 		   is 32 bits.  Where the constant is placed is based
8622 		   on how the MIPS assembler does things:
8623 		    F -- .rdata
8624 		    L -- .lit8
8625 		    f -- immediate value
8626 		    l -- .lit4
8627 
8628 		    The .lit4 and .lit8 sections are only used if
8629 		    permitted by the -G argument.
8630 
8631 		    The code below needs to know whether the target register
8632 		    is 32 or 64 bits wide.  It relies on the fact 'f' and
8633 		    'F' are used with GPR-based instructions and 'l' and
8634 		    'L' are used with FPR-based instructions.  */
8635 
8636 		f64 = *args == 'F' || *args == 'L';
8637 		using_gprs = *args == 'F' || *args == 'f';
8638 
8639 		save_in = input_line_pointer;
8640 		input_line_pointer = s;
8641 		err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8642 		length = len;
8643 		s = input_line_pointer;
8644 		input_line_pointer = save_in;
8645 		if (err != NULL && *err != '\0')
8646 		  {
8647 		    as_bad (_("Bad floating point constant: %s"), err);
8648 		    memset (temp, '\0', sizeof temp);
8649 		    length = f64 ? 8 : 4;
8650 		  }
8651 
8652 		assert (length == (unsigned) (f64 ? 8 : 4));
8653 
8654 		if (*args == 'f'
8655 		    || (*args == 'l'
8656 			&& (g_switch_value < 4
8657 			    || (temp[0] == 0 && temp[1] == 0)
8658 			    || (temp[2] == 0 && temp[3] == 0))))
8659 		  {
8660 		    imm_expr.X_op = O_constant;
8661 		    if (! target_big_endian)
8662 		      imm_expr.X_add_number = bfd_getl32 (temp);
8663 		    else
8664 		      imm_expr.X_add_number = bfd_getb32 (temp);
8665 		  }
8666 		else if (length > 4
8667 			 && ! mips_disable_float_construction
8668 			 /* Constants can only be constructed in GPRs and
8669 			    copied to FPRs if the GPRs are at least as wide
8670 			    as the FPRs.  Force the constant into memory if
8671 			    we are using 64-bit FPRs but the GPRs are only
8672 			    32 bits wide.  */
8673 			 && (using_gprs
8674 			     || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8675 			 && ((temp[0] == 0 && temp[1] == 0)
8676 			     || (temp[2] == 0 && temp[3] == 0))
8677 			 && ((temp[4] == 0 && temp[5] == 0)
8678 			     || (temp[6] == 0 && temp[7] == 0)))
8679 		  {
8680 		    /* The value is simple enough to load with a couple of
8681                        instructions.  If using 32-bit registers, set
8682                        imm_expr to the high order 32 bits and offset_expr to
8683                        the low order 32 bits.  Otherwise, set imm_expr to
8684                        the entire 64 bit constant.  */
8685 		    if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8686 		      {
8687 			imm_expr.X_op = O_constant;
8688 			offset_expr.X_op = O_constant;
8689 			if (! target_big_endian)
8690 			  {
8691 			    imm_expr.X_add_number = bfd_getl32 (temp + 4);
8692 			    offset_expr.X_add_number = bfd_getl32 (temp);
8693 			  }
8694 			else
8695 			  {
8696 			    imm_expr.X_add_number = bfd_getb32 (temp);
8697 			    offset_expr.X_add_number = bfd_getb32 (temp + 4);
8698 			  }
8699 			if (offset_expr.X_add_number == 0)
8700 			  offset_expr.X_op = O_absent;
8701 		      }
8702 		    else if (sizeof (imm_expr.X_add_number) > 4)
8703 		      {
8704 			imm_expr.X_op = O_constant;
8705 			if (! target_big_endian)
8706 			  imm_expr.X_add_number = bfd_getl64 (temp);
8707 			else
8708 			  imm_expr.X_add_number = bfd_getb64 (temp);
8709 		      }
8710 		    else
8711 		      {
8712 			imm_expr.X_op = O_big;
8713 			imm_expr.X_add_number = 4;
8714 			if (! target_big_endian)
8715 			  {
8716 			    generic_bignum[0] = bfd_getl16 (temp);
8717 			    generic_bignum[1] = bfd_getl16 (temp + 2);
8718 			    generic_bignum[2] = bfd_getl16 (temp + 4);
8719 			    generic_bignum[3] = bfd_getl16 (temp + 6);
8720 			  }
8721 			else
8722 			  {
8723 			    generic_bignum[0] = bfd_getb16 (temp + 6);
8724 			    generic_bignum[1] = bfd_getb16 (temp + 4);
8725 			    generic_bignum[2] = bfd_getb16 (temp + 2);
8726 			    generic_bignum[3] = bfd_getb16 (temp);
8727 			  }
8728 		      }
8729 		  }
8730 		else
8731 		  {
8732 		    const char *newname;
8733 		    segT new_seg;
8734 
8735 		    /* Switch to the right section.  */
8736 		    seg = now_seg;
8737 		    subseg = now_subseg;
8738 		    switch (*args)
8739 		      {
8740 		      default: /* unused default case avoids warnings.  */
8741 		      case 'L':
8742 			newname = RDATA_SECTION_NAME;
8743 			if (g_switch_value >= 8)
8744 			  newname = ".lit8";
8745 			break;
8746 		      case 'F':
8747 			newname = RDATA_SECTION_NAME;
8748 			break;
8749 		      case 'l':
8750 			assert (g_switch_value >= 4);
8751 			newname = ".lit4";
8752 			break;
8753 		      }
8754 		    new_seg = subseg_new (newname, (subsegT) 0);
8755 		    if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8756 		      bfd_set_section_flags (stdoutput, new_seg,
8757 					     (SEC_ALLOC
8758 					      | SEC_LOAD
8759 					      | SEC_READONLY
8760 					      | SEC_DATA));
8761 		    frag_align (*args == 'l' ? 2 : 3, 0, 0);
8762 		    if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8763 			&& strcmp (TARGET_OS, "elf") != 0)
8764 		      record_alignment (new_seg, 4);
8765 		    else
8766 		      record_alignment (new_seg, *args == 'l' ? 2 : 3);
8767 		    if (seg == now_seg)
8768 		      as_bad (_("Can't use floating point insn in this section"));
8769 
8770 		    /* Set the argument to the current address in the
8771 		       section.  */
8772 		    offset_expr.X_op = O_symbol;
8773 		    offset_expr.X_add_symbol =
8774 		      symbol_new ("L0\001", now_seg,
8775 				  (valueT) frag_now_fix (), frag_now);
8776 		    offset_expr.X_add_number = 0;
8777 
8778 		    /* Put the floating point number into the section.  */
8779 		    p = frag_more ((int) length);
8780 		    memcpy (p, temp, length);
8781 
8782 		    /* Switch back to the original section.  */
8783 		    subseg_set (seg, subseg);
8784 		  }
8785 	      }
8786 	      continue;
8787 
8788 	    case 'i':		/* 16 bit unsigned immediate */
8789 	    case 'j':		/* 16 bit signed immediate */
8790 	      *imm_reloc = BFD_RELOC_LO16;
8791 	      if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8792 		{
8793 		  int more;
8794 		  offsetT minval, maxval;
8795 
8796 		  more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8797 			  && strcmp (insn->name, insn[1].name) == 0);
8798 
8799 		  /* If the expression was written as an unsigned number,
8800 		     only treat it as signed if there are no more
8801 		     alternatives.  */
8802 		  if (more
8803 		      && *args == 'j'
8804 		      && sizeof (imm_expr.X_add_number) <= 4
8805 		      && imm_expr.X_op == O_constant
8806 		      && imm_expr.X_add_number < 0
8807 		      && imm_expr.X_unsigned
8808 		      && HAVE_64BIT_GPRS)
8809 		    break;
8810 
8811 		  /* For compatibility with older assemblers, we accept
8812 		     0x8000-0xffff as signed 16-bit numbers when only
8813 		     signed numbers are allowed.  */
8814 		  if (*args == 'i')
8815 		    minval = 0, maxval = 0xffff;
8816 		  else if (more)
8817 		    minval = -0x8000, maxval = 0x7fff;
8818 		  else
8819 		    minval = -0x8000, maxval = 0xffff;
8820 
8821 		  if (imm_expr.X_op != O_constant
8822 		      || imm_expr.X_add_number < minval
8823 		      || imm_expr.X_add_number > maxval)
8824 		    {
8825 		      if (more)
8826 			break;
8827 		      if (imm_expr.X_op == O_constant
8828 			  || imm_expr.X_op == O_big)
8829 			as_bad (_("expression out of range"));
8830 		    }
8831 		}
8832 	      s = expr_end;
8833 	      continue;
8834 
8835 	    case 'o':		/* 16 bit offset */
8836 	      /* Check whether there is only a single bracketed expression
8837 		 left.  If so, it must be the base register and the
8838 		 constant must be zero.  */
8839 	      if (*s == '(' && strchr (s + 1, '(') == 0)
8840 		{
8841 		  offset_expr.X_op = O_constant;
8842 		  offset_expr.X_add_number = 0;
8843 		  continue;
8844 		}
8845 
8846 	      /* If this value won't fit into a 16 bit offset, then go
8847 		 find a macro that will generate the 32 bit offset
8848 		 code pattern.  */
8849 	      if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8850 		  && (offset_expr.X_op != O_constant
8851 		      || offset_expr.X_add_number >= 0x8000
8852 		      || offset_expr.X_add_number < -0x8000))
8853 		break;
8854 
8855 	      s = expr_end;
8856 	      continue;
8857 
8858 	    case 'p':		/* pc relative offset */
8859 	      *offset_reloc = BFD_RELOC_16_PCREL_S2;
8860 	      my_getExpression (&offset_expr, s);
8861 	      s = expr_end;
8862 	      continue;
8863 
8864 	    case 'u':		/* upper 16 bits */
8865 	      if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8866 		  && imm_expr.X_op == O_constant
8867 		  && (imm_expr.X_add_number < 0
8868 		      || imm_expr.X_add_number >= 0x10000))
8869 		as_bad (_("lui expression not in range 0..65535"));
8870 	      s = expr_end;
8871 	      continue;
8872 
8873 	    case 'a':		/* 26 bit address */
8874 	      my_getExpression (&offset_expr, s);
8875 	      s = expr_end;
8876 	      *offset_reloc = BFD_RELOC_MIPS_JMP;
8877 	      continue;
8878 
8879 	    case 'N':		/* 3 bit branch condition code */
8880 	    case 'M':		/* 3 bit compare condition code */
8881 	      if (strncmp (s, "$fcc", 4) != 0)
8882 		break;
8883 	      s += 4;
8884 	      regno = 0;
8885 	      do
8886 		{
8887 		  regno *= 10;
8888 		  regno += *s - '0';
8889 		  ++s;
8890 		}
8891 	      while (ISDIGIT (*s));
8892 	      if (regno > 7)
8893 		as_bad (_("Invalid condition code register $fcc%d"), regno);
8894 	      if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8895 		   || strcmp(str + strlen(str) - 5, "any2f") == 0
8896 		   || strcmp(str + strlen(str) - 5, "any2t") == 0)
8897 		  && (regno & 1) != 0)
8898 		as_warn(_("Condition code register should be even for %s, was %d"),
8899 			str, regno);
8900 	      if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8901 		   || strcmp(str + strlen(str) - 5, "any4t") == 0)
8902 		  && (regno & 3) != 0)
8903 		as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
8904 			str, regno);
8905 	      if (*args == 'N')
8906 		INSERT_OPERAND (BCC, *ip, regno);
8907 	      else
8908 		INSERT_OPERAND (CCC, *ip, regno);
8909 	      continue;
8910 
8911 	    case 'H':
8912 	      if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8913 		s += 2;
8914 	      if (ISDIGIT (*s))
8915 		{
8916 		  c = 0;
8917 		  do
8918 		    {
8919 		      c *= 10;
8920 		      c += *s - '0';
8921 		      ++s;
8922 		    }
8923 		  while (ISDIGIT (*s));
8924 		}
8925 	      else
8926 		c = 8; /* Invalid sel value.  */
8927 
8928 	      if (c > 7)
8929 		as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8930 	      ip->insn_opcode |= c;
8931 	      continue;
8932 
8933 	    case 'e':
8934 	      /* Must be at least one digit.  */
8935 	      my_getExpression (&imm_expr, s);
8936 	      check_absolute_expr (ip, &imm_expr);
8937 
8938 	      if ((unsigned long) imm_expr.X_add_number
8939 		  > (unsigned long) OP_MASK_VECBYTE)
8940 		{
8941 		  as_bad (_("bad byte vector index (%ld)"),
8942 			   (long) imm_expr.X_add_number);
8943 		  imm_expr.X_add_number = 0;
8944 		}
8945 
8946 	      INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
8947 	      imm_expr.X_op = O_absent;
8948 	      s = expr_end;
8949 	      continue;
8950 
8951 	    case '%':
8952 	      my_getExpression (&imm_expr, s);
8953 	      check_absolute_expr (ip, &imm_expr);
8954 
8955 	      if ((unsigned long) imm_expr.X_add_number
8956 		  > (unsigned long) OP_MASK_VECALIGN)
8957 		{
8958 		  as_bad (_("bad byte vector index (%ld)"),
8959 			   (long) imm_expr.X_add_number);
8960 		  imm_expr.X_add_number = 0;
8961 		}
8962 
8963 	      INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
8964 	      imm_expr.X_op = O_absent;
8965 	      s = expr_end;
8966 	      continue;
8967 
8968 	    default:
8969 	      as_bad (_("bad char = '%c'\n"), *args);
8970 	      internalError ();
8971 	    }
8972 	  break;
8973 	}
8974       /* Args don't match.  */
8975       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8976 	  !strcmp (insn->name, insn[1].name))
8977 	{
8978 	  ++insn;
8979 	  s = argsStart;
8980 	  insn_error = _("illegal operands");
8981 	  continue;
8982 	}
8983       if (save_c)
8984 	*(--s) = save_c;
8985       insn_error = _("illegal operands");
8986       return;
8987     }
8988 }
8989 
8990 /* This routine assembles an instruction into its binary format when
8991    assembling for the mips16.  As a side effect, it sets one of the
8992    global variables imm_reloc or offset_reloc to the type of
8993    relocation to do if one of the operands is an address expression.
8994    It also sets mips16_small and mips16_ext if the user explicitly
8995    requested a small or extended instruction.  */
8996 
8997 static void
mips16_ip(char * str,struct mips_cl_insn * ip)8998 mips16_ip (char *str, struct mips_cl_insn *ip)
8999 {
9000   char *s;
9001   const char *args;
9002   struct mips_opcode *insn;
9003   char *argsstart;
9004   unsigned int regno;
9005   unsigned int lastregno = 0;
9006   char *s_reset;
9007   size_t i;
9008 
9009   insn_error = NULL;
9010 
9011   mips16_small = FALSE;
9012   mips16_ext = FALSE;
9013 
9014   for (s = str; ISLOWER (*s); ++s)
9015     ;
9016   switch (*s)
9017     {
9018     case '\0':
9019       break;
9020 
9021     case ' ':
9022       *s++ = '\0';
9023       break;
9024 
9025     case '.':
9026       if (s[1] == 't' && s[2] == ' ')
9027 	{
9028 	  *s = '\0';
9029 	  mips16_small = TRUE;
9030 	  s += 3;
9031 	  break;
9032 	}
9033       else if (s[1] == 'e' && s[2] == ' ')
9034 	{
9035 	  *s = '\0';
9036 	  mips16_ext = TRUE;
9037 	  s += 3;
9038 	  break;
9039 	}
9040       /* Fall through.  */
9041     default:
9042       insn_error = _("unknown opcode");
9043       return;
9044     }
9045 
9046   if (mips_opts.noautoextend && ! mips16_ext)
9047     mips16_small = TRUE;
9048 
9049   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9050     {
9051       insn_error = _("unrecognized opcode");
9052       return;
9053     }
9054 
9055   argsstart = s;
9056   for (;;)
9057     {
9058       assert (strcmp (insn->name, str) == 0);
9059 
9060       create_insn (ip, insn);
9061       imm_expr.X_op = O_absent;
9062       imm_reloc[0] = BFD_RELOC_UNUSED;
9063       imm_reloc[1] = BFD_RELOC_UNUSED;
9064       imm_reloc[2] = BFD_RELOC_UNUSED;
9065       imm2_expr.X_op = O_absent;
9066       offset_expr.X_op = O_absent;
9067       offset_reloc[0] = BFD_RELOC_UNUSED;
9068       offset_reloc[1] = BFD_RELOC_UNUSED;
9069       offset_reloc[2] = BFD_RELOC_UNUSED;
9070       for (args = insn->args; 1; ++args)
9071 	{
9072 	  int c;
9073 
9074 	  if (*s == ' ')
9075 	    ++s;
9076 
9077 	  /* In this switch statement we call break if we did not find
9078              a match, continue if we did find a match, or return if we
9079              are done.  */
9080 
9081 	  c = *args;
9082 	  switch (c)
9083 	    {
9084 	    case '\0':
9085 	      if (*s == '\0')
9086 		{
9087 		  /* Stuff the immediate value in now, if we can.  */
9088 		  if (imm_expr.X_op == O_constant
9089 		      && *imm_reloc > BFD_RELOC_UNUSED
9090 		      && insn->pinfo != INSN_MACRO)
9091 		    {
9092 		      valueT tmp;
9093 
9094 		      switch (*offset_reloc)
9095 			{
9096 			  case BFD_RELOC_MIPS16_HI16_S:
9097 			    tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9098 			    break;
9099 
9100 			  case BFD_RELOC_MIPS16_HI16:
9101 			    tmp = imm_expr.X_add_number >> 16;
9102 			    break;
9103 
9104 			  case BFD_RELOC_MIPS16_LO16:
9105 			    tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9106 				  - 0x8000;
9107 			    break;
9108 
9109 			  case BFD_RELOC_UNUSED:
9110 			    tmp = imm_expr.X_add_number;
9111 			    break;
9112 
9113 			  default:
9114 			    internalError ();
9115 			}
9116 		      *offset_reloc = BFD_RELOC_UNUSED;
9117 
9118 		      mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9119 				    tmp, TRUE, mips16_small,
9120 				    mips16_ext, &ip->insn_opcode,
9121 				    &ip->use_extend, &ip->extend);
9122 		      imm_expr.X_op = O_absent;
9123 		      *imm_reloc = BFD_RELOC_UNUSED;
9124 		    }
9125 
9126 		  return;
9127 		}
9128 	      break;
9129 
9130 	    case ',':
9131 	      if (*s++ == c)
9132 		continue;
9133 	      s--;
9134 	      switch (*++args)
9135 		{
9136 		case 'v':
9137 		  MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9138 		  continue;
9139 		case 'w':
9140 		  MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9141 		  continue;
9142 		}
9143 	      break;
9144 
9145 	    case '(':
9146 	    case ')':
9147 	      if (*s++ == c)
9148 		continue;
9149 	      break;
9150 
9151 	    case 'v':
9152 	    case 'w':
9153 	      if (s[0] != '$')
9154 		{
9155 		  if (c == 'v')
9156 		    MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9157 		  else
9158 		    MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9159 		  ++args;
9160 		  continue;
9161 		}
9162 	      /* Fall through.  */
9163 	    case 'x':
9164 	    case 'y':
9165 	    case 'z':
9166 	    case 'Z':
9167 	    case '0':
9168 	    case 'S':
9169 	    case 'R':
9170 	    case 'X':
9171 	    case 'Y':
9172 	      if (s[0] != '$')
9173 		break;
9174 	      s_reset = s;
9175 	      if (ISDIGIT (s[1]))
9176 		{
9177 		  ++s;
9178 		  regno = 0;
9179 		  do
9180 		    {
9181 		      regno *= 10;
9182 		      regno += *s - '0';
9183 		      ++s;
9184 		    }
9185 		  while (ISDIGIT (*s));
9186 		  if (regno > 31)
9187 		    {
9188 		      as_bad (_("invalid register number (%d)"), regno);
9189 		      regno = 2;
9190 		    }
9191 		}
9192 	      else
9193 		{
9194 		  if (s[1] == 'r' && s[2] == 'a')
9195 		    {
9196 		      s += 3;
9197 		      regno = RA;
9198 		    }
9199 		  else if (s[1] == 'f' && s[2] == 'p')
9200 		    {
9201 		      s += 3;
9202 		      regno = FP;
9203 		    }
9204 		  else if (s[1] == 's' && s[2] == 'p')
9205 		    {
9206 		      s += 3;
9207 		      regno = SP;
9208 		    }
9209 		  else if (s[1] == 'g' && s[2] == 'p')
9210 		    {
9211 		      s += 3;
9212 		      regno = GP;
9213 		    }
9214 		  else if (s[1] == 'a' && s[2] == 't')
9215 		    {
9216 		      s += 3;
9217 		      regno = AT;
9218 		    }
9219 		  else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9220 		    {
9221 		      s += 4;
9222 		      regno = KT0;
9223 		    }
9224 		  else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9225 		    {
9226 		      s += 4;
9227 		      regno = KT1;
9228 		    }
9229 		  else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9230 		    {
9231 		      s += 5;
9232 		      regno = ZERO;
9233 		    }
9234 		  else
9235 		    break;
9236 		}
9237 
9238 	      if (*s == ' ')
9239 		++s;
9240 	      if (args[1] != *s)
9241 		{
9242 		  if (c == 'v' || c == 'w')
9243 		    {
9244 		      regno = mips16_to_32_reg_map[lastregno];
9245 		      s = s_reset;
9246 		      ++args;
9247 		    }
9248 		}
9249 
9250 	      switch (c)
9251 		{
9252 		case 'x':
9253 		case 'y':
9254 		case 'z':
9255 		case 'v':
9256 		case 'w':
9257 		case 'Z':
9258 		  regno = mips32_to_16_reg_map[regno];
9259 		  break;
9260 
9261 		case '0':
9262 		  if (regno != 0)
9263 		    regno = ILLEGAL_REG;
9264 		  break;
9265 
9266 		case 'S':
9267 		  if (regno != SP)
9268 		    regno = ILLEGAL_REG;
9269 		  break;
9270 
9271 		case 'R':
9272 		  if (regno != RA)
9273 		    regno = ILLEGAL_REG;
9274 		  break;
9275 
9276 		case 'X':
9277 		case 'Y':
9278 		  if (regno == AT && ! mips_opts.noat)
9279 		    as_warn (_("used $at without \".set noat\""));
9280 		  break;
9281 
9282 		default:
9283 		  internalError ();
9284 		}
9285 
9286 	      if (regno == ILLEGAL_REG)
9287 		break;
9288 
9289 	      switch (c)
9290 		{
9291 		case 'x':
9292 		case 'v':
9293 		  MIPS16_INSERT_OPERAND (RX, *ip, regno);
9294 		  break;
9295 		case 'y':
9296 		case 'w':
9297 		  MIPS16_INSERT_OPERAND (RY, *ip, regno);
9298 		  break;
9299 		case 'z':
9300 		  MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9301 		  break;
9302 		case 'Z':
9303 		  MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9304 		case '0':
9305 		case 'S':
9306 		case 'R':
9307 		  break;
9308 		case 'X':
9309 		  MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9310 		  break;
9311 		case 'Y':
9312 		  regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9313 		  MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9314 		  break;
9315 		default:
9316 		  internalError ();
9317 		}
9318 
9319 	      lastregno = regno;
9320 	      continue;
9321 
9322 	    case 'P':
9323 	      if (strncmp (s, "$pc", 3) == 0)
9324 		{
9325 		  s += 3;
9326 		  continue;
9327 		}
9328 	      break;
9329 
9330 	    case '5':
9331 	    case 'H':
9332 	    case 'W':
9333 	    case 'D':
9334 	    case 'j':
9335 	    case 'V':
9336 	    case 'C':
9337 	    case 'U':
9338 	    case 'k':
9339 	    case 'K':
9340 	      i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9341 	      if (i > 0)
9342 		{
9343 		  if (imm_expr.X_op != O_constant)
9344 		    {
9345 		      mips16_ext = TRUE;
9346 		      ip->use_extend = TRUE;
9347 		      ip->extend = 0;
9348 		    }
9349 		  else
9350 		    {
9351 		      /* We need to relax this instruction.  */
9352 		      *offset_reloc = *imm_reloc;
9353 		      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9354 		    }
9355 		  s = expr_end;
9356 		  continue;
9357 		}
9358 	      *imm_reloc = BFD_RELOC_UNUSED;
9359 	      /* Fall through.  */
9360 	    case '<':
9361 	    case '>':
9362 	    case '[':
9363 	    case ']':
9364 	    case '4':
9365 	    case '8':
9366 	      my_getExpression (&imm_expr, s);
9367 	      if (imm_expr.X_op == O_register)
9368 		{
9369 		  /* What we thought was an expression turned out to
9370                      be a register.  */
9371 
9372 		  if (s[0] == '(' && args[1] == '(')
9373 		    {
9374 		      /* It looks like the expression was omitted
9375 			 before a register indirection, which means
9376 			 that the expression is implicitly zero.  We
9377 			 still set up imm_expr, so that we handle
9378 			 explicit extensions correctly.  */
9379 		      imm_expr.X_op = O_constant;
9380 		      imm_expr.X_add_number = 0;
9381 		      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9382 		      continue;
9383 		    }
9384 
9385 		  break;
9386 		}
9387 
9388 	      /* We need to relax this instruction.  */
9389 	      *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9390 	      s = expr_end;
9391 	      continue;
9392 
9393 	    case 'p':
9394 	    case 'q':
9395 	    case 'A':
9396 	    case 'B':
9397 	    case 'E':
9398 	      /* We use offset_reloc rather than imm_reloc for the PC
9399                  relative operands.  This lets macros with both
9400                  immediate and address operands work correctly.  */
9401 	      my_getExpression (&offset_expr, s);
9402 
9403 	      if (offset_expr.X_op == O_register)
9404 		break;
9405 
9406 	      /* We need to relax this instruction.  */
9407 	      *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9408 	      s = expr_end;
9409 	      continue;
9410 
9411 	    case '6':		/* break code */
9412 	      my_getExpression (&imm_expr, s);
9413 	      check_absolute_expr (ip, &imm_expr);
9414 	      if ((unsigned long) imm_expr.X_add_number > 63)
9415 		as_warn (_("Invalid value for `%s' (%lu)"),
9416 			 ip->insn_mo->name,
9417 			 (unsigned long) imm_expr.X_add_number);
9418 	      MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
9419 	      imm_expr.X_op = O_absent;
9420 	      s = expr_end;
9421 	      continue;
9422 
9423 	    case 'a':		/* 26 bit address */
9424 	      my_getExpression (&offset_expr, s);
9425 	      s = expr_end;
9426 	      *offset_reloc = BFD_RELOC_MIPS16_JMP;
9427 	      ip->insn_opcode <<= 16;
9428 	      continue;
9429 
9430 	    case 'l':		/* register list for entry macro */
9431 	    case 'L':		/* register list for exit macro */
9432 	      {
9433 		int mask;
9434 
9435 		if (c == 'l')
9436 		  mask = 0;
9437 		else
9438 		  mask = 7 << 3;
9439 		while (*s != '\0')
9440 		  {
9441 		    int freg, reg1, reg2;
9442 
9443 		    while (*s == ' ' || *s == ',')
9444 		      ++s;
9445 		    if (*s != '$')
9446 		      {
9447 			as_bad (_("can't parse register list"));
9448 			break;
9449 		      }
9450 		    ++s;
9451 		    if (*s != 'f')
9452 		      freg = 0;
9453 		    else
9454 		      {
9455 			freg = 1;
9456 			++s;
9457 		      }
9458 		    reg1 = 0;
9459 		    while (ISDIGIT (*s))
9460 		      {
9461 			reg1 *= 10;
9462 			reg1 += *s - '0';
9463 			++s;
9464 		      }
9465 		    if (*s == ' ')
9466 		      ++s;
9467 		    if (*s != '-')
9468 		      reg2 = reg1;
9469 		    else
9470 		      {
9471 			++s;
9472 			if (*s != '$')
9473 			  break;
9474 			++s;
9475 			if (freg)
9476 			  {
9477 			    if (*s == 'f')
9478 			      ++s;
9479 			    else
9480 			      {
9481 				as_bad (_("invalid register list"));
9482 				break;
9483 			      }
9484 			  }
9485 			reg2 = 0;
9486 			while (ISDIGIT (*s))
9487 			  {
9488 			    reg2 *= 10;
9489 			    reg2 += *s - '0';
9490 			    ++s;
9491 			  }
9492 		      }
9493 		    if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9494 		      {
9495 			mask &= ~ (7 << 3);
9496 			mask |= 5 << 3;
9497 		      }
9498 		    else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9499 		      {
9500 			mask &= ~ (7 << 3);
9501 			mask |= 6 << 3;
9502 		      }
9503 		    else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9504 		      mask |= (reg2 - 3) << 3;
9505 		    else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9506 		      mask |= (reg2 - 15) << 1;
9507 		    else if (reg1 == RA && reg2 == RA)
9508 		      mask |= 1;
9509 		    else
9510 		      {
9511 			as_bad (_("invalid register list"));
9512 			break;
9513 		      }
9514 		  }
9515 		/* The mask is filled in in the opcode table for the
9516                    benefit of the disassembler.  We remove it before
9517                    applying the actual mask.  */
9518 		ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9519 		ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9520 	      }
9521 	    continue;
9522 
9523 	    case 'e':		/* extend code */
9524 	      my_getExpression (&imm_expr, s);
9525 	      check_absolute_expr (ip, &imm_expr);
9526 	      if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9527 		{
9528 		  as_warn (_("Invalid value for `%s' (%lu)"),
9529 			   ip->insn_mo->name,
9530 			   (unsigned long) imm_expr.X_add_number);
9531 		  imm_expr.X_add_number &= 0x7ff;
9532 		}
9533 	      ip->insn_opcode |= imm_expr.X_add_number;
9534 	      imm_expr.X_op = O_absent;
9535 	      s = expr_end;
9536 	      continue;
9537 
9538 	    default:
9539 	      internalError ();
9540 	    }
9541 	  break;
9542 	}
9543 
9544       /* Args don't match.  */
9545       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9546 	  strcmp (insn->name, insn[1].name) == 0)
9547 	{
9548 	  ++insn;
9549 	  s = argsstart;
9550 	  continue;
9551 	}
9552 
9553       insn_error = _("illegal operands");
9554 
9555       return;
9556     }
9557 }
9558 
9559 /* This structure holds information we know about a mips16 immediate
9560    argument type.  */
9561 
9562 struct mips16_immed_operand
9563 {
9564   /* The type code used in the argument string in the opcode table.  */
9565   int type;
9566   /* The number of bits in the short form of the opcode.  */
9567   int nbits;
9568   /* The number of bits in the extended form of the opcode.  */
9569   int extbits;
9570   /* The amount by which the short form is shifted when it is used;
9571      for example, the sw instruction has a shift count of 2.  */
9572   int shift;
9573   /* The amount by which the short form is shifted when it is stored
9574      into the instruction code.  */
9575   int op_shift;
9576   /* Non-zero if the short form is unsigned.  */
9577   int unsp;
9578   /* Non-zero if the extended form is unsigned.  */
9579   int extu;
9580   /* Non-zero if the value is PC relative.  */
9581   int pcrel;
9582 };
9583 
9584 /* The mips16 immediate operand types.  */
9585 
9586 static const struct mips16_immed_operand mips16_immed_operands[] =
9587 {
9588   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9589   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9590   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
9591   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
9592   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9593   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9594   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9595   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9596   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9597   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9598   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9599   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9600   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9601   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9602   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9603   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9604   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9605   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9606   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9607   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9608   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9609 };
9610 
9611 #define MIPS16_NUM_IMMED \
9612   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9613 
9614 /* Handle a mips16 instruction with an immediate value.  This or's the
9615    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
9616    whether an extended value is needed; if one is needed, it sets
9617    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
9618    If SMALL is true, an unextended opcode was explicitly requested.
9619    If EXT is true, an extended opcode was explicitly requested.  If
9620    WARN is true, warn if EXT does not match reality.  */
9621 
9622 static void
mips16_immed(char * file,unsigned int line,int type,offsetT val,bfd_boolean warn,bfd_boolean small,bfd_boolean ext,unsigned long * insn,bfd_boolean * use_extend,unsigned short * extend)9623 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9624 	      bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9625 	      unsigned long *insn, bfd_boolean *use_extend,
9626 	      unsigned short *extend)
9627 {
9628   register const struct mips16_immed_operand *op;
9629   int mintiny, maxtiny;
9630   bfd_boolean needext;
9631 
9632   op = mips16_immed_operands;
9633   while (op->type != type)
9634     {
9635       ++op;
9636       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9637     }
9638 
9639   if (op->unsp)
9640     {
9641       if (type == '<' || type == '>' || type == '[' || type == ']')
9642 	{
9643 	  mintiny = 1;
9644 	  maxtiny = 1 << op->nbits;
9645 	}
9646       else
9647 	{
9648 	  mintiny = 0;
9649 	  maxtiny = (1 << op->nbits) - 1;
9650 	}
9651     }
9652   else
9653     {
9654       mintiny = - (1 << (op->nbits - 1));
9655       maxtiny = (1 << (op->nbits - 1)) - 1;
9656     }
9657 
9658   /* Branch offsets have an implicit 0 in the lowest bit.  */
9659   if (type == 'p' || type == 'q')
9660     val /= 2;
9661 
9662   if ((val & ((1 << op->shift) - 1)) != 0
9663       || val < (mintiny << op->shift)
9664       || val > (maxtiny << op->shift))
9665     needext = TRUE;
9666   else
9667     needext = FALSE;
9668 
9669   if (warn && ext && ! needext)
9670     as_warn_where (file, line,
9671 		   _("extended operand requested but not required"));
9672   if (small && needext)
9673     as_bad_where (file, line, _("invalid unextended operand value"));
9674 
9675   if (small || (! ext && ! needext))
9676     {
9677       int insnval;
9678 
9679       *use_extend = FALSE;
9680       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9681       insnval <<= op->op_shift;
9682       *insn |= insnval;
9683     }
9684   else
9685     {
9686       long minext, maxext;
9687       int extval;
9688 
9689       if (op->extu)
9690 	{
9691 	  minext = 0;
9692 	  maxext = (1 << op->extbits) - 1;
9693 	}
9694       else
9695 	{
9696 	  minext = - (1 << (op->extbits - 1));
9697 	  maxext = (1 << (op->extbits - 1)) - 1;
9698 	}
9699       if (val < minext || val > maxext)
9700 	as_bad_where (file, line,
9701 		      _("operand value out of range for instruction"));
9702 
9703       *use_extend = TRUE;
9704       if (op->extbits == 16)
9705 	{
9706 	  extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9707 	  val &= 0x1f;
9708 	}
9709       else if (op->extbits == 15)
9710 	{
9711 	  extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9712 	  val &= 0xf;
9713 	}
9714       else
9715 	{
9716 	  extval = ((val & 0x1f) << 6) | (val & 0x20);
9717 	  val = 0;
9718 	}
9719 
9720       *extend = (unsigned short) extval;
9721       *insn |= val;
9722     }
9723 }
9724 
9725 struct percent_op_match
9726 {
9727   const char *str;
9728   bfd_reloc_code_real_type reloc;
9729 };
9730 
9731 static const struct percent_op_match mips_percent_op[] =
9732 {
9733   {"%lo", BFD_RELOC_LO16},
9734 #ifdef OBJ_ELF
9735   {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9736   {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9737   {"%call16", BFD_RELOC_MIPS_CALL16},
9738   {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9739   {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9740   {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9741   {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9742   {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9743   {"%got", BFD_RELOC_MIPS_GOT16},
9744   {"%gp_rel", BFD_RELOC_GPREL16},
9745   {"%half", BFD_RELOC_16},
9746   {"%highest", BFD_RELOC_MIPS_HIGHEST},
9747   {"%higher", BFD_RELOC_MIPS_HIGHER},
9748   {"%neg", BFD_RELOC_MIPS_SUB},
9749   {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9750   {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9751   {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9752   {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9753   {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9754   {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9755   {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9756 #endif
9757   {"%hi", BFD_RELOC_HI16_S}
9758 };
9759 
9760 static const struct percent_op_match mips16_percent_op[] =
9761 {
9762   {"%lo", BFD_RELOC_MIPS16_LO16},
9763   {"%gprel", BFD_RELOC_MIPS16_GPREL},
9764   {"%hi", BFD_RELOC_MIPS16_HI16_S}
9765 };
9766 
9767 
9768 /* Return true if *STR points to a relocation operator.  When returning true,
9769    move *STR over the operator and store its relocation code in *RELOC.
9770    Leave both *STR and *RELOC alone when returning false.  */
9771 
9772 static bfd_boolean
parse_relocation(char ** str,bfd_reloc_code_real_type * reloc)9773 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9774 {
9775   const struct percent_op_match *percent_op;
9776   size_t limit, i;
9777 
9778   if (mips_opts.mips16)
9779     {
9780       percent_op = mips16_percent_op;
9781       limit = ARRAY_SIZE (mips16_percent_op);
9782     }
9783   else
9784     {
9785       percent_op = mips_percent_op;
9786       limit = ARRAY_SIZE (mips_percent_op);
9787     }
9788 
9789   for (i = 0; i < limit; i++)
9790     if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9791       {
9792 	int len = strlen (percent_op[i].str);
9793 
9794 	if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9795 	  continue;
9796 
9797 	*str += strlen (percent_op[i].str);
9798 	*reloc = percent_op[i].reloc;
9799 
9800 	/* Check whether the output BFD supports this relocation.
9801 	   If not, issue an error and fall back on something safe.  */
9802 	if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9803 	  {
9804 	    as_bad ("relocation %s isn't supported by the current ABI",
9805 		    percent_op[i].str);
9806 	    *reloc = BFD_RELOC_UNUSED;
9807 	  }
9808 	return TRUE;
9809       }
9810   return FALSE;
9811 }
9812 
9813 
9814 /* Parse string STR as a 16-bit relocatable operand.  Store the
9815    expression in *EP and the relocations in the array starting
9816    at RELOC.  Return the number of relocation operators used.
9817 
9818    On exit, EXPR_END points to the first character after the expression.  */
9819 
9820 static size_t
my_getSmallExpression(expressionS * ep,bfd_reloc_code_real_type * reloc,char * str)9821 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9822 		       char *str)
9823 {
9824   bfd_reloc_code_real_type reversed_reloc[3];
9825   size_t reloc_index, i;
9826   int crux_depth, str_depth;
9827   char *crux;
9828 
9829   /* Search for the start of the main expression, recoding relocations
9830      in REVERSED_RELOC.  End the loop with CRUX pointing to the start
9831      of the main expression and with CRUX_DEPTH containing the number
9832      of open brackets at that point.  */
9833   reloc_index = -1;
9834   str_depth = 0;
9835   do
9836     {
9837       reloc_index++;
9838       crux = str;
9839       crux_depth = str_depth;
9840 
9841       /* Skip over whitespace and brackets, keeping count of the number
9842 	 of brackets.  */
9843       while (*str == ' ' || *str == '\t' || *str == '(')
9844 	if (*str++ == '(')
9845 	  str_depth++;
9846     }
9847   while (*str == '%'
9848 	 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9849 	 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9850 
9851   my_getExpression (ep, crux);
9852   str = expr_end;
9853 
9854   /* Match every open bracket.  */
9855   while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9856     if (*str++ == ')')
9857       crux_depth--;
9858 
9859   if (crux_depth > 0)
9860     as_bad ("unclosed '('");
9861 
9862   expr_end = str;
9863 
9864   if (reloc_index != 0)
9865     {
9866       prev_reloc_op_frag = frag_now;
9867       for (i = 0; i < reloc_index; i++)
9868 	reloc[i] = reversed_reloc[reloc_index - 1 - i];
9869     }
9870 
9871   return reloc_index;
9872 }
9873 
9874 static void
my_getExpression(expressionS * ep,char * str)9875 my_getExpression (expressionS *ep, char *str)
9876 {
9877   char *save_in;
9878   valueT val;
9879 
9880   save_in = input_line_pointer;
9881   input_line_pointer = str;
9882   expression (ep);
9883   expr_end = input_line_pointer;
9884   input_line_pointer = save_in;
9885 
9886   /* If we are in mips16 mode, and this is an expression based on `.',
9887      then we bump the value of the symbol by 1 since that is how other
9888      text symbols are handled.  We don't bother to handle complex
9889      expressions, just `.' plus or minus a constant.  */
9890   if (mips_opts.mips16
9891       && ep->X_op == O_symbol
9892       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9893       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9894       && symbol_get_frag (ep->X_add_symbol) == frag_now
9895       && symbol_constant_p (ep->X_add_symbol)
9896       && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9897     S_SET_VALUE (ep->X_add_symbol, val + 1);
9898 }
9899 
9900 /* Turn a string in input_line_pointer into a floating point constant
9901    of type TYPE, and store the appropriate bytes in *LITP.  The number
9902    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
9903    returned, or NULL on OK.  */
9904 
9905 char *
md_atof(int type,char * litP,int * sizeP)9906 md_atof (int type, char *litP, int *sizeP)
9907 {
9908   int prec;
9909   LITTLENUM_TYPE words[4];
9910   char *t;
9911   int i;
9912 
9913   switch (type)
9914     {
9915     case 'f':
9916       prec = 2;
9917       break;
9918 
9919     case 'd':
9920       prec = 4;
9921       break;
9922 
9923     default:
9924       *sizeP = 0;
9925       return _("bad call to md_atof");
9926     }
9927 
9928   t = atof_ieee (input_line_pointer, type, words);
9929   if (t)
9930     input_line_pointer = t;
9931 
9932   *sizeP = prec * 2;
9933 
9934   if (! target_big_endian)
9935     {
9936       for (i = prec - 1; i >= 0; i--)
9937 	{
9938 	  md_number_to_chars (litP, words[i], 2);
9939 	  litP += 2;
9940 	}
9941     }
9942   else
9943     {
9944       for (i = 0; i < prec; i++)
9945 	{
9946 	  md_number_to_chars (litP, words[i], 2);
9947 	  litP += 2;
9948 	}
9949     }
9950 
9951   return NULL;
9952 }
9953 
9954 void
md_number_to_chars(char * buf,valueT val,int n)9955 md_number_to_chars (char *buf, valueT val, int n)
9956 {
9957   if (target_big_endian)
9958     number_to_chars_bigendian (buf, val, n);
9959   else
9960     number_to_chars_littleendian (buf, val, n);
9961 }
9962 
9963 #ifdef OBJ_ELF
support_64bit_objects(void)9964 static int support_64bit_objects(void)
9965 {
9966   const char **list, **l;
9967   int yes;
9968 
9969   list = bfd_target_list ();
9970   for (l = list; *l != NULL; l++)
9971 #ifdef TE_TMIPS
9972     /* This is traditional mips */
9973     if (strcmp (*l, "elf64-tradbigmips") == 0
9974 	|| strcmp (*l, "elf64-tradlittlemips") == 0)
9975 #else
9976     if (strcmp (*l, "elf64-bigmips") == 0
9977 	|| strcmp (*l, "elf64-littlemips") == 0)
9978 #endif
9979       break;
9980   yes = (*l != NULL);
9981   free (list);
9982   return yes;
9983 }
9984 #endif /* OBJ_ELF */
9985 
9986 const char *md_shortopts = "O::g::G:";
9987 
9988 struct option md_longopts[] =
9989 {
9990   /* Options which specify architecture.  */
9991 #define OPTION_ARCH_BASE    (OPTION_MD_BASE)
9992 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
9993   {"march", required_argument, NULL, OPTION_MARCH},
9994 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
9995   {"mtune", required_argument, NULL, OPTION_MTUNE},
9996 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
9997   {"mips0", no_argument, NULL, OPTION_MIPS1},
9998   {"mips1", no_argument, NULL, OPTION_MIPS1},
9999 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10000   {"mips2", no_argument, NULL, OPTION_MIPS2},
10001 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10002   {"mips3", no_argument, NULL, OPTION_MIPS3},
10003 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10004   {"mips4", no_argument, NULL, OPTION_MIPS4},
10005 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10006   {"mips5", no_argument, NULL, OPTION_MIPS5},
10007 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10008   {"mips32", no_argument, NULL, OPTION_MIPS32},
10009 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10010   {"mips64", no_argument, NULL, OPTION_MIPS64},
10011 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10012   {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10013 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10014   {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10015 
10016   /* Options which specify Application Specific Extensions (ASEs).  */
10017 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10018 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10019   {"mips16", no_argument, NULL, OPTION_MIPS16},
10020 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10021   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10022 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10023   {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10024 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10025   {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10026 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10027   {"mdmx", no_argument, NULL, OPTION_MDMX},
10028 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10029   {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10030 
10031   /* Old-style architecture options.  Don't add more of these.  */
10032 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10033 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10034   {"m4650", no_argument, NULL, OPTION_M4650},
10035 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10036   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10037 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10038   {"m4010", no_argument, NULL, OPTION_M4010},
10039 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10040   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10041 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10042   {"m4100", no_argument, NULL, OPTION_M4100},
10043 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10044   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10045 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10046   {"m3900", no_argument, NULL, OPTION_M3900},
10047 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10048   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10049 
10050   /* Options which enable bug fixes.  */
10051 #define OPTION_FIX_BASE    (OPTION_COMPAT_ARCH_BASE + 8)
10052 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10053   {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10054 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10055   {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10056   {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10057 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10058 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10059   {"mfix-vr4120",    no_argument, NULL, OPTION_FIX_VR4120},
10060   {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10061 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10062 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10063   {"mfix-vr4130",    no_argument, NULL, OPTION_FIX_VR4130},
10064   {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10065 
10066   /* Miscellaneous options.  */
10067 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10068 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10069   {"trap", no_argument, NULL, OPTION_TRAP},
10070   {"no-break", no_argument, NULL, OPTION_TRAP},
10071 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10072   {"break", no_argument, NULL, OPTION_BREAK},
10073   {"no-trap", no_argument, NULL, OPTION_BREAK},
10074 #define OPTION_EB (OPTION_MISC_BASE + 2)
10075   {"EB", no_argument, NULL, OPTION_EB},
10076 #define OPTION_EL (OPTION_MISC_BASE + 3)
10077   {"EL", no_argument, NULL, OPTION_EL},
10078 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10079   {"mfp32", no_argument, NULL, OPTION_FP32},
10080 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10081   {"mgp32", no_argument, NULL, OPTION_GP32},
10082 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10083   {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10084 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10085   {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10086 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10087   {"mfp64", no_argument, NULL, OPTION_FP64},
10088 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10089   {"mgp64", no_argument, NULL, OPTION_GP64},
10090 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10091 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10092   {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10093   {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10094 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10095 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10096   {"mshared", no_argument, NULL, OPTION_MSHARED},
10097   {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10098 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10099 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10100   {"msym32", no_argument, NULL, OPTION_MSYM32},
10101   {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10102 
10103   /* ELF-specific options.  */
10104 #ifdef OBJ_ELF
10105 #define OPTION_ELF_BASE    (OPTION_MISC_BASE + 16)
10106 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10107   {"KPIC",        no_argument, NULL, OPTION_CALL_SHARED},
10108   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10109 #define OPTION_NON_SHARED  (OPTION_ELF_BASE + 1)
10110   {"non_shared",  no_argument, NULL, OPTION_NON_SHARED},
10111 #define OPTION_XGOT        (OPTION_ELF_BASE + 2)
10112   {"xgot",        no_argument, NULL, OPTION_XGOT},
10113 #define OPTION_MABI        (OPTION_ELF_BASE + 3)
10114   {"mabi", required_argument, NULL, OPTION_MABI},
10115 #define OPTION_32 	   (OPTION_ELF_BASE + 4)
10116   {"32",          no_argument, NULL, OPTION_32},
10117 #define OPTION_N32 	   (OPTION_ELF_BASE + 5)
10118   {"n32",         no_argument, NULL, OPTION_N32},
10119 #define OPTION_64          (OPTION_ELF_BASE + 6)
10120   {"64",          no_argument, NULL, OPTION_64},
10121 #define OPTION_MDEBUG      (OPTION_ELF_BASE + 7)
10122   {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10123 #define OPTION_NO_MDEBUG   (OPTION_ELF_BASE + 8)
10124   {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10125 #define OPTION_PDR	   (OPTION_ELF_BASE + 9)
10126   {"mpdr", no_argument, NULL, OPTION_PDR},
10127 #define OPTION_NO_PDR	   (OPTION_ELF_BASE + 10)
10128   {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10129 #endif /* OBJ_ELF */
10130 
10131   {NULL, no_argument, NULL, 0}
10132 };
10133 size_t md_longopts_size = sizeof (md_longopts);
10134 
10135 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10136    NEW_VALUE.  Warn if another value was already specified.  Note:
10137    we have to defer parsing the -march and -mtune arguments in order
10138    to handle 'from-abi' correctly, since the ABI might be specified
10139    in a later argument.  */
10140 
10141 static void
mips_set_option_string(const char ** string_ptr,const char * new_value)10142 mips_set_option_string (const char **string_ptr, const char *new_value)
10143 {
10144   if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10145     as_warn (_("A different %s was already specified, is now %s"),
10146 	     string_ptr == &mips_arch_string ? "-march" : "-mtune",
10147 	     new_value);
10148 
10149   *string_ptr = new_value;
10150 }
10151 
10152 int
md_parse_option(int c,char * arg)10153 md_parse_option (int c, char *arg)
10154 {
10155   switch (c)
10156     {
10157     case OPTION_CONSTRUCT_FLOATS:
10158       mips_disable_float_construction = 0;
10159       break;
10160 
10161     case OPTION_NO_CONSTRUCT_FLOATS:
10162       mips_disable_float_construction = 1;
10163       break;
10164 
10165     case OPTION_TRAP:
10166       mips_trap = 1;
10167       break;
10168 
10169     case OPTION_BREAK:
10170       mips_trap = 0;
10171       break;
10172 
10173     case OPTION_EB:
10174       target_big_endian = 1;
10175       break;
10176 
10177     case OPTION_EL:
10178       target_big_endian = 0;
10179       break;
10180 
10181     case 'O':
10182       if (arg && arg[1] == '0')
10183 	mips_optimize = 1;
10184       else
10185 	mips_optimize = 2;
10186       break;
10187 
10188     case 'g':
10189       if (arg == NULL)
10190 	mips_debug = 2;
10191       else
10192 	mips_debug = atoi (arg);
10193       /* When the MIPS assembler sees -g or -g2, it does not do
10194          optimizations which limit full symbolic debugging.  We take
10195          that to be equivalent to -O0.  */
10196       if (mips_debug == 2)
10197 	mips_optimize = 1;
10198       break;
10199 
10200     case OPTION_MIPS1:
10201       file_mips_isa = ISA_MIPS1;
10202       break;
10203 
10204     case OPTION_MIPS2:
10205       file_mips_isa = ISA_MIPS2;
10206       break;
10207 
10208     case OPTION_MIPS3:
10209       file_mips_isa = ISA_MIPS3;
10210       break;
10211 
10212     case OPTION_MIPS4:
10213       file_mips_isa = ISA_MIPS4;
10214       break;
10215 
10216     case OPTION_MIPS5:
10217       file_mips_isa = ISA_MIPS5;
10218       break;
10219 
10220     case OPTION_MIPS32:
10221       file_mips_isa = ISA_MIPS32;
10222       break;
10223 
10224     case OPTION_MIPS32R2:
10225       file_mips_isa = ISA_MIPS32R2;
10226       break;
10227 
10228     case OPTION_MIPS64R2:
10229       file_mips_isa = ISA_MIPS64R2;
10230       break;
10231 
10232     case OPTION_MIPS64:
10233       file_mips_isa = ISA_MIPS64;
10234       break;
10235 
10236     case OPTION_MTUNE:
10237       mips_set_option_string (&mips_tune_string, arg);
10238       break;
10239 
10240     case OPTION_MARCH:
10241       mips_set_option_string (&mips_arch_string, arg);
10242       break;
10243 
10244     case OPTION_M4650:
10245       mips_set_option_string (&mips_arch_string, "4650");
10246       mips_set_option_string (&mips_tune_string, "4650");
10247       break;
10248 
10249     case OPTION_NO_M4650:
10250       break;
10251 
10252     case OPTION_M4010:
10253       mips_set_option_string (&mips_arch_string, "4010");
10254       mips_set_option_string (&mips_tune_string, "4010");
10255       break;
10256 
10257     case OPTION_NO_M4010:
10258       break;
10259 
10260     case OPTION_M4100:
10261       mips_set_option_string (&mips_arch_string, "4100");
10262       mips_set_option_string (&mips_tune_string, "4100");
10263       break;
10264 
10265     case OPTION_NO_M4100:
10266       break;
10267 
10268     case OPTION_M3900:
10269       mips_set_option_string (&mips_arch_string, "3900");
10270       mips_set_option_string (&mips_tune_string, "3900");
10271       break;
10272 
10273     case OPTION_NO_M3900:
10274       break;
10275 
10276     case OPTION_MDMX:
10277       mips_opts.ase_mdmx = 1;
10278       break;
10279 
10280     case OPTION_NO_MDMX:
10281       mips_opts.ase_mdmx = 0;
10282       break;
10283 
10284     case OPTION_MIPS16:
10285       mips_opts.mips16 = 1;
10286       mips_no_prev_insn ();
10287       break;
10288 
10289     case OPTION_NO_MIPS16:
10290       mips_opts.mips16 = 0;
10291       mips_no_prev_insn ();
10292       break;
10293 
10294     case OPTION_MIPS3D:
10295       mips_opts.ase_mips3d = 1;
10296       break;
10297 
10298     case OPTION_NO_MIPS3D:
10299       mips_opts.ase_mips3d = 0;
10300       break;
10301 
10302     case OPTION_FIX_VR4120:
10303       mips_fix_vr4120 = 1;
10304       break;
10305 
10306     case OPTION_NO_FIX_VR4120:
10307       mips_fix_vr4120 = 0;
10308       break;
10309 
10310     case OPTION_FIX_VR4130:
10311       mips_fix_vr4130 = 1;
10312       break;
10313 
10314     case OPTION_NO_FIX_VR4130:
10315       mips_fix_vr4130 = 0;
10316       break;
10317 
10318     case OPTION_RELAX_BRANCH:
10319       mips_relax_branch = 1;
10320       break;
10321 
10322     case OPTION_NO_RELAX_BRANCH:
10323       mips_relax_branch = 0;
10324       break;
10325 
10326     case OPTION_MSHARED:
10327       mips_in_shared = TRUE;
10328       break;
10329 
10330     case OPTION_MNO_SHARED:
10331       mips_in_shared = FALSE;
10332       break;
10333 
10334     case OPTION_MSYM32:
10335       mips_opts.sym32 = TRUE;
10336       break;
10337 
10338     case OPTION_MNO_SYM32:
10339       mips_opts.sym32 = FALSE;
10340       break;
10341 
10342 #ifdef OBJ_ELF
10343       /* When generating ELF code, we permit -KPIC and -call_shared to
10344 	 select SVR4_PIC, and -non_shared to select no PIC.  This is
10345 	 intended to be compatible with Irix 5.  */
10346     case OPTION_CALL_SHARED:
10347       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10348 	{
10349 	  as_bad (_("-call_shared is supported only for ELF format"));
10350 	  return 0;
10351 	}
10352       mips_pic = SVR4_PIC;
10353       mips_abicalls = TRUE;
10354       if (g_switch_seen && g_switch_value != 0)
10355 	{
10356 	  as_bad (_("-G may not be used with SVR4 PIC code"));
10357 	  return 0;
10358 	}
10359       g_switch_value = 0;
10360       break;
10361 
10362     case OPTION_NON_SHARED:
10363       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10364 	{
10365 	  as_bad (_("-non_shared is supported only for ELF format"));
10366 	  return 0;
10367 	}
10368       mips_pic = NO_PIC;
10369       mips_abicalls = FALSE;
10370       break;
10371 
10372       /* The -xgot option tells the assembler to use 32 offsets when
10373          accessing the got in SVR4_PIC mode.  It is for Irix
10374          compatibility.  */
10375     case OPTION_XGOT:
10376       mips_big_got = 1;
10377       break;
10378 #endif /* OBJ_ELF */
10379 
10380     case 'G':
10381       g_switch_value = atoi (arg);
10382       g_switch_seen = 1;
10383       if (mips_pic == SVR4_PIC && g_switch_value != 0)
10384 	{
10385 	  as_bad (_("-G may not be used with SVR4 PIC code"));
10386 	  return 0;
10387 	}
10388       break;
10389 
10390 #ifdef OBJ_ELF
10391       /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10392 	 and -mabi=64.  */
10393     case OPTION_32:
10394       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10395 	{
10396 	  as_bad (_("-32 is supported for ELF format only"));
10397 	  return 0;
10398 	}
10399       mips_abi = O32_ABI;
10400       break;
10401 
10402     case OPTION_N32:
10403       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10404 	{
10405 	  as_bad (_("-n32 is supported for ELF format only"));
10406 	  return 0;
10407 	}
10408       mips_abi = N32_ABI;
10409       break;
10410 
10411     case OPTION_64:
10412       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10413 	{
10414 	  as_bad (_("-64 is supported for ELF format only"));
10415 	  return 0;
10416 	}
10417       mips_abi = N64_ABI;
10418       if (! support_64bit_objects())
10419 	as_fatal (_("No compiled in support for 64 bit object file format"));
10420       break;
10421 #endif /* OBJ_ELF */
10422 
10423     case OPTION_GP32:
10424       file_mips_gp32 = 1;
10425       break;
10426 
10427     case OPTION_GP64:
10428       file_mips_gp32 = 0;
10429       break;
10430 
10431     case OPTION_FP32:
10432       file_mips_fp32 = 1;
10433       break;
10434 
10435     case OPTION_FP64:
10436       file_mips_fp32 = 0;
10437       break;
10438 
10439 #ifdef OBJ_ELF
10440     case OPTION_MABI:
10441       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10442 	{
10443 	  as_bad (_("-mabi is supported for ELF format only"));
10444 	  return 0;
10445 	}
10446       if (strcmp (arg, "32") == 0)
10447 	mips_abi = O32_ABI;
10448       else if (strcmp (arg, "o64") == 0)
10449 	mips_abi = O64_ABI;
10450       else if (strcmp (arg, "n32") == 0)
10451 	mips_abi = N32_ABI;
10452       else if (strcmp (arg, "64") == 0)
10453 	{
10454 	  mips_abi = N64_ABI;
10455 	  if (! support_64bit_objects())
10456 	    as_fatal (_("No compiled in support for 64 bit object file "
10457 			"format"));
10458 	}
10459       else if (strcmp (arg, "eabi") == 0)
10460 	mips_abi = EABI_ABI;
10461       else
10462 	{
10463 	  as_fatal (_("invalid abi -mabi=%s"), arg);
10464 	  return 0;
10465 	}
10466       break;
10467 #endif /* OBJ_ELF */
10468 
10469     case OPTION_M7000_HILO_FIX:
10470       mips_7000_hilo_fix = TRUE;
10471       break;
10472 
10473     case OPTION_MNO_7000_HILO_FIX:
10474       mips_7000_hilo_fix = FALSE;
10475       break;
10476 
10477 #ifdef OBJ_ELF
10478     case OPTION_MDEBUG:
10479       mips_flag_mdebug = TRUE;
10480       break;
10481 
10482     case OPTION_NO_MDEBUG:
10483       mips_flag_mdebug = FALSE;
10484       break;
10485 
10486     case OPTION_PDR:
10487       mips_flag_pdr = TRUE;
10488       break;
10489 
10490     case OPTION_NO_PDR:
10491       mips_flag_pdr = FALSE;
10492       break;
10493 #endif /* OBJ_ELF */
10494 
10495     default:
10496       return 0;
10497     }
10498 
10499   return 1;
10500 }
10501 
10502 /* Set up globals to generate code for the ISA or processor
10503    described by INFO.  */
10504 
10505 static void
mips_set_architecture(const struct mips_cpu_info * info)10506 mips_set_architecture (const struct mips_cpu_info *info)
10507 {
10508   if (info != 0)
10509     {
10510       file_mips_arch = info->cpu;
10511       mips_opts.arch = info->cpu;
10512       mips_opts.isa = info->isa;
10513     }
10514 }
10515 
10516 
10517 /* Likewise for tuning.  */
10518 
10519 static void
mips_set_tune(const struct mips_cpu_info * info)10520 mips_set_tune (const struct mips_cpu_info *info)
10521 {
10522   if (info != 0)
10523     mips_tune = info->cpu;
10524 }
10525 
10526 
10527 void
mips_after_parse_args(void)10528 mips_after_parse_args (void)
10529 {
10530   const struct mips_cpu_info *arch_info = 0;
10531   const struct mips_cpu_info *tune_info = 0;
10532 
10533   /* GP relative stuff not working for PE */
10534   if (strncmp (TARGET_OS, "pe", 2) == 0)
10535     {
10536       if (g_switch_seen && g_switch_value != 0)
10537 	as_bad (_("-G not supported in this configuration."));
10538       g_switch_value = 0;
10539     }
10540 
10541   if (mips_abi == NO_ABI)
10542     mips_abi = MIPS_DEFAULT_ABI;
10543 
10544   /* The following code determines the architecture and register size.
10545      Similar code was added to GCC 3.3 (see override_options() in
10546      config/mips/mips.c).  The GAS and GCC code should be kept in sync
10547      as much as possible.  */
10548 
10549   if (mips_arch_string != 0)
10550     arch_info = mips_parse_cpu ("-march", mips_arch_string);
10551 
10552   if (file_mips_isa != ISA_UNKNOWN)
10553     {
10554       /* Handle -mipsN.  At this point, file_mips_isa contains the
10555 	 ISA level specified by -mipsN, while arch_info->isa contains
10556 	 the -march selection (if any).  */
10557       if (arch_info != 0)
10558 	{
10559 	  /* -march takes precedence over -mipsN, since it is more descriptive.
10560 	     There's no harm in specifying both as long as the ISA levels
10561 	     are the same.  */
10562 	  if (file_mips_isa != arch_info->isa)
10563 	    as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10564 		    mips_cpu_info_from_isa (file_mips_isa)->name,
10565 		    mips_cpu_info_from_isa (arch_info->isa)->name);
10566 	}
10567       else
10568 	arch_info = mips_cpu_info_from_isa (file_mips_isa);
10569     }
10570 
10571   if (arch_info == 0)
10572     arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10573 
10574   if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10575     as_bad ("-march=%s is not compatible with the selected ABI",
10576 	    arch_info->name);
10577 
10578   mips_set_architecture (arch_info);
10579 
10580   /* Optimize for file_mips_arch, unless -mtune selects a different processor.  */
10581   if (mips_tune_string != 0)
10582     tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10583 
10584   if (tune_info == 0)
10585     mips_set_tune (arch_info);
10586   else
10587     mips_set_tune (tune_info);
10588 
10589   if (file_mips_gp32 >= 0)
10590     {
10591       /* The user specified the size of the integer registers.  Make sure
10592 	 it agrees with the ABI and ISA.  */
10593       if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10594 	as_bad (_("-mgp64 used with a 32-bit processor"));
10595       else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10596 	as_bad (_("-mgp32 used with a 64-bit ABI"));
10597       else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10598 	as_bad (_("-mgp64 used with a 32-bit ABI"));
10599     }
10600   else
10601     {
10602       /* Infer the integer register size from the ABI and processor.
10603 	 Restrict ourselves to 32-bit registers if that's all the
10604 	 processor has, or if the ABI cannot handle 64-bit registers.  */
10605       file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10606 			|| !ISA_HAS_64BIT_REGS (mips_opts.isa));
10607     }
10608 
10609   /* ??? GAS treats single-float processors as though they had 64-bit
10610      float registers (although it complains when double-precision
10611      instructions are used).  As things stand, saying they have 32-bit
10612      registers would lead to spurious "register must be even" messages.
10613      So here we assume float registers are always the same size as
10614      integer ones, unless the user says otherwise.  */
10615   if (file_mips_fp32 < 0)
10616     file_mips_fp32 = file_mips_gp32;
10617 
10618   /* End of GCC-shared inference code.  */
10619 
10620   /* This flag is set when we have a 64-bit capable CPU but use only
10621      32-bit wide registers.  Note that EABI does not use it.  */
10622   if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10623       && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10624 	  || mips_abi == O32_ABI))
10625     mips_32bitmode = 1;
10626 
10627   if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10628     as_bad (_("trap exception not supported at ISA 1"));
10629 
10630   /* If the selected architecture includes support for ASEs, enable
10631      generation of code for them.  */
10632   if (mips_opts.mips16 == -1)
10633     mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10634   if (mips_opts.ase_mips3d == -1)
10635     mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10636   if (mips_opts.ase_mdmx == -1)
10637     mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10638 
10639   file_mips_isa = mips_opts.isa;
10640   file_ase_mips16 = mips_opts.mips16;
10641   file_ase_mips3d = mips_opts.ase_mips3d;
10642   file_ase_mdmx = mips_opts.ase_mdmx;
10643   mips_opts.gp32 = file_mips_gp32;
10644   mips_opts.fp32 = file_mips_fp32;
10645 
10646   if (mips_flag_mdebug < 0)
10647     {
10648 #ifdef OBJ_MAYBE_ECOFF
10649       if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10650 	mips_flag_mdebug = 1;
10651       else
10652 #endif /* OBJ_MAYBE_ECOFF */
10653 	mips_flag_mdebug = 0;
10654     }
10655 }
10656 
10657 void
mips_init_after_args(void)10658 mips_init_after_args (void)
10659 {
10660   /* initialize opcodes */
10661   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10662   mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10663 }
10664 
10665 long
md_pcrel_from(fixS * fixP)10666 md_pcrel_from (fixS *fixP)
10667 {
10668   valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10669   switch (fixP->fx_r_type)
10670     {
10671     case BFD_RELOC_16_PCREL_S2:
10672     case BFD_RELOC_MIPS_JMP:
10673       /* Return the address of the delay slot.  */
10674       return addr + 4;
10675     default:
10676       return addr;
10677     }
10678 }
10679 
10680 /* This is called before the symbol table is processed.  In order to
10681    work with gcc when using mips-tfile, we must keep all local labels.
10682    However, in other cases, we want to discard them.  If we were
10683    called with -g, but we didn't see any debugging information, it may
10684    mean that gcc is smuggling debugging information through to
10685    mips-tfile, in which case we must generate all local labels.  */
10686 
10687 void
mips_frob_file_before_adjust(void)10688 mips_frob_file_before_adjust (void)
10689 {
10690 #ifndef NO_ECOFF_DEBUGGING
10691   if (ECOFF_DEBUGGING
10692       && mips_debug != 0
10693       && ! ecoff_debugging_seen)
10694     flag_keep_locals = 1;
10695 #endif
10696 }
10697 
10698 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10699    the corresponding LO16 reloc.  This is called before md_apply_fix3 and
10700    tc_gen_reloc.  Unmatched relocs can only be generated by use of explicit
10701    relocation operators.
10702 
10703    For our purposes, a %lo() expression matches a %got() or %hi()
10704    expression if:
10705 
10706       (a) it refers to the same symbol; and
10707       (b) the offset applied in the %lo() expression is no lower than
10708 	  the offset applied in the %got() or %hi().
10709 
10710    (b) allows us to cope with code like:
10711 
10712 	lui	$4,%hi(foo)
10713 	lh	$4,%lo(foo+2)($4)
10714 
10715    ...which is legal on RELA targets, and has a well-defined behaviour
10716    if the user knows that adding 2 to "foo" will not induce a carry to
10717    the high 16 bits.
10718 
10719    When several %lo()s match a particular %got() or %hi(), we use the
10720    following rules to distinguish them:
10721 
10722      (1) %lo()s with smaller offsets are a better match than %lo()s with
10723          higher offsets.
10724 
10725      (2) %lo()s with no matching %got() or %hi() are better than those
10726          that already have a matching %got() or %hi().
10727 
10728      (3) later %lo()s are better than earlier %lo()s.
10729 
10730    These rules are applied in order.
10731 
10732    (1) means, among other things, that %lo()s with identical offsets are
10733    chosen if they exist.
10734 
10735    (2) means that we won't associate several high-part relocations with
10736    the same low-part relocation unless there's no alternative.  Having
10737    several high parts for the same low part is a GNU extension; this rule
10738    allows careful users to avoid it.
10739 
10740    (3) is purely cosmetic.  mips_hi_fixup_list is is in reverse order,
10741    with the last high-part relocation being at the front of the list.
10742    It therefore makes sense to choose the last matching low-part
10743    relocation, all other things being equal.  It's also easier
10744    to code that way.  */
10745 
10746 void
mips_frob_file(void)10747 mips_frob_file (void)
10748 {
10749   struct mips_hi_fixup *l;
10750 
10751   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10752     {
10753       segment_info_type *seginfo;
10754       bfd_boolean matched_lo_p;
10755       fixS **hi_pos, **lo_pos, **pos;
10756 
10757       assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10758 
10759       /* If a GOT16 relocation turns out to be against a global symbol,
10760 	 there isn't supposed to be a matching LO.  */
10761       if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10762 	  && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10763 	continue;
10764 
10765       /* Check quickly whether the next fixup happens to be a matching %lo.  */
10766       if (fixup_has_matching_lo_p (l->fixp))
10767 	continue;
10768 
10769       seginfo = seg_info (l->seg);
10770 
10771       /* Set HI_POS to the position of this relocation in the chain.
10772 	 Set LO_POS to the position of the chosen low-part relocation.
10773 	 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10774 	 relocation that matches an immediately-preceding high-part
10775 	 relocation.  */
10776       hi_pos = NULL;
10777       lo_pos = NULL;
10778       matched_lo_p = FALSE;
10779       for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10780 	{
10781 	  if (*pos == l->fixp)
10782 	    hi_pos = pos;
10783 
10784 	  if (((*pos)->fx_r_type == BFD_RELOC_LO16
10785 	       || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
10786 	      && (*pos)->fx_addsy == l->fixp->fx_addsy
10787 	      && (*pos)->fx_offset >= l->fixp->fx_offset
10788 	      && (lo_pos == NULL
10789 		  || (*pos)->fx_offset < (*lo_pos)->fx_offset
10790 		  || (!matched_lo_p
10791 		      && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10792 	    lo_pos = pos;
10793 
10794 	  matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10795 			  && fixup_has_matching_lo_p (*pos));
10796 	}
10797 
10798       /* If we found a match, remove the high-part relocation from its
10799 	 current position and insert it before the low-part relocation.
10800 	 Make the offsets match so that fixup_has_matching_lo_p()
10801 	 will return true.
10802 
10803 	 We don't warn about unmatched high-part relocations since some
10804 	 versions of gcc have been known to emit dead "lui ...%hi(...)"
10805 	 instructions.  */
10806       if (lo_pos != NULL)
10807 	{
10808 	  l->fixp->fx_offset = (*lo_pos)->fx_offset;
10809 	  if (l->fixp->fx_next != *lo_pos)
10810 	    {
10811 	      *hi_pos = l->fixp->fx_next;
10812 	      l->fixp->fx_next = *lo_pos;
10813 	      *lo_pos = l->fixp;
10814 	    }
10815 	}
10816     }
10817 }
10818 
10819 /* We may have combined relocations without symbols in the N32/N64 ABI.
10820    We have to prevent gas from dropping them.  */
10821 
10822 int
mips_force_relocation(fixS * fixp)10823 mips_force_relocation (fixS *fixp)
10824 {
10825   if (generic_force_reloc (fixp))
10826     return 1;
10827 
10828   if (HAVE_NEWABI
10829       && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10830       && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10831 	  || fixp->fx_r_type == BFD_RELOC_HI16_S
10832 	  || fixp->fx_r_type == BFD_RELOC_LO16))
10833     return 1;
10834 
10835   return 0;
10836 }
10837 
10838 /* This hook is called before a fix is simplified.  We don't really
10839    decide whether to skip a fix here.  Rather, we turn global symbols
10840    used as branch targets into local symbols, such that they undergo
10841    simplification.  We can only do this if the symbol is defined and
10842    it is in the same section as the branch.  If this doesn't hold, we
10843    emit a better error message than just saying the relocation is not
10844    valid for the selected object format.
10845 
10846    FIXP is the fix-up we're going to try to simplify, SEG is the
10847    segment in which the fix up occurs.  The return value should be
10848    non-zero to indicate the fix-up is valid for further
10849    simplifications.  */
10850 
10851 int
mips_validate_fix(struct fix * fixP,asection * seg)10852 mips_validate_fix (struct fix *fixP, asection *seg)
10853 {
10854   /* There's a lot of discussion on whether it should be possible to
10855      use R_MIPS_PC16 to represent branch relocations.  The outcome
10856      seems to be that it can, but gas/bfd are very broken in creating
10857      RELA relocations for this, so for now we only accept branches to
10858      symbols in the same section.  Anything else is of dubious value,
10859      since there's no guarantee that at link time the symbol would be
10860      in range.  Even for branches to local symbols this is arguably
10861      wrong, since it we assume the symbol is not going to be
10862      overridden, which should be possible per ELF library semantics,
10863      but then, there isn't a dynamic relocation that could be used to
10864      this effect, and the target would likely be out of range as well.
10865 
10866      Unfortunately, it seems that there is too much code out there
10867      that relies on branches to symbols that are global to be resolved
10868      as if they were local, like the IRIX tools do, so we do it as
10869      well, but with a warning so that people are reminded to fix their
10870      code.  If we ever get back to using R_MIPS_PC16 for branch
10871      targets, this entire block should go away (and probably the
10872      whole function).  */
10873 
10874   if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10875       && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10876 	   || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10877 	  || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10878       && fixP->fx_addsy)
10879     {
10880       if (! S_IS_DEFINED (fixP->fx_addsy))
10881 	{
10882 	  as_bad_where (fixP->fx_file, fixP->fx_line,
10883 			_("Cannot branch to undefined symbol."));
10884 	  /* Avoid any further errors about this fixup.  */
10885 	  fixP->fx_done = 1;
10886 	}
10887       else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10888 	{
10889 	  as_bad_where (fixP->fx_file, fixP->fx_line,
10890 			_("Cannot branch to symbol in another section."));
10891 	  fixP->fx_done = 1;
10892 	}
10893       else if (S_IS_EXTERNAL (fixP->fx_addsy))
10894 	{
10895 	  symbolS *sym = fixP->fx_addsy;
10896 
10897 	  if (mips_pic == SVR4_PIC)
10898 	    as_warn_where (fixP->fx_file, fixP->fx_line,
10899 			   _("Pretending global symbol used as branch target is local."));
10900 
10901 	  fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10902 					  S_GET_SEGMENT (sym),
10903 					  S_GET_VALUE (sym),
10904 					  symbol_get_frag (sym));
10905 	  copy_symbol_attributes (fixP->fx_addsy, sym);
10906 	  S_CLEAR_EXTERNAL (fixP->fx_addsy);
10907 	  assert (symbol_resolved_p (sym));
10908 	  symbol_mark_resolved (fixP->fx_addsy);
10909 	}
10910     }
10911 
10912   return 1;
10913 }
10914 
10915 /* Apply a fixup to the object file.  */
10916 
10917 void
md_apply_fix3(fixS * fixP,valueT * valP,segT seg ATTRIBUTE_UNUSED)10918 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
10919 {
10920   bfd_byte *buf;
10921   long insn;
10922   reloc_howto_type *howto;
10923 
10924   /* We ignore generic BFD relocations we don't know about.  */
10925   howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10926   if (! howto)
10927     return;
10928 
10929   assert (fixP->fx_size == 4
10930 	  || fixP->fx_r_type == BFD_RELOC_16
10931 	  || fixP->fx_r_type == BFD_RELOC_64
10932 	  || fixP->fx_r_type == BFD_RELOC_CTOR
10933 	  || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10934 	  || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10935 	  || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10936 
10937   buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
10938 
10939   assert (! fixP->fx_pcrel);
10940 
10941   /* Don't treat parts of a composite relocation as done.  There are two
10942      reasons for this:
10943 
10944      (1) The second and third parts will be against 0 (RSS_UNDEF) but
10945 	 should nevertheless be emitted if the first part is.
10946 
10947      (2) In normal usage, composite relocations are never assembly-time
10948 	 constants.  The easiest way of dealing with the pathological
10949 	 exceptions is to generate a relocation against STN_UNDEF and
10950 	 leave everything up to the linker.  */
10951   if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
10952     fixP->fx_done = 1;
10953 
10954   switch (fixP->fx_r_type)
10955     {
10956     case BFD_RELOC_MIPS_TLS_GD:
10957     case BFD_RELOC_MIPS_TLS_LDM:
10958     case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
10959     case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
10960     case BFD_RELOC_MIPS_TLS_GOTTPREL:
10961     case BFD_RELOC_MIPS_TLS_TPREL_HI16:
10962     case BFD_RELOC_MIPS_TLS_TPREL_LO16:
10963       S_SET_THREAD_LOCAL (fixP->fx_addsy);
10964       /* fall through */
10965 
10966     case BFD_RELOC_MIPS_JMP:
10967     case BFD_RELOC_MIPS_SHIFT5:
10968     case BFD_RELOC_MIPS_SHIFT6:
10969     case BFD_RELOC_MIPS_GOT_DISP:
10970     case BFD_RELOC_MIPS_GOT_PAGE:
10971     case BFD_RELOC_MIPS_GOT_OFST:
10972     case BFD_RELOC_MIPS_SUB:
10973     case BFD_RELOC_MIPS_INSERT_A:
10974     case BFD_RELOC_MIPS_INSERT_B:
10975     case BFD_RELOC_MIPS_DELETE:
10976     case BFD_RELOC_MIPS_HIGHEST:
10977     case BFD_RELOC_MIPS_HIGHER:
10978     case BFD_RELOC_MIPS_SCN_DISP:
10979     case BFD_RELOC_MIPS_REL16:
10980     case BFD_RELOC_MIPS_RELGOT:
10981     case BFD_RELOC_MIPS_JALR:
10982     case BFD_RELOC_HI16:
10983     case BFD_RELOC_HI16_S:
10984     case BFD_RELOC_GPREL16:
10985     case BFD_RELOC_MIPS_LITERAL:
10986     case BFD_RELOC_MIPS_CALL16:
10987     case BFD_RELOC_MIPS_GOT16:
10988     case BFD_RELOC_GPREL32:
10989     case BFD_RELOC_MIPS_GOT_HI16:
10990     case BFD_RELOC_MIPS_GOT_LO16:
10991     case BFD_RELOC_MIPS_CALL_HI16:
10992     case BFD_RELOC_MIPS_CALL_LO16:
10993     case BFD_RELOC_MIPS16_GPREL:
10994     case BFD_RELOC_MIPS16_HI16:
10995     case BFD_RELOC_MIPS16_HI16_S:
10996       assert (! fixP->fx_pcrel);
10997       /* Nothing needed to do. The value comes from the reloc entry */
10998       break;
10999 
11000     case BFD_RELOC_MIPS16_JMP:
11001       /* We currently always generate a reloc against a symbol, which
11002          means that we don't want an addend even if the symbol is
11003          defined.  */
11004       *valP = 0;
11005       break;
11006 
11007     case BFD_RELOC_64:
11008       /* This is handled like BFD_RELOC_32, but we output a sign
11009          extended value if we are only 32 bits.  */
11010       if (fixP->fx_done)
11011 	{
11012 	  if (8 <= sizeof (valueT))
11013 	    md_number_to_chars ((char *) buf, *valP, 8);
11014 	  else
11015 	    {
11016 	      valueT hiv;
11017 
11018 	      if ((*valP & 0x80000000) != 0)
11019 		hiv = 0xffffffff;
11020 	      else
11021 		hiv = 0;
11022 	      md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
11023 				  *valP, 4);
11024 	      md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
11025 				  hiv, 4);
11026 	    }
11027 	}
11028       break;
11029 
11030     case BFD_RELOC_RVA:
11031     case BFD_RELOC_32:
11032       /* If we are deleting this reloc entry, we must fill in the
11033 	 value now.  This can happen if we have a .word which is not
11034 	 resolved when it appears but is later defined.   */
11035       if (fixP->fx_done)
11036 	md_number_to_chars ((char *) buf, *valP, 4);
11037       break;
11038 
11039     case BFD_RELOC_16:
11040       /* If we are deleting this reloc entry, we must fill in the
11041          value now.  */
11042       if (fixP->fx_done)
11043 	md_number_to_chars ((char *) buf, *valP, 2);
11044       break;
11045 
11046     case BFD_RELOC_LO16:
11047     case BFD_RELOC_MIPS16_LO16:
11048       /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11049 	 may be safe to remove, but if so it's not obvious.  */
11050       /* When handling an embedded PIC switch statement, we can wind
11051 	 up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
11052       if (fixP->fx_done)
11053 	{
11054 	  if (*valP + 0x8000 > 0xffff)
11055 	    as_bad_where (fixP->fx_file, fixP->fx_line,
11056 			  _("relocation overflow"));
11057 	  if (target_big_endian)
11058 	    buf += 2;
11059 	  md_number_to_chars ((char *) buf, *valP, 2);
11060 	}
11061       break;
11062 
11063     case BFD_RELOC_16_PCREL_S2:
11064       if ((*valP & 0x3) != 0)
11065 	as_bad_where (fixP->fx_file, fixP->fx_line,
11066 		      _("Branch to odd address (%lx)"), (long) *valP);
11067 
11068       /*
11069        * We need to save the bits in the instruction since fixup_segment()
11070        * might be deleting the relocation entry (i.e., a branch within
11071        * the current segment).
11072        */
11073       if (! fixP->fx_done)
11074 	break;
11075 
11076       /* update old instruction data */
11077       if (target_big_endian)
11078 	insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11079       else
11080 	insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11081 
11082       if (*valP + 0x20000 <= 0x3ffff)
11083 	{
11084 	  insn |= (*valP >> 2) & 0xffff;
11085 	  md_number_to_chars ((char *) buf, insn, 4);
11086 	}
11087       else if (mips_pic == NO_PIC
11088 	       && fixP->fx_done
11089 	       && fixP->fx_frag->fr_address >= text_section->vma
11090 	       && (fixP->fx_frag->fr_address
11091 		   < text_section->vma + bfd_get_section_size (text_section))
11092 	       && ((insn & 0xffff0000) == 0x10000000	 /* beq $0,$0 */
11093 		   || (insn & 0xffff0000) == 0x04010000	 /* bgez $0 */
11094 		   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11095 	{
11096 	  /* The branch offset is too large.  If this is an
11097              unconditional branch, and we are not generating PIC code,
11098              we can convert it to an absolute jump instruction.  */
11099 	  if ((insn & 0xffff0000) == 0x04110000)	 /* bgezal $0 */
11100 	    insn = 0x0c000000;	/* jal */
11101 	  else
11102 	    insn = 0x08000000;	/* j */
11103 	  fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11104 	  fixP->fx_done = 0;
11105 	  fixP->fx_addsy = section_symbol (text_section);
11106 	  *valP += md_pcrel_from (fixP);
11107 	  md_number_to_chars ((char *) buf, insn, 4);
11108 	}
11109       else
11110 	{
11111 	  /* If we got here, we have branch-relaxation disabled,
11112 	     and there's nothing we can do to fix this instruction
11113 	     without turning it into a longer sequence.  */
11114 	  as_bad_where (fixP->fx_file, fixP->fx_line,
11115 			_("Branch out of range"));
11116 	}
11117       break;
11118 
11119     case BFD_RELOC_VTABLE_INHERIT:
11120       fixP->fx_done = 0;
11121       if (fixP->fx_addsy
11122           && !S_IS_DEFINED (fixP->fx_addsy)
11123           && !S_IS_WEAK (fixP->fx_addsy))
11124         S_SET_WEAK (fixP->fx_addsy);
11125       break;
11126 
11127     case BFD_RELOC_VTABLE_ENTRY:
11128       fixP->fx_done = 0;
11129       break;
11130 
11131     default:
11132       internalError ();
11133     }
11134 
11135   /* Remember value for tc_gen_reloc.  */
11136   fixP->fx_addnumber = *valP;
11137 }
11138 
11139 static symbolS *
get_symbol(void)11140 get_symbol (void)
11141 {
11142   int c;
11143   char *name;
11144   symbolS *p;
11145 
11146   name = input_line_pointer;
11147   c = get_symbol_end ();
11148   p = (symbolS *) symbol_find_or_make (name);
11149   *input_line_pointer = c;
11150   return p;
11151 }
11152 
11153 /* Align the current frag to a given power of two.  The MIPS assembler
11154    also automatically adjusts any preceding label.  */
11155 
11156 static void
mips_align(int to,int fill,symbolS * label)11157 mips_align (int to, int fill, symbolS *label)
11158 {
11159   mips_emit_delays ();
11160   frag_align (to, fill, 0);
11161   record_alignment (now_seg, to);
11162   if (label != NULL)
11163     {
11164       assert (S_GET_SEGMENT (label) == now_seg);
11165       symbol_set_frag (label, frag_now);
11166       S_SET_VALUE (label, (valueT) frag_now_fix ());
11167     }
11168 }
11169 
11170 /* Align to a given power of two.  .align 0 turns off the automatic
11171    alignment used by the data creating pseudo-ops.  */
11172 
11173 static void
s_align(int x ATTRIBUTE_UNUSED)11174 s_align (int x ATTRIBUTE_UNUSED)
11175 {
11176   register int temp;
11177   register long temp_fill;
11178   long max_alignment = 15;
11179 
11180   /*
11181 
11182     o  Note that the assembler pulls down any immediately preceding label
11183        to the aligned address.
11184     o  It's not documented but auto alignment is reinstated by
11185        a .align pseudo instruction.
11186     o  Note also that after auto alignment is turned off the mips assembler
11187        issues an error on attempt to assemble an improperly aligned data item.
11188        We don't.
11189 
11190     */
11191 
11192   temp = get_absolute_expression ();
11193   if (temp > max_alignment)
11194     as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11195   else if (temp < 0)
11196     {
11197       as_warn (_("Alignment negative: 0 assumed."));
11198       temp = 0;
11199     }
11200   if (*input_line_pointer == ',')
11201     {
11202       ++input_line_pointer;
11203       temp_fill = get_absolute_expression ();
11204     }
11205   else
11206     temp_fill = 0;
11207   if (temp)
11208     {
11209       auto_align = 1;
11210       mips_align (temp, (int) temp_fill,
11211 		  insn_labels != NULL ? insn_labels->label : NULL);
11212     }
11213   else
11214     {
11215       auto_align = 0;
11216     }
11217 
11218   demand_empty_rest_of_line ();
11219 }
11220 
11221 static void
s_change_sec(int sec)11222 s_change_sec (int sec)
11223 {
11224   segT seg;
11225 
11226 #ifdef OBJ_ELF
11227   /* The ELF backend needs to know that we are changing sections, so
11228      that .previous works correctly.  We could do something like check
11229      for an obj_section_change_hook macro, but that might be confusing
11230      as it would not be appropriate to use it in the section changing
11231      functions in read.c, since obj-elf.c intercepts those.  FIXME:
11232      This should be cleaner, somehow.  */
11233   obj_elf_section_change_hook ();
11234 #endif
11235 
11236   mips_emit_delays ();
11237   switch (sec)
11238     {
11239     case 't':
11240       s_text (0);
11241       break;
11242     case 'd':
11243       s_data (0);
11244       break;
11245     case 'b':
11246       subseg_set (bss_section, (subsegT) get_absolute_expression ());
11247       demand_empty_rest_of_line ();
11248       break;
11249 
11250     case 'r':
11251       seg = subseg_new (RDATA_SECTION_NAME,
11252 			(subsegT) get_absolute_expression ());
11253       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11254 	{
11255 	  bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11256 						  | SEC_READONLY | SEC_RELOC
11257 						  | SEC_DATA));
11258 	  if (strcmp (TARGET_OS, "elf") != 0)
11259 	    record_alignment (seg, 4);
11260 	}
11261       demand_empty_rest_of_line ();
11262       break;
11263 
11264     case 's':
11265       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11266       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11267 	{
11268 	  bfd_set_section_flags (stdoutput, seg,
11269 				 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11270 	  if (strcmp (TARGET_OS, "elf") != 0)
11271 	    record_alignment (seg, 4);
11272 	}
11273       demand_empty_rest_of_line ();
11274       break;
11275     }
11276 
11277   auto_align = 1;
11278 }
11279 
11280 void
s_change_section(int ignore ATTRIBUTE_UNUSED)11281 s_change_section (int ignore ATTRIBUTE_UNUSED)
11282 {
11283 #ifdef OBJ_ELF
11284   char *section_name;
11285   char c;
11286   char next_c = 0;
11287   int section_type;
11288   int section_flag;
11289   int section_entry_size;
11290   int section_alignment;
11291 
11292   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11293     return;
11294 
11295   section_name = input_line_pointer;
11296   c = get_symbol_end ();
11297   if (c)
11298     next_c = *(input_line_pointer + 1);
11299 
11300   /* Do we have .section Name<,"flags">?  */
11301   if (c != ',' || (c == ',' && next_c == '"'))
11302     {
11303       /* just after name is now '\0'.  */
11304       *input_line_pointer = c;
11305       input_line_pointer = section_name;
11306       obj_elf_section (ignore);
11307       return;
11308     }
11309   input_line_pointer++;
11310 
11311   /* Do we have .section Name<,type><,flag><,entry_size><,alignment>  */
11312   if (c == ',')
11313     section_type = get_absolute_expression ();
11314   else
11315     section_type = 0;
11316   if (*input_line_pointer++ == ',')
11317     section_flag = get_absolute_expression ();
11318   else
11319     section_flag = 0;
11320   if (*input_line_pointer++ == ',')
11321     section_entry_size = get_absolute_expression ();
11322   else
11323     section_entry_size = 0;
11324   if (*input_line_pointer++ == ',')
11325     section_alignment = get_absolute_expression ();
11326   else
11327     section_alignment = 0;
11328 
11329   section_name = xstrdup (section_name);
11330 
11331   /* When using the generic form of .section (as implemented by obj-elf.c),
11332      there's no way to set the section type to SHT_MIPS_DWARF.  Users have
11333      traditionally had to fall back on the more common @progbits instead.
11334 
11335      There's nothing really harmful in this, since bfd will correct
11336      SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file.  But it
11337      means that, for backwards compatibiltiy, the special_section entries
11338      for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11339 
11340      Even so, we shouldn't force users of the MIPS .section syntax to
11341      incorrectly label the sections as SHT_PROGBITS.  The best compromise
11342      seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11343      generic type-checking code.  */
11344   if (section_type == SHT_MIPS_DWARF)
11345     section_type = SHT_PROGBITS;
11346 
11347   obj_elf_change_section (section_name, section_type, section_flag,
11348 			  section_entry_size, 0, 0, 0);
11349 
11350   if (now_seg->name != section_name)
11351     free (section_name);
11352 #endif /* OBJ_ELF */
11353 }
11354 
11355 void
mips_enable_auto_align(void)11356 mips_enable_auto_align (void)
11357 {
11358   auto_align = 1;
11359 }
11360 
11361 static void
s_cons(int log_size)11362 s_cons (int log_size)
11363 {
11364   symbolS *label;
11365 
11366   label = insn_labels != NULL ? insn_labels->label : NULL;
11367   mips_emit_delays ();
11368   if (log_size > 0 && auto_align)
11369     mips_align (log_size, 0, label);
11370   mips_clear_insn_labels ();
11371   cons (1 << log_size);
11372 }
11373 
11374 static void
s_float_cons(int type)11375 s_float_cons (int type)
11376 {
11377   symbolS *label;
11378 
11379   label = insn_labels != NULL ? insn_labels->label : NULL;
11380 
11381   mips_emit_delays ();
11382 
11383   if (auto_align)
11384     {
11385       if (type == 'd')
11386 	mips_align (3, 0, label);
11387       else
11388 	mips_align (2, 0, label);
11389     }
11390 
11391   mips_clear_insn_labels ();
11392 
11393   float_cons (type);
11394 }
11395 
11396 /* Handle .globl.  We need to override it because on Irix 5 you are
11397    permitted to say
11398        .globl foo .text
11399    where foo is an undefined symbol, to mean that foo should be
11400    considered to be the address of a function.  */
11401 
11402 static void
s_mips_globl(int x ATTRIBUTE_UNUSED)11403 s_mips_globl (int x ATTRIBUTE_UNUSED)
11404 {
11405   char *name;
11406   int c;
11407   symbolS *symbolP;
11408   flagword flag;
11409 
11410   name = input_line_pointer;
11411   c = get_symbol_end ();
11412   symbolP = symbol_find_or_make (name);
11413   *input_line_pointer = c;
11414   SKIP_WHITESPACE ();
11415 
11416   /* On Irix 5, every global symbol that is not explicitly labelled as
11417      being a function is apparently labelled as being an object.  */
11418   flag = BSF_OBJECT;
11419 
11420   if (! is_end_of_line[(unsigned char) *input_line_pointer])
11421     {
11422       char *secname;
11423       asection *sec;
11424 
11425       secname = input_line_pointer;
11426       c = get_symbol_end ();
11427       sec = bfd_get_section_by_name (stdoutput, secname);
11428       if (sec == NULL)
11429 	as_bad (_("%s: no such section"), secname);
11430       *input_line_pointer = c;
11431 
11432       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11433 	flag = BSF_FUNCTION;
11434     }
11435 
11436   symbol_get_bfdsym (symbolP)->flags |= flag;
11437 
11438   S_SET_EXTERNAL (symbolP);
11439   demand_empty_rest_of_line ();
11440 }
11441 
11442 static void
s_option(int x ATTRIBUTE_UNUSED)11443 s_option (int x ATTRIBUTE_UNUSED)
11444 {
11445   char *opt;
11446   char c;
11447 
11448   opt = input_line_pointer;
11449   c = get_symbol_end ();
11450 
11451   if (*opt == 'O')
11452     {
11453       /* FIXME: What does this mean?  */
11454     }
11455   else if (strncmp (opt, "pic", 3) == 0)
11456     {
11457       int i;
11458 
11459       i = atoi (opt + 3);
11460       if (i == 0)
11461 	mips_pic = NO_PIC;
11462       else if (i == 2)
11463 	{
11464 	mips_pic = SVR4_PIC;
11465 	  mips_abicalls = TRUE;
11466 	}
11467       else
11468 	as_bad (_(".option pic%d not supported"), i);
11469 
11470       if (mips_pic == SVR4_PIC)
11471 	{
11472 	  if (g_switch_seen && g_switch_value != 0)
11473 	    as_warn (_("-G may not be used with SVR4 PIC code"));
11474 	  g_switch_value = 0;
11475 	  bfd_set_gp_size (stdoutput, 0);
11476 	}
11477     }
11478   else
11479     as_warn (_("Unrecognized option \"%s\""), opt);
11480 
11481   *input_line_pointer = c;
11482   demand_empty_rest_of_line ();
11483 }
11484 
11485 /* This structure is used to hold a stack of .set values.  */
11486 
11487 struct mips_option_stack
11488 {
11489   struct mips_option_stack *next;
11490   struct mips_set_options options;
11491 };
11492 
11493 static struct mips_option_stack *mips_opts_stack;
11494 
11495 /* Handle the .set pseudo-op.  */
11496 
11497 static void
s_mipsset(int x ATTRIBUTE_UNUSED)11498 s_mipsset (int x ATTRIBUTE_UNUSED)
11499 {
11500   char *name = input_line_pointer, ch;
11501 
11502   while (!is_end_of_line[(unsigned char) *input_line_pointer])
11503     ++input_line_pointer;
11504   ch = *input_line_pointer;
11505   *input_line_pointer = '\0';
11506 
11507   if (strcmp (name, "reorder") == 0)
11508     {
11509       if (mips_opts.noreorder)
11510 	end_noreorder ();
11511     }
11512   else if (strcmp (name, "noreorder") == 0)
11513     {
11514       if (!mips_opts.noreorder)
11515 	start_noreorder ();
11516     }
11517   else if (strcmp (name, "at") == 0)
11518     {
11519       mips_opts.noat = 0;
11520     }
11521   else if (strcmp (name, "noat") == 0)
11522     {
11523       mips_opts.noat = 1;
11524     }
11525   else if (strcmp (name, "macro") == 0)
11526     {
11527       mips_opts.warn_about_macros = 0;
11528     }
11529   else if (strcmp (name, "nomacro") == 0)
11530     {
11531       if (mips_opts.noreorder == 0)
11532 	as_bad (_("`noreorder' must be set before `nomacro'"));
11533       mips_opts.warn_about_macros = 1;
11534     }
11535   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11536     {
11537       mips_opts.nomove = 0;
11538     }
11539   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11540     {
11541       mips_opts.nomove = 1;
11542     }
11543   else if (strcmp (name, "bopt") == 0)
11544     {
11545       mips_opts.nobopt = 0;
11546     }
11547   else if (strcmp (name, "nobopt") == 0)
11548     {
11549       mips_opts.nobopt = 1;
11550     }
11551   else if (strcmp (name, "mips16") == 0
11552 	   || strcmp (name, "MIPS-16") == 0)
11553     mips_opts.mips16 = 1;
11554   else if (strcmp (name, "nomips16") == 0
11555 	   || strcmp (name, "noMIPS-16") == 0)
11556     mips_opts.mips16 = 0;
11557   else if (strcmp (name, "mips3d") == 0)
11558     mips_opts.ase_mips3d = 1;
11559   else if (strcmp (name, "nomips3d") == 0)
11560     mips_opts.ase_mips3d = 0;
11561   else if (strcmp (name, "mdmx") == 0)
11562     mips_opts.ase_mdmx = 1;
11563   else if (strcmp (name, "nomdmx") == 0)
11564     mips_opts.ase_mdmx = 0;
11565   else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11566     {
11567       int reset = 0;
11568 
11569       /* Permit the user to change the ISA and architecture on the fly.
11570 	 Needless to say, misuse can cause serious problems.  */
11571       if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11572 	{
11573 	  reset = 1;
11574 	  mips_opts.isa = file_mips_isa;
11575 	  mips_opts.arch = file_mips_arch;
11576 	}
11577       else if (strncmp (name, "arch=", 5) == 0)
11578 	{
11579 	  const struct mips_cpu_info *p;
11580 
11581 	  p = mips_parse_cpu("internal use", name + 5);
11582 	  if (!p)
11583 	    as_bad (_("unknown architecture %s"), name + 5);
11584 	  else
11585 	    {
11586 	      mips_opts.arch = p->cpu;
11587 	      mips_opts.isa = p->isa;
11588 	    }
11589 	}
11590       else if (strncmp (name, "mips", 4) == 0)
11591 	{
11592 	  const struct mips_cpu_info *p;
11593 
11594 	  p = mips_parse_cpu("internal use", name);
11595 	  if (!p)
11596 	    as_bad (_("unknown ISA level %s"), name + 4);
11597 	  else
11598 	    {
11599 	      mips_opts.arch = p->cpu;
11600 	      mips_opts.isa = p->isa;
11601 	    }
11602 	}
11603       else
11604 	as_bad (_("unknown ISA or architecture %s"), name);
11605 
11606       switch (mips_opts.isa)
11607 	{
11608 	case  0:
11609 	  break;
11610 	case ISA_MIPS1:
11611 	case ISA_MIPS2:
11612 	case ISA_MIPS32:
11613 	case ISA_MIPS32R2:
11614 	  mips_opts.gp32 = 1;
11615 	  mips_opts.fp32 = 1;
11616 	  break;
11617 	case ISA_MIPS3:
11618 	case ISA_MIPS4:
11619 	case ISA_MIPS5:
11620 	case ISA_MIPS64:
11621 	case ISA_MIPS64R2:
11622 	  mips_opts.gp32 = 0;
11623 	  mips_opts.fp32 = 0;
11624 	  break;
11625 	default:
11626 	  as_bad (_("unknown ISA level %s"), name + 4);
11627 	  break;
11628 	}
11629       if (reset)
11630 	{
11631 	  mips_opts.gp32 = file_mips_gp32;
11632 	  mips_opts.fp32 = file_mips_fp32;
11633 	}
11634     }
11635   else if (strcmp (name, "autoextend") == 0)
11636     mips_opts.noautoextend = 0;
11637   else if (strcmp (name, "noautoextend") == 0)
11638     mips_opts.noautoextend = 1;
11639   else if (strcmp (name, "push") == 0)
11640     {
11641       struct mips_option_stack *s;
11642 
11643       s = (struct mips_option_stack *) xmalloc (sizeof *s);
11644       s->next = mips_opts_stack;
11645       s->options = mips_opts;
11646       mips_opts_stack = s;
11647     }
11648   else if (strcmp (name, "pop") == 0)
11649     {
11650       struct mips_option_stack *s;
11651 
11652       s = mips_opts_stack;
11653       if (s == NULL)
11654 	as_bad (_(".set pop with no .set push"));
11655       else
11656 	{
11657 	  /* If we're changing the reorder mode we need to handle
11658              delay slots correctly.  */
11659 	  if (s->options.noreorder && ! mips_opts.noreorder)
11660 	    start_noreorder ();
11661 	  else if (! s->options.noreorder && mips_opts.noreorder)
11662 	    end_noreorder ();
11663 
11664 	  mips_opts = s->options;
11665 	  mips_opts_stack = s->next;
11666 	  free (s);
11667 	}
11668     }
11669   else if (strcmp (name, "sym32") == 0)
11670     mips_opts.sym32 = TRUE;
11671   else if (strcmp (name, "nosym32") == 0)
11672     mips_opts.sym32 = FALSE;
11673   else
11674     {
11675       as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11676     }
11677   *input_line_pointer = ch;
11678   demand_empty_rest_of_line ();
11679 }
11680 
11681 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
11682    .option pic2.  It means to generate SVR4 PIC calls.  */
11683 
11684 static void
s_abicalls(int ignore ATTRIBUTE_UNUSED)11685 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11686 {
11687   mips_pic = SVR4_PIC;
11688   mips_abicalls = TRUE;
11689 
11690   if (g_switch_seen && g_switch_value != 0)
11691     as_warn (_("-G may not be used with SVR4 PIC code"));
11692   g_switch_value = 0;
11693 
11694   bfd_set_gp_size (stdoutput, 0);
11695   demand_empty_rest_of_line ();
11696 }
11697 
11698 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
11699    PIC code.  It sets the $gp register for the function based on the
11700    function address, which is in the register named in the argument.
11701    This uses a relocation against _gp_disp, which is handled specially
11702    by the linker.  The result is:
11703 	lui	$gp,%hi(_gp_disp)
11704 	addiu	$gp,$gp,%lo(_gp_disp)
11705 	addu	$gp,$gp,.cpload argument
11706    The .cpload argument is normally $25 == $t9.
11707 
11708    The -mno-shared option changes this to:
11709 	lui	$gp,%hi(__gnu_local_gp)
11710 	addiu	$gp,$gp,%lo(__gnu_local_gp)
11711    and the argument is ignored.  This saves an instruction, but the
11712    resulting code is not position independent; it uses an absolute
11713    address for __gnu_local_gp.  Thus code assembled with -mno-shared
11714    can go into an ordinary executable, but not into a shared library.  */
11715 
11716 static void
s_cpload(int ignore ATTRIBUTE_UNUSED)11717 s_cpload (int ignore ATTRIBUTE_UNUSED)
11718 {
11719   expressionS ex;
11720   int reg;
11721   int in_shared;
11722 
11723   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11724      .cpload is ignored.  */
11725   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11726     {
11727       s_ignore (0);
11728       return;
11729     }
11730 
11731   /* .cpload should be in a .set noreorder section.  */
11732   if (mips_opts.noreorder == 0)
11733     as_warn (_(".cpload not in noreorder section"));
11734 
11735   reg = tc_get_register (0);
11736 
11737   /* If we need to produce a 64-bit address, we are better off using
11738      the default instruction sequence.  */
11739   in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11740 
11741   ex.X_op = O_symbol;
11742   ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11743                                          "__gnu_local_gp");
11744   ex.X_op_symbol = NULL;
11745   ex.X_add_number = 0;
11746 
11747   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11748   symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11749 
11750   macro_start ();
11751   macro_build_lui (&ex, mips_gp_register);
11752   macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11753 	       mips_gp_register, BFD_RELOC_LO16);
11754   if (in_shared)
11755     macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11756 		 mips_gp_register, reg);
11757   macro_end ();
11758 
11759   demand_empty_rest_of_line ();
11760 }
11761 
11762 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code.  The syntax is:
11763      .cpsetup $reg1, offset|$reg2, label
11764 
11765    If offset is given, this results in:
11766      sd		$gp, offset($sp)
11767      lui	$gp, %hi(%neg(%gp_rel(label)))
11768      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
11769      daddu	$gp, $gp, $reg1
11770 
11771    If $reg2 is given, this results in:
11772      daddu	$reg2, $gp, $0
11773      lui	$gp, %hi(%neg(%gp_rel(label)))
11774      addiu	$gp, $gp, %lo(%neg(%gp_rel(label)))
11775      daddu	$gp, $gp, $reg1
11776    $reg1 is normally $25 == $t9.
11777 
11778    The -mno-shared option replaces the last three instructions with
11779 	lui	$gp,%hi(_gp)
11780 	addiu	$gp,$gp,%lo(_gp)
11781    */
11782 
11783 static void
s_cpsetup(int ignore ATTRIBUTE_UNUSED)11784 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11785 {
11786   expressionS ex_off;
11787   expressionS ex_sym;
11788   int reg1;
11789 
11790   /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11791      We also need NewABI support.  */
11792   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11793     {
11794       s_ignore (0);
11795       return;
11796     }
11797 
11798   reg1 = tc_get_register (0);
11799   SKIP_WHITESPACE ();
11800   if (*input_line_pointer != ',')
11801     {
11802       as_bad (_("missing argument separator ',' for .cpsetup"));
11803       return;
11804     }
11805   else
11806     ++input_line_pointer;
11807   SKIP_WHITESPACE ();
11808   if (*input_line_pointer == '$')
11809     {
11810       mips_cpreturn_register = tc_get_register (0);
11811       mips_cpreturn_offset = -1;
11812     }
11813   else
11814     {
11815       mips_cpreturn_offset = get_absolute_expression ();
11816       mips_cpreturn_register = -1;
11817     }
11818   SKIP_WHITESPACE ();
11819   if (*input_line_pointer != ',')
11820     {
11821       as_bad (_("missing argument separator ',' for .cpsetup"));
11822       return;
11823     }
11824   else
11825     ++input_line_pointer;
11826   SKIP_WHITESPACE ();
11827   expression (&ex_sym);
11828 
11829   macro_start ();
11830   if (mips_cpreturn_register == -1)
11831     {
11832       ex_off.X_op = O_constant;
11833       ex_off.X_add_symbol = NULL;
11834       ex_off.X_op_symbol = NULL;
11835       ex_off.X_add_number = mips_cpreturn_offset;
11836 
11837       macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11838 		   BFD_RELOC_LO16, SP);
11839     }
11840   else
11841     macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11842 		 mips_gp_register, 0);
11843 
11844   if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11845     {
11846       macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11847 		   -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11848 		   BFD_RELOC_HI16_S);
11849 
11850       macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11851 		   mips_gp_register, -1, BFD_RELOC_GPREL16,
11852 		   BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11853 
11854       macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11855 		   mips_gp_register, reg1);
11856     }
11857   else
11858     {
11859       expressionS ex;
11860 
11861       ex.X_op = O_symbol;
11862       ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
11863       ex.X_op_symbol = NULL;
11864       ex.X_add_number = 0;
11865 
11866       /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
11867       symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11868 
11869       macro_build_lui (&ex, mips_gp_register);
11870       macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11871 		   mips_gp_register, BFD_RELOC_LO16);
11872     }
11873 
11874   macro_end ();
11875 
11876   demand_empty_rest_of_line ();
11877 }
11878 
11879 static void
s_cplocal(int ignore ATTRIBUTE_UNUSED)11880 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11881 {
11882   /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11883    .cplocal is ignored.  */
11884   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11885     {
11886       s_ignore (0);
11887       return;
11888     }
11889 
11890   mips_gp_register = tc_get_register (0);
11891   demand_empty_rest_of_line ();
11892 }
11893 
11894 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
11895    offset from $sp.  The offset is remembered, and after making a PIC
11896    call $gp is restored from that location.  */
11897 
11898 static void
s_cprestore(int ignore ATTRIBUTE_UNUSED)11899 s_cprestore (int ignore ATTRIBUTE_UNUSED)
11900 {
11901   expressionS ex;
11902 
11903   /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11904      .cprestore is ignored.  */
11905   if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11906     {
11907       s_ignore (0);
11908       return;
11909     }
11910 
11911   mips_cprestore_offset = get_absolute_expression ();
11912   mips_cprestore_valid = 1;
11913 
11914   ex.X_op = O_constant;
11915   ex.X_add_symbol = NULL;
11916   ex.X_op_symbol = NULL;
11917   ex.X_add_number = mips_cprestore_offset;
11918 
11919   macro_start ();
11920   macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
11921 				SP, HAVE_64BIT_ADDRESSES);
11922   macro_end ();
11923 
11924   demand_empty_rest_of_line ();
11925 }
11926 
11927 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11928    was given in the preceding .cpsetup, it results in:
11929      ld		$gp, offset($sp)
11930 
11931    If a register $reg2 was given there, it results in:
11932      daddu	$gp, $reg2, $0
11933  */
11934 static void
s_cpreturn(int ignore ATTRIBUTE_UNUSED)11935 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
11936 {
11937   expressionS ex;
11938 
11939   /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11940      We also need NewABI support.  */
11941   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11942     {
11943       s_ignore (0);
11944       return;
11945     }
11946 
11947   macro_start ();
11948   if (mips_cpreturn_register == -1)
11949     {
11950       ex.X_op = O_constant;
11951       ex.X_add_symbol = NULL;
11952       ex.X_op_symbol = NULL;
11953       ex.X_add_number = mips_cpreturn_offset;
11954 
11955       macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
11956     }
11957   else
11958     macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
11959 		 mips_cpreturn_register, 0);
11960   macro_end ();
11961 
11962   demand_empty_rest_of_line ();
11963 }
11964 
11965 /* Handle the .gpvalue pseudo-op.  This is used when generating NewABI PIC
11966    code.  It sets the offset to use in gp_rel relocations.  */
11967 
11968 static void
s_gpvalue(int ignore ATTRIBUTE_UNUSED)11969 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
11970 {
11971   /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11972      We also need NewABI support.  */
11973   if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11974     {
11975       s_ignore (0);
11976       return;
11977     }
11978 
11979   mips_gprel_offset = get_absolute_expression ();
11980 
11981   demand_empty_rest_of_line ();
11982 }
11983 
11984 /* Handle the .gpword pseudo-op.  This is used when generating PIC
11985    code.  It generates a 32 bit GP relative reloc.  */
11986 
11987 static void
s_gpword(int ignore ATTRIBUTE_UNUSED)11988 s_gpword (int ignore ATTRIBUTE_UNUSED)
11989 {
11990   symbolS *label;
11991   expressionS ex;
11992   char *p;
11993 
11994   /* When not generating PIC code, this is treated as .word.  */
11995   if (mips_pic != SVR4_PIC)
11996     {
11997       s_cons (2);
11998       return;
11999     }
12000 
12001   label = insn_labels != NULL ? insn_labels->label : NULL;
12002   mips_emit_delays ();
12003   if (auto_align)
12004     mips_align (2, 0, label);
12005   mips_clear_insn_labels ();
12006 
12007   expression (&ex);
12008 
12009   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12010     {
12011       as_bad (_("Unsupported use of .gpword"));
12012       ignore_rest_of_line ();
12013     }
12014 
12015   p = frag_more (4);
12016   md_number_to_chars (p, 0, 4);
12017   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12018 	       BFD_RELOC_GPREL32);
12019 
12020   demand_empty_rest_of_line ();
12021 }
12022 
12023 static void
s_gpdword(int ignore ATTRIBUTE_UNUSED)12024 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12025 {
12026   symbolS *label;
12027   expressionS ex;
12028   char *p;
12029 
12030   /* When not generating PIC code, this is treated as .dword.  */
12031   if (mips_pic != SVR4_PIC)
12032     {
12033       s_cons (3);
12034       return;
12035     }
12036 
12037   label = insn_labels != NULL ? insn_labels->label : NULL;
12038   mips_emit_delays ();
12039   if (auto_align)
12040     mips_align (3, 0, label);
12041   mips_clear_insn_labels ();
12042 
12043   expression (&ex);
12044 
12045   if (ex.X_op != O_symbol || ex.X_add_number != 0)
12046     {
12047       as_bad (_("Unsupported use of .gpdword"));
12048       ignore_rest_of_line ();
12049     }
12050 
12051   p = frag_more (8);
12052   md_number_to_chars (p, 0, 8);
12053   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12054 	       BFD_RELOC_GPREL32)->fx_tcbit = 1;
12055 
12056   /* GPREL32 composed with 64 gives a 64-bit GP offset.  */
12057   fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12058 	   FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12059 
12060   demand_empty_rest_of_line ();
12061 }
12062 
12063 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
12064    tables in SVR4 PIC code.  */
12065 
12066 static void
s_cpadd(int ignore ATTRIBUTE_UNUSED)12067 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12068 {
12069   int reg;
12070 
12071   /* This is ignored when not generating SVR4 PIC code.  */
12072   if (mips_pic != SVR4_PIC)
12073     {
12074       s_ignore (0);
12075       return;
12076     }
12077 
12078   /* Add $gp to the register named as an argument.  */
12079   macro_start ();
12080   reg = tc_get_register (0);
12081   macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12082   macro_end ();
12083 
12084   demand_empty_rest_of_line ();
12085 }
12086 
12087 /* Handle the .insn pseudo-op.  This marks instruction labels in
12088    mips16 mode.  This permits the linker to handle them specially,
12089    such as generating jalx instructions when needed.  We also make
12090    them odd for the duration of the assembly, in order to generate the
12091    right sort of code.  We will make them even in the adjust_symtab
12092    routine, while leaving them marked.  This is convenient for the
12093    debugger and the disassembler.  The linker knows to make them odd
12094    again.  */
12095 
12096 static void
s_insn(int ignore ATTRIBUTE_UNUSED)12097 s_insn (int ignore ATTRIBUTE_UNUSED)
12098 {
12099   mips16_mark_labels ();
12100 
12101   demand_empty_rest_of_line ();
12102 }
12103 
12104 /* Handle a .stabn directive.  We need these in order to mark a label
12105    as being a mips16 text label correctly.  Sometimes the compiler
12106    will emit a label, followed by a .stabn, and then switch sections.
12107    If the label and .stabn are in mips16 mode, then the label is
12108    really a mips16 text label.  */
12109 
12110 static void
s_mips_stab(int type)12111 s_mips_stab (int type)
12112 {
12113   if (type == 'n')
12114     mips16_mark_labels ();
12115 
12116   s_stab (type);
12117 }
12118 
12119 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12120  */
12121 
12122 static void
s_mips_weakext(int ignore ATTRIBUTE_UNUSED)12123 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12124 {
12125   char *name;
12126   int c;
12127   symbolS *symbolP;
12128   expressionS exp;
12129 
12130   name = input_line_pointer;
12131   c = get_symbol_end ();
12132   symbolP = symbol_find_or_make (name);
12133   S_SET_WEAK (symbolP);
12134   *input_line_pointer = c;
12135 
12136   SKIP_WHITESPACE ();
12137 
12138   if (! is_end_of_line[(unsigned char) *input_line_pointer])
12139     {
12140       if (S_IS_DEFINED (symbolP))
12141 	{
12142 	  as_bad ("ignoring attempt to redefine symbol %s",
12143 		  S_GET_NAME (symbolP));
12144 	  ignore_rest_of_line ();
12145 	  return;
12146 	}
12147 
12148       if (*input_line_pointer == ',')
12149 	{
12150 	  ++input_line_pointer;
12151 	  SKIP_WHITESPACE ();
12152 	}
12153 
12154       expression (&exp);
12155       if (exp.X_op != O_symbol)
12156 	{
12157 	  as_bad ("bad .weakext directive");
12158 	  ignore_rest_of_line ();
12159 	  return;
12160 	}
12161       symbol_set_value_expression (symbolP, &exp);
12162     }
12163 
12164   demand_empty_rest_of_line ();
12165 }
12166 
12167 /* Parse a register string into a number.  Called from the ECOFF code
12168    to parse .frame.  The argument is non-zero if this is the frame
12169    register, so that we can record it in mips_frame_reg.  */
12170 
12171 int
tc_get_register(int frame)12172 tc_get_register (int frame)
12173 {
12174   int reg;
12175 
12176   SKIP_WHITESPACE ();
12177   if (*input_line_pointer++ != '$')
12178     {
12179       as_warn (_("expected `$'"));
12180       reg = ZERO;
12181     }
12182   else if (ISDIGIT (*input_line_pointer))
12183     {
12184       reg = get_absolute_expression ();
12185       if (reg < 0 || reg >= 32)
12186 	{
12187 	  as_warn (_("Bad register number"));
12188 	  reg = ZERO;
12189 	}
12190     }
12191   else
12192     {
12193       if (strncmp (input_line_pointer, "ra", 2) == 0)
12194 	{
12195 	  reg = RA;
12196 	  input_line_pointer += 2;
12197 	}
12198       else if (strncmp (input_line_pointer, "fp", 2) == 0)
12199 	{
12200 	  reg = FP;
12201 	  input_line_pointer += 2;
12202 	}
12203       else if (strncmp (input_line_pointer, "sp", 2) == 0)
12204 	{
12205 	  reg = SP;
12206 	  input_line_pointer += 2;
12207 	}
12208       else if (strncmp (input_line_pointer, "gp", 2) == 0)
12209 	{
12210 	  reg = GP;
12211 	  input_line_pointer += 2;
12212 	}
12213       else if (strncmp (input_line_pointer, "at", 2) == 0)
12214 	{
12215 	  reg = AT;
12216 	  input_line_pointer += 2;
12217 	}
12218       else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12219 	{
12220 	  reg = KT0;
12221 	  input_line_pointer += 3;
12222 	}
12223       else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12224 	{
12225 	  reg = KT1;
12226 	  input_line_pointer += 3;
12227 	}
12228       else if (strncmp (input_line_pointer, "zero", 4) == 0)
12229 	{
12230 	  reg = ZERO;
12231 	  input_line_pointer += 4;
12232 	}
12233       else
12234 	{
12235 	  as_warn (_("Unrecognized register name"));
12236 	  reg = ZERO;
12237 	  while (ISALNUM(*input_line_pointer))
12238 	   input_line_pointer++;
12239 	}
12240     }
12241   if (frame)
12242     {
12243       mips_frame_reg = reg != 0 ? reg : SP;
12244       mips_frame_reg_valid = 1;
12245       mips_cprestore_valid = 0;
12246     }
12247   return reg;
12248 }
12249 
12250 valueT
md_section_align(asection * seg,valueT addr)12251 md_section_align (asection *seg, valueT addr)
12252 {
12253   int align = bfd_get_section_alignment (stdoutput, seg);
12254 
12255 #ifdef OBJ_ELF
12256   /* We don't need to align ELF sections to the full alignment.
12257      However, Irix 5 may prefer that we align them at least to a 16
12258      byte boundary.  We don't bother to align the sections if we are
12259      targeted for an embedded system.  */
12260   if (strcmp (TARGET_OS, "elf") == 0)
12261     return addr;
12262   if (align > 4)
12263     align = 4;
12264 #endif
12265 
12266   return ((addr + (1 << align) - 1) & (-1 << align));
12267 }
12268 
12269 /* Utility routine, called from above as well.  If called while the
12270    input file is still being read, it's only an approximation.  (For
12271    example, a symbol may later become defined which appeared to be
12272    undefined earlier.)  */
12273 
12274 static int
nopic_need_relax(symbolS * sym,int before_relaxing)12275 nopic_need_relax (symbolS *sym, int before_relaxing)
12276 {
12277   if (sym == 0)
12278     return 0;
12279 
12280   if (g_switch_value > 0)
12281     {
12282       const char *symname;
12283       int change;
12284 
12285       /* Find out whether this symbol can be referenced off the $gp
12286 	 register.  It can be if it is smaller than the -G size or if
12287 	 it is in the .sdata or .sbss section.  Certain symbols can
12288 	 not be referenced off the $gp, although it appears as though
12289 	 they can.  */
12290       symname = S_GET_NAME (sym);
12291       if (symname != (const char *) NULL
12292 	  && (strcmp (symname, "eprol") == 0
12293 	      || strcmp (symname, "etext") == 0
12294 	      || strcmp (symname, "_gp") == 0
12295 	      || strcmp (symname, "edata") == 0
12296 	      || strcmp (symname, "_fbss") == 0
12297 	      || strcmp (symname, "_fdata") == 0
12298 	      || strcmp (symname, "_ftext") == 0
12299 	      || strcmp (symname, "end") == 0
12300 	      || strcmp (symname, "_gp_disp") == 0))
12301 	change = 1;
12302       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12303 	       && (0
12304 #ifndef NO_ECOFF_DEBUGGING
12305 		   || (symbol_get_obj (sym)->ecoff_extern_size != 0
12306 		       && (symbol_get_obj (sym)->ecoff_extern_size
12307 			   <= g_switch_value))
12308 #endif
12309 		   /* We must defer this decision until after the whole
12310 		      file has been read, since there might be a .extern
12311 		      after the first use of this symbol.  */
12312 		   || (before_relaxing
12313 #ifndef NO_ECOFF_DEBUGGING
12314 		       && symbol_get_obj (sym)->ecoff_extern_size == 0
12315 #endif
12316 		       && S_GET_VALUE (sym) == 0)
12317 		   || (S_GET_VALUE (sym) != 0
12318 		       && S_GET_VALUE (sym) <= g_switch_value)))
12319 	change = 0;
12320       else
12321 	{
12322 	  const char *segname;
12323 
12324 	  segname = segment_name (S_GET_SEGMENT (sym));
12325 	  assert (strcmp (segname, ".lit8") != 0
12326 		  && strcmp (segname, ".lit4") != 0);
12327 	  change = (strcmp (segname, ".sdata") != 0
12328 		    && strcmp (segname, ".sbss") != 0
12329 		    && strncmp (segname, ".sdata.", 7) != 0
12330 		    && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12331 	}
12332       return change;
12333     }
12334   else
12335     /* We are not optimizing for the $gp register.  */
12336     return 1;
12337 }
12338 
12339 
12340 /* Return true if the given symbol should be considered local for SVR4 PIC.  */
12341 
12342 static bfd_boolean
pic_need_relax(symbolS * sym,asection * segtype)12343 pic_need_relax (symbolS *sym, asection *segtype)
12344 {
12345   asection *symsec;
12346   bfd_boolean linkonce;
12347 
12348   /* Handle the case of a symbol equated to another symbol.  */
12349   while (symbol_equated_reloc_p (sym))
12350     {
12351       symbolS *n;
12352 
12353       /* It's possible to get a loop here in a badly written
12354 	 program.  */
12355       n = symbol_get_value_expression (sym)->X_add_symbol;
12356       if (n == sym)
12357 	break;
12358       sym = n;
12359     }
12360 
12361   symsec = S_GET_SEGMENT (sym);
12362 
12363   /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12364   linkonce = FALSE;
12365   if (symsec != segtype && ! S_IS_LOCAL (sym))
12366     {
12367       if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12368 	  != 0)
12369 	linkonce = TRUE;
12370 
12371       /* The GNU toolchain uses an extension for ELF: a section
12372 	 beginning with the magic string .gnu.linkonce is a linkonce
12373 	 section.  */
12374       if (strncmp (segment_name (symsec), ".gnu.linkonce",
12375 		   sizeof ".gnu.linkonce" - 1) == 0)
12376 	linkonce = TRUE;
12377     }
12378 
12379   /* This must duplicate the test in adjust_reloc_syms.  */
12380   return (symsec != &bfd_und_section
12381 	  && symsec != &bfd_abs_section
12382 	  && ! bfd_is_com_section (symsec)
12383 	  && !linkonce
12384 #ifdef OBJ_ELF
12385 	  /* A global or weak symbol is treated as external.  */
12386 	  && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12387 	      || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12388 #endif
12389 	  );
12390 }
12391 
12392 
12393 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12394    extended opcode.  SEC is the section the frag is in.  */
12395 
12396 static int
mips16_extended_frag(fragS * fragp,asection * sec,long stretch)12397 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12398 {
12399   int type;
12400   register const struct mips16_immed_operand *op;
12401   offsetT val;
12402   int mintiny, maxtiny;
12403   segT symsec;
12404   fragS *sym_frag;
12405 
12406   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12407     return 0;
12408   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12409     return 1;
12410 
12411   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12412   op = mips16_immed_operands;
12413   while (op->type != type)
12414     {
12415       ++op;
12416       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12417     }
12418 
12419   if (op->unsp)
12420     {
12421       if (type == '<' || type == '>' || type == '[' || type == ']')
12422 	{
12423 	  mintiny = 1;
12424 	  maxtiny = 1 << op->nbits;
12425 	}
12426       else
12427 	{
12428 	  mintiny = 0;
12429 	  maxtiny = (1 << op->nbits) - 1;
12430 	}
12431     }
12432   else
12433     {
12434       mintiny = - (1 << (op->nbits - 1));
12435       maxtiny = (1 << (op->nbits - 1)) - 1;
12436     }
12437 
12438   sym_frag = symbol_get_frag (fragp->fr_symbol);
12439   val = S_GET_VALUE (fragp->fr_symbol);
12440   symsec = S_GET_SEGMENT (fragp->fr_symbol);
12441 
12442   if (op->pcrel)
12443     {
12444       addressT addr;
12445 
12446       /* We won't have the section when we are called from
12447          mips_relax_frag.  However, we will always have been called
12448          from md_estimate_size_before_relax first.  If this is a
12449          branch to a different section, we mark it as such.  If SEC is
12450          NULL, and the frag is not marked, then it must be a branch to
12451          the same section.  */
12452       if (sec == NULL)
12453 	{
12454 	  if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12455 	    return 1;
12456 	}
12457       else
12458 	{
12459 	  /* Must have been called from md_estimate_size_before_relax.  */
12460 	  if (symsec != sec)
12461 	    {
12462 	      fragp->fr_subtype =
12463 		RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12464 
12465 	      /* FIXME: We should support this, and let the linker
12466                  catch branches and loads that are out of range.  */
12467 	      as_bad_where (fragp->fr_file, fragp->fr_line,
12468 			    _("unsupported PC relative reference to different section"));
12469 
12470 	      return 1;
12471 	    }
12472 	  if (fragp != sym_frag && sym_frag->fr_address == 0)
12473 	    /* Assume non-extended on the first relaxation pass.
12474 	       The address we have calculated will be bogus if this is
12475 	       a forward branch to another frag, as the forward frag
12476 	       will have fr_address == 0.  */
12477 	    return 0;
12478 	}
12479 
12480       /* In this case, we know for sure that the symbol fragment is in
12481 	 the same section.  If the relax_marker of the symbol fragment
12482 	 differs from the relax_marker of this fragment, we have not
12483 	 yet adjusted the symbol fragment fr_address.  We want to add
12484 	 in STRETCH in order to get a better estimate of the address.
12485 	 This particularly matters because of the shift bits.  */
12486       if (stretch != 0
12487 	  && sym_frag->relax_marker != fragp->relax_marker)
12488 	{
12489 	  fragS *f;
12490 
12491 	  /* Adjust stretch for any alignment frag.  Note that if have
12492              been expanding the earlier code, the symbol may be
12493              defined in what appears to be an earlier frag.  FIXME:
12494              This doesn't handle the fr_subtype field, which specifies
12495              a maximum number of bytes to skip when doing an
12496              alignment.  */
12497 	  for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12498 	    {
12499 	      if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12500 		{
12501 		  if (stretch < 0)
12502 		    stretch = - ((- stretch)
12503 				 & ~ ((1 << (int) f->fr_offset) - 1));
12504 		  else
12505 		    stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12506 		  if (stretch == 0)
12507 		    break;
12508 		}
12509 	    }
12510 	  if (f != NULL)
12511 	    val += stretch;
12512 	}
12513 
12514       addr = fragp->fr_address + fragp->fr_fix;
12515 
12516       /* The base address rules are complicated.  The base address of
12517          a branch is the following instruction.  The base address of a
12518          PC relative load or add is the instruction itself, but if it
12519          is in a delay slot (in which case it can not be extended) use
12520          the address of the instruction whose delay slot it is in.  */
12521       if (type == 'p' || type == 'q')
12522 	{
12523 	  addr += 2;
12524 
12525 	  /* If we are currently assuming that this frag should be
12526 	     extended, then, the current address is two bytes
12527 	     higher.  */
12528 	  if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12529 	    addr += 2;
12530 
12531 	  /* Ignore the low bit in the target, since it will be set
12532              for a text label.  */
12533 	  if ((val & 1) != 0)
12534 	    --val;
12535 	}
12536       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12537 	addr -= 4;
12538       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12539 	addr -= 2;
12540 
12541       val -= addr & ~ ((1 << op->shift) - 1);
12542 
12543       /* Branch offsets have an implicit 0 in the lowest bit.  */
12544       if (type == 'p' || type == 'q')
12545 	val /= 2;
12546 
12547       /* If any of the shifted bits are set, we must use an extended
12548          opcode.  If the address depends on the size of this
12549          instruction, this can lead to a loop, so we arrange to always
12550          use an extended opcode.  We only check this when we are in
12551          the main relaxation loop, when SEC is NULL.  */
12552       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12553 	{
12554 	  fragp->fr_subtype =
12555 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12556 	  return 1;
12557 	}
12558 
12559       /* If we are about to mark a frag as extended because the value
12560          is precisely maxtiny + 1, then there is a chance of an
12561          infinite loop as in the following code:
12562 	     la	$4,foo
12563 	     .skip	1020
12564 	     .align	2
12565 	   foo:
12566 	 In this case when the la is extended, foo is 0x3fc bytes
12567 	 away, so the la can be shrunk, but then foo is 0x400 away, so
12568 	 the la must be extended.  To avoid this loop, we mark the
12569 	 frag as extended if it was small, and is about to become
12570 	 extended with a value of maxtiny + 1.  */
12571       if (val == ((maxtiny + 1) << op->shift)
12572 	  && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12573 	  && sec == NULL)
12574 	{
12575 	  fragp->fr_subtype =
12576 	    RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12577 	  return 1;
12578 	}
12579     }
12580   else if (symsec != absolute_section && sec != NULL)
12581     as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12582 
12583   if ((val & ((1 << op->shift) - 1)) != 0
12584       || val < (mintiny << op->shift)
12585       || val > (maxtiny << op->shift))
12586     return 1;
12587   else
12588     return 0;
12589 }
12590 
12591 /* Compute the length of a branch sequence, and adjust the
12592    RELAX_BRANCH_TOOFAR bit accordingly.  If FRAGP is NULL, the
12593    worst-case length is computed, with UPDATE being used to indicate
12594    whether an unconditional (-1), branch-likely (+1) or regular (0)
12595    branch is to be computed.  */
12596 static int
relaxed_branch_length(fragS * fragp,asection * sec,int update)12597 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12598 {
12599   bfd_boolean toofar;
12600   int length;
12601 
12602   if (fragp
12603       && S_IS_DEFINED (fragp->fr_symbol)
12604       && sec == S_GET_SEGMENT (fragp->fr_symbol))
12605     {
12606       addressT addr;
12607       offsetT val;
12608 
12609       val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12610 
12611       addr = fragp->fr_address + fragp->fr_fix + 4;
12612 
12613       val -= addr;
12614 
12615       toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12616     }
12617   else if (fragp)
12618     /* If the symbol is not defined or it's in a different segment,
12619        assume the user knows what's going on and emit a short
12620        branch.  */
12621     toofar = FALSE;
12622   else
12623     toofar = TRUE;
12624 
12625   if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12626     fragp->fr_subtype
12627       = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12628 			     RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12629 			     RELAX_BRANCH_LINK (fragp->fr_subtype),
12630 			     toofar);
12631 
12632   length = 4;
12633   if (toofar)
12634     {
12635       if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12636 	length += 8;
12637 
12638       if (mips_pic != NO_PIC)
12639 	{
12640 	  /* Additional space for PIC loading of target address.  */
12641 	  length += 8;
12642 	  if (mips_opts.isa == ISA_MIPS1)
12643 	    /* Additional space for $at-stabilizing nop.  */
12644 	    length += 4;
12645 	}
12646 
12647       /* If branch is conditional.  */
12648       if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12649 	length += 8;
12650     }
12651 
12652   return length;
12653 }
12654 
12655 /* Estimate the size of a frag before relaxing.  Unless this is the
12656    mips16, we are not really relaxing here, and the final size is
12657    encoded in the subtype information.  For the mips16, we have to
12658    decide whether we are using an extended opcode or not.  */
12659 
12660 int
md_estimate_size_before_relax(fragS * fragp,asection * segtype)12661 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12662 {
12663   int change;
12664 
12665   if (RELAX_BRANCH_P (fragp->fr_subtype))
12666     {
12667 
12668       fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12669 
12670       return fragp->fr_var;
12671     }
12672 
12673   if (RELAX_MIPS16_P (fragp->fr_subtype))
12674     /* We don't want to modify the EXTENDED bit here; it might get us
12675        into infinite loops.  We change it only in mips_relax_frag().  */
12676     return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12677 
12678   if (mips_pic == NO_PIC)
12679     change = nopic_need_relax (fragp->fr_symbol, 0);
12680   else if (mips_pic == SVR4_PIC)
12681     change = pic_need_relax (fragp->fr_symbol, segtype);
12682   else
12683     abort ();
12684 
12685   if (change)
12686     {
12687       fragp->fr_subtype |= RELAX_USE_SECOND;
12688       return -RELAX_FIRST (fragp->fr_subtype);
12689     }
12690   else
12691     return -RELAX_SECOND (fragp->fr_subtype);
12692 }
12693 
12694 /* This is called to see whether a reloc against a defined symbol
12695    should be converted into a reloc against a section.  */
12696 
12697 int
mips_fix_adjustable(fixS * fixp)12698 mips_fix_adjustable (fixS *fixp)
12699 {
12700   /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12701      about the format of the offset in the .o file. */
12702   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12703     return 0;
12704 
12705   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12706       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12707     return 0;
12708 
12709   if (fixp->fx_addsy == NULL)
12710     return 1;
12711 
12712   /* If symbol SYM is in a mergeable section, relocations of the form
12713      SYM + 0 can usually be made section-relative.  The mergeable data
12714      is then identified by the section offset rather than by the symbol.
12715 
12716      However, if we're generating REL LO16 relocations, the offset is split
12717      between the LO16 and parterning high part relocation.  The linker will
12718      need to recalculate the complete offset in order to correctly identify
12719      the merge data.
12720 
12721      The linker has traditionally not looked for the parterning high part
12722      relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12723      placed anywhere.  Rather than break backwards compatibility by changing
12724      this, it seems better not to force the issue, and instead keep the
12725      original symbol.  This will work with either linker behavior.  */
12726   if ((fixp->fx_r_type == BFD_RELOC_LO16
12727        || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
12728        || reloc_needs_lo_p (fixp->fx_r_type))
12729       && HAVE_IN_PLACE_ADDENDS
12730       && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12731     return 0;
12732 
12733 #ifdef OBJ_ELF
12734   /* Don't adjust relocations against mips16 symbols, so that the linker
12735      can find them if it needs to set up a stub.  */
12736   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12737       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12738       && fixp->fx_subsy == NULL)
12739     return 0;
12740 #endif
12741 
12742   return 1;
12743 }
12744 
12745 /* Translate internal representation of relocation info to BFD target
12746    format.  */
12747 
12748 arelent **
tc_gen_reloc(asection * section ATTRIBUTE_UNUSED,fixS * fixp)12749 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12750 {
12751   static arelent *retval[4];
12752   arelent *reloc;
12753   bfd_reloc_code_real_type code;
12754 
12755   memset (retval, 0, sizeof(retval));
12756   reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12757   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12758   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12759   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12760 
12761   assert (! fixp->fx_pcrel);
12762   reloc->addend = fixp->fx_addnumber;
12763 
12764   /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12765      entry to be used in the relocation's section offset.  */
12766   if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12767     {
12768       reloc->address = reloc->addend;
12769       reloc->addend = 0;
12770     }
12771 
12772   code = fixp->fx_r_type;
12773 
12774   /* To support a PC relative reloc, we used a Cygnus extension.
12775      We check for that here to make sure that we don't let such a
12776      reloc escape normally.  (FIXME: This was formerly used by
12777      embedded-PIC support, but is now used by branch handling in
12778      general.  That probably should be fixed.)  */
12779   if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12780        || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12781       && code == BFD_RELOC_16_PCREL_S2)
12782     reloc->howto = NULL;
12783   else
12784     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12785 
12786   if (reloc->howto == NULL)
12787     {
12788       as_bad_where (fixp->fx_file, fixp->fx_line,
12789 		    _("Can not represent %s relocation in this object file format"),
12790 		    bfd_get_reloc_code_name (code));
12791       retval[0] = NULL;
12792     }
12793 
12794   return retval;
12795 }
12796 
12797 /* Relax a machine dependent frag.  This returns the amount by which
12798    the current size of the frag should change.  */
12799 
12800 int
mips_relax_frag(asection * sec,fragS * fragp,long stretch)12801 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12802 {
12803   if (RELAX_BRANCH_P (fragp->fr_subtype))
12804     {
12805       offsetT old_var = fragp->fr_var;
12806 
12807       fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12808 
12809       return fragp->fr_var - old_var;
12810     }
12811 
12812   if (! RELAX_MIPS16_P (fragp->fr_subtype))
12813     return 0;
12814 
12815   if (mips16_extended_frag (fragp, NULL, stretch))
12816     {
12817       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12818 	return 0;
12819       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12820       return 2;
12821     }
12822   else
12823     {
12824       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12825 	return 0;
12826       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12827       return -2;
12828     }
12829 
12830   return 0;
12831 }
12832 
12833 /* Convert a machine dependent frag.  */
12834 
12835 void
md_convert_frag(bfd * abfd ATTRIBUTE_UNUSED,segT asec,fragS * fragp)12836 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12837 {
12838   if (RELAX_BRANCH_P (fragp->fr_subtype))
12839     {
12840       bfd_byte *buf;
12841       unsigned long insn;
12842       expressionS exp;
12843       fixS *fixp;
12844 
12845       buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12846 
12847       if (target_big_endian)
12848 	insn = bfd_getb32 (buf);
12849       else
12850 	insn = bfd_getl32 (buf);
12851 
12852       if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12853 	{
12854 	  /* We generate a fixup instead of applying it right now
12855 	     because, if there are linker relaxations, we're going to
12856 	     need the relocations.  */
12857 	  exp.X_op = O_symbol;
12858 	  exp.X_add_symbol = fragp->fr_symbol;
12859 	  exp.X_add_number = fragp->fr_offset;
12860 
12861 	  fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12862 			      4, &exp, 1,
12863 			      BFD_RELOC_16_PCREL_S2);
12864 	  fixp->fx_file = fragp->fr_file;
12865 	  fixp->fx_line = fragp->fr_line;
12866 
12867 	  md_number_to_chars ((char *) buf, insn, 4);
12868 	  buf += 4;
12869 	}
12870       else
12871 	{
12872 	  int i;
12873 
12874 	  as_warn_where (fragp->fr_file, fragp->fr_line,
12875 			 _("relaxed out-of-range branch into a jump"));
12876 
12877 	  if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12878 	    goto uncond;
12879 
12880 	  if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12881 	    {
12882 	      /* Reverse the branch.  */
12883 	      switch ((insn >> 28) & 0xf)
12884 		{
12885 		case 4:
12886 		  /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12887 		     have the condition reversed by tweaking a single
12888 		     bit, and their opcodes all have 0x4???????.  */
12889 		  assert ((insn & 0xf1000000) == 0x41000000);
12890 		  insn ^= 0x00010000;
12891 		  break;
12892 
12893 		case 0:
12894 		  /* bltz	0x04000000	bgez	0x04010000
12895 		     bltzal	0x04100000	bgezal	0x04110000 */
12896 		  assert ((insn & 0xfc0e0000) == 0x04000000);
12897 		  insn ^= 0x00010000;
12898 		  break;
12899 
12900 		case 1:
12901 		  /* beq	0x10000000	bne	0x14000000
12902 		     blez	0x18000000	bgtz	0x1c000000 */
12903 		  insn ^= 0x04000000;
12904 		  break;
12905 
12906 		default:
12907 		  abort ();
12908 		}
12909 	    }
12910 
12911 	  if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12912 	    {
12913 	      /* Clear the and-link bit.  */
12914 	      assert ((insn & 0xfc1c0000) == 0x04100000);
12915 
12916 	      /* bltzal	0x04100000	bgezal	0x04110000
12917 		bltzall	0x04120000     bgezall	0x04130000 */
12918 	      insn &= ~0x00100000;
12919 	    }
12920 
12921 	  /* Branch over the branch (if the branch was likely) or the
12922 	     full jump (not likely case).  Compute the offset from the
12923 	     current instruction to branch to.  */
12924 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12925 	    i = 16;
12926 	  else
12927 	    {
12928 	      /* How many bytes in instructions we've already emitted?  */
12929 	      i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12930 	      /* How many bytes in instructions from here to the end?  */
12931 	      i = fragp->fr_var - i;
12932 	    }
12933 	  /* Convert to instruction count.  */
12934 	  i >>= 2;
12935 	  /* Branch counts from the next instruction.  */
12936 	  i--;
12937 	  insn |= i;
12938 	  /* Branch over the jump.  */
12939 	  md_number_to_chars ((char *) buf, insn, 4);
12940 	  buf += 4;
12941 
12942 	  /* Nop */
12943 	  md_number_to_chars ((char *) buf, 0, 4);
12944 	  buf += 4;
12945 
12946 	  if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12947 	    {
12948 	      /* beql $0, $0, 2f */
12949 	      insn = 0x50000000;
12950 	      /* Compute the PC offset from the current instruction to
12951 		 the end of the variable frag.  */
12952 	      /* How many bytes in instructions we've already emitted?  */
12953 	      i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12954 	      /* How many bytes in instructions from here to the end?  */
12955 	      i = fragp->fr_var - i;
12956 	      /* Convert to instruction count.  */
12957 	      i >>= 2;
12958 	      /* Don't decrement i, because we want to branch over the
12959 		 delay slot.  */
12960 
12961 	      insn |= i;
12962 	      md_number_to_chars ((char *) buf, insn, 4);
12963 	      buf += 4;
12964 
12965 	      md_number_to_chars ((char *) buf, 0, 4);
12966 	      buf += 4;
12967 	    }
12968 
12969 	uncond:
12970 	  if (mips_pic == NO_PIC)
12971 	    {
12972 	      /* j or jal.  */
12973 	      insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
12974 		      ? 0x0c000000 : 0x08000000);
12975 	      exp.X_op = O_symbol;
12976 	      exp.X_add_symbol = fragp->fr_symbol;
12977 	      exp.X_add_number = fragp->fr_offset;
12978 
12979 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12980 				  4, &exp, 0, BFD_RELOC_MIPS_JMP);
12981 	      fixp->fx_file = fragp->fr_file;
12982 	      fixp->fx_line = fragp->fr_line;
12983 
12984 	      md_number_to_chars ((char *) buf, insn, 4);
12985 	      buf += 4;
12986 	    }
12987 	  else
12988 	    {
12989 	      /* lw/ld $at, <sym>($gp)  R_MIPS_GOT16 */
12990 	      insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
12991 	      exp.X_op = O_symbol;
12992 	      exp.X_add_symbol = fragp->fr_symbol;
12993 	      exp.X_add_number = fragp->fr_offset;
12994 
12995 	      if (fragp->fr_offset)
12996 		{
12997 		  exp.X_add_symbol = make_expr_symbol (&exp);
12998 		  exp.X_add_number = 0;
12999 		}
13000 
13001 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13002 				  4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13003 	      fixp->fx_file = fragp->fr_file;
13004 	      fixp->fx_line = fragp->fr_line;
13005 
13006 	      md_number_to_chars ((char *) buf, insn, 4);
13007 	      buf += 4;
13008 
13009 	      if (mips_opts.isa == ISA_MIPS1)
13010 		{
13011 		  /* nop */
13012 		  md_number_to_chars ((char *) buf, 0, 4);
13013 		  buf += 4;
13014 		}
13015 
13016 	      /* d/addiu $at, $at, <sym>  R_MIPS_LO16 */
13017 	      insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13018 
13019 	      fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13020 				  4, &exp, 0, BFD_RELOC_LO16);
13021 	      fixp->fx_file = fragp->fr_file;
13022 	      fixp->fx_line = fragp->fr_line;
13023 
13024 	      md_number_to_chars ((char *) buf, insn, 4);
13025 	      buf += 4;
13026 
13027 	      /* j(al)r $at.  */
13028 	      if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13029 		insn = 0x0020f809;
13030 	      else
13031 		insn = 0x00200008;
13032 
13033 	      md_number_to_chars ((char *) buf, insn, 4);
13034 	      buf += 4;
13035 	    }
13036 	}
13037 
13038       assert (buf == (bfd_byte *)fragp->fr_literal
13039 	      + fragp->fr_fix + fragp->fr_var);
13040 
13041       fragp->fr_fix += fragp->fr_var;
13042 
13043       return;
13044     }
13045 
13046   if (RELAX_MIPS16_P (fragp->fr_subtype))
13047     {
13048       int type;
13049       register const struct mips16_immed_operand *op;
13050       bfd_boolean small, ext;
13051       offsetT val;
13052       bfd_byte *buf;
13053       unsigned long insn;
13054       bfd_boolean use_extend;
13055       unsigned short extend;
13056 
13057       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13058       op = mips16_immed_operands;
13059       while (op->type != type)
13060 	++op;
13061 
13062       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13063 	{
13064 	  small = FALSE;
13065 	  ext = TRUE;
13066 	}
13067       else
13068 	{
13069 	  small = TRUE;
13070 	  ext = FALSE;
13071 	}
13072 
13073       resolve_symbol_value (fragp->fr_symbol);
13074       val = S_GET_VALUE (fragp->fr_symbol);
13075       if (op->pcrel)
13076 	{
13077 	  addressT addr;
13078 
13079 	  addr = fragp->fr_address + fragp->fr_fix;
13080 
13081 	  /* The rules for the base address of a PC relative reloc are
13082              complicated; see mips16_extended_frag.  */
13083 	  if (type == 'p' || type == 'q')
13084 	    {
13085 	      addr += 2;
13086 	      if (ext)
13087 		addr += 2;
13088 	      /* Ignore the low bit in the target, since it will be
13089                  set for a text label.  */
13090 	      if ((val & 1) != 0)
13091 		--val;
13092 	    }
13093 	  else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13094 	    addr -= 4;
13095 	  else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13096 	    addr -= 2;
13097 
13098 	  addr &= ~ (addressT) ((1 << op->shift) - 1);
13099 	  val -= addr;
13100 
13101 	  /* Make sure the section winds up with the alignment we have
13102              assumed.  */
13103 	  if (op->shift > 0)
13104 	    record_alignment (asec, op->shift);
13105 	}
13106 
13107       if (ext
13108 	  && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13109 	      || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13110 	as_warn_where (fragp->fr_file, fragp->fr_line,
13111 		       _("extended instruction in delay slot"));
13112 
13113       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13114 
13115       if (target_big_endian)
13116 	insn = bfd_getb16 (buf);
13117       else
13118 	insn = bfd_getl16 (buf);
13119 
13120       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13121 		    RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13122 		    small, ext, &insn, &use_extend, &extend);
13123 
13124       if (use_extend)
13125 	{
13126 	  md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13127 	  fragp->fr_fix += 2;
13128 	  buf += 2;
13129 	}
13130 
13131       md_number_to_chars ((char *) buf, insn, 2);
13132       fragp->fr_fix += 2;
13133       buf += 2;
13134     }
13135   else
13136     {
13137       int first, second;
13138       fixS *fixp;
13139 
13140       first = RELAX_FIRST (fragp->fr_subtype);
13141       second = RELAX_SECOND (fragp->fr_subtype);
13142       fixp = (fixS *) fragp->fr_opcode;
13143 
13144       /* Possibly emit a warning if we've chosen the longer option.  */
13145       if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13146 	  == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13147 	{
13148 	  const char *msg = macro_warning (fragp->fr_subtype);
13149 	  if (msg != 0)
13150 	    as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13151 	}
13152 
13153       /* Go through all the fixups for the first sequence.  Disable them
13154 	 (by marking them as done) if we're going to use the second
13155 	 sequence instead.  */
13156       while (fixp
13157 	     && fixp->fx_frag == fragp
13158 	     && fixp->fx_where < fragp->fr_fix - second)
13159 	{
13160 	  if (fragp->fr_subtype & RELAX_USE_SECOND)
13161 	    fixp->fx_done = 1;
13162 	  fixp = fixp->fx_next;
13163 	}
13164 
13165       /* Go through the fixups for the second sequence.  Disable them if
13166 	 we're going to use the first sequence, otherwise adjust their
13167 	 addresses to account for the relaxation.  */
13168       while (fixp && fixp->fx_frag == fragp)
13169 	{
13170 	  if (fragp->fr_subtype & RELAX_USE_SECOND)
13171 	    fixp->fx_where -= first;
13172 	  else
13173 	    fixp->fx_done = 1;
13174 	  fixp = fixp->fx_next;
13175 	}
13176 
13177       /* Now modify the frag contents.  */
13178       if (fragp->fr_subtype & RELAX_USE_SECOND)
13179 	{
13180 	  char *start;
13181 
13182 	  start = fragp->fr_literal + fragp->fr_fix - first - second;
13183 	  memmove (start, start + first, second);
13184 	  fragp->fr_fix -= first;
13185 	}
13186       else
13187 	fragp->fr_fix -= second;
13188     }
13189 }
13190 
13191 #ifdef OBJ_ELF
13192 
13193 /* This function is called after the relocs have been generated.
13194    We've been storing mips16 text labels as odd.  Here we convert them
13195    back to even for the convenience of the debugger.  */
13196 
13197 void
mips_frob_file_after_relocs(void)13198 mips_frob_file_after_relocs (void)
13199 {
13200   asymbol **syms;
13201   unsigned int count, i;
13202 
13203   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13204     return;
13205 
13206   syms = bfd_get_outsymbols (stdoutput);
13207   count = bfd_get_symcount (stdoutput);
13208   for (i = 0; i < count; i++, syms++)
13209     {
13210       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13211 	  && ((*syms)->value & 1) != 0)
13212 	{
13213 	  (*syms)->value &= ~1;
13214 	  /* If the symbol has an odd size, it was probably computed
13215 	     incorrectly, so adjust that as well.  */
13216 	  if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13217 	    ++elf_symbol (*syms)->internal_elf_sym.st_size;
13218 	}
13219     }
13220 }
13221 
13222 #endif
13223 
13224 /* This function is called whenever a label is defined.  It is used
13225    when handling branch delays; if a branch has a label, we assume we
13226    can not move it.  */
13227 
13228 void
mips_define_label(symbolS * sym)13229 mips_define_label (symbolS *sym)
13230 {
13231   struct insn_label_list *l;
13232 
13233   if (free_insn_labels == NULL)
13234     l = (struct insn_label_list *) xmalloc (sizeof *l);
13235   else
13236     {
13237       l = free_insn_labels;
13238       free_insn_labels = l->next;
13239     }
13240 
13241   l->label = sym;
13242   l->next = insn_labels;
13243   insn_labels = l;
13244 }
13245 
13246 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13247 
13248 /* Some special processing for a MIPS ELF file.  */
13249 
13250 void
mips_elf_final_processing(void)13251 mips_elf_final_processing (void)
13252 {
13253   /* Write out the register information.  */
13254   if (mips_abi != N64_ABI)
13255     {
13256       Elf32_RegInfo s;
13257 
13258       s.ri_gprmask = mips_gprmask;
13259       s.ri_cprmask[0] = mips_cprmask[0];
13260       s.ri_cprmask[1] = mips_cprmask[1];
13261       s.ri_cprmask[2] = mips_cprmask[2];
13262       s.ri_cprmask[3] = mips_cprmask[3];
13263       /* The gp_value field is set by the MIPS ELF backend.  */
13264 
13265       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13266 				       ((Elf32_External_RegInfo *)
13267 					mips_regmask_frag));
13268     }
13269   else
13270     {
13271       Elf64_Internal_RegInfo s;
13272 
13273       s.ri_gprmask = mips_gprmask;
13274       s.ri_pad = 0;
13275       s.ri_cprmask[0] = mips_cprmask[0];
13276       s.ri_cprmask[1] = mips_cprmask[1];
13277       s.ri_cprmask[2] = mips_cprmask[2];
13278       s.ri_cprmask[3] = mips_cprmask[3];
13279       /* The gp_value field is set by the MIPS ELF backend.  */
13280 
13281       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13282 				       ((Elf64_External_RegInfo *)
13283 					mips_regmask_frag));
13284     }
13285 
13286   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
13287      sort of BFD interface for this.  */
13288   if (mips_any_noreorder)
13289     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13290   if (mips_pic != NO_PIC)
13291     {
13292     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13293       elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13294     }
13295   if (mips_abicalls)
13296     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13297 
13298   /* Set MIPS ELF flags for ASEs.  */
13299   if (file_ase_mips16)
13300     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13301 #if 0 /* XXX FIXME */
13302   if (file_ase_mips3d)
13303     elf_elfheader (stdoutput)->e_flags |= ???;
13304 #endif
13305   if (file_ase_mdmx)
13306     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13307 
13308   /* Set the MIPS ELF ABI flags.  */
13309   if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13310     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13311   else if (mips_abi == O64_ABI)
13312     elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13313   else if (mips_abi == EABI_ABI)
13314     {
13315       if (!file_mips_gp32)
13316 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13317       else
13318 	elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13319     }
13320   else if (mips_abi == N32_ABI)
13321     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13322 
13323   /* Nothing to do for N64_ABI.  */
13324 
13325   if (mips_32bitmode)
13326     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13327 }
13328 
13329 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13330 
13331 typedef struct proc {
13332   symbolS *func_sym;
13333   symbolS *func_end_sym;
13334   unsigned long reg_mask;
13335   unsigned long reg_offset;
13336   unsigned long fpreg_mask;
13337   unsigned long fpreg_offset;
13338   unsigned long frame_offset;
13339   unsigned long frame_reg;
13340   unsigned long pc_reg;
13341 } procS;
13342 
13343 static procS cur_proc;
13344 static procS *cur_proc_ptr;
13345 static int numprocs;
13346 
13347 /* Fill in an rs_align_code fragment.  */
13348 
13349 void
mips_handle_align(fragS * fragp)13350 mips_handle_align (fragS *fragp)
13351 {
13352   if (fragp->fr_type != rs_align_code)
13353     return;
13354 
13355   if (mips_opts.mips16)
13356     {
13357       static const unsigned char be_nop[] = { 0x65, 0x00 };
13358       static const unsigned char le_nop[] = { 0x00, 0x65 };
13359 
13360       int bytes;
13361       char *p;
13362 
13363       bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13364       p = fragp->fr_literal + fragp->fr_fix;
13365 
13366       if (bytes & 1)
13367 	{
13368 	  *p++ = 0;
13369 	  fragp->fr_fix++;
13370 	}
13371 
13372       memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13373       fragp->fr_var = 2;
13374     }
13375 
13376   /* For mips32, a nop is a zero, which we trivially get by doing nothing.  */
13377 }
13378 
13379 static void
md_obj_begin(void)13380 md_obj_begin (void)
13381 {
13382 }
13383 
13384 static void
md_obj_end(void)13385 md_obj_end (void)
13386 {
13387   /* check for premature end, nesting errors, etc */
13388   if (cur_proc_ptr)
13389     as_warn (_("missing .end at end of assembly"));
13390 }
13391 
13392 static long
get_number(void)13393 get_number (void)
13394 {
13395   int negative = 0;
13396   long val = 0;
13397 
13398   if (*input_line_pointer == '-')
13399     {
13400       ++input_line_pointer;
13401       negative = 1;
13402     }
13403   if (!ISDIGIT (*input_line_pointer))
13404     as_bad (_("expected simple number"));
13405   if (input_line_pointer[0] == '0')
13406     {
13407       if (input_line_pointer[1] == 'x')
13408 	{
13409 	  input_line_pointer += 2;
13410 	  while (ISXDIGIT (*input_line_pointer))
13411 	    {
13412 	      val <<= 4;
13413 	      val |= hex_value (*input_line_pointer++);
13414 	    }
13415 	  return negative ? -val : val;
13416 	}
13417       else
13418 	{
13419 	  ++input_line_pointer;
13420 	  while (ISDIGIT (*input_line_pointer))
13421 	    {
13422 	      val <<= 3;
13423 	      val |= *input_line_pointer++ - '0';
13424 	    }
13425 	  return negative ? -val : val;
13426 	}
13427     }
13428   if (!ISDIGIT (*input_line_pointer))
13429     {
13430       printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13431 	      *input_line_pointer, *input_line_pointer);
13432       as_warn (_("invalid number"));
13433       return -1;
13434     }
13435   while (ISDIGIT (*input_line_pointer))
13436     {
13437       val *= 10;
13438       val += *input_line_pointer++ - '0';
13439     }
13440   return negative ? -val : val;
13441 }
13442 
13443 /* The .file directive; just like the usual .file directive, but there
13444    is an initial number which is the ECOFF file index.  In the non-ECOFF
13445    case .file implies DWARF-2.  */
13446 
13447 static void
s_mips_file(int x ATTRIBUTE_UNUSED)13448 s_mips_file (int x ATTRIBUTE_UNUSED)
13449 {
13450   static int first_file_directive = 0;
13451 
13452   if (ECOFF_DEBUGGING)
13453     {
13454       get_number ();
13455       s_app_file (0);
13456     }
13457   else
13458     {
13459       char *filename;
13460 
13461       filename = dwarf2_directive_file (0);
13462 
13463       /* Versions of GCC up to 3.1 start files with a ".file"
13464 	 directive even for stabs output.  Make sure that this
13465 	 ".file" is handled.  Note that you need a version of GCC
13466          after 3.1 in order to support DWARF-2 on MIPS.  */
13467       if (filename != NULL && ! first_file_directive)
13468 	{
13469 	  (void) new_logical_line (filename, -1);
13470 	  s_app_file_string (filename, 0);
13471 	}
13472       first_file_directive = 1;
13473     }
13474 }
13475 
13476 /* The .loc directive, implying DWARF-2.  */
13477 
13478 static void
s_mips_loc(int x ATTRIBUTE_UNUSED)13479 s_mips_loc (int x ATTRIBUTE_UNUSED)
13480 {
13481   if (!ECOFF_DEBUGGING)
13482     dwarf2_directive_loc (0);
13483 }
13484 
13485 /* The .end directive.  */
13486 
13487 static void
s_mips_end(int x ATTRIBUTE_UNUSED)13488 s_mips_end (int x ATTRIBUTE_UNUSED)
13489 {
13490   symbolS *p;
13491 
13492   /* Following functions need their own .frame and .cprestore directives.  */
13493   mips_frame_reg_valid = 0;
13494   mips_cprestore_valid = 0;
13495 
13496   if (!is_end_of_line[(unsigned char) *input_line_pointer])
13497     {
13498       p = get_symbol ();
13499       demand_empty_rest_of_line ();
13500     }
13501   else
13502     p = NULL;
13503 
13504   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13505     as_warn (_(".end not in text section"));
13506 
13507   if (!cur_proc_ptr)
13508     {
13509       as_warn (_(".end directive without a preceding .ent directive."));
13510       demand_empty_rest_of_line ();
13511       return;
13512     }
13513 
13514   if (p != NULL)
13515     {
13516       assert (S_GET_NAME (p));
13517       if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13518 	as_warn (_(".end symbol does not match .ent symbol."));
13519 
13520       if (debug_type == DEBUG_STABS)
13521 	stabs_generate_asm_endfunc (S_GET_NAME (p),
13522 				    S_GET_NAME (p));
13523     }
13524   else
13525     as_warn (_(".end directive missing or unknown symbol"));
13526 
13527 #ifdef OBJ_ELF
13528   /* Create an expression to calculate the size of the function.  */
13529   if (p && cur_proc_ptr)
13530     {
13531       OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13532       expressionS *exp = xmalloc (sizeof (expressionS));
13533 
13534       obj->size = exp;
13535       exp->X_op = O_subtract;
13536       exp->X_add_symbol = symbol_temp_new_now ();
13537       exp->X_op_symbol = p;
13538       exp->X_add_number = 0;
13539 
13540       cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13541     }
13542 
13543   /* Generate a .pdr section.  */
13544   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13545       && mips_flag_pdr)
13546     {
13547       segT saved_seg = now_seg;
13548       subsegT saved_subseg = now_subseg;
13549       valueT dot;
13550       expressionS exp;
13551       char *fragp;
13552 
13553       dot = frag_now_fix ();
13554 
13555 #ifdef md_flush_pending_output
13556       md_flush_pending_output ();
13557 #endif
13558 
13559       assert (pdr_seg);
13560       subseg_set (pdr_seg, 0);
13561 
13562       /* Write the symbol.  */
13563       exp.X_op = O_symbol;
13564       exp.X_add_symbol = p;
13565       exp.X_add_number = 0;
13566       emit_expr (&exp, 4);
13567 
13568       fragp = frag_more (7 * 4);
13569 
13570       md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13571       md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13572       md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13573       md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13574       md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13575       md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13576       md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13577 
13578       subseg_set (saved_seg, saved_subseg);
13579     }
13580 #endif /* OBJ_ELF */
13581 
13582   cur_proc_ptr = NULL;
13583 }
13584 
13585 /* The .aent and .ent directives.  */
13586 
13587 static void
s_mips_ent(int aent)13588 s_mips_ent (int aent)
13589 {
13590   symbolS *symbolP;
13591 
13592   symbolP = get_symbol ();
13593   if (*input_line_pointer == ',')
13594     ++input_line_pointer;
13595   SKIP_WHITESPACE ();
13596   if (ISDIGIT (*input_line_pointer)
13597       || *input_line_pointer == '-')
13598     get_number ();
13599 
13600   if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13601     as_warn (_(".ent or .aent not in text section."));
13602 
13603   if (!aent && cur_proc_ptr)
13604     as_warn (_("missing .end"));
13605 
13606   if (!aent)
13607     {
13608       /* This function needs its own .frame and .cprestore directives.  */
13609       mips_frame_reg_valid = 0;
13610       mips_cprestore_valid = 0;
13611 
13612       cur_proc_ptr = &cur_proc;
13613       memset (cur_proc_ptr, '\0', sizeof (procS));
13614 
13615       cur_proc_ptr->func_sym = symbolP;
13616 
13617       symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13618 
13619       ++numprocs;
13620 
13621       if (debug_type == DEBUG_STABS)
13622         stabs_generate_asm_func (S_GET_NAME (symbolP),
13623 				 S_GET_NAME (symbolP));
13624     }
13625 
13626   demand_empty_rest_of_line ();
13627 }
13628 
13629 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13630    then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13631    s_mips_frame is used so that we can set the PDR information correctly.
13632    We can't use the ecoff routines because they make reference to the ecoff
13633    symbol table (in the mdebug section).  */
13634 
13635 static void
s_mips_frame(int ignore ATTRIBUTE_UNUSED)13636 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13637 {
13638 #ifdef OBJ_ELF
13639   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13640     {
13641       long val;
13642 
13643       if (cur_proc_ptr == (procS *) NULL)
13644 	{
13645 	  as_warn (_(".frame outside of .ent"));
13646 	  demand_empty_rest_of_line ();
13647 	  return;
13648 	}
13649 
13650       cur_proc_ptr->frame_reg = tc_get_register (1);
13651 
13652       SKIP_WHITESPACE ();
13653       if (*input_line_pointer++ != ','
13654 	  || get_absolute_expression_and_terminator (&val) != ',')
13655 	{
13656 	  as_warn (_("Bad .frame directive"));
13657 	  --input_line_pointer;
13658 	  demand_empty_rest_of_line ();
13659 	  return;
13660 	}
13661 
13662       cur_proc_ptr->frame_offset = val;
13663       cur_proc_ptr->pc_reg = tc_get_register (0);
13664 
13665       demand_empty_rest_of_line ();
13666     }
13667   else
13668 #endif /* OBJ_ELF */
13669     s_ignore (ignore);
13670 }
13671 
13672 /* The .fmask and .mask directives. If the mdebug section is present
13673    (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13674    embedded targets, s_mips_mask is used so that we can set the PDR
13675    information correctly. We can't use the ecoff routines because they
13676    make reference to the ecoff symbol table (in the mdebug section).  */
13677 
13678 static void
s_mips_mask(int reg_type)13679 s_mips_mask (int reg_type)
13680 {
13681 #ifdef OBJ_ELF
13682   if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13683     {
13684       long mask, off;
13685 
13686       if (cur_proc_ptr == (procS *) NULL)
13687 	{
13688 	  as_warn (_(".mask/.fmask outside of .ent"));
13689 	  demand_empty_rest_of_line ();
13690 	  return;
13691 	}
13692 
13693       if (get_absolute_expression_and_terminator (&mask) != ',')
13694 	{
13695 	  as_warn (_("Bad .mask/.fmask directive"));
13696 	  --input_line_pointer;
13697 	  demand_empty_rest_of_line ();
13698 	  return;
13699 	}
13700 
13701       off = get_absolute_expression ();
13702 
13703       if (reg_type == 'F')
13704 	{
13705 	  cur_proc_ptr->fpreg_mask = mask;
13706 	  cur_proc_ptr->fpreg_offset = off;
13707 	}
13708       else
13709 	{
13710 	  cur_proc_ptr->reg_mask = mask;
13711 	  cur_proc_ptr->reg_offset = off;
13712 	}
13713 
13714       demand_empty_rest_of_line ();
13715     }
13716   else
13717 #endif /* OBJ_ELF */
13718     s_ignore (reg_type);
13719 }
13720 
13721 /* A table describing all the processors gas knows about.  Names are
13722    matched in the order listed.
13723 
13724    To ease comparison, please keep this table in the same order as
13725    gcc's mips_cpu_info_table[].  */
13726 static const struct mips_cpu_info mips_cpu_info_table[] =
13727 {
13728   /* Entries for generic ISAs */
13729   { "mips1",          1,      ISA_MIPS1,      CPU_R3000 },
13730   { "mips2",          1,      ISA_MIPS2,      CPU_R6000 },
13731   { "mips3",          1,      ISA_MIPS3,      CPU_R4000 },
13732   { "mips4",          1,      ISA_MIPS4,      CPU_R8000 },
13733   { "mips5",          1,      ISA_MIPS5,      CPU_MIPS5 },
13734   { "mips32",         1,      ISA_MIPS32,     CPU_MIPS32 },
13735   { "mips32r2",       1,      ISA_MIPS32R2,   CPU_MIPS32R2 },
13736   { "mips64",         1,      ISA_MIPS64,     CPU_MIPS64 },
13737   { "mips64r2",       1,      ISA_MIPS64R2,   CPU_MIPS64R2 },
13738 
13739   /* MIPS I */
13740   { "r3000",          0,      ISA_MIPS1,      CPU_R3000 },
13741   { "r2000",          0,      ISA_MIPS1,      CPU_R3000 },
13742   { "r3900",          0,      ISA_MIPS1,      CPU_R3900 },
13743 
13744   /* MIPS II */
13745   { "r6000",          0,      ISA_MIPS2,      CPU_R6000 },
13746 
13747   /* MIPS III */
13748   { "r4000",          0,      ISA_MIPS3,      CPU_R4000 },
13749   { "r4010",          0,      ISA_MIPS2,      CPU_R4010 },
13750   { "vr4100",         0,      ISA_MIPS3,      CPU_VR4100 },
13751   { "vr4111",         0,      ISA_MIPS3,      CPU_R4111 },
13752   { "vr4120",         0,      ISA_MIPS3,      CPU_VR4120 },
13753   { "vr4130",         0,      ISA_MIPS3,      CPU_VR4120 },
13754   { "vr4181",         0,      ISA_MIPS3,      CPU_R4111 },
13755   { "vr4300",         0,      ISA_MIPS3,      CPU_R4300 },
13756   { "r4400",          0,      ISA_MIPS3,      CPU_R4400 },
13757   { "r4600",          0,      ISA_MIPS3,      CPU_R4600 },
13758   { "orion",          0,      ISA_MIPS3,      CPU_R4600 },
13759   { "r4650",          0,      ISA_MIPS3,      CPU_R4650 },
13760 
13761   /* MIPS IV */
13762   { "r8000",          0,      ISA_MIPS4,      CPU_R8000 },
13763   { "r10000",         0,      ISA_MIPS4,      CPU_R10000 },
13764   { "r12000",         0,      ISA_MIPS4,      CPU_R12000 },
13765   { "vr5000",         0,      ISA_MIPS4,      CPU_R5000 },
13766   { "vr5400",         0,      ISA_MIPS4,      CPU_VR5400 },
13767   { "vr5500",         0,      ISA_MIPS4,      CPU_VR5500 },
13768   { "rm5200",         0,      ISA_MIPS4,      CPU_R5000 },
13769   { "rm5230",         0,      ISA_MIPS4,      CPU_R5000 },
13770   { "rm5231",         0,      ISA_MIPS4,      CPU_R5000 },
13771   { "rm5261",         0,      ISA_MIPS4,      CPU_R5000 },
13772   { "rm5721",         0,      ISA_MIPS4,      CPU_R5000 },
13773   { "rm7000",         0,      ISA_MIPS4,      CPU_RM7000 },
13774   { "rm9000",         0,      ISA_MIPS4,      CPU_RM9000 },
13775 
13776   /* MIPS 32 */
13777   { "4kc",            0,      ISA_MIPS32,     CPU_MIPS32 },
13778   { "4km",            0,      ISA_MIPS32,     CPU_MIPS32 },
13779   { "4kp",            0,      ISA_MIPS32,     CPU_MIPS32 },
13780 
13781   /* MIPS 64 */
13782   { "5kc",            0,      ISA_MIPS64,     CPU_MIPS64 },
13783   { "20kc",           0,      ISA_MIPS64,     CPU_MIPS64 },
13784 
13785   /* Broadcom SB-1 CPU core */
13786   { "sb1",            0,      ISA_MIPS64,     CPU_SB1 },
13787 
13788   /* End marker */
13789   { NULL, 0, 0, 0 }
13790 };
13791 
13792 
13793 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13794    with a final "000" replaced by "k".  Ignore case.
13795 
13796    Note: this function is shared between GCC and GAS.  */
13797 
13798 static bfd_boolean
mips_strict_matching_cpu_name_p(const char * canonical,const char * given)13799 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13800 {
13801   while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13802     given++, canonical++;
13803 
13804   return ((*given == 0 && *canonical == 0)
13805 	  || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13806 }
13807 
13808 
13809 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13810    CPU name.  We've traditionally allowed a lot of variation here.
13811 
13812    Note: this function is shared between GCC and GAS.  */
13813 
13814 static bfd_boolean
mips_matching_cpu_name_p(const char * canonical,const char * given)13815 mips_matching_cpu_name_p (const char *canonical, const char *given)
13816 {
13817   /* First see if the name matches exactly, or with a final "000"
13818      turned into "k".  */
13819   if (mips_strict_matching_cpu_name_p (canonical, given))
13820     return TRUE;
13821 
13822   /* If not, try comparing based on numerical designation alone.
13823      See if GIVEN is an unadorned number, or 'r' followed by a number.  */
13824   if (TOLOWER (*given) == 'r')
13825     given++;
13826   if (!ISDIGIT (*given))
13827     return FALSE;
13828 
13829   /* Skip over some well-known prefixes in the canonical name,
13830      hoping to find a number there too.  */
13831   if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13832     canonical += 2;
13833   else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13834     canonical += 2;
13835   else if (TOLOWER (canonical[0]) == 'r')
13836     canonical += 1;
13837 
13838   return mips_strict_matching_cpu_name_p (canonical, given);
13839 }
13840 
13841 
13842 /* Parse an option that takes the name of a processor as its argument.
13843    OPTION is the name of the option and CPU_STRING is the argument.
13844    Return the corresponding processor enumeration if the CPU_STRING is
13845    recognized, otherwise report an error and return null.
13846 
13847    A similar function exists in GCC.  */
13848 
13849 static const struct mips_cpu_info *
mips_parse_cpu(const char * option,const char * cpu_string)13850 mips_parse_cpu (const char *option, const char *cpu_string)
13851 {
13852   const struct mips_cpu_info *p;
13853 
13854   /* 'from-abi' selects the most compatible architecture for the given
13855      ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs.  For the
13856      EABIs, we have to decide whether we're using the 32-bit or 64-bit
13857      version.  Look first at the -mgp options, if given, otherwise base
13858      the choice on MIPS_DEFAULT_64BIT.
13859 
13860      Treat NO_ABI like the EABIs.  One reason to do this is that the
13861      plain 'mips' and 'mips64' configs have 'from-abi' as their default
13862      architecture.  This code picks MIPS I for 'mips' and MIPS III for
13863      'mips64', just as we did in the days before 'from-abi'.  */
13864   if (strcasecmp (cpu_string, "from-abi") == 0)
13865     {
13866       if (ABI_NEEDS_32BIT_REGS (mips_abi))
13867 	return mips_cpu_info_from_isa (ISA_MIPS1);
13868 
13869       if (ABI_NEEDS_64BIT_REGS (mips_abi))
13870 	return mips_cpu_info_from_isa (ISA_MIPS3);
13871 
13872       if (file_mips_gp32 >= 0)
13873 	return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13874 
13875       return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13876 				     ? ISA_MIPS3
13877 				     : ISA_MIPS1);
13878     }
13879 
13880   /* 'default' has traditionally been a no-op.  Probably not very useful.  */
13881   if (strcasecmp (cpu_string, "default") == 0)
13882     return 0;
13883 
13884   for (p = mips_cpu_info_table; p->name != 0; p++)
13885     if (mips_matching_cpu_name_p (p->name, cpu_string))
13886       return p;
13887 
13888   as_bad ("Bad value (%s) for %s", cpu_string, option);
13889   return 0;
13890 }
13891 
13892 /* Return the canonical processor information for ISA (a member of the
13893    ISA_MIPS* enumeration).  */
13894 
13895 static const struct mips_cpu_info *
mips_cpu_info_from_isa(int isa)13896 mips_cpu_info_from_isa (int isa)
13897 {
13898   int i;
13899 
13900   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13901     if (mips_cpu_info_table[i].is_isa
13902 	&& isa == mips_cpu_info_table[i].isa)
13903       return (&mips_cpu_info_table[i]);
13904 
13905   return NULL;
13906 }
13907 
13908 static const struct mips_cpu_info *
mips_cpu_info_from_arch(int arch)13909 mips_cpu_info_from_arch (int arch)
13910 {
13911   int i;
13912 
13913   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13914     if (arch == mips_cpu_info_table[i].cpu)
13915       return (&mips_cpu_info_table[i]);
13916 
13917   return NULL;
13918 }
13919 
13920 static void
show(FILE * stream,const char * string,int * col_p,int * first_p)13921 show (FILE *stream, const char *string, int *col_p, int *first_p)
13922 {
13923   if (*first_p)
13924     {
13925       fprintf (stream, "%24s", "");
13926       *col_p = 24;
13927     }
13928   else
13929     {
13930       fprintf (stream, ", ");
13931       *col_p += 2;
13932     }
13933 
13934   if (*col_p + strlen (string) > 72)
13935     {
13936       fprintf (stream, "\n%24s", "");
13937       *col_p = 24;
13938     }
13939 
13940   fprintf (stream, "%s", string);
13941   *col_p += strlen (string);
13942 
13943   *first_p = 0;
13944 }
13945 
13946 void
md_show_usage(FILE * stream)13947 md_show_usage (FILE *stream)
13948 {
13949   int column, first;
13950   size_t i;
13951 
13952   fprintf (stream, _("\
13953 MIPS options:\n\
13954 -EB			generate big endian output\n\
13955 -EL			generate little endian output\n\
13956 -g, -g2			do not remove unneeded NOPs or swap branches\n\
13957 -G NUM			allow referencing objects up to NUM bytes\n\
13958 			implicitly with the gp register [default 8]\n"));
13959   fprintf (stream, _("\
13960 -mips1			generate MIPS ISA I instructions\n\
13961 -mips2			generate MIPS ISA II instructions\n\
13962 -mips3			generate MIPS ISA III instructions\n\
13963 -mips4			generate MIPS ISA IV instructions\n\
13964 -mips5                  generate MIPS ISA V instructions\n\
13965 -mips32                 generate MIPS32 ISA instructions\n\
13966 -mips32r2               generate MIPS32 release 2 ISA instructions\n\
13967 -mips64                 generate MIPS64 ISA instructions\n\
13968 -mips64r2               generate MIPS64 release 2 ISA instructions\n\
13969 -march=CPU/-mtune=CPU	generate code/schedule for CPU, where CPU is one of:\n"));
13970 
13971   first = 1;
13972 
13973   for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13974     show (stream, mips_cpu_info_table[i].name, &column, &first);
13975   show (stream, "from-abi", &column, &first);
13976   fputc ('\n', stream);
13977 
13978   fprintf (stream, _("\
13979 -mCPU			equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13980 -no-mCPU		don't generate code specific to CPU.\n\
13981 			For -mCPU and -no-mCPU, CPU must be one of:\n"));
13982 
13983   first = 1;
13984 
13985   show (stream, "3900", &column, &first);
13986   show (stream, "4010", &column, &first);
13987   show (stream, "4100", &column, &first);
13988   show (stream, "4650", &column, &first);
13989   fputc ('\n', stream);
13990 
13991   fprintf (stream, _("\
13992 -mips16			generate mips16 instructions\n\
13993 -no-mips16		do not generate mips16 instructions\n"));
13994   fprintf (stream, _("\
13995 -mfix-vr4120		work around certain VR4120 errata\n\
13996 -mfix-vr4130		work around VR4130 mflo/mfhi errata\n\
13997 -mgp32			use 32-bit GPRs, regardless of the chosen ISA\n\
13998 -mfp32			use 32-bit FPRs, regardless of the chosen ISA\n\
13999 -mno-shared		optimize output for executables\n\
14000 -msym32			assume all symbols have 32-bit values\n\
14001 -O0			remove unneeded NOPs, do not swap branches\n\
14002 -O			remove unneeded NOPs and swap branches\n\
14003 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14004 --trap, --no-break	trap exception on div by 0 and mult overflow\n\
14005 --break, --no-trap	break exception on div by 0 and mult overflow\n"));
14006 #ifdef OBJ_ELF
14007   fprintf (stream, _("\
14008 -KPIC, -call_shared	generate SVR4 position independent code\n\
14009 -non_shared		do not generate position independent code\n\
14010 -xgot			assume a 32 bit GOT\n\
14011 -mpdr, -mno-pdr		enable/disable creation of .pdr sections\n\
14012 -mshared, -mno-shared   disable/enable .cpload optimization for\n\
14013                         non-shared code\n\
14014 -mabi=ABI		create ABI conformant object file for:\n"));
14015 
14016   first = 1;
14017 
14018   show (stream, "32", &column, &first);
14019   show (stream, "o64", &column, &first);
14020   show (stream, "n32", &column, &first);
14021   show (stream, "64", &column, &first);
14022   show (stream, "eabi", &column, &first);
14023 
14024   fputc ('\n', stream);
14025 
14026   fprintf (stream, _("\
14027 -32			create o32 ABI object file (default)\n\
14028 -n32			create n32 ABI object file\n\
14029 -64			create 64 ABI object file\n"));
14030 #endif
14031 }
14032 
14033 enum dwarf2_format
mips_dwarf2_format(void)14034 mips_dwarf2_format (void)
14035 {
14036   if (mips_abi == N64_ABI)
14037     {
14038 #ifdef TE_IRIX
14039       return dwarf2_format_64bit_irix;
14040 #else
14041       return dwarf2_format_64bit;
14042 #endif
14043     }
14044   else
14045     return dwarf2_format_32bit;
14046 }
14047 
14048 int
mips_dwarf2_addr_size(void)14049 mips_dwarf2_addr_size (void)
14050 {
14051   if (mips_abi == N64_ABI)
14052     return 8;
14053   else
14054     return 4;
14055 }
14056 
14057 /* Standard calling conventions leave the CFA at SP on entry.  */
14058 void
mips_cfi_frame_initial_instructions(void)14059 mips_cfi_frame_initial_instructions (void)
14060 {
14061   cfi_add_CFA_def_cfa_register (SP);
14062 }
14063 
14064