home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gas / config / tc-mips.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  225KB  |  8,120 lines

  1. /* tc-mips.c -- assemble code for a MIPS chip.
  2.    Copyright (C) 1993, 1995, 1996 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. #include "libiberty.h"
  29.  
  30. #include <ctype.h>
  31.  
  32. #ifdef USE_STDARG
  33. #include <stdarg.h>
  34. #endif
  35. #ifdef USE_VARARGS
  36. #include <varargs.h>
  37. #endif
  38.  
  39. #include "opcode/mips.h"
  40.  
  41. #ifdef OBJ_MAYBE_ELF
  42. /* Clean up namespace so we can include obj-elf.h too.  */
  43. static int mips_output_flavor () { return OUTPUT_FLAVOR; }
  44. #undef OBJ_PROCESS_STAB
  45. #undef OUTPUT_FLAVOR
  46. #undef S_GET_ALIGN
  47. #undef S_GET_SIZE
  48. #undef S_SET_ALIGN
  49. #undef S_SET_SIZE
  50. #undef TARGET_SYMBOL_FIELDS
  51. #undef obj_frob_file
  52. #undef obj_frob_symbol
  53. #undef obj_pop_insert
  54. #undef obj_sec_sym_ok_for_reloc
  55.  
  56. #include "obj-elf.h"
  57. /* Fix any of them that we actually care about.  */
  58. #undef OUTPUT_FLAVOR
  59. #define OUTPUT_FLAVOR mips_output_flavor()
  60. #endif
  61.  
  62. #if defined (OBJ_ELF)
  63. #include "elf/mips.h"
  64. #endif
  65.  
  66. #ifndef ECOFF_DEBUGGING
  67. #define NO_ECOFF_DEBUGGING
  68. #define ECOFF_DEBUGGING 0
  69. #endif
  70.  
  71. #include "ecoff.h"
  72.  
  73. static char *mips_regmask_frag;
  74.  
  75. #define AT  1
  76. #define PIC_CALL_REG 25
  77. #define KT0 26
  78. #define KT1 27
  79. #define GP  28
  80. #define SP  29
  81. #define FP  30
  82. #define RA  31
  83.  
  84. extern int target_big_endian;
  85.  
  86. /* 1 is we should use the 64 bit MIPS ELF ABI, 0 if we should use the
  87.    32 bit ABI.  This has no meaning for ECOFF.  */
  88. static int mips_64;
  89.  
  90. /* The default target format to use.  */
  91. const char *
  92. mips_target_format ()
  93. {
  94.   switch (OUTPUT_FLAVOR)
  95.     {
  96.     case bfd_target_aout_flavour:
  97.       return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
  98.     case bfd_target_ecoff_flavour:
  99.       return target_big_endian ? "ecoff-bigmips" : "ecoff-littlemips";
  100.     case bfd_target_elf_flavour:
  101.       return (target_big_endian
  102.           ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
  103.           : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
  104.     default:
  105.       abort ();
  106.     }
  107. }
  108.  
  109. /* The name of the readonly data section.  */
  110. #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
  111.                 ? ".data" \
  112.                 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
  113.                 ? ".rdata" \
  114.                 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
  115.                 ? ".rodata" \
  116.                 : (abort (), ""))
  117.  
  118. /* These variables are filled in with the masks of registers used.
  119.    The object format code reads them and puts them in the appropriate
  120.    place.  */
  121. unsigned long mips_gprmask;
  122. unsigned long mips_cprmask[4];
  123.  
  124. /* MIPS ISA (Instruction Set Architecture) level (may be changed
  125.    temporarily using .set mipsN).  */
  126. static int mips_isa = -1;
  127.  
  128. /* MIPS ISA we are using for this output file.  */
  129. static int file_mips_isa;
  130.  
  131. /* The CPU type as a number: 2000, 3000, 4000, 4400, etc.  */
  132. static int mips_cpu = -1;
  133.  
  134. /* Whether the 4650 instructions (mad/madu) are permitted.  */
  135. static int mips_4650 = -1;
  136.  
  137. /* Whether the 4010 instructions are permitted.  */
  138. static int mips_4010 = -1;
  139.  
  140. /* Whether the 4100 MADD16 and DMADD16 are permitted. */
  141. static int mips_4100 = -1;
  142.  
  143. /* Whether the processor uses hardware interlocks, and thus does not
  144.    require nops to be inserted.  */
  145. static int interlocks = -1;
  146.  
  147. /* As with "interlocks" this is used by hardware that has FP
  148.    (co-processor) interlocks.  */
  149. static int cop_interlocks = -1;
  150.  
  151. /* MIPS PIC level.  */
  152.  
  153. enum mips_pic_level
  154. {
  155.   /* Do not generate PIC code.  */
  156.   NO_PIC,
  157.  
  158.   /* Generate PIC code as in Irix 4.  This is not implemented, and I'm
  159.      not sure what it is supposed to do.  */
  160.   IRIX4_PIC,
  161.  
  162.   /* Generate PIC code as in the SVR4 MIPS ABI.  */
  163.   SVR4_PIC,
  164.  
  165.   /* Generate PIC code without using a global offset table: the data
  166.      segment has a maximum size of 64K, all data references are off
  167.      the $gp register, and all text references are PC relative.  This
  168.      is used on some embedded systems.  */
  169.   EMBEDDED_PIC
  170. };
  171.  
  172. static enum mips_pic_level mips_pic;
  173.  
  174. /* 1 if we should generate 32 bit offsets from the GP register in
  175.    SVR4_PIC mode.  Currently has no meaning in other modes.  */
  176. static int mips_big_got;
  177.  
  178. /* 1 if trap instructions should used for overflow rather than break
  179.    instructions.  */
  180. static int mips_trap;
  181.  
  182. static int mips_warn_about_macros;
  183. static int mips_noreorder;
  184. static int mips_any_noreorder;
  185. static int mips_nomove;
  186. static int mips_noat;
  187. static int mips_nobopt;
  188.  
  189. /* The size of the small data section.  */
  190. static int g_switch_value = 8;
  191. /* Whether the -G option was used.  */
  192. static int g_switch_seen = 0;
  193.  
  194. #define N_RMASK 0xc4
  195. #define N_VFP   0xd4
  196.  
  197. /* If we can determine in advance that GP optimization won't be
  198.    possible, we can skip the relaxation stuff that tries to produce
  199.    GP-relative references.  This makes delay slot optimization work
  200.    better.
  201.  
  202.    This function can only provide a guess, but it seems to work for
  203.    gcc output.  If it guesses wrong, the only loss should be in
  204.    efficiency; it shouldn't introduce any bugs.
  205.  
  206.    I don't know if a fix is needed for the SVR4_PIC mode.  I've only
  207.    fixed it for the non-PIC mode.  KR 95/04/07  */
  208. static int nopic_need_relax PARAMS ((symbolS *));
  209.  
  210. /* handle of the OPCODE hash table */
  211. static struct hash_control *op_hash = NULL;
  212.  
  213. /* This array holds the chars that always start a comment.  If the
  214.     pre-processor is disabled, these aren't very useful */
  215. const char comment_chars[] = "#";
  216.  
  217. /* This array holds the chars that only start a comment at the beginning of
  218.    a line.  If the line seems to have the form '# 123 filename'
  219.    .line and .file directives will appear in the pre-processed output */
  220. /* Note that input_file.c hand checks for '#' at the beginning of the
  221.    first line of the input file.  This is because the compiler outputs
  222.    #NO_APP at the beginning of its output. */
  223. /* Also note that C style comments are always supported.  */
  224. const char line_comment_chars[] = "#";
  225.  
  226. /* This array holds machine specific line separator characters. */
  227. const char line_separator_chars[] = "";
  228.  
  229. /* Chars that can be used to separate mant from exp in floating point nums */
  230. const char EXP_CHARS[] = "eE";
  231.  
  232. /* Chars that mean this number is a floating point constant */
  233. /* As in 0f12.456 */
  234. /* or    0d1.2345e12 */
  235. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  236.  
  237. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  238.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  239.    but nothing is ideal around here.
  240.  */
  241.  
  242. static char *insn_error;
  243.  
  244. static int byte_order;
  245.  
  246. static int auto_align = 1;
  247.  
  248. /* Symbol labelling the current insn.  */
  249. static symbolS *insn_label;
  250.  
  251. /* When outputting SVR4 PIC code, the assembler needs to know the
  252.    offset in the stack frame from which to restore the $gp register.
  253.    This is set by the .cprestore pseudo-op, and saved in this
  254.    variable.  */
  255. static offsetT mips_cprestore_offset = -1;
  256.  
  257. /* This is the register which holds the stack frame, as set by the
  258.    .frame pseudo-op.  This is needed to implement .cprestore.  */
  259. static int mips_frame_reg = SP;
  260.  
  261. /* To output NOP instructions correctly, we need to keep information
  262.    about the previous two instructions.  */
  263.  
  264. /* Whether we are optimizing.  The default value of 2 means to remove
  265.    unneeded NOPs and swap branch instructions when possible.  A value
  266.    of 1 means to not swap branches.  A value of 0 means to always
  267.    insert NOPs.  */
  268. static int mips_optimize = 2;
  269.  
  270. /* Debugging level.  -g sets this to 2.  -gN sets this to N.  -g0 is
  271.    equivalent to seeing no -g option at all.  */
  272. static int mips_debug = 0;
  273.  
  274. /* The previous instruction.  */
  275. static struct mips_cl_insn prev_insn;
  276.  
  277. /* The instruction before prev_insn.  */
  278. static struct mips_cl_insn prev_prev_insn;
  279.  
  280. /* If we don't want information for prev_insn or prev_prev_insn, we
  281.    point the insn_mo field at this dummy integer.  */
  282. static const struct mips_opcode dummy_opcode = { 0 };
  283.  
  284. /* Non-zero if prev_insn is valid.  */
  285. static int prev_insn_valid;
  286.  
  287. /* The frag for the previous instruction.  */
  288. static struct frag *prev_insn_frag;
  289.  
  290. /* The offset into prev_insn_frag for the previous instruction.  */
  291. static long prev_insn_where;
  292.  
  293. /* The reloc for the previous instruction, if any.  */
  294. static fixS *prev_insn_fixp;
  295.  
  296. /* Non-zero if the previous instruction was in a delay slot.  */
  297. static int prev_insn_is_delay_slot;
  298.  
  299. /* Non-zero if the previous instruction was in a .set noreorder.  */
  300. static int prev_insn_unreordered;
  301.  
  302. /* Non-zero if the previous previous instruction was in a .set
  303.    noreorder.  */
  304. static int prev_prev_insn_unreordered;
  305.  
  306. /* For ECOFF and ELF, relocations against symbols are done in two
  307.    parts, with a HI relocation and a LO relocation.  Each relocation
  308.    has only 16 bits of space to store an addend.  This means that in
  309.    order for the linker to handle carries correctly, it must be able
  310.    to locate both the HI and the LO relocation.  This means that the
  311.    relocations must appear in order in the relocation table.
  312.  
  313.    In order to implement this, we keep track of each unmatched HI
  314.    relocation.  We then sort them so that they immediately precede the
  315.    corresponding LO relocation. */
  316.  
  317. struct mips_hi_fixup
  318. {
  319.   /* Next HI fixup.  */
  320.   struct mips_hi_fixup *next;
  321.   /* This fixup.  */
  322.   fixS *fixp;
  323.   /* The section this fixup is in.  */
  324.   segT seg;
  325. };
  326.  
  327. /* The list of unmatched HI relocs.  */
  328.  
  329. static struct mips_hi_fixup *mips_hi_fixup_list;
  330.  
  331. /* Since the MIPS does not have multiple forms of PC relative
  332.    instructions, we do not have to do relaxing as is done on other
  333.    platforms.  However, we do have to handle GP relative addressing
  334.    correctly, which turns out to be a similar problem.
  335.  
  336.    Every macro that refers to a symbol can occur in (at least) two
  337.    forms, one with GP relative addressing and one without.  For
  338.    example, loading a global variable into a register generally uses
  339.    a macro instruction like this:
  340.      lw $4,i
  341.    If i can be addressed off the GP register (this is true if it is in
  342.    the .sbss or .sdata section, or if it is known to be smaller than
  343.    the -G argument) this will generate the following instruction:
  344.      lw $4,i($gp)
  345.    This instruction will use a GPREL reloc.  If i can not be addressed
  346.    off the GP register, the following instruction sequence will be used:
  347.      lui $at,i
  348.      lw $4,i($at)
  349.    In this case the first instruction will have a HI16 reloc, and the
  350.    second reloc will have a LO16 reloc.  Both relocs will be against
  351.    the symbol i.
  352.  
  353.    The issue here is that we may not know whether i is GP addressable
  354.    until after we see the instruction that uses it.  Therefore, we
  355.    want to be able to choose the final instruction sequence only at
  356.    the end of the assembly.  This is similar to the way other
  357.    platforms choose the size of a PC relative instruction only at the
  358.    end of assembly.
  359.  
  360.    When generating position independent code we do not use GP
  361.    addressing in quite the same way, but the issue still arises as
  362.    external symbols and local symbols must be handled differently.
  363.  
  364.    We handle these issues by actually generating both possible
  365.    instruction sequences.  The longer one is put in a frag_var with
  366.    type rs_machine_dependent.  We encode what to do with the frag in
  367.    the subtype field.  We encode (1) the number of existing bytes to
  368.    replace, (2) the number of new bytes to use, (3) the offset from
  369.    the start of the existing bytes to the first reloc we must generate
  370.    (that is, the offset is applied from the start of the existing
  371.    bytes after they are replaced by the new bytes, if any), (4) the
  372.    offset from the start of the existing bytes to the second reloc,
  373.    (5) whether a third reloc is needed (the third reloc is always four
  374.    bytes after the second reloc), and (6) whether to warn if this
  375.    variant is used (this is sometimes needed if .set nomacro or .set
  376.    noat is in effect).  All these numbers are reasonably small.
  377.  
  378.    Generating two instruction sequences must be handled carefully to
  379.    ensure that delay slots are handled correctly.  Fortunately, there
  380.    are a limited number of cases.  When the second instruction
  381.    sequence is generated, append_insn is directed to maintain the
  382.    existing delay slot information, so it continues to apply to any
  383.    code after the second instruction sequence.  This means that the
  384.    second instruction sequence must not impose any requirements not
  385.    required by the first instruction sequence.
  386.  
  387.    These variant frags are then handled in functions called by the
  388.    machine independent code.  md_estimate_size_before_relax returns
  389.    the final size of the frag.  md_convert_frag sets up the final form
  390.    of the frag.  tc_gen_reloc adjust the first reloc and adds a second
  391.    one if needed.  */
  392. #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
  393.   ((relax_substateT) \
  394.    (((old) << 24) \
  395.     | ((new) << 16) \
  396.     | (((reloc1) + 64) << 9) \
  397.     | (((reloc2) + 64) << 2) \
  398.     | ((reloc3) ? (1 << 1) : 0) \
  399.     | ((warn) ? 1 : 0)))
  400. #define RELAX_OLD(i) (((i) >> 24) & 0xff)
  401. #define RELAX_NEW(i) (((i) >> 16) & 0xff)
  402. #define RELAX_RELOC1(i) ((bfd_vma)(((i) >> 9) & 0x7f) - 64)
  403. #define RELAX_RELOC2(i) ((bfd_vma)(((i) >> 2) & 0x7f) - 64)
  404. #define RELAX_RELOC3(i) (((i) >> 1) & 1)
  405. #define RELAX_WARN(i) ((i) & 1)
  406.  
  407. /* Prototypes for static functions.  */
  408.  
  409. #ifdef __STDC__
  410. #define internalError() \
  411.     as_fatal ("internal Error, line %d, %s", __LINE__, __FILE__)
  412. #else
  413. #define internalError() as_fatal ("MIPS internal Error");
  414. #endif
  415.  
  416. static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
  417.                   unsigned int reg, int fpr));
  418. static int reg_needs_delay PARAMS ((int));
  419. static void append_insn PARAMS ((char *place,
  420.                  struct mips_cl_insn * ip,
  421.                  expressionS * p,
  422.                  bfd_reloc_code_real_type r,
  423.                  boolean));
  424. static void mips_no_prev_insn PARAMS ((void));
  425. static void mips_emit_delays PARAMS ((void));
  426. #ifdef USE_STDARG
  427. static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
  428.                  const char *name, const char *fmt,
  429.                  ...));
  430. #else
  431. static void macro_build ();
  432. #endif
  433. static void macro_build_lui PARAMS ((char *place, int *counter,
  434.                      expressionS * ep, int regnum));
  435. static void set_at PARAMS ((int *counter, int reg, int unsignedp));
  436. static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
  437.                      expressionS *));
  438. static void load_register PARAMS ((int *, int, expressionS *, int));
  439. static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
  440. static void macro PARAMS ((struct mips_cl_insn * ip));
  441. #ifdef LOSING_COMPILER
  442. static void macro2 PARAMS ((struct mips_cl_insn * ip));
  443. #endif
  444. static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
  445. static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
  446. static void my_getExpression PARAMS ((expressionS * ep, char *str));
  447. static symbolS *get_symbol PARAMS ((void));
  448. static void mips_align PARAMS ((int to, int fill, symbolS *label));
  449. static void s_align PARAMS ((int));
  450. static void s_change_sec PARAMS ((int));
  451. static void s_cons PARAMS ((int));
  452. static void s_float_cons PARAMS ((int));
  453. static void s_mips_globl PARAMS ((int));
  454. static void s_option PARAMS ((int));
  455. static void s_mipsset PARAMS ((int));
  456. static void s_abicalls PARAMS ((int));
  457. static void s_cpload PARAMS ((int));
  458. static void s_cprestore PARAMS ((int));
  459. static void s_gpword PARAMS ((int));
  460. static void s_cpadd PARAMS ((int));
  461. static void md_obj_begin PARAMS ((void));
  462. static void md_obj_end PARAMS ((void));
  463. static long get_number PARAMS ((void));
  464. static void s_ent PARAMS ((int));
  465. static void s_mipsend PARAMS ((int));
  466. static void s_file PARAMS ((int));
  467.  
  468. /* Pseudo-op table.
  469.  
  470.    The following pseudo-ops from the Kane and Heinrich MIPS book
  471.    should be defined here, but are currently unsupported: .alias,
  472.    .galive, .gjaldef, .gjrlive, .livereg, .noalias.
  473.  
  474.    The following pseudo-ops from the Kane and Heinrich MIPS book are
  475.    specific to the type of debugging information being generated, and
  476.    should be defined by the object format: .aent, .begin, .bend,
  477.    .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
  478.    .vreg.
  479.  
  480.    The following pseudo-ops from the Kane and Heinrich MIPS book are
  481.    not MIPS CPU specific, but are also not specific to the object file
  482.    format.  This file is probably the best place to define them, but
  483.    they are not currently supported: .asm0, .endr, .lab, .repeat,
  484.    .struct, .weakext.  */
  485.  
  486. static const pseudo_typeS mips_pseudo_table[] =
  487. {
  488.  /* MIPS specific pseudo-ops.  */
  489.   {"option", s_option, 0},
  490.   {"set", s_mipsset, 0},
  491.   {"rdata", s_change_sec, 'r'},
  492.   {"sdata", s_change_sec, 's'},
  493.   {"livereg", s_ignore, 0},
  494.   {"abicalls", s_abicalls, 0},
  495.   {"cpload", s_cpload, 0},
  496.   {"cprestore", s_cprestore, 0},
  497.   {"gpword", s_gpword, 0},
  498.   {"cpadd", s_cpadd, 0},
  499.  
  500.  /* Relatively generic pseudo-ops that happen to be used on MIPS
  501.      chips.  */
  502.   {"asciiz", stringer, 1},
  503.   {"bss", s_change_sec, 'b'},
  504.   {"err", s_err, 0},
  505.   {"half", s_cons, 1},
  506.   {"dword", s_cons, 3},
  507.  
  508.  /* These pseudo-ops are defined in read.c, but must be overridden
  509.      here for one reason or another.  */
  510.   {"align", s_align, 0},
  511.   {"byte", s_cons, 0},
  512.   {"data", s_change_sec, 'd'},
  513.   {"double", s_float_cons, 'd'},
  514.   {"float", s_float_cons, 'f'},
  515.   {"globl", s_mips_globl, 0},
  516.   {"global", s_mips_globl, 0},
  517.   {"hword", s_cons, 1},
  518.   {"int", s_cons, 2},
  519.   {"long", s_cons, 2},
  520.   {"octa", s_cons, 4},
  521.   {"quad", s_cons, 3},
  522.   {"short", s_cons, 1},
  523.   {"single", s_float_cons, 'f'},
  524.   {"text", s_change_sec, 't'},
  525.   {"word", s_cons, 2},
  526.   { 0 },
  527. };
  528.  
  529. static const pseudo_typeS mips_nonecoff_pseudo_table[] = {
  530.  /* These pseudo-ops should be defined by the object file format.
  531.     However, a.out doesn't support them, so we have versions here.  */
  532.   {"aent", s_ent, 1},
  533.   {"bgnb", s_ignore, 0},
  534.   {"end", s_mipsend, 0},
  535.   {"endb", s_ignore, 0},
  536.   {"ent", s_ent, 0},
  537.   {"file", s_file, 0},
  538.   {"fmask", s_ignore, 'F'},
  539.   {"frame", s_ignore, 0},
  540.   {"loc", s_ignore, 0},
  541.   {"mask", s_ignore, 'R'},
  542.   {"verstamp", s_ignore, 0},
  543.   { 0 },
  544. };
  545.  
  546. extern void pop_insert PARAMS ((const pseudo_typeS *));
  547.  
  548. void
  549. mips_pop_insert ()
  550. {
  551.   pop_insert (mips_pseudo_table);
  552.   if (! ECOFF_DEBUGGING)
  553.     pop_insert (mips_nonecoff_pseudo_table);
  554. }
  555.  
  556. static char *expr_end;
  557.  
  558. /* Expressions which appear in instructions.  These are set by
  559.    mips_ip.  */
  560.  
  561. static expressionS imm_expr;
  562. static expressionS offset_expr;
  563.  
  564. /* Relocs associated with imm_expr and offset_expr.  */
  565.  
  566. static bfd_reloc_code_real_type imm_reloc;
  567. static bfd_reloc_code_real_type offset_reloc;
  568.  
  569. /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc.  */
  570.  
  571. static boolean imm_unmatched_hi;
  572.  
  573. /*
  574.  * This function is called once, at assembler startup time.  It should
  575.  * set up all the tables, etc. that the MD part of the assembler will need.
  576.  */
  577. void
  578. md_begin ()
  579. {
  580.   boolean ok = false;
  581.   register const char *retval = NULL;
  582.   register unsigned int i = 0;
  583.  
  584.   if (mips_isa == -1)
  585.     {
  586.       const char *cpu;
  587.       char *a = NULL;
  588.  
  589.       cpu = TARGET_CPU;
  590.       if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
  591.     {
  592.       a = xmalloc (sizeof TARGET_CPU);
  593.       strcpy (a, TARGET_CPU);
  594.       a[(sizeof TARGET_CPU) - 3] = '\0';
  595.       cpu = a;
  596.     }
  597.  
  598.       if (strcmp (cpu, "mips") == 0)
  599.     {
  600.       mips_isa = 1;
  601.       if (mips_cpu == -1)
  602.         mips_cpu = 3000;
  603.     }
  604.       else if (strcmp (cpu, "r6000") == 0
  605.            || strcmp (cpu, "mips2") == 0)
  606.     {
  607.       mips_isa = 2;
  608.       if (mips_cpu == -1)
  609.         mips_cpu = 6000;
  610.     }
  611.       else if (strcmp (cpu, "mips64") == 0
  612.            || strcmp (cpu, "r4000") == 0
  613.            || strcmp (cpu, "mips3") == 0)
  614.     {
  615.       mips_isa = 3;
  616.       if (mips_cpu == -1)
  617.         mips_cpu = 4000;
  618.     }
  619.       else if (strcmp (cpu, "r4400") == 0)
  620.     {
  621.       mips_isa = 3;
  622.       if (mips_cpu == -1)
  623.         mips_cpu = 4400;
  624.     }
  625.       else if (strcmp (cpu, "mips64orion") == 0
  626.            || strcmp (cpu, "r4600") == 0)
  627.     {
  628.       mips_isa = 3;
  629.       if (mips_cpu == -1)
  630.         mips_cpu = 4600;
  631.     }
  632.       else if (strcmp (cpu, "r4650") == 0)
  633.     {
  634.       mips_isa = 3;
  635.       if (mips_cpu == -1)
  636.         mips_cpu = 4650;
  637.       if (mips_4650 == -1)
  638.         mips_4650 = 1;
  639.     }
  640.       else if (strcmp (cpu, "mips64vr4300") == 0)
  641.     {
  642.       mips_isa = 3;
  643.       if (mips_cpu == -1)
  644.         mips_cpu = 4300;
  645.     }
  646.       else if (strcmp (cpu, "mips64vr4100") == 0)
  647.         {
  648.           mips_isa = 3;
  649.           if (mips_cpu == -1)
  650.             mips_cpu = 4100;
  651.           if (mips_4100 == -1)
  652.             mips_4100 = 1;
  653.         }
  654.       else if (strcmp (cpu, "r4010") == 0)
  655.     {
  656.       mips_isa = 2;
  657.       if (mips_cpu == -1)
  658.         mips_cpu = 4010;
  659.       if (mips_4010 == -1)
  660.         mips_4010 = 1;
  661.     }
  662.       else if (strcmp (cpu, "r8000") == 0
  663.            || strcmp (cpu, "mips4") == 0)
  664.     {
  665.       mips_isa = 4;
  666.       if (mips_cpu == -1)
  667.         mips_cpu = 8000;
  668.     }
  669.       else if (strcmp (cpu, "r10000") == 0)
  670.     {
  671.       mips_isa = 4;
  672.       if (mips_cpu == -1)
  673.         mips_cpu = 10000;
  674.     }
  675.       else
  676.     {
  677.       mips_isa = 1;
  678.       if (mips_cpu == -1)
  679.         mips_cpu = 3000;
  680.     }
  681.  
  682.       if (a != NULL)
  683.     free (a);
  684.     }
  685.  
  686.   if (mips_4650 < 0)
  687.     mips_4650 = 0;
  688.  
  689.   if (mips_4010 < 0)
  690.     mips_4010 = 0;
  691.  
  692.   if (mips_4100 < 0)
  693.     mips_4100 = 0;
  694.  
  695.   if (mips_4650 || mips_4010 || mips_4100 || mips_cpu == 4300)
  696.     interlocks = 1;
  697.   else
  698.     interlocks = 0;
  699.  
  700.   if (mips_cpu == 4300)
  701.     cop_interlocks = 1;
  702.   else
  703.     cop_interlocks = 0;
  704.  
  705.   if (mips_isa < 2 && mips_trap)
  706.     as_bad ("trap exception not supported at ISA 1");
  707.  
  708.   switch (mips_isa)
  709.     {
  710.     case 1:
  711.       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 3000);
  712.       break;
  713.     case 2:
  714.       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 6000);
  715.       break;
  716.     case 3:
  717.       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 4000);
  718.       break;
  719.     case 4:
  720.       ok = bfd_set_arch_mach (stdoutput, bfd_arch_mips, 8000);
  721.       break;
  722.     }
  723.   if (! ok)
  724.     as_warn ("Could not set architecture and machine");
  725.  
  726.   file_mips_isa = mips_isa;
  727.  
  728.   op_hash = hash_new ();
  729.  
  730.   for (i = 0; i < NUMOPCODES;)
  731.     {
  732.       const char *name = mips_opcodes[i].name;
  733.  
  734.       retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
  735.       if (retval != NULL)
  736.     {
  737.       fprintf (stderr, "internal error: can't hash `%s': %s\n",
  738.            mips_opcodes[i].name, retval);
  739.       as_fatal ("Broken assembler.  No assembly attempted.");
  740.     }
  741.       do
  742.     {
  743.       if (mips_opcodes[i].pinfo != INSN_MACRO
  744.           && ((mips_opcodes[i].match & mips_opcodes[i].mask)
  745.           != mips_opcodes[i].match))
  746.         {
  747.           fprintf (stderr, "internal error: bad opcode: `%s' \"%s\"\n",
  748.                mips_opcodes[i].name, mips_opcodes[i].args);
  749.           as_fatal ("Broken assembler.  No assembly attempted.");
  750.         }
  751.       ++i;
  752.     }
  753.       while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
  754.     }
  755.  
  756.   mips_no_prev_insn ();
  757.  
  758.   mips_gprmask = 0;
  759.   mips_cprmask[0] = 0;
  760.   mips_cprmask[1] = 0;
  761.   mips_cprmask[2] = 0;
  762.   mips_cprmask[3] = 0;
  763.  
  764.   /* set the default alignment for the text section (2**2) */
  765.   record_alignment (text_section, 2);
  766.  
  767.   if (USE_GLOBAL_POINTER_OPT)
  768.     bfd_set_gp_size (stdoutput, g_switch_value);
  769.  
  770.   if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
  771.     {
  772.       /* Sections must be aligned to 16 byte boundaries.  */
  773.       (void) bfd_set_section_alignment (stdoutput, text_section, 4);
  774.       (void) bfd_set_section_alignment (stdoutput, data_section, 4);
  775.       (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
  776.  
  777.       /* Create a .reginfo section for register masks and a .mdebug
  778.      section for debugging information.  */
  779.       {
  780.     segT seg;
  781.     subsegT subseg;
  782.     segT sec;
  783.  
  784.     seg = now_seg;
  785.     subseg = now_subseg;
  786.  
  787.     if (! mips_64)
  788.       {
  789.         sec = subseg_new (".reginfo", (subsegT) 0);
  790.  
  791.         /* The ABI says this section should be loaded so that the
  792.            running program can access it.  */
  793.         (void) bfd_set_section_flags (stdoutput, sec,
  794.                       (SEC_ALLOC | SEC_LOAD
  795.                        | SEC_READONLY | SEC_DATA));
  796.         (void) bfd_set_section_alignment (stdoutput, sec, 2);
  797.     
  798. #ifdef OBJ_ELF
  799.         mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
  800. #endif
  801.       }
  802.     else
  803.       {
  804.         /* The 64-bit ABI uses a .MIPS.options section rather than
  805.                .reginfo section.  */
  806.         sec = subseg_new (".MIPS.options", (subsegT) 0);
  807.         (void) bfd_set_section_flags (stdoutput, sec,
  808.                       (SEC_ALLOC | SEC_LOAD
  809.                        | SEC_READONLY | SEC_DATA));
  810.         (void) bfd_set_section_alignment (stdoutput, sec, 3);
  811.  
  812. #ifdef OBJ_ELF
  813.         /* Set up the option header.  */
  814.         {
  815.           Elf_Internal_Options opthdr;
  816.           char *f;
  817.  
  818.           opthdr.kind = ODK_REGINFO;
  819.           opthdr.size = (sizeof (Elf_External_Options)
  820.                  + sizeof (Elf64_External_RegInfo));
  821.           opthdr.section = 0;
  822.           opthdr.info = 0;
  823.           f = frag_more (sizeof (Elf_External_Options));
  824.           bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
  825.                          (Elf_External_Options *) f);
  826.  
  827.           mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
  828.         }
  829. #endif
  830.       }
  831.  
  832.     if (ECOFF_DEBUGGING)
  833.       {
  834.         sec = subseg_new (".mdebug", (subsegT) 0);
  835.         (void) bfd_set_section_flags (stdoutput, sec,
  836.                       SEC_HAS_CONTENTS | SEC_READONLY);
  837.         (void) bfd_set_section_alignment (stdoutput, sec, 2);
  838.       }
  839.  
  840.     subseg_set (seg, subseg);
  841.       }
  842.     }
  843.  
  844.   if (! ECOFF_DEBUGGING)
  845.     md_obj_begin ();
  846. }
  847.  
  848. void
  849. md_mips_end ()
  850. {
  851.   if (! ECOFF_DEBUGGING)
  852.     md_obj_end ();
  853. }
  854.  
  855. void
  856. md_assemble (str)
  857.      char *str;
  858. {
  859.   struct mips_cl_insn insn;
  860.  
  861.   imm_expr.X_op = O_absent;
  862.   imm_reloc = BFD_RELOC_UNUSED;
  863.   imm_unmatched_hi = false;
  864.   offset_expr.X_op = O_absent;
  865.   offset_reloc = BFD_RELOC_UNUSED;
  866.  
  867.   mips_ip (str, &insn);
  868.   if (insn_error)
  869.     {
  870.       as_bad ("%s `%s'", insn_error, str);
  871.       return;
  872.     }
  873.   if (insn.insn_mo->pinfo == INSN_MACRO)
  874.     {
  875.       macro (&insn);
  876.     }
  877.   else
  878.     {
  879.       if (imm_expr.X_op != O_absent)
  880.     append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
  881.              imm_unmatched_hi);
  882.       else if (offset_expr.X_op != O_absent)
  883.     append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
  884.       else
  885.     append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
  886.     }
  887. }
  888.  
  889. /* See whether instruction IP reads register REG.  If FPR is non-zero,
  890.    REG is a floating point register.  */
  891.  
  892. static int
  893. insn_uses_reg (ip, reg, fpr)
  894.      struct mips_cl_insn *ip;
  895.      unsigned int reg;
  896.      int fpr;
  897. {
  898.   /* Don't report on general register 0, since it never changes.  */
  899.   if (! fpr && reg == 0)
  900.     return 0;
  901.  
  902.   if (fpr)
  903.     {
  904.       /* If we are called with either $f0 or $f1, we must check $f0.
  905.      This is not optimal, because it will introduce an unnecessary
  906.      NOP between "lwc1 $f0" and "swc1 $f1".  To fix this we would
  907.      need to distinguish reading both $f0 and $f1 or just one of
  908.      them.  Note that we don't have to check the other way,
  909.      because there is no instruction that sets both $f0 and $f1
  910.      and requires a delay.  */
  911.       if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
  912.       && (((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS)
  913.           == (reg &~ (unsigned) 1)))
  914.     return 1;
  915.       if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
  916.       && (((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT)
  917.           == (reg &~ (unsigned) 1)))
  918.     return 1;
  919.     }
  920.   else
  921.     {
  922.       if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
  923.       && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
  924.     return 1;
  925.       if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
  926.       && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
  927.     return 1;
  928.     }
  929.  
  930.   return 0;
  931. }
  932.  
  933. /* This function returns true if modifying a register requires a
  934.    delay.  */
  935.  
  936. static int
  937. reg_needs_delay (reg)
  938.      int reg;
  939. {
  940.   unsigned long prev_pinfo;
  941.  
  942.   prev_pinfo = prev_insn.insn_mo->pinfo;
  943.   if (! mips_noreorder
  944.       && mips_isa < 4
  945.       && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
  946.       || (mips_isa < 2
  947.           && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
  948.     {
  949.       /* A load from a coprocessor or from memory.  All load
  950.      delays delay the use of general register rt for one
  951.      instruction on the r3000.  The r6000 and r4000 use
  952.      interlocks.  */
  953.       know (prev_pinfo & INSN_WRITE_GPR_T);
  954.       if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
  955.     return 1;
  956.     }
  957.  
  958.   return 0;
  959. }
  960.  
  961. /* Output an instruction.  PLACE is where to put the instruction; if
  962.    it is NULL, this uses frag_more to get room.  IP is the instruction
  963.    information.  ADDRESS_EXPR is an operand of the instruction to be
  964.    used with RELOC_TYPE.  */
  965.  
  966. static void
  967. append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
  968.      char *place;
  969.      struct mips_cl_insn *ip;
  970.      expressionS *address_expr;
  971.      bfd_reloc_code_real_type reloc_type;
  972.      boolean unmatched_hi;
  973. {
  974.   register unsigned long prev_pinfo, pinfo;
  975.   char *f;
  976.   fixS *fixp;
  977.   int nops = 0;
  978.  
  979.   prev_pinfo = prev_insn.insn_mo->pinfo;
  980.   pinfo = ip->insn_mo->pinfo;
  981.  
  982.   if (place == NULL && ! mips_noreorder)
  983.     {
  984.       /* If the previous insn required any delay slots, see if we need
  985.      to insert a NOP or two.  There are eight kinds of possible
  986.      hazards, of which an instruction can have at most one type.
  987.      (1) a load from memory delay
  988.      (2) a load from a coprocessor delay
  989.      (3) an unconditional branch delay
  990.      (4) a conditional branch delay
  991.      (5) a move to coprocessor register delay
  992.      (6) a load coprocessor register from memory delay
  993.      (7) a coprocessor condition code delay
  994.      (8) a HI/LO special register delay
  995.  
  996.      There are a lot of optimizations we could do that we don't.
  997.      In particular, we do not, in general, reorder instructions.
  998.      If you use gcc with optimization, it will reorder
  999.      instructions and generally do much more optimization then we
  1000.      do here; repeating all that work in the assembler would only
  1001.      benefit hand written assembly code, and does not seem worth
  1002.      it.  */
  1003.  
  1004.       /* This is how a NOP is emitted.  */
  1005. #define emit_nop() md_number_to_chars (frag_more (4), 0, 4)
  1006.  
  1007.       /* The previous insn might require a delay slot, depending upon
  1008.      the contents of the current insn.  */
  1009.       if (mips_isa < 4
  1010.       && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
  1011.                && ! cop_interlocks)
  1012.           || (mips_isa < 2
  1013.           && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
  1014.     {
  1015.       /* A load from a coprocessor or from memory.  All load
  1016.          delays delay the use of general register rt for one
  1017.          instruction on the r3000.  The r6000 and r4000 use
  1018.          interlocks.  */
  1019.       know (prev_pinfo & INSN_WRITE_GPR_T);
  1020.       if (mips_optimize == 0
  1021.           || insn_uses_reg (ip,
  1022.                 ((prev_insn.insn_opcode >> OP_SH_RT)
  1023.                  & OP_MASK_RT),
  1024.                 0))
  1025.         ++nops;
  1026.     }
  1027.       else if (mips_isa < 4
  1028.            && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
  1029.                     && ! cop_interlocks)
  1030.            || (mips_isa < 2
  1031.                && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
  1032.     {
  1033.       /* A generic coprocessor delay.  The previous instruction
  1034.          modified a coprocessor general or control register.  If
  1035.          it modified a control register, we need to avoid any
  1036.          coprocessor instruction (this is probably not always
  1037.          required, but it sometimes is).  If it modified a general
  1038.          register, we avoid using that register.
  1039.  
  1040.          On the r6000 and r4000 loading a coprocessor register
  1041.          from memory is interlocked, and does not require a delay.
  1042.  
  1043.          This case is not handled very well.  There is no special
  1044.          knowledge of CP0 handling, and the coprocessors other
  1045.          than the floating point unit are not distinguished at
  1046.          all.  */
  1047.       if (prev_pinfo & INSN_WRITE_FPR_T)
  1048.         {
  1049.           if (mips_optimize == 0
  1050.           || insn_uses_reg (ip,
  1051.                     ((prev_insn.insn_opcode >> OP_SH_FT)
  1052.                      & OP_MASK_FT),
  1053.                     1))
  1054.         ++nops;
  1055.         }
  1056.       else if (prev_pinfo & INSN_WRITE_FPR_S)
  1057.         {
  1058.           if (mips_optimize == 0
  1059.           || insn_uses_reg (ip,
  1060.                     ((prev_insn.insn_opcode >> OP_SH_FS)
  1061.                      & OP_MASK_FS),
  1062.                     1))
  1063.         ++nops;
  1064.         }
  1065.       else
  1066.         {
  1067.           /* We don't know exactly what the previous instruction
  1068.          does.  If the current instruction uses a coprocessor
  1069.          register, we must insert a NOP.  If previous
  1070.          instruction may set the condition codes, and the
  1071.          current instruction uses them, we must insert two
  1072.          NOPS.  */
  1073.           if (mips_optimize == 0
  1074.           || ((prev_pinfo & INSN_WRITE_COND_CODE)
  1075.               && (pinfo & INSN_READ_COND_CODE)))
  1076.         nops += 2;
  1077.           else if (pinfo & INSN_COP)
  1078.         ++nops;
  1079.         }
  1080.     }
  1081.       else if (mips_isa < 4
  1082.            && (prev_pinfo & INSN_WRITE_COND_CODE)
  1083.                && ! cop_interlocks)
  1084.     {
  1085.       /* The previous instruction sets the coprocessor condition
  1086.          codes, but does not require a general coprocessor delay
  1087.          (this means it is a floating point comparison
  1088.          instruction).  If this instruction uses the condition
  1089.          codes, we need to insert a single NOP.  */
  1090.       if (mips_optimize == 0
  1091.           || (pinfo & INSN_READ_COND_CODE))
  1092.         ++nops;
  1093.     }
  1094.       else if (prev_pinfo & INSN_READ_LO)
  1095.     {
  1096.       /* The previous instruction reads the LO register; if the
  1097.          current instruction writes to the LO register, we must
  1098.          insert two NOPS.  The R4650, VR4100 and VR4300 have
  1099.          interlocks.  */
  1100.       if (! interlocks
  1101.           && (mips_optimize == 0
  1102.           || (pinfo & INSN_WRITE_LO)))
  1103.         nops += 2;
  1104.     }
  1105.       else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
  1106.     {
  1107.       /* The previous instruction reads the HI register; if the
  1108.          current instruction writes to the HI register, we must
  1109.          insert a NOP.  The R4650, VR4100 and VR4300 have
  1110.          interlocks.  */
  1111.       if (! interlocks
  1112.           && (mips_optimize == 0
  1113.           || (pinfo & INSN_WRITE_HI)))
  1114.         nops += 2;
  1115.     }
  1116.  
  1117.       /* There are two cases which require two intervening
  1118.      instructions: 1) setting the condition codes using a move to
  1119.      coprocessor instruction which requires a general coprocessor
  1120.      delay and then reading the condition codes 2) reading the HI
  1121.      or LO register and then writing to it (except on the R4650,
  1122.      VR4100, and VR4300 which have interlocks).  If we are not
  1123.      already emitting a NOP instruction, we must check for these
  1124.      cases compared to the instruction previous to the previous
  1125.      instruction.  */
  1126.       if (nops == 0
  1127.       && ((mips_isa < 4
  1128.            && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
  1129.            && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
  1130.            && (pinfo & INSN_READ_COND_CODE)
  1131.                && ! cop_interlocks)
  1132.           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
  1133.           && (pinfo & INSN_WRITE_LO)
  1134.           && ! interlocks)
  1135.           || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
  1136.           && (pinfo & INSN_WRITE_HI)
  1137.           && ! interlocks)))
  1138.     ++nops;
  1139.  
  1140.       /* If we are being given a nop instruction, don't bother with
  1141.      one of the nops we would otherwise output.  This will only
  1142.      happen when a nop instruction is used with mips_optimize set
  1143.      to 0.  */
  1144.       if (nops > 0 && ip->insn_opcode == 0)
  1145.     --nops;
  1146.  
  1147.       /* Now emit the right number of NOP instructions.  */
  1148.       if (nops > 0)
  1149.     {
  1150.       fragS *old_frag;
  1151.       unsigned long old_frag_offset;
  1152.       int i;
  1153.  
  1154.       old_frag = frag_now;
  1155.       old_frag_offset = frag_now_fix ();
  1156.  
  1157.       for (i = 0; i < nops; i++)
  1158.         emit_nop ();
  1159.  
  1160.       if (listing)
  1161.         {
  1162.           listing_prev_line ();
  1163.           /* We may be at the start of a variant frag.  In case we
  1164.                  are, make sure there is enough space for the frag
  1165.                  after the frags created by listing_prev_line.  The
  1166.                  argument to frag_grow here must be at least as large
  1167.                  as the argument to all other calls to frag_grow in
  1168.                  this file.  We don't have to worry about being in the
  1169.                  middle of a variant frag, because the variants insert
  1170.                  all needed nop instructions themselves.  */
  1171.           frag_grow (40);
  1172.         }
  1173.  
  1174.       if (insn_label != NULL)
  1175.         {
  1176.           assert (S_GET_SEGMENT (insn_label) == now_seg);
  1177.           insn_label->sy_frag = frag_now;
  1178.           S_SET_VALUE (insn_label, (valueT) frag_now_fix ());
  1179.         }
  1180.  
  1181. #ifndef NO_ECOFF_DEBUGGING
  1182.       if (ECOFF_DEBUGGING)
  1183.         ecoff_fix_loc (old_frag, old_frag_offset);
  1184. #endif
  1185.     }
  1186.     }
  1187.   
  1188.   if (place == NULL)
  1189.     f = frag_more (4);
  1190.   else
  1191.     f = place;
  1192.   fixp = NULL;
  1193.   if (address_expr != NULL)
  1194.     {
  1195.       if (address_expr->X_op == O_constant)
  1196.     {
  1197.       switch (reloc_type)
  1198.         {
  1199.         case BFD_RELOC_32:
  1200.           ip->insn_opcode |= address_expr->X_add_number;
  1201.           break;
  1202.  
  1203.         case BFD_RELOC_LO16:
  1204.           ip->insn_opcode |= address_expr->X_add_number & 0xffff;
  1205.           break;
  1206.  
  1207.         case BFD_RELOC_MIPS_JMP:
  1208.         case BFD_RELOC_16_PCREL_S2:
  1209.           goto need_reloc;
  1210.  
  1211.         default:
  1212.           internalError ();
  1213.         }
  1214.     }
  1215.       else
  1216.     {
  1217.       assert (reloc_type != BFD_RELOC_UNUSED);
  1218.     need_reloc:
  1219.       /* Don't generate a reloc if we are writing into a variant
  1220.          frag.  */
  1221.       if (place == NULL)
  1222.         {
  1223.           fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
  1224.                   address_expr,
  1225.                   reloc_type == BFD_RELOC_16_PCREL_S2,
  1226.                   reloc_type);
  1227.           if (unmatched_hi)
  1228.         {
  1229.           struct mips_hi_fixup *hi_fixup;
  1230.  
  1231.           assert (reloc_type == BFD_RELOC_HI16_S);
  1232.           hi_fixup = ((struct mips_hi_fixup *)
  1233.                   xmalloc (sizeof (struct mips_hi_fixup)));
  1234.           hi_fixup->fixp = fixp;
  1235.           hi_fixup->seg = now_seg;
  1236.           hi_fixup->next = mips_hi_fixup_list;
  1237.           mips_hi_fixup_list = hi_fixup;
  1238.         }
  1239.         }
  1240.     }
  1241.     }
  1242.  
  1243.   md_number_to_chars (f, ip->insn_opcode, 4);
  1244.  
  1245.   /* Update the register mask information.  */
  1246.   if (pinfo & INSN_WRITE_GPR_D)
  1247.     mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
  1248.   if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
  1249.     mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
  1250.   if (pinfo & INSN_READ_GPR_S)
  1251.     mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
  1252.   if (pinfo & INSN_WRITE_GPR_31)
  1253.     mips_gprmask |= 1 << 31;
  1254.   if (pinfo & INSN_WRITE_FPR_D)
  1255.     mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
  1256.   if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
  1257.     mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
  1258.   if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
  1259.     mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
  1260.   if ((pinfo & INSN_READ_FPR_R) != 0)
  1261.     mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
  1262.   if (pinfo & INSN_COP)
  1263.     {
  1264.       /* We don't keep enough information to sort these cases out.  */
  1265.     }
  1266.   /* Never set the bit for $0, which is always zero.  */
  1267.   mips_gprmask &=~ 1 << 0;
  1268.  
  1269.   if (place == NULL && ! mips_noreorder)
  1270.     {
  1271.       /* Filling the branch delay slot is more complex.  We try to
  1272.      switch the branch with the previous instruction, which we can
  1273.      do if the previous instruction does not set up a condition
  1274.      that the branch tests and if the branch is not itself the
  1275.      target of any branch.  */
  1276.       if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
  1277.       || (pinfo & INSN_COND_BRANCH_DELAY))
  1278.     {
  1279.       if (mips_optimize < 2
  1280.           /* If we have seen .set volatile or .set nomove, don't
  1281.          optimize.  */
  1282.           || mips_nomove != 0
  1283.           /* If we had to emit any NOP instructions, then we
  1284.          already know we can not swap.  */
  1285.           || nops != 0
  1286.           /* If we don't even know the previous insn, we can not
  1287.          swap. */
  1288.           || ! prev_insn_valid
  1289.           /* If the previous insn is already in a branch delay
  1290.          slot, then we can not swap.  */
  1291.           || prev_insn_is_delay_slot
  1292.           /* If the previous previous insn was in a .set
  1293.          noreorder, we can't swap.  Actually, the MIPS
  1294.          assembler will swap in this situation.  However, gcc
  1295.          configured -with-gnu-as will generate code like
  1296.            .set noreorder
  1297.            lw    $4,XXX
  1298.            .set    reorder
  1299.            INSN
  1300.            bne    $4,$0,foo
  1301.          in which we can not swap the bne and INSN.  If gcc is
  1302.          not configured -with-gnu-as, it does not output the
  1303.          .set pseudo-ops.  We don't have to check
  1304.          prev_insn_unreordered, because prev_insn_valid will
  1305.          be 0 in that case.  We don't want to use
  1306.          prev_prev_insn_valid, because we do want to be able
  1307.          to swap at the start of a function.  */
  1308.           || prev_prev_insn_unreordered
  1309.           /* If the branch is itself the target of a branch, we
  1310.          can not swap.  We cheat on this; all we check for is
  1311.          whether there is a label on this instruction.  If
  1312.          there are any branches to anything other than a
  1313.          label, users must use .set noreorder.  */
  1314.           || insn_label != NULL
  1315.           /* If the previous instruction is in a variant frag, we
  1316.          can not do the swap.  */
  1317.           || prev_insn_frag->fr_type == rs_machine_dependent
  1318.           /* If the branch reads the condition codes, we don't
  1319.          even try to swap, because in the sequence
  1320.            ctc1 $X,$31
  1321.            INSN
  1322.            INSN
  1323.            bc1t LABEL
  1324.          we can not swap, and I don't feel like handling that
  1325.          case.  */
  1326.           || (mips_isa < 4
  1327.           && (pinfo & INSN_READ_COND_CODE))
  1328.           /* We can not swap with an instruction that requires a
  1329.          delay slot, becase the target of the branch might
  1330.          interfere with that instruction.  */
  1331.           || (mips_isa < 4
  1332.           && (prev_pinfo
  1333.               & (INSN_LOAD_COPROC_DELAY
  1334.              | INSN_COPROC_MOVE_DELAY
  1335.              | INSN_WRITE_COND_CODE)))
  1336.           || (! interlocks
  1337.           && (prev_pinfo
  1338.               & (INSN_READ_LO
  1339.              | INSN_READ_HI)))
  1340.           || (mips_isa < 2
  1341.           && (prev_pinfo
  1342.               & (INSN_LOAD_MEMORY_DELAY
  1343.              | INSN_COPROC_MEMORY_DELAY)))
  1344.           /* We can not swap with a branch instruction.  */
  1345.           || (prev_pinfo
  1346.           & (INSN_UNCOND_BRANCH_DELAY
  1347.              | INSN_COND_BRANCH_DELAY
  1348.              | INSN_COND_BRANCH_LIKELY))
  1349.           /* We do not swap with a trap instruction, since it
  1350.          complicates trap handlers to have the trap
  1351.          instruction be in a delay slot.  */
  1352.           || (prev_pinfo & INSN_TRAP)
  1353.           /* If the branch reads a register that the previous
  1354.          instruction sets, we can not swap.  */
  1355.           || ((prev_pinfo & INSN_WRITE_GPR_T)
  1356.           && insn_uses_reg (ip,
  1357.                     ((prev_insn.insn_opcode >> OP_SH_RT)
  1358.                      & OP_MASK_RT),
  1359.                     0))
  1360.           || ((prev_pinfo & INSN_WRITE_GPR_D)
  1361.           && insn_uses_reg (ip,
  1362.                     ((prev_insn.insn_opcode >> OP_SH_RD)
  1363.                      & OP_MASK_RD),
  1364.                     0))
  1365.           /* If the branch writes a register that the previous
  1366.          instruction sets, we can not swap (we know that
  1367.          branches write only to RD or to $31).  */
  1368.           || ((prev_pinfo & INSN_WRITE_GPR_T)
  1369.           && (((pinfo & INSN_WRITE_GPR_D)
  1370.                && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
  1371.                == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
  1372.               || ((pinfo & INSN_WRITE_GPR_31)
  1373.               && (((prev_insn.insn_opcode >> OP_SH_RT)
  1374.                    & OP_MASK_RT)
  1375.                   == 31))))
  1376.           || ((prev_pinfo & INSN_WRITE_GPR_D)
  1377.           && (((pinfo & INSN_WRITE_GPR_D)
  1378.                && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
  1379.                == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
  1380.               || ((pinfo & INSN_WRITE_GPR_31)
  1381.               && (((prev_insn.insn_opcode >> OP_SH_RD)
  1382.                    & OP_MASK_RD)
  1383.                   == 31))))
  1384.           /* If the branch writes a register that the previous
  1385.          instruction reads, we can not swap (we know that
  1386.          branches only write to RD or to $31).  */
  1387.           || ((pinfo & INSN_WRITE_GPR_D)
  1388.           && insn_uses_reg (&prev_insn,
  1389.                     ((ip->insn_opcode >> OP_SH_RD)
  1390.                      & OP_MASK_RD),
  1391.                     0))
  1392.           || ((pinfo & INSN_WRITE_GPR_31)
  1393.           && insn_uses_reg (&prev_insn, 31, 0))
  1394.           /* If we are generating embedded PIC code, the branch
  1395.          might be expanded into a sequence which uses $at, so
  1396.          we can't swap with an instruction which reads it.  */
  1397.           || (mips_pic == EMBEDDED_PIC
  1398.           && insn_uses_reg (&prev_insn, AT, 0))
  1399.           /* If the previous previous instruction has a load
  1400.          delay, and sets a register that the branch reads, we
  1401.          can not swap.  */
  1402.           || (mips_isa < 4
  1403.           && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
  1404.               || (mips_isa < 2
  1405.               && (prev_prev_insn.insn_mo->pinfo
  1406.                   & INSN_LOAD_MEMORY_DELAY)))
  1407.           && insn_uses_reg (ip,
  1408.                     ((prev_prev_insn.insn_opcode >> OP_SH_RT)
  1409.                      & OP_MASK_RT),
  1410.                     0)))
  1411.         {
  1412.           /* We could do even better for unconditional branches to
  1413.          portions of this object file; we could pick up the
  1414.          instruction at the destination, put it in the delay
  1415.          slot, and bump the destination address.  */
  1416.           emit_nop ();
  1417.           /* Update the previous insn information.  */
  1418.           prev_prev_insn = *ip;
  1419.           prev_insn.insn_mo = &dummy_opcode;
  1420.         }
  1421.       else
  1422.         {
  1423.           char *prev_f;
  1424.           char temp[4];
  1425.  
  1426.           /* It looks like we can actually do the swap.  */
  1427.           prev_f = prev_insn_frag->fr_literal + prev_insn_where;
  1428.           memcpy (temp, prev_f, 4);
  1429.           memcpy (prev_f, f, 4);
  1430.           memcpy (f, temp, 4);
  1431.           if (prev_insn_fixp)
  1432.         {
  1433.           prev_insn_fixp->fx_frag = frag_now;
  1434.           prev_insn_fixp->fx_where = f - frag_now->fr_literal;
  1435.         }
  1436.           if (fixp)
  1437.         {
  1438.           fixp->fx_frag = prev_insn_frag;
  1439.           fixp->fx_where = prev_insn_where;
  1440.         }
  1441.           /* Update the previous insn information; leave prev_insn
  1442.          unchanged.  */
  1443.           prev_prev_insn = *ip;
  1444.         }
  1445.       prev_insn_is_delay_slot = 1;
  1446.  
  1447.       /* If that was an unconditional branch, forget the previous
  1448.          insn information.  */
  1449.       if (pinfo & INSN_UNCOND_BRANCH_DELAY)
  1450.         {
  1451.           prev_prev_insn.insn_mo = &dummy_opcode;
  1452.           prev_insn.insn_mo = &dummy_opcode;
  1453.         }
  1454.     }
  1455.       else if (pinfo & INSN_COND_BRANCH_LIKELY)
  1456.     {
  1457.       /* We don't yet optimize a branch likely.  What we should do
  1458.          is look at the target, copy the instruction found there
  1459.          into the delay slot, and increment the branch to jump to
  1460.          the next instruction.  */
  1461.       emit_nop ();
  1462.       /* Update the previous insn information.  */
  1463.       prev_prev_insn = *ip;
  1464.       prev_insn.insn_mo = &dummy_opcode;
  1465.     }
  1466.       else
  1467.     {
  1468.       /* Update the previous insn information.  */
  1469.       if (nops > 0)
  1470.         prev_prev_insn.insn_mo = &dummy_opcode;
  1471.       else
  1472.         prev_prev_insn = prev_insn;
  1473.       prev_insn = *ip;
  1474.  
  1475.       /* Any time we see a branch, we always fill the delay slot
  1476.          immediately; since this insn is not a branch, we know it
  1477.          is not in a delay slot.  */
  1478.       prev_insn_is_delay_slot = 0;
  1479.     }
  1480.  
  1481.       prev_prev_insn_unreordered = prev_insn_unreordered;
  1482.       prev_insn_unreordered = 0;
  1483.       prev_insn_frag = frag_now;
  1484.       prev_insn_where = f - frag_now->fr_literal;
  1485.       prev_insn_fixp = fixp;
  1486.       prev_insn_valid = 1;
  1487.     }
  1488.  
  1489.   /* We just output an insn, so the next one doesn't have a label.  */
  1490.   insn_label = NULL;
  1491. }
  1492.  
  1493. /* This function forgets that there was any previous instruction or
  1494.    label.  */
  1495.  
  1496. static void
  1497. mips_no_prev_insn ()
  1498. {
  1499.   prev_insn.insn_mo = &dummy_opcode;
  1500.   prev_prev_insn.insn_mo = &dummy_opcode;
  1501.   prev_insn_valid = 0;
  1502.   prev_insn_is_delay_slot = 0;
  1503.   prev_insn_unreordered = 0;
  1504.   prev_prev_insn_unreordered = 0;
  1505.   insn_label = NULL;
  1506. }
  1507.  
  1508. /* This function must be called whenever we turn on noreorder or emit
  1509.    something other than instructions.  It inserts any NOPS which might
  1510.    be needed by the previous instruction, and clears the information
  1511.    kept for the previous instructions.  */
  1512.  
  1513. static void
  1514. mips_emit_delays ()
  1515. {
  1516.   if (! mips_noreorder)
  1517.     {
  1518.       int nop;
  1519.  
  1520.       nop = 0;
  1521.       if ((mips_isa < 4
  1522.        && (! cop_interlocks
  1523.                && (prev_insn.insn_mo->pinfo
  1524.                    & (INSN_LOAD_COPROC_DELAY
  1525.                       | INSN_COPROC_MOVE_DELAY
  1526.                       | INSN_WRITE_COND_CODE))))
  1527.       || (! interlocks
  1528.           && (prev_insn.insn_mo->pinfo
  1529.           & (INSN_READ_LO
  1530.              | INSN_READ_HI)))
  1531.       || (mips_isa < 2
  1532.           && (prev_insn.insn_mo->pinfo
  1533.           & (INSN_LOAD_MEMORY_DELAY
  1534.              | INSN_COPROC_MEMORY_DELAY))))
  1535.     {
  1536.       nop = 1;
  1537.       if ((mips_isa < 4
  1538.            && (! cop_interlocks
  1539.                    && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
  1540.           || (! interlocks
  1541.           && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
  1542.               || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
  1543.         emit_nop ();
  1544.     }
  1545.       else if ((mips_isa < 4
  1546.         && (! cop_interlocks
  1547.                     && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
  1548.            || (! interlocks
  1549.            && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
  1550.                || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
  1551.     nop = 1;
  1552.       if (nop)
  1553.     {
  1554.       emit_nop ();
  1555.       if (insn_label != NULL)
  1556.         {
  1557.           assert (S_GET_SEGMENT (insn_label) == now_seg);
  1558.           insn_label->sy_frag = frag_now;
  1559.           S_SET_VALUE (insn_label, (valueT) frag_now_fix ());
  1560.         }
  1561.     }
  1562.     }
  1563.  
  1564.   mips_no_prev_insn ();
  1565. }
  1566.  
  1567. /* Build an instruction created by a macro expansion.  This is passed
  1568.    a pointer to the count of instructions created so far, an
  1569.    expression, the name of the instruction to build, an operand format
  1570.    string, and corresponding arguments.  */
  1571.  
  1572. #ifdef USE_STDARG
  1573. static void
  1574. macro_build (char *place,
  1575.          int *counter,
  1576.          expressionS * ep,
  1577.          const char *name,
  1578.          const char *fmt,
  1579.          ...)
  1580. #else
  1581. static void
  1582. macro_build (place, counter, ep, name, fmt, va_alist)
  1583.      char *place;
  1584.      int *counter;
  1585.      expressionS *ep;
  1586.      const char *name;
  1587.      const char *fmt;
  1588.      va_dcl
  1589. #endif
  1590. {
  1591.   struct mips_cl_insn insn;
  1592.   bfd_reloc_code_real_type r;
  1593.   va_list args;
  1594.  
  1595. #ifdef USE_STDARG
  1596.   va_start (args, fmt);
  1597. #else
  1598.   va_start (args);
  1599. #endif
  1600.  
  1601.   /*
  1602.    * If the macro is about to expand into a second instruction,
  1603.    * print a warning if needed. We need to pass ip as a parameter
  1604.    * to generate a better warning message here...
  1605.    */
  1606.   if (mips_warn_about_macros && place == NULL && *counter == 1)
  1607.     as_warn ("Macro instruction expanded into multiple instructions");
  1608.  
  1609.   if (place == NULL)
  1610.     *counter += 1;        /* bump instruction counter */
  1611.  
  1612.   r = BFD_RELOC_UNUSED;
  1613.   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
  1614.   assert (insn.insn_mo);
  1615.   assert (strcmp (name, insn.insn_mo->name) == 0);
  1616.  
  1617.   while (strcmp (fmt, insn.insn_mo->args) != 0
  1618.      || insn.insn_mo->pinfo == INSN_MACRO
  1619.      || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA2
  1620.          && mips_isa < 2)
  1621.      || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA3
  1622.          && mips_isa < 3)
  1623.      || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_ISA4
  1624.          && mips_isa < 4)
  1625.      || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4650
  1626.          && ! mips_4650)
  1627.      || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4010
  1628.          && ! mips_4010)
  1629.      || ((insn.insn_mo->pinfo & INSN_ISA) == INSN_4100
  1630.          && ! mips_4100))
  1631.     {
  1632.       ++insn.insn_mo;
  1633.       assert (insn.insn_mo->name);
  1634.       assert (strcmp (name, insn.insn_mo->name) == 0);
  1635.     }
  1636.   insn.insn_opcode = insn.insn_mo->match;
  1637.   for (;;)
  1638.     {
  1639.       switch (*fmt++)
  1640.     {
  1641.     case '\0':
  1642.       break;
  1643.  
  1644.     case ',':
  1645.     case '(':
  1646.     case ')':
  1647.       continue;
  1648.  
  1649.     case 't':
  1650.     case 'w':
  1651.     case 'E':
  1652.       insn.insn_opcode |= va_arg (args, int) << 16;
  1653.       continue;
  1654.  
  1655.     case 'c':
  1656.     case 'T':
  1657.     case 'W':
  1658.       insn.insn_opcode |= va_arg (args, int) << 16;
  1659.       continue;
  1660.  
  1661.     case 'd':
  1662.     case 'G':
  1663.       insn.insn_opcode |= va_arg (args, int) << 11;
  1664.       continue;
  1665.  
  1666.     case 'V':
  1667.     case 'S':
  1668.       insn.insn_opcode |= va_arg (args, int) << 11;
  1669.       continue;
  1670.  
  1671.     case 'z':
  1672.       continue;
  1673.  
  1674.     case '<':
  1675.       insn.insn_opcode |= va_arg (args, int) << 6;
  1676.       continue;
  1677.  
  1678.     case 'D':
  1679.       insn.insn_opcode |= va_arg (args, int) << 6;
  1680.       continue;
  1681.  
  1682.     case 'B':
  1683.       insn.insn_opcode |= va_arg (args, int) << 6;
  1684.       continue;
  1685.  
  1686.     case 'b':
  1687.     case 's':
  1688.     case 'r':
  1689.     case 'v':
  1690.       insn.insn_opcode |= va_arg (args, int) << 21;
  1691.       continue;
  1692.  
  1693.     case 'i':
  1694.     case 'j':
  1695.     case 'o':
  1696.       r = (bfd_reloc_code_real_type) va_arg (args, int);
  1697.       assert (r == BFD_RELOC_MIPS_GPREL
  1698.           || r == BFD_RELOC_MIPS_LITERAL
  1699.           || r == BFD_RELOC_LO16
  1700.           || r == BFD_RELOC_MIPS_GOT16
  1701.           || r == BFD_RELOC_MIPS_CALL16
  1702.           || r == BFD_RELOC_MIPS_GOT_LO16
  1703.           || r == BFD_RELOC_MIPS_CALL_LO16
  1704.           || (ep->X_op == O_subtract
  1705.               && now_seg == text_section
  1706.               && r == BFD_RELOC_PCREL_LO16));
  1707.       continue;
  1708.  
  1709.     case 'u':
  1710.       r = (bfd_reloc_code_real_type) va_arg (args, int);
  1711.       assert (ep != NULL
  1712.           && (ep->X_op == O_constant
  1713.               || (ep->X_op == O_symbol
  1714.               && (r == BFD_RELOC_HI16_S
  1715.                   || r == BFD_RELOC_HI16
  1716.                   || r == BFD_RELOC_MIPS_GOT_HI16
  1717.                   || r == BFD_RELOC_MIPS_CALL_HI16))
  1718.               || (ep->X_op == O_subtract
  1719.               && now_seg == text_section
  1720.               && r == BFD_RELOC_PCREL_HI16_S)));
  1721.       if (ep->X_op == O_constant)
  1722.         {
  1723.           insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
  1724.           ep = NULL;
  1725.           r = BFD_RELOC_UNUSED;
  1726.         }
  1727.       continue;
  1728.  
  1729.     case 'p':
  1730.       assert (ep != NULL);
  1731.       /*
  1732.        * This allows macro() to pass an immediate expression for
  1733.        * creating short branches without creating a symbol.
  1734.        * Note that the expression still might come from the assembly
  1735.        * input, in which case the value is not checked for range nor
  1736.        * is a relocation entry generated (yuck).
  1737.        */
  1738.       if (ep->X_op == O_constant)
  1739.         {
  1740.           insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
  1741.           ep = NULL;
  1742.         }
  1743.       else
  1744.         r = BFD_RELOC_16_PCREL_S2;
  1745.       continue;
  1746.  
  1747.     case 'a':
  1748.       assert (ep != NULL);
  1749.       r = BFD_RELOC_MIPS_JMP;
  1750.       continue;
  1751.  
  1752.     default:
  1753.       internalError ();
  1754.     }
  1755.       break;
  1756.     }
  1757.   va_end (args);
  1758.   assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
  1759.  
  1760.   append_insn (place, &insn, ep, r, false);
  1761. }
  1762.  
  1763. /*
  1764.  * Generate a "lui" instruction.
  1765.  */
  1766. static void
  1767. macro_build_lui (place, counter, ep, regnum)
  1768.      char *place;
  1769.      int *counter;
  1770.      expressionS *ep;
  1771.      int regnum;
  1772. {
  1773.   expressionS high_expr;
  1774.   struct mips_cl_insn insn;
  1775.   bfd_reloc_code_real_type r;
  1776.   CONST char *name = "lui";
  1777.   CONST char *fmt = "t,u";
  1778.  
  1779.   if (place == NULL)
  1780.     high_expr = *ep;
  1781.   else
  1782.     {
  1783.       high_expr.X_op = O_constant;
  1784.       high_expr.X_add_number = ep->X_add_number;
  1785.     }
  1786.  
  1787.   if (high_expr.X_op == O_constant)
  1788.     {
  1789.       /* we can compute the instruction now without a relocation entry */
  1790.       if (high_expr.X_add_number & 0x8000)
  1791.     high_expr.X_add_number += 0x10000;
  1792.       high_expr.X_add_number =
  1793.     ((unsigned long) high_expr.X_add_number >> 16) & 0xffff;
  1794.       r = BFD_RELOC_UNUSED;
  1795.     }
  1796.   else
  1797.     {
  1798.       assert (ep->X_op == O_symbol);
  1799.       /* _gp_disp is a special case, used from s_cpload.  */
  1800.       assert (mips_pic == NO_PIC
  1801.           || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
  1802.       r = BFD_RELOC_HI16_S;
  1803.     }
  1804.  
  1805.   /*
  1806.    * If the macro is about to expand into a second instruction,
  1807.    * print a warning if needed. We need to pass ip as a parameter
  1808.    * to generate a better warning message here...
  1809.    */
  1810.   if (mips_warn_about_macros && place == NULL && *counter == 1)
  1811.     as_warn ("Macro instruction expanded into multiple instructions");
  1812.  
  1813.   if (place == NULL)
  1814.     *counter += 1;        /* bump instruction counter */
  1815.  
  1816.   insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
  1817.   assert (insn.insn_mo);
  1818.   assert (strcmp (name, insn.insn_mo->name) == 0);
  1819.   assert (strcmp (fmt, insn.insn_mo->args) == 0);
  1820.  
  1821.   insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
  1822.   if (r == BFD_RELOC_UNUSED)
  1823.     {
  1824.       insn.insn_opcode |= high_expr.X_add_number;
  1825.       append_insn (place, &insn, NULL, r, false);
  1826.     }
  1827.   else
  1828.     append_insn (place, &insn, &high_expr, r, false);
  1829. }
  1830.  
  1831. /*            set_at()
  1832.  * Generates code to set the $at register to true (one)
  1833.  * if reg is less than the immediate expression.
  1834.  */
  1835. static void
  1836. set_at (counter, reg, unsignedp)
  1837.      int *counter;
  1838.      int reg;
  1839.      int unsignedp;
  1840. {
  1841.   if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
  1842.     macro_build ((char *) NULL, counter, &imm_expr,
  1843.          unsignedp ? "sltiu" : "slti",
  1844.          "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
  1845.   else
  1846.     {
  1847.       load_register (counter, AT, &imm_expr, 0);
  1848.       macro_build ((char *) NULL, counter, NULL,
  1849.            unsignedp ? "sltu" : "slt",
  1850.            "d,v,t", AT, reg, AT);
  1851.     }
  1852. }
  1853.  
  1854. /* Warn if an expression is not a constant.  */
  1855.  
  1856. static void
  1857. check_absolute_expr (ip, ex)
  1858.      struct mips_cl_insn *ip;
  1859.      expressionS *ex;
  1860. {
  1861.   if (ex->X_op != O_constant)
  1862.     as_warn ("Instruction %s requires absolute expression", ip->insn_mo->name);
  1863. }
  1864.  
  1865. /*            load_register()
  1866.  *  This routine generates the least number of instructions neccessary to load
  1867.  *  an absolute expression value into a register.
  1868.  */
  1869. static void
  1870. load_register (counter, reg, ep, dbl)
  1871.      int *counter;
  1872.      int reg;
  1873.      expressionS *ep;
  1874.      int dbl;
  1875. {
  1876.   int shift, freg;
  1877.   expressionS hi32, lo32, tmp;
  1878.  
  1879.   if (ep->X_op != O_big)
  1880.     {
  1881.       assert (ep->X_op == O_constant);
  1882.       if (ep->X_add_number < 0x8000
  1883.       && (ep->X_add_number >= 0
  1884.           || (ep->X_add_number >= -0x8000
  1885.           && (! dbl
  1886.               || ! ep->X_unsigned
  1887.               || sizeof (ep->X_add_number) > 4))))
  1888.     {
  1889.       /* We can handle 16 bit signed values with an addiu to
  1890.          $zero.  No need to ever use daddiu here, since $zero and
  1891.          the result are always correct in 32 bit mode.  */
  1892.       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
  1893.                (int) BFD_RELOC_LO16);
  1894.       return;
  1895.     }
  1896.       else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
  1897.     {
  1898.       /* We can handle 16 bit unsigned values with an ori to
  1899.              $zero.  */
  1900.       macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
  1901.                (int) BFD_RELOC_LO16);
  1902.       return;
  1903.     }
  1904.       else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
  1905.          || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
  1906.              == ~ (offsetT) 0x7fffffff))
  1907.         && (! dbl
  1908.             || ! ep->X_unsigned
  1909.             || sizeof (ep->X_add_number) > 4
  1910.             || (ep->X_add_number & 0x80000000) == 0))
  1911.            || (mips_isa < 3
  1912.            && (ep->X_add_number &~ 0xffffffff) == 0))
  1913.     {
  1914.       /* 32 bit values require an lui.  */
  1915.       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
  1916.                (int) BFD_RELOC_HI16);
  1917.       if ((ep->X_add_number & 0xffff) != 0)
  1918.         macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
  1919.              (int) BFD_RELOC_LO16);
  1920.       return;
  1921.     }
  1922.       else
  1923.     {
  1924.       /* 32 bit value with high bit set being loaded into a 64 bit
  1925.              register.  We can't use lui, because that would
  1926.              incorrectly set the 32 high bits.  */
  1927.       generic_bignum[3] = 0;
  1928.       generic_bignum[2] = 0;
  1929.       generic_bignum[1] = (ep->X_add_number >> 16) & 0xffff;
  1930.       generic_bignum[0] = ep->X_add_number & 0xffff;
  1931.       tmp.X_op = O_big;
  1932.       tmp.X_add_number = 4;
  1933.       ep = &tmp;
  1934.     }
  1935.     }
  1936.  
  1937.   /* The value is larger than 32 bits.  */
  1938.  
  1939.   if (mips_isa < 3)
  1940.     {
  1941.       as_bad ("Number larger than 32 bits");
  1942.       macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
  1943.            (int) BFD_RELOC_LO16);
  1944.       return;
  1945.     }
  1946.  
  1947.   if (ep->X_op != O_big)
  1948.     {
  1949.       hi32 = *ep;
  1950.       shift = 32;
  1951.       hi32.X_add_number >>= shift;
  1952.       hi32.X_add_number &= 0xffffffff;
  1953.       if ((hi32.X_add_number & 0x80000000) != 0)
  1954.     hi32.X_add_number |= ~ (offsetT) 0xffffffff;
  1955.       lo32 = *ep;
  1956.       lo32.X_add_number &= 0xffffffff;
  1957.     }
  1958.   else
  1959.     {
  1960.       assert (ep->X_add_number > 2);
  1961.       if (ep->X_add_number == 3)
  1962.     generic_bignum[3] = 0;
  1963.       else if (ep->X_add_number > 4)
  1964.     as_bad ("Number larger than 64 bits");
  1965.       lo32.X_op = O_constant;
  1966.       lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
  1967.       hi32.X_op = O_constant;
  1968.       hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
  1969.     }
  1970.  
  1971.   if (hi32.X_add_number == 0)
  1972.     freg = 0;
  1973.   else
  1974.     {
  1975.       if (hi32.X_add_number == 0xffffffff)
  1976.         {
  1977.           if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
  1978.             {
  1979.               macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
  1980.                            (int) BFD_RELOC_LO16);
  1981.               return;
  1982.             }
  1983.           if (lo32.X_add_number & 0x80000000)
  1984.             {
  1985.               macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
  1986.                            (int) BFD_RELOC_HI16);
  1987.               macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
  1988.                            (int) BFD_RELOC_LO16);
  1989.               return;
  1990.             }
  1991.         }
  1992.       load_register (counter, reg, &hi32, 0);
  1993.       freg = reg;
  1994.     }
  1995.   if ((lo32.X_add_number & 0xffff0000) == 0)
  1996.     {
  1997.       if (freg != 0)
  1998.     {
  1999.       macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
  2000.                freg, 0);
  2001.       freg = reg;
  2002.     }
  2003.     }
  2004.   else
  2005.     {
  2006.       expressionS mid16;
  2007.  
  2008.       if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
  2009.         {
  2010.       macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
  2011.                (int) BFD_RELOC_HI16);
  2012.           macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
  2013.                        reg, 32);
  2014.           return;
  2015.         }
  2016.  
  2017.       if (freg != 0)
  2018.     {
  2019.       macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
  2020.                freg, 16);
  2021.       freg = reg;
  2022.     }
  2023.       mid16 = lo32;
  2024.       mid16.X_add_number >>= 16;
  2025.       macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
  2026.            freg, (int) BFD_RELOC_LO16);
  2027.       macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
  2028.            reg, 16);
  2029.       freg = reg;
  2030.     }
  2031.   if ((lo32.X_add_number & 0xffff) != 0)
  2032.     macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
  2033.          (int) BFD_RELOC_LO16);
  2034. }
  2035.  
  2036. /* Load an address into a register.  */
  2037.  
  2038. static void
  2039. load_address (counter, reg, ep)
  2040.      int *counter;
  2041.      int reg;
  2042.      expressionS *ep;
  2043. {
  2044.   char *p;
  2045.  
  2046.   if (ep->X_op != O_constant
  2047.       && ep->X_op != O_symbol)
  2048.     {
  2049.       as_bad ("expression too complex");
  2050.       ep->X_op = O_constant;
  2051.     }
  2052.  
  2053.   if (ep->X_op == O_constant)
  2054.     {
  2055.       load_register (counter, reg, ep, 0);
  2056.       return;
  2057.     }
  2058.  
  2059.   if (mips_pic == NO_PIC)
  2060.     {
  2061.       /* If this is a reference to a GP relative symbol, we want
  2062.        addiu    $reg,$gp,<sym>        (BFD_RELOC_MIPS_GPREL)
  2063.      Otherwise we want
  2064.        lui        $reg,<sym>        (BFD_RELOC_HI16_S)
  2065.        addiu    $reg,$reg,<sym>        (BFD_RELOC_LO16)
  2066.      If we have an addend, we always use the latter form.  */
  2067.       if ((valueT) ep->X_add_number >= MAX_GPREL_OFFSET
  2068.           || nopic_need_relax (ep->X_add_symbol))
  2069.     p = NULL;
  2070.       else
  2071.     {
  2072.       frag_grow (20);
  2073.       macro_build ((char *) NULL, counter, ep,
  2074.                mips_isa < 3 ? "addiu" : "daddiu",
  2075.                "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
  2076.       p = frag_var (rs_machine_dependent, 8, 0,
  2077.             RELAX_ENCODE (4, 8, 0, 4, 0, mips_warn_about_macros),
  2078.             ep->X_add_symbol, (long) 0, (char *) NULL);
  2079.     }
  2080.       macro_build_lui (p, counter, ep, reg);
  2081.       if (p != NULL)
  2082.     p += 4;
  2083.       macro_build (p, counter, ep,
  2084.            mips_isa < 3 ? "addiu" : "daddiu",
  2085.            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
  2086.     }
  2087.   else if (mips_pic == SVR4_PIC && ! mips_big_got)
  2088.     {
  2089.       expressionS ex;
  2090.  
  2091.       /* If this is a reference to an external symbol, we want
  2092.        lw        $reg,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  2093.      Otherwise we want
  2094.        lw        $reg,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  2095.        nop
  2096.        addiu    $reg,$reg,<sym>        (BFD_RELOC_LO16)
  2097.      If there is a constant, it must be added in after.  */
  2098.       ex.X_add_number = ep->X_add_number;
  2099.       ep->X_add_number = 0;
  2100.       frag_grow (20);
  2101.       macro_build ((char *) NULL, counter, ep,
  2102.            mips_isa < 3 ? "lw" : "ld",
  2103.            "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
  2104.       macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
  2105.       p = frag_var (rs_machine_dependent, 4, 0,
  2106.             RELAX_ENCODE (0, 4, -8, 0, 0, mips_warn_about_macros),
  2107.             ep->X_add_symbol, (long) 0, (char *) NULL);
  2108.       macro_build (p, counter, ep,
  2109.            mips_isa < 3 ? "addiu" : "daddiu",
  2110.            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
  2111.       if (ex.X_add_number != 0)
  2112.     {
  2113.       if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
  2114.         as_bad ("PIC code offset overflow (max 16 signed bits)");
  2115.       ex.X_op = O_constant;
  2116.       macro_build ((char *) NULL, counter, &ex,
  2117.                mips_isa < 3 ? "addiu" : "daddiu",
  2118.                "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
  2119.     }
  2120.     }
  2121.   else if (mips_pic == SVR4_PIC)
  2122.     {
  2123.       expressionS ex;
  2124.       int off;
  2125.  
  2126.       /* This is the large GOT case.  If this is a reference to an
  2127.      external symbol, we want
  2128.        lui        $reg,<sym>        (BFD_RELOC_MIPS_GOT_HI16)
  2129.        addu        $reg,$reg,$gp
  2130.        lw        $reg,<sym>($reg)    (BFD_RELOC_MIPS_GOT_LO16)
  2131.      Otherwise, for a reference to a local symbol, we want
  2132.        lw        $reg,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  2133.        nop
  2134.        addiu    $reg,$reg,<sym>        (BFD_RELOC_LO16)
  2135.      If there is a constant, it must be added in after.  */
  2136.       ex.X_add_number = ep->X_add_number;
  2137.       ep->X_add_number = 0;
  2138.       if (reg_needs_delay (GP))
  2139.     off = 4;
  2140.       else
  2141.     off = 0;
  2142.       frag_grow (32);
  2143.       macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
  2144.            (int) BFD_RELOC_MIPS_GOT_HI16);
  2145.       macro_build ((char *) NULL, counter, (expressionS *) NULL,
  2146.            mips_isa < 3 ? "addu" : "daddu",
  2147.            "d,v,t", reg, reg, GP);
  2148.       macro_build ((char *) NULL, counter, ep,
  2149.            mips_isa < 3 ? "lw" : "ld",
  2150.            "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
  2151.       p = frag_var (rs_machine_dependent, 12 + off, 0,
  2152.             RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
  2153.                   mips_warn_about_macros),
  2154.             ep->X_add_symbol, (long) 0, (char *) NULL);
  2155.       if (off > 0)
  2156.     {
  2157.       /* We need a nop before loading from $gp.  This special
  2158.              check is required because the lui which starts the main
  2159.              instruction stream does not refer to $gp, and so will not
  2160.              insert the nop which may be required.  */
  2161.       macro_build (p, counter, (expressionS *) NULL, "nop", "");
  2162.       p += 4;
  2163.     }
  2164.       macro_build (p, counter, ep,
  2165.            mips_isa < 3 ? "lw" : "ld",
  2166.            "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
  2167.       p += 4;
  2168.       macro_build (p, counter, (expressionS *) NULL, "nop", "");
  2169.       p += 4;
  2170.       macro_build (p, counter, ep,
  2171.            mips_isa < 3 ? "addiu" : "daddiu",
  2172.            "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
  2173.       if (ex.X_add_number != 0)
  2174.     {
  2175.       if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
  2176.         as_bad ("PIC code offset overflow (max 16 signed bits)");
  2177.       ex.X_op = O_constant;
  2178.       macro_build ((char *) NULL, counter, &ex,
  2179.                mips_isa < 3 ? "addiu" : "daddiu",
  2180.                "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
  2181.     }
  2182.     }
  2183.   else if (mips_pic == EMBEDDED_PIC)
  2184.     {
  2185.       /* We always do
  2186.        addiu    $reg,$gp,<sym>        (BFD_RELOC_MIPS_GPREL)
  2187.      */
  2188.       macro_build ((char *) NULL, counter, ep,
  2189.            mips_isa < 3 ? "addiu" : "daddiu",
  2190.            "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
  2191.     }
  2192.   else
  2193.     abort ();
  2194. }
  2195.  
  2196. /*
  2197.  *            Build macros
  2198.  *   This routine implements the seemingly endless macro or synthesized
  2199.  * instructions and addressing modes in the mips assembly language. Many
  2200.  * of these macros are simple and are similar to each other. These could
  2201.  * probably be handled by some kind of table or grammer aproach instead of
  2202.  * this verbose method. Others are not simple macros but are more like
  2203.  * optimizing code generation.
  2204.  *   One interesting optimization is when several store macros appear
  2205.  * consecutivly that would load AT with the upper half of the same address.
  2206.  * The ensuing load upper instructions are ommited. This implies some kind
  2207.  * of global optimization. We currently only optimize within a single macro.
  2208.  *   For many of the load and store macros if the address is specified as a
  2209.  * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
  2210.  * first load register 'at' with zero and use it as the base register. The
  2211.  * mips assembler simply uses register $zero. Just one tiny optimization
  2212.  * we're missing.
  2213.  */
  2214. static void
  2215. macro (ip)
  2216.      struct mips_cl_insn *ip;
  2217. {
  2218.   register int treg, sreg, dreg, breg;
  2219.   int tempreg;
  2220.   int mask;
  2221.   int icnt = 0;
  2222.   int used_at;
  2223.   expressionS expr1;
  2224.   const char *s;
  2225.   const char *s2;
  2226.   const char *fmt;
  2227.   int likely = 0;
  2228.   int dbl = 0;
  2229.   int coproc = 0;
  2230.   int lr = 0;
  2231.   offsetT maxnum;
  2232.   int off;
  2233.   bfd_reloc_code_real_type r;
  2234.   char *p;
  2235.   int hold_mips_optimize;
  2236.  
  2237.   treg = (ip->insn_opcode >> 16) & 0x1f;
  2238.   dreg = (ip->insn_opcode >> 11) & 0x1f;
  2239.   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
  2240.   mask = ip->insn_mo->mask;
  2241.  
  2242.   expr1.X_op = O_constant;
  2243.   expr1.X_op_symbol = NULL;
  2244.   expr1.X_add_symbol = NULL;
  2245.   expr1.X_add_number = 1;
  2246.  
  2247.   switch (mask)
  2248.     {
  2249.     case M_DABS:
  2250.       dbl = 1;
  2251.     case M_ABS:
  2252.       /* bgez $a0,.+12
  2253.      move v0,$a0
  2254.      sub v0,$zero,$a0
  2255.      */
  2256.  
  2257.       mips_emit_delays ();
  2258.       ++mips_noreorder;
  2259.       mips_any_noreorder = 1;
  2260.  
  2261.       expr1.X_add_number = 8;
  2262.       macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
  2263.       if (dreg == sreg)
  2264.     macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  2265.       else
  2266.     macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, sreg, 0);
  2267.       macro_build ((char *) NULL, &icnt, NULL,
  2268.            dbl ? "dsub" : "sub",
  2269.            "d,v,t", dreg, 0, sreg);
  2270.  
  2271.       --mips_noreorder;
  2272.       return;
  2273.  
  2274.     case M_ADD_I:
  2275.       s = "addi";
  2276.       s2 = "add";
  2277.       goto do_addi;
  2278.     case M_ADDU_I:
  2279.       s = "addiu";
  2280.       s2 = "addu";
  2281.       goto do_addi;
  2282.     case M_DADD_I:
  2283.       dbl = 1;
  2284.       s = "daddi";
  2285.       s2 = "dadd";
  2286.       goto do_addi;
  2287.     case M_DADDU_I:
  2288.       dbl = 1;
  2289.       s = "daddiu";
  2290.       s2 = "daddu";
  2291.     do_addi:
  2292.       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
  2293.     {
  2294.       macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
  2295.                (int) BFD_RELOC_LO16);
  2296.       return;
  2297.     }
  2298.       load_register (&icnt, AT, &imm_expr, dbl);
  2299.       macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
  2300.       break;
  2301.  
  2302.     case M_AND_I:
  2303.       s = "andi";
  2304.       s2 = "and";
  2305.       goto do_bit;
  2306.     case M_OR_I:
  2307.       s = "ori";
  2308.       s2 = "or";
  2309.       goto do_bit;
  2310.     case M_NOR_I:
  2311.       s = "";
  2312.       s2 = "nor";
  2313.       goto do_bit;
  2314.     case M_XOR_I:
  2315.       s = "xori";
  2316.       s2 = "xor";
  2317.     do_bit:
  2318.       if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
  2319.     {
  2320.       if (mask != M_NOR_I)
  2321.         macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
  2322.              sreg, (int) BFD_RELOC_LO16);
  2323.       else
  2324.         {
  2325.           macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
  2326.                treg, sreg, (int) BFD_RELOC_LO16);
  2327.           macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
  2328.                treg, treg, 0);
  2329.         }
  2330.       return;
  2331.     }
  2332.  
  2333.       load_register (&icnt, AT, &imm_expr, 0);
  2334.       macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
  2335.       break;
  2336.  
  2337.     case M_BEQ_I:
  2338.       s = "beq";
  2339.       goto beq_i;
  2340.     case M_BEQL_I:
  2341.       s = "beql";
  2342.       likely = 1;
  2343.       goto beq_i;
  2344.     case M_BNE_I:
  2345.       s = "bne";
  2346.       goto beq_i;
  2347.     case M_BNEL_I:
  2348.       s = "bnel";
  2349.       likely = 1;
  2350.     beq_i:
  2351.       if (imm_expr.X_add_number == 0)
  2352.     {
  2353.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
  2354.                0);
  2355.       return;
  2356.     }
  2357.       load_register (&icnt, AT, &imm_expr, 0);
  2358.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
  2359.       break;
  2360.  
  2361.     case M_BGEL:
  2362.       likely = 1;
  2363.     case M_BGE:
  2364.       if (treg == 0)
  2365.     {
  2366.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2367.                likely ? "bgezl" : "bgez",
  2368.                "s,p", sreg);
  2369.       return;
  2370.     }
  2371.       if (sreg == 0)
  2372.     {
  2373.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2374.                likely ? "blezl" : "blez",
  2375.                "s,p", treg);
  2376.       return;
  2377.     }
  2378.       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
  2379.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2380.            likely ? "beql" : "beq",
  2381.            "s,t,p", AT, 0);
  2382.       break;
  2383.  
  2384.     case M_BGTL_I:
  2385.       likely = 1;
  2386.     case M_BGT_I:
  2387.       /* check for > max integer */
  2388.       maxnum = 0x7fffffff;
  2389.       if (mips_isa >= 3)
  2390.     {
  2391.       maxnum <<= 16;
  2392.       maxnum |= 0xffff;
  2393.       maxnum <<= 16;
  2394.       maxnum |= 0xffff;
  2395.     }
  2396.       if (imm_expr.X_add_number >= maxnum
  2397.       && (mips_isa < 3 || sizeof (maxnum) > 4))
  2398.     {
  2399.     do_false:
  2400.       /* result is always false */
  2401.       if (! likely)
  2402.         {
  2403.           as_warn ("Branch %s is always false (nop)", ip->insn_mo->name);
  2404.           macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  2405.         }
  2406.       else
  2407.         {
  2408.           as_warn ("Branch likely %s is always false", ip->insn_mo->name);
  2409.           macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
  2410.                "s,t,p", 0, 0);
  2411.         }
  2412.       return;
  2413.     }
  2414.       imm_expr.X_add_number++;
  2415.       /* FALLTHROUGH */
  2416.     case M_BGE_I:
  2417.     case M_BGEL_I:
  2418.       if (mask == M_BGEL_I)
  2419.     likely = 1;
  2420.       if (imm_expr.X_add_number == 0)
  2421.     {
  2422.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2423.                likely ? "bgezl" : "bgez",
  2424.                "s,p", sreg);
  2425.       return;
  2426.     }
  2427.       if (imm_expr.X_add_number == 1)
  2428.     {
  2429.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2430.                likely ? "bgtzl" : "bgtz",
  2431.                "s,p", sreg);
  2432.       return;
  2433.     }
  2434.       maxnum = 0x7fffffff;
  2435.       if (mips_isa >= 3)
  2436.     {
  2437.       maxnum <<= 16;
  2438.       maxnum |= 0xffff;
  2439.       maxnum <<= 16;
  2440.       maxnum |= 0xffff;
  2441.     }
  2442.       maxnum = - maxnum - 1;
  2443.       if (imm_expr.X_add_number <= maxnum
  2444.       && (mips_isa < 3 || sizeof (maxnum) > 4))
  2445.     {
  2446.     do_true:
  2447.       /* result is always true */
  2448.       as_warn ("Branch %s is always true", ip->insn_mo->name);
  2449.       macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
  2450.       return;
  2451.     }
  2452.       set_at (&icnt, sreg, 0);
  2453.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2454.            likely ? "beql" : "beq",
  2455.            "s,t,p", AT, 0);
  2456.       break;
  2457.  
  2458.     case M_BGEUL:
  2459.       likely = 1;
  2460.     case M_BGEU:
  2461.       if (treg == 0)
  2462.     goto do_true;
  2463.       if (sreg == 0)
  2464.     {
  2465.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2466.                likely ? "beql" : "beq",
  2467.                "s,t,p", 0, treg);
  2468.       return;
  2469.     }
  2470.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
  2471.            treg);
  2472.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2473.            likely ? "beql" : "beq",
  2474.            "s,t,p", AT, 0);
  2475.       break;
  2476.  
  2477.     case M_BGTUL_I:
  2478.       likely = 1;
  2479.     case M_BGTU_I:
  2480.       if (sreg == 0 || imm_expr.X_add_number == -1)
  2481.     goto do_false;
  2482.       imm_expr.X_add_number++;
  2483.       /* FALLTHROUGH */
  2484.     case M_BGEU_I:
  2485.     case M_BGEUL_I:
  2486.       if (mask == M_BGEUL_I)
  2487.     likely = 1;
  2488.       if (imm_expr.X_add_number == 0)
  2489.     goto do_true;
  2490.       if (imm_expr.X_add_number == 1)
  2491.     {
  2492.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2493.                likely ? "bnel" : "bne",
  2494.                "s,t,p", sreg, 0);
  2495.       return;
  2496.     }
  2497.       set_at (&icnt, sreg, 1);
  2498.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2499.            likely ? "beql" : "beq",
  2500.            "s,t,p", AT, 0);
  2501.       break;
  2502.  
  2503.     case M_BGTL:
  2504.       likely = 1;
  2505.     case M_BGT:
  2506.       if (treg == 0)
  2507.     {
  2508.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2509.                likely ? "bgtzl" : "bgtz",
  2510.                "s,p", sreg);
  2511.       return;
  2512.     }
  2513.       if (sreg == 0)
  2514.     {
  2515.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2516.                likely ? "bltzl" : "bltz",
  2517.                "s,p", treg);
  2518.       return;
  2519.     }
  2520.       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
  2521.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2522.            likely ? "bnel" : "bne",
  2523.            "s,t,p", AT, 0);
  2524.       break;
  2525.  
  2526.     case M_BGTUL:
  2527.       likely = 1;
  2528.     case M_BGTU:
  2529.       if (treg == 0)
  2530.     {
  2531.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2532.                likely ? "bnel" : "bne",
  2533.                "s,t,p", sreg, 0);
  2534.       return;
  2535.     }
  2536.       if (sreg == 0)
  2537.     goto do_false;
  2538.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
  2539.            sreg);
  2540.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2541.            likely ? "bnel" : "bne",
  2542.            "s,t,p", AT, 0);
  2543.       break;
  2544.  
  2545.     case M_BLEL:
  2546.       likely = 1;
  2547.     case M_BLE:
  2548.       if (treg == 0)
  2549.     {
  2550.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2551.                likely ? "blezl" : "blez",
  2552.                "s,p", sreg);
  2553.       return;
  2554.     }
  2555.       if (sreg == 0)
  2556.     {
  2557.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2558.                likely ? "bgezl" : "bgez",
  2559.                "s,p", treg);
  2560.       return;
  2561.     }
  2562.       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
  2563.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2564.            likely ? "beql" : "beq",
  2565.            "s,t,p", AT, 0);
  2566.       break;
  2567.  
  2568.     case M_BLEL_I:
  2569.       likely = 1;
  2570.     case M_BLE_I:
  2571.       maxnum = 0x7fffffff;
  2572.       if (mips_isa >= 3)
  2573.     {
  2574.       maxnum <<= 16;
  2575.       maxnum |= 0xffff;
  2576.       maxnum <<= 16;
  2577.       maxnum |= 0xffff;
  2578.     }
  2579.       if (imm_expr.X_add_number >= maxnum
  2580.       && (mips_isa < 3 || sizeof (maxnum) > 4))
  2581.     goto do_true;
  2582.       imm_expr.X_add_number++;
  2583.       /* FALLTHROUGH */
  2584.     case M_BLT_I:
  2585.     case M_BLTL_I:
  2586.       if (mask == M_BLTL_I)
  2587.     likely = 1;
  2588.       if (imm_expr.X_add_number == 0)
  2589.     {
  2590.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2591.                likely ? "bltzl" : "bltz",
  2592.                "s,p", sreg);
  2593.       return;
  2594.     }
  2595.       if (imm_expr.X_add_number == 1)
  2596.     {
  2597.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2598.                likely ? "blezl" : "blez",
  2599.                "s,p", sreg);
  2600.       return;
  2601.     }
  2602.       set_at (&icnt, sreg, 0);
  2603.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2604.            likely ? "bnel" : "bne",
  2605.            "s,t,p", AT, 0);
  2606.       break;
  2607.  
  2608.     case M_BLEUL:
  2609.       likely = 1;
  2610.     case M_BLEU:
  2611.       if (treg == 0)
  2612.     {
  2613.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2614.                likely ? "beql" : "beq",
  2615.                "s,t,p", sreg, 0);
  2616.       return;
  2617.     }
  2618.       if (sreg == 0)
  2619.     goto do_true;
  2620.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
  2621.            sreg);
  2622.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2623.            likely ? "beql" : "beq",
  2624.            "s,t,p", AT, 0);
  2625.       break;
  2626.  
  2627.     case M_BLEUL_I:
  2628.       likely = 1;
  2629.     case M_BLEU_I:
  2630.       if (sreg == 0 || imm_expr.X_add_number == -1)
  2631.     goto do_true;
  2632.       imm_expr.X_add_number++;
  2633.       /* FALLTHROUGH */
  2634.     case M_BLTU_I:
  2635.     case M_BLTUL_I:
  2636.       if (mask == M_BLTUL_I)
  2637.     likely = 1;
  2638.       if (imm_expr.X_add_number == 0)
  2639.     goto do_false;
  2640.       if (imm_expr.X_add_number == 1)
  2641.     {
  2642.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2643.                likely ? "beql" : "beq",
  2644.                "s,t,p", sreg, 0);
  2645.       return;
  2646.     }
  2647.       set_at (&icnt, sreg, 1);
  2648.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2649.            likely ? "bnel" : "bne",
  2650.            "s,t,p", AT, 0);
  2651.       break;
  2652.  
  2653.     case M_BLTL:
  2654.       likely = 1;
  2655.     case M_BLT:
  2656.       if (treg == 0)
  2657.     {
  2658.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2659.                likely ? "bltzl" : "bltz",
  2660.                "s,p", sreg);
  2661.       return;
  2662.     }
  2663.       if (sreg == 0)
  2664.     {
  2665.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2666.                likely ? "bgtzl" : "bgtz",
  2667.                "s,p", treg);
  2668.       return;
  2669.     }
  2670.       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
  2671.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2672.            likely ? "bnel" : "bne",
  2673.            "s,t,p", AT, 0);
  2674.       break;
  2675.  
  2676.     case M_BLTUL:
  2677.       likely = 1;
  2678.     case M_BLTU:
  2679.       if (treg == 0)
  2680.     goto do_false;
  2681.       if (sreg == 0)
  2682.     {
  2683.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2684.                likely ? "bnel" : "bne",
  2685.                "s,t,p", 0, treg);
  2686.       return;
  2687.     }
  2688.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
  2689.            treg);
  2690.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2691.            likely ? "bnel" : "bne",
  2692.            "s,t,p", AT, 0);
  2693.       break;
  2694.  
  2695.     case M_DDIV_3:
  2696.       dbl = 1;
  2697.     case M_DIV_3:
  2698.       s = "mflo";
  2699.       goto do_div3;
  2700.     case M_DREM_3:
  2701.       dbl = 1;
  2702.     case M_REM_3:
  2703.       s = "mfhi";
  2704.     do_div3:
  2705.       if (treg == 0)
  2706.     {
  2707.       as_warn ("Divide by zero.");
  2708.       if (mips_trap)
  2709.         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
  2710.       else
  2711.         macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
  2712.       return;
  2713.     }
  2714.  
  2715.       mips_emit_delays ();
  2716.       ++mips_noreorder;
  2717.       mips_any_noreorder = 1;
  2718.       macro_build ((char *) NULL, &icnt, NULL,
  2719.            dbl ? "ddiv" : "div",
  2720.            "z,s,t", sreg, treg);
  2721.       if (mips_trap)
  2722.     macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
  2723.       else
  2724.     {
  2725.       expr1.X_add_number = 8;
  2726.       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
  2727.       macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  2728.       macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
  2729.     }
  2730.       expr1.X_add_number = -1;
  2731.       macro_build ((char *) NULL, &icnt, &expr1,
  2732.            dbl ? "daddiu" : "addiu",
  2733.            "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
  2734.       expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
  2735.       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
  2736.       if (dbl)
  2737.     {
  2738.       expr1.X_add_number = 1;
  2739.       macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
  2740.                (int) BFD_RELOC_LO16);
  2741.       macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
  2742.                31);
  2743.     }
  2744.       else
  2745.     {
  2746.       expr1.X_add_number = 0x80000000;
  2747.       macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
  2748.                (int) BFD_RELOC_HI16);
  2749.     }
  2750.       if (mips_trap)
  2751.     macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
  2752.       else
  2753.     {
  2754.       expr1.X_add_number = 8;
  2755.       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
  2756.       macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  2757.       macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
  2758.     }
  2759.       --mips_noreorder;
  2760.       macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
  2761.       break;
  2762.  
  2763.     case M_DIV_3I:
  2764.       s = "div";
  2765.       s2 = "mflo";
  2766.       goto do_divi;
  2767.     case M_DIVU_3I:
  2768.       s = "divu";
  2769.       s2 = "mflo";
  2770.       goto do_divi;
  2771.     case M_REM_3I:
  2772.       s = "div";
  2773.       s2 = "mfhi";
  2774.       goto do_divi;
  2775.     case M_REMU_3I:
  2776.       s = "divu";
  2777.       s2 = "mfhi";
  2778.       goto do_divi;
  2779.     case M_DDIV_3I:
  2780.       dbl = 1;
  2781.       s = "ddiv";
  2782.       s2 = "mflo";
  2783.       goto do_divi;
  2784.     case M_DDIVU_3I:
  2785.       dbl = 1;
  2786.       s = "ddivu";
  2787.       s2 = "mflo";
  2788.       goto do_divi;
  2789.     case M_DREM_3I:
  2790.       dbl = 1;
  2791.       s = "ddiv";
  2792.       s2 = "mfhi";
  2793.       goto do_divi;
  2794.     case M_DREMU_3I:
  2795.       dbl = 1;
  2796.       s = "ddivu";
  2797.       s2 = "mfhi";
  2798.     do_divi:
  2799.       if (imm_expr.X_add_number == 0)
  2800.     {
  2801.       as_warn ("Divide by zero.");
  2802.       if (mips_trap)
  2803.         macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
  2804.       else
  2805.         macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
  2806.       return;
  2807.     }
  2808.       if (imm_expr.X_add_number == 1)
  2809.     {
  2810.       if (strcmp (s2, "mflo") == 0)
  2811.         macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg,
  2812.              sreg);
  2813.       else
  2814.         macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
  2815.       return;
  2816.     }
  2817.       if (imm_expr.X_add_number == -1
  2818.       && s[strlen (s) - 1] != 'u')
  2819.     {
  2820.       if (strcmp (s2, "mflo") == 0)
  2821.         {
  2822.           if (dbl)
  2823.         macro_build ((char *) NULL, &icnt, NULL, "dneg", "d,w", dreg,
  2824.                  sreg);
  2825.           else
  2826.         macro_build ((char *) NULL, &icnt, NULL, "neg", "d,w", dreg,
  2827.                  sreg);
  2828.         }
  2829.       else
  2830.         macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
  2831.       return;
  2832.     }
  2833.  
  2834.       load_register (&icnt, AT, &imm_expr, dbl);
  2835.       macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
  2836.       macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
  2837.       break;
  2838.  
  2839.     case M_DIVU_3:
  2840.       s = "divu";
  2841.       s2 = "mflo";
  2842.       goto do_divu3;
  2843.     case M_REMU_3:
  2844.       s = "divu";
  2845.       s2 = "mfhi";
  2846.       goto do_divu3;
  2847.     case M_DDIVU_3:
  2848.       s = "ddivu";
  2849.       s2 = "mflo";
  2850.       goto do_divu3;
  2851.     case M_DREMU_3:
  2852.       s = "ddivu";
  2853.       s2 = "mfhi";
  2854.     do_divu3:
  2855.       mips_emit_delays ();
  2856.       ++mips_noreorder;
  2857.       mips_any_noreorder = 1;
  2858.       macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
  2859.       if (mips_trap)
  2860.     macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
  2861.       else
  2862.     {
  2863.       expr1.X_add_number = 8;
  2864.       macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
  2865.       macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  2866.       macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
  2867.     }
  2868.       --mips_noreorder;
  2869.       macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
  2870.       return;
  2871.  
  2872.     case M_DLA_AB:
  2873.       dbl = 1;
  2874.     case M_LA_AB:
  2875.       /* Load the address of a symbol into a register.  If breg is not
  2876.      zero, we then add a base register to it.  */
  2877.  
  2878.       /* When generating embedded PIC code, we permit expressions of
  2879.      the form
  2880.        la    $4,foo-bar
  2881.      where bar is an address in the .text section.  These are used
  2882.      when getting the addresses of functions.  We don't permit
  2883.      X_add_number to be non-zero, because if the symbol is
  2884.      external the relaxing code needs to know that any addend is
  2885.      purely the offset to X_op_symbol.  */
  2886.       if (mips_pic == EMBEDDED_PIC
  2887.       && offset_expr.X_op == O_subtract
  2888.       && now_seg == text_section
  2889.       && (offset_expr.X_op_symbol->sy_value.X_op == O_constant
  2890.           ? S_GET_SEGMENT (offset_expr.X_op_symbol) == text_section
  2891.           : (offset_expr.X_op_symbol->sy_value.X_op == O_symbol
  2892.          && (S_GET_SEGMENT (offset_expr.X_op_symbol
  2893.                     ->sy_value.X_add_symbol)
  2894.              == text_section)))
  2895.       && breg == 0
  2896.       && offset_expr.X_add_number == 0)
  2897.     {
  2898.       macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
  2899.                treg, (int) BFD_RELOC_PCREL_HI16_S);
  2900.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2901.                mips_isa < 3 ? "addiu" : "daddiu",
  2902.                "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
  2903.       return;
  2904.     }
  2905.  
  2906.       if (offset_expr.X_op != O_symbol
  2907.       && offset_expr.X_op != O_constant)
  2908.     {
  2909.       as_bad ("expression too complex");
  2910.       offset_expr.X_op = O_constant;
  2911.     }
  2912.  
  2913.       if (treg == breg)
  2914.     {
  2915.       tempreg = AT;
  2916.       used_at = 1;
  2917.     }
  2918.       else
  2919.     {
  2920.       tempreg = treg;
  2921.       used_at = 0;
  2922.     }
  2923.  
  2924.       if (offset_expr.X_op == O_constant)
  2925.     load_register (&icnt, tempreg, &offset_expr, dbl);
  2926.       else if (mips_pic == NO_PIC)
  2927.     {
  2928.       /* If this is a reference to an GP relative symbol, we want
  2929.            addiu    $tempreg,$gp,<sym>    (BFD_RELOC_MIPS_GPREL)
  2930.          Otherwise we want
  2931.            lui    $tempreg,<sym>        (BFD_RELOC_HI16_S)
  2932.            addiu    $tempreg,$tempreg,<sym>    (BFD_RELOC_LO16)
  2933.          If we have a constant, we need two instructions anyhow,
  2934.          so we may as well always use the latter form.  */
  2935.       if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
  2936.           || nopic_need_relax (offset_expr.X_add_symbol))
  2937.         p = NULL;
  2938.       else
  2939.         {
  2940.           frag_grow (20);
  2941.           macro_build ((char *) NULL, &icnt, &offset_expr,
  2942.                mips_isa < 3 ? "addiu" : "daddiu",
  2943.                "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
  2944.           p = frag_var (rs_machine_dependent, 8, 0,
  2945.                 RELAX_ENCODE (4, 8, 0, 4, 0,
  2946.                       mips_warn_about_macros),
  2947.                 offset_expr.X_add_symbol, (long) 0,
  2948.                 (char *) NULL);
  2949.         }
  2950.       macro_build_lui (p, &icnt, &offset_expr, tempreg);
  2951.       if (p != NULL)
  2952.         p += 4;
  2953.       macro_build (p, &icnt, &offset_expr,
  2954.                mips_isa < 3 ? "addiu" : "daddiu",
  2955.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  2956.     }
  2957.       else if (mips_pic == SVR4_PIC && ! mips_big_got)
  2958.     {
  2959.       /* If this is a reference to an external symbol, and there
  2960.          is no constant, we want
  2961.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  2962.          For a local symbol, we want
  2963.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  2964.            nop
  2965.            addiu    $tempreg,$tempreg,<sym>    (BFD_RELOC_LO16)
  2966.  
  2967.          If we have a small constant, and this is a reference to
  2968.          an external symbol, we want
  2969.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  2970.            nop
  2971.            addiu    $tempreg,$tempreg,<constant>
  2972.          For a local symbol, we want the same instruction
  2973.          sequence, but we output a BFD_RELOC_LO16 reloc on the
  2974.          addiu instruction.
  2975.  
  2976.          If we have a large constant, and this is a reference to
  2977.          an external symbol, we want
  2978.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  2979.            lui    $at,<hiconstant>
  2980.            addiu    $at,$at,<loconstant>
  2981.            addu    $tempreg,$tempreg,$at
  2982.          For a local symbol, we want the same instruction
  2983.          sequence, but we output a BFD_RELOC_LO16 reloc on the
  2984.          addiu instruction.  */
  2985.       expr1.X_add_number = offset_expr.X_add_number;
  2986.       offset_expr.X_add_number = 0;
  2987.       frag_grow (32);
  2988.       macro_build ((char *) NULL, &icnt, &offset_expr,
  2989.                dbl ? "ld" : "lw",
  2990.                "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
  2991.       if (expr1.X_add_number == 0)
  2992.         {
  2993.           int off;
  2994.  
  2995.           if (breg == 0)
  2996.         off = 0;
  2997.           else
  2998.         {
  2999.           /* We're going to put in an addu instruction using
  3000.              tempreg, so we may as well insert the nop right
  3001.              now.  */
  3002.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3003.                    "nop", "");
  3004.           off = 4;
  3005.         }
  3006.           p = frag_var (rs_machine_dependent, 8 - off, 0,
  3007.                 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
  3008.                       (breg == 0
  3009.                        ? mips_warn_about_macros
  3010.                        : 0)),
  3011.                 offset_expr.X_add_symbol, (long) 0,
  3012.                 (char *) NULL);
  3013.           if (breg == 0)
  3014.         {
  3015.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3016.           p += 4;
  3017.         }
  3018.           macro_build (p, &icnt, &expr1,
  3019.                mips_isa < 3 ? "addiu" : "daddiu",
  3020.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  3021.           /* FIXME: If breg == 0, and the next instruction uses
  3022.          $tempreg, then if this variant case is used an extra
  3023.          nop will be generated.  */
  3024.         }
  3025.       else if (expr1.X_add_number >= -0x8000
  3026.            && expr1.X_add_number < 0x8000)
  3027.         {
  3028.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3029.                "nop", "");
  3030.           macro_build ((char *) NULL, &icnt, &expr1,
  3031.                mips_isa < 3 ? "addiu" : "daddiu",
  3032.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  3033.           (void) frag_var (rs_machine_dependent, 0, 0,
  3034.                    RELAX_ENCODE (0, 0, -12, -4, 0, 0),
  3035.                    offset_expr.X_add_symbol, (long) 0,
  3036.                    (char *) NULL);
  3037.         }
  3038.       else
  3039.         {
  3040.           int off1;
  3041.  
  3042.           /* If we are going to add in a base register, and the
  3043.          target register and the base register are the same,
  3044.          then we are using AT as a temporary register.  Since
  3045.          we want to load the constant into AT, we add our
  3046.          current AT (from the global offset table) and the
  3047.          register into the register now, and pretend we were
  3048.          not using a base register.  */
  3049.           if (breg != treg)
  3050.         off1 = 0;
  3051.           else
  3052.         {
  3053.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3054.                    "nop", "");
  3055.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3056.                    mips_isa < 3 ? "addu" : "daddu",
  3057.                    "d,v,t", treg, AT, breg);
  3058.           breg = 0;
  3059.           tempreg = treg;
  3060.           off1 = -8;
  3061.         }
  3062.  
  3063.           /* Set mips_optimize around the lui instruction to avoid
  3064.          inserting an unnecessary nop after the lw.  */
  3065.           hold_mips_optimize = mips_optimize;
  3066.           mips_optimize = 2;
  3067.           macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
  3068.           mips_optimize = hold_mips_optimize;
  3069.  
  3070.           macro_build ((char *) NULL, &icnt, &expr1,
  3071.                mips_isa < 3 ? "addiu" : "daddiu",
  3072.                "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
  3073.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3074.                mips_isa < 3 ? "addu" : "daddu",
  3075.                "d,v,t", tempreg, tempreg, AT);
  3076.           (void) frag_var (rs_machine_dependent, 0, 0,
  3077.                    RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
  3078.                    offset_expr.X_add_symbol, (long) 0,
  3079.                    (char *) NULL);
  3080.           used_at = 1;
  3081.         }
  3082.     }
  3083.       else if (mips_pic == SVR4_PIC)
  3084.     {
  3085.       int gpdel;
  3086.  
  3087.       /* This is the large GOT case.  If this is a reference to an
  3088.          external symbol, and there is no constant, we want
  3089.            lui    $tempreg,<sym>        (BFD_RELOC_MIPS_GOT_HI16)
  3090.            addu    $tempreg,$tempreg,$gp
  3091.            lw    $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
  3092.          For a local symbol, we want
  3093.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  3094.            nop
  3095.            addiu    $tempreg,$tempreg,<sym>    (BFD_RELOC_LO16)
  3096.  
  3097.          If we have a small constant, and this is a reference to
  3098.          an external symbol, we want
  3099.            lui    $tempreg,<sym>        (BFD_RELOC_MIPS_GOT_HI16)
  3100.            addu    $tempreg,$tempreg,$gp
  3101.            lw    $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
  3102.            nop
  3103.            addiu    $tempreg,$tempreg,<constant>
  3104.          For a local symbol, we want
  3105.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  3106.            nop
  3107.            addiu    $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
  3108.  
  3109.          If we have a large constant, and this is a reference to
  3110.          an external symbol, we want
  3111.            lui    $tempreg,<sym>        (BFD_RELOC_MIPS_GOT_HI16)
  3112.            addu    $tempreg,$tempreg,$gp
  3113.            lw    $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
  3114.            lui    $at,<hiconstant>
  3115.            addiu    $at,$at,<loconstant>
  3116.            addu    $tempreg,$tempreg,$at
  3117.          For a local symbol, we want
  3118.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  3119.            lui    $at,<hiconstant>
  3120.            addiu    $at,$at,<loconstant>    (BFD_RELOC_LO16)
  3121.            addu    $tempreg,$tempreg,$at
  3122.          */
  3123.       expr1.X_add_number = offset_expr.X_add_number;
  3124.       offset_expr.X_add_number = 0;
  3125.       frag_grow (52);
  3126.       if (reg_needs_delay (GP))
  3127.         gpdel = 4;
  3128.       else
  3129.         gpdel = 0;
  3130.       macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
  3131.                tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
  3132.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3133.                mips_isa < 3 ? "addu" : "daddu",
  3134.                "d,v,t", tempreg, tempreg, GP);
  3135.       macro_build ((char *) NULL, &icnt, &offset_expr,
  3136.                dbl ? "ld" : "lw",
  3137.                "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
  3138.                tempreg);
  3139.       if (expr1.X_add_number == 0)
  3140.         {
  3141.           int off;
  3142.  
  3143.           if (breg == 0)
  3144.         off = 0;
  3145.           else
  3146.         {
  3147.           /* We're going to put in an addu instruction using
  3148.              tempreg, so we may as well insert the nop right
  3149.              now.  */
  3150.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3151.                    "nop", "");
  3152.           off = 4;
  3153.         }
  3154.  
  3155.           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
  3156.                 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
  3157.                       8 + gpdel, 0,
  3158.                       (breg == 0
  3159.                        ? mips_warn_about_macros
  3160.                        : 0)),
  3161.                 offset_expr.X_add_symbol, (long) 0,
  3162.                 (char *) NULL);
  3163.         }
  3164.       else if (expr1.X_add_number >= -0x8000
  3165.            && expr1.X_add_number < 0x8000)
  3166.         {
  3167.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3168.                "nop", "");
  3169.           macro_build ((char *) NULL, &icnt, &expr1,
  3170.                mips_isa < 3 ? "addiu" : "daddiu",
  3171.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  3172.  
  3173.           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
  3174.                 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
  3175.                       (breg == 0
  3176.                        ? mips_warn_about_macros
  3177.                        : 0)),
  3178.                 offset_expr.X_add_symbol, (long) 0,
  3179.                 (char *) NULL);
  3180.         }
  3181.       else
  3182.         {
  3183.           int adj, dreg;
  3184.  
  3185.           /* If we are going to add in a base register, and the
  3186.          target register and the base register are the same,
  3187.          then we are using AT as a temporary register.  Since
  3188.          we want to load the constant into AT, we add our
  3189.          current AT (from the global offset table) and the
  3190.          register into the register now, and pretend we were
  3191.          not using a base register.  */
  3192.           if (breg != treg)
  3193.         {
  3194.           adj = 0;
  3195.           dreg = tempreg;
  3196.         }
  3197.           else
  3198.         {
  3199.           assert (tempreg == AT);
  3200.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3201.                    "nop", "");
  3202.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3203.                    mips_isa < 3 ? "addu" : "daddu",
  3204.                    "d,v,t", treg, AT, breg);
  3205.           dreg = treg;
  3206.           adj = 8;
  3207.         }
  3208.  
  3209.           /* Set mips_optimize around the lui instruction to avoid
  3210.          inserting an unnecessary nop after the lw.  */
  3211.           hold_mips_optimize = mips_optimize;
  3212.           mips_optimize = 2;
  3213.           macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
  3214.           mips_optimize = hold_mips_optimize;
  3215.  
  3216.           macro_build ((char *) NULL, &icnt, &expr1,
  3217.                mips_isa < 3 ? "addiu" : "daddiu",
  3218.                "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
  3219.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3220.                mips_isa < 3 ? "addu" : "daddu",
  3221.                "d,v,t", dreg, dreg, AT);
  3222.  
  3223.           p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
  3224.                 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
  3225.                       8 + gpdel, 0,
  3226.                       (breg == 0
  3227.                        ? mips_warn_about_macros
  3228.                        : 0)),
  3229.                 offset_expr.X_add_symbol, (long) 0,
  3230.                 (char *) NULL);
  3231.  
  3232.           used_at = 1;
  3233.         }
  3234.  
  3235.       if (gpdel > 0)
  3236.         {
  3237.           /* This is needed because this instruction uses $gp, but
  3238.                  the first instruction on the main stream does not.  */
  3239.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3240.           p += 4;
  3241.         }
  3242.       macro_build (p, &icnt, &offset_expr,
  3243.                dbl ? "ld" : "lw",
  3244.                "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
  3245.       p += 4;
  3246.       if (expr1.X_add_number >= -0x8000
  3247.           && expr1.X_add_number < 0x8000)
  3248.         {
  3249.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3250.           p += 4;
  3251.           macro_build (p, &icnt, &expr1,
  3252.                mips_isa < 3 ? "addiu" : "daddiu",
  3253.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  3254.           /* FIXME: If add_number is 0, and there was no base
  3255.                  register, the external symbol case ended with a load,
  3256.                  so if the symbol turns out to not be external, and
  3257.                  the next instruction uses tempreg, an unnecessary nop
  3258.                  will be inserted.  */
  3259.         }
  3260.       else
  3261.         {
  3262.           if (breg == treg)
  3263.         {
  3264.           /* We must add in the base register now, as in the
  3265.                      external symbol case.  */
  3266.           assert (tempreg == AT);
  3267.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3268.           p += 4;
  3269.           macro_build (p, &icnt, (expressionS *) NULL,
  3270.                    mips_isa < 3 ? "addu" : "daddu",
  3271.                    "d,v,t", treg, AT, breg);
  3272.           p += 4;
  3273.           tempreg = treg;
  3274.           /* We set breg to 0 because we have arranged to add
  3275.                      it in in both cases.  */
  3276.           breg = 0;
  3277.         }
  3278.  
  3279.           macro_build_lui (p, &icnt, &expr1, AT);
  3280.           p += 4;
  3281.           macro_build (p, &icnt, &expr1,
  3282.                mips_isa < 3 ? "addiu" : "daddiu",
  3283.                "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
  3284.           p += 4;
  3285.           macro_build (p, &icnt, (expressionS *) NULL,
  3286.                mips_isa < 3 ? "addu" : "daddu",
  3287.                "d,v,t", tempreg, tempreg, AT);
  3288.           p += 4;
  3289.         }
  3290.     }
  3291.       else if (mips_pic == EMBEDDED_PIC)
  3292.     {
  3293.       /* We use
  3294.            addiu    $tempreg,$gp,<sym>    (BFD_RELOC_MIPS_GPREL)
  3295.          */
  3296.       macro_build ((char *) NULL, &icnt, &offset_expr,
  3297.                mips_isa < 3 ? "addiu" : "daddiu",
  3298.                "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
  3299.     }
  3300.       else
  3301.     abort ();
  3302.  
  3303.       if (breg != 0)
  3304.     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3305.              mips_isa < 3 ? "addu" : "daddu",
  3306.              "d,v,t", treg, tempreg, breg);
  3307.  
  3308.       if (! used_at)
  3309.     return;
  3310.  
  3311.       break;
  3312.  
  3313.     case M_J_A:
  3314.       /* The j instruction may not be used in PIC code, since it
  3315.      requires an absolute address.  We convert it to a b
  3316.      instruction.  */
  3317.       if (mips_pic == NO_PIC)
  3318.     macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
  3319.       else
  3320.     macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
  3321.       return;
  3322.  
  3323.       /* The jal instructions must be handled as macros because when
  3324.      generating PIC code they expand to multi-instruction
  3325.      sequences.  Normally they are simple instructions.  */
  3326.     case M_JAL_1:
  3327.       dreg = RA;
  3328.       /* Fall through.  */
  3329.     case M_JAL_2:
  3330.       if (mips_pic == NO_PIC
  3331.       || mips_pic == EMBEDDED_PIC)
  3332.     macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
  3333.              "d,s", dreg, sreg);
  3334.       else if (mips_pic == SVR4_PIC)
  3335.     {
  3336.       if (sreg != PIC_CALL_REG)
  3337.         as_warn ("MIPS PIC call to register other than $25");
  3338.       
  3339.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
  3340.                "d,s", dreg, sreg);
  3341.       if (mips_cprestore_offset < 0)
  3342.         as_warn ("No .cprestore pseudo-op used in PIC code");
  3343.       else
  3344.         {
  3345.           expr1.X_add_number = mips_cprestore_offset;
  3346.           macro_build ((char *) NULL, &icnt, &expr1,
  3347.                mips_isa < 3 ? "lw" : "ld",
  3348.                "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
  3349.         }
  3350.     }
  3351.       else
  3352.     abort ();
  3353.  
  3354.       return;
  3355.  
  3356.     case M_JAL_A:
  3357.       if (mips_pic == NO_PIC)
  3358.     macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
  3359.       else if (mips_pic == SVR4_PIC)
  3360.     {
  3361.       /* If this is a reference to an external symbol, and we are
  3362.          using a small GOT, we want
  3363.            lw    $25,<sym>($gp)        (BFD_RELOC_MIPS_CALL16)
  3364.            nop
  3365.            jalr    $25
  3366.            nop
  3367.            lw    $gp,cprestore($sp)
  3368.          The cprestore value is set using the .cprestore
  3369.          pseudo-op.  If we are using a big GOT, we want
  3370.            lui    $25,<sym>        (BFD_RELOC_MIPS_CALL_HI16)
  3371.            addu    $25,$25,$gp
  3372.            lw    $25,<sym>($25)        (BFD_RELOC_MIPS_CALL_LO16)
  3373.            nop
  3374.            jalr    $25
  3375.            nop
  3376.            lw    $gp,cprestore($sp)
  3377.          If the symbol is not external, we want
  3378.            lw    $25,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  3379.            nop
  3380.            addiu    $25,$25,<sym>        (BFD_RELOC_LO16)
  3381.            jalr    $25
  3382.            nop
  3383.            lw $gp,cprestore($sp) */
  3384.       frag_grow (40);
  3385.       if (! mips_big_got)
  3386.         {
  3387.           macro_build ((char *) NULL, &icnt, &offset_expr,
  3388.                mips_isa < 3 ? "lw" : "ld",
  3389.                "t,o(b)", PIC_CALL_REG,
  3390.                (int) BFD_RELOC_MIPS_CALL16, GP);
  3391.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3392.                "nop", "");
  3393.           p = frag_var (rs_machine_dependent, 4, 0,
  3394.                 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
  3395.                 offset_expr.X_add_symbol, (long) 0, (char *) NULL);
  3396.         }
  3397.       else
  3398.         {
  3399.           int gpdel;
  3400.  
  3401.           if (reg_needs_delay (GP))
  3402.         gpdel = 4;
  3403.           else
  3404.         gpdel = 0;
  3405.           macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
  3406.                PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
  3407.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3408.                mips_isa < 3 ? "addu" : "daddu",
  3409.                "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
  3410.           macro_build ((char *) NULL, &icnt, &offset_expr,
  3411.                mips_isa < 3 ? "lw" : "ld",
  3412.                "t,o(b)", PIC_CALL_REG,
  3413.                (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
  3414.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3415.                "nop", "");
  3416.           p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
  3417.                 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
  3418.                       0, 0),
  3419.                 offset_expr.X_add_symbol, (long) 0, (char *) NULL);
  3420.           if (gpdel > 0)
  3421.         {
  3422.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3423.           p += 4;
  3424.         }
  3425.           macro_build (p, &icnt, &offset_expr,
  3426.                mips_isa < 3 ? "lw" : "ld",
  3427.                "t,o(b)", PIC_CALL_REG,
  3428.                (int) BFD_RELOC_MIPS_GOT16, GP);
  3429.           p += 4;
  3430.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3431.           p += 4;
  3432.         }               
  3433.       macro_build (p, &icnt, &offset_expr,
  3434.                mips_isa < 3 ? "addiu" : "daddiu",
  3435.                "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
  3436.                (int) BFD_RELOC_LO16);
  3437.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3438.                "jalr", "s", PIC_CALL_REG);
  3439.       if (mips_cprestore_offset < 0)
  3440.         as_warn ("No .cprestore pseudo-op used in PIC code");
  3441.       else
  3442.         {
  3443.           if (mips_noreorder)
  3444.         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3445.                  "nop", "");
  3446.           expr1.X_add_number = mips_cprestore_offset;
  3447.           macro_build ((char *) NULL, &icnt, &expr1,
  3448.                mips_isa < 3 ? "lw" : "ld",
  3449.                "t,o(b)", GP, (int) BFD_RELOC_LO16,
  3450.                mips_frame_reg);
  3451.         }
  3452.     }
  3453.       else if (mips_pic == EMBEDDED_PIC)
  3454.     {
  3455.       macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
  3456.       /* The linker may expand the call to a longer sequence which
  3457.          uses $at, so we must break rather than return.  */
  3458.       break;
  3459.     }
  3460.       else
  3461.     abort ();
  3462.  
  3463.       return;
  3464.  
  3465.     case M_LB_AB:
  3466.       s = "lb";
  3467.       goto ld;
  3468.     case M_LBU_AB:
  3469.       s = "lbu";
  3470.       goto ld;
  3471.     case M_LH_AB:
  3472.       s = "lh";
  3473.       goto ld;
  3474.     case M_LHU_AB:
  3475.       s = "lhu";
  3476.       goto ld;
  3477.     case M_LW_AB:
  3478.       s = "lw";
  3479.       goto ld;
  3480.     case M_LWC0_AB:
  3481.       s = "lwc0";
  3482.       coproc = 1;
  3483.       goto ld;
  3484.     case M_LWC1_AB:
  3485.       s = "lwc1";
  3486.       coproc = 1;
  3487.       goto ld;
  3488.     case M_LWC2_AB:
  3489.       s = "lwc2";
  3490.       coproc = 1;
  3491.       goto ld;
  3492.     case M_LWC3_AB:
  3493.       s = "lwc3";
  3494.       coproc = 1;
  3495.       goto ld;
  3496.     case M_LWL_AB:
  3497.       s = "lwl";
  3498.       lr = 1;
  3499.       goto ld;
  3500.     case M_LWR_AB:
  3501.       s = "lwr";
  3502.       lr = 1;
  3503.       goto ld;
  3504.     case M_LDC1_AB:
  3505.       s = "ldc1";
  3506.       coproc = 1;
  3507.       goto ld;
  3508.     case M_LDC2_AB:
  3509.       s = "ldc2";
  3510.       coproc = 1;
  3511.       goto ld;
  3512.     case M_LDC3_AB:
  3513.       s = "ldc3";
  3514.       coproc = 1;
  3515.       goto ld;
  3516.     case M_LDL_AB:
  3517.       s = "ldl";
  3518.       lr = 1;
  3519.       goto ld;
  3520.     case M_LDR_AB:
  3521.       s = "ldr";
  3522.       lr = 1;
  3523.       goto ld;
  3524.     case M_LL_AB:
  3525.       s = "ll";
  3526.       goto ld;
  3527.     case M_LLD_AB:
  3528.       s = "lld";
  3529.       goto ld;
  3530.     case M_LWU_AB:
  3531.       s = "lwu";
  3532.     ld:
  3533.       if (breg == treg || coproc || lr)
  3534.     {
  3535.       tempreg = AT;
  3536.       used_at = 1;
  3537.     }
  3538.       else
  3539.     {
  3540.       tempreg = treg;
  3541.       used_at = 0;
  3542.     }
  3543.       goto ld_st;
  3544.     case M_SB_AB:
  3545.       s = "sb";
  3546.       goto st;
  3547.     case M_SH_AB:
  3548.       s = "sh";
  3549.       goto st;
  3550.     case M_SW_AB:
  3551.       s = "sw";
  3552.       goto st;
  3553.     case M_SWC0_AB:
  3554.       s = "swc0";
  3555.       coproc = 1;
  3556.       goto st;
  3557.     case M_SWC1_AB:
  3558.       s = "swc1";
  3559.       coproc = 1;
  3560.       goto st;
  3561.     case M_SWC2_AB:
  3562.       s = "swc2";
  3563.       coproc = 1;
  3564.       goto st;
  3565.     case M_SWC3_AB:
  3566.       s = "swc3";
  3567.       coproc = 1;
  3568.       goto st;
  3569.     case M_SWL_AB:
  3570.       s = "swl";
  3571.       goto st;
  3572.     case M_SWR_AB:
  3573.       s = "swr";
  3574.       goto st;
  3575.     case M_SC_AB:
  3576.       s = "sc";
  3577.       goto st;
  3578.     case M_SCD_AB:
  3579.       s = "scd";
  3580.       goto st;
  3581.     case M_SDC1_AB:
  3582.       s = "sdc1";
  3583.       coproc = 1;
  3584.       goto st;
  3585.     case M_SDC2_AB:
  3586.       s = "sdc2";
  3587.       coproc = 1;
  3588.       goto st;
  3589.     case M_SDC3_AB:
  3590.       s = "sdc3";
  3591.       coproc = 1;
  3592.       goto st;
  3593.     case M_SDL_AB:
  3594.       s = "sdl";
  3595.       goto st;
  3596.     case M_SDR_AB:
  3597.       s = "sdr";
  3598.     st:
  3599.       tempreg = AT;
  3600.       used_at = 1;
  3601.     ld_st:
  3602.       if (mask == M_LWC1_AB
  3603.       || mask == M_SWC1_AB
  3604.       || mask == M_LDC1_AB
  3605.       || mask == M_SDC1_AB
  3606.       || mask == M_L_DAB
  3607.       || mask == M_S_DAB)
  3608.     fmt = "T,o(b)";
  3609.       else if (coproc)
  3610.     fmt = "E,o(b)";
  3611.       else
  3612.     fmt = "t,o(b)";
  3613.  
  3614.       if (offset_expr.X_op != O_constant
  3615.       && offset_expr.X_op != O_symbol)
  3616.     {
  3617.       as_bad ("expression too complex");
  3618.       offset_expr.X_op = O_constant;
  3619.     }
  3620.  
  3621.       /* A constant expression in PIC code can be handled just as it
  3622.      is in non PIC code.  */
  3623.       if (mips_pic == NO_PIC
  3624.       || offset_expr.X_op == O_constant)
  3625.     {
  3626.       /* If this is a reference to a GP relative symbol, and there
  3627.          is no base register, we want
  3628.            <op>    $treg,<sym>($gp)    (BFD_RELOC_MIPS_GPREL)
  3629.          Otherwise, if there is no base register, we want
  3630.            lui    $tempreg,<sym>        (BFD_RELOC_HI16_S)
  3631.            <op>    $treg,<sym>($tempreg)    (BFD_RELOC_LO16)
  3632.          If we have a constant, we need two instructions anyhow,
  3633.          so we always use the latter form.
  3634.  
  3635.          If we have a base register, and this is a reference to a
  3636.          GP relative symbol, we want
  3637.            addu    $tempreg,$breg,$gp
  3638.            <op>    $treg,<sym>($tempreg)    (BFD_RELOC_MIPS_GPREL)
  3639.          Otherwise we want
  3640.            lui    $tempreg,<sym>        (BFD_RELOC_HI16_S)
  3641.            addu    $tempreg,$tempreg,$breg
  3642.            <op>    $treg,<sym>($tempreg)    (BFD_RELOC_LO16)
  3643.          With a constant we always use the latter case.  */
  3644.       if (breg == 0)
  3645.         {
  3646.           if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
  3647.           || nopic_need_relax (offset_expr.X_add_symbol))
  3648.         p = NULL;
  3649.           else
  3650.         {
  3651.           frag_grow (20);
  3652.           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  3653.                    treg, (int) BFD_RELOC_MIPS_GPREL, GP);
  3654.           p = frag_var (rs_machine_dependent, 8, 0,
  3655.                 RELAX_ENCODE (4, 8, 0, 4, 0,
  3656.                           (mips_warn_about_macros
  3657.                            || (used_at && mips_noat))),
  3658.                 offset_expr.X_add_symbol, (long) 0,
  3659.                 (char *) NULL);
  3660.           used_at = 0;
  3661.         }
  3662.           macro_build_lui (p, &icnt, &offset_expr, tempreg);
  3663.           if (p != NULL)
  3664.         p += 4;
  3665.           macro_build (p, &icnt, &offset_expr, s, fmt, treg,
  3666.                (int) BFD_RELOC_LO16, tempreg);
  3667.         }
  3668.       else
  3669.         {
  3670.           if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
  3671.           || nopic_need_relax (offset_expr.X_add_symbol))
  3672.         p = NULL;
  3673.           else
  3674.         {
  3675.           frag_grow (28);
  3676.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3677.                    mips_isa < 3 ? "addu" : "daddu",
  3678.                    "d,v,t", tempreg, breg, GP);
  3679.           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  3680.                    treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
  3681.           p = frag_var (rs_machine_dependent, 12, 0,
  3682.                 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
  3683.                 offset_expr.X_add_symbol, (long) 0,
  3684.                 (char *) NULL);
  3685.         }
  3686.           macro_build_lui (p, &icnt, &offset_expr, tempreg);
  3687.           if (p != NULL)
  3688.         p += 4;
  3689.           macro_build (p, &icnt, (expressionS *) NULL,
  3690.                mips_isa < 3 ? "addu" : "daddu",
  3691.                "d,v,t", tempreg, tempreg, breg);
  3692.           if (p != NULL)
  3693.         p += 4;
  3694.           macro_build (p, &icnt, &offset_expr, s, fmt, treg,
  3695.                (int) BFD_RELOC_LO16, tempreg);
  3696.         }
  3697.     }
  3698.       else if (mips_pic == SVR4_PIC && ! mips_big_got)
  3699.     {
  3700.       /* If this is a reference to an external symbol, we want
  3701.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  3702.            nop
  3703.            <op>    $treg,0($tempreg)
  3704.          Otherwise we want
  3705.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  3706.            nop
  3707.            addiu    $tempreg,$tempreg,<sym>    (BFD_RELOC_LO16)
  3708.            <op>    $treg,0($tempreg)
  3709.          If there is a base register, we add it to $tempreg before
  3710.          the <op>.  If there is a constant, we stick it in the
  3711.          <op> instruction.  We don't handle constants larger than
  3712.          16 bits, because we have no way to load the upper 16 bits
  3713.          (actually, we could handle them for the subset of cases
  3714.          in which we are not using $at).  */
  3715.       assert (offset_expr.X_op == O_symbol);
  3716.       expr1.X_add_number = offset_expr.X_add_number;
  3717.       offset_expr.X_add_number = 0;
  3718.       if (expr1.X_add_number < -0x8000
  3719.           || expr1.X_add_number >= 0x8000)
  3720.         as_bad ("PIC code offset overflow (max 16 signed bits)");
  3721.       frag_grow (20);
  3722.       macro_build ((char *) NULL, &icnt, &offset_expr,
  3723.                mips_isa < 3 ? "lw" : "ld",
  3724.                "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
  3725.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
  3726.       p = frag_var (rs_machine_dependent, 4, 0, 
  3727.             RELAX_ENCODE (0, 4, -8, 0, 0, 0),
  3728.             offset_expr.X_add_symbol, (long) 0,
  3729.             (char *) NULL);
  3730.       macro_build (p, &icnt, &offset_expr,
  3731.                mips_isa < 3 ? "addiu" : "daddiu",
  3732.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  3733.       if (breg != 0)
  3734.         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3735.              mips_isa < 3 ? "addu" : "daddu",
  3736.              "d,v,t", tempreg, tempreg, breg);
  3737.       macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
  3738.                (int) BFD_RELOC_LO16, tempreg);
  3739.     }
  3740.       else if (mips_pic == SVR4_PIC)
  3741.     {
  3742.       int gpdel;
  3743.  
  3744.       /* If this is a reference to an external symbol, we want
  3745.            lui    $tempreg,<sym>        (BFD_RELOC_MIPS_GOT_HI16)
  3746.            addu    $tempreg,$tempreg,$gp
  3747.            lw    $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
  3748.            <op>    $treg,0($tempreg)
  3749.          Otherwise we want
  3750.            lw    $tempreg,<sym>($gp)    (BFD_RELOC_MIPS_GOT16)
  3751.            nop
  3752.            addiu    $tempreg,$tempreg,<sym>    (BFD_RELOC_LO16)
  3753.            <op>    $treg,0($tempreg)
  3754.          If there is a base register, we add it to $tempreg before
  3755.          the <op>.  If there is a constant, we stick it in the
  3756.          <op> instruction.  We don't handle constants larger than
  3757.          16 bits, because we have no way to load the upper 16 bits
  3758.          (actually, we could handle them for the subset of cases
  3759.          in which we are not using $at).  */
  3760.       assert (offset_expr.X_op == O_symbol);
  3761.       expr1.X_add_number = offset_expr.X_add_number;
  3762.       offset_expr.X_add_number = 0;
  3763.       if (expr1.X_add_number < -0x8000
  3764.           || expr1.X_add_number >= 0x8000)
  3765.         as_bad ("PIC code offset overflow (max 16 signed bits)");
  3766.       if (reg_needs_delay (GP))
  3767.         gpdel = 4;
  3768.       else
  3769.         gpdel = 0;
  3770.       frag_grow (36);
  3771.       macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
  3772.                tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
  3773.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3774.                mips_isa < 3 ? "addu" : "daddu",
  3775.                "d,v,t", tempreg, tempreg, GP);
  3776.       macro_build ((char *) NULL, &icnt, &offset_expr,
  3777.                mips_isa < 3 ? "lw" : "ld",
  3778.                "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
  3779.                tempreg);
  3780.       p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
  3781.             RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
  3782.             offset_expr.X_add_symbol, (long) 0, (char *) NULL);
  3783.       if (gpdel > 0)
  3784.         {
  3785.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3786.           p += 4;
  3787.         }
  3788.       macro_build (p, &icnt, &offset_expr,
  3789.                mips_isa < 3 ? "lw" : "ld",
  3790.                "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
  3791.       p += 4;
  3792.       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  3793.       p += 4;
  3794.       macro_build (p, &icnt, &offset_expr,
  3795.                mips_isa < 3 ? "addiu" : "daddiu",
  3796.                "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
  3797.       if (breg != 0)
  3798.         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3799.              mips_isa < 3 ? "addu" : "daddu",
  3800.              "d,v,t", tempreg, tempreg, breg);
  3801.       macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
  3802.                (int) BFD_RELOC_LO16, tempreg);
  3803.     }
  3804.       else if (mips_pic == EMBEDDED_PIC)
  3805.     {
  3806.       /* If there is no base register, we want
  3807.            <op>    $treg,<sym>($gp)    (BFD_RELOC_MIPS_GPREL)
  3808.          If there is a base register, we want
  3809.            addu    $tempreg,$breg,$gp
  3810.            <op>    $treg,<sym>($tempreg)    (BFD_RELOC_MIPS_GPREL)
  3811.          */
  3812.       assert (offset_expr.X_op == O_symbol);
  3813.       if (breg == 0)
  3814.         {
  3815.           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  3816.                treg, (int) BFD_RELOC_MIPS_GPREL, GP);
  3817.           used_at = 0;
  3818.         }
  3819.       else
  3820.         {
  3821.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3822.                mips_isa < 3 ? "addu" : "daddu",
  3823.                "d,v,t", tempreg, breg, GP);
  3824.           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  3825.                treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
  3826.         }
  3827.     }
  3828.       else
  3829.     abort ();
  3830.  
  3831.       if (! used_at)
  3832.     return;
  3833.  
  3834.       break;
  3835.  
  3836.     case M_LI:
  3837.     case M_LI_S:
  3838.       load_register (&icnt, treg, &imm_expr, 0);
  3839.       return;
  3840.  
  3841.     case M_DLI:
  3842.       load_register (&icnt, treg, &imm_expr, 1);
  3843.       return;
  3844.  
  3845.     case M_LI_SS:
  3846.       if (imm_expr.X_op == O_constant)
  3847.     {
  3848.       load_register (&icnt, AT, &imm_expr, 0);
  3849.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  3850.                "mtc1", "t,G", AT, treg);
  3851.       break;
  3852.     }
  3853.       else
  3854.     {
  3855.       assert (offset_expr.X_op == O_symbol
  3856.           && strcmp (segment_name (S_GET_SEGMENT
  3857.                        (offset_expr.X_add_symbol)),
  3858.                  ".lit4") == 0
  3859.           && offset_expr.X_add_number == 0);
  3860.       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
  3861.                treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
  3862.       return;
  3863.     }
  3864.  
  3865.     case M_LI_D:
  3866.       /* We know that sym is in the .rdata section.  First we get the
  3867.      upper 16 bits of the address.  */
  3868.       if (mips_pic == NO_PIC)
  3869.     {
  3870.       /* FIXME: This won't work for a 64 bit address.  */
  3871.       macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
  3872.     }
  3873.       else if (mips_pic == SVR4_PIC)
  3874.     {
  3875.       macro_build ((char *) NULL, &icnt, &offset_expr,
  3876.                mips_isa < 3 ? "lw" : "ld",
  3877.                "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
  3878.     }
  3879.       else if (mips_pic == EMBEDDED_PIC)
  3880.     {
  3881.       /* For embedded PIC we pick up the entire address off $gp in
  3882.          a single instruction.  */
  3883.       macro_build ((char *) NULL, &icnt, &offset_expr,
  3884.                mips_isa < 3 ? "addiu" : "daddiu",
  3885.                "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
  3886.       offset_expr.X_op = O_constant;
  3887.       offset_expr.X_add_number = 0;
  3888.     }
  3889.       else
  3890.     abort ();
  3891.     
  3892.       /* Now we load the register(s).  */
  3893.       if (mips_isa >= 3)
  3894.     macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
  3895.              treg, (int) BFD_RELOC_LO16, AT);
  3896.       else
  3897.     {
  3898.       macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
  3899.                treg, (int) BFD_RELOC_LO16, AT);
  3900.       if (treg != 31)
  3901.         {
  3902.           /* FIXME: How in the world do we deal with the possible
  3903.          overflow here?  */
  3904.           offset_expr.X_add_number += 4;
  3905.           macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
  3906.                treg + 1, (int) BFD_RELOC_LO16, AT);
  3907.         }
  3908.     }
  3909.  
  3910.       /* To avoid confusion in tc_gen_reloc, we must ensure that this
  3911.      does not become a variant frag.  */
  3912.       frag_wane (frag_now);
  3913.       frag_new (0);
  3914.  
  3915.       break;
  3916.  
  3917.     case M_LI_DD:
  3918.       assert (offset_expr.X_op == O_symbol
  3919.           && offset_expr.X_add_number == 0);
  3920.       s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
  3921.       if (strcmp (s, ".lit8") == 0)
  3922.     {
  3923.       if (mips_isa >= 2)
  3924.         {
  3925.           macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
  3926.                "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
  3927.           return;
  3928.         }
  3929.       breg = GP;
  3930.       r = BFD_RELOC_MIPS_LITERAL;
  3931.       goto dob;
  3932.     }
  3933.       else
  3934.     {
  3935.       assert (strcmp (s, RDATA_SECTION_NAME) == 0);
  3936.       if (mips_pic == SVR4_PIC)
  3937.         macro_build ((char *) NULL, &icnt, &offset_expr,
  3938.              mips_isa < 3 ? "lw" : "ld",
  3939.              "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
  3940.       else
  3941.         {
  3942.           /* FIXME: This won't work for a 64 bit address.  */
  3943.           macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
  3944.         }
  3945.           
  3946.       if (mips_isa >= 2)
  3947.         {
  3948.           macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
  3949.                "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
  3950.  
  3951.           /* To avoid confusion in tc_gen_reloc, we must ensure
  3952.          that this does not become a variant frag.  */
  3953.           frag_wane (frag_now);
  3954.           frag_new (0);
  3955.  
  3956.           break;
  3957.         }
  3958.       breg = AT;
  3959.       r = BFD_RELOC_LO16;
  3960.       goto dob;
  3961.     }
  3962.  
  3963.     case M_L_DOB:
  3964.       /* Even on a big endian machine $fn comes before $fn+1.  We have
  3965.      to adjust when loading from memory.  */
  3966.       r = BFD_RELOC_LO16;
  3967.     dob:
  3968.       assert (mips_isa < 2);
  3969.       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
  3970.            byte_order == LITTLE_ENDIAN ? treg : treg + 1,
  3971.            (int) r, breg);
  3972.       /* FIXME: A possible overflow which I don't know how to deal
  3973.      with.  */
  3974.       offset_expr.X_add_number += 4;
  3975.       macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
  3976.            byte_order == LITTLE_ENDIAN ? treg + 1 : treg,
  3977.            (int) r, breg);
  3978.  
  3979.       /* To avoid confusion in tc_gen_reloc, we must ensure that this
  3980.      does not become a variant frag.  */
  3981.       frag_wane (frag_now);
  3982.       frag_new (0);
  3983.  
  3984.       if (breg != AT)
  3985.     return;
  3986.       break;
  3987.  
  3988.     case M_L_DAB:
  3989.       /*
  3990.        * The MIPS assembler seems to check for X_add_number not
  3991.        * being double aligned and generating:
  3992.        *    lui    at,%hi(foo+1)
  3993.        *    addu    at,at,v1
  3994.        *    addiu    at,at,%lo(foo+1)
  3995.        *    lwc1    f2,0(at)
  3996.        *    lwc1    f3,4(at)
  3997.        * But, the resulting address is the same after relocation so why
  3998.        * generate the extra instruction?
  3999.        */
  4000.       coproc = 1;
  4001.       if (mips_isa >= 2)
  4002.     {
  4003.       s = "ldc1";
  4004.       goto ld;
  4005.     }
  4006.  
  4007.       s = "lwc1";
  4008.       fmt = "T,o(b)";
  4009.       goto ldd_std;
  4010.  
  4011.     case M_S_DAB:
  4012.       if (mips_isa >= 2)
  4013.     {
  4014.       s = "sdc1";
  4015.       goto st;
  4016.     }
  4017.  
  4018.       s = "swc1";
  4019.       fmt = "T,o(b)";
  4020.       coproc = 1;
  4021.       goto ldd_std;
  4022.  
  4023.     case M_LD_AB:
  4024.       if (mips_isa >= 3)
  4025.     {
  4026.       s = "ld";
  4027.       goto ld;
  4028.     }
  4029.  
  4030.       s = "lw";
  4031.       fmt = "t,o(b)";
  4032.       goto ldd_std;
  4033.  
  4034.     case M_SD_AB:
  4035.       if (mips_isa >= 3)
  4036.     {
  4037.       s = "sd";
  4038.       goto st;
  4039.     }
  4040.  
  4041.       s = "sw";
  4042.       fmt = "t,o(b)";
  4043.  
  4044.     ldd_std:
  4045.       if (offset_expr.X_op != O_symbol
  4046.       && offset_expr.X_op != O_constant)
  4047.     {
  4048.       as_bad ("expression too complex");
  4049.       offset_expr.X_op = O_constant;
  4050.     }
  4051.  
  4052.       /* Even on a big endian machine $fn comes before $fn+1.  We have
  4053.      to adjust when loading from memory.  We set coproc if we must
  4054.      load $fn+1 first.  */
  4055.       if (byte_order == LITTLE_ENDIAN)
  4056.     coproc = 0;
  4057.  
  4058.       if (mips_pic == NO_PIC
  4059.       || offset_expr.X_op == O_constant)
  4060.     {
  4061.       /* If this is a reference to a GP relative symbol, we want
  4062.            <op>    $treg,<sym>($gp)    (BFD_RELOC_MIPS_GPREL)
  4063.            <op>    $treg+1,<sym>+4($gp)    (BFD_RELOC_MIPS_GPREL)
  4064.          If we have a base register, we use this
  4065.            addu    $at,$breg,$gp
  4066.            <op>    $treg,<sym>($at)    (BFD_RELOC_MIPS_GPREL)
  4067.            <op>    $treg+1,<sym>+4($at)    (BFD_RELOC_MIPS_GPREL)
  4068.          If this is not a GP relative symbol, we want
  4069.            lui    $at,<sym>        (BFD_RELOC_HI16_S)
  4070.            <op>    $treg,<sym>($at)    (BFD_RELOC_LO16)
  4071.            <op>    $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
  4072.          If there is a base register, we add it to $at after the
  4073.          lui instruction.  If there is a constant, we always use
  4074.          the last case.  */
  4075.       if ((valueT) offset_expr.X_add_number >= MAX_GPREL_OFFSET
  4076.           || nopic_need_relax (offset_expr.X_add_symbol))
  4077.         {
  4078.           p = NULL;
  4079.           used_at = 1;
  4080.         }
  4081.       else
  4082.         {
  4083.           int off;
  4084.  
  4085.           if (breg == 0)
  4086.         {
  4087.           frag_grow (28);
  4088.           tempreg = GP;
  4089.           off = 0;
  4090.           used_at = 0;
  4091.         }
  4092.           else
  4093.         {
  4094.           frag_grow (36);
  4095.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4096.                    mips_isa < 3 ? "addu" : "daddu",
  4097.                    "d,v,t", AT, breg, GP);
  4098.           tempreg = AT;
  4099.           off = 4;
  4100.           used_at = 1;
  4101.         }
  4102.  
  4103.           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  4104.                coproc ? treg + 1 : treg,
  4105.                (int) BFD_RELOC_MIPS_GPREL, tempreg);
  4106.           offset_expr.X_add_number += 4;
  4107.  
  4108.           /* Set mips_optimize to 2 to avoid inserting an
  4109.                  undesired nop.  */
  4110.           hold_mips_optimize = mips_optimize;
  4111.           mips_optimize = 2;
  4112.           macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  4113.                coproc ? treg : treg + 1,
  4114.                (int) BFD_RELOC_MIPS_GPREL, tempreg);
  4115.           mips_optimize = hold_mips_optimize;
  4116.  
  4117.           p = frag_var (rs_machine_dependent, 12 + off, 0,
  4118.                 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
  4119.                       used_at && mips_noat),
  4120.                 offset_expr.X_add_symbol, (long) 0,
  4121.                 (char *) NULL);
  4122.  
  4123.           /* We just generated two relocs.  When tc_gen_reloc
  4124.          handles this case, it will skip the first reloc and
  4125.          handle the second.  The second reloc already has an
  4126.          extra addend of 4, which we added above.  We must
  4127.          subtract it out, and then subtract another 4 to make
  4128.          the first reloc come out right.  The second reloc
  4129.          will come out right because we are going to add 4 to
  4130.          offset_expr when we build its instruction below.  */
  4131.           offset_expr.X_add_number -= 8;
  4132.           offset_expr.X_op = O_constant;
  4133.         }
  4134.       macro_build_lui (p, &icnt, &offset_expr, AT);
  4135.       if (p != NULL)
  4136.         p += 4;
  4137.       if (breg != 0)
  4138.         {
  4139.           macro_build (p, &icnt, (expressionS *) NULL,
  4140.                mips_isa < 3 ? "addu" : "daddu",
  4141.                "d,v,t", AT, breg, AT);
  4142.           if (p != NULL)
  4143.         p += 4;
  4144.         }
  4145.       macro_build (p, &icnt, &offset_expr, s, fmt,
  4146.                coproc ? treg + 1 : treg,
  4147.                (int) BFD_RELOC_LO16, AT);
  4148.       if (p != NULL)
  4149.         p += 4;
  4150.       /* FIXME: How do we handle overflow here?  */
  4151.       offset_expr.X_add_number += 4;
  4152.       macro_build (p, &icnt, &offset_expr, s, fmt,
  4153.                coproc ? treg : treg + 1,
  4154.                (int) BFD_RELOC_LO16, AT);
  4155.     }      
  4156.       else if (mips_pic == SVR4_PIC && ! mips_big_got)
  4157.     {
  4158.       int off;
  4159.  
  4160.       /* If this is a reference to an external symbol, we want
  4161.            lw    $at,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  4162.            nop
  4163.            <op>    $treg,0($at)
  4164.            <op>    $treg+1,4($at)
  4165.          Otherwise we want
  4166.            lw    $at,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  4167.            nop
  4168.            <op>    $treg,<sym>($at)    (BFD_RELOC_LO16)
  4169.            <op>    $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
  4170.          If there is a base register we add it to $at before the
  4171.          lwc1 instructions.  If there is a constant we include it
  4172.          in the lwc1 instructions.  */
  4173.       used_at = 1;
  4174.       expr1.X_add_number = offset_expr.X_add_number;
  4175.       offset_expr.X_add_number = 0;
  4176.       if (expr1.X_add_number < -0x8000
  4177.           || expr1.X_add_number >= 0x8000 - 4)
  4178.         as_bad ("PIC code offset overflow (max 16 signed bits)");
  4179.       if (breg == 0)
  4180.         off = 0;
  4181.       else
  4182.         off = 4;
  4183.       frag_grow (24 + off);
  4184.       macro_build ((char *) NULL, &icnt, &offset_expr,
  4185.                mips_isa < 3 ? "lw" : "ld",
  4186.                "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
  4187.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
  4188.       if (breg != 0)
  4189.         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4190.              mips_isa < 3 ? "addu" : "daddu",
  4191.              "d,v,t", AT, breg, AT);
  4192.       macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
  4193.                coproc ? treg + 1 : treg,
  4194.                (int) BFD_RELOC_LO16, AT);
  4195.       expr1.X_add_number += 4;
  4196.  
  4197.       /* Set mips_optimize to 2 to avoid inserting an undesired
  4198.              nop.  */
  4199.       hold_mips_optimize = mips_optimize;
  4200.       mips_optimize = 2;
  4201.       macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
  4202.                coproc ? treg : treg + 1,
  4203.                (int) BFD_RELOC_LO16, AT);
  4204.       mips_optimize = hold_mips_optimize;
  4205.  
  4206.       (void) frag_var (rs_machine_dependent, 0, 0,
  4207.                RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
  4208.                offset_expr.X_add_symbol, (long) 0,
  4209.                (char *) NULL);
  4210.     }
  4211.       else if (mips_pic == SVR4_PIC)
  4212.     {
  4213.       int gpdel, off;
  4214.  
  4215.       /* If this is a reference to an external symbol, we want
  4216.            lui    $at,<sym>        (BFD_RELOC_MIPS_GOT_HI16)
  4217.            addu    $at,$at,$gp
  4218.            lw    $at,<sym>($at)        (BFD_RELOC_MIPS_GOT_LO16)
  4219.            nop
  4220.            <op>    $treg,0($at)
  4221.            <op>    $treg+1,4($at)
  4222.          Otherwise we want
  4223.            lw    $at,<sym>($gp)        (BFD_RELOC_MIPS_GOT16)
  4224.            nop
  4225.            <op>    $treg,<sym>($at)    (BFD_RELOC_LO16)
  4226.            <op>    $treg+1,<sym>+4($at)    (BFD_RELOC_LO16)
  4227.          If there is a base register we add it to $at before the
  4228.          lwc1 instructions.  If there is a constant we include it
  4229.          in the lwc1 instructions.  */
  4230.       used_at = 1;
  4231.       expr1.X_add_number = offset_expr.X_add_number;
  4232.       offset_expr.X_add_number = 0;
  4233.       if (expr1.X_add_number < -0x8000
  4234.           || expr1.X_add_number >= 0x8000 - 4)
  4235.         as_bad ("PIC code offset overflow (max 16 signed bits)");
  4236.       if (reg_needs_delay (GP))
  4237.         gpdel = 4;
  4238.       else
  4239.         gpdel = 0;
  4240.       if (breg == 0)
  4241.         off = 0;
  4242.       else
  4243.         off = 4;
  4244.       frag_grow (56);
  4245.       macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
  4246.                AT, (int) BFD_RELOC_MIPS_GOT_HI16);
  4247.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4248.                mips_isa < 3 ? "addu" : "daddu",
  4249.                "d,v,t", AT, AT, GP);
  4250.       macro_build ((char *) NULL, &icnt, &offset_expr,
  4251.                mips_isa < 3 ? "lw" : "ld",
  4252.                "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
  4253.       macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
  4254.       if (breg != 0)
  4255.         macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4256.              mips_isa < 3 ? "addu" : "daddu",
  4257.              "d,v,t", AT, breg, AT);
  4258.       macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
  4259.                coproc ? treg + 1 : treg,
  4260.                (int) BFD_RELOC_LO16, AT);
  4261.       expr1.X_add_number += 4;
  4262.  
  4263.       /* Set mips_optimize to 2 to avoid inserting an undesired
  4264.              nop.  */
  4265.       hold_mips_optimize = mips_optimize;
  4266.       mips_optimize = 2;
  4267.       macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
  4268.                coproc ? treg : treg + 1,
  4269.                (int) BFD_RELOC_LO16, AT);
  4270.       mips_optimize = hold_mips_optimize;
  4271.       expr1.X_add_number -= 4;
  4272.  
  4273.       p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
  4274.             RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
  4275.                       8 + gpdel + off, 1, 0),
  4276.             offset_expr.X_add_symbol, (long) 0,
  4277.             (char *) NULL);
  4278.       if (gpdel > 0)
  4279.         {
  4280.           macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  4281.           p += 4;
  4282.         }
  4283.       macro_build (p, &icnt, &offset_expr,
  4284.                mips_isa < 3 ? "lw" : "ld",
  4285.                "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
  4286.       p += 4;
  4287.       macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
  4288.       p += 4;
  4289.       if (breg != 0)
  4290.         {
  4291.           macro_build (p, &icnt, (expressionS *) NULL,
  4292.                mips_isa < 3 ? "addu" : "daddu",
  4293.                "d,v,t", AT, breg, AT);
  4294.           p += 4;
  4295.         }
  4296.       macro_build (p, &icnt, &expr1, s, fmt,
  4297.                coproc ? treg + 1 : treg,
  4298.                (int) BFD_RELOC_LO16, AT);
  4299.       p += 4;
  4300.       expr1.X_add_number += 4;
  4301.  
  4302.       /* Set mips_optimize to 2 to avoid inserting an undesired
  4303.              nop.  */
  4304.       hold_mips_optimize = mips_optimize;
  4305.       mips_optimize = 2;
  4306.       macro_build (p, &icnt, &expr1, s, fmt,
  4307.                coproc ? treg : treg + 1,
  4308.                (int) BFD_RELOC_LO16, AT);
  4309.       mips_optimize = hold_mips_optimize;
  4310.     }
  4311.       else if (mips_pic == EMBEDDED_PIC)
  4312.     {
  4313.       /* If there is no base register, we use
  4314.            <op>    $treg,<sym>($gp)    (BFD_RELOC_MIPS_GPREL)
  4315.            <op>    $treg+1,<sym>+4($gp)    (BFD_RELOC_MIPS_GPREL)
  4316.          If we have a base register, we use
  4317.            addu    $at,$breg,$gp
  4318.            <op>    $treg,<sym>($at)    (BFD_RELOC_MIPS_GPREL)
  4319.            <op>    $treg+1,<sym>+4($at)    (BFD_RELOC_MIPS_GPREL)
  4320.          */
  4321.       if (breg == 0)
  4322.         {
  4323.           tempreg = GP;
  4324.           used_at = 0;
  4325.         }
  4326.       else
  4327.         {
  4328.           macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4329.                mips_isa < 3 ? "addu" : "daddu",
  4330.                "d,v,t", AT, breg, GP);
  4331.           tempreg = AT;
  4332.           used_at = 1;
  4333.         }
  4334.  
  4335.       macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  4336.                coproc ? treg + 1 : treg,
  4337.                (int) BFD_RELOC_MIPS_GPREL, tempreg);
  4338.       offset_expr.X_add_number += 4;
  4339.       macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
  4340.                coproc ? treg : treg + 1,
  4341.                (int) BFD_RELOC_MIPS_GPREL, tempreg);
  4342.     }
  4343.       else
  4344.     abort ();
  4345.  
  4346.       if (! used_at)
  4347.     return;
  4348.  
  4349.       break;
  4350.  
  4351.     case M_LD_OB:
  4352.       s = "lw";
  4353.       goto sd_ob;
  4354.     case M_SD_OB:
  4355.       s = "sw";
  4356.     sd_ob:
  4357.       assert (mips_isa < 3);
  4358.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
  4359.            (int) BFD_RELOC_LO16, breg);
  4360.       offset_expr.X_add_number += 4;
  4361.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
  4362.            (int) BFD_RELOC_LO16, breg);
  4363.       return;
  4364. #ifdef LOSING_COMPILER
  4365.     default:
  4366.       macro2 (ip);
  4367.       return;
  4368.     }
  4369.   if (mips_noat)
  4370.     as_warn ("Macro used $at after \".set noat\"");
  4371. }
  4372.           
  4373. static void
  4374. macro2 (ip)
  4375.      struct mips_cl_insn *ip;
  4376. {
  4377.   register int treg, sreg, dreg, breg;
  4378.   int tempreg;
  4379.   int mask;
  4380.   int icnt = 0;
  4381.   int used_at;
  4382.   expressionS expr1;
  4383.   const char *s;
  4384.   const char *s2;
  4385.   const char *fmt;
  4386.   int likely = 0;
  4387.   int dbl = 0;
  4388.   int coproc = 0;
  4389.   int lr = 0;
  4390.   int off;
  4391.   offsetT maxnum;
  4392.   bfd_reloc_code_real_type r;
  4393.   char *p;
  4394.           
  4395.   treg = (ip->insn_opcode >> 16) & 0x1f;
  4396.   dreg = (ip->insn_opcode >> 11) & 0x1f;
  4397.   sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
  4398.   mask = ip->insn_mo->mask;
  4399.           
  4400.   expr1.X_op = O_constant;
  4401.   expr1.X_op_symbol = NULL;
  4402.   expr1.X_add_symbol = NULL;
  4403.   expr1.X_add_number = 1;
  4404.           
  4405.   switch (mask)
  4406.     {
  4407. #endif /* LOSING_COMPILER */
  4408.  
  4409.     case M_DMUL:
  4410.       dbl = 1;
  4411.     case M_MUL:
  4412.       macro_build ((char *) NULL, &icnt, NULL,
  4413.            dbl ? "dmultu" : "multu",
  4414.            "s,t", sreg, treg);
  4415.       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
  4416.       return;
  4417.  
  4418.     case M_DMUL_I:
  4419.       dbl = 1;
  4420.     case M_MUL_I:
  4421.       /* The MIPS assembler some times generates shifts and adds.  I'm
  4422.      not trying to be that fancy. GCC should do this for us
  4423.      anyway.  */
  4424.       load_register (&icnt, AT, &imm_expr, dbl);
  4425.       macro_build ((char *) NULL, &icnt, NULL,
  4426.            dbl ? "dmult" : "mult",
  4427.            "s,t", sreg, AT);
  4428.       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
  4429.       break;
  4430.  
  4431.     case M_DMULO:
  4432.       dbl = 1;
  4433.     case M_MULO:
  4434.       mips_emit_delays ();
  4435.       ++mips_noreorder;
  4436.       mips_any_noreorder = 1;
  4437.       macro_build ((char *) NULL, &icnt, NULL,
  4438.            dbl ? "dmult" : "mult",
  4439.            "s,t", sreg, treg);
  4440.       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
  4441.       macro_build ((char *) NULL, &icnt, NULL,
  4442.            dbl ? "dsra32" : "sra",
  4443.            "d,w,<", dreg, dreg, 31);
  4444.       macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
  4445.       if (mips_trap)
  4446.     macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
  4447.       else
  4448.     {
  4449.       expr1.X_add_number = 8;
  4450.       macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
  4451.       macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  4452.       macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
  4453.     }
  4454.       --mips_noreorder;
  4455.       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
  4456.       break;
  4457.  
  4458.     case M_DMULOU:
  4459.       dbl = 1;
  4460.     case M_MULOU:
  4461.       mips_emit_delays ();
  4462.       ++mips_noreorder;
  4463.       mips_any_noreorder = 1;
  4464.       macro_build ((char *) NULL, &icnt, NULL,
  4465.            dbl ? "dmultu" : "multu",
  4466.            "s,t", sreg, treg);
  4467.       macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
  4468.       macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
  4469.       if (mips_trap)
  4470.     macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
  4471.       else
  4472.     {
  4473.       expr1.X_add_number = 8;
  4474.       macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
  4475.       macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
  4476.       macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
  4477.     }
  4478.       --mips_noreorder;
  4479.       break;
  4480.  
  4481.     case M_ROL:
  4482.       macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
  4483.       macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
  4484.       macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
  4485.            treg);
  4486.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
  4487.       break;
  4488.  
  4489.     case M_ROL_I:
  4490.       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
  4491.            (int) (imm_expr.X_add_number & 0x1f));
  4492.       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
  4493.            (int) ((0 - imm_expr.X_add_number) & 0x1f));
  4494.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
  4495.       break;
  4496.  
  4497.     case M_ROR:
  4498.       macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
  4499.       macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
  4500.       macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
  4501.            treg);
  4502.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
  4503.       break;
  4504.  
  4505.     case M_ROR_I:
  4506.       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
  4507.            (int) (imm_expr.X_add_number & 0x1f));
  4508.       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
  4509.            (int) ((0 - imm_expr.X_add_number) & 0x1f));
  4510.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
  4511.       break;
  4512.  
  4513.     case M_S_DOB:
  4514.       assert (mips_isa < 2);
  4515.       /* Even on a big endian machine $fn comes before $fn+1.  We have
  4516.      to adjust when storing to memory.  */
  4517.       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
  4518.            byte_order == LITTLE_ENDIAN ? treg : treg + 1,
  4519.            (int) BFD_RELOC_LO16, breg);
  4520.       offset_expr.X_add_number += 4;
  4521.       macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
  4522.            byte_order == LITTLE_ENDIAN ? treg + 1 : treg,
  4523.            (int) BFD_RELOC_LO16, breg);
  4524.       return;
  4525.  
  4526.     case M_SEQ:
  4527.       if (sreg == 0)
  4528.     macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
  4529.              treg, (int) BFD_RELOC_LO16);
  4530.       else if (treg == 0)
  4531.     macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
  4532.              sreg, (int) BFD_RELOC_LO16);
  4533.       else
  4534.     {
  4535.       macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
  4536.                sreg, treg);
  4537.       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
  4538.                dreg, (int) BFD_RELOC_LO16);
  4539.     }
  4540.       return;
  4541.  
  4542.     case M_SEQ_I:
  4543.       if (imm_expr.X_add_number == 0)
  4544.     {
  4545.       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
  4546.                sreg, (int) BFD_RELOC_LO16);
  4547.       return;
  4548.     }
  4549.       if (sreg == 0)
  4550.     {
  4551.       as_warn ("Instruction %s: result is always false",
  4552.            ip->insn_mo->name);
  4553.       macro_build ((char *) NULL, &icnt, NULL, "move", "d,s", dreg, 0);
  4554.       return;
  4555.     }
  4556.       if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
  4557.     {
  4558.       macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
  4559.                sreg, (int) BFD_RELOC_LO16);
  4560.       used_at = 0;
  4561.     }
  4562.       else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
  4563.     {
  4564.       imm_expr.X_add_number = -imm_expr.X_add_number;
  4565.       macro_build ((char *) NULL, &icnt, &imm_expr,
  4566.                mips_isa < 3 ? "addiu" : "daddiu",
  4567.                "t,r,j", dreg, sreg,
  4568.                (int) BFD_RELOC_LO16);
  4569.       used_at = 0;
  4570.     }
  4571.       else
  4572.     {
  4573.       load_register (&icnt, AT, &imm_expr, 0);
  4574.       macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
  4575.                sreg, AT);
  4576.       used_at = 1;
  4577.     }
  4578.       macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
  4579.            (int) BFD_RELOC_LO16);
  4580.       if (used_at)
  4581.     break;
  4582.       return;
  4583.  
  4584.     case M_SGE:        /* sreg >= treg <==> not (sreg < treg) */
  4585.       s = "slt";
  4586.       goto sge;
  4587.     case M_SGEU:
  4588.       s = "sltu";
  4589.     sge:
  4590.       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
  4591.       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
  4592.            (int) BFD_RELOC_LO16);
  4593.       return;
  4594.  
  4595.     case M_SGE_I:        /* sreg >= I <==> not (sreg < I) */
  4596.     case M_SGEU_I:
  4597.       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
  4598.     {
  4599.       macro_build ((char *) NULL, &icnt, &expr1,
  4600.                mask == M_SGE_I ? "slti" : "sltiu",
  4601.                "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
  4602.       used_at = 0;
  4603.     }
  4604.       else
  4605.     {
  4606.       load_register (&icnt, AT, &imm_expr, 0);
  4607.       macro_build ((char *) NULL, &icnt, NULL,
  4608.                mask == M_SGE_I ? "slt" : "sltu",
  4609.                "d,v,t", dreg, sreg, AT);
  4610.       used_at = 1;
  4611.     }
  4612.       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
  4613.            (int) BFD_RELOC_LO16);
  4614.       if (used_at)
  4615.     break;
  4616.       return;
  4617.  
  4618.     case M_SGT:        /* sreg > treg  <==>  treg < sreg */
  4619.       s = "slt";
  4620.       goto sgt;
  4621.     case M_SGTU:
  4622.       s = "sltu";
  4623.     sgt:
  4624.       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
  4625.       return;
  4626.  
  4627.     case M_SGT_I:        /* sreg > I  <==>  I < sreg */
  4628.       s = "slt";
  4629.       goto sgti;
  4630.     case M_SGTU_I:
  4631.       s = "sltu";
  4632.     sgti:
  4633.       load_register (&icnt, AT, &imm_expr, 0);
  4634.       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
  4635.       break;
  4636.  
  4637.     case M_SLE:        /* sreg <= treg  <==>  treg >= sreg  <==>  not (treg < sreg) */
  4638.       s = "slt";
  4639.       goto sle;
  4640.     case M_SLEU:
  4641.       s = "sltu";
  4642.     sle:
  4643.       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
  4644.       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
  4645.            (int) BFD_RELOC_LO16);
  4646.       return;
  4647.  
  4648.     case M_SLE_I:        /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
  4649.       s = "slt";
  4650.       goto slei;
  4651.     case M_SLEU_I:
  4652.       s = "sltu";
  4653.     slei:
  4654.       load_register (&icnt, AT, &imm_expr, 0);
  4655.       macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
  4656.       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
  4657.            (int) BFD_RELOC_LO16);
  4658.       break;
  4659.  
  4660.     case M_SLT_I:
  4661.       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
  4662.     {
  4663.       macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
  4664.                dreg, sreg, (int) BFD_RELOC_LO16);
  4665.       return;
  4666.     }
  4667.       load_register (&icnt, AT, &imm_expr, 0);
  4668.       macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
  4669.       break;
  4670.  
  4671.     case M_SLTU_I:
  4672.       if (imm_expr.X_add_number >= -0x8000 && imm_expr.X_add_number < 0x8000)
  4673.     {
  4674.       macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
  4675.                dreg, sreg, (int) BFD_RELOC_LO16);
  4676.       return;
  4677.     }
  4678.       load_register (&icnt, AT, &imm_expr, 0);
  4679.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
  4680.            AT);
  4681.       break;
  4682.  
  4683.     case M_SNE:
  4684.       if (sreg == 0)
  4685.     macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
  4686.              treg);
  4687.       else if (treg == 0)
  4688.     macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
  4689.              sreg);
  4690.       else
  4691.     {
  4692.       macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
  4693.                sreg, treg);
  4694.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
  4695.                dreg);
  4696.     }
  4697.       return;
  4698.  
  4699.     case M_SNE_I:
  4700.       if (imm_expr.X_add_number == 0)
  4701.     {
  4702.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
  4703.                sreg);
  4704.       return;
  4705.     }
  4706.       if (sreg == 0)
  4707.     {
  4708.       as_warn ("Instruction %s: result is always true",
  4709.            ip->insn_mo->name);
  4710.       macro_build ((char *) NULL, &icnt, &expr1,
  4711.                mips_isa < 3 ? "addiu" : "daddiu",
  4712.                "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
  4713.       return;
  4714.     }
  4715.       if (imm_expr.X_add_number >= 0 && imm_expr.X_add_number < 0x10000)
  4716.     {
  4717.       macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
  4718.                dreg, sreg, (int) BFD_RELOC_LO16);
  4719.       used_at = 0;
  4720.     }
  4721.       else if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number < 0)
  4722.     {
  4723.       imm_expr.X_add_number = -imm_expr.X_add_number;
  4724.       macro_build ((char *) NULL, &icnt, &imm_expr,
  4725.                mips_isa < 3 ? "addiu" : "daddiu",
  4726.                "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
  4727.       used_at = 0;
  4728.     }
  4729.       else
  4730.     {
  4731.       load_register (&icnt, AT, &imm_expr, 0);
  4732.       macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
  4733.                sreg, AT);
  4734.       used_at = 1;
  4735.     }
  4736.       macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
  4737.       if (used_at)
  4738.     break;
  4739.       return;
  4740.  
  4741.     case M_DSUB_I:
  4742.       dbl = 1;
  4743.     case M_SUB_I:
  4744.       if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
  4745.     {
  4746.       imm_expr.X_add_number = -imm_expr.X_add_number;
  4747.       macro_build ((char *) NULL, &icnt, &imm_expr,
  4748.                dbl ? "daddi" : "addi",
  4749.                "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
  4750.       return;
  4751.     }
  4752.       load_register (&icnt, AT, &imm_expr, dbl);
  4753.       macro_build ((char *) NULL, &icnt, NULL,
  4754.            dbl ? "dsub" : "sub",
  4755.            "d,v,t", dreg, sreg, AT);
  4756.       break;
  4757.  
  4758.     case M_DSUBU_I:
  4759.       dbl = 1;
  4760.     case M_SUBU_I:
  4761.       if (imm_expr.X_add_number > -0x8000 && imm_expr.X_add_number <= 0x8000)
  4762.     {
  4763.       imm_expr.X_add_number = -imm_expr.X_add_number;
  4764.       macro_build ((char *) NULL, &icnt, &imm_expr,
  4765.                dbl ? "daddiu" : "addiu",
  4766.                "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
  4767.       return;
  4768.     }
  4769.       load_register (&icnt, AT, &imm_expr, dbl);
  4770.       macro_build ((char *) NULL, &icnt, NULL,
  4771.            dbl ? "dsubu" : "subu",
  4772.            "d,v,t", dreg, sreg, AT);
  4773.       break;
  4774.  
  4775.     case M_TEQ_I:
  4776.       s = "teq";
  4777.       goto trap;
  4778.     case M_TGE_I:
  4779.       s = "tge";
  4780.       goto trap;
  4781.     case M_TGEU_I:
  4782.       s = "tgeu";
  4783.       goto trap;
  4784.     case M_TLT_I:
  4785.       s = "tlt";
  4786.       goto trap;
  4787.     case M_TLTU_I:
  4788.       s = "tltu";
  4789.       goto trap;
  4790.     case M_TNE_I:
  4791.       s = "tne";
  4792.     trap:
  4793.       load_register (&icnt, AT, &imm_expr, 0);
  4794.       macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
  4795.       break;
  4796.  
  4797.     case M_TRUNCWD:
  4798.     case M_TRUNCWS:
  4799.       assert (mips_isa < 2);
  4800.       sreg = (ip->insn_opcode >> 11) & 0x1f;    /* floating reg */
  4801.       dreg = (ip->insn_opcode >> 06) & 0x1f;    /* floating reg */
  4802.  
  4803.       /*
  4804.        * Is the double cfc1 instruction a bug in the mips assembler;
  4805.        * or is there a reason for it?
  4806.        */
  4807.       mips_emit_delays ();
  4808.       ++mips_noreorder;
  4809.       mips_any_noreorder = 1;
  4810.       macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
  4811.       macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
  4812.       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
  4813.       expr1.X_add_number = 3;
  4814.       macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
  4815.            (int) BFD_RELOC_LO16);
  4816.       expr1.X_add_number = 2;
  4817.       macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
  4818.              (int) BFD_RELOC_LO16);
  4819.       macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
  4820.       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
  4821.       macro_build ((char *) NULL, &icnt, NULL,
  4822.           mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
  4823.       macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
  4824.       macro_build ((char *) NULL, &icnt, NULL, "nop", "");
  4825.       --mips_noreorder;
  4826.       break;
  4827.  
  4828.     case M_ULH:
  4829.       s = "lb";
  4830.       goto ulh;
  4831.     case M_ULHU:
  4832.       s = "lbu";
  4833.     ulh:
  4834.       if (offset_expr.X_add_number >= 0x7fff)
  4835.     as_bad ("operand overflow");
  4836.       /* avoid load delay */
  4837.       if (byte_order == LITTLE_ENDIAN)
  4838.     offset_expr.X_add_number += 1;
  4839.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
  4840.            (int) BFD_RELOC_LO16, breg);
  4841.       if (byte_order == LITTLE_ENDIAN)
  4842.     offset_expr.X_add_number -= 1;
  4843.       else
  4844.     offset_expr.X_add_number += 1;
  4845.       macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
  4846.            (int) BFD_RELOC_LO16, breg);
  4847.       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
  4848.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
  4849.       break;
  4850.  
  4851.     case M_ULD:
  4852.       s = "ldl";
  4853.       s2 = "ldr";
  4854.       off = 7;
  4855.       goto ulw;
  4856.     case M_ULW:
  4857.       s = "lwl";
  4858.       s2 = "lwr";
  4859.       off = 3;
  4860.     ulw:
  4861.       if (offset_expr.X_add_number >= 0x8000 - off)
  4862.     as_bad ("operand overflow");
  4863.       if (byte_order == LITTLE_ENDIAN)
  4864.     offset_expr.X_add_number += off;
  4865.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
  4866.            (int) BFD_RELOC_LO16, breg);
  4867.       if (byte_order == LITTLE_ENDIAN)
  4868.     offset_expr.X_add_number -= off;
  4869.       else
  4870.     offset_expr.X_add_number += off;
  4871.       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
  4872.            (int) BFD_RELOC_LO16, breg);
  4873.       return;
  4874.  
  4875.     case M_ULD_A:
  4876.       s = "ldl";
  4877.       s2 = "ldr";
  4878.       off = 7;
  4879.       goto ulwa;
  4880.     case M_ULW_A:
  4881.       s = "lwl";
  4882.       s2 = "lwr";
  4883.       off = 3;
  4884.     ulwa:
  4885.       load_address (&icnt, AT, &offset_expr);
  4886.       if (breg != 0)
  4887.     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4888.              mips_isa < 3 ? "addu" : "daddu",
  4889.              "d,v,t", AT, AT, breg);
  4890.       if (byte_order == LITTLE_ENDIAN)
  4891.     expr1.X_add_number = off;
  4892.       else
  4893.     expr1.X_add_number = 0;
  4894.       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
  4895.            (int) BFD_RELOC_LO16, AT);
  4896.       if (byte_order == LITTLE_ENDIAN)
  4897.     expr1.X_add_number = 0;
  4898.       else
  4899.     expr1.X_add_number = off;
  4900.       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
  4901.            (int) BFD_RELOC_LO16, AT);
  4902.       break;
  4903.  
  4904.     case M_ULH_A:
  4905.     case M_ULHU_A:
  4906.       load_address (&icnt, AT, &offset_expr);
  4907.       if (breg != 0)
  4908.     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4909.              mips_isa < 3 ? "addu" : "daddu",
  4910.              "d,v,t", AT, AT, breg);
  4911.       if (byte_order == BIG_ENDIAN)
  4912.     expr1.X_add_number = 0;
  4913.       macro_build ((char *) NULL, &icnt, &expr1,
  4914.            mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
  4915.            (int) BFD_RELOC_LO16, AT);
  4916.       if (byte_order == BIG_ENDIAN)
  4917.     expr1.X_add_number = 1;
  4918.       else
  4919.     expr1.X_add_number = 0;
  4920.       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
  4921.            (int) BFD_RELOC_LO16, AT);
  4922.       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
  4923.            treg, 8);
  4924.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
  4925.            treg, AT);
  4926.       break;
  4927.  
  4928.     case M_USH:
  4929.       if (offset_expr.X_add_number >= 0x7fff)
  4930.     as_bad ("operand overflow");
  4931.       if (byte_order == BIG_ENDIAN)
  4932.     offset_expr.X_add_number += 1;
  4933.       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
  4934.            (int) BFD_RELOC_LO16, breg);
  4935.       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
  4936.       if (byte_order == BIG_ENDIAN)
  4937.     offset_expr.X_add_number -= 1;
  4938.       else
  4939.     offset_expr.X_add_number += 1;
  4940.       macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
  4941.            (int) BFD_RELOC_LO16, breg);
  4942.       break;
  4943.  
  4944.     case M_USD:
  4945.       s = "sdl";
  4946.       s2 = "sdr";
  4947.       off = 7;
  4948.       goto usw;
  4949.     case M_USW:
  4950.       s = "swl";
  4951.       s2 = "swr";
  4952.       off = 3;
  4953.     usw:
  4954.       if (offset_expr.X_add_number >= 0x8000 - off)
  4955.     as_bad ("operand overflow");
  4956.       if (byte_order == LITTLE_ENDIAN)
  4957.     offset_expr.X_add_number += off;
  4958.       macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
  4959.            (int) BFD_RELOC_LO16, breg);
  4960.       if (byte_order == LITTLE_ENDIAN)
  4961.     offset_expr.X_add_number -= off;
  4962.       else
  4963.     offset_expr.X_add_number += off;
  4964.       macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
  4965.            (int) BFD_RELOC_LO16, breg);
  4966.       return;
  4967.  
  4968.     case M_USD_A:
  4969.       s = "sdl";
  4970.       s2 = "sdr";
  4971.       off = 7;
  4972.       goto uswa;
  4973.     case M_USW_A:
  4974.       s = "swl";
  4975.       s2 = "swr";
  4976.       off = 3;
  4977.     uswa:
  4978.       load_address (&icnt, AT, &offset_expr);
  4979.       if (breg != 0)
  4980.     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  4981.              mips_isa < 3 ? "addu" : "daddu",
  4982.              "d,v,t", AT, AT, breg);
  4983.       if (byte_order == LITTLE_ENDIAN)
  4984.     expr1.X_add_number = off;
  4985.       else
  4986.     expr1.X_add_number = 0;
  4987.       macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
  4988.            (int) BFD_RELOC_LO16, AT);
  4989.       if (byte_order == LITTLE_ENDIAN)
  4990.     expr1.X_add_number = 0;
  4991.       else
  4992.     expr1.X_add_number = off;
  4993.       macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
  4994.            (int) BFD_RELOC_LO16, AT);
  4995.       break;
  4996.  
  4997.     case M_USH_A:
  4998.       load_address (&icnt, AT, &offset_expr);
  4999.       if (breg != 0)
  5000.     macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  5001.              mips_isa < 3 ? "addu" : "daddu",
  5002.              "d,v,t", AT, AT, breg);
  5003.       if (byte_order == LITTLE_ENDIAN)
  5004.     expr1.X_add_number = 0;
  5005.       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
  5006.            (int) BFD_RELOC_LO16, AT);
  5007.       macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
  5008.            treg, 8);
  5009.       if (byte_order == LITTLE_ENDIAN)
  5010.     expr1.X_add_number = 1;
  5011.       else
  5012.     expr1.X_add_number = 0;
  5013.       macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
  5014.            (int) BFD_RELOC_LO16, AT);
  5015.       if (byte_order == LITTLE_ENDIAN)
  5016.     expr1.X_add_number = 0;
  5017.       else
  5018.     expr1.X_add_number = 1;
  5019.       macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
  5020.            (int) BFD_RELOC_LO16, AT);
  5021.       macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
  5022.            treg, 8);
  5023.       macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
  5024.            treg, AT);
  5025.       break;
  5026.  
  5027.     default:
  5028.       as_bad ("Macro %s not implemented yet", ip->insn_mo->name);
  5029.       break;
  5030.     }
  5031.   if (mips_noat)
  5032.     as_warn ("Macro used $at after \".set noat\"");
  5033. }
  5034.  
  5035. /* This routine assembles an instruction into its binary format.  As a
  5036.    side effect, it sets one of the global variables imm_reloc or
  5037.    offset_reloc to the type of relocation to do if one of the operands
  5038.    is an address expression.  */
  5039.  
  5040. static void
  5041. mips_ip (str, ip)
  5042.      char *str;
  5043.      struct mips_cl_insn *ip;
  5044. {
  5045.   char *s;
  5046.   const char *args;
  5047.   char c;
  5048.   struct mips_opcode *insn;
  5049.   char *argsStart;
  5050.   unsigned int regno;
  5051.   unsigned int lastregno = 0;
  5052.   char *s_reset;
  5053.  
  5054.   insn_error = NULL;
  5055.  
  5056.   for (s = str; islower (*s) || (*s >= '0' && *s <= '3') || *s == '6' || *s == '.'; ++s)
  5057.     continue;
  5058.   switch (*s)
  5059.     {
  5060.     case '\0':
  5061.       break;
  5062.  
  5063.     case ' ':
  5064.       *s++ = '\0';
  5065.       break;
  5066.  
  5067.     default:
  5068.       as_fatal ("Unknown opcode: `%s'", str);
  5069.     }
  5070.   if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
  5071.     {
  5072.       insn_error = "unrecognized opcode";
  5073.       return;
  5074.     }
  5075.   argsStart = s;
  5076.   for (;;)
  5077.     {
  5078.       int insn_isa;
  5079.  
  5080.       assert (strcmp (insn->name, str) == 0);
  5081.  
  5082.       if (insn->pinfo == INSN_MACRO)
  5083.     insn_isa = insn->match;
  5084.       else if ((insn->pinfo & INSN_ISA) == INSN_ISA2)
  5085.     insn_isa = 2;
  5086.       else if ((insn->pinfo & INSN_ISA) == INSN_ISA3)
  5087.     insn_isa = 3;
  5088.       else if ((insn->pinfo & INSN_ISA) == INSN_ISA4)
  5089.     insn_isa = 4;
  5090.       else
  5091.     insn_isa = 1;
  5092.  
  5093.       if (insn_isa > mips_isa
  5094.       || ((insn->pinfo & INSN_ISA) == INSN_4650
  5095.           && ! mips_4650)
  5096.       || ((insn->pinfo & INSN_ISA) == INSN_4010
  5097.           && ! mips_4010)
  5098.       || ((insn->pinfo & INSN_ISA) == INSN_4100
  5099.           && ! mips_4100))
  5100.     {
  5101.       if (insn + 1 < &mips_opcodes[NUMOPCODES]
  5102.           && strcmp (insn->name, insn[1].name) == 0)
  5103.         {
  5104.           ++insn;
  5105.           continue;
  5106.         }
  5107.       insn_error = "opcode not supported on this processor";
  5108.       return;
  5109.     }
  5110.  
  5111.       ip->insn_mo = insn;
  5112.       ip->insn_opcode = insn->match;
  5113.       for (args = insn->args;; ++args)
  5114.     {
  5115.       if (*s == ' ')
  5116.         ++s;
  5117.       switch (*args)
  5118.         {
  5119.         case '\0':        /* end of args */
  5120.           if (*s == '\0')
  5121.         return;
  5122.           break;
  5123.  
  5124.         case ',':
  5125.           if (*s++ == *args)
  5126.         continue;
  5127.           s--;
  5128.           switch (*++args)
  5129.         {
  5130.         case 'r':
  5131.         case 'v':
  5132.           ip->insn_opcode |= lastregno << 21;
  5133.           continue;
  5134.  
  5135.         case 'w':
  5136.         case 'W':
  5137.           ip->insn_opcode |= lastregno << 16;
  5138.           continue;
  5139.  
  5140.         case 'V':
  5141.           ip->insn_opcode |= lastregno << 11;
  5142.           continue;
  5143.         }
  5144.           break;
  5145.  
  5146.         case '(':
  5147.           /* handle optional base register.
  5148.          Either the base register is omitted or
  5149.          we must have a left paren. */
  5150.           /* this is dependent on the next operand specifier
  5151.          is a 'b' for base register */
  5152.           assert (args[1] == 'b');
  5153.           if (*s == '\0')
  5154.         return;
  5155.  
  5156.         case ')':        /* these must match exactly */
  5157.           if (*s++ == *args)
  5158.         continue;
  5159.           break;
  5160.  
  5161.         case '<':        /* must be at least one digit */
  5162.           /*
  5163.            * According to the manual, if the shift amount is greater
  5164.            * than 31 or less than 0 the the shift amount should be
  5165.            * mod 32. In reality the mips assembler issues an error.
  5166.            * We issue a warning and mask out all but the low 5 bits.
  5167.            */
  5168.           my_getExpression (&imm_expr, s);
  5169.           check_absolute_expr (ip, &imm_expr);
  5170.           if ((unsigned long) imm_expr.X_add_number > 31)
  5171.         {
  5172.           as_warn ("Improper shift amount (%ld)",
  5173.                (long) imm_expr.X_add_number);
  5174.           imm_expr.X_add_number = imm_expr.X_add_number & 0x1f;
  5175.         }
  5176.           ip->insn_opcode |= imm_expr.X_add_number << 6;
  5177.           imm_expr.X_op = O_absent;
  5178.           s = expr_end;
  5179.           continue;
  5180.  
  5181.         case '>':        /* shift amount minus 32 */
  5182.           my_getExpression (&imm_expr, s);
  5183.           check_absolute_expr (ip, &imm_expr);
  5184.           if ((unsigned long) imm_expr.X_add_number < 32
  5185.           || (unsigned long) imm_expr.X_add_number > 63)
  5186.         break;
  5187.           ip->insn_opcode |= (imm_expr.X_add_number - 32) << 6;
  5188.           imm_expr.X_op = O_absent;
  5189.           s = expr_end;
  5190.           continue;
  5191.  
  5192.         case 'k':        /* cache code */
  5193.         case 'h':        /* prefx code */
  5194.           my_getExpression (&imm_expr, s);
  5195.           check_absolute_expr (ip, &imm_expr);
  5196.           if ((unsigned long) imm_expr.X_add_number > 31)
  5197.         {
  5198.           as_warn ("Invalid value for `%s' (%lu)",
  5199.                ip->insn_mo->name,
  5200.                (unsigned long) imm_expr.X_add_number);
  5201.           imm_expr.X_add_number &= 0x1f;
  5202.         }
  5203.           if (*args == 'k')
  5204.         ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
  5205.           else
  5206.         ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
  5207.           imm_expr.X_op = O_absent;
  5208.           s = expr_end;
  5209.           continue;
  5210.  
  5211.         case 'c':        /* break code */
  5212.           my_getExpression (&imm_expr, s);
  5213.           check_absolute_expr (ip, &imm_expr);
  5214.           if ((unsigned) imm_expr.X_add_number > 1023)
  5215.         as_warn ("Illegal break code (%ld)",
  5216.              (long) imm_expr.X_add_number);
  5217.           ip->insn_opcode |= imm_expr.X_add_number << 16;
  5218.           imm_expr.X_op = O_absent;
  5219.           s = expr_end;
  5220.           continue;
  5221.  
  5222.         case 'B':        /* syscall code */
  5223.           my_getExpression (&imm_expr, s);
  5224.           check_absolute_expr (ip, &imm_expr);
  5225.           if ((unsigned) imm_expr.X_add_number > 0xfffff)
  5226.         as_warn ("Illegal syscall code (%ld)",
  5227.              (long) imm_expr.X_add_number);
  5228.           ip->insn_opcode |= imm_expr.X_add_number << 6;
  5229.           imm_expr.X_op = O_absent;
  5230.           s = expr_end;
  5231.           continue;
  5232.  
  5233.             case 'C':           /* Coprocessor code */
  5234.               my_getExpression (&imm_expr, s);
  5235.           check_absolute_expr (ip, &imm_expr);
  5236.               if ((unsigned long) imm_expr.X_add_number >= (1<<25))
  5237.         {
  5238.                   as_warn ("Coproccesor code > 25 bits (%ld)",
  5239.                (long) imm_expr.X_add_number);
  5240.                   imm_expr.X_add_number &= ((1<<25) - 1);
  5241.         }
  5242.               ip->insn_opcode |= imm_expr.X_add_number;
  5243.               imm_expr.X_op = O_absent;
  5244.               s = expr_end;
  5245.               continue;
  5246.  
  5247.         case 'b':        /* base register */
  5248.         case 'd':        /* destination register */
  5249.         case 's':        /* source register */
  5250.         case 't':        /* target register */
  5251.         case 'r':        /* both target and source */
  5252.         case 'v':        /* both dest and source */
  5253.         case 'w':        /* both dest and target */
  5254.         case 'E':        /* coprocessor target register */
  5255.         case 'G':        /* coprocessor destination register */
  5256.         case 'x':        /* ignore register name */
  5257.         case 'z':        /* must be zero register */
  5258.           s_reset = s;
  5259.           if (s[0] == '$')
  5260.         {
  5261.           if (isdigit (s[1]))
  5262.             {
  5263.               ++s;
  5264.               regno = 0;
  5265.               do
  5266.             {
  5267.               regno *= 10;
  5268.               regno += *s - '0';
  5269.               ++s;
  5270.             }
  5271.               while (isdigit (*s));
  5272.               if (regno > 31)
  5273.             as_bad ("Invalid register number (%d)", regno);
  5274.             }
  5275.           else if (*args == 'E' || *args == 'G')
  5276.             goto notreg;
  5277.           else
  5278.             {
  5279.               if (s[1] == 'f' && s[2] == 'p')
  5280.             {
  5281.               s += 3;
  5282.               regno = FP;
  5283.             }
  5284.               else if (s[1] == 's' && s[2] == 'p')
  5285.             {
  5286.               s += 3;
  5287.               regno = SP;
  5288.             }
  5289.               else if (s[1] == 'g' && s[2] == 'p')
  5290.             {
  5291.               s += 3;
  5292.               regno = GP;
  5293.             }
  5294.               else if (s[1] == 'a' && s[2] == 't')
  5295.             {
  5296.               s += 3;
  5297.               regno = AT;
  5298.             }
  5299.               else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
  5300.             {
  5301.               s += 4;
  5302.               regno = KT0;
  5303.             }
  5304.               else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
  5305.             {
  5306.               s += 4;
  5307.               regno = KT1;
  5308.             }
  5309.               else
  5310.             goto notreg;
  5311.             }
  5312.           if (regno == AT
  5313.               && ! mips_noat
  5314.               && *args != 'E'
  5315.               && *args != 'G')
  5316.             as_warn ("Used $at without \".set noat\"");
  5317.           c = *args;
  5318.           if (*s == ' ')
  5319.             s++;
  5320.           if (args[1] != *s)
  5321.             {
  5322.               if (c == 'r' || c == 'v' || c == 'w')
  5323.             {
  5324.               regno = lastregno;
  5325.               s = s_reset;
  5326.               args++;
  5327.             }
  5328.             }
  5329.           /* 'z' only matches $0.  */
  5330.           if (c == 'z' && regno != 0)
  5331.             break;
  5332.           switch (c)
  5333.             {
  5334.             case 'r':
  5335.             case 's':
  5336.             case 'v':
  5337.             case 'b':
  5338.               ip->insn_opcode |= regno << 21;
  5339.               break;
  5340.             case 'd':
  5341.             case 'G':
  5342.               ip->insn_opcode |= regno << 11;
  5343.               break;
  5344.             case 'w':
  5345.             case 't':
  5346.             case 'E':
  5347.               ip->insn_opcode |= regno << 16;
  5348.               break;
  5349.             case 'x':
  5350.               /* This case exists because on the r3000 trunc
  5351.              expands into a macro which requires a gp
  5352.              register.  On the r6000 or r4000 it is
  5353.              assembled into a single instruction which
  5354.              ignores the register.  Thus the insn version
  5355.              is MIPS_ISA2 and uses 'x', and the macro
  5356.              version is MIPS_ISA1 and uses 't'.  */
  5357.               break;
  5358.             case 'z':
  5359.               /* This case is for the div instruction, which
  5360.              acts differently if the destination argument
  5361.              is $0.  This only matches $0, and is checked
  5362.              outside the switch.  */
  5363.               break;
  5364.             }
  5365.           lastregno = regno;
  5366.           continue;
  5367.         }
  5368.         notreg:
  5369.           switch (*args++)
  5370.         {
  5371.         case 'r':
  5372.         case 'v':
  5373.           ip->insn_opcode |= lastregno << 21;
  5374.           continue;
  5375.         case 'w':
  5376.           ip->insn_opcode |= lastregno << 16;
  5377.           continue;
  5378.         }
  5379.           break;
  5380.  
  5381.         case 'D':        /* floating point destination register */
  5382.         case 'S':        /* floating point source register */
  5383.         case 'T':        /* floating point target register */
  5384.         case 'R':        /* floating point source register */
  5385.         case 'V':
  5386.         case 'W':
  5387.           s_reset = s;
  5388.           if (s[0] == '$' && s[1] == 'f' && isdigit (s[2]))
  5389.         {
  5390.           s += 2;
  5391.           regno = 0;
  5392.           do
  5393.             {
  5394.               regno *= 10;
  5395.               regno += *s - '0';
  5396.               ++s;
  5397.             }
  5398.           while (isdigit (*s));
  5399.  
  5400.           if (regno > 31)
  5401.             as_bad ("Invalid float register number (%d)", regno);
  5402.  
  5403.           if ((regno & 1) != 0
  5404.               && mips_isa < 3
  5405.               && ! (strcmp (str, "mtc1") == 0 ||
  5406.                 strcmp (str, "mfc1") == 0 ||
  5407.                 strcmp (str, "lwc1") == 0 ||
  5408.                 strcmp (str, "swc1") == 0))
  5409.             as_warn ("Float register should be even, was %d",
  5410.                  regno);
  5411.  
  5412.           c = *args;
  5413.           if (*s == ' ')
  5414.             s++;
  5415.           if (args[1] != *s)
  5416.             {
  5417.               if (c == 'V' || c == 'W')
  5418.             {
  5419.               regno = lastregno;
  5420.               s = s_reset;
  5421.               args++;
  5422.             }
  5423.             }
  5424.           switch (c)
  5425.             {
  5426.             case 'D':
  5427.               ip->insn_opcode |= regno << 6;
  5428.               break;
  5429.             case 'V':
  5430.             case 'S':
  5431.               ip->insn_opcode |= regno << 11;
  5432.               break;
  5433.             case 'W':
  5434.             case 'T':
  5435.               ip->insn_opcode |= regno << 16;
  5436.               break;
  5437.             case 'R':
  5438.               ip->insn_opcode |= regno << 21;
  5439.               break;
  5440.             }
  5441.           lastregno = regno;
  5442.           continue;
  5443.         }
  5444.           switch (*args++)
  5445.         {
  5446.         case 'V':
  5447.           ip->insn_opcode |= lastregno << 11;
  5448.           continue;
  5449.         case 'W':
  5450.           ip->insn_opcode |= lastregno << 16;
  5451.           continue;
  5452.         }
  5453.           break;
  5454.  
  5455.         case 'I':
  5456.           my_getExpression (&imm_expr, s);
  5457.           if (imm_expr.X_op != O_big
  5458.           && imm_expr.X_op != O_constant)
  5459.         insn_error = "absolute expression required";
  5460.           s = expr_end;
  5461.           continue;
  5462.  
  5463.         case 'A':
  5464.           my_getExpression (&offset_expr, s);
  5465.           imm_reloc = BFD_RELOC_32;
  5466.           s = expr_end;
  5467.           continue;
  5468.  
  5469.         case 'F':
  5470.         case 'L':
  5471.         case 'f':
  5472.         case 'l':
  5473.           {
  5474.         int f64;
  5475.         char *save_in;
  5476.         char *err;
  5477.         unsigned char temp[8];
  5478.         int len;
  5479.         unsigned int length;
  5480.         segT seg;
  5481.         subsegT subseg;
  5482.         char *p;
  5483.  
  5484.         /* These only appear as the last operand in an
  5485.            instruction, and every instruction that accepts
  5486.            them in any variant accepts them in all variants.
  5487.            This means we don't have to worry about backing out
  5488.            any changes if the instruction does not match.
  5489.  
  5490.            The difference between them is the size of the
  5491.            floating point constant and where it goes.  For 'F'
  5492.            and 'L' the constant is 64 bits; for 'f' and 'l' it
  5493.            is 32 bits.  Where the constant is placed is based
  5494.            on how the MIPS assembler does things:
  5495.             F -- .rdata
  5496.             L -- .lit8
  5497.             f -- immediate value
  5498.             l -- .lit4
  5499.  
  5500.             The .lit4 and .lit8 sections are only used if
  5501.             permitted by the -G argument.
  5502.  
  5503.             When generating embedded PIC code, we use the
  5504.             .lit8 section but not the .lit4 section (we can do
  5505.             .lit4 inline easily; we need to put .lit8
  5506.             somewhere in the data segment, and using .lit8
  5507.             permits the linker to eventually combine identical
  5508.             .lit8 entries).  */
  5509.  
  5510.         f64 = *args == 'F' || *args == 'L';
  5511.  
  5512.         save_in = input_line_pointer;
  5513.         input_line_pointer = s;
  5514.         err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
  5515.         length = len;
  5516.         s = input_line_pointer;
  5517.         input_line_pointer = save_in;
  5518.         if (err != NULL && *err != '\0')
  5519.           {
  5520.             as_bad ("Bad floating point constant: %s", err);
  5521.             memset (temp, '\0', sizeof temp);
  5522.             length = f64 ? 8 : 4;
  5523.           }
  5524.  
  5525.         assert (length == (f64 ? 8 : 4));
  5526.  
  5527.         if (*args == 'f'
  5528.             || (*args == 'l'
  5529.             && (! USE_GLOBAL_POINTER_OPT
  5530.                 || mips_pic == EMBEDDED_PIC
  5531.                 || g_switch_value < 4)
  5532.             ))
  5533.           {
  5534.             imm_expr.X_op = O_constant;
  5535.             if (byte_order == LITTLE_ENDIAN)
  5536.               imm_expr.X_add_number =
  5537.             (((((((int) temp[3] << 8)
  5538.                  | temp[2]) << 8)
  5539.                | temp[1]) << 8)
  5540.              | temp[0]);
  5541.             else
  5542.               imm_expr.X_add_number =
  5543.             (((((((int) temp[0] << 8)
  5544.                  | temp[1]) << 8)
  5545.                | temp[2]) << 8)
  5546.              | temp[3]);
  5547.           }
  5548.         else
  5549.           {
  5550.             const char *newname;
  5551.             segT new_seg;
  5552.  
  5553.             /* Switch to the right section.  */
  5554.             seg = now_seg;
  5555.             subseg = now_subseg;
  5556.             switch (*args)
  5557.               {
  5558.               default: /* unused default case avoids warnings.  */
  5559.               case 'L':
  5560.             newname = RDATA_SECTION_NAME;
  5561.             if (USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
  5562.               newname = ".lit8";
  5563.             break;
  5564.               case 'F':
  5565.             newname = RDATA_SECTION_NAME;
  5566.             break;
  5567.               case 'l':
  5568.             assert (!USE_GLOBAL_POINTER_OPT
  5569.                 || g_switch_value >= 4);
  5570.             newname = ".lit4";
  5571.             break;
  5572.               }
  5573.             new_seg = subseg_new (newname, (subsegT) 0);
  5574.             if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
  5575.               bfd_set_section_flags (stdoutput, new_seg,
  5576.                          (SEC_ALLOC
  5577.                           | SEC_LOAD
  5578.                           | SEC_READONLY
  5579.                           | SEC_DATA));
  5580.             frag_align (*args == 'l' ? 2 : 3, 0);
  5581.             if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
  5582.               record_alignment (new_seg, 4);
  5583.             else
  5584.               record_alignment (new_seg, *args == 'l' ? 2 : 3);
  5585.             if (seg == now_seg)
  5586.               as_bad ("Can't use floating point insn in this section");
  5587.  
  5588.             /* Set the argument to the current address in the
  5589.                section.  */
  5590.             offset_expr.X_op = O_symbol;
  5591.             offset_expr.X_add_symbol =
  5592.               symbol_new ("L0\001", now_seg,
  5593.                   (valueT) frag_now_fix (), frag_now);
  5594.             offset_expr.X_add_number = 0;
  5595.  
  5596.             /* Put the floating point number into the section.  */
  5597.             p = frag_more ((int) length);
  5598.             memcpy (p, temp, length);
  5599.  
  5600.             /* Switch back to the original section.  */
  5601.             subseg_set (seg, subseg);
  5602.           }
  5603.           }
  5604.           continue;
  5605.  
  5606.         case 'i':        /* 16 bit unsigned immediate */
  5607.         case 'j':        /* 16 bit signed immediate */
  5608.           imm_reloc = BFD_RELOC_LO16;
  5609.           c = my_getSmallExpression (&imm_expr, s);
  5610.           if (c != '\0')
  5611.         {
  5612.           if (c != 'l')
  5613.             {
  5614.               if (imm_expr.X_op == O_constant)
  5615.             imm_expr.X_add_number =
  5616.               (imm_expr.X_add_number >> 16) & 0xffff;
  5617.               else if (c == 'h')
  5618.             {
  5619.               imm_reloc = BFD_RELOC_HI16_S;
  5620.               imm_unmatched_hi = true;
  5621.             }
  5622.               else
  5623.             imm_reloc = BFD_RELOC_HI16;
  5624.             }
  5625.         }
  5626.           if (*args == 'i')
  5627.         {
  5628.           if ((c == '\0' && imm_expr.X_op != O_constant)
  5629.               || ((imm_expr.X_add_number < 0
  5630.                            || imm_expr.X_add_number >= 0x10000)
  5631.                           && imm_expr.X_op == O_constant))
  5632.             {
  5633.               if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
  5634.               !strcmp (insn->name, insn[1].name))
  5635.             break;
  5636.               if (imm_expr.X_op != O_constant
  5637.               && imm_expr.X_op != O_big)
  5638.             insn_error = "absolute expression required";
  5639.               else
  5640.             as_bad ("16 bit expression not in range 0..65535");
  5641.             }
  5642.         }
  5643.           else
  5644.         {
  5645.           int more;
  5646.           offsetT max;
  5647.  
  5648.           /* The upper bound should be 0x8000, but
  5649.              unfortunately the MIPS assembler accepts numbers
  5650.              from 0x8000 to 0xffff and sign extends them, and
  5651.              we want to be compatible.  We only permit this
  5652.              extended range for an instruction which does not
  5653.              provide any further alternates, since those
  5654.              alternates may handle other cases.  People should
  5655.              use the numbers they mean, rather than relying on
  5656.              a mysterious sign extension.  */
  5657.           more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
  5658.               strcmp (insn->name, insn[1].name) == 0);
  5659.           if (more)
  5660.             max = 0x8000;
  5661.           else
  5662.             max = 0x10000;
  5663.           if ((c == '\0' && imm_expr.X_op != O_constant)
  5664.               || ((imm_expr.X_add_number < -0x8000
  5665.                            || imm_expr.X_add_number >= max)
  5666.                           && imm_expr.X_op == O_constant)
  5667.               || (more
  5668.               && imm_expr.X_add_number < 0
  5669.               && mips_isa >= 3
  5670.               && imm_expr.X_unsigned
  5671.               && sizeof (imm_expr.X_add_number) <= 4))
  5672.             {
  5673.               if (more)
  5674.             break;
  5675.               if (imm_expr.X_op != O_constant
  5676.               && imm_expr.X_op != O_big)
  5677.             insn_error = "absolute expression required";
  5678.               else
  5679.             as_bad ("16 bit expression not in range -32768..32767");
  5680.             }
  5681.         }
  5682.           s = expr_end;
  5683.           continue;
  5684.  
  5685.         case 'o':        /* 16 bit offset */
  5686.           c = my_getSmallExpression (&offset_expr, s);
  5687.  
  5688.           /* If this value won't fit into a 16 bit offset, then go
  5689.          find a macro that will generate the 32 bit offset
  5690.          code pattern.  As a special hack, we accept the
  5691.          difference of two local symbols as a constant.  This
  5692.          is required to suppose embedded PIC switches, which
  5693.          use an instruction which looks like
  5694.              lw $4,$L12-$LS12($4)
  5695.          The problem with handling this in a more general
  5696.          fashion is that the macro function doesn't expect to
  5697.          see anything which can be handled in a single
  5698.          constant instruction.  */
  5699.           if (c == 0
  5700.           && (offset_expr.X_op != O_constant
  5701.               || offset_expr.X_add_number >= 0x8000
  5702.               || offset_expr.X_add_number < -0x8000)
  5703.           && (mips_pic != EMBEDDED_PIC
  5704.               || offset_expr.X_op != O_subtract
  5705.               || now_seg != text_section
  5706.               || (S_GET_SEGMENT (offset_expr.X_op_symbol)
  5707.               != text_section)))
  5708.         break;
  5709.  
  5710.           offset_reloc = BFD_RELOC_LO16;
  5711.           if (c == 'h' || c == 'H')
  5712.         {
  5713.           assert (offset_expr.X_op == O_constant);
  5714.           offset_expr.X_add_number =
  5715.             (offset_expr.X_add_number >> 16) & 0xffff;
  5716.         }
  5717.           s = expr_end;
  5718.           continue;
  5719.  
  5720.         case 'p':        /* pc relative offset */
  5721.           offset_reloc = BFD_RELOC_16_PCREL_S2;
  5722.           my_getExpression (&offset_expr, s);
  5723.           s = expr_end;
  5724.           continue;
  5725.  
  5726.         case 'u':        /* upper 16 bits */
  5727.           c = my_getSmallExpression (&imm_expr, s);
  5728.           if (imm_expr.X_op == O_constant
  5729.           && (imm_expr.X_add_number < 0
  5730.               || imm_expr.X_add_number >= 0x10000))
  5731.         as_bad ("lui expression not in range 0..65535");
  5732.           imm_reloc = BFD_RELOC_LO16;
  5733.           if (c)
  5734.         {
  5735.           if (c != 'l')
  5736.             {
  5737.               if (imm_expr.X_op == O_constant)
  5738.             imm_expr.X_add_number =
  5739.               (imm_expr.X_add_number >> 16) & 0xffff;
  5740.               else if (c == 'h')
  5741.             {
  5742.               imm_reloc = BFD_RELOC_HI16_S;
  5743.               imm_unmatched_hi = true;
  5744.             }
  5745.               else
  5746.             imm_reloc = BFD_RELOC_HI16;
  5747.             }
  5748.         }
  5749.           s = expr_end;
  5750.           continue;
  5751.  
  5752.         case 'a':        /* 26 bit address */
  5753.           my_getExpression (&offset_expr, s);
  5754.           s = expr_end;
  5755.           offset_reloc = BFD_RELOC_MIPS_JMP;
  5756.           continue;
  5757.  
  5758.         case 'N':        /* 3 bit branch condition code */
  5759.         case 'M':        /* 3 bit compare condition code */
  5760.           my_getExpression (&imm_expr, s);
  5761.           check_absolute_expr (ip, &imm_expr);
  5762.               if ((unsigned long) imm_expr.X_add_number > 7)
  5763.         {
  5764.                   as_warn ("Condition code > 7 (%ld)",
  5765.                (long) imm_expr.X_add_number);
  5766.                   imm_expr.X_add_number &= 7;
  5767.         }
  5768.           if (*args == 'N')
  5769.         ip->insn_opcode |= imm_expr.X_add_number << OP_SH_BCC;
  5770.           else
  5771.         ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CCC;
  5772.               imm_expr.X_op = O_absent;
  5773.               s = expr_end;
  5774.               continue;
  5775.  
  5776.         default:
  5777.           fprintf (stderr, "bad char = '%c'\n", *args);
  5778.           internalError ();
  5779.         }
  5780.       break;
  5781.     }
  5782.       /* Args don't match.  */
  5783.       if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
  5784.       !strcmp (insn->name, insn[1].name))
  5785.     {
  5786.       ++insn;
  5787.       s = argsStart;
  5788.       continue;
  5789.     }
  5790.       insn_error = "illegal operands";
  5791.       return;
  5792.     }
  5793. }
  5794.  
  5795. #define LP '('
  5796. #define RP ')'
  5797.  
  5798. static int
  5799. my_getSmallExpression (ep, str)
  5800.      expressionS *ep;
  5801.      char *str;
  5802. {
  5803.   char *sp;
  5804.   int c = 0;
  5805.  
  5806.   if (*str == ' ')
  5807.     str++;
  5808.   if (*str == LP
  5809.       || (*str == '%' &&
  5810.       ((str[1] == 'h' && str[2] == 'i')
  5811.        || (str[1] == 'H' && str[2] == 'I')
  5812.        || (str[1] == 'l' && str[2] == 'o'))
  5813.       && str[3] == LP))
  5814.     {
  5815.       if (*str == LP)
  5816.     c = 0;
  5817.       else
  5818.     {
  5819.       c = str[1];
  5820.       str += 3;
  5821.     }
  5822.  
  5823.       /*
  5824.        * A small expression may be followed by a base register.
  5825.        * Scan to the end of this operand, and then back over a possible
  5826.        * base register.  Then scan the small expression up to that
  5827.        * point.  (Based on code in sparc.c...)
  5828.        */
  5829.       for (sp = str; *sp && *sp != ','; sp++)
  5830.     ;
  5831.       if (sp - 4 >= str && sp[-1] == RP)
  5832.     {
  5833.       if (isdigit (sp[-2]))
  5834.         {
  5835.           for (sp -= 3; sp >= str && isdigit (*sp); sp--)
  5836.         ;
  5837.           if (*sp == '$' && sp > str && sp[-1] == LP)
  5838.         {
  5839.           sp--;
  5840.           goto do_it;
  5841.         }
  5842.         }
  5843.       else if (sp - 5 >= str
  5844.            && sp[-5] == LP
  5845.            && sp[-4] == '$'
  5846.            && ((sp[-3] == 'f' && sp[-2] == 'p')
  5847.                || (sp[-3] == 's' && sp[-2] == 'p')
  5848.                || (sp[-3] == 'g' && sp[-2] == 'p')
  5849.                || (sp[-3] == 'a' && sp[-2] == 't')))
  5850.         {
  5851.           sp -= 5;
  5852.         do_it:
  5853.           if (sp == str)
  5854.         {
  5855.           /* no expression means zero offset */
  5856.           if (c)
  5857.             {
  5858.               /* %xx(reg) is an error */
  5859.               ep->X_op = O_absent;
  5860.               expr_end = str - 3;
  5861.             }
  5862.           else
  5863.             {
  5864.               ep->X_op = O_constant;
  5865.               expr_end = sp;
  5866.             }
  5867.           ep->X_add_symbol = NULL;
  5868.           ep->X_op_symbol = NULL;
  5869.           ep->X_add_number = 0;
  5870.         }
  5871.           else
  5872.         {
  5873.           *sp = '\0';
  5874.           my_getExpression (ep, str);
  5875.           *sp = LP;
  5876.         }
  5877.           return c;
  5878.         }
  5879.     }
  5880.     }
  5881.   my_getExpression (ep, str);
  5882.   return c;            /* => %hi or %lo encountered */
  5883. }
  5884.  
  5885. static void
  5886. my_getExpression (ep, str)
  5887.      expressionS *ep;
  5888.      char *str;
  5889. {
  5890.   char *save_in;
  5891.  
  5892.   save_in = input_line_pointer;
  5893.   input_line_pointer = str;
  5894.   expression (ep);
  5895.   expr_end = input_line_pointer;
  5896.   input_line_pointer = save_in;
  5897. }
  5898.  
  5899. /* Turn a string in input_line_pointer into a floating point constant
  5900.    of type type, and store the appropriate bytes in *litP.  The number
  5901.    of LITTLENUMS emitted is stored in *sizeP .  An error message is
  5902.    returned, or NULL on OK.  */
  5903.  
  5904. char *
  5905. md_atof (type, litP, sizeP)
  5906.      int type;
  5907.      char *litP;
  5908.      int *sizeP;
  5909. {
  5910.   int prec;
  5911.   LITTLENUM_TYPE words[4];
  5912.   char *t;
  5913.   int i;
  5914.  
  5915.   switch (type)
  5916.     {
  5917.     case 'f':
  5918.       prec = 2;
  5919.       break;
  5920.  
  5921.     case 'd':
  5922.       prec = 4;
  5923.       break;
  5924.  
  5925.     default:
  5926.       *sizeP = 0;
  5927.       return "bad call to md_atof";
  5928.     }
  5929.  
  5930.   t = atof_ieee (input_line_pointer, type, words);
  5931.   if (t)
  5932.     input_line_pointer = t;
  5933.  
  5934.   *sizeP = prec * 2;
  5935.  
  5936.   if (byte_order == LITTLE_ENDIAN)
  5937.     {
  5938.       for (i = prec - 1; i >= 0; i--)
  5939.     {
  5940.       md_number_to_chars (litP, (valueT) words[i], 2);
  5941.       litP += 2;
  5942.     }
  5943.     }
  5944.   else
  5945.     {
  5946.       for (i = 0; i < prec; i++)
  5947.     {
  5948.       md_number_to_chars (litP, (valueT) words[i], 2);
  5949.       litP += 2;
  5950.     }
  5951.     }
  5952.      
  5953.   return NULL;
  5954. }
  5955.  
  5956. void
  5957. md_number_to_chars (buf, val, n)
  5958.      char *buf;
  5959.      valueT val;
  5960.      int n;
  5961. {
  5962.   switch (byte_order)
  5963.     {
  5964.     case LITTLE_ENDIAN:
  5965.       number_to_chars_littleendian (buf, val, n);
  5966.       break;
  5967.  
  5968.     case BIG_ENDIAN:
  5969.       number_to_chars_bigendian (buf, val, n);
  5970.       break;
  5971.  
  5972.     default:
  5973.       internalError ();
  5974.     }
  5975. }
  5976.  
  5977. CONST char *md_shortopts = "O::g::G:";
  5978.  
  5979. struct option md_longopts[] = {
  5980. #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
  5981.   {"mips0", no_argument, NULL, OPTION_MIPS1},
  5982.   {"mips1", no_argument, NULL, OPTION_MIPS1},
  5983. #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
  5984.   {"mips2", no_argument, NULL, OPTION_MIPS2},
  5985. #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
  5986.   {"mips3", no_argument, NULL, OPTION_MIPS3},
  5987. #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
  5988.   {"mips4", no_argument, NULL, OPTION_MIPS4},
  5989. #define OPTION_MCPU (OPTION_MD_BASE + 5)
  5990.   {"mcpu", required_argument, NULL, OPTION_MCPU},
  5991. #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
  5992.   {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
  5993. #define OPTION_TRAP (OPTION_MD_BASE + 9)
  5994.   {"trap", no_argument, NULL, OPTION_TRAP},
  5995.   {"no-break", no_argument, NULL, OPTION_TRAP},
  5996. #define OPTION_BREAK (OPTION_MD_BASE + 10)
  5997.   {"break", no_argument, NULL, OPTION_BREAK},
  5998.   {"no-trap", no_argument, NULL, OPTION_BREAK},
  5999. #define OPTION_EB (OPTION_MD_BASE + 11)
  6000.   {"EB", no_argument, NULL, OPTION_EB},
  6001. #define OPTION_EL (OPTION_MD_BASE + 12)
  6002.   {"EL", no_argument, NULL, OPTION_EL},
  6003. #define OPTION_M4650 (OPTION_MD_BASE + 13)
  6004.   {"m4650", no_argument, NULL, OPTION_M4650},
  6005. #define OPTION_NO_M4650 (OPTION_MD_BASE + 14)
  6006.   {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
  6007. #define OPTION_M4010 (OPTION_MD_BASE + 15)
  6008.   {"m4010", no_argument, NULL, OPTION_M4010},
  6009. #define OPTION_NO_M4010 (OPTION_MD_BASE + 16)
  6010.   {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
  6011. #define OPTION_M4100 (OPTION_MD_BASE + 17)
  6012.   {"m4100", no_argument, NULL, OPTION_M4100},
  6013. #define OPTION_NO_M4100 (OPTION_MD_BASE + 18)
  6014.   {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
  6015.  
  6016. #define OPTION_CALL_SHARED (OPTION_MD_BASE + 7)
  6017. #define OPTION_NON_SHARED (OPTION_MD_BASE + 8)
  6018. #define OPTION_XGOT (OPTION_MD_BASE + 19)
  6019. #define OPTION_32 (OPTION_MD_BASE + 20)
  6020. #define OPTION_64 (OPTION_MD_BASE + 21)
  6021. #ifdef OBJ_ELF
  6022.   {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
  6023.   {"xgot", no_argument, NULL, OPTION_XGOT},
  6024.   {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
  6025.   {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
  6026.   {"32", no_argument, NULL, OPTION_32},
  6027.   {"64", no_argument, NULL, OPTION_64},
  6028. #endif
  6029.  
  6030.   {NULL, no_argument, NULL, 0}
  6031. };
  6032. size_t md_longopts_size = sizeof(md_longopts);
  6033.  
  6034. int
  6035. md_parse_option (c, arg)
  6036.      int c;
  6037.      char *arg;
  6038. {
  6039.   switch (c)
  6040.     {
  6041.     case OPTION_TRAP:
  6042.       mips_trap = 1;
  6043.       break;
  6044.  
  6045.     case OPTION_BREAK:
  6046.       mips_trap = 0;
  6047.       break;
  6048.  
  6049.     case OPTION_EB:
  6050.       target_big_endian = 1;
  6051.       break;
  6052.  
  6053.     case OPTION_EL:
  6054.       target_big_endian = 0;
  6055.       break;
  6056.  
  6057.     case 'O':
  6058.       if (arg && arg[1] == '0')
  6059.     mips_optimize = 1;
  6060.       else
  6061.     mips_optimize = 2;
  6062.       break;
  6063.  
  6064.     case 'g':
  6065.       if (arg == NULL)
  6066.     mips_debug = 2;
  6067.       else
  6068.     mips_debug = atoi (arg);
  6069.       /* When the MIPS assembler sees -g or -g2, it does not do
  6070.          optimizations which limit full symbolic debugging.  We take
  6071.          that to be equivalent to -O0.  */
  6072.       if (mips_debug == 2)
  6073.     mips_optimize = 0;
  6074.       break;
  6075.  
  6076.     case OPTION_MIPS1:
  6077.       mips_isa = 1;
  6078.       if (mips_cpu == -1)
  6079.     mips_cpu = 3000;
  6080.       break;
  6081.  
  6082.     case OPTION_MIPS2:
  6083.       mips_isa = 2;
  6084.       if (mips_cpu == -1)
  6085.     mips_cpu = 6000;
  6086.       break;
  6087.  
  6088.     case OPTION_MIPS3:
  6089.       mips_isa = 3;
  6090.       if (mips_cpu == -1)
  6091.     mips_cpu = 4000;
  6092.       break;
  6093.  
  6094.     case OPTION_MIPS4:
  6095.       mips_isa = 4;
  6096.       if (mips_cpu == -1)
  6097.     mips_cpu = 8000;
  6098.       break;
  6099.  
  6100.     case OPTION_MCPU:
  6101.       {
  6102.     char *p;
  6103.  
  6104.     /* Identify the processor type */
  6105.     p = arg;
  6106.     if (strcmp (p, "default") == 0
  6107.         || strcmp (p, "DEFAULT") == 0)
  6108.       mips_cpu = -1;
  6109.     else
  6110.       {
  6111.         int sv = 0;
  6112.  
  6113.         /* We need to cope with the various "vr" prefixes for the 4300
  6114.            processor.  */
  6115.         if (*p == 'v' || *p == 'V')
  6116.           {
  6117.         sv = 1;
  6118.         p++;
  6119.           }
  6120.  
  6121.         if (*p == 'r' || *p == 'R')
  6122.           p++;
  6123.  
  6124.         mips_cpu = -1;
  6125.         switch (*p)
  6126.           {
  6127.           case '1':
  6128.         if (strcmp (p, "10000") == 0
  6129.             || strcmp (p, "10k") == 0
  6130.             || strcmp (p, "10K") == 0)
  6131.           mips_cpu = 10000;
  6132.         break;
  6133.  
  6134.           case '2':
  6135.         if (strcmp (p, "2000") == 0
  6136.             || strcmp (p, "2k") == 0
  6137.             || strcmp (p, "2K") == 0)
  6138.           mips_cpu = 2000;
  6139.         break;
  6140.  
  6141.           case '3':
  6142.         if (strcmp (p, "3000") == 0
  6143.             || strcmp (p, "3k") == 0
  6144.             || strcmp (p, "3K") == 0)
  6145.           mips_cpu = 3000;
  6146.         break;
  6147.  
  6148.           case '4':
  6149.         if (strcmp (p, "4000") == 0
  6150.             || strcmp (p, "4k") == 0
  6151.             || strcmp (p, "4K") == 0)
  6152.           mips_cpu = 4000;
  6153.         else if (strcmp (p, "4100") == 0)
  6154.                   {
  6155.                     mips_cpu = 4100;
  6156.                     if (mips_4100 < 0)
  6157.                       mips_4100 = 1;
  6158.                   }
  6159.         else if (strcmp (p, "4300") == 0)
  6160.           mips_cpu = 4300;
  6161.         else if (strcmp (p, "4400") == 0)
  6162.           mips_cpu = 4400;
  6163.         else if (strcmp (p, "4600") == 0)
  6164.           mips_cpu = 4600;
  6165.         else if (strcmp (p, "4650") == 0)
  6166.           {
  6167.             mips_cpu = 4650;
  6168.             if (mips_4650 < 0)
  6169.               mips_4650 = 1;
  6170.           }
  6171.         else if (strcmp (p, "4010") == 0)
  6172.           {
  6173.             mips_cpu = 4010;
  6174.             if (mips_4010 < 0)
  6175.               mips_4010 = 1;
  6176.           }
  6177.         break;
  6178.  
  6179.           case '6':
  6180.         if (strcmp (p, "6000") == 0
  6181.             || strcmp (p, "6k") == 0
  6182.             || strcmp (p, "6K") == 0)
  6183.           mips_cpu = 6000;
  6184.         break;
  6185.  
  6186.           case '8':
  6187.         if (strcmp (p, "8000") == 0
  6188.             || strcmp (p, "8k") == 0
  6189.             || strcmp (p, "8K") == 0)
  6190.           mips_cpu = 8000;
  6191.         break;
  6192.  
  6193.           case 'o':
  6194.         if (strcmp (p, "orion") == 0)
  6195.           mips_cpu = 4600;
  6196.         break;
  6197.           }
  6198.  
  6199.         if (sv && mips_cpu != 4300 && mips_cpu != 4100)
  6200.           {
  6201.         as_bad ("ignoring invalid leading 'v' in -mcpu=%s switch", arg);
  6202.         return 0;
  6203.           }
  6204.  
  6205.         if (mips_cpu == -1)
  6206.           {
  6207.         as_bad ("invalid architecture -mcpu=%s", arg);
  6208.         return 0;
  6209.           }
  6210.       }
  6211.       }
  6212.       break;
  6213.  
  6214.     case OPTION_M4650:
  6215.       mips_4650 = 1;
  6216.       break;
  6217.  
  6218.     case OPTION_NO_M4650:
  6219.       mips_4650 = 0;
  6220.       break;
  6221.  
  6222.     case OPTION_M4010:
  6223.       mips_4010 = 1;
  6224.       break;
  6225.  
  6226.     case OPTION_NO_M4010:
  6227.       mips_4010 = 0;
  6228.       break;
  6229.  
  6230.     case OPTION_M4100:
  6231.       mips_4100 = 1;
  6232.       break;
  6233.  
  6234.     case OPTION_NO_M4100:
  6235.       mips_4100 = 0;
  6236.       break;
  6237.  
  6238.     case OPTION_MEMBEDDED_PIC:
  6239.       mips_pic = EMBEDDED_PIC;
  6240.       if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
  6241.     {
  6242.       as_bad ("-G may not be used with embedded PIC code");
  6243.       return 0;
  6244.     }
  6245.       g_switch_value = 0x7fffffff;
  6246.       break;
  6247.  
  6248.       /* When generating ELF code, we permit -KPIC and -call_shared to
  6249.      select SVR4_PIC, and -non_shared to select no PIC.  This is
  6250.      intended to be compatible with Irix 5.  */
  6251.     case OPTION_CALL_SHARED:
  6252.       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
  6253.     {
  6254.       as_bad ("-call_shared is supported only for ELF format");
  6255.       return 0;
  6256.     }
  6257.       mips_pic = SVR4_PIC;
  6258.       if (g_switch_seen && g_switch_value != 0)
  6259.     {
  6260.       as_bad ("-G may not be used with SVR4 PIC code");
  6261.       return 0;
  6262.     }
  6263.       g_switch_value = 0;
  6264.       break;
  6265.  
  6266.     case OPTION_NON_SHARED:
  6267.       if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
  6268.     {
  6269.       as_bad ("-non_shared is supported only for ELF format");
  6270.       return 0;
  6271.     }
  6272.       mips_pic = NO_PIC;
  6273.       break;
  6274.  
  6275.       /* The -xgot option tells the assembler to use 32 offsets when
  6276.          accessing the got in SVR4_PIC mode.  It is for Irix
  6277.          compatibility.  */
  6278.     case OPTION_XGOT:
  6279.       mips_big_got = 1;
  6280.       break;
  6281.  
  6282.     case 'G':
  6283.       if (! USE_GLOBAL_POINTER_OPT)
  6284.     {
  6285.       as_bad ("-G is not supported for this configuration");
  6286.       return 0;
  6287.     }
  6288.       else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
  6289.     {
  6290.       as_bad ("-G may not be used with SVR4 or embedded PIC code");
  6291.       return 0;
  6292.     }
  6293.       else
  6294.     g_switch_value = atoi (arg);
  6295.       g_switch_seen = 1;
  6296.       break;
  6297.  
  6298.       /* The -32 and -64 options tell the assembler to output the 32
  6299.          bit or the 64 bit MIPS ELF format.  */
  6300.     case OPTION_32:
  6301.       mips_64 = 0;
  6302.       break;
  6303.  
  6304.     case OPTION_64:
  6305.       {
  6306.     const char **list, **l;
  6307.  
  6308.     list = bfd_target_list ();
  6309.     for (l = list; *l != NULL; l++)
  6310.       if (strcmp (*l, "elf64-bigmips") == 0
  6311.           || strcmp (*l, "elf64-littlemips") == 0)
  6312.         break;
  6313.     if (*l == NULL)
  6314.       as_fatal ("No compiled in support for 64 bit object file format");
  6315.     free (list);
  6316.     mips_64 = 1;
  6317.       }
  6318.       break;
  6319.  
  6320.     default:
  6321.       return 0;
  6322.     }
  6323.  
  6324.   return 1;
  6325. }
  6326.  
  6327. void
  6328. md_show_usage (stream)
  6329.      FILE *stream;
  6330. {
  6331.   fprintf(stream, "\
  6332. MIPS options:\n\
  6333. -membedded-pic        generate embedded position independent code\n\
  6334. -EB            generate big endian output\n\
  6335. -EL            generate little endian output\n\
  6336. -g, -g2            do not remove uneeded NOPs or swap branches\n\
  6337. -G NUM            allow referencing objects up to NUM bytes\n\
  6338.             implicitly with the gp register [default 8]\n");
  6339.   fprintf(stream, "\
  6340. -mips1, -mcpu=r{2,3}000    generate code for r2000 and r3000\n\
  6341. -mips2, -mcpu=r6000    generate code for r6000\n\
  6342. -mips3, -mcpu=r4000    generate code for r4000\n\
  6343. -mips4, -mcpu=r8000    generate code for r8000\n\
  6344. -mcpu=vr4300        generate code for vr4300\n\
  6345. -mcpu=vr4100        generate code for vr4100\n\
  6346. -m4650            permit R4650 instructions\n\
  6347. -no-m4650        do not permit R4650 instructions\n\
  6348. -m4010            permit R4010 instructions\n\
  6349. -no-m4010        do not permit R4010 instructions\n\
  6350. -m4100                  permit VR4100 instructions\n\
  6351. -no-m4100        do not permit VR4100 instructions\n");
  6352.   fprintf(stream, "\
  6353. -O0            remove unneeded NOPs, do not swap branches\n\
  6354. -O            remove unneeded NOPs and swap branches\n\
  6355. --trap, --no-break    trap exception on div by 0 and mult overflow\n\
  6356. --break, --no-trap    break exception on div by 0 and mult overflow\n");
  6357. #ifdef OBJ_ELF
  6358.   fprintf(stream, "\
  6359. -KPIC, -call_shared    generate SVR4 position independent code\n\
  6360. -non_shared        do not generate position independent code\n\
  6361. -xgot            assume a 32 bit GOT\n\
  6362. -32            create 32 bit object file (default)\n\
  6363. -64            create 64 bit object file\n");
  6364. #endif
  6365. }
  6366.  
  6367. void
  6368. mips_init_after_args ()
  6369. {
  6370.   if (target_big_endian)
  6371.     byte_order = BIG_ENDIAN;
  6372.   else
  6373.     byte_order = LITTLE_ENDIAN;
  6374. }
  6375.  
  6376. long
  6377. md_pcrel_from (fixP)
  6378.      fixS *fixP;
  6379. {
  6380.   if (OUTPUT_FLAVOR != bfd_target_aout_flavour
  6381.       && fixP->fx_addsy != (symbolS *) NULL
  6382.       && ! S_IS_DEFINED (fixP->fx_addsy))
  6383.     {
  6384.       /* This makes a branch to an undefined symbol be a branch to the
  6385.      current location.  */
  6386.       return 4;
  6387.     }
  6388.  
  6389.   /* return the address of the delay slot */
  6390.   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  6391. }
  6392.  
  6393. /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
  6394.    reloc for a cons.  We could use the definition there, except that
  6395.    we want to handle 64 bit relocs specially.  */
  6396.  
  6397. void
  6398. cons_fix_new_mips (frag, where, nbytes, exp)
  6399.      fragS *frag;
  6400.      int where;
  6401.      unsigned int nbytes;
  6402.      expressionS *exp;
  6403. {
  6404.   /* If we are assembling in 32 bit mode, turn an 8 byte reloc into a
  6405.      4 byte reloc.  */
  6406.   if (nbytes == 8 && ! mips_64)
  6407.     {
  6408.       if (byte_order == BIG_ENDIAN)
  6409.     where += 4;
  6410.       nbytes = 4;
  6411.     }
  6412.  
  6413.   if (nbytes != 2 && nbytes != 4 && nbytes != 8)
  6414.     as_bad ("Unsupported reloc size %d", nbytes);
  6415.  
  6416.   fix_new_exp (frag_now, where, (int) nbytes, exp, 0,
  6417.            (nbytes == 2
  6418.         ? BFD_RELOC_16
  6419.         : (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
  6420. }
  6421.  
  6422. /* Sort any unmatched HI16_S relocs so that they immediately precede
  6423.    the corresponding LO reloc.  This is called before md_apply_fix and
  6424.    tc_gen_reloc.  Unmatched HI16_S relocs can only be generated by
  6425.    explicit use of the %hi modifier.  */
  6426.  
  6427. void
  6428. mips_frob_file ()
  6429. {
  6430.   struct mips_hi_fixup *l;
  6431.  
  6432.   for (l = mips_hi_fixup_list; l != NULL; l = l->next)
  6433.     {
  6434.       segment_info_type *seginfo;
  6435.       int pass;
  6436.  
  6437.       assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
  6438.  
  6439.       /* Check quickly whether the next fixup happens to be a matching
  6440.          %lo.  */
  6441.       if (l->fixp->fx_next != NULL
  6442.       && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
  6443.       && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
  6444.       && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
  6445.     continue;
  6446.  
  6447.       /* Look through the fixups for this segment for a matching %lo.
  6448.          When we find one, move the %hi just in front of it.  We do
  6449.          this in two passes.  In the first pass, we try to find a
  6450.          unique %lo.  In the second pass, we permit multiple %hi
  6451.          relocs for a single %lo (this is a GNU extension).  */
  6452.       seginfo = seg_info (l->seg);
  6453.       for (pass = 0; pass < 2; pass++)
  6454.     {
  6455.       fixS *f, *prev;
  6456.  
  6457.       prev = NULL;
  6458.       for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
  6459.         {
  6460.           /* Check whether this is a %lo fixup which matches l->fixp.  */
  6461.           if (f->fx_r_type == BFD_RELOC_LO16
  6462.           && f->fx_addsy == l->fixp->fx_addsy
  6463.           && f->fx_offset == l->fixp->fx_offset
  6464.           && (pass == 1
  6465.               || prev == NULL
  6466.               || prev->fx_r_type != BFD_RELOC_HI16_S
  6467.               || prev->fx_addsy != f->fx_addsy
  6468.               || prev->fx_offset !=  f->fx_offset))
  6469.         {
  6470.           fixS **pf;
  6471.  
  6472.           /* Move l->fixp before f.  */
  6473.           for (pf = &seginfo->fix_root;
  6474.                *pf != l->fixp;
  6475.                pf = &(*pf)->fx_next)
  6476.             assert (*pf != NULL);
  6477.  
  6478.           *pf = l->fixp->fx_next;
  6479.  
  6480.           l->fixp->fx_next = f;
  6481.           if (prev == NULL)
  6482.             seginfo->fix_root = l->fixp;
  6483.           else
  6484.             prev->fx_next = l->fixp;
  6485.  
  6486.           break;
  6487.         }
  6488.  
  6489.           prev = f;
  6490.         }
  6491.  
  6492.       if (f != NULL)
  6493.         break;
  6494.  
  6495.       if (pass == 1)
  6496.         as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
  6497.                "Unmatched %%hi reloc");
  6498.     }
  6499.     }
  6500. }
  6501.  
  6502. /* When generating embedded PIC code we need to use a special
  6503.    relocation to represent the difference of two symbols in the .text
  6504.    section (switch tables use a difference of this sort).  See
  6505.    include/coff/mips.h for details.  This macro checks whether this
  6506.    fixup requires the special reloc.  */
  6507. #define SWITCH_TABLE(fixp) \
  6508.   ((fixp)->fx_r_type == BFD_RELOC_32 \
  6509.    && (fixp)->fx_addsy != NULL \
  6510.    && (fixp)->fx_subsy != NULL \
  6511.    && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
  6512.    && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
  6513.  
  6514. /* When generating embedded PIC code we must keep all PC relative
  6515.    relocations, in case the linker has to relax a call.  We also need
  6516.    to keep relocations for switch table entries.  */
  6517.  
  6518. /*ARGSUSED*/
  6519. int
  6520. mips_force_relocation (fixp)
  6521.      fixS *fixp;
  6522. {
  6523.   return (mips_pic == EMBEDDED_PIC
  6524.       && (fixp->fx_pcrel
  6525.           || SWITCH_TABLE (fixp)
  6526.           || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
  6527.           || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
  6528. }
  6529.  
  6530. /* Apply a fixup to the object file.  */
  6531.  
  6532. int
  6533. md_apply_fix (fixP, valueP)
  6534.      fixS *fixP;
  6535.      valueT *valueP;
  6536. {
  6537.   unsigned char *buf;
  6538.   long insn, value;
  6539.  
  6540.   assert (fixP->fx_size == 4 || fixP->fx_r_type == BFD_RELOC_16);
  6541.  
  6542.   value = *valueP;
  6543.   fixP->fx_addnumber = value;    /* Remember value for tc_gen_reloc */
  6544.  
  6545.   if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
  6546.     fixP->fx_done = 1;
  6547.  
  6548.   switch (fixP->fx_r_type)
  6549.     {
  6550.     case BFD_RELOC_MIPS_JMP:
  6551.     case BFD_RELOC_HI16:
  6552.     case BFD_RELOC_HI16_S:
  6553.     case BFD_RELOC_MIPS_GPREL:
  6554.     case BFD_RELOC_MIPS_LITERAL:
  6555.     case BFD_RELOC_MIPS_CALL16:
  6556.     case BFD_RELOC_MIPS_GOT16:
  6557.     case BFD_RELOC_MIPS_GPREL32:
  6558.     case BFD_RELOC_MIPS_GOT_HI16:
  6559.     case BFD_RELOC_MIPS_GOT_LO16:
  6560.     case BFD_RELOC_MIPS_CALL_HI16:
  6561.     case BFD_RELOC_MIPS_CALL_LO16:
  6562.       if (fixP->fx_pcrel)
  6563.     as_bad_where (fixP->fx_file, fixP->fx_line,
  6564.               "Invalid PC relative reloc");
  6565.       /* Nothing needed to do. The value comes from the reloc entry */
  6566.       break;
  6567.  
  6568.     case BFD_RELOC_PCREL_HI16_S:
  6569.       /* The addend for this is tricky if it is internal, so we just
  6570.      do everything here rather than in bfd_perform_relocation.  */
  6571.       if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
  6572.     {
  6573.       /* For an external symbol adjust by the address to make it
  6574.          pcrel_offset.  We use the address of the RELLO reloc
  6575.          which follows this one.  */
  6576.       value += (fixP->fx_next->fx_frag->fr_address
  6577.             + fixP->fx_next->fx_where);
  6578.     }
  6579.       if (value & 0x8000)
  6580.     value += 0x10000;
  6581.       value >>= 16;
  6582.       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
  6583.       if (byte_order == BIG_ENDIAN)
  6584.     buf += 2;
  6585.       md_number_to_chars (buf, value, 2);
  6586.       break;
  6587.  
  6588.     case BFD_RELOC_PCREL_LO16:
  6589.       /* The addend for this is tricky if it is internal, so we just
  6590.      do everything here rather than in bfd_perform_relocation.  */
  6591.       if ((fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
  6592.     value += fixP->fx_frag->fr_address + fixP->fx_where;
  6593.       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
  6594.       if (byte_order == BIG_ENDIAN)
  6595.     buf += 2;
  6596.       md_number_to_chars (buf, value, 2);
  6597.       break;
  6598.  
  6599.     case BFD_RELOC_32:
  6600.       /* If we are deleting this reloc entry, we must fill in the
  6601.      value now.  This can happen if we have a .word which is not
  6602.      resolved when it appears but is later defined.  We also need
  6603.      to fill in the value if this is an embedded PIC switch table
  6604.      entry.  */
  6605.       if (fixP->fx_done
  6606.       || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
  6607.     md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
  6608.                 value, 4);
  6609.       break;
  6610.  
  6611.     case BFD_RELOC_16:
  6612.       /* If we are deleting this reloc entry, we must fill in the
  6613.          value now.  */
  6614.       assert (fixP->fx_size == 2);
  6615.       if (fixP->fx_done)
  6616.     md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
  6617.                 value, 2);
  6618.       break;
  6619.  
  6620.     case BFD_RELOC_LO16:
  6621.       /* When handling an embedded PIC switch statement, we can wind
  6622.      up deleting a LO16 reloc.  See the 'o' case in mips_ip.  */
  6623.       if (fixP->fx_done)
  6624.     {
  6625.       if (value < -0x8000 || value > 0x7fff)
  6626.         as_bad_where (fixP->fx_file, fixP->fx_line,
  6627.               "relocation overflow");
  6628.       buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
  6629.       if (byte_order == BIG_ENDIAN)
  6630.         buf += 2;
  6631.       md_number_to_chars (buf, value, 2);
  6632.     }
  6633.       break;
  6634.  
  6635.     case BFD_RELOC_16_PCREL_S2:
  6636.       /*
  6637.        * We need to save the bits in the instruction since fixup_segment()
  6638.        * might be deleting the relocation entry (i.e., a branch within
  6639.        * the current segment).
  6640.        */
  6641.       if (value & 0x3)
  6642.     as_warn_where (fixP->fx_file, fixP->fx_line,
  6643.                "Branch to odd address (%lx)", value);
  6644.       value >>= 2;
  6645.  
  6646.       /* update old instruction data */
  6647.       buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
  6648.       switch (byte_order)
  6649.     {
  6650.     case LITTLE_ENDIAN:
  6651.       insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
  6652.       break;
  6653.  
  6654.     case BIG_ENDIAN:
  6655.       insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  6656.       break;
  6657.  
  6658.     default:
  6659.       internalError ();
  6660.       return 0;
  6661.     }
  6662.  
  6663.       if (value >= -0x8000 && value < 0x8000)
  6664.     insn |= value & 0xffff;
  6665.       else
  6666.     {
  6667.       /* The branch offset is too large.  If this is an
  6668.              unconditional branch, and we are not generating PIC code,
  6669.              we can convert it to an absolute jump instruction.  */
  6670.       if (mips_pic == NO_PIC
  6671.           && fixP->fx_done
  6672.           && fixP->fx_frag->fr_address >= text_section->vma
  6673.           && (fixP->fx_frag->fr_address
  6674.           < text_section->vma + text_section->_raw_size)
  6675.           && ((insn & 0xffff0000) == 0x10000000     /* beq $0,$0 */
  6676.           || (insn & 0xffff0000) == 0x04010000     /* bgez $0 */
  6677.           || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
  6678.         {
  6679.           if ((insn & 0xffff0000) == 0x04110000)     /* bgezal $0 */
  6680.         insn = 0x0c000000;    /* jal */
  6681.           else
  6682.         insn = 0x08000000;    /* j */
  6683.           fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
  6684.           fixP->fx_done = 0;
  6685.           fixP->fx_addsy = section_symbol (text_section);
  6686.           fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
  6687.         }
  6688.       else
  6689.         {
  6690.           /* FIXME.  It would be possible in principle to handle
  6691.                  conditional branches which overflow.  They could be
  6692.                  transformed into a branch around a jump.  This would
  6693.                  require setting up variant frags for each different
  6694.                  branch type.  The native MIPS assembler attempts to
  6695.                  handle these cases, but it appears to do it
  6696.                  incorrectly.  */
  6697.           as_bad_where (fixP->fx_file, fixP->fx_line,
  6698.                 "Relocation overflow");
  6699.         }
  6700.     }
  6701.  
  6702.       md_number_to_chars ((char *) buf, (valueT) insn, 4);
  6703.       break;
  6704.  
  6705.     default:
  6706.       internalError ();
  6707.     }
  6708.  
  6709.   return 1;
  6710. }
  6711.  
  6712. #if 0
  6713. void
  6714. printInsn (oc)
  6715.      unsigned long oc;
  6716. {
  6717.   const struct mips_opcode *p;
  6718.   int treg, sreg, dreg, shamt;
  6719.   short imm;
  6720.   const char *args;
  6721.   int i;
  6722.  
  6723.   for (i = 0; i < NUMOPCODES; ++i)
  6724.     {
  6725.       p = &mips_opcodes[i];
  6726.       if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
  6727.     {
  6728.       printf ("%08lx %s\t", oc, p->name);
  6729.       treg = (oc >> 16) & 0x1f;
  6730.       sreg = (oc >> 21) & 0x1f;
  6731.       dreg = (oc >> 11) & 0x1f;
  6732.       shamt = (oc >> 6) & 0x1f;
  6733.       imm = oc;
  6734.       for (args = p->args;; ++args)
  6735.         {
  6736.           switch (*args)
  6737.         {
  6738.         case '\0':
  6739.           printf ("\n");
  6740.           break;
  6741.  
  6742.         case ',':
  6743.         case '(':
  6744.         case ')':
  6745.           printf ("%c", *args);
  6746.           continue;
  6747.  
  6748.         case 'r':
  6749.           assert (treg == sreg);
  6750.           printf ("$%d,$%d", treg, sreg);
  6751.           continue;
  6752.  
  6753.         case 'd':
  6754.         case 'G':
  6755.           printf ("$%d", dreg);
  6756.           continue;
  6757.  
  6758.         case 't':
  6759.         case 'E':
  6760.           printf ("$%d", treg);
  6761.           continue;
  6762.  
  6763.         case 'k':
  6764.           printf ("0x%x", treg);
  6765.           continue;
  6766.  
  6767.         case 'b':
  6768.         case 's':
  6769.           printf ("$%d", sreg);
  6770.           continue;
  6771.  
  6772.         case 'a':
  6773.           printf ("0x%08lx", oc & 0x1ffffff);
  6774.           continue;
  6775.  
  6776.         case 'i':
  6777.         case 'j':
  6778.         case 'o':
  6779.         case 'u':
  6780.           printf ("%d", imm);
  6781.           continue;
  6782.  
  6783.         case '<':
  6784.         case '>':
  6785.           printf ("$%d", shamt);
  6786.           continue;
  6787.  
  6788.         default:
  6789.           internalError ();
  6790.         }
  6791.           break;
  6792.         }
  6793.       return;
  6794.     }
  6795.     }
  6796.   printf ("%08lx  UNDEFINED\n", oc);
  6797. }
  6798. #endif
  6799.  
  6800. static symbolS *
  6801. get_symbol ()
  6802. {
  6803.   int c;
  6804.   char *name;
  6805.   symbolS *p;
  6806.  
  6807.   name = input_line_pointer;
  6808.   c = get_symbol_end ();
  6809.   p = (symbolS *) symbol_find_or_make (name);
  6810.   *input_line_pointer = c;
  6811.   return p;
  6812. }
  6813.  
  6814. /* Align the current frag to a given power of two.  The MIPS assembler
  6815.    also automatically adjusts any preceding label.  */
  6816.  
  6817. static void
  6818. mips_align (to, fill, label)
  6819.      int to;
  6820.      int fill;
  6821.      symbolS *label;
  6822. {
  6823.   mips_emit_delays ();
  6824.   frag_align (to, fill);
  6825.   record_alignment (now_seg, to);
  6826.   if (label != NULL)
  6827.     {
  6828.       assert (S_GET_SEGMENT (label) == now_seg);
  6829.       label->sy_frag = frag_now;
  6830.       S_SET_VALUE (label, (valueT) frag_now_fix ());
  6831.     }
  6832. }
  6833.  
  6834. /* Align to a given power of two.  .align 0 turns off the automatic
  6835.    alignment used by the data creating pseudo-ops.  */
  6836.  
  6837. static void
  6838. s_align (x)
  6839.      int x;
  6840. {
  6841.   register int temp;
  6842.   register long temp_fill;
  6843.   long max_alignment = 15;
  6844.  
  6845.   /*
  6846.  
  6847.     o  Note that the assembler pulls down any immediately preceeding label
  6848.        to the aligned address.
  6849.     o  It's not documented but auto alignment is reinstated by
  6850.        a .align pseudo instruction.
  6851.     o  Note also that after auto alignment is turned off the mips assembler
  6852.        issues an error on attempt to assemble an improperly aligned data item.
  6853.        We don't.
  6854.  
  6855.     */
  6856.  
  6857.   temp = get_absolute_expression ();
  6858.   if (temp > max_alignment)
  6859.     as_bad ("Alignment too large: %d. assumed.", temp = max_alignment);
  6860.   else if (temp < 0)
  6861.     {
  6862.       as_warn ("Alignment negative: 0 assumed.");
  6863.       temp = 0;
  6864.     }
  6865.   if (*input_line_pointer == ',')
  6866.     {
  6867.       input_line_pointer++;
  6868.       temp_fill = get_absolute_expression ();
  6869.     }
  6870.   else
  6871.     temp_fill = 0;
  6872.   if (temp)
  6873.     {
  6874.       auto_align = 1;
  6875.       mips_align (temp, (int) temp_fill, insn_label);
  6876.     }
  6877.   else
  6878.     {
  6879.       auto_align = 0;
  6880.     }
  6881.  
  6882.   demand_empty_rest_of_line ();
  6883. }
  6884.  
  6885. void
  6886. mips_flush_pending_output ()
  6887. {
  6888.   mips_emit_delays ();
  6889.   insn_label = NULL;
  6890. }
  6891.  
  6892. static void
  6893. s_change_sec (sec)
  6894.      int sec;
  6895. {
  6896.   segT seg;
  6897.  
  6898.   /* When generating embedded PIC code, we only use the .text, .lit8,
  6899.      .sdata and .sbss sections.  We change the .data and .rdata
  6900.      pseudo-ops to use .sdata.  */
  6901.   if (mips_pic == EMBEDDED_PIC
  6902.       && (sec == 'd' || sec == 'r'))
  6903.     sec = 's';
  6904.  
  6905.   mips_emit_delays ();
  6906.   switch (sec)
  6907.     {
  6908.     case 't':
  6909.       s_text (0);
  6910.       break;
  6911.     case 'd':
  6912.       s_data (0);
  6913.       break;
  6914.     case 'b':
  6915.       subseg_set (bss_section, (subsegT) get_absolute_expression ());
  6916.       demand_empty_rest_of_line ();
  6917.       break;
  6918.  
  6919.     case 'r':
  6920.       if (USE_GLOBAL_POINTER_OPT)
  6921.     {
  6922.       seg = subseg_new (RDATA_SECTION_NAME,
  6923.                 (subsegT) get_absolute_expression ());
  6924.       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
  6925.         {
  6926.           bfd_set_section_flags (stdoutput, seg,
  6927.                      (SEC_ALLOC
  6928.                       | SEC_LOAD
  6929.                       | SEC_READONLY
  6930.                       | SEC_RELOC
  6931.                       | SEC_DATA));
  6932.           bfd_set_section_alignment (stdoutput, seg, 4);
  6933.         }
  6934.       demand_empty_rest_of_line ();
  6935.     }
  6936.       else
  6937.     {
  6938.       as_bad ("No read only data section in this object file format");
  6939.       demand_empty_rest_of_line ();
  6940.       return;
  6941.     }
  6942.       break;
  6943.  
  6944.     case 's':
  6945.       if (USE_GLOBAL_POINTER_OPT)
  6946.     {
  6947.       seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
  6948.       if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
  6949.         {
  6950.           bfd_set_section_flags (stdoutput, seg,
  6951.                      SEC_ALLOC | SEC_LOAD | SEC_RELOC
  6952.                      | SEC_DATA);
  6953.           bfd_set_section_alignment (stdoutput, seg, 4);
  6954.         }
  6955.       demand_empty_rest_of_line ();
  6956.       break;
  6957.     }
  6958.       else
  6959.     {
  6960.       as_bad ("Global pointers not supported; recompile -G 0");
  6961.       demand_empty_rest_of_line ();
  6962.       return;
  6963.     }
  6964.     }
  6965.  
  6966.   auto_align = 1;
  6967. }
  6968.  
  6969. void
  6970. mips_enable_auto_align ()
  6971. {
  6972.   auto_align = 1;
  6973. }
  6974.  
  6975. static void
  6976. s_cons (log_size)
  6977.      int log_size;
  6978. {
  6979.   symbolS *label;
  6980.  
  6981.   label = insn_label;
  6982.   mips_emit_delays ();
  6983.   if (log_size > 0 && auto_align)
  6984.     mips_align (log_size, 0, label);
  6985.   insn_label = NULL;
  6986.   cons (1 << log_size);
  6987. }
  6988.  
  6989. static void
  6990. s_float_cons (type)
  6991.      int type;
  6992. {
  6993.   symbolS *label;
  6994.  
  6995.   label = insn_label;
  6996.  
  6997.   mips_emit_delays ();
  6998.  
  6999.   if (auto_align)
  7000.     if (type == 'd')
  7001.       mips_align (3, 0, label);
  7002.     else
  7003.       mips_align (2, 0, label);
  7004.  
  7005.   insn_label = NULL;
  7006.  
  7007.   float_cons (type);
  7008. }
  7009.  
  7010. /* Handle .globl.  We need to override it because on Irix 5 you are
  7011.    permitted to say
  7012.        .globl foo .text
  7013.    where foo is an undefined symbol, to mean that foo should be
  7014.    considered to be the address of a function.  */
  7015.  
  7016. static void
  7017. s_mips_globl (x)
  7018.      int x;
  7019. {
  7020.   char *name;
  7021.   int c;
  7022.   symbolS *symbolP;
  7023.   flagword flag;
  7024.  
  7025.   name = input_line_pointer;
  7026.   c = get_symbol_end ();
  7027.   symbolP = symbol_find_or_make (name);
  7028.   *input_line_pointer = c;
  7029.   SKIP_WHITESPACE ();
  7030.  
  7031.   /* On Irix 5, every global symbol that is not explicitly labelled as
  7032.      being a function is apparently labelled as being an object.  */
  7033.   flag = BSF_OBJECT;
  7034.  
  7035.   if (! is_end_of_line[(unsigned char) *input_line_pointer])
  7036.     {
  7037.       char *secname;
  7038.       asection *sec;
  7039.  
  7040.       secname = input_line_pointer;
  7041.       c = get_symbol_end ();
  7042.       sec = bfd_get_section_by_name (stdoutput, secname);
  7043.       if (sec == NULL)
  7044.     as_bad ("%s: no such section", secname);
  7045.       *input_line_pointer = c;
  7046.  
  7047.       if (sec != NULL && (sec->flags & SEC_CODE) != 0)
  7048.     flag = BSF_FUNCTION;
  7049.     }
  7050.  
  7051.   symbolP->bsym->flags |= flag;
  7052.  
  7053.   S_SET_EXTERNAL (symbolP);
  7054.   demand_empty_rest_of_line ();
  7055. }
  7056.  
  7057. static void
  7058. s_option (x)
  7059.      int x;
  7060. {
  7061.   char *opt;
  7062.   char c;
  7063.  
  7064.   opt = input_line_pointer;
  7065.   c = get_symbol_end ();
  7066.  
  7067.   if (*opt == 'O')
  7068.     {
  7069.       /* FIXME: What does this mean?  */
  7070.     }
  7071.   else if (strncmp (opt, "pic", 3) == 0)
  7072.     {
  7073.       int i;
  7074.  
  7075.       i = atoi (opt + 3);
  7076.       if (i == 0)
  7077.     mips_pic = NO_PIC;
  7078.       else if (i == 2)
  7079.     mips_pic = SVR4_PIC;
  7080.       else
  7081.     as_bad (".option pic%d not supported", i);
  7082.  
  7083.       if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
  7084.     {
  7085.       if (g_switch_seen && g_switch_value != 0)
  7086.         as_warn ("-G may not be used with SVR4 PIC code");
  7087.       g_switch_value = 0;
  7088.       bfd_set_gp_size (stdoutput, 0);
  7089.     }
  7090.     }
  7091.   else
  7092.     as_warn ("Unrecognized option \"%s\"", opt);
  7093.  
  7094.   *input_line_pointer = c;
  7095.   demand_empty_rest_of_line ();
  7096. }
  7097.  
  7098. static void
  7099. s_mipsset (x)
  7100.      int x;
  7101. {
  7102.   char *name = input_line_pointer, ch;
  7103.  
  7104.   while (!is_end_of_line[(unsigned char) *input_line_pointer])
  7105.     input_line_pointer++;
  7106.   ch = *input_line_pointer;
  7107.   *input_line_pointer = '\0';
  7108.  
  7109.   if (strcmp (name, "reorder") == 0)
  7110.     {
  7111.       if (mips_noreorder)
  7112.     {
  7113.       prev_insn_unreordered = 1;
  7114.       prev_prev_insn_unreordered = 1;
  7115.     }
  7116.       mips_noreorder = 0;
  7117.     }
  7118.   else if (strcmp (name, "noreorder") == 0)
  7119.     {
  7120.       mips_emit_delays ();
  7121.       mips_noreorder = 1;
  7122.       mips_any_noreorder = 1;
  7123.     }
  7124.   else if (strcmp (name, "at") == 0)
  7125.     {
  7126.       mips_noat = 0;
  7127.     }
  7128.   else if (strcmp (name, "noat") == 0)
  7129.     {
  7130.       mips_noat = 1;
  7131.     }
  7132.   else if (strcmp (name, "macro") == 0)
  7133.     {
  7134.       mips_warn_about_macros = 0;
  7135.     }
  7136.   else if (strcmp (name, "nomacro") == 0)
  7137.     {
  7138.       if (mips_noreorder == 0)
  7139.     as_bad ("`noreorder' must be set before `nomacro'");
  7140.       mips_warn_about_macros = 1;
  7141.     }
  7142.   else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
  7143.     {
  7144.       mips_nomove = 0;
  7145.     }
  7146.   else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
  7147.     {
  7148.       mips_nomove = 1;
  7149.     }
  7150.   else if (strcmp (name, "bopt") == 0)
  7151.     {
  7152.       mips_nobopt = 0;
  7153.     }
  7154.   else if (strcmp (name, "nobopt") == 0)
  7155.     {
  7156.       mips_nobopt = 1;
  7157.     }
  7158.   else if (strncmp (name, "mips", 4) == 0)
  7159.     {
  7160.       int isa;
  7161.  
  7162.       /* Permit the user to change the ISA on the fly.  Needless to
  7163.      say, misuse can cause serious problems.  */
  7164.       isa = atoi (name + 4);
  7165.       if (isa == 0)
  7166.     mips_isa = file_mips_isa;
  7167.       else if (isa < 1 || isa > 4)
  7168.     as_bad ("unknown ISA level");
  7169.       else
  7170.     mips_isa = isa;
  7171.     }
  7172.   else
  7173.     {
  7174.       as_warn ("Tried to set unrecognized symbol: %s\n", name);
  7175.     }
  7176.   *input_line_pointer = ch;
  7177.   demand_empty_rest_of_line ();
  7178. }
  7179.  
  7180. /* Handle the .abicalls pseudo-op.  I believe this is equivalent to
  7181.    .option pic2.  It means to generate SVR4 PIC calls.  */
  7182.  
  7183. static void
  7184. s_abicalls (ignore)
  7185.      int ignore;
  7186. {
  7187.   mips_pic = SVR4_PIC;
  7188.   if (USE_GLOBAL_POINTER_OPT)
  7189.     {
  7190.       if (g_switch_seen && g_switch_value != 0)
  7191.     as_warn ("-G may not be used with SVR4 PIC code");
  7192.       g_switch_value = 0;
  7193.     }
  7194.   bfd_set_gp_size (stdoutput, 0);
  7195.   demand_empty_rest_of_line ();
  7196. }
  7197.  
  7198. /* Handle the .cpload pseudo-op.  This is used when generating SVR4
  7199.    PIC code.  It sets the $gp register for the function based on the
  7200.    function address, which is in the register named in the argument.
  7201.    This uses a relocation against _gp_disp, which is handled specially
  7202.    by the linker.  The result is:
  7203.     lui    $gp,%hi(_gp_disp)
  7204.     addiu    $gp,$gp,%lo(_gp_disp)
  7205.     addu    $gp,$gp,.cpload argument
  7206.    The .cpload argument is normally $25 == $t9.  */
  7207.  
  7208. static void
  7209. s_cpload (ignore)
  7210.      int ignore;
  7211. {
  7212.   expressionS ex;
  7213.   int icnt = 0;
  7214.  
  7215.   /* If we are not generating SVR4 PIC code, .cpload is ignored.  */
  7216.   if (mips_pic != SVR4_PIC)
  7217.     {
  7218.       s_ignore (0);
  7219.       return;
  7220.     }
  7221.  
  7222.   /* .cpload should be a in .set noreorder section.  */
  7223.   if (mips_noreorder == 0)
  7224.     as_warn (".cpload not in noreorder section");
  7225.  
  7226.   ex.X_op = O_symbol;
  7227.   ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
  7228.   ex.X_op_symbol = NULL;
  7229.   ex.X_add_number = 0;
  7230.  
  7231.   /* In ELF, this symbol is implicitly an STT_OBJECT symbol.  */
  7232.   ex.X_add_symbol->bsym->flags |= BSF_OBJECT;
  7233.  
  7234.   macro_build_lui ((char *) NULL, &icnt, &ex, GP);
  7235.   macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
  7236.            (int) BFD_RELOC_LO16);
  7237.  
  7238.   macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
  7239.            GP, GP, tc_get_register (0));
  7240.  
  7241.   demand_empty_rest_of_line ();
  7242. }
  7243.  
  7244. /* Handle the .cprestore pseudo-op.  This stores $gp into a given
  7245.    offset from $sp.  The offset is remembered, and after making a PIC
  7246.    call $gp is restored from that location.  */
  7247.  
  7248. static void
  7249. s_cprestore (ignore)
  7250.      int ignore;
  7251. {
  7252.   expressionS ex;
  7253.   int icnt = 0;
  7254.  
  7255.   /* If we are not generating SVR4 PIC code, .cprestore is ignored.  */
  7256.   if (mips_pic != SVR4_PIC)
  7257.     {
  7258.       s_ignore (0);
  7259.       return;
  7260.     }
  7261.  
  7262.   mips_cprestore_offset = get_absolute_expression ();
  7263.  
  7264.   ex.X_op = O_constant;
  7265.   ex.X_add_symbol = NULL;
  7266.   ex.X_op_symbol = NULL;
  7267.   ex.X_add_number = mips_cprestore_offset;
  7268.  
  7269.   macro_build ((char *) NULL, &icnt, &ex,
  7270.            mips_isa < 3 ? "sw" : "sd",
  7271.            "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
  7272.  
  7273.   demand_empty_rest_of_line ();
  7274. }
  7275.  
  7276. /* Handle the .gpword pseudo-op.  This is used when generating PIC
  7277.    code.  It generates a 32 bit GP relative reloc.  */
  7278.  
  7279. static void
  7280. s_gpword (ignore)
  7281.      int ignore;
  7282. {
  7283.   symbolS *label;
  7284.   expressionS ex;
  7285.   char *p;
  7286.  
  7287.   /* When not generating PIC code, this is treated as .word.  */
  7288.   if (mips_pic != SVR4_PIC)
  7289.     {
  7290.       s_cons (2);
  7291.       return;
  7292.     }
  7293.  
  7294.   label = insn_label;
  7295.   mips_emit_delays ();
  7296.   if (auto_align)
  7297.     mips_align (2, 0, label);
  7298.   insn_label = NULL;
  7299.  
  7300.   expression (&ex);
  7301.  
  7302.   if (ex.X_op != O_symbol || ex.X_add_number != 0)
  7303.     {
  7304.       as_bad ("Unsupported use of .gpword");
  7305.       ignore_rest_of_line ();
  7306.     }
  7307.  
  7308.   p = frag_more (4);
  7309.   md_number_to_chars (p, (valueT) 0, 4);
  7310.   fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
  7311.            BFD_RELOC_MIPS_GPREL32);
  7312.  
  7313.   demand_empty_rest_of_line ();
  7314. }
  7315.  
  7316. /* Handle the .cpadd pseudo-op.  This is used when dealing with switch
  7317.    tables in SVR4 PIC code.  */
  7318.  
  7319. static void
  7320. s_cpadd (ignore)
  7321.      int ignore;
  7322. {
  7323.   int icnt = 0;
  7324.   int reg;
  7325.  
  7326.   /* This is ignored when not generating SVR4 PIC code.  */
  7327.   if (mips_pic != SVR4_PIC)
  7328.     {
  7329.       s_ignore (0);
  7330.       return;
  7331.     }
  7332.  
  7333.   /* Add $gp to the register named as an argument.  */
  7334.   reg = tc_get_register (0);
  7335.   macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
  7336.            mips_isa < 3 ? "addu" : "daddu",
  7337.            "d,v,t", reg, reg, GP);
  7338.  
  7339.   demand_empty_rest_of_line ();  
  7340. }
  7341.  
  7342. /* Parse a register string into a number.  Called from the ECOFF code
  7343.    to parse .frame.  The argument is non-zero if this is the frame
  7344.    register, so that we can record it in mips_frame_reg.  */
  7345.  
  7346. int
  7347. tc_get_register (frame)
  7348.      int frame;
  7349. {
  7350.   int reg;
  7351.  
  7352.   SKIP_WHITESPACE ();
  7353.   if (*input_line_pointer++ != '$')
  7354.     {
  7355.       as_warn ("expected `$'");
  7356.       reg = 0;
  7357.     }
  7358.   else if (isdigit ((unsigned char) *input_line_pointer))
  7359.     {
  7360.       reg = get_absolute_expression ();
  7361.       if (reg < 0 || reg >= 32)
  7362.     {
  7363.       as_warn ("Bad register number");
  7364.       reg = 0;
  7365.     }
  7366.     }
  7367.   else
  7368.     {
  7369.       if (strncmp (input_line_pointer, "fp", 2) == 0)
  7370.     reg = FP;
  7371.       else if (strncmp (input_line_pointer, "sp", 2) == 0)
  7372.     reg = SP;
  7373.       else if (strncmp (input_line_pointer, "gp", 2) == 0)
  7374.     reg = GP;
  7375.       else if (strncmp (input_line_pointer, "at", 2) == 0)
  7376.     reg = AT;
  7377.       else
  7378.     {
  7379.       as_warn ("Unrecognized register name");
  7380.       reg = 0;
  7381.     }
  7382.       input_line_pointer += 2;
  7383.     }
  7384.   if (frame)
  7385.     mips_frame_reg = reg != 0 ? reg : SP;
  7386.   return reg;
  7387. }
  7388.  
  7389. valueT
  7390. md_section_align (seg, addr)
  7391.      asection *seg;
  7392.      valueT addr;
  7393. {
  7394.   int align = bfd_get_section_alignment (stdoutput, seg);
  7395.  
  7396.   return ((addr + (1 << align) - 1) & (-1 << align));
  7397. }
  7398.  
  7399. /* Utility routine, called from above as well.  If called while the
  7400.    input file is still being read, it's only an approximation.  (For
  7401.    example, a symbol may later become defined which appeared to be
  7402.    undefined earlier.)  */
  7403.  
  7404. static int
  7405. nopic_need_relax (sym)
  7406.      symbolS *sym;
  7407. {
  7408.   if (sym == 0)
  7409.     return 0;
  7410.  
  7411.   if (USE_GLOBAL_POINTER_OPT)
  7412.     {
  7413.       const char *symname;
  7414.       int change;
  7415.  
  7416.       /* Find out whether this symbol can be referenced off the GP
  7417.      register.  It can be if it is smaller than the -G size or if
  7418.      it is in the .sdata or .sbss section.  Certain symbols can
  7419.      not be referenced off the GP, although it appears as though
  7420.      they can.  */
  7421.       symname = S_GET_NAME (sym);
  7422.       if (symname != (const char *) NULL
  7423.       && (strcmp (symname, "eprol") == 0
  7424.           || strcmp (symname, "etext") == 0
  7425.           || strcmp (symname, "_gp") == 0
  7426.           || strcmp (symname, "edata") == 0
  7427.           || strcmp (symname, "_fbss") == 0
  7428.           || strcmp (symname, "_fdata") == 0
  7429.           || strcmp (symname, "_ftext") == 0
  7430.           || strcmp (symname, "end") == 0
  7431.           || strcmp (symname, "_gp_disp") == 0))
  7432.     change = 1;
  7433.       else if (! S_IS_DEFINED (sym)
  7434.            && (0
  7435. #ifndef NO_ECOFF_DEBUGGING
  7436.            || (sym->ecoff_extern_size != 0
  7437.                && sym->ecoff_extern_size <= g_switch_value)
  7438. #endif
  7439.            || (S_GET_VALUE (sym) != 0
  7440.                && S_GET_VALUE (sym) <= g_switch_value)))
  7441.     change = 0;
  7442.       else
  7443.     {
  7444.       const char *segname;
  7445.  
  7446.       segname = segment_name (S_GET_SEGMENT (sym));
  7447.       assert (strcmp (segname, ".lit8") != 0
  7448.           && strcmp (segname, ".lit4") != 0);
  7449.       change = (strcmp (segname, ".sdata") != 0
  7450.             && strcmp (segname, ".sbss") != 0);
  7451.     }
  7452.       return change;
  7453.     }
  7454.   else
  7455.     /* We are not optimizing for the GP register.  */
  7456.     return 1;
  7457. }
  7458.  
  7459. /* Estimate the size of a frag before relaxing.  We are not really
  7460.    relaxing here, and the final size is encoded in the subtype
  7461.    information.  */
  7462.  
  7463. /*ARGSUSED*/
  7464. int
  7465. md_estimate_size_before_relax (fragp, segtype)
  7466.      fragS *fragp;
  7467.      asection *segtype;
  7468. {
  7469.   int change;
  7470.  
  7471.   if (mips_pic == NO_PIC)
  7472.     {
  7473.       change = nopic_need_relax (fragp->fr_symbol);
  7474.     }
  7475.   else if (mips_pic == SVR4_PIC)
  7476.     {
  7477.       asection *symsec = fragp->fr_symbol->bsym->section;
  7478.  
  7479.       /* This must duplicate the test in adjust_reloc_syms.  */
  7480.       change = (symsec != &bfd_und_section
  7481.         && symsec != &bfd_abs_section
  7482.         && ! bfd_is_com_section (symsec));
  7483.     }
  7484.   else
  7485.     abort ();
  7486.  
  7487.   if (change)
  7488.     {
  7489.       /* Record the offset to the first reloc in the fr_opcode field.
  7490.      This lets md_convert_frag and tc_gen_reloc know that the code
  7491.      must be expanded.  */
  7492.       fragp->fr_opcode = (fragp->fr_literal
  7493.               + fragp->fr_fix
  7494.               - RELAX_OLD (fragp->fr_subtype)
  7495.               + RELAX_RELOC1 (fragp->fr_subtype));
  7496.       /* FIXME: This really needs as_warn_where.  */
  7497.       if (RELAX_WARN (fragp->fr_subtype))
  7498.     as_warn ("AT used after \".set noat\" or macro used after \".set nomacro\"");
  7499.     }
  7500.  
  7501.   if (! change)
  7502.     return 0;
  7503.   else
  7504.     return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
  7505. }
  7506.  
  7507. /* Translate internal representation of relocation info to BFD target
  7508.    format.  */
  7509.  
  7510. arelent **
  7511. tc_gen_reloc (section, fixp)
  7512.      asection *section;
  7513.      fixS *fixp;
  7514. {
  7515.   static arelent *retval[4];
  7516.   arelent *reloc;
  7517.  
  7518.   reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
  7519.   retval[1] = NULL;
  7520.  
  7521.   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
  7522.   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  7523.  
  7524.   if (mips_pic == EMBEDDED_PIC
  7525.       && SWITCH_TABLE (fixp))
  7526.     {
  7527.       /* For a switch table entry we use a special reloc.  The addend
  7528.      is actually the difference between the reloc address and the
  7529.      subtrahend.  */
  7530.       reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
  7531.       if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
  7532.     as_fatal ("Double check fx_r_type in tc-mips.c:tc_gen_reloc");
  7533.       fixp->fx_r_type = BFD_RELOC_GPREL32;
  7534.     }
  7535.   else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
  7536.     {
  7537.       /* We use a special addend for an internal RELLO reloc.  */
  7538.       if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
  7539.     reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
  7540.       else
  7541.     reloc->addend = fixp->fx_addnumber + reloc->address;
  7542.     }
  7543.   else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
  7544.     {
  7545.       assert (fixp->fx_next != NULL
  7546.           && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
  7547.       /* We use a special addend for an internal RELHI reloc.  The
  7548.      reloc is relative to the RELLO; adjust the addend
  7549.      accordingly.  */
  7550.       if (fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM)
  7551.     reloc->addend = (fixp->fx_next->fx_frag->fr_address
  7552.              + fixp->fx_next->fx_where
  7553.              - S_GET_VALUE (fixp->fx_subsy));
  7554.       else
  7555.     reloc->addend = (fixp->fx_addnumber
  7556.              + fixp->fx_next->fx_frag->fr_address
  7557.              + fixp->fx_next->fx_where);
  7558.     }
  7559.   else if (fixp->fx_pcrel == 0)
  7560.     reloc->addend = fixp->fx_addnumber;
  7561.   else
  7562.     {
  7563.       if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
  7564.     /* A gruesome hack which is a result of the gruesome gas reloc
  7565.        handling.  */
  7566.     reloc->addend = reloc->address;
  7567.       else
  7568.     reloc->addend = -reloc->address;
  7569.     }
  7570.  
  7571.   /* If this is a variant frag, we may need to adjust the existing
  7572.      reloc and generate a new one.  */
  7573.   if (fixp->fx_frag->fr_opcode != NULL
  7574.       && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
  7575.       || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
  7576.       || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
  7577.       || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
  7578.       || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
  7579.       || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
  7580.       || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
  7581.     {
  7582.       arelent *reloc2;
  7583.  
  7584.       /* If this is not the last reloc in this frag, then we have two
  7585.      GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
  7586.      CALL_HI16/CALL_LO16, both of which are being replaced.  Let
  7587.      the second one handle all of them.  */
  7588.       if (fixp->fx_next != NULL
  7589.       && fixp->fx_frag == fixp->fx_next->fx_frag)
  7590.     {
  7591.       assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
  7592.            && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
  7593.           || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
  7594.               && (fixp->fx_next->fx_r_type
  7595.               == BFD_RELOC_MIPS_GOT_LO16))
  7596.           || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
  7597.               && (fixp->fx_next->fx_r_type
  7598.               == BFD_RELOC_MIPS_CALL_LO16)));
  7599.       retval[0] = NULL;
  7600.       return retval;
  7601.     }
  7602.  
  7603.       fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
  7604.       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  7605.       reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
  7606.       retval[2] = NULL;
  7607.       reloc2->sym_ptr_ptr = &fixp->fx_addsy->bsym;
  7608.       reloc2->address = (reloc->address
  7609.              + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
  7610.                 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
  7611.       reloc2->addend = fixp->fx_addnumber;
  7612.       reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
  7613.       assert (reloc2->howto != NULL);
  7614.  
  7615.       if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
  7616.     {
  7617.       arelent *reloc3;
  7618.  
  7619.       reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
  7620.       retval[3] = NULL;
  7621.       *reloc3 = *reloc2;
  7622.       reloc3->address += 4;
  7623.     }
  7624.  
  7625.       if (mips_pic == NO_PIC)
  7626.     {
  7627.       assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
  7628.       fixp->fx_r_type = BFD_RELOC_HI16_S;
  7629.     }
  7630.       else if (mips_pic == SVR4_PIC)
  7631.     {
  7632.       switch (fixp->fx_r_type)
  7633.         {
  7634.         default:
  7635.           abort ();
  7636.         case BFD_RELOC_MIPS_GOT16:
  7637.           break;
  7638.         case BFD_RELOC_MIPS_CALL16:
  7639.         case BFD_RELOC_MIPS_GOT_LO16:
  7640.         case BFD_RELOC_MIPS_CALL_LO16:
  7641.           fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
  7642.           break;
  7643.         }
  7644.     }
  7645.       else
  7646.     abort ();
  7647.     }
  7648.  
  7649.   /* To support a PC relative reloc when generating embedded PIC code
  7650.      for ECOFF, we use a Cygnus extension.  We check for that here to
  7651.      make sure that we don't let such a reloc escape normally.  */
  7652.   if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour
  7653.       && fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
  7654.       && mips_pic != EMBEDDED_PIC)
  7655.     reloc->howto = NULL;
  7656.   else
  7657.     reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  7658.  
  7659.   if (reloc->howto == NULL)
  7660.     {
  7661.       as_bad_where (fixp->fx_file, fixp->fx_line,
  7662.             "Can not represent relocation in this object file format");
  7663.       retval[0] = NULL;
  7664.     }
  7665.  
  7666.   return retval;
  7667. }
  7668.  
  7669. /* Convert a machine dependent frag.  */
  7670.  
  7671. void
  7672. md_convert_frag (abfd, asec, fragp)
  7673.      bfd *abfd;
  7674.      segT asec;
  7675.      fragS *fragp;
  7676. {
  7677.   int old, new;
  7678.   char *fixptr;
  7679.  
  7680.   if (fragp->fr_opcode == NULL)
  7681.     return;
  7682.  
  7683.   old = RELAX_OLD (fragp->fr_subtype);
  7684.   new = RELAX_NEW (fragp->fr_subtype);
  7685.   fixptr = fragp->fr_literal + fragp->fr_fix;
  7686.  
  7687.   if (new > 0)
  7688.     memcpy (fixptr - old, fixptr, new);
  7689.  
  7690.   fragp->fr_fix += new - old;
  7691. }
  7692.  
  7693. /* This function is called whenever a label is defined.  It is used
  7694.    when handling branch delays; if a branch has a label, we assume we
  7695.    can not move it.  */
  7696.  
  7697. void
  7698. mips_define_label (sym)
  7699.      symbolS *sym;
  7700. {
  7701.   insn_label = sym;
  7702. }
  7703.  
  7704. /* Decide whether a label is local.  This is called by LOCAL_LABEL.
  7705.    In order to work with gcc when using mips-tfile, we must keep all
  7706.    local labels.  However, in other cases, we want to discard them,
  7707.    since they are useless.  */
  7708.  
  7709. int
  7710. mips_local_label (name)
  7711.      const char *name;
  7712. {
  7713. #ifndef NO_ECOFF_DEBUGGING
  7714.   if (ECOFF_DEBUGGING
  7715.       && mips_debug != 0
  7716.       && ! ecoff_debugging_seen)
  7717.     {
  7718.       /* We were called with -g, but we didn't see any debugging
  7719.          information.  That may mean that gcc is smuggling debugging
  7720.          information through to mips-tfile, in which case we must
  7721.          generate all local labels.  */
  7722.       return 0;
  7723.     }
  7724. #endif
  7725.  
  7726.   /* Here it's OK to discard local labels.  */
  7727.  
  7728.   return name[0] == '$';
  7729. }
  7730.  
  7731. #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
  7732.  
  7733. /* Some special processing for a MIPS ELF file.  */
  7734.  
  7735. void
  7736. mips_elf_final_processing ()
  7737. {
  7738.   /* Write out the register information.  */
  7739.   if (! mips_64)
  7740.     {
  7741.       Elf32_RegInfo s;
  7742.  
  7743.       s.ri_gprmask = mips_gprmask;
  7744.       s.ri_cprmask[0] = mips_cprmask[0];
  7745.       s.ri_cprmask[1] = mips_cprmask[1];
  7746.       s.ri_cprmask[2] = mips_cprmask[2];
  7747.       s.ri_cprmask[3] = mips_cprmask[3];
  7748.       /* The gp_value field is set by the MIPS ELF backend.  */
  7749.  
  7750.       bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
  7751.                        ((Elf32_External_RegInfo *)
  7752.                     mips_regmask_frag));
  7753.     }
  7754.   else
  7755.     {
  7756.       Elf64_Internal_RegInfo s;
  7757.  
  7758.       s.ri_gprmask = mips_gprmask;
  7759.       s.ri_pad = 0;
  7760.       s.ri_cprmask[0] = mips_cprmask[0];
  7761.       s.ri_cprmask[1] = mips_cprmask[1];
  7762.       s.ri_cprmask[2] = mips_cprmask[2];
  7763.       s.ri_cprmask[3] = mips_cprmask[3];
  7764.       /* The gp_value field is set by the MIPS ELF backend.  */
  7765.  
  7766.       bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
  7767.                        ((Elf64_External_RegInfo *)
  7768.                     mips_regmask_frag));
  7769.     }
  7770.  
  7771.   /* Set the MIPS ELF flag bits.  FIXME: There should probably be some
  7772.      sort of BFD interface for this.  */
  7773.   if (mips_any_noreorder)
  7774.     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
  7775.   if (mips_pic != NO_PIC)
  7776.     elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
  7777. }
  7778.  
  7779. #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
  7780.  
  7781. /* These functions should really be defined by the object file format,
  7782.    since they are related to debugging information.  However, this
  7783.    code has to work for the a.out format, which does not define them,
  7784.    so we provide simple versions here.  These don't actually generate
  7785.    any debugging information, but they do simple checking and someday
  7786.    somebody may make them useful.  */
  7787.  
  7788. typedef struct loc
  7789. {
  7790.   struct loc *loc_next;
  7791.   unsigned long loc_fileno;
  7792.   unsigned long loc_lineno;
  7793.   unsigned long loc_offset;
  7794.   unsigned short loc_delta;
  7795.   unsigned short loc_count;
  7796. #if 0
  7797.   fragS *loc_frag;
  7798. #endif
  7799. }
  7800. locS;
  7801.  
  7802. typedef struct proc
  7803.   {
  7804.     struct proc *proc_next;
  7805.     struct symbol *proc_isym;
  7806.     struct symbol *proc_end;
  7807.     unsigned long proc_reg_mask;
  7808.     unsigned long proc_reg_offset;
  7809.     unsigned long proc_fpreg_mask;
  7810.     unsigned long proc_fpreg_offset;
  7811.     unsigned long proc_frameoffset;
  7812.     unsigned long proc_framereg;
  7813.     unsigned long proc_pcreg;
  7814.     locS *proc_iline;
  7815.     struct file *proc_file;
  7816.     int proc_index;
  7817.   }
  7818. procS;
  7819.  
  7820. typedef struct file
  7821.   {
  7822.     struct file *file_next;
  7823.     unsigned long file_fileno;
  7824.     struct symbol *file_symbol;
  7825.     struct symbol *file_end;
  7826.     struct proc *file_proc;
  7827.     int file_numprocs;
  7828.   }
  7829. fileS;
  7830.  
  7831. static struct obstack proc_frags;
  7832. static procS *proc_lastP;
  7833. static procS *proc_rootP;
  7834. static int numprocs;
  7835.  
  7836. static void
  7837. md_obj_begin ()
  7838. {
  7839.   obstack_begin (&proc_frags, 0x2000);
  7840. }
  7841.  
  7842. static void
  7843. md_obj_end ()
  7844. {
  7845.   /* check for premature end, nesting errors, etc */
  7846.   if (proc_lastP && proc_lastP->proc_end == NULL)
  7847.     as_warn ("missing `.end' at end of assembly");
  7848. }
  7849.  
  7850. static long
  7851. get_number ()
  7852. {
  7853.   int negative = 0;
  7854.   long val = 0;
  7855.  
  7856.   if (*input_line_pointer == '-')
  7857.     {
  7858.       ++input_line_pointer;
  7859.       negative = 1;
  7860.     }
  7861.   if (!isdigit (*input_line_pointer))
  7862.     as_bad ("Expected simple number.");
  7863.   if (input_line_pointer[0] == '0')
  7864.     {
  7865.       if (input_line_pointer[1] == 'x')
  7866.     {
  7867.       input_line_pointer += 2;
  7868.       while (isxdigit (*input_line_pointer))
  7869.         {
  7870.           val <<= 4;
  7871.           val |= hex_value (*input_line_pointer++);
  7872.         }
  7873.       return negative ? -val : val;
  7874.     }
  7875.       else
  7876.     {
  7877.       ++input_line_pointer;
  7878.       while (isdigit (*input_line_pointer))
  7879.         {
  7880.           val <<= 3;
  7881.           val |= *input_line_pointer++ - '0';
  7882.         }
  7883.       return negative ? -val : val;
  7884.     }
  7885.     }
  7886.   if (!isdigit (*input_line_pointer))
  7887.     {
  7888.       printf (" *input_line_pointer == '%c' 0x%02x\n",
  7889.           *input_line_pointer, *input_line_pointer);
  7890.       as_warn ("Invalid number");
  7891.       return -1;
  7892.     }
  7893.   while (isdigit (*input_line_pointer))
  7894.     {
  7895.       val *= 10;
  7896.       val += *input_line_pointer++ - '0';
  7897.     }
  7898.   return negative ? -val : val;
  7899. }
  7900.  
  7901. /* The .file directive; just like the usual .file directive, but there
  7902.    is an initial number which is the ECOFF file index.  */
  7903.  
  7904. static void
  7905. s_file (x)
  7906.      int x;
  7907. {
  7908.   int line;
  7909.  
  7910.   line = get_number ();
  7911.   s_app_file (0);
  7912. }
  7913.  
  7914.  
  7915. /* The .end directive.  */
  7916.  
  7917. static void
  7918. s_mipsend (x)
  7919.      int x;
  7920. {
  7921.   symbolS *p;
  7922.  
  7923.   if (!is_end_of_line[(unsigned char) *input_line_pointer])
  7924.     {
  7925.       p = get_symbol ();
  7926.       demand_empty_rest_of_line ();
  7927.     }
  7928.   else
  7929.     p = NULL;
  7930.   if (now_seg != text_section)
  7931.     as_warn (".end not in text section");
  7932.   if (!proc_lastP)
  7933.     {
  7934.       as_warn (".end and no .ent seen yet.");
  7935.       return;
  7936.     }
  7937.  
  7938.   if (p != NULL)
  7939.     {
  7940.       assert (S_GET_NAME (p));
  7941.       if (strcmp (S_GET_NAME (p), S_GET_NAME (proc_lastP->proc_isym)))
  7942.     as_warn (".end symbol does not match .ent symbol.");
  7943.     }
  7944.  
  7945.   proc_lastP->proc_end = (symbolS *) 1;
  7946. }
  7947.  
  7948. /* The .aent and .ent directives.  */
  7949.  
  7950. static void
  7951. s_ent (aent)
  7952.      int aent;
  7953. {
  7954.   int number = 0;
  7955.   procS *procP;
  7956.   symbolS *symbolP;
  7957.  
  7958.   symbolP = get_symbol ();
  7959.   if (*input_line_pointer == ',')
  7960.     input_line_pointer++;
  7961.   SKIP_WHITESPACE ();
  7962.   if (isdigit (*input_line_pointer) || *input_line_pointer == '-')
  7963.     number = get_number ();
  7964.   if (now_seg != text_section)
  7965.     as_warn (".ent or .aent not in text section.");
  7966.  
  7967.   if (!aent && proc_lastP && proc_lastP->proc_end == NULL)
  7968.     as_warn ("missing `.end'");
  7969.  
  7970.   if (!aent)
  7971.     {
  7972.       procP = (procS *) obstack_alloc (&proc_frags, sizeof (*procP));
  7973.       procP->proc_isym = symbolP;
  7974.       procP->proc_reg_mask = 0;
  7975.       procP->proc_reg_offset = 0;
  7976.       procP->proc_fpreg_mask = 0;
  7977.       procP->proc_fpreg_offset = 0;
  7978.       procP->proc_frameoffset = 0;
  7979.       procP->proc_framereg = 0;
  7980.       procP->proc_pcreg = 0;
  7981.       procP->proc_end = NULL;
  7982.       procP->proc_next = NULL;
  7983.       if (proc_lastP)
  7984.     proc_lastP->proc_next = procP;
  7985.       else
  7986.     proc_rootP = procP;
  7987.       proc_lastP = procP;
  7988.       numprocs++;
  7989.     }
  7990.   demand_empty_rest_of_line ();
  7991. }
  7992.  
  7993. /* The .frame directive.  */
  7994.  
  7995. #if 0
  7996. static void
  7997. s_frame (x)
  7998.      int x;
  7999. {
  8000.   char str[100];
  8001.   symbolS *symP;
  8002.   int frame_reg;
  8003.   int frame_off;
  8004.   int pcreg;
  8005.  
  8006.   frame_reg = tc_get_register (1);
  8007.   if (*input_line_pointer == ',')
  8008.     input_line_pointer++;
  8009.   frame_off = get_absolute_expression ();
  8010.   if (*input_line_pointer == ',')
  8011.     input_line_pointer++;
  8012.   pcreg = tc_get_register (0);
  8013.  
  8014.   /* bob third eye */
  8015.   assert (proc_rootP);
  8016.   proc_rootP->proc_framereg = frame_reg;
  8017.   proc_rootP->proc_frameoffset = frame_off;
  8018.   proc_rootP->proc_pcreg = pcreg;
  8019.   /* bob macho .frame */
  8020.  
  8021.   /* We don't have to write out a frame stab for unoptimized code. */
  8022.   if (!(frame_reg == FP && frame_off == 0))
  8023.     {
  8024.       if (!proc_lastP)
  8025.     as_warn ("No .ent for .frame to use.");
  8026.       (void) sprintf (str, "R%d;%d", frame_reg, frame_off);
  8027.       symP = symbol_new (str, N_VFP, 0, frag_now);
  8028.       S_SET_TYPE (symP, N_RMASK);
  8029.       S_SET_OTHER (symP, 0);
  8030.       S_SET_DESC (symP, 0);
  8031.       symP->sy_forward = proc_lastP->proc_isym;
  8032.       /* bob perhaps I should have used pseudo set */
  8033.     }
  8034.   demand_empty_rest_of_line ();
  8035. }
  8036. #endif
  8037.  
  8038. /* The .fmask and .mask directives.  */
  8039.  
  8040. #if 0
  8041. static void
  8042. s_mask (reg_type)
  8043.      char reg_type;
  8044. {
  8045.   char str[100], *strP;
  8046.   symbolS *symP;
  8047.   int i;
  8048.   unsigned int mask;
  8049.   int off;
  8050.  
  8051.   mask = get_number ();
  8052.   if (*input_line_pointer == ',')
  8053.     input_line_pointer++;
  8054.   off = get_absolute_expression ();
  8055.  
  8056.   /* bob only for coff */
  8057.   assert (proc_rootP);
  8058.   if (reg_type == 'F')
  8059.     {
  8060.       proc_rootP->proc_fpreg_mask = mask;
  8061.       proc_rootP->proc_fpreg_offset = off;
  8062.     }
  8063.   else
  8064.     {
  8065.       proc_rootP->proc_reg_mask = mask;
  8066.       proc_rootP->proc_reg_offset = off;
  8067.     }
  8068.  
  8069.   /* bob macho .mask + .fmask */
  8070.  
  8071.   /* We don't have to write out a mask stab if no saved regs. */
  8072.   if (!(mask == 0))
  8073.     {
  8074.       if (!proc_lastP)
  8075.     as_warn ("No .ent for .mask to use.");
  8076.       strP = str;
  8077.       for (i = 0; i < 32; i++)
  8078.     {
  8079.       if (mask % 2)
  8080.         {
  8081.           sprintf (strP, "%c%d,", reg_type, i);
  8082.           strP += strlen (strP);
  8083.         }
  8084.       mask /= 2;
  8085.     }
  8086.       sprintf (strP, ";%d,", off);
  8087.       symP = symbol_new (str, N_RMASK, 0, frag_now);
  8088.       S_SET_TYPE (symP, N_RMASK);
  8089.       S_SET_OTHER (symP, 0);
  8090.       S_SET_DESC (symP, 0);
  8091.       symP->sy_forward = proc_lastP->proc_isym;
  8092.       /* bob perhaps I should have used pseudo set */
  8093.     }
  8094. }
  8095. #endif
  8096.  
  8097. /* The .loc directive.  */
  8098.  
  8099. #if 0
  8100. static void
  8101. s_loc (x)
  8102.      int x;
  8103. {
  8104.   symbolS *symbolP;
  8105.   int lineno;
  8106.   int addroff;
  8107.  
  8108.   assert (now_seg == text_section);
  8109.  
  8110.   lineno = get_number ();
  8111.   addroff = frag_now_fix ();
  8112.  
  8113.   symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
  8114.   S_SET_TYPE (symbolP, N_SLINE);
  8115.   S_SET_OTHER (symbolP, 0);
  8116.   S_SET_DESC (symbolP, lineno);
  8117.   symbolP->sy_segment = now_seg;
  8118. }
  8119. #endif
  8120.