Back to index

cell-binutils  2.17cvs20070401
tc-mn10300.c
Go to the documentation of this file.
00001 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
00002    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00003    2006, 2007 Free Software Foundation, Inc.
00004 
00005    This file is part of GAS, the GNU Assembler.
00006 
00007    GAS is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    GAS is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with GAS; see the file COPYING.  If not, write to
00019    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
00020    Boston, MA 02110-1301, USA.  */
00021 
00022 #include "as.h"
00023 #include "safe-ctype.h"
00024 #include "subsegs.h"
00025 #include "opcode/mn10300.h"
00026 #include "dwarf2dbg.h"
00027 
00028 /* Structure to hold information about predefined registers.  */
00029 struct reg_name
00030 {
00031   const char *name;
00032   int value;
00033 };
00034 
00035 /* Generic assembler global variables which must be defined by all
00036    targets.  */
00037 
00038 /* Characters which always start a comment.  */
00039 const char comment_chars[] = "#";
00040 
00041 /* Characters which start a comment at the beginning of a line.  */
00042 const char line_comment_chars[] = ";#";
00043 
00044 /* Characters which may be used to separate multiple commands on a
00045    single line.  */
00046 const char line_separator_chars[] = ";";
00047 
00048 /* Characters which are used to indicate an exponent in a floating
00049    point number.  */
00050 const char EXP_CHARS[] = "eE";
00051 
00052 /* Characters which mean that a number is a floating point constant,
00053    as in 0d1.0.  */
00054 const char FLT_CHARS[] = "dD";
00055 
00056 const relax_typeS md_relax_table[] =
00057 {
00058   /* The plus values for the bCC and fBCC instructions in the table below
00059      are because the branch instruction is translated into a jump
00060      instruction that is now +2 or +3 bytes further on in memory, and the
00061      correct size of jump instruction must be selected.  */
00062   /* bCC relaxing  */
00063   {0x7f, -0x80, 2, 1},
00064   {0x7fff + 2, -0x8000 + 2, 5, 2},
00065   {0x7fffffff, -0x80000000, 7, 0},
00066 
00067   /* bCC relaxing (uncommon cases for 3byte length instructions)  */
00068   {0x7f, -0x80, 3, 4},
00069   {0x7fff + 3, -0x8000 + 3, 6, 5},
00070   {0x7fffffff, -0x80000000, 8, 0},
00071 
00072   /* call relaxing  */
00073   {0x7fff, -0x8000, 5, 7},
00074   {0x7fffffff, -0x80000000, 7, 0},
00075 
00076   /* calls relaxing  */
00077   {0x7fff, -0x8000, 4, 9},
00078   {0x7fffffff, -0x80000000, 6, 0},
00079 
00080   /* jmp relaxing  */
00081   {0x7f, -0x80, 2, 11},
00082   {0x7fff, -0x8000, 3, 12},
00083   {0x7fffffff, -0x80000000, 5, 0},
00084 
00085   /* fbCC relaxing  */
00086   {0x7f, -0x80, 3, 14},
00087   {0x7fff + 3, -0x8000 + 3, 6, 15},
00088   {0x7fffffff, -0x80000000, 8, 0},
00089 
00090 };
00091 
00092 /* Local functions.  */
00093 static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
00094                                        const struct mn10300_operand *,
00095                                        offsetT, char *, unsigned,
00096                                        unsigned));
00097 static unsigned long check_operand PARAMS ((unsigned long,
00098                                        const struct mn10300_operand *,
00099                                        offsetT));
00100 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
00101 static bfd_boolean data_register_name PARAMS ((expressionS *expressionP));
00102 static bfd_boolean address_register_name PARAMS ((expressionS *expressionP));
00103 static bfd_boolean other_register_name PARAMS ((expressionS *expressionP));
00104 static bfd_boolean r_register_name PARAMS ((expressionS *expressionP));
00105 static bfd_boolean xr_register_name PARAMS ((expressionS *expressionP));
00106 static void set_arch_mach PARAMS ((int));
00107 
00108 /*  Set linkrelax here to avoid fixups in most sections.  */
00109 int linkrelax = 1;
00110 
00111 static int current_machine;
00112 
00113 /* Fixups.  */
00114 #define MAX_INSN_FIXUPS (5)
00115 struct mn10300_fixup
00116 {
00117   expressionS exp;
00118   int opindex;
00119   bfd_reloc_code_real_type reloc;
00120 };
00121 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
00122 static int fc;
00123 
00124 /* We must store the value of each register operand so that we can
00125    verify that certain registers do not match.  */
00126 int mn10300_reg_operands[MN10300_MAX_OPERANDS];
00127 
00128 const char *md_shortopts = "";
00129 struct option md_longopts[] = {
00130   {NULL, no_argument, NULL, 0}
00131 };
00132 size_t md_longopts_size = sizeof (md_longopts);
00133 
00134 /* The target specific pseudo-ops which we support.  */
00135 const pseudo_typeS md_pseudo_table[] =
00136 {
00137   { "am30",   set_arch_mach,              AM30 },
00138   { "am33",   set_arch_mach,              AM33 },
00139   { "am33_2", (void (*) PARAMS ((int))) set_arch_mach, AM33_2 },
00140   { "mn10300",       set_arch_mach,              MN103 },
00141   {NULL, 0, 0}
00142 };
00143 
00144 #define HAVE_AM33_2 (current_machine == AM33_2)
00145 #define HAVE_AM33 (current_machine == AM33 || HAVE_AM33_2)
00146 #define HAVE_AM30 (current_machine == AM30)
00147 
00148 /* Opcode hash table.  */
00149 static struct hash_control *mn10300_hash;
00150 
00151 /* This table is sorted. Suitable for searching by a binary search.  */
00152 static const struct reg_name data_registers[] =
00153 {
00154   { "d0", 0 },
00155   { "d1", 1 },
00156   { "d2", 2 },
00157   { "d3", 3 },
00158 };
00159 #define DATA_REG_NAME_CNT                        \
00160   (sizeof (data_registers) / sizeof (struct reg_name))
00161 
00162 static const struct reg_name address_registers[] =
00163 {
00164   { "a0", 0 },
00165   { "a1", 1 },
00166   { "a2", 2 },
00167   { "a3", 3 },
00168 };
00169 
00170 #define ADDRESS_REG_NAME_CNT                                   \
00171   (sizeof (address_registers) / sizeof (struct reg_name))
00172 
00173 static const struct reg_name r_registers[] =
00174 {
00175   { "a0", 8 },
00176   { "a1", 9 },
00177   { "a2", 10 },
00178   { "a3", 11 },
00179   { "d0", 12 },
00180   { "d1", 13 },
00181   { "d2", 14 },
00182   { "d3", 15 },
00183   { "e0", 0 },
00184   { "e1", 1 },
00185   { "e10", 10 },
00186   { "e11", 11 },
00187   { "e12", 12 },
00188   { "e13", 13 },
00189   { "e14", 14 },
00190   { "e15", 15 },
00191   { "e2", 2 },
00192   { "e3", 3 },
00193   { "e4", 4 },
00194   { "e5", 5 },
00195   { "e6", 6 },
00196   { "e7", 7 },
00197   { "e8", 8 },
00198   { "e9", 9 },
00199   { "r0", 0 },
00200   { "r1", 1 },
00201   { "r10", 10 },
00202   { "r11", 11 },
00203   { "r12", 12 },
00204   { "r13", 13 },
00205   { "r14", 14 },
00206   { "r15", 15 },
00207   { "r2", 2 },
00208   { "r3", 3 },
00209   { "r4", 4 },
00210   { "r5", 5 },
00211   { "r6", 6 },
00212   { "r7", 7 },
00213   { "r8", 8 },
00214   { "r9", 9 },
00215 };
00216 
00217 #define R_REG_NAME_CNT                                  \
00218   (sizeof (r_registers) / sizeof (struct reg_name))
00219 
00220 static const struct reg_name xr_registers[] =
00221 {
00222   { "mcrh", 2 },
00223   { "mcrl", 3 },
00224   { "mcvf", 4 },
00225   { "mdrq", 1 },
00226   { "sp", 0 },
00227   { "xr0", 0 },
00228   { "xr1", 1 },
00229   { "xr10", 10 },
00230   { "xr11", 11 },
00231   { "xr12", 12 },
00232   { "xr13", 13 },
00233   { "xr14", 14 },
00234   { "xr15", 15 },
00235   { "xr2", 2 },
00236   { "xr3", 3 },
00237   { "xr4", 4 },
00238   { "xr5", 5 },
00239   { "xr6", 6 },
00240   { "xr7", 7 },
00241   { "xr8", 8 },
00242   { "xr9", 9 },
00243 };
00244 
00245 #define XR_REG_NAME_CNT                                 \
00246   (sizeof (xr_registers) / sizeof (struct reg_name))
00247 
00248 /* We abuse the `value' field, that would be otherwise unused, to
00249    encode the architecture on which (access to) the register was
00250    introduced.  FIXME: we should probably warn when we encounter a
00251    register name when assembling for an architecture that doesn't
00252    support it, before parsing it as a symbol name.  */
00253 static const struct reg_name other_registers[] =
00254 {
00255   { "epsw", AM33 },
00256   { "mdr", 0 },
00257   { "pc", AM33 },
00258   { "psw", 0 },
00259   { "sp", 0 },
00260 };
00261 
00262 #define OTHER_REG_NAME_CNT                       \
00263   (sizeof (other_registers) / sizeof (struct reg_name))
00264 
00265 static const struct reg_name float_registers[] =
00266 {
00267   { "fs0", 0 },
00268   { "fs1", 1 },
00269   { "fs10", 10 },
00270   { "fs11", 11 },
00271   { "fs12", 12 },
00272   { "fs13", 13 },
00273   { "fs14", 14 },
00274   { "fs15", 15 },
00275   { "fs16", 16 },
00276   { "fs17", 17 },
00277   { "fs18", 18 },
00278   { "fs19", 19 },
00279   { "fs2",   2 },
00280   { "fs20", 20 },
00281   { "fs21", 21 },
00282   { "fs22", 22 },
00283   { "fs23", 23 },
00284   { "fs24", 24 },
00285   { "fs25", 25 },
00286   { "fs26", 26 },
00287   { "fs27", 27 },
00288   { "fs28", 28 },
00289   { "fs29", 29 },
00290   { "fs3",   3 },
00291   { "fs30", 30 },
00292   { "fs31", 31 },
00293   { "fs4",   4 },
00294   { "fs5",   5 },
00295   { "fs6",   6 },
00296   { "fs7",   7 },
00297   { "fs8",   8 },
00298   { "fs9",   9 },
00299 };
00300 
00301 #define FLOAT_REG_NAME_CNT \
00302   (sizeof (float_registers) / sizeof (struct reg_name))
00303 
00304 static const struct reg_name double_registers[] =
00305 {
00306   { "fd0",   0 },
00307   { "fd10", 10 },
00308   { "fd12", 12 },
00309   { "fd14", 14 },
00310   { "fd16", 16 },
00311   { "fd18", 18 },
00312   { "fd2",   2 },
00313   { "fd20", 20 },
00314   { "fd22", 22 },
00315   { "fd24", 24 },
00316   { "fd26", 26 },
00317   { "fd28", 28 },
00318   { "fd30", 30 },
00319   { "fd4",   4 },
00320   { "fd6",   6 },
00321   { "fd8",   8 },
00322 };
00323 
00324 #define DOUBLE_REG_NAME_CNT \
00325   (sizeof (double_registers) / sizeof (struct reg_name))
00326 
00327 
00328 /* reg_name_search does a binary search of the given register table
00329    to see if "name" is a valid regiter name.  Returns the register
00330    number from the array on success, or -1 on failure.  */
00331 
00332 static int
00333 reg_name_search (regs, regcount, name)
00334      const struct reg_name *regs;
00335      int regcount;
00336      const char *name;
00337 {
00338   int middle, low, high;
00339   int cmp;
00340 
00341   low = 0;
00342   high = regcount - 1;
00343 
00344   do
00345     {
00346       middle = (low + high) / 2;
00347       cmp = strcasecmp (name, regs[middle].name);
00348       if (cmp < 0)
00349        high = middle - 1;
00350       else if (cmp > 0)
00351        low = middle + 1;
00352       else
00353        return regs[middle].value;
00354     }
00355   while (low <= high);
00356   return -1;
00357 }
00358 
00359 /* Summary of register_name().
00360  *
00361  * in: Input_line_pointer points to 1st char of operand.
00362  *
00363  * out: An expressionS.
00364  *     The operand may have been a register: in this case, X_op == O_register,
00365  *     X_add_number is set to the register number, and truth is returned.
00366  *     Input_line_pointer->(next non-blank) char after operand, or is in
00367  *     its original state.
00368  */
00369 
00370 static bfd_boolean
00371 r_register_name (expressionP)
00372      expressionS *expressionP;
00373 {
00374   int reg_number;
00375   char *name;
00376   char *start;
00377   char c;
00378 
00379   /* Find the spelling of the operand.  */
00380   start = name = input_line_pointer;
00381 
00382   c = get_symbol_end ();
00383   reg_number = reg_name_search (r_registers, R_REG_NAME_CNT, name);
00384 
00385   /* Put back the delimiting char.  */
00386   *input_line_pointer = c;
00387 
00388   /* Look to see if it's in the register table.  */
00389   if (reg_number >= 0)
00390     {
00391       expressionP->X_op = O_register;
00392       expressionP->X_add_number = reg_number;
00393 
00394       /* Make the rest nice.  */
00395       expressionP->X_add_symbol = NULL;
00396       expressionP->X_op_symbol = NULL;
00397 
00398       return TRUE;
00399     }
00400 
00401   /* Reset the line as if we had not done anything.  */
00402   input_line_pointer = start;
00403   return FALSE;
00404 }
00405 
00406 /* Summary of register_name().
00407  *
00408  * in: Input_line_pointer points to 1st char of operand.
00409  *
00410  * out: An expressionS.
00411  *     The operand may have been a register: in this case, X_op == O_register,
00412  *     X_add_number is set to the register number, and truth is returned.
00413  *     Input_line_pointer->(next non-blank) char after operand, or is in
00414  *     its original state.
00415  */
00416 
00417 static bfd_boolean
00418 xr_register_name (expressionP)
00419      expressionS *expressionP;
00420 {
00421   int reg_number;
00422   char *name;
00423   char *start;
00424   char c;
00425 
00426   /* Find the spelling of the operand.  */
00427   start = name = input_line_pointer;
00428 
00429   c = get_symbol_end ();
00430   reg_number = reg_name_search (xr_registers, XR_REG_NAME_CNT, name);
00431 
00432   /* Put back the delimiting char.  */
00433   *input_line_pointer = c;
00434 
00435   /* Look to see if it's in the register table.  */
00436   if (reg_number >= 0)
00437     {
00438       expressionP->X_op = O_register;
00439       expressionP->X_add_number = reg_number;
00440 
00441       /* Make the rest nice.  */
00442       expressionP->X_add_symbol = NULL;
00443       expressionP->X_op_symbol = NULL;
00444 
00445       return TRUE;
00446     }
00447 
00448   /* Reset the line as if we had not done anything.  */
00449   input_line_pointer = start;
00450   return FALSE;
00451 }
00452 
00453 /* Summary of register_name().
00454  *
00455  * in: Input_line_pointer points to 1st char of operand.
00456  *
00457  * out: An expressionS.
00458  *     The operand may have been a register: in this case, X_op == O_register,
00459  *     X_add_number is set to the register number, and truth is returned.
00460  *     Input_line_pointer->(next non-blank) char after operand, or is in
00461  *     its original state.
00462  */
00463 
00464 static bfd_boolean
00465 data_register_name (expressionP)
00466      expressionS *expressionP;
00467 {
00468   int reg_number;
00469   char *name;
00470   char *start;
00471   char c;
00472 
00473   /* Find the spelling of the operand.  */
00474   start = name = input_line_pointer;
00475 
00476   c = get_symbol_end ();
00477   reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
00478 
00479   /* Put back the delimiting char.  */
00480   *input_line_pointer = c;
00481 
00482   /* Look to see if it's in the register table.  */
00483   if (reg_number >= 0)
00484     {
00485       expressionP->X_op = O_register;
00486       expressionP->X_add_number = reg_number;
00487 
00488       /* Make the rest nice.  */
00489       expressionP->X_add_symbol = NULL;
00490       expressionP->X_op_symbol = NULL;
00491 
00492       return TRUE;
00493     }
00494 
00495   /* Reset the line as if we had not done anything.  */
00496   input_line_pointer = start;
00497   return FALSE;
00498 }
00499 
00500 /* Summary of register_name().
00501  *
00502  * in: Input_line_pointer points to 1st char of operand.
00503  *
00504  * out: An expressionS.
00505  *     The operand may have been a register: in this case, X_op == O_register,
00506  *     X_add_number is set to the register number, and truth is returned.
00507  *     Input_line_pointer->(next non-blank) char after operand, or is in
00508  *     its original state.
00509  */
00510 
00511 static bfd_boolean
00512 address_register_name (expressionP)
00513      expressionS *expressionP;
00514 {
00515   int reg_number;
00516   char *name;
00517   char *start;
00518   char c;
00519 
00520   /* Find the spelling of the operand.  */
00521   start = name = input_line_pointer;
00522 
00523   c = get_symbol_end ();
00524   reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
00525 
00526   /* Put back the delimiting char.  */
00527   *input_line_pointer = c;
00528 
00529   /* Look to see if it's in the register table.  */
00530   if (reg_number >= 0)
00531     {
00532       expressionP->X_op = O_register;
00533       expressionP->X_add_number = reg_number;
00534 
00535       /* Make the rest nice.  */
00536       expressionP->X_add_symbol = NULL;
00537       expressionP->X_op_symbol = NULL;
00538 
00539       return TRUE;
00540     }
00541 
00542   /* Reset the line as if we had not done anything.  */
00543   input_line_pointer = start;
00544   return FALSE;
00545 }
00546 
00547 /* Summary of register_name().
00548  *
00549  * in: Input_line_pointer points to 1st char of operand.
00550  *
00551  * out: An expressionS.
00552  *     The operand may have been a register: in this case, X_op == O_register,
00553  *     X_add_number is set to the register number, and truth is returned.
00554  *     Input_line_pointer->(next non-blank) char after operand, or is in
00555  *     its original state.
00556  */
00557 
00558 static bfd_boolean
00559 other_register_name (expressionP)
00560      expressionS *expressionP;
00561 {
00562   int reg_number;
00563   char *name;
00564   char *start;
00565   char c;
00566 
00567   /* Find the spelling of the operand.  */
00568   start = name = input_line_pointer;
00569 
00570   c = get_symbol_end ();
00571   reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
00572 
00573   /* Put back the delimiting char.  */
00574   *input_line_pointer = c;
00575 
00576   /* Look to see if it's in the register table.  */
00577   if (reg_number == 0
00578       || (reg_number == AM33 && HAVE_AM33))
00579     {
00580       expressionP->X_op = O_register;
00581       expressionP->X_add_number = 0;
00582 
00583       /* Make the rest nice.  */
00584       expressionP->X_add_symbol = NULL;
00585       expressionP->X_op_symbol = NULL;
00586 
00587       return TRUE;
00588     }
00589 
00590   /* Reset the line as if we had not done anything.  */
00591   input_line_pointer = start;
00592   return FALSE;
00593 }
00594 
00595 static bfd_boolean double_register_name PARAMS ((expressionS *));
00596 static bfd_boolean float_register_name  PARAMS ((expressionS *));
00597 
00598 /* Summary of float_register_name:
00599 
00600    in: Input_line_pointer points to 1st char of operand.
00601 
00602    out: A expressionS.
00603        The operand may have been a register: in this case, X_op == O_register,
00604        X_add_number is set to the register number, and truth is returned.
00605        Input_line_pointer->(next non-blank) char after operand, or is in
00606        its original state.  */
00607 
00608 static bfd_boolean
00609 float_register_name (expressionP)
00610      expressionS *expressionP;
00611 {
00612   int reg_number;
00613   char *name;
00614   char *start;
00615   char c;
00616 
00617   /* Find the spelling of the operand.  */
00618   start = name = input_line_pointer;
00619 
00620   c = get_symbol_end ();
00621   reg_number = reg_name_search (float_registers, FLOAT_REG_NAME_CNT, name);
00622 
00623   /* Put back the delimiting char.  */
00624   * input_line_pointer = c;
00625 
00626   /* Look to see if it's in the register table.  */
00627   if (reg_number >= 0)
00628     {
00629       expressionP->X_op = O_register;
00630       expressionP->X_add_number = reg_number;
00631 
00632       /* Make the rest nice.  */
00633       expressionP->X_add_symbol = NULL;
00634       expressionP->X_op_symbol = NULL;
00635 
00636       return TRUE;
00637     }
00638 
00639   /* Reset the line as if we had not done anything.  */
00640   input_line_pointer = start;
00641   return FALSE;
00642 }
00643 
00644 /* Summary of double_register_name:
00645 
00646    in: Input_line_pointer points to 1st char of operand.
00647 
00648    out: A expressionS.
00649        The operand may have been a register: in this case, X_op == O_register,
00650        X_add_number is set to the register number, and truth is returned.
00651        Input_line_pointer->(next non-blank) char after operand, or is in
00652        its original state.  */
00653 
00654 static bfd_boolean
00655 double_register_name (expressionP)
00656      expressionS *expressionP;
00657 {
00658   int reg_number;
00659   char *name;
00660   char *start;
00661   char c;
00662 
00663   /* Find the spelling of the operand.  */
00664   start = name = input_line_pointer;
00665 
00666   c = get_symbol_end ();
00667   reg_number = reg_name_search (double_registers, DOUBLE_REG_NAME_CNT, name);
00668 
00669   /* Put back the delimiting char.  */
00670   * input_line_pointer = c;
00671 
00672   /* Look to see if it's in the register table.  */
00673   if (reg_number >= 0)
00674     {
00675       expressionP->X_op = O_register;
00676       expressionP->X_add_number = reg_number;
00677 
00678       /* Make the rest nice.  */
00679       expressionP->X_add_symbol = NULL;
00680       expressionP->X_op_symbol = NULL;
00681 
00682       return TRUE;
00683     }
00684 
00685   /* Reset the line as if we had not done anything.  */
00686   input_line_pointer = start;
00687   return FALSE;
00688 }
00689 
00690 void
00691 md_show_usage (stream)
00692      FILE *stream;
00693 {
00694   fprintf (stream, _("MN10300 assembler options:\n\
00695 none yet\n"));
00696 }
00697 
00698 int
00699 md_parse_option (c, arg)
00700      int c ATTRIBUTE_UNUSED;
00701      char *arg ATTRIBUTE_UNUSED;
00702 {
00703   return 0;
00704 }
00705 
00706 symbolS *
00707 md_undefined_symbol (name)
00708      char *name ATTRIBUTE_UNUSED;
00709 {
00710   return 0;
00711 }
00712 
00713 char *
00714 md_atof (type, litp, sizep)
00715      int type;
00716      char *litp;
00717      int *sizep;
00718 {
00719   int prec;
00720   LITTLENUM_TYPE words[4];
00721   char *t;
00722   int i;
00723 
00724   switch (type)
00725     {
00726     case 'f':
00727       prec = 2;
00728       break;
00729 
00730     case 'd':
00731       prec = 4;
00732       break;
00733 
00734     default:
00735       *sizep = 0;
00736       return "bad call to md_atof";
00737     }
00738 
00739   t = atof_ieee (input_line_pointer, type, words);
00740   if (t)
00741     input_line_pointer = t;
00742 
00743   *sizep = prec * 2;
00744 
00745   for (i = prec - 1; i >= 0; i--)
00746     {
00747       md_number_to_chars (litp, (valueT) words[i], 2);
00748       litp += 2;
00749     }
00750 
00751   return NULL;
00752 }
00753 
00754 void
00755 md_convert_frag (abfd, sec, fragP)
00756      bfd *abfd ATTRIBUTE_UNUSED;
00757      asection *sec;
00758      fragS *fragP;
00759 {
00760   static unsigned long label_count = 0;
00761   char buf[40];
00762 
00763   subseg_change (sec, 0);
00764   if (fragP->fr_subtype == 0)
00765     {
00766       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
00767               fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
00768       fragP->fr_var = 0;
00769       fragP->fr_fix += 2;
00770     }
00771   else if (fragP->fr_subtype == 1)
00772     {
00773       /* Reverse the condition of the first branch.  */
00774       int offset = fragP->fr_fix;
00775       int opcode = fragP->fr_literal[offset] & 0xff;
00776 
00777       switch (opcode)
00778        {
00779        case 0xc8:
00780          opcode = 0xc9;
00781          break;
00782        case 0xc9:
00783          opcode = 0xc8;
00784          break;
00785        case 0xc0:
00786          opcode = 0xc2;
00787          break;
00788        case 0xc2:
00789          opcode = 0xc0;
00790          break;
00791        case 0xc3:
00792          opcode = 0xc1;
00793          break;
00794        case 0xc1:
00795          opcode = 0xc3;
00796          break;
00797        case 0xc4:
00798          opcode = 0xc6;
00799          break;
00800        case 0xc6:
00801          opcode = 0xc4;
00802          break;
00803        case 0xc7:
00804          opcode = 0xc5;
00805          break;
00806        case 0xc5:
00807          opcode = 0xc7;
00808          break;
00809        default:
00810          abort ();
00811        }
00812       fragP->fr_literal[offset] = opcode;
00813 
00814       /* Create a fixup for the reversed conditional branch.  */
00815       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
00816       fix_new (fragP, fragP->fr_fix + 1, 1,
00817               symbol_new (buf, sec, 0, fragP->fr_next),
00818               fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
00819 
00820       /* Now create the unconditional branch + fixup to the
00821         final target.  */
00822       fragP->fr_literal[offset + 2] = 0xcc;
00823       fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
00824               fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
00825       fragP->fr_var = 0;
00826       fragP->fr_fix += 5;
00827     }
00828   else if (fragP->fr_subtype == 2)
00829     {
00830       /* Reverse the condition of the first branch.  */
00831       int offset = fragP->fr_fix;
00832       int opcode = fragP->fr_literal[offset] & 0xff;
00833 
00834       switch (opcode)
00835        {
00836        case 0xc8:
00837          opcode = 0xc9;
00838          break;
00839        case 0xc9:
00840          opcode = 0xc8;
00841          break;
00842        case 0xc0:
00843          opcode = 0xc2;
00844          break;
00845        case 0xc2:
00846          opcode = 0xc0;
00847          break;
00848        case 0xc3:
00849          opcode = 0xc1;
00850          break;
00851        case 0xc1:
00852          opcode = 0xc3;
00853          break;
00854        case 0xc4:
00855          opcode = 0xc6;
00856          break;
00857        case 0xc6:
00858          opcode = 0xc4;
00859          break;
00860        case 0xc7:
00861          opcode = 0xc5;
00862          break;
00863        case 0xc5:
00864          opcode = 0xc7;
00865          break;
00866        default:
00867          abort ();
00868        }
00869       fragP->fr_literal[offset] = opcode;
00870 
00871       /* Create a fixup for the reversed conditional branch.  */
00872       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
00873       fix_new (fragP, fragP->fr_fix + 1, 1,
00874               symbol_new (buf, sec, 0, fragP->fr_next),
00875               fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
00876 
00877       /* Now create the unconditional branch + fixup to the
00878         final target.  */
00879       fragP->fr_literal[offset + 2] = 0xdc;
00880       fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
00881               fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
00882       fragP->fr_var = 0;
00883       fragP->fr_fix += 7;
00884     }
00885   else if (fragP->fr_subtype == 3)
00886     {
00887       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
00888               fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
00889       fragP->fr_var = 0;
00890       fragP->fr_fix += 3;
00891     }
00892   else if (fragP->fr_subtype == 4)
00893     {
00894       /* Reverse the condition of the first branch.  */
00895       int offset = fragP->fr_fix;
00896       int opcode = fragP->fr_literal[offset + 1] & 0xff;
00897 
00898       switch (opcode)
00899        {
00900        case 0xe8:
00901          opcode = 0xe9;
00902          break;
00903        case 0xe9:
00904          opcode = 0xe8;
00905          break;
00906        case 0xea:
00907          opcode = 0xeb;
00908          break;
00909        case 0xeb:
00910          opcode = 0xea;
00911          break;
00912        default:
00913          abort ();
00914        }
00915       fragP->fr_literal[offset + 1] = opcode;
00916 
00917       /* Create a fixup for the reversed conditional branch.  */
00918       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
00919       fix_new (fragP, fragP->fr_fix + 2, 1,
00920               symbol_new (buf, sec, 0, fragP->fr_next),
00921               fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
00922 
00923       /* Now create the unconditional branch + fixup to the
00924         final target.  */
00925       fragP->fr_literal[offset + 3] = 0xcc;
00926       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
00927               fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
00928       fragP->fr_var = 0;
00929       fragP->fr_fix += 6;
00930     }
00931   else if (fragP->fr_subtype == 5)
00932     {
00933       /* Reverse the condition of the first branch.  */
00934       int offset = fragP->fr_fix;
00935       int opcode = fragP->fr_literal[offset + 1] & 0xff;
00936 
00937       switch (opcode)
00938        {
00939        case 0xe8:
00940          opcode = 0xe9;
00941          break;
00942        case 0xea:
00943          opcode = 0xeb;
00944          break;
00945        case 0xeb:
00946          opcode = 0xea;
00947          break;
00948        default:
00949          abort ();
00950        }
00951       fragP->fr_literal[offset + 1] = opcode;
00952 
00953       /* Create a fixup for the reversed conditional branch.  */
00954       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
00955       fix_new (fragP, fragP->fr_fix + 2, 1,
00956               symbol_new (buf, sec, 0, fragP->fr_next),
00957               fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
00958 
00959       /* Now create the unconditional branch + fixup to the
00960         final target.  */
00961       fragP->fr_literal[offset + 3] = 0xdc;
00962       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
00963               fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
00964       fragP->fr_var = 0;
00965       fragP->fr_fix += 8;
00966     }
00967   else if (fragP->fr_subtype == 6)
00968     {
00969       int offset = fragP->fr_fix;
00970       fragP->fr_literal[offset] = 0xcd;
00971       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
00972               fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
00973       fragP->fr_var = 0;
00974       fragP->fr_fix += 5;
00975     }
00976   else if (fragP->fr_subtype == 7)
00977     {
00978       int offset = fragP->fr_fix;
00979       fragP->fr_literal[offset] = 0xdd;
00980       fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
00981       fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
00982 
00983       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
00984               fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
00985       fragP->fr_var = 0;
00986       fragP->fr_fix += 7;
00987     }
00988   else if (fragP->fr_subtype == 8)
00989     {
00990       int offset = fragP->fr_fix;
00991       fragP->fr_literal[offset] = 0xfa;
00992       fragP->fr_literal[offset + 1] = 0xff;
00993       fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
00994               fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
00995       fragP->fr_var = 0;
00996       fragP->fr_fix += 4;
00997     }
00998   else if (fragP->fr_subtype == 9)
00999     {
01000       int offset = fragP->fr_fix;
01001       fragP->fr_literal[offset] = 0xfc;
01002       fragP->fr_literal[offset + 1] = 0xff;
01003 
01004       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
01005               fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
01006       fragP->fr_var = 0;
01007       fragP->fr_fix += 6;
01008     }
01009   else if (fragP->fr_subtype == 10)
01010     {
01011       fragP->fr_literal[fragP->fr_fix] = 0xca;
01012       fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
01013               fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
01014       fragP->fr_var = 0;
01015       fragP->fr_fix += 2;
01016     }
01017   else if (fragP->fr_subtype == 11)
01018     {
01019       int offset = fragP->fr_fix;
01020       fragP->fr_literal[offset] = 0xcc;
01021 
01022       fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
01023               fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
01024       fragP->fr_var = 0;
01025       fragP->fr_fix += 3;
01026     }
01027   else if (fragP->fr_subtype == 12)
01028     {
01029       int offset = fragP->fr_fix;
01030       fragP->fr_literal[offset] = 0xdc;
01031 
01032       fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
01033               fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
01034       fragP->fr_var = 0;
01035       fragP->fr_fix += 5;
01036     }
01037   else if (fragP->fr_subtype == 13)
01038     {
01039       fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
01040               fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
01041       fragP->fr_var = 0;
01042       fragP->fr_fix += 3;
01043     }
01044   else if (fragP->fr_subtype == 14)
01045     {
01046       /* Reverse the condition of the first branch.  */
01047       int offset = fragP->fr_fix;
01048       int opcode = fragP->fr_literal[offset + 1] & 0xff;
01049 
01050       switch (opcode)
01051        {
01052        case 0xd0:
01053          opcode = 0xd1;
01054          break;
01055        case 0xd1:
01056          opcode = 0xd0;
01057          break;
01058        case 0xd2:
01059          opcode = 0xdc;
01060          break;
01061        case 0xd3:
01062          opcode = 0xdb;
01063          break;
01064        case 0xd4:
01065          opcode = 0xda;
01066          break;
01067        case 0xd5:
01068          opcode = 0xd9;
01069          break;
01070        case 0xd6:
01071          opcode = 0xd8;
01072          break;
01073        case 0xd7:
01074          opcode = 0xdd;
01075          break;
01076        case 0xd8:
01077          opcode = 0xd6;
01078          break;
01079        case 0xd9:
01080          opcode = 0xd5;
01081          break;
01082        case 0xda:
01083          opcode = 0xd4;
01084          break;
01085        case 0xdb:
01086          opcode = 0xd3;
01087          break;
01088        case 0xdc:
01089          opcode = 0xd2;
01090          break;
01091        case 0xdd:
01092          opcode = 0xd7;
01093          break;
01094        default:
01095          abort ();
01096        }
01097       fragP->fr_literal[offset + 1] = opcode;
01098 
01099       /* Create a fixup for the reversed conditional branch.  */
01100       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
01101       fix_new (fragP, fragP->fr_fix + 2, 1,
01102               symbol_new (buf, sec, 0, fragP->fr_next),
01103               fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
01104 
01105       /* Now create the unconditional branch + fixup to the
01106         final target.  */
01107       fragP->fr_literal[offset + 3] = 0xcc;
01108       fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
01109               fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
01110       fragP->fr_var = 0;
01111       fragP->fr_fix += 6;
01112     }
01113   else if (fragP->fr_subtype == 15)
01114     {
01115       /* Reverse the condition of the first branch.  */
01116       int offset = fragP->fr_fix;
01117       int opcode = fragP->fr_literal[offset + 1] & 0xff;
01118 
01119       switch (opcode)
01120        {
01121        case 0xd0:
01122          opcode = 0xd1;
01123          break;
01124        case 0xd1:
01125          opcode = 0xd0;
01126          break;
01127        case 0xd2:
01128          opcode = 0xdc;
01129          break;
01130        case 0xd3:
01131          opcode = 0xdb;
01132          break;
01133        case 0xd4:
01134          opcode = 0xda;
01135          break;
01136        case 0xd5:
01137          opcode = 0xd9;
01138          break;
01139        case 0xd6:
01140          opcode = 0xd8;
01141          break;
01142        case 0xd7:
01143          opcode = 0xdd;
01144          break;
01145        case 0xd8:
01146          opcode = 0xd6;
01147          break;
01148        case 0xd9:
01149          opcode = 0xd5;
01150          break;
01151        case 0xda:
01152          opcode = 0xd4;
01153          break;
01154        case 0xdb:
01155          opcode = 0xd3;
01156          break;
01157        case 0xdc:
01158          opcode = 0xd2;
01159          break;
01160        case 0xdd:
01161          opcode = 0xd7;
01162          break;
01163        default:
01164          abort ();
01165        }
01166       fragP->fr_literal[offset + 1] = opcode;
01167 
01168       /* Create a fixup for the reversed conditional branch.  */
01169       sprintf (buf, ".%s_%ld", FAKE_LABEL_NAME, label_count++);
01170       fix_new (fragP, fragP->fr_fix + 2, 1,
01171               symbol_new (buf, sec, 0, fragP->fr_next),
01172               fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
01173 
01174       /* Now create the unconditional branch + fixup to the
01175         final target.  */
01176       fragP->fr_literal[offset + 3] = 0xdc;
01177       fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
01178               fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
01179       fragP->fr_var = 0;
01180       fragP->fr_fix += 8;
01181     }
01182   else
01183     abort ();
01184 }
01185 
01186 valueT
01187 md_section_align (seg, addr)
01188      asection *seg;
01189      valueT addr;
01190 {
01191   int align = bfd_get_section_alignment (stdoutput, seg);
01192   return ((addr + (1 << align) - 1) & (-1 << align));
01193 }
01194 
01195 void
01196 md_begin ()
01197 {
01198   char *prev_name = "";
01199   register const struct mn10300_opcode *op;
01200 
01201   mn10300_hash = hash_new ();
01202 
01203   /* Insert unique names into hash table.  The MN10300 instruction set
01204      has many identical opcode names that have different opcodes based
01205      on the operands.  This hash table then provides a quick index to
01206      the first opcode with a particular name in the opcode table.  */
01207 
01208   op = mn10300_opcodes;
01209   while (op->name)
01210     {
01211       if (strcmp (prev_name, op->name))
01212        {
01213          prev_name = (char *) op->name;
01214          hash_insert (mn10300_hash, op->name, (char *) op);
01215        }
01216       op++;
01217     }
01218 
01219   /* Set the default machine type.  */
01220 #ifdef TE_LINUX
01221   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, AM33_2))
01222     as_warn (_("could not set architecture and machine"));
01223 
01224   current_machine = AM33_2;
01225 #else  
01226   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, MN103))
01227     as_warn (_("could not set architecture and machine"));
01228 
01229   current_machine = MN103;
01230 #endif
01231 }
01232 
01233 static symbolS *GOT_symbol;
01234 
01235 static inline int mn10300_check_fixup PARAMS ((struct mn10300_fixup *));
01236 static inline int mn10300_PIC_related_p PARAMS ((symbolS *));
01237 
01238 static inline int
01239 mn10300_PIC_related_p (sym)
01240      symbolS *sym;
01241 {
01242   expressionS *exp;
01243 
01244   if (! sym)
01245     return 0;
01246 
01247   if (sym == GOT_symbol)
01248     return 1;
01249 
01250   exp = symbol_get_value_expression (sym);
01251 
01252   return (exp->X_op == O_PIC_reloc
01253          || mn10300_PIC_related_p (exp->X_add_symbol)
01254          || mn10300_PIC_related_p (exp->X_op_symbol));
01255 }
01256 
01257 static inline int
01258 mn10300_check_fixup (fixup)
01259      struct mn10300_fixup *fixup;
01260 {
01261   expressionS *exp = &fixup->exp;
01262 
01263  repeat:
01264   switch (exp->X_op)
01265     {
01266     case O_add:
01267     case O_subtract: /* If we're sufficiently unlucky that the label
01268                      and the expression that references it happen
01269                      to end up in different frags, the subtract
01270                      won't be simplified within expression().  */
01271       /* The PIC-related operand must be the first operand of a sum.  */
01272       if (exp != &fixup->exp || mn10300_PIC_related_p (exp->X_op_symbol))
01273        return 1;
01274 
01275       if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
01276        fixup->reloc = BFD_RELOC_32_GOT_PCREL;
01277 
01278       exp = symbol_get_value_expression (exp->X_add_symbol);
01279       goto repeat;
01280 
01281     case O_symbol:
01282       if (exp->X_add_symbol && exp->X_add_symbol == GOT_symbol)
01283        fixup->reloc = BFD_RELOC_32_GOT_PCREL;
01284       break;
01285 
01286     case O_PIC_reloc:
01287       fixup->reloc = exp->X_md;
01288       exp->X_op = O_symbol;
01289       if (fixup->reloc == BFD_RELOC_32_PLT_PCREL
01290          && fixup->opindex >= 0
01291          && (mn10300_operands[fixup->opindex].flags
01292              & MN10300_OPERAND_RELAX))
01293        return 1;
01294       break;
01295 
01296     default:
01297       return (mn10300_PIC_related_p (exp->X_add_symbol)
01298              || mn10300_PIC_related_p (exp->X_op_symbol));
01299     }
01300 
01301   return 0;
01302 }
01303 
01304 void
01305 mn10300_cons_fix_new (frag, off, size, exp)
01306      fragS *frag;
01307      int off, size;
01308      expressionS *exp;
01309 {
01310   struct mn10300_fixup fixup;
01311 
01312   fixup.opindex = -1;
01313   fixup.exp = *exp;
01314   fixup.reloc = BFD_RELOC_UNUSED;
01315 
01316   mn10300_check_fixup (&fixup);
01317 
01318   if (fixup.reloc == BFD_RELOC_MN10300_GOT32)
01319     switch (size)
01320       {
01321       case 2:
01322        fixup.reloc = BFD_RELOC_MN10300_GOT16;
01323        break;
01324 
01325       case 3:
01326        fixup.reloc = BFD_RELOC_MN10300_GOT24;
01327        break;
01328 
01329       case 4:
01330        break;
01331 
01332       default:
01333        goto error;
01334       }
01335   else if (fixup.reloc == BFD_RELOC_UNUSED)
01336     switch (size)
01337       {
01338       case 1:
01339        fixup.reloc = BFD_RELOC_8;
01340        break;
01341 
01342       case 2:
01343        fixup.reloc = BFD_RELOC_16;
01344        break;
01345 
01346       case 3:
01347        fixup.reloc = BFD_RELOC_24;
01348        break;
01349 
01350       case 4:
01351        fixup.reloc = BFD_RELOC_32;
01352        break;
01353 
01354       default:
01355        goto error;
01356       }
01357   else if (size != 4)
01358     {
01359     error:
01360       as_bad (_("unsupported BFD relocation size %u"), size);
01361       fixup.reloc = BFD_RELOC_UNUSED;
01362     }
01363     
01364   fix_new_exp (frag, off, size, &fixup.exp, 0, fixup.reloc);
01365 }
01366 
01367 void
01368 md_assemble (str)
01369      char *str;
01370 {
01371   char *s;
01372   struct mn10300_opcode *opcode;
01373   struct mn10300_opcode *next_opcode;
01374   const unsigned char *opindex_ptr;
01375   int next_opindex, relaxable;
01376   unsigned long insn, extension, size = 0;
01377   char *f;
01378   int i;
01379   int match;
01380 
01381   /* Get the opcode.  */
01382   for (s = str; *s != '\0' && !ISSPACE (*s); s++)
01383     ;
01384   if (*s != '\0')
01385     *s++ = '\0';
01386 
01387   /* Find the first opcode with the proper name.  */
01388   opcode = (struct mn10300_opcode *) hash_find (mn10300_hash, str);
01389   if (opcode == NULL)
01390     {
01391       as_bad (_("Unrecognized opcode: `%s'"), str);
01392       return;
01393     }
01394 
01395   str = s;
01396   while (ISSPACE (*str))
01397     ++str;
01398 
01399   input_line_pointer = str;
01400 
01401   for (;;)
01402     {
01403       const char *errmsg;
01404       int op_idx;
01405       char *hold;
01406       int extra_shift = 0;
01407 
01408       errmsg = _("Invalid opcode/operands");
01409 
01410       /* Reset the array of register operands.  */
01411       memset (mn10300_reg_operands, -1, sizeof (mn10300_reg_operands));
01412 
01413       relaxable = 0;
01414       fc = 0;
01415       match = 0;
01416       next_opindex = 0;
01417       insn = opcode->opcode;
01418       extension = 0;
01419 
01420       /* If the instruction is not available on the current machine
01421         then it can not possibly match.  */
01422       if (opcode->machine
01423          && !(opcode->machine == AM33_2 && HAVE_AM33_2)
01424          && !(opcode->machine == AM33 && HAVE_AM33)
01425          && !(opcode->machine == AM30 && HAVE_AM30))
01426        goto error;
01427 
01428       for (op_idx = 1, opindex_ptr = opcode->operands;
01429           *opindex_ptr != 0;
01430           opindex_ptr++, op_idx++)
01431        {
01432          const struct mn10300_operand *operand;
01433          expressionS ex;
01434 
01435          if (next_opindex == 0)
01436            {
01437              operand = &mn10300_operands[*opindex_ptr];
01438            }
01439          else
01440            {
01441              operand = &mn10300_operands[next_opindex];
01442              next_opindex = 0;
01443            }
01444 
01445          while (*str == ' ' || *str == ',')
01446            ++str;
01447 
01448          if (operand->flags & MN10300_OPERAND_RELAX)
01449            relaxable = 1;
01450 
01451          /* Gather the operand.  */
01452          hold = input_line_pointer;
01453          input_line_pointer = str;
01454 
01455          if (operand->flags & MN10300_OPERAND_PAREN)
01456            {
01457              if (*input_line_pointer != ')' && *input_line_pointer != '(')
01458               {
01459                 input_line_pointer = hold;
01460                 str = hold;
01461                 goto error;
01462               }
01463              input_line_pointer++;
01464              goto keep_going;
01465            }
01466          /* See if we can match the operands.  */
01467          else if (operand->flags & MN10300_OPERAND_DREG)
01468            {
01469              if (!data_register_name (&ex))
01470               {
01471                 input_line_pointer = hold;
01472                 str = hold;
01473                 goto error;
01474               }
01475            }
01476          else if (operand->flags & MN10300_OPERAND_AREG)
01477            {
01478              if (!address_register_name (&ex))
01479               {
01480                 input_line_pointer = hold;
01481                 str = hold;
01482                 goto error;
01483               }
01484            }
01485          else if (operand->flags & MN10300_OPERAND_SP)
01486            {
01487              char *start = input_line_pointer;
01488              char c = get_symbol_end ();
01489 
01490              if (strcasecmp (start, "sp") != 0)
01491               {
01492                 *input_line_pointer = c;
01493                 input_line_pointer = hold;
01494                 str = hold;
01495                 goto error;
01496               }
01497              *input_line_pointer = c;
01498              goto keep_going;
01499            }
01500          else if (operand->flags & MN10300_OPERAND_RREG)
01501            {
01502              if (!r_register_name (&ex))
01503               {
01504                 input_line_pointer = hold;
01505                 str = hold;
01506                 goto error;
01507               }
01508            }
01509          else if (operand->flags & MN10300_OPERAND_XRREG)
01510            {
01511              if (!xr_register_name (&ex))
01512               {
01513                 input_line_pointer = hold;
01514                 str = hold;
01515                 goto error;
01516               }
01517            }
01518          else if (operand->flags & MN10300_OPERAND_FSREG)
01519            {
01520              if (!float_register_name (&ex))
01521               {
01522                 input_line_pointer = hold;
01523                 str = hold;
01524                 goto error;
01525               }
01526            }
01527          else if (operand->flags & MN10300_OPERAND_FDREG)
01528            {
01529              if (!double_register_name (&ex))
01530               {
01531                 input_line_pointer = hold;
01532                 str = hold;
01533                 goto error;
01534               }
01535            }
01536          else if (operand->flags & MN10300_OPERAND_FPCR)
01537            {
01538              char *start = input_line_pointer;
01539              char c = get_symbol_end ();
01540 
01541              if (strcasecmp (start, "fpcr") != 0)
01542               {
01543                 *input_line_pointer = c;
01544                 input_line_pointer = hold;
01545                 str = hold;
01546                 goto error;
01547               }
01548              *input_line_pointer = c;
01549              goto keep_going;
01550            }
01551          else if (operand->flags & MN10300_OPERAND_USP)
01552            {
01553              char *start = input_line_pointer;
01554              char c = get_symbol_end ();
01555 
01556              if (strcasecmp (start, "usp") != 0)
01557               {
01558                 *input_line_pointer = c;
01559                 input_line_pointer = hold;
01560                 str = hold;
01561                 goto error;
01562               }
01563              *input_line_pointer = c;
01564              goto keep_going;
01565            }
01566          else if (operand->flags & MN10300_OPERAND_SSP)
01567            {
01568              char *start = input_line_pointer;
01569              char c = get_symbol_end ();
01570 
01571              if (strcasecmp (start, "ssp") != 0)
01572               {
01573                 *input_line_pointer = c;
01574                 input_line_pointer = hold;
01575                 str = hold;
01576                 goto error;
01577               }
01578              *input_line_pointer = c;
01579              goto keep_going;
01580            }
01581          else if (operand->flags & MN10300_OPERAND_MSP)
01582            {
01583              char *start = input_line_pointer;
01584              char c = get_symbol_end ();
01585 
01586              if (strcasecmp (start, "msp") != 0)
01587               {
01588                 *input_line_pointer = c;
01589                 input_line_pointer = hold;
01590                 str = hold;
01591                 goto error;
01592               }
01593              *input_line_pointer = c;
01594              goto keep_going;
01595            }
01596          else if (operand->flags & MN10300_OPERAND_PC)
01597            {
01598              char *start = input_line_pointer;
01599              char c = get_symbol_end ();
01600 
01601              if (strcasecmp (start, "pc") != 0)
01602               {
01603                 *input_line_pointer = c;
01604                 input_line_pointer = hold;
01605                 str = hold;
01606                 goto error;
01607               }
01608              *input_line_pointer = c;
01609              goto keep_going;
01610            }
01611          else if (operand->flags & MN10300_OPERAND_EPSW)
01612            {
01613              char *start = input_line_pointer;
01614              char c = get_symbol_end ();
01615 
01616              if (strcasecmp (start, "epsw") != 0)
01617               {
01618                 *input_line_pointer = c;
01619                 input_line_pointer = hold;
01620                 str = hold;
01621                 goto error;
01622               }
01623              *input_line_pointer = c;
01624              goto keep_going;
01625            }
01626          else if (operand->flags & MN10300_OPERAND_PLUS)
01627            {
01628              if (*input_line_pointer != '+')
01629               {
01630                 input_line_pointer = hold;
01631                 str = hold;
01632                 goto error;
01633               }
01634              input_line_pointer++;
01635              goto keep_going;
01636            }
01637          else if (operand->flags & MN10300_OPERAND_PSW)
01638            {
01639              char *start = input_line_pointer;
01640              char c = get_symbol_end ();
01641 
01642              if (strcasecmp (start, "psw") != 0)
01643               {
01644                 *input_line_pointer = c;
01645                 input_line_pointer = hold;
01646                 str = hold;
01647                 goto error;
01648               }
01649              *input_line_pointer = c;
01650              goto keep_going;
01651            }
01652          else if (operand->flags & MN10300_OPERAND_MDR)
01653            {
01654              char *start = input_line_pointer;
01655              char c = get_symbol_end ();
01656 
01657              if (strcasecmp (start, "mdr") != 0)
01658               {
01659                 *input_line_pointer = c;
01660                 input_line_pointer = hold;
01661                 str = hold;
01662                 goto error;
01663               }
01664              *input_line_pointer = c;
01665              goto keep_going;
01666            }
01667          else if (operand->flags & MN10300_OPERAND_REG_LIST)
01668            {
01669              unsigned int value = 0;
01670              if (*input_line_pointer != '[')
01671               {
01672                 input_line_pointer = hold;
01673                 str = hold;
01674                 goto error;
01675               }
01676 
01677              /* Eat the '['.  */
01678              input_line_pointer++;
01679 
01680              /* We used to reject a null register list here; however,
01681                we accept it now so the compiler can emit "call"
01682                instructions for all calls to named functions.
01683 
01684                The linker can then fill in the appropriate bits for the
01685                register list and stack size or change the instruction
01686                into a "calls" if using "call" is not profitable.  */
01687              while (*input_line_pointer != ']')
01688               {
01689                 char *start;
01690                 char c;
01691 
01692                 if (*input_line_pointer == ',')
01693                   input_line_pointer++;
01694 
01695                 start = input_line_pointer;
01696                 c = get_symbol_end ();
01697 
01698                 if (strcasecmp (start, "d2") == 0)
01699                   {
01700                     value |= 0x80;
01701                     *input_line_pointer = c;
01702                   }
01703                 else if (strcasecmp (start, "d3") == 0)
01704                   {
01705                     value |= 0x40;
01706                     *input_line_pointer = c;
01707                   }
01708                 else if (strcasecmp (start, "a2") == 0)
01709                   {
01710                     value |= 0x20;
01711                     *input_line_pointer = c;
01712                   }
01713                 else if (strcasecmp (start, "a3") == 0)
01714                   {
01715                     value |= 0x10;
01716                     *input_line_pointer = c;
01717                   }
01718                 else if (strcasecmp (start, "other") == 0)
01719                   {
01720                     value |= 0x08;
01721                     *input_line_pointer = c;
01722                   }
01723                 else if (HAVE_AM33
01724                         && strcasecmp (start, "exreg0") == 0)
01725                   {
01726                     value |= 0x04;
01727                     *input_line_pointer = c;
01728                   }
01729                 else if (HAVE_AM33
01730                         && strcasecmp (start, "exreg1") == 0)
01731                   {
01732                     value |= 0x02;
01733                     *input_line_pointer = c;
01734                   }
01735                 else if (HAVE_AM33
01736                         && strcasecmp (start, "exother") == 0)
01737                   {
01738                     value |= 0x01;
01739                     *input_line_pointer = c;
01740                   }
01741                 else if (HAVE_AM33
01742                         && strcasecmp (start, "all") == 0)
01743                   {
01744                     value |= 0xff;
01745                     *input_line_pointer = c;
01746                   }
01747                 else
01748                   {
01749                     input_line_pointer = hold;
01750                     str = hold;
01751                     goto error;
01752                   }
01753               }
01754              input_line_pointer++;
01755               mn10300_insert_operand (&insn, &extension, operand,
01756                                       value, (char *) NULL, 0, 0);
01757              goto keep_going;
01758 
01759            }
01760          else if (data_register_name (&ex))
01761            {
01762              input_line_pointer = hold;
01763              str = hold;
01764              goto error;
01765            }
01766          else if (address_register_name (&ex))
01767            {
01768              input_line_pointer = hold;
01769              str = hold;
01770              goto error;
01771            }
01772          else if (other_register_name (&ex))
01773            {
01774              input_line_pointer = hold;
01775              str = hold;
01776              goto error;
01777            }
01778          else if (HAVE_AM33 && r_register_name (&ex))
01779            {
01780              input_line_pointer = hold;
01781              str = hold;
01782              goto error;
01783            }
01784          else if (HAVE_AM33 && xr_register_name (&ex))
01785            {
01786              input_line_pointer = hold;
01787              str = hold;
01788              goto error;
01789            }
01790          else if (HAVE_AM33_2 && float_register_name (&ex))
01791            {
01792              input_line_pointer = hold;
01793              str = hold;
01794              goto error;
01795            }
01796          else if (HAVE_AM33_2 && double_register_name (&ex))
01797            {
01798              input_line_pointer = hold;
01799              str = hold;
01800              goto error;
01801            }
01802          else if (*str == ')' || *str == '(')
01803            {
01804              input_line_pointer = hold;
01805              str = hold;
01806              goto error;
01807            }
01808          else
01809            {
01810              expression (&ex);
01811            }
01812 
01813          switch (ex.X_op)
01814            {
01815            case O_illegal:
01816              errmsg = _("illegal operand");
01817              goto error;
01818            case O_absent:
01819              errmsg = _("missing operand");
01820              goto error;
01821            case O_register:
01822              {
01823               int mask;
01824 
01825               mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
01826               if (HAVE_AM33)
01827                 mask |= MN10300_OPERAND_RREG | MN10300_OPERAND_XRREG;
01828               if (HAVE_AM33_2)
01829                 mask |= MN10300_OPERAND_FSREG | MN10300_OPERAND_FDREG;
01830               if ((operand->flags & mask) == 0)
01831                 {
01832                   input_line_pointer = hold;
01833                   str = hold;
01834                   goto error;
01835                 }
01836 
01837               if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
01838                 extra_shift = 8;
01839               else if (opcode->format == FMT_D2
01840                       || opcode->format == FMT_D4
01841                       || opcode->format == FMT_S2
01842                       || opcode->format == FMT_S4
01843                       || opcode->format == FMT_S6
01844                       || opcode->format == FMT_D5)
01845                 extra_shift = 16;
01846               else if (opcode->format == FMT_D7)
01847                 extra_shift = 8;
01848               else if (opcode->format == FMT_D8 || opcode->format == FMT_D9)
01849                 extra_shift = 8;
01850               else
01851                 extra_shift = 0;
01852 
01853               mn10300_insert_operand (&insn, &extension, operand,
01854                                    ex.X_add_number, (char *) NULL,
01855                                    0, extra_shift);
01856 
01857               /* And note the register number in the register array.  */
01858               mn10300_reg_operands[op_idx - 1] = ex.X_add_number;
01859               break;
01860              }
01861 
01862            case O_constant:
01863              /* If this operand can be promoted, and it doesn't
01864                fit into the allocated bitfield for this insn,
01865                then promote it (ie this opcode does not match).  */
01866              if (operand->flags
01867                 & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
01868                 && !check_operand (insn, operand, ex.X_add_number))
01869               {
01870                 input_line_pointer = hold;
01871                 str = hold;
01872                 goto error;
01873               }
01874 
01875              mn10300_insert_operand (&insn, &extension, operand,
01876                                   ex.X_add_number, (char *) NULL,
01877                                   0, 0);
01878              break;
01879 
01880            default:
01881              /* If this operand can be promoted, then this opcode didn't
01882                match since we can't know if it needed promotion!  */
01883              if (operand->flags & MN10300_OPERAND_PROMOTE)
01884               {
01885                 input_line_pointer = hold;
01886                 str = hold;
01887                 goto error;
01888               }
01889 
01890              /* We need to generate a fixup for this expression.  */
01891              if (fc >= MAX_INSN_FIXUPS)
01892               as_fatal (_("too many fixups"));
01893              fixups[fc].exp = ex;
01894              fixups[fc].opindex = *opindex_ptr;
01895              fixups[fc].reloc = BFD_RELOC_UNUSED;
01896              if (mn10300_check_fixup (& fixups[fc]))
01897               goto error;
01898              ++fc;
01899              break;
01900            }
01901 
01902 keep_going:
01903          str = input_line_pointer;
01904          input_line_pointer = hold;
01905 
01906          while (*str == ' ' || *str == ',')
01907            ++str;
01908 
01909        }
01910 
01911       /* Make sure we used all the operands!  */
01912       if (*str != ',')
01913        match = 1;
01914 
01915       /* If this instruction has registers that must not match, verify
01916         that they do indeed not match.  */
01917       if (opcode->no_match_operands)
01918        {
01919          int i;
01920 
01921          /* Look at each operand to see if it's marked.  */
01922          for (i = 0; i < MN10300_MAX_OPERANDS; i++)
01923            {
01924              if ((1 << i) & opcode->no_match_operands)
01925               {
01926                 int j;
01927 
01928                 /* operand I is marked.  Check that it does not match any
01929                    operands > I which are marked.  */
01930                 for (j = i + 1; j < MN10300_MAX_OPERANDS; j++)
01931                   {
01932                     if (((1 << j) & opcode->no_match_operands)
01933                        && mn10300_reg_operands[i] == mn10300_reg_operands[j])
01934                      {
01935                        errmsg = _("Invalid register specification.");
01936                        match = 0;
01937                        goto error;
01938                      }
01939                   }
01940               }
01941            }
01942        }
01943 
01944     error:
01945       if (match == 0)
01946        {
01947          next_opcode = opcode + 1;
01948          if (!strcmp (next_opcode->name, opcode->name))
01949            {
01950              opcode = next_opcode;
01951              continue;
01952            }
01953 
01954          as_bad ("%s", errmsg);
01955          return;
01956        }
01957       break;
01958     }
01959 
01960   while (ISSPACE (*str))
01961     ++str;
01962 
01963   if (*str != '\0')
01964     as_bad (_("junk at end of line: `%s'"), str);
01965 
01966   input_line_pointer = str;
01967 
01968   /* Determine the size of the instruction.  */
01969   if (opcode->format == FMT_S0)
01970     size = 1;
01971 
01972   if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
01973     size = 2;
01974 
01975   if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
01976     size = 3;
01977 
01978   if (opcode->format == FMT_D6)
01979     size = 3;
01980 
01981   if (opcode->format == FMT_D7 || opcode->format == FMT_D10)
01982     size = 4;
01983 
01984   if (opcode->format == FMT_D8)
01985     size = 6;
01986 
01987   if (opcode->format == FMT_D9)
01988     size = 7;
01989 
01990   if (opcode->format == FMT_S4)
01991     size = 5;
01992 
01993   if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
01994     size = 7;
01995 
01996   if (opcode->format == FMT_D2)
01997     size = 4;
01998 
01999   if (opcode->format == FMT_D3)
02000     size = 5;
02001 
02002   if (opcode->format == FMT_D4)
02003     size = 6;
02004 
02005   if (relaxable && fc > 0)
02006     {
02007       /* On a 64-bit host the size of an 'int' is not the same
02008         as the size of a pointer, so we need a union to convert
02009         the opindex field of the fr_cgen structure into a char *
02010         so that it can be stored in the frag.  We do not have
02011         to worry about loosing accuracy as we are not going to
02012         be even close to the 32bit limit of the int.  */
02013       union
02014       {
02015        int opindex;
02016        char * ptr;
02017       }
02018       opindex_converter;
02019       int type;
02020 
02021       /* We want to anchor the line info to the previous frag (if
02022         there isn't one, create it), so that, when the insn is
02023         resized, we still get the right address for the beginning of
02024         the region.  */
02025       f = frag_more (0);
02026       dwarf2_emit_insn (0);
02027 
02028       /* bCC  */
02029       if (size == 2)
02030        {
02031          /* Handle bra specially.  Basically treat it like jmp so
02032             that we automatically handle 8, 16 and 32 bit offsets
02033             correctly as well as jumps to an undefined address.
02034 
02035             It is also important to not treat it like other bCC
02036             instructions since the long forms of bra is different
02037             from other bCC instructions.  */
02038          if (opcode->opcode == 0xca00)
02039            type = 10;
02040          else
02041            type = 0;
02042        }
02043       /* call  */
02044       else if (size == 5)
02045        type = 6;
02046       /* calls  */
02047       else if (size == 4)
02048        type = 8;
02049       /* jmp  */
02050       else if (size == 3 && opcode->opcode == 0xcc0000)
02051        type = 10;
02052       else if (size == 3 && (opcode->opcode & 0xfff000) == 0xf8d000)
02053        type = 13;
02054       /* bCC (uncommon cases)  */
02055       else
02056        type = 3;
02057 
02058       opindex_converter.opindex = fixups[0].opindex;
02059       f = frag_var (rs_machine_dependent, 8, 8 - size, type,
02060                   fixups[0].exp.X_add_symbol,
02061                   fixups[0].exp.X_add_number,
02062                   opindex_converter.ptr);
02063 
02064       /* This is pretty hokey.  We basically just care about the
02065         opcode, so we have to write out the first word big endian.
02066 
02067         The exception is "call", which has two operands that we
02068         care about.
02069 
02070         The first operand (the register list) happens to be in the
02071         first instruction word, and will be in the right place if
02072         we output the first word in big endian mode.
02073 
02074         The second operand (stack size) is in the extension word,
02075         and we want it to appear as the first character in the extension
02076         word (as it appears in memory).  Luckily, writing the extension
02077         word in big endian format will do what we want.  */
02078       number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
02079       if (size > 8)
02080        {
02081          number_to_chars_bigendian (f + 4, extension, 4);
02082          number_to_chars_bigendian (f + 8, 0, size - 8);
02083        }
02084       else if (size > 4)
02085        number_to_chars_bigendian (f + 4, extension, size - 4);
02086     }
02087   else
02088     {
02089       /* Allocate space for the instruction.  */
02090       f = frag_more (size);
02091 
02092       /* Fill in bytes for the instruction.  Note that opcode fields
02093         are written big-endian, 16 & 32bit immediates are written
02094         little endian.  Egad.  */
02095       if (opcode->format == FMT_S0
02096          || opcode->format == FMT_S1
02097          || opcode->format == FMT_D0
02098          || opcode->format == FMT_D6
02099          || opcode->format == FMT_D7
02100          || opcode->format == FMT_D10
02101          || opcode->format == FMT_D1)
02102        {
02103          number_to_chars_bigendian (f, insn, size);
02104        }
02105       else if (opcode->format == FMT_S2
02106               && opcode->opcode != 0xdf0000
02107               && opcode->opcode != 0xde0000)
02108        {
02109          /* A format S2 instruction that is _not_ "ret" and "retf".  */
02110          number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
02111          number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
02112        }
02113       else if (opcode->format == FMT_S2)
02114        {
02115          /* This must be a ret or retf, which is written entirely in
02116             big-endian format.  */
02117          number_to_chars_bigendian (f, insn, 3);
02118        }
02119       else if (opcode->format == FMT_S4
02120               && opcode->opcode != 0xdc000000)
02121        {
02122          /* This must be a format S4 "call" instruction.  What a pain.  */
02123          unsigned long temp = (insn >> 8) & 0xffff;
02124          number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
02125          number_to_chars_littleendian (f + 1, temp, 2);
02126          number_to_chars_bigendian (f + 3, insn & 0xff, 1);
02127          number_to_chars_bigendian (f + 4, extension & 0xff, 1);
02128        }
02129       else if (opcode->format == FMT_S4)
02130        {
02131          /* This must be a format S4 "jmp" instruction.  */
02132          unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
02133          number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
02134          number_to_chars_littleendian (f + 1, temp, 4);
02135        }
02136       else if (opcode->format == FMT_S6)
02137        {
02138          unsigned long temp = ((insn & 0xffffff) << 8)
02139            | ((extension >> 16) & 0xff);
02140          number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
02141          number_to_chars_littleendian (f + 1, temp, 4);
02142          number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
02143          number_to_chars_bigendian (f + 6, extension & 0xff, 1);
02144        }
02145       else if (opcode->format == FMT_D2
02146               && opcode->opcode != 0xfaf80000
02147               && opcode->opcode != 0xfaf00000
02148               && opcode->opcode != 0xfaf40000)
02149        {
02150          /* A format D2 instruction where the 16bit immediate is
02151             really a single 16bit value, not two 8bit values.  */
02152          number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
02153          number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
02154        }
02155       else if (opcode->format == FMT_D2)
02156        {
02157          /* A format D2 instruction where the 16bit immediate
02158             is really two 8bit immediates.  */
02159          number_to_chars_bigendian (f, insn, 4);
02160        }
02161       else if (opcode->format == FMT_D3)
02162        {
02163          number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
02164          number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
02165          number_to_chars_bigendian (f + 4, extension & 0xff, 1);
02166        }
02167       else if (opcode->format == FMT_D4)
02168        {
02169          unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
02170 
02171          number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
02172          number_to_chars_littleendian (f + 2, temp, 4);
02173        }
02174       else if (opcode->format == FMT_D5)
02175        {
02176          unsigned long temp = (((insn & 0xffff) << 16)
02177                             | ((extension >> 8) & 0xffff));
02178 
02179          number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
02180          number_to_chars_littleendian (f + 2, temp, 4);
02181          number_to_chars_bigendian (f + 6, extension & 0xff, 1);
02182        }
02183       else if (opcode->format == FMT_D8)
02184        {
02185          unsigned long temp = ((insn & 0xff) << 16) | (extension & 0xffff);
02186 
02187          number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
02188          number_to_chars_bigendian (f + 3, (temp & 0xff), 1);
02189          number_to_chars_littleendian (f + 4, temp >> 8, 2);
02190        }
02191       else if (opcode->format == FMT_D9)
02192        {
02193          unsigned long temp = ((insn & 0xff) << 24) | (extension & 0xffffff);
02194 
02195          number_to_chars_bigendian (f, (insn >> 8) & 0xffffff, 3);
02196          number_to_chars_littleendian (f + 3, temp, 4);
02197        }
02198 
02199       /* Create any fixups.  */
02200       for (i = 0; i < fc; i++)
02201        {
02202          const struct mn10300_operand *operand;
02203 
02204          operand = &mn10300_operands[fixups[i].opindex];
02205          if (fixups[i].reloc != BFD_RELOC_UNUSED
02206              && fixups[i].reloc != BFD_RELOC_32_GOT_PCREL
02207              && fixups[i].reloc != BFD_RELOC_32_GOTOFF
02208              && fixups[i].reloc != BFD_RELOC_32_PLT_PCREL
02209              && fixups[i].reloc != BFD_RELOC_MN10300_GOT32)
02210            {
02211              reloc_howto_type *reloc_howto;
02212              int size;
02213              int offset;
02214              fixS *fixP;
02215 
02216              reloc_howto = bfd_reloc_type_lookup (stdoutput,
02217                                              fixups[i].reloc);
02218 
02219              if (!reloc_howto)
02220               abort ();
02221 
02222              size = bfd_get_reloc_size (reloc_howto);
02223 
02224              if (size < 1 || size > 4)
02225               abort ();
02226 
02227              offset = 4 - size;
02228              fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
02229                               size, &fixups[i].exp,
02230                               reloc_howto->pc_relative,
02231                               fixups[i].reloc);
02232            }
02233          else
02234            {
02235              int reloc, pcrel, reloc_size, offset;
02236              fixS *fixP;
02237 
02238              reloc = BFD_RELOC_NONE;
02239              if (fixups[i].reloc != BFD_RELOC_UNUSED)
02240               reloc = fixups[i].reloc;
02241              /* How big is the reloc?  Remember SPLIT relocs are
02242                implicitly 32bits.  */
02243              if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
02244               reloc_size = 32;
02245              else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
02246               reloc_size = 24;
02247              else
02248               reloc_size = operand->bits;
02249 
02250              /* Is the reloc pc-relative?  */
02251              pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
02252              if (reloc != BFD_RELOC_NONE)
02253               pcrel = bfd_reloc_type_lookup (stdoutput, reloc)->pc_relative;
02254 
02255              offset = size - (reloc_size + operand->shift) / 8;
02256 
02257              /* Choose a proper BFD relocation type.  */
02258              if (reloc != BFD_RELOC_NONE)
02259               ;
02260              else if (pcrel)
02261               {
02262                 if (reloc_size == 32)
02263                   reloc = BFD_RELOC_32_PCREL;
02264                 else if (reloc_size == 16)
02265                   reloc = BFD_RELOC_16_PCREL;
02266                 else if (reloc_size == 8)
02267                   reloc = BFD_RELOC_8_PCREL;
02268                 else
02269                   abort ();
02270               }
02271              else
02272               {
02273                 if (reloc_size == 32)
02274                   reloc = BFD_RELOC_32;
02275                 else if (reloc_size == 16)
02276                   reloc = BFD_RELOC_16;
02277                 else if (reloc_size == 8)
02278                   reloc = BFD_RELOC_8;
02279                 else
02280                   abort ();
02281               }
02282 
02283              fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
02284                               reloc_size / 8, &fixups[i].exp, pcrel,
02285                               ((bfd_reloc_code_real_type) reloc));
02286 
02287              if (pcrel)
02288               fixP->fx_offset += offset;
02289            }
02290        }
02291 
02292       dwarf2_emit_insn (size);
02293     }
02294 }
02295 
02296 /* If while processing a fixup, a reloc really needs to be created
02297    then it is done here.  */
02298 
02299 arelent *
02300 tc_gen_reloc (seg, fixp)
02301      asection *seg ATTRIBUTE_UNUSED;
02302      fixS *fixp;
02303 {
02304   arelent *reloc;
02305   reloc = (arelent *) xmalloc (sizeof (arelent));
02306 
02307   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
02308   if (reloc->howto == (reloc_howto_type *) NULL)
02309     {
02310       as_bad_where (fixp->fx_file, fixp->fx_line,
02311                   _("reloc %d not supported by object file format"),
02312                   (int) fixp->fx_r_type);
02313       return NULL;
02314     }
02315   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
02316 
02317   if (fixp->fx_subsy
02318       && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
02319     {
02320       fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy);
02321       fixp->fx_subsy = 0;
02322     }
02323 
02324   if (fixp->fx_addsy && fixp->fx_subsy)
02325     {
02326       reloc->sym_ptr_ptr = NULL;
02327 
02328       /* If we got a difference between two symbols, and the
02329         subtracted symbol is in the current section, use a
02330         PC-relative relocation.  If both symbols are in the same
02331         section, the difference would have already been simplified
02332         to a constant.  */
02333       if (S_GET_SEGMENT (fixp->fx_subsy) == seg)
02334        {
02335          reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
02336          *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
02337          reloc->addend = (reloc->address - S_GET_VALUE (fixp->fx_subsy)
02338                         + fixp->fx_offset);
02339 
02340          switch (fixp->fx_r_type)
02341            {
02342            case BFD_RELOC_8:
02343              reloc->howto = bfd_reloc_type_lookup (stdoutput,
02344                                               BFD_RELOC_8_PCREL);
02345              return reloc;
02346 
02347            case BFD_RELOC_16:
02348              reloc->howto = bfd_reloc_type_lookup (stdoutput,
02349                                               BFD_RELOC_16_PCREL);
02350              return reloc;
02351 
02352            case BFD_RELOC_24:
02353              reloc->howto = bfd_reloc_type_lookup (stdoutput,
02354                                               BFD_RELOC_24_PCREL);
02355              return reloc;
02356 
02357            case BFD_RELOC_32:
02358              reloc->howto = bfd_reloc_type_lookup (stdoutput,
02359                                               BFD_RELOC_32_PCREL);
02360              return reloc;
02361 
02362            default:
02363              /* Try to compute the absolute value below.  */
02364              break;
02365            }
02366        }
02367 
02368       if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
02369          || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
02370        {
02371          as_bad_where (fixp->fx_file, fixp->fx_line,
02372                      "Difference of symbols in different sections is not supported");
02373        }
02374       else
02375        {
02376          char *fixpos = fixp->fx_where + fixp->fx_frag->fr_literal;
02377 
02378          reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
02379                         - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
02380 
02381          switch (fixp->fx_r_type)
02382            {
02383            case BFD_RELOC_8:
02384              md_number_to_chars (fixpos, reloc->addend, 1);
02385              break;
02386 
02387            case BFD_RELOC_16:
02388              md_number_to_chars (fixpos, reloc->addend, 2);
02389              break;
02390 
02391            case BFD_RELOC_24:
02392              md_number_to_chars (fixpos, reloc->addend, 3);
02393              break;
02394 
02395            case BFD_RELOC_32:
02396              md_number_to_chars (fixpos, reloc->addend, 4);
02397              break;
02398 
02399            default:
02400              reloc->sym_ptr_ptr
02401               = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
02402              return reloc;
02403            }
02404        }
02405 
02406       if (reloc->sym_ptr_ptr)
02407        free (reloc->sym_ptr_ptr);
02408       free (reloc);
02409       return NULL;
02410     }
02411   else
02412     {
02413       reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
02414       *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
02415       reloc->addend = fixp->fx_offset;
02416     }
02417   return reloc;
02418 }
02419 
02420 int
02421 md_estimate_size_before_relax (fragp, seg)
02422      fragS *fragp;
02423      asection *seg;
02424 {
02425   if (fragp->fr_subtype == 6
02426       && (!S_IS_DEFINED (fragp->fr_symbol)
02427          || seg != S_GET_SEGMENT (fragp->fr_symbol)))
02428     fragp->fr_subtype = 7;
02429   else if (fragp->fr_subtype == 8
02430           && (!S_IS_DEFINED (fragp->fr_symbol)
02431               || seg != S_GET_SEGMENT (fragp->fr_symbol)))
02432     fragp->fr_subtype = 9;
02433   else if (fragp->fr_subtype == 10
02434           &&  (!S_IS_DEFINED (fragp->fr_symbol)
02435               || seg != S_GET_SEGMENT (fragp->fr_symbol)))
02436     fragp->fr_subtype = 12;
02437 
02438   if (fragp->fr_subtype == 13)
02439     return 3;
02440   if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
02441     abort ();
02442 
02443   return md_relax_table[fragp->fr_subtype].rlx_length;
02444 }
02445 
02446 long
02447 md_pcrel_from (fixp)
02448      fixS *fixp;
02449 {
02450   if (fixp->fx_addsy != (symbolS *) NULL && !S_IS_DEFINED (fixp->fx_addsy))
02451     {
02452       /* The symbol is undefined.  Let the linker figure it out.  */
02453       return 0;
02454     }
02455   return fixp->fx_frag->fr_address + fixp->fx_where;
02456 }
02457 
02458 void
02459 md_apply_fix (fixP, valP, seg)
02460      fixS * fixP;
02461      valueT * valP;
02462      segT seg;
02463 {
02464   char * fixpos = fixP->fx_where + fixP->fx_frag->fr_literal;
02465   int size = 0;
02466   int value = (int) * valP;
02467 
02468   assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
02469 
02470   /* This should never happen.  */
02471   if (seg->flags & SEC_ALLOC)
02472     abort ();
02473 
02474   /* The value we are passed in *valuep includes the symbol values.
02475      If we are doing this relocation the code in write.c is going to
02476      call bfd_install_relocation, which is also going to use the symbol
02477      value.  That means that if the reloc is fully resolved we want to
02478      use *valuep since bfd_install_relocation is not being used.
02479 
02480      However, if the reloc is not fully resolved we do not want to use
02481      *valuep, and must use fx_offset instead.  However, if the reloc
02482      is PC relative, we do want to use *valuep since it includes the
02483      result of md_pcrel_from.  */
02484   if (fixP->fx_addsy != (symbolS *) NULL && ! fixP->fx_pcrel)
02485     value = fixP->fx_offset;
02486 
02487   /* If the fix is relative to a symbol which is not defined, or not
02488      in the same segment as the fix, we cannot resolve it here.  */
02489   if (fixP->fx_addsy != NULL
02490       && (! S_IS_DEFINED (fixP->fx_addsy)
02491          || (S_GET_SEGMENT (fixP->fx_addsy) != seg)))
02492     {
02493       fixP->fx_done = 0;
02494       return;
02495     }
02496 
02497   switch (fixP->fx_r_type)
02498     {
02499     case BFD_RELOC_8:
02500     case BFD_RELOC_8_PCREL:
02501       size = 1;
02502       break;
02503 
02504     case BFD_RELOC_16:
02505     case BFD_RELOC_16_PCREL:
02506       size = 2;
02507       break;
02508 
02509     case BFD_RELOC_32:
02510     case BFD_RELOC_32_PCREL:
02511       size = 4;
02512       break;
02513 
02514     case BFD_RELOC_VTABLE_INHERIT:
02515     case BFD_RELOC_VTABLE_ENTRY:
02516       fixP->fx_done = 0;
02517       return;
02518 
02519     case BFD_RELOC_NONE:
02520     default:
02521       as_bad_where (fixP->fx_file, fixP->fx_line,
02522                    _("Bad relocation fixup type (%d)"), fixP->fx_r_type);
02523     }
02524 
02525   md_number_to_chars (fixpos, value, size);
02526 
02527   /* If a symbol remains, pass the fixup, as a reloc, onto the linker.  */
02528   if (fixP->fx_addsy == NULL)
02529     fixP->fx_done = 1;
02530 }
02531 
02532 /* Return zero if the fixup in fixp should be left alone and not
02533    adjusted.  */
02534 
02535 bfd_boolean
02536 mn10300_fix_adjustable (fixp)
02537      struct fix *fixp;
02538 {
02539   if (TC_FORCE_RELOCATION_LOCAL (fixp))
02540     return 0;
02541 
02542   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
02543       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
02544     return 0;
02545 
02546   /* Do not adjust relocations involving symbols in code sections,
02547      because it breaks linker relaxations.  This could be fixed in the
02548      linker, but this fix is simpler, and it pretty much only affects
02549      object size a little bit.  */
02550   if (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_CODE)
02551     return 0;
02552 
02553   /* Likewise, do not adjust symbols that won't be merged, or debug
02554      symbols, because they too break relaxation.  We do want to adjust
02555      other mergable symbols, like .rodata, because code relaxations
02556      need section-relative symbols to properly relax them.  */
02557   if (! (S_GET_SEGMENT(fixp->fx_addsy)->flags & SEC_MERGE))
02558     return 0;
02559   if (strncmp (S_GET_SEGMENT (fixp->fx_addsy)->name, ".debug", 6) == 0)
02560     return 0;
02561 
02562   return 1;
02563 }
02564 
02565 /* Insert an operand value into an instruction.  */
02566 
02567 static void
02568 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
02569      unsigned long *insnp;
02570      unsigned long *extensionp;
02571      const struct mn10300_operand *operand;
02572      offsetT val;
02573      char *file;
02574      unsigned int line;
02575      unsigned int shift;
02576 {
02577   /* No need to check 32bit operands for a bit.  Note that
02578      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
02579   if (operand->bits != 32
02580       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
02581     {
02582       long min, max;
02583       offsetT test;
02584       int bits;
02585 
02586       bits = operand->bits;
02587       if (operand->flags & MN10300_OPERAND_24BIT)
02588        bits = 24;
02589 
02590       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
02591        {
02592          max = (1 << (bits - 1)) - 1;
02593          min = - (1 << (bits - 1));
02594        }
02595       else
02596        {
02597          max = (1 << bits) - 1;
02598          min = 0;
02599        }
02600 
02601       test = val;
02602 
02603       if (test < (offsetT) min || test > (offsetT) max)
02604        as_warn_value_out_of_range (_("operand"), test, (offsetT) min, (offsetT) max, file, line);
02605     }
02606 
02607   if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
02608     {
02609       *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
02610       *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
02611                     << operand->shift);
02612     }
02613   else if ((operand->flags & MN10300_OPERAND_24BIT) != 0)
02614     {
02615       *insnp |= (val >> (24 - operand->bits)) & ((1 << operand->bits) - 1);
02616       *extensionp |= ((val & ((1 << (24 - operand->bits)) - 1))
02617                     << operand->shift);
02618     }
02619   else if ((operand->flags & (MN10300_OPERAND_FSREG | MN10300_OPERAND_FDREG)))
02620     {
02621       /* See devo/opcodes/m10300-opc.c just before #define FSM0 for an
02622          explanation of these variables.  Note that FMT-implied shifts
02623         are not taken into account for FP registers.  */
02624       unsigned long mask_low, mask_high;
02625       int shl_low, shr_high, shl_high;
02626 
02627       switch (operand->bits)
02628        {
02629        case 5:
02630          /* Handle regular FP registers.  */
02631          if (operand->shift >= 0)
02632            {
02633              /* This is an `m' register.  */
02634              shl_low = operand->shift;
02635              shl_high = 8 + (8 & shl_low) + (shl_low & 4) / 4;
02636            }
02637          else
02638            {
02639              /* This is an `n' register.  */
02640              shl_low = -operand->shift;
02641              shl_high = shl_low / 4;
02642            }
02643 
02644          mask_low = 0x0f;
02645          mask_high = 0x10;
02646          shr_high = 4;
02647          break;
02648 
02649        case 3:
02650          /* Handle accumulators.  */
02651          shl_low = -operand->shift;
02652          shl_high = 0;
02653          mask_low = 0x03;
02654          mask_high = 0x04;
02655          shr_high = 2;
02656          break;
02657 
02658        default:
02659          abort ();
02660        }
02661       *insnp |= ((((val & mask_high) >> shr_high) << shl_high)
02662                | ((val & mask_low) << shl_low));
02663     }
02664   else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
02665     {
02666       *insnp |= (((long) val & ((1 << operand->bits) - 1))
02667                << (operand->shift + shift));
02668 
02669       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
02670        *insnp |= (((long) val & ((1 << operand->bits) - 1))
02671                  << (operand->shift + shift + operand->bits));
02672     }
02673   else
02674     {
02675       *extensionp |= (((long) val & ((1 << operand->bits) - 1))
02676                     << (operand->shift + shift));
02677 
02678       if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
02679        *extensionp |= (((long) val & ((1 << operand->bits) - 1))
02680                      << (operand->shift + shift + operand->bits));
02681     }
02682 }
02683 
02684 static unsigned long
02685 check_operand (insn, operand, val)
02686      unsigned long insn ATTRIBUTE_UNUSED;
02687      const struct mn10300_operand *operand;
02688      offsetT val;
02689 {
02690   /* No need to check 32bit operands for a bit.  Note that
02691      MN10300_OPERAND_SPLIT is an implicit 32bit operand.  */
02692   if (operand->bits != 32
02693       && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
02694     {
02695       long min, max;
02696       offsetT test;
02697       int bits;
02698 
02699       bits = operand->bits;
02700       if (operand->flags & MN10300_OPERAND_24BIT)
02701        bits = 24;
02702 
02703       if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
02704        {
02705          max = (1 << (bits - 1)) - 1;
02706          min = - (1 << (bits - 1));
02707        }
02708       else
02709        {
02710          max = (1 << bits) - 1;
02711          min = 0;
02712        }
02713 
02714       test = val;
02715 
02716       if (test < (offsetT) min || test > (offsetT) max)
02717        return 0;
02718       else
02719        return 1;
02720     }
02721   return 1;
02722 }
02723 
02724 static void
02725 set_arch_mach (mach)
02726      int mach;
02727 {
02728   if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, mach))
02729     as_warn (_("could not set architecture and machine"));
02730 
02731   current_machine = mach;
02732 }
02733 
02734 static inline char * mn10300_end_of_match PARAMS ((char *, char *));
02735 
02736 static inline char *
02737 mn10300_end_of_match (cont, what)
02738      char *cont, *what;
02739 {
02740   int len = strlen (what);
02741 
02742   if (strncmp (cont, what, strlen (what)) == 0
02743       && ! is_part_of_name (cont[len]))
02744     return cont + len;
02745 
02746   return NULL;
02747 }  
02748 
02749 int
02750 mn10300_parse_name (name, exprP, mode, nextcharP)
02751      char const *name;
02752      expressionS *exprP;
02753      enum expr_mode mode;
02754      char *nextcharP;
02755 {
02756   char *next = input_line_pointer;
02757   char *next_end;
02758   int reloc_type;
02759   segT segment;
02760 
02761   exprP->X_op_symbol = NULL;
02762 
02763   if (strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
02764     {
02765       if (! GOT_symbol)
02766        GOT_symbol = symbol_find_or_make (name);
02767 
02768       exprP->X_add_symbol = GOT_symbol;
02769     no_suffix:
02770       /* If we have an absolute symbol or a reg,
02771         then we know its value now.  */
02772       segment = S_GET_SEGMENT (exprP->X_add_symbol);
02773       if (mode != expr_defer && segment == absolute_section)
02774        {
02775          exprP->X_op = O_constant;
02776          exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
02777          exprP->X_add_symbol = NULL;
02778        }
02779       else if (mode != expr_defer && segment == reg_section)
02780        {
02781          exprP->X_op = O_register;
02782          exprP->X_add_number = S_GET_VALUE (exprP->X_add_symbol);
02783          exprP->X_add_symbol = NULL;
02784        }
02785       else
02786        {
02787          exprP->X_op = O_symbol;
02788          exprP->X_add_number = 0;
02789        }
02790 
02791       return 1;
02792     }
02793 
02794   exprP->X_add_symbol = symbol_find_or_make (name);
02795   
02796   if (*nextcharP != '@')
02797     goto no_suffix;
02798   else if ((next_end = mn10300_end_of_match (next + 1, "GOTOFF")))
02799     reloc_type = BFD_RELOC_32_GOTOFF;
02800   else if ((next_end = mn10300_end_of_match (next + 1, "GOT")))
02801     reloc_type = BFD_RELOC_MN10300_GOT32;
02802   else if ((next_end = mn10300_end_of_match (next + 1, "PLT")))
02803     reloc_type = BFD_RELOC_32_PLT_PCREL;
02804   else
02805     goto no_suffix;
02806 
02807   *input_line_pointer = *nextcharP;
02808   input_line_pointer = next_end;
02809   *nextcharP = *input_line_pointer;
02810   *input_line_pointer = '\0';
02811 
02812   exprP->X_op = O_PIC_reloc;
02813   exprP->X_add_number = 0;
02814   exprP->X_md = reloc_type;
02815 
02816   return 1;
02817 }