]> Git Repo - binutils.git/blob - gas/config/tc-mips.c
* config/tc-mips.c: Undef OBJ_COPY_SYMBOL_ATTRIBUTES before
[binutils.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2    Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3    Contributed by the OSF and Ralph Campbell.
4    Written by Keith Knowles and Ralph Campbell, working independently.
5    Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6    Support.
7
8    This file is part of GAS.
9
10    GAS is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2, or (at your option)
13    any later version.
14
15    GAS is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GAS; see the file COPYING.  If not, write to the Free
22    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.  */
24
25 #include "as.h"
26 #include "config.h"
27 #include "subsegs.h"
28
29 #include <ctype.h>
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too.  */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef TARGET_SYMBOL_FIELDS
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about.  */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 #include "ecoff.h"
81
82 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
83 static char *mips_regmask_frag;
84 #endif
85
86 #define AT  1
87 #define TREG 24
88 #define PIC_CALL_REG 25
89 #define KT0 26
90 #define KT1 27
91 #define GP  28
92 #define SP  29
93 #define FP  30
94 #define RA  31
95
96 #define ILLEGAL_REG (32)
97
98 /* Allow override of standard little-endian ECOFF format.  */
99
100 #ifndef ECOFF_LITTLE_FORMAT
101 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
102 #endif
103
104 extern int target_big_endian;
105
106 /* 1 is we should use the 64 bit MIPS ELF ABI, 0 if we should use the
107    32 bit ABI.  This has no meaning for ECOFF.  */
108 static int mips_64;
109
110 /* The default target format to use.  */
111 const char *
112 mips_target_format ()
113 {
114   switch (OUTPUT_FLAVOR)
115     {
116     case bfd_target_aout_flavour:
117       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
118     case bfd_target_ecoff_flavour:
119       return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
120     case bfd_target_elf_flavour:
121       return (target_big_endian
122               ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
123               : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
124     default:
125       abort ();
126       return NULL;
127     }
128 }
129
130 /* The name of the readonly data section.  */
131 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
132                             ? ".data" \
133                             : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
134                             ? ".rdata" \
135                             : OUTPUT_FLAVOR == bfd_target_elf_flavour \
136                             ? ".rodata" \
137                             : (abort (), ""))
138
139 /* This is the set of options which may be modified by the .set
140    pseudo-op.  We use a struct so that .set push and .set pop are more
141    reliable.  */
142
143 struct mips_set_options
144 {
145   /* MIPS ISA (Instruction Set Architecture) level.  This is set to -1
146      if it has not been initialized.  Changed by `.set mipsN', and the
147      -mipsN command line option, and the default CPU.  */
148   int isa;
149   /* Whether we are assembling for the mips16 processor.  0 if we are
150      not, 1 if we are, and -1 if the value has not been initialized.
151      Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152      -nomips16 command line options, and the default CPU.  */
153   int mips16;
154   /* Non-zero if we should not reorder instructions.  Changed by `.set
155      reorder' and `.set noreorder'.  */
156   int noreorder;
157   /* Non-zero if we should not permit the $at ($1) register to be used
158      in instructions.  Changed by `.set at' and `.set noat'.  */
159   int noat;
160   /* Non-zero if we should warn when a macro instruction expands into
161      more than one machine instruction.  Changed by `.set nomacro' and
162      `.set macro'.  */
163   int warn_about_macros;
164   /* Non-zero if we should not move instructions.  Changed by `.set
165      move', `.set volatile', `.set nomove', and `.set novolatile'.  */
166   int nomove;
167   /* Non-zero if we should not optimize branches by moving the target
168      of the branch into the delay slot.  Actually, we don't perform
169      this optimization anyhow.  Changed by `.set bopt' and `.set
170      nobopt'.  */
171   int nobopt;
172   /* Non-zero if we should not autoextend mips16 instructions.
173      Changed by `.set autoextend' and `.set noautoextend'.  */
174   int noautoextend;
175 };
176
177 /* This is the struct we use to hold the current set of options.  Note
178    that we must set the isa and mips16 fields to -1 to indicate that
179    they have not been initialized.  */
180
181 static struct mips_set_options mips_opts = { -1, -1 };
182
183 /* These variables are filled in with the masks of registers used.
184    The object format code reads them and puts them in the appropriate
185    place.  */
186 unsigned long mips_gprmask;
187 unsigned long mips_cprmask[4];
188
189 /* MIPS ISA we are using for this output file.  */
190 static int file_mips_isa;
191
192 /* The CPU type as a number: 2000, 3000, 4000, 4400, etc.  */
193 static int mips_cpu = -1;
194
195 /* Whether the 4650 instructions (mad/madu) are permitted.  */
196 static int mips_4650 = -1;
197
198 /* Whether the 4010 instructions are permitted.  */
199 static int mips_4010 = -1;
200
201 /* Whether the 4100 MADD16 and DMADD16 are permitted. */
202 static int mips_4100 = -1;
203
204 /* start-sanitize-r5900 */
205 /* Whether Toshiba r5900 instructions are permitted. */
206 static int mips_5900 = -1;
207 /* end-sanitize-r5900 */
208
209 /* Whether Toshiba r3900 instructions are permitted. */
210 static int mips_3900 = -1;
211
212 /* start-sanitize-tx19 */
213 /* The tx19 (r1900) is a mips16 decoder with a tx39(r3900) behind it.
214    The tx19 related options and configuration bits are handled by 
215    the tx39 flags. */
216 /* end-sanitize-tx19 */
217
218 /* Whether the processor uses hardware interlocks to protect 
219    reads from the HI and LO registers, and thus does not
220    require nops to be inserted.  */
221 #define hilo_interlocks (mips_4010 || mips_cpu == 4300 || mips_3900)
222
223 /* Whether the processor uses hardware interlocks to protect reads
224    from the GPRs, and thus does not require nops to be inserted.  */
225 #define gpr_interlocks (mips_opts.isa >= 2 || mips_3900)
226
227 /* As with other "interlocks" this is used by hardware that has FP
228    (co-processor) interlocks.  */
229 /* Itbl support may require additional care here. */
230 #define cop_interlocks (mips_cpu == 4300)
231
232 /* MIPS PIC level.  */
233
234 enum mips_pic_level
235 {
236   /* Do not generate PIC code.  */
237   NO_PIC,
238
239   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
240      not sure what it is supposed to do.  */
241   IRIX4_PIC,
242
243   /* Generate PIC code as in the SVR4 MIPS ABI.  */
244   SVR4_PIC,
245
246   /* Generate PIC code without using a global offset table: the data
247      segment has a maximum size of 64K, all data references are off
248      the $gp register, and all text references are PC relative.  This
249      is used on some embedded systems.  */
250   EMBEDDED_PIC
251 };
252
253 static enum mips_pic_level mips_pic;
254
255 /* 1 if we should generate 32 bit offsets from the GP register in
256    SVR4_PIC mode.  Currently has no meaning in other modes.  */
257 static int mips_big_got;
258
259 /* 1 if trap instructions should used for overflow rather than break
260    instructions.  */
261 static int mips_trap;
262
263 /* Non-zero if any .set noreorder directives were used.  */
264
265 static int mips_any_noreorder;
266
267 /* The size of the small data section.  */
268 static int g_switch_value = 8;
269 /* Whether the -G option was used.  */
270 static int g_switch_seen = 0;
271
272 #define N_RMASK 0xc4
273 #define N_VFP   0xd4
274
275 /* If we can determine in advance that GP optimization won't be
276    possible, we can skip the relaxation stuff that tries to produce
277    GP-relative references.  This makes delay slot optimization work
278    better.
279
280    This function can only provide a guess, but it seems to work for
281    gcc output.  If it guesses wrong, the only loss should be in
282    efficiency; it shouldn't introduce any bugs.
283
284    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
285    fixed it for the non-PIC mode.  KR 95/04/07  */
286 static int nopic_need_relax PARAMS ((symbolS *, int));
287
288 /* handle of the OPCODE hash table */
289 static struct hash_control *op_hash = NULL;
290
291 /* The opcode hash table we use for the mips16.  */
292 static struct hash_control *mips16_op_hash = NULL;
293
294 /* This array holds the chars that always start a comment.  If the
295     pre-processor is disabled, these aren't very useful */
296 const char comment_chars[] = "#";
297
298 /* This array holds the chars that only start a comment at the beginning of
299    a line.  If the line seems to have the form '# 123 filename'
300    .line and .file directives will appear in the pre-processed output */
301 /* Note that input_file.c hand checks for '#' at the beginning of the
302    first line of the input file.  This is because the compiler outputs
303    #NO_APP at the beginning of its output. */
304 /* Also note that C style comments are always supported.  */
305 const char line_comment_chars[] = "#";
306
307 /* This array holds machine specific line separator characters. */
308 const char line_separator_chars[] = "";
309
310 /* Chars that can be used to separate mant from exp in floating point nums */
311 const char EXP_CHARS[] = "eE";
312
313 /* Chars that mean this number is a floating point constant */
314 /* As in 0f12.456 */
315 /* or    0d1.2345e12 */
316 const char FLT_CHARS[] = "rRsSfFdDxXpP";
317
318 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
319    changed in read.c .  Ideally it shouldn't have to know about it at all,
320    but nothing is ideal around here.
321  */
322
323 static char *insn_error;
324
325 static int auto_align = 1;
326
327 /* When outputting SVR4 PIC code, the assembler needs to know the
328    offset in the stack frame from which to restore the $gp register.
329    This is set by the .cprestore pseudo-op, and saved in this
330    variable.  */
331 static offsetT mips_cprestore_offset = -1;
332
333 /* This is the register which holds the stack frame, as set by the
334    .frame pseudo-op.  This is needed to implement .cprestore.  */
335 static int mips_frame_reg = SP;
336
337 /* To output NOP instructions correctly, we need to keep information
338    about the previous two instructions.  */
339
340 /* Whether we are optimizing.  The default value of 2 means to remove
341    unneeded NOPs and swap branch instructions when possible.  A value
342    of 1 means to not swap branches.  A value of 0 means to always
343    insert NOPs.  */
344 static int mips_optimize = 2;
345
346 /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
347    equivalent to seeing no -g option at all.  */
348 static int mips_debug = 0;
349
350 /* The previous instruction.  */
351 static struct mips_cl_insn prev_insn;
352
353 /* The instruction before prev_insn.  */
354 static struct mips_cl_insn prev_prev_insn;
355
356 /* If we don't want information for prev_insn or prev_prev_insn, we
357    point the insn_mo field at this dummy integer.  */
358 static const struct mips_opcode dummy_opcode = { 0 };
359
360 /* Non-zero if prev_insn is valid.  */
361 static int prev_insn_valid;
362
363 /* The frag for the previous instruction.  */
364 static struct frag *prev_insn_frag;
365
366 /* The offset into prev_insn_frag for the previous instruction.  */
367 static long prev_insn_where;
368
369 /* The reloc type for the previous instruction, if any.  */
370 static bfd_reloc_code_real_type prev_insn_reloc_type;
371
372 /* The reloc for the previous instruction, if any.  */
373 static fixS *prev_insn_fixp;
374
375 /* Non-zero if the previous instruction was in a delay slot.  */
376 static int prev_insn_is_delay_slot;
377
378 /* Non-zero if the previous instruction was in a .set noreorder.  */
379 static int prev_insn_unreordered;
380
381 /* Non-zero if the previous instruction uses an extend opcode (if
382    mips16).  */
383 static int prev_insn_extended;
384
385 /* Non-zero if the previous previous instruction was in a .set
386    noreorder.  */
387 static int prev_prev_insn_unreordered;
388
389 /* If this is set, it points to a frag holding nop instructions which
390    were inserted before the start of a noreorder section.  If those
391    nops turn out to be unnecessary, the size of the frag can be
392    decreased.  */
393 static fragS *prev_nop_frag;
394
395 /* The number of nop instructions we created in prev_nop_frag.  */
396 static int prev_nop_frag_holds;
397
398 /* The number of nop instructions that we know we need in
399    prev_nop_frag. */
400 static int prev_nop_frag_required;
401
402 /* The number of instructions we've seen since prev_nop_frag.  */
403 static int prev_nop_frag_since;
404
405 /* For ECOFF and ELF, relocations against symbols are done in two
406    parts, with a HI relocation and a LO relocation.  Each relocation
407    has only 16 bits of space to store an addend.  This means that in
408    order for the linker to handle carries correctly, it must be able
409    to locate both the HI and the LO relocation.  This means that the
410    relocations must appear in order in the relocation table.
411
412    In order to implement this, we keep track of each unmatched HI
413    relocation.  We then sort them so that they immediately precede the
414    corresponding LO relocation. */
415
416 struct mips_hi_fixup
417 {
418   /* Next HI fixup.  */
419   struct mips_hi_fixup *next;
420   /* This fixup.  */
421   fixS *fixp;
422   /* The section this fixup is in.  */
423   segT seg;
424 };
425
426 /* The list of unmatched HI relocs.  */
427
428 static struct mips_hi_fixup *mips_hi_fixup_list;
429
430 /* Map normal MIPS register numbers to mips16 register numbers.  */
431
432 #define X ILLEGAL_REG
433 static const int mips32_to_16_reg_map[] =
434 {
435   X, X, 2, 3, 4, 5, 6, 7,
436   X, X, X, X, X, X, X, X,
437   0, 1, X, X, X, X, X, X,
438   X, X, X, X, X, X, X, X
439 };
440 #undef X
441
442 /* Map mips16 register numbers to normal MIPS register numbers.  */
443
444 static const int mips16_to_32_reg_map[] =
445 {
446   16, 17, 2, 3, 4, 5, 6, 7
447 };
448 \f
449 /* Since the MIPS does not have multiple forms of PC relative
450    instructions, we do not have to do relaxing as is done on other
451    platforms.  However, we do have to handle GP relative addressing
452    correctly, which turns out to be a similar problem.
453
454    Every macro that refers to a symbol can occur in (at least) two
455    forms, one with GP relative addressing and one without.  For
456    example, loading a global variable into a register generally uses
457    a macro instruction like this:
458      lw $4,i
459    If i can be addressed off the GP register (this is true if it is in
460    the .sbss or .sdata section, or if it is known to be smaller than
461    the -G argument) this will generate the following instruction:
462      lw $4,i($gp)
463    This instruction will use a GPREL reloc.  If i can not be addressed
464    off the GP register, the following instruction sequence will be used:
465      lui $at,i
466      lw $4,i($at)
467    In this case the first instruction will have a HI16 reloc, and the
468    second reloc will have a LO16 reloc.  Both relocs will be against
469    the symbol i.
470
471    The issue here is that we may not know whether i is GP addressable
472    until after we see the instruction that uses it.  Therefore, we
473    want to be able to choose the final instruction sequence only at
474    the end of the assembly.  This is similar to the way other
475    platforms choose the size of a PC relative instruction only at the
476    end of assembly.
477
478    When generating position independent code we do not use GP
479    addressing in quite the same way, but the issue still arises as
480    external symbols and local symbols must be handled differently.
481
482    We handle these issues by actually generating both possible
483    instruction sequences.  The longer one is put in a frag_var with
484    type rs_machine_dependent.  We encode what to do with the frag in
485    the subtype field.  We encode (1) the number of existing bytes to
486    replace, (2) the number of new bytes to use, (3) the offset from
487    the start of the existing bytes to the first reloc we must generate
488    (that is, the offset is applied from the start of the existing
489    bytes after they are replaced by the new bytes, if any), (4) the
490    offset from the start of the existing bytes to the second reloc,
491    (5) whether a third reloc is needed (the third reloc is always four
492    bytes after the second reloc), and (6) whether to warn if this
493    variant is used (this is sometimes needed if .set nomacro or .set
494    noat is in effect).  All these numbers are reasonably small.
495
496    Generating two instruction sequences must be handled carefully to
497    ensure that delay slots are handled correctly.  Fortunately, there
498    are a limited number of cases.  When the second instruction
499    sequence is generated, append_insn is directed to maintain the
500    existing delay slot information, so it continues to apply to any
501    code after the second instruction sequence.  This means that the
502    second instruction sequence must not impose any requirements not
503    required by the first instruction sequence.
504
505    These variant frags are then handled in functions called by the
506    machine independent code.  md_estimate_size_before_relax returns
507    the final size of the frag.  md_convert_frag sets up the final form
508    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
509    one if needed.  */
510 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
511   ((relax_substateT) \
512    (((old) << 23) \
513     | ((new) << 16) \
514     | (((reloc1) + 64) << 9) \
515     | (((reloc2) + 64) << 2) \
516     | ((reloc3) ? (1 << 1) : 0) \
517     | ((warn) ? 1 : 0)))
518 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
519 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
520 #define RELAX_RELOC1(i) ((bfd_vma)(((i) >> 9) & 0x7f) - 64)
521 #define RELAX_RELOC2(i) ((bfd_vma)(((i) >> 2) & 0x7f) - 64)
522 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
523 #define RELAX_WARN(i) ((i) & 1)
524
525 /* For mips16 code, we use an entirely different form of relaxation.
526    mips16 supports two versions of most instructions which take
527    immediate values: a small one which takes some small value, and a
528    larger one which takes a 16 bit value.  Since branches also follow
529    this pattern, relaxing these values is required.
530
531    We can assemble both mips16 and normal MIPS code in a single
532    object.  Therefore, we need to support this type of relaxation at
533    the same time that we support the relaxation described above.  We
534    use the high bit of the subtype field to distinguish these cases.
535
536    The information we store for this type of relaxation is the
537    argument code found in the opcode file for this relocation, whether
538    the user explicitly requested a small or extended form, and whether
539    the relocation is in a jump or jal delay slot.  That tells us the
540    size of the value, and how it should be stored.  We also store
541    whether the fragment is considered to be extended or not.  We also
542    store whether this is known to be a branch to a different section,
543    whether we have tried to relax this frag yet, and whether we have
544    ever extended a PC relative fragment because of a shift count.  */
545 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
546   (0x80000000                                                   \
547    | ((type) & 0xff)                                            \
548    | ((small) ? 0x100 : 0)                                      \
549    | ((ext) ? 0x200 : 0)                                        \
550    | ((dslot) ? 0x400 : 0)                                      \
551    | ((jal_dslot) ? 0x800 : 0))
552 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
553 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
554 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
555 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
556 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
557 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
558 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
559 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
560 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
561 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
562 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
563 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
564 \f
565 /* Prototypes for static functions.  */
566
567 #ifdef __STDC__
568 #define internalError() \
569     as_fatal ("internal Error, line %d, %s", __LINE__, __FILE__)
570 #else
571 #define internalError() as_fatal ("MIPS internal Error");
572 #endif
573
574 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
575
576 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
577                                   unsigned int reg, enum mips_regclass class));
578 static int reg_needs_delay PARAMS ((int));
579 static void mips16_mark_labels PARAMS ((void));
580 static void append_insn PARAMS ((char *place,
581                                  struct mips_cl_insn * ip,
582                                  expressionS * p,
583                                  bfd_reloc_code_real_type r,
584                                  boolean));
585 static void mips_no_prev_insn PARAMS ((int));
586 static void mips_emit_delays PARAMS ((boolean));
587 #ifdef USE_STDARG
588 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
589                                  const char *name, const char *fmt,
590                                  ...));
591 #else
592 static void macro_build ();
593 #endif
594 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
595                                         const char *, const char *,
596                                         va_list));
597 static void macro_build_lui PARAMS ((char *place, int *counter,
598                                      expressionS * ep, int regnum));
599 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
600 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
601                                          expressionS *));
602 static void load_register PARAMS ((int *, int, expressionS *, int));
603 static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
604 static void macro PARAMS ((struct mips_cl_insn * ip));
605 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
606 #ifdef LOSING_COMPILER
607 static void macro2 PARAMS ((struct mips_cl_insn * ip));
608 #endif
609 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
610 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
611 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
612                                   boolean, boolean, unsigned long *,
613                                   boolean *, unsigned short *));
614 static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
615 static void my_getExpression PARAMS ((expressionS * ep, char *str));
616 static symbolS *get_symbol PARAMS ((void));
617 static void mips_align PARAMS ((int to, int fill, symbolS *label));
618 static void s_align PARAMS ((int));
619 static void s_change_sec PARAMS ((int));
620 static void s_cons PARAMS ((int));
621 static void s_float_cons PARAMS ((int));
622 static void s_mips_globl PARAMS ((int));
623 static void s_option PARAMS ((int));
624 static void s_mipsset PARAMS ((int));
625 static void s_abicalls PARAMS ((int));
626 static void s_cpload PARAMS ((int));
627 static void s_cprestore PARAMS ((int));
628 static void s_gpword PARAMS ((int));
629 static void s_cpadd PARAMS ((int));
630 static void s_insn PARAMS ((int));
631 static void md_obj_begin PARAMS ((void));
632 static void md_obj_end PARAMS ((void));
633 static long get_number PARAMS ((void));
634 static void s_ent PARAMS ((int));
635 static void s_mipsend PARAMS ((int));
636 static void s_file PARAMS ((int));
637 static void s_mips_stab PARAMS ((int));
638 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
639 \f
640 /* Pseudo-op table.
641
642    The following pseudo-ops from the Kane and Heinrich MIPS book
643    should be defined here, but are currently unsupported: .alias,
644    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
645
646    The following pseudo-ops from the Kane and Heinrich MIPS book are
647    specific to the type of debugging information being generated, and
648    should be defined by the object format: .aent, .begin, .bend,
649    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
650    .vreg.
651
652    The following pseudo-ops from the Kane and Heinrich MIPS book are
653    not MIPS CPU specific, but are also not specific to the object file
654    format.  This file is probably the best place to define them, but
655    they are not currently supported: .asm0, .endr, .lab, .repeat,
656    .struct, .weakext.  */
657
658 static const pseudo_typeS mips_pseudo_table[] =
659 {
660  /* MIPS specific pseudo-ops.  */
661   {"option", s_option, 0},
662   {"set", s_mipsset, 0},
663   {"rdata", s_change_sec, 'r'},
664   {"sdata", s_change_sec, 's'},
665   {"livereg", s_ignore, 0},
666   {"abicalls", s_abicalls, 0},
667   {"cpload", s_cpload, 0},
668   {"cprestore", s_cprestore, 0},
669   {"gpword", s_gpword, 0},
670   {"cpadd", s_cpadd, 0},
671   {"insn", s_insn, 0},
672
673  /* Relatively generic pseudo-ops that happen to be used on MIPS
674      chips.  */
675   {"asciiz", stringer, 1},
676   {"bss", s_change_sec, 'b'},
677   {"err", s_err, 0},
678   {"half", s_cons, 1},
679   {"dword", s_cons, 3},
680
681  /* These pseudo-ops are defined in read.c, but must be overridden
682      here for one reason or another.  */
683   {"align", s_align, 0},
684   {"byte", s_cons, 0},
685   {"data", s_change_sec, 'd'},
686   {"double", s_float_cons, 'd'},
687   {"float", s_float_cons, 'f'},
688   {"globl", s_mips_globl, 0},
689   {"global", s_mips_globl, 0},
690   {"hword", s_cons, 1},
691   {"int", s_cons, 2},
692   {"long", s_cons, 2},
693   {"octa", s_cons, 4},
694   {"quad", s_cons, 3},
695   {"short", s_cons, 1},
696   {"single", s_float_cons, 'f'},
697   {"stabn", s_mips_stab, 'n'},
698   {"text", s_change_sec, 't'},
699   {"word", s_cons, 2},
700   { 0 },
701 };
702
703 static const pseudo_typeS mips_nonecoff_pseudo_table[] = {
704  /* These pseudo-ops should be defined by the object file format.
705     However, a.out doesn't support them, so we have versions here.  */
706   {"aent", s_ent, 1},
707   {"bgnb", s_ignore, 0},
708   {"end", s_mipsend, 0},
709   {"endb", s_ignore, 0},
710   {"ent", s_ent, 0},
711   {"file", s_file, 0},
712   {"fmask", s_ignore, 'F'},
713   {"frame", s_ignore, 0},
714   {"loc", s_ignore, 0},
715   {"mask", s_ignore, 'R'},
716   {"verstamp", s_ignore, 0},
717   { 0 },
718 };
719
720 extern void pop_insert PARAMS ((const pseudo_typeS *));
721
722 void
723 mips_pop_insert ()
724 {
725   pop_insert (mips_pseudo_table);
726   if (! ECOFF_DEBUGGING)
727     pop_insert (mips_nonecoff_pseudo_table);
728 }
729 \f
730 /* Symbols labelling the current insn.  */
731
732 struct insn_label_list
733 {
734   struct insn_label_list *next;
735   symbolS *label;
736 };
737
738 static struct insn_label_list *insn_labels;
739 static struct insn_label_list *free_insn_labels;
740
741 static void mips_clear_insn_labels PARAMS ((void));
742
743 static inline void
744 mips_clear_insn_labels ()
745 {
746   register struct insn_label_list **pl;
747
748   for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
749     ;
750   *pl = insn_labels;
751   insn_labels = NULL;
752 }
753 \f
754 static char *expr_end;
755
756 /* Expressions which appear in instructions.  These are set by
757    mips_ip.  */
758
759 static expressionS imm_expr;
760 static expressionS offset_expr;
761
762 /* Relocs associated with imm_expr and offset_expr.  */
763
764 static bfd_reloc_code_real_type imm_reloc;
765 static bfd_reloc_code_real_type offset_reloc;
766
767 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
768
769 static boolean imm_unmatched_hi;
770
771 /* These are set by mips16_ip if an explicit extension is used.  */
772
773 static boolean mips16_small, mips16_ext;
774
775 /*
776  * This function is called once, at assembler startup time.  It should
777  * set up all the tables, etc. that the MD part of the assembler will need.
778  */
779 void
780 md_begin ()
781 {
782   boolean ok = false;
783   register const char *retval = NULL;
784   register unsigned int i = 0;
785
786   if (mips_opts.isa == -1)
787     {
788       const char *cpu;
789       char *a = NULL;
790
791       cpu = TARGET_CPU;
792       if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
793         {
794           a = xmalloc (sizeof TARGET_CPU);
795           strcpy (a, TARGET_CPU);
796           a[(sizeof TARGET_CPU) - 3] = '\0';
797           cpu = a;
798         }
799
800       if (strcmp (cpu, "mips") == 0)
801         {
802           mips_opts.isa = 1;
803           if (mips_cpu == -1)
804             mips_cpu = 3000;
805         }
806       else if (strcmp (cpu, "r3900") == 0
807                || strcmp (cpu, "mipsr3900") == 0
808                /* start-sanitize-tx19 */
809                || strcmp (cpu, "r1900") == 0
810                || strcmp (cpu, "mipstx19") == 0
811                /* end-sanitize-tx19 */
812                )
813         {
814           mips_opts.isa = 1;
815           if (mips_cpu == -1)
816             mips_cpu = 3900;
817           if (mips_3900 == -1)
818             mips_3900 = 1;
819         }
820       else if (strcmp (cpu, "r6000") == 0
821                || strcmp (cpu, "mips2") == 0)
822         {
823           mips_opts.isa = 2;
824           if (mips_cpu == -1)
825             mips_cpu = 6000;
826         }
827       else if (strcmp (cpu, "mips64") == 0
828                || strcmp (cpu, "r4000") == 0
829                || strcmp (cpu, "mips3") == 0)
830         {
831           mips_opts.isa = 3;
832           if (mips_cpu == -1)
833             mips_cpu = 4000;
834         }
835       else if (strcmp (cpu, "r4400") == 0)
836         {
837           mips_opts.isa = 3;
838           if (mips_cpu == -1)
839             mips_cpu = 4400;
840         }
841       else if (strcmp (cpu, "mips64orion") == 0
842                || strcmp (cpu, "r4600") == 0)
843         {
844           mips_opts.isa = 3;
845           if (mips_cpu == -1)
846             mips_cpu = 4600;
847         }
848       else if (strcmp (cpu, "r4650") == 0)
849         {
850           mips_opts.isa = 3;
851           if (mips_cpu == -1)
852             mips_cpu = 4650;
853           if (mips_4650 == -1)
854             mips_4650 = 1;
855         }
856       else if (strcmp (cpu, "mips64vr4300") == 0)
857         {
858           mips_opts.isa = 3;
859           if (mips_cpu == -1)
860             mips_cpu = 4300;
861         }
862       else if (strcmp (cpu, "mips64vr4100") == 0)
863         {
864           mips_opts.isa = 3;
865           if (mips_cpu == -1)
866             mips_cpu = 4100;
867           if (mips_4100 == -1)
868             mips_4100 = 1;
869         }
870       else if (strcmp (cpu, "r4010") == 0)
871         {
872           mips_opts.isa = 2;
873           if (mips_cpu == -1)
874             mips_cpu = 4010;
875           if (mips_4010 == -1)
876             mips_4010 = 1;
877         }
878       else if (strcmp (cpu, "r5000") == 0
879                || strcmp (cpu, "mips64vr5000") == 0)
880         {
881           mips_opts.isa = 4;
882           if (mips_cpu == -1)
883             mips_cpu = 5000;
884         }
885       /* start-sanitize-r5900 */
886       else if (strcmp (cpu, "r5900") == 0
887                || strcmp (cpu, "mips64r5900") == 0
888                || strcmp (cpu, "mips64r5900el") == 0)
889         {
890           mips_opts.isa = 3;
891           if (mips_cpu == -1)
892             mips_cpu = 5900;
893           if (mips_5900 == -1)
894             mips_5900 = 1;
895         }
896       /* end-sanitize-r5900 */
897       else if (strcmp (cpu, "r8000") == 0
898                || strcmp (cpu, "mips4") == 0)
899         {
900           mips_opts.isa = 4;
901           if (mips_cpu == -1)
902             mips_cpu = 8000;
903         }
904       else if (strcmp (cpu, "r10000") == 0)
905         {
906           mips_opts.isa = 4;
907           if (mips_cpu == -1)
908             mips_cpu = 10000;
909         }
910       else if (strcmp (cpu, "mips16") == 0)
911         {
912           mips_opts.isa = 3;
913           if (mips_cpu == -1)
914             mips_cpu = 0; /* FIXME */
915         }
916       else
917         {
918           mips_opts.isa = 1;
919           if (mips_cpu == -1)
920             mips_cpu = 3000;
921         }
922
923       if (a != NULL)
924         free (a);
925     }
926
927   if (mips_opts.mips16 < 0)
928     {
929       if (strncmp (TARGET_CPU, "mips16", sizeof "mips16" - 1) == 0)
930         mips_opts.mips16 = 1;
931       else
932         mips_opts.mips16 = 0;
933     }
934
935   if (mips_4650 < 0)
936     mips_4650 = 0;
937
938   if (mips_4010 < 0)
939     mips_4010 = 0;
940
941   if (mips_4100 < 0)
942     mips_4100 = 0;
943
944   /* start-sanitize-r5900 */
945   if (mips_5900 < 0)
946     mips_5900 = 0;
947   /* end-sanitize-r5900 */
948
949   if (mips_3900 < 0)
950     mips_3900 = 0;
951   
952   if (mips_opts.isa < 2 && mips_trap)
953     as_bad ("trap exception not supported at ISA 1");
954
955   switch (mips_opts.isa)
956     {
957     case 1:
958       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 3000);
959       break;
960     case 2:
961       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 6000);
962       break;
963     case 3:
964       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 4000);
965       break;
966     case 4:
967       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 8000);
968       break;
969     }
970   if (! ok)
971     as_warn ("Could not set architecture and machine");
972
973   file_mips_isa = mips_opts.isa;
974
975   op_hash = hash_new ();
976
977   for (i = 0; i < NUMOPCODES;)
978     {
979       const char *name = mips_opcodes[i].name;
980
981       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
982       if (retval != NULL)
983         {
984           fprintf (stderr, "internal error: can't hash `%s': %s\n",
985                    mips_opcodes[i].name, retval);
986           as_fatal ("Broken assembler.  No assembly attempted.");
987         }
988       do
989         {
990           if (mips_opcodes[i].pinfo != INSN_MACRO
991               && ((mips_opcodes[i].match & mips_opcodes[i].mask)
992                   != mips_opcodes[i].match))
993             {
994               fprintf (stderr, "internal error: bad opcode: `%s' \"%s\"\n",
995                        mips_opcodes[i].name, mips_opcodes[i].args);
996               as_fatal ("Broken assembler.  No assembly attempted.");
997             }
998           ++i;
999         }
1000       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1001     }
1002
1003   mips16_op_hash = hash_new ();
1004
1005   i = 0;
1006   while (i < bfd_mips16_num_opcodes)
1007     {
1008       const char *name = mips16_opcodes[i].name;
1009
1010       retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1011       if (retval != NULL)
1012         as_fatal ("internal error: can't hash `%s': %s\n",
1013                   mips16_opcodes[i].name, retval);
1014       do
1015         {
1016           if (mips16_opcodes[i].pinfo != INSN_MACRO
1017               && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1018                   != mips16_opcodes[i].match))
1019             as_fatal ("internal error: bad opcode: `%s' \"%s\"\n",
1020                       mips16_opcodes[i].name, mips16_opcodes[i].args);
1021           ++i;
1022         }
1023       while (i < bfd_mips16_num_opcodes
1024              && strcmp (mips16_opcodes[i].name, name) == 0);
1025     }
1026
1027   /* We add all the general register names to the symbol table.  This
1028      helps us detect invalid uses of them.  */
1029   for (i = 0; i < 32; i++)
1030     {
1031       char buf[5];
1032
1033       sprintf (buf, "$%d", i);
1034       symbol_table_insert (symbol_new (buf, reg_section, i,
1035                                        &zero_address_frag));
1036     }
1037   symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1038                                    &zero_address_frag));
1039   symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1040                                    &zero_address_frag));
1041   symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1042                                    &zero_address_frag));
1043   symbol_table_insert (symbol_new ("$at", reg_section, AT,
1044                                    &zero_address_frag));
1045   symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1046                                    &zero_address_frag));
1047   symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1048                                    &zero_address_frag));
1049   symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1050                                    &zero_address_frag));
1051
1052   mips_no_prev_insn (false);
1053
1054   mips_gprmask = 0;
1055   mips_cprmask[0] = 0;
1056   mips_cprmask[1] = 0;
1057   mips_cprmask[2] = 0;
1058   mips_cprmask[3] = 0;
1059
1060   /* set the default alignment for the text section (2**2) */
1061   record_alignment (text_section, 2);
1062
1063   if (USE_GLOBAL_POINTER_OPT)
1064     bfd_set_gp_size (stdoutput, g_switch_value);
1065
1066   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1067     {
1068       /* On a native system, sections must be aligned to 16 byte
1069          boundaries.  When configured for an embedded ELF target, we
1070          don't bother.  */
1071       if (strcmp (TARGET_OS, "elf") != 0)
1072         {
1073           (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1074           (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1075           (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1076         }
1077
1078       /* Create a .reginfo section for register masks and a .mdebug
1079          section for debugging information.  */
1080       {
1081         segT seg;
1082         subsegT subseg;
1083         flagword flags;
1084         segT sec;
1085
1086         seg = now_seg;
1087         subseg = now_subseg;
1088
1089         /* The ABI says this section should be loaded so that the
1090            running program can access it.  However, we don't load it
1091            if we are configured for an embedded target */
1092         flags = SEC_READONLY | SEC_DATA;
1093         if (strcmp (TARGET_OS, "elf") != 0)
1094           flags |= SEC_ALLOC | SEC_LOAD;
1095
1096         if (! mips_64)
1097           {
1098             sec = subseg_new (".reginfo", (subsegT) 0);
1099
1100
1101             (void) bfd_set_section_flags (stdoutput, sec, flags);
1102             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1103         
1104 #ifdef OBJ_ELF
1105             mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1106 #endif
1107           }
1108         else
1109           {
1110             /* The 64-bit ABI uses a .MIPS.options section rather than
1111                .reginfo section.  */
1112             sec = subseg_new (".MIPS.options", (subsegT) 0);
1113             (void) bfd_set_section_flags (stdoutput, sec, flags);
1114             (void) bfd_set_section_alignment (stdoutput, sec, 3);
1115
1116 #ifdef OBJ_ELF
1117             /* Set up the option header.  */
1118             {
1119               Elf_Internal_Options opthdr;
1120               char *f;
1121
1122               opthdr.kind = ODK_REGINFO;
1123               opthdr.size = (sizeof (Elf_External_Options)
1124                              + sizeof (Elf64_External_RegInfo));
1125               opthdr.section = 0;
1126               opthdr.info = 0;
1127               f = frag_more (sizeof (Elf_External_Options));
1128               bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1129                                              (Elf_External_Options *) f);
1130
1131               mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1132             }
1133 #endif
1134           }
1135
1136         if (ECOFF_DEBUGGING)
1137           {
1138             sec = subseg_new (".mdebug", (subsegT) 0);
1139             (void) bfd_set_section_flags (stdoutput, sec,
1140                                           SEC_HAS_CONTENTS | SEC_READONLY);
1141             (void) bfd_set_section_alignment (stdoutput, sec, 2);
1142           }
1143
1144         subseg_set (seg, subseg);
1145       }
1146     }
1147
1148   if (! ECOFF_DEBUGGING)
1149     md_obj_begin ();
1150 }
1151
1152 void
1153 md_mips_end ()
1154 {
1155   if (! ECOFF_DEBUGGING)
1156     md_obj_end ();
1157 }
1158
1159 void
1160 md_assemble (str)
1161      char *str;
1162 {
1163   struct mips_cl_insn insn;
1164
1165   imm_expr.X_op = O_absent;
1166   imm_reloc = BFD_RELOC_UNUSED;
1167   imm_unmatched_hi = false;
1168   offset_expr.X_op = O_absent;
1169   offset_reloc = BFD_RELOC_UNUSED;
1170
1171   if (mips_opts.mips16)
1172     mips16_ip (str, &insn);
1173   else
1174     {
1175       mips_ip (str, &insn);
1176       DBG(("returned from mips_ip(%s) insn_opcode = 0x%x\n", 
1177                 str, insn.insn_opcode));
1178     }
1179
1180   if (insn_error)
1181     {
1182       as_bad ("%s `%s'", insn_error, str);
1183       return;
1184     }
1185
1186   if (insn.insn_mo->pinfo == INSN_MACRO)
1187     {
1188       if (mips_opts.mips16)
1189         mips16_macro (&insn);
1190       else
1191         macro (&insn);
1192     }
1193   else
1194     {
1195       if (imm_expr.X_op != O_absent)
1196         append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1197                      imm_unmatched_hi);
1198       else if (offset_expr.X_op != O_absent)
1199         append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1200       else
1201         append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
1202     }
1203 }
1204
1205 /* See whether instruction IP reads register REG.  CLASS is the type
1206    of register.  */
1207
1208 static int
1209 insn_uses_reg (ip, reg, class)
1210      struct mips_cl_insn *ip;
1211      unsigned int reg;
1212      enum mips_regclass class;
1213 {
1214   if (class == MIPS16_REG)
1215     {
1216       assert (mips_opts.mips16);
1217       reg = mips16_to_32_reg_map[reg];
1218       class = MIPS_GR_REG;
1219     }
1220
1221   /* Don't report on general register 0, since it never changes.  */
1222   if (class == MIPS_GR_REG && reg == 0)
1223     return 0;
1224
1225   if (class == MIPS_FP_REG)
1226     {
1227       assert (! mips_opts.mips16);
1228       /* If we are called with either $f0 or $f1, we must check $f0.
1229          This is not optimal, because it will introduce an unnecessary
1230          NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
1231          need to distinguish reading both $f0 and $f1 or just one of
1232          them.  Note that we don't have to check the other way,
1233          because there is no instruction that sets both $f0 and $f1
1234          and requires a delay.  */
1235       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1236           && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1237               == (reg &~ (unsigned) 1)))
1238         return 1;
1239       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1240           && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1241               == (reg &~ (unsigned) 1)))
1242         return 1;
1243     }
1244   else if (! mips_opts.mips16)
1245     {
1246       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1247           && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1248         return 1;
1249       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1250           && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1251         return 1;
1252     }
1253   else
1254     {
1255       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1256           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1257                                     & MIPS16OP_MASK_RX)]
1258               == reg))
1259         return 1;
1260       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1261           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1262                                     & MIPS16OP_MASK_RY)]
1263               == reg))
1264         return 1;
1265       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1266           && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1267                                     & MIPS16OP_MASK_MOVE32Z)]
1268               == reg))
1269         return 1;
1270       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1271         return 1;
1272       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1273         return 1;
1274       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1275         return 1;
1276       if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1277           && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1278               & MIPS16OP_MASK_REGR32) == reg)
1279         return 1;
1280     }
1281
1282   return 0;
1283 }
1284
1285 /* This function returns true if modifying a register requires a
1286    delay.  */
1287
1288 static int
1289 reg_needs_delay (reg)
1290      int reg;
1291 {
1292   unsigned long prev_pinfo;
1293
1294   prev_pinfo = prev_insn.insn_mo->pinfo;
1295   if (! mips_opts.noreorder
1296       && mips_opts.isa < 4
1297       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1298           || (! gpr_interlocks
1299               && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1300     {
1301       /* A load from a coprocessor or from memory.  All load
1302          delays delay the use of general register rt for one
1303          instruction on the r3000.  The r6000 and r4000 use
1304          interlocks.  */
1305       /* Itbl support may require additional care here. */
1306       know (prev_pinfo & INSN_WRITE_GPR_T);
1307       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1308         return 1;
1309     }
1310
1311   return 0;
1312 }
1313
1314 /* Mark instruction labels in mips16 mode.  This permits the linker to
1315    handle them specially, such as generating jalx instructions when
1316    needed.  We also make them odd for the duration of the assembly, in
1317    order to generate the right sort of code.  We will make them even
1318    in the adjust_symtab routine, while leaving them marked.  This is
1319    convenient for the debugger and the disassembler.  The linker knows
1320    to make them odd again.  */
1321
1322 static void
1323 mips16_mark_labels ()
1324 {
1325   if (mips_opts.mips16)
1326     {
1327       struct insn_label_list *l;
1328
1329       for (l = insn_labels; l != NULL; l = l->next)
1330         {
1331 #ifdef OBJ_ELF
1332           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1333             S_SET_OTHER (l->label, STO_MIPS16);
1334 #endif
1335           if ((l->label->sy_value.X_add_number & 1) == 0)
1336             ++l->label->sy_value.X_add_number;
1337         }
1338     }
1339 }
1340
1341 /* Output an instruction.  PLACE is where to put the instruction; if
1342    it is NULL, this uses frag_more to get room.  IP is the instruction
1343    information.  ADDRESS_EXPR is an operand of the instruction to be
1344    used with RELOC_TYPE.  */
1345
1346 static void
1347 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1348      char *place;
1349      struct mips_cl_insn *ip;
1350      expressionS *address_expr;
1351      bfd_reloc_code_real_type reloc_type;
1352      boolean unmatched_hi;
1353 {
1354   register unsigned long prev_pinfo, pinfo;
1355   char *f;
1356   fixS *fixp;
1357   int nops = 0;
1358
1359   /* Mark instruction labels in mips16 mode.  */
1360   if (mips_opts.mips16)
1361     mips16_mark_labels ();
1362
1363   prev_pinfo = prev_insn.insn_mo->pinfo;
1364   pinfo = ip->insn_mo->pinfo;
1365
1366   if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1367     {
1368       int prev_prev_nop;
1369
1370       /* If the previous insn required any delay slots, see if we need
1371          to insert a NOP or two.  There are eight kinds of possible
1372          hazards, of which an instruction can have at most one type.
1373          (1) a load from memory delay
1374          (2) a load from a coprocessor delay
1375          (3) an unconditional branch delay
1376          (4) a conditional branch delay
1377          (5) a move to coprocessor register delay
1378          (6) a load coprocessor register from memory delay
1379          (7) a coprocessor condition code delay
1380          (8) a HI/LO special register delay
1381
1382          There are a lot of optimizations we could do that we don't.
1383          In particular, we do not, in general, reorder instructions.
1384          If you use gcc with optimization, it will reorder
1385          instructions and generally do much more optimization then we
1386          do here; repeating all that work in the assembler would only
1387          benefit hand written assembly code, and does not seem worth
1388          it.  */
1389
1390       /* This is how a NOP is emitted.  */
1391 #define emit_nop()                                      \
1392   (mips_opts.mips16                                     \
1393    ? md_number_to_chars (frag_more (2), 0x6500, 2)      \
1394    : md_number_to_chars (frag_more (4), 0, 4))
1395
1396       /* The previous insn might require a delay slot, depending upon
1397          the contents of the current insn.  */
1398       if (! mips_opts.mips16
1399           && mips_opts.isa < 4
1400           && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1401                && ! cop_interlocks)
1402               || (! gpr_interlocks
1403                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1404         {
1405           /* A load from a coprocessor or from memory.  All load
1406              delays delay the use of general register rt for one
1407              instruction on the r3000.  The r6000 and r4000 use
1408              interlocks.  */
1409           /* Itbl support may require additional care here. */
1410           know (prev_pinfo & INSN_WRITE_GPR_T);
1411           if (mips_optimize == 0
1412               || insn_uses_reg (ip,
1413                                 ((prev_insn.insn_opcode >> OP_SH_RT)
1414                                  & OP_MASK_RT),
1415                                 MIPS_GR_REG))
1416             ++nops;
1417         }
1418       else if (! mips_opts.mips16
1419                && mips_opts.isa < 4
1420                && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1421                     && ! cop_interlocks)
1422                    || (mips_opts.isa < 2
1423                        && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1424         {
1425           /* A generic coprocessor delay.  The previous instruction
1426              modified a coprocessor general or control register.  If
1427              it modified a control register, we need to avoid any
1428              coprocessor instruction (this is probably not always
1429              required, but it sometimes is).  If it modified a general
1430              register, we avoid using that register.
1431
1432              On the r6000 and r4000 loading a coprocessor register
1433              from memory is interlocked, and does not require a delay.
1434
1435              This case is not handled very well.  There is no special
1436              knowledge of CP0 handling, and the coprocessors other
1437              than the floating point unit are not distinguished at
1438              all.  */
1439           /* Itbl support may require additional care here. FIXME!
1440              Need to modify this to include knowledge about 
1441              user specified delays!  */
1442           if (prev_pinfo & INSN_WRITE_FPR_T)
1443             {
1444               if (mips_optimize == 0
1445                   || insn_uses_reg (ip,
1446                                     ((prev_insn.insn_opcode >> OP_SH_FT)
1447                                      & OP_MASK_FT),
1448                                     MIPS_FP_REG))
1449                 ++nops;
1450             }
1451           else if (prev_pinfo & INSN_WRITE_FPR_S)
1452             {
1453               if (mips_optimize == 0
1454                   || insn_uses_reg (ip,
1455                                     ((prev_insn.insn_opcode >> OP_SH_FS)
1456                                      & OP_MASK_FS),
1457                                     MIPS_FP_REG))
1458                 ++nops;
1459             }
1460           else
1461             {
1462               /* We don't know exactly what the previous instruction
1463                  does.  If the current instruction uses a coprocessor
1464                  register, we must insert a NOP.  If previous
1465                  instruction may set the condition codes, and the
1466                  current instruction uses them, we must insert two
1467                  NOPS.  */
1468               /* Itbl support may require additional care here. */
1469               if (mips_optimize == 0
1470                   || ((prev_pinfo & INSN_WRITE_COND_CODE)
1471                       && (pinfo & INSN_READ_COND_CODE)))
1472                 nops += 2;
1473               else if (pinfo & INSN_COP)
1474                 ++nops;
1475             }
1476         }
1477       else if (! mips_opts.mips16
1478                && mips_opts.isa < 4
1479                && (prev_pinfo & INSN_WRITE_COND_CODE)
1480                && ! cop_interlocks)
1481         {
1482           /* The previous instruction sets the coprocessor condition
1483              codes, but does not require a general coprocessor delay
1484              (this means it is a floating point comparison
1485              instruction).  If this instruction uses the condition
1486              codes, we need to insert a single NOP.  */
1487           /* Itbl support may require additional care here. */
1488           if (mips_optimize == 0
1489               || (pinfo & INSN_READ_COND_CODE))
1490             ++nops;
1491         }
1492       else if (prev_pinfo & INSN_READ_LO)
1493         {
1494           /* The previous instruction reads the LO register; if the
1495              current instruction writes to the LO register, we must
1496              insert two NOPS.  Some newer processors have interlocks.  */
1497           if (! hilo_interlocks
1498               && (mips_optimize == 0
1499                   || (pinfo & INSN_WRITE_LO)))
1500             nops += 2;
1501         }
1502       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1503         {
1504           /* The previous instruction reads the HI register; if the
1505              current instruction writes to the HI register, we must
1506              insert a NOP.  Some newer processors have interlocks.  */
1507           if (! hilo_interlocks
1508               && (mips_optimize == 0
1509                   || (pinfo & INSN_WRITE_HI)))
1510             nops += 2;
1511         }
1512
1513       /* If the previous instruction was in a noreorder section, then
1514          we don't want to insert the nop after all.  */
1515       /* Itbl support may require additional care here. */
1516       if (prev_insn_unreordered)
1517         nops = 0;
1518
1519       /* There are two cases which require two intervening
1520          instructions: 1) setting the condition codes using a move to
1521          coprocessor instruction which requires a general coprocessor
1522          delay and then reading the condition codes 2) reading the HI
1523          or LO register and then writing to it (except on processors
1524          which have interlocks).  If we are not already emitting a NOP
1525          instruction, we must check for these cases compared to the
1526          instruction previous to the previous instruction.  */
1527       if ((! mips_opts.mips16
1528            && mips_opts.isa < 4
1529            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1530            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1531            && (pinfo & INSN_READ_COND_CODE)
1532            && ! cop_interlocks)
1533           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1534               && (pinfo & INSN_WRITE_LO)
1535               && ! hilo_interlocks)
1536           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1537               && (pinfo & INSN_WRITE_HI)
1538               && ! hilo_interlocks))
1539         prev_prev_nop = 1;
1540       else
1541         prev_prev_nop = 0;
1542
1543       if (prev_prev_insn_unreordered)
1544         prev_prev_nop = 0;
1545
1546       if (prev_prev_nop && nops == 0)
1547         ++nops;
1548
1549       /* If we are being given a nop instruction, don't bother with
1550          one of the nops we would otherwise output.  This will only
1551          happen when a nop instruction is used with mips_optimize set
1552          to 0.  */
1553       if (nops > 0
1554           && ! mips_opts.noreorder
1555           && ip->insn_opcode == (mips_opts.mips16 ? 0x6500 : 0))
1556         --nops;
1557
1558       /* Now emit the right number of NOP instructions.  */
1559       if (nops > 0 && ! mips_opts.noreorder)
1560         {
1561           fragS *old_frag;
1562           unsigned long old_frag_offset;
1563           int i;
1564           struct insn_label_list *l;
1565
1566           old_frag = frag_now;
1567           old_frag_offset = frag_now_fix ();
1568
1569           for (i = 0; i < nops; i++)
1570             emit_nop ();
1571
1572           if (listing)
1573             {
1574               listing_prev_line ();
1575               /* We may be at the start of a variant frag.  In case we
1576                  are, make sure there is enough space for the frag
1577                  after the frags created by listing_prev_line.  The
1578                  argument to frag_grow here must be at least as large
1579                  as the argument to all other calls to frag_grow in
1580                  this file.  We don't have to worry about being in the
1581                  middle of a variant frag, because the variants insert
1582                  all needed nop instructions themselves.  */
1583               frag_grow (40);
1584             }
1585
1586           for (l = insn_labels; l != NULL; l = l->next)
1587             {
1588               assert (S_GET_SEGMENT (l->label) == now_seg);
1589               l->label->sy_frag = frag_now;
1590               S_SET_VALUE (l->label, (valueT) frag_now_fix ());
1591               /* mips16 text labels are stored as odd.  */
1592               if (mips_opts.mips16)
1593                 ++l->label->sy_value.X_add_number;
1594             }
1595
1596 #ifndef NO_ECOFF_DEBUGGING
1597           if (ECOFF_DEBUGGING)
1598             ecoff_fix_loc (old_frag, old_frag_offset);
1599 #endif
1600         }
1601       else if (prev_nop_frag != NULL)
1602         {
1603           /* We have a frag holding nops we may be able to remove.  If
1604              we don't need any nops, we can decrease the size of
1605              prev_nop_frag by the size of one instruction.  If we do
1606              need some nops, we count them in prev_nops_required. */
1607           if (prev_nop_frag_since == 0)
1608             {
1609               if (nops == 0)
1610                 {
1611                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1612                   --prev_nop_frag_holds;
1613                 }
1614               else
1615                 prev_nop_frag_required += nops;
1616             }
1617           else
1618             {
1619               if (prev_prev_nop == 0)
1620                 {
1621                   prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1622                   --prev_nop_frag_holds;
1623                 }
1624               else
1625                 ++prev_nop_frag_required;
1626             }
1627
1628           if (prev_nop_frag_holds <= prev_nop_frag_required)
1629             prev_nop_frag = NULL;
1630
1631           ++prev_nop_frag_since;
1632
1633           /* Sanity check: by the time we reach the second instruction
1634              after prev_nop_frag, we should have used up all the nops
1635              one way or another.  */
1636           assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1637         }
1638     }
1639
1640   if (reloc_type > BFD_RELOC_UNUSED)
1641     {
1642       /* We need to set up a variant frag.  */
1643       assert (mips_opts.mips16 && address_expr != NULL);
1644       f = frag_var (rs_machine_dependent, 4, 0,
1645                     RELAX_MIPS16_ENCODE (reloc_type - BFD_RELOC_UNUSED,
1646                                          mips16_small, mips16_ext,
1647                                          (prev_pinfo
1648                                           & INSN_UNCOND_BRANCH_DELAY),
1649                                          (prev_insn_reloc_type
1650                                           == BFD_RELOC_MIPS16_JMP)),
1651                     make_expr_symbol (address_expr), (offsetT) 0,
1652                     (char *) NULL);
1653     }
1654   else if (place != NULL)
1655     f = place;
1656   else if (mips_opts.mips16
1657            && ! ip->use_extend
1658            && reloc_type != BFD_RELOC_MIPS16_JMP)
1659     {
1660       /* Make sure there is enough room to swap this instruction with
1661          a following jump instruction.  */
1662       frag_grow (6);
1663       f = frag_more (2);
1664     }
1665   else
1666     {
1667       if (mips_opts.mips16
1668           && mips_opts.noreorder
1669           && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1670         as_warn ("extended instruction in delay slot");
1671
1672       f = frag_more (4);
1673     }
1674
1675   fixp = NULL;
1676   if (address_expr != NULL && reloc_type < BFD_RELOC_UNUSED)
1677     {
1678       if (address_expr->X_op == O_constant)
1679         {
1680           switch (reloc_type)
1681             {
1682             case BFD_RELOC_32:
1683               ip->insn_opcode |= address_expr->X_add_number;
1684               break;
1685
1686             case BFD_RELOC_LO16:
1687               ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1688               break;
1689
1690             case BFD_RELOC_MIPS_JMP:
1691               if ((address_expr->X_add_number & 3) != 0)
1692                 as_bad ("jump to misaligned address (0x%lx)",
1693                         (unsigned long) address_expr->X_add_number);
1694               ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1695               break;
1696
1697             case BFD_RELOC_MIPS16_JMP:
1698               if ((address_expr->X_add_number & 3) != 0)
1699                 as_bad ("jump to misaligned address (0x%lx)",
1700                         (unsigned long) address_expr->X_add_number);
1701               ip->insn_opcode |=
1702                 (((address_expr->X_add_number & 0x7c0000) << 3)
1703                  | ((address_expr->X_add_number & 0xf800000) >> 7)
1704                  | ((address_expr->X_add_number & 0x3fffc) >> 2));
1705               break;
1706
1707             case BFD_RELOC_16_PCREL_S2:
1708               goto need_reloc;
1709
1710             default:
1711               internalError ();
1712             }
1713         }
1714       else
1715         {
1716         need_reloc:
1717           /* Don't generate a reloc if we are writing into a variant
1718              frag.  */
1719           if (place == NULL)
1720             {
1721               fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1722                                   address_expr,
1723                                   reloc_type == BFD_RELOC_16_PCREL_S2,
1724                                   reloc_type);
1725               if (unmatched_hi)
1726                 {
1727                   struct mips_hi_fixup *hi_fixup;
1728
1729                   assert (reloc_type == BFD_RELOC_HI16_S);
1730                   hi_fixup = ((struct mips_hi_fixup *)
1731                               xmalloc (sizeof (struct mips_hi_fixup)));
1732                   hi_fixup->fixp = fixp;
1733                   hi_fixup->seg = now_seg;
1734                   hi_fixup->next = mips_hi_fixup_list;
1735                   mips_hi_fixup_list = hi_fixup;
1736                 }
1737             }
1738         }
1739     }
1740
1741   if (! mips_opts.mips16)
1742     md_number_to_chars (f, ip->insn_opcode, 4);
1743   else if (reloc_type == BFD_RELOC_MIPS16_JMP)
1744     {
1745       md_number_to_chars (f, ip->insn_opcode >> 16, 2);
1746       md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
1747     }
1748   else
1749     {
1750       if (ip->use_extend)
1751         {
1752           md_number_to_chars (f, 0xf000 | ip->extend, 2);
1753           f += 2;
1754         }
1755       md_number_to_chars (f, ip->insn_opcode, 2);
1756     }
1757
1758   /* Update the register mask information.  */
1759   if (! mips_opts.mips16)
1760     {
1761       if (pinfo & INSN_WRITE_GPR_D)
1762         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
1763       if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
1764         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
1765       if (pinfo & INSN_READ_GPR_S)
1766         mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
1767       if (pinfo & INSN_WRITE_GPR_31)
1768         mips_gprmask |= 1 << 31;
1769       if (pinfo & INSN_WRITE_FPR_D)
1770         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
1771       if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
1772         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
1773       if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
1774         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
1775       if ((pinfo & INSN_READ_FPR_R) != 0)
1776         mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
1777       if (pinfo & INSN_COP)
1778         {
1779           /* We don't keep enough information to sort these cases out. 
1780              The itbl support does keep this information however, although 
1781              we currently don't support itbl fprmats as part of the cop 
1782              instruction.  May want to add this support in the future. */
1783         }
1784       /* Never set the bit for $0, which is always zero.  */
1785       mips_gprmask &=~ 1 << 0;
1786     }
1787   else
1788     {
1789       if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
1790         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
1791                               & MIPS16OP_MASK_RX);
1792       if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
1793         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
1794                               & MIPS16OP_MASK_RY);
1795       if (pinfo & MIPS16_INSN_WRITE_Z)
1796         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
1797                               & MIPS16OP_MASK_RZ);
1798       if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
1799         mips_gprmask |= 1 << TREG;
1800       if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
1801         mips_gprmask |= 1 << SP;
1802       if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
1803         mips_gprmask |= 1 << RA;
1804       if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
1805         mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
1806       if (pinfo & MIPS16_INSN_READ_Z)
1807         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1808                               & MIPS16OP_MASK_MOVE32Z);
1809       if (pinfo & MIPS16_INSN_READ_GPR_X)
1810         mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1811                               & MIPS16OP_MASK_REGR32);
1812     }
1813
1814   if (place == NULL && ! mips_opts.noreorder)
1815     {
1816       /* Filling the branch delay slot is more complex.  We try to
1817          switch the branch with the previous instruction, which we can
1818          do if the previous instruction does not set up a condition
1819          that the branch tests and if the branch is not itself the
1820          target of any branch.  */
1821       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
1822           || (pinfo & INSN_COND_BRANCH_DELAY))
1823         {
1824           if (mips_optimize < 2
1825               /* If we have seen .set volatile or .set nomove, don't
1826                  optimize.  */
1827               || mips_opts.nomove != 0
1828               /* If we had to emit any NOP instructions, then we
1829                  already know we can not swap.  */
1830               || nops != 0
1831               /* If we don't even know the previous insn, we can not
1832                  swap. */
1833               || ! prev_insn_valid
1834               /* If the previous insn is already in a branch delay
1835                  slot, then we can not swap.  */
1836               || prev_insn_is_delay_slot
1837               /* If the previous previous insn was in a .set
1838                  noreorder, we can't swap.  Actually, the MIPS
1839                  assembler will swap in this situation.  However, gcc
1840                  configured -with-gnu-as will generate code like
1841                    .set noreorder
1842                    lw   $4,XXX
1843                    .set reorder
1844                    INSN
1845                    bne  $4,$0,foo
1846                  in which we can not swap the bne and INSN.  If gcc is
1847                  not configured -with-gnu-as, it does not output the
1848                  .set pseudo-ops.  We don't have to check
1849                  prev_insn_unreordered, because prev_insn_valid will
1850                  be 0 in that case.  We don't want to use
1851                  prev_prev_insn_valid, because we do want to be able
1852                  to swap at the start of a function.  */
1853               || prev_prev_insn_unreordered
1854               /* If the branch is itself the target of a branch, we
1855                  can not swap.  We cheat on this; all we check for is
1856                  whether there is a label on this instruction.  If
1857                  there are any branches to anything other than a
1858                  label, users must use .set noreorder.  */
1859               || insn_labels != NULL
1860               /* If the previous instruction is in a variant frag, we
1861                  can not do the swap.  This does not apply to the
1862                  mips16, which uses variant frags for different
1863                  purposes.  */
1864               || (! mips_opts.mips16
1865                   && prev_insn_frag->fr_type == rs_machine_dependent)
1866               /* If the branch reads the condition codes, we don't
1867                  even try to swap, because in the sequence
1868                    ctc1 $X,$31
1869                    INSN
1870                    INSN
1871                    bc1t LABEL
1872                  we can not swap, and I don't feel like handling that
1873                  case.  */
1874               || (! mips_opts.mips16
1875                   && mips_opts.isa < 4
1876                   && (pinfo & INSN_READ_COND_CODE))
1877               /* We can not swap with an instruction that requires a
1878                  delay slot, becase the target of the branch might
1879                  interfere with that instruction.  */
1880               || (! mips_opts.mips16
1881                   && mips_opts.isa < 4
1882                   && (prev_pinfo
1883               /* Itbl support may require additional care here. */
1884                       & (INSN_LOAD_COPROC_DELAY
1885                          | INSN_COPROC_MOVE_DELAY
1886                          | INSN_WRITE_COND_CODE)))
1887               || (! hilo_interlocks
1888                   && (prev_pinfo
1889                       & (INSN_READ_LO
1890                          | INSN_READ_HI)))
1891               || (! mips_opts.mips16
1892                   && ! gpr_interlocks
1893                   && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
1894               || (! mips_opts.mips16
1895                   && mips_opts.isa < 2
1896                   /* Itbl support may require additional care here. */
1897                   && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
1898               /* We can not swap with a branch instruction.  */
1899               || (prev_pinfo
1900                   & (INSN_UNCOND_BRANCH_DELAY
1901                      | INSN_COND_BRANCH_DELAY
1902                      | INSN_COND_BRANCH_LIKELY))
1903               /* We do not swap with a trap instruction, since it
1904                  complicates trap handlers to have the trap
1905                  instruction be in a delay slot.  */
1906               || (prev_pinfo & INSN_TRAP)
1907               /* If the branch reads a register that the previous
1908                  instruction sets, we can not swap.  */
1909               || (! mips_opts.mips16
1910                   && (prev_pinfo & INSN_WRITE_GPR_T)
1911                   && insn_uses_reg (ip,
1912                                     ((prev_insn.insn_opcode >> OP_SH_RT)
1913                                      & OP_MASK_RT),
1914                                     MIPS_GR_REG))
1915               || (! mips_opts.mips16
1916                   && (prev_pinfo & INSN_WRITE_GPR_D)
1917                   && insn_uses_reg (ip,
1918                                     ((prev_insn.insn_opcode >> OP_SH_RD)
1919                                      & OP_MASK_RD),
1920                                     MIPS_GR_REG))
1921               || (mips_opts.mips16
1922                   && (((prev_pinfo & MIPS16_INSN_WRITE_X)
1923                        && insn_uses_reg (ip,
1924                                          ((prev_insn.insn_opcode
1925                                            >> MIPS16OP_SH_RX)
1926                                           & MIPS16OP_MASK_RX),
1927                                          MIPS16_REG))
1928                       || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
1929                           && insn_uses_reg (ip,
1930                                             ((prev_insn.insn_opcode
1931                                               >> MIPS16OP_SH_RY)
1932                                              & MIPS16OP_MASK_RY),
1933                                             MIPS16_REG))
1934                       || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
1935                           && insn_uses_reg (ip,
1936                                             ((prev_insn.insn_opcode
1937                                               >> MIPS16OP_SH_RZ)
1938                                              & MIPS16OP_MASK_RZ),
1939                                             MIPS16_REG))
1940                       || ((prev_pinfo & MIPS16_INSN_WRITE_T)
1941                           && insn_uses_reg (ip, TREG, MIPS_GR_REG))
1942                       || ((prev_pinfo & MIPS16_INSN_WRITE_31)
1943                           && insn_uses_reg (ip, RA, MIPS_GR_REG))
1944                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
1945                           && insn_uses_reg (ip,
1946                                             MIPS16OP_EXTRACT_REG32R (prev_insn.
1947                                                                      insn_opcode),
1948                                             MIPS_GR_REG))))
1949               /* If the branch writes a register that the previous
1950                  instruction sets, we can not swap (we know that
1951                  branches write only to RD or to $31).  */
1952               || (! mips_opts.mips16
1953                   && (prev_pinfo & INSN_WRITE_GPR_T)
1954                   && (((pinfo & INSN_WRITE_GPR_D)
1955                        && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
1956                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
1957                       || ((pinfo & INSN_WRITE_GPR_31)
1958                           && (((prev_insn.insn_opcode >> OP_SH_RT)
1959                                & OP_MASK_RT)
1960                               == 31))))
1961               || (! mips_opts.mips16
1962                   && (prev_pinfo & INSN_WRITE_GPR_D)
1963                   && (((pinfo & INSN_WRITE_GPR_D)
1964                        && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
1965                            == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
1966                       || ((pinfo & INSN_WRITE_GPR_31)
1967                           && (((prev_insn.insn_opcode >> OP_SH_RD)
1968                                & OP_MASK_RD)
1969                               == 31))))
1970               || (mips_opts.mips16
1971                   && (pinfo & MIPS16_INSN_WRITE_31)
1972                   && ((prev_pinfo & MIPS16_INSN_WRITE_31)
1973                       || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
1974                           && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
1975                               == RA))))
1976               /* If the branch writes a register that the previous
1977                  instruction reads, we can not swap (we know that
1978                  branches only write to RD or to $31).  */
1979               || (! mips_opts.mips16
1980                   && (pinfo & INSN_WRITE_GPR_D)
1981                   && insn_uses_reg (&prev_insn,
1982                                     ((ip->insn_opcode >> OP_SH_RD)
1983                                      & OP_MASK_RD),
1984                                     MIPS_GR_REG))
1985               || (! mips_opts.mips16
1986                   && (pinfo & INSN_WRITE_GPR_31)
1987                   && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
1988               || (mips_opts.mips16
1989                   && (pinfo & MIPS16_INSN_WRITE_31)
1990                   && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
1991               /* If we are generating embedded PIC code, the branch
1992                  might be expanded into a sequence which uses $at, so
1993                  we can't swap with an instruction which reads it.  */
1994               || (mips_pic == EMBEDDED_PIC
1995                   && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
1996               /* If the previous previous instruction has a load
1997                  delay, and sets a register that the branch reads, we
1998                  can not swap.  */
1999               || (! mips_opts.mips16
2000                   && mips_opts.isa < 4
2001               /* Itbl support may require additional care here. */
2002                   && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2003                       || (! gpr_interlocks
2004                           && (prev_prev_insn.insn_mo->pinfo
2005                               & INSN_LOAD_MEMORY_DELAY)))
2006                   && insn_uses_reg (ip,
2007                                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2008                                      & OP_MASK_RT),
2009                                     MIPS_GR_REG))
2010               /* If one instruction sets a condition code and the
2011                  other one uses a condition code, we can not swap.  */
2012               || ((pinfo & INSN_READ_COND_CODE)
2013                   && (prev_pinfo & INSN_WRITE_COND_CODE))
2014               || ((pinfo & INSN_WRITE_COND_CODE)
2015                   && (prev_pinfo & INSN_READ_COND_CODE))
2016               /* If the previous instruction uses the PC, we can not
2017                  swap.  */
2018               || (mips_opts.mips16
2019                   && (prev_pinfo & MIPS16_INSN_READ_PC))
2020               /* If the previous instruction was extended, we can not
2021                  swap.  */
2022               || (mips_opts.mips16 && prev_insn_extended)
2023               /* If the previous instruction had a fixup in mips16
2024                  mode, we can not swap.  This normally means that the
2025                  previous instruction was a 4 byte branch anyhow.  */
2026               || (mips_opts.mips16 && prev_insn_fixp))
2027             {
2028               /* We could do even better for unconditional branches to
2029                  portions of this object file; we could pick up the
2030                  instruction at the destination, put it in the delay
2031                  slot, and bump the destination address.  */
2032               emit_nop ();
2033               /* Update the previous insn information.  */
2034               prev_prev_insn = *ip;
2035               prev_insn.insn_mo = &dummy_opcode;
2036             }
2037           else
2038             {
2039               /* It looks like we can actually do the swap.  */
2040               if (! mips_opts.mips16)
2041                 {
2042                   char *prev_f;
2043                   char temp[4];
2044
2045                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2046                   memcpy (temp, prev_f, 4);
2047                   memcpy (prev_f, f, 4);
2048                   memcpy (f, temp, 4);
2049                   if (prev_insn_fixp)
2050                     {
2051                       prev_insn_fixp->fx_frag = frag_now;
2052                       prev_insn_fixp->fx_where = f - frag_now->fr_literal;
2053                     }
2054                   if (fixp)
2055                     {
2056                       fixp->fx_frag = prev_insn_frag;
2057                       fixp->fx_where = prev_insn_where;
2058                     }
2059                 }
2060               else
2061                 {
2062                   char *prev_f;
2063                   char temp[2];
2064
2065                   assert (prev_insn_fixp == NULL);
2066                   prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2067                   memcpy (temp, prev_f, 2);
2068                   memcpy (prev_f, f, 2);
2069                   if (reloc_type != BFD_RELOC_MIPS16_JMP)
2070                     {
2071                       assert (reloc_type == BFD_RELOC_UNUSED);
2072                       memcpy (f, temp, 2);
2073                     }
2074                   else
2075                     {
2076                       memcpy (f, f + 2, 2);
2077                       memcpy (f + 2, temp, 2);
2078                     }
2079                   if (fixp)
2080                     {
2081                       fixp->fx_frag = prev_insn_frag;
2082                       fixp->fx_where = prev_insn_where;
2083                     }
2084                 }
2085
2086               /* Update the previous insn information; leave prev_insn
2087                  unchanged.  */
2088               prev_prev_insn = *ip;
2089             }
2090           prev_insn_is_delay_slot = 1;
2091
2092           /* If that was an unconditional branch, forget the previous
2093              insn information.  */
2094           if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2095             {
2096               prev_prev_insn.insn_mo = &dummy_opcode;
2097               prev_insn.insn_mo = &dummy_opcode;
2098             }
2099
2100           prev_insn_fixp = NULL;
2101           prev_insn_reloc_type = BFD_RELOC_UNUSED;
2102           prev_insn_extended = 0;
2103         }
2104       else if (pinfo & INSN_COND_BRANCH_LIKELY)
2105         {
2106           /* We don't yet optimize a branch likely.  What we should do
2107              is look at the target, copy the instruction found there
2108              into the delay slot, and increment the branch to jump to
2109              the next instruction.  */
2110           emit_nop ();
2111           /* Update the previous insn information.  */
2112           prev_prev_insn = *ip;
2113           prev_insn.insn_mo = &dummy_opcode;
2114           prev_insn_fixp = NULL;
2115           prev_insn_reloc_type = BFD_RELOC_UNUSED;
2116           prev_insn_extended = 0;
2117         }
2118       else
2119         {
2120           /* Update the previous insn information.  */
2121           if (nops > 0)
2122             prev_prev_insn.insn_mo = &dummy_opcode;
2123           else
2124             prev_prev_insn = prev_insn;
2125           prev_insn = *ip;
2126
2127           /* Any time we see a branch, we always fill the delay slot
2128              immediately; since this insn is not a branch, we know it
2129              is not in a delay slot.  */
2130           prev_insn_is_delay_slot = 0;
2131
2132           prev_insn_fixp = fixp;
2133           prev_insn_reloc_type = reloc_type;
2134           if (mips_opts.mips16)
2135             prev_insn_extended = (ip->use_extend
2136                                   || reloc_type > BFD_RELOC_UNUSED);
2137         }
2138
2139       prev_prev_insn_unreordered = prev_insn_unreordered;
2140       prev_insn_unreordered = 0;
2141       prev_insn_frag = frag_now;
2142       prev_insn_where = f - frag_now->fr_literal;
2143       prev_insn_valid = 1;
2144     }
2145   else if (place == NULL)
2146     {
2147       /* We need to record a bit of information even when we are not
2148          reordering, in order to determine the base address for mips16
2149          PC relative relocs.  */
2150       prev_prev_insn = prev_insn;
2151       prev_insn = *ip;
2152       prev_insn_reloc_type = reloc_type;
2153       prev_prev_insn_unreordered = prev_insn_unreordered;
2154       prev_insn_unreordered = 1;
2155     }
2156
2157   /* We just output an insn, so the next one doesn't have a label.  */
2158   mips_clear_insn_labels ();
2159 }
2160
2161 /* This function forgets that there was any previous instruction or
2162    label.  If PRESERVE is non-zero, it remembers enough information to
2163    know whether nops are needed before a noreorder section. */
2164
2165 static void
2166 mips_no_prev_insn (preserve)
2167      int preserve;
2168 {
2169   if (! preserve)
2170     {
2171       prev_insn.insn_mo = &dummy_opcode;
2172       prev_prev_insn.insn_mo = &dummy_opcode;
2173       prev_nop_frag = NULL;
2174       prev_nop_frag_holds = 0;
2175       prev_nop_frag_required = 0;
2176       prev_nop_frag_since = 0;
2177     }
2178   prev_insn_valid = 0;
2179   prev_insn_is_delay_slot = 0;
2180   prev_insn_unreordered = 0;
2181   prev_insn_extended = 0;
2182   prev_insn_reloc_type = BFD_RELOC_UNUSED;
2183   prev_prev_insn_unreordered = 0;
2184   mips_clear_insn_labels ();
2185 }
2186
2187 /* This function must be called whenever we turn on noreorder or emit
2188    something other than instructions.  It inserts any NOPS which might
2189    be needed by the previous instruction, and clears the information
2190    kept for the previous instructions.  The INSNS parameter is true if
2191    instructions are to follow. */
2192
2193 static void
2194 mips_emit_delays (insns)
2195      boolean insns;
2196 {
2197   if (! mips_opts.noreorder)
2198     {
2199       int nops;
2200
2201       nops = 0;
2202       if ((! mips_opts.mips16
2203            && mips_opts.isa < 4
2204            && (! cop_interlocks
2205                && (prev_insn.insn_mo->pinfo
2206                    & (INSN_LOAD_COPROC_DELAY
2207                       | INSN_COPROC_MOVE_DELAY
2208                       | INSN_WRITE_COND_CODE))))
2209           || (! hilo_interlocks
2210               && (prev_insn.insn_mo->pinfo
2211                   & (INSN_READ_LO
2212                      | INSN_READ_HI)))
2213           || (! mips_opts.mips16
2214               && ! gpr_interlocks
2215               && (prev_insn.insn_mo->pinfo 
2216                   & INSN_LOAD_MEMORY_DELAY))
2217           || (! mips_opts.mips16
2218               && mips_opts.isa < 2
2219               && (prev_insn.insn_mo->pinfo
2220                   & INSN_COPROC_MEMORY_DELAY)))
2221         {
2222           /* Itbl support may require additional care here. */
2223           ++nops;
2224           if ((! mips_opts.mips16
2225                && mips_opts.isa < 4
2226                && (! cop_interlocks
2227                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2228               || (! hilo_interlocks
2229                   && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2230                       || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2231             ++nops;
2232
2233           if (prev_insn_unreordered)
2234             nops = 0;
2235         }
2236       else if ((! mips_opts.mips16
2237                 && mips_opts.isa < 4
2238                 && (! cop_interlocks
2239                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2240                || (! hilo_interlocks
2241                    && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2242                        || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2243         {
2244           /* Itbl support may require additional care here. */
2245           if (! prev_prev_insn_unreordered)
2246             ++nops;
2247         }
2248
2249       if (nops > 0)
2250         {
2251           struct insn_label_list *l;
2252
2253           if (insns)
2254             {
2255               /* Record the frag which holds the nop instructions, so
2256                  that we can remove them if we don't need them.  */
2257               frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2258               prev_nop_frag = frag_now;
2259               prev_nop_frag_holds = nops;
2260               prev_nop_frag_required = 0;
2261               prev_nop_frag_since = 0;
2262             }
2263
2264           for (; nops > 0; --nops)
2265             emit_nop ();
2266
2267           if (insns)
2268             {
2269               /* Move on to a new frag, so that it is safe to simply
2270                  decrease the size of prev_nop_frag. */
2271               frag_wane (frag_now);
2272               frag_new (0);
2273             }
2274
2275           for (l = insn_labels; l != NULL; l = l->next)
2276             {
2277               assert (S_GET_SEGMENT (l->label) == now_seg);
2278               l->label->sy_frag = frag_now;
2279               S_SET_VALUE (l->label, (valueT) frag_now_fix ());
2280               /* mips16 text labels are stored as odd.  */
2281               if (mips_opts.mips16)
2282                 ++l->label->sy_value.X_add_number;
2283             }
2284         }
2285     }
2286
2287   /* Mark instruction labels in mips16 mode.  */
2288   if (mips_opts.mips16 && insns)
2289     mips16_mark_labels ();
2290
2291   mips_no_prev_insn (insns);
2292 }
2293
2294 /* Build an instruction created by a macro expansion.  This is passed
2295    a pointer to the count of instructions created so far, an
2296    expression, the name of the instruction to build, an operand format
2297    string, and corresponding arguments.  */
2298
2299 #ifdef USE_STDARG
2300 static void
2301 macro_build (char *place,
2302              int *counter,
2303              expressionS * ep,
2304              const char *name,
2305              const char *fmt,
2306              ...)
2307 #else
2308 static void
2309 macro_build (place, counter, ep, name, fmt, va_alist)
2310      char *place;
2311      int *counter;
2312      expressionS *ep;
2313      const char *name;
2314      const char *fmt;
2315      va_dcl
2316 #endif
2317 {
2318   struct mips_cl_insn insn;
2319   bfd_reloc_code_real_type r;
2320   va_list args;
2321   int insn_isa;
2322
2323 #ifdef USE_STDARG
2324   va_start (args, fmt);
2325 #else
2326   va_start (args);
2327 #endif
2328
2329   /*
2330    * If the macro is about to expand into a second instruction,
2331    * print a warning if needed. We need to pass ip as a parameter
2332    * to generate a better warning message here...
2333    */
2334   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2335     as_warn ("Macro instruction expanded into multiple instructions");
2336
2337   if (place == NULL)
2338     *counter += 1;              /* bump instruction counter */
2339
2340   if (mips_opts.mips16)
2341     {
2342       mips16_macro_build (place, counter, ep, name, fmt, args);
2343       va_end (args);
2344       return;
2345     }
2346
2347   r = BFD_RELOC_UNUSED;
2348   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2349   assert (insn.insn_mo);
2350   assert (strcmp (name, insn.insn_mo->name) == 0);
2351
2352   /* Search until we get a match for NAME.  */
2353   while (1)
2354     {
2355       if (insn.insn_mo->pinfo == INSN_MACRO)
2356         insn_isa = insn.insn_mo->match;
2357       else if ((insn.insn_mo->membership & INSN_ISA) == INSN_ISA1)
2358         insn_isa = 1;
2359       else if ((insn.insn_mo->membership & INSN_ISA) == INSN_ISA2)
2360         insn_isa = 2;
2361       else if ((insn.insn_mo->membership & INSN_ISA) == INSN_ISA3)
2362         insn_isa = 3;
2363       else if ((insn.insn_mo->membership & INSN_ISA) == INSN_ISA4)
2364         insn_isa = 4;
2365       else
2366         insn_isa = 15;
2367
2368       if (strcmp (fmt, insn.insn_mo->args) == 0
2369           && insn.insn_mo->pinfo != INSN_MACRO
2370           && (insn_isa <= mips_opts.isa
2371               || (mips_4650
2372                   && (insn.insn_mo->membership & INSN_4650) != 0)
2373               || (mips_4010
2374                   && (insn.insn_mo->membership & INSN_4010) != 0)
2375               || (mips_4100
2376                   && (insn.insn_mo->membership & INSN_4100) != 0)
2377               /* start-sanitize-r5900 */
2378               || (mips_5900
2379                   && (insn.insn_mo->membership & INSN_5900) != 0)
2380               /* end-sanitize-r5900 */
2381               || (mips_3900
2382                   && (insn.insn_mo->membership & INSN_3900) != 0))
2383           /* start-sanitize-r5900 */
2384           && (! mips_5900 || (insn.insn_mo->pinfo & FP_D) == 0)
2385           /* end-sanitize-r5900 */
2386           && (! mips_4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2387         break;
2388
2389       ++insn.insn_mo;
2390       assert (insn.insn_mo->name);
2391       assert (strcmp (name, insn.insn_mo->name) == 0);
2392     }
2393
2394   insn.insn_opcode = insn.insn_mo->match;
2395   for (;;)
2396     {
2397       switch (*fmt++)
2398         {
2399         case '\0':
2400           break;
2401
2402         case ',':
2403         case '(':
2404         case ')':
2405           continue;
2406
2407         case 't':
2408         case 'w':
2409         case 'E':
2410           insn.insn_opcode |= va_arg (args, int) << 16;
2411           continue;
2412
2413         case 'c':
2414         case 'T':
2415         case 'W':
2416           insn.insn_opcode |= va_arg (args, int) << 16;
2417           continue;
2418
2419         case 'd':
2420         case 'G':
2421           insn.insn_opcode |= va_arg (args, int) << 11;
2422           continue;
2423
2424         case 'V':
2425         case 'S':
2426           insn.insn_opcode |= va_arg (args, int) << 11;
2427           continue;
2428
2429         case 'z':
2430           continue;
2431
2432         case '<':
2433           insn.insn_opcode |= va_arg (args, int) << 6;
2434           continue;
2435
2436         case 'D':
2437           insn.insn_opcode |= va_arg (args, int) << 6;
2438           continue;
2439
2440         case 'B':
2441           insn.insn_opcode |= va_arg (args, int) << 6;
2442           continue;
2443
2444         case 'b':
2445         case 's':
2446         case 'r':
2447         case 'v':
2448           insn.insn_opcode |= va_arg (args, int) << 21;
2449           continue;
2450
2451         case 'i':
2452         case 'j':
2453         case 'o':
2454           r = (bfd_reloc_code_real_type) va_arg (args, int);
2455           assert (r == BFD_RELOC_MIPS_GPREL
2456                   || r == BFD_RELOC_MIPS_LITERAL
2457                   || r == BFD_RELOC_LO16
2458                   || r == BFD_RELOC_MIPS_GOT16
2459                   || r == BFD_RELOC_MIPS_CALL16
2460                   || r == BFD_RELOC_MIPS_GOT_LO16
2461                   || r == BFD_RELOC_MIPS_CALL_LO16
2462                   || (ep->X_op == O_subtract
2463                       && now_seg == text_section
2464                       && r == BFD_RELOC_PCREL_LO16));
2465           continue;
2466
2467         case 'u':
2468           r = (bfd_reloc_code_real_type) va_arg (args, int);
2469           assert (ep != NULL
2470                   && (ep->X_op == O_constant
2471                       || (ep->X_op == O_symbol
2472                           && (r == BFD_RELOC_HI16_S
2473                               || r == BFD_RELOC_HI16
2474                               || r == BFD_RELOC_MIPS_GOT_HI16
2475                               || r == BFD_RELOC_MIPS_CALL_HI16))
2476                       || (ep->X_op == O_subtract
2477                           && now_seg == text_section
2478                           && r == BFD_RELOC_PCREL_HI16_S)));
2479           if (ep->X_op == O_constant)
2480             {
2481               insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
2482               ep = NULL;
2483               r = BFD_RELOC_UNUSED;
2484             }
2485           continue;
2486
2487         case 'p':
2488           assert (ep != NULL);
2489           /*
2490            * This allows macro() to pass an immediate expression for
2491            * creating short branches without creating a symbol.
2492            * Note that the expression still might come from the assembly
2493            * input, in which case the value is not checked for range nor
2494            * is a relocation entry generated (yuck).
2495            */
2496           if (ep->X_op == O_constant)
2497             {
2498               insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2499               ep = NULL;
2500             }
2501           else
2502             r = BFD_RELOC_16_PCREL_S2;
2503           continue;
2504
2505         case 'a':
2506           assert (ep != NULL);
2507           r = BFD_RELOC_MIPS_JMP;
2508           continue;
2509
2510         default:
2511           internalError ();
2512         }
2513       break;
2514     }
2515   va_end (args);
2516   assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2517
2518   append_insn (place, &insn, ep, r, false);
2519 }
2520
2521 static void
2522 mips16_macro_build (place, counter, ep, name, fmt, args)
2523      char *place;
2524      int *counter;
2525      expressionS *ep;
2526      const char *name;
2527      const char *fmt;
2528      va_list args;
2529 {
2530   struct mips_cl_insn insn;
2531   bfd_reloc_code_real_type r;
2532
2533   r = BFD_RELOC_UNUSED;
2534   insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2535   assert (insn.insn_mo);
2536   assert (strcmp (name, insn.insn_mo->name) == 0);
2537
2538   while (strcmp (fmt, insn.insn_mo->args) != 0
2539          || insn.insn_mo->pinfo == INSN_MACRO)
2540     {
2541       ++insn.insn_mo;
2542       assert (insn.insn_mo->name);
2543       assert (strcmp (name, insn.insn_mo->name) == 0);
2544     }
2545
2546   insn.insn_opcode = insn.insn_mo->match;
2547   insn.use_extend = false;
2548
2549   for (;;)
2550     {
2551       int c;
2552
2553       c = *fmt++;
2554       switch (c)
2555         {
2556         case '\0':
2557           break;
2558
2559         case ',':
2560         case '(':
2561         case ')':
2562           continue;
2563
2564         case 'y':
2565         case 'w':
2566           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2567           continue;
2568
2569         case 'x':
2570         case 'v':
2571           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2572           continue;
2573
2574         case 'z':
2575           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2576           continue;
2577
2578         case 'Z':
2579           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2580           continue;
2581
2582         case '0':
2583         case 'S':
2584         case 'P':
2585         case 'R':
2586           continue;
2587
2588         case 'X':
2589           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2590           continue;
2591
2592         case 'Y':
2593           {
2594             int regno;
2595
2596             regno = va_arg (args, int);
2597             regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2598             insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2599           }
2600           continue;
2601
2602         case '<':
2603         case '>':
2604         case '4':
2605         case '5':
2606         case 'H':
2607         case 'W':
2608         case 'D':
2609         case 'j':
2610         case '8':
2611         case 'V':
2612         case 'C':
2613         case 'U':
2614         case 'k':
2615         case 'K':
2616         case 'p':
2617         case 'q':
2618           {
2619             assert (ep != NULL);
2620
2621             if (ep->X_op != O_constant)
2622               r = BFD_RELOC_UNUSED + c;
2623             else
2624               {
2625                 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
2626                               false, false, &insn.insn_opcode,
2627                               &insn.use_extend, &insn.extend);
2628                 ep = NULL;
2629                 r = BFD_RELOC_UNUSED;
2630               }
2631           }
2632           continue;
2633
2634         case '6':
2635           insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2636           continue;
2637         }
2638
2639       break;
2640     }
2641
2642   assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2643
2644   append_insn (place, &insn, ep, r, false);
2645 }
2646
2647 /*
2648  * Generate a "lui" instruction.
2649  */
2650 static void
2651 macro_build_lui (place, counter, ep, regnum)
2652      char *place;
2653      int *counter;
2654      expressionS *ep;
2655      int regnum;
2656 {
2657   expressionS high_expr;
2658   struct mips_cl_insn insn;
2659   bfd_reloc_code_real_type r;
2660   CONST char *name = "lui";
2661   CONST char *fmt = "t,u";
2662
2663   assert (! mips_opts.mips16);
2664
2665   if (place == NULL)
2666     high_expr = *ep;
2667   else
2668     {
2669       high_expr.X_op = O_constant;
2670       high_expr.X_add_number = ep->X_add_number;
2671     }
2672
2673   if (high_expr.X_op == O_constant)
2674     {
2675       /* we can compute the instruction now without a relocation entry */
2676       if (high_expr.X_add_number & 0x8000)
2677         high_expr.X_add_number += 0x10000;
2678       high_expr.X_add_number =
2679         ((unsigned long) high_expr.X_add_number >> 16) & 0xffff;
2680       r = BFD_RELOC_UNUSED;
2681     }
2682   else
2683     {
2684       assert (ep->X_op == O_symbol);
2685       /* _gp_disp is a special case, used from s_cpload.  */
2686       assert (mips_pic == NO_PIC
2687               || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
2688       r = BFD_RELOC_HI16_S;
2689     }
2690
2691   /*
2692    * If the macro is about to expand into a second instruction,
2693    * print a warning if needed. We need to pass ip as a parameter
2694    * to generate a better warning message here...
2695    */
2696   if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2697     as_warn ("Macro instruction expanded into multiple instructions");
2698
2699   if (place == NULL)
2700     *counter += 1;              /* bump instruction counter */
2701
2702   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2703   assert (insn.insn_mo);
2704   assert (strcmp (name, insn.insn_mo->name) == 0);
2705   assert (strcmp (fmt, insn.insn_mo->args) == 0);
2706
2707   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
2708   if (r == BFD_RELOC_UNUSED)
2709     {
2710       insn.insn_opcode |= high_expr.X_add_number;
2711       append_insn (place, &insn, NULL, r, false);
2712     }
2713   else
2714     append_insn (place, &insn, &high_expr, r, false);
2715 }
2716
2717 /*                      set_at()
2718  * Generates code to set the $at register to true (one)
2719  * if reg is less than the immediate expression.
2720  */
2721 static void
2722 set_at (counter, reg, unsignedp)
2723      int *counter;
2724      int reg;
2725      int unsignedp;
2726 {
2727   if (imm_expr.X_op == O_constant
2728       && imm_expr.X_add_number >= -0x8000
2729       && imm_expr.X_add_number < 0x8000)
2730     macro_build ((char *) NULL, counter, &imm_expr,
2731                  unsignedp ? "sltiu" : "slti",
2732                  "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
2733   else
2734     {
2735       load_register (counter, AT, &imm_expr, 0);
2736       macro_build ((char *) NULL, counter, NULL,
2737                    unsignedp ? "sltu" : "slt",
2738                    "d,v,t", AT, reg, AT);
2739     }
2740 }
2741
2742 /* Warn if an expression is not a constant.  */
2743
2744 static void
2745 check_absolute_expr (ip, ex)
2746      struct mips_cl_insn *ip;
2747      expressionS *ex;
2748 {
2749   if (ex->X_op == O_big)
2750     as_bad ("unsupported large constant");
2751   else if (ex->X_op != O_constant)
2752     as_bad ("Instruction %s requires absolute expression", ip->insn_mo->name);
2753 }
2754
2755 /* Count the leading zeroes by performing a binary chop. This is a
2756    bulky bit of source, but performance is a LOT better for the
2757    majority of values than a simple loop to count the bits:
2758        for (lcnt = 0; (lcnt < 32); lcnt++)
2759          if ((v) & (1 << (31 - lcnt)))
2760            break;
2761   However it is not code size friendly, and the gain will drop a bit
2762   on certain cached systems.
2763 */
2764 #define COUNT_TOP_ZEROES(v)             \
2765   (((v) & ~0xffff) == 0                 \
2766    ? ((v) & ~0xff) == 0                 \
2767      ? ((v) & ~0xf) == 0                \
2768        ? ((v) & ~0x3) == 0              \
2769          ? ((v) & ~0x1) == 0            \
2770            ? !(v)                       \
2771              ? 32                       \
2772              : 31                       \
2773            : 30                         \
2774          : ((v) & ~0x7) == 0            \
2775            ? 29                         \
2776            : 28                         \
2777        : ((v) & ~0x3f) == 0             \
2778          ? ((v) & ~0x1f) == 0           \
2779            ? 27                         \
2780            : 26                         \
2781          : ((v) & ~0x7f) == 0           \
2782            ? 25                         \
2783            : 24                         \
2784      : ((v) & ~0xfff) == 0              \
2785        ? ((v) & ~0x3ff) == 0            \
2786          ? ((v) & ~0x1ff) == 0          \
2787            ? 23                         \
2788            : 22                         \
2789          : ((v) & ~0x7ff) == 0          \
2790            ? 21                         \
2791            : 20                         \
2792        : ((v) & ~0x3fff) == 0           \
2793          ? ((v) & ~0x1fff) == 0         \
2794            ? 19                         \
2795            : 18                         \
2796          : ((v) & ~0x7fff) == 0         \
2797            ? 17                         \
2798            : 16                         \
2799    : ((v) & ~0xffffff) == 0             \
2800      ? ((v) & ~0xfffff) == 0            \
2801        ? ((v) & ~0x3ffff) == 0          \
2802          ? ((v) & ~0x1ffff) == 0        \
2803            ? 15                         \
2804            : 14                         \
2805          : ((v) & ~0x7ffff) == 0        \
2806            ? 13                         \
2807            : 12                         \
2808        : ((v) & ~0x3fffff) == 0         \
2809          ? ((v) & ~0x1fffff) == 0       \
2810            ? 11                         \
2811            : 10                         \
2812          : ((v) & ~0x7fffff) == 0       \
2813            ? 9                          \
2814            : 8                          \
2815      : ((v) & ~0xfffffff) == 0          \
2816        ? ((v) & ~0x3ffffff) == 0        \
2817          ? ((v) & ~0x1ffffff) == 0      \
2818            ? 7                          \
2819            : 6                          \
2820          : ((v) & ~0x7ffffff) == 0      \
2821            ? 5                          \
2822            : 4                          \
2823        : ((v) & ~0x3fffffff) == 0       \
2824          ? ((v) & ~0x1fffffff) == 0     \
2825            ? 3                          \
2826            : 2                          \
2827          : ((v) & ~0x7fffffff) == 0     \
2828            ? 1                          \
2829            : 0)
2830
2831 /*                      load_register()
2832  *  This routine generates the least number of instructions neccessary to load
2833  *  an absolute expression value into a register.
2834  */
2835 static void
2836 load_register (counter, reg, ep, dbl)
2837      int *counter;
2838      int reg;
2839      expressionS *ep;
2840      int dbl;
2841 {
2842   int freg;
2843   expressionS hi32, lo32;
2844
2845   if (ep->X_op != O_big)
2846     {
2847       assert (ep->X_op == O_constant);
2848       if (ep->X_add_number < 0x8000
2849           && (ep->X_add_number >= 0
2850               || (ep->X_add_number >= -0x8000
2851                   && (! dbl
2852                       || ! ep->X_unsigned
2853                       || sizeof (ep->X_add_number) > 4))))
2854         {
2855           /* We can handle 16 bit signed values with an addiu to
2856              $zero.  No need to ever use daddiu here, since $zero and
2857              the result are always correct in 32 bit mode.  */
2858           macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
2859                        (int) BFD_RELOC_LO16);
2860           return;
2861         }
2862       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
2863         {
2864           /* We can handle 16 bit unsigned values with an ori to
2865              $zero.  */
2866           macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
2867                        (int) BFD_RELOC_LO16);
2868           return;
2869         }
2870       else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
2871                  || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
2872                      == ~ (offsetT) 0x7fffffff))
2873                 && (! dbl
2874                     || ! ep->X_unsigned
2875                     || sizeof (ep->X_add_number) > 4
2876                     || (ep->X_add_number & 0x80000000) == 0))
2877                || ((mips_opts.isa < 3 || ! dbl)
2878                    && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
2879                || (mips_opts.isa < 3
2880                    && ! dbl
2881                    && ((ep->X_add_number &~ (offsetT) 0xffffffff)
2882                        == ~ (offsetT) 0xffffffff)))
2883         {
2884           /* 32 bit values require an lui.  */
2885           macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
2886                        (int) BFD_RELOC_HI16);
2887           if ((ep->X_add_number & 0xffff) != 0)
2888             macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
2889                          (int) BFD_RELOC_LO16);
2890           return;
2891         }
2892     }
2893
2894   /* The value is larger than 32 bits.  */
2895
2896   if (mips_opts.isa < 3)
2897     {
2898       as_bad ("Number larger than 32 bits");
2899       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
2900                    (int) BFD_RELOC_LO16);
2901       return;
2902     }
2903
2904   if (ep->X_op != O_big)
2905     {
2906       hi32 = *ep;
2907       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
2908       hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
2909       hi32.X_add_number &= 0xffffffff;
2910       lo32 = *ep;
2911       lo32.X_add_number &= 0xffffffff;
2912     }
2913   else
2914     {
2915       assert (ep->X_add_number > 2);
2916       if (ep->X_add_number == 3)
2917         generic_bignum[3] = 0;
2918       else if (ep->X_add_number > 4)
2919         as_bad ("Number larger than 64 bits");
2920       lo32.X_op = O_constant;
2921       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
2922       hi32.X_op = O_constant;
2923       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
2924     }
2925
2926   if (hi32.X_add_number == 0)
2927     freg = 0;
2928   else
2929     {
2930       int shift, bit;
2931       unsigned long hi, lo;
2932
2933       if (hi32.X_add_number == 0xffffffff)
2934         {
2935           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
2936             {
2937               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
2938                            reg, 0, (int) BFD_RELOC_LO16);
2939               return;
2940             }
2941           if (lo32.X_add_number & 0x80000000)
2942             {
2943               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
2944                            (int) BFD_RELOC_HI16);
2945               if (lo32.X_add_number & 0xffff)
2946                 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
2947                              reg, reg, (int) BFD_RELOC_LO16);
2948               return;
2949             }
2950         }
2951
2952       /* Check for 16bit shifted constant.  We know that hi32 is
2953          non-zero, so start the mask on the first bit of the hi32
2954          value.  */
2955       shift = 17;
2956       do
2957        {
2958          unsigned long himask, lomask;
2959
2960          if (shift < 32)
2961            {
2962              himask = 0xffff >> (32 - shift);
2963              lomask = (0xffff << shift) & 0xffffffff;
2964            }
2965          else
2966            {
2967              himask = 0xffff << (shift - 32);
2968              lomask = 0;
2969            }
2970          if ((hi32.X_add_number & ~ (offsetT) himask) == 0
2971              && (lo32.X_add_number & ~ (offsetT) lomask) == 0)
2972            {
2973              expressionS tmp;
2974
2975              tmp.X_op = O_constant;
2976              if (shift < 32)
2977                tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
2978                                    | (lo32.X_add_number >> shift));
2979              else
2980                tmp.X_add_number = hi32.X_add_number >> (shift - 32);
2981              macro_build ((char *) NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
2982                           (int) BFD_RELOC_LO16);
2983              macro_build ((char *) NULL, counter, NULL,
2984                           (shift >= 32) ? "dsll32" : "dsll",
2985                           "d,w,<", reg, reg,
2986                           (shift >= 32) ? shift - 32 : shift);
2987              return;
2988            }
2989          shift++;
2990        } while (shift <= (64 - 16));
2991
2992       /* Find the bit number of the lowest one bit, and store the
2993          shifted value in hi/lo.  */
2994       hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
2995       lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
2996       if (lo != 0)
2997         {
2998           bit = 0;
2999           while ((lo & 1) == 0)
3000             {
3001               lo >>= 1;
3002               ++bit;
3003             }
3004           lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3005           hi >>= bit;
3006         }
3007       else
3008         {
3009           bit = 32;
3010           while ((hi & 1) == 0)
3011             {
3012               hi >>= 1;
3013               ++bit;
3014             }
3015           lo = hi;
3016           hi = 0;
3017         }
3018
3019       /* Optimize if the shifted value is a (power of 2) - 1.  */
3020       if ((hi == 0 && ((lo + 1) & lo) == 0)
3021           || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3022         {
3023           shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3024           if (shift != 0)
3025             {
3026               expressionS tmp;
3027
3028               /* This instruction will set the register to be all
3029                  ones.  */
3030               tmp.X_op = O_constant;
3031               tmp.X_add_number = (offsetT) -1;
3032               macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3033                            reg, 0, (int) BFD_RELOC_LO16);
3034               if (bit != 0)
3035                 {
3036                   bit += shift;
3037                   macro_build ((char *) NULL, counter, NULL,
3038                                (bit >= 32) ? "dsll32" : "dsll",
3039                                "d,w,<", reg, reg,
3040                                (bit >= 32) ? bit - 32 : bit);
3041                 }
3042               macro_build ((char *) NULL, counter, NULL,
3043                            (shift >= 32) ? "dsrl32" : "dsrl",
3044                            "d,w,<", reg, reg,
3045                            (shift >= 32) ? shift - 32 : shift);
3046               return;
3047             }
3048         }
3049
3050       /* Sign extend hi32 before calling load_register, because we can
3051          generally get better code when we load a sign extended value.  */
3052       if ((hi32.X_add_number & 0x80000000) != 0)
3053         hi32.X_add_number |= ~ (offsetT) 0xffffffff;
3054       load_register (counter, reg, &hi32, 0);
3055       freg = reg;
3056     }
3057   if ((lo32.X_add_number & 0xffff0000) == 0)
3058     {
3059       if (freg != 0)
3060         {
3061           macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
3062                        freg, 0);
3063           freg = reg;
3064         }
3065     }
3066   else
3067     {
3068       expressionS mid16;
3069
3070       if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
3071         {
3072           macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3073                        (int) BFD_RELOC_HI16);
3074           macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3075                        reg, 0);
3076           return;
3077         }
3078
3079       if (freg != 0)
3080         {
3081           macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3082                        freg, 16);
3083           freg = reg;
3084         }
3085       mid16 = lo32;
3086       mid16.X_add_number >>= 16;
3087       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3088                    freg, (int) BFD_RELOC_LO16);
3089       macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3090                    reg, 16);
3091       freg = reg;
3092     }
3093   if ((lo32.X_add_number & 0xffff) != 0)
3094     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3095                  (int) BFD_RELOC_LO16);
3096 }
3097
3098 /* Load an address into a register.  */
3099
3100 static void
3101 load_address (counter, reg, ep)
3102      int *counter;
3103      int reg;
3104      expressionS *ep;
3105 {
3106   char *p;
3107
3108   if (ep->X_op != O_constant
3109       && ep->X_op != O_symbol)
3110     {
3111       as_bad ("expression too complex");
3112       ep->X_op = O_constant;
3113     }
3114
3115   if (ep->X_op == O_constant)
3116     {
3117       load_register (counter, reg, ep, 0);
3118       return;
3119     }
3120
3121   if (mips_pic == NO_PIC)
3122     {
3123       /* If this is a reference to a GP relative symbol, we want
3124            addiu        $reg,$gp,<sym>          (BFD_RELOC_MIPS_GPREL)
3125          Otherwise we want
3126            lui          $reg,<sym>              (BFD_RELOC_HI16_S)
3127            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3128          If we have an addend, we always use the latter form.  */
3129       if ((valueT) ep->X_add_number >= MAX_GPREL_OFFSET
3130           || nopic_need_relax (ep->X_add_symbol, 1))
3131         p = NULL;
3132       else
3133         {
3134           frag_grow (20);
3135           macro_build ((char *) NULL, counter, ep,
3136                        mips_opts.isa < 3 ? "addiu" : "daddiu",
3137                        "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3138           p = frag_var (rs_machine_dependent, 8, 0,
3139                         RELAX_ENCODE (4, 8, 0, 4, 0,
3140                                       mips_opts.warn_about_macros),
3141                         ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3142         }
3143       macro_build_lui (p, counter, ep, reg);
3144       if (p != NULL)
3145         p += 4;
3146       macro_build (p, counter, ep,
3147                    mips_opts.isa < 3 ? "addiu" : "daddiu",
3148                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3149     }
3150   else if (mips_pic == SVR4_PIC && ! mips_big_got)
3151     {
3152       expressionS ex;
3153
3154       /* If this is a reference to an external symbol, we want
3155            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3156          Otherwise we want
3157            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3158            nop
3159            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3160          If there is a constant, it must be added in after.  */
3161       ex.X_add_number = ep->X_add_number;
3162       ep->X_add_number = 0;
3163       frag_grow (20);
3164       macro_build ((char *) NULL, counter, ep,
3165                    mips_opts.isa < 3 ? "lw" : "ld",
3166                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3167       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3168       p = frag_var (rs_machine_dependent, 4, 0,
3169                     RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3170                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3171       macro_build (p, counter, ep,
3172                    mips_opts.isa < 3 ? "addiu" : "daddiu",
3173                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3174       if (ex.X_add_number != 0)
3175         {
3176           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3177             as_bad ("PIC code offset overflow (max 16 signed bits)");
3178           ex.X_op = O_constant;
3179           macro_build ((char *) NULL, counter, &ex,
3180                        mips_opts.isa < 3 ? "addiu" : "daddiu",
3181                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3182         }
3183     }
3184   else if (mips_pic == SVR4_PIC)
3185     {
3186       expressionS ex;
3187       int off;
3188
3189       /* This is the large GOT case.  If this is a reference to an
3190          external symbol, we want
3191            lui          $reg,<sym>              (BFD_RELOC_MIPS_GOT_HI16)
3192            addu         $reg,$reg,$gp
3193            lw           $reg,<sym>($reg)        (BFD_RELOC_MIPS_GOT_LO16)
3194          Otherwise, for a reference to a local symbol, we want
3195            lw           $reg,<sym>($gp)         (BFD_RELOC_MIPS_GOT16)
3196            nop
3197            addiu        $reg,$reg,<sym>         (BFD_RELOC_LO16)
3198          If there is a constant, it must be added in after.  */
3199       ex.X_add_number = ep->X_add_number;
3200       ep->X_add_number = 0;
3201       if (reg_needs_delay (GP))
3202         off = 4;
3203       else
3204         off = 0;
3205       frag_grow (32);
3206       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3207                    (int) BFD_RELOC_MIPS_GOT_HI16);
3208       macro_build ((char *) NULL, counter, (expressionS *) NULL,
3209                    mips_opts.isa < 3 ? "addu" : "daddu",
3210                    "d,v,t", reg, reg, GP);
3211       macro_build ((char *) NULL, counter, ep,
3212                    mips_opts.isa < 3 ? "lw" : "ld",
3213                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3214       p = frag_var (rs_machine_dependent, 12 + off, 0,
3215                     RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3216                                   mips_opts.warn_about_macros),
3217                     ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3218       if (off > 0)
3219         {
3220           /* We need a nop before loading from $gp.  This special
3221              check is required because the lui which starts the main
3222              instruction stream does not refer to $gp, and so will not
3223              insert the nop which may be required.  */
3224           macro_build (p, counter, (expressionS *) NULL, "nop", "");
3225           p += 4;
3226         }
3227       macro_build (p, counter, ep,
3228                    mips_opts.isa < 3 ? "lw" : "ld",
3229                    "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3230       p += 4;
3231       macro_build (p, counter, (expressionS *) NULL, "nop", "");
3232       p += 4;
3233       macro_build (p, counter, ep,
3234                    mips_opts.isa < 3 ? "addiu" : "daddiu",
3235                    "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3236       if (ex.X_add_number != 0)
3237         {
3238           if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3239             as_bad ("PIC code offset overflow (max 16 signed bits)");
3240           ex.X_op = O_constant;
3241           macro_build ((char *) NULL, counter, &ex,
3242                        mips_opts.isa < 3 ? "addiu" : "daddiu",
3243                        "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3244         }
3245     }
3246   else if (mips_pic == EMBEDDED_PIC)
3247     {
3248       /* We always do
3249            addiu        $reg,$gp,<sym>          (BFD_RELOC_MIPS_GPREL)
3250          */
3251       macro_build ((char *) NULL, counter, ep,
3252                    mips_opts.isa < 3 ? "addiu" : "daddiu",
3253                    "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3254     }
3255   else
3256     abort ();
3257 }
3258
3259 /*
3260  *                      Build macros
3261  *   This routine implements the seemingly endless macro or synthesized
3262  * instructions and addressing modes in the mips assembly language. Many
3263  * of these macros are simple and are similar to each other. These could
3264  * probably be handled by some kind of table or grammer aproach instead of
3265  * this verbose method. Others are not simple macros but are more like
3266  * optimizing code generation.
3267  *   One interesting optimization is when several store macros appear
3268  * consecutivly that would load AT with the upper half of the same address.
3269  * The ensuing load upper instructions are ommited. This implies some kind
3270  * of global optimization. We currently only optimize within a single macro.
3271  *   For many of the load and store macros if the address is specified as a
3272  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3273  * first load register 'at' with zero and use it as the base register. The
3274  * mips assembler simply uses register $zero. Just one tiny optimization
3275  * we're missing.
3276  */
3277 static void
3278 macro (ip)
3279      struct mips_cl_insn *ip;
3280 {
3281   register int treg, sreg, dreg, breg;
3282   int tempreg;
3283   int mask;
3284   int icnt = 0;
3285   int used_at;
3286   expressionS expr1;
3287   const char *s;
3288   const char *s2;
3289   const char *fmt;
3290   int likely = 0;
3291   int dbl = 0;
3292   int coproc = 0;
3293   int lr = 0;
3294   offsetT maxnum;
3295   int off;
3296   bfd_reloc_code_real_type r;
3297   char *p;
3298   int hold_mips_optimize;
3299
3300   assert (! mips_opts.mips16);
3301
3302   treg = (ip->insn_opcode >> 16) & 0x1f;
3303   dreg = (ip->insn_opcode >> 11) & 0x1f;
3304   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3305   mask = ip->insn_mo->mask;
3306
3307   expr1.X_op = O_constant;
3308   expr1.X_op_symbol = NULL;
3309   expr1.X_add_symbol = NULL;
3310   expr1.X_add_number = 1;
3311
3312   switch (mask)
3313     {
3314     case M_DABS:
3315       dbl = 1;
3316     case M_ABS:
3317       /* bgez $a0,.+12
3318          move v0,$a0
3319          sub v0,$zero,$a0
3320          */
3321
3322       mips_emit_delays (true);
3323       ++mips_opts.noreorder;
3324       mips_any_noreorder = 1;
3325
3326       expr1.X_add_number = 8;
3327       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3328       if (dreg == sreg)
3329         macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3330       else
3331         macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, sreg, 0);
3332       macro_build ((char *) NULL, &icnt, NULL,
3333                    dbl ? "dsub" : "sub",
3334                    "d,v,t", dreg, 0, sreg);
3335
3336       --mips_opts.noreorder;
3337       return;
3338
3339     case M_ADD_I:
3340       s = "addi";
3341       s2 = "add";
3342       goto do_addi;
3343     case M_ADDU_I:
3344       s = "addiu";
3345       s2 = "addu";
3346       goto do_addi;
3347     case M_DADD_I:
3348       dbl = 1;
3349       s = "daddi";
3350       s2 = "dadd";
3351       goto do_addi;
3352     case M_DADDU_I:
3353       dbl = 1;
3354       s = "daddiu";
3355       s2 = "daddu";
3356     do_addi:
3357       if (imm_expr.X_op == O_constant
3358           && imm_expr.X_add_number >= -0x8000
3359           && imm_expr.X_add_number < 0x8000)
3360         {
3361           macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3362                        (int) BFD_RELOC_LO16);
3363           return;
3364         }
3365       load_register (&icnt, AT, &imm_expr, dbl);
3366       macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3367       break;
3368
3369     case M_AND_I:
3370       s = "andi";
3371       s2 = "and";
3372       goto do_bit;
3373     case M_OR_I:
3374       s = "ori";
3375       s2 = "or";
3376       goto do_bit;
3377     case M_NOR_I:
3378       s = "";
3379       s2 = "nor";
3380       goto do_bit;
3381     case M_XOR_I:
3382       s = "xori";
3383       s2 = "xor";
3384     do_bit:
3385       if (imm_expr.X_op == O_constant
3386           && imm_expr.X_add_number >= 0
3387           && imm_expr.X_add_number < 0x10000)
3388         {
3389           if (mask != M_NOR_I)
3390             macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3391                          sreg, (int) BFD_RELOC_LO16);
3392           else
3393             {
3394               macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3395                            treg, sreg, (int) BFD_RELOC_LO16);
3396               macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3397                            treg, treg, 0);
3398             }
3399           return;
3400         }
3401
3402       load_register (&icnt, AT, &imm_expr, 0);
3403       macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3404       break;
3405
3406     case M_BEQ_I:
3407       s = "beq";
3408       goto beq_i;
3409     case M_BEQL_I:
3410       s = "beql";
3411       likely = 1;
3412       goto beq_i;
3413     case M_BNE_I:
3414       s = "bne";
3415       goto beq_i;
3416     case M_BNEL_I:
3417       s = "bnel";
3418       likely = 1;
3419     beq_i:
3420       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3421         {
3422           macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3423                        0);
3424           return;
3425         }
3426       load_register (&icnt, AT, &imm_expr, 0);
3427       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3428       break;
3429
3430     case M_BGEL:
3431       likely = 1;
3432     case M_BGE:
3433       if (treg == 0)
3434         {
3435           macro_build ((char *) NULL, &icnt, &offset_expr,
3436                        likely ? "bgezl" : "bgez",
3437                        "s,p", sreg);
3438           return;
3439         }
3440       if (sreg == 0)
3441         {
3442           macro_build ((char *) NULL, &icnt, &offset_expr,
3443                        likely ? "blezl" : "blez",
3444                        "s,p", treg);
3445           return;
3446         }
3447       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3448       macro_build ((char *) NULL, &icnt, &offset_expr,
3449                    likely ? "beql" : "beq",
3450                    "s,t,p", AT, 0);
3451       break;
3452
3453     case M_BGTL_I:
3454       likely = 1;
3455     case M_BGT_I:
3456       /* check for > max integer */
3457       maxnum = 0x7fffffff;
3458       if (mips_opts.isa >= 3 && sizeof (maxnum) > 4)
3459         {
3460           maxnum <<= 16;
3461           maxnum |= 0xffff;
3462           maxnum <<= 16;
3463           maxnum |= 0xffff;
3464         }
3465       if (imm_expr.X_op == O_constant
3466           && imm_expr.X_add_number >= maxnum
3467           && (mips_opts.isa < 3 || sizeof (maxnum) > 4))
3468         {
3469         do_false:
3470           /* result is always false */
3471           if (! likely)
3472             {
3473               as_warn ("Branch %s is always false (nop)", ip->insn_mo->name);
3474               macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3475             }
3476           else
3477             {
3478               as_warn ("Branch likely %s is always false", ip->insn_mo->name);
3479               macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3480                            "s,t,p", 0, 0);
3481             }
3482           return;
3483         }
3484       if (imm_expr.X_op != O_constant)
3485         as_bad ("Unsupported large constant");
3486       imm_expr.X_add_number++;
3487       /* FALLTHROUGH */
3488     case M_BGE_I:
3489     case M_BGEL_I:
3490       if (mask == M_BGEL_I)
3491         likely = 1;
3492       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3493         {
3494           macro_build ((char *) NULL, &icnt, &offset_expr,
3495                        likely ? "bgezl" : "bgez",
3496                        "s,p", sreg);
3497           return;
3498         }
3499       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3500         {
3501           macro_build ((char *) NULL, &icnt, &offset_expr,
3502                        likely ? "bgtzl" : "bgtz",
3503                        "s,p", sreg);
3504           return;
3505         }
3506       maxnum = 0x7fffffff;
3507       if (mips_opts.isa >= 3 && sizeof (maxnum) > 4)
3508         {
3509           maxnum <<= 16;
3510           maxnum |= 0xffff;
3511           maxnum <<= 16;
3512           maxnum |= 0xffff;
3513         }
3514       maxnum = - maxnum - 1;
3515       if (imm_expr.X_op == O_constant
3516           && imm_expr.X_add_number <= maxnum
3517           && (mips_opts.isa < 3 || sizeof (maxnum) > 4))
3518         {
3519         do_true:
3520           /* result is always true */
3521           as_warn ("Branch %s is always true", ip->insn_mo->name);
3522           macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3523           return;
3524         }
3525       set_at (&icnt, sreg, 0);
3526       macro_build ((char *) NULL, &icnt, &offset_expr,
3527                    likely ? "beql" : "beq",
3528                    "s,t,p", AT, 0);
3529       break;
3530
3531     case M_BGEUL:
3532       likely = 1;
3533     case M_BGEU:
3534       if (treg == 0)
3535         goto do_true;
3536       if (sreg == 0)
3537         {
3538           macro_build ((char *) NULL, &icnt, &offset_expr,
3539                        likely ? "beql" : "beq",
3540                        "s,t,p", 0, treg);
3541           return;
3542         }
3543       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3544                    treg);
3545       macro_build ((char *) NULL, &icnt, &offset_expr,
3546                    likely ? "beql" : "beq",
3547                    "s,t,p", AT, 0);
3548       break;
3549
3550     case M_BGTUL_I:
3551       likely = 1;
3552     case M_BGTU_I:
3553       if (sreg == 0
3554           || (mips_opts.isa < 3
3555               && imm_expr.X_op == O_constant
3556               && imm_expr.X_add_number == 0xffffffff))
3557         goto do_false;
3558       if (imm_expr.X_op != O_constant)
3559         as_bad ("Unsupported large constant");
3560       imm_expr.X_add_number++;
3561       /* FALLTHROUGH */
3562     case M_BGEU_I:
3563     case M_BGEUL_I:
3564       if (mask == M_BGEUL_I)
3565         likely = 1;
3566       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3567         goto do_true;
3568       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3569         {
3570           macro_build ((char *) NULL, &icnt, &offset_expr,
3571                        likely ? "bnel" : "bne",
3572                        "s,t,p", sreg, 0);
3573           return;
3574         }
3575       set_at (&icnt, sreg, 1);
3576       macro_build ((char *) NULL, &icnt, &offset_expr,
3577                    likely ? "beql" : "beq",
3578                    "s,t,p", AT, 0);
3579       break;
3580
3581     case M_BGTL:
3582       likely = 1;
3583     case M_BGT:
3584       if (treg == 0)
3585         {
3586           macro_build ((char *) NULL, &icnt, &offset_expr,
3587                        likely ? "bgtzl" : "bgtz",
3588                        "s,p", sreg);
3589           return;
3590         }
3591       if (sreg == 0)
3592         {
3593           macro_build ((char *) NULL, &icnt, &offset_expr,
3594                        likely ? "bltzl" : "bltz",
3595                        "s,p", treg);
3596           return;
3597         }
3598       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3599       macro_build ((char *) NULL, &icnt, &offset_expr,
3600                    likely ? "bnel" : "bne",
3601                    "s,t,p", AT, 0);
3602       break;
3603
3604     case M_BGTUL:
3605       likely = 1;
3606     case M_BGTU:
3607       if (treg == 0)
3608         {
3609           macro_build ((char *) NULL, &icnt, &offset_expr,
3610                        likely ? "bnel" : "bne",
3611                        "s,t,p", sreg, 0);
3612           return;
3613         }
3614       if (sreg == 0)
3615         goto do_false;
3616       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3617                    sreg);
3618       macro_build ((char *) NULL, &icnt, &offset_expr,
3619                    likely ? "bnel" : "bne",
3620                    "s,t,p", AT, 0);
3621       break;
3622
3623     case M_BLEL:
3624       likely = 1;
3625     case M_BLE:
3626       if (treg == 0)
3627         {
3628           macro_build ((char *) NULL, &icnt, &offset_expr,
3629                        likely ? "blezl" : "blez",
3630                        "s,p", sreg);
3631           return;
3632         }
3633       if (sreg == 0)
3634         {
3635           macro_build ((char *) NULL, &icnt, &offset_expr,
3636                        likely ? "bgezl" : "bgez",
3637                        "s,p", treg);
3638           return;
3639         }
3640       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3641       macro_build ((char *) NULL, &icnt, &offset_expr,
3642                    likely ? "beql" : "beq",
3643                    "s,t,p", AT, 0);
3644       break;
3645
3646     case M_BLEL_I:
3647       likely = 1;
3648     case M_BLE_I:
3649       maxnum = 0x7fffffff;
3650       if (mips_opts.isa >= 3 && sizeof (maxnum) > 4)
3651         {
3652           maxnum <<= 16;
3653           maxnum |= 0xffff;
3654           maxnum <<= 16;
3655           maxnum |= 0xffff;
3656         }
3657       if (imm_expr.X_op == O_constant
3658           && imm_expr.X_add_number >= maxnum
3659           && (mips_opts.isa < 3 || sizeof (maxnum) > 4))
3660         goto do_true;
3661       if (imm_expr.X_op != O_constant)
3662         as_bad ("Unsupported large constant");
3663       imm_expr.X_add_number++;
3664       /* FALLTHROUGH */
3665     case M_BLT_I:
3666     case M_BLTL_I:
3667       if (mask == M_BLTL_I)
3668         likely = 1;
3669       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3670         {
3671           macro_build ((char *) NULL, &icnt, &offset_expr,
3672                        likely ? "bltzl" : "bltz",
3673                        "s,p", sreg);
3674           return;
3675         }
3676       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3677         {
3678           macro_build ((char *) NULL, &icnt, &offset_expr,
3679                        likely ? "blezl" : "blez",
3680                        "s,p", sreg);
3681           return;
3682         }
3683       set_at (&icnt, sreg, 0);
3684       macro_build ((char *) NULL, &icnt, &offset_expr,
3685                    likely ? "bnel" : "bne",
3686                    "s,t,p", AT, 0);
3687       break;
3688
3689     case M_BLEUL:
3690       likely = 1;
3691     case M_BLEU:
3692       if (treg == 0)
3693         {
3694           macro_build ((char *) NULL, &icnt, &offset_expr,
3695                        likely ? "beql" : "beq",
3696                        "s,t,p", sreg, 0);
3697           return;
3698         }
3699       if (sreg == 0)
3700         goto do_true;
3701       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3702                    sreg);
3703       macro_build ((char *) NULL, &icnt, &offset_expr,
3704                    likely ? "beql" : "beq",
3705                    "s,t,p", AT, 0);
3706       break;
3707
3708     case M_BLEUL_I:
3709       likely = 1;
3710     case M_BLEU_I:
3711       if (sreg == 0
3712           || (mips_opts.isa < 3
3713               && imm_expr.X_op == O_constant
3714               && imm_expr.X_add_number == 0xffffffff))
3715         goto do_true;
3716       if (imm_expr.X_op != O_constant)
3717         as_bad ("Unsupported large constant");
3718       imm_expr.X_add_number++;
3719       /* FALLTHROUGH */
3720     case M_BLTU_I:
3721     case M_BLTUL_I:
3722       if (mask == M_BLTUL_I)
3723         likely = 1;
3724       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3725         goto do_false;
3726       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3727         {
3728           macro_build ((char *) NULL, &icnt, &offset_expr,
3729                        likely ? "beql" : "beq",
3730                        "s,t,p", sreg, 0);
3731           return;
3732         }
3733       set_at (&icnt, sreg, 1);
3734       macro_build ((char *) NULL, &icnt, &offset_expr,
3735                    likely ? "bnel" : "bne",
3736                    "s,t,p", AT, 0);
3737       break;
3738
3739     case M_BLTL:
3740       likely = 1;
3741     case M_BLT:
3742       if (treg == 0)
3743         {
3744           macro_build ((char *) NULL, &icnt, &offset_expr,
3745                        likely ? "bltzl" : "bltz",
3746                        "s,p", sreg);
3747           return;
3748         }
3749       if (sreg == 0)
3750         {
3751           macro_build ((char *) NULL, &icnt, &offset_expr,
3752                        likely ? "bgtzl" : "bgtz",
3753                        "s,p", treg);
3754           return;
3755         }
3756       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3757       macro_build ((char *) NULL, &icnt, &offset_expr,
3758                    likely ? "bnel" : "bne",
3759                    "s,t,p", AT, 0);
3760       break;
3761
3762     case M_BLTUL:
3763       likely = 1;
3764     case M_BLTU:
3765       if (treg == 0)
3766         goto do_false;
3767       if (sreg == 0)
3768         {
3769           macro_build ((char *) NULL, &icnt, &offset_expr,
3770                        likely ? "bnel" : "bne",
3771                        "s,t,p", 0, treg);
3772           return;
3773         }
3774       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3775                    treg);
3776       macro_build ((char *) NULL, &icnt, &offset_expr,
3777                    likely ? "bnel" : "bne",
3778                    "s,t,p", AT, 0);
3779       break;
3780
3781     case M_DDIV_3:
3782       dbl = 1;
3783     case M_DIV_3:
3784       s = "mflo";
3785       goto do_div3;
3786     case M_DREM_3:
3787       dbl = 1;
3788     case M_REM_3:
3789       s = "mfhi";
3790     do_div3:
3791       if (treg == 0)
3792         {
3793           as_warn ("Divide by zero.");
3794           if (mips_trap)
3795             macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3796           else
3797             macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3798           return;
3799         }
3800
3801       mips_emit_delays (true);
3802       ++mips_opts.noreorder;
3803       mips_any_noreorder = 1;
3804       macro_build ((char *) NULL, &icnt, NULL,
3805                    dbl ? "ddiv" : "div",
3806                    "z,s,t", sreg, treg);
3807       if (mips_trap)
3808         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
3809       else
3810         {
3811           expr1.X_add_number = 8;
3812           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
3813           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3814           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3815         }
3816       expr1.X_add_number = -1;
3817       macro_build ((char *) NULL, &icnt, &expr1,
3818                    dbl ? "daddiu" : "addiu",
3819                    "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
3820       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
3821       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
3822       if (dbl)
3823         {
3824           expr1.X_add_number = 1;
3825           macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
3826                        (int) BFD_RELOC_LO16);
3827           macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
3828                        31);
3829         }
3830       else
3831         {
3832           expr1.X_add_number = 0x80000000;
3833           macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
3834                        (int) BFD_RELOC_HI16);
3835         }
3836       if (mips_trap)
3837         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
3838       else
3839         {
3840           expr1.X_add_number = 8;
3841           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
3842           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3843           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
3844         }
3845       --mips_opts.noreorder;
3846       macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
3847       break;
3848
3849     case M_DIV_3I:
3850       s = "div";
3851       s2 = "mflo";
3852       goto do_divi;
3853     case M_DIVU_3I:
3854       s = "divu";
3855       s2 = "mflo";
3856       goto do_divi;
3857     case M_REM_3I:
3858       s = "div";
3859       s2 = "mfhi";
3860       goto do_divi;
3861     case M_REMU_3I:
3862       s = "divu";
3863       s2 = "mfhi";
3864       goto do_divi;
3865     case M_DDIV_3I:
3866       dbl = 1;
3867       s = "ddiv";
3868       s2 = "mflo";
3869       goto do_divi;
3870     case M_DDIVU_3I:
3871       dbl = 1;
3872       s = "ddivu";
3873       s2 = "mflo";
3874       goto do_divi;
3875     case M_DREM_3I:
3876       dbl = 1;
3877       s = "ddiv";
3878       s2 = "mfhi";
3879       goto do_divi;
3880     case M_DREMU_3I:
3881       dbl = 1;
3882       s = "ddivu";
3883       s2 = "mfhi";
3884     do_divi:
3885       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3886         {
3887           as_warn ("Divide by zero.");
3888           if (mips_trap)
3889             macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3890           else
3891             macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3892           return;
3893         }
3894       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3895         {
3896           if (strcmp (s2, "mflo") == 0)
3897             macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg,
3898                          sreg);
3899           else
3900             macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
3901           return;
3902         }
3903       if (imm_expr.X_op == O_constant
3904           && imm_expr.X_add_number == -1
3905           && s[strlen (s) - 1] != 'u')
3906         {
3907           if (strcmp (s2, "mflo") == 0)
3908             {
3909               if (dbl)
3910                 macro_build ((char *) NULL, &icnt, NULL, "dneg", "d,w", dreg,
3911                              sreg);
3912               else
3913                 macro_build ((char *) NULL, &icnt, NULL, "neg", "d,w", dreg,
3914                              sreg);
3915             }
3916           else
3917             macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
3918           return;
3919         }
3920
3921       load_register (&icnt, AT, &imm_expr, dbl);
3922       macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
3923       macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
3924       break;
3925
3926     case M_DIVU_3:
3927       s = "divu";
3928       s2 = "mflo";
3929       goto do_divu3;
3930     case M_REMU_3:
3931       s = "divu";
3932       s2 = "mfhi";
3933       goto do_divu3;
3934     case M_DDIVU_3:
3935       s = "ddivu";
3936       s2 = "mflo";
3937       goto do_divu3;
3938     case M_DREMU_3:
3939       s = "ddivu";
3940       s2 = "mfhi";
3941     do_divu3:
3942       mips_emit_delays (true);
3943       ++mips_opts.noreorder;
3944       mips_any_noreorder = 1;
3945       macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
3946       if (mips_trap)
3947         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
3948       else
3949         {
3950           expr1.X_add_number = 8;
3951           macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
3952           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3953           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3954         }
3955       --mips_opts.noreorder;
3956       macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
3957       return;
3958
3959     case M_DLA_AB:
3960       dbl = 1;
3961     case M_LA_AB:
3962       /* Load the address of a symbol into a register.  If breg is not
3963          zero, we then add a base register to it.  */
3964
3965       /* When generating embedded PIC code, we permit expressions of
3966          the form
3967            la   $4,foo-bar
3968          where bar is an address in the .text section.  These are used
3969          when getting the addresses of functions.  We don't permit
3970          X_add_number to be non-zero, because if the symbol is
3971          external the relaxing code needs to know that any addend is
3972          purely the offset to X_op_symbol.  */
3973       if (mips_pic == EMBEDDED_PIC
3974           && offset_expr.X_op == O_subtract
3975           && now_seg == text_section
3976           && (offset_expr.X_op_symbol->sy_value.X_op == O_constant
3977               ? S_GET_SEGMENT (offset_expr.X_op_symbol) == text_section
3978               : (offset_expr.X_op_symbol->sy_value.X_op == O_symbol
3979                  && (S_GET_SEGMENT (offset_expr.X_op_symbol
3980                                     ->sy_value.X_add_symbol)
3981                      == text_section)))
3982           && breg == 0
3983           && offset_expr.X_add_number == 0)
3984         {
3985           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
3986                        treg, (int) BFD_RELOC_PCREL_HI16_S);
3987           macro_build ((char *) NULL, &icnt, &offset_expr,
3988                        mips_opts.isa < 3 ? "addiu" : "daddiu",
3989                        "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
3990           return;
3991         }
3992
3993       if (offset_expr.X_op != O_symbol
3994           && offset_expr.X_op != O_constant)
3995         {
3996           as_bad ("expression too complex");
3997           offset_expr.X_op = O_constant;
3998         }
3999
4000       if (treg == breg)
4001         {
4002           tempreg = AT;
4003           used_at = 1;
4004         }
4005       else
4006         {
4007           tempreg = treg;
4008           used_at = 0;
4009         }
4010
4011       if (offset_expr.X_op == O_constant)
4012         load_register (&icnt, tempreg, &offset_expr, dbl);
4013       else if (mips_pic == NO_PIC)
4014         {
4015           /* If this is a reference to an GP relative symbol, we want
4016                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_MIPS_GPREL)
4017              Otherwise we want
4018                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4019                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4020              If we have a constant, we need two instructions anyhow,
4021              so we may as well always use the latter form.  */
4022           if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4023               || nopic_need_relax (offset_expr.X_add_symbol, 1))
4024             p = NULL;
4025           else
4026             {
4027               frag_grow (20);
4028               macro_build ((char *) NULL, &icnt, &offset_expr,
4029                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4030                            "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4031               p = frag_var (rs_machine_dependent, 8, 0,
4032                             RELAX_ENCODE (4, 8, 0, 4, 0,
4033                                           mips_opts.warn_about_macros),
4034                             offset_expr.X_add_symbol, (offsetT) 0,
4035                             (char *) NULL);
4036             }
4037           macro_build_lui (p, &icnt, &offset_expr, tempreg);
4038           if (p != NULL)
4039             p += 4;
4040           macro_build (p, &icnt, &offset_expr,
4041                        mips_opts.isa < 3 ? "addiu" : "daddiu",
4042                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4043         }
4044       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4045         {
4046           /* If this is a reference to an external symbol, and there
4047              is no constant, we want
4048                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4049              For a local symbol, we want
4050                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4051                nop
4052                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4053
4054              If we have a small constant, and this is a reference to
4055              an external symbol, we want
4056                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4057                nop
4058                addiu    $tempreg,$tempreg,<constant>
4059              For a local symbol, we want the same instruction
4060              sequence, but we output a BFD_RELOC_LO16 reloc on the
4061              addiu instruction.
4062
4063              If we have a large constant, and this is a reference to
4064              an external symbol, we want
4065                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4066                lui      $at,<hiconstant>
4067                addiu    $at,$at,<loconstant>
4068                addu     $tempreg,$tempreg,$at
4069              For a local symbol, we want the same instruction
4070              sequence, but we output a BFD_RELOC_LO16 reloc on the
4071              addiu instruction.  */
4072           expr1.X_add_number = offset_expr.X_add_number;
4073           offset_expr.X_add_number = 0;
4074           frag_grow (32);
4075           macro_build ((char *) NULL, &icnt, &offset_expr,
4076                        dbl ? "ld" : "lw",
4077                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4078           if (expr1.X_add_number == 0)
4079             {
4080               int off;
4081
4082               if (breg == 0)
4083                 off = 0;
4084               else
4085                 {
4086                   /* We're going to put in an addu instruction using
4087                      tempreg, so we may as well insert the nop right
4088                      now.  */
4089                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4090                                "nop", "");
4091                   off = 4;
4092                 }
4093               p = frag_var (rs_machine_dependent, 8 - off, 0,
4094                             RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4095                                           (breg == 0
4096                                            ? mips_opts.warn_about_macros
4097                                            : 0)),
4098                             offset_expr.X_add_symbol, (offsetT) 0,
4099                             (char *) NULL);
4100               if (breg == 0)
4101                 {
4102                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4103                   p += 4;
4104                 }
4105               macro_build (p, &icnt, &expr1,
4106                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4107                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4108               /* FIXME: If breg == 0, and the next instruction uses
4109                  $tempreg, then if this variant case is used an extra
4110                  nop will be generated.  */
4111             }
4112           else if (expr1.X_add_number >= -0x8000
4113                    && expr1.X_add_number < 0x8000)
4114             {
4115               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4116                            "nop", "");
4117               macro_build ((char *) NULL, &icnt, &expr1,
4118                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4119                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4120               (void) frag_var (rs_machine_dependent, 0, 0,
4121                                RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4122                                offset_expr.X_add_symbol, (offsetT) 0,
4123                                (char *) NULL);
4124             }
4125           else
4126             {
4127               int off1;
4128
4129               /* If we are going to add in a base register, and the
4130                  target register and the base register are the same,
4131                  then we are using AT as a temporary register.  Since
4132                  we want to load the constant into AT, we add our
4133                  current AT (from the global offset table) and the
4134                  register into the register now, and pretend we were
4135                  not using a base register.  */
4136               if (breg != treg)
4137                 off1 = 0;
4138               else
4139                 {
4140                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4141                                "nop", "");
4142                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4143                                mips_opts.isa < 3 ? "addu" : "daddu",
4144                                "d,v,t", treg, AT, breg);
4145                   breg = 0;
4146                   tempreg = treg;
4147                   off1 = -8;
4148                 }
4149
4150               /* Set mips_optimize around the lui instruction to avoid
4151                  inserting an unnecessary nop after the lw.  */
4152               hold_mips_optimize = mips_optimize;
4153               mips_optimize = 2;
4154               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4155               mips_optimize = hold_mips_optimize;
4156
4157               macro_build ((char *) NULL, &icnt, &expr1,
4158                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4159                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4160               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4161                            mips_opts.isa < 3 ? "addu" : "daddu",
4162                            "d,v,t", tempreg, tempreg, AT);
4163               (void) frag_var (rs_machine_dependent, 0, 0,
4164                                RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4165                                offset_expr.X_add_symbol, (offsetT) 0,
4166                                (char *) NULL);
4167               used_at = 1;
4168             }
4169         }
4170       else if (mips_pic == SVR4_PIC)
4171         {
4172           int gpdel;
4173
4174           /* This is the large GOT case.  If this is a reference to an
4175              external symbol, and there is no constant, we want
4176                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4177                addu     $tempreg,$tempreg,$gp
4178                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4179              For a local symbol, we want
4180                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4181                nop
4182                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4183
4184              If we have a small constant, and this is a reference to
4185              an external symbol, we want
4186                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4187                addu     $tempreg,$tempreg,$gp
4188                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4189                nop
4190                addiu    $tempreg,$tempreg,<constant>
4191              For a local symbol, we want
4192                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4193                nop
4194                addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4195
4196              If we have a large constant, and this is a reference to
4197              an external symbol, we want
4198                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4199                addu     $tempreg,$tempreg,$gp
4200                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4201                lui      $at,<hiconstant>
4202                addiu    $at,$at,<loconstant>
4203                addu     $tempreg,$tempreg,$at
4204              For a local symbol, we want
4205                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4206                lui      $at,<hiconstant>
4207                addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
4208                addu     $tempreg,$tempreg,$at
4209              */
4210           expr1.X_add_number = offset_expr.X_add_number;
4211           offset_expr.X_add_number = 0;
4212           frag_grow (52);
4213           if (reg_needs_delay (GP))
4214             gpdel = 4;
4215           else
4216             gpdel = 0;
4217           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4218                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
4219           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4220                        mips_opts.isa < 3 ? "addu" : "daddu",
4221                        "d,v,t", tempreg, tempreg, GP);
4222           macro_build ((char *) NULL, &icnt, &offset_expr,
4223                        dbl ? "ld" : "lw",
4224                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
4225                        tempreg);
4226           if (expr1.X_add_number == 0)
4227             {
4228               int off;
4229
4230               if (breg == 0)
4231                 off = 0;
4232               else
4233                 {
4234                   /* We're going to put in an addu instruction using
4235                      tempreg, so we may as well insert the nop right
4236                      now.  */
4237                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4238                                "nop", "");
4239                   off = 4;
4240                 }
4241
4242               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4243                             RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4244                                           8 + gpdel, 0,
4245                                           (breg == 0
4246                                            ? mips_opts.warn_about_macros
4247                                            : 0)),
4248                             offset_expr.X_add_symbol, (offsetT) 0,
4249                             (char *) NULL);
4250             }
4251           else if (expr1.X_add_number >= -0x8000
4252                    && expr1.X_add_number < 0x8000)
4253             {
4254               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4255                            "nop", "");
4256               macro_build ((char *) NULL, &icnt, &expr1,
4257                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4258                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4259
4260               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4261                             RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4262                                           (breg == 0
4263                                            ? mips_opts.warn_about_macros
4264                                            : 0)),
4265                             offset_expr.X_add_symbol, (offsetT) 0,
4266                             (char *) NULL);
4267             }
4268           else
4269             {
4270               int adj, dreg;
4271
4272               /* If we are going to add in a base register, and the
4273                  target register and the base register are the same,
4274                  then we are using AT as a temporary register.  Since
4275                  we want to load the constant into AT, we add our
4276                  current AT (from the global offset table) and the
4277                  register into the register now, and pretend we were
4278                  not using a base register.  */
4279               if (breg != treg)
4280                 {
4281                   adj = 0;
4282                   dreg = tempreg;
4283                 }
4284               else
4285                 {
4286                   assert (tempreg == AT);
4287                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4288                                "nop", "");
4289                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4290                                mips_opts.isa < 3 ? "addu" : "daddu",
4291                                "d,v,t", treg, AT, breg);
4292                   dreg = treg;
4293                   adj = 8;
4294                 }
4295
4296               /* Set mips_optimize around the lui instruction to avoid
4297                  inserting an unnecessary nop after the lw.  */
4298               hold_mips_optimize = mips_optimize;
4299               mips_optimize = 2;
4300               macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4301               mips_optimize = hold_mips_optimize;
4302
4303               macro_build ((char *) NULL, &icnt, &expr1,
4304                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4305                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4306               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4307                            mips_opts.isa < 3 ? "addu" : "daddu",
4308                            "d,v,t", dreg, dreg, AT);
4309
4310               p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4311                             RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4312                                           8 + gpdel, 0,
4313                                           (breg == 0
4314                                            ? mips_opts.warn_about_macros
4315                                            : 0)),
4316                             offset_expr.X_add_symbol, (offsetT) 0,
4317                             (char *) NULL);
4318
4319               used_at = 1;
4320             }
4321
4322           if (gpdel > 0)
4323             {
4324               /* This is needed because this instruction uses $gp, but
4325                  the first instruction on the main stream does not.  */
4326               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4327               p += 4;
4328             }
4329           macro_build (p, &icnt, &offset_expr,
4330                        dbl ? "ld" : "lw",
4331                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4332           p += 4;
4333           if (expr1.X_add_number >= -0x8000
4334               && expr1.X_add_number < 0x8000)
4335             {
4336               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4337               p += 4;
4338               macro_build (p, &icnt, &expr1,
4339                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4340                            "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4341               /* FIXME: If add_number is 0, and there was no base
4342                  register, the external symbol case ended with a load,
4343                  so if the symbol turns out to not be external, and
4344                  the next instruction uses tempreg, an unnecessary nop
4345                  will be inserted.  */
4346             }
4347           else
4348             {
4349               if (breg == treg)
4350                 {
4351                   /* We must add in the base register now, as in the
4352                      external symbol case.  */
4353                   assert (tempreg == AT);
4354                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4355                   p += 4;
4356                   macro_build (p, &icnt, (expressionS *) NULL,
4357                                mips_opts.isa < 3 ? "addu" : "daddu",
4358                                "d,v,t", treg, AT, breg);
4359                   p += 4;
4360                   tempreg = treg;
4361                   /* We set breg to 0 because we have arranged to add
4362                      it in in both cases.  */
4363                   breg = 0;
4364                 }
4365
4366               macro_build_lui (p, &icnt, &expr1, AT);
4367               p += 4;
4368               macro_build (p, &icnt, &expr1,
4369                            mips_opts.isa < 3 ? "addiu" : "daddiu",
4370                            "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4371               p += 4;
4372               macro_build (p, &icnt, (expressionS *) NULL,
4373                            mips_opts.isa < 3 ? "addu" : "daddu",
4374                            "d,v,t", tempreg, tempreg, AT);
4375               p += 4;
4376             }
4377         }
4378       else if (mips_pic == EMBEDDED_PIC)
4379         {
4380           /* We use
4381                addiu    $tempreg,$gp,<sym>      (BFD_RELOC_MIPS_GPREL)
4382              */
4383           macro_build ((char *) NULL, &icnt, &offset_expr,
4384                        mips_opts.isa < 3 ? "addiu" : "daddiu",
4385                        "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4386         }
4387       else
4388         abort ();
4389
4390       if (breg != 0)
4391         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4392                      mips_opts.isa < 3 ? "addu" : "daddu",
4393                      "d,v,t", treg, tempreg, breg);
4394
4395       if (! used_at)
4396         return;
4397
4398       break;
4399
4400     case M_J_A:
4401       /* The j instruction may not be used in PIC code, since it
4402          requires an absolute address.  We convert it to a b
4403          instruction.  */
4404       if (mips_pic == NO_PIC)
4405         macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4406       else
4407         macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4408       return;
4409
4410       /* The jal instructions must be handled as macros because when
4411          generating PIC code they expand to multi-instruction
4412          sequences.  Normally they are simple instructions.  */
4413     case M_JAL_1:
4414       dreg = RA;
4415       /* Fall through.  */
4416     case M_JAL_2:
4417       if (mips_pic == NO_PIC
4418           || mips_pic == EMBEDDED_PIC)
4419         macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4420                      "d,s", dreg, sreg);
4421       else if (mips_pic == SVR4_PIC)
4422         {
4423           if (sreg != PIC_CALL_REG)
4424             as_warn ("MIPS PIC call to register other than $25");
4425       
4426           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4427                        "d,s", dreg, sreg);
4428           if (mips_cprestore_offset < 0)
4429             as_warn ("No .cprestore pseudo-op used in PIC code");
4430           else
4431             {
4432               expr1.X_add_number = mips_cprestore_offset;
4433               macro_build ((char *) NULL, &icnt, &expr1,
4434                            mips_opts.isa < 3 ? "lw" : "ld",
4435                            "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
4436             }
4437         }
4438       else
4439         abort ();
4440
4441       return;
4442
4443     case M_JAL_A:
4444       if (mips_pic == NO_PIC)
4445         macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
4446       else if (mips_pic == SVR4_PIC)
4447         {
4448           /* If this is a reference to an external symbol, and we are
4449              using a small GOT, we want
4450                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_CALL16)
4451                nop
4452                jalr     $25
4453                nop
4454                lw       $gp,cprestore($sp)
4455              The cprestore value is set using the .cprestore
4456              pseudo-op.  If we are using a big GOT, we want
4457                lui      $25,<sym>               (BFD_RELOC_MIPS_CALL_HI16)
4458                addu     $25,$25,$gp
4459                lw       $25,<sym>($25)          (BFD_RELOC_MIPS_CALL_LO16)
4460                nop
4461                jalr     $25
4462                nop
4463                lw       $gp,cprestore($sp)
4464              If the symbol is not external, we want
4465                lw       $25,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
4466                nop
4467                addiu    $25,$25,<sym>           (BFD_RELOC_LO16)
4468                jalr     $25
4469                nop
4470                lw $gp,cprestore($sp) */
4471           frag_grow (40);
4472           if (! mips_big_got)
4473             {
4474               macro_build ((char *) NULL, &icnt, &offset_expr,
4475                            mips_opts.isa < 3 ? "lw" : "ld",
4476                            "t,o(b)", PIC_CALL_REG,
4477                            (int) BFD_RELOC_MIPS_CALL16, GP);
4478               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4479                            "nop", "");
4480               p = frag_var (rs_machine_dependent, 4, 0,
4481                             RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4482                             offset_expr.X_add_symbol, (offsetT) 0,
4483                             (char *) NULL);
4484             }
4485           else
4486             {
4487               int gpdel;
4488
4489               if (reg_needs_delay (GP))
4490                 gpdel = 4;
4491               else
4492                 gpdel = 0;
4493               macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4494                            PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
4495               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4496                            mips_opts.isa < 3 ? "addu" : "daddu",
4497                            "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
4498               macro_build ((char *) NULL, &icnt, &offset_expr,
4499                            mips_opts.isa < 3 ? "lw" : "ld",
4500                            "t,o(b)", PIC_CALL_REG,
4501                            (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
4502               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4503                            "nop", "");
4504               p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4505                             RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
4506                                           0, 0),
4507                             offset_expr.X_add_symbol, (offsetT) 0,
4508                             (char *) NULL);
4509               if (gpdel > 0)
4510                 {
4511                   macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4512                   p += 4;
4513                 }
4514               macro_build (p, &icnt, &offset_expr,
4515                            mips_opts.isa < 3 ? "lw" : "ld",
4516                            "t,o(b)", PIC_CALL_REG,
4517                            (int) BFD_RELOC_MIPS_GOT16, GP);
4518               p += 4;
4519               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4520               p += 4;
4521             }                      
4522           macro_build (p, &icnt, &offset_expr,
4523                        mips_opts.isa < 3 ? "addiu" : "daddiu",
4524                        "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
4525                        (int) BFD_RELOC_LO16);
4526           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4527                        "jalr", "s", PIC_CALL_REG);
4528           if (mips_cprestore_offset < 0)
4529             as_warn ("No .cprestore pseudo-op used in PIC code");
4530           else
4531             {
4532               if (mips_opts.noreorder)
4533                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4534                              "nop", "");
4535               expr1.X_add_number = mips_cprestore_offset;
4536               macro_build ((char *) NULL, &icnt, &expr1,
4537                            mips_opts.isa < 3 ? "lw" : "ld",
4538                            "t,o(b)", GP, (int) BFD_RELOC_LO16,
4539                            mips_frame_reg);
4540             }
4541         }
4542       else if (mips_pic == EMBEDDED_PIC)
4543         {
4544           macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
4545           /* The linker may expand the call to a longer sequence which
4546              uses $at, so we must break rather than return.  */
4547           break;
4548         }
4549       else
4550         abort ();
4551
4552       return;
4553
4554     case M_LB_AB:
4555       s = "lb";
4556       goto ld;
4557     case M_LBU_AB:
4558       s = "lbu";
4559       goto ld;
4560     case M_LH_AB:
4561       s = "lh";
4562       goto ld;
4563     case M_LHU_AB:
4564       s = "lhu";
4565       goto ld;
4566     case M_LW_AB:
4567       s = "lw";
4568       goto ld;
4569     case M_LWC0_AB:
4570       s = "lwc0";
4571       /* Itbl support may require additional care here. */
4572       coproc = 1;
4573       goto ld;
4574     case M_LWC1_AB:
4575       s = "lwc1";
4576       /* Itbl support may require additional care here. */
4577       coproc = 1;
4578       goto ld;
4579     case M_LWC2_AB:
4580       s = "lwc2";
4581       /* Itbl support may require additional care here. */
4582       coproc = 1;
4583       goto ld;
4584     case M_LWC3_AB:
4585       s = "lwc3";
4586       /* Itbl support may require additional care here. */
4587       coproc = 1;
4588       goto ld;
4589     case M_LWL_AB:
4590       s = "lwl";
4591       lr = 1;
4592       goto ld;
4593     case M_LWR_AB:
4594       s = "lwr";
4595       lr = 1;
4596       goto ld;
4597     case M_LDC1_AB:
4598       s = "ldc1";
4599       /* Itbl support may require additional care here. */
4600       coproc = 1;
4601       goto ld;
4602     case M_LDC2_AB:
4603       s = "ldc2";
4604       /* Itbl support may require additional care here. */
4605       coproc = 1;
4606       goto ld;
4607     case M_LDC3_AB:
4608       s = "ldc3";
4609       /* Itbl support may require additional care here. */
4610       coproc = 1;
4611       goto ld;
4612     case M_LDL_AB:
4613       s = "ldl";
4614       lr = 1;
4615       goto ld;
4616     case M_LDR_AB:
4617       s = "ldr";
4618       lr = 1;
4619       goto ld;
4620     case M_LL_AB:
4621       s = "ll";
4622       goto ld;
4623     case M_LLD_AB:
4624       s = "lld";
4625       goto ld;
4626     case M_LWU_AB:
4627       s = "lwu";
4628     ld:
4629       if (breg == treg || coproc || lr)
4630         {
4631           tempreg = AT;
4632           used_at = 1;
4633         }
4634       else
4635         {
4636           tempreg = treg;
4637           used_at = 0;
4638         }
4639       goto ld_st;
4640     case M_SB_AB:
4641       s = "sb";
4642       goto st;
4643     case M_SH_AB:
4644       s = "sh";
4645       goto st;
4646     case M_SW_AB:
4647       s = "sw";
4648       goto st;
4649     case M_SWC0_AB:
4650       s = "swc0";
4651       /* Itbl support may require additional care here. */
4652       coproc = 1;
4653       goto st;
4654     case M_SWC1_AB:
4655       s = "swc1";
4656       /* Itbl support may require additional care here. */
4657       coproc = 1;
4658       goto st;
4659     case M_SWC2_AB:
4660       s = "swc2";
4661       /* Itbl support may require additional care here. */
4662       coproc = 1;
4663       goto st;
4664     case M_SWC3_AB:
4665       s = "swc3";
4666       /* Itbl support may require additional care here. */
4667       coproc = 1;
4668       goto st;
4669     case M_SWL_AB:
4670       s = "swl";
4671       goto st;
4672     case M_SWR_AB:
4673       s = "swr";
4674       goto st;
4675     case M_SC_AB:
4676       s = "sc";
4677       goto st;
4678     case M_SCD_AB:
4679       s = "scd";
4680       goto st;
4681     case M_SDC1_AB:
4682       s = "sdc1";
4683       coproc = 1;
4684       /* Itbl support may require additional care here. */
4685       goto st;
4686     case M_SDC2_AB:
4687       s = "sdc2";
4688       /* Itbl support may require additional care here. */
4689       coproc = 1;
4690       goto st;
4691     case M_SDC3_AB:
4692       s = "sdc3";
4693       /* Itbl support may require additional care here. */
4694       coproc = 1;
4695       goto st;
4696     case M_SDL_AB:
4697       s = "sdl";
4698       goto st;
4699     case M_SDR_AB:
4700       s = "sdr";
4701     st:
4702       tempreg = AT;
4703       used_at = 1;
4704     ld_st:
4705       /* Itbl support may require additional care here. */
4706       if (mask == M_LWC1_AB
4707           || mask == M_SWC1_AB
4708           || mask == M_LDC1_AB
4709           || mask == M_SDC1_AB
4710           || mask == M_L_DAB
4711           || mask == M_S_DAB)
4712         fmt = "T,o(b)";
4713       else if (coproc)
4714         fmt = "E,o(b)";
4715       else
4716         fmt = "t,o(b)";
4717
4718       if (offset_expr.X_op != O_constant
4719           && offset_expr.X_op != O_symbol)
4720         {
4721           as_bad ("expression too complex");
4722           offset_expr.X_op = O_constant;
4723         }
4724
4725       /* A constant expression in PIC code can be handled just as it
4726          is in non PIC code.  */
4727       if (mips_pic == NO_PIC
4728           || offset_expr.X_op == O_constant)
4729         {
4730           /* If this is a reference to a GP relative symbol, and there
4731              is no base register, we want
4732                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
4733              Otherwise, if there is no base register, we want
4734                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4735                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
4736              If we have a constant, we need two instructions anyhow,
4737              so we always use the latter form.
4738
4739              If we have a base register, and this is a reference to a
4740              GP relative symbol, we want
4741                addu     $tempreg,$breg,$gp
4742                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GPREL)
4743              Otherwise we want
4744                lui      $tempreg,<sym>          (BFD_RELOC_HI16_S)
4745                addu     $tempreg,$tempreg,$breg
4746                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_LO16)
4747              With a constant we always use the latter case.  */
4748           if (breg == 0)
4749             {
4750               if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4751                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
4752                 p = NULL;
4753               else
4754                 {
4755                   frag_grow (20);
4756                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4757                                treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4758                   p = frag_var (rs_machine_dependent, 8, 0,
4759                                 RELAX_ENCODE (4, 8, 0, 4, 0,
4760                                               (mips_opts.warn_about_macros
4761                                                || (used_at
4762                                                    && mips_opts.noat))),
4763                                 offset_expr.X_add_symbol, (offsetT) 0,
4764                                 (char *) NULL);
4765                   used_at = 0;
4766                 }
4767               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4768               if (p != NULL)
4769                 p += 4;
4770               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
4771                            (int) BFD_RELOC_LO16, tempreg);
4772             }
4773           else
4774             {
4775               if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
4776                   || nopic_need_relax (offset_expr.X_add_symbol, 1))
4777                 p = NULL;
4778               else
4779                 {
4780                   frag_grow (28);
4781                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4782                                mips_opts.isa < 3 ? "addu" : "daddu",
4783                                "d,v,t", tempreg, breg, GP);
4784                   macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4785                                treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
4786                   p = frag_var (rs_machine_dependent, 12, 0,
4787                                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
4788                                 offset_expr.X_add_symbol, (offsetT) 0,
4789                                 (char *) NULL);
4790                 }
4791               macro_build_lui (p, &icnt, &offset_expr, tempreg);
4792               if (p != NULL)
4793                 p += 4;
4794               macro_build (p, &icnt, (expressionS *) NULL,
4795                            mips_opts.isa < 3 ? "addu" : "daddu",
4796                            "d,v,t", tempreg, tempreg, breg);
4797               if (p != NULL)
4798                 p += 4;
4799               macro_build (p, &icnt, &offset_expr, s, fmt, treg,
4800                            (int) BFD_RELOC_LO16, tempreg);
4801             }
4802         }
4803       else if (mips_pic == SVR4_PIC && ! mips_big_got)
4804         {
4805           /* If this is a reference to an external symbol, we want
4806                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4807                nop
4808                <op>     $treg,0($tempreg)
4809              Otherwise we want
4810                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4811                nop
4812                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4813                <op>     $treg,0($tempreg)
4814              If there is a base register, we add it to $tempreg before
4815              the <op>.  If there is a constant, we stick it in the
4816              <op> instruction.  We don't handle constants larger than
4817              16 bits, because we have no way to load the upper 16 bits
4818              (actually, we could handle them for the subset of cases
4819              in which we are not using $at).  */
4820           assert (offset_expr.X_op == O_symbol);
4821           expr1.X_add_number = offset_expr.X_add_number;
4822           offset_expr.X_add_number = 0;
4823           if (expr1.X_add_number < -0x8000
4824               || expr1.X_add_number >= 0x8000)
4825             as_bad ("PIC code offset overflow (max 16 signed bits)");
4826           frag_grow (20);
4827           macro_build ((char *) NULL, &icnt, &offset_expr,
4828                        mips_opts.isa < 3 ? "lw" : "ld",
4829                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4830           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
4831           p = frag_var (rs_machine_dependent, 4, 0, 
4832                         RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4833                         offset_expr.X_add_symbol, (offsetT) 0,
4834                         (char *) NULL);
4835           macro_build (p, &icnt, &offset_expr,
4836                        mips_opts.isa < 3 ? "addiu" : "daddiu",
4837                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4838           if (breg != 0)
4839             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4840                          mips_opts.isa < 3 ? "addu" : "daddu",
4841                          "d,v,t", tempreg, tempreg, breg);
4842           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
4843                        (int) BFD_RELOC_LO16, tempreg);
4844         }
4845       else if (mips_pic == SVR4_PIC)
4846         {
4847           int gpdel;
4848
4849           /* If this is a reference to an external symbol, we want
4850                lui      $tempreg,<sym>          (BFD_RELOC_MIPS_GOT_HI16)
4851                addu     $tempreg,$tempreg,$gp
4852                lw       $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4853                <op>     $treg,0($tempreg)
4854              Otherwise we want
4855                lw       $tempreg,<sym>($gp)     (BFD_RELOC_MIPS_GOT16)
4856                nop
4857                addiu    $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4858                <op>     $treg,0($tempreg)
4859              If there is a base register, we add it to $tempreg before
4860              the <op>.  If there is a constant, we stick it in the
4861              <op> instruction.  We don't handle constants larger than
4862              16 bits, because we have no way to load the upper 16 bits
4863              (actually, we could handle them for the subset of cases
4864              in which we are not using $at).  */
4865           assert (offset_expr.X_op == O_symbol);
4866           expr1.X_add_number = offset_expr.X_add_number;
4867           offset_expr.X_add_number = 0;
4868           if (expr1.X_add_number < -0x8000
4869               || expr1.X_add_number >= 0x8000)
4870             as_bad ("PIC code offset overflow (max 16 signed bits)");
4871           if (reg_needs_delay (GP))
4872             gpdel = 4;
4873           else
4874             gpdel = 0;
4875           frag_grow (36);
4876           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4877                        tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
4878           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4879                        mips_opts.isa < 3 ? "addu" : "daddu",
4880                        "d,v,t", tempreg, tempreg, GP);
4881           macro_build ((char *) NULL, &icnt, &offset_expr,
4882                        mips_opts.isa < 3 ? "lw" : "ld",
4883                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
4884                        tempreg);
4885           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4886                         RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
4887                         offset_expr.X_add_symbol, (offsetT) 0, (char *) NULL);
4888           if (gpdel > 0)
4889             {
4890               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4891               p += 4;
4892             }
4893           macro_build (p, &icnt, &offset_expr,
4894                        mips_opts.isa < 3 ? "lw" : "ld",
4895                        "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4896           p += 4;
4897           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4898           p += 4;
4899           macro_build (p, &icnt, &offset_expr,
4900                        mips_opts.isa < 3 ? "addiu" : "daddiu",
4901                        "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4902           if (breg != 0)
4903             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4904                          mips_opts.isa < 3 ? "addu" : "daddu",
4905                          "d,v,t", tempreg, tempreg, breg);
4906           macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
4907                        (int) BFD_RELOC_LO16, tempreg);
4908         }
4909       else if (mips_pic == EMBEDDED_PIC)
4910         {
4911           /* If there is no base register, we want
4912                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
4913              If there is a base register, we want
4914                addu     $tempreg,$breg,$gp
4915                <op>     $treg,<sym>($tempreg)   (BFD_RELOC_MIPS_GPREL)
4916              */
4917           assert (offset_expr.X_op == O_symbol);
4918           if (breg == 0)
4919             {
4920               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4921                            treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4922               used_at = 0;
4923             }
4924           else
4925             {
4926               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4927                            mips_opts.isa < 3 ? "addu" : "daddu",
4928                            "d,v,t", tempreg, breg, GP);
4929               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4930                            treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
4931             }
4932         }
4933       else
4934         abort ();
4935
4936       if (! used_at)
4937         return;
4938
4939       break;
4940
4941     case M_LI:
4942     case M_LI_S:
4943       load_register (&icnt, treg, &imm_expr, 0);
4944       return;
4945
4946     case M_DLI:
4947       load_register (&icnt, treg, &imm_expr, 1);
4948       return;
4949
4950     case M_LI_SS:
4951       if (imm_expr.X_op == O_constant)
4952         {
4953           load_register (&icnt, AT, &imm_expr, 0);
4954           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4955                        "mtc1", "t,G", AT, treg);
4956           break;
4957         }
4958       else
4959         {
4960           assert (offset_expr.X_op == O_symbol
4961                   && strcmp (segment_name (S_GET_SEGMENT
4962                                            (offset_expr.X_add_symbol)),
4963                              ".lit4") == 0
4964                   && offset_expr.X_add_number == 0);
4965           macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
4966                        treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
4967           return;
4968         }
4969
4970     case M_LI_D:
4971       /* If we have a constant in IMM_EXPR, then in mips3 mode it is
4972          the entire value, and in mips1 mode it is the high order 32
4973          bits of the value and the low order 32 bits are either zero
4974          or in offset_expr.  */
4975       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
4976         {
4977           load_register (&icnt, treg, &imm_expr, mips_opts.isa >= 3);
4978           if (mips_opts.isa < 3 && treg != 31)
4979             {
4980               if (offset_expr.X_op == O_absent)
4981                 macro_build ((char *) NULL, &icnt, NULL, "move", "d,s",
4982                              treg + 1, 0);
4983               else
4984                 {
4985                   assert (offset_expr.X_op == O_constant);
4986                   load_register (&icnt, treg + 1, &offset_expr, 0);
4987                 }
4988             }
4989           return;
4990         }
4991
4992       /* We know that sym is in the .rdata section.  First we get the
4993          upper 16 bits of the address.  */
4994       if (mips_pic == NO_PIC)
4995         {
4996           /* FIXME: This won't work for a 64 bit address.  */
4997           macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
4998         }
4999       else if (mips_pic == SVR4_PIC)
5000         {
5001           macro_build ((char *) NULL, &icnt, &offset_expr,
5002                        mips_opts.isa < 3 ? "lw" : "ld",
5003                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5004         }
5005       else if (mips_pic == EMBEDDED_PIC)
5006         {
5007           /* For embedded PIC we pick up the entire address off $gp in
5008              a single instruction.  */
5009           macro_build ((char *) NULL, &icnt, &offset_expr,
5010                        mips_opts.isa < 3 ? "addiu" : "daddiu",
5011                        "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5012           offset_expr.X_op = O_constant;
5013           offset_expr.X_add_number = 0;
5014         }
5015       else
5016         abort ();
5017         
5018       /* Now we load the register(s).  */
5019       if (mips_opts.isa >= 3)
5020         macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5021                      treg, (int) BFD_RELOC_LO16, AT);
5022       else
5023         {
5024           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5025                        treg, (int) BFD_RELOC_LO16, AT);
5026           if (treg != 31)
5027             {
5028               /* FIXME: How in the world do we deal with the possible
5029                  overflow here?  */
5030               offset_expr.X_add_number += 4;
5031               macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5032                            treg + 1, (int) BFD_RELOC_LO16, AT);
5033             }
5034         }
5035
5036       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5037          does not become a variant frag.  */
5038       frag_wane (frag_now);
5039       frag_new (0);
5040
5041       break;
5042
5043     case M_LI_DD:
5044       /* If we have a constant in IMM_EXPR, then in mips3 mode it is
5045          the entire value, and in mips1 mode it is the high order 32
5046          bits of the value and the low order 32 bits are either zero
5047          or in offset_expr.  */
5048       if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5049         {
5050           load_register (&icnt, AT, &imm_expr, mips_opts.isa >= 3);
5051           if (mips_opts.isa >= 3)
5052             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5053                          "dmtc1", "t,S", AT, treg);
5054           else
5055             {
5056               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5057                            "mtc1", "t,G", AT, treg + 1);
5058               if (offset_expr.X_op == O_absent)
5059                 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5060                              "mtc1", "t,G", 0, treg);
5061               else
5062                 {
5063                   assert (offset_expr.X_op == O_constant);
5064                   load_register (&icnt, AT, &offset_expr, 0);
5065                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5066                                "mtc1", "t,G", AT, treg);
5067                 }
5068             }
5069           break;
5070         }
5071
5072       assert (offset_expr.X_op == O_symbol
5073               && offset_expr.X_add_number == 0);
5074       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5075       if (strcmp (s, ".lit8") == 0)
5076         {
5077           if (mips_opts.isa >= 2)
5078             {
5079               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5080                            "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5081               return;
5082             }
5083           breg = GP;
5084           r = BFD_RELOC_MIPS_LITERAL;
5085           goto dob;
5086         }
5087       else
5088         {
5089           assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5090           if (mips_pic == SVR4_PIC)
5091             macro_build ((char *) NULL, &icnt, &offset_expr,
5092                          mips_opts.isa < 3 ? "lw" : "ld",
5093                          "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5094           else
5095             {
5096               /* FIXME: This won't work for a 64 bit address.  */
5097               macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5098             }
5099               
5100           if (mips_opts.isa >= 2)
5101             {
5102               macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5103                            "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5104
5105               /* To avoid confusion in tc_gen_reloc, we must ensure
5106                  that this does not become a variant frag.  */
5107               frag_wane (frag_now);
5108               frag_new (0);
5109
5110               break;
5111             }
5112           breg = AT;
5113           r = BFD_RELOC_LO16;
5114           goto dob;
5115         }
5116
5117     case M_L_DOB:
5118       /* Even on a big endian machine $fn comes before $fn+1.  We have
5119          to adjust when loading from memory.  */
5120       r = BFD_RELOC_LO16;
5121     dob:
5122       assert (mips_opts.isa < 2);
5123       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5124                    target_big_endian ? treg + 1 : treg,
5125                    (int) r, breg);
5126       /* FIXME: A possible overflow which I don't know how to deal
5127          with.  */
5128       offset_expr.X_add_number += 4;
5129       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5130                    target_big_endian ? treg : treg + 1,
5131                    (int) r, breg);
5132
5133       /* To avoid confusion in tc_gen_reloc, we must ensure that this
5134          does not become a variant frag.  */
5135       frag_wane (frag_now);
5136       frag_new (0);
5137
5138       if (breg != AT)
5139         return;
5140       break;
5141
5142     case M_L_DAB:
5143       /*
5144        * The MIPS assembler seems to check for X_add_number not
5145        * being double aligned and generating:
5146        *        lui     at,%hi(foo+1)
5147        *        addu    at,at,v1
5148        *        addiu   at,at,%lo(foo+1)
5149        *        lwc1    f2,0(at)
5150        *        lwc1    f3,4(at)
5151        * But, the resulting address is the same after relocation so why
5152        * generate the extra instruction?
5153        */
5154       /* Itbl support may require additional care here. */
5155       coproc = 1;
5156       if (mips_opts.isa >= 2)
5157         {
5158           s = "ldc1";
5159           goto ld;
5160         }
5161
5162       s = "lwc1";
5163       fmt = "T,o(b)";
5164       goto ldd_std;
5165
5166     case M_S_DAB:
5167       if (mips_opts.isa >= 2)
5168         {
5169           s = "sdc1";
5170           goto st;
5171         }
5172
5173       s = "swc1";
5174       fmt = "T,o(b)";
5175       /* Itbl support may require additional care here. */
5176       coproc = 1;
5177       goto ldd_std;
5178
5179     case M_LD_AB:
5180       if (mips_opts.isa >= 3)
5181         {
5182           s = "ld";
5183           goto ld;
5184         }
5185
5186       s = "lw";
5187       fmt = "t,o(b)";
5188       goto ldd_std;
5189
5190     case M_SD_AB:
5191       if (mips_opts.isa >= 3)
5192         {
5193           s = "sd";
5194           goto st;
5195         }
5196
5197       s = "sw";
5198       fmt = "t,o(b)";
5199
5200     ldd_std:
5201       if (offset_expr.X_op != O_symbol
5202           && offset_expr.X_op != O_constant)
5203         {
5204           as_bad ("expression too complex");
5205           offset_expr.X_op = O_constant;
5206         }
5207
5208       /* Even on a big endian machine $fn comes before $fn+1.  We have
5209          to adjust when loading from memory.  We set coproc if we must
5210          load $fn+1 first.  */
5211       /* Itbl support may require additional care here. */
5212       if (! target_big_endian)
5213         coproc = 0;
5214
5215       if (mips_pic == NO_PIC
5216           || offset_expr.X_op == O_constant)
5217         {
5218           /* If this is a reference to a GP relative symbol, we want
5219                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
5220                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_MIPS_GPREL)
5221              If we have a base register, we use this
5222                addu     $at,$breg,$gp
5223                <op>     $treg,<sym>($at)        (BFD_RELOC_MIPS_GPREL)
5224                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_MIPS_GPREL)
5225              If this is not a GP relative symbol, we want
5226                lui      $at,<sym>               (BFD_RELOC_HI16_S)
5227                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
5228                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
5229              If there is a base register, we add it to $at after the
5230              lui instruction.  If there is a constant, we always use
5231              the last case.  */
5232           if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
5233               || nopic_need_relax (offset_expr.X_add_symbol, 1))
5234             {
5235               p = NULL;
5236               used_at = 1;
5237             }
5238           else
5239             {
5240               int off;
5241
5242               if (breg == 0)
5243                 {
5244                   frag_grow (28);
5245                   tempreg = GP;
5246                   off = 0;
5247                   used_at = 0;
5248                 }
5249               else
5250                 {
5251                   frag_grow (36);
5252                   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5253                                mips_opts.isa < 3 ? "addu" : "daddu",
5254                                "d,v,t", AT, breg, GP);
5255                   tempreg = AT;
5256                   off = 4;
5257                   used_at = 1;
5258                 }
5259
5260               /* Itbl support may require additional care here. */
5261               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5262                            coproc ? treg + 1 : treg,
5263                            (int) BFD_RELOC_MIPS_GPREL, tempreg);
5264               offset_expr.X_add_number += 4;
5265
5266               /* Set mips_optimize to 2 to avoid inserting an
5267                  undesired nop.  */
5268               hold_mips_optimize = mips_optimize;
5269               mips_optimize = 2;
5270               /* Itbl support may require additional care here. */
5271               macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5272                            coproc ? treg : treg + 1,
5273                            (int) BFD_RELOC_MIPS_GPREL, tempreg);
5274               mips_optimize = hold_mips_optimize;
5275
5276               p = frag_var (rs_machine_dependent, 12 + off, 0,
5277                             RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
5278                                           used_at && mips_opts.noat),
5279                             offset_expr.X_add_symbol, (offsetT) 0,
5280                             (char *) NULL);
5281
5282               /* We just generated two relocs.  When tc_gen_reloc
5283                  handles this case, it will skip the first reloc and
5284                  handle the second.  The second reloc already has an
5285                  extra addend of 4, which we added above.  We must
5286                  subtract it out, and then subtract another 4 to make
5287                  the first reloc come out right.  The second reloc
5288                  will come out right because we are going to add 4 to
5289                  offset_expr when we build its instruction below.
5290
5291                  If we have a symbol, then we don't want to include
5292                  the offset, because it will wind up being included
5293                  when we generate the reloc.  */
5294
5295               if (offset_expr.X_op == O_constant)
5296                 offset_expr.X_add_number -= 8;
5297               else
5298                 {
5299                   offset_expr.X_add_number = -4;
5300                   offset_expr.X_op = O_constant;
5301                 }
5302             }
5303           macro_build_lui (p, &icnt, &offset_expr, AT);
5304           if (p != NULL)
5305             p += 4;
5306           if (breg != 0)
5307             {
5308               macro_build (p, &icnt, (expressionS *) NULL,
5309                            mips_opts.isa < 3 ? "addu" : "daddu",
5310                            "d,v,t", AT, breg, AT);
5311               if (p != NULL)
5312                 p += 4;
5313             }
5314           /* Itbl support may require additional care here. */
5315           macro_build (p, &icnt, &offset_expr, s, fmt,
5316                        coproc ? treg + 1 : treg,
5317                        (int) BFD_RELOC_LO16, AT);
5318           if (p != NULL)
5319             p += 4;
5320           /* FIXME: How do we handle overflow here?  */
5321           offset_expr.X_add_number += 4;
5322           /* Itbl support may require additional care here. */
5323           macro_build (p, &icnt, &offset_expr, s, fmt,
5324                        coproc ? treg : treg + 1,
5325                        (int) BFD_RELOC_LO16, AT);
5326         }         
5327       else if (mips_pic == SVR4_PIC && ! mips_big_got)
5328         {
5329           int off;
5330
5331           /* If this is a reference to an external symbol, we want
5332                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5333                nop
5334                <op>     $treg,0($at)
5335                <op>     $treg+1,4($at)
5336              Otherwise we want
5337                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5338                nop
5339                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
5340                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
5341              If there is a base register we add it to $at before the
5342              lwc1 instructions.  If there is a constant we include it
5343              in the lwc1 instructions.  */
5344           used_at = 1;
5345           expr1.X_add_number = offset_expr.X_add_number;
5346           offset_expr.X_add_number = 0;
5347           if (expr1.X_add_number < -0x8000
5348               || expr1.X_add_number >= 0x8000 - 4)
5349             as_bad ("PIC code offset overflow (max 16 signed bits)");
5350           if (breg == 0)
5351             off = 0;
5352           else
5353             off = 4;
5354           frag_grow (24 + off);
5355           macro_build ((char *) NULL, &icnt, &offset_expr,
5356                        mips_opts.isa < 3 ? "lw" : "ld",
5357                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5358           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5359           if (breg != 0)
5360             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5361                          mips_opts.isa < 3 ? "addu" : "daddu",
5362                          "d,v,t", AT, breg, AT);
5363           /* Itbl support may require additional care here. */
5364           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5365                        coproc ? treg + 1 : treg,
5366                        (int) BFD_RELOC_LO16, AT);
5367           expr1.X_add_number += 4;
5368
5369           /* Set mips_optimize to 2 to avoid inserting an undesired
5370              nop.  */
5371           hold_mips_optimize = mips_optimize;
5372           mips_optimize = 2;
5373           /* Itbl support may require additional care here. */
5374           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5375                        coproc ? treg : treg + 1,
5376                        (int) BFD_RELOC_LO16, AT);
5377           mips_optimize = hold_mips_optimize;
5378
5379           (void) frag_var (rs_machine_dependent, 0, 0,
5380                            RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
5381                            offset_expr.X_add_symbol, (offsetT) 0,
5382                            (char *) NULL);
5383         }
5384       else if (mips_pic == SVR4_PIC)
5385         {
5386           int gpdel, off;
5387
5388           /* If this is a reference to an external symbol, we want
5389                lui      $at,<sym>               (BFD_RELOC_MIPS_GOT_HI16)
5390                addu     $at,$at,$gp
5391                lw       $at,<sym>($at)          (BFD_RELOC_MIPS_GOT_LO16)
5392                nop
5393                <op>     $treg,0($at)
5394                <op>     $treg+1,4($at)
5395              Otherwise we want
5396                lw       $at,<sym>($gp)          (BFD_RELOC_MIPS_GOT16)
5397                nop
5398                <op>     $treg,<sym>($at)        (BFD_RELOC_LO16)
5399                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
5400              If there is a base register we add it to $at before the
5401              lwc1 instructions.  If there is a constant we include it
5402              in the lwc1 instructions.  */
5403           used_at = 1;
5404           expr1.X_add_number = offset_expr.X_add_number;
5405           offset_expr.X_add_number = 0;
5406           if (expr1.X_add_number < -0x8000
5407               || expr1.X_add_number >= 0x8000 - 4)
5408             as_bad ("PIC code offset overflow (max 16 signed bits)");
5409           if (reg_needs_delay (GP))
5410             gpdel = 4;
5411           else
5412             gpdel = 0;
5413           if (breg == 0)
5414             off = 0;
5415           else
5416             off = 4;
5417           frag_grow (56);
5418           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5419                        AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5420           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5421                        mips_opts.isa < 3 ? "addu" : "daddu",
5422                        "d,v,t", AT, AT, GP);
5423           macro_build ((char *) NULL, &icnt, &offset_expr,
5424                        mips_opts.isa < 3 ? "lw" : "ld",
5425                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5426           macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5427           if (breg != 0)
5428             macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5429                          mips_opts.isa < 3 ? "addu" : "daddu",
5430                          "d,v,t", AT, breg, AT);
5431           /* Itbl support may require additional care here. */
5432           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5433                        coproc ? treg + 1 : treg,
5434                        (int) BFD_RELOC_LO16, AT);
5435           expr1.X_add_number += 4;
5436
5437           /* Set mips_optimize to 2 to avoid inserting an undesired
5438              nop.  */
5439           hold_mips_optimize = mips_optimize;
5440           mips_optimize = 2;
5441           /* Itbl support may require additional care here. */
5442           macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5443                        coproc ? treg : treg + 1,
5444                        (int) BFD_RELOC_LO16, AT);
5445           mips_optimize = hold_mips_optimize;
5446           expr1.X_add_number -= 4;
5447
5448           p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5449                         RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5450                                       8 + gpdel + off, 1, 0),
5451                         offset_expr.X_add_symbol, (offsetT) 0,
5452                         (char *) NULL);
5453           if (gpdel > 0)
5454             {
5455               macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5456               p += 4;
5457             }
5458           macro_build (p, &icnt, &offset_expr,
5459                        mips_opts.isa < 3 ? "lw" : "ld",
5460                        "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5461           p += 4;
5462           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5463           p += 4;
5464           if (breg != 0)
5465             {
5466               macro_build (p, &icnt, (expressionS *) NULL,
5467                            mips_opts.isa < 3 ? "addu" : "daddu",
5468                            "d,v,t", AT, breg, AT);
5469               p += 4;
5470             }
5471           /* Itbl support may require additional care here. */
5472           macro_build (p, &icnt, &expr1, s, fmt,
5473                        coproc ? treg + 1 : treg,
5474                        (int) BFD_RELOC_LO16, AT);
5475           p += 4;
5476           expr1.X_add_number += 4;
5477
5478           /* Set mips_optimize to 2 to avoid inserting an undesired
5479              nop.  */
5480           hold_mips_optimize = mips_optimize;
5481           mips_optimize = 2;
5482           /* Itbl support may require additional care here. */
5483           macro_build (p, &icnt, &expr1, s, fmt,
5484                        coproc ? treg : treg + 1,
5485                        (int) BFD_RELOC_LO16, AT);
5486           mips_optimize = hold_mips_optimize;
5487         }
5488       else if (mips_pic == EMBEDDED_PIC)
5489         {
5490           /* If there is no base register, we use
5491                <op>     $treg,<sym>($gp)        (BFD_RELOC_MIPS_GPREL)
5492                <op>     $treg+1,<sym>+4($gp)    (BFD_RELOC_MIPS_GPREL)
5493              If we have a base register, we use
5494                addu     $at,$breg,$gp
5495                <op>     $treg,<sym>($at)        (BFD_RELOC_MIPS_GPREL)
5496                <op>     $treg+1,<sym>+4($at)    (BFD_RELOC_MIPS_GPREL)
5497              */
5498           if (breg == 0)
5499             {
5500               tempreg = GP;
5501               used_at = 0;
5502             }
5503           else
5504             {
5505               macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5506                            mips_opts.isa < 3 ? "addu" : "daddu",
5507                            "d,v,t", AT, breg, GP);
5508               tempreg = AT;
5509               used_at = 1;
5510             }
5511
5512           /* Itbl support may require additional care here. */
5513           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5514                        coproc ? treg + 1 : treg,
5515                        (int) BFD_RELOC_MIPS_GPREL, tempreg);
5516           offset_expr.X_add_number += 4;
5517           /* Itbl support may require additional care here. */
5518           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5519                        coproc ? treg : treg + 1,
5520                        (int) BFD_RELOC_MIPS_GPREL, tempreg);
5521         }
5522       else
5523         abort ();
5524
5525       if (! used_at)
5526         return;
5527
5528       break;
5529
5530     case M_LD_OB:
5531       s = "lw";
5532       goto sd_ob;
5533     case M_SD_OB:
5534       s = "sw";
5535     sd_ob:
5536       assert (mips_opts.isa < 3);
5537       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5538                    (int) BFD_RELOC_LO16, breg);
5539       offset_expr.X_add_number += 4;
5540       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5541                    (int) BFD_RELOC_LO16, breg);
5542       return;
5543
5544    /* New code added to support COPZ instructions.
5545       This code builds table entries out of the macros in mip_opcodes.
5546       R4000 uses interlocks to handle coproc delays.
5547       Other chips (like the R3000) require nops to be inserted for delays.
5548
5549       FIXME: Currently, we require that the user handle delays.
5550       In order to fill delay slots for non-interlocked chips,
5551       we must have a way to specify delays based on the coprocessor.
5552       Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
5553       What are the side-effects of the cop instruction?
5554       What cache support might we have and what are its effects?
5555       Both coprocessor & memory require delays. how long???
5556       What registers are read/set/modified? 
5557
5558       If an itbl is provided to interpret cop instructions,
5559       this knowledge can be encoded in the itbl spec. */
5560
5561     case M_COP0:
5562       s = "cop0";
5563       goto copz;
5564     case M_COP1:
5565       s = "cop1";
5566       goto copz;
5567     case M_COP2:
5568       s = "cop2";
5569       goto copz;
5570     case M_COP3:
5571       s = "cop3";
5572     copz:
5573       /* For now we just do C (same as Cz). */
5574       macro_build ((char *) NULL, &icnt, &offset_expr, s, "C");
5575       return;
5576
5577 #ifdef LOSING_COMPILER
5578     default:
5579       /* Try and see if this is a new itbl instruction.
5580          This code builds table entries out of the macros in mip_opcodes.
5581          FIXME: For now we just assemble the expression and pass it's
5582          value along as a 32-bit immediate.
5583          We may want to have the assembler assemble this value, 
5584          so that we gain the assembler's knowledge of delay slots,
5585          symbols, etc.
5586          Would it be more efficient to use mask (id) here? */
5587       if (itbl_have_entries 
5588           && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
5589         {
5590           s = ip->insn_mo->name;
5591           s2 = "cop3";
5592           coproc = ITBL_DECODE_PNUM (immed_expr);;
5593           macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
5594           return;
5595         }
5596       macro2 (ip);
5597       return;
5598     }
5599   if (mips_opts.noat)
5600     as_warn ("Macro used $at after \".set noat\"");
5601 }
5602           
5603 static void
5604 macro2 (ip)
5605      struct mips_cl_insn *ip;
5606 {
5607   register int treg, sreg, dreg, breg;
5608   int tempreg;
5609   int mask;
5610   int icnt = 0;
5611   int used_at;
5612   expressionS expr1;
5613   const char *s;
5614   const char *s2;
5615   const char *fmt;
5616   int likely = 0;
5617   int dbl = 0;
5618   int coproc = 0;
5619   int lr = 0;
5620   int off;
5621   offsetT maxnum;
5622   bfd_reloc_code_real_type r;
5623   char *p;
5624           
5625   treg = (ip->insn_opcode >> 16) & 0x1f;
5626   dreg = (ip->insn_opcode >> 11) & 0x1f;
5627   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
5628   mask = ip->insn_mo->mask;
5629           
5630   expr1.X_op = O_constant;
5631   expr1.X_op_symbol = NULL;
5632   expr1.X_add_symbol = NULL;
5633   expr1.X_add_number = 1;
5634           
5635   switch (mask)
5636     {
5637 #endif /* LOSING_COMPILER */
5638
5639     case M_DMUL:
5640       dbl = 1;
5641     case M_MUL:
5642       macro_build ((char *) NULL, &icnt, NULL,
5643                    dbl ? "dmultu" : "multu",
5644                    "s,t", sreg, treg);
5645       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5646       return;
5647
5648     case M_DMUL_I:
5649       dbl = 1;
5650     case M_MUL_I:
5651       /* The MIPS assembler some times generates shifts and adds.  I'm
5652          not trying to be that fancy. GCC should do this for us
5653          anyway.  */
5654       load_register (&icnt, AT, &imm_expr, dbl);
5655       macro_build ((char *) NULL, &icnt, NULL,
5656                    dbl ? "dmult" : "mult",
5657                    "s,t", sreg, AT);
5658       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5659       break;
5660
5661     case M_DMULO:
5662       dbl = 1;
5663     case M_MULO:
5664       mips_emit_delays (true);
5665       ++mips_opts.noreorder;
5666       mips_any_noreorder = 1;
5667       macro_build ((char *) NULL, &icnt, NULL,
5668                    dbl ? "dmult" : "mult",
5669                    "s,t", sreg, treg);
5670       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5671       macro_build ((char *) NULL, &icnt, NULL,
5672                    dbl ? "dsra32" : "sra",
5673                    "d,w,<", dreg, dreg, 31);
5674       macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5675       if (mips_trap)
5676         macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
5677       else
5678         {
5679           expr1.X_add_number = 8;
5680           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
5681           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5682           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5683         }
5684       --mips_opts.noreorder;
5685       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5686       break;
5687
5688     case M_DMULOU:
5689       dbl = 1;
5690     case M_MULOU:
5691       mips_emit_delays (true);
5692       ++mips_opts.noreorder;
5693       mips_any_noreorder = 1;
5694       macro_build ((char *) NULL, &icnt, NULL,
5695                    dbl ? "dmultu" : "multu",
5696                    "s,t", sreg, treg);
5697       macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5698       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5699       if (mips_trap)
5700         macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
5701       else
5702         {
5703           expr1.X_add_number = 8;
5704           macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
5705           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5706           macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5707         }
5708       --mips_opts.noreorder;
5709       break;
5710
5711     case M_ROL:
5712       macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
5713       macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
5714       macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
5715                    treg);
5716       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5717       break;
5718
5719     case M_ROL_I:
5720       if (imm_expr.X_op != O_constant)
5721         as_bad ("rotate count too large");
5722       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
5723                    (int) (imm_expr.X_add_number & 0x1f));
5724       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
5725                    (int) ((0 - imm_expr.X_add_number) & 0x1f));
5726       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5727       break;
5728
5729     case M_ROR:
5730       macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
5731       macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
5732       macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
5733                    treg);
5734       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5735       break;
5736
5737     case M_ROR_I:
5738       if (imm_expr.X_op != O_constant)
5739         as_bad ("rotate count too large");
5740       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
5741                    (int) (imm_expr.X_add_number & 0x1f));
5742       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
5743                    (int) ((0 - imm_expr.X_add_number) & 0x1f));
5744       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
5745       break;
5746
5747     case M_S_DOB:
5748       assert (mips_opts.isa < 2);
5749       /* Even on a big endian machine $fn comes before $fn+1.  We have
5750          to adjust when storing to memory.  */
5751       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
5752                    target_big_endian ? treg + 1 : treg,
5753                    (int) BFD_RELOC_LO16, breg);
5754       offset_expr.X_add_number += 4;
5755       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
5756                    target_big_endian ? treg : treg + 1,
5757                    (int) BFD_RELOC_LO16, breg);
5758       return;
5759
5760     case M_SEQ:
5761       if (sreg == 0)
5762         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5763                      treg, (int) BFD_RELOC_LO16);
5764       else if (treg == 0)
5765         macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5766                      sreg, (int) BFD_RELOC_LO16);
5767       else
5768         {
5769           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5770                        sreg, treg);
5771           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5772                        dreg, (int) BFD_RELOC_LO16);
5773         }
5774       return;
5775
5776     case M_SEQ_I:
5777       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5778         {
5779           macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
5780                        sreg, (int) BFD_RELOC_LO16);
5781           return;
5782         }
5783       if (sreg == 0)
5784         {
5785           as_warn ("Instruction %s: result is always false",
5786                    ip->insn_mo->name);
5787           macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
5788           return;
5789         }
5790       if (imm_expr.X_op == O_constant
5791           && imm_expr.X_add_number >= 0
5792           && imm_expr.X_add_number < 0x10000)
5793         {
5794           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
5795                        sreg, (int) BFD_RELOC_LO16);
5796           used_at = 0;
5797         }
5798       else if (imm_expr.X_op == O_constant
5799                && imm_expr.X_add_number > -0x8000
5800                && imm_expr.X_add_number < 0)
5801         {
5802           imm_expr.X_add_number = -imm_expr.X_add_number;
5803           macro_build ((char *) NULL, &icnt, &imm_expr,
5804                        mips_opts.isa < 3 ? "addiu" : "daddiu",
5805                        "t,r,j", dreg, sreg,
5806                        (int) BFD_RELOC_LO16);
5807           used_at = 0;
5808         }
5809       else
5810         {
5811           load_register (&icnt, AT, &imm_expr, 0);
5812           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5813                        sreg, AT);
5814           used_at = 1;
5815         }
5816       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
5817                    (int) BFD_RELOC_LO16);
5818       if (used_at)
5819         break;
5820       return;
5821
5822     case M_SGE:         /* sreg >= treg <==> not (sreg < treg) */
5823       s = "slt";
5824       goto sge;
5825     case M_SGEU:
5826       s = "sltu";
5827     sge:
5828       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
5829       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5830                    (int) BFD_RELOC_LO16);
5831       return;
5832
5833     case M_SGE_I:               /* sreg >= I <==> not (sreg < I) */
5834     case M_SGEU_I:
5835       if (imm_expr.X_op == O_constant
5836           && imm_expr.X_add_number >= -0x8000
5837           && imm_expr.X_add_number < 0x8000)
5838         {
5839           macro_build ((char *) NULL, &icnt, &expr1,
5840                        mask == M_SGE_I ? "slti" : "sltiu",
5841                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5842           used_at = 0;
5843         }
5844       else
5845         {
5846           load_register (&icnt, AT, &imm_expr, 0);
5847           macro_build ((char *) NULL, &icnt, NULL,
5848                        mask == M_SGE_I ? "slt" : "sltu",
5849                        "d,v,t", dreg, sreg, AT);
5850           used_at = 1;
5851         }
5852       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5853                    (int) BFD_RELOC_LO16);
5854       if (used_at)
5855         break;
5856       return;
5857
5858     case M_SGT:         /* sreg > treg  <==>  treg < sreg */
5859       s = "slt";
5860       goto sgt;
5861     case M_SGTU:
5862       s = "sltu";
5863     sgt:
5864       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
5865       return;
5866
5867     case M_SGT_I:               /* sreg > I  <==>  I < sreg */
5868       s = "slt";
5869       goto sgti;
5870     case M_SGTU_I:
5871       s = "sltu";
5872     sgti:
5873       load_register (&icnt, AT, &imm_expr, 0);
5874       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
5875       break;
5876
5877     case M_SLE:         /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
5878       s = "slt";
5879       goto sle;
5880     case M_SLEU:
5881       s = "sltu";
5882     sle:
5883       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
5884       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5885                    (int) BFD_RELOC_LO16);
5886       return;
5887
5888     case M_SLE_I:               /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
5889       s = "slt";
5890       goto slei;
5891     case M_SLEU_I:
5892       s = "sltu";
5893     slei:
5894       load_register (&icnt, AT, &imm_expr, 0);
5895       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
5896       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
5897                    (int) BFD_RELOC_LO16);
5898       break;
5899
5900     case M_SLT_I:
5901       if (imm_expr.X_op == O_constant
5902           && imm_expr.X_add_number >= -0x8000
5903           && imm_expr.X_add_number < 0x8000)
5904         {
5905           macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
5906                        dreg, sreg, (int) BFD_RELOC_LO16);
5907           return;
5908         }
5909       load_register (&icnt, AT, &imm_expr, 0);
5910       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
5911       break;
5912
5913     case M_SLTU_I:
5914       if (imm_expr.X_op == O_constant
5915           && imm_expr.X_add_number >= -0x8000
5916           && imm_expr.X_add_number < 0x8000)
5917         {
5918           macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
5919                        dreg, sreg, (int) BFD_RELOC_LO16);
5920           return;
5921         }
5922       load_register (&icnt, AT, &imm_expr, 0);
5923       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
5924                    AT);
5925       break;
5926
5927     case M_SNE:
5928       if (sreg == 0)
5929         macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5930                      treg);
5931       else if (treg == 0)
5932         macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5933                      sreg);
5934       else
5935         {
5936           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5937                        sreg, treg);
5938           macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5939                        dreg);
5940         }
5941       return;
5942
5943     case M_SNE_I:
5944       if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5945         {
5946           macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
5947                        sreg);
5948           return;
5949         }
5950       if (sreg == 0)
5951         {
5952           as_warn ("Instruction %s: result is always true",
5953                    ip->insn_mo->name);
5954           macro_build ((char *) NULL, &icnt, &expr1,
5955                        mips_opts.isa < 3 ? "addiu" : "daddiu",
5956                        "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
5957           return;
5958         }
5959       if (imm_expr.X_op == O_constant
5960           && imm_expr.X_add_number >= 0
5961           && imm_expr.X_add_number < 0x10000)
5962         {
5963           macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
5964                        dreg, sreg, (int) BFD_RELOC_LO16);
5965           used_at = 0;
5966         }
5967       else if (imm_expr.X_op == O_constant
5968                && imm_expr.X_add_number > -0x8000
5969                && imm_expr.X_add_number < 0)
5970         {
5971           imm_expr.X_add_number = -imm_expr.X_add_number;
5972           macro_build ((char *) NULL, &icnt, &imm_expr,
5973                        mips_opts.isa < 3 ? "addiu" : "daddiu",
5974                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
5975           used_at = 0;
5976         }
5977       else
5978         {
5979           load_register (&icnt, AT, &imm_expr, 0);
5980           macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
5981                        sreg, AT);
5982           used_at = 1;
5983         }
5984       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
5985       if (used_at)
5986         break;
5987       return;
5988
5989     case M_DSUB_I:
5990       dbl = 1;
5991     case M_SUB_I:
5992       if (imm_expr.X_op == O_constant
5993           && imm_expr.X_add_number > -0x8000
5994           && imm_expr.X_add_number <= 0x8000)
5995         {
5996           imm_expr.X_add_number = -imm_expr.X_add_number;
5997           macro_build ((char *) NULL, &icnt, &imm_expr,
5998                        dbl ? "daddi" : "addi",
5999                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6000           return;
6001         }
6002       load_register (&icnt, AT, &imm_expr, dbl);
6003       macro_build ((char *) NULL, &icnt, NULL,
6004                    dbl ? "dsub" : "sub",
6005                    "d,v,t", dreg, sreg, AT);
6006       break;
6007
6008     case M_DSUBU_I:
6009       dbl = 1;
6010     case M_SUBU_I:
6011       if (imm_expr.X_op == O_constant
6012           && imm_expr.X_add_number > -0x8000
6013           && imm_expr.X_add_number <= 0x8000)
6014         {
6015           imm_expr.X_add_number = -imm_expr.X_add_number;
6016           macro_build ((char *) NULL, &icnt, &imm_expr,
6017                        dbl ? "daddiu" : "addiu",
6018                        "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6019           return;
6020         }
6021       load_register (&icnt, AT, &imm_expr, dbl);
6022       macro_build ((char *) NULL, &icnt, NULL,
6023                    dbl ? "dsubu" : "subu",
6024                    "d,v,t", dreg, sreg, AT);
6025       break;
6026
6027     case M_TEQ_I:
6028       s = "teq";
6029       goto trap;
6030     case M_TGE_I:
6031       s = "tge";
6032       goto trap;
6033     case M_TGEU_I:
6034       s = "tgeu";
6035       goto trap;
6036     case M_TLT_I:
6037       s = "tlt";
6038       goto trap;
6039     case M_TLTU_I:
6040       s = "tltu";
6041       goto trap;
6042     case M_TNE_I:
6043       s = "tne";
6044     trap:
6045       load_register (&icnt, AT, &imm_expr, 0);
6046       macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6047       break;
6048
6049     case M_TRUNCWD:
6050     case M_TRUNCWS:
6051       assert (mips_opts.isa < 2);
6052       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
6053       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
6054
6055       /*
6056        * Is the double cfc1 instruction a bug in the mips assembler;
6057        * or is there a reason for it?
6058        */
6059       mips_emit_delays (true);
6060       ++mips_opts.noreorder;
6061       mips_any_noreorder = 1;
6062       macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6063       macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6064       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6065       expr1.X_add_number = 3;
6066       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6067                    (int) BFD_RELOC_LO16);
6068       expr1.X_add_number = 2;
6069       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6070                      (int) BFD_RELOC_LO16);
6071       macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6072       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6073       macro_build ((char *) NULL, &icnt, NULL,
6074               mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6075       macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6076       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6077       --mips_opts.noreorder;
6078       break;
6079
6080     case M_ULH:
6081       s = "lb";
6082       goto ulh;
6083     case M_ULHU:
6084       s = "lbu";
6085     ulh:
6086       if (offset_expr.X_add_number >= 0x7fff)
6087         as_bad ("operand overflow");
6088       /* avoid load delay */
6089       if (! target_big_endian)
6090         offset_expr.X_add_number += 1;
6091       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6092                    (int) BFD_RELOC_LO16, breg);
6093       if (! target_big_endian)
6094         offset_expr.X_add_number -= 1;
6095       else
6096         offset_expr.X_add_number += 1;
6097       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6098                    (int) BFD_RELOC_LO16, breg);
6099       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6100       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6101       break;
6102
6103     case M_ULD:
6104       s = "ldl";
6105       s2 = "ldr";
6106       off = 7;
6107       goto ulw;
6108     case M_ULW:
6109       s = "lwl";
6110       s2 = "lwr";
6111       off = 3;
6112     ulw:
6113       if (offset_expr.X_add_number >= 0x8000 - off)
6114         as_bad ("operand overflow");
6115       if (! target_big_endian)
6116         offset_expr.X_add_number += off;
6117       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6118                    (int) BFD_RELOC_LO16, breg);
6119       if (! target_big_endian)
6120         offset_expr.X_add_number -= off;
6121       else
6122         offset_expr.X_add_number += off;
6123       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6124                    (int) BFD_RELOC_LO16, breg);
6125       return;
6126
6127     case M_ULD_A:
6128       s = "ldl";
6129       s2 = "ldr";
6130       off = 7;
6131       goto ulwa;
6132     case M_ULW_A:
6133       s = "lwl";
6134       s2 = "lwr";
6135       off = 3;
6136     ulwa:
6137       load_address (&icnt, AT, &offset_expr);
6138       if (breg != 0)
6139         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6140                      mips_opts.isa < 3 ? "addu" : "daddu",
6141                      "d,v,t", AT, AT, breg);
6142       if (! target_big_endian)
6143         expr1.X_add_number = off;
6144       else
6145         expr1.X_add_number = 0;
6146       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6147                    (int) BFD_RELOC_LO16, AT);
6148       if (! target_big_endian)
6149         expr1.X_add_number = 0;
6150       else
6151         expr1.X_add_number = off;
6152       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6153                    (int) BFD_RELOC_LO16, AT);
6154       break;
6155
6156     case M_ULH_A:
6157     case M_ULHU_A:
6158       load_address (&icnt, AT, &offset_expr);
6159       if (breg != 0)
6160         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6161                      mips_opts.isa < 3 ? "addu" : "daddu",
6162                      "d,v,t", AT, AT, breg);
6163       if (target_big_endian)
6164         expr1.X_add_number = 0;
6165       macro_build ((char *) NULL, &icnt, &expr1,
6166                    mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6167                    (int) BFD_RELOC_LO16, AT);
6168       if (target_big_endian)
6169         expr1.X_add_number = 1;
6170       else
6171         expr1.X_add_number = 0;
6172       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6173                    (int) BFD_RELOC_LO16, AT);
6174       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6175                    treg, 8);
6176       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6177                    treg, AT);
6178       break;
6179
6180     case M_USH:
6181       if (offset_expr.X_add_number >= 0x7fff)
6182         as_bad ("operand overflow");
6183       if (target_big_endian)
6184         offset_expr.X_add_number += 1;
6185       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6186                    (int) BFD_RELOC_LO16, breg);
6187       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6188       if (target_big_endian)
6189         offset_expr.X_add_number -= 1;
6190       else
6191         offset_expr.X_add_number += 1;
6192       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6193                    (int) BFD_RELOC_LO16, breg);
6194       break;
6195
6196     case M_USD:
6197       s = "sdl";
6198       s2 = "sdr";
6199       off = 7;
6200       goto usw;
6201     case M_USW:
6202       s = "swl";
6203       s2 = "swr";
6204       off = 3;
6205     usw:
6206       if (offset_expr.X_add_number >= 0x8000 - off)
6207         as_bad ("operand overflow");
6208       if (! target_big_endian)
6209         offset_expr.X_add_number += off;
6210       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6211                    (int) BFD_RELOC_LO16, breg);
6212       if (! target_big_endian)
6213         offset_expr.X_add_number -= off;
6214       else
6215         offset_expr.X_add_number += off;
6216       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6217                    (int) BFD_RELOC_LO16, breg);
6218       return;
6219
6220     case M_USD_A:
6221       s = "sdl";
6222       s2 = "sdr";
6223       off = 7;
6224       goto uswa;
6225     case M_USW_A:
6226       s = "swl";
6227       s2 = "swr";
6228       off = 3;
6229     uswa:
6230       load_address (&icnt, AT, &offset_expr);
6231       if (breg != 0)
6232         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6233                      mips_opts.isa < 3 ? "addu" : "daddu",
6234                      "d,v,t", AT, AT, breg);
6235       if (! target_big_endian)
6236         expr1.X_add_number = off;
6237       else
6238         expr1.X_add_number = 0;
6239       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6240                    (int) BFD_RELOC_LO16, AT);
6241       if (! target_big_endian)
6242         expr1.X_add_number = 0;
6243       else
6244         expr1.X_add_number = off;
6245       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6246                    (int) BFD_RELOC_LO16, AT);
6247       break;
6248
6249     case M_USH_A:
6250       load_address (&icnt, AT, &offset_expr);
6251       if (breg != 0)
6252         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6253                      mips_opts.isa < 3 ? "addu" : "daddu",
6254                      "d,v,t", AT, AT, breg);
6255       if (! target_big_endian)
6256         expr1.X_add_number = 0;
6257       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6258                    (int) BFD_RELOC_LO16, AT);
6259       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
6260                    treg, 8);
6261       if (! target_big_endian)
6262         expr1.X_add_number = 1;
6263       else
6264         expr1.X_add_number = 0;
6265       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6266                    (int) BFD_RELOC_LO16, AT);
6267       if (! target_big_endian)
6268         expr1.X_add_number = 0;
6269       else
6270         expr1.X_add_number = 1;
6271       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6272                    (int) BFD_RELOC_LO16, AT);
6273       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6274                    treg, 8);
6275       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6276                    treg, AT);
6277       break;
6278
6279     default:
6280       /* FIXME: Check if this is one of the itbl macros, since they
6281          are added dynamically. */
6282       as_bad ("Macro %s not implemented yet", ip->insn_mo->name);
6283       break;
6284     }
6285   if (mips_opts.noat)
6286     as_warn ("Macro used $at after \".set noat\"");
6287 }
6288
6289 /* Implement macros in mips16 mode.  */
6290
6291 static void
6292 mips16_macro (ip)
6293      struct mips_cl_insn *ip;
6294 {
6295   int mask;
6296   int xreg, yreg, zreg, tmp;
6297   int icnt;
6298   expressionS expr1;
6299   int dbl;
6300   const char *s, *s2, *s3;
6301
6302   mask = ip->insn_mo->mask;
6303
6304   xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
6305   yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
6306   zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
6307
6308   icnt = 0;
6309
6310   expr1.X_op = O_constant;
6311   expr1.X_op_symbol = NULL;
6312   expr1.X_add_symbol = NULL;
6313   expr1.X_add_number = 1;
6314
6315   dbl = 0;
6316
6317   switch (mask)
6318     {
6319     default:
6320       internalError ();
6321
6322     case M_DDIV_3:
6323       dbl = 1;
6324     case M_DIV_3:
6325       s = "mflo";
6326       goto do_div3;
6327     case M_DREM_3:
6328       dbl = 1;
6329     case M_REM_3:
6330       s = "mfhi";
6331     do_div3:
6332       mips_emit_delays (true);
6333       ++mips_opts.noreorder;
6334       mips_any_noreorder = 1;
6335       macro_build ((char *) NULL, &icnt, NULL,
6336                    dbl ? "ddiv" : "div",
6337                    "0,x,y", xreg, yreg);
6338       expr1.X_add_number = 2;
6339       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6340       macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6341       /* FIXME: The normal code checks for of -1 / -0x80000000 here,
6342          since that causes an overflow.  We should do that as well,
6343          but I don't see how to do the comparisons without a temporary
6344          register.  */
6345       --mips_opts.noreorder;
6346       macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
6347       break;
6348
6349     case M_DIVU_3:
6350       s = "divu";
6351       s2 = "mflo";
6352       goto do_divu3;
6353     case M_REMU_3:
6354       s = "divu";
6355       s2 = "mfhi";
6356       goto do_divu3;
6357     case M_DDIVU_3:
6358       s = "ddivu";
6359       s2 = "mflo";
6360       goto do_divu3;
6361     case M_DREMU_3:
6362       s = "ddivu";
6363       s2 = "mfhi";
6364     do_divu3:
6365       mips_emit_delays (true);
6366       ++mips_opts.noreorder;
6367       mips_any_noreorder = 1;
6368       macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
6369       expr1.X_add_number = 2;
6370       macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6371       macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6372       --mips_opts.noreorder;
6373       macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
6374       break;
6375
6376     case M_DMUL:
6377       dbl = 1;
6378     case M_MUL:
6379       macro_build ((char *) NULL, &icnt, NULL,
6380                    dbl ? "dmultu" : "multu",
6381                    "x,y", xreg, yreg);
6382       macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
6383       return;
6384
6385     case M_DSUBU_I:
6386       dbl = 1;
6387       goto do_subu;
6388     case M_SUBU_I:
6389     do_subu:
6390       if (imm_expr.X_op != O_constant)
6391         as_bad ("Unsupported large constant");
6392       imm_expr.X_add_number = -imm_expr.X_add_number;
6393       macro_build ((char *) NULL, &icnt, &imm_expr,
6394                    dbl ? "daddiu" : "addiu",
6395                    "y,x,4", yreg, xreg);
6396       break;
6397
6398     case M_SUBU_I_2:
6399       if (imm_expr.X_op != O_constant)
6400         as_bad ("Unsupported large constant");
6401       imm_expr.X_add_number = -imm_expr.X_add_number;
6402       macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
6403                    "x,k", xreg);
6404       break;
6405
6406     case M_DSUBU_I_2:
6407       if (imm_expr.X_op != O_constant)
6408         as_bad ("Unsupported large constant");
6409       imm_expr.X_add_number = -imm_expr.X_add_number;
6410       macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
6411                    "y,j", yreg);
6412       break;
6413
6414     case M_BEQ:
6415       s = "cmp";
6416       s2 = "bteqz";
6417       goto do_branch;
6418     case M_BNE:
6419       s = "cmp";
6420       s2 = "btnez";
6421       goto do_branch;
6422     case M_BLT:
6423       s = "slt";
6424       s2 = "btnez";
6425       goto do_branch;
6426     case M_BLTU:
6427       s = "sltu";
6428       s2 = "btnez";
6429       goto do_branch;
6430     case M_BLE:
6431       s = "slt";
6432       s2 = "bteqz";
6433       goto do_reverse_branch;
6434     case M_BLEU:
6435       s = "sltu";
6436       s2 = "bteqz";
6437       goto do_reverse_branch;
6438     case M_BGE:
6439       s = "slt";
6440       s2 = "bteqz";
6441       goto do_branch;
6442     case M_BGEU:
6443       s = "sltu";
6444       s2 = "bteqz";
6445       goto do_branch;
6446     case M_BGT:
6447       s = "slt";
6448       s2 = "btnez";
6449       goto do_reverse_branch;
6450     case M_BGTU:
6451       s = "sltu";
6452       s2 = "btnez";
6453
6454     do_reverse_branch:
6455       tmp = xreg;
6456       xreg = yreg;
6457       yreg = tmp;
6458
6459     do_branch:
6460       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
6461                    xreg, yreg);
6462       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6463       break;
6464
6465     case M_BEQ_I:
6466       s = "cmpi";
6467       s2 = "bteqz";
6468       s3 = "x,U";
6469       goto do_branch_i;
6470     case M_BNE_I:
6471       s = "cmpi";
6472       s2 = "btnez";
6473       s3 = "x,U";
6474       goto do_branch_i;
6475     case M_BLT_I:
6476       s = "slti";
6477       s2 = "btnez";
6478       s3 = "x,8";
6479       goto do_branch_i;
6480     case M_BLTU_I:
6481       s = "sltiu";
6482       s2 = "btnez";
6483       s3 = "x,8";
6484       goto do_branch_i;
6485     case M_BLE_I:
6486       s = "slti";
6487       s2 = "btnez";
6488       s3 = "x,8";
6489       goto do_addone_branch_i;
6490     case M_BLEU_I:
6491       s = "sltiu";
6492       s2 = "btnez";
6493       s3 = "x,8";
6494       goto do_addone_branch_i;
6495     case M_BGE_I:
6496       s = "slti";
6497       s2 = "bteqz";
6498       s3 = "x,8";
6499       goto do_branch_i;
6500     case M_BGEU_I:
6501       s = "sltiu";
6502       s2 = "bteqz";
6503       s3 = "x,8";
6504       goto do_branch_i;
6505     case M_BGT_I:
6506       s = "slti";
6507       s2 = "bteqz";
6508       s3 = "x,8";
6509       goto do_addone_branch_i;
6510     case M_BGTU_I:
6511       s = "sltiu";
6512       s2 = "bteqz";
6513       s3 = "x,8";
6514
6515     do_addone_branch_i:
6516       if (imm_expr.X_op != O_constant)
6517         as_bad ("Unsupported large constant");
6518       ++imm_expr.X_add_number;
6519
6520     do_branch_i:
6521       macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
6522       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6523       break;
6524
6525     case M_ABS:
6526       expr1.X_add_number = 0;
6527       macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8",  yreg);
6528       if (xreg != yreg)
6529         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6530                      "move", "y,X", xreg, yreg);
6531       expr1.X_add_number = 2;
6532       macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
6533       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6534                    "neg", "x,w", xreg, xreg);
6535     }
6536 }
6537
6538 /* This routine assembles an instruction into its binary format.  As a
6539    side effect, it sets one of the global variables imm_reloc or
6540    offset_reloc to the type of relocation to do if one of the operands
6541    is an address expression.  */
6542
6543 static void
6544 mips_ip (str, ip)
6545      char *str;
6546      struct mips_cl_insn *ip;
6547 {
6548   char *s;
6549   const char *args;
6550   char c;
6551   struct mips_opcode *insn;
6552   char *argsStart;
6553   unsigned int regno;
6554   unsigned int lastregno = 0;
6555   char *s_reset;
6556
6557   insn_error = NULL;
6558
6559   for (s = str; *s != '\0' && !isspace(*s); ++s)
6560     continue;
6561   if (isspace (*s))
6562     *s++ = '\0';
6563
6564   if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
6565     {
6566       insn_error = "unrecognized opcode";
6567       return;
6568     }
6569   argsStart = s;
6570   for (;;)
6571     {
6572       int insn_isa;
6573       boolean ok;
6574
6575       assert (strcmp (insn->name, str) == 0);
6576
6577       if (insn->pinfo == INSN_MACRO)
6578         insn_isa = insn->match;
6579       else if ((insn->membership & INSN_ISA) == INSN_ISA1)
6580         insn_isa = 1;
6581       else if ((insn->membership & INSN_ISA) == INSN_ISA2)
6582         insn_isa = 2;
6583       else if ((insn->membership & INSN_ISA) == INSN_ISA3)
6584         insn_isa = 3;
6585       else if ((insn->membership & INSN_ISA) == INSN_ISA4)
6586         insn_isa = 4;
6587       else
6588         insn_isa = 15;
6589
6590       if (insn_isa <= mips_opts.isa)
6591         ok = true;
6592       else if (insn->pinfo == INSN_MACRO)
6593         ok = false;
6594       else if ((mips_4650 && (insn->membership & INSN_4650) != 0)
6595                || (mips_4010 && (insn->membership & INSN_4010) != 0)
6596                || (mips_4100 && (insn->membership & INSN_4100) != 0)
6597                /* start-sanitize-r5900 */
6598                || (mips_5900 && (insn->membership & INSN_5900) != 0)
6599                /* end-sanitize-r5900 */
6600                || (mips_3900 && (insn->membership & INSN_3900) != 0))
6601         {
6602           ok = true;
6603           if (mips_4650 && (insn->pinfo & FP_D) != 0)
6604             ok = false;
6605           /* start-sanitize-r5900 */
6606           if (mips_5900 && (insn->pinfo & FP_D) != 0)
6607             ok = false;
6608           /* end-sanitize-r5900 */
6609         }
6610       else
6611         ok = false;
6612
6613       if (! ok)
6614         {
6615           if (insn + 1 < &mips_opcodes[NUMOPCODES]
6616               && strcmp (insn->name, insn[1].name) == 0)
6617             {
6618               ++insn;
6619               continue;
6620             }
6621           if (insn_isa <= mips_opts.isa)
6622             insn_error = "opcode not supported on this processor";
6623           else
6624             {
6625               static char buf[100];
6626
6627               sprintf (buf, "opcode requires -mips%d or greater", insn_isa);
6628               insn_error = buf;
6629             }
6630           return;
6631         }
6632
6633       ip->insn_mo = insn;
6634       ip->insn_opcode = insn->match;
6635       for (args = insn->args;; ++args)
6636         {
6637           if (*s == ' ')
6638             ++s;
6639           switch (*args)
6640             {
6641             case '\0':          /* end of args */
6642               if (*s == '\0')
6643                 return;
6644               break;
6645
6646             case ',':
6647               if (*s++ == *args)
6648                 continue;
6649               s--;
6650               switch (*++args)
6651                 {
6652                 case 'r':
6653                 case 'v':
6654                   ip->insn_opcode |= lastregno << 21;
6655                   continue;
6656
6657                 case 'w':
6658                 case 'W':
6659                   ip->insn_opcode |= lastregno << 16;
6660                   continue;
6661
6662                 case 'V':
6663                   ip->insn_opcode |= lastregno << 11;
6664                   continue;
6665                 }
6666               break;
6667
6668             case '(':
6669               /* handle optional base register.
6670                  Either the base register is omitted or
6671                  we must have a left paren. */
6672               /* this is dependent on the next operand specifier
6673                  is a 'b' for base register */
6674               assert (args[1] == 'b');
6675               if (*s == '\0')
6676                 return;
6677
6678             case ')':           /* these must match exactly */
6679               if (*s++ == *args)
6680                 continue;
6681               break;
6682
6683             case '<':           /* must be at least one digit */
6684               /*
6685                * According to the manual, if the shift amount is greater
6686                * than 31 or less than 0 the the shift amount should be
6687                * mod 32. In reality the mips assembler issues an error.
6688                * We issue a warning and mask out all but the low 5 bits.
6689                */
6690               my_getExpression (&imm_expr, s);
6691               check_absolute_expr (ip, &imm_expr);
6692               if ((unsigned long) imm_expr.X_add_number > 31)
6693                 {
6694                   as_warn ("Improper shift amount (%ld)",
6695                            (long) imm_expr.X_add_number);
6696                   imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
6697                 }
6698               ip->insn_opcode |= imm_expr.X_add_number << 6;
6699               imm_expr.X_op = O_absent;
6700               s = expr_end;
6701               continue;
6702
6703             case '>':           /* shift amount minus 32 */
6704               my_getExpression (&imm_expr, s);
6705               check_absolute_expr (ip, &imm_expr);
6706               if ((unsigned long) imm_expr.X_add_number < 32
6707                   || (unsigned long) imm_expr.X_add_number > 63)
6708                 break;
6709               ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
6710               imm_expr.X_op = O_absent;
6711               s = expr_end;
6712               continue;
6713
6714             case 'k':           /* cache code */
6715             case 'h':           /* prefx code */
6716               my_getExpression (&imm_expr, s);
6717               check_absolute_expr (ip, &imm_expr);
6718               if ((unsigned long) imm_expr.X_add_number > 31)
6719                 {
6720                   as_warn ("Invalid value for `%s' (%lu)",
6721                            ip->insn_mo->name,
6722                            (unsigned long) imm_expr.X_add_number);
6723                   imm_expr.X_add_number &= 0x1f;
6724                 }
6725               if (*args == 'k')
6726                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
6727               else
6728                 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
6729               imm_expr.X_op = O_absent;
6730               s = expr_end;
6731               continue;
6732
6733             case 'c':           /* break code */
6734               my_getExpression (&imm_expr, s);
6735               check_absolute_expr (ip, &imm_expr);
6736               if ((unsigned) imm_expr.X_add_number > 1023)
6737                 as_warn ("Illegal break code (%ld)",
6738                          (long) imm_expr.X_add_number);
6739               ip->insn_opcode |= imm_expr.X_add_number << 16;
6740               imm_expr.X_op = O_absent;
6741               s = expr_end;
6742               continue;
6743
6744             case 'B':           /* syscall code */
6745               my_getExpression (&imm_expr, s);
6746               check_absolute_expr (ip, &imm_expr);
6747               if ((unsigned) imm_expr.X_add_number > 0xfffff)
6748                 as_warn ("Illegal syscall code (%ld)",
6749                          (long) imm_expr.X_add_number);
6750               ip->insn_opcode |= imm_expr.X_add_number << 6;
6751               imm_expr.X_op = O_absent;
6752               s = expr_end;
6753               continue;
6754
6755             case 'C':           /* Coprocessor code */
6756               my_getExpression (&imm_expr, s);
6757               check_absolute_expr (ip, &imm_expr);
6758               if ((unsigned long) imm_expr.X_add_number >= (1<<25))
6759                 {
6760                   as_warn ("Coproccesor code > 25 bits (%ld)",
6761                            (long) imm_expr.X_add_number);
6762                   imm_expr.X_add_number &= ((1<<25) - 1);
6763                 }
6764               ip->insn_opcode |= imm_expr.X_add_number;
6765               imm_expr.X_op = O_absent;
6766               s = expr_end;
6767               continue;
6768
6769             case 'b':           /* base register */
6770             case 'd':           /* destination register */
6771             case 's':           /* source register */
6772             case 't':           /* target register */
6773             case 'r':           /* both target and source */
6774             case 'v':           /* both dest and source */
6775             case 'w':           /* both dest and target */
6776             case 'E':           /* coprocessor target register */
6777             case 'G':           /* coprocessor destination register */
6778             case 'x':           /* ignore register name */
6779             case 'z':           /* must be zero register */
6780               s_reset = s;
6781               if (s[0] == '$')
6782                 {
6783                   if (isdigit (s[1]))
6784                     {
6785                       ++s;
6786                       regno = 0;
6787                       do
6788                         {
6789                           regno *= 10;
6790                           regno += *s - '0';
6791                           ++s;
6792                         }
6793                       while (isdigit (*s));
6794                       if (regno > 31)
6795                         as_bad ("Invalid register number (%d)", regno);
6796                     }
6797                   else if (*args == 'E' || *args == 'G')
6798                     goto notreg;
6799                   else
6800                     {
6801                       if (s[1] == 'f' && s[2] == 'p')
6802                         {
6803                           s += 3;
6804                           regno = FP;
6805                         }
6806                       else if (s[1] == 's' && s[2] == 'p')
6807                         {
6808                           s += 3;
6809                           regno = SP;
6810                         }
6811                       else if (s[1] == 'g' && s[2] == 'p')
6812                         {
6813                           s += 3;
6814                           regno = GP;
6815                         }
6816                       else if (s[1] == 'a' && s[2] == 't')
6817                         {
6818                           s += 3;
6819                           regno = AT;
6820                         }
6821                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
6822                         {
6823                           s += 4;
6824                           regno = KT0;
6825                         }
6826                       else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
6827                         {
6828                           s += 4;
6829                           regno = KT1;
6830                         }
6831                       else if (itbl_have_entries)
6832                         {
6833                           char *p, *n;
6834                           int r;
6835
6836                           p = s+1;      /* advance past '$' */
6837                           n = itbl_get_field (&p);  /* n is name */
6838
6839                           /* See if this is a register defined in an 
6840                              itbl entry */
6841                           r = itbl_get_reg_val (n);
6842                           if (r)
6843                             {
6844                               /* Get_field advances to the start of
6845                                  the next field, so we need to back
6846                                  rack to the end of the last field. */
6847                               if (p) 
6848                                 s = p - 1;
6849                               else 
6850                                 s = strchr (s,'\0');
6851                               regno = r;
6852                             }
6853                           else
6854                             goto notreg;
6855                           }
6856                       else
6857                         goto notreg;
6858                     }
6859                   if (regno == AT
6860                       && ! mips_opts.noat
6861                       && *args != 'E'
6862                       && *args != 'G')
6863                     as_warn ("Used $at without \".set noat\"");
6864                   c = *args;
6865                   if (*s == ' ')
6866                     s++;
6867                   if (args[1] != *s)
6868                     {
6869                       if (c == 'r' || c == 'v' || c == 'w')
6870                         {
6871                           regno = lastregno;
6872                           s = s_reset;
6873                           args++;
6874                         }
6875                     }
6876                   /* 'z' only matches $0.  */
6877                   if (c == 'z' && regno != 0)
6878                     break;
6879
6880         /* Now that we have assembled one operand, we use the args string 
6881          * to figure out where it goes in the instruction. */
6882                   switch (c)
6883                     {
6884                     case 'r':
6885                     case 's':
6886                     case 'v':
6887                     case 'b':
6888                       ip->insn_opcode |= regno << 21;
6889                       break;
6890                     case 'd':
6891                     case 'G':
6892                       ip->insn_opcode |= regno << 11;
6893                       break;
6894                     case 'w':
6895                     case 't':
6896                     case 'E':
6897                       ip->insn_opcode |= regno << 16;
6898                       break;
6899                     case 'x':
6900                       /* This case exists because on the r3000 trunc
6901                          expands into a macro which requires a gp
6902                          register.  On the r6000 or r4000 it is
6903                          assembled into a single instruction which
6904                          ignores the register.  Thus the insn version
6905                          is MIPS_ISA2 and uses 'x', and the macro
6906                          version is MIPS_ISA1 and uses 't'.  */
6907                       break;
6908                     case 'z':
6909                       /* This case is for the div instruction, which
6910                          acts differently if the destination argument
6911                          is $0.  This only matches $0, and is checked
6912                          outside the switch.  */
6913                       break;
6914                     case 'D':
6915                       /* Itbl operand; not yet implemented. FIXME ?? */
6916                       break;
6917                       /* What about all other operands like 'i', which
6918                          can be specified in the opcode table? */
6919                     }
6920                   lastregno = regno;
6921                   continue;
6922                 }
6923             notreg:
6924               switch (*args++)
6925                 {
6926                 case 'r':
6927                 case 'v':
6928                   ip->insn_opcode |= lastregno << 21;
6929                   continue;
6930                 case 'w':
6931                   ip->insn_opcode |= lastregno << 16;
6932                   continue;
6933                 }
6934               break;
6935
6936             case 'D':           /* floating point destination register */
6937             case 'S':           /* floating point source register */
6938             case 'T':           /* floating point target register */
6939             case 'R':           /* floating point source register */
6940             case 'V':
6941             case 'W':
6942               s_reset = s;
6943               if (s[0] == '$' && s[1] == 'f' && isdigit (s[2]))
6944                 {
6945                   s += 2;
6946                   regno = 0;
6947                   do
6948                     {
6949                       regno *= 10;
6950                       regno += *s - '0';
6951                       ++s;
6952                     }
6953                   while (isdigit (*s));
6954
6955                   if (regno > 31)
6956                     as_bad ("Invalid float register number (%d)", regno);
6957
6958                   if ((regno & 1) != 0
6959                       && mips_opts.isa < 3
6960                       && ! (strcmp (str, "mtc1") == 0
6961                             || strcmp (str, "mfc1") == 0
6962                             || strcmp (str, "lwc1") == 0
6963                             || strcmp (str, "swc1") == 0
6964                             || strcmp (str, "l.s") == 0
6965                             || strcmp (str, "s.s") == 0))
6966                     as_warn ("Float register should be even, was %d",
6967                              regno);
6968
6969                   c = *args;
6970                   if (*s == ' ')
6971                     s++;
6972                   if (args[1] != *s)
6973                     {
6974                       if (c == 'V' || c == 'W')
6975                         {
6976                           regno = lastregno;
6977                           s = s_reset;
6978                           args++;
6979                         }
6980                     }
6981                   switch (c)
6982                     {
6983                     case 'D':
6984                       ip->insn_opcode |= regno << 6;
6985                       break;
6986                     case 'V':
6987                     case 'S':
6988                       ip->insn_opcode |= regno << 11;
6989                       break;
6990                     case 'W':
6991                     case 'T':
6992                       ip->insn_opcode |= regno << 16;
6993                       break;
6994                     case 'R':
6995                       ip->insn_opcode |= regno << 21;
6996                       break;
6997                     }
6998                   lastregno = regno;
6999                   continue;
7000                 }
7001               switch (*args++)
7002                 {
7003                 case 'V':
7004                   ip->insn_opcode |= lastregno << 11;
7005                   continue;
7006                 case 'W':
7007                   ip->insn_opcode |= lastregno << 16;
7008                   continue;
7009                 }
7010               break;
7011
7012             case 'I':
7013               my_getExpression (&imm_expr, s);
7014               if (imm_expr.X_op != O_big
7015                   && imm_expr.X_op != O_constant)
7016                 insn_error = "absolute expression required";
7017               s = expr_end;
7018               continue;
7019
7020             case 'A':
7021               my_getExpression (&offset_expr, s);
7022               imm_reloc = BFD_RELOC_32;
7023               s = expr_end;
7024               continue;
7025
7026             case 'F':
7027             case 'L':
7028             case 'f':
7029             case 'l':
7030               {
7031                 int f64;
7032                 char *save_in;
7033                 char *err;
7034                 unsigned char temp[8];
7035                 int len;
7036                 unsigned int length;
7037                 segT seg;
7038                 subsegT subseg;
7039                 char *p;
7040
7041                 /* These only appear as the last operand in an
7042                    instruction, and every instruction that accepts
7043                    them in any variant accepts them in all variants.
7044                    This means we don't have to worry about backing out
7045                    any changes if the instruction does not match.
7046
7047                    The difference between them is the size of the
7048                    floating point constant and where it goes.  For 'F'
7049                    and 'L' the constant is 64 bits; for 'f' and 'l' it
7050                    is 32 bits.  Where the constant is placed is based
7051                    on how the MIPS assembler does things:
7052                     F -- .rdata
7053                     L -- .lit8
7054                     f -- immediate value
7055                     l -- .lit4
7056
7057                     The .lit4 and .lit8 sections are only used if
7058                     permitted by the -G argument.
7059
7060                     When generating embedded PIC code, we use the
7061                     .lit8 section but not the .lit4 section (we can do
7062                     .lit4 inline easily; we need to put .lit8
7063                     somewhere in the data segment, and using .lit8
7064                     permits the linker to eventually combine identical
7065                     .lit8 entries).  */
7066
7067                 f64 = *args == 'F' || *args == 'L';
7068
7069                 save_in = input_line_pointer;
7070                 input_line_pointer = s;
7071                 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
7072                 length = len;
7073                 s = input_line_pointer;
7074                 input_line_pointer = save_in;
7075                 if (err != NULL && *err != '\0')
7076                   {
7077                     as_bad ("Bad floating point constant: %s", err);
7078                     memset (temp, '\0', sizeof temp);
7079                     length = f64 ? 8 : 4;
7080                   }
7081
7082                 assert (length == (f64 ? 8 : 4));
7083
7084                 if (*args == 'f'
7085                     || (*args == 'l'
7086                         && (! USE_GLOBAL_POINTER_OPT
7087                             || mips_pic == EMBEDDED_PIC
7088                             || g_switch_value < 4
7089                             || (temp[0] == 0 && temp[1] == 0)
7090                             || (temp[2] == 0 && temp[3] == 0))))
7091                   {
7092                     imm_expr.X_op = O_constant;
7093                     if (! target_big_endian)
7094                       imm_expr.X_add_number = bfd_getl32 (temp);
7095                     else
7096                       imm_expr.X_add_number = bfd_getb32 (temp);
7097                   }
7098                 else if (length > 4
7099                          && ((temp[0] == 0 && temp[1] == 0)
7100                              || (temp[2] == 0 && temp[3] == 0))
7101                          && ((temp[4] == 0 && temp[5] == 0)
7102                              || (temp[6] == 0 && temp[7] == 0)))
7103                   {
7104                     /* The value is simple enough to load with a
7105                        couple of instructions.  In mips1 mode, set
7106                        imm_expr to the high order 32 bits and
7107                        offset_expr to the low order 32 bits.
7108                        Otherwise, set imm_expr to the entire 64 bit
7109                        constant.  */
7110                     if (mips_opts.isa < 3)
7111                       {
7112                         imm_expr.X_op = O_constant;
7113                         offset_expr.X_op = O_constant;
7114                         if (! target_big_endian)
7115                           {
7116                             imm_expr.X_add_number = bfd_getl32 (temp + 4);
7117                             offset_expr.X_add_number = bfd_getl32 (temp);
7118                           }
7119                         else
7120                           {
7121                             imm_expr.X_add_number = bfd_getb32 (temp);
7122                             offset_expr.X_add_number = bfd_getb32 (temp + 4);
7123                           }
7124                         if (offset_expr.X_add_number == 0)
7125                           offset_expr.X_op = O_absent;
7126                       }
7127                     else if (sizeof (imm_expr.X_add_number) > 4)
7128                       {
7129                         imm_expr.X_op = O_constant;
7130                         if (! target_big_endian)
7131                           imm_expr.X_add_number = bfd_getl64 (temp);
7132                         else
7133                           imm_expr.X_add_number = bfd_getb64 (temp);
7134                       }
7135                     else
7136                       {
7137                         imm_expr.X_op = O_big;
7138                         imm_expr.X_add_number = 4;
7139                         if (! target_big_endian)
7140                           {
7141                             generic_bignum[0] = bfd_getl16 (temp);
7142                             generic_bignum[1] = bfd_getl16 (temp + 2);
7143                             generic_bignum[2] = bfd_getl16 (temp + 4);
7144                             generic_bignum[3] = bfd_getl16 (temp + 6);
7145                           }
7146                         else
7147                           {
7148                             generic_bignum[0] = bfd_getb16 (temp + 6);
7149                             generic_bignum[1] = bfd_getb16 (temp + 4);
7150                             generic_bignum[2] = bfd_getb16 (temp + 2);
7151                             generic_bignum[3] = bfd_getb16 (temp);
7152                           }
7153                       }
7154                   }
7155                 else
7156                   {
7157                     const char *newname;
7158                     segT new_seg;
7159
7160                     /* Switch to the right section.  */
7161                     seg = now_seg;
7162                     subseg = now_subseg;
7163                     switch (*args)
7164                       {
7165                       default: /* unused default case avoids warnings.  */
7166                       case 'L':
7167                         newname = RDATA_SECTION_NAME;
7168                         if (USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
7169                           newname = ".lit8";
7170                         break;
7171                       case 'F':
7172                         newname = RDATA_SECTION_NAME;
7173                         break;
7174                       case 'l':
7175                         assert (!USE_GLOBAL_POINTER_OPT
7176                                 || g_switch_value >= 4);
7177                         newname = ".lit4";
7178                         break;
7179                       }
7180                     new_seg = subseg_new (newname, (subsegT) 0);
7181                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
7182                       bfd_set_section_flags (stdoutput, new_seg,
7183                                              (SEC_ALLOC
7184                                               | SEC_LOAD
7185                                               | SEC_READONLY
7186                                               | SEC_DATA));
7187                     frag_align (*args == 'l' ? 2 : 3, 0, 0);
7188                     if (OUTPUT_FLAVOR == bfd_target_elf_flavour
7189                         && strcmp (TARGET_OS, "elf") != 0)
7190                       record_alignment (new_seg, 4);
7191                     else
7192                       record_alignment (new_seg, *args == 'l' ? 2 : 3);
7193                     if (seg == now_seg)
7194                       as_bad ("Can't use floating point insn in this section");
7195
7196                     /* Set the argument to the current address in the
7197                        section.  */
7198                     offset_expr.X_op = O_symbol;
7199                     offset_expr.X_add_symbol =
7200                       symbol_new ("L0\001", now_seg,
7201                                   (valueT) frag_now_fix (), frag_now);
7202                     offset_expr.X_add_number = 0;
7203
7204                     /* Put the floating point number into the section.  */
7205                     p = frag_more ((int) length);
7206                     memcpy (p, temp, length);
7207
7208                     /* Switch back to the original section.  */
7209                     subseg_set (seg, subseg);
7210                   }
7211               }
7212               continue;
7213
7214             case 'i':           /* 16 bit unsigned immediate */
7215             case 'j':           /* 16 bit signed immediate */
7216               imm_reloc = BFD_RELOC_LO16;
7217               c = my_getSmallExpression (&imm_expr, s);
7218               if (c != '\0')
7219                 {
7220                   if (c != 'l')
7221                     {
7222                       if (imm_expr.X_op == O_constant)
7223                         imm_expr.X_add_number =
7224                           (imm_expr.X_add_number >> 16) & 0xffff;
7225                       else if (c == 'h')
7226                         {
7227                           imm_reloc = BFD_RELOC_HI16_S;
7228                           imm_unmatched_hi = true;
7229                         }
7230                       else
7231                         imm_reloc = BFD_RELOC_HI16;
7232                     }
7233                 }
7234               if (*args == 'i')
7235                 {
7236                   if ((c == '\0' && imm_expr.X_op != O_constant)
7237                       || ((imm_expr.X_add_number < 0
7238                            || imm_expr.X_add_number >= 0x10000)
7239                           && imm_expr.X_op == O_constant))
7240                     {
7241                       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7242                           !strcmp (insn->name, insn[1].name))
7243                         break;
7244                       if (imm_expr.X_op != O_constant
7245                           && imm_expr.X_op != O_big)
7246                         insn_error = "absolute expression required";
7247                       else
7248                         as_bad ("16 bit expression not in range 0..65535");
7249                     }
7250                 }
7251               else
7252                 {
7253                   int more;
7254                   offsetT max;
7255
7256                   /* The upper bound should be 0x8000, but
7257                      unfortunately the MIPS assembler accepts numbers
7258                      from 0x8000 to 0xffff and sign extends them, and
7259                      we want to be compatible.  We only permit this
7260                      extended range for an instruction which does not
7261                      provide any further alternates, since those
7262                      alternates may handle other cases.  People should
7263                      use the numbers they mean, rather than relying on
7264                      a mysterious sign extension.  */
7265                   more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7266                           strcmp (insn->name, insn[1].name) == 0);
7267                   if (more)
7268                     max = 0x8000;
7269                   else
7270                     max = 0x10000;
7271                   if ((c == '\0' && imm_expr.X_op != O_constant)
7272                       || ((imm_expr.X_add_number < -0x8000
7273                            || imm_expr.X_add_number >= max)
7274                           && imm_expr.X_op == O_constant)
7275                       || (more
7276                           && imm_expr.X_add_number < 0
7277                           && mips_opts.isa >= 3
7278                           && imm_expr.X_unsigned
7279                           && sizeof (imm_expr.X_add_number) <= 4))
7280                     {
7281                       if (more)
7282                         break;
7283                       if (imm_expr.X_op != O_constant
7284                           && imm_expr.X_op != O_big)
7285                         insn_error = "absolute expression required";
7286                       else
7287                         as_bad ("16 bit expression not in range -32768..32767");
7288                     }
7289                 }
7290               s = expr_end;
7291               continue;
7292
7293             case 'o':           /* 16 bit offset */
7294               c = my_getSmallExpression (&offset_expr, s);
7295
7296               /* If this value won't fit into a 16 bit offset, then go
7297                  find a macro that will generate the 32 bit offset
7298                  code pattern.  As a special hack, we accept the
7299                  difference of two local symbols as a constant.  This
7300                  is required to suppose embedded PIC switches, which
7301                  use an instruction which looks like
7302                      lw $4,$L12-$LS12($4)
7303                  The problem with handling this in a more general
7304                  fashion is that the macro function doesn't expect to
7305                  see anything which can be handled in a single
7306                  constant instruction.  */
7307               if (c == 0
7308                   && (offset_expr.X_op != O_constant
7309                       || offset_expr.X_add_number >= 0x8000
7310                       || offset_expr.X_add_number < -0x8000)
7311                   && (mips_pic != EMBEDDED_PIC
7312                       || offset_expr.X_op != O_subtract
7313                       || now_seg != text_section
7314                       || (S_GET_SEGMENT (offset_expr.X_op_symbol)
7315                           != text_section)))
7316                 break;
7317
7318               offset_reloc = BFD_RELOC_LO16;
7319               if (c == 'h' || c == 'H')
7320                 {
7321                   assert (offset_expr.X_op == O_constant);
7322                   offset_expr.X_add_number =
7323                     (offset_expr.X_add_number >> 16) & 0xffff;
7324                 }
7325               s = expr_end;
7326               continue;
7327
7328             case 'p':           /* pc relative offset */
7329               offset_reloc = BFD_RELOC_16_PCREL_S2;
7330               my_getExpression (&offset_expr, s);
7331               s = expr_end;
7332               continue;
7333
7334             case 'u':           /* upper 16 bits */
7335               c = my_getSmallExpression (&imm_expr, s);
7336               if (imm_expr.X_op == O_constant
7337                   && (imm_expr.X_add_number < 0
7338                       || imm_expr.X_add_number >= 0x10000))
7339                 as_bad ("lui expression not in range 0..65535");
7340               imm_reloc = BFD_RELOC_LO16;
7341               if (c)
7342                 {
7343                   if (c != 'l')
7344                     {
7345                       if (imm_expr.X_op == O_constant)
7346                         imm_expr.X_add_number =
7347                           (imm_expr.X_add_number >> 16) & 0xffff;
7348                       else if (c == 'h')
7349                         {
7350                           imm_reloc = BFD_RELOC_HI16_S;
7351                           imm_unmatched_hi = true;
7352                         }
7353                       else
7354                         imm_reloc = BFD_RELOC_HI16;
7355                     }
7356                 }
7357               s = expr_end;
7358               continue;
7359
7360             case 'a':           /* 26 bit address */
7361               my_getExpression (&offset_expr, s);
7362               s = expr_end;
7363               offset_reloc = BFD_RELOC_MIPS_JMP;
7364               continue;
7365
7366             case 'N':           /* 3 bit branch condition code */
7367             case 'M':           /* 3 bit compare condition code */
7368               if (strncmp (s, "$fcc", 4) != 0)
7369                 break;
7370               s += 4;
7371               regno = 0;
7372               do
7373                 {
7374                   regno *= 10;
7375                   regno += *s - '0';
7376                   ++s;
7377                 }
7378               while (isdigit (*s));
7379               if (regno > 7)
7380                 as_bad ("invalid condition code register $fcc%d", regno);
7381               if (*args == 'N')
7382                 ip->insn_opcode |= regno << OP_SH_BCC;
7383               else
7384                 ip->insn_opcode |= regno << OP_SH_CCC;
7385               continue;
7386
7387             default:
7388               fprintf (stderr, "bad char = '%c'\n", *args);
7389               internalError ();
7390             }
7391           break;
7392         }
7393       /* Args don't match.  */
7394       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7395           !strcmp (insn->name, insn[1].name))
7396         {
7397           ++insn;
7398           s = argsStart;
7399           continue;
7400         }
7401       insn_error = "illegal operands";
7402       return;
7403     }
7404 }
7405
7406 /* This routine assembles an instruction into its binary format when
7407    assembling for the mips16.  As a side effect, it sets one of the
7408    global variables imm_reloc or offset_reloc to the type of
7409    relocation to do if one of the operands is an address expression.
7410    It also sets mips16_small and mips16_ext if the user explicitly
7411    requested a small or extended instruction.  */
7412
7413 static void
7414 mips16_ip (str, ip)
7415      char *str;
7416      struct mips_cl_insn *ip;
7417 {
7418   char *s;
7419   const char *args;
7420   struct mips_opcode *insn;
7421   char *argsstart;
7422   unsigned int regno;
7423   unsigned int lastregno = 0;
7424   char *s_reset;
7425
7426   insn_error = NULL;
7427
7428   mips16_small = false;
7429   mips16_ext = false;
7430
7431   for (s = str; islower (*s); ++s)
7432     ;
7433   switch (*s)
7434     {
7435     case '\0':
7436       break;
7437
7438     case ' ':
7439       *s++ = '\0';
7440       break;
7441
7442     case '.':
7443       if (s[1] == 't' && s[2] == ' ')
7444         {
7445           *s = '\0';
7446           mips16_small = true;
7447           s += 3;
7448           break;
7449         }
7450       else if (s[1] == 'e' && s[2] == ' ')
7451         {
7452           *s = '\0';
7453           mips16_ext = true;
7454           s += 3;
7455           break;
7456         }
7457       /* Fall through.  */
7458     default:
7459       insn_error = "unknown opcode";
7460       return;
7461     }
7462
7463   if (mips_opts.noautoextend && ! mips16_ext)
7464     mips16_small = true;
7465
7466   if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
7467     {
7468       insn_error = "unrecognized opcode";
7469       return;
7470     }
7471
7472   argsstart = s;
7473   for (;;)
7474     {
7475       assert (strcmp (insn->name, str) == 0);
7476
7477       ip->insn_mo = insn;
7478       ip->insn_opcode = insn->match;
7479       ip->use_extend = false;
7480       imm_expr.X_op = O_absent;
7481       imm_reloc = BFD_RELOC_UNUSED;
7482       offset_expr.X_op = O_absent;
7483       offset_reloc = BFD_RELOC_UNUSED;
7484       for (args = insn->args; 1; ++args)
7485         {
7486           int c;
7487
7488           if (*s == ' ')
7489             ++s;
7490
7491           /* In this switch statement we call break if we did not find
7492              a match, continue if we did find a match, or return if we
7493              are done.  */
7494
7495           c = *args;
7496           switch (c)
7497             {
7498             case '\0':
7499               if (*s == '\0')
7500                 {
7501                   /* Stuff the immediate value in now, if we can.  */
7502                   if (imm_expr.X_op == O_constant
7503                       && imm_reloc > BFD_RELOC_UNUSED
7504                       && insn->pinfo != INSN_MACRO)
7505                     {
7506                       mips16_immed ((char *) NULL, 0,
7507                                     imm_reloc - BFD_RELOC_UNUSED,
7508                                     imm_expr.X_add_number, true, mips16_small,
7509                                     mips16_ext, &ip->insn_opcode,
7510                                     &ip->use_extend, &ip->extend);
7511                       imm_expr.X_op = O_absent;
7512                       imm_reloc = BFD_RELOC_UNUSED;
7513                     }
7514
7515                   return;
7516                 }
7517               break;
7518
7519             case ',':
7520               if (*s++ == c)
7521                 continue;
7522               s--;
7523               switch (*++args)
7524                 {
7525                 case 'v':
7526                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
7527                   continue;
7528                 case 'w':
7529                   ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
7530                   continue;
7531                 }
7532               break;
7533
7534             case '(':
7535             case ')':
7536               if (*s++ == c)
7537                 continue;
7538               break;
7539
7540             case 'v':
7541             case 'w':
7542               if (s[0] != '$')
7543                 {
7544                   if (c == 'v')
7545                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
7546                   else
7547                     ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
7548                   ++args;
7549                   continue;
7550                 }
7551               /* Fall through.  */
7552             case 'x':
7553             case 'y':
7554             case 'z':
7555             case 'Z':
7556             case '0':
7557             case 'S':
7558             case 'R':
7559             case 'X':
7560             case 'Y':
7561               if (s[0] != '$')
7562                 break;
7563               s_reset = s;
7564               if (isdigit (s[1]))
7565                 {
7566                   ++s;
7567                   regno = 0;
7568                   do
7569                     {
7570                       regno *= 10;
7571                       regno += *s - '0';
7572                       ++s;
7573                     }
7574                   while (isdigit (*s));
7575                   if (regno > 31)
7576                     {
7577                       as_bad ("invalid register number (%d)", regno);
7578                       regno = 2;
7579                     }
7580                 }
7581               else
7582                 {
7583                   if (s[1] == 'f' && s[2] == 'p')
7584                     {
7585                       s += 3;
7586                       regno = FP;
7587                     }
7588                   else if (s[1] == 's' && s[2] == 'p')
7589                     {
7590                       s += 3;
7591                       regno = SP;
7592                     }
7593                   else if (s[1] == 'g' && s[2] == 'p')
7594                     {
7595                       s += 3;
7596                       regno = GP;
7597                     }
7598                   else if (s[1] == 'a' && s[2] == 't')
7599                     {
7600                       s += 3;
7601                       regno = AT;
7602                     }
7603                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7604                     {
7605                       s += 4;
7606                       regno = KT0;
7607                     }
7608                   else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7609                     {
7610                       s += 4;
7611                       regno = KT1;
7612                     }
7613                   else
7614                     break;
7615                 }
7616
7617               if (*s == ' ')
7618                 ++s;
7619               if (args[1] != *s)
7620                 {
7621                   if (c == 'v' || c == 'w')
7622                     {
7623                       regno = mips16_to_32_reg_map[lastregno];
7624                       s = s_reset;
7625                       args++;
7626                     }
7627                 }
7628
7629               switch (c)
7630                 {
7631                 case 'x':
7632                 case 'y':
7633                 case 'z':
7634                 case 'v':
7635                 case 'w':
7636                 case 'Z':
7637                   regno = mips32_to_16_reg_map[regno];
7638                   break;
7639
7640                 case '0':
7641                   if (regno != 0)
7642                     regno = ILLEGAL_REG;
7643                   break;
7644
7645                 case 'S':
7646                   if (regno != SP)
7647                     regno = ILLEGAL_REG;
7648                   break;
7649
7650                 case 'R':
7651                   if (regno != RA)
7652                     regno = ILLEGAL_REG;
7653                   break;
7654
7655                 case 'X':
7656                 case 'Y':
7657                   if (regno == AT && ! mips_opts.noat)
7658                     as_warn ("used $at without \".set noat\"");
7659                   break;
7660
7661                 default:
7662                   internalError ();
7663                 }
7664
7665               if (regno == ILLEGAL_REG)
7666                 break;
7667
7668               switch (c)
7669                 {
7670                 case 'x':
7671                 case 'v':
7672                   ip->insn_opcode |= regno << MIPS16OP_SH_RX;
7673                   break;
7674                 case 'y':
7675                 case 'w':
7676                   ip->insn_opcode |= regno << MIPS16OP_SH_RY;
7677                   break;
7678                 case 'z':
7679                   ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
7680                   break;
7681                 case 'Z':
7682                   ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
7683                 case '0':
7684                 case 'S':
7685                 case 'R':
7686                   break;
7687                 case 'X':
7688                   ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
7689                   break;
7690                 case 'Y':
7691                   regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
7692                   ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
7693                   break;
7694                 default:
7695                   internalError ();
7696                 }
7697
7698               lastregno = regno;
7699               continue;
7700
7701             case 'P':
7702               if (strncmp (s, "$pc", 3) == 0)
7703                 {
7704                   s += 3;
7705                   continue;
7706                 }
7707               break;
7708
7709             case '<':
7710             case '>':
7711             case '[':
7712             case ']':
7713             case '4':
7714             case '5':
7715             case 'H':
7716             case 'W':
7717             case 'D':
7718             case 'j':
7719             case '8':
7720             case 'V':
7721             case 'C':
7722             case 'U':
7723             case 'k':
7724             case 'K':
7725               if (s[0] == '%'
7726                   && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
7727                 {
7728                   /* This is %gprel(SYMBOL).  We need to read SYMBOL,
7729                      and generate the appropriate reloc.  If the text
7730                      inside %gprel is not a symbol name with an
7731                      optional offset, then we generate a normal reloc
7732                      and will probably fail later.  */
7733                   my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
7734                   if (imm_expr.X_op == O_symbol)
7735                     {
7736                       mips16_ext = true;
7737                       imm_reloc = BFD_RELOC_MIPS16_GPREL;
7738                       s = expr_end;
7739                       ip->use_extend = true;
7740                       ip->extend = 0;
7741                       continue;
7742                     }
7743                 }
7744               else
7745                 {
7746                   /* Just pick up a normal expression.  */
7747                   my_getExpression (&imm_expr, s);
7748                 }
7749
7750               if (imm_expr.X_op == O_register)
7751                 {
7752                   /* What we thought was an expression turned out to
7753                      be a register.  */
7754
7755                   if (s[0] == '(' && args[1] == '(')
7756                     {
7757                       /* It looks like the expression was omitted
7758                          before a register indirection, which means
7759                          that the expression is implicitly zero.  We
7760                          still set up imm_expr, so that we handle
7761                          explicit extensions correctly.  */
7762                       imm_expr.X_op = O_constant;
7763                       imm_expr.X_add_number = 0;
7764                       imm_reloc = (int) BFD_RELOC_UNUSED + c;
7765                       continue;
7766                     }
7767
7768                   break;
7769                 }
7770
7771               /* We need to relax this instruction.  */
7772               imm_reloc = (int) BFD_RELOC_UNUSED + c;
7773               s = expr_end;
7774               continue;
7775
7776             case 'p':
7777             case 'q':
7778             case 'A':
7779             case 'B':
7780             case 'E':
7781               /* We use offset_reloc rather than imm_reloc for the PC
7782                  relative operands.  This lets macros with both
7783                  immediate and address operands work correctly.  */
7784               my_getExpression (&offset_expr, s);
7785
7786               if (offset_expr.X_op == O_register)
7787                 break;
7788
7789               /* We need to relax this instruction.  */
7790               offset_reloc = (int) BFD_RELOC_UNUSED + c;
7791               s = expr_end;
7792               continue;
7793
7794             case '6':           /* break code */
7795               my_getExpression (&imm_expr, s);
7796               check_absolute_expr (ip, &imm_expr);
7797               if ((unsigned long) imm_expr.X_add_number > 63)
7798                 {
7799                   as_warn ("Invalid value for `%s' (%lu)",
7800                            ip->insn_mo->name,
7801                            (unsigned long) imm_expr.X_add_number);
7802                   imm_expr.X_add_number &= 0x3f;
7803                 }
7804               ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
7805               imm_expr.X_op = O_absent;
7806               s = expr_end;
7807               continue;
7808
7809             case 'a':           /* 26 bit address */
7810               my_getExpression (&offset_expr, s);
7811               s = expr_end;
7812               offset_reloc = BFD_RELOC_MIPS16_JMP;
7813               ip->insn_opcode <<= 16;
7814               continue;
7815
7816             case 'l':           /* register list for entry macro */
7817             case 'L':           /* register list for exit macro */
7818               {
7819                 int mask;
7820
7821                 if (c == 'l')
7822                   mask = 0;
7823                 else
7824                   mask = 7 << 3;
7825                 while (*s != '\0')
7826                   {
7827                     int freg, reg1, reg2;
7828
7829                     while (*s == ' ' || *s == ',')
7830                       ++s;
7831                     if (*s != '$')
7832                       {
7833                         as_bad ("can't parse register list");
7834                         break;
7835                       }
7836                     ++s;
7837                     if (*s != 'f')
7838                       freg = 0;
7839                     else
7840                       {
7841                         freg = 1;
7842                         ++s;
7843                       }
7844                     reg1 = 0;
7845                     while (isdigit (*s))
7846                       {
7847                         reg1 *= 10;
7848                         reg1 += *s - '0';
7849                         ++s;
7850                       }
7851                     if (*s == ' ')
7852                       ++s;
7853                     if (*s != '-')
7854                       reg2 = reg1;
7855                     else
7856                       {
7857                         ++s;
7858                         if (*s != '$')
7859                           break;
7860                         ++s;
7861                         if (freg)
7862                           {
7863                             if (*s == 'f')
7864                               ++s;
7865                             else
7866                               {
7867                                 as_bad ("invalid register list");
7868                                 break;
7869                               }
7870                           }
7871                         reg2 = 0;
7872                         while (isdigit (*s))
7873                           {
7874                             reg2 *= 10;
7875                             reg2 += *s - '0';
7876                             ++s;
7877                           }
7878                       }
7879                     if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
7880                       {
7881                         mask &= ~ (7 << 3);
7882                         mask |= 5 << 3;
7883                       }
7884                     else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
7885                       {
7886                         mask &= ~ (7 << 3);
7887                         mask |= 6 << 3;
7888                       }
7889                     else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
7890                       mask |= (reg2 - 3) << 3;
7891                     else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
7892                       mask |= (reg2 - 15) << 1;
7893                     else if (reg1 == 31 && reg2 == 31)
7894                       mask |= 1;
7895                     else
7896                       {
7897                         as_bad ("invalid register list");
7898                         break;
7899                       }
7900                   }
7901                 /* The mask is filled in in the opcode table for the
7902                    benefit of the disassembler.  We remove it before
7903                    applying the actual mask.  */
7904                 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
7905                 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
7906               }
7907             continue;
7908
7909             case 'e':           /* extend code */
7910               my_getExpression (&imm_expr, s);
7911               check_absolute_expr (ip, &imm_expr);
7912               if ((unsigned long) imm_expr.X_add_number > 0x7ff)
7913                 {
7914                   as_warn ("Invalid value for `%s' (%lu)",
7915                            ip->insn_mo->name,
7916                            (unsigned long) imm_expr.X_add_number);
7917                   imm_expr.X_add_number &= 0x7ff;
7918                 }
7919               ip->insn_opcode |= imm_expr.X_add_number;
7920               imm_expr.X_op = O_absent;
7921               s = expr_end;
7922               continue;
7923
7924             default:
7925               internalError ();
7926             }
7927           break;
7928         }
7929
7930       /* Args don't match.  */
7931       if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
7932           strcmp (insn->name, insn[1].name) == 0)
7933         {
7934           ++insn;
7935           s = argsstart;
7936           continue;
7937         }
7938
7939       insn_error = "illegal operands";
7940
7941       return;
7942     }
7943 }
7944
7945 /* This structure holds information we know about a mips16 immediate
7946    argument type.  */
7947
7948 struct mips16_immed_operand
7949 {
7950   /* The type code used in the argument string in the opcode table.  */
7951   int type;
7952   /* The number of bits in the short form of the opcode.  */
7953   int nbits;
7954   /* The number of bits in the extended form of the opcode.  */
7955   int extbits;
7956   /* The amount by which the short form is shifted when it is used;
7957      for example, the sw instruction has a shift count of 2.  */
7958   int shift;
7959   /* The amount by which the short form is shifted when it is stored
7960      into the instruction code.  */
7961   int op_shift;
7962   /* Non-zero if the short form is unsigned.  */
7963   int unsp;
7964   /* Non-zero if the extended form is unsigned.  */
7965   int extu;
7966   /* Non-zero if the value is PC relative.  */
7967   int pcrel;
7968 };
7969
7970 /* The mips16 immediate operand types.  */
7971
7972 static const struct mips16_immed_operand mips16_immed_operands[] =
7973 {
7974   { '<',  3,  5, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
7975   { '>',  3,  5, 0, MIPS16OP_SH_RX,   1, 1, 0 },
7976   { '[',  3,  6, 0, MIPS16OP_SH_RZ,   1, 1, 0 },
7977   { ']',  3,  6, 0, MIPS16OP_SH_RX,   1, 1, 0 },
7978   { '4',  4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
7979   { '5',  5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
7980   { 'H',  5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
7981   { 'W',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
7982   { 'D',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
7983   { 'j',  5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
7984   { '8',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
7985   { 'V',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
7986   { 'C',  8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
7987   { 'U',  8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
7988   { 'k',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
7989   { 'K',  8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
7990   { 'p',  8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
7991   { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
7992   { 'A',  8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
7993   { 'B',  5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
7994   { 'E',  5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
7995 };
7996
7997 #define MIPS16_NUM_IMMED \
7998   (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
7999
8000 /* Handle a mips16 instruction with an immediate value.  This or's the
8001    small immediate value into *INSN.  It sets *USE_EXTEND to indicate
8002    whether an extended value is needed; if one is needed, it sets
8003    *EXTEND to the value.  The argument type is TYPE.  The value is VAL.
8004    If SMALL is true, an unextended opcode was explicitly requested.
8005    If EXT is true, an extended opcode was explicitly requested.  If
8006    WARN is true, warn if EXT does not match reality.  */
8007
8008 static void
8009 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
8010               extend)
8011      char *file;
8012      unsigned int line;
8013      int type;
8014      offsetT val;
8015      boolean warn;
8016      boolean small;
8017      boolean ext;
8018      unsigned long *insn;
8019      boolean *use_extend;
8020      unsigned short *extend;
8021 {
8022   register const struct mips16_immed_operand *op;
8023   int mintiny, maxtiny;
8024   boolean needext;
8025
8026   op = mips16_immed_operands;
8027   while (op->type != type)
8028     {
8029       ++op;
8030       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
8031     }
8032
8033   if (op->unsp)
8034     {
8035       if (type == '<' || type == '>' || type == '[' || type == ']')
8036         {
8037           mintiny = 1;
8038           maxtiny = 1 << op->nbits;
8039         }
8040       else
8041         {
8042           mintiny = 0;
8043           maxtiny = (1 << op->nbits) - 1;
8044         }
8045     }
8046   else
8047     {
8048       mintiny = - (1 << (op->nbits - 1));
8049       maxtiny = (1 << (op->nbits - 1)) - 1;
8050     }
8051
8052   /* Branch offsets have an implicit 0 in the lowest bit.  */
8053   if (type == 'p' || type == 'q')
8054     val /= 2;
8055
8056   if ((val & ((1 << op->shift) - 1)) != 0
8057       || val < (mintiny << op->shift)
8058       || val > (maxtiny << op->shift))
8059     needext = true;
8060   else
8061     needext = false;
8062
8063   if (warn && ext && ! needext)
8064     as_warn_where (file, line, "extended operand requested but not required");
8065   if (small && needext)
8066     as_bad_where (file, line, "invalid unextended operand value");
8067
8068   if (small || (! ext && ! needext))
8069     {
8070       int insnval;
8071
8072       *use_extend = false;
8073       insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
8074       insnval <<= op->op_shift;
8075       *insn |= insnval;
8076     }
8077   else
8078     {
8079       long minext, maxext;
8080       int extval;
8081
8082       if (op->extu)
8083         {
8084           minext = 0;
8085           maxext = (1 << op->extbits) - 1;
8086         }
8087       else
8088         {
8089           minext = - (1 << (op->extbits - 1));
8090           maxext = (1 << (op->extbits - 1)) - 1;
8091         }
8092       if (val < minext || val > maxext)
8093         as_bad_where (file, line,
8094                       "operand value out of range for instruction");
8095
8096       *use_extend = true;
8097       if (op->extbits == 16)
8098         {
8099           extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
8100           val &= 0x1f;
8101         }
8102       else if (op->extbits == 15)
8103         {
8104           extval = ((val >> 11) & 0xf) | (val & 0x7f0);
8105           val &= 0xf;
8106         }
8107       else
8108         {
8109           extval = ((val & 0x1f) << 6) | (val & 0x20);
8110           val = 0;
8111         }
8112
8113       *extend = (unsigned short) extval;
8114       *insn |= val;
8115     }
8116 }
8117 \f
8118 #define LP '('
8119 #define RP ')'
8120
8121 static int
8122 my_getSmallExpression (ep, str)
8123      expressionS *ep;
8124      char *str;
8125 {
8126   char *sp;
8127   int c = 0;
8128
8129   if (*str == ' ')
8130     str++;
8131   if (*str == LP
8132       || (*str == '%' &&
8133           ((str[1] == 'h' && str[2] == 'i')
8134            || (str[1] == 'H' && str[2] == 'I')
8135            || (str[1] == 'l' && str[2] == 'o'))
8136           && str[3] == LP))
8137     {
8138       if (*str == LP)
8139         c = 0;
8140       else
8141         {
8142           c = str[1];
8143           str += 3;
8144         }
8145
8146       /*
8147        * A small expression may be followed by a base register.
8148        * Scan to the end of this operand, and then back over a possible
8149        * base register.  Then scan the small expression up to that
8150        * point.  (Based on code in sparc.c...)
8151        */
8152       for (sp = str; *sp && *sp != ','; sp++)
8153         ;
8154       if (sp - 4 >= str && sp[-1] == RP)
8155         {
8156           if (isdigit (sp[-2]))
8157             {
8158               for (sp -= 3; sp >= str && isdigit (*sp); sp--)
8159                 ;
8160               if (*sp == '$' && sp > str && sp[-1] == LP)
8161                 {
8162                   sp--;
8163                   goto do_it;
8164                 }
8165             }
8166           else if (sp - 5 >= str
8167                    && sp[-5] == LP
8168                    && sp[-4] == '$'
8169                    && ((sp[-3] == 'f' && sp[-2] == 'p')
8170                        || (sp[-3] == 's' && sp[-2] == 'p')
8171                        || (sp[-3] == 'g' && sp[-2] == 'p')
8172                        || (sp[-3] == 'a' && sp[-2] == 't')))
8173             {
8174               sp -= 5;
8175             do_it:
8176               if (sp == str)
8177                 {
8178                   /* no expression means zero offset */
8179                   if (c)
8180                     {
8181                       /* %xx(reg) is an error */
8182                       ep->X_op = O_absent;
8183                       expr_end = str - 3;
8184                     }
8185                   else
8186                     {
8187                       ep->X_op = O_constant;
8188                       expr_end = sp;
8189                     }
8190                   ep->X_add_symbol = NULL;
8191                   ep->X_op_symbol = NULL;
8192                   ep->X_add_number = 0;
8193                 }
8194               else
8195                 {
8196                   *sp = '\0';
8197                   my_getExpression (ep, str);
8198                   *sp = LP;
8199                 }
8200               return c;
8201             }
8202         }
8203     }
8204   my_getExpression (ep, str);
8205   return c;                     /* => %hi or %lo encountered */
8206 }
8207
8208 static void
8209 my_getExpression (ep, str)
8210      expressionS *ep;
8211      char *str;
8212 {
8213   char *save_in;
8214
8215   save_in = input_line_pointer;
8216   input_line_pointer = str;
8217   expression (ep);
8218   expr_end = input_line_pointer;
8219   input_line_pointer = save_in;
8220
8221   /* If we are in mips16 mode, and this is an expression based on `.',
8222      then we bump the value of the symbol by 1 since that is how other
8223      text symbols are handled.  We don't bother to handle complex
8224      expressions, just `.' plus or minus a constant.  */
8225   if (mips_opts.mips16
8226       && ep->X_op == O_symbol
8227       && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
8228       && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
8229       && ep->X_add_symbol->sy_frag == frag_now
8230       && ep->X_add_symbol->sy_value.X_op == O_constant
8231       && ep->X_add_symbol->sy_value.X_add_number == frag_now_fix ())
8232     ++ep->X_add_symbol->sy_value.X_add_number;
8233 }
8234
8235 /* Turn a string in input_line_pointer into a floating point constant
8236    of type type, and store the appropriate bytes in *litP.  The number
8237    of LITTLENUMS emitted is stored in *sizeP .  An error message is
8238    returned, or NULL on OK.  */
8239
8240 char *
8241 md_atof (type, litP, sizeP)
8242      int type;
8243      char *litP;
8244      int *sizeP;
8245 {
8246   int prec;
8247   LITTLENUM_TYPE words[4];
8248   char *t;
8249   int i;
8250
8251   switch (type)
8252     {
8253     case 'f':
8254       prec = 2;
8255       break;
8256
8257     case 'd':
8258       prec = 4;
8259       break;
8260
8261     default:
8262       *sizeP = 0;
8263       return "bad call to md_atof";
8264     }
8265
8266   t = atof_ieee (input_line_pointer, type, words);
8267   if (t)
8268     input_line_pointer = t;
8269
8270   *sizeP = prec * 2;
8271
8272   if (! target_big_endian)
8273     {
8274       for (i = prec - 1; i >= 0; i--)
8275         {
8276           md_number_to_chars (litP, (valueT) words[i], 2);
8277           litP += 2;
8278         }
8279     }
8280   else
8281     {
8282       for (i = 0; i < prec; i++)
8283         {
8284           md_number_to_chars (litP, (valueT) words[i], 2);
8285           litP += 2;
8286         }
8287     }
8288      
8289   return NULL;
8290 }
8291
8292 void
8293 md_number_to_chars (buf, val, n)
8294      char *buf;
8295      valueT val;
8296      int n;
8297 {
8298   if (target_big_endian)
8299     number_to_chars_bigendian (buf, val, n);
8300   else
8301     number_to_chars_littleendian (buf, val, n);
8302 }
8303 \f
8304 CONST char *md_shortopts = "O::g::G:";
8305
8306 struct option md_longopts[] = {
8307 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
8308   {"mips0", no_argument, NULL, OPTION_MIPS1},
8309   {"mips1", no_argument, NULL, OPTION_MIPS1},
8310 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
8311   {"mips2", no_argument, NULL, OPTION_MIPS2},
8312 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
8313   {"mips3", no_argument, NULL, OPTION_MIPS3},
8314 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
8315   {"mips4", no_argument, NULL, OPTION_MIPS4},
8316 #define OPTION_MCPU (OPTION_MD_BASE + 5)
8317   {"mcpu", required_argument, NULL, OPTION_MCPU},
8318 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
8319   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
8320 #define OPTION_TRAP (OPTION_MD_BASE + 9)
8321   {"trap", no_argument, NULL, OPTION_TRAP},
8322   {"no-break", no_argument, NULL, OPTION_TRAP},
8323 #define OPTION_BREAK (OPTION_MD_BASE + 10)
8324   {"break", no_argument, NULL, OPTION_BREAK},
8325   {"no-trap", no_argument, NULL, OPTION_BREAK},
8326 #define OPTION_EB (OPTION_MD_BASE + 11)
8327   {"EB", no_argument, NULL, OPTION_EB},
8328 #define OPTION_EL (OPTION_MD_BASE + 12)
8329   {"EL", no_argument, NULL, OPTION_EL},
8330 #define OPTION_M4650 (OPTION_MD_BASE + 13)
8331   {"m4650", no_argument, NULL, OPTION_M4650},
8332 #define OPTION_NO_M4650 (OPTION_MD_BASE + 14)
8333   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
8334 #define OPTION_M4010 (OPTION_MD_BASE + 15)
8335   {"m4010", no_argument, NULL, OPTION_M4010},
8336 #define OPTION_NO_M4010 (OPTION_MD_BASE + 16)
8337   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
8338 #define OPTION_M4100 (OPTION_MD_BASE + 17)
8339   {"m4100", no_argument, NULL, OPTION_M4100},
8340 #define OPTION_NO_M4100 (OPTION_MD_BASE + 18)
8341   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
8342 #define OPTION_MIPS16 (OPTION_MD_BASE + 22)
8343   {"mips16", no_argument, NULL, OPTION_MIPS16},
8344 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 23)
8345   {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
8346   /* start-sanitize-r5900 */
8347 #define OPTION_M5900 (OPTION_MD_BASE + 24)
8348   {"m5900", no_argument, NULL, OPTION_M5900},
8349 #define OPTION_NO_M5900 (OPTION_MD_BASE + 25)
8350   {"no-m5900", no_argument, NULL, OPTION_NO_M5900},
8351   /* end-sanitize-r5900 */
8352 #define OPTION_M3900 (OPTION_MD_BASE + 26)
8353   {"m3900", no_argument, NULL, OPTION_M3900},
8354 #define OPTION_NO_M3900 (OPTION_MD_BASE + 27)
8355   {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
8356
8357   /* start-sanitize-tx19 */
8358   {"m1900", no_argument, NULL, OPTION_M3900},
8359   {"no-m1900", no_argument, NULL, OPTION_NO_M3900},
8360   /* end-sanitize-tx19 */
8361
8362 #define OPTION_CALL_SHARED (OPTION_MD_BASE + 7)
8363 #define OPTION_NON_SHARED (OPTION_MD_BASE + 8)
8364 #define OPTION_XGOT (OPTION_MD_BASE + 19)
8365 #define OPTION_32 (OPTION_MD_BASE + 20)
8366 #define OPTION_64 (OPTION_MD_BASE + 21)
8367 #ifdef OBJ_ELF
8368   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
8369   {"xgot", no_argument, NULL, OPTION_XGOT},
8370   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
8371   {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
8372   {"32", no_argument, NULL, OPTION_32},
8373   {"64", no_argument, NULL, OPTION_64},
8374 #endif
8375
8376   {NULL, no_argument, NULL, 0}
8377 };
8378 size_t md_longopts_size = sizeof(md_longopts);
8379
8380 int
8381 md_parse_option (c, arg)
8382      int c;
8383      char *arg;
8384 {
8385   switch (c)
8386     {
8387     case OPTION_TRAP:
8388       mips_trap = 1;
8389       break;
8390
8391     case OPTION_BREAK:
8392       mips_trap = 0;
8393       break;
8394
8395     case OPTION_EB:
8396       target_big_endian = 1;
8397       break;
8398
8399     case OPTION_EL:
8400       target_big_endian = 0;
8401       break;
8402
8403     case 'O':
8404       if (arg && arg[1] == '0')
8405         mips_optimize = 1;
8406       else
8407         mips_optimize = 2;
8408       break;
8409
8410     case 'g':
8411       if (arg == NULL)
8412         mips_debug = 2;
8413       else
8414         mips_debug = atoi (arg);
8415       /* When the MIPS assembler sees -g or -g2, it does not do
8416          optimizations which limit full symbolic debugging.  We take
8417          that to be equivalent to -O0.  */
8418       if (mips_debug == 2)
8419         mips_optimize = 1;
8420       break;
8421
8422     case OPTION_MIPS1:
8423       mips_opts.isa = 1;
8424       if (mips_cpu == -1)
8425         mips_cpu = 3000;
8426       break;
8427
8428     case OPTION_MIPS2:
8429       mips_opts.isa = 2;
8430       if (mips_cpu == -1)
8431         mips_cpu = 6000;
8432       break;
8433
8434     case OPTION_MIPS3:
8435       mips_opts.isa = 3;
8436       if (mips_cpu == -1)
8437         mips_cpu = 4000;
8438       break;
8439
8440     case OPTION_MIPS4:
8441       mips_opts.isa = 4;
8442       if (mips_cpu == -1)
8443         mips_cpu = 8000;
8444       break;
8445
8446     case OPTION_MCPU:
8447       {
8448         char *p;
8449
8450         /* Identify the processor type */
8451         p = arg;
8452         if (strcmp (p, "default") == 0
8453             || strcmp (p, "DEFAULT") == 0)
8454           mips_cpu = -1;
8455         else
8456           {
8457             int sv = 0;
8458
8459             /* We need to cope with the various "vr" prefixes for the 4300
8460                processor.  */
8461             if (*p == 'v' || *p == 'V')
8462               {
8463                 sv = 1;
8464                 p++;
8465               }
8466
8467             if (*p == 'r' || *p == 'R')
8468               p++;
8469
8470             mips_cpu = -1;
8471             switch (*p)
8472               {
8473               case '1':
8474                 if (strcmp (p, "10000") == 0
8475                     || strcmp (p, "10k") == 0
8476                     || strcmp (p, "10K") == 0)
8477                   mips_cpu = 10000;
8478                 /* start-sanitize-tx19 */
8479                 else if (strcmp (p, "1900") == 0)
8480                   mips_cpu = 3900;
8481                 /* end-sanitize-tx19 */
8482                 break;
8483
8484               case '2':
8485                 if (strcmp (p, "2000") == 0
8486                     || strcmp (p, "2k") == 0
8487                     || strcmp (p, "2K") == 0)
8488                   mips_cpu = 2000;
8489                 break;
8490
8491               case '3':
8492                 if (strcmp (p, "3000") == 0
8493                     || strcmp (p, "3k") == 0
8494                     || strcmp (p, "3K") == 0)
8495                   mips_cpu = 3000;
8496                 else if (strcmp (p, "3900") == 0)
8497                   mips_cpu = 3900;
8498                 break;
8499
8500               case '4':
8501                 if (strcmp (p, "4000") == 0
8502                     || strcmp (p, "4k") == 0
8503                     || strcmp (p, "4K") == 0)
8504                   mips_cpu = 4000;
8505                 else if (strcmp (p, "4100") == 0)
8506                   {
8507                     mips_cpu = 4100;
8508                     if (mips_4100 < 0)
8509                       mips_4100 = 1;
8510                   }
8511                 else if (strcmp (p, "4300") == 0)
8512                   mips_cpu = 4300;
8513                 else if (strcmp (p, "4400") == 0)
8514                   mips_cpu = 4400;
8515                 else if (strcmp (p, "4600") == 0)
8516                   mips_cpu = 4600;
8517                 else if (strcmp (p, "4650") == 0)
8518                   {
8519                     mips_cpu = 4650;
8520                     if (mips_4650 < 0)
8521                       mips_4650 = 1;
8522                   }
8523                 else if (strcmp (p, "4010") == 0)
8524                   {
8525                     mips_cpu = 4010;
8526                     if (mips_4010 < 0)
8527                       mips_4010 = 1;
8528                   }
8529                 break;
8530
8531               case '5':
8532                 if (strcmp (p, "5000") == 0
8533                     || strcmp (p, "5k") == 0
8534                     || strcmp (p, "5K") == 0)
8535                   mips_cpu = 5000;
8536                 /* start-sanitize-r5900 */
8537                 else if (strcmp (p, "5900") == 0)
8538                   mips_cpu = 5900;
8539                 /* end-sanitize-r5900 */
8540                 break;
8541
8542               case '6':
8543                 if (strcmp (p, "6000") == 0
8544                     || strcmp (p, "6k") == 0
8545                     || strcmp (p, "6K") == 0)
8546                   mips_cpu = 6000;
8547                 break;
8548
8549               case '8':
8550                 if (strcmp (p, "8000") == 0
8551                     || strcmp (p, "8k") == 0
8552                     || strcmp (p, "8K") == 0)
8553                   mips_cpu = 8000;
8554                 break;
8555
8556               case 'o':
8557                 if (strcmp (p, "orion") == 0)
8558                   mips_cpu = 4600;
8559                 break;
8560               }
8561
8562             if (sv && mips_cpu != 4300 && mips_cpu != 4100 && mips_cpu != 5000)
8563               {
8564                 as_bad ("ignoring invalid leading 'v' in -mcpu=%s switch", arg);
8565                 return 0;
8566               }
8567
8568             if (mips_cpu == -1)
8569               {
8570                 as_bad ("invalid architecture -mcpu=%s", arg);
8571                 return 0;
8572               }
8573           }
8574       }
8575       break;
8576
8577     case OPTION_M4650:
8578       mips_4650 = 1;
8579       break;
8580
8581     case OPTION_NO_M4650:
8582       mips_4650 = 0;
8583       break;
8584
8585     case OPTION_M4010:
8586       mips_4010 = 1;
8587       break;
8588
8589     case OPTION_NO_M4010:
8590       mips_4010 = 0;
8591       break;
8592
8593     case OPTION_M4100:
8594       mips_4100 = 1;
8595       break;
8596
8597     case OPTION_NO_M4100:
8598       mips_4100 = 0;
8599       break;
8600
8601       /* start-sanitize-r5900 */
8602     case OPTION_M5900:
8603       mips_5900 = 1;
8604       break;
8605
8606     case OPTION_NO_M5900:
8607       mips_5900 = 0;
8608       break;
8609       /* end-sanitize-r5900 */
8610
8611     case OPTION_M3900:
8612       mips_3900 = 1;
8613       break;
8614       
8615     case OPTION_NO_M3900:
8616       mips_3900 = 0;
8617       break;
8618
8619     case OPTION_MIPS16:
8620       mips_opts.mips16 = 1;
8621       mips_no_prev_insn (false);
8622       break;
8623
8624     case OPTION_NO_MIPS16:
8625       mips_opts.mips16 = 0;
8626       mips_no_prev_insn (false);
8627       break;
8628
8629     case OPTION_MEMBEDDED_PIC:
8630       mips_pic = EMBEDDED_PIC;
8631       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
8632         {
8633           as_bad ("-G may not be used with embedded PIC code");
8634           return 0;
8635         }
8636       g_switch_value = 0x7fffffff;
8637       break;
8638
8639       /* When generating ELF code, we permit -KPIC and -call_shared to
8640          select SVR4_PIC, and -non_shared to select no PIC.  This is
8641          intended to be compatible with Irix 5.  */
8642     case OPTION_CALL_SHARED:
8643       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
8644         {
8645           as_bad ("-call_shared is supported only for ELF format");
8646           return 0;
8647         }
8648       mips_pic = SVR4_PIC;
8649       if (g_switch_seen && g_switch_value != 0)
8650         {
8651           as_bad ("-G may not be used with SVR4 PIC code");
8652           return 0;
8653         }
8654       g_switch_value = 0;
8655       break;
8656
8657     case OPTION_NON_SHARED:
8658       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
8659         {
8660           as_bad ("-non_shared is supported only for ELF format");
8661           return 0;
8662         }
8663       mips_pic = NO_PIC;
8664       break;
8665
8666       /* The -xgot option tells the assembler to use 32 offsets when
8667          accessing the got in SVR4_PIC mode.  It is for Irix
8668          compatibility.  */
8669     case OPTION_XGOT:
8670       mips_big_got = 1;
8671       break;
8672
8673     case 'G':
8674       if (! USE_GLOBAL_POINTER_OPT)
8675         {
8676           as_bad ("-G is not supported for this configuration");
8677           return 0;
8678         }
8679       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
8680         {
8681           as_bad ("-G may not be used with SVR4 or embedded PIC code");
8682           return 0;
8683         }
8684       else
8685         g_switch_value = atoi (arg);
8686       g_switch_seen = 1;
8687       break;
8688
8689       /* The -32 and -64 options tell the assembler to output the 32
8690          bit or the 64 bit MIPS ELF format.  */
8691     case OPTION_32:
8692       mips_64 = 0;
8693       break;
8694
8695     case OPTION_64:
8696       {
8697         const char **list, **l;
8698
8699         list = bfd_target_list ();
8700         for (l = list; *l != NULL; l++)
8701           if (strcmp (*l, "elf64-bigmips") == 0
8702               || strcmp (*l, "elf64-littlemips") == 0)
8703             break;
8704         if (*l == NULL)
8705           as_fatal ("No compiled in support for 64 bit object file format");
8706         free (list);
8707         mips_64 = 1;
8708       }
8709       break;
8710
8711     default:
8712       return 0;
8713     }
8714
8715   return 1;
8716 }
8717
8718 void
8719 md_show_usage (stream)
8720      FILE *stream;
8721 {
8722   fprintf(stream, "\
8723 MIPS options:\n\
8724 -membedded-pic          generate embedded position independent code\n\
8725 -EB                     generate big endian output\n\
8726 -EL                     generate little endian output\n\
8727 -g, -g2                 do not remove uneeded NOPs or swap branches\n\
8728 -G NUM                  allow referencing objects up to NUM bytes\n\
8729                         implicitly with the gp register [default 8]\n");
8730   fprintf(stream, "\
8731 -mips1, -mcpu=r{2,3}000 generate code for r2000 and r3000\n\
8732 -mips2, -mcpu=r6000     generate code for r6000\n\
8733 -mips3, -mcpu=r4000     generate code for r4000\n\
8734 -mips4, -mcpu=r8000     generate code for r8000\n\
8735 -mcpu=vr4300            generate code for vr4300\n\
8736 -mcpu=vr4100            generate code for vr4100\n\
8737 -m4650                  permit R4650 instructions\n\
8738 -no-m4650               do not permit R4650 instructions\n\
8739 -m4010                  permit R4010 instructions\n\
8740 -no-m4010               do not permit R4010 instructions\n\
8741 -m4100                  permit VR4100 instructions\n\
8742 -no-m4100               do not permit VR4100 instructions\n");
8743   fprintf(stream, "\
8744 -mips16                 generate mips16 instructions\n\
8745 -no-mips16              do not generate mips16 instructions\n");
8746   fprintf(stream, "\
8747 -O0                     remove unneeded NOPs, do not swap branches\n\
8748 -O                      remove unneeded NOPs and swap branches\n\
8749 --trap, --no-break      trap exception on div by 0 and mult overflow\n\
8750 --break, --no-trap      break exception on div by 0 and mult overflow\n");
8751 #ifdef OBJ_ELF
8752   fprintf(stream, "\
8753 -KPIC, -call_shared     generate SVR4 position independent code\n\
8754 -non_shared             do not generate position independent code\n\
8755 -xgot                   assume a 32 bit GOT\n\
8756 -32                     create 32 bit object file (default)\n\
8757 -64                     create 64 bit object file\n");
8758 #endif
8759 }
8760 \f
8761 void
8762 mips_init_after_args ()
8763 {
8764   /* initialize opcodes */
8765   bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
8766   mips_opcodes = (struct mips_opcode*) mips_builtin_opcodes;
8767 }
8768
8769 long
8770 md_pcrel_from (fixP)
8771      fixS *fixP;
8772 {
8773   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
8774       && fixP->fx_addsy != (symbolS *) NULL
8775       && ! S_IS_DEFINED (fixP->fx_addsy))
8776     {
8777       /* This makes a branch to an undefined symbol be a branch to the
8778          current location.  */
8779       return 4;
8780     }
8781
8782   /* return the address of the delay slot */
8783   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
8784 }
8785
8786 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
8787    reloc for a cons.  We could use the definition there, except that
8788    we want to handle 64 bit relocs specially.  */
8789
8790 void
8791 cons_fix_new_mips (frag, where, nbytes, exp)
8792      fragS *frag;
8793      int where;
8794      unsigned int nbytes;
8795      expressionS *exp;
8796 {
8797 #ifndef OBJ_ELF
8798   /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
8799      4 byte reloc.  */
8800   if (nbytes == 8 && ! mips_64)
8801     {
8802       if (target_big_endian)
8803         where += 4;
8804       nbytes = 4;
8805     }
8806 #endif
8807
8808   if (nbytes != 2 && nbytes != 4 && nbytes != 8)
8809     as_bad ("Unsupported reloc size %d", nbytes);
8810
8811   fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
8812                (nbytes == 2
8813                 ? BFD_RELOC_16
8814                 : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
8815 }
8816
8817 /* This is called before the symbol table is processed.  In order to
8818    work with gcc when using mips-tfile, we must keep all local labels.
8819    However, in other cases, we want to discard them.  If we were
8820    called with -g, but we didn't see any debugging information, it may
8821    mean that gcc is smuggling debugging information through to
8822    mips-tfile, in which case we must generate all local labels.  */
8823
8824 void
8825 mips_frob_file_before_adjust ()
8826 {
8827 #ifndef NO_ECOFF_DEBUGGING
8828   if (ECOFF_DEBUGGING
8829       && mips_debug != 0
8830       && ! ecoff_debugging_seen)
8831     flag_keep_locals = 1;
8832 #endif
8833 }
8834
8835 /* Sort any unmatched HI16_S relocs so that they immediately precede
8836    the corresponding LO reloc.  This is called before md_apply_fix and
8837    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
8838    explicit use of the %hi modifier.  */
8839
8840 void
8841 mips_frob_file ()
8842 {
8843   struct mips_hi_fixup *l;
8844
8845   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
8846     {
8847       segment_info_type *seginfo;
8848       int pass;
8849
8850       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
8851
8852       /* Check quickly whether the next fixup happens to be a matching
8853          %lo.  */
8854       if (l->fixp->fx_next != NULL
8855           && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
8856           && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
8857           && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
8858         continue;
8859
8860       /* Look through the fixups for this segment for a matching %lo.
8861          When we find one, move the %hi just in front of it.  We do
8862          this in two passes.  In the first pass, we try to find a
8863          unique %lo.  In the second pass, we permit multiple %hi
8864          relocs for a single %lo (this is a GNU extension).  */
8865       seginfo = seg_info (l->seg);
8866       for (pass = 0; pass < 2; pass++)
8867         {
8868           fixS *f, *prev;
8869
8870           prev = NULL;
8871           for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
8872             {
8873               /* Check whether this is a %lo fixup which matches l->fixp.  */
8874               if (f->fx_r_type == BFD_RELOC_LO16
8875                   && f->fx_addsy == l->fixp->fx_addsy
8876                   && f->fx_offset == l->fixp->fx_offset
8877                   && (pass == 1
8878                       || prev == NULL
8879                       || prev->fx_r_type != BFD_RELOC_HI16_S
8880                       || prev->fx_addsy != f->fx_addsy
8881                       || prev->fx_offset !=  f->fx_offset))
8882                 {
8883                   fixS **pf;
8884
8885                   /* Move l->fixp before f.  */
8886                   for (pf = &seginfo->fix_root;
8887                        *pf != l->fixp;
8888                        pf = &(*pf)->fx_next)
8889                     assert (*pf != NULL);
8890
8891                   *pf = l->fixp->fx_next;
8892
8893                   l->fixp->fx_next = f;
8894                   if (prev == NULL)
8895                     seginfo->fix_root = l->fixp;
8896                   else
8897                     prev->fx_next = l->fixp;
8898
8899                   break;
8900                 }
8901
8902               prev = f;
8903             }
8904
8905           if (f != NULL)
8906             break;
8907
8908           if (pass == 1)
8909             as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
8910                            "Unmatched %%hi reloc");
8911         }
8912     }
8913 }
8914
8915 /* When generating embedded PIC code we need to use a special
8916    relocation to represent the difference of two symbols in the .text
8917    section (switch tables use a difference of this sort).  See
8918    include/coff/mips.h for details.  This macro checks whether this
8919    fixup requires the special reloc.  */
8920 #define SWITCH_TABLE(fixp) \
8921   ((fixp)->fx_r_type == BFD_RELOC_32 \
8922    && (fixp)->fx_addsy != NULL \
8923    && (fixp)->fx_subsy != NULL \
8924    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
8925    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
8926
8927 /* When generating embedded PIC code we must keep all PC relative
8928    relocations, in case the linker has to relax a call.  We also need
8929    to keep relocations for switch table entries.  */
8930
8931 /*ARGSUSED*/
8932 int
8933 mips_force_relocation (fixp)
8934      fixS *fixp;
8935 {
8936   return (mips_pic == EMBEDDED_PIC
8937           && (fixp->fx_pcrel
8938               || SWITCH_TABLE (fixp)
8939               || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
8940               || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
8941 }
8942
8943 /* Apply a fixup to the object file.  */
8944
8945 int
8946 md_apply_fix (fixP, valueP)
8947      fixS *fixP;
8948      valueT *valueP;
8949 {
8950   unsigned char *buf;
8951   long insn, value;
8952
8953   assert (fixP->fx_size == 4
8954           || fixP->fx_r_type == BFD_RELOC_16
8955           || fixP->fx_r_type == BFD_RELOC_64);
8956
8957   value = *valueP;
8958
8959   /* If we aren't adjusting this fixup to be against the section
8960      symbol, we need to adjust the value.  */
8961 #ifdef OBJ_ELF
8962   if (fixP->fx_addsy != NULL
8963       && OUTPUT_FLAVOR == bfd_target_elf_flavour
8964       && S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
8965     {
8966       value -= S_GET_VALUE (fixP->fx_addsy);
8967       if (value != 0 && ! fixP->fx_pcrel)
8968         {
8969           /* In this case, the bfd_install_relocation routine will
8970              incorrectly add the symbol value back in.  We just want
8971              the addend to appear in the object file.  */
8972           value -= S_GET_VALUE (fixP->fx_addsy);
8973         }
8974     }
8975 #endif
8976
8977   fixP->fx_addnumber = value;   /* Remember value for tc_gen_reloc */
8978
8979   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
8980     fixP->fx_done = 1;
8981
8982   switch (fixP->fx_r_type)
8983     {
8984     case BFD_RELOC_MIPS_JMP:
8985     case BFD_RELOC_HI16:
8986     case BFD_RELOC_HI16_S:
8987     case BFD_RELOC_MIPS_GPREL:
8988     case BFD_RELOC_MIPS_LITERAL:
8989     case BFD_RELOC_MIPS_CALL16:
8990     case BFD_RELOC_MIPS_GOT16:
8991     case BFD_RELOC_MIPS_GPREL32:
8992     case BFD_RELOC_MIPS_GOT_HI16:
8993     case BFD_RELOC_MIPS_GOT_LO16:
8994     case BFD_RELOC_MIPS_CALL_HI16:
8995     case BFD_RELOC_MIPS_CALL_LO16:
8996     case BFD_RELOC_MIPS16_GPREL:
8997       if (fixP->fx_pcrel)
8998         as_bad_where (fixP->fx_file, fixP->fx_line,
8999                       "Invalid PC relative reloc");
9000       /* Nothing needed to do. The value comes from the reloc entry */
9001       break;
9002
9003     case BFD_RELOC_MIPS16_JMP:
9004       /* We currently always generate a reloc against a symbol, which
9005          means that we don't want an addend even if the symbol is
9006          defined.  */
9007       fixP->fx_addnumber = 0;
9008       break;
9009
9010     case BFD_RELOC_PCREL_HI16_S:
9011       /* The addend for this is tricky if it is internal, so we just
9012          do everything here rather than in bfd_install_relocation.  */
9013       if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
9014         {
9015           /* For an external symbol adjust by the address to make it
9016              pcrel_offset.  We use the address of the RELLO reloc
9017              which follows this one.  */
9018           value += (fixP->fx_next->fx_frag->fr_address
9019                     + fixP->fx_next->fx_where);
9020         }
9021       if (value & 0x8000)
9022         value += 0x10000;
9023       value >>= 16;
9024       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9025       if (target_big_endian)
9026         buf += 2;
9027       md_number_to_chars (buf, value, 2);
9028       break;
9029
9030     case BFD_RELOC_PCREL_LO16:
9031       /* The addend for this is tricky if it is internal, so we just
9032          do everything here rather than in bfd_install_relocation.  */
9033       if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
9034         value += fixP->fx_frag->fr_address + fixP->fx_where;
9035       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9036       if (target_big_endian)
9037         buf += 2;
9038       md_number_to_chars (buf, value, 2);
9039       break;
9040
9041     case BFD_RELOC_64:
9042       /* This is handled like BFD_RELOC_32, but we output a sign
9043          extended value if we are only 32 bits.  */
9044       if (fixP->fx_done
9045           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9046         {
9047           if (8 <= sizeof (valueT))
9048             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9049                                 value, 8);
9050           else
9051             {
9052               long w1, w2;
9053               long hiv;
9054
9055               w1 = w2 = fixP->fx_where;
9056               if (target_big_endian)
9057                 w1 += 4;
9058               else
9059                 w2 += 4;
9060               md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
9061               if ((value & 0x80000000) != 0)
9062                 hiv = 0xffffffff;
9063               else
9064                 hiv = 0;
9065               md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
9066             }
9067         }
9068       break;
9069
9070     case BFD_RELOC_32:
9071       /* If we are deleting this reloc entry, we must fill in the
9072          value now.  This can happen if we have a .word which is not
9073          resolved when it appears but is later defined.  We also need
9074          to fill in the value if this is an embedded PIC switch table
9075          entry.  */
9076       if (fixP->fx_done
9077           || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9078         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9079                             value, 4);
9080       break;
9081
9082     case BFD_RELOC_16:
9083       /* If we are deleting this reloc entry, we must fill in the
9084          value now.  */
9085       assert (fixP->fx_size == 2);
9086       if (fixP->fx_done)
9087         md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9088                             value, 2);
9089       break;
9090
9091     case BFD_RELOC_LO16:
9092       /* When handling an embedded PIC switch statement, we can wind
9093          up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
9094       if (fixP->fx_done)
9095         {
9096           if (value < -0x8000 || value > 0x7fff)
9097             as_bad_where (fixP->fx_file, fixP->fx_line,
9098                           "relocation overflow");
9099           buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9100           if (target_big_endian)
9101             buf += 2;
9102           md_number_to_chars (buf, value, 2);
9103         }
9104       break;
9105
9106     case BFD_RELOC_16_PCREL_S2:
9107       /*
9108        * We need to save the bits in the instruction since fixup_segment()
9109        * might be deleting the relocation entry (i.e., a branch within
9110        * the current segment).
9111        */
9112       if ((value & 0x3) != 0)
9113         as_bad_where (fixP->fx_file, fixP->fx_line,
9114                       "Branch to odd address (%lx)", value);
9115       value >>= 2;
9116
9117       /* update old instruction data */
9118       buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
9119       if (target_big_endian)
9120         insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
9121       else
9122         insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
9123
9124       if (value >= -0x8000 && value < 0x8000)
9125         insn |= value & 0xffff;
9126       else
9127         {
9128           /* The branch offset is too large.  If this is an
9129              unconditional branch, and we are not generating PIC code,
9130              we can convert it to an absolute jump instruction.  */
9131           if (mips_pic == NO_PIC
9132               && fixP->fx_done
9133               && fixP->fx_frag->fr_address >= text_section->vma
9134               && (fixP->fx_frag->fr_address
9135                   < text_section->vma + text_section->_raw_size)
9136               && ((insn & 0xffff0000) == 0x10000000      /* beq $0,$0 */
9137                   || (insn & 0xffff0000) == 0x04010000   /* bgez $0 */
9138                   || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
9139             {
9140               if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
9141                 insn = 0x0c000000;      /* jal */
9142               else
9143                 insn = 0x08000000;      /* j */
9144               fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
9145               fixP->fx_done = 0;
9146               fixP->fx_addsy = section_symbol (text_section);
9147               fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
9148             }
9149           else
9150             {
9151               /* FIXME.  It would be possible in principle to handle
9152                  conditional branches which overflow.  They could be
9153                  transformed into a branch around a jump.  This would
9154                  require setting up variant frags for each different
9155                  branch type.  The native MIPS assembler attempts to
9156                  handle these cases, but it appears to do it
9157                  incorrectly.  */
9158               as_bad_where (fixP->fx_file, fixP->fx_line,
9159                             "Branch out of range");
9160             }
9161         }
9162
9163       md_number_to_chars ((char *) buf, (valueT) insn, 4);
9164       break;
9165
9166     default:
9167       internalError ();
9168     }
9169
9170   return 1;
9171 }
9172
9173 #if 0
9174 void
9175 printInsn (oc)
9176      unsigned long oc;
9177 {
9178   const struct mips_opcode *p;
9179   int treg, sreg, dreg, shamt;
9180   short imm;
9181   const char *args;
9182   int i;
9183
9184   for (i = 0; i < NUMOPCODES; ++i)
9185     {
9186       p = &mips_opcodes[i];
9187       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
9188         {
9189           printf ("%08lx %s\t", oc, p->name);
9190           treg = (oc >> 16) & 0x1f;
9191           sreg = (oc >> 21) & 0x1f;
9192           dreg = (oc >> 11) & 0x1f;
9193           shamt = (oc >> 6) & 0x1f;
9194           imm = oc;
9195           for (args = p->args;; ++args)
9196             {
9197               switch (*args)
9198                 {
9199                 case '\0':
9200                   printf ("\n");
9201                   break;
9202
9203                 case ',':
9204                 case '(':
9205                 case ')':
9206                   printf ("%c", *args);
9207                   continue;
9208
9209                 case 'r':
9210                   assert (treg == sreg);
9211                   printf ("$%d,$%d", treg, sreg);
9212                   continue;
9213
9214                 case 'd':
9215                 case 'G':
9216                   printf ("$%d", dreg);
9217                   continue;
9218
9219                 case 't':
9220                 case 'E':
9221                   printf ("$%d", treg);
9222                   continue;
9223
9224                 case 'k':
9225                   printf ("0x%x", treg);
9226                   continue;
9227
9228                 case 'b':
9229                 case 's':
9230                   printf ("$%d", sreg);
9231                   continue;
9232
9233                 case 'a':
9234                   printf ("0x%08lx", oc & 0x1ffffff);
9235                   continue;
9236
9237                 case 'i':
9238                 case 'j':
9239                 case 'o':
9240                 case 'u':
9241                   printf ("%d", imm);
9242                   continue;
9243
9244                 case '<':
9245                 case '>':
9246                   printf ("$%d", shamt);
9247                   continue;
9248
9249                 default:
9250                   internalError ();
9251                 }
9252               break;
9253             }
9254           return;
9255         }
9256     }
9257   printf ("%08lx  UNDEFINED\n", oc);
9258 }
9259 #endif
9260
9261 static symbolS *
9262 get_symbol ()
9263 {
9264   int c;
9265   char *name;
9266   symbolS *p;
9267
9268   name = input_line_pointer;
9269   c = get_symbol_end ();
9270   p = (symbolS *) symbol_find_or_make (name);
9271   *input_line_pointer = c;
9272   return p;
9273 }
9274
9275 /* Align the current frag to a given power of two.  The MIPS assembler
9276    also automatically adjusts any preceding label.  */
9277
9278 static void
9279 mips_align (to, fill, label)
9280      int to;
9281      int fill;
9282      symbolS *label;
9283 {
9284   mips_emit_delays (false);
9285   frag_align (to, fill, 0);
9286   record_alignment (now_seg, to);
9287   if (label != NULL)
9288     {
9289       assert (S_GET_SEGMENT (label) == now_seg);
9290       label->sy_frag = frag_now;
9291       S_SET_VALUE (label, (valueT) frag_now_fix ());
9292     }
9293 }
9294
9295 /* Align to a given power of two.  .align 0 turns off the automatic
9296    alignment used by the data creating pseudo-ops.  */
9297
9298 static void
9299 s_align (x)
9300      int x;
9301 {
9302   register int temp;
9303   register long temp_fill;
9304   long max_alignment = 15;
9305
9306   /*
9307
9308     o  Note that the assembler pulls down any immediately preceeding label
9309        to the aligned address.
9310     o  It's not documented but auto alignment is reinstated by
9311        a .align pseudo instruction.
9312     o  Note also that after auto alignment is turned off the mips assembler
9313        issues an error on attempt to assemble an improperly aligned data item.
9314        We don't.
9315
9316     */
9317
9318   temp = get_absolute_expression ();
9319   if (temp > max_alignment)
9320     as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
9321   else if (temp < 0)
9322     {
9323       as_warn ("Alignment negative: 0 assumed.");
9324       temp = 0;
9325     }
9326   if (*input_line_pointer == ',')
9327     {
9328       input_line_pointer++;
9329       temp_fill = get_absolute_expression ();
9330     }
9331   else
9332     temp_fill = 0;
9333   if (temp)
9334     {
9335       auto_align = 1;
9336       mips_align (temp, (int) temp_fill,
9337                   insn_labels != NULL ? insn_labels->label : NULL);
9338     }
9339   else
9340     {
9341       auto_align = 0;
9342     }
9343
9344   demand_empty_rest_of_line ();
9345 }
9346
9347 void
9348 mips_flush_pending_output ()
9349 {
9350   mips_emit_delays (false);
9351   mips_clear_insn_labels ();
9352 }
9353
9354 static void
9355 s_change_sec (sec)
9356      int sec;
9357 {
9358   segT seg;
9359
9360   /* When generating embedded PIC code, we only use the .text, .lit8,
9361      .sdata and .sbss sections.  We change the .data and .rdata
9362      pseudo-ops to use .sdata.  */
9363   if (mips_pic == EMBEDDED_PIC
9364       && (sec == 'd' || sec == 'r'))
9365     sec = 's';
9366
9367 #ifdef OBJ_ELF
9368   /* The ELF backend needs to know that we are changing sections, so
9369      that .previous works correctly.  We could do something like check
9370      for a obj_section_change_hook macro, but that might be confusing
9371      as it would not be appropriate to use it in the section changing
9372      functions in read.c, since obj-elf.c intercepts those.  FIXME:
9373      This should be cleaner, somehow.  */
9374   obj_elf_section_change_hook ();
9375 #endif
9376
9377   mips_emit_delays (false);
9378   switch (sec)
9379     {
9380     case 't':
9381       s_text (0);
9382       break;
9383     case 'd':
9384       s_data (0);
9385       break;
9386     case 'b':
9387       subseg_set (bss_section, (subsegT) get_absolute_expression ());
9388       demand_empty_rest_of_line ();
9389       break;
9390
9391     case 'r':
9392       if (USE_GLOBAL_POINTER_OPT)
9393         {
9394           seg = subseg_new (RDATA_SECTION_NAME,
9395                             (subsegT) get_absolute_expression ());
9396           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9397             {
9398               bfd_set_section_flags (stdoutput, seg,
9399                                      (SEC_ALLOC
9400                                       | SEC_LOAD
9401                                       | SEC_READONLY
9402                                       | SEC_RELOC
9403                                       | SEC_DATA));
9404               if (strcmp (TARGET_OS, "elf") != 0)
9405                 bfd_set_section_alignment (stdoutput, seg, 4);
9406             }
9407           demand_empty_rest_of_line ();
9408         }
9409       else
9410         {
9411           as_bad ("No read only data section in this object file format");
9412           demand_empty_rest_of_line ();
9413           return;
9414         }
9415       break;
9416
9417     case 's':
9418       if (USE_GLOBAL_POINTER_OPT)
9419         {
9420           seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
9421           if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9422             {
9423               bfd_set_section_flags (stdoutput, seg,
9424                                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
9425                                      | SEC_DATA);
9426               if (strcmp (TARGET_OS, "elf") != 0)
9427                 bfd_set_section_alignment (stdoutput, seg, 4);
9428             }
9429           demand_empty_rest_of_line ();
9430           break;
9431         }
9432       else
9433         {
9434           as_bad ("Global pointers not supported; recompile -G 0");
9435           demand_empty_rest_of_line ();
9436           return;
9437         }
9438     }
9439
9440   auto_align = 1;
9441 }
9442
9443 void
9444 mips_enable_auto_align ()
9445 {
9446   auto_align = 1;
9447 }
9448
9449 static void
9450 s_cons (log_size)
9451      int log_size;
9452 {
9453   symbolS *label;
9454
9455   label = insn_labels != NULL ? insn_labels->label : NULL;
9456   mips_emit_delays (false);
9457   if (log_size > 0 && auto_align)
9458     mips_align (log_size, 0, label);
9459   mips_clear_insn_labels ();
9460   cons (1 << log_size);
9461 }
9462
9463 static void
9464 s_float_cons (type)
9465      int type;
9466 {
9467   symbolS *label;
9468
9469   label = insn_labels != NULL ? insn_labels->label : NULL;
9470
9471   mips_emit_delays (false);
9472
9473   if (auto_align)
9474     if (type == 'd')
9475       mips_align (3, 0, label);
9476     else
9477       mips_align (2, 0, label);
9478
9479   mips_clear_insn_labels ();
9480
9481   float_cons (type);
9482 }
9483
9484 /* Handle .globl.  We need to override it because on Irix 5 you are
9485    permitted to say
9486        .globl foo .text
9487    where foo is an undefined symbol, to mean that foo should be
9488    considered to be the address of a function.  */
9489
9490 static void
9491 s_mips_globl (x)
9492      int x;
9493 {
9494   char *name;
9495   int c;
9496   symbolS *symbolP;
9497   flagword flag;
9498
9499   name = input_line_pointer;
9500   c = get_symbol_end ();
9501   symbolP = symbol_find_or_make (name);
9502   *input_line_pointer = c;
9503   SKIP_WHITESPACE ();
9504
9505   /* On Irix 5, every global symbol that is not explicitly labelled as
9506      being a function is apparently labelled as being an object.  */
9507   flag = BSF_OBJECT;
9508
9509   if (! is_end_of_line[(unsigned char) *input_line_pointer])
9510     {
9511       char *secname;
9512       asection *sec;
9513
9514       secname = input_line_pointer;
9515       c = get_symbol_end ();
9516       sec = bfd_get_section_by_name (stdoutput, secname);
9517       if (sec == NULL)
9518         as_bad ("%s: no such section", secname);
9519       *input_line_pointer = c;
9520
9521       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
9522         flag = BSF_FUNCTION;
9523     }
9524
9525   symbolP->bsym->flags |= flag;
9526
9527   S_SET_EXTERNAL (symbolP);
9528   demand_empty_rest_of_line ();
9529 }
9530
9531 static void
9532 s_option (x)
9533      int x;
9534 {
9535   char *opt;
9536   char c;
9537
9538   opt = input_line_pointer;
9539   c = get_symbol_end ();
9540
9541   if (*opt == 'O')
9542     {
9543       /* FIXME: What does this mean?  */
9544     }
9545   else if (strncmp (opt, "pic", 3) == 0)
9546     {
9547       int i;
9548
9549       i = atoi (opt + 3);
9550       if (i == 0)
9551         mips_pic = NO_PIC;
9552       else if (i == 2)
9553         mips_pic = SVR4_PIC;
9554       else
9555         as_bad (".option pic%d not supported", i);
9556
9557       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
9558         {
9559           if (g_switch_seen && g_switch_value != 0)
9560             as_warn ("-G may not be used with SVR4 PIC code");
9561           g_switch_value = 0;
9562           bfd_set_gp_size (stdoutput, 0);
9563         }
9564     }
9565   else
9566     as_warn ("Unrecognized option \"%s\"", opt);
9567
9568   *input_line_pointer = c;
9569   demand_empty_rest_of_line ();
9570 }
9571
9572 /* This structure is used to hold a stack of .set values.  */
9573
9574 struct mips_option_stack
9575 {
9576   struct mips_option_stack *next;
9577   struct mips_set_options options;
9578 };
9579
9580 static struct mips_option_stack *mips_opts_stack;
9581
9582 /* Handle the .set pseudo-op.  */
9583
9584 static void
9585 s_mipsset (x)
9586      int x;
9587 {
9588   char *name = input_line_pointer, ch;
9589
9590   while (!is_end_of_line[(unsigned char) *input_line_pointer])
9591     input_line_pointer++;
9592   ch = *input_line_pointer;
9593   *input_line_pointer = '\0';
9594
9595   if (strcmp (name, "reorder") == 0)
9596     {
9597       if (mips_opts.noreorder && prev_nop_frag != NULL)
9598         {
9599           /* If we still have pending nops, we can discard them.  The
9600              usual nop handling will insert any that are still
9601              needed. */
9602           prev_nop_frag->fr_fix -= (prev_nop_frag_holds
9603                                     * (mips_opts.mips16 ? 2 : 4));
9604           prev_nop_frag = NULL;
9605         }
9606       mips_opts.noreorder = 0;
9607     }
9608   else if (strcmp (name, "noreorder") == 0)
9609     {
9610       mips_emit_delays (true);
9611       mips_opts.noreorder = 1;
9612       mips_any_noreorder = 1;
9613     }
9614   else if (strcmp (name, "at") == 0)
9615     {
9616       mips_opts.noat = 0;
9617     }
9618   else if (strcmp (name, "noat") == 0)
9619     {
9620       mips_opts.noat = 1;
9621     }
9622   else if (strcmp (name, "macro") == 0)
9623     {
9624       mips_opts.warn_about_macros = 0;
9625     }
9626   else if (strcmp (name, "nomacro") == 0)
9627     {
9628       if (mips_opts.noreorder == 0)
9629         as_bad ("`noreorder' must be set before `nomacro'");
9630       mips_opts.warn_about_macros = 1;
9631     }
9632   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
9633     {
9634       mips_opts.nomove = 0;
9635     }
9636   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
9637     {
9638       mips_opts.nomove = 1;
9639     }
9640   else if (strcmp (name, "bopt") == 0)
9641     {
9642       mips_opts.nobopt = 0;
9643     }
9644   else if (strcmp (name, "nobopt") == 0)
9645     {
9646       mips_opts.nobopt = 1;
9647     }
9648   else if (strcmp (name, "mips16") == 0
9649            || strcmp (name, "MIPS-16") == 0)
9650     mips_opts.mips16 = 1;
9651   else if (strcmp (name, "nomips16") == 0
9652            || strcmp (name, "noMIPS-16") == 0)
9653     mips_opts.mips16 = 0;
9654   else if (strncmp (name, "mips", 4) == 0)
9655     {
9656       int isa;
9657
9658       /* Permit the user to change the ISA on the fly.  Needless to
9659          say, misuse can cause serious problems.  */
9660       isa = atoi (name + 4);
9661       if (isa == 0)
9662         mips_opts.isa = file_mips_isa;
9663       else if (isa < 1 || isa > 4)
9664         as_bad ("unknown ISA level");
9665       else
9666         mips_opts.isa = isa;
9667     }
9668   else if (strcmp (name, "autoextend") == 0)
9669     mips_opts.noautoextend = 0;
9670   else if (strcmp (name, "noautoextend") == 0)
9671     mips_opts.noautoextend = 1;
9672   else if (strcmp (name, "push") == 0)
9673     {
9674       struct mips_option_stack *s;
9675
9676       s = (struct mips_option_stack *) xmalloc (sizeof *s);
9677       s->next = mips_opts_stack;
9678       s->options = mips_opts;
9679       mips_opts_stack = s;
9680     }
9681   else if (strcmp (name, "pop") == 0)
9682     {
9683       struct mips_option_stack *s;
9684
9685       s = mips_opts_stack;
9686       if (s == NULL)
9687         as_bad (".set pop with no .set push");
9688       else
9689         {
9690           /* If we're changing the reorder mode we need to handle
9691              delay slots correctly.  */
9692           if (s->options.noreorder && ! mips_opts.noreorder)
9693             mips_emit_delays (true);
9694           else if (! s->options.noreorder && mips_opts.noreorder)
9695             {
9696               if (prev_nop_frag != NULL)
9697                 {
9698                   prev_nop_frag->fr_fix -= (prev_nop_frag_holds
9699                                             * (mips_opts.mips16 ? 2 : 4));
9700                   prev_nop_frag = NULL;
9701                 }
9702             }
9703
9704           mips_opts = s->options;
9705           mips_opts_stack = s->next;
9706           free (s);
9707         }
9708     }
9709   else
9710     {
9711       as_warn ("Tried to set unrecognized symbol: %s\n", name);
9712     }
9713   *input_line_pointer = ch;
9714   demand_empty_rest_of_line ();
9715 }
9716
9717 /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
9718    .option pic2.  It means to generate SVR4 PIC calls.  */
9719
9720 static void
9721 s_abicalls (ignore)
9722      int ignore;
9723 {
9724   mips_pic = SVR4_PIC;
9725   if (USE_GLOBAL_POINTER_OPT)
9726     {
9727       if (g_switch_seen && g_switch_value != 0)
9728         as_warn ("-G may not be used with SVR4 PIC code");
9729       g_switch_value = 0;
9730     }
9731   bfd_set_gp_size (stdoutput, 0);
9732   demand_empty_rest_of_line ();
9733 }
9734
9735 /* Handle the .cpload pseudo-op.  This is used when generating SVR4
9736    PIC code.  It sets the $gp register for the function based on the
9737    function address, which is in the register named in the argument.
9738    This uses a relocation against _gp_disp, which is handled specially
9739    by the linker.  The result is:
9740         lui     $gp,%hi(_gp_disp)
9741         addiu   $gp,$gp,%lo(_gp_disp)
9742         addu    $gp,$gp,.cpload argument
9743    The .cpload argument is normally $25 == $t9.  */
9744
9745 static void
9746 s_cpload (ignore)
9747      int ignore;
9748 {
9749   expressionS ex;
9750   int icnt = 0;
9751
9752   /* If we are not generating SVR4 PIC code, .cpload is ignored.  */
9753   if (mips_pic != SVR4_PIC)
9754     {
9755       s_ignore (0);
9756       return;
9757     }
9758
9759   /* .cpload should be a in .set noreorder section.  */
9760   if (mips_opts.noreorder == 0)
9761     as_warn (".cpload not in noreorder section");
9762
9763   ex.X_op = O_symbol;
9764   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
9765   ex.X_op_symbol = NULL;
9766   ex.X_add_number = 0;
9767
9768   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
9769   ex.X_add_symbol->bsym->flags |= BSF_OBJECT;
9770
9771   macro_build_lui ((char *) NULL, &icnt, &ex, GP);
9772   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
9773                (int) BFD_RELOC_LO16);
9774
9775   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
9776                GP, GP, tc_get_register (0));
9777
9778   demand_empty_rest_of_line ();
9779 }
9780
9781 /* Handle the .cprestore pseudo-op.  This stores $gp into a given
9782    offset from $sp.  The offset is remembered, and after making a PIC
9783    call $gp is restored from that location.  */
9784
9785 static void
9786 s_cprestore (ignore)
9787      int ignore;
9788 {
9789   expressionS ex;
9790   int icnt = 0;
9791
9792   /* If we are not generating SVR4 PIC code, .cprestore is ignored.  */
9793   if (mips_pic != SVR4_PIC)
9794     {
9795       s_ignore (0);
9796       return;
9797     }
9798
9799   mips_cprestore_offset = get_absolute_expression ();
9800
9801   ex.X_op = O_constant;
9802   ex.X_add_symbol = NULL;
9803   ex.X_op_symbol = NULL;
9804   ex.X_add_number = mips_cprestore_offset;
9805
9806   macro_build ((char *) NULL, &icnt, &ex,
9807                mips_opts.isa < 3 ? "sw" : "sd",
9808                "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
9809
9810   demand_empty_rest_of_line ();
9811 }
9812
9813 /* Handle the .gpword pseudo-op.  This is used when generating PIC
9814    code.  It generates a 32 bit GP relative reloc.  */
9815
9816 static void
9817 s_gpword (ignore)
9818      int ignore;
9819 {
9820   symbolS *label;
9821   expressionS ex;
9822   char *p;
9823
9824   /* When not generating PIC code, this is treated as .word.  */
9825   if (mips_pic != SVR4_PIC)
9826     {
9827       s_cons (2);
9828       return;
9829     }
9830
9831   label = insn_labels != NULL ? insn_labels->label : NULL;
9832   mips_emit_delays (true);
9833   if (auto_align)
9834     mips_align (2, 0, label);
9835   mips_clear_insn_labels ();
9836
9837   expression (&ex);
9838
9839   if (ex.X_op != O_symbol || ex.X_add_number != 0)
9840     {
9841       as_bad ("Unsupported use of .gpword");
9842       ignore_rest_of_line ();
9843     }
9844
9845   p = frag_more (4);
9846   md_number_to_chars (p, (valueT) 0, 4);
9847   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
9848                BFD_RELOC_MIPS_GPREL32);
9849
9850   demand_empty_rest_of_line ();
9851 }
9852
9853 /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
9854    tables in SVR4 PIC code.  */
9855
9856 static void
9857 s_cpadd (ignore)
9858      int ignore;
9859 {
9860   int icnt = 0;
9861   int reg;
9862
9863   /* This is ignored when not generating SVR4 PIC code.  */
9864   if (mips_pic != SVR4_PIC)
9865     {
9866       s_ignore (0);
9867       return;
9868     }
9869
9870   /* Add $gp to the register named as an argument.  */
9871   reg = tc_get_register (0);
9872   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9873                mips_opts.isa < 3 ? "addu" : "daddu",
9874                "d,v,t", reg, reg, GP);
9875
9876   demand_empty_rest_of_line ();  
9877 }
9878
9879 /* Handle the .insn pseudo-op.  This marks instruction labels in
9880    mips16 mode.  This permits the linker to handle them specially,
9881    such as generating jalx instructions when needed.  We also make
9882    them odd for the duration of the assembly, in order to generate the
9883    right sort of code.  We will make them even in the adjust_symtab
9884    routine, while leaving them marked.  This is convenient for the
9885    debugger and the disassembler.  The linker knows to make them odd
9886    again.  */
9887
9888 static void
9889 s_insn (ignore)
9890      int ignore;
9891 {
9892   if (mips_opts.mips16)
9893     mips16_mark_labels ();
9894
9895   demand_empty_rest_of_line ();
9896 }
9897
9898 /* Handle a .stabn directive.  We need these in order to mark a label
9899    as being a mips16 text label correctly.  Sometimes the compiler
9900    will emit a label, followed by a .stabn, and then switch sections.
9901    If the label and .stabn are in mips16 mode, then the label is
9902    really a mips16 text label.  */
9903
9904 static void
9905 s_mips_stab (type)
9906      int type;
9907 {
9908   if (type == 'n' && mips_opts.mips16)
9909     mips16_mark_labels ();
9910
9911   s_stab (type);
9912 }
9913
9914 /* Parse a register string into a number.  Called from the ECOFF code
9915    to parse .frame.  The argument is non-zero if this is the frame
9916    register, so that we can record it in mips_frame_reg.  */
9917
9918 int
9919 tc_get_register (frame)
9920      int frame;
9921 {
9922   int reg;
9923
9924   SKIP_WHITESPACE ();
9925   if (*input_line_pointer++ != '$')
9926     {
9927       as_warn ("expected `$'");
9928       reg = 0;
9929     }
9930   else if (isdigit ((unsigned char) *input_line_pointer))
9931     {
9932       reg = get_absolute_expression ();
9933       if (reg < 0 || reg >= 32)
9934         {
9935           as_warn ("Bad register number");
9936           reg = 0;
9937         }
9938     }
9939   else
9940     {
9941       if (strncmp (input_line_pointer, "fp", 2) == 0)
9942         reg = FP;
9943       else if (strncmp (input_line_pointer, "sp", 2) == 0)
9944         reg = SP;
9945       else if (strncmp (input_line_pointer, "gp", 2) == 0)
9946         reg = GP;
9947       else if (strncmp (input_line_pointer, "at", 2) == 0)
9948         reg = AT;
9949       else
9950         {
9951           as_warn ("Unrecognized register name");
9952           reg = 0;
9953         }
9954       input_line_pointer += 2;
9955     }
9956   if (frame)
9957     mips_frame_reg = reg != 0 ? reg : SP;
9958   return reg;
9959 }
9960
9961 valueT
9962 md_section_align (seg, addr)
9963      asection *seg;
9964      valueT addr;
9965 {
9966   int align = bfd_get_section_alignment (stdoutput, seg);
9967
9968 #ifdef OBJ_ELF
9969   /* We don't need to align ELF sections to the full alignment.
9970      However, Irix 5 may prefer that we align them at least to a 16
9971      byte boundary.  We don't bother to align the sections if we are
9972      targeted for an embedded system.  */
9973   if (strcmp (TARGET_OS, "elf") == 0)
9974     return addr;
9975   if (align > 4)
9976     align = 4;
9977 #endif
9978
9979   return ((addr + (1 << align) - 1) & (-1 << align));
9980 }
9981
9982 /* Utility routine, called from above as well.  If called while the
9983    input file is still being read, it's only an approximation.  (For
9984    example, a symbol may later become defined which appeared to be
9985    undefined earlier.)  */
9986
9987 static int
9988 nopic_need_relax (sym, before_relaxing)
9989      symbolS *sym;
9990      int before_relaxing;
9991 {
9992   if (sym == 0)
9993     return 0;
9994
9995   if (USE_GLOBAL_POINTER_OPT)
9996     {
9997       const char *symname;
9998       int change;
9999
10000       /* Find out whether this symbol can be referenced off the GP
10001          register.  It can be if it is smaller than the -G size or if
10002          it is in the .sdata or .sbss section.  Certain symbols can
10003          not be referenced off the GP, although it appears as though
10004          they can.  */
10005       symname = S_GET_NAME (sym);
10006       if (symname != (const char *) NULL
10007           && (strcmp (symname, "eprol") == 0
10008               || strcmp (symname, "etext") == 0
10009               || strcmp (symname, "_gp") == 0
10010               || strcmp (symname, "edata") == 0
10011               || strcmp (symname, "_fbss") == 0
10012               || strcmp (symname, "_fdata") == 0
10013               || strcmp (symname, "_ftext") == 0
10014               || strcmp (symname, "end") == 0
10015               || strcmp (symname, "_gp_disp") == 0))
10016         change = 1;
10017       else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
10018                && (0
10019 #ifndef NO_ECOFF_DEBUGGING
10020                    || (sym->ecoff_extern_size != 0
10021                        && sym->ecoff_extern_size <= g_switch_value)
10022 #endif
10023                    /* We must defer this decision until after the whole
10024                       file has been read, since there might be a .extern
10025                       after the first use of this symbol.  */
10026                    || (before_relaxing
10027 #ifndef NO_ECOFF_DEBUGGING
10028                        && sym->ecoff_extern_size == 0
10029 #endif
10030                        && S_GET_VALUE (sym) == 0)
10031                    || (S_GET_VALUE (sym) != 0
10032                        && S_GET_VALUE (sym) <= g_switch_value)))
10033         change = 0;
10034       else
10035         {
10036           const char *segname;
10037
10038           segname = segment_name (S_GET_SEGMENT (sym));
10039           assert (strcmp (segname, ".lit8") != 0
10040                   && strcmp (segname, ".lit4") != 0);
10041           change = (strcmp (segname, ".sdata") != 0
10042                     && strcmp (segname, ".sbss") != 0);
10043         }
10044       return change;
10045     }
10046   else
10047     /* We are not optimizing for the GP register.  */
10048     return 1;
10049 }
10050
10051 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
10052    extended opcode.  SEC is the section the frag is in.  */
10053
10054 static int
10055 mips16_extended_frag (fragp, sec, stretch)
10056      fragS *fragp;
10057      asection *sec;
10058      long stretch;
10059 {
10060   int type;
10061   register const struct mips16_immed_operand *op;
10062   offsetT val;
10063   int mintiny, maxtiny;
10064   segT symsec;
10065
10066   if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
10067     return 0;
10068   if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
10069     return 1;
10070
10071   type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10072   op = mips16_immed_operands;
10073   while (op->type != type)
10074     {
10075       ++op;
10076       assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10077     }
10078
10079   if (op->unsp)
10080     {
10081       if (type == '<' || type == '>' || type == '[' || type == ']')
10082         {
10083           mintiny = 1;
10084           maxtiny = 1 << op->nbits;
10085         }
10086       else
10087         {
10088           mintiny = 0;
10089           maxtiny = (1 << op->nbits) - 1;
10090         }
10091     }
10092   else
10093     {
10094       mintiny = - (1 << (op->nbits - 1));
10095       maxtiny = (1 << (op->nbits - 1)) - 1;
10096     }
10097
10098   /* We can't call S_GET_VALUE here, because we don't want to lock in
10099      a particular frag address.  */
10100   if (fragp->fr_symbol->sy_value.X_op == O_constant)
10101     {
10102       val = (fragp->fr_symbol->sy_value.X_add_number
10103              + fragp->fr_symbol->sy_frag->fr_address);
10104       symsec = S_GET_SEGMENT (fragp->fr_symbol);
10105     }
10106   else if (fragp->fr_symbol->sy_value.X_op == O_symbol
10107            && (fragp->fr_symbol->sy_value.X_add_symbol->sy_value.X_op
10108                == O_constant))
10109     {
10110       val = (fragp->fr_symbol->sy_value.X_add_symbol->sy_value.X_add_number
10111              + fragp->fr_symbol->sy_value.X_add_symbol->sy_frag->fr_address
10112              + fragp->fr_symbol->sy_value.X_add_number
10113              + fragp->fr_symbol->sy_frag->fr_address);
10114       symsec = S_GET_SEGMENT (fragp->fr_symbol->sy_value.X_add_symbol);
10115     }
10116   else
10117     return 1;
10118
10119   if (op->pcrel)
10120     {
10121       addressT addr;
10122
10123       /* We won't have the section when we are called from
10124          mips_relax_frag.  However, we will always have been called
10125          from md_estimate_size_before_relax first.  If this is a
10126          branch to a different section, we mark it as such.  If SEC is
10127          NULL, and the frag is not marked, then it must be a branch to
10128          the same section.  */
10129       if (sec == NULL)
10130         {
10131           if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
10132             return 1;
10133         }
10134       else
10135         {
10136           if (symsec != sec)
10137             {
10138               fragp->fr_subtype =
10139                 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10140
10141               /* FIXME: We should support this, and let the linker
10142                  catch branches and loads that are out of range.  */
10143               as_bad_where (fragp->fr_file, fragp->fr_line,
10144                             "unsupported PC relative reference to different section");
10145
10146               return 1;
10147             }
10148         }
10149
10150       /* In this case, we know for sure that the symbol fragment is in
10151          the same section.  If the fr_address of the symbol fragment
10152          is greater then the address of this fragment we want to add
10153          in STRETCH in order to get a better estimate of the address.
10154          This particularly matters because of the shift bits.  */
10155       if (stretch != 0
10156           && fragp->fr_symbol->sy_frag->fr_address >= fragp->fr_address)
10157         {
10158           fragS *f;
10159
10160           /* Adjust stretch for any alignment frag.  Note that if have
10161              been expanding the earlier code, the symbol may be
10162              defined in what appears to be an earlier frag.  FIXME:
10163              This doesn't handle the fr_subtype field, which specifies
10164              a maximum number of bytes to skip when doing an
10165              alignment.  */
10166           for (f = fragp;
10167                f != NULL && f != fragp->fr_symbol->sy_frag;
10168                f = f->fr_next)
10169             {
10170               if (f->fr_type == rs_align || f->fr_type == rs_align_code)
10171                 {
10172                   if (stretch < 0)
10173                     stretch = - ((- stretch)
10174                                  & ~ ((1 << (int) f->fr_offset) - 1));
10175                   else
10176                     stretch &= ~ ((1 << (int) f->fr_offset) - 1);
10177                   if (stretch == 0)
10178                     break;
10179                 }
10180             }
10181           if (f != NULL)
10182             val += stretch;
10183         }
10184
10185       addr = fragp->fr_address + fragp->fr_fix;
10186
10187       /* The base address rules are complicated.  The base address of
10188          a branch is the following instruction.  The base address of a
10189          PC relative load or add is the instruction itself, but if it
10190          is in a delay slot (in which case it can not be extended) use
10191          the address of the instruction whose delay slot it is in.  */
10192       if (type == 'p' || type == 'q')
10193         {
10194           addr += 2;
10195
10196           /* If we are currently assuming that this frag should be
10197              extended, then, the current address is two bytes
10198              higher. */
10199           if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10200             addr += 2;
10201
10202           /* Ignore the low bit in the target, since it will be set
10203              for a text label.  */
10204           if ((val & 1) != 0)
10205             --val;
10206         }
10207       else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
10208         addr -= 4;
10209       else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
10210         addr -= 2;
10211
10212       val -= addr & ~ ((1 << op->shift) - 1);
10213
10214       /* Branch offsets have an implicit 0 in the lowest bit.  */
10215       if (type == 'p' || type == 'q')
10216         val /= 2;
10217
10218       /* If any of the shifted bits are set, we must use an extended
10219          opcode.  If the address depends on the size of this
10220          instruction, this can lead to a loop, so we arrange to always
10221          use an extended opcode.  We only check this when we are in
10222          the main relaxation loop, when SEC is NULL.  */
10223       if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
10224         {
10225           fragp->fr_subtype =
10226             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10227           return 1;
10228         }
10229
10230       /* If we are about to mark a frag as extended because the value
10231          is precisely maxtiny + 1, then there is a chance of an
10232          infinite loop as in the following code:
10233              la $4,foo
10234              .skip      1020
10235              .align     2
10236            foo:
10237          In this case when the la is extended, foo is 0x3fc bytes
10238          away, so the la can be shrunk, but then foo is 0x400 away, so
10239          the la must be extended.  To avoid this loop, we mark the
10240          frag as extended if it was small, and is about to become
10241          extended with a value of maxtiny + 1.  */
10242       if (val == ((maxtiny + 1) << op->shift)
10243           && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
10244           && sec == NULL)
10245         {
10246           fragp->fr_subtype =
10247             RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10248           return 1;
10249         }
10250     }
10251   else if (symsec != absolute_section && sec != NULL)
10252     as_bad_where (fragp->fr_file, fragp->fr_line, "unsupported relocation");
10253
10254   if ((val & ((1 << op->shift) - 1)) != 0
10255       || val < (mintiny << op->shift)
10256       || val > (maxtiny << op->shift))
10257     return 1;
10258   else
10259     return 0;
10260 }
10261
10262 /* Estimate the size of a frag before relaxing.  Unless this is the
10263    mips16, we are not really relaxing here, and the final size is
10264    encoded in the subtype information.  For the mips16, we have to
10265    decide whether we are using an extended opcode or not.  */
10266
10267 /*ARGSUSED*/
10268 int
10269 md_estimate_size_before_relax (fragp, segtype)
10270      fragS *fragp;
10271      asection *segtype;
10272 {
10273   int change;
10274
10275   if (RELAX_MIPS16_P (fragp->fr_subtype))
10276     {
10277       if (mips16_extended_frag (fragp, segtype, 0))
10278         {
10279           fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
10280           return 4;
10281         }
10282       else
10283         {
10284           fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
10285           return 2;
10286         }
10287     }
10288
10289   if (mips_pic == NO_PIC)
10290     {
10291       change = nopic_need_relax (fragp->fr_symbol, 0);
10292     }
10293   else if (mips_pic == SVR4_PIC)
10294     {
10295       symbolS *sym;
10296       asection *symsec;
10297
10298       sym = fragp->fr_symbol;
10299
10300       /* Handle the case of a symbol equated to another symbol.  */
10301       while (sym->sy_value.X_op == O_symbol
10302              && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
10303         {
10304           symbolS *n;
10305
10306           /* It's possible to get a loop here in a badly written
10307              program.  */
10308           n = sym->sy_value.X_add_symbol;
10309           if (n == sym)
10310             break;
10311           sym = n;
10312         }
10313
10314       symsec = S_GET_SEGMENT (sym);
10315
10316       /* This must duplicate the test in adjust_reloc_syms.  */
10317       change = (symsec != &bfd_und_section
10318                 && symsec != &bfd_abs_section
10319                 && ! bfd_is_com_section (symsec));
10320     }
10321   else
10322     abort ();
10323
10324   if (change)
10325     {
10326       /* Record the offset to the first reloc in the fr_opcode field.
10327          This lets md_convert_frag and tc_gen_reloc know that the code
10328          must be expanded.  */
10329       fragp->fr_opcode = (fragp->fr_literal
10330                           + fragp->fr_fix
10331                           - RELAX_OLD (fragp->fr_subtype)
10332                           + RELAX_RELOC1 (fragp->fr_subtype));
10333       /* FIXME: This really needs as_warn_where.  */
10334       if (RELAX_WARN (fragp->fr_subtype))
10335         as_warn ("AT used after \".set noat\" or macro used after \".set nomacro\"");
10336     }
10337
10338   if (! change)
10339     return 0;
10340   else
10341     return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
10342 }
10343
10344 /* This is called to see whether a reloc against a defined symbol
10345    should be converted into a reloc against a section.  Don't adjust
10346    MIPS16 jump relocations, so we don't have to worry about the format
10347    of the offset in the .o file.  Don't adjust relocations against
10348    mips16 symbols, so that the linker can find them if it needs to set
10349    up a stub.  */
10350
10351 int
10352 mips_fix_adjustable (fixp)
10353      fixS *fixp;
10354 {
10355   if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
10356     return 0;
10357   if (fixp->fx_addsy == NULL)
10358     return 1;
10359 #ifdef OBJ_ELF
10360   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10361       && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
10362       && fixp->fx_subsy == NULL)
10363     return 0;
10364 #endif
10365   return 1;
10366 }
10367
10368 /* Translate internal representation of relocation info to BFD target
10369    format.  */
10370
10371 arelent **
10372 tc_gen_reloc (section, fixp)
10373      asection *section;
10374      fixS *fixp;
10375 {
10376   static arelent *retval[4];
10377   arelent *reloc;
10378   bfd_reloc_code_real_type code;
10379
10380   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
10381   retval[1] = NULL;
10382
10383   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
10384   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
10385
10386   if (mips_pic == EMBEDDED_PIC
10387       && SWITCH_TABLE (fixp))
10388     {
10389       /* For a switch table entry we use a special reloc.  The addend
10390          is actually the difference between the reloc address and the
10391          subtrahend.  */
10392       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
10393       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
10394         as_fatal ("Double check fx_r_type in tc-mips.c:tc_gen_reloc");
10395       fixp->fx_r_type = BFD_RELOC_GPREL32;
10396     }
10397   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
10398     {
10399       /* We use a special addend for an internal RELLO reloc.  */
10400       if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
10401         reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
10402       else
10403         reloc->addend = fixp->fx_addnumber + reloc->address;
10404     }
10405   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
10406     {
10407       assert (fixp->fx_next != NULL
10408               && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
10409       /* We use a special addend for an internal RELHI reloc.  The
10410          reloc is relative to the RELLO; adjust the addend
10411          accordingly.  */
10412       if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
10413         reloc->addend = (fixp->fx_next->fx_frag->fr_address
10414                          + fixp->fx_next->fx_where
10415                          - S_GET_VALUE (fixp->fx_subsy));
10416       else
10417         reloc->addend = (fixp->fx_addnumber
10418                          + fixp->fx_next->fx_frag->fr_address
10419                          + fixp->fx_next->fx_where);
10420     }
10421   else if (fixp->fx_pcrel == 0)
10422     reloc->addend = fixp->fx_addnumber;
10423   else
10424     {
10425       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
10426         /* A gruesome hack which is a result of the gruesome gas reloc
10427            handling.  */
10428         reloc->addend = reloc->address;
10429       else
10430         reloc->addend = -reloc->address;
10431     }
10432
10433   /* If this is a variant frag, we may need to adjust the existing
10434      reloc and generate a new one.  */
10435   if (fixp->fx_frag->fr_opcode != NULL
10436       && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
10437           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10438           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
10439           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
10440           || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
10441           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
10442           || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
10443     {
10444       arelent *reloc2;
10445
10446       assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
10447
10448       /* If this is not the last reloc in this frag, then we have two
10449          GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
10450          CALL_HI16/CALL_LO16, both of which are being replaced.  Let
10451          the second one handle all of them.  */
10452       if (fixp->fx_next != NULL
10453           && fixp->fx_frag == fixp->fx_next->fx_frag)
10454         {
10455           assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
10456                    && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
10457                   || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
10458                       && (fixp->fx_next->fx_r_type
10459                           == BFD_RELOC_MIPS_GOT_LO16))
10460                   || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
10461                       && (fixp->fx_next->fx_r_type
10462                           == BFD_RELOC_MIPS_CALL_LO16)));
10463           retval[0] = NULL;
10464           return retval;
10465         }
10466
10467       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
10468       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
10469       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
10470       retval[2] = NULL;
10471       reloc2->sym_ptr_ptr = &fixp->fx_addsy->bsym;
10472       reloc2->address = (reloc->address
10473                          + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
10474                             - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
10475       reloc2->addend = fixp->fx_addnumber;
10476       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
10477       assert (reloc2->howto != NULL);
10478
10479       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
10480         {
10481           arelent *reloc3;
10482
10483           reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
10484           retval[3] = NULL;
10485           *reloc3 = *reloc2;
10486           reloc3->address += 4;
10487         }
10488
10489       if (mips_pic == NO_PIC)
10490         {
10491           assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
10492           fixp->fx_r_type = BFD_RELOC_HI16_S;
10493         }
10494       else if (mips_pic == SVR4_PIC)
10495         {
10496           switch (fixp->fx_r_type)
10497             {
10498             default:
10499               abort ();
10500             case BFD_RELOC_MIPS_GOT16:
10501               break;
10502             case BFD_RELOC_MIPS_CALL16:
10503             case BFD_RELOC_MIPS_GOT_LO16:
10504             case BFD_RELOC_MIPS_CALL_LO16:
10505               fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
10506               break;
10507             }
10508         }
10509       else
10510         abort ();
10511     }
10512
10513   /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
10514      fixup_segment converted a non-PC relative reloc into a PC
10515      relative reloc.  In such a case, we need to convert the reloc
10516      code.  */
10517   code = fixp->fx_r_type;
10518   if (fixp->fx_pcrel)
10519     {
10520       switch (code)
10521         {
10522         case BFD_RELOC_8:
10523           code = BFD_RELOC_8_PCREL;
10524           break;
10525         case BFD_RELOC_16:
10526           code = BFD_RELOC_16_PCREL;
10527           break;
10528         case BFD_RELOC_32:
10529           code = BFD_RELOC_32_PCREL;
10530           break;
10531         case BFD_RELOC_64:
10532           code = BFD_RELOC_64_PCREL;
10533           break;
10534         case BFD_RELOC_8_PCREL:
10535         case BFD_RELOC_16_PCREL:
10536         case BFD_RELOC_32_PCREL:
10537         case BFD_RELOC_64_PCREL:
10538         case BFD_RELOC_16_PCREL_S2:
10539         case BFD_RELOC_PCREL_HI16_S:
10540         case BFD_RELOC_PCREL_LO16:
10541           break;
10542         default:
10543           as_bad_where (fixp->fx_file, fixp->fx_line,
10544                         "Cannot make %s relocation PC relative",
10545                         bfd_get_reloc_code_name (code));
10546         }
10547     }
10548
10549   /* To support a PC relative reloc when generating embedded PIC code
10550      for ECOFF, we use a Cygnus extension.  We check for that here to
10551      make sure that we don't let such a reloc escape normally.  */
10552   if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10553       && code == BFD_RELOC_16_PCREL_S2
10554       && mips_pic != EMBEDDED_PIC)
10555     reloc->howto = NULL;
10556   else
10557     reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
10558
10559   if (reloc->howto == NULL)
10560     {
10561       as_bad_where (fixp->fx_file, fixp->fx_line,
10562                     "Can not represent %s relocation in this object file format",
10563                     bfd_get_reloc_code_name (code));
10564       retval[0] = NULL;
10565     }
10566
10567   return retval;
10568 }
10569
10570 /* Relax a machine dependent frag.  This returns the amount by which
10571    the current size of the frag should change.  */
10572
10573 int
10574 mips_relax_frag (fragp, stretch)
10575      fragS *fragp;
10576      long stretch;
10577 {
10578   if (! RELAX_MIPS16_P (fragp->fr_subtype))
10579     return 0;
10580
10581   if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
10582     {
10583       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10584         return 0;
10585       fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
10586       return 2;
10587     }
10588   else
10589     {
10590       if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10591         return 0;
10592       fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
10593       return -2;
10594     }
10595
10596   return 0;
10597 }
10598
10599 /* Convert a machine dependent frag.  */
10600
10601 void
10602 md_convert_frag (abfd, asec, fragp)
10603      bfd *abfd;
10604      segT asec;
10605      fragS *fragp;
10606 {
10607   int old, new;
10608   char *fixptr;
10609
10610   if (RELAX_MIPS16_P (fragp->fr_subtype))
10611     {
10612       int type;
10613       register const struct mips16_immed_operand *op;
10614       boolean small, ext;
10615       offsetT val;
10616       bfd_byte *buf;
10617       unsigned long insn;
10618       boolean use_extend;
10619       unsigned short extend;
10620
10621       type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10622       op = mips16_immed_operands;
10623       while (op->type != type)
10624         ++op;
10625
10626       if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
10627         {
10628           small = false;
10629           ext = true;
10630         }
10631       else
10632         {
10633           small = true;
10634           ext = false;
10635         }
10636
10637       resolve_symbol_value (fragp->fr_symbol, 1);
10638       val = S_GET_VALUE (fragp->fr_symbol);
10639       if (op->pcrel)
10640         {
10641           addressT addr;
10642
10643           addr = fragp->fr_address + fragp->fr_fix;
10644
10645           /* The rules for the base address of a PC relative reloc are
10646              complicated; see mips16_extended_frag.  */
10647           if (type == 'p' || type == 'q')
10648             {
10649               addr += 2;
10650               if (ext)
10651                 addr += 2;
10652               /* Ignore the low bit in the target, since it will be
10653                  set for a text label.  */
10654               if ((val & 1) != 0)
10655                 --val;
10656             }
10657           else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
10658             addr -= 4;
10659           else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
10660             addr -= 2;
10661
10662           addr &= ~ (addressT) ((1 << op->shift) - 1);
10663           val -= addr;
10664
10665           /* Make sure the section winds up with the alignment we have
10666              assumed.  */
10667           if (op->shift > 0)
10668             record_alignment (asec, op->shift);
10669         }
10670
10671       if (ext
10672           && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
10673               || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
10674         as_warn_where (fragp->fr_file, fragp->fr_line,
10675                        "extended instruction in delay slot");
10676
10677       buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
10678
10679       if (target_big_endian)
10680         insn = bfd_getb16 (buf);
10681       else
10682         insn = bfd_getl16 (buf);
10683
10684       mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
10685                     RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
10686                     small, ext, &insn, &use_extend, &extend);
10687
10688       if (use_extend)
10689         {
10690           md_number_to_chars (buf, 0xf000 | extend, 2);
10691           fragp->fr_fix += 2;
10692           buf += 2;
10693         }
10694
10695       md_number_to_chars (buf, insn, 2);
10696       fragp->fr_fix += 2;
10697       buf += 2;
10698     }
10699   else
10700     {
10701       if (fragp->fr_opcode == NULL)
10702         return;
10703
10704       old = RELAX_OLD (fragp->fr_subtype);
10705       new = RELAX_NEW (fragp->fr_subtype);
10706       fixptr = fragp->fr_literal + fragp->fr_fix;
10707
10708       if (new > 0)
10709         memcpy (fixptr - old, fixptr, new);
10710
10711       fragp->fr_fix += new - old;
10712     }
10713 }
10714
10715 #ifdef OBJ_ELF
10716
10717 /* This function is called after the relocs have been generated.
10718    We've been storing mips16 text labels as odd.  Here we convert them
10719    back to even for the convenience of the debugger.  */
10720
10721 void
10722 mips_frob_file_after_relocs ()
10723 {
10724   asymbol **syms;
10725   unsigned int count, i;
10726
10727   if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10728     return;
10729
10730   syms = bfd_get_outsymbols (stdoutput);
10731   count = bfd_get_symcount (stdoutput);
10732   for (i = 0; i < count; i++, syms++)
10733     {
10734       if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
10735           && ((*syms)->value & 1) != 0)
10736         {
10737           (*syms)->value &= ~1;
10738           /* If the symbol has an odd size, it was probably computed
10739              incorrectly, so adjust that as well.  */
10740           if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
10741             ++elf_symbol (*syms)->internal_elf_sym.st_size;
10742         }
10743     }
10744 }
10745
10746 #endif
10747
10748 /* This function is called whenever a label is defined.  It is used
10749    when handling branch delays; if a branch has a label, we assume we
10750    can not move it.  */
10751
10752 void
10753 mips_define_label (sym)
10754      symbolS *sym;
10755 {
10756   struct insn_label_list *l;
10757
10758   if (free_insn_labels == NULL)
10759     l = (struct insn_label_list *) xmalloc (sizeof *l);
10760   else
10761     {
10762       l = free_insn_labels;
10763       free_insn_labels = l->next;
10764     }
10765
10766   l->label = sym;
10767   l->next = insn_labels;
10768   insn_labels = l;
10769 }
10770 \f
10771 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10772
10773 /* Some special processing for a MIPS ELF file.  */
10774
10775 void
10776 mips_elf_final_processing ()
10777 {
10778   /* Write out the register information.  */
10779   if (! mips_64)
10780     {
10781       Elf32_RegInfo s;
10782
10783       s.ri_gprmask = mips_gprmask;
10784       s.ri_cprmask[0] = mips_cprmask[0];
10785       s.ri_cprmask[1] = mips_cprmask[1];
10786       s.ri_cprmask[2] = mips_cprmask[2];
10787       s.ri_cprmask[3] = mips_cprmask[3];
10788       /* The gp_value field is set by the MIPS ELF backend.  */
10789
10790       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
10791                                        ((Elf32_External_RegInfo *)
10792                                         mips_regmask_frag));
10793     }
10794   else
10795     {
10796       Elf64_Internal_RegInfo s;
10797
10798       s.ri_gprmask = mips_gprmask;
10799       s.ri_pad = 0;
10800       s.ri_cprmask[0] = mips_cprmask[0];
10801       s.ri_cprmask[1] = mips_cprmask[1];
10802       s.ri_cprmask[2] = mips_cprmask[2];
10803       s.ri_cprmask[3] = mips_cprmask[3];
10804       /* The gp_value field is set by the MIPS ELF backend.  */
10805
10806       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
10807                                        ((Elf64_External_RegInfo *)
10808                                         mips_regmask_frag));
10809     }
10810
10811   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
10812      sort of BFD interface for this.  */
10813   if (mips_any_noreorder)
10814     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
10815   if (mips_pic != NO_PIC)
10816     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
10817 }
10818
10819 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
10820 \f
10821 /* These functions should really be defined by the object file format,
10822    since they are related to debugging information.  However, this
10823    code has to work for the a.out format, which does not define them,
10824    so we provide simple versions here.  These don't actually generate
10825    any debugging information, but they do simple checking and someday
10826    somebody may make them useful.  */
10827
10828 typedef struct loc
10829 {
10830   struct loc *loc_next;
10831   unsigned long loc_fileno;
10832   unsigned long loc_lineno;
10833   unsigned long loc_offset;
10834   unsigned short loc_delta;
10835   unsigned short loc_count;
10836 #if 0
10837   fragS *loc_frag;
10838 #endif
10839 }
10840 locS;
10841
10842 typedef struct proc
10843   {
10844     struct proc *proc_next;
10845     struct symbol *proc_isym;
10846     struct symbol *proc_end;
10847     unsigned long proc_reg_mask;
10848     unsigned long proc_reg_offset;
10849     unsigned long proc_fpreg_mask;
10850     unsigned long proc_fpreg_offset;
10851     unsigned long proc_frameoffset;
10852     unsigned long proc_framereg;
10853     unsigned long proc_pcreg;
10854     locS *proc_iline;
10855     struct file *proc_file;
10856     int proc_index;
10857   }
10858 procS;
10859
10860 typedef struct file
10861   {
10862     struct file *file_next;
10863     unsigned long file_fileno;
10864     struct symbol *file_symbol;
10865     struct symbol *file_end;
10866     struct proc *file_proc;
10867     int file_numprocs;
10868   }
10869 fileS;
10870
10871 static struct obstack proc_frags;
10872 static procS *proc_lastP;
10873 static procS *proc_rootP;
10874 static int numprocs;
10875
10876 static void
10877 md_obj_begin ()
10878 {
10879   obstack_begin (&proc_frags, 0x2000);
10880 }
10881
10882 static void
10883 md_obj_end ()
10884 {
10885   /* check for premature end, nesting errors, etc */
10886   if (proc_lastP && proc_lastP->proc_end == NULL)
10887     as_warn ("missing `.end' at end of assembly");
10888 }
10889
10890 static long
10891 get_number ()
10892 {
10893   int negative = 0;
10894   long val = 0;
10895
10896   if (*input_line_pointer == '-')
10897     {
10898       ++input_line_pointer;
10899       negative = 1;
10900     }
10901   if (!isdigit (*input_line_pointer))
10902     as_bad ("Expected simple number.");
10903   if (input_line_pointer[0] == '0')
10904     {
10905       if (input_line_pointer[1] == 'x')
10906         {
10907           input_line_pointer += 2;
10908           while (isxdigit (*input_line_pointer))
10909             {
10910               val <<= 4;
10911               val |= hex_value (*input_line_pointer++);
10912             }
10913           return negative ? -val : val;
10914         }
10915       else
10916         {
10917           ++input_line_pointer;
10918           while (isdigit (*input_line_pointer))
10919             {
10920               val <<= 3;
10921               val |= *input_line_pointer++ - '0';
10922             }
10923           return negative ? -val : val;
10924         }
10925     }
10926   if (!isdigit (*input_line_pointer))
10927     {
10928       printf (" *input_line_pointer == '%c' 0x%02x\n",
10929               *input_line_pointer, *input_line_pointer);
10930       as_warn ("Invalid number");
10931       return -1;
10932     }
10933   while (isdigit (*input_line_pointer))
10934     {
10935       val *= 10;
10936       val += *input_line_pointer++ - '0';
10937     }
10938   return negative ? -val : val;
10939 }
10940
10941 /* The .file directive; just like the usual .file directive, but there
10942    is an initial number which is the ECOFF file index.  */
10943
10944 static void
10945 s_file (x)
10946      int x;
10947 {
10948   int line;
10949
10950   line = get_number ();
10951   s_app_file (0);
10952 }
10953
10954
10955 /* The .end directive.  */
10956
10957 static void
10958 s_mipsend (x)
10959      int x;
10960 {
10961   symbolS *p;
10962
10963   if (!is_end_of_line[(unsigned char) *input_line_pointer])
10964     {
10965       p = get_symbol ();
10966       demand_empty_rest_of_line ();
10967     }
10968   else
10969     p = NULL;
10970   if (now_seg != text_section)
10971     as_warn (".end not in text section");
10972   if (!proc_lastP)
10973     {
10974       as_warn (".end and no .ent seen yet.");
10975       return;
10976     }
10977
10978   if (p != NULL)
10979     {
10980       assert (S_GET_NAME (p));
10981       if (strcmp (S_GET_NAME (p), S_GET_NAME (proc_lastP->proc_isym)))
10982         as_warn (".end symbol does not match .ent symbol.");
10983     }
10984
10985   proc_lastP->proc_end = (symbolS *) 1;
10986 }
10987
10988 /* The .aent and .ent directives.  */
10989
10990 static void
10991 s_ent (aent)
10992      int aent;
10993 {
10994   int number = 0;
10995   procS *procP;
10996   symbolS *symbolP;
10997
10998   symbolP = get_symbol ();
10999   if (*input_line_pointer == ',')
11000     input_line_pointer++;
11001   SKIP_WHITESPACE ();
11002   if (isdigit (*input_line_pointer) || *input_line_pointer == '-')
11003     number = get_number ();
11004   if (now_seg != text_section)
11005     as_warn (".ent or .aent not in text section.");
11006
11007   if (!aent && proc_lastP && proc_lastP->proc_end == NULL)
11008     as_warn ("missing `.end'");
11009
11010   if (!aent)
11011     {
11012       procP = (procS *) obstack_alloc (&proc_frags, sizeof (*procP));
11013       procP->proc_isym = symbolP;
11014       procP->proc_reg_mask = 0;
11015       procP->proc_reg_offset = 0;
11016       procP->proc_fpreg_mask = 0;
11017       procP->proc_fpreg_offset = 0;
11018       procP->proc_frameoffset = 0;
11019       procP->proc_framereg = 0;
11020       procP->proc_pcreg = 0;
11021       procP->proc_end = NULL;
11022       procP->proc_next = NULL;
11023       if (proc_lastP)
11024         proc_lastP->proc_next = procP;
11025       else
11026         proc_rootP = procP;
11027       proc_lastP = procP;
11028       numprocs++;
11029     }
11030   demand_empty_rest_of_line ();
11031 }
11032
11033 /* The .frame directive.  */
11034
11035 #if 0
11036 static void
11037 s_frame (x)
11038      int x;
11039 {
11040   char str[100];
11041   symbolS *symP;
11042   int frame_reg;
11043   int frame_off;
11044   int pcreg;
11045
11046   frame_reg = tc_get_register (1);
11047   if (*input_line_pointer == ',')
11048     input_line_pointer++;
11049   frame_off = get_absolute_expression ();
11050   if (*input_line_pointer == ',')
11051     input_line_pointer++;
11052   pcreg = tc_get_register (0);
11053
11054   /* bob third eye */
11055   assert (proc_rootP);
11056   proc_rootP->proc_framereg = frame_reg;
11057   proc_rootP->proc_frameoffset = frame_off;
11058   proc_rootP->proc_pcreg = pcreg;
11059   /* bob macho .frame */
11060
11061   /* We don't have to write out a frame stab for unoptimized code. */
11062   if (!(frame_reg == FP && frame_off == 0))
11063     {
11064       if (!proc_lastP)
11065         as_warn ("No .ent for .frame to use.");
11066       (void) sprintf (str, "R%d;%d", frame_reg, frame_off);
11067       symP = symbol_new (str, N_VFP, 0, frag_now);
11068       S_SET_TYPE (symP, N_RMASK);
11069       S_SET_OTHER (symP, 0);
11070       S_SET_DESC (symP, 0);
11071       symP->sy_forward = proc_lastP->proc_isym;
11072       /* bob perhaps I should have used pseudo set */
11073     }
11074   demand_empty_rest_of_line ();
11075 }
11076 #endif
11077
11078 /* The .fmask and .mask directives.  */
11079
11080 #if 0
11081 static void
11082 s_mask (reg_type)
11083      char reg_type;
11084 {
11085   char str[100], *strP;
11086   symbolS *symP;
11087   int i;
11088   unsigned int mask;
11089   int off;
11090
11091   mask = get_number ();
11092   if (*input_line_pointer == ',')
11093     input_line_pointer++;
11094   off = get_absolute_expression ();
11095
11096   /* bob only for coff */
11097   assert (proc_rootP);
11098   if (reg_type == 'F')
11099     {
11100       proc_rootP->proc_fpreg_mask = mask;
11101       proc_rootP->proc_fpreg_offset = off;
11102     }
11103   else
11104     {
11105       proc_rootP->proc_reg_mask = mask;
11106       proc_rootP->proc_reg_offset = off;
11107     }
11108
11109   /* bob macho .mask + .fmask */
11110
11111   /* We don't have to write out a mask stab if no saved regs. */
11112   if (!(mask == 0))
11113     {
11114       if (!proc_lastP)
11115         as_warn ("No .ent for .mask to use.");
11116       strP = str;
11117       for (i = 0; i < 32; i++)
11118         {
11119           if (mask % 2)
11120             {
11121               sprintf (strP, "%c%d,", reg_type, i);
11122               strP += strlen (strP);
11123             }
11124           mask /= 2;
11125         }
11126       sprintf (strP, ";%d,", off);
11127       symP = symbol_new (str, N_RMASK, 0, frag_now);
11128       S_SET_TYPE (symP, N_RMASK);
11129       S_SET_OTHER (symP, 0);
11130       S_SET_DESC (symP, 0);
11131       symP->sy_forward = proc_lastP->proc_isym;
11132       /* bob perhaps I should have used pseudo set */
11133     }
11134 }
11135 #endif
11136
11137 /* The .loc directive.  */
11138
11139 #if 0
11140 static void
11141 s_loc (x)
11142      int x;
11143 {
11144   symbolS *symbolP;
11145   int lineno;
11146   int addroff;
11147
11148   assert (now_seg == text_section);
11149
11150   lineno = get_number ();
11151   addroff = frag_now_fix ();
11152
11153   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
11154   S_SET_TYPE (symbolP, N_SLINE);
11155   S_SET_OTHER (symbolP, 0);
11156   S_SET_DESC (symbolP, lineno);
11157   symbolP->sy_segment = now_seg;
11158 }
11159 #endif
This page took 0.667944 seconds and 4 git commands to generate.